combobox.c (5672B)
1 /* 2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client 3 * Copyright (C) 2006-2012 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 3 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, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 #include <glib.h> 21 #include <glib/gi18n.h> 22 #include <gdk/gdkkeysyms.h> 23 #include <gtk/gtk.h> 24 #include "gtkutils.h" 25 #include "combobox.h" 26 #include "utils.h" 27 28 typedef struct _combobox_sel_by_data_ctx { 29 GtkComboBox *combobox; 30 gint data; 31 const gchar *cdata; 32 } ComboboxSelCtx; 33 34 GtkWidget *combobox_text_new(const gboolean with_entry, const gchar *text, ...) 35 { 36 GtkWidget *combo; 37 va_list args; 38 gchar *string; 39 40 if(text == NULL) 41 return NULL; 42 43 if (with_entry) 44 combo = gtk_combo_box_text_new_with_entry(); 45 else 46 combo = gtk_combo_box_text_new(); 47 gtk_widget_show(combo); 48 49 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), text); 50 va_start(args, text); 51 while ((string = va_arg(args, gchar*)) != NULL) 52 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), string); 53 va_end(args); 54 55 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0); 56 57 return combo; 58 } 59 60 static gboolean _select_by_data_func(GtkTreeModel *model, GtkTreePath *path, 61 GtkTreeIter *iter, ComboboxSelCtx *ctx) 62 { 63 GtkComboBox *combobox = ctx->combobox; 64 gint data = ctx->data; 65 gint curdata; 66 67 gtk_tree_model_get(GTK_TREE_MODEL(model), iter, COMBOBOX_DATA, &curdata, -1); 68 if (data == curdata) { 69 gtk_combo_box_set_active_iter(combobox, iter); 70 return TRUE; 71 } 72 73 return FALSE; 74 } 75 76 void combobox_select_by_data(GtkComboBox *combobox, gint data) 77 { 78 GtkTreeModel *model; 79 ComboboxSelCtx *ctx = NULL; 80 81 cm_return_if_fail(combobox != NULL); 82 cm_return_if_fail(GTK_IS_COMBO_BOX (combobox)); 83 84 model = gtk_combo_box_get_model(combobox); 85 86 ctx = g_new(ComboboxSelCtx, 87 sizeof(ComboboxSelCtx)); 88 ctx->combobox = combobox; 89 ctx->data = data; 90 91 gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc)_select_by_data_func, ctx); 92 g_free(ctx); 93 } 94 95 static gboolean _select_by_text_func(GtkTreeModel *model, GtkTreePath *path, 96 GtkTreeIter *iter, ComboboxSelCtx *ctx) 97 { 98 GtkComboBox *combobox = ctx->combobox; 99 const gchar *data = ctx->cdata; 100 gchar *curdata; 101 102 cm_return_val_if_fail(combobox != NULL, FALSE); 103 cm_return_val_if_fail(GTK_IS_COMBO_BOX (combobox), FALSE); 104 105 gtk_tree_model_get (GTK_TREE_MODEL(model), iter, 0, &curdata, -1); 106 if (!g_utf8_collate(data, curdata)) { 107 gtk_combo_box_set_active_iter(combobox, iter); 108 g_free(curdata); 109 return TRUE; 110 } else { 111 g_free(curdata); 112 } 113 114 return FALSE; 115 } 116 117 void combobox_select_by_text(GtkComboBox *combobox, const gchar *data) 118 { 119 GtkTreeModel *model; 120 ComboboxSelCtx *ctx = NULL; 121 122 cm_return_if_fail(combobox != NULL); 123 cm_return_if_fail(GTK_IS_COMBO_BOX (combobox)); 124 125 model = gtk_combo_box_get_model(combobox); 126 127 ctx = g_new(ComboboxSelCtx, 128 sizeof(ComboboxSelCtx)); 129 ctx->combobox = combobox; 130 ctx->cdata = data; 131 132 gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc)_select_by_text_func, ctx); 133 g_free(ctx); 134 } 135 136 gint combobox_get_active_data(GtkComboBox *combobox) 137 { 138 GtkTreeModel *model; 139 GtkTreeIter iter; 140 gint data; 141 142 cm_return_val_if_fail(combobox != NULL, -1); 143 cm_return_val_if_fail(GTK_IS_COMBO_BOX (combobox), -1); 144 cm_return_val_if_fail(gtk_combo_box_get_active_iter(combobox, &iter), -1); 145 146 model = gtk_combo_box_get_model(combobox); 147 148 gtk_tree_model_get(model, &iter, COMBOBOX_DATA, &data, -1); 149 150 return data; 151 } 152 153 void combobox_unset_popdown_strings(GtkComboBoxText *combobox) 154 { 155 GtkTreeModel *model; 156 gint count, i; 157 158 cm_return_if_fail(combobox != NULL); 159 cm_return_if_fail(GTK_IS_COMBO_BOX_TEXT (combobox)); 160 161 model = gtk_combo_box_get_model(GTK_COMBO_BOX(combobox)); 162 count = gtk_tree_model_iter_n_children(model, NULL); 163 for (i = 0; i < count; i++) 164 gtk_combo_box_text_remove(GTK_COMBO_BOX_TEXT(combobox), 0); 165 } 166 167 void combobox_set_popdown_strings(GtkComboBoxText *combobox, 168 GList *list) 169 { 170 GList *cur; 171 172 cm_return_if_fail(combobox != NULL); 173 cm_return_if_fail(GTK_IS_COMBO_BOX_TEXT (combobox)); 174 175 for (cur = list; cur != NULL; cur = g_list_next(cur)) 176 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combobox), 177 (const gchar*) cur->data); 178 } 179 180 static void store_set_sensitive(GtkTreeModel *model, GtkTreeIter *iter, 181 const gboolean sensitive) 182 { 183 if(GTK_IS_LIST_STORE(model)) { 184 gtk_list_store_set(GTK_LIST_STORE(model), iter, 185 COMBOBOX_SENS, sensitive, 186 -1); 187 } else { 188 gtk_tree_store_set(GTK_TREE_STORE(model), iter, 189 COMBOBOX_SENS, sensitive, 190 -1); 191 } 192 } 193 194 void combobox_set_sensitive(GtkComboBox *combobox, const guint index, 195 const gboolean sensitive) 196 { 197 GtkTreeModel *model; 198 GtkTreeIter iter, child; 199 guint i; 200 201 if((model = gtk_combo_box_get_model(combobox)) == NULL) 202 return; 203 204 if(gtk_tree_model_get_iter_first(model, &iter) == FALSE) 205 return; 206 for(i=0; i<index; i++) { 207 if(gtk_tree_model_iter_next(model, &iter) == FALSE) 208 return; 209 } 210 211 store_set_sensitive(model, &iter, sensitive); 212 213 if(gtk_tree_model_iter_children(model, &child, &iter) == FALSE) 214 return; 215 216 do { 217 store_set_sensitive(model, &child, sensitive); 218 } while (gtk_tree_model_iter_next(model, &child) == TRUE); 219 }