~siretart/gnucash/ubuntu-fullsource

« back to all changes in this revision

Viewing changes to src/gnome/dialog-tax-info.c

  • Committer: Reinhard Tartler
  • Date: 2008-08-03 07:25:46 UTC
  • Revision ID: siretart@tauware.de-20080803072546-y6p8xda8zpfi62ys
import gnucash_2.2.4.orig.tar.gz

The original tarball had the md5sum: 27e660297dc5b8ce574515779d05a5a5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************\
 
2
 * dialog-tax-info.c -- tax information dialog                      *
 
3
 * Copyright (C) 2001 Gnumatic, Inc.                                *
 
4
 * Author: Dave Peticolas <dave@krondo.com>                         *
 
5
 *                                                                  *
 
6
 * This program is free software; you can redistribute it and/or    *
 
7
 * modify it under the terms of the GNU General Public License as   *
 
8
 * published by the Free Software Foundation; either version 2 of   *
 
9
 * the License, or (at your option) any later version.              *
 
10
 *                                                                  *
 
11
 * This program is distributed in the hope that it will be useful,  *
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
 
14
 * GNU General Public License for more details.                     *
 
15
 *                                                                  *
 
16
 * You should have received a copy of the GNU General Public License*
 
17
 * along with this program; if not, contact:                        *
 
18
 *                                                                  *
 
19
 * Free Software Foundation           Voice:  +1-617-542-5942       *
 
20
 * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
 
21
 * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
 
22
\********************************************************************/
 
23
 
 
24
#include "config.h"
 
25
 
 
26
#include <gtk/gtk.h>
 
27
#include <glib/gi18n.h>
 
28
#include <libguile.h>
 
29
#include "guile-mappings.h"
 
30
 
 
31
#include "Account.h"
 
32
#include "gnc-ui-util.h"
 
33
#include "dialog-utils.h"
 
34
#include "gnc-gconf-utils.h"
 
35
#include "gnc-tree-view-account.h"
 
36
#include "gnc-component-manager.h"
 
37
#include "qof.h"
 
38
#include "gnc-ui.h"
 
39
 
 
40
 
 
41
#define DIALOG_TAX_INFO_CM_CLASS "dialog-tax-info"
 
42
#define GCONF_SECTION "dialogs/tax_info"
 
43
#define PANED_POSITION "paned_position"
 
44
 
 
45
/* This static indicates the debugging module that this .o belongs to.  */
 
46
/* static short module = MOD_GUI; */
 
47
 
 
48
static struct
 
49
{
 
50
  SCM payer_name_source;
 
51
  SCM form;
 
52
  SCM description;
 
53
  SCM help;
 
54
 
 
55
  SCM codes;
 
56
} getters;
 
57
 
 
58
typedef struct
 
59
{
 
60
  char *code;
 
61
  char *payer_name_source;
 
62
  char *form;
 
63
  char *description;
 
64
  char *help;
 
65
} TXFInfo;
 
66
 
 
67
typedef struct
 
68
{
 
69
  GtkWidget * dialog;
 
70
 
 
71
  GtkWidget * account_treeview;
 
72
  GtkWidget * select_button;
 
73
 
 
74
  GtkWidget * tax_related_button;
 
75
  GtkWidget * txf_category_view;
 
76
  GtkWidget * txf_help_text;
 
77
  GtkWidget * current_account_button;
 
78
  GtkWidget * parent_account_button;
 
79
 
 
80
  GList * income_txf_infos;
 
81
  GList * expense_txf_infos;
 
82
 
 
83
  gboolean income;
 
84
  gboolean changed;
 
85
 
 
86
  GNCAccountType account_type;
 
87
} TaxInfoDialog;
 
88
 
 
89
 
 
90
static gboolean getters_initialized = FALSE;
 
91
 
 
92
 
 
93
static void
 
94
initialize_getters (void)
 
95
{
 
96
  if (getters_initialized)
 
97
    return;
 
98
 
 
99
  getters.payer_name_source = scm_c_eval_string ("gnc:txf-get-payer-name-source");
 
100
  getters.form              = scm_c_eval_string ("gnc:txf-get-form");
 
101
  getters.description       = scm_c_eval_string ("gnc:txf-get-description");
 
102
  getters.help              = scm_c_eval_string ("gnc:txf-get-help");
 
103
 
 
104
  getters.codes             = scm_c_eval_string ("gnc:txf-get-codes");
 
105
 
 
106
  getters_initialized = TRUE;
 
107
}
 
108
 
 
109
static void
 
110
destroy_txf_info (gpointer data, gpointer user_data)
 
