Sample Code 427: How to set up check items in menu?
Written by kksou   
Wednesday, 30 January 2008
Problem

You want to set up check items in menus as shown below:

How to set up check items in menu?


Solution

Sample Code
1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
21   
22   
23   
24   
25   
27   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
44   
45   
46   
50   
51   
52   
53   
54   
55   
56   
57   
59   
60   
61   
62   
63   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
75   
76   
77   
78   
79   
80   
81   
84   
86   
87   
88   
89   
90   
91   
93   
94   
95   
96   
102   
103   
104   
105   
109   
110   
111   
113   
114   
115   
116   
117   
118   
119   
120   
121   
122   
123   
124   
126   
127   
128   
129   
130   
131   
132   
133   
134   
140   
141   
143   
145   
146   
147   
148   
149   
150   
152   
159   
160   
161   
163   
164   
168   
169   
170   
171   
172   
173   
175   
178   
179   
185   
186   
187   
188   
189   
190   
191   
192   
193   
<?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());
$accel_group = new GtkAccelGroup();
$window->add_accel_group($accel_group);

// define menu definition
$menu_definition = array(
    '_File' => array('_New|N', '_Open|O', '_Close|C', '<hr>', '_Save|S', 'Save _As','<hr>', 'E_xit'),
    '_Edit' => array('Cu_t|X', '_Copy|C', '_Paste|V', '<hr>', 'Select _All|A', '<hr>', '_Undo|Z','_Redo|Y'),
    '_Test' => array('Test_1|1', 'Test_2|2', 'Test_3|3', '<hr>',
                array('Selection 1', 'Selection 2', 'Selection 3'), '<hr>',
                '<checkitem>Test_4|4', '<checkitem>Test_5|5', // note 1
                '<checkitem>Test_6|6') // note 1
);
$menu = new Menu($vbox, $menu_definition, $accel_group);

// display title
$title = new GtkLabel("Set up check items in menu");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$vbox->pack_start($title);

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

// class Menu
class Menu {
    var $prev_keyval = 0;
    var $prev_state = 0;
    var $prev_keypress = '';
    function Menu($vbox, $menu_definition, $accel_group) {
        $this->menu_definition = $menu_definition;
        $menubar = new GtkMenuBar();
        $vbox->pack_start($menubar, 0, 0);
        foreach($menu_definition as $toplevel => $sublevels) {
            $top_menu = new GtkMenuItem($toplevel);
            $menubar->append($top_menu);
            $menu = new GtkMenu();
            $top_menu->set_submenu($menu);

            // let's ask php-gtk to tell us when user press the 2nd Alt key
            $menu->connect('key-press-event', array(&$this, 'on_menu_keypress'), $toplevel);

            foreach($sublevels as $submenu) {
                if (strpos("$submenu", '|') === false) {
                    $accel_key = '';
                } else {
                    list($submenu, $accel_key) = explode('|', $submenu);
                }

                if (is_array($submenu)) { // set up radio menus
                    $i=0;
                    $radio[0] = null;
                    foreach($submenu as $radio_item) {
                        $radio[$i] = new GtkRadioMenuItem($radio[0], $radio_item);
                        $radio[$i]->connect('toggled', array(&$this, "on_toggle"));
                        $menu->append($radio[$i]);
                        ++$i;
                    }
                    $radio[0]->set_active(1); // select the first item
                } else {
                    if ($submenu=='<hr>') {
                        $menu->append(new GtkSeparatorMenuItem());
                    } else {
                        $submenu2 = str_replace('_', '', $submenu);
                        $submenu2 = str_replace(' ', '_', $submenu2);
                        $stock_image_name = 'Gtk::STOCK_'.strtoupper($submenu2);
                        if (defined($stock_image_name)) {
                            $menu_item = new GtkImageMenuItem(constant($stock_image_name));
                        } else {
                            if (preg_match("/^<checkitem>(.*)/", $submenu, $matches)) { // note 2
                                $menu_item = new GtkCheckMenuItem($matches[1]); // note 3
                                $menu_item->connect('toggled', array(&$this, "on_toggle")); // note 4
                            } else {
                                $menu_item = new GtkMenuItem($submenu);
                            }
                        }
                        if ($accel_key!='') {
                            $menu_item->add_accelerator("activate",
                                $accel_group, ord($accel_key), Gdk::CONTROL_MASK, 1);
                        }

                        $menu->append($menu_item);
                        $menu_item->connect('activate', array(&$this, 'on_menu_select'));
                        $this->menuitem[$toplevel][$submenu] = $menu_item;
                    }
                }
            }
        }
    }

