112. How to allow only numbers in GtkEntry - Part 1?

Problem

Suppose you would like to ensure that only numbers, i.e. 0 to 9, are allowed to be entered in a GtkEntry field as shown below:

How to allow only numbers in GtkEntry - Part 1?


Solution

There are many ways to accomplish this. In this example, we will make use of a new function emit_stop_by_name() recently added in by Andrei.

  • Use insert-text to let us know everytime the user enters a text.
  • If it's a number, let the default handler displays the number.
  • If it's not a number, use emit_stop_by_name() to prevent the text from appearing.
Important Note

To be able to use this function, you need to download the latest copy of php-gtk2 from the cvs repository (gtk.php.net/download.php) and do a recompilation. For linux users, this should take less than 10 minutes to recompile. For windows users using Gnope Installer, we have to wait for the php-gtk2 core team to give us an update of the php-gtk2.dll.

By the way, there are some people telling me that they have problem logging on to the cvs server from countries outside US. If you encounter the same problem, you may drop me a note via the feedback, and I can email you a copy of the php-gtk2 source code on the cvs server.


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   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 240);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Allow only numbers in GtkEntry - 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("uses emit_stop_by_name()"), 0, 0);

$item_number = new GtkEntry();
$item_number->connect('insert-text', 'on_insert'); // note 1

$hbox = new GtkHBox();
$hbox->pack_start(new GtkLabel('Item Number: '), 0, 0);
$hbox->pack_start($item_number, 0, 0);
$vbox->pack_start($hbox);
$vbox->pack_start(new GtkLabel("Note: item numbers should contain only numbers 0 to 9"), 0, 0);

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

function on_insert($entry, $text, $len, $position) {
    $text = substr($text, 0, $len); // this is the new text inserted
    if (!preg_match("/[0-9]/", $text)) { // is this a number?
        echo "not number: $text!\n";
        $entry->emit_stop_by_name('insert-text'); // note 2
        return true; // tell php-gtk2 that we're done
    }
    return false; // yes, let the default handler display the number
}

?>

Output

As shown above.

 

Explanation

  1. Notify us when user enters any text.
  2. Use emit_stop_by_name() to gobble up any text that are not numbers.

Related Links

Add comment


Security code
Refresh