2 * This file is a part of hildon
4 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
6 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; version 2.1 of
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * SECTION:hildon-calendar-popup
27 * @shortdesc: CalendarPopup allows choosing a date from a popup calendar.
28 * @longdesc: The Calendar popup is a dialog that contains a GtkCalendar
29 * widget. The pop-up is cancelled by pressing the ESC key.
32 * @seealso: #HildonDateEditor, #HildonTimeEditor
34 * HildonCalendarPopup is a dialog which contains a GtkCalendar. It
35 * also contains arrow buttons for changing the month/year. If an
36 * entered date is invalid, an information message will be shown.
43 #include "hildon-calendar-popup.h"
45 #include <gtk/gtkcalendar.h>
47 #include <gdk/gdkkeysyms.h>
51 #include "hildon-calendar-popup-private.h"
54 dgettext("hildon-libs", String)
65 hildon_calendar_popup_class_init (HildonCalendarPopupClass *cal_class);
68 hildon_calendar_popup_init (HildonCalendarPopup *cal);
71 hildon_calendar_selected_date (GtkWidget *self,
75 hildon_key_pressed (GtkWidget *widget,
80 hildon_calendar_popup_set_property (GObject *object,
85 hildon_calendar_popup_get_property (GObject *object,
90 static GtkDialog* parent_class;
103 hildon_calendar_popup_get_type (void)
105 static GType popup_type = 0;
108 static const GTypeInfo popup_info = {
109 sizeof (HildonCalendarPopupClass),
110 NULL, /* base_init */
111 NULL, /* base_finalize */
112 (GClassInitFunc) hildon_calendar_popup_class_init,
113 NULL, /* class_finalize */
114 NULL, /* class_data */
115 sizeof (HildonCalendarPopup),
117 (GInstanceInitFunc) hildon_calendar_popup_init,
119 popup_type = g_type_register_static (GTK_TYPE_DIALOG,
120 "HildonCalendarPopup",
128 * hildon_calendar_popup_new:
129 * @parent: parent window for dialog
130 * @year: initial year
131 * @month: initial month
134 * This function returns a new HildonCalendarPopup. The initially
135 * selected date is specified by the parameters (year, month, day).
136 * If the specified date is invalid, the current date is used.
138 * Returns: new @HildonCalendarPopup widget
141 hildon_calendar_popup_new (GtkWindow *parent,
146 HildonCalendarPopup *cal = NULL;
148 /* Create new HildonCalendarPopup */
149 cal = HILDON_CALENDAR_POPUP (g_object_new (HILDON_TYPE_CALENDAR_POPUP,
150 "year", year, "month", month, "day", day,
154 gtk_window_set_transient_for (GTK_WINDOW(cal), parent);
157 return GTK_WIDGET (cal);
161 * hildon_calendar_popup_set_date:
162 * @cal: the @HildonCalendarPopup widget
167 * Activates a new date on the calendar popup.
170 hildon_calendar_popup_set_date (HildonCalendarPopup *cal,
175 guint dtmp, mtmp, ytmp = 0;
176 HildonCalendarPopupPrivate *priv;
178 g_return_if_fail (HILDON_IS_CALENDAR_POPUP (cal));
180 priv = HILDON_CALENDAR_POPUP_GET_PRIVATE (cal);
183 /* Choose current date if the date is invalid: */
184 init_dmy (year, month, day, &dtmp, &mtmp, &ytmp);
186 /* Remove all visual markers */
187 gtk_calendar_clear_marks (GTK_CALENDAR (priv->cal));
190 gtk_calendar_select_month (GTK_CALENDAR (priv->cal), mtmp - 1, ytmp);
191 gtk_calendar_select_day (GTK_CALENDAR (priv->cal), dtmp);
195 * hildon_calendar_popup_get_date:
196 * @cal: the @HildonCalendarPopup widget
201 * Gets the currently selected year, month, and day.
202 * It's possible to pass NULL to any of the pointers if you don't need that data.
205 hildon_calendar_popup_get_date (HildonCalendarPopup *cal,
210 HildonCalendarPopupPrivate *priv;
212 g_return_if_fail (HILDON_IS_CALENDAR_POPUP (cal));
214 priv = HILDON_CALENDAR_POPUP_GET_PRIVATE (cal);
217 gtk_calendar_get_date (GTK_CALENDAR (priv->cal), year, month, day);
224 ! g_date_valid_dmy (*day, *month, *year))
225 *day = g_date_get_days_in_month (*month, *year);
229 hildon_calendar_popup_class_init (HildonCalendarPopupClass *cal_class)
231 GObjectClass *object_class = G_OBJECT_CLASS (cal_class);
232 parent_class = g_type_class_peek_parent (cal_class);
234 object_class->set_property = hildon_calendar_popup_set_property;
235 object_class->get_property = hildon_calendar_popup_get_property;
237 g_type_class_add_private(cal_class, sizeof (HildonCalendarPopupPrivate));
239 /* Install new properties for the GObject_class */
241 g_object_class_install_property (object_class, PROP_MIN_YEAR,
242 g_param_spec_uint ("min-year",
243 "Minimum valid year",
244 "Minimum valid year",
249 g_object_class_install_property (object_class, PROP_MAX_YEAR,
250 g_param_spec_uint ("max-year",
251 "Maximum valid year",
252 "Maximum valid year",
257 g_object_class_install_property (object_class, PROP_DAY,
258 g_param_spec_int ("day",
260 "currently selected day",
266 g_object_class_install_property (object_class, PROP_MONTH,
267 g_param_spec_int ("month",
269 "currently selected month",
275 g_object_class_install_property (object_class, PROP_YEAR,
276 g_param_spec_int ("year",
278 "the currently selected year",
287 hildon_calendar_popup_init (HildonCalendarPopup *cal)
289 HildonCalendarPopupPrivate *priv;
290 static int set_domain = 1;
292 priv = HILDON_CALENDAR_POPUP_GET_PRIVATE(cal);
295 /* set the domain directory for different language */
296 /* FIXME I can't exactly figure out why is this here... */
298 (void) bindtextdomain ("hildon-libs", LOCALEDIR);
302 priv->cal = gtk_calendar_new ();
304 /* dialog options and packing */
305 gtk_calendar_set_display_options (GTK_CALENDAR (priv->cal),
306 GTK_CALENDAR_SHOW_HEADING |
307 GTK_CALENDAR_SHOW_DAY_NAMES |
308 GTK_CALENDAR_SHOW_WEEK_NUMBERS);
310 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (cal)->vbox), priv->cal,
312 gtk_dialog_set_has_separator (GTK_DIALOG (cal), FALSE);
313 gtk_dialog_add_button (GTK_DIALOG (cal), _("ecdg_bd_calendar_popout_done"),
315 gtk_widget_show(priv->cal);
317 /* Connect signals */
318 g_signal_connect (G_OBJECT (priv->cal), "key-press-event",
319 G_CALLBACK (hildon_key_pressed), cal);
321 g_signal_connect (G_OBJECT (priv->cal), "selected_date",
322 G_CALLBACK (hildon_calendar_selected_date), cal);
324 /* set decorations, needs realizing first */
325 /* FIXME That should be moved to on_realize */
326 gtk_widget_realize (GTK_WIDGET (cal));
327 gdk_window_set_decorations (GTK_WIDGET (cal)->window, GDK_DECOR_BORDER);
331 * Signal handler for key-press-event. Closes the dialog for some
335 hildon_key_pressed (GtkWidget *widget,
339 g_assert (HILDON_IS_CALENDAR_POPUP (cal_popup));
341 /* Handle Return key press as OK response */
342 if (event->keyval == GDK_Return)
344 gtk_dialog_response (GTK_DIALOG (cal_popup), GTK_RESPONSE_OK);
348 /* Handle Esc key press as CANCEL response */
349 if ((event->keyval == GDK_Escape))
351 gtk_dialog_response (GTK_DIALOG (cal_popup), GTK_RESPONSE_CANCEL);
359 * Validates the given date or initializes it with the current date
362 init_dmy (guint year,
369 g_assert (d != NULL);
370 g_assert (m != NULL);
371 g_assert (y != NULL);
375 /* Initialize the date with a valid selected date */
376 if (g_date_valid_dmy (day, month, year)) {
382 /* If selected date is invalid initialize the date with current date */
383 g_date_clear (&date, 1);
384 g_date_set_time (&date, time (NULL));
386 *d = g_date_get_day (&date);
387 *m = g_date_get_month (&date);
388 *y = g_date_get_year (&date);
393 * Exits the dialog when "selected_date" signal is emmited. The
394 * "selected_date" signal is a Hildon addition to GtkCalendar and is
395 * emitted on button-release.
398 hildon_calendar_selected_date (GtkWidget *self,
401 g_assert (GTK_IS_WIDGET (self));
402 g_assert (HILDON_IS_CALENDAR_POPUP (cal_popup));
404 gtk_dialog_response (GTK_DIALOG (cal_popup), GTK_RESPONSE_OK);
409 hildon_calendar_popup_set_property (GObject *object,
414 HildonCalendarPopup *popup = HILDON_CALENDAR_POPUP (object);
416 HildonCalendarPopupPrivate *priv =
417 HILDON_CALENDAR_POPUP_GET_PRIVATE(HILDON_CALENDAR_POPUP (object));
420 switch (property_id) {
424 guint year, month, day = 0;
425 hildon_calendar_popup_get_date (popup, &year, &month, &day);
427 /*Verifies that the date is valid: */
428 hildon_calendar_popup_set_date (popup, year, month, g_value_get_int (value));
434 guint year, month, day = 0;
435 hildon_calendar_popup_get_date (popup, &year, &month, &day);
437 /*Verifies that the date is valid: */
438 hildon_calendar_popup_set_date (popup, year, g_value_get_int (value), day);
444 guint year, month, day = 0;
445 hildon_calendar_popup_get_date (popup, &year, &month, &day);
447 /*Verifies that the date is valid: */
448 hildon_calendar_popup_set_date (popup, g_value_get_int (value), month, day);
453 g_object_set_property (G_OBJECT (priv->cal), "min-year", value);
457 g_object_set_property (G_OBJECT (priv->cal), "max-year", value);
461 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
467 hildon_calendar_popup_get_property (GObject *object,
472 HildonCalendarPopupPrivate *priv =
473 HILDON_CALENDAR_POPUP_GET_PRIVATE (HILDON_CALENDAR_POPUP (object));
476 switch (property_id) {
479 g_object_get_property (G_OBJECT (priv->cal), pspec->name, value);
483 g_object_get_property (G_OBJECT (priv->cal), pspec->name, value);
487 g_object_get_property (G_OBJECT (priv->cal), pspec->name, value);
491 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);