PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 521: How to create a popup menu when user right click on a GtkToolbar?
Written by kksou   
Wednesday, 13 August 2008
Problem

This is in response to Vadi's post titled "How to make a popup menu on a GtkToolbar".

He would like to create a popup menu when a user right-click on a GtkToolbar as shown below:

How to create a popup menu when user right click on a GtkToolbar?


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   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(400, 200);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// define menu definition
$toolbar_definition = array('New', 'Open', 'Save', '<hr>',
    'Cut', 'Copy', 'Paste', '<hr>',
    'Undo','Redo');
setup_toolbar($vbox, $toolbar_definition);

// display title
$title = new GtkLabel("Set up popup menu with right mouse click in toolbar");
$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 toolbar
function setup_toolbar($vbox, $toolbar_definition) {
    $toolbar = new GtkToolBar(); // note 2
    $vbox->pack_start($toolbar, 0, 0);
    foreach($toolbar_definition as $item) {
        if ($item=='<hr>') {
            $toolbar->insert(new GtkSeparatorToolItem(), -1);
        } else {
            $stock_image_name = 'Gtk::STOCK_'.strtoupper($item);
            if (defined($stock_image_name)) {
                $toolbar_item = GtkToolButton::new_from_stock(
                    constant($stock_image_name));
                $toolbar->insert($toolbar_item, -1);
                $toolbar_item->connect('clicked', 'on_toolbar_button', $item);
                $toolbar_item->child->connect('button-press-event', // note 1
                    'on_button', $item, $toolbar_item); 
            }
        }
    }
}

// process toolbar
function on_toolbar_button($button, $item) {
    echo "toolbar clicked: $item\n";
}

function on_button($button, $event, $item, $toolitem) {
    if ($event->button==1) {
        return false;
    }
    if ($event->button==2) return true; // do nothing
    if ($event->button==3) { // n4 note 2
         $prefix = $item;
        // displays the popup menu
        $menu_definition = array(
            "$item - menu item 1",
            "$item - menu item 2",
            "$item - menu item 3",
            '<hr>',
            "$item - menu item 4",
            "$item - menu item 5",
            "$item - menu item 6"
            );
  • 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 code above is based on How to set up toolbar?

What's new here:

  1. Register for the signal button-press-event.
  2. Check for right mouse click.
  3. Process the selected menu item of the popup menu.

Related Links

User reviews   Average user ratings:    4.0   (from 1 user)
  1. Mr Programmer
    September 23, 2008 4:07pm

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

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