2009-01-13 Claudio Saavedra <csaavedra@igalia.com>
[hildon] / src / hildon-date-button.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser Public License as published by
8  * the Free Software Foundation; version 2 of the license.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser Public License for more details.
14  *
15  */
16
17 #include "hildon-date-button.h"
18 #include "hildon-date-selector.h"
19 #include "hildon-touch-selector.h"
20
21 /**
22  * SECTION:hildon-date-button
23  * @Short_Description: Button displaying and allowing selection of a date.
24  * @See_Also: #HildonPickerButton, #HildonTimeButton
25  *
26  * #HildonDateButton is a widget that shows a text label and a date, and allows
27  * the user to select a different date. Visually, it's a button that, once clicked,
28  * presents a #HildonPickerDialog containing a #HildonDateSelector. Once the user selects
29  * a different date from the selector, this will be shown in the button.
30  */
31
32 G_DEFINE_TYPE (HildonDateButton, hildon_date_button, HILDON_TYPE_PICKER_BUTTON)
33
34 #if 0
35 #define GET_PRIVATE(o)                                                  \
36   (G_TYPE_INSTANCE_GET_PRIVATE ((o), HILDON_TYPE_DATE_BUTTON, HildonDateButtonPrivate))
37
38 typedef struct _HildonDateButtonPrivate HildonDateButtonPrivate;
39
40 struct _HildonDateButtonPrivate
41 {
42 };
43 #endif
44
45 #if 0
46 static void
47 hildon_date_button_get_property (GObject * object, guint property_id,
48                                  GValue * value, GParamSpec * pspec)
49 {
50   switch (property_id) {
51   default:
52     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
53   }
54 }
55
56 static void
57 hildon_date_button_set_property (GObject * object, guint property_id,
58                                  const GValue * value, GParamSpec * pspec)
59 {
60   switch (property_id) {
61   default:
62     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
63   }
64 }
65 #endif
66
67 static void
68 hildon_date_button_class_init (HildonDateButtonClass * klass)
69 {
70 #if 0
71   GObjectClass *object_class = G_OBJECT_CLASS (klass);
72
73   g_type_class_add_private (klass, sizeof (HildonDateButtonPrivate));
74
75   object_class->get_property = hildon_date_button_get_property;
76   object_class->set_property = hildon_date_button_set_property;
77 #endif
78 }
79
80 static void
81 hildon_date_button_init (HildonDateButton * self)
82 {
83   GtkWidget *date_selector;
84
85   date_selector = hildon_date_selector_new ();
86
87   hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self),
88                                      HILDON_TOUCH_SELECTOR (date_selector));
89
90   g_object_unref (date_selector);
91 }
92
93 /**
94  * hildon_date_button_new:
95  * @size: One of #HildonSizeType
96  * @arrangement: one of #HildonButtonArrangement
97  *
98  * Creates a new #HildonDateButton. See hildon_button_new() for details on the
99  * parameters.
100  *
101  * Returns: a new #HildonDateButton
102  *
103  * Since: 2.2
104  **/
105 GtkWidget *
106 hildon_date_button_new (HildonSizeType          size,
107                         HildonButtonArrangement arrangement)
108 {
109   return g_object_new (HILDON_TYPE_DATE_BUTTON,
110                        "title", "Date",
111                        "arrangement", arrangement,
112                        "size", size,
113                        NULL);
114 }
115
116 /**
117  * hildon_date_button_get_date:
118  * @button: a #HildonDateButton
119  * @year: return location for the selected year
120  * @month: return location for the selected month
121  * @day: return location for the selected day
122  *
123  * Retrieves currently selected date from @button.
124  *
125  * Since: 2.2
126  **/
127 void
128 hildon_date_button_get_date (HildonDateButton * button,
129                              guint * year, guint * month, guint * day)
130 {
131   HildonTouchSelector *selector;
132
133   g_return_if_fail (HILDON_IS_DATE_BUTTON (button));
134
135   selector = hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (button));
136
137   hildon_date_selector_get_date (HILDON_DATE_SELECTOR (selector), year, month, day);
138 }
139
140 /**
141  * hildon_date_button_set_date:
142  * @button: a #HildonDateButton
143  * @year: the year to set.
144  * @month: the month number to set.
145  * @day: the day of the month to set.
146  *
147  * Sets the date in @button. The date set will be displayed
148  * and will be the default selected option on the shown #HildonDateSelector.
149  *
150  * Since: 2.2
151  **/
152 void
153 hildon_date_button_set_date (HildonDateButton * button,
154                              guint year, guint month, guint day)
155 {
156   HildonTouchSelector *selector;
157   gchar *date;
158
159   g_return_if_fail (HILDON_IS_DATE_BUTTON (button));
160
161   selector = hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (button));
162
163   hildon_date_selector_select_current_date (HILDON_DATE_SELECTOR (selector),
164                                             year, month, day);
165   date = hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector));
166
167   hildon_button_set_value (HILDON_BUTTON (button), date);
168
169   g_free (date);
170 }