6.4. Radio Buttons

Radio buttons are similar to check buttons except they are grouped so that only one may be selected/depressed at a time. This is good for places in your application where you need to select from a short list of options.

Creating a new radio button is done with this call:

  radio_button := GTK.GtkRadioButton newWithLabel: group label: label

You'll notice the extra argument to this call. Radio buttons require a group to operate properly. The first call to GTK.GtkRadioButton should pass nil as the first argument and a new radio button group will be created with the new radio button as its only member.

To add more radio buttons to a group, pass in a reference to a radio button in group in subsequent calls to GTK.GtkRadioButton.

If a label argument is specified the text will be parsed for '_'-prefixed mnemonic characters.

It is also a good idea to explicitly set which button should be the default depressed button with:

  radio_button setActive: is_active

This is described in the section on toggle buttons, and works in exactly the same way. Once the radio buttons are grouped together, only one of the group may be active at a time. If the user clicks on one radio button, and then on another, the first radio button will first emit a "toggled" signal (to report becoming inactive), and then the second will emit its "toggled" signal (to report becoming active).

The example program radiobuttons.st creates a radio button group with three buttons. Figure 6.4, “Radio Buttons Example” illustrates the resulting window:

Figure 6.4. Radio Buttons Example

Radio Buttons Example

The source code for the example program is:

   1    #!/usr/bin/env gst                                                                     
   2                                                                                           
   3    " example radiobutton.st"                                                              
   4                                                                                           
   5    Eval [                                                                                 
   6        PackageLoader fileInPackage: 'GTK'.                                                
   7    ]                                                                                      
   8                                                                                           
   9    Object subclass: RadioButton [                                                         
  10        | button separator vbox1 vbox2 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            window := GTK.GtkWindow new:GTK.Gtk gtkWindowToplevel.                                                           
  23                                                                                                                             
  24            window setTitle: 'Radio Button'.                                                                                 
  25                                                                                                                             
  26            window connectSignal: #'destroy' to: self selector: #quit.
  27            window connectSignal: #'delete_event' to: self selector: #quit.
  28
  29            window setBorderWidth: 10.
  30
  31            vbox1 := GTK.GtkVBox new: false spacing: 0.
  32            window add: vbox1.
  33            vbox1 show.
  34
  35            vbox2 := GTK.GtkVBox new: false spacing: 2.
  36            vbox1 packStart: vbox2 expand: true fill: true padding: 0.
  37            vbox2 show.
  38
  39            button := GTK.GtkRadioButton newWithLabel: nil label: 'radio button 1'.
  40            button connectSignal: #'toggled' to: self selector: #'callback:data:' userData: 'radio button 1'.
  41            vbox2 packStart: button expand: true fill: true padding: 0.
  42            button show.
  43
  44            button := GTK.GtkRadioButton newWithLabel: button getGroup label: 'radio button 2'.
  45            button connectSignal: #'toggled' to: self selector: #'callback:data:' userData: 'radio button 2'.
  46            button setActive: true.
  47            vbox2 packStart: button expand: true fill: true padding: 0.
  48            button show.
  49
  50            button := GTK.GtkRadioButton newWithLabel: button getGroup label: 'radio button 3'.
  51            button connectSignal: #'toggled' to: self selector: #'callback:data:' userData: 'radio button 3'.
  52            vbox2 packStart: button expand: true fill: true padding: 0.
  53            button show.
  54
  55            separator := GTK.GtkHSeparator new.
  56            vbox2 packStart: separator expand: false fill: true padding: 0.
  57            separator show.
  58
  59            vbox2 := GTK.GtkVBox new: false spacing: 2.
  60            vbox1 packStart: vbox2 expand: false fill: true padding: 0.
  61            vbox2 show.
  62
  63            button := GTK.GtkButton newWithLabel: 'Quit'.
  64            button connectSignal: #'clicked' to: self selector: #quit.
  65            vbox2 packStart: button expand: true fill: true padding: 2.
  66            button
  67                setFlags: GTK.Gtk gtkCanDefault;
  68                grabDefault.
  69            button show.
  70
  71            window show.
  72        ]
  73    ]
  74
  75    Eval [
  76        radiobutton := RadioButton new.
  77        radiobutton show.
  78        GTK.Gtk main
  79    ]

The code is fairly straight forward. Lines 66-68 make the "close" button the default widget so that pressing the "Enter" key when the window is active causes the "close" button to emit the "clicked" signal.