4.5. Table Packing Example

The example program table.st makes a window with three buttons in a 2x2 table. The first two buttons will be placed in the upper row. A third, quit button, is placed in the lower row, spanning both columns. Figure 4.6, “Packing using a Table” illustrates the resulting window:

Figure 4.6. Packing using a Table

Packing using a Table

Here's the source code:

   1    #!/usr/bin/env gst                                               
   2                                                                     
   3    " example table.st "                                             
   4                                                                     
   5    Eval [                                                           
   6        PackageLoader fileInPackage: 'GTK'.                          
   7    ]                                                                
   8                                                                     
   9    Object subclass: Table [                                         
  10            | button table window |                                  
  11        " Our callback.                                              
  12          The data passed to this method is printed to stdout "      
  13        clicked: widget string: aString [                            
  14            ('Hello again - ', aString, ' was pressed') printNl      
  15            ]                                                        
  16                                                                     
  17        " This callback quits the program "                          
  18        delete: aWidhet event: anEvent [                             
  19            GTK.Gtk mainQuit.                                        
  20                    ^ false                                          
  21            ]                                                        
  22                                                                     
  23        show [                                                       
  24            " Create a new window "                                  
  25            window := GTK.GtkWindow new:GTK.Gtk gtkWindowToplevel.   
  26                                                                     
  27            " Set the window title "                                 
  28            window setTitle: 'Table'.                                
  29                                                                     
  30            " Set a handler for delete_event that immediately        
  31              exits GTK. "                                           
  32                    window connectSignal: 'delete_event' to: self selector: #delete:event:.
  33                                                                                           
  34            " Sets the border width of the window. "                                       
  35            window setBorderWidth: 20.                                                     
  36                                                                                           
  37            " Create a 2x2 table "                                                         
  38            table := GTK.GtkTable new: 2 columns: 2 homogeneous: true.                     
  39                                                                                           
  40            " Put the table in the main window "
  41            window add: table.
  42
  43            " Create first button "
  44            button := GTK.GtkButton newWithLabel: 'button 1'.
  45
  46            " When the button is clicked, we call the 'callback' method
  47              with a pointer to 'button 1' as its argument "
  48                    button connectSignal: 'clicked' to: self selector: #clicked:string: userData: 'button 1'.
  49
  50            " Insert button 1 into the upper left quadrant of the table "
  51                    table attach: button leftAttach: 0 rightAttach: 1 topAttach: 0 bottomAttach: 1
  52                   xoptions:  (GTK.Gtk gtkExpand bitOr: GTK.Gtk gtkFill) yoptions: (GTK.Gtk gtkExpand bitOr: GTK.Gtk gtkFill) xpadding: 0 ypadding: 0.
  53
  54            button show.
  55
  56            " Create second button "
  57            button := GTK.GtkButton newWithLabel: 'button 2'.
  58
  59            " When the button is clicked, we call the 'callback' method
  60              with a pointer to 'button 2' as its argument "
  61                    button connectSignal: 'clicked' to: self selector: #clicked:string: userData: 'button 2'.
  62            " Insert button 2 into the upper right quadrant of the table "
  63                    table attach: button leftAttach: 1 rightAttach: 2 topAttach: 0 bottomAttach: 1
  64                   xoptions:  (GTK.Gtk gtkExpand bitOr: GTK.Gtk gtkFill) yoptions: (GTK.Gtk gtkExpand bitOr: GTK.Gtk gtkFill) xpadding: 0 ypadding: 0.
  65
  66            button show.
  67
  68            " Create 'Quit' button "
  69            button := GTK.GtkButton newWithLabel: 'Quit'.
  70
  71            " When the button is clicked, we call the mainQuit function
  72              and the program exits "
  73                    button connectSignal: 'clicked' to: self selector: #delete:event: userData: 'button 2'.
  74
  75            " Insert the quit button into the both lower quadrants of the table "
  76                    table attach: button leftAttach: 0 rightAttach: 2 topAttach: 1 bottomAttach: 2
  77                   xoptions:  (GTK.Gtk gtkExpand bitOr: GTK.Gtk gtkFill) yoptions: (GTK.Gtk gtkExpand bitOr: GTK.Gtk gtkFill) xpadding: 0 ypadding: 0.
  78
  79            button show.
  80            table show.
  81            window show.
  82            ]
  83    ]
  84
  85    Eval [
  86            table := Table new.
  87            table show.
  88            GTK.Gtk main
  89    ]

The Table class is defined in line 9-82. Lines 10-15 define the callback method which is called when two of the buttons are "clicked". The callback just prints a message to the console indicating which button was pressed using the passed in string data.

Lines 18-21 define the deleteEvent method which is called when the window is slated for deletion by the window manager.

Lines 23-82 define the show. It creates a window (line 25), sets the window title (line 28), connects the deleteEvent callback to the "delete_event" signal (line 32), and sets the border width (line 35). A GTK.GtkTable is created in line 38 and added to the window in line 41.

The two upper buttons are created (lines 44 and 57), their "clicked" signals are connected to the callback method (lines 45 and 59), and attached to the table in the first row (lines 51 and 63). Lines 69-75 create the "Quit" button, connect its "clicked" signal to the main_quit function and attach it to the table spanning the whole second row.