aboutsummaryrefslogtreecommitdiff
path: root/hildon/hildon-calendar-popup.c
blob: 607008be984c33a4864fde15e7634a8317f2c1c5 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
 * This file is a part of hildon
 *
 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
 *
 * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**
 * SECTION:hildon-calendar-popup
 * @short_description: CalendarPopup allows choosing a date from a popup calendar.
 * @see_also: #HildonDateEditor, #HildonTimeEditor
 *
 * HildonCalendarPopup is a dialog which contains a HildonCalendar.  It
 * also contains arrow buttons for changing the month/year. If an
 * entered date is invalid, an information message will be shown.
 *
 * <note>
 *   <para>
 * #HildonCalendarPopup has been deprecated since Hildon 2.2 and should not
 * be used in newly written code.
 * See <link linkend="hildon-migrating-date-widgets">Migrating Date Widgets</link>
 * section to know how to migrate this deprecated widget.
 *   </para>
 * </note>
 *
 * <example>
 * <title>HildonCalendarPopup example</title>
 * <programlisting>
 * ...
 * gint y, m, d;
 * GtkWidget *parent, *popup;
 * <!-- -->
 * // get current date into &amp;y, &amp;m, &amp;d...
 * <!-- -->
 * gtk_widget_get_ancestor (GTK_WIDGET (data), GTK_TYPE_WINDOW);
 * popup = hildon_calendar_popup_new (GTK_WINDOW (parent), y, m, d);
 * <!-- -->
 * result = gtk_dialog_run (GTK_DIALOG (popup));
 * switch (result)
 * {
 *      case GTK_RESPONSE_OK:
 *      case GTK_RESPONSE_ACCEPT:
 * <!-- -->
 * hildon_calendar_popup_get_date (HILDON_CALENDAR_POPUP (popup), &amp;y, &amp;m, &amp;d);
 * <!-- -->
 * // here set the new date
 * }
 * gtk_widget_destroy(popup);
 * ...
 * </programlisting>
 * </example>
 *
 */


#undef                                          HILDON_DISABLE_DEPRECATED

#ifdef                                          HAVE_CONFIG_H
#include                                        <config.h>
#endif

#include                                        <langinfo.h>
#include                                        <time.h>
#include                                        <libintl.h>

#include                                        <gtk/gtk.h>
#include                                        <gdk/gdkkeysyms.h>

#include                                        "hildon-calendar-popup.h"
#include                                        "hildon-calendar-popup-private.h"
#include                                        "hildon-calendar.h"
#include                                        "hildon-stock.h"

#define                                         _(String)\
                                                dgettext("hildon-libs", String)

static void 
init_dmy                                        (guint year, 
                                                 guint month, 
                                                 guint day, 
                                                 guint *d,
                                                 guint *m, 
                                                 guint * y);

static void
hildon_calendar_popup_class_init                (HildonCalendarPopupClass *cal_class);

static void
hildon_calendar_popup_init                      (HildonCalendarPopup *cal);

static void
hildon_calendar_selected_date                   (GtkWidget *self, 
                                                 gpointer cal_popup);

static gboolean
hildon_key_pressed                              (GtkWidget *widget, 
                                                 GdkEventKey *event,
                                                 gpointer cal_popup);

static void
hildon_calendar_popup_set_property              (GObject *object,
                                                 guint property_id,
                                                 const GValue * value, 
                                                 GParamSpec * pspec);
static void
hildon_calendar_popup_get_property              (GObject *object, 
                                                 guint property_id,
                                                 GValue *value,
                                                 GParamSpec *pspec);

static GtkDialog*                               parent_class;

enum 
{
    PROP_0,
    PROP_DAY,
    PROP_MONTH,
    PROP_YEAR,
    PROP_MIN_YEAR,
    PROP_MAX_YEAR
};

