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