517040eac78d6e42a26577586b84ed668f15401a
[claws.git] / src / gtk / combobox.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2006 Andrej Kacian and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <utils.h>
28
29 typedef struct _combobox_sel_by_data_ctx {
30         GtkComboBox *combobox;
31         gint data;
32 } ComboboxSelCtx;
33
34 static gboolean _select_by_data_func(GtkTreeModel *model,       GtkTreePath *path,
35                 GtkTreeIter *iter, ComboboxSelCtx *ctx)
36 {
37         GtkComboBox *combobox = ctx->combobox;
38         gint data = ctx->data;
39         gint curdata;
40
41         gtk_tree_model_get(model, iter, 1, &curdata, -1);
42         if (data == curdata) {
43                 gtk_combo_box_set_active_iter(combobox, iter);
44                 return TRUE;
45         }
46
47         return FALSE;
48 }
49
50 void combobox_select_by_data(GtkComboBox *combobox, gint data)
51 {
52         GtkTreeModel *model;
53         ComboboxSelCtx *ctx = NULL;
54         g_return_if_fail(combobox != NULL);
55
56         model = gtk_combo_box_get_model(combobox);
57
58         ctx = g_new(ComboboxSelCtx,
59                         sizeof(ComboboxSelCtx));
60         ctx->combobox = combobox;
61         ctx->data = data;
62
63         gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc)_select_by_data_func, ctx);
64         g_free(ctx);
65 }
66
67 gint combobox_get_active_data(GtkComboBox *combobox)
68 {
69         GtkTreeModel *model;
70         GtkTreeIter iter;
71         gint data;
72
73         g_return_val_if_fail(combobox != NULL, -1);
74
75         gtk_combo_box_get_active_iter(combobox, &iter);
76
77         model = gtk_combo_box_get_model(combobox);
78
79         gtk_tree_model_get(model, &iter, 1, &data, -1);
80
81         return data;
82 }
83
84 void combobox_unset_popdown_strings(GtkComboBox *combobox)
85 {
86         GtkTreeModel *model;
87         gint count, i;
88
89         g_return_if_fail(combobox != NULL);
90
91         model = gtk_combo_box_get_model(combobox);
92         count = gtk_tree_model_iter_n_children(model, NULL);
93         for (i = 0; i < count; i++)
94                 gtk_combo_box_remove_text(combobox, 0);
95 }
96
97 void combobox_set_popdown_strings(GtkComboBox *combobox,
98                                  GList       *list)
99 {
100         GList *cur;
101
102         g_return_if_fail(combobox != NULL);
103
104         for (cur = list; cur != NULL; cur = g_list_next(cur))
105                 gtk_combo_box_append_text(combobox, (const gchar*) cur->data);
106 }