In the true tradition of teaching, we'll show you the hard way first. :)
There are three widgets that go into making a menubar and submenus:
This is slightly complicated by the fact that menu item widgets are used for two different things. They are both the widgets that are packed into the menu, and the widget that is packed into the menubar, which, when selected, activates the menu.
Let's look at the methods that are used to create menus and menubars. This first method is used to create a new menubar.
Gtk::MenuBar.new
This rather self explanatory method creates a new menubar. You use Gtk::Container#add to pack this into a window, or the box_pack methods to pack it into a box - the same as buttons.
Gtk::Menu.new
This method returns a new menu; it is never actually shown (with Gtk::Widget#show), it is just a container for the menu items. I hope this will become more clear when you look at the example below.
The next three calls are used to create menu items that are packed into the menu (and menubar).
Gtk::MenuItem.new Gtk::MenuItem.new( label ) Gtk::MenuItem.new( label, use_mnemonic )
These calls are used to create the menu items that are to be displayed. Remember to differentiate between a "menu" as created with Gtk::Menu.new and a "menu item" as created by the Gtk::MenuItem.new methods. The menu item will be an actual button with an associated action, whereas a menu will be a container holding menu items.
The Gtk::MenuItem.new( label ) and Gtk::MenuItem.new methods are just as you'd expect after reading about the buttons. One creates a new menu item with a label already packed into it, and the other just creates a blank menu item.
Once you've created a menu item you have to put it into a menu. This is done using the method Gtk::MenuShell#append. In order to capture when the item is selected by the user, we need to connect to the activate signal in the usual way. So, if we wanted to create a standard File menu, with the options Open, Save, and Quit, the code would look something like:
file_menu = Gtk::Menu.new; # Don't need to show menus # Create the menu items open_item = Gtk::MenuItem.new("Open") save_item = Gtk::MenuItem.new("Save") quit_item = Gtk::MenuItem.new("Quit") # Add them to the menu file_menu.append( open_item ) file_menu.append( save_item ) file_menu.append( quit_item ) # Attach the callback methods to the activate signal file_menu.signal_connect( "activate" ) { menuitem_response } save_item.signal_connect( "activate" } { menuitem_response } # We can attach the Quit menu item to our exit method g_signal_connect_swapped ( "activate" ) { Gtk.main_quit } # We do need to show menu items open_item.show save_item.show quit_item.show
At this point we have our menu. Now we need to create a menubar and a menu item for the File entry, to which we add our menu. The code looks like this:
menu_bar = Gtk::MenuBar.new window.add( menu_bar ) menu_bar.show file_item = Gtk::MenuItem.new( "File" ) file_item.show
Now we need to associate the menu with file_item. This is done with the method
Gtk::MenuItem#set_submenu( submenu )
So, our example would continue with
file_item.set_submenu( file_menu )
All that is left to do is to add the menu to the menubar, which is accomplished using the method
Gtk::MenuShell#append( menu_item )
which in our case looks like this:
menu_bar.append( file_item )
If we wanted the menu right justified on the menubar, such as help menus often are, we can use the following method (again on file_item in the current example) before attaching it to the menubar.
menu_item.set_right_justified( true ) # OR menu_item.right_justified=( true )
Here is a summary of the steps needed to create a menu bar with menus attached:
Creating a popup menu is nearly the same. The difference is that the menu is not posted "automatically" by a menubar, but explicitly by calling the method Gtk::Menu#popup from a button-press event, for example. Take these steps:
Create an event handling method. It needs to have the prototype,
def handler( widget, event )
and it will use the event to find out where to pop up the menu.
In the event handler, if the event is a mouse button press, treat event as a button event (which it is) and use it as shown in the sample code to pass information to Gtk::Menu#popup.
Bind that event handler to a widget with
signal_connect ( "event", Gdk::Event::BUTTON_PRESS { |w, e| handler( w, e, menu ) }
where widget is the widget you are binding to, handler is the handling method, and menu is a menu created with Gtk::Menu.new. This can be a menu which is also posted by a menu bar, as shown in the sample code on the next page.
Prev | Next |