Toggle buttons are derived from normal buttons and are very similar, except they will always be in one of two states, alternated by a click. They may be depressed, and when you click again, they will pop back up. Click again, and they will pop back down.
Toggle buttons are the basis for check buttons and radio buttons, as such, many of the calls used for toggle buttons are inherited by radio and check buttons. I will point these out when we come to them.
Creating a new toggle button:
toggle_button := GTK.GtkToggleButton newWithLabel:label
As you can imagine, these work identically to the normal button widget calls. If no label is specified the button will be blank. The label text will be parsed for '_'-prefixed mnemonic characters.
To retrieve the state of the toggle widget, including radio and
check buttons, we use a construct as shown in our example below. This tests
the state of the toggle, by calling the #getActive
method of the toggle button object. The signal of interest to us that is
emitted by toggle buttons (the toggle button, check button, and radio button
widgets) is the "toggled" signal. To check the state of these buttons, set
up a signal handler to catch the toggled signal, and access the object
attributes to determine its state. The callback will look something
like:
toggle_button_callback: aGtkWidget data: anObject [
aGtkWidget getActive
ifTrue: [ " If control reaches here, the toggle button is down " ]
ifFalse: [ " If control reaches here, the toggle button is up " ]
]
To force the state of a toggle button, and its children, the radio and check buttons, use this method:
toggle_button setActive: aBoolean
The above method can be used to set the state of the toggle
button, and its children the radio and check buttons. Specifying a
true or false for the
setActive: argument indicates whether the button
should be down (depressed) or up (released). When the toggle button is
created its default is up or false.
Note that when you use the setActive:
method, and the state is actually changed, it causes the "clicked" and
"toggled" signals to be emitted from the button.
toggle_button getActive
This method returns the current state of the toggle button as a
boolean true or false value.
The togglebutton.st program provides a simple example using toggle buttons. Figure 6.2, “Toggle Button Example” illustrates the resulting window with the second toggle button active:
The source code for the program is:
1 #!/usr/bin/env gst
2
3 " example togglebutton.st"
4
5 Eval [
6 PackageLoader fileInPackage: 'GTK'.
7 ]
8
9 Object subclass: ToggleButton [
10 | button vbox window |
11
12 " Our usual callback method "
13 callback: aGtkWidget data: aString [
14 ('Hello again - ', aString, ' was toggled ', (aGtkWidget getActive ifTrue: [ 'ON' ] ifFalse: [ 'OFF' ]) ) printNl
15 ]
16
17 quit [
18 GTK.Gtk mainQuit
19 ]
20
21 show [
22 " Create a new window "
23 window := GTK.GtkWindow new:GTK.Gtk gtkWindowToplevel.
24
25 window setTitle: 'Toggle Button'.
26
27 " It's a good idea to do this for all windows. "
28 window connectSignal: #'destroy' to: self selector: #quit.
29 window connectSignal: #'delete_event' to: self selector: #quit.
30
31 " Sets the border width of the window. "
32 window setBorderWidth: 10.
33
34 " Create a vertical box "
35 vbox := GTK.GtkVBox new: true spacing: 2.
36
37 " Put the vbox in the main window "
38 window add: vbox.
39
40 " Create a new button "
41 button := GTK.GtkToggleButton newWithLabel: 'toggle button 1'.
42
43 " Connect the #'clicked' signal of the button to our callback "
44 button connectSignal: #'toggled' to: self selector: #'callback:data:' userData: 'toggle button 1'.
45
46 " Insert button 1 "
47 vbox packStart: button expand: true fill: true padding: 2.
48
49 button show.
50
51 " Create a new button "
52 button := GTK.GtkToggleButton newWithLabel: 'toggle button 2'.
53
54 " Connect the #'clicked' signal of the button to our callback "
55 button connectSignal: #'toggled' to: self selector: #'callback:data:' userData: 'toggle button 2'.
56
57 " Insert button 2 "
58 vbox packStart: button expand: true fill: true padding: 2.
59
60 button show.
61
62 " Create a new button "
63 button := GTK.GtkButton newWithLabel: 'Quit'.
64
65 " Connect the #'clicked' signal of the button to our callback "
66 button connectSignal: #'clicked' to: self selector: #quit.
67
68 " Insert quit button "
69 vbox packStart: button expand: true fill: true padding: 2.
70
71 button show.
72 vbox show.
73 window show.
74 ]
75 ]
76
77 Eval [
78 togglebutton := ToggleButton new.
79 togglebutton show.
80 GTK.Gtk main
81 ]
The interesting lines are 13-15 which define the
#callback:data: method that prints the toggle button
label and its state when it is toggled. Lines 44 and 55 connect the
"toggled" signal of the toggle buttons to the
#callback:data: method.