GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dialog-tax-info.c
1 /********************************************************************\
2  * dialog-tax-info.c -- tax information dialog *
3  * Copyright (C) 2001 Gnumatic, Inc. *
4  * Author: Dave Peticolas <[email protected]> *
5  * *
6  * *
7  * updated by J. Alex Aycinena, July 2009 *
8  * *
9  * This program is free software; you can redistribute it and/or *
10  * modify it under the terms of the GNU General Public License as *
11  * published by the Free Software Foundation; either version 2 of *
12  * the License, or (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License*
20  * along with this program; if not, contact: *
21  * *
22  * Free Software Foundation Voice: +1-617-542-5942 *
23  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
24  * Boston, MA 02110-1301, USA [email protected] *
25 \********************************************************************/
26 
27 #include "config.h"
28 
29 #include <gtk/gtk.h>
30 #include <glib/gi18n.h>
31 #include <libguile.h>
32 #include "guile-mappings.h"
33 #include "guile-util.h"
34 #include "gnc-guile-utils.h"
35 
36 #include "Account.h"
37 #include "gnc-ui-util.h"
38 #include "dialog-utils.h"
39 #include "gnc-prefs.h"
40 #include "gnc-tree-view-account.h"
41 #include "gnc-component-manager.h"
42 #include "gnc-session.h"
43 #include "qof.h"
44 #include "gnc-ui.h"
45 
46 #define DIALOG_TAX_INFO_CM_CLASS "dialog-tax-info"
47 #define GNC_PREFS_GROUP "dialogs.tax-info"
48 #define GNC_PREF_PANED_POS "paned-position"
49 
50 enum
51 {
52  INCOME,
53  EXPENSE,
54  ASSET,
55  LIAB_EQ,
56  N_CATEGORIES
57 };
58 
59 static struct
60 {
61  SCM payer_name_source;
62  SCM form;
63  SCM description;
64  SCM help;
65  SCM line_data;
66  SCM last_year;
67  SCM copy;
68 
69  SCM codes;
70 
71  SCM tax_entity_type;
72  SCM tax_entity_desc;
73 
74  SCM tax_entity_types;
75 } getters;
76 
77 typedef struct
78 {
79  char *type_code;
80  char *type;
81  char *description;
82  char *combo_box_entry;
83 } TaxTypeInfo;
84 
85 typedef struct
86 {
87  char *code;
88  char *payer_name_source;
89  char *form;
90  char *description;
91  char *help;
92  gboolean copy;
93 } TXFInfo;
94 
95 typedef struct
96 {
97  GtkWidget * dialog;
98 
99  GtkWidget * entity_name_display;
100  GtkWidget * entity_name_entry;
101  GtkWidget * entity_type_display;
102  GtkWidget * entity_type_combo;
103  GtkWidget * tax_identity_edit_button;
104 
105  GtkWidget * acct_info;
106  GtkWidget * expense_radio;
107  GtkWidget * asset_radio;
108  GtkWidget * liab_eq_radio;
109  GtkWidget * account_treeview;
110  GtkWidget * select_button;
111  GtkWidget * num_acct_label;
112 
113  GtkWidget * txf_info;
114  GtkWidget * tax_related_button;
115  GtkWidget * txf_vbox;
116  GtkWidget * txf_category_view;
117  GtkWidget * txf_help_text;
118  GtkWidget * help_scroll;
119  GtkWidget * payer_vbox;
120  GtkWidget * pns_vbox;
121  GtkWidget * current_account_button;
122  GtkWidget * parent_account_button;
123  GtkWidget * copy_vbox;
124  GtkWidget * copy_spin_button;
125 
126  GList * entity_type_infos;
127  GList * income_txf_infos;
128  GList * expense_txf_infos;
129  GList * asset_txf_infos;
130  GList * liab_eq_txf_infos;
131 
132  const gchar * tax_name;
133  const gchar * tax_type;
134  const gchar * tax_type_combo_text;
135  const gchar * default_tax_type;
136 
137  QofBook *this_book;
138 
139  gboolean changed;
140  gboolean tax_type_changed;
141 
142  GNCAccountType account_type;
143 } TaxInfoDialog;
144 
145 static void
146 initialize_getters (void)
147 {
148  getters.payer_name_source = scm_c_eval_string ("gnc:txf-get-payer-name-source");
149  getters.form = scm_c_eval_string ("gnc:txf-get-form");
150  getters.description = scm_c_eval_string ("gnc:txf-get-description");
151  getters.help = scm_c_eval_string ("gnc:txf-get-help");
152  getters.line_data = scm_c_eval_string ("gnc:txf-get-line-data");
153  getters.last_year = scm_c_eval_string ("gnc:txf-get-last-year");
154  getters.copy = scm_c_eval_string ("gnc:txf-get-multiple");
155 
156  getters.codes = scm_c_eval_string ("gnc:txf-get-codes");
157 
158  getters.tax_entity_type = scm_c_eval_string ("gnc:txf-get-tax-entity-type");
159  getters.tax_entity_desc = scm_c_eval_string
160  ("gnc:txf-get-tax-entity-type-description");
161 
162  getters.tax_entity_types = scm_c_eval_string
163  ("gnc:txf-get-tax-entity-type-codes");
164 }
165 
166 static void
167 destroy_tax_type_info (gpointer data, gpointer user_data)
168 {
169  TaxTypeInfo *tax_type = data;
170 
171  g_free (tax_type->type_code);
172  tax_type->type_code = NULL;
173 
174  g_free (tax_type->type);
175  tax_type->type = NULL;
176 
177  g_free (tax_type->description);
178  tax_type->description = NULL;
179 
180  g_free (tax_type->combo_box_entry);
181  tax_type->combo_box_entry = NULL;
182 
183  g_free (tax_type);
184 }
185 
186 static void
187 destroy_tax_type_infos (GList *types)
188 {
189  g_list_foreach (types, destroy_tax_type_info, NULL);
190  g_list_free (types);
191 }
192 
193 static void
194 destroy_txf_info (gpointer data, gpointer user_data)
195 {
196  TXFInfo *txf_info = data;
197 
198  g_free (txf_info->code);
199  txf_info->code = NULL;
200 
201  g_free (txf_info->payer_name_source);
202  txf_info->payer_name_source = NULL;
203 
204  g_free (txf_info->form);
205  txf_info->form = NULL;
206 
207  g_free (txf_info->description);
208  txf_info->description = NULL;
209 
210  g_free (txf_info->help);
211  txf_info->help = NULL;
212 
213  g_free (txf_info);
214 }
215 
216 static void
217 destroy_txf_infos (GList *infos)
218 {
219  g_list_foreach (infos, destroy_txf_info, NULL);
220  g_list_free (infos);
221 }
222 
223 static void
224 gnc_tax_info_set_changed (TaxInfoDialog *ti_dialog, gboolean changed)
225 {
226  ti_dialog->changed = changed;
227 }
228 
229 static GList *
230 load_txf_info (gint acct_category, TaxInfoDialog *ti_dialog)
231 {
232  GList *infos = NULL;
233  SCM tax_entity_type;
234  SCM category;
235  SCM codes;
236 
237  if (ti_dialog->tax_type == NULL ||
238  (g_strcmp0 (ti_dialog->tax_type, "") == 0))
239  {
240  destroy_txf_infos (infos);
241  return NULL;
242  }
243  else
244  {
245  tax_entity_type = scm_from_utf8_string (ti_dialog->tax_type);
246  }
247 
248  switch (acct_category)
249  {
250  case INCOME:
251  category = scm_c_eval_string ("txf-income-categories");
252  break;
253  case EXPENSE:
254  category = scm_c_eval_string ("txf-expense-categories");
255  break;
256  case ASSET:
257  category = scm_c_eval_string ("txf-asset-categories");
258  break;
259  case LIAB_EQ:
260  category = scm_c_eval_string ("txf-liab-eq-categories");
261  break;
262  default:
263  destroy_txf_infos (infos);
264  return NULL;
265  }
266 
267  if (category == SCM_UNDEFINED)
268  {
269  destroy_txf_infos (infos);
270  return NULL;
271  }
272 
273  codes = scm_call_2 (getters.codes, category, tax_entity_type);
274  if (!scm_is_list (codes))
275  {
276  destroy_txf_infos (infos);
277  return NULL;
278  }
279 
280  while (!scm_is_null (codes))
281  {
282  TXFInfo *txf_info;
283  SCM code_scm;
284  const gchar *last_yr = _("Last Valid Year: ");
285  const gchar *form_line = _("Form Line Data: ");
286  const gchar *code_line_word = _("Code");
287  const gchar *code_line_colon = ": ";
288  const gchar *prefix = "N";
289  gchar *str = NULL;
290  gchar *num_code = NULL;
291  gchar *form_line_data = NULL;
292  gchar *help_text = NULL;
293  SCM scm;
294  gint year;
295  gboolean cpy;
296 
297  code_scm = SCM_CAR (codes);
298  codes = SCM_CDR (codes);
299 
300  scm = scm_call_3 (getters.payer_name_source, category, code_scm,
301  tax_entity_type);
302  if (scm_is_symbol(scm))
303  str = gnc_scm_symbol_to_locale_string (scm);
304  else
305  str = g_strdup ("");
306  if (g_strcmp0 (str, "not-impl") == 0)
307  {
308  g_free (str);
309  continue;
310  }
311 
312  txf_info = g_new0 (TXFInfo, 1);
313 
314  if (g_strcmp0 (str, "none") == 0)
315  txf_info->payer_name_source = NULL;
316  else
317  txf_info->payer_name_source = g_strdup (str);
318  g_free (str);
319 
320  if (scm_is_symbol(code_scm))
321  str = gnc_scm_symbol_to_locale_string (code_scm);
322  else
323  str = g_strdup ("");
324  txf_info->code = g_strdup (str);
325  if (g_str_has_prefix (str, prefix))
326  {
327  const gchar *num_code_tmp;
328  num_code_tmp = g_strdup (str);
329  num_code_tmp++; /* to lose the leading N */
330  num_code = g_strdup (num_code_tmp);
331  num_code_tmp--;
332  g_free ((gpointer *) num_code_tmp);
333  }
334  else
335  num_code = g_strdup (str);
336  g_free (str);
337 
338  scm = scm_call_3 (getters.form, category, code_scm, tax_entity_type);
339  if (scm_is_string(scm))
340  txf_info->form = gnc_scm_to_utf8_string(scm);
341  else
342  txf_info->form = g_strdup ("");
343 
344  scm = scm_call_3 (getters.description, category, code_scm, tax_entity_type);
345  if (scm_is_string(scm))
346  txf_info->description = gnc_scm_to_utf8_string(scm);
347  else
348  txf_info->description = g_strdup ("");
349 
350  scm = scm_call_2 (getters.help, category, code_scm);
351  if (scm_is_string(scm))
352  help_text = gnc_scm_to_utf8_string(scm);
353  else
354  help_text = g_strdup ("");
355 
356  scm = scm_call_3 (getters.last_year, category, code_scm, tax_entity_type);
357  year = scm_is_bool (scm) ? 0 : scm_to_int(scm);
358  scm = scm_call_3 (getters.line_data, category, code_scm, tax_entity_type);
359  if (scm_is_list (scm))
360  {
361  const gchar *now = _("now");
362  gchar *until;
363 
364  until = (gchar *) now;
365  form_line_data = g_strconcat ("\n", "\n", form_line, NULL);
366  while (!scm_is_null (scm))
367  {
368  SCM year_scm;
369  gint line_year;
370  gchar *line = NULL;
371  gchar *temp = NULL;
372  gchar *temp2 = NULL;
373 
374  year_scm = SCM_CAR (scm);
375  scm = SCM_CDR (scm);
376 
377  line_year = scm_is_bool (SCM_CAR (year_scm)) ? 0 :
378  scm_to_int (SCM_CAR (year_scm));
379  if (scm_is_string((SCM_CAR (SCM_CDR (year_scm)))))
380  line = gnc_scm_to_utf8_string((SCM_CAR (SCM_CDR
381  (year_scm))));
382  else
383  line = g_strdup ("");
384  temp2 = g_strdup_printf ("%d", line_year);
385  temp = g_strconcat (form_line_data, "\n", temp2, " - ",
386  until, " ", line, NULL);
387  if (until != now)
388  g_free (until);
389  until = g_strdup_printf ("%d", (line_year - 1));
390  g_free (form_line_data);
391  form_line_data = g_strdup (temp);
392  g_free (line);
393  g_free (temp);
394  g_free (temp2);
395  }
396  if (g_strcmp0 (until, now) != 0)
397  g_free (until);
398  }
399  if (year != 0)
400  {
401  gchar *temp = g_strdup_printf("%d", year);
402  if (form_line_data != NULL)
403  txf_info->help = g_strconcat (last_yr, temp, "\n", "\n",
404  help_text, "\n", "\n",
405  code_line_word,
406  code_line_colon, num_code,
407  form_line_data, NULL);
408  else
409  txf_info->help = g_strconcat (last_yr, temp, "\n", "\n",
410  help_text, "\n", "\n",
411  code_line_word,
412  code_line_colon, num_code, NULL);
413  g_free (temp);
414  }
415  else
416  {
417  if (form_line_data != NULL)
418  txf_info->help = g_strconcat (help_text, "\n", "\n",
419  code_line_word,
420  code_line_colon, num_code,
421  form_line_data, NULL);
422  else
423  txf_info->help = g_strconcat (help_text, "\n", "\n",
424  code_line_word,
425  code_line_colon, num_code, NULL);
426  }
427 
428  g_free (num_code);
429  g_free (help_text);
430  g_free (form_line_data);
431 
432  scm = scm_call_3 (getters.copy, category, code_scm, tax_entity_type);
433  cpy = scm_is_bool (scm) ? (scm_is_false (scm) ? FALSE : TRUE) : FALSE;
434  txf_info->copy = cpy;
435 
436  infos = g_list_prepend (infos, txf_info);
437  }
438  return g_list_reverse (infos);
439 }
440 
441 static GList *
442 tax_infos (TaxInfoDialog *ti_dialog)
443 {
444  return
445  (ti_dialog->account_type == ACCT_TYPE_INCOME)
446  ? ti_dialog->income_txf_infos :
447  ((ti_dialog->account_type == ACCT_TYPE_EXPENSE)
448  ? ti_dialog->expense_txf_infos :
449  (((ti_dialog->account_type == ACCT_TYPE_ASSET)
450  ? ti_dialog->asset_txf_infos :
451  ti_dialog->liab_eq_txf_infos)));
452 }
453 
454 static void
455 load_tax_entity_type_list (TaxInfoDialog *ti_dialog)
456 {
457  GList *types = NULL;
458  SCM tax_types;
459 
460  ti_dialog->tax_type_combo_text = NULL;
461  tax_types = scm_call_0 (getters.tax_entity_types);
462  if (!scm_is_list (tax_types))
463  {
464  destroy_tax_type_infos (types);
465  return;
466  }
467 
468  while (!scm_is_null (tax_types))
469  {
470  TaxTypeInfo *tax_type_info;
471  SCM type_scm;
472  gchar *str = NULL;
473  SCM scm;
474 
475  type_scm = SCM_CAR (tax_types);
476  tax_types = SCM_CDR (tax_types);
477 
478  ti_dialog->default_tax_type = NULL;
479 
480  tax_type_info = g_new0 (TaxTypeInfo, 1);
481 
482  if (scm_is_symbol(type_scm))
483  tax_type_info->type_code = gnc_scm_symbol_to_locale_string (type_scm);
484  else
485  tax_type_info->type_code = g_strdup ("");
486 
487  scm = scm_call_1 (getters.tax_entity_type, type_scm);
488  if (scm_is_string(scm))
489  tax_type_info->type = gnc_scm_to_utf8_string(scm);
490  else
491  tax_type_info->type = g_strdup ("");
492 
493  scm = scm_call_1 (getters.tax_entity_desc, type_scm);
494  if (scm_is_string(scm))
495  tax_type_info->description = gnc_scm_to_utf8_string(scm);
496  else
497  tax_type_info->description = g_strdup ("");
498 
499  tax_type_info->combo_box_entry = g_strconcat(tax_type_info->type,
500  " - ",
501  tax_type_info->description, NULL);
502  /* save combo text for current tax type code */
503  if (g_strcmp0 (ti_dialog->tax_type, tax_type_info->type_code) == 0)
504  ti_dialog->tax_type_combo_text = tax_type_info->combo_box_entry;
505  /* the last will be default */
506  ti_dialog->default_tax_type = tax_type_info->combo_box_entry;
507 
508  types = g_list_prepend (types, tax_type_info);
509  }
510  ti_dialog->entity_type_infos = g_list_reverse (types);
511 }
512 
513 static void
514 load_category_list (TaxInfoDialog *ti_dialog)
515 {
516  GtkTreeView *view;
517  GtkListStore *store;
518  GtkTreeIter iter;
519  GList *codes;
520 
521  view = GTK_TREE_VIEW(ti_dialog->txf_category_view);
522  store = GTK_LIST_STORE(gtk_tree_view_get_model(view));
523  g_object_ref(store);
524  gtk_tree_view_set_model(view, NULL);
525 
526  gtk_list_store_clear(store);
527 
528  codes = tax_infos (ti_dialog);
529  for ( ; codes; codes = codes->next)
530  {
531  TXFInfo *txf_info = codes->data;
532 
533  gtk_list_store_append(store, &iter);
534  gtk_list_store_set(store, &iter,
535  0, txf_info->form,
536  1, txf_info->description,
537  -1);
538  }
539 
540  gtk_tree_view_set_model(view, GTK_TREE_MODEL(store));
541  g_object_unref(store);
542 }
543 
544 static void
545 clear_gui (TaxInfoDialog *ti_dialog)
546 {
547  GtkTreeView *view;
548  GtkTreeSelection *selection;
549 
550  gtk_toggle_button_set_active
551  (GTK_TOGGLE_BUTTON (ti_dialog->tax_related_button), FALSE);
552 
553  view = GTK_TREE_VIEW(ti_dialog->txf_category_view);
554  selection = gtk_tree_view_get_selection(view);
555  gtk_tree_selection_unselect_all(selection);
556 
557  gtk_toggle_button_set_active
558  (GTK_TOGGLE_BUTTON (ti_dialog->current_account_button), TRUE);
559 
560  gtk_spin_button_set_value
561  (GTK_SPIN_BUTTON (ti_dialog->copy_spin_button), 1);
562 }
563 
564 static gboolean
565 gnc_tax_info_dialog_account_filter_func (Account *account,
566  gpointer data)
567 {
568  TaxInfoDialog *dialog = data;
569  gboolean included = FALSE;
570 
571  if ((dialog->account_type == ACCT_TYPE_INCOME) ||
572  (dialog->account_type == ACCT_TYPE_EXPENSE))
573  included = (xaccAccountGetType (account) == dialog->account_type);
574  else if (dialog->account_type == ACCT_TYPE_ASSET)
575  included = ((xaccAccountGetType (account) == ACCT_TYPE_BANK) ||
576  (xaccAccountGetType (account) == ACCT_TYPE_CASH) ||
577  (xaccAccountGetType (account) == ACCT_TYPE_ASSET) ||
578  (xaccAccountGetType (account) == ACCT_TYPE_STOCK) ||
579  (xaccAccountGetType (account) == ACCT_TYPE_MUTUAL) ||
581  else if (dialog->account_type == ACCT_TYPE_LIABILITY)
582  included = ((xaccAccountGetType (account) == ACCT_TYPE_CREDIT) ||
583  (xaccAccountGetType (account) == ACCT_TYPE_LIABILITY) ||
584  (xaccAccountGetType (account) == ACCT_TYPE_EQUITY) ||
585  (xaccAccountGetType (account) == ACCT_TYPE_PAYABLE));
586  else
587  included = FALSE;
588  return included;
589 }
590 
591 static TXFInfo *
592 txf_infos_find_code (GList *infos, const char *code)
593 {
594  for (; infos; infos = infos->next)
595  {
596  TXFInfo *info = infos->data;
597 
598  if (g_strcmp0 (code, info->code) == 0)
599  return info;
600  }
601 
602  return NULL;
603 }
604 
605 static void
606 account_to_gui (TaxInfoDialog *ti_dialog, Account *account)
607 {
608  GtkTreeView *view;
609  GtkTreeSelection *selection;
610  GtkTreePath *path;
611  gboolean tax_related;
612  const char *str;
613  TXFInfo *info;
614  GList *infos;
615  gint index = 0;
616 
617  if (!account)
618  {
619  clear_gui (ti_dialog);
620  return;
621  }
622 
623  tax_related = xaccAccountGetTaxRelated (account);
624  gtk_toggle_button_set_active
625  (GTK_TOGGLE_BUTTON (ti_dialog->tax_related_button), tax_related);
626 
627  infos = tax_infos (ti_dialog);
628 
629  str = xaccAccountGetTaxUSCode (account);
630  info = txf_infos_find_code (infos, str);
631  if (info)
632  index = g_list_index (infos, info);
633 
634  if (index < 0)
635  index = 0;
636 
637  view = GTK_TREE_VIEW(ti_dialog->txf_category_view);
638  selection = gtk_tree_view_get_selection(view);
639  path = gtk_tree_path_new_from_indices(index, -1);
640  gtk_tree_selection_select_path(selection, path);
641  gtk_tree_view_scroll_to_cell(view, path, NULL, FALSE, 0, 0);
642  gtk_tree_path_free(path);
643 
644  str = xaccAccountGetTaxUSPayerNameSource (account);
645  if (g_strcmp0 (str, "parent") == 0)
646  gtk_toggle_button_set_active
647  (GTK_TOGGLE_BUTTON (ti_dialog->parent_account_button), TRUE);
648  else
649  gtk_toggle_button_set_active
650  (GTK_TOGGLE_BUTTON (ti_dialog->current_account_button), TRUE);
651 
652  gtk_spin_button_set_value
653  (GTK_SPIN_BUTTON (ti_dialog->copy_spin_button),
654  (gdouble) xaccAccountGetTaxUSCopyNumber (account));
655 }
656 
657 static void
658 gui_to_accounts (TaxInfoDialog *ti_dialog)
659 {
660  GtkTreeView *view;
661  GtkTreeModel *model;
662  GtkTreeSelection *selection;
663  GtkTreePath *path;
664  GtkTreeIter iter;
665  gint *indices;
666  gboolean tax_related;
667  const char *code;
668  const char *pns;
669  GList *accounts;
670  TXFInfo *info;
671  GList *infos;
672  GList *node;
673  gint64 copy_number;
674 
675  tax_related = gtk_toggle_button_get_active
676  (GTK_TOGGLE_BUTTON (ti_dialog->tax_related_button));
677 
678  infos = tax_infos (ti_dialog);
679 
680  view = GTK_TREE_VIEW(ti_dialog->txf_category_view);
681  selection = gtk_tree_view_get_selection(view);
682  if (!gtk_tree_selection_get_selected(selection, &model, &iter))
683  return;
684  path = gtk_tree_model_get_path(model, &iter);
685  indices = gtk_tree_path_get_indices(path);
686  info = g_list_nth_data (infos, indices[0]);
687  gtk_tree_path_free(path);
688  g_return_if_fail (info != NULL);
689 
690  code = tax_related ? info->code : NULL;
691 
692  if (tax_related && info->payer_name_source)
693  {
694  gboolean current;
695 
696  current = gtk_toggle_button_get_active
697  (GTK_TOGGLE_BUTTON (ti_dialog->current_account_button));
698 
699  pns = current ? "current" : "parent";
700  }
701  else
702  pns = NULL;
703 
704  if (tax_related && info->copy)
705  {
706  copy_number = gtk_spin_button_get_value_as_int
707  (GTK_SPIN_BUTTON (ti_dialog->copy_spin_button));
708  }
709  else
710  copy_number = 0;/* setting to zero removes slot */
711 
713  (GNC_TREE_VIEW_ACCOUNT(ti_dialog->account_treeview));
714 
715  for (node = accounts; node; node = node->next)
716  {
717  Account *account = node->data;
718 
719  xaccAccountBeginEdit (account);
720 
721  xaccAccountSetTaxRelated (account, tax_related);
722  xaccAccountSetTaxUSPayerNameSource (account, pns);
723  xaccAccountSetTaxUSCopyNumber (account, copy_number);
724  /* USCode is last because it removes TaxUS KVP if not tax_related */
725  xaccAccountSetTaxUSCode (account, code);
726 
727  xaccAccountCommitEdit (account);
728  }
729  g_list_free (accounts);
730 }
731 
732 static void
733 identity_edit_destroy_cb (GtkDialog *dialog, gpointer data)
734 {
735  TaxInfoDialog *ti_dialog = data;
736 
737  ti_dialog->entity_name_entry = NULL;
738  ti_dialog->entity_type_combo = NULL;
739 
740  gtk_widget_destroy(GTK_WIDGET(dialog));
741 }
742 
743 static void
744 window_destroy_cb (GtkObject *object, gpointer data)
745 {
746  TaxInfoDialog *ti_dialog = data;
747 
748  gnc_unregister_gui_component_by_data (DIALOG_TAX_INFO_CM_CLASS, ti_dialog);
749 
750  destroy_tax_type_infos (ti_dialog->entity_type_infos);
751  ti_dialog->entity_type_infos = NULL;
752 
753  destroy_txf_infos (ti_dialog->income_txf_infos);
754  ti_dialog->income_txf_infos = NULL;
755 
756  destroy_txf_infos (ti_dialog->expense_txf_infos);
757  ti_dialog->expense_txf_infos = NULL;
758 
759  destroy_txf_infos (ti_dialog->asset_txf_infos);
760  ti_dialog->asset_txf_infos = NULL;
761 
762  destroy_txf_infos (ti_dialog->liab_eq_txf_infos);
763  ti_dialog->liab_eq_txf_infos = NULL;
764 
765  g_free (ti_dialog);
766 }
767 
768 static void
769 cursor_changed_cb (GtkWidget *widget, gpointer data)
770 {
771  TaxInfoDialog *ti_dialog = data;
772  GncTreeViewAccount *account_tree;
773  Account *account;
774  gint num_children;
775 
776  account_tree = GNC_TREE_VIEW_ACCOUNT (ti_dialog->account_treeview);
777  account = gnc_tree_view_account_get_cursor_account (account_tree);
778  if (!account)
779  {
780  gtk_widget_set_sensitive(ti_dialog->select_button, FALSE);
781  return;
782  }
783 
784  num_children = gnc_tree_view_account_count_children(account_tree, account);
785  gtk_widget_set_sensitive(ti_dialog->select_button, num_children > 0);
786 }
787 
788 static void
789 select_subaccounts_clicked (GtkWidget *widget, gpointer data)
790 {
791  TaxInfoDialog *ti_dialog = data;
792  GncTreeViewAccount *account_tree;
793  Account *account;
794 
795  account_tree = GNC_TREE_VIEW_ACCOUNT (ti_dialog->account_treeview);
796  account = gnc_tree_view_account_get_cursor_account (account_tree);
797  if (!account)
798  return;
799 
800  gnc_tree_view_account_select_subaccounts (account_tree, account);
801 
802  gtk_widget_grab_focus (ti_dialog->account_treeview);
803 }
804 
805 static void
806 gnc_tax_info_dialog_response (GtkDialog *dialog, gint response, gpointer data)
807 {
808  TaxInfoDialog *ti_dialog = data;
809 
810  if (response == GTK_RESPONSE_OK && ti_dialog->changed)
811  gui_to_accounts (ti_dialog);
812 
813  gnc_close_gui_component_by_data (DIALOG_TAX_INFO_CM_CLASS, ti_dialog);
814 }
815 
816 static void
817 tax_info_show_acct_type_accounts (TaxInfoDialog *ti_dialog)
818 {
819  GncTreeViewAccount *tree;
820  AccountViewInfo info;
821  GNCAccountType type;
822 
823  tree = GNC_TREE_VIEW_ACCOUNT (ti_dialog->account_treeview);
824 
826 
827  for (type = 0; type < NUM_ACCOUNT_TYPES; type++) /* from Account.h */
828  {
829  if (ti_dialog->account_type == ACCT_TYPE_EXPENSE)
830  info.include_type[type] = (type == ACCT_TYPE_EXPENSE);
831  else if (ti_dialog->account_type == ACCT_TYPE_INCOME)
832  info.include_type[type] = (type == ACCT_TYPE_INCOME);
833  else if (ti_dialog->account_type == ACCT_TYPE_ASSET)
834  info.include_type[type] = ((type == ACCT_TYPE_BANK) ||
835  (type == ACCT_TYPE_CASH) ||
836  (type == ACCT_TYPE_ASSET) ||
837  (type == ACCT_TYPE_STOCK) ||
838  (type == ACCT_TYPE_MUTUAL) ||
839  (type == ACCT_TYPE_RECEIVABLE));
840  else if (ti_dialog->account_type == ACCT_TYPE_LIABILITY)
841  info.include_type[type] = ((type == ACCT_TYPE_CREDIT) ||
842  (type == ACCT_TYPE_LIABILITY) ||
843  (type == ACCT_TYPE_EQUITY) ||
844  (type == ACCT_TYPE_PAYABLE));
845  else
846  info.include_type[type] = FALSE;
847  }
848 
850 
851  load_category_list (ti_dialog);
852  cursor_changed_cb(GTK_WIDGET(tree), ti_dialog);
853 }
854 
855 static int
856 gnc_tax_info_update_accounts (TaxInfoDialog *ti_dialog)
857 {
858  GncTreeViewAccount *tree;
859  GtkTreeSelection* selection;
860  GtkWidget *label;
861  int num_accounts;
862  char *string;
863 
864  tree = GNC_TREE_VIEW_ACCOUNT(ti_dialog->account_treeview);
865  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(tree));
866  num_accounts = gtk_tree_selection_count_selected_rows (selection);
867 
868  label = ti_dialog->num_acct_label;
869 
870  string = g_strdup_printf ("%d", num_accounts);
871  gtk_label_set_text (GTK_LABEL (label), string);
872  g_free (string);
873 
874  gtk_widget_set_sensitive (ti_dialog->txf_info, num_accounts > 0);
875 
876  return num_accounts;
877 }
878 
879 static void
880 gnc_tax_info_acct_type_cb (GtkWidget *w, gpointer data)
881 {
882  TaxInfoDialog *ti_dialog = data;
883  const gchar *button_name;
884 
885  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w)))
886  {
887  button_name = gtk_buildable_get_name(GTK_BUILDABLE(w));
888  if (g_strcmp0 (button_name, "income_radio") == 0)
889  ti_dialog->account_type = ACCT_TYPE_INCOME;
890  else if (g_strcmp0 (button_name, "expense_radio") == 0)
891  ti_dialog->account_type = ACCT_TYPE_EXPENSE;
892  else if (g_strcmp0 (button_name, "asset_radio") == 0)
893  ti_dialog->account_type = ACCT_TYPE_ASSET;
894  else if (g_strcmp0 (button_name, "liab_eq_radio") == 0)
895  ti_dialog->account_type = ACCT_TYPE_LIABILITY;
896  else
897  return;
898  tax_info_show_acct_type_accounts (ti_dialog);
900  (GNC_TREE_VIEW_ACCOUNT (ti_dialog->account_treeview));
901  gnc_tax_info_update_accounts (ti_dialog);
902  clear_gui (ti_dialog);
903  }
904  else
905  return;
906 }
907 
908 static void
909 gnc_tax_info_account_changed_cb (GtkTreeSelection *selection,
910  gpointer data)
911 {
912  TaxInfoDialog *ti_dialog = data;
913  GncTreeViewAccount *view;
914  GList *accounts;
915  int num_accounts;
916 
917  g_return_if_fail(GTK_IS_TREE_SELECTION(selection));
918 
919  num_accounts = gnc_tax_info_update_accounts (ti_dialog);
920  switch (num_accounts)
921  {
922  case 0:
923  clear_gui (ti_dialog);
924  gnc_tax_info_set_changed (ti_dialog, FALSE);
925  return;
926 
927  case 1:
928  /* Get the account. This view is set for multiple selection, so we
929  can only get a list of accounts. */
930  view = GNC_TREE_VIEW_ACCOUNT(ti_dialog->account_treeview);
932  account_to_gui (ti_dialog, accounts->data);
933  g_list_free(accounts);
934 
935  gnc_tax_info_set_changed (ti_dialog, FALSE);
936  break;
937 
938  default:
939  gnc_tax_info_set_changed (ti_dialog, TRUE);
940  return;
941  }
942 }
943 
944 static void
945 txf_code_select_row_cb (GtkTreeSelection *selection,
946  gpointer user_data)
947 {
948  TaxInfoDialog *ti_dialog = user_data;
949  GtkTreeModel *model;
950  GtkTreePath *path;
951  GtkTreeIter iter;
952  gint *indices;
953  TXFInfo *txf_info;
954  GtkAdjustment *adj;
955  GtkWidget *vbox;
956  GtkTextBuffer *tb;
957  const char *text;
958 
959  if (!gtk_tree_selection_get_selected(selection, &model, &iter))
960  return;
961  path = gtk_tree_model_get_path(model, &iter);
962  indices = gtk_tree_path_get_indices(path);
963  txf_info = g_list_nth_data (tax_infos (ti_dialog), indices[0]);
964  gtk_tree_path_free(path);
965 
966  tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(ti_dialog->txf_help_text));
967 
968  text = (txf_info && txf_info->help) ? txf_info->help : "";
969  gtk_text_buffer_set_text (tb, text, -1);
970 
971  adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (ti_dialog->help_scroll));
972  gtk_adjustment_set_value (adj, 0.0);
973 
974  vbox = ti_dialog->payer_vbox;
975 
976  if (txf_info && txf_info->payer_name_source)
977  {
978  gboolean current;
979 
980  gtk_widget_set_sensitive (vbox, TRUE);
981 
982  current = (strcmp ("current", txf_info->payer_name_source) == 0);
983 
984  if (current)
985  gtk_toggle_button_set_active
986  (GTK_TOGGLE_BUTTON (ti_dialog->current_account_button), TRUE);
987  else
988  gtk_toggle_button_set_active
989  (GTK_TOGGLE_BUTTON (ti_dialog->parent_account_button), TRUE);
990  }
991  else
992  {
993  gtk_widget_set_sensitive (vbox, FALSE);
994  gtk_toggle_button_set_active
995  (GTK_TOGGLE_BUTTON (ti_dialog->current_account_button), TRUE);
996  }
997 
998  vbox = ti_dialog->copy_vbox;
999 
1000  if (txf_info && txf_info->copy)
1001  {
1002  gtk_widget_set_sensitive (vbox, TRUE);
1003  }
1004  else
1005  {
1006  gtk_widget_set_sensitive (vbox, FALSE);
1007  }
1008 
1009  gnc_tax_info_set_changed (ti_dialog, TRUE);
1010 }
1011 
1012 static void
1013 set_focus_sensitivity (TaxInfoDialog *ti_dialog)
1014 {
1015  if ((ti_dialog->tax_type == NULL) ||
1016  (g_strcmp0 (ti_dialog->tax_type, "Other") == 0) ||
1017  (g_strcmp0 (ti_dialog->tax_type, "") == 0))
1018  {
1019  gtk_widget_grab_focus (ti_dialog->tax_identity_edit_button);
1020  gtk_widget_set_sensitive (ti_dialog->acct_info, FALSE);
1021  gtk_widget_set_sensitive (ti_dialog->txf_info, FALSE);
1022  gtk_widget_hide (ti_dialog->txf_help_text); /* textview doesn't go insensitive!? */
1023  }
1024  else if (ti_dialog->tax_type_changed)
1025  {
1026  gtk_widget_set_sensitive (ti_dialog->acct_info, TRUE);
1027  gtk_widget_set_sensitive (ti_dialog->txf_info, TRUE);
1028  gtk_widget_grab_focus (ti_dialog->account_treeview);
1029  }
1030  else
1031  {
1032  gtk_widget_set_sensitive (ti_dialog->acct_info, TRUE);
1033  gtk_widget_grab_focus (ti_dialog->account_treeview);
1034  }
1035  if (ti_dialog->asset_txf_infos == NULL)
1036  gtk_widget_hide (ti_dialog->asset_radio);
1037  else
1038  gtk_widget_show (ti_dialog->asset_radio);
1039  if (ti_dialog->liab_eq_txf_infos == NULL)
1040  gtk_widget_hide (ti_dialog->liab_eq_radio);
1041  else
1042  gtk_widget_show (ti_dialog->liab_eq_radio);
1043 }
1044 
1045 static void
1046 identity_edit_response_cb (GtkDialog *dialog, gint response, gpointer data)
1047 {
1048  TaxInfoDialog *ti_dialog = data;
1049  const gchar *entry_name = NULL;
1050  const gchar *entry_type = NULL;
1051  gint active_item = 0;
1052  TaxTypeInfo *selected_type = NULL;
1053 
1054  if (response == GTK_RESPONSE_APPLY)
1055  {
1056  entry_name = gtk_entry_get_text (GTK_ENTRY (ti_dialog->entity_name_entry));
1057  active_item = gtk_combo_box_get_active
1058  (GTK_COMBO_BOX (ti_dialog->entity_type_combo));
1059  if (active_item != -1) /* -1 if there's no active item */
1060  {
1061  selected_type = g_list_nth_data (ti_dialog->entity_type_infos,
1062  (guint) active_item);
1063  if (selected_type)
1064  {
1065  entry_type = selected_type->type_code;
1066  if (!(g_strcmp0 (ti_dialog->tax_type, entry_type) == 0))
1067  {
1068  ti_dialog->tax_type_changed = TRUE;
1069  gnc_set_current_book_tax_type (entry_type);
1070  ti_dialog->tax_type = entry_type;
1071  if (entry_type != NULL)
1072  {
1073  gtk_label_set_text (GTK_LABEL (ti_dialog->entity_type_display),
1074  selected_type->combo_box_entry);
1075  }
1076  else
1077  {
1078  gtk_label_set_text (GTK_LABEL (ti_dialog->entity_type_display),
1079  ti_dialog->default_tax_type);
1080  }
1081  if (ti_dialog->income_txf_infos != NULL)
1082  destroy_txf_infos (ti_dialog->income_txf_infos);
1083  ti_dialog->income_txf_infos = load_txf_info (INCOME, ti_dialog);
1084  if (ti_dialog->expense_txf_infos != NULL)
1085  destroy_txf_infos (ti_dialog->expense_txf_infos);
1086  ti_dialog->expense_txf_infos = load_txf_info (EXPENSE, ti_dialog);
1087  if (ti_dialog->asset_txf_infos != NULL)
1088  destroy_txf_infos (ti_dialog->asset_txf_infos);
1089  ti_dialog->asset_txf_infos = load_txf_info (ASSET, ti_dialog);
1090  if (ti_dialog->liab_eq_txf_infos != NULL)
1091  destroy_txf_infos (ti_dialog->liab_eq_txf_infos);
1092  ti_dialog->liab_eq_txf_infos = load_txf_info (LIAB_EQ, ti_dialog);
1093  gtk_toggle_button_set_active
1094  (GTK_TOGGLE_BUTTON(ti_dialog->expense_radio), TRUE);
1095  tax_info_show_acct_type_accounts (ti_dialog);
1097  (GNC_TREE_VIEW_ACCOUNT (ti_dialog->account_treeview));
1098  gnc_tax_info_update_accounts (ti_dialog);
1099  clear_gui (ti_dialog);
1100  }
1101  }
1102  }
1103  if (!(g_strcmp0 (ti_dialog->tax_name, entry_name) == 0))
1104  {
1105  gnc_set_current_book_tax_name (entry_name);
1106  ti_dialog->tax_name = g_strdup (entry_name);
1107  gtk_label_set_text (GTK_LABEL (ti_dialog->entity_name_display),
1108  entry_name);
1109  }
1110  set_focus_sensitivity (ti_dialog);
1111  ti_dialog->tax_type_changed = FALSE;
1112  }
1113  identity_edit_destroy_cb (GTK_DIALOG (dialog), ti_dialog);
1114 }
1115 
1116 static void
1117 identity_edit_clicked_cb (GtkButton *button,
1118  gpointer user_data)
1119 {
1120  TaxInfoDialog *ti_dialog = user_data;
1121  GtkWidget *dialog;
1122  GtkWidget *content_area;
1123  GtkWidget *name_entry;
1124  GtkWidget *label;
1125  GtkWidget *alignment;
1126  GtkWidget *table;
1127  GtkListStore *store;
1128  GList *types = NULL;
1129  GtkTreeIter iter;
1130  gint current_item = -1;
1131  gint item = 0;
1132  GtkCellRenderer *renderer;
1133  GtkWidget *type_combo;
1134 
1135  dialog = gtk_dialog_new_with_buttons (_("Income Tax Identity"),
1136  (GtkWindow *)ti_dialog->dialog,
1137  GTK_DIALOG_MODAL |
1138  GTK_DIALOG_DESTROY_WITH_PARENT,
1139  GTK_STOCK_CANCEL,
1140  GTK_RESPONSE_CANCEL,
1141  GTK_STOCK_APPLY,
1142  GTK_RESPONSE_APPLY,
1143  NULL);
1144 
1145  content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
1146  name_entry = gtk_entry_new();
1147  ti_dialog->entity_name_entry = name_entry;
1148  if (!(g_strcmp0 (ti_dialog->tax_name, NULL) == 0))
1149  gtk_entry_set_text (GTK_ENTRY (name_entry), ti_dialog->tax_name);
1150  label = gtk_label_new (_("Name"));
1151  gtk_misc_set_alignment (GTK_MISC (label), 1.00, 0.50);
1152  alignment = gtk_alignment_new(1.00, 0.50, 1.00, 0.00);
1153  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
1154  gtk_container_add (GTK_CONTAINER (alignment), label);
1155  table = gtk_table_new (3, 2, FALSE);
1156  gtk_table_attach_defaults (GTK_TABLE (table), alignment, 0, 1, 0, 1);
1157  alignment = gtk_alignment_new(0.00, 0.50, 1.00, 0.00);
1158  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
1159  gtk_container_add (GTK_CONTAINER (alignment), name_entry);
1160  gtk_table_attach_defaults (GTK_TABLE (table), alignment, 1, 2, 0, 1);
1161  store = gtk_list_store_new (1, G_TYPE_STRING);
1162  gtk_list_store_clear(store);
1163  types = ti_dialog->entity_type_infos;
1164  for ( ; types; types = types->next)
1165  {
1166  TaxTypeInfo *tax_type_info = types->data;
1167 
1168  gtk_list_store_append(store, &iter);
1169  gtk_list_store_set(store, &iter, 0, tax_type_info->combo_box_entry, -1);
1170  if (g_strcmp0 (ti_dialog->tax_type, tax_type_info->type_code) == 0)
1171  current_item = item;
1172  item++;
1173  }
1174  type_combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL(store));
1175  g_object_unref(G_OBJECT (store));
1176  renderer = gtk_cell_renderer_text_new();
1177  gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(type_combo), renderer, TRUE);
1178  gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(type_combo), renderer,
1179  "text", 0, NULL);
1180  ti_dialog->entity_type_combo = type_combo;
1181  if (ti_dialog->tax_type)
1182  {
1183  gtk_combo_box_set_active (GTK_COMBO_BOX (type_combo), current_item);
1184  }
1185  else /* set to no active item */
1186  {
1187  gtk_combo_box_set_active (GTK_COMBO_BOX (type_combo), -1);
1188  }
1189  label = gtk_label_new (_("Type"));
1190  gtk_misc_set_alignment (GTK_MISC (label), 1.00, 0.50);
1191  alignment = gtk_alignment_new(1.00, 0.50, 1.00, 0.00);
1192  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
1193  gtk_container_add (GTK_CONTAINER (alignment), label);
1194  gtk_table_attach_defaults (GTK_TABLE (table), alignment, 0, 1, 1, 2);
1195  alignment = gtk_alignment_new(0.00, 0.50, 1.00, 0.00);
1196  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
1197  gtk_container_add (GTK_CONTAINER (alignment), type_combo);
1198  gtk_table_attach_defaults (GTK_TABLE (table), alignment, 1, 2, 1, 2);
1199  label = gtk_label_new (_("CAUTION: If you set TXF categories, and later change 'Type', you will need to manually reset those categories one at a time"));
1200  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1201  gtk_misc_set_alignment (GTK_MISC (label), 0.50, 0.50);
1202  alignment = gtk_alignment_new(0.50, 0.50, 1.00, 0.00);
1203  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 6, 6, 4, 4);
1204  gtk_container_add (GTK_CONTAINER (alignment), label);
1205  gtk_table_attach_defaults (GTK_TABLE (table), alignment, 0, 2, 2, 3);
1206  gtk_container_add (GTK_CONTAINER (content_area), table);
1207  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_APPLY);
1208  g_signal_connect (G_OBJECT (dialog), "response",
1209  G_CALLBACK (identity_edit_response_cb), ti_dialog);
1210  g_signal_connect (G_OBJECT (dialog), "destroy",
1211  G_CALLBACK (identity_edit_destroy_cb), ti_dialog);
1212  gtk_widget_show_all (dialog);
1213 }
1214 
1215 static void
1216 tax_related_toggled_cb (GtkToggleButton *togglebutton,
1217  gpointer user_data)
1218 {
1219  TaxInfoDialog *ti_dialog = user_data;
1220  GtkWidget *vbox;
1221  GtkWidget *hbox;
1222  gboolean on;
1223 
1224  on = gtk_toggle_button_get_active (togglebutton);
1225 
1226  vbox = ti_dialog->txf_vbox;
1227  hbox = ti_dialog->pns_vbox;
1228  gtk_widget_set_sensitive (vbox, on);
1229 
1230  gtk_widget_set_sensitive (hbox, on);
1231 
1232  if (on == FALSE)
1233  gtk_widget_hide (ti_dialog->txf_help_text); /* textview doesn't go insensitive!? */
1234  else
1235  gtk_widget_show (ti_dialog->txf_help_text);
1236 
1237  gnc_tax_info_set_changed (ti_dialog, TRUE);
1238 }
1239 
1240 static void
1241 current_account_toggled_cb (GtkToggleButton *togglebutton,
1242  gpointer user_data)
1243 {
1244  TaxInfoDialog *ti_dialog = user_data;
1245 
1246  gnc_tax_info_set_changed (ti_dialog, TRUE);
1247 }
1248 
1249 static void
1250 copy_number_value_changed_cb (GtkSpinButton *spinbutton,
1251  gpointer user_data)
1252 {
1253  TaxInfoDialog *ti_dialog = user_data;
1254 
1255  gnc_tax_info_set_changed (ti_dialog, TRUE);
1256 }
1257 
1258 static void
1259 gnc_tax_info_dialog_create (GtkWidget * parent, TaxInfoDialog *ti_dialog)
1260 {
1261  GtkWidget *dialog;
1262  GtkBuilder *builder;
1263  GtkTreeView *tree_view;
1264  GtkTreeSelection *selection;
1265  GtkWidget *label;
1266 
1267  builder = gtk_builder_new();
1268  gnc_builder_add_from_file (builder, "dialog-tax-info.glade", "copy_spin_adj");
1269  gnc_builder_add_from_file (builder, "dialog-tax-info.glade", "Tax Information Dialog");
1270 
1271  dialog = GTK_WIDGET(gtk_builder_get_object (builder, "Tax Information Dialog"));
1272  ti_dialog->dialog = dialog;
1273 
1274  initialize_getters ();
1275 
1276  g_signal_connect (G_OBJECT (dialog), "response",
1277  G_CALLBACK (gnc_tax_info_dialog_response), ti_dialog);
1278 
1279  g_signal_connect (G_OBJECT (dialog), "destroy",
1280  G_CALLBACK (window_destroy_cb), ti_dialog);
1281 
1282  /* parent */
1283  if (parent != NULL)
1284  gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
1285 
1286  /* default to ok */
1287  gtk_dialog_set_default_response (GTK_DIALOG(dialog), GTK_RESPONSE_OK);
1288 
1289  /* tax identity */
1290  {
1291  GtkWidget *label;
1292  GtkWidget *edit_button;
1293 
1294  ti_dialog->this_book = gnc_get_current_book();
1295  ti_dialog->tax_name = gnc_get_current_book_tax_name();
1296  ti_dialog->tax_type = gnc_get_current_book_tax_type();
1297 
1298  label = GTK_WIDGET(gtk_builder_get_object (builder, "entity_name"));
1299  ti_dialog->entity_name_display = label;
1300  gtk_label_set_text (GTK_LABEL (label), ti_dialog->tax_name);
1301  ti_dialog->entity_name_entry = NULL;
1302 
1303  load_tax_entity_type_list (ti_dialog); /* initialize tax_type_combo_text */
1304 
1305  label = GTK_WIDGET(gtk_builder_get_object (builder, "entity_type"));
1306  ti_dialog->entity_type_display = label;
1307  if (ti_dialog->tax_type != NULL)
1308  gtk_label_set_text (GTK_LABEL (label), ti_dialog->tax_type_combo_text);
1309  ti_dialog->entity_type_combo = NULL;
1310 
1311  edit_button = GTK_WIDGET(gtk_builder_get_object (builder, "identity_edit_button"));
1312  ti_dialog->tax_identity_edit_button = edit_button;
1313  g_signal_connect (G_OBJECT (edit_button), "clicked",
1314  G_CALLBACK (identity_edit_clicked_cb), ti_dialog);
1315  ti_dialog->tax_type_changed = FALSE;
1316  }
1317 
1318  ti_dialog->income_txf_infos = load_txf_info (INCOME, ti_dialog);
1319  ti_dialog->expense_txf_infos = load_txf_info (EXPENSE, ti_dialog);
1320  ti_dialog->asset_txf_infos = load_txf_info (ASSET, ti_dialog);
1321  ti_dialog->liab_eq_txf_infos = load_txf_info (LIAB_EQ, ti_dialog);
1322 
1323  /* tax information */
1324  {
1325  GtkListStore *store;
1326  GtkTreeViewColumn *column;
1327  GtkCellRenderer *renderer;
1328  GtkWidget *button;
1329  GtkWidget *text;
1330 
1331  ti_dialog->txf_info = GTK_WIDGET(gtk_builder_get_object (builder, "tax_info_vbox"));
1332  button = GTK_WIDGET(gtk_builder_get_object (builder, "tax_related_button"));
1333  ti_dialog->tax_related_button = button;
1334 
1335  g_signal_connect (G_OBJECT (button), "toggled",
1336  G_CALLBACK (tax_related_toggled_cb), ti_dialog);
1337 
1338  text = GTK_WIDGET(gtk_builder_get_object (builder, "txf_help_text"));
1339  gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1340  ti_dialog->txf_help_text = text;
1341 
1342  tree_view = GTK_TREE_VIEW(gtk_builder_get_object (builder, "txf_category_view"));
1343  store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
1344  gtk_tree_view_set_model(tree_view, GTK_TREE_MODEL(store));
1345  g_object_unref(store);
1346  renderer = gtk_cell_renderer_text_new();
1347  column = gtk_tree_view_column_new_with_attributes
1348  (_("Form"), renderer, "text", 0, NULL);
1349  gtk_tree_view_append_column(tree_view, GTK_TREE_VIEW_COLUMN(column));
1350  renderer = gtk_cell_renderer_text_new();
1351  column = gtk_tree_view_column_new_with_attributes
1352  (_("Description"), renderer, "text", 1, NULL);
1353  gtk_tree_view_append_column(tree_view, GTK_TREE_VIEW_COLUMN(column));
1354  ti_dialog->txf_category_view = GTK_WIDGET(tree_view);
1355 
1356  selection = gtk_tree_view_get_selection(tree_view);
1357  g_signal_connect (G_OBJECT (selection), "changed",
1358  G_CALLBACK (txf_code_select_row_cb), ti_dialog);
1359 
1360  label = GTK_WIDGET(gtk_builder_get_object (builder, "txf_category_label"));
1361  gtk_label_set_mnemonic_widget(GTK_LABEL(label), GTK_WIDGET(tree_view));
1362 
1363  button = GTK_WIDGET(gtk_builder_get_object (builder, "current_account_button"));
1364  ti_dialog->current_account_button = button;
1365 
1366  button = GTK_WIDGET(gtk_builder_get_object (builder, "parent_account_button"));
1367  ti_dialog->parent_account_button = button;
1368 
1369  ti_dialog->help_scroll = GTK_WIDGET(gtk_builder_get_object (builder, "help_scroll"));
1370  ti_dialog->payer_vbox = GTK_WIDGET(gtk_builder_get_object (builder, "payer_name_source_vbox"));
1371  ti_dialog->copy_vbox = GTK_WIDGET(gtk_builder_get_object (builder, "copy_number_vbox"));
1372  ti_dialog->txf_vbox = GTK_WIDGET(gtk_builder_get_object (builder, "txf_categories_vbox"));
1373  ti_dialog->pns_vbox = GTK_WIDGET(gtk_builder_get_object (builder, "pns_copy_hbox"));
1374 
1375  g_signal_connect (G_OBJECT (button), "toggled",
1376  G_CALLBACK (current_account_toggled_cb),
1377  ti_dialog);
1378 
1379  button = GTK_WIDGET(gtk_builder_get_object (builder, "copy_spin_button"));
1380  ti_dialog->copy_spin_button = button;
1381 
1382  g_signal_connect (G_OBJECT (button), "value-changed",
1383  G_CALLBACK (copy_number_value_changed_cb),
1384  ti_dialog);
1385  }
1386 
1387  /* account tree */
1388  {
1389  GtkWidget *income_radio, *expense_radio, *asset_radio,
1390  *liab_eq_radio, *box;
1391 
1392  ti_dialog->acct_info = GTK_WIDGET(gtk_builder_get_object (builder, "acct_info_vbox"));
1393  ti_dialog->num_acct_label = GTK_WIDGET(gtk_builder_get_object (builder, "num_accounts_label"));
1394 
1395  tree_view = gnc_tree_view_account_new (FALSE);
1396  gnc_tree_view_account_set_filter (GNC_TREE_VIEW_ACCOUNT(tree_view),
1397  gnc_tax_info_dialog_account_filter_func,
1398  ti_dialog, NULL);
1399  ti_dialog->account_treeview = GTK_WIDGET(tree_view);
1400 
1401  selection = gtk_tree_view_get_selection (tree_view);
1402  gtk_tree_selection_set_mode (selection, GTK_SELECTION_EXTENDED);
1403  g_signal_connect (G_OBJECT (selection), "changed",
1404  G_CALLBACK (gnc_tax_info_account_changed_cb),
1405  ti_dialog);
1406 
1407  gtk_widget_show (ti_dialog->account_treeview);
1408  box = GTK_WIDGET(gtk_builder_get_object (builder, "account_scroll"));
1409  gtk_container_add (GTK_CONTAINER (box), ti_dialog->account_treeview);
1410 
1411  label = GTK_WIDGET(gtk_builder_get_object (builder, "accounts_label"));
1412  gtk_label_set_mnemonic_widget(GTK_LABEL(label), GTK_WIDGET(tree_view));
1413 
1414  income_radio = GTK_WIDGET(gtk_builder_get_object (builder, "income_radio"));
1415  expense_radio = GTK_WIDGET(gtk_builder_get_object (builder, "expense_radio"));
1416  ti_dialog->expense_radio = expense_radio;
1417  asset_radio = GTK_WIDGET(gtk_builder_get_object (builder, "asset_radio"));
1418  ti_dialog->asset_radio = asset_radio;
1419  liab_eq_radio = GTK_WIDGET(gtk_builder_get_object (builder, "liab_eq_radio"));
1420  ti_dialog->liab_eq_radio = liab_eq_radio;
1421  ti_dialog->account_type = ACCT_TYPE_EXPENSE;
1422  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(expense_radio), TRUE);
1423 
1424  g_signal_connect (G_OBJECT (income_radio), "toggled",
1425  G_CALLBACK (gnc_tax_info_acct_type_cb),
1426  ti_dialog);
1427  g_signal_connect (G_OBJECT (expense_radio), "toggled",
1428  G_CALLBACK (gnc_tax_info_acct_type_cb),
1429  ti_dialog);
1430  g_signal_connect (G_OBJECT (asset_radio), "toggled",
1431  G_CALLBACK (gnc_tax_info_acct_type_cb),
1432  ti_dialog);
1433  g_signal_connect (G_OBJECT (liab_eq_radio), "toggled",
1434  G_CALLBACK (gnc_tax_info_acct_type_cb),
1435  ti_dialog);
1436  }
1437 
1438  /* select subaccounts button */
1439  {
1440  GtkWidget *button;
1441 
1442  button = GTK_WIDGET(gtk_builder_get_object (builder, "select_subaccounts_button"));
1443  ti_dialog->select_button = button;
1444 
1445  g_signal_connect (G_OBJECT (button), "clicked",
1446  G_CALLBACK (select_subaccounts_clicked),
1447  ti_dialog);
1448  g_signal_connect (G_OBJECT (ti_dialog->account_treeview), "cursor_changed",
1449  G_CALLBACK (cursor_changed_cb),
1450  ti_dialog);
1451  }
1452 
1453  tax_info_show_acct_type_accounts (ti_dialog);
1454  gnc_tax_info_update_accounts (ti_dialog);
1455  clear_gui (ti_dialog);
1456  gnc_tax_info_set_changed (ti_dialog, FALSE);
1457 
1458  gnc_restore_window_size(GNC_PREFS_GROUP, GTK_WINDOW(ti_dialog->dialog));
1459 
1460 
1461  if (gnc_prefs_get_bool(GNC_PREFS_GROUP_GENERAL, GNC_PREF_SAVE_GEOMETRY))
1462  {
1463  GObject *object = gtk_builder_get_object (builder, "paned");
1464  gnc_prefs_bind (GNC_PREFS_GROUP, GNC_PREF_PANED_POS, object, "position");
1465  }
1466  g_object_unref (builder);
1467 }
1468 
1469 static void
1470 close_handler (gpointer user_data)
1471 {
1472  TaxInfoDialog *ti_dialog = user_data;
1473 
1474  gnc_save_window_size(GNC_PREFS_GROUP, GTK_WINDOW(ti_dialog->dialog));
1475  gtk_widget_destroy (ti_dialog->dialog);
1476 }
1477 
1478 static void
1479 refresh_handler (GHashTable *changes, gpointer user_data)
1480 {
1481  TaxInfoDialog *ti_dialog = user_data;
1482 
1483  gnc_tax_info_update_accounts (ti_dialog);
1484 }
1485 
1486 /********************************************************************\
1487  * gnc_tax_info_dialog *
1488  * opens up a window to set account tax information *
1489  * *
1490  * Args: parent - the parent of the window to be created *
1491  * Return: nothing *
1492 \********************************************************************/
1493 void
1494 gnc_tax_info_dialog (GtkWidget * parent)
1495 {
1496  TaxInfoDialog *ti_dialog;
1497  gint component_id;
1498 
1499  ti_dialog = g_new0 (TaxInfoDialog, 1);
1500 
1501  gnc_tax_info_dialog_create (parent, ti_dialog);
1502 
1503  component_id = gnc_register_gui_component (DIALOG_TAX_INFO_CM_CLASS,
1504  refresh_handler, close_handler,
1505  ti_dialog);
1506  gnc_gui_component_set_session (component_id, gnc_get_current_session ());
1507 
1508  gnc_gui_component_watch_entity_type (component_id,
1509  GNC_ID_ACCOUNT,
1510  QOF_EVENT_MODIFY | QOF_EVENT_DESTROY);
1511 
1512  set_focus_sensitivity (ti_dialog);
1513 
1514  gtk_widget_show (ti_dialog->dialog);
1515 }
void gnc_tree_view_account_get_view_info(GncTreeViewAccount *account_view, AccountViewInfo *avi)
gint64 xaccAccountGetTaxUSCopyNumber(const Account *acc)
Definition: Account.c:3876
GList * gnc_tree_view_account_get_selected_accounts(GncTreeViewAccount *view)
Account * gnc_tree_view_account_get_cursor_account(GncTreeViewAccount *view)
utility functions for the GnuCash UI
GNCAccountType xaccAccountGetType(const Account *acc)
Definition: Account.c:3009
void xaccAccountSetTaxRelated(Account *acc, gboolean tax_related)
Definition: Account.c:3815
void gnc_tree_view_account_set_view_info(GncTreeViewAccount *account_view, AccountViewInfo *avi)
void gnc_tree_view_account_set_filter(GncTreeViewAccount *view, gnc_tree_view_account_filter_func func, gpointer data, GSourceFunc destroy)
Account handling public routines.
void gnc_tree_view_account_select_subaccounts(GncTreeViewAccount *view, Account *account)
void gnc_tree_view_account_refilter(GncTreeViewAccount *view)
void gnc_prefs_bind(const gchar *group, const gchar *pref_name, gpointer object, const gchar *property)
Definition: gnc-prefs.c:186
GtkTreeView implementation for gnucash account tree.
GtkTreeView * gnc_tree_view_account_new(gboolean show_root)
const char * xaccAccountGetTaxUSPayerNameSource(const Account *acc)
Definition: Account.c:3856
const char * xaccAccountGetTaxUSCode(const Account *acc)
Definition: Account.c:3833
gboolean xaccAccountGetTaxRelated(const Account *acc)
Definition: Account.c:3808
GNCAccountType
Definition: Account.h:96
Generic api to store and retrieve preferences.
void xaccAccountSetTaxUSCode(Account *acc, const char *code)
Definition: Account.c:3840
void xaccAccountBeginEdit(Account *acc)
Definition: Account.c:1280
gint gnc_tree_view_account_count_children(GncTreeViewAccount *view, Account *account)
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
Definition: gnc-prefs.c:196
void xaccAccountSetTaxUSPayerNameSource(Account *acc, const char *source)
Definition: Account.c:3864
void xaccAccountCommitEdit(Account *acc)
Definition: Account.c:1321
void xaccAccountSetTaxUSCopyNumber(Account *acc, gint64 copy_number)
Definition: Account.c:3887