111
{
 
112
  TXFInfo *txf_info = data;
 
113
 
 
114
  g_free (txf_info->code);
 
115
  txf_info->code = NULL;
 
116
 
 
117
  g_free (txf_info->payer_name_source);
 
118
  txf_info->payer_name_source = NULL;
 
119
 
 
120
  g_free (txf_info->form);
 
121
  txf_info->form = NULL;
 
122
 
 
123
  g_free (txf_info->description);
 
124
  txf_info->description = NULL;
 
125
 
 
126
  g_free (txf_info->help);
 
127
  txf_info->help = NULL;
 
128
 
 
129
  g_free (txf_info);
 
130
}
 
131
 
 
132
static void
 
133
destroy_txf_infos (GList *infos)
 
134
{
 
135
  g_list_foreach (infos, destroy_txf_info, NULL);
 
136
  g_list_free (infos);
 
137
}
 
138
 
 
139
static void
 
140
gnc_tax_info_set_changed (TaxInfoDialog *ti_dialog, gboolean changed)
 
141
{
 
142
  ti_dialog->changed = changed;
 
143
}
 
144
 
 
145
static GList *
 
146
load_txf_info (gboolean income)
 
147
{
 
148
  GList *infos = NULL;
 
149
  SCM category;
 
150
  SCM codes;
 
151
 
 
152
  initialize_getters ();
 
153
 
 
154
  category = scm_c_eval_string (income ?
 
155
                                "txf-income-categories" :
 
156
                                "txf-expense-categories");
 
157
  if (category == SCM_UNDEFINED)
 
158
  {
 
159
    destroy_txf_infos (infos);
 
160
    return NULL;
 
161
  }
 
162
 
 
163
  codes = scm_call_1 (getters.codes, category);
 
164
  if (!SCM_LISTP (codes))
 
165
  {
 
166
    destroy_txf_infos (infos);
 
167
    return NULL;
 
168
  }
 
169
 
 
170
  while (!SCM_NULLP (codes))
 
171
  {
 
172
    TXFInfo *txf_info;
 
173
    SCM code_scm;
 
174
    const gchar *str;
 
175
    SCM scm;
 
176
 
 
177
    code_scm  = SCM_CAR (codes);
 
178
    codes     = SCM_CDR (codes);
 
179
 
 
180
    scm = scm_call_2 (getters.payer_name_source, category, code_scm);
 
181
    str = SCM_SYMBOL_CHARS (scm);
 
182
    if (safe_strcmp (str, "not-impl") == 0)
 
183
    {
 
184
      continue;
 
185
    }
 
186
 
 
187
    txf_info = g_new0 (TXFInfo, 1);
 
188
 
 
189
    if (safe_strcmp (str, "none") == 0)
 
190
      txf_info->payer_name_source = NULL;
 
191
    else
 
192
      txf_info->payer_name_source = g_strdup (str);
 
193
 
 
194
    str = SCM_SYMBOLP(code_scm) ? SCM_SYMBOL_CHARS(code_scm) : "";
 
195
    txf_info->code = g_strdup (str);
 
196
 
 
197
    scm = scm_call_2 (getters.form, category, code_scm);
 
198
    str = SCM_STRINGP(scm) ? SCM_STRING_CHARS(scm) : "";
 
199
    txf_info->form = g_strdup (str);
 
200
 
 
201
    scm = scm_call_2 (getters.description, category, code_scm);
 
202
    str = SCM_STRINGP(scm) ? SCM_STRING_CHARS(scm) : "";
 
203
    txf_info->description = g_strdup (str);
 
204
 
 
205
    scm = scm_call_2 (getters.help, category, code_scm);
 
206
    str = SCM_STRINGP(scm) ? SCM_STRING_CHARS(scm) : "";
 
207
    txf_info->help = g_strdup (str);
 
208
 
 
209
    infos = g_list_prepend (infos, txf_info);
 
210
  }
 
211
 
 
212
  return g_list_reverse (infos);
 
213
}
 
214
 
 
215
static GList *
 
216
tax_infos (TaxInfoDialog *ti_dialog)
 
217
{
 
218
  return
 
219
    ti_dialog->income ?
 
220
    ti_dialog->income_txf_infos : ti_dialog->expense_txf_infos;
 
221
}
 
222
 
 
223
static void
 
224
load_category_list (TaxInfoDialog *ti_dialog)
 
225
{
 
226
  GtkTreeView *view;
 
227
  GtkListStore *store;
 
228
  GtkTreeIter iter;
 
229
  GList *codes;
 
230
 
 
231
  view = GTK_TREE_VIEW(ti_dialog->txf_category_view);
 
232
  store = GTK_LIST_STORE(gtk_tree_view_get_model(view));
 
233
  g_object_ref(store);
 
234
  gtk_tree_view_set_model(view, NULL);
 
235
 
 
236
  gtk_list_store_clear(store);
 
237
 
 
238
  codes = tax_infos (ti_dialog);
 
239
  for ( ; codes; codes = codes->next)
 
240
  {
 
241
    TXFInfo *txf_info = codes->data;
 
242
 
 
243
    gtk_list_store_append(store, &iter);
 
244
    gtk_list_store_set(store, &iter,
 
245
                       0, txf_info->form,
 
246
                       1, txf_info->description,
 
247
                       -1);
 
248
  }
 
249
 
 
250
  gtk_tree_view_set_model(view, GTK_TREE_MODEL(store));
 
251
  g_object_unref(store);
 
252
}
 
253
 
 
254
static void
 
255
clear_gui (TaxInfoDialog *ti_dialog)
 
256
{
 
257
  GtkTreeView *view;
 
258
  GtkTreeSelection *selection;
 
259
 
 
260
  gtk_toggle_button_set_active
 
261
    (GTK_TOGGLE_BUTTON (ti_dialog->tax_related_button), FALSE);
 
262
 
 
263
  view = GTK_TREE_VIEW(ti_dialog->txf_category_view);
 
264
  selection = gtk_tree_view_get_selection(view);
 
265
  gtk_tree_selection_unselect_all(selection);
 
266
 
 
267
  gtk_toggle_button_set_active
 
268
    (GTK_TOGGLE_BUTTON (ti_dialog->current_account_button), TRUE);
 
269
}
 
270
 
 
271
static gboolean
 
272
gnc_tax_info_dialog_account_filter_func (Account *account,
 
273
                                         gpointer data)
 
274
{
 
275
  TaxInfoDialog *dialog = data;
 
276
 
 
277
  return xaccAccountGetType (account) == dialog->account_type;
 
278
}
 
279
 
 
280
static TXFInfo *
 
281
txf_infos_find_code (GList *infos, const char *code)
 
282
{
 
283
  for (; infos; infos = infos->next)
 
284
  {
 
285
    TXFInfo *info = infos->data;
 
286
 
 
287
    if (safe_strcmp (code, info->code) == 0)
 
288
      return info;
 
289
  }
 
290
 
 
291
  return NULL;
 
292
}
 
293
 
 
294
static void
 
295
account_to_gui (TaxInfoDialog *ti_dialog, Account *account)
 
296
{
 
297
  GtkTreeView *view;
 
298
  GtkTreeSelection *selection;
 
299
  GtkTreePath *path;
 
300
  gboolean tax_related;
 
301
  const char *str;
 
302
  TXFInfo *info;
 
303
  GList *infos;
 
304
  gint index;
 
305
 
 
306
  if (!account)
 
307
  {
 
308
    clear_gui (ti_dialog);
 
309
    return;
 
310
  }
 
311
 
 
312
  tax_related = xaccAccountGetTaxRelated (account);
 
313
  gtk_toggle_button_set_active
 
314
    (GTK_TOGGLE_BUTTON (ti_dialog->tax_related_button), tax_related);
 
315
 
 
316
  infos = tax_infos (ti_dialog);
 
317
 
 
318
  str = xaccAccountGetTaxUSCode (account);
 
319
  info = txf_infos_find_code (infos, str);
 
320
  if (info)
 
321
    index = g_list_index (infos, info);
 
322
  else
 
323
    index = 0;
 
324
  if (index < 0)
 
325
    index = 0;
 
326
 
 
327
  view = GTK_TREE_VIEW(ti_dialog->txf_category_view);
 
328
  selection = gtk_tree_view_get_selection(view);
 
329
  path =  gtk_tree_path_new_from_indices(index, -1);
 
330
  gtk_tree_selection_select_path(selection, path);
 
331
  gtk_tree_view_scroll_to_cell(view, path, NULL, FALSE, 0, 0);
 
332
  gtk_tree_path_free(path);
 
333
 
 
334
  str = xaccAccountGetTaxUSPayerNameSource (account);
 
335
  if (safe_strcmp (str, "parent") == 0)
 
336
    gtk_toggle_button_set_active
 
337
      (GTK_TOGGLE_BUTTON (ti_dialog->parent_account_button), TRUE);
 
338
  else
 
339
    gtk_toggle_button_set_active
 
340
      (GTK_TOGGLE_BUTTON (ti_dialog->current_account_button), TRUE);
 
341
}
 
342
 
 
343
static void
 
344
gui_to_accounts (TaxInfoDialog *ti_dialog)
 
345
{
 
346
  GtkTreeView *view;
 
347
  GtkTreeModel *model;
 
348
  GtkTreeSelection *selection;
 
349
  GtkTreePath *path;
 
350
  GtkTreeIter iter;
 
351
  gint *indices;
 
352
  gboolean tax_related;
 
353
  const char *code;
 
354
  const char *pns;
 
355
  GList *accounts;
 
356
  TXFInfo *info;
 
357
  GList *infos;
 
358
  GList *node;
 
359
 
 
360
  tax_related = gtk_toggle_button_get_active
 
361
    (GTK_TOGGLE_BUTTON (ti_dialog->tax_related_button));
 
362
 
 
363
  infos = tax_infos (ti_dialog);
 
364
 
 
365
  view = GTK_TREE_VIEW(ti_dialog->txf_category_view);
 
366
  selection = gtk_tree_view_get_selection(view);
 
367
  if (!gtk_tree_selection_get_selected(selection, &model, &iter))
 
368
    return;
 
369
  path = gtk_tree_model_get_path(model, &iter);
 
370
  indices = gtk_tree_path_get_indices(path);
 
371
  info = g_list_nth_data (infos, indices[0]);
 
372
  gtk_tree_path_free(path);
 
373
  g_return_if_fail (info != NULL);
 
374
 
 
375
  code = tax_related ? info->code : NULL;
 
376
 
 
377
  if (tax_related && info->payer_name_source)
 
378
  {
 
379
    gboolean current;
 
380
 
 
381
    current = gtk_toggle_button_get_active
 
382
      (GTK_TOGGLE_BUTTON (ti_dialog->current_account_button));
 
383
 
 
384
    pns = current ? "current" : "parent";
 
385
  }
 
386
  else
 
387
    pns = NULL;
 
388
 
 
389
  accounts = gnc_tree_view_account_get_selected_accounts
 
390
    (GNC_TREE_VIEW_ACCOUNT(ti_dialog->account_treeview));
 
391
 
 
392
  for (node = accounts; node; node = node->next)
 
393
  {
 
394
    Account *account = node->data;
 
395
 
 
396
    xaccAccountBeginEdit (account);
 
397
 
 
398
    xaccAccountSetTaxRelated (account, tax_related);
 
399
    xaccAccountSetTaxUSCode (account, code);
 
400
    xaccAccountSetTaxUSPayerNameSource (account, pns);
 
401
 
 
402
    xaccAccountCommitEdit (account);
 
403
  }
 
404
}
 
405
 
 
406
static void
 
407
window_destroy_cb (GtkObject *object, gpointer data)
 
408
{
 
409
  TaxInfoDialog *ti_dialog = data;
 
410
 
 
411
  gnc_unregister_gui_component_by_data (DIALOG_TAX_INFO_CM_CLASS, ti_dialog);
 
412
 
 
413
  destroy_txf_infos (ti_dialog->income_txf_infos);
 
414
  ti_dialog->income_txf_infos = NULL;
 
415
 
 
416
  destroy_txf_infos (ti_dialog->expense_txf_infos);
 
417
  ti_dialog->expense_txf_infos = NULL;
 
418
 
 
419
  g_free (ti_dialog);
 
420
}
 
421
 
 
422
static void
 
423
cursor_changed_cb (GtkWidget *widget, gpointer data)
 
424
{
 
425
  TaxInfoDialog *ti_dialog = data;
 
426
  GncTreeViewAccount *account_tree;
 
427
  Account *account;
 
428
  gint num_children;
 
429
 
 
430
  account_tree = GNC_TREE_VIEW_ACCOUNT (ti_dialog->account_treeview);
 
431
  account = gnc_tree_view_account_get_cursor_account (account_tree);
 
432
  if (!account) {
 
433
    gtk_widget_set_sensitive(ti_dialog->select_button, FALSE);
 
434
    return;
 
435
  }
 
436
 
 
437
  num_children = gnc_tree_view_account_count_children(account_tree, account);
 
438
  gtk_widget_set_sensitive(ti_dialog->select_button, num_children > 0);
 
439
}
 
440
 
 
441
static void
 
442
select_subaccounts_clicked (GtkWidget *widget, gpointer data)
 
443
{
 
444
  TaxInfoDialog *ti_dialog = data;
 
445
  GncTreeViewAccount *account_tree;
 
446
  Account *account;
 
447
 
 
448
  account_tree = GNC_TREE_VIEW_ACCOUNT (ti_dialog->account_treeview);
 
449
  account = gnc_tree_view_account_get_cursor_account (account_tree);
 
450
  if (!account)
 
451
    return;
 
452
 
 
453
  gnc_tree_view_account_select_subaccounts (account_tree, account);
 
454
 
 
455
  gtk_widget_grab_focus (ti_dialog->account_treeview);
 
456
}
 
457
 
 
458
static void
 
459
gnc_tax_info_dialog_response (GtkDialog *dialog, gint response, gpointer data)
 
460
{
 
461
  TaxInfoDialog *ti_dialog = data;
 
462
 
 
463
  if (response == GTK_RESPONSE_OK && ti_dialog->changed)
 
464
      gui_to_accounts (ti_dialog);
 
465
 
 
466
  gnc_close_gui_component_by_data (DIALOG_TAX_INFO_CM_CLASS, ti_dialog);
 
467
}
 
