search-date.c

00001 /*
00002  *  Copyright (C) 2002 Derek Atkins
00003  *
00004  *  Authors: Derek Atkins <warlord@MIT.EDU>
00005  *
00006  * Copyright (c) 2006 David Hampton <hampton@employees.org>
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of version 2 of the GNU General Public
00010  * License as published by the Free Software Foundation.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public
00018  * License along with this program; if not, write to the
00019  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <gtk/gtk.h>
00028 #include <glib/gi18n.h>
00029 
00030 #include "gnc-date.h"
00031 #include "gnc-date-edit.h"
00032 #include "QueryCore.h"
00033 
00034 #include "search-date.h"
00035 #include "search-core-utils.h"
00036 
00037 #define d(x)
00038 
00039 static void editable_enters (GNCSearchCoreType *fe);
00040 static void grab_focus (GNCSearchCoreType *fe);
00041 static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe);
00042 static gboolean gncs_validate (GNCSearchCoreType *fe);
00043 static GtkWidget *gncs_get_widget(GNCSearchCoreType *fe);
00044 static QueryPredData_t gncs_get_predicate (GNCSearchCoreType *fe);
00045 
00046 static void gnc_search_date_class_init  (GNCSearchDateClass *class);
00047 static void gnc_search_date_init        (GNCSearchDate *gspaper);
00048 static void gnc_search_date_finalize    (GObject *obj);
00049 
00050 typedef struct _GNCSearchDatePrivate GNCSearchDatePrivate;
00051 
00052 struct _GNCSearchDatePrivate {
00053   GtkWidget *entry;
00054 };
00055 
00056 #define _PRIVATE(o) \
00057    (G_TYPE_INSTANCE_GET_PRIVATE ((o), GNC_TYPE_SEARCH_DATE, GNCSearchDatePrivate))
00058 
00059 static GNCSearchCoreTypeClass *parent_class;
00060 
00061 GType
00062 gnc_search_date_get_type (void)
00063 {
00064   static GType type = 0;
00065         
00066   if (!type) {
00067     GTypeInfo type_info = {
00068       sizeof(GNCSearchDateClass),       /* class_size */
00069       NULL,                             /* base_init */
00070       NULL,                             /* base_finalize */
00071       (GClassInitFunc)gnc_search_date_class_init,
00072       NULL,                             /* class_finalize */
00073       NULL,                             /* class_data */
00074       sizeof(GNCSearchDate),            /* */
00075       0,                                /* n_preallocs */
00076       (GInstanceInitFunc)gnc_search_date_init,
00077     };
00078                 
00079     type = g_type_register_static (GNC_TYPE_SEARCH_CORE_TYPE,
00080                                    "GNCSearchDate",
00081                                    &type_info, 0);
00082   }
00083         
00084   return type;
00085 }
00086 
00087 static void
00088 gnc_search_date_class_init (GNCSearchDateClass *class)
00089 {
00090   GObjectClass *object_class;
00091   GNCSearchCoreTypeClass *gnc_search_core_type = (GNCSearchCoreTypeClass *)class;
00092 
00093   object_class = G_OBJECT_CLASS (class);
00094   parent_class = g_type_class_peek_parent (class);
00095 
00096   object_class->finalize = gnc_search_date_finalize;
00097 
00098   /* override methods */
00099   gnc_search_core_type->editable_enters = editable_enters;
00100   gnc_search_core_type->grab_focus = grab_focus;
00101   gnc_search_core_type->validate = gncs_validate;
00102   gnc_search_core_type->get_widget = gncs_get_widget;
00103   gnc_search_core_type->get_predicate = gncs_get_predicate;
00104   gnc_search_core_type->clone = gncs_clone;
00105 
00106   g_type_class_add_private(class, sizeof(GNCSearchDatePrivate));
00107 }
00108 
00109 static void
00110 gnc_search_date_init (GNCSearchDate *o)
00111 {
00112   o->ts.tv_sec = time(NULL);
00113   o->how = COMPARE_LT;
00114 }
00115 
00116 static void
00117 gnc_search_date_finalize (GObject *obj)
00118 {
00119   GNCSearchDate *o;
00120   GNCSearchDatePrivate *priv;
00121 
00122   g_assert (IS_GNCSEARCH_DATE (obj));
00123 
00124   o = GNCSEARCH_DATE(obj);
00125   priv = _PRIVATE(o);
00126   if (priv->entry)
00127     gtk_widget_destroy (priv->entry);
00128 
00129   G_OBJECT_CLASS (parent_class)->finalize(obj);
00130 }
00131 
00139 GNCSearchDate *
00140 gnc_search_date_new (void)
00141 {
00142   GNCSearchDate *o = g_object_new(GNC_TYPE_SEARCH_DATE, NULL);
00143   return o;
00144 }
00145 
00146 void
00147 gnc_search_date_set_date (GNCSearchDate *fi, Timespec ts)
00148 {
00149   g_return_if_fail (fi);
00150   g_return_if_fail (IS_GNCSEARCH_DATE (fi));
00151         
00152   fi->ts = ts;
00153 }
00154 
00155 void
00156 gnc_search_date_set_how (GNCSearchDate *fi, query_compare_t how)
00157 {
00158   g_return_if_fail (fi);
00159   g_return_if_fail (IS_GNCSEARCH_DATE (fi));
00160   fi->how = how;
00161 }
00162 
00163 static gboolean
00164 gncs_validate (GNCSearchCoreType *fe)
00165 {
00166   GNCSearchDate *fi = (GNCSearchDate *)fe;
00167   gboolean valid = TRUE;
00168 
00169   g_return_val_if_fail (fi, FALSE);
00170   g_return_val_if_fail (IS_GNCSEARCH_DATE (fi), FALSE);
00171         
00172   /* XXX */
00173 
00174   return valid;
00175 }
00176 
00177 static void
00178 date_changed (GNCDateEdit *date_edit, GNCSearchDate *fe)
00179 {
00180   fe->ts = gnc_date_edit_get_date_ts (date_edit);
00181 }
00182 
00183 static GtkWidget *
00184 make_menu (GNCSearchCoreType *fe)
00185 {
00186   GNCSearchDate *fi = (GNCSearchDate *)fe;
00187   GtkComboBox *combo;
00188 
00189   combo = GTK_COMBO_BOX(gnc_combo_box_new_search());
00190 
00191   gnc_combo_box_search_add(combo, _("is before"), COMPARE_LT);
00192   gnc_combo_box_search_add(combo, _("is before or on"), COMPARE_LTE);
00193   gnc_combo_box_search_add(combo, _("is on"), COMPARE_EQUAL);
00194   gnc_combo_box_search_add(combo, _("is not on"), COMPARE_NEQ);
00195   gnc_combo_box_search_add(combo, _("is after"), COMPARE_GT);
00196   gnc_combo_box_search_add(combo, _("is on or after"), COMPARE_GTE);
00197   gnc_combo_box_search_changed(combo, &fi->how);
00198   gnc_combo_box_search_set_active(combo, fi->how ? fi->how : COMPARE_LT);
00199 
00200   return GTK_WIDGET(combo);
00201 }
00202 
00203 static void
00204 grab_focus (GNCSearchCoreType *fe)
00205 {
00206   GNCSearchDate *fi = (GNCSearchDate *)fe;
00207   GNCSearchDatePrivate *priv;
00208 
00209   g_return_if_fail (fi);
00210   g_return_if_fail (IS_GNCSEARCH_DATE (fi));
00211 
00212   priv = _PRIVATE(fi);
00213   if (priv->entry)
00214     gtk_widget_grab_focus (GNC_DATE_EDIT(priv->entry)->date_entry);
00215 }
00216 
00217 static void
00218 editable_enters (GNCSearchCoreType *fe)
00219 {
00220   GNCSearchDate *fi = (GNCSearchDate *)fe;
00221   GNCSearchDatePrivate *priv;
00222 
00223   g_return_if_fail (fi);
00224   g_return_if_fail (IS_GNCSEARCH_DATE (fi));
00225 
00226   priv = _PRIVATE(fi);
00227   if (priv->entry)
00228     gnc_date_activates_default (GNC_DATE_EDIT (priv->entry), TRUE);
00229 }
00230 
00231 static GtkWidget *
00232 gncs_get_widget (GNCSearchCoreType *fe)
00233 {
00234   GtkWidget *entry, *menu, *box;
00235   GNCSearchDate *fi = (GNCSearchDate *)fe;
00236   GNCSearchDatePrivate *priv;
00237         
00238   g_return_val_if_fail (fi, NULL);
00239   g_return_val_if_fail (IS_GNCSEARCH_DATE (fi), NULL);
00240 
00241   priv = _PRIVATE(fi);
00242   box = gtk_hbox_new (FALSE, 3);
00243 
00244   /* Build and connect the option menu */
00245   menu = make_menu (fe);
00246   gtk_box_pack_start (GTK_BOX (box), menu, FALSE, FALSE, 3);
00247 
00248   /* Build and connect the date entry window */
00249   entry = gnc_date_edit_new_ts (fi->ts, FALSE, FALSE);
00250   g_signal_connect (G_OBJECT (entry), "date_changed", G_CALLBACK (date_changed), fe);
00251   gtk_box_pack_start (GTK_BOX (box), entry, FALSE, FALSE, 3);
00252   g_object_ref (entry);
00253   priv->entry = entry;
00254 
00255   /* And return the box */
00256   return box;
00257 }
00258 
00259 static QueryPredData_t gncs_get_predicate (GNCSearchCoreType *fe)
00260 {
00261   GNCSearchDate *fi = (GNCSearchDate *)fe;
00262   GNCSearchDatePrivate *priv;
00263 
00264   g_return_val_if_fail (fi, NULL);
00265   g_return_val_if_fail (IS_GNCSEARCH_DATE (fi), NULL);
00266 
00267   /* Make sure we actually use the currently-entered date */
00268   priv = _PRIVATE(fi);
00269   if (priv->entry)
00270     fi->ts = gnc_date_edit_get_date_ts (GNC_DATE_EDIT (priv->entry));
00271 
00272   return gncQueryDatePredicate (fi->how, DATE_MATCH_NORMAL, fi->ts);
00273 }
00274 
00275 static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe)
00276 {
00277   GNCSearchDate *se, *fse = (GNCSearchDate *)fe;
00278 
00279   g_return_val_if_fail (fse, NULL);
00280   g_return_val_if_fail (IS_GNCSEARCH_DATE (fse), NULL);
00281 
00282   se = gnc_search_date_new ();
00283   gnc_search_date_set_date (se, fse->ts);
00284   gnc_search_date_set_how (se, fse->how);
00285 
00286   return (GNCSearchCoreType *)se;
00287 }

Generated on Tue Oct 14 05:05:02 2008 for GnuCash by  doxygen 1.5.2