ruby-****@sourc*****
ruby-****@sourc*****
2012年 10月 31日 (水) 02:18:00 JST
------------------------- REMOTE_ADDR = 74.14.158.59 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-mnstbs-mnui ------------------------- @@ -197,11 +197,53 @@ {{br}} Let's now turn attention to signal handling and callbacks related to menu and sub-menu items. + + + + {{br}} === Signal Handling Mechanisms And Callbacks With Sub-menus +The workhorse in the first part of our 'menuitems-n-submenus*.rb' project was the 'mk_submenu' method. But if we think about implementing most likely different signal handler callbacks for each menu and sub-menu item, we have a bit of a problem with our 'mk_submenu' method, which creates numerous menu items without providing us with the references to them. As we can see, in the following code segment, variable 'menuitem' is local to the block and repeatedly reused for different menu items. + + submenu = Gtk::Menu.new + menuitems.each do |mi| + . . . + if mi.is_a? String + menuitem = Gtk::RadioMenuItem.new(mi) + submenu.append(menuitem) + end + . . . + end + + +There are a number of ways to fix this shortcoming. It seems, that it would be best if we could along with the menu item text, supply a callback procedure, so we could augment the above code with something like the following: + + menuitems_hash.each do |mi, callback| + . . . + menuitem.signal_connect('activate') { |w| callback[mi, w] } + . . . + end + +That would require us to separately create the procedures for each menu item, before assigning them to a hash variable, that would then be passed to our 'mk_submenu' method: + + top_mitem_x.submenu = mk_submenu(hash_of_top_cntxt_menu_n_calbacks, true) + +This seems like a reasonable plan however, for a deeply nested menu hierarchies and many menu items, this will require some planning, and more importantly, we will need to come up with a sensible naming convention for both menu items, and their respective callback procedures. Since this process in our GUI development will most likely be repeated over and over again, it may be a very good idea to, when developing our coding strategy, to perhaps start aiming to develop a 'Ruby Gtk Menu Building Design Pattern'. + + + + + + + + + + + +{{br}} {{br}} You have already seen two kinds of menu items at work, namely the straight Gtk::MenuItem, and Gtk::RadioMenuItem. But there are more of these. Following is a short summary: