Create  Edit  Ruby-GNOME2 Project Website  Index  Search  Changes  History  RSS  Login

Context Menus

Context Menus

Context menus are context-dependent menus that pop up when a user right-clicks on a list or tree and usually let the user do something with the selected items or manipulate the list or tree in other ways.

Right-clicks on a tree view are caught just like mouse button clicks are caught with any other widgets, namely by connecting to the tree view's "button_press_event" signal handler (which is a Gtk::Widget signal, and as Gtk::TreeView is derived from Gtk::Widget it has this signal as well). Additionally, you should also connect to the "popup-menu" signal, so users can access your context menu without a mouse. The "popup-menu" signal is emitted when the user presses Shift-F10. Also, you should make sure that all actions provided in your context menu can also be performed by other means such as the application's main menu. See the GNOME Human Interface Guidelines (HIG) for more details. Straight from the a-snippet-of-code-says-more-than-a-thousand-words-department, some code to look at:

# Create a menu
menu = Gtk::Menu.new

# Add an item with a silly callback
item = Gtk::MenuItem.new("Do Something")
item.signal_connect("activate") { puts "Did something!" }
menu.append(item)

menu.show_all

# Popup the menu on right click
view.signal_connect("button_press_event") do |widget, event|
  if event.kind_of? Gdk::EventButton and event.button == 3
    menu.popup(nil, nil, event.button, event.time)
  end
end

# Popup the menu on Shift-F10
view.signal_connect("popup_menu") { menu.popup(nil, nil, 0, Gdk::Event::CURRENT_TIME) }
Last modified:2005/05/03 02:51:12
Keyword(s):
References:[Ruby/GTK TreeView Tutorial]