ruby-****@sourc*****
ruby-****@sourc*****
2012年 11月 6日 (火) 04:46:35 JST
------------------------- REMOTE_ADDR = 184.145.95.170 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-mnstbs-mnub ------------------------- @@ -6,9 +6,10 @@ {{image_right("menubars.png")}} + Gtk::MenuBar is a widget that organizes multiple pop-up menus into a horizontal or vertical row. Each root element is a Gtk::MenuItem that pops down into a sub-menu. An instance of Gtk::MenuBar is usually displayed along the top of the main application window to provide access to functionality provided by the application. -In the following example program called "menubars.rb" a Gtk::MenuBar widget is created with four menus: File, Edit, Organize and Help. Each of the menus is actually a Gtk::MenuItem with a sub-menu. A number of menu items are then added to each sub-menu. The Organize menu has two menu items. The first Preferences is final (leaf) menu item and second Languages menu item which contains a sub-menu with five leaf menu items. +In the following example program called 'menubars.rb' a Gtk::MenuBar widget is created with four menus: File, Edit, Organize and Help. Each of the menus is actually a Gtk::MenuItem with a sub-menu. A number of menu items are then added to each sub-menu. The Organize menu has two menu items. The first, Preferences, is a final (leaf) menu item and second Languages menu item is a actually a menu which contains a sub-menu with five leaf menu items. We create all the menu items as well as sub-menus and their menu items manually. This also gives us the opportunity to look at menu items' signal handling a bit more thoroughly than before, expanding the discussion to the keyboard accelerator keys, and how they relate to menu items and their signal handling mechanisms. {{br}} @@ -23,7 +24,7 @@ window.signal_connect('destroy') { Gtk.main_quit } window.set_size_request(250, -1) - group = Gtk::AccelGroup.new menubar = Gtk::MenuBar.new filemenu = Gtk::Menu.new @@ -46,9 +46,15 @@ menubar.append(org) menubar.append(help) + # We will use Accelerator keys in File, Edit and Language menus + group = Gtk::AccelGroup.new + # Create the File menu content. new = Gtk::ImageMenuItem.new(Gtk::Stock::NEW, group) open = Gtk::ImageMenuItem.new(Gtk::Stock::OPEN, group) + new.signal_connect('activate') { |w| puts "w=#{w.class}:New selected" } + open.signal_connect('activate') { |w| puts "w=#{w.class}:Open selected" } + filemenu.append(new) filemenu.append(open) @@ -56,6 +62,16 @@ cut = Gtk::ImageMenuItem.new(Gtk::Stock::CUT, group) copy = Gtk::ImageMenuItem.new(Gtk::Stock::COPY, group) paste = Gtk::ImageMenuItem.new(Gtk::Stock::PASTE, group) + cut.signal_connect('activate') { |w| puts "w=#{w.class}:Cut selected" } + copy.signal_connect('activate') { |w| puts "w=#{w.class}:Copy selected" } + paste.signal_connect('activate') { |w| puts "w=#{w.class}:Paste selected" } + editmenu.append(cut) editmenu.append(copy) editmenu.append(paste) @@ -63,12 +73,46 @@ # Create Language sub menu langmenu = Gtk::Menu.new + # Add '<Ctrl>+L' accelerator to Gtk::AccelGroup object, and + # open the Languages context menu + group.connect(Gdk::Keyval::GDK_L, Gdk::Window::CONTROL_MASK, Gtk::ACCEL_VISIBLE) do + puts "You've Pressed 'L'." + langmenu.popup(nil, nil, 0, Gdk::Event::CURRENT_TIME) + end + english = Gtk::MenuItem.new("English") french = Gtk::MenuItem.new("French") german = Gtk::MenuItem.new("German") russian = Gtk::MenuItem.new("Russian") italian = Gtk::MenuItem.new("Italian") + english.add_accelerator('activate', group, Gdk::Keyval::GDK_E, + Gdk::Window::CONTROL_MASK|Gdk::Window::SHIFT_MASK, Gtk::ACCEL_VISIBLE) + french.add_accelerator('activate', group, Gdk::Keyval::GDK_F, + Gdk::Window::CONTROL_MASK|Gdk::Window::SHIFT_MASK, Gtk::ACCEL_VISIBLE) + german.add_accelerator('activate', group, Gdk::Keyval::GDK_G, + Gdk::Window::CONTROL_MASK|Gdk::Window::SHIFT_MASK, Gtk::ACCEL_VISIBLE) + russian.add_accelerator('activate', group, Gdk::Keyval::GDK_R, + Gdk::Window::CONTROL_MASK|Gdk::Window::SHIFT_MASK, Gtk::ACCEL_VISIBLE) + italian.add_accelerator('activate', group, Gdk::Keyval::GDK_I, + Gdk::Window::CONTROL_MASK|Gdk::Window::SHIFT_MASK, Gtk::ACCEL_VISIBLE) + + english.signal_connect('activate') { |w| puts "w=#{w.class}:English selected" } + french.signal_connect('activate') { |w| puts "w=#{w.class}:French selected" } + german.signal_connect('activate') { |w| puts "w=#{w.class}:German selected" } + russian.signal_connect('activate') { |w| puts "w=#{w.class}:Russian selected" } + italian.signal_connect('activate') { |w| puts "w=#{w.class}:Italian selected" } + langmenu.append(english) langmenu.append(french) langmenu.append(german) @@ -96,7 +130,40 @@ Gtk.main - A new menu bar widget is created with Gtk::MenuBar.new. This creates an empty menu shell into which you add content. After you have created a menu bar you can set a pack direction with Gtk::MenuBar#pack_direction=(pack_dir), where pack_dir is one of the constants defined in Gtk::MenuBar::PackDirection. By default pack direction is left to right (Gtk::MenuBar::PACK_DIRECTION_LTR). Gtk::MenuBar class provides also child_pack_direction=(pack_dir) instance method, which sets how widgets should be packed inside the children of a menubar. Each child item in a menu bar is actually another Gtk::MenuItem widget. It is important that you understand the structure underneath the Gtk::MenuBar. Most likely the children of menu bar will be sub-menus. However they are not added directly to the menu bar. The direct children of any menu are menu items created with Gtk::MenuItem.new. You assign your sub-menus, which you created with my_sub_menu=Gtk::Menu.new, to these menu items with Gtk::MenuItem#submenu(my_sub_menu). @@ -104,3 +137,46 @@ Since all menus are derived from Gtk::MenuShell abstract class, we assigning menu items to sub-menus, using Gtk::MenuShell#append(child). Note that we use the this instance method on both menu bar objects as well as on menus i.e. sub-menus. We also have Gtk::MenuShell#prepend or Gtk::MenuShell#insert at our disposal. It is much easier to comprehend these concepts by looking at the code, so review the example program above to observe the relationships between these widgets. + + + + +{{br}} +{{br}} + +=== Keyboard Accelerators And Their Relationships With Menus And Other Widgets + +Though we have already discussed keyboard accelerators in section 9.2 '((<Keyboard Accelerators|tut-gtk2-mnstbs-popup#Keyboard Accelerators>))' now that we are familiar with menus, menu bars, and sub-menus we can expose also the relationships between them and other widgets such as top windows and other nested widgets particularly menu an sub-menu items.