432. How to setup and process a GtkButton using GtkAction - Part 1?

Problem

GtkAction is an interest widget that is very little explained in the manual and on the web. In some occasions, you'll find this an extremely useful widget.

Most people used this together with GtkUIManager to manage menus and toolbar items. But it can actually be used with almost any standard GtkWidget such as GtkLabel and GtkButton.

I will illustrate in the next couple of articles on the use of GtkAction.

Let's start with this simple example: to set up and process a GtkButton using GtkAction as shown below.

Although this is a simple example, it will lay the foundation for understanding the subsequent examples on GtkAction and GtkActionGroup.

How to setup and process a GtkButton using GtkAction - Part 1?


Solution

  • First take a look at the PHP-GTK2 manual on GtkAction.
  • Create and set up a standard GtkButton.
  • Now create a GtkAction.
  • Binds the GtkButton to the action using GtkAction::connect_proxy().

Sample Code

1   
2   
3   
4   
5   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
33   
34   
36   
37   
38   
39   
40   
41   
46   
47   
48   
49   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 120);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Set up a button using GtkAction - Part 1");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

$vbox->pack_start($hbox = new GtkHBox(), 0);
$button = setup_GtkAction_button('Play');
$button->set_size_request(80, -1);
$hbox->pack_start(new GtkLabel('This button is set up using GtkAction: '), 0);
$hbox->pack_start($button, 0);

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

function setup_GtkAction_button($button_label) {

    $button = new GtkButton($button_label); // note 1

    $action = new GtkAction($button_label, '_'.$button_label, '', ''); // note 2
    $action->connect_proxy($button); // note 3
    $action->connect('activate', "on_click", $button_label); // note 4
    return $button;
}

function on_click($button, $label) {
    echo "button clicked: $label\n";
}

?>

Output

As shown above.
 

Explanation

  1. Create a standard GtkButton.
  2. Create a new action
  3. Binds the button to the action.
  4. Note that instead of the 'clicked' signal for a standard GtkButton, we use the 'activate' signal on the GtkAction.

Related Links

Add comment


Security code
Refresh