Jump to content
Sign in to follow this  

Right-click context menu : could be customized?

Recommended Posts


Widget pathname of that menu is 



But from v14, it doesn't come in a static way (depends on what you click on) so if you add a menu item then it will be cleared before pop up.


To hook an item just before the menu pops up, thanks to Tk mechanism providing <<MenuSelect>> event. This is a way I used in menuexportdeck script:


proc ::nexpd::p_Initialize {} {
	set BindScript [bind Menu <<MenuSelect>>]
	if {![string match *::nexpd::p_HookMenu* $BindScript]} {
		bind Menu <<MenuSelect>> {+after idle [list ::nexpd::p_HookMenu %W]}
proc ::nexpd::p_HookMenu {menuName} {
	if {![winfo exists $menuName]} return
	if {[string equal Menu [winfo class $menuName]]} {
		if {[string match .model*.frmTreeArea.frmTree.* $menuName]} {
			set menuParent [winfo parent $menuName]
			while {![string equal TreeCtrl [winfo class $menuParent]]} {
				set menuName $menuParent
				set menuParent [winfo parent $menuName]
				if {![string equal Menu [winfo class $menuName]]} return
			set Hooked 0
			set Count [$menuName index end]
			for {set i 0} {$i<=$Count} {incr i} {
				if {[lsearch -exact {cascade command} [$menuName type $i]]!=-1&&[string equal "Export Deck" [$menuName entrycget $i -label]]} {
					set Hooked 1
			set Selection [$menuParent selection get]
			if {[llength $Selection]} {
				if {!$Hooked} {
					if {![winfo exists $menuName.exportdeck]} {
						menu $menuName.exportdeck -tearoff 0 -activebackground [$menuName cget -activebackground] \
							-activeforeground [$menuName cget -activeforeground] \
							-background [$menuName cget -background] \
							-foreground [$menuName cget -foreground]
						$menuName.exportdeck add command -label "Only Selected" -command [list ::nexpd::p_ExportSelection $menuParent $Selection 0]
						$menuName.exportdeck add command -label "With References" -command [list ::nexpd::p_ExportSelection $menuParent $Selection 7]
						$menuName.exportdeck add command -label "Save Geometry" -command [list ::nexpd::p_ExportSelection $menuParent $Selection -1]
					$menuName insert 0 cascade -label "Export Deck" -menu $menuName.exportdeck
				} else {
					if {[string equal disabled [$menuName entrycget "Export Deck" -label]]} {
						$menuName entryconfigure "Export Deck" -state normal
			} else {
				if {$Hooked} {
					if {![string equal disabled [$menuName entrycget "Export Deck" -label]]} {
						$menuName entryconfigure "Export Deck" -state disabled




Q.Nguyen-Dai and Livil Lyle like this

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

You are posting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...