    // process radio menu selection
    function on_toggle($radio) { // note 4
        $label = $radio->child->get_label();
        $active = $radio->get_active();
        if ($active) echo("radio/check menu selected: $label\n");
    }

    // process menu item selection
    function on_menu_select($menu_item) {
        $item = $menu_item->child->get_label();
        echo "menu selected: $item\n";
        if ($item=='E_xit') Gtk::main_quit();
    }

    // processing of menu keypress
    function on_menu_keypress($menu, $event, $toplevel) {
        if (!$event->state & Gdk::MOD1_MASK) return false;

        
// get the ascii equivalent of the keypress
        $keypress = '';
        if ($event->keyval<255) {
            $keypress = chr($event->keyval); // ascii equivalent
            $keypress = strtolower($keypress); // convert to lowercase
        }

        $match = 0; 
// flag to see if there's a match
        foreach($this->menu_definition[$toplevel] as $submenu) {
            if (!preg_match("/.*_([a-zA-Z0-9]).*/", "$submenu", $matches)) continue;
            $key2 = strtolower($matches[1]); 
// convert to lowercase
            if ($keypress==$key2) { // check if there's a match
                if (strpos("$submenu", '|') === false) {
                    $accel_key = '';
                } else {
                    list($submenu, $accel_key) = explode('|', $submenu); 
// remove any accelerator
                }
                $menuitem = $this->menuitem[$toplevel][$submenu]; 
// get the ID
                $menuitem->activate(); // simulate a click
                $menu->popdown(); // close the toplevel menu
                $match = 1; // set the match flag
                break;
            }
        }
        return $match; 
// return control back to php-gtk
    }
}


?>
Explanation

The above code is based on that of How to set up menu and radio menu - Part 4 - allow Alt F Alt N?

What's new here:

  1. Note how we define check items in the menu definitions.
  2. Check whether this is a check item.
  3. Create the check menu item.
  4. Register the toggled signal on the check menu item.
  5. Toggle the check menu item. Note that we make use of the same signal hanlder as that of the radio menu items.

Related Links
 
< Prev   Next >

Latest Sample Codes

Sample Code 535: How to display a 2D array in GtkTreeView - Part 4 - format cell content - Method 2?
User rating:    0.0   (from 0 users)

This is an extension of the sample code How to display a 2D array in GtkTreeView - Part 3 - with alternate row colors - Method 2?

Using Elizabeth's method, I'll show you that in addition to specifying the background color in the tree store or list store, you can also store the formatted value of a column in another column as shown below.

Note that the output is the same as that of How to display a 2D array in GtkTreeView - Part 4 - format cell content?, but without the use of the custom display function.

In this example, column 3 contains the original floating point number. This column is not displayed in the treeview. Column 4 contains the corresponding values of column 3 formatted with 2 decimal places. This is the column that gets displayed in the treeview.

How to display a 2D array in GtkTreeView - Part 4 - format cell content - Method 2?

 
Sample Code 534: How to display a 2D array in GtkTreeView - Part 8 - with alternate fg and bg row colors - Method 4 - add_attribute?

This is another PHP-GTK2 tips and techniques contributed by Dysmas de Lassus of France.

In How to display a 2D array in GtkTreeView - Part 8 - with alternate fg and bg row colors - Method 2? we use the Elizabeth's method to specify the colums of the foreground and background colors in the list store at the point when we set up the GtkTreeViewColumn.

Dysmas pointed out that you can delay the setting of columns for these attributes to a later point in time in your program with the use of the method GtkCellView::add_attributes() as shown below. Both will produce the same effect.

