d2a727b36b1807a4e4c08a7cd67bf861d2eec6b8
[hildon] / hildon-widgets / hildon-sort-dialog.c
1 /*
2  * This file is part of hildon-libs
3  *
4  * Copyright (C) 2005 Nokia Corporation.
5  *
6  * Contact: Luc Pionchon <luc.pionchon@nokia.com>
7  *
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; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
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.
17  *
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
21  * 02110-1301 USA
22  *
23  */
24
25 /* 
26  * @file hildon-sort-dialog.c
27  * 
28  * This file contains API for Hildon Sort dialog.
29  * @desc: The sort dialog is used to define the order in which item are 
30  * shown in a list. Choise lists always display the current value when 
31  * the dialog is opened. 
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <stdio.h>
39 #include <libintl.h>
40
41 #include <gtk/gtkcombobox.h>
42 #include <gtk/gtkbox.h>
43 #include <gdk/gdkkeysyms.h>
44 #include <glib-object.h>
45
46 #include <hildon-widgets/hildon-caption.h>
47 #include "hildon-sort-dialog.h"
48
49
50 #define _(String) dgettext(PACKAGE, String)
51
52 static GtkDialogClass *parent_class;
53
54 #define HILDON_SORT_DIALOG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE \
55                 ((obj), HILDON_TYPE_SORT_DIALOG, HildonSortDialogPrivate));
56
57 typedef struct _HildonSortDialogPrivate HildonSortDialogPrivate;
58
59 static void hildon_sort_dialog_class_init(HildonSortDialogClass * class);
60 static void hildon_sort_dialog_init(HildonSortDialog * widget);
61
62 /* private data */
63 struct _HildonSortDialogPrivate {
64     /* Tab one */
65     GtkWidget *combo1;
66     GtkWidget *caption1;
67
68     /* Tab two */
69     GtkWidget *combo2;
70     GtkWidget *caption2;
71
72     /* OK/Cancel buttons */
73     GtkWidget *okButton;
74     GtkWidget *cancelButton;
75
76     /* Index value for sort_by */
77     gint sort_by_value;
78
79     /* Index value for sort_order */
80     gint sort_order_type;
81
82     gboolean index_first;
83     /* Index value counter */
84     gint index_counter;
85 };
86
87 /* Private functions */
88
89 /*
90  * Initialises the sort dialog class.
91  */
92 static void hildon_sort_dialog_class_init(HildonSortDialogClass * class)
93 {
94     parent_class = g_type_class_peek_parent(class);
95     g_type_class_add_private(class, sizeof(HildonSortDialogPrivate));
96 }
97
98 static void hildon_sort_dialog_init(HildonSortDialog * dialog)
99 {
100     HildonSortDialogPrivate *priv;
101     GtkSizeGroup *group;
102
103     g_return_if_fail(HILDON_IS_SORT_DIALOG(dialog));
104
105     priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
106
107     priv->index_first = TRUE;
108     priv->index_counter = 0;
109
110     group = GTK_SIZE_GROUP(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL));
111
112     gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
113     gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
114     gtk_window_set_title(GTK_WINDOW(dialog), _("ckdg_ti_sort"));
115
116     /* Tab one */
117     priv->combo1 = gtk_combo_box_new_text();
118     priv->caption1 = hildon_caption_new(group, _("ckdg_fi_sort_field"), priv->combo1,
119                                         NULL, HILDON_CAPTION_OPTIONAL);
120
121     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
122                        priv->caption1, FALSE, FALSE, 0);
123
124     /* Tab two */
125     priv->combo2 = gtk_combo_box_new_text();
126     gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo2),
127                               _("ckdg_va_sort_ascending"));
128     gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo2),
129                               _("ckdg_va_sort_descending"));
130
131     priv->caption2 = hildon_caption_new(group, _("ckdg_fi_sort_order"),
132                                         priv->combo2,
133                                         NULL, HILDON_CAPTION_OPTIONAL);
134
135     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
136                        priv->caption2, FALSE, FALSE, 0);
137
138     gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combo1), 0);
139     gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combo2), 0);
140
141     /* Create the OK/CANCEL buttons */
142     priv->okButton = gtk_dialog_add_button(GTK_DIALOG(dialog),
143                                            _("ckdg_bd_sort_dialog_ok"),
144                                            GTK_RESPONSE_OK);
145     priv->cancelButton = gtk_dialog_add_button(GTK_DIALOG(dialog),
146                                                _("ckdg_bd_sort_dialog_cancel"),
147                                                GTK_RESPONSE_CANCEL);
148
149     gtk_window_resize(GTK_WINDOW(dialog), 370, 100);
150     gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
151 }
152
153 /* Public functions */
154
155 /**
156  * hildon_sort_dialog_get_type:
157  *
158  * Returns GType for HildonSortDialog as produced by 
159  * g_type_register_static().
160  *
161  * Return value: HildonSortDialog type
162  **/
163 GType hildon_sort_dialog_get_type()
164 {
165     static GType dialog_type = 0;
166
167     if (!dialog_type) {
168         static const GTypeInfo dialog_info = {
169             sizeof(HildonSortDialogClass),
170             NULL,       /* base_init */
171             NULL,       /* base_finalize */
172             (GClassInitFunc) hildon_sort_dialog_class_init,
173             NULL,       /* class_finalize */
174             NULL,       /* class_data */
175             sizeof(HildonSortDialog),
176             0,  /* n_preallocs */
177             (GInstanceInitFunc) hildon_sort_dialog_init
178         };
179
180         dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
181                                              "HildonSortDialog",
182                                              &dialog_info, 0);
183     }
184     return dialog_type;
185 }
186
187 /**
188  * hildon_sort_dialog_new:
189  * @parent: Widget to be transient for, or NULL if none.
190  *
191  * HildonSortDialog contains two #HildonCaption:s with combo boxes. 
192  *
193  * Return value: pointer to a new @HildonSortDialog widget.
194  */
195 GtkWidget *hildon_sort_dialog_new(GtkWindow * parent)
196 {
197     GtkWidget *sort_dialog = g_object_new(HILDON_TYPE_SORT_DIALOG, NULL);
198
199     if (parent)
200         gtk_window_set_transient_for(GTK_WINDOW(sort_dialog), parent);
201
202     return sort_dialog;
203 }
204
205 /**
206  * hildon_sort_dialog_get_sort_key:
207  * @dialog: the #HildonSortDialog widget.
208  *
209  * Gets index to currently active sort key.
210  * 
211  * Return value: An integer which is the index value of the "Sort by" 
212  * field 
213  */
214 gint hildon_sort_dialog_get_sort_key(HildonSortDialog * dialog)
215 {
216     GtkWidget *combo_key;
217     HildonSortDialogPrivate *priv;
218
219     g_return_val_if_fail(HILDON_IS_SORT_DIALOG(dialog), -1);
220
221     priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
222     combo_key = hildon_caption_get_control(HILDON_CAPTION(priv->caption1));
223     priv->sort_by_value =
224         gtk_combo_box_get_active(GTK_COMBO_BOX(combo_key));
225     return priv->sort_by_value;
226 }
227
228 /**
229  * hildon_sort_dialog_get_sort_order:
230  * @dialog: the #HildonSortDialog widget.
231  *
232  * Gets current sorting order from "Sort order" field.
233  *
234  * Return value: current sorting order as #GtkSortType.
235  */
236 GtkSortType hildon_sort_dialog_get_sort_order(HildonSortDialog * dialog)
237 {
238     GtkWidget *combo_key;
239     HildonSortDialogPrivate *priv;
240
241     g_return_val_if_fail(HILDON_IS_SORT_DIALOG(dialog), 0);
242
243     priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
244     combo_key = hildon_caption_get_control(HILDON_CAPTION(priv->caption2));
245     priv->sort_order_type =
246         gtk_combo_box_get_active(GTK_COMBO_BOX(combo_key));
247     return priv->sort_order_type;
248 }
249
250 /**
251  * hildon_sort_dialog_set_sort_key:
252  * @dialog: the #HildonSortDialog widget.
253  * @key: Combo box's index value.
254  *
255  * Sets the index value of the #HildonSortDialog widget.
256  */
257 void hildon_sort_dialog_set_sort_key(HildonSortDialog * dialog, gint key)
258 {
259     GtkWidget *combo_key;
260     HildonSortDialogPrivate *priv;
261
262     g_return_if_fail(HILDON_IS_SORT_DIALOG(dialog));
263
264     priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
265     combo_key = hildon_caption_get_control(HILDON_CAPTION(priv->caption1));
266     gtk_combo_box_set_active(GTK_COMBO_BOX(combo_key), key);
267 }
268
269 /**
270  * hildon_sort_dialog_set_sort_order:
271  * @dialog: the #HildonSortDialog widget.
272  * @order: Combo box's index value.
273  *
274  * Sets the index value of the #HildonSortDialog widget.
275  */
276 void
277 hildon_sort_dialog_set_sort_order(HildonSortDialog * dialog,
278                                   GtkSortType order)
279 {
280     GtkWidget *combo_order;
281     HildonSortDialogPrivate *priv;
282
283     g_return_if_fail(HILDON_IS_SORT_DIALOG(dialog));
284
285     priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
286     combo_order =
287         hildon_caption_get_control(HILDON_CAPTION(priv->caption2));
288     gtk_combo_box_set_active(GTK_COMBO_BOX(combo_order), order);
289 }
290
291 /**
292  * hildon_sort_dialog_add_sort_key:
293  * @dialog: the #HildonSortDialog widget.
294  * @sort_key: Combo box's index value
295  *
296  * Adds a new sort key and returns the respective index in
297  * sort key combobox.
298  *
299  * Return value: An integer which is the index of the added combo box's
300  * item.
301  */
302 gint
303 hildon_sort_dialog_add_sort_key(HildonSortDialog * dialog,
304                                 const gchar * sort_key)
305 {
306     HildonSortDialogPrivate *priv;
307
308     g_return_val_if_fail(HILDON_IS_SORT_DIALOG(dialog), -1);
309
310     priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
311     gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo1), sort_key);
312
313     if (priv->index_first == TRUE) {
314         priv->index_first = FALSE;
315         return priv->index_counter;
316
317     } else {
318         return priv->index_counter += 1;
319     }
320 }