2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2006 Andrej Kacian and the Claws Mail team
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.
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.
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.
25 #include <glib/gi18n.h>
26 #include <gdk/gdkkeysyms.h>
30 typedef struct _combobox_sel_by_data_ctx {
31 GtkComboBox *combobox;
35 static gboolean _select_by_data_func(GtkTreeModel *model, GtkTreePath *path,
36 GtkTreeIter *iter, ComboboxSelCtx *ctx)
38 GtkComboBox *combobox = ctx->combobox;
39 gint data = ctx->data;
42 gtk_tree_model_get(model, iter, 1, &curdata, -1);
43 if (data == curdata) {
44 gtk_combo_box_set_active_iter(combobox, iter);
51 void combobox_select_by_data(GtkComboBox *combobox, gint data)
54 ComboboxSelCtx *ctx = NULL;
55 g_return_if_fail(combobox != NULL);
57 model = gtk_combo_box_get_model(combobox);
59 ctx = g_new(ComboboxSelCtx,
60 sizeof(ComboboxSelCtx));
61 ctx->combobox = combobox;
64 gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc)_select_by_data_func, ctx);
68 gint combobox_get_active_data(GtkComboBox *combobox)
74 g_return_val_if_fail(combobox != NULL, -1);
76 gtk_combo_box_get_active_iter(combobox, &iter);
78 model = gtk_combo_box_get_model(combobox);
80 gtk_tree_model_get(model, &iter, 1, &data, -1);
85 void combobox_unset_popdown_strings(GtkComboBox *combobox)
90 g_return_if_fail(combobox != NULL);
92 model = gtk_combo_box_get_model(combobox);
93 count = gtk_tree_model_iter_n_children(model, NULL);
94 for (i = 0; i < count; i++)
95 gtk_combo_box_remove_text(combobox, 0);
98 void combobox_set_popdown_strings(GtkComboBox *combobox,
103 g_return_if_fail(combobox != NULL);
105 for (cur = list; cur != NULL; cur = g_list_next(cur))
106 gtk_combo_box_append_text(combobox, (const gchar*) cur->data);
109 gboolean combobox_set_value_from_arrow_key(GtkComboBox *combobox,
111 /* used from key_press events upon gtk_combo_box_entry with one text column
112 (gtk_combo_box_new_text() and with GtkComboBoxEntry's for instance),
113 make sure that up and down arrow keys behave the same as old with old
116 if the current text in entry widget is not found in combo list,
117 get last value from combo list
118 if the current text in entry widget exists in combo list,
119 get prev value from combo list
121 if the current text in entry widget is not found in combo list,
122 get first value from combo list
123 if the current text in entry widget exists in combo list,
124 get next value from combo list
127 gboolean valid = FALSE;
129 g_return_val_if_fail(combobox != NULL, FALSE);
131 /* reproduce the behaviour of old gtk_combo_box */
132 GtkTreeModel *model = gtk_combo_box_get_model(combobox);
135 if (gtk_combo_box_get_active_iter(combobox, &iter)) {
136 /* if current text is in list, get prev or next one */
138 if (keyval == GDK_Up)
139 valid = gtkut_tree_model_text_iter_prev(model, &iter,
140 gtk_combo_box_get_active_text(combobox));
142 if (keyval == GDK_Down)
143 valid = gtk_tree_model_iter_next(model, &iter);
146 gtk_combo_box_set_active_iter(combobox, &iter);
149 /* current text is not in list, get first or next one */
151 if (keyval == GDK_Up)
152 valid = gtkut_tree_model_get_iter_last(model, &iter);
154 if (keyval == GDK_Down)
155 valid = gtk_tree_model_get_iter_first(model, &iter);
158 gtk_combo_box_set_active_iter(combobox, &iter);
161 /* return TRUE if value could be set */