GnuCash c935c2f+
Loading...
Searching...
No Matches
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
42static GNCSearchCoreType *gncs_clone(GNCSearchCoreType *fe);
43static void pass_parent (GNCSearchCoreType *fe, gpointer parent);
44static gboolean gncs_validate (GNCSearchCoreType *fe);
45static GtkWidget *gncs_get_widget(GNCSearchCoreType *fe);
46static QofQueryPredData* gncs_get_predicate (GNCSearchCoreType *fe);
47
48static 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
62G_DEFINE_TYPE(GNCSearchOwner, gnc_search_owner, GNC_TYPE_SEARCH_CORE_TYPE)
63
64enum
65{
66 LAST_SIGNAL
67};
68
69#if LAST_SIGNAL > 0
70static guint signals[LAST_SIGNAL] = { 0 };
71#endif
72
73static void
74gnc_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
91static void
92gnc_search_owner_init (GNCSearchOwner *o)
93{
94}
95
96static void
97gnc_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
111GNCSearchOwner *
112gnc_search_owner_new (void)
113{
114 GNCSearchOwner *o = g_object_new(gnc_search_owner_get_type (), NULL);
115 return o;
116}
117
118static gboolean
119gncs_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
138static int
139owner_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
147static void
148set_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
166static void
167type_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
186static GtkWidget *
187make_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
209static GtkWidget *
210make_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
224static void
225pass_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
235static GtkWidget *
236gncs_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
269static 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
284static 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}
utility functions for the GnuCash UI
Business Interface: Object OWNERs.
const GncGUID * gncOwnerGetGUID(const GncOwner *owner)
Get the GncGUID of the immediate owner.
Definition gncOwner.c:518
GncOwnerType gncOwnerGetType(const GncOwner *owner)
Returns the GncOwnerType of this owner.
Definition gncOwner.c:200
QofGuidMatch
@ QOF_GUID_MATCH_ANY
These expect a single object and expect the QofAccessFunc returns GncGUID*.
The type used to store guids in C.
Definition guid.h:75