468
 
 
469
static void
 
470
tax_info_show_income_accounts (TaxInfoDialog *ti_dialog, gboolean show_income)
 
471
{
 
472
  GncTreeViewAccount *tree;
 
473
  AccountViewInfo info;
 
474
  GNCAccountType type;
 
475
  GNCAccountType show_type;
 
476
 
 
477
  ti_dialog->income = show_income;
 
478
 
 
479
  tree = GNC_TREE_VIEW_ACCOUNT (ti_dialog->account_treeview);
 
480
  show_type = show_income ? ACCT_TYPE_INCOME : ACCT_TYPE_EXPENSE;
 
481
 
 
482
  gnc_tree_view_account_get_view_info (tree, &info);
 
483
 
 
484
  for (type = 0; type < NUM_ACCOUNT_TYPES; type++)
 
485
    info.include_type[type] = (type == show_type);
 
486
 
 
487
  gnc_tree_view_account_set_view_info (tree, &info);
 
488
 
 
489
  load_category_list (ti_dialog);
 
490
  cursor_changed_cb(GTK_WIDGET(tree), ti_dialog);
 
491
}
 
492
 
 
493
static int
 
494
gnc_tax_info_update_accounts (TaxInfoDialog *ti_dialog)
 
495
{
 
496
  GncTreeViewAccount *tree;
 
497
  GtkTreeSelection* selection;
 
498
  GtkWidget *label;
 
499
  GtkWidget *vbox;
 
500
  int num_accounts;
 
501
  char *string;
 
502
 
 
503
  tree = GNC_TREE_VIEW_ACCOUNT(ti_dialog->account_treeview);
 
504
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(tree));
 
505
  num_accounts = gtk_tree_selection_count_selected_rows (selection);
 
506
 
 
507
  label = gnc_glade_lookup_widget (ti_dialog->dialog, "num_accounts_label");
 
508
  vbox = gnc_glade_lookup_widget (ti_dialog->dialog, "tax_info_vbox");
 
509
 
 
510
  string = g_strdup_printf ("%d", num_accounts);
 
511
  gtk_label_set_text (GTK_LABEL (label), string);
 
512
  g_free (string);
 
513
 
 
514
  gtk_widget_set_sensitive (vbox, num_accounts > 0);
 
515
 
 
516
  return num_accounts;
 
517
}
 
518
 
 
519
static void
 
520
gnc_tax_info_income_cb (GtkWidget *w, gpointer data)
 
521
{
 
522
  TaxInfoDialog *ti_dialog = data;
 
523
  gboolean show_income;
 
524
 
 
525
  show_income = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w));
 
526
 
 
527
  tax_info_show_income_accounts (ti_dialog, show_income);
 
528
 
 
529
  ti_dialog->account_type = show_income ? ACCT_TYPE_INCOME : ACCT_TYPE_EXPENSE;
 
530
  gnc_tree_view_account_refilter (GNC_TREE_VIEW_ACCOUNT (ti_dialog->account_treeview));
 
531
 
 
532
  gnc_tax_info_update_accounts (ti_dialog);
 
533
 
 
534
  clear_gui (ti_dialog);
 
535
}
 
536
 
 
537
static void
 
538
gnc_tax_info_account_changed_cb (GtkTreeSelection *selection,
 
539
                                 gpointer data)
 
540
{
 
541
  TaxInfoDialog *ti_dialog = data;
 
542
  GncTreeViewAccount *view;
 
543
  GList *accounts;
 
544
  int num_accounts;
 
545
 
 
546
  g_return_if_fail(GTK_IS_TREE_SELECTION(selection));
 
547
 
 
548
  num_accounts = gnc_tax_info_update_accounts (ti_dialog);
 
549
  switch (num_accounts) {
 
550
   case 0:
 
551
    clear_gui (ti_dialog);
 
552
    gnc_tax_info_set_changed (ti_dialog, FALSE);
 
553
    return;
 
554
 
 
555
   case 1:
 
556
    /* Get the account. This view is set for multiple selection, so we
 
557
       can only get a list of accounts. */
 
558
    view = GNC_TREE_VIEW_ACCOUNT(ti_dialog->account_treeview);
 
559
    accounts = gnc_tree_view_account_get_selected_accounts (view);
 
560
    account_to_gui (ti_dialog, accounts->data);
 
561
    g_list_free(accounts);
 
562
 
 
563
    gnc_tax_info_set_changed (ti_dialog, FALSE);
 
564
    break;
 
565
 
 
566
   default:
 
567
    gnc_tax_info_set_changed (ti_dialog, TRUE);
 
568
    return;
 
569
  }
 
570
}
 
571
 
 
572
static void
 