How to display a 2D array in GtkTreeView - Part 8 - with alternate fg and bg row colors - Method 4 - add_attribute?

 
Sample Code 533: How to display a 2D array in GtkTreeView - Part 8 - with alternate fg and bg row colors - Method 3 - set_attributes?

This is another PHP-GTK2 tips and techniques contributed by Dysmas de Lassus of France.

In How to display a 2D array in GtkTreeView - Part 8 - with alternate fg and bg row colors - Method 2? we use the Elizabeth's method to specify the colums of the foreground and background colors in the list store at the point when we set up the GtkTreeViewColumn.

Dysmas pointed out that you can delay the setting of columns for these attributes to a later point in time in your program with the use of the method GtkCellView::set_attributes() as shown below. Both will produce the same effect.

How to display a 2D array in GtkTreeView - Part 8 - with alternate fg and bg row colors - Method 3 - set_attributes?

 
Sample Code 532: How to display a 2D array in GtkTreeView - Part 8 - with alternate fg and bg row colors - Method 2?

This is an extension of the sample code How to display a 2D array in GtkTreeView - Part 3 - with alternate row colors - Method 2?

Using Elizabeth's method, I'll show you that in addition to specifying the background color in the tree store or list store, you can also specify the foreground color as shown below:

How to display a 2D array in GtkTreeView - Part 8 - with alternate fg and bg row colors - Method 2?

 
Sample Code 531: How to display a 2D array in GtkTreeView - Part 3 - with alternate row colors - Method 2?
User rating:    5.0   (from 1 user)

Elizabeth Smith wrote a very interesting blog article on October 6, 2008 titled "Treeviews and Cell Renderer Properties - Practical PHP-GTK". I only saw it today, and found this very interesting and useful.

Although she has only showed an example of how to change the background color of a single cell in a treeview, the method can be applied to a lot of situations in treeview. Instead of using a custom dispay function, you can actually do it right inside a tree store or list store.

However, this does not mean that you can now throw the custom display function out of the window. You will find that in many situations, you will still need to use the custom display function.

In order to understand when to use the custom display function, and when you can use Elizabeth's method, I think the best way is to let you see how the two methods are used in practice.

In the next couple of sample codes, you will see how I transform some of the treeview sample codes I've written in the past and achieve the same result using Elizabeth's method. I would strongly encourage you to compare the two sample codes so that you understand the differences and similarities between the two methods.

The first example I'll show is the display of a list view in alternate row colors.

First take a look at the sample code using the custom display function: How to display a 2D array in GtkTreeView - Part 3 - with alternate row colors?

Then study the sample code below that uses Elizabeth's method. Both will produce the sample output as shown below:

How to display a 2D array in GtkTreeView - Part 3 - with alternate row colors - Method 2?

 
Sample Code 530: How to load all widgets defined in a glade file all at once into an array?
User rating:    4.5   (from 3 users)

This is another PHP-GTK/Glade trick contributed by Dysmas de Lassus of France.

When you're using PHP-GTK with Glade, normally we use the following to load the pointer to the widget 'entry1' defined in the glade file:

$glade->get_widget('entry1');

Dysmas found it "bored" to have to load each widget one by one.

So he found a way that allows him to load all the widgets defined in a glade file all at once into an array using just two lines as shown below:

How to load all widgets defined in a glade file all at once into an array?

 
Sample Code 529: How to setup combobox with multi level options - Part 2 - display as hierarchical sub menus?

This is another undocumented PHP-GTK2 feature discovered by Dysmas de Lassus of France.

In the article How to setup combobox with multi level options - Part 1 - display as tree structure? Dysmas showed us how to display a multi-level options in a combobox using a GtkTreeStore as shown below:

How to setup combobox with multi level options - Part 1 - display as tree structure?

Dysmas found out by adding just one more line, you can display the same multi-level options in the form of a hierarchical sub-menus as shown below:

How to setup combobox with multi level options - Part 2 - display as hierarchical sub menus?

 
Sample Code 528: How to setup combobox with multi level options - Part 1 - display as tree structure?

This is contributed by Dysmas de Lassus of France.