GType G_GNUC_CONST
hildon_calendar_popup_get_type                  (void)
{
    static GType popup_type = 0;

    if (!popup_type) {
        static const GTypeInfo popup_info = {
            sizeof (HildonCalendarPopupClass),
            NULL,       /* base_init */
            NULL,       /* base_finalize */
            (GClassInitFunc) hildon_calendar_popup_class_init,
            NULL,       /* class_finalize */
            NULL,       /* class_data */
            sizeof (HildonCalendarPopup),
            0,  /* n_preallocs */
            (GInstanceInitFunc) hildon_calendar_popup_init,
        };
        popup_type = g_type_register_static (GTK_TYPE_DIALOG,
                "HildonCalendarPopup",
                &popup_info, 0);
    }

    return popup_type;
}

/**
 * hildon_calendar_popup_new:
 * @parent: parent window for dialog
 * @year: initial year
 * @month: initial month
 * @day: initial day
 *
 * This function returns a new HildonCalendarPopup. The initially
 * selected date is specified by the parameters (year, month, day).
 * If the specified date is invalid, the current date is used. 
 *
 * Returns: new @HildonCalendarPopup widget
 */
GtkWidget*
hildon_calendar_popup_new                       (GtkWindow *parent, 
                                                 guint year,
                                                 guint month,
                                                 guint day)
{
    HildonCalendarPopup *cal = NULL;

    /* Create new HildonCalendarPopup */
    cal = HILDON_CALENDAR_POPUP (g_object_new (HILDON_TYPE_CALENDAR_POPUP,
                "year", year, "month", month, "day", day,
                NULL));

    if (parent) {
        gtk_window_set_transient_for (GTK_WINDOW(cal), parent);
    }

    return GTK_WIDGET (cal);
}

/**
 * hildon_calendar_popup_set_date:
 * @cal: the @HildonCalendarPopup widget
 * @year: year
 * @month: month
 * @day: day
 *
 * Activates a new date on the calendar popup.
 **/
void
hildon_calendar_popup_set_date                  (HildonCalendarPopup *cal,
                                                 guint year, 
                                                 guint month, 
                                                 guint day)
{
    guint dtmp, mtmp, ytmp = 0;
    HildonCalendarPopupPrivate *priv;

    g_return_if_fail (HILDON_IS_CALENDAR_POPUP (cal));

    priv = HILDON_CALENDAR_POPUP_GET_PRIVATE (cal);
    g_assert (priv);

    /* Choose current date if the date is invalid:  */
    init_dmy (year, month, day, &dtmp, &mtmp, &ytmp);

    /* Remove all visual markers */
    hildon_calendar_clear_marks (HILDON_CALENDAR (priv->cal));

    /* Set a new date */
    hildon_calendar_select_month (HILDON_CALENDAR (priv->cal), mtmp - 1, ytmp);
    hildon_calendar_select_day (HILDON_CALENDAR (priv->cal), dtmp);
}

/**
 * hildon_calendar_popup_get_date:
 * @cal: the @HildonCalendarPopup widget
 * @year: year
 * @month: month
 * @day: day
 *
 * Gets the currently selected year, month, and day. 
 * It's possible to pass NULL to any of the pointers if you don't need that data.
 */
void
hildon_calendar_popup_get_date                  (HildonCalendarPopup *cal,
                                                 guint *year, 
                                                 guint *month, 
                                                 guint *day)
{
    HildonCalendarPopupPrivate *priv;

    g_return_if_fail (HILDON_IS_CALENDAR_POPUP (cal));

    priv = HILDON_CALENDAR_POPUP_GET_PRIVATE (cal);
    g_assert (priv);

    hildon_calendar_get_date (HILDON_CALENDAR (priv->cal), year, month, day);
    if (month != NULL)
        *month = *month + 1;

    if (day != NULL && 
        month != NULL &&
        year != NULL &&
        ! g_date_valid_dmy (*day, *month, *year)) 
        *day = g_date_get_days_in_month (*month, *year);
}

