Saturday, February 19, 2011

gnocl::text contextualized popups

The GtkText and GtkEntry widgets have their own popup menus which are activated by the press of B3 (right mouse button). It is possible to create and display popup menus for other widgets but existing methods didn't allow for the direct augmentation of these widget specific menus. Today I completed the support for this functionality which I know is something that I'll use a lot. The code works but still might undergo some revision. At the moment menu items are only appended to the bottom of the list, but these could be prepended too. I'll look at this some other time. Here's the code snippets from my test application.

$trans(sl) [gnocl:text]
$trans(sl) configure -onPopulatePopup { popups %w }


#---------------
# -onPopulatePopup callback proceedure
#---------------
proc popups {w} {
   # add some items to the text widget popup menu
   # 1) create some menu items
  set m1 [gnocl::menuItem -icon "%#Open" -text "%_Use _Bird" -onClicked {puts "use bird" ; gnocl::fileChooserDialog}]
   set m2 [gnocl::menuItem -text "%_Use _Dog" -onClicked {puts "use dog"}]

   # 2) add them to the widget popup
   $w popup separator
   $w popup item $m1
   $w popup item $m2

   # 3) create a new menu
   set menu [gnocl::menu -title "Gnocl"]
   $menu add [gnocl::menuItem -text "%#Save" -onClicked {puts "save"}]
   $menu add [gnocl::menuSeparator]
   $menu add [gnocl::menuItem -text "%#Quit" -onClicked exit]

  # 4) attach this to a pre-existing menu item
  $w popup subMenu $m2 $menu
}

No comments: