]> git.sur5r.net Git - glabels/blob - src/combo-util.c
Imported Upstream version 3.0.0
[glabels] / src / combo-util.c
1 /*
2  *  combo-util.c
3  *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "combo-util.h"
24
25 #include <string.h>
26
27 #include <libglabels.h>
28
29
30 /****************************************************************************/
31 /* Convienience function to set strings in a text combo_box from a GList    */
32 /****************************************************************************/
33 void
34 gl_combo_util_set_strings (GtkComboBoxText   *combo,
35                            GList             *list)
36 {
37         GList        *p;
38
39         g_return_if_fail (list);
40
41         gtk_combo_box_text_remove_all (combo);
42
43         for (p=list; p!=NULL; p=p->next) {
44                 if (p->data) {
45                         gtk_combo_box_text_append_text (combo, p->data);
46                 }
47         }
48 }
49
50
51 /*---------------------------------------------------------------------------*/
52 /* PRIVATE.  gl_combo_util_set_active_text support.                          */
53 /*---------------------------------------------------------------------------*/
54
55 typedef struct {
56   const gchar *text;
57   GtkTreeIter  iter;
58   gboolean     found;
59 } TextSearchData;
60
61 static gboolean
62 search_text_func (GtkTreeModel *model,
63                   GtkTreePath  *path,
64                   GtkTreeIter  *iter,
65                   gpointer      data)
66 {
67   TextSearchData *search_data = (TextSearchData *)data;
68   gchar          *text = NULL;
69
70   gtk_tree_model_get (model, iter, 0, &text, -1);
71
72   if (strcmp (text,search_data->text) == 0) {
73     search_data->found = TRUE;
74     search_data->iter  = *iter;
75   }
76
77   g_free (text);
78   
79   return FALSE;
80 }
81
82
83 /****************************************************************************/
84 /* Convienience function to set active text in a text combo_box from text   */
85 /****************************************************************************/
86 void
87 gl_combo_util_set_active_text (GtkComboBox       *combo,
88                                const gchar       *text)
89 {
90         GtkTreeModel   *model = gtk_combo_box_get_model(combo);
91
92         g_return_if_fail (GTK_IS_LIST_STORE (model));
93
94         if (!text) {
95
96                 gtk_combo_box_set_active (combo, -1);
97
98         } else {
99                 TextSearchData  search_data;
100
101                 search_data.text        = text;
102                 search_data.found       = FALSE;
103
104                 gtk_tree_model_foreach (model, search_text_func, &search_data);
105                 if (search_data.found) {
106                         gtk_combo_box_set_active_iter (combo,
107                                                        &search_data.iter);
108                 } else {
109                         gtk_combo_box_set_active (combo, -1);
110                 }    
111
112         }
113
114 }
115
116
117
118 /*
119  * Local Variables:       -- emacs
120  * mode: C                -- emacs
121  * c-basic-offset: 8      -- emacs
122  * tab-width: 8           -- emacs
123  * indent-tabs-mode: nil  -- emacs
124  * End:                   -- emacs
125  */