PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 52: How to let user enter date with a popup calendar - Part 1?
Written by kksou   
Tuesday, 10 October 2006
Problem

You want to display grouped radio buttons as shown below:

How to let user enter date with a popup calendar - Part 1?


Solution

Sample Code

Note: If you have installed php-gtk2 using Gnope Installer on Windows, and if running the sample code below gives you warning that the Symbolic names for keys (e.g. Gdk::KEY_Return) is not defined, you might want to update your php-gtk2 with the latest php-gtk2.dll available here. Simply download the php-gtk2.dll and replace the copy in the folder php-gtk2\ext. The latest compilation has put in the Symbolic names for keys listed here.

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

// display title
$title = new GtkLabel("Press F1 to choose a date");
$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);

$hbox = new GtkHBox();
$vbox->pack_start($hbox, 0, 0);
$hbox->pack_start(new GtkLabel("Select Date: "), 0, 0);
$hbox->pack_start($entry = new GtkEntry(), 0, 0);

// let us know when user press F1 key
$entry->connect('key-press-event', 'on_keypress', $entry); 

function on_keypress($widget, $event, $entry) {
    // we only want F1. return all the rest
    if ($event->keyval!=Gdk::KEY_F1) return false; 
    $selected_date = get_date(); // note 1
    $entry->set_text($selected_date);
    $entry->grab_focus();
}

function get_date() { // note 1
    $getdate_dialog = new GetDate();
    $date = $getdate_dialog->calendar->get_date();
    $selected_date = 1+$date[1].'/'.$date[2].'/'.$date[0]; // note 2
    return $selected_date;
}

class GetDate{ // note 1

    var $calendar;

    function GetDate() {
        $dialog = new GtkDialog('Get Date', null, Gtk::DIALOG_MODAL);
        $dialog->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
        $top_area = $dialog->vbox;
        setlocale(LC_ALL, 'english');
  • 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. We created the function get_date() so that one can allow user to enter date with a popup calendar using a one-liner: $date=get_date().
  2. Note that month returned is zero-basd, i.e. from 0 to 11. To get the "real" month, we have to add a 1.

Note

In this example, the user has to click on a date, and then click the OK button - a total of 2 clicks to select a date. We will improve on this in the next article.


User reviews   Average user ratings:    4.0   (from 1 user)
  1. csc
    September 19, 2008 10:01am

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