GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
search-numeric.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 "gnc-amount-edit.h"
32 #include "qof.h"
33 
34 #include "search-numeric.h"
35 #include "search-core-utils.h"
36 
37 #define d(x)
38 
39 static void editable_enters (GNCSearchCoreType *fe);
40 static void grab_focus (GNCSearchCoreType *fe);
41 static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe);
42 static gboolean gncs_validate (GNCSearchCoreType *fe);
43 static GtkWidget *gncs_get_widget(GNCSearchCoreType *fe);
44 static QofQueryPredData* gncs_get_predicate (GNCSearchCoreType *fe);
45 
46 static void gnc_search_numeric_class_init (GNCSearchNumericClass *klass);
47 static void gnc_search_numeric_init (GNCSearchNumeric *gspaper);
48 static void gnc_search_numeric_finalize (GObject *obj);
49 
51 
53 {
54  gboolean is_debcred;
55  GtkWidget * entry;
56  GNCAmountEdit *gae;
57 };
58 
59 #define _PRIVATE(o) \
60  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GNC_TYPE_SEARCH_NUMERIC, GNCSearchNumericPrivate))
61 
62 static GNCSearchCoreTypeClass *parent_class;
63 
64 GType
65 gnc_search_numeric_get_type (void)
66 {
67  static GType type = 0;
68 
69  if (!type)
70  {
71  GTypeInfo type_info =
72  {
73  sizeof(GNCSearchNumericClass), /* class_size */
74  NULL, /* base_init */
75  NULL, /* base_finalize */
76  (GClassInitFunc)gnc_search_numeric_class_init,
77  NULL, /* class_finalize */
78  NULL, /* class_data */
79  sizeof(GNCSearchNumeric), /* */
80  0, /* n_preallocs */
81  (GInstanceInitFunc)gnc_search_numeric_init,
82  };
83 
84  type = g_type_register_static (GNC_TYPE_SEARCH_CORE_TYPE,
85  "GNCSearchNumeric",
86  &type_info, 0);
87  }
88 
89  return type;
90 }
91 
92 static void
93 gnc_search_numeric_class_init (GNCSearchNumericClass *klass)
94 {
95  GObjectClass *object_class;
96  GNCSearchCoreTypeClass *gnc_search_core_type = (GNCSearchCoreTypeClass *)klass;
97 
98  object_class = G_OBJECT_CLASS (klass);
99  parent_class = g_type_class_peek_parent (klass);
100 
101  object_class->finalize = gnc_search_numeric_finalize;
102 
103  /* override methods */
104  gnc_search_core_type->editable_enters = editable_enters;
105  gnc_search_core_type->grab_focus = grab_focus;
106  gnc_search_core_type->validate = gncs_validate;
107  gnc_search_core_type->get_widget = gncs_get_widget;
108  gnc_search_core_type->get_predicate = gncs_get_predicate;
109  gnc_search_core_type->clone = gncs_clone;
110 
111  g_type_class_add_private(klass, sizeof(GNCSearchNumericPrivate));
112 }
113 
114 static void
115 gnc_search_numeric_init (GNCSearchNumeric *o)
116 {
117  o->value = gnc_numeric_zero ();
118  o->how = QOF_COMPARE_EQUAL;
119  o->option = QOF_NUMERIC_MATCH_ANY;
120 }
121 
122 static void
123 gnc_search_numeric_finalize (GObject *obj)
124 {
126  g_assert (IS_GNCSEARCH_NUMERIC (o));
127 
128  G_OBJECT_CLASS (parent_class)->finalize(obj);
129 }
130 
139 gnc_search_numeric_new (void)
140 {
141  GNCSearchNumeric *o = g_object_new(GNC_TYPE_SEARCH_NUMERIC, NULL);
142  return o;
143 }
144 
153 gnc_search_numeric_debcred_new (void)
154 {
155  GNCSearchNumeric *o;
157 
158  o = g_object_new(GNC_TYPE_SEARCH_NUMERIC, NULL);
159  priv = _PRIVATE(o);
160  priv->is_debcred = TRUE;
161  return o;
162 }
163 
164 void
165 gnc_search_numeric_set_value (GNCSearchNumeric *fi, gnc_numeric value)
166 {
167  g_return_if_fail (fi);
168  g_return_if_fail (IS_GNCSEARCH_NUMERIC (fi));
169 
170  fi->value = value;
171 }
172 
173 void
174 gnc_search_numeric_set_how (GNCSearchNumeric *fi, QofQueryCompare how)
175 {
176  g_return_if_fail (fi);
177  g_return_if_fail (IS_GNCSEARCH_NUMERIC (fi));
178  fi->how = how;
179 }
180 
181 void
182 gnc_search_numeric_set_option (GNCSearchNumeric *fi, QofNumericMatch option)
183 {
184  g_return_if_fail (fi);
185  g_return_if_fail (IS_GNCSEARCH_NUMERIC (fi));
186  fi->option = option;
187 }
188 
189 static gboolean
190 gncs_validate (GNCSearchCoreType *fe)
191 {
193  gboolean valid = TRUE;
194 
195  g_return_val_if_fail (fi, FALSE);
196  g_return_val_if_fail (IS_GNCSEARCH_NUMERIC (fi), FALSE);
197 
198  /* XXX */
199 
200  return valid;
201 }
202 
203 static void
204 entry_changed (GNCAmountEdit *entry, GNCSearchNumeric *fe)
205 {
206  fe->value = gnc_amount_edit_get_amount (entry);
207 }
208 
209 static GtkWidget *
210 make_how_menu (GNCSearchCoreType *fe)
211 {
214  GtkComboBox *combo;
215 
216  priv = _PRIVATE(fi);
217 
218  combo = GTK_COMBO_BOX(gnc_combo_box_new_search());
219  gnc_combo_box_search_add(combo, (priv->is_debcred ?
220  _("less than") : _("is less than")),
221  QOF_COMPARE_LT);
222  gnc_combo_box_search_add(combo, (priv->is_debcred ?
223  _("less than or equal to") :
224  _("is less than or equal to")),
225  QOF_COMPARE_LTE);
226  gnc_combo_box_search_add(combo, (priv->is_debcred ?
227  _("equal to") : _("equals")),
228  QOF_COMPARE_EQUAL);
229  gnc_combo_box_search_add(combo, (priv->is_debcred ?
230  _("not equal to") : _("does not equal")),
231  QOF_COMPARE_NEQ);
232  gnc_combo_box_search_add(combo, (priv->is_debcred ?
233  _("greater than") : _("is greater than")),
234  QOF_COMPARE_GT);
235  gnc_combo_box_search_add(combo, (priv->is_debcred ?
236  _("greater than or equal to") :
237  _("is greater than or equal to")),
238  QOF_COMPARE_GTE);
239 
240  gnc_combo_box_search_changed(combo, &fi->how);
241  gnc_combo_box_search_set_active(combo, fi->how ? fi->how : QOF_COMPARE_LT);
242 
243  return GTK_WIDGET(combo);
244 }
245 
246 static GtkWidget *
247 make_option_menu (GNCSearchCoreType *fe)
248 {
250  GtkComboBox *combo;
251 
252  combo = GTK_COMBO_BOX(gnc_combo_box_new_search());
253  gnc_combo_box_search_add(combo, _("has credits or debits"), QOF_NUMERIC_MATCH_ANY);
254  gnc_combo_box_search_add(combo, _("has debits"), QOF_NUMERIC_MATCH_DEBIT);
255  gnc_combo_box_search_add(combo, _("has credits"), QOF_NUMERIC_MATCH_CREDIT);
256  gnc_combo_box_search_changed(combo, &fi->option);
257  gnc_combo_box_search_set_active(combo, fi->option ? fi->option : QOF_NUMERIC_MATCH_ANY);
258 
259  return GTK_WIDGET(combo);
260 }
261 
262 static void
263 grab_focus (GNCSearchCoreType *fe)
264 {
267 
268  g_return_if_fail (fi);
269  g_return_if_fail (IS_GNCSEARCH_NUMERIC (fi));
270 
271  priv = _PRIVATE(fi);
272  if (priv->entry)
273  gtk_widget_grab_focus (priv->entry);
274 }
275 
276 static void
277 editable_enters (GNCSearchCoreType *fe)
278 {
281 
282  g_return_if_fail (fi);
283  g_return_if_fail (IS_GNCSEARCH_NUMERIC (fi));
284 
285  priv = _PRIVATE(fi);
286  if (priv->entry)
287  gtk_entry_set_activates_default(GTK_ENTRY (priv->entry), TRUE);
288 }
289 
290 static GtkWidget *
291 gncs_get_widget (GNCSearchCoreType *fe)
292 {
293  GtkWidget *entry, *menu, *box;
296 
297  g_return_val_if_fail (fi, NULL);
298  g_return_val_if_fail (IS_GNCSEARCH_NUMERIC (fi), NULL);
299 
300  priv = _PRIVATE(fi);
301  box = gtk_hbox_new (FALSE, 3);
302 
303  /* Build and connect the option menu(s) */
304  if (priv->is_debcred)
305  {
306  menu = make_option_menu (fe);
307  gtk_box_pack_start (GTK_BOX (box), menu, FALSE, FALSE, 3);
308  }
309 
310  menu = make_how_menu (fe);
311  gtk_box_pack_start (GTK_BOX (box), menu, FALSE, FALSE, 3);
312 
313  /* Build and connect the entry window */
314  entry = gnc_amount_edit_new ();
315  gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT (entry), fi->value);
316  g_signal_connect (G_OBJECT (entry), "amount_changed", G_CALLBACK (entry_changed), fe);
317  gtk_box_pack_start (GTK_BOX (box), entry, FALSE, FALSE, 3);
318  priv->gae = GNC_AMOUNT_EDIT (entry);
319  priv->entry = gnc_amount_edit_gtk_entry (GNC_AMOUNT_EDIT (entry));
320 
321  /* And return the box */
322  return box;
323 }
324 
325 static QofQueryPredData* gncs_get_predicate (GNCSearchCoreType *fe)
326 {
329 
330  g_return_val_if_fail (fi, NULL);
331  g_return_val_if_fail (IS_GNCSEARCH_NUMERIC (fi), NULL);
332 
333  /* force the computation of the entry, because we may not get the signal */
334  priv = _PRIVATE(fi);
335  entry_changed (priv->gae, fi);
336 
337  return qof_query_numeric_predicate (fi->how, fi->option, fi->value);
338 }
339 
340 static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe)
341 {
342  GNCSearchNumeric *se, *fse = (GNCSearchNumeric *)fe;
343  GNCSearchNumericPrivate *se_priv, *fse_priv;
344 
345  g_return_val_if_fail (fse, NULL);
346  g_return_val_if_fail (IS_GNCSEARCH_NUMERIC (fse), NULL);
347  fse_priv = _PRIVATE(fse);
348 
349  se = gnc_search_numeric_new ();
350  gnc_search_numeric_set_value (se, fse->value);
351  gnc_search_numeric_set_how (se, fse->how);
352  se_priv = _PRIVATE(se);
353  gnc_search_numeric_set_option (se, fse->option);
354  se_priv->is_debcred = fse_priv->is_debcred;
355 
356  return (GNCSearchCoreType *)se;
357 }
QofQueryCompare
Definition: qofquerycore.h:55
QofNumericMatch
Definition: qofquerycore.h:102