184. How to setup a pulldown calendar for date selection?

Problem

In the article How to let user enter date with a popup calendar - Part 2?, we have showed how to allow users to enter date through a popup calendar.

This example shows another way to prompt for date, by having the calendar in the form of a "pulldown menu" as shown below:

How to setup a pulldown calendar for date selection?


Solution


Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
42   
43   
44   
45   
46   
47   
48   
49   
52   
53   
54   
55   
56   
57   
58   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
74   
75   
76   
77   
78   
79   
80   
81   
82   
83   
84   
85   
86   
87   
89   
90   
91   
92   
93   
94   
95   
96   
98   
99   
100   
101   
102   
103   
104   
105   
106   
108   
112   
113   
114   
115   
116   
117   
118   
119   
120   
121   
122   
123   
124   
125   
<?php
$window = new GtkWindow();
$window->connect_simple('destroy', array( 'Gtk', 'main_quit'));
$window->set_size_request(400,150);

$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Setup a pulldown calendar for date selection");
$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);
$title->set_justify(Gtk::JUSTIFY_CENTER);
$alignment = new GtkAlignment(0.5, 0, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

$vbox->pack_start($hbox=new GtkHBox(), 0, 0);
$hbox->pack_start(new GtkLabel('Select: '), 0, 0);

// Setup combobox

$combobox = new ComboBox1($window);
$hbox->pack_start($combobox, 0, 0);
$hbox->pack_start(new GtkLabel('  '), 0, 0);
$hbox->pack_start($button = new GtkButton('Submit'), 0, 0);
$button->set_size_request(60, 24);
$button->connect('clicked', "on_submit_button", $combobox);

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

class ComboBox1 extends GtkHBox {

    function __construct($parent_window) {
        parent::__construct();
        $this->parent_window = $parent_window;
        $this->entry = new GtkEntry();
        $this->pack_start($this->entry, 0, 0);
        $this->pack_start($this->button = new GtkButton('V'), 0, 0);

        $this->button->connect('clicked', array(&$this, 'on_button'));
        $this->entry->connect('key-press-event',
            array(&$this, 'on_key_or_button_press'));
        $this->entry->connect('button-press-event',
            array(&$this, 'on_key_or_button_press'));
    }

    function on_key_or_button_press($widget, $event) {
        $this->button->clicked();
    }

    function on_button($button) { // process button click
        $win_pos=$this->parent_window->get_position();
        $x_field_pos=$this->entry->allocation->x;
        $y_field_pos=$this->entry->allocation->y;
        $x_shift=4;
        $y_shift=23+$this->entry->allocation->height;

        $x_win=$win_pos[0]+$x_field_pos+$x_shift;
        $y_win=$win_pos[1]+$y_field_pos+$y_shift;

        $this->dialog = new GtkDialog(null, null,
            Gtk::DIALOG_MODAL|Gtk::DIALOG_NO_SEPARATOR);
        $this->dialog->set_decorated(false);
        $this->dialog->set_uposition($x_win,$y_win);
        $top_area = $this->dialog->vbox;

        $this->calendar = new GtkCalendar(); // note 1
        $this->dialog->vbox->pack_start($this->calendar, 0, 0);

        $this->calendar->connect('day-selected',
            array(&$this, 'on_select'), 'day-selected');
        $this->calendar->connect('month-changed',
            array(&$this, 'on_select'), 'month-changed');
        $this->dialog->connect('button-press-event',
            array(&$this, 'on_buttonpress'));

        $this->dialog->action_area->set_size_request(-1, 0);

        $this->dialog ->show_all();
        $this->dialog->run();
    }

    function on_buttonpress($widget, $event) {
        if (!$this->month_changed) {
            $date = $this->calendar->get_date(); // note 2
            $selected_date = 1+$date[1].'/'.$date[2].'/'.$date[0]; // note 3
            $this->entry->set_text($selected_date); // note 4
            $this->dialog->destroy(); // note 5
        } else {
            $this->month_changed = 0;
        }
    }

    function on_select($calendar, $signal_name) {
        if ($signal_name=='month-changed') { // note 6
            $this->month_changed = 1;
        } else {
            $this->month_changed = 0;
        }
        return false;
    }
}

function on_submit_button($button, $combobox) {
    $selection = $combobox->entry->get_text();
    print "selection = $selection\n";
}

?>

Output

As shown above.
 

Explanation

The above sample code is based on How to setup pulldown menu with 2 columns - Part 3 - using self defined combobox? and How to let user enter date with a popup calendar - Part 2?

What's new here:

  1. Create a GtkCalendar.
  2. Get the date selected by user.
  3. Format the date. Here we use the US format: mm/dd/yyyy. You can modify this to use your country's conventional date format.
  4. Copy the formatted date to the GtkEntry.
  5. Close the dialog box.
  6. Note that a 'button-press-event' signal is also generated when the user changes the month. Hence the use of 'on_select' signal to help us differentiate between date selection and month change. For more details, please refer to How to let user enter date with a popup calendar - Part 2?.

Related Links

Add comment


Security code
Refresh