PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 437: How to set up toolbar using GtkAction - Part 2 - add accelerators?
Written by kksou   
Wednesday, 20 February 2008
Problem

You have learned how to create toolbars using GtkAction in the previous article

Suppose now you want to add accelerators to the toolbar so that the users can press Ctrl-N for New, Ctrl-O for Open, etc. as shown below.

The essense to get the accelerator working for GtkAction is just 4 lines. But it took me two whole nights of trials and errors to get it working! Take a look at the code below, and you'll understand why. There're just too little documentation on the Internet related to GtkAction...

How to set up toolbar using GtkAction - Part 2 - add accelerators?


Solution

You'll be surprised that all these methods are available way back in alpha version!


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   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(400, 175);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());
$accel_group = new GtkAccelGroup(); // note 1
$window->add_accel_group($accel_group); // note 1
$actiongrp = new GtkActionGroup('toolbar'); // note 2

// define menu definition
$toolbar_definition = array('New|N', 'Open|O', 'Save|S', '<hr>',
    'Cut|X', 'Copy|C', 'Paste|V', '<hr>',
    'Undo|Z','Redo|Y');
setup_toolbar($vbox, $toolbar_definition);

// display title
$title = new GtkLabel("Set up Toolbar using GtkAction\n".
"      Part 2 - Add Accelerators");
$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(''));
$vbox->pack_start(new GtkLabel('You can press Ctrl-N (New), Ctrl-O (Open), etc.', 0));
$vbox->pack_start(new GtkLabel('to activate the toolbar items.'), 0);
$vbox->pack_start(new GtkLabel(''));

$window->show_all();
Gtk::main();

function setup_toolbar($vbox, $toolbar_definition) {
    $toolbar = new GtkToolBar();
    $vbox->pack_start($toolbar, 0, 0);
    foreach($toolbar_definition as $item) {
        if (strpos("$item", '|') === false) {
            $accel_key = '';
        } else {
            list($item, $accel_key) = explode('|', $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);

            $action = new GtkAction($item, $item, '', $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. Create a new accelerator and attach it to the window.
  2. Create an action group.
  3. Define the accelerator key. Here we use the <control> key. You can change this to use 'Alt', or 'Ctrl-Alt'.
  4. Add the action to the action group, specifying the accelerator key at the same time.
  5. Specify the accelerator group.
  6. Connect the accelerator with the action.

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