GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
search-boolean.c
1 /*
2  * Copyright (C) 2002 Derek Atkins
3  *
4  * Authors: Derek Atkins <[email protected]>
5  *
6  * Copyright (c) 2006 David Hampton <[email protected]>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
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 "qof.h"
32 
33 #include "search-boolean.h"
34 #include "search-core-utils.h"
35 
36 #define d(x)
37 
38 static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe);
39 static gboolean gncs_validate (GNCSearchCoreType *fe);
40 static GtkWidget *gncs_get_widget(GNCSearchCoreType *fe);
41 static QofQueryPredData* gncs_get_predicate (GNCSearchCoreType *fe);
42 
43 static void gnc_search_boolean_class_init (GNCSearchBooleanClass *klass);
44 static void gnc_search_boolean_init (GNCSearchBoolean *gspaper);
45 static void gnc_search_boolean_finalize (GObject *obj);
46 
48 
50 {
51  gpointer dummy;
52 };
53 
54 #define _PRIVATE(o) \
55  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GNC_TYPE_SEARCH_BOOLEAN, GNCSearchBooleanPrivate))
56 
57 static GNCSearchCoreTypeClass *parent_class;
58 
59 GType
60 gnc_search_boolean_get_type (void)
61 {
62  static GType type = 0;
63 
64  if (!type)
65  {
66  GTypeInfo type_info =
67  {
68  sizeof(GNCSearchBooleanClass), /* class_size */
69  NULL, /* base_init */
70  NULL, /* base_finalize */
71  (GClassInitFunc)gnc_search_boolean_class_init,
72  NULL, /* class_finalize */
73  NULL, /* class_data */
74  sizeof(GNCSearchBoolean), /* */
75  0, /* n_preallocs */
76  (GInstanceInitFunc)gnc_search_boolean_init,
77  };
78 
79  type = g_type_register_static (GNC_TYPE_SEARCH_CORE_TYPE,
80  "GNCSearchBoolean",
81  &type_info, 0);
82  }
83 
84  return type;
85 }
86 
87 static void
88 gnc_search_boolean_class_init (GNCSearchBooleanClass *klass)
89 {
90  GObjectClass *object_class;
91  GNCSearchCoreTypeClass *gnc_search_core_type = (GNCSearchCoreTypeClass *)klass;
92 
93  object_class = G_OBJECT_CLASS (klass);
94  parent_class = g_type_class_peek_parent (klass);
95 
96  object_class->finalize = gnc_search_boolean_finalize;
97 
98  /* override methods */
99  gnc_search_core_type->validate = gncs_validate;
100  gnc_search_core_type->get_widget = gncs_get_widget;
101  gnc_search_core_type->get_predicate = gncs_get_predicate;
102  gnc_search_core_type->clone = gncs_clone;
103 
104  g_type_class_add_private(klass, sizeof(GNCSearchBooleanPrivate));
105 }
106 
107 static void
108 gnc_search_boolean_init (GNCSearchBoolean *o)
109 {
110  o->how = QOF_COMPARE_EQUAL;
111  o->value = TRUE;
112 }
113 
114 static void
115 gnc_search_boolean_finalize (GObject *obj)
116 {
118  g_assert (IS_GNCSEARCH_BOOLEAN (o));
119 
120  G_OBJECT_CLASS (parent_class)->finalize(obj);
121 }
122 
131 gnc_search_boolean_new (void)
132 {
133  GNCSearchBoolean *o = g_object_new(GNC_TYPE_SEARCH_BOOLEAN, NULL);
134  return o;
135 }
136 
137 void
138 gnc_search_boolean_set_value (GNCSearchBoolean *fi, gboolean value)
139 {
140  g_return_if_fail (fi);
141  g_return_if_fail (IS_GNCSEARCH_BOOLEAN (fi));
142 
143  fi->value = value;
144 }
145 
146 void
147 gnc_search_boolean_set_how (GNCSearchBoolean *fi, QofQueryCompare how)
148 {
149  g_return_if_fail (fi);
150  g_return_if_fail (IS_GNCSEARCH_BOOLEAN (fi));
151  fi->how = how;
152 }
153 
154 static gboolean
155 gncs_validate (GNCSearchCoreType *fe)
156 {
158  gboolean valid = TRUE;
159 
160  g_return_val_if_fail (fi, FALSE);
161  g_return_val_if_fail (IS_GNCSEARCH_BOOLEAN (fi), FALSE);
162 
163  /* XXX */
164 
165  return valid;
166 }
167 
168 static void
169 toggle_changed (GtkToggleButton *button, GNCSearchBoolean *fe)
170 {
171  fe->value = gtk_toggle_button_get_active (button);
172 }
173 
174 static GtkWidget *
175 make_menu (GNCSearchCoreType *fe)
176 {
178  GtkComboBox *combo;
179 
180  combo = GTK_COMBO_BOX(gnc_combo_box_new_search());
181  gnc_combo_box_search_add(combo, _("is"), QOF_COMPARE_EQUAL);
182  gnc_combo_box_search_add(combo, _("is not"), QOF_COMPARE_NEQ);
183  gnc_combo_box_search_changed(combo, &fi->how);
184  gnc_combo_box_search_set_active(combo, fi->how ? fi->how : QOF_COMPARE_EQUAL);
185 
186  return GTK_WIDGET(combo);
187 }
188 
189 static GtkWidget *
190 gncs_get_widget (GNCSearchCoreType *fe)
191 {
192  GtkWidget *toggle, *menu, *box;
194 
195  g_return_val_if_fail (fi, NULL);
196  g_return_val_if_fail (IS_GNCSEARCH_BOOLEAN (fi), NULL);
197 
198  box = gtk_hbox_new (FALSE, 3);
199 
200  /* Build and connect the option menu */
201  menu = make_menu (fe);
202  gtk_box_pack_start (GTK_BOX (box), menu, FALSE, FALSE, 3);
203 
204  /* Build and connect the toggle */
205  toggle = gtk_toggle_button_new_with_label (_("set true"));
206  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), fi->value);
207  g_signal_connect (G_OBJECT (toggle), "toggled", G_CALLBACK (toggle_changed), fe);
208  gtk_box_pack_start (GTK_BOX (box), toggle, FALSE, FALSE, 3);
209 
210  /* And return the box */
211  return box;
212 }
213 
214 static QofQueryPredData* gncs_get_predicate (GNCSearchCoreType *fe)
215 {
217 
218  g_return_val_if_fail (fi, NULL);
219  g_return_val_if_fail (IS_GNCSEARCH_BOOLEAN (fi), NULL);
220 
221  return qof_query_boolean_predicate (fi->how, fi->value);
222 }
223 
224 static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe)
225 {
226  GNCSearchBoolean *se, *fse = (GNCSearchBoolean *)fe;
227 
228  g_return_val_if_fail (fse, NULL);
229  g_return_val_if_fail (IS_GNCSEARCH_BOOLEAN (fse), NULL);
230 
231  se = gnc_search_boolean_new ();
232  gnc_search_boolean_set_value (se, fse->value);
233  gnc_search_boolean_set_how (se, fse->how);
234 
235  return (GNCSearchCoreType *)se;
236 }
QofQueryCompare
Definition: qofquerycore.h:55