GnuCash  5.6-150-g038405b370+
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
search-owner.c
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 "qof.h"
32 #include "gnc-ui-util.h"
33 #include "gnc-gui-query.h"
34 #include "gncOwner.h"
35 
36 #include "business-gnome-utils.h"
37 #include "search-owner.h"
38 #include "search-core-utils.h"
39 
40 #define d(x)
41 
42 static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe);
43 static void pass_parent (GNCSearchCoreType *fe, gpointer parent);
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_owner_finalize (GObject *obj);
49 
51 {
52  GNCSearchCoreType parent_instance;
53 
54  QofGuidMatch how;
55 
56  GncOwner owner;
57  GtkWindow * parent;
58  GtkWidget * owner_box;
59  GtkWidget * owner_choice;
60 } GNCSearchOwnerPrivate;
61 
62 G_DEFINE_TYPE(GNCSearchOwner, gnc_search_owner, GNC_TYPE_SEARCH_CORE_TYPE)
63 
64 enum
65 {
66  LAST_SIGNAL
67 };
68 
69 #if LAST_SIGNAL > 0
70 static guint signals[LAST_SIGNAL] = { 0 };
71 #endif
72 
73 static void
74 gnc_search_owner_class_init (GNCSearchOwnerClass *klass)
75 {
76  GObjectClass *object_class;
77  GNCSearchCoreTypeClass *gnc_search_core_type = (GNCSearchCoreTypeClass *)klass;
78 
79  object_class = G_OBJECT_CLASS (klass);
80 
81  object_class->finalize = gnc_search_owner_finalize;
82 
83  /* override methods */
84  gnc_search_core_type->validate = gncs_validate;
85  gnc_search_core_type->pass_parent = pass_parent;
86  gnc_search_core_type->get_widget = gncs_get_widget;
87  gnc_search_core_type->get_predicate = gncs_get_predicate;
88  gnc_search_core_type->clone = gncs_clone;
89 }
90 
91 static void
92 gnc_search_owner_init (GNCSearchOwner *o)
93 {
94 }
95 
96 static void
97 gnc_search_owner_finalize (GObject *obj)
98 {
99  g_assert (GNC_IS_SEARCH_OWNER (obj));
100 
101  G_OBJECT_CLASS (gnc_search_owner_parent_class)->finalize(obj);
102 }
103 
111 GNCSearchOwner *
112 gnc_search_owner_new (void)
113 {
114  GNCSearchOwner *o = g_object_new(gnc_search_owner_get_type (), NULL);
115  return o;
116 }
117 
118 static gboolean
119 gncs_validate (GNCSearchCoreType *fe)
120 {
121  GNCSearchOwner *fi = (GNCSearchOwner *)fe;
122  gboolean valid = TRUE;
123 
124  g_return_val_if_fail (fi, FALSE);
125  g_return_val_if_fail (GNC_IS_SEARCH_OWNER (fi), FALSE);
126 
127  if (fi->owner.owner.undefined == NULL)
128  {
129  valid = FALSE;
130  gnc_error_dialog (GTK_WINDOW(fi->parent), "%s", _("You have not selected an owner"));
131  }
132 
133  /* XXX */
134 
135  return valid;
136 }
137 
138 static int
139 owner_changed_cb (GtkWidget *widget, gpointer data)
140 {
141  GNCSearchOwner *fe = data;
142 
143  gnc_owner_get_owner (fe->owner_choice, &(fe->owner));
144  return FALSE;
145 }
146 
147 static void
148 set_owner_widget (GNCSearchOwner *fe)
149 {
150  /* Remove the old choice widget */
151  if (fe->owner_choice)
152  gtk_container_remove (GTK_CONTAINER (fe->owner_box), fe->owner_choice);
153 
154  /* Create a new choice widget */
155  fe->owner_choice =
156  gnc_owner_select_create (NULL, fe->owner_box,
157  gnc_get_current_book(), &(fe->owner));
158 
159  /* Setup the "changed" callback */
160  g_signal_connect (G_OBJECT (fe->owner_choice), "changed",
161  G_CALLBACK (owner_changed_cb), fe);
162 
163  gtk_widget_show_all (fe->owner_choice);
164 }
165 
166 static void
167 type_combo_changed (GtkWidget *widget, GNCSearchOwner *fe)
168 {
169  GncOwnerType type;
170 
171  g_return_if_fail(GTK_IS_COMBO_BOX(widget));
172 
173  type = gnc_combo_box_search_get_active(GTK_COMBO_BOX(widget));
174 
175  /* If the type changed or if we don't have a type create the owner_choice */
176  if (type != gncOwnerGetType (&(fe->owner)))
177  {
178  fe->owner.type = type;
179  fe->owner.owner.undefined = NULL;
180  set_owner_widget (fe);
181  }
182  else if (fe->owner_choice == NULL)
183  set_owner_widget (fe);
184 }
185 
186 static GtkWidget *
187 make_type_menu (GNCSearchCoreType *fe)
188 {
189  GNCSearchOwner *fi = (GNCSearchOwner *)fe;
190  GtkComboBox *combo;
191  GncOwnerType type;
192 
193  type = gncOwnerGetType (&(fi->owner));
194 
195  combo = GTK_COMBO_BOX(gnc_combo_box_new_search());
196  gnc_combo_box_search_add(combo, _("Customer"), GNC_OWNER_CUSTOMER);
197  gnc_combo_box_search_add(combo, _("Vendor"), GNC_OWNER_VENDOR);
198  gnc_combo_box_search_add(combo, _("Employee"), GNC_OWNER_EMPLOYEE);
199  gnc_combo_box_search_add(combo, _("Job"), GNC_OWNER_JOB);
200 
201  g_signal_connect (combo, "changed", G_CALLBACK (type_combo_changed), fe);
202  gnc_combo_box_search_set_active(combo, type);
203 
204  return GTK_WIDGET(combo);
205 
206 
207 }
208 
209 static GtkWidget *
210 make_how_menu (GNCSearchCoreType *fe)
211 {
212  GNCSearchOwner *fi = (GNCSearchOwner *)fe;
213  GtkComboBox *combo;
214 
215  combo = GTK_COMBO_BOX(gnc_combo_box_new_search());
216  gnc_combo_box_search_add(combo, _("is"), QOF_GUID_MATCH_ANY);
217  gnc_combo_box_search_add(combo, _("is not"), QOF_GUID_MATCH_NONE);
218  gnc_combo_box_search_changed(combo, &fi->how);
219  gnc_combo_box_search_set_active(combo, fi->how ? fi->how : QOF_GUID_MATCH_ANY);
220 
221  return GTK_WIDGET(combo);
222 }
223 
224 static void
225 pass_parent (GNCSearchCoreType *fe, gpointer parent)
226 {
227  GNCSearchOwner *fi = (GNCSearchOwner *)fe;
228 
229  g_return_if_fail (fi);
230  g_return_if_fail (GNC_IS_SEARCH_OWNER (fi));
231 
232  fi->parent = GTK_WINDOW(parent);
233 }
234 
235 static GtkWidget *
236 gncs_get_widget (GNCSearchCoreType *fe)
237 {
238  GtkWidget *how_menu, *type_menu, *box;
239  GNCSearchOwner *fi = (GNCSearchOwner *)fe;
240 
241  g_return_val_if_fail (fi, NULL);
242  g_return_val_if_fail (GNC_IS_SEARCH_OWNER (fi), NULL);
243 
244  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 3);
245  gtk_box_set_homogeneous (GTK_BOX (box), FALSE);
246 
247  /* Build and connect the "how" option menu. */
248  how_menu = make_how_menu (fe);
249  gtk_box_pack_start (GTK_BOX (box), how_menu, FALSE, FALSE, 3);
250 
251  /* Create the owner box */
252  fi->owner_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
253  gtk_box_set_homogeneous (GTK_BOX (fi->owner_box), FALSE);
254 
255  /* Build and connect the "type" option menu.
256  * Note that this will build the owner_choice and
257  * put it in the owner_box we just created.
258  */
259  type_menu = make_type_menu (fe);
260  gtk_box_pack_start (GTK_BOX (box), type_menu, FALSE, FALSE, 3);
261 
262  /* connect the owner box */
263  gtk_box_pack_start (GTK_BOX (box), fi->owner_box, FALSE, FALSE, 3);
264 
265  /* And return the box */
266  return box;
267 }
268 
269 static QofQueryPredData* gncs_get_predicate (GNCSearchCoreType *fe)
270 {
271  GNCSearchOwner *fi = (GNCSearchOwner *)fe;
272  const GncGUID *guid;
273  GList *l = NULL;
274 
275  g_return_val_if_fail (fi, NULL);
276  g_return_val_if_fail (GNC_IS_SEARCH_OWNER (fi), NULL);
277 
278  guid = gncOwnerGetGUID (&(fi->owner));
279  l = g_list_prepend (l, (gpointer)guid);
280 
281  return qof_query_guid_predicate (fi->how, l);
282 }
283 
284 static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe)
285 {
286  GNCSearchOwner *se, *fse = (GNCSearchOwner *)fe;
287 
288  g_return_val_if_fail (fse, NULL);
289  g_return_val_if_fail (GNC_IS_SEARCH_OWNER (fse), NULL);
290 
291  se = gnc_search_owner_new ();
292  se->how = fse->how;
293  gncOwnerCopy (&(fse->owner), &(se->owner));
294 
295  return (GNCSearchCoreType *)se;
296 }
const GncGUID * gncOwnerGetGUID(const GncOwner *owner)
Get the GncGUID of the immediate owner.
Definition: gncOwner.c:518
Business Interface: Object OWNERs.
utility functions for the GnuCash UI
These expect a single object and expect the QofAccessFunc returns GncGUID*.
Definition: qofquerycore.h:113
QofGuidMatch
Definition: qofquerycore.h:109
GncOwnerType gncOwnerGetType(const GncOwner *owner)
Returns the GncOwnerType of this owner.
Definition: gncOwner.c:200
The type used to store guids in C.
Definition: guid.h:75