2009-02-24 Alejandro G. Castro <alex@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 <libintl.h>
18
19 #include "hildon-date-button.h"
20 #include "hildon-date-selector.h"
21 #include "hildon-touch-selector.h"
22
23 #define                                         _(String) \
24                                                 dgettext("hildon-libs", String)
25
26 #define                                         c_(String) \
27                                                 dgettext("hildon-common-strings", String)
28
29 /**
30  * SECTION:hildon-date-button
31  * @Short_Description: Button displaying and allowing selection of a date.
32  * @See_Also: #HildonPickerButton, #HildonTimeButton
33  *
34  * #HildonDateButton is a widget that shows a text label and a date, and allows
35  * the user to select a different date. Visually, it's a button that, once clicked,
36  * presents a #HildonPickerDialog containing a #HildonDateSelector. Once the user selects
37  * a different date from the selector, this will be shown in the button.
38  */
39
40 G_DEFINE_TYPE (HildonDateButton, hildon_date_button, HILDON_TYPE_PICKER_BUTTON)
41
42 #if 0
43 #define GET_PRIVATE(o)                                                  \
44   (G_TYPE_INSTANCE_GET_PRIVATE ((o), HILDON_TYPE_DATE_BUTTON, HildonDateButtonPrivate))
45
46 typedef struct _HildonDateButtonPrivate HildonDateButtonPrivate;
47
48 struct _HildonDateButtonPrivate
49 {
50 };
51 #endif
52
53 #if 0
54 static void
55 hildon_date_button_get_property (GObject * object, guint property_id,
56                                  GValue * value, GParamSpec * pspec)
57 {
58   switch (property_id) {
59   default:
60     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
61   }
62 }
63
64 static void
65 hildon_date_button_set_property (GObject * object, guint property_id,
66                                  const GValue * value, GParamSpec * pspec)
67 {
68   switch (property_id) {
69   default:
70     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
71   }
72 }
73 #endif
74
75 static void
76 hildon_date_button_class_init (HildonDateButtonClass * klass)
77 {
78 #if 0
79   GObjectClass *object_class = G_OBJECT_CLASS (klass);
80
81   g_type_class_add_private (klass, sizeof (HildonDateButtonPrivate));
82
83   object_class->get_property = hildon_date_button_get_property;
84   object_class->set_property = hildon_date_button_set_property;
85 #endif
86 }
87
88 static void
89 hildon_date_button_init (HildonDateButton * self)
90 {
91   GtkWidget *date_selector;
92
93   date_selector = hildon_date_selector_new ();
94
95   hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self),
96                                      HILDON_TOUCH_SELECTOR (date_selector));
97 }
98
99 /**
100  * hildon_date_button_new:
101  * @size: One of #HildonSizeType
102  * @arrangement: one of #HildonButtonArrangement
103  *
104  * Creates a new #HildonDateButton. See hildon_button_new() for details on the
105  * parameters.
106  *
107  * Returns: a new #HildonDateButton
108  *
109  * Since: 2.2
110  **/
111 GtkWidget *
112 hildon_date_button_new (HildonSizeType          size,
113                         HildonButtonArrangement arrangement)
114 {
115   return g_object_new (HILDON_TYPE_DATE_BUTTON,
116                        "title", _("wdgt_ti_date"),
117                        "arrangement", arrangement,
118                        "size", size,
119                        NULL);
120 }
121
122 /**
123  * hildon_date_button_new_with_year_range:
124  * @size: One of #HildonSizeType
125  * @arrangement: one of #HildonButtonArrangement
126  * @min_year: the minimum available year or -1 to ignore
127  * @max_year: the maximum available year or -1 to ignore
128  *
129  * Creates a new #HildonDateButton with a specific valid range of years.
130  * See hildon_date_selector_new_with_year_range() for details on the range.
131  *
132  * Returns: a new #HildonDateButton
133  *
134  * Since: 2.2
135  **/
136 GtkWidget *
137 hildon_date_button_new_with_year_range (HildonSizeType size,
138                                         HildonButtonArrangement arrangement,
139                                         gint min_year,
140                                         gint max_year)
141 {
142   GtkWidget *button;
143   GtkWidget *selector;
144
145   button = hildon_date_button_new (size, arrangement);
146   selector = hildon_date_selector_new_with_year_range (min_year, max_year);
147   hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
148                                      HILDON_TOUCH_SELECTOR (selector));
149
150   return button;
151 }
152
153 /**
154  * hildon_date_button_get_date:
155  * @button: a #HildonDateButton
156  * @year: return location for the selected year
157  * @month: return location for the selected month
158  * @day: return location for the selected day
159  *
160  * Retrieves currently selected date from @button.
161  *
162  * Since: 2.2
163  **/
164 void
165 hildon_date_button_get_date (HildonDateButton * button,
166                              guint * year, guint * month, guint * day)
167 {
168   HildonTouchSelector *selector;
169
170   g_return_if_fail (HILDON_IS_DATE_BUTTON (button));
171
172   selector = hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (button));
173
174   hildon_date_selector_get_date (HILDON_DATE_SELECTOR (selector), year, month, day);
175 }
176
177 /**
178  * hildon_date_button_set_date:
179  * @button: a #HildonDateButton
180  * @year: the year to set.
181  * @month: the month number to set.
182  * @day: the day of the month to set.
183  *
184  * Sets the date in @button. The date set will be displayed
185  * and will be the default selected option on the shown #HildonDateSelector.
186  *
187  * Since: 2.2
188  **/
189 void
190 hildon_date_button_set_date (HildonDateButton * button,
191                              guint year, guint month, guint day)
192 {
193   HildonTouchSelector *selector;
194   gchar *date;
195
196   g_return_if_fail (HILDON_IS_DATE_BUTTON (button));
197
198   selector = hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (button));
199
200   hildon_date_selector_select_current_date (HILDON_DATE_SELECTOR (selector),
201                                             year, month, day);
202   date = hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector));
203
204   hildon_button_set_value (HILDON_BUTTON (button), date);
205
206   g_free (date);
207 }