GtkOptionMenu - Example Code
| From: | Arnd Vehling | Date: | Thu, 04 Sep 2003 17:13:17 +0000 |
| Subject: | GtkOptionMenu - Example Code | ||
| Groups: | php.gtk.general | ||
| Request: | Send a blank email to php-gtk-general+get-3791@lists.php.net to get a copy of this message | ||
Hello,
after trying for at least a day to figure out how to get a selected
value from an GtkOptionMenu when the Option Values are specified
via a ".glade" xml file i gave up and wrote me 2 functions to ease
the creation of GtkMenuItem(s) for the GtkOptionMenu.
Maybe someone browsing the mail archive finds this helpfull.
This programm creates 2 Windows (sorry, you need to resize them
manually) with a GtkOptionMenu in each window.
The Option Values are defined in an according array. I hope the
code explain itself. Keep in mind that i am coding in php/gtk
for only 1 week now. I tested this with a more complex GUI which
contains a notebook which contains several GtkOptionMenu's and
it works well.
Example
-------
<?php
dl('php_gtk.' . (strstr(PHP_OS, 'WIN') ? 'dll' : 'so'));
//
// Generates a "GtkMenu" and connects it to a given "GtkOptionMenu"
// Sets the handler function on "activate" event
//
// Parameters
//
// optmenu gtkOptionmenu Widget// option_set Array containing the options as strings // optmenuname Index to the global array containing all option names
// func function to connect "activate" event to.// function GenOptMenu( &$optmenu, $option_set, $optmenuname, $func ) { $menu = &new GtkMenu(); $i = 0; foreach( $option_set as $option ) {
$menuitems[$i] = &new GtkMenuItem( $option );
$menuitems[$i]->connect('activate', $func, $option_set, $optmenuname, $i);
$menu->append($menuitems[$i]);
$i++;
}
// Connect Menu to OptionMenu Widget
$optmenu->set_menu( $menu );
$menu->show_all();
}
// Returns the OptionItem index
function GetOption( $widget, &$option_set, $optmenuname, $i )
{
global $menu_arr;
echo ":".$optmenuname.": Selected option is ".$option_set[$i]."\n";
$menu_arr[$optmenuname]=$i;
}
// Setup main windows
$window = &new GtkWindow();
$window->set_position(GTK_WIN_POS_CENTER);
$window->connect_object('destroy', array('gtk', 'main_quit'));
$window2 = &new GtkWindow();
$window2->set_position(GTK_WIN_POS_MOUSE);
$window2->connect_object('destroy', array('gtk', 'main_quit'));
// Start MenuItem creation
$menu_arr = array( 'filemenu' => 0, 'carmenu' => 0 );
// Create option menu and option array
$omenu = &new GtkOptionMenu();
$cmenu = &new GtkOptionMenu();
// Fileoption menu array
$filemenulist = array('File', 'Edit', 'Save', 'Exit');
// create menuitems and set callbacks for FileMenu
GenOptMenu( $omenu, $filemenulist, 'filemenu', 'GetOption' );
// Fileoption menu array
$carmenulist = array('Dodge', 'BMW', 'Chrysler', 'Porsche');
// create menuitems and set callbacks for CarMenu
GenOptMenu( $cmenu, $carmenulist, 'carmenu', 'GetOption' );
// End Menuitem creation
$window->add($omenu);
$window2->add($cmenu);
$window->show_all();
$window2->show_all();
gtk::main();
print "Last selected options had been\nFileMenu: $menu_arr[filemenu]\nCarMenu: $menu_arr[carmenu]\n";
?>