GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
search-double.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-double.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_double_class_init (GNCSearchDoubleClass *klass);
47 static void gnc_search_double_init (GNCSearchDouble *gspaper);
48 static void gnc_search_double_finalize (GObject *obj);
49 
51 
53 {
54  GtkWidget * entry;
55  GNCAmountEdit *gae;
56 };
57 
58 #define _PRIVATE(o) \
59  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GNC_TYPE_SEARCH_DOUBLE, GNCSearchDoublePrivate))
60 
61 static GNCSearchCoreTypeClass *parent_class;
62 
63 GType
64 gnc_search_double_get_type (void)
65 {
66  static GType type = 0;
67 
68  if (!type)
69  {
70  GTypeInfo type_info =
71  {
72  sizeof(GNCSearchDoubleClass), /* class_size */
73  NULL, /* base_init */
74  NULL, /* base_finalize */
75  (GClassInitFunc)gnc_search_double_class_init,
76  NULL, /* class_finalize */
77  NULL, /* class_data */
78  sizeof(GNCSearchDouble), /* */
79  0, /* n_preallocs */
80  (GInstanceInitFunc)gnc_search_double_init,
81  };
82 
83  type = g_type_register_static (GNC_TYPE_SEARCH_CORE_TYPE,
84  "GNCSearchDouble",
85  &type_info, 0);
86  }
87 
88  return type;
89 }
90 
91 static void
92 gnc_search_double_class_init (GNCSearchDoubleClass *klass)
93 {
94  GObjectClass *object_class;
95  GNCSearchCoreTypeClass *gnc_search_core_type = (GNCSearchCoreTypeClass *)klass;
96 
97  object_class = G_OBJECT_CLASS (klass);
98  parent_class = g_type_class_peek_parent (klass);
99 
100  object_class->finalize = gnc_search_double_finalize;
101 
102  /* override methods */
103  gnc_search_core_type->editable_enters = editable_enters;
104  gnc_search_core_type->grab_focus = grab_focus;
105  gnc_search_core_type->validate = gncs_validate;
106  gnc_search_core_type->get_widget = gncs_get_widget;
107  gnc_search_core_type->get_predicate = gncs_get_predicate;
108  gnc_search_core_type->clone = gncs_clone;
109 
110  g_type_class_add_private(klass, sizeof(GNCSearchDoublePrivate));
111 }
112 
113 static void
114 gnc_search_double_init (GNCSearchDouble *o)
115 {
116  o->how = QOF_COMPARE_EQUAL;
117 }
118 
119 static void
120 gnc_search_double_finalize (GObject *obj)
121 {
122  GNCSearchDouble *o = (GNCSearchDouble *)obj;
123  g_assert (IS_GNCSEARCH_DOUBLE (o));
124 
125  G_OBJECT_CLASS (parent_class)->finalize(obj);
126 }
127 
136 gnc_search_double_new (void)
137 {
138  GNCSearchDouble *o = g_object_new(GNC_TYPE_SEARCH_DOUBLE, NULL);
139  return o;
140 }
141 
142 void
143 gnc_search_double_set_value (GNCSearchDouble *fi, double value)
144 {
145  g_return_if_fail (fi);
146  g_return_if_fail (IS_GNCSEARCH_DOUBLE (fi));
147 
148  fi->value = value;
149 }
150 
151 void
152 gnc_search_double_set_how (GNCSearchDouble *fi, QofQueryCompare how)
153 {
154  g_return_if_fail (fi);
155  g_return_if_fail (IS_GNCSEARCH_DOUBLE (fi));
156  fi->how = how;
157 }
158 
159 static gboolean
160 gncs_validate (GNCSearchCoreType *fe)
161 {
162  GNCSearchDouble *fi = (GNCSearchDouble *)fe;
163  gboolean valid = TRUE;
164 
165  g_return_val_if_fail (fi, FALSE);
166  g_return_val_if_fail (IS_GNCSEARCH_DOUBLE (fi), FALSE);
167 
168  /* XXX */
169 
170  return valid;
171 }
172 
173 static void
174 entry_changed (GNCAmountEdit *entry, GNCSearchDouble *fe)
175 {
176  fe->value = gnc_amount_edit_get_damount (entry);
177 }
178 
179 static GtkWidget *
180 make_menu (GNCSearchCoreType *fe)
181 {
182  GNCSearchDouble *fi = (GNCSearchDouble *)fe;
183  GtkComboBox *combo;
184 
185  combo = GTK_COMBO_BOX(gnc_combo_box_new_search());
186 
187  gnc_combo_box_search_add(combo, _("is less than"), QOF_COMPARE_LT);
188  gnc_combo_box_search_add(combo, _("is less than or equal to"), QOF_COMPARE_LTE);
189  gnc_combo_box_search_add(combo, _("equals"), QOF_COMPARE_EQUAL);
190  gnc_combo_box_search_add(combo, _("does not equal"), QOF_COMPARE_NEQ);
191  gnc_combo_box_search_add(combo, _("is greater than"), QOF_COMPARE_GT);
192  gnc_combo_box_search_add(combo, _("is greater than or equal to"), QOF_COMPARE_GTE);
193  gnc_combo_box_search_changed(combo, &fi->how);
194  gnc_combo_box_search_set_active(combo, fi->how ? fi->how : QOF_COMPARE_LT);
195 
196  return GTK_WIDGET(combo);
197 }
198 
199 static void
200 grab_focus (GNCSearchCoreType *fe)
201 {
202  GNCSearchDouble *fi = (GNCSearchDouble *)fe;
203  GNCSearchDoublePrivate *priv ;
204 
205  g_return_if_fail (fi);
206  g_return_if_fail (IS_GNCSEARCH_DOUBLE (fi));
207 
208  priv = _PRIVATE(fi);
209  if (priv->entry)
210  gtk_widget_grab_focus (priv->entry);
211 }
212 
213 static void
214 editable_enters (GNCSearchCoreType *fe)
215 {
216  GNCSearchDouble *fi = (GNCSearchDouble *)fe;
217  GNCSearchDoublePrivate *priv ;
218 
219  g_return_if_fail (fi);
220  g_return_if_fail (IS_GNCSEARCH_DOUBLE (fi));
221 
222  priv = _PRIVATE(fi);
223  if (priv->entry)
224  gtk_entry_set_activates_default(GTK_ENTRY (priv->entry), TRUE);
225 }
226 
227 static GtkWidget *
228 gncs_get_widget (GNCSearchCoreType *fe)
229 {
230  GtkWidget *entry, *menu, *box;
231  GNCSearchDouble *fi = (GNCSearchDouble *)fe;
232  GNCSearchDoublePrivate *priv ;
233 
234  g_return_val_if_fail (fi, NULL);
235  g_return_val_if_fail (IS_GNCSEARCH_DOUBLE (fi), NULL);
236 
237  priv = _PRIVATE(fi);
238  box = gtk_hbox_new (FALSE, 3);
239 
240  /* Build and connect the option menu */
241  menu = make_menu (fe);
242  gtk_box_pack_start (GTK_BOX (box), menu, FALSE, FALSE, 3);
243 
244  /* Build and connect the entry window */
245  entry = gnc_amount_edit_new ();
246  if (fi->value)
247  gnc_amount_edit_set_damount (GNC_AMOUNT_EDIT (entry), fi->value);
248  g_signal_connect (G_OBJECT (entry), "amount_changed", G_CALLBACK (entry_changed), fe);
249  gtk_box_pack_start (GTK_BOX (box), entry, FALSE, FALSE, 3);
250  priv->entry = gnc_amount_edit_gtk_entry (GNC_AMOUNT_EDIT (entry));
251  priv->gae = GNC_AMOUNT_EDIT (entry);
252 
253  /* And return the box */
254  return box;
255 }
256 
257 static QofQueryPredData* gncs_get_predicate (GNCSearchCoreType *fe)
258 {
259  GNCSearchDouble *fi = (GNCSearchDouble *)fe;
260  GNCSearchDoublePrivate *priv ;
261 
262  g_return_val_if_fail (fi, NULL);
263  g_return_val_if_fail (IS_GNCSEARCH_DOUBLE (fi), NULL);
264 
265  /* force the computation of the entry, because we may not get the signal */
266  priv = _PRIVATE(fi);
267  entry_changed (priv->gae, fi);
268 
269  return qof_query_double_predicate (fi->how, fi->value);
270 }
271 
272 static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe)
273 {
274  GNCSearchDouble *se, *fse = (GNCSearchDouble *)fe;
275 
276  g_return_val_if_fail (fse, NULL);
277  g_return_val_if_fail (IS_GNCSEARCH_DOUBLE (fse), NULL);
278 
279  se = gnc_search_double_new ();
280  gnc_search_double_set_value (se, fse->value);
281  gnc_search_double_set_how (se, fse->how);
282 
283  return (GNCSearchCoreType *)se;
284 }
QofQueryCompare
Definition: qofquerycore.h:55