573
txf_code_select_row_cb (GtkTreeSelection *selection,
 
574
                        gpointer user_data)
 
575
{
 
576
  TaxInfoDialog *ti_dialog = user_data;
 
577
  GtkTreeModel *model;
 
578
  GtkTreePath *path;
 
579
  GtkTreeIter iter;
 
580
  gint *indices;
 
581
  TXFInfo *txf_info;
 
582
  GtkAdjustment *adj;
 
583
  GtkWidget *scroll;
 
584
  GtkWidget *vbox;
 
585
  GtkTextBuffer *tb;
 
586
  const char *text;
 
587
 
 
588
  if (!gtk_tree_selection_get_selected(selection, &model, &iter))
 
589
    return;
 
590
  path = gtk_tree_model_get_path(model, &iter);
 
591
  indices = gtk_tree_path_get_indices(path);
 
592
  txf_info = g_list_nth_data (tax_infos (ti_dialog), indices[0]);
 
593
  gtk_tree_path_free(path);
 
594
 
 
595
  tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(ti_dialog->txf_help_text));
 
596
 
 
597
  text = (txf_info && txf_info->help) ? txf_info->help : "";
 
598
  gtk_text_buffer_set_text (tb, text, -1);
 
599
 
 
600
  scroll = gnc_glade_lookup_widget (GTK_WIDGET (ti_dialog->dialog),
 
601
                                    "help_scroll");
 
602
 
 
603
  adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (scroll));
 
604
  gtk_adjustment_set_value (adj, 0.0);
 
605
 
 
606
  vbox = gnc_glade_lookup_widget (GTK_WIDGET (ti_dialog->dialog),
 
607
                                   "payer_name_source_vbox");
 
608
 
 
609
  if (txf_info && txf_info->payer_name_source)
 
610
  {
 
611
    gboolean current;
 
612
 
 
613
    gtk_widget_set_sensitive (vbox, TRUE);
 
614
 
 
615
    current = (strcmp ("current", txf_info->payer_name_source) == 0);
 
616
 
 
617
    if (current)
 
618
      gtk_toggle_button_set_active
 
619
        (GTK_TOGGLE_BUTTON (ti_dialog->current_account_button), TRUE);
 
620
    else
 
621
      gtk_toggle_button_set_active
 
622
        (GTK_TOGGLE_BUTTON (ti_dialog->parent_account_button), TRUE);
 
623
  }
 
624
  else
 
625
  {
 
626
    gtk_widget_set_sensitive (vbox, FALSE);
 
627
    gtk_toggle_button_set_active
 
628
      (GTK_TOGGLE_BUTTON (ti_dialog->current_account_button), TRUE);
 
629
  }
 
630
 
 
631
  gnc_tax_info_set_changed (ti_dialog, TRUE);
 
632
}
 
633
 
 
634
static void
 
635
tax_related_toggled_cb (GtkToggleButton *togglebutton,
 
636
                        gpointer user_data)
 
637
{
 
638
  TaxInfoDialog *ti_dialog = user_data;
 
639
  GtkWidget *vbox;
 
640
  gboolean on;
 
641
 
 
642
  on = gtk_toggle_button_get_active (togglebutton);
 
643
 
 
644
  vbox = gnc_glade_lookup_widget (GTK_WIDGET (togglebutton),
 
645
                                   "txf_categories_vbox");
 
646
  gtk_widget_set_sensitive (vbox, on);
 
647
 
 
648
  gnc_tax_info_set_changed (ti_dialog, TRUE);
 
649
}
 
650
 
 
651
static void
 
652
current_account_toggled_cb (GtkToggleButton *togglebutton,
 
653
                            gpointer user_data)
 
654
{
 
655
  TaxInfoDialog *ti_dialog = user_data;
 
656
 
 
657
  gnc_tax_info_set_changed (ti_dialog, TRUE);
 
658
}
 
659
 
 
660
static void
 
661
gnc_tax_info_dialog_create (GtkWidget * parent, TaxInfoDialog *ti_dialog)
 