static void
hildon_calendar_popup_class_init                (HildonCalendarPopupClass *cal_class)
{
    GObjectClass *object_class = G_OBJECT_CLASS (cal_class);
    parent_class = g_type_class_peek_parent (cal_class);

    object_class->set_property = hildon_calendar_popup_set_property;
    object_class->get_property = hildon_calendar_popup_get_property;

    g_type_class_add_private(cal_class, sizeof (HildonCalendarPopupPrivate));

    /* Install new properties for the GObject_class */

    g_object_class_install_property (object_class, PROP_MIN_YEAR,
            g_param_spec_uint ("min-year",
                "Minimum valid year",
                "Minimum valid year",
                1, 10000,
                1970,
                G_PARAM_WRITABLE));

    g_object_class_install_property (object_class, PROP_MAX_YEAR,
            g_param_spec_uint ("max-year",
                "Maximum valid year",
                "Maximum valid year",
                1, 10000,
                2037,
                G_PARAM_WRITABLE));

    g_object_class_install_property (object_class, PROP_DAY,
            g_param_spec_int ("day",
                "Day",
                "currently selected day",
                G_MININT,
                G_MAXINT,
                0,
                G_PARAM_READWRITE));

    g_object_class_install_property (object_class, PROP_MONTH,
            g_param_spec_int ("month",
                "Month",
                "currently selected month",
                G_MININT,
                G_MAXINT,
                0,
                G_PARAM_READWRITE));

    g_object_class_install_property (object_class, PROP_YEAR,
            g_param_spec_int ("year",
                "Year",
                "the currently selected year",
                G_MININT,
                G_MAXINT,
                0,
                G_PARAM_READWRITE));

}

static void
hildon_calendar_popup_init                      (HildonCalendarPopup *cal)
{
    HildonCalendarPopupPrivate *priv;
    static int set_domain = 1;

    priv = HILDON_CALENDAR_POPUP_GET_PRIVATE(cal);
    g_assert (priv);

    /* set the domain directory for different language */
    /* FIXME I can't exactly figure out why is this here... */
    if (set_domain) {
        (void) bindtextdomain ("hildon-libs", LOCALEDIR);
        set_domain = 0;
    }

    priv->cal = hildon_calendar_new ();

    /* dialog options and packing */
    hildon_calendar_set_display_options (HILDON_CALENDAR (priv->cal),
            HILDON_CALENDAR_SHOW_HEADING |
            HILDON_CALENDAR_SHOW_DAY_NAMES |
            HILDON_CALENDAR_SHOW_WEEK_NUMBERS);

    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (cal)->vbox), priv->cal,
            TRUE, TRUE, 0);
    gtk_dialog_set_has_separator (GTK_DIALOG (cal), FALSE);
    gtk_dialog_add_button (GTK_DIALOG (cal), HILDON_STOCK_DONE, GTK_RESPONSE_OK);
    gtk_widget_show(priv->cal);

    /* Connect signals */
    g_signal_connect (G_OBJECT (priv->cal), "key-press-event",
            G_CALLBACK (hildon_key_pressed), cal);

    g_signal_connect (G_OBJECT (priv->cal), "selected_date",
            G_CALLBACK (hildon_calendar_selected_date), cal);

    /* set decorations, needs realizing first */
    /* FIXME That should be moved to on_realize */
    gtk_widget_realize (GTK_WIDGET (cal));
    gdk_window_set_decorations (GTK_WIDGET (cal)->window, GDK_DECOR_BORDER);
}

/*
 * Signal handler for key-press-event. Closes the dialog for some
 * special keys.
 */
static gboolean
hildon_key_pressed                              (GtkWidget *widget, 
                                                 GdkEventKey *event, 
                                                 gpointer cal_popup)
{
    g_assert (HILDON_IS_CALENDAR_POPUP (cal_popup));

    /* Handle Return key press as OK response */
    if (event->keyval == GDK_Return)
    {
        gtk_dialog_response (GTK_DIALOG (cal_popup), GTK_RESPONSE_OK);
        return TRUE;
    }

    /* Handle Esc key press as CANCEL response */
    if ((event->keyval == GDK_Escape))
    {
        gtk_dialog_response (GTK_DIALOG (cal_popup), GTK_RESPONSE_CANCEL);
        return TRUE;
    }

    return FALSE;
}

