Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2002 Derek Atkins
3 : : *
4 : : * Authors: Derek Atkins <warlord@MIT.EDU>
5 : : *
6 : : * Copyright (c) 2006 David Hampton <hampton@employees.org>
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 : : #include "gnc-gui-query.h"
34 : :
35 : : #include "search-int64.h"
36 : : #include "search-core-utils.h"
37 : :
38 : : #define d(x)
39 : :
40 : : static void pass_parent (GNCSearchCoreType *fe, gpointer parent);
41 : : static void editable_enters (GNCSearchCoreType *fe);
42 : : static void grab_focus (GNCSearchCoreType *fe);
43 : : static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe);
44 : : static gboolean gncs_validate (GNCSearchCoreType *fe);
45 : : static GtkWidget *gncs_get_widget(GNCSearchCoreType *fe);
46 : : static QofQueryPredData* gncs_get_predicate (GNCSearchCoreType *fe);
47 : :
48 : : static void gnc_search_int64_finalize (GObject *obj);
49 : :
50 : : struct _GNCSearchInt64
51 : : {
52 : : GNCSearchCoreType parent_instance;
53 : :
54 : : QofQueryCompare how;
55 : : gint64 value;
56 : :
57 : : GtkWidget *entry;
58 : : GNCAmountEdit *gae;
59 : : GtkWindow *parent;
60 : : };
61 : :
62 : 0 : G_DEFINE_TYPE(GNCSearchInt64, gnc_search_int64, GNC_TYPE_SEARCH_CORE_TYPE)
63 : :
64 : : static void
65 : 0 : gnc_search_int64_class_init (GNCSearchInt64Class *klass)
66 : : {
67 : : GObjectClass *object_class;
68 : 0 : GNCSearchCoreTypeClass *gnc_search_core_type = (GNCSearchCoreTypeClass *)klass;
69 : :
70 : 0 : object_class = G_OBJECT_CLASS (klass);
71 : :
72 : 0 : object_class->finalize = gnc_search_int64_finalize;
73 : :
74 : : /* override methods */
75 : 0 : gnc_search_core_type->pass_parent = pass_parent;
76 : 0 : gnc_search_core_type->editable_enters = editable_enters;
77 : 0 : gnc_search_core_type->grab_focus = grab_focus;
78 : 0 : gnc_search_core_type->validate = gncs_validate;
79 : 0 : gnc_search_core_type->get_widget = gncs_get_widget;
80 : 0 : gnc_search_core_type->get_predicate = gncs_get_predicate;
81 : 0 : gnc_search_core_type->clone = gncs_clone;
82 : 0 : }
83 : :
84 : : static void
85 : 0 : gnc_search_int64_init (GNCSearchInt64 *o)
86 : : {
87 : 0 : o->how = QOF_COMPARE_EQUAL;
88 : 0 : }
89 : :
90 : : static void
91 : 0 : gnc_search_int64_finalize (GObject *obj)
92 : : {
93 : 0 : GNCSearchInt64 *o = (GNCSearchInt64 *)obj;
94 : 0 : g_assert (GNC_IS_SEARCH_INT64 (o));
95 : :
96 : 0 : G_OBJECT_CLASS (gnc_search_int64_parent_class)->finalize(obj);
97 : 0 : }
98 : :
99 : : /**
100 : : * gnc_search_int64_new:
101 : : *
102 : : * Create a new GNCSearchInt64 object.
103 : : *
104 : : * Return value: A new #GNCSearchInt64 object.
105 : : **/
106 : : GNCSearchInt64 *
107 : 0 : gnc_search_int64_new (void)
108 : : {
109 : 0 : GNCSearchInt64 *o = g_object_new(GNC_TYPE_SEARCH_INT64, NULL);
110 : 0 : return o;
111 : : }
112 : :
113 : : void
114 : 0 : gnc_search_int64_set_value (GNCSearchInt64 *fi, gint64 value)
115 : : {
116 : 0 : g_return_if_fail (fi);
117 : 0 : g_return_if_fail (GNC_IS_SEARCH_INT64 (fi));
118 : :
119 : 0 : fi->value = value;
120 : : }
121 : :
122 : : void
123 : 0 : gnc_search_int64_set_how (GNCSearchInt64 *fi, QofQueryCompare how)
124 : : {
125 : 0 : g_return_if_fail (fi);
126 : 0 : g_return_if_fail (GNC_IS_SEARCH_INT64 (fi));
127 : 0 : fi->how = how;
128 : : }
129 : :
130 : : static void
131 : 0 : pass_parent (GNCSearchCoreType *fe, gpointer parent)
132 : : {
133 : 0 : GNCSearchInt64 *fi = (GNCSearchInt64 *)fe;
134 : :
135 : 0 : g_return_if_fail (fi);
136 : 0 : g_return_if_fail (GNC_IS_SEARCH_INT64 (fi));
137 : :
138 : 0 : fi->parent = GTK_WINDOW(parent);
139 : : }
140 : :
141 : : static gboolean
142 : 0 : gncs_validate (GNCSearchCoreType *fe)
143 : : {
144 : 0 : GNCSearchInt64 *fi = (GNCSearchInt64 *)fe;
145 : 0 : gboolean valid = TRUE;
146 : 0 : GError *error = NULL;
147 : :
148 : 0 : g_return_val_if_fail (fi, FALSE);
149 : 0 : g_return_val_if_fail (GNC_IS_SEARCH_INT64 (fi), FALSE);
150 : :
151 : 0 : if (!gnc_amount_edit_evaluate (GNC_AMOUNT_EDIT(fi->gae), &error))
152 : : {
153 : 0 : gnc_error_dialog (GTK_WINDOW(fi->parent), "%s", error->message);
154 : 0 : valid = FALSE;
155 : 0 : g_error_free (error);
156 : : }
157 : :
158 : 0 : return valid;
159 : : }
160 : :
161 : : static void
162 : 0 : entry_changed (GNCAmountEdit *entry, GNCSearchInt64 *fe)
163 : : {
164 : 0 : gnc_numeric value = gnc_amount_edit_get_amount (entry);
165 : 0 : g_assert (value.denom == 1);
166 : 0 : fe->value = value.num;
167 : 0 : }
168 : :
169 : : static GtkWidget *
170 : 0 : make_menu (GNCSearchCoreType *fe)
171 : : {
172 : 0 : GNCSearchInt64 *fi = (GNCSearchInt64 *)fe;
173 : : GtkComboBox *combo;
174 : :
175 : 0 : combo = GTK_COMBO_BOX(gnc_combo_box_new_search());
176 : 0 : gnc_combo_box_search_add(combo, _("is less than"), QOF_COMPARE_LT);
177 : 0 : gnc_combo_box_search_add(combo, _("is less than or equal to"), QOF_COMPARE_LTE);
178 : 0 : gnc_combo_box_search_add(combo, _("equals"), QOF_COMPARE_EQUAL);
179 : 0 : gnc_combo_box_search_add(combo, _("does not equal"), QOF_COMPARE_NEQ);
180 : 0 : gnc_combo_box_search_add(combo, _("is greater than"), QOF_COMPARE_GT);
181 : 0 : gnc_combo_box_search_add(combo, _("is greater than or equal to"), QOF_COMPARE_GTE);
182 : 0 : gnc_combo_box_search_changed(combo, &fi->how);
183 : 0 : gnc_combo_box_search_set_active(combo, fi->how ? fi->how : QOF_COMPARE_LT);
184 : :
185 : 0 : return GTK_WIDGET(combo);
186 : : }
187 : :
188 : : static void
189 : 0 : grab_focus (GNCSearchCoreType *fe)
190 : : {
191 : 0 : GNCSearchInt64 *fi = (GNCSearchInt64 *)fe;
192 : :
193 : 0 : g_return_if_fail (fi);
194 : 0 : g_return_if_fail (GNC_IS_SEARCH_INT64 (fi));
195 : :
196 : 0 : if (fi->entry)
197 : 0 : gtk_widget_grab_focus (fi->entry);
198 : : }
199 : :
200 : : static void
201 : 0 : editable_enters (GNCSearchCoreType *fe)
202 : : {
203 : 0 : GNCSearchInt64 *fi = (GNCSearchInt64 *)fe;
204 : :
205 : 0 : g_return_if_fail (fi);
206 : 0 : g_return_if_fail (GNC_IS_SEARCH_INT64 (fi));
207 : :
208 : 0 : if (fi->entry)
209 : 0 : gtk_entry_set_activates_default(GTK_ENTRY (fi->entry), TRUE);
210 : : }
211 : :
212 : : static GtkWidget *
213 : 0 : gncs_get_widget (GNCSearchCoreType *fe)
214 : : {
215 : : GtkWidget *entry, *menu, *box;
216 : 0 : GNCSearchInt64 *fi = (GNCSearchInt64 *)fe;
217 : :
218 : 0 : g_return_val_if_fail (fi, NULL);
219 : 0 : g_return_val_if_fail (GNC_IS_SEARCH_INT64 (fi), NULL);
220 : :
221 : 0 : box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 3);
222 : 0 : gtk_box_set_homogeneous (GTK_BOX (box), FALSE);
223 : :
224 : : /* Build and connect the option menu */
225 : 0 : menu = make_menu (fe);
226 : 0 : gtk_box_pack_start (GTK_BOX (box), menu, FALSE, FALSE, 3);
227 : :
228 : : /* Build and connect the entry window */
229 : 0 : entry = gnc_amount_edit_new ();
230 : 0 : gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (entry),
231 : : gnc_integral_print_info ());
232 : 0 : if (fi->value)
233 : : {
234 : 0 : gnc_numeric value = gnc_numeric_create (fi->value, 1);
235 : 0 : gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT (entry), value);
236 : : }
237 : 0 : g_signal_connect (G_OBJECT (entry), "amount_changed", G_CALLBACK (entry_changed), fe);
238 : 0 : gtk_box_pack_start (GTK_BOX (box), entry, FALSE, FALSE, 3);
239 : 0 : fi->entry = gnc_amount_edit_gtk_entry (GNC_AMOUNT_EDIT (entry));
240 : 0 : fi->gae = GNC_AMOUNT_EDIT (entry);
241 : :
242 : : /* And return the box */
243 : 0 : return box;
244 : : }
245 : :
246 : 0 : static QofQueryPredData* gncs_get_predicate (GNCSearchCoreType *fe)
247 : : {
248 : 0 : GNCSearchInt64 *fi = (GNCSearchInt64 *)fe;
249 : :
250 : 0 : g_return_val_if_fail (fi, NULL);
251 : 0 : g_return_val_if_fail (GNC_IS_SEARCH_INT64 (fi), NULL);
252 : :
253 : : /* force the computation of the entry, because we may not get the signal */
254 : 0 : entry_changed (fi->gae, fi);
255 : :
256 : 0 : return qof_query_int64_predicate (fi->how, fi->value);
257 : : }
258 : :
259 : 0 : static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe)
260 : : {
261 : 0 : GNCSearchInt64 *se, *fse = (GNCSearchInt64 *)fe;
262 : :
263 : 0 : g_return_val_if_fail (fse, NULL);
264 : 0 : g_return_val_if_fail (GNC_IS_SEARCH_INT64 (fse), NULL);
265 : :
266 : 0 : se = gnc_search_int64_new ();
267 : 0 : gnc_search_int64_set_value (se, fse->value);
268 : 0 : gnc_search_int64_set_how (se, fse->how);
269 : :
270 : 0 : return (GNCSearchCoreType *)se;
271 : : }
|