Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Back  Login

tut-gtk2-btt-toggle Diff - Ruby-GNOME2 Project Website

  • Added parts are displayed like this.
  • Deleted parts are displayed like this.

= Buttons And Toggles
{{link "tut-gtk2-btt", "tut-gtk2-btt", "tut-gtk", "tut-gtk2-btt-check"}}

== Toggle Button
* ((<Mnemonics|tut-gtk2-btt-toggle#Mnemonics>)) (skip forward)

{{br}}
{{image_right("btt-toggle.png")}}
{{br}}
=== Toggle Button

Toggle buttons widget is of a type Button. After it is clicked it holds its "active" or "inactive" state. The "active" state is shown as pressed down. Clicking an active toggle button will cause it to return to its normal state. As you can see from the listing the state is a property of the Widget, hence it is controlled by Widget instance method Gtk::Widget#sensitive. You can obtain a state of a Widget object (w) by querying it via "Gtk::ToggleButton#active?" method. At this point it should be clear that since ToggleButton inherits state properties from Widgets it may be prudent to check out constants that pertain to Widget state - they can be found in Gtk documentation (Gtk::Widget) under the heading GtkWidgetFlags.    

There are two widgets derived from ToggleButton: Gtk::CheckButton and Gtk::RadioButton.


#!/usr/bin/env ruby

require 'gtk2'
def toggle_em(w, other_tgg)
   # Gtk::Widget#sensitive=boolean |or #set_sensitive(boolean)
   other_tgg.sensitive = w.active? ? false : true
end

window = Gtk::Window.new(Gtk::Window::TOPLEVEL)
window.set_title  "Toggle Buttons"
window.border_width = 10
window.signal_connect('delete_event') { Gtk.main_quit }

vbox = Gtk::VBox.new(true, 5)
toggle1 = Gtk::ToggleButton.new("_Deactivate the bottom button")
toggle2 = Gtk::ToggleButton.new("_No deactivate the above button")

toggle1.signal_connect( "toggled" ) { |w| toggle_em(w, toggle2) }
toggle2.signal_connect( "toggled" ) { |w| toggle_em(w, toggle1) }

vbox.pack_start_defaults(toggle1)
vbox.pack_start_defaults(toggle2)

window.add(vbox)
window.show_all
Gtk.main

=== Mnemonics
Please pay attention to the underlined letters on the "Toggle Buttons" window image above. This is an additional feature commonly found in buttons, namely the "mnemonics". The mnemonics is a combination of keys which, when pressed, perform some type of an action. In our case a user can press the "Alt" key and the underlined letter simultaneously, i.e. "Alt + D" or more properly <Alt>+<D> would have the same effect as clicking the top button, just like the "<Alt>+<N>" combination would trigger the bottom button press action (of course the buttons have to be active to respond to either a keyboard or mouse activity).

In Ruby Gtk programs mnemonics is automatically enabled by placing an "underscore" (_) character in the label of a button when a button is created with for instance a call to  Gtk::Button.new.


=== Following is a similar example from the Japanese tutorial page:


Things to note are multiple mnemonic characters for the first button and how mnemonics is disabled with the Boolean value "false" for the second button.  

{{image_right("btt-toggle-jp-040-00.png")}}
{{br}}

#!/usr/bin/env ruby

require 'gtk2'

window = Gtk::Window.new
window.title = "Mnemonics and stock"
window.signal_connect("delete_event") {Gtk::main_quit}
window.signal_connect("destroy") {Gtk::main_quit}

box = Gtk::VBox.new(false, 0)
window.add(box)

button = Gtk::Button.new("_Tex_t")
button.signal_connect("activate") do
   puts "activated"
end
box.pack_start(button, false, false, 5)

button = Gtk::Button.new("_Text", false)
box.pack_start(button, false, false, 5)

count = 0
button = Gtk::Button.new
button.signal_connect("clicked") do |b|
b.label = "#{count} times"
count += 1
end
box.pack_start(button, false, false, 5)

button = Gtk::Button.new(Gtk::Stock::OK)
box.pack_start(button, false, false, 5)

window.show_all
Gtk.main