Use GTK+ single includes
[modest] / src / gnome / modest-gnome-sort-dialog.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <glib/gi18n.h>
31 #include "modest-gnome-sort-dialog.h"
32 #include "widgets/modest-sort-criterium-view.h"
33 #include <gtk/gtk.h>
34
35 static gint    modest_gnome_sort_dialog_add_sort_key        (ModestSortCriteriumView *self,
36                                                                       const gchar *sort_key);
37 static void    modest_gnome_sort_dialog_set_sort_key (ModestSortCriteriumView *self, gint key);
38 static void    modest_gnome_sort_dialog_set_sort_order (ModestSortCriteriumView *self, GtkSortType sort_type);
39 static gint    modest_gnome_sort_dialog_get_sort_key (ModestSortCriteriumView *self);
40 static GtkSortType modest_gnome_sort_dialog_get_sort_order (ModestSortCriteriumView *self);
41 static void modest_sort_criterium_view_init (gpointer g_iface, gpointer iface_data);
42
43 typedef struct _ModestGnomeSortDialogPrivate ModestGnomeSortDialogPrivate;
44 struct _ModestGnomeSortDialogPrivate {
45         GtkWidget *sort_field_combo;
46         GtkWidget *sort_type_combo;
47         gint sort_field_next;
48 };
49
50 #define MODEST_GNOME_SORT_DIALOG_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
51                                                                                   MODEST_TYPE_GNOME_SORT_DIALOG, \
52                                                                                  ModestGnomeSortDialogPrivate))
53
54
55 G_DEFINE_TYPE_EXTENDED (ModestGnomeSortDialog, 
56                         modest_gnome_sort_dialog, 
57                         GTK_TYPE_DIALOG,
58                         0,
59                         G_IMPLEMENT_INTERFACE (MODEST_TYPE_SORT_CRITERIUM_VIEW, modest_sort_criterium_view_init));
60
61 static void
62 modest_gnome_sort_dialog_finalize (GObject *object)
63 {
64         G_OBJECT_CLASS (modest_gnome_sort_dialog_parent_class)->finalize (object);
65 }
66
67 static void
68 modest_gnome_sort_dialog_class_init (ModestGnomeSortDialogClass *klass)
69 {
70         GObjectClass *object_class = G_OBJECT_CLASS (klass);
71
72         object_class->finalize = modest_gnome_sort_dialog_finalize;
73
74         g_type_class_add_private (object_class, sizeof(ModestGnomeSortDialogPrivate));
75
76 }
77
78 static void
79 modest_gnome_sort_dialog_init (ModestGnomeSortDialog *self)
80 {
81         GtkWidget *vbox;
82         GtkStockItem item;
83         ModestGnomeSortDialogPrivate *priv = MODEST_GNOME_SORT_DIALOG_GET_PRIVATE (self);
84
85         gtk_dialog_add_buttons (GTK_DIALOG (self),
86                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
87                                 GTK_STOCK_OK, GTK_RESPONSE_OK,
88                                 NULL);
89         vbox = gtk_vbox_new (FALSE, 3);
90         priv->sort_field_combo = gtk_combo_box_new_text ();
91         priv->sort_field_next = 0;
92         priv->sort_type_combo = gtk_combo_box_new_text ();
93         gtk_stock_lookup (GTK_STOCK_SORT_ASCENDING, &item);
94         gtk_combo_box_insert_text (GTK_COMBO_BOX (priv->sort_type_combo), GTK_SORT_ASCENDING, _(item.label));
95         gtk_stock_lookup (GTK_STOCK_SORT_DESCENDING, &item);
96         gtk_combo_box_insert_text (GTK_COMBO_BOX (priv->sort_type_combo), GTK_SORT_DESCENDING, _(item.label));
97
98         gtk_box_pack_start (GTK_BOX (vbox), priv->sort_field_combo, FALSE, FALSE, 0);
99         gtk_box_pack_start (GTK_BOX (vbox), priv->sort_type_combo, FALSE, FALSE, 0);
100
101         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (self)->vbox), vbox, FALSE, FALSE, 0);
102         gtk_window_set_title (GTK_WINDOW (self), _("mcen_me_inbox_sort"));
103
104         gtk_widget_show_all (vbox);
105 }
106
107 static gint
108 modest_gnome_sort_dialog_add_sort_key        (ModestSortCriteriumView *self,
109                                                const gchar *sort_key)
110 {
111         ModestGnomeSortDialogPrivate *priv;
112         gint retval;
113
114         g_return_val_if_fail (MODEST_IS_GNOME_SORT_DIALOG (self), 0);
115         priv = MODEST_GNOME_SORT_DIALOG_GET_PRIVATE (self);
116
117         retval = priv->sort_field_next;
118         priv->sort_field_next++;
119
120         gtk_combo_box_insert_text (GTK_COMBO_BOX (priv->sort_field_combo), retval, sort_key);
121
122         return retval;
123 }
124
125 static void
126 modest_gnome_sort_dialog_set_sort_key (ModestSortCriteriumView *self, gint key)
127 {
128         ModestGnomeSortDialogPrivate *priv;
129
130         g_return_if_fail (MODEST_IS_GNOME_SORT_DIALOG (self));
131         priv = MODEST_GNOME_SORT_DIALOG_GET_PRIVATE (self);
132
133         gtk_combo_box_set_active (GTK_COMBO_BOX (priv->sort_field_combo), key);
134 }
135
136 static void
137 modest_gnome_sort_dialog_set_sort_order (ModestSortCriteriumView *self, GtkSortType sort_type)
138 {
139         ModestGnomeSortDialogPrivate *priv;
140
141         g_return_if_fail (MODEST_IS_GNOME_SORT_DIALOG (self));
142         priv = MODEST_GNOME_SORT_DIALOG_GET_PRIVATE (self);
143
144         gtk_combo_box_set_active (GTK_COMBO_BOX (priv->sort_type_combo), sort_type);
145 }
146
147 static gint
148 modest_gnome_sort_dialog_get_sort_key (ModestSortCriteriumView *self)
149 {
150         ModestGnomeSortDialogPrivate *priv;
151
152         g_return_val_if_fail (MODEST_IS_GNOME_SORT_DIALOG (self), 0);
153         priv = MODEST_GNOME_SORT_DIALOG_GET_PRIVATE (self);
154
155         return gtk_combo_box_get_active (GTK_COMBO_BOX (priv->sort_field_combo));
156 }
157
158 static GtkSortType 
159 modest_gnome_sort_dialog_get_sort_order (ModestSortCriteriumView *self)
160 {
161         ModestGnomeSortDialogPrivate *priv;
162
163         g_return_val_if_fail (MODEST_IS_GNOME_SORT_DIALOG (self), GTK_SORT_ASCENDING);
164         priv = MODEST_GNOME_SORT_DIALOG_GET_PRIVATE (self);
165
166         return gtk_combo_box_get_active (GTK_COMBO_BOX (priv->sort_type_combo));
167 }
168
169 static void
170 modest_sort_criterium_view_init (gpointer g_iface,
171                                  gpointer iface_data)
172 {
173         ModestSortCriteriumViewIface *iface = (ModestSortCriteriumViewIface *) g_iface;
174
175         iface->add_sort_key_func = modest_gnome_sort_dialog_add_sort_key;
176         iface->get_sort_key_func = modest_gnome_sort_dialog_get_sort_key;
177         iface->set_sort_key_func = modest_gnome_sort_dialog_set_sort_key;
178         iface->get_sort_order_func = modest_gnome_sort_dialog_get_sort_order;
179         iface->set_sort_order_func = modest_gnome_sort_dialog_set_sort_order;
180 }
181
182 GtkWidget*
183 modest_gnome_sort_dialog_new (GtkWindow *parent)
184 {
185         GtkWidget *result = g_object_new (MODEST_TYPE_GNOME_SORT_DIALOG, NULL);
186
187
188         if (parent)
189                 gtk_window_set_transient_for(GTK_WINDOW(result), parent);
190
191         return result;
192 }
193