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.
42 #include <gtk/gtkcombobox.h>
43 #include <gtk/gtkbox.h>
44 #include <gdk/gdkkeysyms.h>
45 #include <glib-object.h>
47 #include <hildon-widgets/hildon-caption.h>
48 #include "hildon-sort-dialog.h"
51 #define _(String) dgettext(PACKAGE, String)
53 static GtkDialogClass *parent_class;
55 #define HILDON_SORT_DIALOG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE \
56 ((obj), HILDON_TYPE_SORT_DIALOG, HildonSortDialogPrivate));
58 typedef struct _HildonSortDialogPrivate HildonSortDialogPrivate;
60 static void hildon_sort_dialog_class_init(HildonSortDialogClass * class);
61 static void hildon_sort_dialog_init(HildonSortDialog * widget);
64 struct _HildonSortDialogPrivate {
73 /* OK/Cancel buttons */
75 GtkWidget *cancelButton;
77 /* Index value for sort_by */
80 /* Index value for sort_order */
84 /* Index value counter */
88 /* Private functions */
91 * Initialises the sort dialog class.
93 static void hildon_sort_dialog_class_init(HildonSortDialogClass * class)
95 parent_class = g_type_class_peek_parent(class);
96 g_type_class_add_private(class, sizeof(HildonSortDialogPrivate));
99 static void hildon_sort_dialog_init(HildonSortDialog * dialog)
101 HildonSortDialogPrivate *priv;
104 g_return_if_fail(HILDON_IS_SORT_DIALOG(dialog));
106 priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
108 priv->index_first = TRUE;
109 priv->index_counter = 0;
111 group = GTK_SIZE_GROUP(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL));
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"));
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);
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"));
132 priv->caption2 = hildon_caption_new(group, _("ckdg_fi_sort_order"),
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);
139 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combo1), 0);
140 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combo2), 0);
142 /* Create the OK/CANCEL buttons */
143 priv->okButton = gtk_dialog_add_button(GTK_DIALOG(dialog),
144 _("ckdg_bd_sort_dialog_ok"),
146 priv->cancelButton = gtk_dialog_add_button(GTK_DIALOG(dialog),
147 _("ckdg_bd_sort_dialog_cancel"),
148 GTK_RESPONSE_CANCEL);
150 gtk_window_resize(GTK_WINDOW(dialog), 370, 100);
151 gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
154 /* Public functions */
157 * hildon_sort_dialog_get_type:
159 * Returns GType for HildonSortDialog as produced by
160 * g_type_register_static().
162 * Return value: HildonSortDialog type
164 GType hildon_sort_dialog_get_type()
166 static GType dialog_type = 0;
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),
178 (GInstanceInitFunc) hildon_sort_dialog_init
181 dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
189 * hildon_sort_dialog_new:
190 * @parent: Widget to be transient for, or NULL if none.
192 * HildonSortDialog contains two #HildonCaption:s with combo boxes.
194 * Return value: pointer to a new @HildonSortDialog widget.
196 GtkWidget *hildon_sort_dialog_new(GtkWindow * parent)
198 GtkWidget *sort_dialog = g_object_new(HILDON_TYPE_SORT_DIALOG, NULL);
201 gtk_window_set_transient_for(GTK_WINDOW(sort_dialog), parent);
207 * hildon_sort_dialog_get_sort_key:
208 * @dialog: the #HildonSortDialog widget.
210 * Gets index to currently active sort key.
212 * Return value: An integer which is the index value of the "Sort by"
215 gint hildon_sort_dialog_get_sort_key(HildonSortDialog * dialog)
217 GtkWidget *combo_key;
218 HildonSortDialogPrivate *priv;
220 g_return_val_if_fail(HILDON_IS_SORT_DIALOG(dialog), -1);
222 priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
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;
231 * hildon_sort_dialog_get_sort_order:
232 * @dialog: the #HildonSortDialog widget.
234 * Gets current sorting order from "Sort order" field.
236 * Return value: current sorting order as #GtkSortType.
238 GtkSortType hildon_sort_dialog_get_sort_order(HildonSortDialog * dialog)
240 GtkWidget *combo_key;
241 HildonSortDialogPrivate *priv;
243 g_return_val_if_fail(HILDON_IS_SORT_DIALOG(dialog), 0);
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;
253 * hildon_sort_dialog_set_sort_key:
254 * @dialog: the #HildonSortDialog widget.
255 * @key: Combo box's index value.
257 * Sets the index value of the #HildonSortDialog widget.
259 void hildon_sort_dialog_set_sort_key(HildonSortDialog * dialog, gint key)
261 GtkWidget *combo_key;
262 HildonSortDialogPrivate *priv;
264 g_return_if_fail(HILDON_IS_SORT_DIALOG(dialog));
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);
272 * hildon_sort_dialog_set_sort_order:
273 * @dialog: the #HildonSortDialog widget.
274 * @order: Combo box's index value.
276 * Sets the index value of the #HildonSortDialog widget.
279 hildon_sort_dialog_set_sort_order(HildonSortDialog * dialog,
282 GtkWidget *combo_order;
283 HildonSortDialogPrivate *priv;
285 g_return_if_fail(HILDON_IS_SORT_DIALOG(dialog));
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);
293 * hildon_sort_dialog_add_sort_key:
294 * @dialog: the #HildonSortDialog widget.
295 * @sort_key: Combo box's index value
297 * Adds a new sort key and returns the respective index in
300 * Return value: An integer which is the index of the added combo box's
304 hildon_sort_dialog_add_sort_key(HildonSortDialog * dialog,
305 const gchar * sort_key)
307 HildonSortDialogPrivate *priv;
309 g_return_val_if_fail(HILDON_IS_SORT_DIALOG(dialog), -1);
311 priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
312 gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo1), sort_key);
314 if (priv->index_first == TRUE) {
315 priv->index_first = FALSE;
316 return priv->index_counter;
319 return priv->index_counter += 1;