GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
search-date.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-date.h"
32 #include "gnc-date-edit.h"
33 #include "qof.h"
34 
35 #include "search-date.h"
36 #include "search-core-utils.h"
37 
38 #define d(x)
39 
40 static void editable_enters (GNCSearchCoreType *fe);
41 static void grab_focus (GNCSearchCoreType *fe);
42 static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe);
43 static gboolean gncs_validate (GNCSearchCoreType *fe);
44 static GtkWidget *gncs_get_widget(GNCSearchCoreType *fe);
45 static QofQueryPredData* gncs_get_predicate (GNCSearchCoreType *fe);
46 
47 static void gnc_search_date_class_init (GNCSearchDateClass *klass);
48 static void gnc_search_date_init (GNCSearchDate *gspaper);
49 static void gnc_search_date_finalize (GObject *obj);
50 
52 
54 {
55  GtkWidget *entry;
56 };
57 
58 #define _PRIVATE(o) \
59  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GNC_TYPE_SEARCH_DATE, GNCSearchDatePrivate))
60 
61 static GNCSearchCoreTypeClass *parent_class;
62 
63 GType
64 gnc_search_date_get_type (void)
65 {
66  static GType type = 0;
67 
68  if (!type)
69  {
70  GTypeInfo type_info =
71  {
72  sizeof(GNCSearchDateClass), /* class_size */
73  NULL, /* base_init */
74  NULL, /* base_finalize */
75  (GClassInitFunc)gnc_search_date_class_init,
76  NULL, /* class_finalize */
77  NULL, /* class_data */
78  sizeof(GNCSearchDate), /* */
79  0, /* n_preallocs */
80  (GInstanceInitFunc)gnc_search_date_init,
81  };
82 
83  type = g_type_register_static (GNC_TYPE_SEARCH_CORE_TYPE,
84  "GNCSearchDate",
85  &type_info, 0);
86  }
87 
88  return type;
89 }
90 
91 static void
92 gnc_search_date_class_init (GNCSearchDateClass *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_date_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(GNCSearchDatePrivate));
111 }
112 
113 static void
114 gnc_search_date_init (GNCSearchDate *o)
115 {
116  o->ts.tv_sec = gnc_time (NULL);
117  o->how = QOF_COMPARE_LT;
118 }
119 
120 static void
121 gnc_search_date_finalize (GObject *obj)
122 {
123  GNCSearchDate *o;
124  GNCSearchDatePrivate *priv;
125 
126  g_assert (IS_GNCSEARCH_DATE (obj));
127 
128  o = GNCSEARCH_DATE(obj);
129  priv = _PRIVATE(o);
130  if (priv->entry)
131  gtk_widget_destroy (priv->entry);
132 
133  G_OBJECT_CLASS (parent_class)->finalize(obj);
134 }
135 
144 gnc_search_date_new (void)
145 {
146  GNCSearchDate *o = g_object_new(GNC_TYPE_SEARCH_DATE, NULL);
147  return o;
148 }
149 
150 void
151 gnc_search_date_set_date (GNCSearchDate *fi, Timespec ts)
152 {
153  g_return_if_fail (fi);
154  g_return_if_fail (IS_GNCSEARCH_DATE (fi));
155 
156  fi->ts = ts;
157 }
158 
159 void
160 gnc_search_date_set_how (GNCSearchDate *fi, QofQueryCompare how)
161 {
162  g_return_if_fail (fi);
163  g_return_if_fail (IS_GNCSEARCH_DATE (fi));
164  fi->how = how;
165 }
166 
167 static gboolean
168 gncs_validate (GNCSearchCoreType *fe)
169 {
170  GNCSearchDate *fi = (GNCSearchDate *)fe;
171  gboolean valid = TRUE;
172 
173  g_return_val_if_fail (fi, FALSE);
174  g_return_val_if_fail (IS_GNCSEARCH_DATE (fi), FALSE);
175 
176  /* XXX */
177 
178  return valid;
179 }
180 
181 static void
182 date_changed (GNCDateEdit *date_edit, GNCSearchDate *fe)
183 {
184  fe->ts = gnc_date_edit_get_date_ts (date_edit);
185 }
186 
187 static GtkWidget *
188 make_menu (GNCSearchCoreType *fe)
189 {
190  GNCSearchDate *fi = (GNCSearchDate *)fe;
191  GtkComboBox *combo;
192 
193  combo = GTK_COMBO_BOX(gnc_combo_box_new_search());
194 
195  gnc_combo_box_search_add(combo, _("is before"), QOF_COMPARE_LT);
196  gnc_combo_box_search_add(combo, _("is before or on"), QOF_COMPARE_LTE);
197  gnc_combo_box_search_add(combo, _("is on"), QOF_COMPARE_EQUAL);
198  gnc_combo_box_search_add(combo, _("is not on"), QOF_COMPARE_NEQ);
199  gnc_combo_box_search_add(combo, _("is after"), QOF_COMPARE_GT);
200  gnc_combo_box_search_add(combo, _("is on or after"), QOF_COMPARE_GTE);
201  gnc_combo_box_search_changed(combo, &fi->how);
202  gnc_combo_box_search_set_active(combo, fi->how ? fi->how : QOF_COMPARE_LT);
203 
204  return GTK_WIDGET(combo);
205 }
206 
207 static void
208 grab_focus (GNCSearchCoreType *fe)
209 {
210  GNCSearchDate *fi = (GNCSearchDate *)fe;
211  GNCSearchDatePrivate *priv;
212 
213  g_return_if_fail (fi);
214  g_return_if_fail (IS_GNCSEARCH_DATE (fi));
215 
216  priv = _PRIVATE(fi);
217  if (priv->entry)
218  gtk_widget_grab_focus (GNC_DATE_EDIT(priv->entry)->date_entry);
219 }
220 
221 static void
222 editable_enters (GNCSearchCoreType *fe)
223 {
224  GNCSearchDate *fi = (GNCSearchDate *)fe;
225  GNCSearchDatePrivate *priv;
226 
227  g_return_if_fail (fi);
228  g_return_if_fail (IS_GNCSEARCH_DATE (fi));
229 
230  priv = _PRIVATE(fi);
231  if (priv->entry)
232  gnc_date_activates_default (GNC_DATE_EDIT (priv->entry), TRUE);
233 }
234 
235 static GtkWidget *
236 gncs_get_widget (GNCSearchCoreType *fe)
237 {
238  GtkWidget *entry, *menu, *box;
239  GNCSearchDate *fi = (GNCSearchDate *)fe;
240  GNCSearchDatePrivate *priv;
241 
242  g_return_val_if_fail (fi, NULL);
243  g_return_val_if_fail (IS_GNCSEARCH_DATE (fi), NULL);
244 
245  priv = _PRIVATE(fi);
246  box = gtk_hbox_new (FALSE, 3);
247 
248  /* Build and connect the option menu */
249  menu = make_menu (fe);
250  gtk_box_pack_start (GTK_BOX (box), menu, FALSE, FALSE, 3);
251 
252  /* Build and connect the date entry window */
253  entry = gnc_date_edit_new_ts (fi->ts, FALSE, FALSE);
254  g_signal_connect (G_OBJECT (entry), "date_changed", G_CALLBACK (date_changed), fe);
255  gtk_box_pack_start (GTK_BOX (box), entry, FALSE, FALSE, 3);
256  g_object_ref (entry);
257  priv->entry = entry;
258 
259  /* And return the box */
260  return box;
261 }
262 
263 static QofQueryPredData* gncs_get_predicate (GNCSearchCoreType *fe)
264 {
265  GNCSearchDate *fi = (GNCSearchDate *)fe;
266  GNCSearchDatePrivate *priv;
267 
268  g_return_val_if_fail (fi, NULL);
269  g_return_val_if_fail (IS_GNCSEARCH_DATE (fi), NULL);
270 
271  /* Make sure we actually use the currently-entered date */
272  priv = _PRIVATE(fi);
273  if (priv->entry)
274  fi->ts = gnc_date_edit_get_date_ts (GNC_DATE_EDIT (priv->entry));
275 
276  return qof_query_date_predicate (fi->how, QOF_DATE_MATCH_NORMAL, fi->ts);
277 }
278 
279 static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe)
280 {
281  GNCSearchDate *se, *fse = (GNCSearchDate *)fe;
282 
283  g_return_val_if_fail (fse, NULL);
284  g_return_val_if_fail (IS_GNCSEARCH_DATE (fse), NULL);
285 
286  se = gnc_search_date_new ();
287  gnc_search_date_set_date (se, fse->ts);
288  gnc_search_date_set_how (se, fse->how);
289 
290  return (GNCSearchCoreType *)se;
291 }
Date and Time handling routines.
Use a 64-bit unsigned int timespec.
Definition: gnc-date.h:299
QofQueryCompare
Definition: qofquerycore.h:55
time64 gnc_time(time64 *tbuf)
get the current local time