|
Problem Suppose you would like to set up menu with images (e.g. standard .gif, .jpg or .png image files) as menu items. In Part 1, each menu item comprises an image + a label. In this Part 2, each menu item contains only an image (without the label) as shown below

Solution
Sample Code Note: The following image files are required by the sample code below. Please save a copy of the image files and put them in the same directory where you store the sample code.
 | square_yellow.jpg |
 | square_green.jpg |
 | square_blue.jpg |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| <?php $window = new GtkWindow(); $window->set_title($argv[0]); $window->set_size_request(400, 150); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox()); $accel_group = new GtkAccelGroup(); // create a new accelerator
$window->add_accel_group($accel_group); // attach it to the window
// define menu definition
$menu_definition = array( '_File' => array('_New|N', '_Open|O', '_Close|C', '<hr>', '_Save|S', 'Save _As','<hr>', '_Quit'), '_Edit' => array('Cut|X', 'Copy|C', '_Paste|V', '<hr>', 'Select All|A', '<hr>', '_Undo|Z'), '_Test' => array('Test_1|1', 'Test_2|2', 'Test_3|3', '<hr>', array('Selection 1', 'Selection 2', 'Selection 3'), '<hr>', 'Test_4|4'), '_Background' => array('Yellow|Y', 'Green|G', 'Blue|B') );
$image_files = array('Yellow' => 'square_yellow.jpg', 'Green' => 'square_green.jpg', 'Blue' => 'square_blue.jpg'); setup_menu($vbox, $menu_definition);
// display title
$title = new GtkLabel("Menu with images as menu items - Part 2 (image only)"); $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) { global $accel_group; $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 (strpos("$submenu", '|') === false) { $accel_key = ''; } else { list($submenu, $accel_key) = explode('|', $submenu); }
if (is_array($submenu)) { $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); $submenu2 = str_replace(' ', '_', $submenu2); $stock_image_name = 'Gtk::STOCK_'.strtoupper($submenu2); if (defined($stock_image_name)) { $menu_item = new GtkImageMenuItem( constant($stock_image_name)); } else { if ($toplevel == '_Background') { global $image_files; $image_file = $image_files[$submenu]; $menu_item = setup_image_menuitem( $submenu, $image_file); // note 1
} else { $menu_item = new GtkMenuItem($submenu); } } if ($accel_key!='') { $menu_item->add_accelerator("activate", $accel_group, ord($accel_key), Gdk::CONTROL_MASK, 1); }
$menu->append($menu_item); $menu_item->connect('activate', 'on_menu_select');
|
- 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 The above makes use of the code from How to set up menu with images as menu items - Part 1?
What's new here:
- Set up image menuitems.
- First remove the default GtkLabel.
- Then attach a GtkHBox.
- Load the image and stuff it into GtkHBox.
- Store the label corresponding to the image.
- Take note of how we retrieve back the label of the image menuitem.
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. |