/
gnc-tree-control-split-reg.c
2186 lines (1758 loc) · 71.3 KB
/
gnc-tree-control-split-reg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************\
* gnc-tree-control-split-reg.c -- GtkTreeView implementation *
* to display registers in a GtkTreeView. *
* *
* Copyright (C) 2006-2007 Chris Shoemaker <c.shoemaker@cox.net> *
* Copyright (C) 2012 Robert Fewell *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
* *
\********************************************************************/
#include "config.h"
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <stdlib.h>
#include <string.h>
#include "gnc-tree-control-split-reg.h"
#include "gnc-tree-model-split-reg.h"
#include "gnc-tree-util-split-reg.h"
#include "gnc-tree-view-split-reg.h"
#include "gnc-component-manager.h"
#include "gnc-ui.h"
#include "gnc-prefs.h"
#include "gnc-gdate-utils.h"
#include "gnome-utils/gnc-warnings.h"
#include "dialog-utils.h"
#include "dialog-dup-trans.h"
#include "dialog-account.h"
#include "Transaction.h"
#include "engine-helpers.h"
#include "gnc-event.h"
#include "Scrub.h"
/** Static Globals *******************************************************/
static QofLogModule log_module = GNC_MOD_LEDGER;
/*****************************************************************************/
/*****************************************************************************/
/* Read only dialog */
static gboolean
gtc_sr_is_trans_readonly_and_warn (GncTreeViewSplitReg *view, Transaction *trans)
{
GncTreeModelSplitReg *model;
GtkWidget *window;
GtkWidget *dialog;
const gchar *reason;
const gchar *title = _("Cannot modify or delete this transaction.");
const gchar *message_reason =
_("This transaction is marked read-only with the comment: '%s'");
if (!trans) return FALSE;
window = gnc_tree_view_split_reg_get_parent (view);
model = gnc_tree_view_split_reg_get_model_from_view (view);
if (xaccTransIsReadonlyByPostedDate (trans))
{
dialog = gtk_message_dialog_new (GTK_WINDOW (window),
0,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"%s", title);
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
"%s", _("The date of this transaction is older than the \"Read-Only Threshold\" set for this book. "
"This setting can be changed in File -> Properties -> Accounts."));
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
return TRUE;
}
reason = xaccTransGetReadOnly (trans);
if (reason)
{
dialog = gtk_message_dialog_new (GTK_WINDOW (window),
0,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"%s", title);
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
message_reason, reason);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
return TRUE;
}
if (gnc_tree_model_split_reg_get_read_only (model, trans))
{
dialog = gtk_message_dialog_new (GTK_WINDOW (window),
0,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"%s", title);
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
"%s", _("You can not change this transaction, the Book or Register is set to Read Only."));
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
return TRUE;
}
return FALSE;
}
/* Transaction is being edited dialog */
#define gtc_sr_trans_open_and_warn gnc_tree_control_split_reg_trans_open_and_warn
gboolean
gnc_tree_control_split_reg_trans_open_and_warn (GncTreeViewSplitReg *view, Transaction *trans)
{
Transaction *dirty_trans;
GtkWidget *window;
GtkWidget *dialog;
gint response;
const char *title = _("Save Transaction before proceeding?");
const char *message =
_("The current transaction has been changed. Would you like to "
"record the changes before proceeding, or cancel?");
window = gnc_tree_view_split_reg_get_parent (view);
dirty_trans = gnc_tree_view_split_reg_get_dirty_trans (view);
if (trans == dirty_trans)
{
dialog = gtk_message_dialog_new (GTK_WINDOW (window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION,
GTK_BUTTONS_CANCEL,
"%s", title);
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
"%s", message);
gtk_dialog_add_button (GTK_DIALOG (dialog),
_("_Record"), GTK_RESPONSE_ACCEPT);
response = gnc_dialog_run (GTK_DIALOG (dialog), GNC_PREF_WARN_REG_TRANS_MOD);
gtk_widget_destroy (dialog);
if (response != GTK_RESPONSE_ACCEPT)
return TRUE;
xaccTransCommitEdit (trans);
gnc_tree_view_split_reg_set_dirty_trans (view, NULL);
return FALSE;
}
else
return FALSE;
}
#define gtc_sr_trans_test_for_edit gnc_tree_control_split_reg_trans_test_for_edit
gboolean
gtc_sr_trans_test_for_edit (GncTreeViewSplitReg *view, Transaction *trans)
{
GtkWidget *window;
Transaction *dirty_trans;
/* Make sure we have stopped editing */
gnc_tree_view_split_reg_finish_edit (view);
window = gnc_tree_view_split_reg_get_parent (view);
/* Get dirty_trans */
dirty_trans = gnc_tree_view_split_reg_get_dirty_trans (view);
/* We are being edited in a different register */
if (xaccTransIsOpen (trans) && (dirty_trans != trans))
{
gnc_error_dialog (window, "%s",
_("This transaction is being edited in a different register."));
return TRUE;
}
return FALSE;
}
/*****************************************************************************/
/*****************************************************************************/
gboolean
gnc_tree_control_split_reg_balance_trans (GncTreeViewSplitReg *view, Transaction *trans)
{
GncTreeModelSplitReg *model;
GtkWidget *window;
int choice;
int default_value;
Account *default_account;
Account *other_account;
Account *root;
GList *radio_list = NULL;
const char *title = _("Rebalance Transaction");
const char *message = _("The current transaction is not balanced.");
Split *split;
Split *other_split;
gboolean two_accounts;
gboolean multi_currency;
if (xaccTransIsBalanced (trans))
return FALSE;
window = gnc_tree_view_split_reg_get_parent (view);
model = gnc_tree_view_split_reg_get_model_from_view (view);
if (xaccTransUseTradingAccounts (trans))
{
MonetaryList *imbal_list;
gnc_monetary *imbal_mon;
imbal_list = xaccTransGetImbalance (trans);
/* See if the imbalance is only in the transaction's currency */
if (!imbal_list)
/* Value imbalance, but not commodity imbalance. This shouldn't
be something that scrubbing can cause to happen. Perhaps someone
entered invalid splits. */
multi_currency = TRUE;
else
{
imbal_mon = imbal_list->data;
if (!imbal_list->next &&
gnc_commodity_equiv(gnc_monetary_commodity(*imbal_mon),
xaccTransGetCurrency(trans)))
multi_currency = FALSE;
else
multi_currency = TRUE;
}
/* We're done with the imbalance list, the real work will be done
by xaccTransScrubImbalance which will get it again. */
gnc_monetary_list_free(imbal_list);
}
else
multi_currency = FALSE;
split = xaccTransGetSplit (trans, 0);
other_split = xaccSplitGetOtherSplit (split);
if (other_split == NULL)
{
/* Attempt to handle the inverted many-to-one mapping */
split = xaccTransGetSplit (trans, 1);
if (split) other_split = xaccSplitGetOtherSplit (split);
else split = xaccTransGetSplit (trans, 0);
}
if (other_split == NULL || multi_currency)
{
two_accounts = FALSE;
other_account = NULL;
}
else
{
two_accounts = TRUE;
other_account = xaccSplitGetAccount (other_split);
}
default_account = gnc_tree_model_split_reg_get_anchor (model);
/* If the two pointers are the same, the account from other_split
* is actually the default account. We must make other_account
* the account from split instead. */
if (default_account == other_account)
other_account = xaccSplitGetAccount (split);
/* If the two pointers are still the same, we have two splits, but
* they both refer to the same account. While non-sensical, we don't
* object. */
if (default_account == other_account)
two_accounts = FALSE;
radio_list = g_list_append (radio_list,
_("Balance it _manually"));
radio_list = g_list_append (radio_list,
_("Let GnuCash _add an adjusting split"));
if (model->type < NUM_SINGLE_REGISTER_TYPES2 && !multi_currency)
{
radio_list = g_list_append (radio_list,
_("Adjust current account _split total"));
default_value = 2;
if (two_accounts)
{
radio_list = g_list_append (radio_list,
_("Adjust _other account split total"));
default_value = 3;
}
}
else
default_value = 0;
choice = gnc_choose_radio_option_dialog
(window,
title,
message,
_("_Rebalance"),
default_value,
radio_list);
g_list_free (radio_list);
root = gnc_account_get_root(default_account);
switch (choice)
{
default:
case 0:
return TRUE;
break;
case 1:
xaccTransScrubImbalance (trans, root, NULL);
break;
case 2:
xaccTransScrubImbalance (trans, root, default_account);
break;
case 3:
xaccTransScrubImbalance (trans, root, other_account);
break;
}
return FALSE;
}
/* Cancel the edit and Rollback */
void
gnc_tree_control_split_reg_cancel_edit (GncTreeViewSplitReg *view, gboolean reg_closing)
{
/* Make sure we have stopped editing */
gnc_tree_view_split_reg_finish_edit (view);
gnc_tree_view_split_reg_cancel_edit (view, reg_closing);
}
/* Amend the Exchange Rate of the transaction */
void
gnc_tree_control_split_reg_exchange_rate (GncTreeViewSplitReg *view)
{
GncTreeModelSplitReg *model;
GtkWidget *window;
Account *anchor;
Transaction *trans;
Split *split = NULL;
Split *osplit = NULL;
gnc_numeric value;
gboolean expanded;
gint depth;
gint num_splits;
const char *message;
gnc_commodity *txn_com;
model = gnc_tree_view_split_reg_get_model_from_view (view);
trans = gnc_tree_view_split_reg_get_current_trans (view);
expanded = gnc_tree_view_split_reg_trans_expanded (view, NULL);
depth = gnc_tree_view_reg_get_selected_row_depth (view);
num_splits = xaccTransCountSplits (trans);
anchor = gnc_tree_model_split_reg_get_anchor (model);
txn_com = xaccTransGetCurrency (trans);
if (trans == NULL)
return;
/* See if we were asked to change a blank trans. */
if (trans == gnc_tree_control_split_reg_get_blank_trans (view))
return;
/* Test for read only */
if (gtc_sr_is_trans_readonly_and_warn (view, trans))
return;
/* See if we are being edited in another register */
if (gtc_sr_trans_test_for_edit (view, trans))
return;
/* Make sure we ask to commit any changes before we proceed */
if (gtc_sr_trans_open_and_warn (view, trans))
return;
if (num_splits < 2)
return;
window = gnc_tree_view_split_reg_get_parent (view);
/* Make sure we NEED this for this type of register */
if (!gnc_tree_util_split_reg_has_rate (view))
{
message = _("This register does not support editing exchange rates.");
gnc_error_dialog(window, "%s", message);
return;
}
/* If the anchor commodity is not a currency, cancel */
if (anchor && !gnc_commodity_is_currency (xaccAccountGetCommodity (anchor)))
{
message = _("This register does not support editing exchange rates.");
gnc_error_dialog (window, "%s", message);
return;
}
/* If we're not expanded AND number of splits greater than two, nothing to do */
if ((gnc_tree_util_split_reg_is_multi (xaccTransGetSplit (trans, 0))) && !expanded)
{
message = _("You need to expand the transaction in order to modify its "
"exchange rates.");
gnc_error_dialog (window, "%s", message);
return;
}
if (!gnc_tree_util_split_reg_is_multi (xaccTransGetSplit (trans, 0)) && anchor != NULL && !expanded)
{
split = gnc_tree_control_split_reg_get_current_trans_split (view);
if (xaccAccountGetType (xaccSplitGetAccount (split)) == ACCT_TYPE_TRADING) // trading split
return;
osplit = xaccSplitGetOtherSplit (split);
value = xaccSplitGetValue (split);
gnc_tree_view_split_reg_set_dirty_trans (view, trans);
xaccTransBeginEdit (trans);
if (txn_com == xaccAccountGetCommodity (xaccSplitGetAccount(split)))
gnc_tree_util_split_reg_set_value_for (view, trans, osplit, gnc_numeric_neg (value), TRUE);
else
gnc_tree_util_split_reg_set_value_for (view, trans, split, value, TRUE);
xaccTransCommitEdit (trans);
gnc_tree_view_split_reg_set_dirty_trans (view, NULL);
}
if (num_splits > 1 && expanded && depth == 3)
{
split = gnc_tree_view_split_reg_get_current_split (view);
if (xaccAccountGetType (xaccSplitGetAccount (split)) == ACCT_TYPE_TRADING) // trading split
return;
value = xaccSplitGetValue (split);
if (txn_com == xaccAccountGetCommodity (xaccSplitGetAccount(split)))
{
message = _("The two currencies involved equal each other.");
gnc_error_dialog (window, "%s", message);
return;
}
else
{
gnc_tree_view_split_reg_set_dirty_trans (view, trans);
xaccTransBeginEdit (trans);
gnc_tree_util_split_reg_set_value_for (view, trans, split, value, TRUE);
xaccTransCommitEdit (trans);
gnc_tree_view_split_reg_set_dirty_trans (view, NULL);
}
}
}
/* Void current transaction */
void
gnc_tree_control_split_reg_void_current_trans (GncTreeViewSplitReg *view, const char *reason)
{
Transaction *trans;
Split *blank_split;
Split *split;
if (!view) return;
blank_split = gnc_tree_control_split_reg_get_blank_split (view);
/* get the current split */
split = gnc_tree_view_split_reg_get_current_split (view);
if (split == NULL)
return;
/* Bail if trying to void the blank split. */
if (split == blank_split)
return;
/* already voided. */
if (xaccSplitGetReconcile (split) == VREC)
return;
trans = xaccSplitGetParent (split);
if (trans == NULL)
return;
/* See if we were asked to change a blank trans. */
if (trans == gnc_tree_control_split_reg_get_blank_trans (view))
return;
/* Test for read only */
if (gtc_sr_is_trans_readonly_and_warn (view, trans))
return;
/* See if we are being edited in another register */
if (gtc_sr_trans_test_for_edit (view, trans))
return;
/* Make sure we ask to commit any changes before we proceed */
if (gtc_sr_trans_open_and_warn (view, trans))
return;
gnc_tree_view_split_reg_set_dirty_trans (view, trans);
xaccTransVoid (trans, reason);
if (xaccTransIsOpen (trans))
{
PERR("We should not be voiding an open transaction.");
xaccTransCommitEdit (trans);
}
gnc_tree_view_split_reg_set_dirty_trans (view, NULL);
}
/* Unvoid current transaction */
void
gnc_tree_control_split_reg_unvoid_current_trans (GncTreeViewSplitReg *view)
{
Transaction *trans;
Split *blank_split;
Split *split;
if (!view) return;
blank_split = gnc_tree_control_split_reg_get_blank_split (view);
/* get the current split based on cursor position */
split = gnc_tree_view_split_reg_get_current_split (view);
if (split == NULL)
return;
/* Bail if trying to unvoid the blank split. */
if (split == blank_split)
return;
/* not voided. */
if (xaccSplitGetReconcile (split) != VREC)
return;
trans = xaccSplitGetParent (split);
if (trans == NULL)
return;
/* See if we were asked to change a blank trans. */
if (trans == gnc_tree_control_split_reg_get_blank_trans (view))
return;
gnc_tree_view_split_reg_set_dirty_trans (view, trans);
xaccTransUnvoid (trans);
gnc_tree_view_split_reg_set_dirty_trans (view, NULL);
}
/* Jump to the Blank transaction */
gboolean
gnc_tree_control_split_reg_jump_to_blank (GncTreeViewSplitReg *view)
{
GncTreeModelSplitReg *model;
GtkTreePath *mpath, *spath;
Transaction *btrans;
model = gnc_tree_view_split_reg_get_model_from_view (view);
btrans = gnc_tree_model_split_get_blank_trans (model);
model->current_trans = btrans;
if (!gnc_tree_model_split_reg_trans_is_in_view (model, btrans))
g_signal_emit_by_name (model, "refresh_trans");
else
{
mpath = gnc_tree_model_split_reg_get_path_to_split_and_trans (model, NULL, btrans);
spath = gnc_tree_view_split_reg_get_sort_path_from_model_path (view, mpath);
/* Set cursor to new spath */
gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), spath, NULL, FALSE);
gtk_tree_path_free (spath);
gtk_tree_path_free (mpath);
/* scroll when view idle */
g_idle_add ((GSourceFunc)gnc_tree_view_split_reg_scroll_to_cell, view );
}
return FALSE;
}
/* Jump to transaction or split */
void
gnc_tree_control_split_reg_jump_to (GncTreeViewSplitReg *view, Transaction *trans, Split *split, gboolean amount)
{
GncTreeModelSplitReg *model;
GtkTreePath *mpath, *spath;
model = gnc_tree_view_split_reg_get_model_from_view (view);
if (split)
trans = NULL;
mpath = gnc_tree_model_split_reg_get_path_to_split_and_trans (model, split, trans);
spath = gnc_tree_view_split_reg_get_sort_path_from_model_path (view, mpath);
if (split)
gnc_tree_view_split_reg_expand_trans (view, xaccSplitGetParent (split));
/* Set cursor to new spath, if amount, cursor is set to correct column ready for editing */
if (amount)
{
GtkCellRenderer *cr0;
GList *renderers;
GList *columns;
GList *column;
gint i;
columns = gtk_tree_view_get_columns (GTK_TREE_VIEW (view));
for (column = columns, i = 1; column; column = g_list_next (column), i++)
{
GtkTreeViewColumn *tvc;
ViewCol viewcol;
tvc = column->data;
// Get the first renderer, it has the view-column value.
renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (tvc));
cr0 = g_list_nth_data (renderers, 0);
g_list_free (renderers);
viewcol = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cr0), "view_column"));
if (viewcol == COL_DEBIT && gnc_numeric_positive_p (xaccSplitGetAmount (split)))
gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), spath, tvc, TRUE);
if (viewcol == COL_CREDIT && gnc_numeric_negative_p (xaccSplitGetAmount (split)))
gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), spath, tvc, TRUE);
}
g_list_free (columns);
}
else
gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), spath, NULL, FALSE);
/* Scroll to cell, mid view */
gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (view), spath, NULL, TRUE, 0.5, 0.0);
gtk_tree_path_free (spath);
gtk_tree_path_free (mpath);
}
/* Returns the Blank Transaction */
Transaction *
gnc_tree_control_split_reg_get_blank_trans (GncTreeViewSplitReg *view)
{
GncTreeModelSplitReg *model;
model = gnc_tree_view_split_reg_get_model_from_view (view);
return gnc_tree_model_split_get_blank_trans (model);
}
/* Return the Split for the current Transaction */
Split *
gnc_tree_control_split_reg_get_current_trans_split (GncTreeViewSplitReg *view)
{
GncTreeModelSplitReg *model;
GtkTreePath *mpath;
GtkTreeIter m_iter;
Split *split = NULL;
Transaction *trans = NULL;
Account *anchor;
gboolean is_trow1, is_trow2, is_split, is_blank;
model = gnc_tree_view_split_reg_get_model_from_view (view);
mpath = gnc_tree_view_split_reg_get_current_path (view);
gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &m_iter, mpath);
gnc_tree_model_split_reg_get_split_and_trans (
GNC_TREE_MODEL_SPLIT_REG (model), &m_iter, &is_trow1, &is_trow2, &is_split, &is_blank, &split, &trans);
anchor = gnc_tree_model_split_reg_get_anchor (model);
split = gnc_tree_model_split_reg_trans_get_split_equal_to_ancestor (trans, anchor);
gtk_tree_path_free (mpath);
return split;
}
/* Returns the Blank Split */
Split *
gnc_tree_control_split_reg_get_blank_split (GncTreeViewSplitReg *view)
{
GncTreeModelSplitReg *model;
model = gnc_tree_view_split_reg_get_model_from_view (view);
return gnc_tree_model_split_get_blank_split (model);
}
/* Move to the relative transaction */
void
gnc_tree_control_split_reg_goto_rel_trans_row (GncTreeViewSplitReg *view, gint relative)
{
GncTreeModelSplitReg *model;
GtkTreePath *mpath, *spath;
GtkTreePath *new_mpath, *new_spath;
gint *indices, sort_direction;
gchar *sstring;
ENTER("Move relative, view is %p, relative is %d", view, relative);
//FIXME Do we need to do some checks on relative maybe -1,0,1 ??
model = gnc_tree_view_split_reg_get_model_from_view (view);
mpath = gnc_tree_view_split_reg_get_current_path (view);
spath = gnc_tree_view_split_reg_get_sort_path_from_model_path (view, mpath);
indices = gtk_tree_path_get_indices (spath);
if (model->sort_direction == GTK_SORT_DESCENDING)
sort_direction = -1;
else
sort_direction = 1;
new_spath = gtk_tree_path_new_from_indices (indices[0] + (relative * sort_direction), -1);
// if relative == 0 we block all selection changes
gnc_tree_view_split_reg_block_selection (view, TRUE);
gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (view)), spath);
if (relative != 0)
gnc_tree_view_split_reg_block_selection (view, FALSE);
/* Set cursor to new spath */
gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), new_spath, NULL, FALSE);
if (relative == 0)
{
gnc_tree_view_split_reg_block_selection (view, FALSE);
/* Get the new model path we are pointing at */
new_mpath = gnc_tree_view_split_reg_get_model_path_from_sort_path (view, new_spath);
/* As we are not emitting selection change, we need to save the current path ref */
gnc_tree_view_split_reg_set_current_path (view, new_mpath);
gtk_tree_path_free (new_mpath);
}
sstring = gtk_tree_path_to_string (new_spath);
LEAVE("new_spath is %s", sstring);
g_free (sstring);
gtk_tree_path_free (new_spath);
gtk_tree_path_free (mpath);
gtk_tree_path_free (spath);
}
/* Enter the transaction */
void
gnc_tree_control_split_reg_enter (GncTreeViewSplitReg *view)
{
GncTreeModelSplitReg *model;
Transaction *btrans, *ctrans;
gboolean goto_blank = FALSE;
gboolean next_trans = TRUE;
model = gnc_tree_view_split_reg_get_model_from_view (view);
goto_blank = gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL_REGISTER,
GNC_PREF_ENTER_MOVES_TO_END);
ENTER("view=%p, goto_blank = %s", view, goto_blank ? "TRUE" : "FALSE");
btrans = gnc_tree_model_split_get_blank_trans (model);
ctrans = gnc_tree_view_split_reg_get_current_trans (view);
/* Are we on the blank transaction */
if (btrans == ctrans)
next_trans = FALSE;
/* First record the transaction */
if (gnc_tree_view_split_reg_enter (view))
{
/* Now move. */
if (goto_blank)
gnc_tree_control_split_reg_jump_to_blank (view);
else if (next_trans)
gnc_tree_control_split_reg_goto_rel_trans_row (view, 1);
}
LEAVE(" ");
}
/* Reinit the transaction */
void
gnc_tree_control_split_reg_reinit (GncTreeViewSplitReg *view, gpointer data)
{
Transaction *trans;
Split *split;
GtkWidget *dialog, *window;
gint response;
const gchar *warning;
const char *title = _("Remove the splits from this transaction?");
const char *recn_warn = _("This transaction contains reconciled splits. "
"Modifying it is not a good idea because that will "
"cause your reconciled balance to be off.");
trans = gnc_tree_view_split_reg_get_current_trans (view);
if (trans == NULL)
return;
/* See if we were asked to change a blank trans. */
if (trans == gnc_tree_control_split_reg_get_blank_trans (view))
return;
/* Test for read only */
if (gtc_sr_is_trans_readonly_and_warn (view, trans))
return;
/* See if we are being edited in another register */
if (gtc_sr_trans_test_for_edit (view, trans))
return;
/* Make sure we ask to commit any changes before we proceed */
if (gtc_sr_trans_open_and_warn (view, trans))
return;
window = gnc_tree_view_split_reg_get_parent (view);
dialog = gtk_message_dialog_new (GTK_WINDOW (window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_NONE,
"%s", title);
if (xaccTransHasReconciledSplits (trans))
{
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
"%s", recn_warn);
warning = GNC_PREF_WARN_REG_SPLIT_DEL_ALL_RECD;
}
else
{
warning = GNC_PREF_WARN_REG_SPLIT_DEL_ALL;
}
gtk_dialog_add_button (GTK_DIALOG (dialog),
_("_Cancel"), GTK_RESPONSE_CANCEL);
gnc_gtk_dialog_add_button(dialog, _("_Remove Splits"),
"edit-delete", GTK_RESPONSE_ACCEPT);
response = gnc_dialog_run (GTK_DIALOG(dialog), warning);
gtk_widget_destroy (dialog);
if (response != GTK_RESPONSE_ACCEPT)
return;
gnc_tree_view_split_reg_reinit_trans (view);
}
/* Delete the currently selected item */
void
gnc_tree_control_split_reg_delete (GncTreeViewSplitReg *view, gpointer data)
{
GncTreeModelSplitReg *model;
Account *anchor;
RowDepth depth;
Transaction *trans;
Split *split;
GtkWidget *dialog, *window;
gint response;
const gchar *warning;
/* get the current split based on cursor position */
split = gnc_tree_view_split_reg_get_current_split (view);
if (split == NULL)
{
split = gnc_tree_control_split_reg_get_current_trans_split (view);
if (split == NULL)
{
LEAVE("split is NULL");
return;
}
}
model = gnc_tree_view_split_reg_get_model_from_view (view);
anchor = gnc_tree_model_split_reg_get_anchor (model);
trans = xaccSplitGetParent (split);
if (trans == NULL)
return;
/* Test for read only */
if (gtc_sr_is_trans_readonly_and_warn (view, trans))
return;
/* See if we are being edited in another register */
if (gtc_sr_trans_test_for_edit (view, trans))
return;
depth = gnc_tree_view_reg_get_selected_row_depth (view);
/* Deleting the blank split just cancels */
{
Split *blank_split = gnc_tree_control_split_reg_get_blank_split (view);
if (split == blank_split)
return;
}
/* Deleting the blank trans just cancels */
{
Transaction *blank_trans = gnc_tree_control_split_reg_get_blank_trans (view);
if (trans == blank_trans)
return;
}
window = gnc_tree_view_split_reg_get_parent (view);
/* On a split cursor, just delete the one split. */
if (depth == SPLIT3)
{
const char *format = _("Delete the split '%s' from the transaction '%s'?");
const char *recn_warn = _("You would be deleting a reconciled split! "
"This is not a good idea as it will cause your "
"reconciled balance to be off.");
const char *anchor_error = _("You cannot delete this split.");
const char *anchor_split = _("This is the split anchoring this transaction "
"to the register. You may not delete it from "
"this register window. You may delete the "
"entire transaction from this window, or you "
"may navigate to a register that shows "
"another side of this same transaction and "
"delete the split from that register.");
char *buf = NULL;
const char *memo;
const char *desc;
char recn;
if ((split == gnc_tree_control_split_reg_get_current_trans_split (view)) ||
(split == gnc_tree_model_split_reg_trans_get_split_equal_to_ancestor (trans, anchor)))
{
dialog = gtk_message_dialog_new (GTK_WINDOW (window),
GTK_DIALOG_MODAL
| GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"%s", anchor_error);
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
"%s", anchor_split);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
return;
}
memo = xaccSplitGetMemo (split);
memo = (memo && *memo) ? memo : _("(no memo)");
desc = xaccTransGetDescription (trans);
desc = (desc && *desc) ? desc : _("(no description)");