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
 

Add comment


Security code
Refresh

< Prev   Next >

Latest Blog Articles

googleMaps plugin for Joomla 1.6

Wednesday, 23 February 2011

For the googleMaps plugin users, the native version for Joomla 1.6 is now available!

Download: googleMaps v1.6

 
DirectPHP for Joomla 1.6

Monday, 21 February 2011

For the DirectPHP users, the native version for Joomla 1.6 is now available!

Download : DirectPHP v1.6

 
Joomla Extensions for Joomla 1.6

Monday, 14 February 2011

I've published more than 40 Joomla Extensions in joomla.org.

Recently I've received a couple of requests to upgrade these plugins/modules to Joomla 1.6.

Some of these are written 3 years ago. I'm not sure if any of you are still using them at all.

So send me a tweet @kksou if you want any of them to be converted to Joomla 1.6. Drop me a note to let me know that they are useful. I don't have the time to convert all the extensions. There has to be some priorities somewhere. So I will start with the plugin/module with more than a hundred requests for conversion.

 

Blog - Forum - Privacy Policy - Contact Us
Links - Classes - Social Business - BPM - Web - General
Copyright © 2006-2013. kksou.com. All Rights Reserved