662
{
 
663
  GtkWidget *dialog;
 
664
  GtkObject *tido;
 
665
  GladeXML  *xml;
 
666
  GtkTreeView *tree_view;
 
667
  GtkTreeSelection *selection;
 
668
  GtkWidget *label;
 
669
 
 
670
  xml = gnc_glade_xml_new ("tax.glade", "Tax Information Dialog");
 
671
 
 
672
  dialog = glade_xml_get_widget (xml, "Tax Information Dialog");
 
673
  ti_dialog->dialog = dialog;
 
674
  tido = GTK_OBJECT (dialog);
 
675
 
 
676
  ti_dialog->account_type = ACCT_TYPE_EXPENSE;
 
677
  ti_dialog->income_txf_infos = load_txf_info (TRUE);
 
678
  ti_dialog->expense_txf_infos = load_txf_info (FALSE);
 
679
 
 
680
  g_signal_connect (G_OBJECT (dialog), "response",
 
681
                    G_CALLBACK (gnc_tax_info_dialog_response), ti_dialog);
 
682
 
 
683
  g_signal_connect (G_OBJECT (dialog), "destroy",
 
684
                    G_CALLBACK (window_destroy_cb), ti_dialog);
 
685
 
 
686
  /* parent */
 
687
  if (parent != NULL)
 
688
    gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
 
689
 
 
690
  /* default to ok */
 
691
  gtk_dialog_set_default_response (GTK_DIALOG(dialog), GTK_RESPONSE_OK);
 
692
 
 
693
  /* tax information */
 
694
  {
 
695
    GtkListStore *store;
 
696
    GtkTreeViewColumn *column;
 
697
    GtkCellRenderer *renderer; 
 
698
    GtkWidget *button;
 
699
    GtkWidget *text;
 
700
 
 
701
    button = glade_xml_get_widget (xml, "tax_related_button");
 
702
    ti_dialog->tax_related_button = button;
 
703
 
 
704
    g_signal_connect (G_OBJECT (button), "toggled",
 
705
                      G_CALLBACK  (tax_related_toggled_cb), ti_dialog);
 
706
 
 
707
    text = glade_xml_get_widget (xml, "txf_help_text");
 
708
    gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
 
709
    ti_dialog->txf_help_text = text;
 
710
 
 
711
    tree_view = GTK_TREE_VIEW(glade_xml_get_widget(xml, "txf_category_view"));
 
712
    store =  gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
 
713
    gtk_tree_view_set_model(tree_view, GTK_TREE_MODEL(store));
 
714
    g_object_unref(store);
 
715
    renderer = gtk_cell_renderer_text_new();
 
716
    column = gtk_tree_view_column_new_with_attributes
 
717
      (_("Form"), renderer, "text", 0, NULL);
 
718
    gtk_tree_view_append_column(tree_view, GTK_TREE_VIEW_COLUMN(column));
 
719
    renderer = gtk_cell_renderer_text_new();
 
720
    column = gtk_tree_view_column_new_with_attributes
 
721
      (_("Description"), renderer, "text", 1, NULL);
 
722
    gtk_tree_view_append_column(tree_view, GTK_TREE_VIEW_COLUMN(column));
 
723
    ti_dialog->txf_category_view = GTK_WIDGET(tree_view);
 
724
 
 
725
    selection = gtk_tree_view_get_selection(tree_view);
 
726
    g_signal_connect (G_OBJECT (selection), "changed",
 
727
                      G_CALLBACK  (txf_code_select_row_cb), ti_dialog);
 
728
 
 
729
    label = glade_xml_get_widget(xml, "txf_category_label");
 
730
    gtk_label_set_mnemonic_widget(GTK_LABEL(label), GTK_WIDGET(tree_view));
 
731
 
 
732
    button = glade_xml_get_widget (xml, "current_account_button");
 
733
    ti_dialog->current_account_button = button;
 
734
 
 
735
    button = glade_xml_get_widget (xml, "parent_account_button");
 
736
    ti_dialog->parent_account_button = button;
 
737
 
 
738
    g_signal_connect (G_OBJECT (button), "toggled",
 
739
                      G_CALLBACK  (current_account_toggled_cb),
 
740
                        ti_dialog);
 
741
  }
 
742
 
 
743
  /* account tree */
 
744
  {
 
745
    GtkWidget *income_radio, *expense_radio, *box;
 
746
 
 
747
    box = glade_xml_get_widget (xml, "account_scroll");
 
748
    tree_view = gnc_tree_view_account_new (FALSE);
 
749
    gnc_tree_view_account_set_filter (GNC_TREE_VIEW_ACCOUNT(tree_view), 
 
750
                                      gnc_tax_info_dialog_account_filter_func,
 
751
                                      ti_dialog, NULL);
 
752
    ti_dialog->account_treeview = GTK_WIDGET(tree_view);
 
753
 
 
754
    selection = gtk_tree_view_get_selection (tree_view);
 
755
    gtk_tree_selection_set_mode (selection, GTK_SELECTION_EXTENDED);
 
756
    g_signal_connect (G_OBJECT (selection), "changed",
 
757
                      G_CALLBACK (gnc_tax_info_account_changed_cb),
 
758
                      ti_dialog);
 
759
 
 
760
    gtk_widget_show (ti_dialog->account_treeview);
 
761
    gtk_container_add (GTK_CONTAINER (box), ti_dialog->account_treeview);
 
762
 
 
763
    label = glade_xml_get_widget(xml, "accounts_label");
 
764
    gtk_label_set_mnemonic_widget(GTK_LABEL(label), GTK_WIDGET(tree_view));
 
765
 
 
766
    income_radio = glade_xml_get_widget (xml, "income_radio");
 
767
    expense_radio = glade_xml_get_widget (xml, "expense_radio");
 
768
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(expense_radio), TRUE);
 
769
    g_signal_connect (G_OBJECT (income_radio), "toggled",
 
770
                      G_CALLBACK  (gnc_tax_info_income_cb),
 
771
                        ti_dialog);
 
772
    //   gtk_button_clicked (GtkButton *button);
 
773
 }
 
