Re: gtk - changing buttons depending on the selection
| From: | kksou | Date: | Wed, 20 Sep 2006 03:24:58 +0000 |
| Subject: | Re: gtk - changing buttons depending on the selection | ||
| References: | 1 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-7257@lists.php.net to get a copy of this message | ||
jerik wrote:
>
> I have to use php-gtk1 for my development. At the moment I have a list
> with some items. When the user chooses a Item of the list, it should be
> displayed some buttons. The displaying of the buttons depends of the
> choosed list-item - so the buttons must be flexible.
>
> At the moment I dont know how I should realize that with buttons. The only
> possibilty which came up to my mind was to create a second list, wich I
> create everytime new when a list-item (of the first list) is choosen and
> append the needed item (buttons) to the second list. But this is an ugly
> workaround.
>
> Any Ideas how to solve this?
>
Like what Brian said. Create the buttons first, then show and hide
accordingly.
Starting from the example in PHP-GTK1 manual, here's a sample code that
shows how you can change the buttons on the fly.
Note: this is for php-gtk1 only.
Sample output:
http://www.kksou.com/php-gtk2/gtk1/gif/0026.gif
http://www.kksou.com/php-gtk2/gtk1/gif/0026.2.gif
http://www.kksou.com/php-gtk2/gtk1/gif/0026.3.gif
A couple of notes:
- The button labels are stored in the array $buttons
- To hide a button, set it as ''
- On my windows machine, sometimes some of the button appear without the
label. However if you move the mouse over it, the label will appear. And it
responds correctly with button click. Guess it's a bug in my version of
php-gtk1...?
- Note how it's possible in php-gtk to use just one event-handler to handle
all the mouse clicks from the different combinations of buttons. You just
need to pass the right variables along.
- Yes, I know. I used 2 global variables. Ugly. But had to do this to keep
the codes short and easier to understand.
- The codes below may be truncated at column 70 in email. So if you just cut
and paste, you might get some errors. If you have problem getting it run,
you may get another copy of the demo code from:
http://www.kksou.com/php-gtk2/gtk1/articles/changing-buttons-based-on-pulldown-menu-selections.php
Sample code:
if( !extension_loaded('gtk')) {
dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);
}
function on_click($widget, $event, $i, $buttons) {
// just call on_key so that we do not need to write duplicate functions
on_key($widget, $i, $buttons);
}
function on_key($widget, $i, $buttons) {
global $button_ptr, $button_handler;
$key = $widget->get_data($i);
echo "on_click: $key\n";
for ($j=0; $j<3; ++$j) {
$button_value = $buttons[$key][$j];
if ($button_value == '') {
// hide the buttons if button value is empty
$button_ptr[$j]->hide();
} else {
$label = $button_ptr[$j]->child;
$label->set_text($button_value);
$label->show();
echo "button_value = $button_value\n";
$button_ptr[$j]->show();
// release any previous event handler first
if (isset($button_handler[$j])) {
$button_ptr[$j]->disconnect($button_handler[$j]);
}
// setup the event handler for button click
$button_handler[$j] = $button_ptr[$j]->connect
('button-press-event', 'on_button_click', $buttons, $key, $j); // note 2
}
}
}
function on_button_click($widget, $event, $buttons, $key, $j) {
$button_value = $buttons[$key][$j];
echo "You have clicked: $button_value ($key) ($j)\n";
// based on $key and $j, you can perform your actions here
// $key is the selection from the $combo
// $j is the button pressed
}
$window = &new GtkWindow();
$window->set_position(GTK_WIN_POS_CENTER);
$window->connect_object("destroy", array("gtk","main_quit"));
$window->set_usize(400, 200);
$vbox = &new GtkVBox();
$window->add($vbox);
$selection_list = array('item1', 'item2', 'item3');
$buttons = array(
'item1' => array('File', '', 'Exit'),
'item2' => array('button2-1', 'button2-2', 'button2-3'),
'item3' => array('', '', 'Exit')
);
$hbox = &new GtkHBox();
$vbox->pack_start($hbox, 0, 0);
$button_ptr = array();
$button_handler = array();
for ($i=0; $i<3; ++$i) {
$hbox2 = &new GtkHBox();
$button = &new GtkButton('button'.$i);
$button->set_usize(100, 32);
$button->hide(); // hide the buttons first
$button_ptr[$i] = $button; // note 1
$hbox2->set_usize(120, 40); // pack the button inside a hbox
$hbox2->pack_start($button, 0, 1); // to fix the position of the 3 buttons
$hbox->pack_start($hbox2, 0, 0); // place the buttons on the same row
}
$combo = &new GtkCombo();
$entry = $combo->entry;
$entry->set_text('choose an item');
$list = $combo->list;
$list->set_selection_mode(GTK_SELECTION_SINGLE);
for ($i=0; $i<count($selection_list); ++$i) {
$item = &new GtkListItem();
$label = &new GtkLabel($selection_list[$i]);
$label->set_alignment(0,0);
$item->add($label);
$combo->set_item_string($item, "You have selected $selection_list[$i]");
$item->set_data($i, $selection_list[$i]);
$item->connect('button-press-event', 'on_click', $i, $buttons);
$item->connect('toggle', 'on_key', $i, $buttons);
$list->add($item);
$item->show_all();
}
$vbox->pack_start($combo);
$window->show_all();
// hide the buttons first
for ($i=0; $i<3; ++$i) {
$button_ptr[$i]->hide();
}
gtk::main();
http://www.kksou.com/php-gtk2/gtk1/articles/changing-buttons-based-on-pulldown-menu-selections.php
More explanations here...
--
View this message in context: http://www.nabble.com/gtk---changing-buttons-depending-on-the-selection-tf2300076.html#a6400866
Sent from the Php - GTK - General mailing list archive at Nabble.com.