PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 441: How to set up menu using GtkAction - Part 1?
Written by kksou   
Monday, 25 February 2008
Problem

You have seen how to set up menu in the article How to set up menu and radio menu - Part 1?

You can also create menus using another way: using the GtkAction. The result is the same – you'll get the same menus as shown below:

How to set up menu using GtkAction - Part 1?


Solution

Compare the sample code below with that of How to set up toolbar using GtkAction - Part 1?

You will find that setting up menus and setting up toolbars using GtkAction is almost identical, except for the following:


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   
<?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());

// define menu definition
// define menu definition
$menu_definition = array( // note 1
    '_File' => array('_New', '_Open', '_Close', '<hr>',
                    '_Save', 'Save _As','<hr>', '_Quit'),
    '_Edit' => array('Cut', 'Copy', '_Paste', '<hr>',
                    'Select All', '<hr>', '_Undo','_Redo'),
);
setup_menu($vbox, $menu_definition);

// display title
$title = new GtkLabel("Set up Menu using GtkAction - Part 1");
$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();

function setup_menu($vbox, $menu_definition) { // note 1
    $menubar = new GtkMenuBar(); // note 2
    $vbox->pack_start($menubar, 0, 0);

    foreach($menu_definition as $toplevel => $sublevels) {
        $menubar->append($top_menu = new GtkMenuItem($toplevel));
        $menu = new GtkMenu();
        $top_menu->set_submenu($menu);

        foreach($sublevels as $item) {

            if ($item=='<hr>') {
                $menu->append(new GtkSeparatorMenuItem());
            } else {
                $item2 = str_replace('_', '', $item);
                $item2 = str_replace(' ', '_', $item2);
                $stock_image_name = 'Gtk::STOCK_'.strtoupper($item2);
                $stock_image = '';
  • 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
  1. This is the menu definition.
  2. Create a new toolbar.
  3. Get the stock image name from menu definition.
  4. Create a new GtkAction.
  5. Create a new menu item from the action.
  6. Register for the 'activate' signal on the action.
  7. Insert the menuitem into the menu.
  8. Process selection of menuitem.

Related Links
 

Add comment


Security code
Refresh

< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2012. kksou.com. All Rights Reserved