6.3. Check Buttons

Check buttons inherit many properties and methods from the the toggle buttons above, but look a little different. Rather than being buttons with text inside them, they are small squares with the text to the right of them. These are often used for toggling options on and off in applications.

The creation method is similar to that of the normal button.

  check_button := GTK.GtkCheckButton newWithLabel: label

If the label argument is specified the method creates a check button with a label beside it. The label text is parsed for '_'-prefixed mnemonic characters.

Checking and setting the state of the check button are identical to that of the toggle button.

The checkbutton.st program provides an example of the use of the check buttons. Figure 6.3, “Check Button Example” illustrates the resulting window:

Figure 6.3. Check Button Example

Check Button Example

The source code for the checkbutton.st program is:

   1    #!/usr/bin/env gst                                                     
   2                                                                           
   3    " example checkbutton.st"                                              
   4                                                                           
   5    Eval [                                                                 
   6        PackageLoader fileInPackage: 'GTK'.                                
   7    ]                                                                      
   8                                                                           
   9    Object subclass: CheckButton [                                         
  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: 'Check 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.GtkCheckButton newWithLabel: 'check button 1'.
  42
  43            " Connect the #'clicked' signal of the button to our callback "
  44            button connectSignal: #'toggled' to: self selector: #'callback:data:' userData: 'check 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.GtkCheckButton newWithLabel: 'check button 2'.
  53
  54            " Connect the #'clicked' signal of the button to our callback "
  55            button connectSignal: #'toggled' to: self selector: #'callback:data:' userData: 'check 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        checkbutton := CheckButton new.
  79        checkbutton show.
  80        GTK.Gtk main
  81    ]