ruby-****@sourc*****
ruby-****@sourc*****
2012年 10月 26日 (金) 22:46:56 JST
------------------------- REMOTE_ADDR = 74.14.158.59 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-mnstbs-statb ------------------------- @@ -55,24 +55,11 @@ There is a slight problem with Ruby implementation, however. Look at the two pictures. One at the top on the right hand side shows us what we get when we run the current C GTK+, the bottom one here on the left is the Ruby version. You can see that the Ruby version does not handle multiple signals on a menu item correctly, namely the ((*enter_notify_event*)) seems to have disturbed the ((*'activate'*)) event, which in Ruby version misses the highlight on the item over which the mouse cursor hovers. {{br}} -((*statbhints.rb*)) +((*statbhints-n-state.rb*)) - #!/usr/bin/env ruby - require 'gtk2' - def statusbar_hint(menui, event, statusbar, hints) - cntxt_id = statusbar.get_context_id("StatBarHints") - if event.event_type == Gdk::Event::ENTER_NOTIFY - statusbar.push(cntxt_id, hints[menui.name]) - else - statusbar.pop(cntxt_id) - end - return # Mandatory, if used together with another signal handler that - # needs to update screen activity, like browsing menu choices! - end - # Create the poup menu with three items and a separator. # Then, attach it to the progress bar and show it to the # user. @@ -90,12 +77,2 @@ "fill" => "Set the progress bar to 100%.", "clear" => "Clear the progress bar to 0%." } - menu.append(pulse) menu.append(separator) menu.append(fill) menu.append(clear) + pulse.signal_connect('activate') { pulse_activated(sbar, progb) } + fill.signal_connect('activate') { fill_activated(sbar, progb) } + clear.signal_connect('activate') { clear_activated(sbar, progb) } + pulse.signal_connect('enter_notify_event') { |w, e| statusbar_hint(w, e, sbar, smsgs) } fill.signal_connect('enter_notify_event') { |w, e| statusbar_hint(w, e, sbar, smsgs) } clear.signal_connect('enter_notify_event') { |w, e| statusbar_hint(w, e, sbar, smsgs) } @@ -104,36 +94,49 @@ fill.signal_connect('leave_notify_event') { |w, e| statusbar_hint(w, e, sbar, smsgs) } clear.signal_connect('leave_notify_event') { |w, e| statusbar_hint(w, e, sbar, smsgs) } - pulse.signal_connect('activate') { |w| pulse_activated(w, progb) } - fill.signal_connect('activate') { |w| fill_activated(w, progb) } - clear.signal_connect('activate') { |w| clear_activated(w, progb) } - menu.show_all end + + # Show in status bar what this menu item will do. + def statusbar_hint(menui, event, sbar, hints) + cntxt_id = sbar.get_context_id("StatBarHints") + if event.event_type == Gdk::Event::ENTER_NOTIFY + sbar.push(cntxt_id, hints[menui.name]) + else + sbar.pop(cntxt_id) + end + return # Mandatory, if used together with another signal handler that + # needs to update screen activity, like browsing menu choices! + end - def pulse_activated(menuitem, progbar) - puts "pulse_activated" + # Update curently selected state in progres bar + def pulse_activated(sbar, progbar) + return if progbar.text == "Pulse!" progbar.pulse progbar.text = "Pulse!" + + cntxt_id = sbar.get_context_id("CurrentStatus") + sbar.pop(cntxt_id) + sbar.push(cntxt_id, "Pulsating") end - def fill_activated(menuitem, progbar) - puts "fill_activated" + # Update curently selected state in progres bar + def fill_activated(sbar, progbar) + return if progbar.text == "One Hundred Percent" progbar.fraction = 1.0 progbar.text = "One Hundred Percent" + + cntxt_id = sbar.get_context_id("CurrentStatus") + sbar.pop(cntxt_id) + sbar.push(cntxt_id, "Set to filled") end - def clear_activated(menuitem, progbar) - puts "clear_activated" + # Update curently selected state in progres bar + def clear_activated(sbar, progbar) + return if progbar.text == "Reset to Zero" progbar.fraction = 0.0 progbar.text = "Reset to Zero" + + cntxt_id = sbar.get_context_id("CurrentStatus") + sbar.pop(cntxt_id) + sbar.push(cntxt_id, "Cleared (ready to pulsate)") end - window = Gtk::Window.new("Ruby: Status Bar Hints") + window = Gtk::Window.new("Status Bar Hints & State") window.resizable = true window.border_width = 10 window.signal_connect('destroy') { Gtk.main_quit } - window.set_size_request(250, -1) + window.set_size_request(275, -1) # Create all of the necessary widgets and initialize the popup menu. menu = Gtk::Menu.new @@ -165,7 +178,19 @@ eventbox.realize window.show_all Gtk.main -