2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2006-2007 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 3 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, see <http://www.gnu.org/licenses/>.
25 #include <glib/gi18n.h>
26 #include <gdk/gdkkeysyms.h>
31 typedef struct _combobox_sel_by_data_ctx {
32 GtkComboBox *combobox;
36 static gboolean _select_by_data_func(GtkTreeModel *model, GtkTreePath *path,
37 GtkTreeIter *iter, ComboboxSelCtx *ctx)
39 GtkComboBox *combobox = ctx->combobox;
40 gint data = ctx->data;
43 gtk_tree_model_get(GTK_TREE_MODEL(model), iter, COMBOBOX_DATA, &curdata, -1);
44 if (data == curdata) {
45 gtk_combo_box_set_active_iter(combobox, iter);
52 void combobox_select_by_data(GtkComboBox *combobox, gint data)
55 ComboboxSelCtx *ctx = NULL;
56 g_return_if_fail(combobox != NULL);
58 model = gtk_combo_box_get_model(combobox);
60 ctx = g_new(ComboboxSelCtx,
61 sizeof(ComboboxSelCtx));
62 ctx->combobox = combobox;
65 gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc)_select_by_data_func, ctx);
69 gint combobox_get_active_data(GtkComboBox *combobox)
75 g_return_val_if_fail(combobox != NULL, -1);
77 g_return_val_if_fail(gtk_combo_box_get_active_iter(combobox, &iter), -1);
79 model = gtk_combo_box_get_model(combobox);
81 gtk_tree_model_get(model, &iter, COMBOBOX_DATA, &data, -1);
86 void combobox_unset_popdown_strings(GtkComboBox *combobox)
91 g_return_if_fail(combobox != NULL);
93 model = gtk_combo_box_get_model(combobox);
94 count = gtk_tree_model_iter_n_children(model, NULL);
95 for (i = 0; i < count; i++)
96 gtk_combo_box_remove_text(combobox, 0);
99 void combobox_set_popdown_strings(GtkComboBox *combobox,
104 g_return_if_fail(combobox != NULL);
106 for (cur = list; cur != NULL; cur = g_list_next(cur))
107 gtk_combo_box_append_text(combobox, (const gchar*) cur->data);
110 gboolean combobox_set_value_from_arrow_key(GtkComboBox *combobox,
112 /* used from key_press events upon gtk_combo_box_entry with one text column
113 (gtk_combo_box_new_text() and with GtkComboBoxEntry's for instance),
114 make sure that up and down arrow keys behave the same as old with old
117 if the current text in entry widget is not found in combo list,
118 get last value from combo list
119 if the current text in entry widget exists in combo list,
120 get prev value from combo list
122 if the current text in entry widget is not found in combo list,
123 get first value from combo list
124 if the current text in entry widget exists in combo list,
125 get next value from combo list
128 gboolean valid = FALSE;
130 g_return_val_if_fail(combobox != NULL, FALSE);
132 /* reproduce the behaviour of old gtk_combo_box */
133 GtkTreeModel *model = gtk_combo_box_get_model(combobox);
136 if (gtk_combo_box_get_active_iter(combobox, &iter)) {
137 /* if current text is in list, get prev or next one */
139 if (keyval == GDK_Up) {
140 gchar *text = gtk_combo_box_get_active_text(combobox);
142 text = gtk_editable_get_chars(GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(combobox))),0,-1);
143 valid = gtkut_tree_model_text_iter_prev(model, &iter, text);
146 if (keyval == GDK_Down)
147 valid = gtk_tree_model_iter_next(model, &iter);
150 gtk_combo_box_set_active_iter(combobox, &iter);
153 /* current text is not in list, get first or next one */
155 if (keyval == GDK_Up)
156 valid = gtkut_tree_model_get_iter_last(model, &iter);
158 if (keyval == GDK_Down)
159 valid = gtk_tree_model_get_iter_first(model, &iter);
162 gtk_combo_box_set_active_iter(combobox, &iter);
165 /* return TRUE if value could be set */