774
 
 
775
  /* select subaccounts button */
 
776
  {
 
777
    GtkWidget *button;
 
778
 
 
779
    button = glade_xml_get_widget (xml, "select_subaccounts_button");
 
780
    ti_dialog->select_button = button;
 
781
 
 
782
    g_signal_connect (G_OBJECT (button), "clicked",
 
783
                      G_CALLBACK  (select_subaccounts_clicked),
 
784
                      ti_dialog);
 
785
    g_signal_connect (G_OBJECT (ti_dialog->account_treeview), "cursor_changed",
 
786
                      G_CALLBACK  (cursor_changed_cb),
 
787
                      ti_dialog);
 
788
  }
 
789
 
 
790
  tax_info_show_income_accounts (ti_dialog, FALSE);
 
791
  gnc_tax_info_update_accounts (ti_dialog);
 
792
  clear_gui (ti_dialog);
 
793
  gnc_tax_info_set_changed (ti_dialog, FALSE);
 
794
 
 
795
  gnc_restore_window_size(GCONF_SECTION, GTK_WINDOW(ti_dialog->dialog));
 
796
 
 
797
  if (gnc_gconf_get_bool(GCONF_GENERAL, KEY_SAVE_GEOMETRY, NULL)) {
 
798
    GtkWidget *paned = glade_xml_get_widget(xml, "paned");
 
799
    gint position = gnc_gconf_get_int(GCONF_SECTION, PANED_POSITION, NULL);
 
800
    gtk_paned_set_position(GTK_PANED(paned), position);
 
801
  }
 
802
}
 
803
 
 
804
static void
 
805
close_handler (gpointer user_data)
 
806
{
 
807
  TaxInfoDialog *ti_dialog = user_data;
 
808
 
 
809
  if (gnc_gconf_get_bool(GCONF_GENERAL, KEY_SAVE_GEOMETRY, NULL)) {
 
810
    GtkWidget *paned = gnc_glade_lookup_widget(ti_dialog->dialog, "paned");
 
811
    gnc_gconf_set_int(GCONF_SECTION, PANED_POSITION,
 
812
                      gtk_paned_get_position(GTK_PANED(paned)), NULL);
 
813
  }
 
814
 
 
815
  gnc_save_window_size(GCONF_SECTION, GTK_WINDOW(ti_dialog->dialog));
 
816
  gtk_widget_destroy (ti_dialog->dialog);
 
817
}
 
818
 
 
819
static void
 
820
refresh_handler (GHashTable *changes, gpointer user_data)
 
821
{
 
822
  TaxInfoDialog *ti_dialog = user_data;
 
823
 
 
824
/*  gnc_account_tree_refresh (GNC_ACCOUNT_TREE (ti_dialog->account_tree));*/
 
825
 
 
826
  gnc_tax_info_update_accounts (ti_dialog);
 
827
}
 
828
 
 
829
/********************************************************************\
 
830
 * gnc_tax_info_dialog                                              *
 
831
 *   opens up a window to set account tax information               *
 
832
 *                                                                  * 
 
833
 * Args:   parent  - the parent of the window to be created         *
 
834
 * Return: nothing                                                  *
 
835
\********************************************************************/
 
836
void
 
837
gnc_tax_info_dialog (GtkWidget * parent)
 
838
{
 
839
  TaxInfoDialog *ti_dialog;
 
840
  gint component_id;
 
841
 
 
842
  ti_dialog = g_new0 (TaxInfoDialog, 1);
 
843
 
 
844
  gnc_tax_info_dialog_create (parent, ti_dialog);
 
845
 
 
846
  component_id = gnc_register_gui_component (DIALOG_TAX_INFO_CM_CLASS,
 
847
                                             refresh_handler, close_handler,
 
848
                                             ti_dialog);
 
849
 
 
850
  gnc_gui_component_watch_entity_type (component_id,
 
851
                                       GNC_ID_ACCOUNT,
 
852
                                       QOF_EVENT_MODIFY | QOF_EVENT_DESTROY);
 
853
 
 
854
  gtk_widget_grab_focus (ti_dialog->account_treeview);
 
855
 
 
856
  gtk_widget_show (ti_dialog->dialog);
 
857
}