]> git.sur5r.net Git - glabels/blob - src/combo-util.c
Update po files to allow me to push this branch.
[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 (GtkComboBox       *combo,
35                            GList             *list)
36 {
37         GtkTreeModel *model;
38         GList        *p;
39
40         g_return_if_fail (list);
41
42         model = gtk_combo_box_get_model(combo);
43         gtk_list_store_clear (GTK_LIST_STORE (model));
44
45         for (p=list; p!=NULL; p=p->next) {
46                 if (p->data) {
47                         gtk_combo_box_append_text (combo, p->data);
48                 }
49         }
50 }
51
52
53 /*---------------------------------------------------------------------------*/
54 /* PRIVATE.  gl_combo_util_set_active_text support.                          */
55 /*---------------------------------------------------------------------------*/
56
57 typedef struct {
58   const gchar *text;
59   GtkTreeIter  iter;
60   gboolean     found;
61 } TextSearchData;
62
63 static gboolean
64 search_text_func (GtkTreeModel *model,
65                   GtkTreePath  *path,
66                   GtkTreeIter  *iter,
67                   gpointer      data)
68 {
69   TextSearchData *search_data = (TextSearchData *)data;
70   gchar          *text = NULL;
71
72   gtk_tree_model_get (model, iter, 0, &text, -1);
73
74   if (strcmp (text,search_data->text) == 0) {
75     search_data->found = TRUE;
76     search_data->iter  = *iter;
77   }
78
79   g_free (text);
80   
81   return FALSE;
82 }
83
84
85 /****************************************************************************/
86 /* Convienience function to set active text in a text combo_box from text   */
87 /****************************************************************************/
88 void
89 gl_combo_util_set_active_text (GtkComboBox       *combo,
90                                const gchar       *text)
91 {
92         GtkTreeModel   *model = gtk_combo_box_get_model(combo);
93
94         g_return_if_fail (GTK_IS_LIST_STORE (model));
95
96         if (!text) {
97
98                 gtk_combo_box_set_active (combo, -1);
99
100         } else {
101                 TextSearchData  search_data;
102
103                 search_data.text        = text;
104                 search_data.found       = FALSE;
105
106                 gtk_tree_model_foreach (model, search_text_func, &search_data);
107                 if (search_data.found) {
108                         gtk_combo_box_set_active_iter (combo,
109                                                        &search_data.iter);
110                 } else {
111                         gtk_combo_box_set_active (combo, -1);
112                 }    
113
114         }
115
116 }
117
118
119 /****************************************************************************/
120 /* Convienience function to add a simple text model to an existing          */
121 /* combo_box.  This is needed since combo_boxes created with glade do not   */
122 /* use the gtk_combo_box_new_text() constructor.                            */
123 /****************************************************************************/
124 void
125 gl_combo_util_add_text_model (GtkComboBox       *combo)
126 {
127         GtkCellRenderer *cell;
128         GtkListStore *store;
129
130         store = gtk_list_store_new (1, G_TYPE_STRING);
131         gtk_combo_box_set_model (combo, GTK_TREE_MODEL (store));
132         g_object_unref (store);
133
134         cell = gtk_cell_renderer_text_new ();
135         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
136         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
137                                         "text", 0,
138                                         NULL);
139 }
140
141
142
143 /*
144  * Local Variables:       -- emacs
145  * mode: C                -- emacs
146  * c-basic-offset: 8      -- emacs
147  * tab-width: 8           -- emacs
148  * indent-tabs-mode: nil  -- emacs
149  * End:                   -- emacs
150  */