GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
search-core-utils.c
1 /*
2  * search-core-utils.c -- common functions for search code
3  * Copyright (C) 2006 David Hampton <[email protected]>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (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, contact:
17  *
18  * Free Software Foundation Voice: +1-617-542-5942
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
20  * Boston, MA 02110-1301, USA [email protected]
21  *
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #include <gtk/gtk.h>
29 #include <glib/gi18n.h>
30 
31 #include "search-core-utils.h"
32 
33 
34 static void
35 search_combo_changed (GtkWidget *widget, gint *value)
36 {
37  GtkTreeModel *model;
38  GtkTreeIter iter;
39 
40  g_return_if_fail(GTK_IS_COMBO_BOX(widget));
41  g_return_if_fail(value);
42 
43  model = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
44  if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(widget), &iter))
45  return;
46 
47  gtk_tree_model_get(model, &iter,
48  GNC_COMBO_SEARCH_COL_VALUE, value,
49  -1);
50 }
51 
52 GtkWidget *
53 gnc_combo_box_new_search (void)
54 {
55  GtkWidget *combo;
56  GtkListStore *store;
57  GtkCellRenderer *renderer;
58 
59  store = gtk_list_store_new(NUM_GNC_COMBO_SEARCH_COLS, G_TYPE_STRING, G_TYPE_UINT);
60  combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
61  g_object_unref(store);
62 
63  renderer = gtk_cell_renderer_text_new ();
64  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
65  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
66  "text", GNC_COMBO_SEARCH_COL_TEXT,
67  NULL);
68  return combo;
69 }
70 
71 void
72 gnc_combo_box_search_add (GtkComboBox *combo, const gchar *text, guint value)
73 {
74  GtkListStore *store;
75  GtkTreeIter iter;
76 
77  g_return_if_fail(GTK_IS_COMBO_BOX(combo));
78  g_return_if_fail(text);
79 
80  store = GTK_LIST_STORE(gtk_combo_box_get_model(combo));
81  gtk_list_store_append(store, &iter);
82  gtk_list_store_set(store, &iter,
83  GNC_COMBO_SEARCH_COL_TEXT, text,
84  GNC_COMBO_SEARCH_COL_VALUE, value,
85  -1);
86 }
87 
88 guint
89 gnc_combo_box_search_get_active (GtkComboBox *combo)
90 {
91  GtkTreeModel *model;
92  GtkTreeIter iter;
93  guint value;
94 
95  g_return_val_if_fail(GTK_IS_COMBO_BOX(combo), 0);
96 
97  model = gtk_combo_box_get_model(combo);
98  if (!gtk_combo_box_get_active_iter(combo, &iter))
99  return 0;
100 
101  gtk_tree_model_get(model, &iter,
102  GNC_COMBO_SEARCH_COL_VALUE, &value,
103  -1);
104  return value;
105 }
106 
107 void
108 gnc_combo_box_search_set_active (GtkComboBox *combo, guint value)
109 {
110  GtkTreeModel *model;
111  GtkTreeIter iter;
112  guint row_value = 0;
113 
114  g_return_if_fail(GTK_IS_COMBO_BOX(combo));
115 
116  model = gtk_combo_box_get_model(combo);
117  if (!gtk_tree_model_get_iter_first(model, &iter))
118  return;
119 
120  do
121  {
122  gtk_tree_model_get(model, &iter,
123  GNC_COMBO_SEARCH_COL_VALUE, &row_value,
124  -1);
125  if (value == row_value)
126  {
127  gtk_combo_box_set_active_iter(combo, &iter);
128  return;
129  }
130  }
131  while (gtk_tree_model_iter_next(model, &iter));
132 
133  /* No match found. Select the first item. */
134  gtk_combo_box_set_active(combo, 0);
135 }
136 
137 void
138 gnc_combo_box_search_changed(GtkComboBox *combo, guint *value)
139 {
140  g_signal_connect (combo, "changed",
141  G_CALLBACK (search_combo_changed), value);
142 }