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