In the article How to setup and process GtkComboBox? we use a GtkListStore to store the options.

Dysmas found out that if you use a GtkTreeStore instead of a GtkListStore, you can actually display a multi-level options as shown below. Interesting, huh!

How to setup combobox with multi level options - Part 1 - display as tree structure?

 
Sample Code 527: How to display a tree structure using GtkTreeView?

I've showed you how to dispay a tree structure from an array in the article How to display a tree structure from array? In that example, defining the tree and each node is quite troublesome.

Suppose you would like to be able to define a tree as simple as the following:

Level 1
+ level 1.1
++ level 1.1.1
++ level 1.1.2
++ level 1.1.3
+++ level 1.1.3.1
+ level 1.2
++ level 1.2.1
++ level 1.2.2
+ level 1.3

This example shows you how to process such a tree definition and populate them into a GtkTreeView as shown below.

Note: For a practical use of this tree definition, you may want to refer to How to setup combobox with multi level options - Part 1 - display as tree structure?

How to display a tree structure using GtkTreeView?

 
Sample Code 526: How to set up a simple PING application?
User rating:    5.0   (from 1 user)

This is in response to Vacendak's post titled "Networking Questions".

He would like to see how a PING application is done using PHP-GTK2. So here it is.

This sample code also shows you how you can retrieve the "output" from a command window and pipe it into your php-gtk application as shown below:

How to set up a simple PING application?

 
Sample Code 525: How to unattach a widget from GtkTable or replace a GtkTable cell with another widget - Part 2?

This is in response to Peter's post titled "The opposite to GtkTable::attach()".

He would like to be able to unattach a cell content and replace the cell with some other widgets.

In particular, he has an original widget that spans from columns 14 to 20. He wants to replace this widget with one that spans columns 15 and 16 and a second one that spans columns 18-20. column 17 is to remain empty.

In Part 1, I've showed you how to replace a single cell widget with another single cell widget.

In this Part 2, I will show you how to handle Peter's case with widgets that span across multiple rows and columns. For this case, you cannot use the technique as described in Part 1 because each vbox may now span across different rows and columns.

When you first run the sample code, you will see some of the GtkLabel's spanning across some rows and columns.

How to unattach a widget from GtkTable or replace a GtkTable cell with another widget - Part 2?

When you click the button "Remove first two rows", the GtkLabel's of all the cells in the first two rows will be removed as shown below:

When you click the button "Replace first two rows", the GtkLabel's of the cells in the first two rows will be replaced with some other GtkLabel's that span across different rows and columns as shown below:

 
Sample Code 524: How to unattach a widget from GtkTable or replace a GtkTable cell with another widget - Part 1?

This is in response to Peter's post titled "The opposite to GtkTable::attach()".

He would like to be able to unattach a cell content and replace the cell with some other widgets.

In particular, he has an original widget that spans from columns 14 to 20. He wants to replace this widget with one that spans columns 15 and 16 and a second one that spans columns 18-20. column 17 is to remain empty.

In this Part 1, I will first show you how to replace a single cell widget with another single cell widget. We will deal specifically with Peter's case in Part 2.

When you first run the sample code, each cell of the GtkTable contains a GtkLabel.

How to unattach a widget from GtkTable or replace a GtkTable cell with another widget - Part 1?

When you click the button "Remove first two rows", the GtkLabel's of all the cells in the first two rows will be removed as shown below:

When you click the button "Replace first two rows", the GtkLabel's of all the cells in the first two rows will be replaced with GtkEtnry's as shown below:

 
Sample Code 523: How to extend GtkSpinButton?
User rating:    5.0   (from 1 user)

This is in response to Peter's post titled "extending GtkSpinButton".

He would like to extend the GtkSpinButton class as shown below:

How to extend GtkSpinButton?

 
Sample Code 522: How to grab focus on GtkEntry in a GtkNotebook page - Part 1 - highlight all text on focus?

This is in response to Tropico's post titled "Finding first gtkentry widget into GtkNotebook page".

He would like to grab_focus on the first GtkEntry widget of one page of the notebook when the page becomes active as shown below:

