ruby-****@sourc*****
ruby-****@sourc*****
2012年 10月 30日 (火) 04:07:32 JST
------------------------- REMOTE_ADDR = 74.14.158.59 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-mnstbs-mnui ------------------------- @@ -9,7 +9,82 @@ 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. -Submenus are useful if you have a list of very specific items that would clutter an otherwise organized menu structure. When using a submenu, you can use the ((*active-item*)) signal provided by the Gtk::MenuItem widget, which will be emitted when the menu item displays its submenu. +Submenus are useful if you have a list of very specific items that would clutter an otherwise organized menu structure. When using a submenu, you can use the((*active-item*))signal provided by the Gtk::MenuItem widget, which will be emitted when the menu item displays its submenu. + +{{image_center("mitems-n-submenus.png")}} + +{{br}} +{{image_right("submenus-w-tearoff.png")}} + +{{br}} +{{*menuitems-n-submenus-1.rb*}} + + #!/usr/bin/env ruby + require 'gtk2' + + def mk_submenu(*menuitems, tearoff) + submenu = Gtk::Menu.new + if tearoff + menuitem = Gtk::TearoffMenuItem.new + submenu.append(menuitem) + menuitem.show + end + menuitems.each do |mi| + if mi.is_a? String + menuitem = Gtk::RadioMenuItem.new(mi) + submenu.append(menuitem) + elsif mi.is_a? Gtk::Menu + puts "creating sub-sub menu: [#{mi.name}]" + menuitem = Gtk::RadioMenuItem.new(mi.name) + submenu.append(menuitem) + menuitem.submenu = mi + else + puts "an unexpected submenu type: #{mi.class}" + end + menuitem.show + end + submenu.show + return submenu + end + + menu = Gtk::Menu.new + + menutearoff = Gtk::TearoffMenuItem.new + menu.append(menutearoff) + + menu.append(mitem1 = Gtk::MenuItem.new("Test1")) + menu.append(mitem2 = Gtk::MenuItem.new("Test2")) + + sub_submenu1 = mk_submenu("sub-sub1:item-1", "sub-sub1:item-2", true) + sub_submenu1.name = "sub-sub menu1" + mitem2.submenu = mk_submenu(sub_submenu1, "t1-test1", "t2-test2", true) + menu.show_all + + mitem1.signal_connect('activate') { |w, e| puts "#{w.class} - Test1" } + + window = Gtk::Window.new("Context Menu w/submenus (1)") + + # Make window sensitive to Right-mouse-click, to open the pop-up menu. + window.add_events(Gdk::Event::BUTTON_PRESS_MASK) + window.signal_connect("button_press_event") do |widget, event| + menu.popup(nil, nil, event.button, event.time) if (event.button == 3) + end + + # Make window sensitive to <Shift+F10> accelerator keys. These + # accelerator keys generate the 'popup-menu' signal for window, + # which opens the popup-menu. + window.signal_connect("popup_menu") do |w| + menu.popup(nil, nil, 0, Gdk::Event::CURRENT_TIME) + end + + window.set_default_size(320, 100).show_all + window.signal_connect('destroy') { Gtk.main_quit } + window.add(Gtk::Label.new("Hello World\n" + + "You may 'right-click' me\n\n" + + "or use <Shift+F10>")) + window.show_all + Gtk.main + === Image Menu Items