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