Object Oriented PHP-GTK2 - Radio Option Groups Object Oriented PHP-GTK2 - Radio Option Groups
it's me, bob. lol. Object Oriented PHP-GTK2 OOPS

Radio Option Groups

Using radio buttons to limit a user to one choice per group.

Topic: GtkRadioButton (View All Tutorials)
Keywords: gtkradiobutton grouping
Updated: 320 Days Ago, 2008/01/17 01:37

A common way to expose options to a user that should only allow one selection out of a possible list is with radio buttons. Radio buttons can be grouped together to form a list of options with only one selected choice at a time. The trick is we have to perform the grouping ourself because as widgets do not know what your program needs to ask the of user.

Radio Button Groups

Without telling each radio button what group they belong to they would not know how to disable one another when a different one is selected. To group radio buttons together, all you have to do is pass the "leader" of the group to the GtkRadioButton constructor (first argument) or by using the set_group() method on it later. Generally, the first radio button we create that is to be part of a group you would use as the leader. It is literally saying "Group this widget with that one there."

 

Building a small dialog of options.

#!/usr/bin/php -c/etc/gtk/php.ini
<?php

class bobWindow extends GtkWindow {
    
    public 
$radiocontain;
    public 
$radiobox;
    public 
$radiolist;
    public 
$radiobtn;

    public function 
__construct() {
        
parent::__construct();
        
        
$this->vbox = new GtkVBox;

        
$this->set_title('Radio Button Demo');
        
$this->set_position(Gtk::WIN_POS_CENTER);
        
$this->set_size_request(300,-1);
        
$this->connect_simple('delete-event',array($this,'on_quit'));
        
        
$this->build_radiolist();

        
$this->vbox->pack_start($this->radiocontain,true,true,3);
        
$this->add($this->vbox);
        
$this->show_all();
        return;
    }
    
    public function 
build_radiolist() {
        if(
is_array($this->radiolist)) {
            return;
        }

        
$this->radiocontain = new GtkHBox;
        
$this->radiolist = array();
        
$this->radiobtn = array();

        
// note 1
        // $radiodefine['List A'][0] is Group 1 Item 1.
        
$radiodefine = array(
            
'List A' => array('Item 1','Item 2','Item 3','Item 4'),
            
'List B' => array('Item 5','Item 6','Item 7','Item 8'),
            
'List C' => array('Item 9','Item 10','Item 11','Item 12')        
        );
        
        foreach(
$radiodefine as $list => $item) {
            
// note 2
            
$grpobj null;

            
// note 3
            
$this->radiolist[$list] = new GtkFrame($list);
            
$this->radiobox[$list] = new GtkVBox;
            
$this->radiolist[$list]->add($this->radiobox[$list]);
            
$this->radiocontain->pack_start($this->radiolist[$list],true,true,3);
            
            foreach(
$item as $btn) {
                
// note 4
                
$this->radiobtn[$btn] = new GtkRadioButton($grpobj,$btn);
                
$this->radiobtn[$btn]->set_border_width(3);
                
$this->radiobox[$list]->pack_start($this->radiobtn[$btn],true,true,3);

                
// note 5        
                
if(!$grpobj) {
                    
$grpobj $this->radiobtn[$btn];
                }
            }
        }        
            
        unset(
$radiodefine,$list,$item,$grpobj,$btn);
        return;
    }
    
    public function 
on_quit() {
        
$this->hide();
        
Gtk::main_quit();
        return;
    }
}

$w = new bobWindow;
Gtk::main();

$w->destroy();
unset(
$w);
exit(
0);

?>

Note 1

Creating all the options manually would be a pain, and it would also be pretty messy code. This multidimensional array groups all the options into separate lists, and the first option of each list will be used as the widget to set the groups.

Note 2

This sentinel value will hold a link to the first button for each group. Note first time around it has the value null, this way the first button does not try to join a group that obviously does not exist yet. Each iteration of this outer loop (every time a new list has begun to build) this $grpobj will be set to null.

Note 3

You should know your difference between your containers and your boxes. Each list will be created inside a GtkFrame that has a Label to say what the list is about. As this widget may only have one child, we will pack a GtkVBox inside it, and inside the GtkVBox is where all the buttons will be packed.

Note 4

Note first time around, $grpobj is still null. The first button created using it will not be assigned a group.

Note 5

Assign $grpobj to the GtkRadioButton if it is still null. Now the other buttons to be created for this group by the inner loop will link to the first button. Remember objects are passed by reference by default, so we are not linking to a copy of the first button, it is the actual first button just linked to the sentinel variable.

 

End Game

Was this document helpful? I appreciate your feedback.
What are the rules about reusing this code?


i can has web two point ooh // copyright © 2007-2008 bob majdak jr
[ xhtml css | firefox ie7 opera ]