/
split-register.c
2871 lines (2366 loc) · 88.6 KB
/
split-register.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
/********************************************************************\
* 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 *
* *
\********************************************************************/
/*
* split-register.c
* author Copyright (c) 1998-2000 Linas Vepstas <linas@linas.org>
* author Copyright (c) 2000-2001 Dave Peticolas <dave@krondo.com>
*/
#include "config.h"
#include <glib.h>
#include <glib/gi18n.h>
#include <libguile.h>
#include <gnc-gdate-utils.h>
#include "combocell.h"
#include "datecell.h"
#include "dialog-utils.h"
#include "gnc-component-manager.h"
#include "gnc-gconf-utils.h"
#include "split-register-p.h"
#include "gnc-ledger-display.h"
#include "gnc-ui.h"
#include "guile-util.h"
#include "numcell.h"
#include "pricecell.h"
#include "quickfillcell.h"
#include "recncell.h"
#include "split-register.h"
#include "split-register-control.h"
#include "split-register-layout.h"
#include "split-register-model.h"
#include "split-register-model-save.h"
#include "table-allgui.h"
#include "dialog-account.h"
#include "dialog-dup-trans.h"
#include "engine-helpers.h"
#include "qofbookslots.h"
/** static variables ******************************************************/
/* This static indicates the debugging module that this .o belongs to. */
static QofLogModule log_module = GNC_MOD_LEDGER;
/* The copied split or transaction, if any */
static CursorClass copied_class = CURSOR_CLASS_NONE;
static SCM copied_item = SCM_UNDEFINED;
static GncGUID copied_leader_guid;
/** static prototypes *****************************************************/
static gboolean gnc_split_register_save_to_scm (SplitRegister *reg,
SCM trans_scm, SCM split_scm,
gboolean use_cut_semantics);
static gboolean gnc_split_register_auto_calc (SplitRegister *reg,
Split *split);
/** implementations *******************************************************/
/* Uses the scheme split copying routines */
static void
gnc_copy_split_onto_split(Split *from, Split *to, gboolean use_cut_semantics)
{
SCM split_scm;
if ((from == NULL) || (to == NULL))
return;
split_scm = gnc_copy_split(from, use_cut_semantics);
if (split_scm == SCM_UNDEFINED)
return;
gnc_copy_split_scm_onto_split(split_scm, to, gnc_get_current_book ());
}
/* Uses the scheme transaction copying routines */
void
gnc_copy_trans_onto_trans(Transaction *from, Transaction *to,
gboolean use_cut_semantics,
gboolean do_commit)
{
SCM trans_scm;
if ((from == NULL) || (to == NULL))
return;
trans_scm = gnc_copy_trans(from, use_cut_semantics);
if (trans_scm == SCM_UNDEFINED)
return;
gnc_copy_trans_scm_onto_trans(trans_scm, to, do_commit,
gnc_get_current_book ());
}
static int
gnc_split_get_value_denom (Split *split)
{
gnc_commodity *currency;
int denom;
currency = xaccTransGetCurrency (xaccSplitGetParent (split));
denom = gnc_commodity_get_fraction (currency);
if (denom == 0)
{
gnc_commodity *commodity = gnc_default_currency ();
denom = gnc_commodity_get_fraction (commodity);
if (denom == 0)
denom = 100;
}
return denom;
}
static int
gnc_split_get_amount_denom (Split *split)
{
int denom;
denom = xaccAccountGetCommoditySCU (xaccSplitGetAccount (split));
if (denom == 0)
{
gnc_commodity *commodity = gnc_default_currency ();
denom = gnc_commodity_get_fraction (commodity);
if (denom == 0)
denom = 100;
}
return denom;
}
/* returns TRUE if begin_edit was aborted */
gboolean
gnc_split_register_begin_edit_or_warn(SRInfo *info, Transaction *trans)
{
ENTER("info=%p, trans=%p", info, trans);
if (!xaccTransIsOpen(trans))
{
xaccTransBeginEdit(trans);
/* This is now the pending transaction */
info->pending_trans_guid = *xaccTransGetGUID(trans);
LEAVE("opened and marked pending");
return FALSE;
}
else
{
Split *blank_split = xaccSplitLookup (&info->blank_split_guid,
gnc_get_current_book ());
Transaction *blank_trans = xaccSplitGetParent (blank_split);
if (trans == blank_trans)
{
/* This is a brand-new transaction. It is already
* open, so just mark it as pending. */
info->pending_trans_guid = *xaccTransGetGUID(trans);
LEAVE("already open, now pending.");
return FALSE;
}
else
{
GtkWidget *parent = NULL;
if (info->get_parent)
parent = info->get_parent(info->user_data);
gnc_error_dialog(parent, "%s", _("This transaction is already being edited in another register. Please finish editing it there first."));
LEAVE("already editing");
return TRUE;
}
}
LEAVE(" ");
return FALSE; /* to satisfy static code analysis */
}
void
gnc_split_register_expand_current_trans (SplitRegister *reg, gboolean expand)
{
SRInfo *info = gnc_split_register_get_info (reg);
VirtualLocation virt_loc;
if (!reg)
return;
if (reg->style == REG_STYLE_AUTO_LEDGER ||
reg->style == REG_STYLE_JOURNAL)
return;
/* ok, so I just wanted an excuse to use exclusive-or */
if (!(expand ^ info->trans_expanded))
return;
if (!expand)
{
virt_loc = reg->table->current_cursor_loc;
gnc_split_register_get_trans_split (reg, virt_loc.vcell_loc,
&virt_loc.vcell_loc);
if (gnc_table_find_close_valid_cell (reg->table, &virt_loc, FALSE))
gnc_table_move_cursor_gui (reg->table, virt_loc);
else
{
PERR ("Can't find place to go!");
return;
}
}
info->trans_expanded = expand;
gnc_table_set_virt_cell_cursor (reg->table,
reg->table->current_cursor_loc.vcell_loc,
gnc_split_register_get_active_cursor (reg));
gnc_split_register_set_trans_visible(
reg, reg->table->current_cursor_loc.vcell_loc, expand, FALSE);
virt_loc = reg->table->current_cursor_loc;
if (!expand || !gnc_table_virtual_loc_valid (reg->table, virt_loc, FALSE))
{
if (gnc_table_find_close_valid_cell (reg->table, &virt_loc, FALSE))
gnc_table_move_cursor_gui (reg->table, virt_loc);
else
{
PERR ("Can't find place to go!");
return;
}
}
gnc_table_refresh_gui (reg->table, TRUE);
if (expand)
gnc_split_register_show_trans (reg,
reg->table->current_cursor_loc.vcell_loc);
}
gboolean
gnc_split_register_current_trans_expanded (SplitRegister *reg)
{
SRInfo *info = gnc_split_register_get_info (reg);
if (!reg)
return FALSE;
if (reg->style == REG_STYLE_AUTO_LEDGER ||
reg->style == REG_STYLE_JOURNAL)
return TRUE;
return info->trans_expanded;
}
Transaction *
gnc_split_register_get_current_trans (SplitRegister *reg)
{
Split *split;
VirtualCellLocation vcell_loc;
if (reg == NULL)
return NULL;
split = gnc_split_register_get_current_split (reg);
if (split != NULL)
return xaccSplitGetParent(split);
/* Split is blank. Assume it is the blank split of a multi-line
* transaction. Go back one row to find a split in the transaction. */
vcell_loc = reg->table->current_cursor_loc.vcell_loc;
vcell_loc.virt_row--;
split = gnc_split_register_get_split (reg, vcell_loc);
return xaccSplitGetParent (split);
}
Split *
gnc_split_register_get_current_split (SplitRegister *reg)
{
if (reg == NULL)
return NULL;
return gnc_split_register_get_split(
reg, reg->table->current_cursor_loc.vcell_loc);
}
Split *
gnc_split_register_get_blank_split (SplitRegister *reg)
{
SRInfo *info = gnc_split_register_get_info (reg);
if (!reg) return NULL;
return xaccSplitLookup (&info->blank_split_guid, gnc_get_current_book ());
}
gboolean
gnc_split_register_get_split_virt_loc (SplitRegister *reg, Split *split,
VirtualCellLocation *vcell_loc)
{
Table *table;
int v_row;
int v_col;
if (!reg || !split) return FALSE;
table = reg->table;
/* go backwards because typically you search for splits at the end
* and because we find split rows before transaction rows. */
for (v_row = table->num_virt_rows - 1; v_row > 0; v_row--)
for (v_col = 0; v_col < table->num_virt_cols; v_col++)
{
VirtualCellLocation vc_loc = { v_row, v_col };
VirtualCell *vcell;
Split *s;
vcell = gnc_table_get_virtual_cell (table, vc_loc);
if (!vcell || !vcell->visible)
continue;
s = xaccSplitLookup (vcell->vcell_data, gnc_get_current_book ());
if (s == split)
{
if (vcell_loc)
*vcell_loc = vc_loc;
return TRUE;
}
}
return FALSE;
}
gboolean
gnc_split_register_get_split_amount_virt_loc (SplitRegister *reg, Split *split,
VirtualLocation *virt_loc)
{
VirtualLocation v_loc;
CursorClass cursor_class;
const char *cell_name;
gnc_numeric value;
if (!gnc_split_register_get_split_virt_loc (reg, split, &v_loc.vcell_loc))
return FALSE;
cursor_class = gnc_split_register_get_cursor_class (reg, v_loc.vcell_loc);
value = xaccSplitGetValue (split);
switch (cursor_class)
{
case CURSOR_CLASS_SPLIT:
case CURSOR_CLASS_TRANS:
cell_name = (gnc_numeric_negative_p (value)) ? CRED_CELL : DEBT_CELL;
break;
default:
return FALSE;
}
if (!gnc_table_get_cell_location (reg->table, cell_name,
v_loc.vcell_loc, &v_loc))
return FALSE;
if (virt_loc == NULL)
return TRUE;
*virt_loc = v_loc;
return TRUE;
}
Split *
gnc_split_register_duplicate_current (SplitRegister *reg)
{
SRInfo *info = gnc_split_register_get_info(reg);
CursorClass cursor_class;
Transaction *trans;
Split *return_split;
Split *trans_split;
Split *blank_split;
gboolean changed;
Split *split;
ENTER("reg=%p", reg);
blank_split = xaccSplitLookup(&info->blank_split_guid,
gnc_get_current_book ());
split = gnc_split_register_get_current_split (reg);
trans = gnc_split_register_get_current_trans (reg);
trans_split = gnc_split_register_get_current_trans_split (reg, NULL);
/* This shouldn't happen, but be paranoid. */
if (trans == NULL)
{
LEAVE("no transaction");
return NULL;
}
cursor_class = gnc_split_register_get_current_cursor_class (reg);
/* Can't do anything with this. */
if (cursor_class == CURSOR_CLASS_NONE)
{
LEAVE("no cursor class");
return NULL;
}
/* This shouldn't happen, but be paranoid. */
if ((split == NULL) && (cursor_class == CURSOR_CLASS_TRANS))
{
LEAVE("no split with transaction class");
return NULL;
}
changed = gnc_table_current_cursor_changed (reg->table, FALSE);
/* See if we were asked to duplicate an unchanged blank split.
* There's no point in doing that! */
if (!changed && ((split == NULL) || (split == blank_split)))
{
LEAVE("skip unchanged blank split");
return NULL;
}
gnc_suspend_gui_refresh ();
/* If the cursor has been edited, we are going to have to commit
* it before we can duplicate. Make sure the user wants to do that. */
if (changed)
{
GtkWidget *dialog, *window;
gint response;
const char *title = _("Save transaction before duplicating?");
const char *message =
_("The current transaction has been changed. Would you like to "
"record the changes before duplicating the transaction, or "
"cancel the duplication?");
window = gnc_split_register_get_parent(reg);
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), "transaction_duplicated");
gtk_widget_destroy(dialog);
if (response != GTK_RESPONSE_ACCEPT)
{
gnc_resume_gui_refresh ();
LEAVE("save cancelled");
return NULL;
}
gnc_split_register_save (reg, TRUE);
/* If the split is NULL, then we were on a blank split row
* in an expanded transaction. The new split (created by
* gnc_split_register_save above) will be the last split in the
* current transaction, as it was just added. */
if (split == NULL)
split = xaccTransGetSplit (trans, xaccTransCountSplits (trans) - 1);
}
/* Ok, we are now ready to make the copy. */
if (cursor_class == CURSOR_CLASS_SPLIT)
{
Split *new_split;
char *out_num;
gboolean new_act_num = FALSE;
/* We are on a split in an expanded transaction.
* Just copy the split and add it to the transaction.
* However, if the split-action field is being used as the register
* number, and the action field is a number, request a new value or
* cancel. Need to get next number and update account last num from
* split account not register account, which may be the same or not */
if (!reg->use_tran_num_for_num_field
&& gnc_strisnum (gnc_get_num_action (NULL, split)))
{
Account *account = xaccSplitGetAccount (split);
const char *in_num = NULL;
const char* title = _("New Split Information");
time64 date = info->last_date_entered;
if (account)
in_num = xaccAccountGetLastNum (account);
else
in_num = gnc_get_num_action (NULL, split);
if (!gnc_dup_trans_dialog (gnc_split_register_get_parent (reg),
title, FALSE, &date, in_num, &out_num, NULL, NULL))
{
gnc_resume_gui_refresh ();
LEAVE("dup cancelled");
return NULL;
}
new_act_num = TRUE;
}
new_split = xaccMallocSplit (gnc_get_current_book ());
xaccTransBeginEdit (trans);
xaccSplitSetParent (new_split, trans);
gnc_copy_split_onto_split (split, new_split, FALSE);
if (new_act_num) /* if new number supplied by user dialog */
gnc_set_num_action (NULL, new_split, out_num, NULL);
xaccTransCommitEdit (trans);
if (new_act_num && gnc_strisnum (out_num))
{
Account *account = xaccSplitGetAccount (new_split);
/* If current register is for account, set last num */
if (xaccAccountEqual(account,
gnc_split_register_get_default_account(reg),
TRUE))
{
NumCell *num_cell;
num_cell = (NumCell *) gnc_table_layout_get_cell (reg->table->layout,
NUM_CELL);
if (gnc_num_cell_set_last_num (num_cell, out_num))
gnc_split_register_set_last_num (reg, out_num);
}
else
{
xaccAccountSetLastNum (account, out_num);
}
}
return_split = new_split;
info->cursor_hint_split = new_split;
info->cursor_hint_cursor_class = CURSOR_CLASS_SPLIT;
if (new_act_num)
g_free (out_num);
}
else
{
NumCell *num_cell;
Transaction *new_trans;
int trans_split_index;
int split_index;
const char *in_num = NULL;
const char *in_tnum = NULL;
char *out_num;
char *out_tnum;
time64 date;
gboolean use_autoreadonly = qof_book_uses_autoreadonly(gnc_get_current_book());
/* We are on a transaction row. Copy the whole transaction. */
date = info->last_date_entered;
if (gnc_strisnum (gnc_get_num_action (trans, trans_split)))
{
Account *account = gnc_split_register_get_default_account (reg);
if (account)
in_num = xaccAccountGetLastNum (account);
else
in_num = gnc_get_num_action (trans, trans_split);
in_tnum = (reg->use_tran_num_for_num_field
? NULL
: gnc_get_num_action (trans, NULL));
}
if (!gnc_dup_trans_dialog (gnc_split_register_get_parent (reg), NULL,
TRUE, &date, in_num, &out_num, in_tnum, &out_tnum))
{
gnc_resume_gui_refresh ();
LEAVE("dup cancelled");
return NULL;
}
if (use_autoreadonly)
{
GDate d;
GDate *readonly_threshold = qof_book_get_autoreadonly_gdate(gnc_get_current_book());
gnc_gdate_set_time64 (&d, date);
if (g_date_compare(&d, readonly_threshold) < 0)
{
GtkWidget *dialog = gtk_message_dialog_new(NULL,
0,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"%s", _("Cannot store a transaction at this date"));
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
"%s", _("The entered date of the duplicated 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);
g_date_free(readonly_threshold);
return NULL;
}
g_date_free(readonly_threshold);
}
split_index = xaccTransGetSplitIndex(trans, split);
trans_split_index = xaccTransGetSplitIndex(trans, trans_split);
/* we should *always* find the split, but be paranoid */
if (split_index < 0)
{
gnc_resume_gui_refresh ();
LEAVE("no split");
return NULL;
}
new_trans = xaccMallocTransaction (gnc_get_current_book ());
xaccTransBeginEdit (new_trans);
gnc_copy_trans_onto_trans (trans, new_trans, FALSE, FALSE);
xaccTransSetDatePostedSecsNormalized (new_trans, date);
/* set per book option */
gnc_set_num_action (new_trans, NULL, out_num, out_tnum);
if (!reg->use_tran_num_for_num_field)
{
/* find split in new_trans that equals trans_split and set
* split_action to out_num */
gnc_set_num_action (NULL,
xaccTransGetSplit (new_trans, trans_split_index),
out_num, NULL);
/* note that if the transaction has multiple splits to the register
* account, only the anchor split will be set with user input. The
* user will have to adjust other splits manually. */
}
xaccTransCommitEdit (new_trans);
num_cell = (NumCell *) gnc_table_layout_get_cell (reg->table->layout,
NUM_CELL);
if (gnc_num_cell_set_last_num (num_cell, out_num))
gnc_split_register_set_last_num (reg, out_num);
g_free (out_num);
if (!reg->use_tran_num_for_num_field)
g_free (out_tnum);
/* This shouldn't happen, but be paranoid. */
if (split_index >= xaccTransCountSplits (new_trans))
split_index = 0;
return_split = xaccTransGetSplit (new_trans, split_index);
trans_split = xaccTransGetSplit (new_trans, trans_split_index);
info->cursor_hint_trans = new_trans;
info->cursor_hint_split = return_split;
info->cursor_hint_trans_split = trans_split;
info->cursor_hint_cursor_class = CURSOR_CLASS_TRANS;
info->trans_expanded = FALSE;
}
/* Refresh the GUI. */
gnc_resume_gui_refresh ();
LEAVE(" ");
return return_split;
}
static void
gnc_split_register_copy_current_internal (SplitRegister *reg,
gboolean use_cut_semantics)
{
SRInfo *info = gnc_split_register_get_info(reg);
CursorClass cursor_class;
Transaction *trans;
Split *blank_split;
gboolean changed;
Split *split;
SCM new_item;
g_return_if_fail(reg);
ENTER("reg=%p, use_cut_semantics=%s", reg,
use_cut_semantics ? "TRUE" : "FALSE");
blank_split = xaccSplitLookup(&info->blank_split_guid,
gnc_get_current_book ());
split = gnc_split_register_get_current_split (reg);
trans = gnc_split_register_get_current_trans (reg);
/* This shouldn't happen, but be paranoid. */
if (trans == NULL)
{
LEAVE("no trans");
return;
}
cursor_class = gnc_split_register_get_current_cursor_class (reg);
/* Can't do anything with this. */
if (cursor_class == CURSOR_CLASS_NONE)
{
LEAVE("no cursor class");
return;
}
/* This shouldn't happen, but be paranoid. */
if ((split == NULL) && (cursor_class == CURSOR_CLASS_TRANS))
{
g_warning("BUG DETECTED: transaction cursor with no anchoring split!");
LEAVE("transaction cursor with no anchoring split");
return;
}
changed = gnc_table_current_cursor_changed (reg->table, FALSE);
/* See if we were asked to copy an unchanged blank split. Don't. */
if (!changed && ((split == NULL) || (split == blank_split)))
{
/* We're either on an unedited, brand-new split or an unedited, brand-new
* transaction (the transaction anchored by the blank split.) */
/* FIXME: This doesn't work exactly right. When entering a new transaction,
* you can edit the description, move to a split row, then move
* back to the description, then ask for a copy, and this code will
* be reached. It forgets that you changed the row the first time
* you were there. -Charles */
LEAVE("nothing to copy/cut");
return;
}
/* Ok, we are now ready to make the copy. */
if (cursor_class == CURSOR_CLASS_SPLIT)
{
/* We are on a split in an expanded transaction. Just copy the split. */
new_item = gnc_copy_split(split, use_cut_semantics);
if (new_item != SCM_UNDEFINED)
{
if (changed)
gnc_split_register_save_to_scm (reg, SCM_UNDEFINED, new_item,
use_cut_semantics);
copied_leader_guid = *guid_null();
}
}
else
{
/* We are on a transaction row. Copy the whole transaction. */
new_item = gnc_copy_trans(trans, use_cut_semantics);
if (new_item != SCM_UNDEFINED)
{
if (changed)
{
int split_index;
SCM split_scm;
split_index = xaccTransGetSplitIndex(trans, split);
if (split_index >= 0)
split_scm = gnc_trans_scm_get_split_scm(new_item, split_index);
else
split_scm = SCM_UNDEFINED;
gnc_split_register_save_to_scm (reg, new_item, split_scm,
use_cut_semantics);
}
copied_leader_guid = info->default_account;
}
}
if (new_item == SCM_UNDEFINED)
{
g_warning("BUG DETECTED: copy failed");
LEAVE("copy failed");
return;
}
/* unprotect the old object, if any */
if (copied_item != SCM_UNDEFINED)
scm_gc_unprotect_object(copied_item);
copied_item = new_item;
scm_gc_protect_object(copied_item);
copied_class = cursor_class;
LEAVE("%s %s", use_cut_semantics ? "cut" : "copied",
cursor_class == CURSOR_CLASS_SPLIT ? "split" : "transaction");
}
void
gnc_split_register_copy_current (SplitRegister *reg)
{
gnc_split_register_copy_current_internal (reg, FALSE);
}
void
gnc_split_register_cut_current (SplitRegister *reg)
{
SRInfo *info = gnc_split_register_get_info (reg);
CursorClass cursor_class;
Transaction *trans;
Split *blank_split;
gboolean changed;
Split *split;
blank_split = xaccSplitLookup (&info->blank_split_guid,
gnc_get_current_book ());
split = gnc_split_register_get_current_split (reg);
trans = gnc_split_register_get_current_trans (reg);
/* This shouldn't happen, but be paranoid. */
if (trans == NULL)
return;
cursor_class = gnc_split_register_get_current_cursor_class (reg);
/* Can't do anything with this. */
if (cursor_class == CURSOR_CLASS_NONE)
return;
/* This shouldn't happen, but be paranoid. */
if ((split == NULL) && (cursor_class == CURSOR_CLASS_TRANS))
return;
changed = gnc_table_current_cursor_changed (reg->table, FALSE);
/* See if we were asked to cut an unchanged blank split. Don't. */
if (!changed && ((split == NULL) || (split == blank_split)))
return;
gnc_split_register_copy_current_internal (reg, TRUE);
if (cursor_class == CURSOR_CLASS_SPLIT)
gnc_split_register_delete_current_split (reg);
else
gnc_split_register_delete_current_trans (reg);
}
void
gnc_split_register_paste_current (SplitRegister *reg)
{
SRInfo *info = gnc_split_register_get_info(reg);
CursorClass cursor_class;
Transaction *trans;
Transaction *blank_trans;
Split *blank_split;
Split *trans_split;
Split *split;
ENTER("reg=%p", reg);
if (copied_class == CURSOR_CLASS_NONE)
{
LEAVE("no copied cursor class");
return;
}
blank_split = xaccSplitLookup (&info->blank_split_guid,
gnc_get_current_book ());
blank_trans = xaccSplitGetParent (blank_split);
split = gnc_split_register_get_current_split (reg);
trans = gnc_split_register_get_current_trans (reg);
trans_split = gnc_split_register_get_current_trans_split (reg, NULL);
/* This shouldn't happen, but be paranoid. */
if (trans == NULL)
{
LEAVE("no transaction");
return;
}
cursor_class = gnc_split_register_get_current_cursor_class (reg);
/* Can't do anything with this. */
if (cursor_class == CURSOR_CLASS_NONE)
{
LEAVE("no current cursor class");
return;
}
/* This shouldn't happen, but be paranoid. */
if ((split == NULL) && (cursor_class == CURSOR_CLASS_TRANS))
{
g_warning("BUG DETECTED: transaction cursor with no anchoring split!");
LEAVE("transaction cursor with no anchoring split");
return;
}
if (cursor_class == CURSOR_CLASS_SPLIT)
{
const char *message = _("You are about to overwrite an existing split. "
"Are you sure you want to do that?");
if (copied_class == CURSOR_CLASS_TRANS)
{
/* An entire transaction was copied, but we're just on a split. */
LEAVE("can't copy trans to split");
return;
}
/* Ask before overwriting an existing split. */
if (split != NULL &&
!gnc_verify_dialog (gnc_split_register_get_parent (reg),
FALSE, "%s", message))
{
LEAVE("user cancelled");
return;
}
gnc_suspend_gui_refresh ();
if (split == NULL)
{
/* We are on a null split in an expanded transaction. */
split = xaccMallocSplit(gnc_get_current_book ());
xaccSplitSetParent(split, trans);
}
gnc_copy_split_scm_onto_split(copied_item, split,
gnc_get_current_book ());
}
else
{
const char *message = _("You are about to overwrite an existing "
"transaction. "
"Are you sure you want to do that?");
Account * copied_leader;
const GncGUID *new_guid;
int trans_split_index;
int split_index;
int num_splits;
if (copied_class == CURSOR_CLASS_SPLIT)
{
LEAVE("can't copy split to transaction");
return;
}
/* Ask before overwriting an existing transaction. */
if (split != blank_split &&
!gnc_verify_dialog(gnc_split_register_get_parent(reg),
FALSE, "%s", message))
{
LEAVE("user cancelled");
return;
}
/* Open the transaction for editing. */
if (gnc_split_register_begin_edit_or_warn(info, trans))
{
LEAVE("can't begin editing");
return;
}
gnc_suspend_gui_refresh ();
DEBUG("Pasting txn, trans=%p, split=%p, blank_trans=%p, blank_split=%p",
trans, split, blank_trans, blank_split);
split_index = xaccTransGetSplitIndex(trans, split);
trans_split_index = xaccTransGetSplitIndex(trans, trans_split);
copied_leader = xaccAccountLookup(&copied_leader_guid,
gnc_get_current_book());
if (copied_leader && (gnc_split_register_get_default_account(reg) != NULL))
{
new_guid = &info->default_account;
gnc_copy_trans_scm_onto_trans_swap_accounts(copied_item, trans,
&copied_leader_guid,
new_guid, FALSE,
gnc_get_current_book ());
}
else
gnc_copy_trans_scm_onto_trans(copied_item, trans, FALSE,
gnc_get_current_book ());
num_splits = xaccTransCountSplits(trans);
if (split_index >= num_splits)
split_index = 0;
if (trans == blank_trans)
{
/* In pasting, the blank split is deleted. Pick a new one. */
blank_split = xaccTransGetSplit(trans, 0);