ruby-****@sourc*****
ruby-****@sourc*****
2012年 10月 29日 (月) 07:15:38 JST
------------------------- REMOTE_ADDR = 74.14.158.59 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-mnstbs-popup ------------------------- @@ -12,6 +12,7 @@ ((*bare-bones-context-menu.rb*)) + #!/usr/bin/env ruby require 'gtk2' @@ -25,19 +26,43 @@ mitem2.signal_connect('activate') { |w| puts "#{w.class} - Test2" } window = Gtk::Window.new("Bare Bones Context Menu") + # 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(300, 100).show_all window.signal_connect('destroy') { Gtk.main_quit } - window.add(Gtk::Label.new("Hello World\nYou may 'right-click' me")) + window.add(Gtk::Label.new("Hello World\n" + + "You may 'right-click' me\n\n" + + "or use <Shift+F10>")) window.show_all Gtk.main + +This short example shod give you a good idea, about creating a pop-up menu. We started by creating the menu, adding to it two menu items. Note, from Gtk::Widget inherited 'show_all' method which you have to run after you set up your menu. Though, at this point not yet essential, but eventually required is the code to connect menu's((*'activate'*))signal to callbacks. The last is a rather important step directly related to pop-up menu implementation, which depending on the capabilities of the widget to which the menu is going to be attached, can be accomplished in more ways. Simpler ones are the two we use here in our bare-bones pop-up menu example, since window provides or inherits all necessary behaviours. + + # 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 +Do not worry, if you do not understand all the features above, and if some are not covered in full, we are going to review them shortly. -This short example shod give you a good idea, about creating a pop-up menu. Do not worry, if you do not understand all the features used in the program, we are going to review them shortly. {{br}} {{br}}