How to grab focus on GtkEntry in a GtkNotebook page - Part 1 - highlight all text on focus?

 
Sample Code 521: How to create a popup menu when user right click on a GtkToolbar?
User rating:    4.0   (from 1 user)

This is in response to Vadi's post titled "How to make a popup menu on a GtkToolbar".

He would like to create a popup menu when a user right-click on a GtkToolbar as shown below:

How to create a popup menu when user right click on a GtkToolbar?

 
Sample Code 520: How to get the position of a label or eventbox?

This is in response to Neil's post titled "Get co-ordnates".

He would like to find the position (x,y co-ordinates) of an eventbox, or a label as shown below:

How to get the position of a label or eventbox?

 
Sample Code 519: How to keep track of the image filenames of 12 drag and drop images - Part 5 - display tooltip?

This is in response to Mote's post titled "2 questions about GtkImage".

He has set up an application that allows drag-and-drop of 12 different images. And he would like to be able to retrieve the URI (or filenames) of these 12 images at some later time for processing.

In Part 1, you can only drag and drop one image at a time.

In Part 2, you can drag and drop multiple images at one go.

In Part 3, you can delete unwanted images with right mouse click.

In Part 4, images will be automatically scaled to 100 by 100 pixels if the image is larger than 100 by 100 pixels.

In this Part 5, I'll show you how to display the image's URI in tooltip when the user hovers its mouse over an image as shown below:

How to keep track of the image filenames of 12 drag and drop images - Part 5 - display tooltip?

 
Sample Code 518: How to keep track of the image filenames of 12 drag and drop images - Part 4 - automatically scale image?

This is in response to Mote's post titled "2 questions about GtkImage".

He has set up an application that allows drag-and-drop of 12 different images. And he would like to be able to retrieve the URI (or filenames) of these 12 images at some later time for processing.

In Part 1, you can only drag and drop one image at a time.

In Part 2, you can drag and drop multiple images at one go.

In Part 3, you can delete unwanted images with right mouse click.

In this Part 4, I'll show you how to automatically scale the image to size 100 by 100 pixels (when the width of height is greater than the 100 x 100) as shown below:

How to keep track of the image filenames of 12 drag and drop images - Part 4 - automatically scale image?

 
Sample Code 517: How to keep track of the image filenames of 12 drag and drop images - Part 3 - delete image?

This is in response to Mote's post titled "2 questions about GtkImage".

He has set up an application that allows drag-and-drop of 12 different images. And he would like to be able to retrieve the URI (or filenames) of these 12 images at some later time for processing.

In Part 1, you can only drag and drop one image at a time.

In Part 2, you can drag and drop multiple images at one go.

In this Part 3, I'll show you how you can delete an image with right mouse click as shown below:

How to keep track of the image filenames of 12 drag and drop images - Part 3 - delete image?

 
Sample Code 516: How to keep track of the image filenames of 12 drag and drop images - Part 2 - multiple images?

This is in response to Mote's post titled "2 questions about GtkImage".

He has set up an application that allows drag-and-drop of 12 different images. And he would like to be able to retrieve the URI (or filenames) of these 12 images at some later time for processing.

In Part 1, you can only drag and drop one image at a time.

In this Part 2, you can now drag and drop multiple files at one go as shown below:

How to keep track of the image filenames of 12 drag and drop images - Part 2 - multiple images?

 
<< Start < Prev 1 Next > End >>

Results 1 - 20 of 20

Latest Blog Articles

Sample PHP-GTK2 + Glade Application - Notepad Application

Wednesday, 24 September 2008

If you're looking for sample applications using PHP-GTK2 + Glade, here's one more which I've just published on devshed.com:

Link: Building Your Own Desktop Notepad Application Using PHP-GTK

This article will show you how easy it is to build a desktop Notepad application using PHP-GTK2 + Glade.

As with many other sample codes on this site, you will have the complete sample code + glade file and detailed explanations of how everything fits together.

 
Compiling standalone PHP-GTK2 applications on windows using PriadoBlender

Monday, 21 July 2008

If you've been wondering how to compile standalone PHP-GTK2 applications on windows using PriadoBlender, I've just written an article explaining the details.

