PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 436: How to set up toolbar using GtkAction - Part 1?
Written by kksou   
Wednesday, 20 February 2008
Problem

You have seen how to set up toolbars in the article How to set up toolbar?

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

However, try to compare the differences between using the two methods, and you will understand why they created this interesting widget GtkAction.

How to set up toolbar using GtkAction - Part 1?


Solution
  • For each toolbutton, first create a GtkAction, specifying the stock image if there's any.
  • Use the method GtkAction::create_tool_item() to automatically create a toolbar item from the action.

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   
<?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
$toolbar_definition = array('_New', 'Open', 'Save', '<hr>', // note 1
    'Cut', 'Copy', 'Paste', '<hr>', 
    'Undo','Redo'); 
setup_toolbar($vbox, $toolbar_definition);

// display title
$title = new GtkLabel("Set up Toolbar 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_toolbar($vbox, $toolbar_definition) { // note 1
    $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 {
            $item2 = str_replace('_', '', $item);
            $stock_image_name = 'Gtk::STOCK_'.strtoupper($item2);
            $stock_image = '';
            if (defined($stock_image_name))
                $stock_image = constant($stock_image_name); // note 3
  • 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 toolbar definition.
  2. Create a new toolbar.
  3. Get the stock image name from toolbar definition.
  4. Create a new GtkAction.
  5. Create a new toolitem from the action.
  6. Register for the 'activate' signal on the action.
  7. Insert the toolitem into the toolbar.
  8. Process the button click on the toolbar.

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