PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 428: How to set up pulldown menu in toolbar?
Written by kksou   
Thursday, 31 January 2008
Problem

Suppose you want to set up a pull down or drop down menu in the toolbar (e.g. for selection of fonts or font size) as shown below:

How to set up pulldown menu in toolbar?


Solution
  • Pull down menus are created in the toolbar using GtkMenuToolButton.
  • However, note that the pulldown menu is not the same as a combobox. Rather, it's implemented using a popup GtkMenu.

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   
<?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
$toolbar_definition = array('New', 'Open', 'Save', '<hr>', // note 1
); 
setup_toolbar($vbox, $toolbar_definition);

// display title
$title = new GtkLabel("Set up \"pulldown\" menu 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();

// process toolbar
function on_toolbar_button($menuitem, $item='') {
    $menuitem_type = $menuitem->get_name();
    if ($menuitem->get_name() == 'GtkMenuToolButton') {
        global $fontsize;
        echo "fontsize selected: ".$fontsize->get_text()."\n";
    } else {
        echo "toolbar clicked: $item\n";
    }
}

function setup_popup_menu($menu_definition) {
    $menu = new GtkMenu();
    foreach($menu_definition as $menuitem_definition) {
        if ($menuitem_definition=='<hr>') {
            $menu->append(new GtkSeparatorMenuItem());
        } else {
            $menu_item = new GtkMenuItem($menuitem_definition);
            $menu->append($menu_item);
            $menu_item->connect('activate', 'on_popup_menu_select');
        }
    }
    $menu->show_all();
    return $menu;
}

// process popup menu item selection
function on_popup_menu_select($menu_item) {
    $item = $menu_item->child->get_label();
    echo "popup menu selected: $item\n";
    global $fontsize;
    $fontsize->set_text($item);
}

// setup toolbar
function setup_toolbar($vbox, $toolbar_definition) {
    $toolbar = new GtkToolBar();
    $toolbar->set_property('toolbar-style', Gtk::TOOLBAR_ICONS);
    $vbox->pack_start($toolbar, 0, 0);
    foreach($toolbar_definition as $item) {
  • 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 definitions for the standard toolbar items.
  2. Create a new GtkEntry that will hold the selected font size.
  3. Create the menutoolbutton. Note that you can pass in any standard GtkWidget for the first parameter. In this example, we used a GtkEntry.
  4. Add this to the toolbar.
  5. Set up the "pulldown" menu (it's actually a popup menu).
  6. Binds the popup menu to this menutoolbutton.

Note

You may want to compare this with the setting up of GtkMenu - How to set up menu and radio menu - Part 1?. The two are very similar.


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.

 
< Prev   Next >

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