Important Note: For now, PriadoBlender only works with the Gnope version of PHP-GTK (which is running PHP-GTK2 alpha version with PHP v5.1.4 and GTK+ v2.6.9). I'm sure the author of PriadoBlender will upgrade the program soon to work with PHP-GTK v2.0.

 
Setting the background color of GtkButton

Wednesday, 16 July 2008

I've written a sample code that shows how to set the background color of a GtkButton.

Sample Code 23: How to set the background color of GtkButton?

Some of you have written to me that the code does not work on your machine. The fix is as follows:

  1. Assuming you're using the latest php-gtk v2.0.1
  2. Go to [php-gtk root folder]/etc/gtk-2.0 and open the file gtkrc in your favorite editor.
  3. Comment out lines 45, 46 and 47 as follows:


  4. #engine "wimp"

    #{

    #}

  5. Save and close the file.


Now try running the sample code again. Did it work this time?
 
interactive search in GtkTreeView for PHP GTK v2.0

Saturday, 28 June 2008

If you have tried the latest PHP-GTK v2.0 or v2.0.1 on the windows platform, you will know that there seems to be some bugs with the interactive search for GtkTreeview. The interactive search works in PHP-GTK2 alpha and beta versions, but not in v2.0 or v2.0.1.

Carl shared with us a brilliant work-around he has found.

Using his method, here are a couple of complete sample codes that show how to set up interactive search in PHP-GTK v2.0 and v2.0.1.

 
PHP-GTK v2.0.1 with GD2 library

Saturday, 28 June 2008

The latest windows binary of PHP-GTK v.2.0.1 from gtk.php.net does not come with the GD2 library.

If you need to set up the php_gd2.dll, please refer to the following article:

PHP-GTK v2.0.1 with GD2 library

 
<< Start < Prev 1 Next > End >>

Results 1 - 5 of 5

PHP-GTK Tools

PHP-GTK Explorer v1.05

User rating:    4.5   (from 7 users)

Monday, 03 March 2008

Your "secret weapon" for learning, exploring, understanding and mastering PHP-GTK2

The "kksou PHP-GTK Explorer" is a unique tool for learning, exploring, understanding and mastering the PHP-GTK2 classes and methods.

It's like the Dev_Inspector (from gnope.org), but with a lot more functionalities added.

Now works with the new PHP-GTK v2.0 release!

 

PHP-GTK References

Compiling standalone PHP-GTK2 applications on windows using PriadoBlender

User rating:    3.0   (from 1 user)

Monday, 21 July 2008

This article contains step-by-step instructions on how to set up and compile standalone PHP-GTK2 applications into .exe files on windows using PriadoBlender.

Interestingly, installing PriadoBlender and compiling the PHP-GTK2 apps is the easy part. The "tricky" part is how to distribute and run the compiled .exe.

Important Note: For now, PriadoBlender only works with the Gnope version of PHP-GTK (which is running PHP-GTK2 alpha version with PHP v5.1.4 and GTK+ v2.6.9). I'm sure the author of PriadoBlender will upgrade the program soon to work with PHP-GTK v2.0.


 
PHP-GTK v2.0.1 with GD2 library

Friday, 27 June 2008

If you have installed the latest windows binary of PHP-GTK v.2.0.1 from gtk.php.net, for one reason or another, it does not include some of the commonly-used libraries.

One of these is the GD2 library.

Note: For those of you who are not familiar with GD2, please refer to the PHP documentation at: http://www.php.net/gd.


 
Barcode printing on receipt printer using the ESC/POS Commands

User rating:    3.0   (from 1 user)

Wednesday, 16 April 2008

This article shows you the ESC/POS commands for the printing of barcodes on standard Epson receipt printer such as the Epson TM-T88III.

The ESC/POS commands are a set of proprietary POS printer command system developed by EPSON.

If you have a different printer, please refer to the corresponding codes in your printer manual. But Epson is pretty much the industry standard for receipt printer and most receipt printers support these ESC/POS commands. You can just try the commands below. Most likely it will work on your receipt printer.

 

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