/*
 * Validates the given date or initializes it with the current date
 */
static void
init_dmy                                        (guint year, 
                                                 guint month, 
                                                 guint day, 
                                                 guint *d, 
                                                 guint *m, 
                                                 guint *y)
{
    g_assert (d != NULL);
    g_assert (m != NULL);
    g_assert (y != NULL);

    GDate date;

    /* Initialize the date with a valid selected date */ 
    if (g_date_valid_dmy (day, month, year)) {
        *d = day;
        *m = month;
        *y = year;
    } else { 

        /* If selected date is invalid initialize the date with current date */ 
        g_date_clear (&date, 1);
        g_date_set_time (&date, time (NULL));

        *d = g_date_get_day (&date);
        *m = g_date_get_month (&date);
        *y = g_date_get_year (&date);
    }
}

/*
 * Exits the dialog when "selected_date" signal is emmited. */
static void
hildon_calendar_selected_date                   (GtkWidget *self, 
                                                 gpointer cal_popup)
{
    g_assert (GTK_IS_WIDGET (self));
    g_assert (HILDON_IS_CALENDAR_POPUP (cal_popup));

    gtk_dialog_response (GTK_DIALOG (cal_popup), GTK_RESPONSE_OK);
}


static void 
hildon_calendar_popup_set_property              (GObject *object, 
                                                 guint property_id,
                                                 const GValue *value, 
                                                 GParamSpec *pspec)
{
    HildonCalendarPopup *popup = HILDON_CALENDAR_POPUP (object);

    HildonCalendarPopupPrivate *priv = 
        HILDON_CALENDAR_POPUP_GET_PRIVATE(HILDON_CALENDAR_POPUP (object));
    g_assert (priv);

    switch (property_id) {

        case PROP_DAY: 
        {
            guint year, month, day = 0;
            hildon_calendar_popup_get_date (popup, &year, &month, &day);

            /*Verifies that the date is valid: */
            hildon_calendar_popup_set_date (popup, year, month, g_value_get_int (value));
            break;
        }

        case PROP_MONTH:
        {
            guint year, month, day = 0;
            hildon_calendar_popup_get_date (popup, &year, &month, &day);

            /*Verifies that the date is valid: */
            hildon_calendar_popup_set_date (popup, year, g_value_get_int (value), day);
            break;
        }

        case PROP_YEAR:
        {
            guint year, month, day = 0;
            hildon_calendar_popup_get_date (popup, &year, &month, &day);

            /*Verifies that the date is valid: */
            hildon_calendar_popup_set_date (popup, g_value_get_int (value), month, day);
            break;
        }

        case PROP_MIN_YEAR:
            g_object_set_property (G_OBJECT (priv->cal), "min-year", value);
            break;

        case PROP_MAX_YEAR:
            g_object_set_property (G_OBJECT (priv->cal), "max-year", value);
            break;

        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
            break;
    }
}

static void 
hildon_calendar_popup_get_property              (GObject *object, 
                                                guint property_id,
                                                GValue *value,
                                                GParamSpec *pspec)
{
    HildonCalendarPopupPrivate *priv = 
        HILDON_CALENDAR_POPUP_GET_PRIVATE (HILDON_CALENDAR_POPUP (object));
    g_assert (priv);

    switch (property_id) {

        case PROP_DAY:
            g_object_get_property (G_OBJECT (priv->cal), pspec->name, value);
            break;

        case PROP_MONTH:
            g_object_get_property (G_OBJECT (priv->cal), pspec->name, value);
            break;

        case PROP_YEAR:
            g_object_get_property (G_OBJECT (priv->cal), pspec->name, value);
            break;

        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
            break;
    }
}