GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
business-options-gnome.c
1 /*
2  * business-options.c -- Initialize Business Options
3  *
4  * Written By: Derek Atkins <[email protected]>
5  * Copyright (C) 2002,2006 Derek Atkins
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, contact:
19  *
20  * Free Software Foundation Voice: +1-617-542-5942
21  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
22  * Boston, MA 02110-1301, USA [email protected]
23  */
24 
25 #include "config.h"
26 
27 #include <gtk/gtk.h>
28 #include <glib/gi18n.h>
29 #include "swig-runtime.h"
30 #include <libguile.h>
31 
32 #include "gnc-ui-util.h"
33 #include "dialog-utils.h"
34 #include "qof.h"
35 #include "option-util.h"
36 #include "gnc-general-search.h"
37 
38 #include "dialog-options.h"
39 #include "business-options-gnome.h"
40 #include "business-gnome-utils.h"
41 #include "dialog-invoice.h"
42 
43 #define FUNC_NAME G_STRFUNC
44 
45 static GtkWidget *
46 create_owner_widget (GNCOption *option, GncOwnerType type, GtkWidget *hbox)
47 {
48  GtkWidget *widget;
49  GncOwner owner;
50 
51  switch (type)
52  {
53  case GNC_OWNER_CUSTOMER:
54  gncOwnerInitCustomer (&owner, NULL);
55  break;
56  case GNC_OWNER_VENDOR:
57  gncOwnerInitVendor (&owner, NULL);
58  break;
59  case GNC_OWNER_EMPLOYEE:
60  gncOwnerInitEmployee (&owner, NULL);
61  break;
62  case GNC_OWNER_JOB:
63  gncOwnerInitJob (&owner, NULL);
64  break;
65  default:
66  return NULL;
67  }
68 
69  widget = gnc_owner_select_create (NULL, hbox,
70  gnc_get_current_book (), &owner);
71  gnc_option_set_widget (option, widget);
72 
73  g_signal_connect (G_OBJECT (widget), "changed",
74  G_CALLBACK (gnc_option_changed_option_cb), option);
75 
76  return widget;
77 }
78 
79 static GtkWidget *
80 make_name_label (char *name)
81 {
82  GtkWidget *label;
83  gchar *colon_name;
84 
85  colon_name = g_strconcat (name, ":", (char *)NULL);
86  label = gtk_label_new (colon_name);
87  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
88  g_free (colon_name);
89 
90  return label;
91 }
92 
93 /********************************************************************/
94 /* "Owner" Option functions */
95 
96 
97 static GncOwnerType
98 get_owner_type_from_option (GNCOption *option)
99 {
100  SCM odata = gnc_option_get_option_data (option);
101 
102  /* The option data is enum-typed. It's just the enum value. */
103  return (GncOwnerType) scm_to_int(odata);
104 }
105 
106 
107 /* Function to set the UI widget based upon the option */
108 static GtkWidget *
109 owner_set_widget (GNCOption *option, GtkBox *page_box,
110  char *name, char *documentation,
111  /* Return values */
112  GtkWidget **enclosing, gboolean *packed)
113 {
114  GtkWidget *value;
115  GtkWidget *label;
116 
117  *enclosing = gtk_hbox_new (FALSE, 5);
118  label = make_name_label (name);
119  gtk_box_pack_start (GTK_BOX (*enclosing), label, FALSE, FALSE, 0);
120 
121  value = create_owner_widget (option, get_owner_type_from_option (option),
122  *enclosing);
123 
124  gnc_option_set_ui_value (option, FALSE);
125 
126  gtk_widget_show_all (*enclosing);
127  return value;
128 }
129 
130 /* Function to set the UI Value for a particular option */
131 static gboolean
132 owner_set_value (GNCOption *option, gboolean use_default,
133  GtkWidget *widget, SCM value)
134 {
135  GncOwner owner_def;
136  GncOwner *owner;
137 
138  if (!SWIG_IsPointer (value))
139  scm_misc_error("business_options:owner_set_value",
140  "SCM is not a wrapped pointer.", value);
141 
142  owner = SWIG_MustGetPtr(value, SWIG_TypeQuery("_p__gncOwner"), 1, 0);
143 
144  /* XXX: should we verify that the owner type is correct? */
145  if (!owner)
146  {
147  owner_def.type = get_owner_type_from_option (option);
148  owner_def.owner.undefined = NULL;
149  owner = &owner_def;
150  }
151 
152  widget = gnc_option_get_gtk_widget (option);
153  gnc_owner_set_owner (widget, owner);
154  return FALSE;
155 }
156 
157 /* Function to get the UI Value for a particular option */
158 static SCM
159 owner_get_value (GNCOption *option, GtkWidget *widget)
160 {
161  static GncOwner owner; /* XXX: might cause trouble? */
162  GncOwnerType type;
163 
164  type = get_owner_type_from_option (option);
165  owner.type = type;
166  gnc_owner_get_owner (widget, &owner);
167 
168  return SWIG_NewPointerObj(&owner, SWIG_TypeQuery("_p__gncOwner"), 0);
169 }
170 
171 
172 /********************************************************************/
173 /* "Customer" Option functions */
174 
175 
176 /* Function to set the UI widget based upon the option */
177 static GtkWidget *
178 customer_set_widget (GNCOption *option, GtkBox *page_box,
179  char *name, char *documentation,
180  /* Return values */
181  GtkWidget **enclosing, gboolean *packed)
182 {
183  GtkWidget *value;
184  GtkWidget *label;
185 
186  *enclosing = gtk_hbox_new (FALSE, 5);
187  label = make_name_label (name);
188  gtk_box_pack_start (GTK_BOX (*enclosing), label, FALSE, FALSE, 0);
189 
190  value = create_owner_widget (option, GNC_OWNER_CUSTOMER, *enclosing);
191 
192  gnc_option_set_ui_value (option, FALSE);
193 
194  gtk_widget_show_all (*enclosing);
195  return value;
196 }
197 
198 /* Function to set the UI Value for a particular option */
199 static gboolean
200 customer_set_value (GNCOption *option, gboolean use_default,
201  GtkWidget *widget, SCM value)
202 {
203  GncOwner owner;
204  GncCustomer *customer;
205 
206  if (!SWIG_IsPointer (value))
207  scm_misc_error("business_options:customer_set_value",
208  "SCM is not a wrapped pointer.", value);
209 
210  customer = SWIG_MustGetPtr(value, SWIG_TypeQuery("_p__gncCustomer"), 1, 0);
211  gncOwnerInitCustomer (&owner, customer);
212 
213  widget = gnc_option_get_gtk_widget (option);
214  gnc_owner_set_owner (widget, &owner);
215  return FALSE;
216 }
217 
218 /* Function to get the UI Value for a particular option */
219 static SCM
220 customer_get_value (GNCOption *option, GtkWidget *widget)
221 {
222  GncOwner owner;
223 
224  gnc_owner_get_owner (widget, &owner);
225  return SWIG_NewPointerObj(owner.owner.undefined,
226  SWIG_TypeQuery("_p__gncCustomer"), 0);
227 }
228 
229 
230 /********************************************************************/
231 /* "Vendor" Option functions */
232 
233 
234 /* Function to set the UI widget based upon the option */
235 static GtkWidget *
236 vendor_set_widget (GNCOption *option, GtkBox *page_box,
237  char *name, char *documentation,
238  /* Return values */
239  GtkWidget **enclosing, gboolean *packed)
240 {
241  GtkWidget *value;
242  GtkWidget *label;
243 
244  *enclosing = gtk_hbox_new (FALSE, 5);
245  label = make_name_label (name);
246  gtk_box_pack_start (GTK_BOX (*enclosing), label, FALSE, FALSE, 0);
247 
248  value = create_owner_widget (option, GNC_OWNER_VENDOR, *enclosing);
249 
250  gnc_option_set_ui_value (option, FALSE);
251 
252  gtk_widget_show_all (*enclosing);
253  return value;
254 }
255 
256 /* Function to set the UI Value for a particular option */
257 static gboolean
258 vendor_set_value (GNCOption *option, gboolean use_default,
259  GtkWidget *widget, SCM value)
260 {
261  GncOwner owner;
262  GncVendor *vendor;
263 
264  if (!SWIG_IsPointer (value))
265  scm_misc_error("business_options:vendor_set_value",
266  "SCM is not a wrapped pointer.", value);
267 
268  vendor = SWIG_MustGetPtr(value, SWIG_TypeQuery("_p__gncVendor"), 1, 0);
269  gncOwnerInitVendor (&owner, vendor);
270 
271  widget = gnc_option_get_gtk_widget (option);
272  gnc_owner_set_owner (widget, &owner);
273  return FALSE;
274 }
275 
276 /* Function to get the UI Value for a particular option */
277 static SCM
278 vendor_get_value (GNCOption *option, GtkWidget *widget)
279 {
280  GncOwner owner;
281 
282  gnc_owner_get_owner (widget, &owner);
283  return SWIG_NewPointerObj(owner.owner.undefined,
284  SWIG_TypeQuery("_p__gncVendor"), 0);
285 }
286 
287 /********************************************************************/
288 /* "Employee" Option functions */
289 
290 
291 /* Function to set the UI widget based upon the option */
292 static GtkWidget *
293 employee_set_widget (GNCOption *option, GtkBox *page_box,
294  char *name, char *documentation,
295  /* Return values */
296  GtkWidget **enclosing, gboolean *packed)
297 {
298  GtkWidget *value;
299  GtkWidget *label;
300 
301  *enclosing = gtk_hbox_new (FALSE, 5);
302  label = make_name_label (name);
303  gtk_box_pack_start (GTK_BOX (*enclosing), label, FALSE, FALSE, 0);
304 
305  value = create_owner_widget (option, GNC_OWNER_EMPLOYEE, *enclosing);
306 
307  gnc_option_set_ui_value (option, FALSE);
308 
309  gtk_widget_show_all (*enclosing);
310  return value;
311 }
312 
313 /* Function to set the UI Value for a particular option */
314 static gboolean
315 employee_set_value (GNCOption *option, gboolean use_default,
316  GtkWidget *widget, SCM value)
317 {
318  GncOwner owner;
319  GncEmployee *employee;
320 
321  if (!SWIG_IsPointer (value))
322  scm_misc_error("business_options:employee_set_value",
323  "SCM is not a wrapped pointer.", value);
324 
325  employee = SWIG_MustGetPtr(value, SWIG_TypeQuery("_p__gncEmployee"), 1, 0);
326  gncOwnerInitEmployee (&owner, employee);
327 
328  widget = gnc_option_get_gtk_widget (option);
329  gnc_owner_set_owner (widget, &owner);
330  return FALSE;
331 }
332 
333 /* Function to get the UI Value for a particular option */
334 static SCM
335 employee_get_value (GNCOption *option, GtkWidget *widget)
336 {
337  GncOwner owner;
338 
339  gnc_owner_get_owner (widget, &owner);
340 
341  return SWIG_NewPointerObj(owner.owner.undefined,
342  SWIG_TypeQuery("_p__gncEmployee"), 0);
343 }
344 
345 /********************************************************************/
346 /* "Invoice" Option functions */
347 
348 
349 static GtkWidget *
350 create_invoice_widget (GNCOption *option, GtkWidget *hbox)
351 {
352  GtkWidget *widget;
353 
354  /* No owner or starting invoice here, but that's okay. */
355  widget = gnc_invoice_select_create (hbox, gnc_get_current_book(),
356  NULL, NULL, NULL);
357 
358  gnc_option_set_widget (option, widget);
359  g_signal_connect (G_OBJECT (widget), "changed",
360  G_CALLBACK (gnc_option_changed_option_cb), option);
361 
362  return widget;
363 }
364 
365 /* Function to set the UI widget based upon the option */
366 static GtkWidget *
367 invoice_set_widget (GNCOption *option, GtkBox *page_box,
368  char *name, char *documentation,
369  /* Return values */
370  GtkWidget **enclosing, gboolean *packed)
371 {
372  GtkWidget *value;
373  GtkWidget *label;
374 
375  *enclosing = gtk_hbox_new (FALSE, 5);
376  label = make_name_label (name);
377  gtk_box_pack_start (GTK_BOX (*enclosing), label, FALSE, FALSE, 0);
378 
379  value = create_invoice_widget (option, *enclosing);
380 
381  gnc_option_set_ui_value (option, FALSE);
382 
383  gtk_widget_show_all (*enclosing);
384  return value;
385 }
386 
387 /* Function to set the UI Value for a particular option */
388 static gboolean
389 invoice_set_value (GNCOption *option, gboolean use_default,
390  GtkWidget *widget, SCM value)
391 {
392  GncInvoice *invoice;
393 
394  if (!SWIG_IsPointer (value))
395  scm_misc_error("business_options:invoice_set_value",
396  "SCM is not a wrapped pointer.", value);
397 
398  invoice = SWIG_MustGetPtr(value, SWIG_TypeQuery("_p__gncInvoice"), 1, 0);
399 
400  widget = gnc_option_get_gtk_widget (option);
401  gnc_general_search_set_selected (GNC_GENERAL_SEARCH (widget), invoice);
402  return FALSE;
403 }
404 
405 /* Function to get the UI Value for a particular option */
406 static SCM
407 invoice_get_value (GNCOption *option, GtkWidget *widget)
408 {
409  GncInvoice *invoice;
410 
411  invoice = gnc_general_search_get_selected (GNC_GENERAL_SEARCH (widget));
412  return SWIG_NewPointerObj(invoice, SWIG_TypeQuery("_p__gncInvoice"), 0);
413 }
414 
415 
416 /********************************************************************/
417 /* "Tax Table" Option functions */
418 
419 
420 static GtkWidget *
421 create_taxtable_widget (GNCOption *option, GtkWidget *hbox)
422 {
423  GtkWidget *widget;
424  GtkBuilder *builder;
425 
426  builder = gtk_builder_new();
427  gnc_builder_add_from_file (builder, "business-options-gnome.glade", "taxtable_store");
428  gnc_builder_add_from_file (builder, "business-options-gnome.glade", "taxtable_menu");
429 
430  widget = GTK_WIDGET (gtk_builder_get_object (builder, "taxtable_menu"));
431  gnc_taxtables_combo (GTK_COMBO_BOX(widget), gnc_get_current_book (), TRUE, NULL);
432  gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
433 
434  gnc_option_set_widget (option, widget);
435 
436  g_signal_connect (widget, "changed",
437  G_CALLBACK (gnc_option_changed_option_cb), option);
438 
439  g_object_unref(G_OBJECT(builder));
440  return widget;
441 }
442 
443 /* Function to set the UI widget based upon the option */
444 static GtkWidget *
445 taxtable_set_widget (GNCOption *option, GtkBox *page_box,
446  char *name, char *documentation,
447  /* Return values */
448  GtkWidget **enclosing, gboolean *packed)
449 {
450  GtkWidget *value;
451  GtkWidget *label;
452 
453  *enclosing = gtk_hbox_new (FALSE, 5);
454  label = make_name_label (name);
455  gtk_box_pack_start (GTK_BOX (*enclosing), label, FALSE, FALSE, 0);
456 
457  value = create_taxtable_widget (option, *enclosing);
458 
459  gnc_option_set_ui_value (option, FALSE);
460 
461  gtk_widget_show_all (*enclosing);
462  return value;
463 }
464 
465 /* Function to set the UI Value for a particular option */
466 static gboolean
467 taxtable_set_value (GNCOption *option, gboolean use_default,
468  GtkWidget *widget, SCM value)
469 {
470  GncTaxTable *taxtable;
471 
472  if (!SWIG_IsPointer (value))
473  scm_misc_error("business_options:taxtable_set_value",
474  "SCM is not a wrapped pointer.", value);
475 
476  taxtable = SWIG_MustGetPtr(value, SWIG_TypeQuery("_p__gncTaxTable"), 1, 0);
477 
478  widget = gnc_option_get_gtk_widget (option);
479  gnc_simple_combo_set_value (GTK_COMBO_BOX(widget), taxtable);
480  return FALSE;
481 }
482 
483 /* Function to get the UI Value for a particular option */
484 static SCM
485 taxtable_get_value (GNCOption *option, GtkWidget *widget)
486 {
487  GncTaxTable *taxtable;
488 
489  taxtable = gnc_simple_combo_get_value (GTK_COMBO_BOX(widget));
490  return SWIG_NewPointerObj(taxtable, SWIG_TypeQuery("_p__gncTaxTable"), 0);
491 }
492 
493 
494 
495 
496 void
497 gnc_business_options_gnome_initialize (void)
498 {
499  int i;
500  static GNCOptionDef_t options[] =
501  {
502  { "owner", owner_set_widget, owner_set_value, owner_get_value },
503  {
504  "customer", customer_set_widget, customer_set_value,
505  customer_get_value
506  },
507  { "vendor", vendor_set_widget, vendor_set_value, vendor_get_value },
508  { "employee", employee_set_widget, employee_set_value, employee_get_value },
509  { "invoice", invoice_set_widget, invoice_set_value, invoice_get_value },
510  { "taxtable", taxtable_set_widget, taxtable_set_value, taxtable_get_value },
511  { NULL }
512  };
513 
514  SWIG_GetModule(NULL); /* Work-around for SWIG bug. */
515  for (i = 0; options[i].option_name; i++)
516  gnc_options_ui_register_option (&(options[i]));
517 }
utility functions for the GnuCash UI