|
Problem You have set up menus (with radio menus) in Part 1.

Now you would like to add in stock images to the menu items as shown below:

Solution
Sample Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| <?php $window = new GtkWindow(); $window->set_size_request(400, 150); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox());
// define menu definition
$menu_definition = array( '_File' => array('_New', '_Open', '_Close', '<hr>', '_Save', 'Save _As','<hr>', '_Quit'), '_Edit' => array('Cut', 'Copy', '_Paste', '<hr>', 'Select All', '<hr>', '_Undo','_Redo'), '_Test' => array('Test_1', 'Test_2', 'Test_3', '<hr>', array('Selection 1', 'Selection 2', 'Selection 3'), '<hr>', 'Test_4') ); setup_menu($vbox, $menu_definition);
// display title
$title = new GtkLabel("Menu and RadioMenuItem - Part 2\n Add Stock Images"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $vbox->pack_start($title); $vbox->pack_start(new GtkLabel(''));
$window->show_all(); Gtk::main();
// setup menu
function setup_menu($vbox, $menus) { $menubar = new GtkMenuBar(); $vbox->pack_start($menubar, 0, 0); foreach($menus as $toplevel => $sublevels) { $menubar->append($top_menu = new GtkMenuItem($toplevel)); $menu = new GtkMenu(); $top_menu->set_submenu($menu); foreach($sublevels as $submenu) { if (is_array($submenu)) { // set up radio menus
$i=0; $radio[0] = null; foreach($submenu as $radio_item) { $radio[$i] = new GtkRadioMenuItem($radio[0], $radio_item); $radio[$i]->connect('toggled', "on_toggle"); $menu->append($radio[$i]); ++$i; } $radio[0]->set_active(1); // select the first item
} else { if ($submenu=='<hr>') { $menu->append(new GtkSeparatorMenuItem()); } else { $submenu2 = str_replace('_', '', $submenu); // note 1
$submenu2 = str_replace(' ', '_', $submenu2); // note 1
$stock_image_name = 'Gtk::STOCK_'.strtoupper($submenu2); // note 1
if (defined($stock_image_name)) { $menu_item = new GtkImageMenuItem(constant($stock_image_name)); // note 2
} else { $menu_item = new GtkMenuItem($submenu);
|
- Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
- Registration is free and immediate.
- Have some doubt about the registration? Please read this forum article.
Explanation
- You can view the entire list of built-in stock images at here. What we are doing here is to check if the menu name has a corresponding built-in stock images. First we need to remove all "_", then replace all <space> with "_", then add "Gtk::STOCK_" in front of the string.
- Then we check if such constant exists. If yes, we construct the menu using GtkImagemenuitem. If no, we use the standard GtkMenuitem
Related Links
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |