2 * This file is part of hildon-libs
4 * Copyright (C) 2005 Nokia Corporation.
6 * Contact: Luc Pionchon <luc.pionchon@nokia.com>
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.
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.
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
26 * @file hildon-sort-dialog.c
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.
41 #include <gtk/gtkcombobox.h>
42 #include <gtk/gtkbox.h>
43 #include <gdk/gdkkeysyms.h>
44 #include <glib-object.h>
46 #include <hildon-widgets/hildon-caption.h>
47 #include "hildon-sort-dialog.h"
50 #define _(String) dgettext(PACKAGE, String)
52 static GtkDialogClass *parent_class;
54 #define HILDON_SORT_DIALOG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE \
55 ((obj), HILDON_TYPE_SORT_DIALOG, HildonSortDialogPrivate));
57 typedef struct _HildonSortDialogPrivate HildonSortDialogPrivate;
59 static void hildon_sort_dialog_class_init(HildonSortDialogClass * class);
60 static void hildon_sort_dialog_init(HildonSortDialog * widget);
63 struct _HildonSortDialogPrivate {
72 /* OK/Cancel buttons */
74 GtkWidget *cancelButton;
76 /* Index value for sort_by */
79 /* Index value for sort_order */
83 /* Index value counter */
87 /* Private functions */
90 * Initialises the sort dialog class.
92 static void hildon_sort_dialog_class_init(HildonSortDialogClass * class)
94 parent_class = g_type_class_peek_parent(class);
95 g_type_class_add_private(class, sizeof(HildonSortDialogPrivate));
98 static void hildon_sort_dialog_init(HildonSortDialog * dialog)
100 HildonSortDialogPrivate *priv;
103 g_return_if_fail(HILDON_IS_SORT_DIALOG(dialog));
105 priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
107 priv->index_first = TRUE;
108 priv->index_counter = 0;
110 group = GTK_SIZE_GROUP(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL));
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"));
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);
121 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
122 priv->caption1, FALSE, FALSE, 0);
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"));
131 priv->caption2 = hildon_caption_new(group, _("ckdg_fi_sort_order"),
133 NULL, HILDON_CAPTION_OPTIONAL);
135 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
136 priv->caption2, FALSE, FALSE, 0);
138 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combo1), 0);
139 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combo2), 0);
141 /* Create the OK/CANCEL buttons */
142 priv->okButton = gtk_dialog_add_button(GTK_DIALOG(dialog),
143 _("ckdg_bd_sort_dialog_ok"),
145 priv->cancelButton = gtk_dialog_add_button(GTK_DIALOG(dialog),
146 _("ckdg_bd_sort_dialog_cancel"),
147 GTK_RESPONSE_CANCEL);
149 gtk_window_resize(GTK_WINDOW(dialog), 370, 100);
150 gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
153 /* Public functions */
156 * hildon_sort_dialog_get_type:
158 * Returns GType for HildonSortDialog as produced by
159 * g_type_register_static().
161 * Return value: HildonSortDialog type
163 GType hildon_sort_dialog_get_type()
165 static GType dialog_type = 0;
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),
177 (GInstanceInitFunc) hildon_sort_dialog_init
180 dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
188 * hildon_sort_dialog_new:
189 * @parent: Widget to be transient for, or NULL if none.
191 * HildonSortDialog contains two #HildonCaption:s with combo boxes.
193 * Return value: pointer to a new @HildonSortDialog widget.
195 GtkWidget *hildon_sort_dialog_new(GtkWindow * parent)
197 GtkWidget *sort_dialog = g_object_new(HILDON_TYPE_SORT_DIALOG, NULL);
200 gtk_window_set_transient_for(GTK_WINDOW(sort_dialog), parent);
206 * hildon_sort_dialog_get_sort_key:
207 * @dialog: the #HildonSortDialog widget.
209 * Gets index to currently active sort key.
211 * Return value: An integer which is the index value of the "Sort by"
214 gint hildon_sort_dialog_get_sort_key(HildonSortDialog * dialog)
216 GtkWidget *combo_key;
217 HildonSortDialogPrivate *priv;
219 g_return_val_if_fail(HILDON_IS_SORT_DIALOG(dialog), -1);
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;
229 * hildon_sort_dialog_get_sort_order:
230 * @dialog: the #HildonSortDialog widget.
232 * Gets current sorting order from "Sort order" field.
234 * Return value: current sorting order as #GtkSortType.
236 GtkSortType hildon_sort_dialog_get_sort_order(HildonSortDialog * dialog)
238 GtkWidget *combo_key;
239 HildonSortDialogPrivate *priv;
241 g_return_val_if_fail(HILDON_IS_SORT_DIALOG(dialog), 0);
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;
251 * hildon_sort_dialog_set_sort_key:
252 * @dialog: the #HildonSortDialog widget.
253 * @key: Combo box's index value.
255 * Sets the index value of the #HildonSortDialog widget.
257 void hildon_sort_dialog_set_sort_key(HildonSortDialog * dialog, gint key)
259 GtkWidget *combo_key;
260 HildonSortDialogPrivate *priv;
262 g_return_if_fail(HILDON_IS_SORT_DIALOG(dialog));
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);
270 * hildon_sort_dialog_set_sort_order:
271 * @dialog: the #HildonSortDialog widget.
272 * @order: Combo box's index value.
274 * Sets the index value of the #HildonSortDialog widget.
277 hildon_sort_dialog_set_sort_order(HildonSortDialog * dialog,
280 GtkWidget *combo_order;
281 HildonSortDialogPrivate *priv;
283 g_return_if_fail(HILDON_IS_SORT_DIALOG(dialog));
285 priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
287 hildon_caption_get_control(HILDON_CAPTION(priv->caption2));
288 gtk_combo_box_set_active(GTK_COMBO_BOX(combo_order), order);
292 * hildon_sort_dialog_add_sort_key:
293 * @dialog: the #HildonSortDialog widget.
294 * @sort_key: Combo box's index value
296 * Adds a new sort key and returns the respective index in
299 * Return value: An integer which is the index of the added combo box's
303 hildon_sort_dialog_add_sort_key(HildonSortDialog * dialog,
304 const gchar * sort_key)
306 HildonSortDialogPrivate *priv;
308 g_return_val_if_fail(HILDON_IS_SORT_DIALOG(dialog), -1);
310 priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
311 gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo1), sort_key);
313 if (priv->index_first == TRUE) {
314 priv->index_first = FALSE;
315 return priv->index_counter;
318 return priv->index_counter += 1;