ruby-****@sourc*****
ruby-****@sourc*****
2012年 10月 30日 (火) 22:37:00 JST
------------------------- REMOTE_ADDR = 74.14.158.59 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-mnstbs-mnui ------------------------- @@ -10,33 +10,34 @@ Submenus in GTK+ are not created by separate type of menu item widget but by calling Gtk::MenuItem#submenu= method. This method calls Gtk::Menu#attach_to_widget to attach the submenu to the menu item and places an arrow beside the menu item to show that it now has a submenu. If the menu item already has a submenu, it will be replaced with the given Gtk::Menu widget. -{{image_right("mechanics-of-building-submenus.png")}} +:The Mechanics Of Building Submenu Hierachies -For instance if we wish to create a menu (see the image here on the right) with two items, of which the second item will have a submenu which in turn will have three final or leaf menu items, the following is the code, that shows the mechanics of creating such a menu hierarchy: + Since we already know how to create context menus, in this sub-section we intend to use our program from section [9.1], there called ((<Pop-up Menus|tut-gtk2-mnstbs-popup>)). -{{br}} + {{image_right("mechanics-of-building-submenus.png")}} -## See: menuitems-n-submenus-mechanics.rb - - menu = Gtk::Menu.new - top_mitem1 = Gtk::MenuItem.new("Top Item 1 (final)") - top_mitem2 = Gtk::MenuItem.new("Top Item 2 (submenu)") - menu.append(top_mitem1) - menu.append(top_mitem2) - - submenu = Gtk::Menu.new - ['Sub Item 1 (final)', 'Sub Item 2 (final)', 'Sub Item 3 (final)'].each do |mi| - menuitem = Gtk::MenuItem.new(mi) - submenu.append(menuitem) - menuitem.show - end - submenu.show + For instance if we wish to create a menu (see the image here on the right) with two items, of which the second item will have a submenu which in turn will have three final or leaf menu items, the following is the code, that shows the mechanics of creating such a menu hierarchy: - top_mitem2.submenu = submenu - menu.show_all + {{br}} + menu = Gtk::Menu.new + top_mitem1 = Gtk::MenuItem.new("Top Item 1 (final)") + top_mitem2 = Gtk::MenuItem.new("Top Item 2 (submenu)") + menu.append(top_mitem1) + menu.append(top_mitem2) + + submenu = Gtk::Menu.new + ['Sub Item 1 (final)', 'Sub Item 2 (final)', 'Sub Item 3 (final)'].each do |mi| + menuitem = Gtk::MenuItem.new(mi) + submenu.append(menuitem) + menuitem.show + end + submenu.show + + top_mitem2.submenu = submenu + menu.show_all -As you can see this can become rather involved, for deeper menu hierarchies. The main point to remember here is that, there are two kinds of menu items, namely, final menu items, and sub-menu menu items, which must all be defined as Gtk::MenuItem objects in their respective menus (Gtk::Menu objects), however, the sub-menu items themselves are again defined as Gtk::Menu objects. + As you can see this can become rather involved, for deeper menu hierarchies. The main point to remember here is that, there are two kinds of menu items, namely, final menu items, and sub-menu menu items, which must all be defined as Gtk::MenuItem objects in their respective menus (Gtk::Menu objects), however, the sub-menu items themselves are again defined as Gtk::Menu objects. For the completeness let me include the complete program example, for what we discussed above: