-
Notifications
You must be signed in to change notification settings - Fork 762
/
dialog-print-check.c
2740 lines (2405 loc) · 98.9 KB
/
dialog-print-check.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
/********************************************************************\
* dialog-print-check.c : dialog to control check printing. *
* Copyright (C) 2000 Bill Gribble <grib@billgribble.com> *
* Copyright (C) 2006,2007 David Hampton <hampton@employees.org> *
* *
* 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 *
\********************************************************************/
/**
* @file dialog-print-check.c
* @brief Print Checks Dialog
* @author Copyright (C) 2000 Bill Gribble <grib@billgribble.com>
* @author Copyright (C) 2006,2007 David Hampton <hampton@employees.org>
*/
#include <config.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <stdio.h>
#include <locale.h>
#include <math.h>
#include "qof.h"
#include "gnc-date.h"
#include "gnc-prefs.h"
#include "gnc-numeric.h"
#include "gnc-plugin-page-register.h"
#include "dialog-print-check.h"
#include "dialog-utils.h"
#include "print-session.h"
#include "gnc-ui.h"
#include "gnc-date-format.h"
#include "gnc-ui-util.h"
#include "gnc-path.h"
#include "gnc-filepath-utils.h"
#include "gnc-gkeyfile-utils.h"
#include "gnc-engine.h"
#include "engine-helpers.h"
#include "Split.h"
#include "Transaction.h"
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "gnc.printing.checks"
/* This static indicates the debugging module that this .o belongs to.
*/
G_GNUC_UNUSED static QofLogModule log_module = "gnc.printing.checks";
#define GNC_PREFS_GROUP "dialogs.checkprinting"
#define GNC_PREF_CHECK_FORMAT_GUID "check-format-guid"
#define GNC_PREF_CHECK_POSITION "check-position"
#define GNC_PREF_FIRST_PAGE_COUNT "first-page-count"
#define GNC_PREF_DATE_FORMAT_USER "date-format-user"
#define GNC_PREF_CUSTOM_PAYEE "custom-payee"
#define GNC_PREF_CUSTOM_DATE "custom-date"
#define GNC_PREF_CUSTOM_WORDS "custom-amount-words"
#define GNC_PREF_CUSTOM_NUMBER "custom-amount-number"
#define GNC_PREF_CUSTOM_ADDRESS "custom-address"
#define GNC_PREF_CUSTOM_NOTES "custom-notes"
#define GNC_PREF_CUSTOM_MEMO "custom-memo"
#define GNC_PREF_CUSTOM_TRANSLATION "custom-translation"
#define GNC_PREF_CUSTOM_ROTATION "custom-rotation"
#define GNC_PREF_CUSTOM_UNITS "custom-units"
#define GNC_PREF_PRINT_DATE_FMT "print-date-format"
#define GNC_PREF_DEFAULT_FONT "default-font"
#define GNC_PREF_BLOCKING_CHARS "blocking-chars"
#define GNC_PREF_SPLITS_AMOUNT "splits-amount"
#define GNC_PREF_SPLITS_MEMO "splits-memo"
#define GNC_PREF_SPLITS_ACCOUNT "splits-account"
#define DEFAULT_FONT "sans 12"
#define CHECK_FMT_DIR "checks"
#define CHECK_NAME_EXTENSION ".chk"
#define DEGREES_TO_RADIANS (G_PI / 180.0)
#define BLOCKING_CHAR_OFF 0
#define BLOCKING_CHAR_ON 1
#define KF_GROUP_TOP "Top"
#define KF_GROUP_POS "Check Positions"
#define KF_GROUP_ITEMS "Check Items"
#define KF_KEY_GUID "Guid"
#define KF_KEY_TITLE "Title"
#define KF_KEY_ROTATION "Rotation"
#define KF_KEY_TRANSLATION "Translation"
#define KF_KEY_FONT "Font"
#define KF_KEY_ALIGN "Align"
#define KF_KEY_BLOCKING "Blocking_Chars"
#define KF_KEY_SHOW_GRID "Show_Grid"
#define KF_KEY_SHOW_BOXES "Show_Boxes"
#define KF_KEY_NAMES "Names"
#define KF_KEY_HEIGHT "Height"
#define KF_KEY_TYPE "Type"
#define KF_KEY_COORDS "Coords"
#define KF_KEY_TEXT "Text"
#define KF_KEY_FILENAME "Filename"
#define KF_KEY_DATE_FORMAT "DateFormat"
#define KF_KEY_SPLITS_AMOUNT "SplitsAmount"
#define KF_KEY_SPLITS_MEMO "SplitsMemo"
#define KF_KEY_SPLITS_ACCOUNT "SplitsAccount"
/* This enum specifies the columns used in the check format combobox.
*/
typedef enum format_combo_col_t
{
COL_NAME = 0, /**< This column holds a copy of the check
* format name and is what is displayed to
* the user in the combobox. It is NULL for
* separator lines. */
COL_DATA, /**< This column holds a pointer to the check
* format data read in from a file. It is
* NULL for the custom check format and for
* separator lines. */
COL_SEP, /**< This column contains the value TRUE if
* this entry specifies a separator line. */
} format_combo_col;
void gnc_ui_print_check_response_cb (GtkDialog *dialog, gint response, PrintCheckDialog *pcd);
void gnc_print_check_format_changed (GtkComboBox *widget, PrintCheckDialog *pcd);
void gnc_print_check_position_changed (GtkComboBox *widget, PrintCheckDialog *pcd);
void gnc_print_check_save_button_clicked (GtkButton *button, PrintCheckDialog *pcd);
void gnc_check_format_title_changed (GtkEditable *editable, GtkWidget *ok_button);
static void initialize_format_combobox (PrintCheckDialog *pcd);
gchar* get_check_address (PrintCheckDialog *pcd);
static gboolean check_format_has_address (PrintCheckDialog *pcd);
gchar* get_check_splits_amount (PrintCheckDialog *pcd);
gchar* get_check_splits_memo (PrintCheckDialog *pcd);
gchar* get_check_splits_account (PrintCheckDialog *pcd);
/* This enum defines the types of items that gnucash knows how to
* print on checks. Most refer to specific fields from a gnucash
* transaction and split, but some are generic items unrelated to
* gnucash.
*/
#define ENUM_CHECK_ITEM_TYPE(_) \
_(NONE,) \
_(PAYEE,) \
_(DATE,) \
_(NOTES,) \
_(CHECK_NUMBER,) \
\
_(MEMO,) \
_(ACTION,) \
_(AMOUNT_NUMBER,) \
_(AMOUNT_WORDS,) \
\
_(TEXT,) \
_(ADDRESS,) \
_(DATE_FORMAT,) \
_(SPLITS_AMOUNT,) \
_(SPLITS_MEMO,) \
_(SPLITS_ACCOUNT,) \
_(PICTURE,)
DEFINE_ENUM(CheckItemType, ENUM_CHECK_ITEM_TYPE)
FROM_STRING_DEC(CheckItemType, ENUM_CHECK_ITEM_TYPE)
FROM_STRING_FUNC(CheckItemType, ENUM_CHECK_ITEM_TYPE)
AS_STRING_DEC(CheckItemType, ENUM_CHECK_ITEM_TYPE)
AS_STRING_FUNC(CheckItemType, ENUM_CHECK_ITEM_TYPE)
/* This data structure describes a single item printed on a check.
* It is built from a description in a text file.
*/
typedef struct _check_item
{
CheckItemType type; /**< What type of item is this? */
gdouble x, y; /**< The x/y coordinates where this item should
* be printed. The origin for these
* coordinates is determined by the print
* system used. */
gdouble w, h; /**< Optional. The width and height of this
* item. Text will be clipped to this
* size. Pictures will be shrunk to fit if
* necessary. */
gchar *filename; /**< The filename for picture items. Otherwise
* unused. */
gchar *text; /**< The text to be displayed (for text based
* items.) Otherwise unused. */
gchar *font; /**< The font to use for text based items. This
* overrides any font in the check format.
* Unused for non-text items. */
gboolean blocking; /**< Optional. Overrides blocking in the check
* format. Default is no blocking characters
* are written. Unused for non-text
* items. */
gboolean print_date_format; /**< Optional. Print date format. Only
* applies to DATE items. Default is no
* format is written. */
PangoAlignment align; /**< The alignment of a text based item. Only
* used for text based items when a width is
* specified. */
} check_item_t;
/* This data structure describes an entire page of checks. Depending
* upon the check format, the page may contain multiple checks or
* only a single check. The data structure is built from a
* description in a text file.
*/
typedef struct _check_format
{
gchar *guid; /**< Unique identifier for this format. */
const gchar *group; /**< The group where this format was found. */
gchar *filename; /**< The name of the file from which this data
* was read. */
gchar *title; /**< Title of this check format. Displayed in
* the dialog box. */
gboolean blocking; /**< Default for printing blocking characters for
* this page of checks. */
gboolean print_date_format; /**< Default for printing date format characters for
* this page of checks. */
gboolean show_grid; /**< Print a grid pattern on the page */
gboolean show_boxes; /**< Print boxes when width and height are
* known. */
gdouble rotation; /**< Rotate the entire page by this amount. */
gdouble trans_x; /**< Move entire page left by this amount. */
gdouble trans_y; /**< Move entire page down by this amount. */
gchar *font; /**< Default font for this page of checks. */
gdouble height; /**< Height of one check on a page. */
GSList *positions; /**< Names of the checks on the page. */
GSList *items; /**< List of items printed on each check. */
} check_format_t;
/* This data structure is used to manage the print check dialog, and
* the overall check printing process. It contains pointers to many
* of the widgets in the dialog, pointers to the check descriptions
* that have been read, and also contains the data from the gnucash
* transaction/split that is to be printed.
*/
struct _print_check_dialog
{
GtkBuilder *builder;
GtkWidget *dialog;
GtkWindow *caller_window;
Split *split;
GList *splits;
Account* account;
GtkWidget *format_combobox;
gint format_max;
GtkWidget *position_combobox;
gint position_max;
GtkSpinButton *first_page_count;
GtkWidget *custom_table;
GtkSpinButton *payee_x, *payee_y;
GtkSpinButton *date_x, *date_y;
GtkSpinButton *words_x, *words_y;
GtkSpinButton *number_x, *number_y;
GtkSpinButton *address_x, *address_y;
GtkSpinButton *notes_x, *notes_y;
GtkSpinButton *memo_x, *memo_y;
GtkSpinButton *splits_amount_x, *splits_amount_y;
GtkSpinButton *splits_memo_x, *splits_memo_y;
GtkSpinButton *splits_account_x, *splits_account_y;
GtkSpinButton *translation_x, *translation_y;
GtkSpinButton *check_rotation;
GtkWidget *translation_label;
GtkWidget *units_combobox;
GtkWidget *date_format;
GtkWidget *check_address_name;
GtkWidget *check_address_1;
GtkWidget *check_address_2;
GtkWidget *check_address_3;
GtkWidget *check_address_4;
gchar *default_font;
check_format_t *selected_format;
};
/* This function walks the list of available check formats looking for a
* specific format as specified by guid number. If found, a pointer to it is
* returned to the caller. Additionally, if the caller passed a pointer to a
* GtkTreeIter, then the iter for that entry will also be returned.
*/
static check_format_t *
find_existing_format (GtkListStore *store, gchar *guid, GtkTreeIter *iter_out)
{
GtkTreeIter iter;
check_format_t *format;
g_return_val_if_fail(store, NULL);
g_return_val_if_fail(guid, NULL);
if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
return NULL;
do
{
gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
COL_DATA, &format, -1);
if (format == NULL)
continue;
if (strcmp(format->guid, guid) != 0)
continue;
if (iter_out)
*iter_out = iter;
return format;
}
while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter));
return NULL;
}
/* This function returns a string containing the check address in a five-line
* format.
*
* Note that the string needs to be freed with g_free.
*/
gchar *
get_check_address( PrintCheckDialog *pcd)
{
gchar *address;
address = g_strconcat(gtk_entry_get_text(GTK_ENTRY(pcd->check_address_name)), "\n",
gtk_entry_get_text(GTK_ENTRY(pcd->check_address_1)), "\n",
gtk_entry_get_text(GTK_ENTRY(pcd->check_address_2)), "\n",
gtk_entry_get_text(GTK_ENTRY(pcd->check_address_3)), "\n",
gtk_entry_get_text(GTK_ENTRY(pcd->check_address_4)),
NULL);
return address;
}
struct _trans_amount
{
const Transaction* trans;
gnc_numeric amount;
};
static void
subtotal_subaccount(const Account *account, struct _trans_amount* trans_amount)
{
/* Get the amount of this account in the transaction.*/
gnc_numeric amount = xaccTransGetAccountAmount(trans_amount->trans, account);
/* Accumulate. */
trans_amount->amount = gnc_numeric_add_fixed(trans_amount->amount, amount);
}
/* This function returns the amount of the check.
*/
static gnc_numeric
get_check_amount(PrintCheckDialog *pcd)
{
gnc_numeric amount;
if (pcd->account)
{
/* A parent account, e.g. from a subaccount register plugin page.
* Subtotal the amount of all splits from descendant accounts. */
struct _trans_amount trans_amount;
trans_amount.trans = xaccSplitGetParent(pcd->split);
trans_amount.amount = gnc_numeric_zero();
gnc_account_foreach_descendant(pcd->account, (AccountCb)subtotal_subaccount, &trans_amount);
amount = trans_amount.amount;
}
else
{
/* Print just the amount of the split. */
amount = xaccSplitGetAmount(pcd->split);
}
return gnc_numeric_abs(amount);
}
//@{
/** @name Split printing functions */
/* This function formats the splits amounts for printing.
*/
gchar *
get_check_splits_amount(PrintCheckDialog *pcd)
{
gchar* amount = NULL;
Transaction *trans;
GList *node;
SplitList* s_list;
trans = xaccSplitGetParent(pcd->split);
s_list = xaccTransGetSplitList(trans);
if ( !s_list ) return NULL;
amount = g_strconcat("", NULL);
node = s_list;
while ( node )
{
Split *split = node->data;
/* Include all splits except the main split for the check */
if (split != pcd->split)
{
const gchar* split_amount;
gchar* amt_temp;
split_amount = xaccPrintAmount(xaccSplitGetAmount(split), gnc_split_amount_print_info(split, TRUE));
amt_temp = amount;
if (amount && *amount)
amount = g_strconcat(amt_temp, "\n", split_amount, NULL);
else
amount = g_strconcat(amt_temp, split_amount, NULL);
g_free(amt_temp);
}
node = node->next;
}
return amount;
}
/* This function formats the splits memo fields for printing.
*/
gchar *
get_check_splits_memo(PrintCheckDialog *pcd)
{
gchar* memo = NULL;
const gchar* split_memo;
Transaction *trans;
GList *node;
SplitList* s_list;
trans = xaccSplitGetParent(pcd->split);
s_list = xaccTransGetSplitList(trans);
if ( !s_list ) return NULL;
memo = g_strconcat("", NULL);
node = s_list;
while ( node )
{
Split *split = node->data;
/* Include all splits except the main split for the check */
if (split != pcd->split)
{
gchar* memo_temp;
split_memo = xaccSplitGetMemo(split);
memo_temp = memo;
if (memo && *memo)
memo = g_strconcat(memo_temp, "\n", split_memo, NULL);
else
memo = g_strconcat(memo_temp, split_memo, NULL);
g_free(memo_temp);
}
node = node->next;
}
return memo;
}
/* This function formats the splits accounts for printing.
*/
gchar *
get_check_splits_account(PrintCheckDialog *pcd)
{
gchar* account = NULL;
Transaction *trans;
GList *node;
SplitList* s_list;
trans = xaccSplitGetParent(pcd->split);
s_list = xaccTransGetSplitList(trans);
if ( !s_list ) return NULL;
account = g_strconcat("", NULL);
node = s_list;
while ( node )
{
Split *split = node->data;
/* Include all splits except the main split for the check */
if (split != pcd->split)
{
gchar* account_temp;
const gchar* aName = NULL;
Account *pAccount;
pAccount = xaccSplitGetAccount(split);
aName = gnc_get_account_name_for_register(pAccount);
account_temp = account;
if (account && *account)
account = g_strconcat(account_temp, "\n", aName, NULL);
else
account = g_strconcat(account_temp, aName, NULL);
g_free(account_temp);
}
node = node->next;
}
return account;
}
//@}
/* This function determines if an address item is present in the check format.
*/
static gboolean
check_format_has_address ( PrintCheckDialog *pcd )
{
/* check format for an ADDRESS item */
check_item_t *item = NULL;
GSList *elem;
check_format_t *format = NULL;
if ( !pcd ) return FALSE;
/* If we're printing more than one check no addresses are allowed */
if (g_list_length(pcd->splits) != 1)
return FALSE;
/* if format is NULL, then the custom format is being used
* which has an ADDRESS item by definition */
format = pcd->selected_format;
if ( !format ) return TRUE;
for (elem = pcd->selected_format->items; elem; elem = g_slist_next(elem))
{
item = elem->data;
if ( item->type == ADDRESS ) return TRUE;
}
return FALSE;
}
static void
gnc_ui_print_save_dialog(PrintCheckDialog *pcd)
{
GtkTreeModel *model;
GtkTreeIter iter;
check_format_t *check;
const gchar *format;
gint active;
/* Options page */
if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(pcd->format_combobox),
&iter))
{
model = gtk_combo_box_get_model(GTK_COMBO_BOX(pcd->format_combobox));
gtk_tree_model_get(model, &iter, COL_DATA, &check, -1);
gnc_prefs_set_string (GNC_PREFS_GROUP, GNC_PREF_CHECK_FORMAT_GUID,
check ? check->guid : "custom");
}
active = gtk_combo_box_get_active(GTK_COMBO_BOX(pcd->position_combobox));
gnc_prefs_set_int(GNC_PREFS_GROUP, GNC_PREF_CHECK_POSITION, active);
active = gtk_spin_button_get_value_as_int(pcd->first_page_count);
gnc_prefs_set_int(GNC_PREFS_GROUP, GNC_PREF_FIRST_PAGE_COUNT, active);
active = gnc_date_format_get_format (GNC_DATE_FORMAT(pcd->date_format));
gnc_prefs_set_int(GNC_PREFS_GROUP, GNC_PREF_DATE_FORMAT, active);
if (active == QOF_DATE_FORMAT_CUSTOM)
{
format = gnc_date_format_get_custom (GNC_DATE_FORMAT(pcd->date_format));
gnc_prefs_set_string (GNC_PREFS_GROUP, GNC_PREF_DATE_FORMAT_USER, format);
}
else
{
gnc_prefs_reset (GNC_PREFS_GROUP, GNC_PREF_DATE_FORMAT_USER);
}
/* Custom format page */
gnc_prefs_set_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_PAYEE,
gtk_spin_button_get_value(pcd->payee_x),
gtk_spin_button_get_value(pcd->payee_y));
gnc_prefs_set_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_DATE,
gtk_spin_button_get_value(pcd->date_x),
gtk_spin_button_get_value(pcd->date_y));
gnc_prefs_set_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_WORDS,
gtk_spin_button_get_value(pcd->words_x),
gtk_spin_button_get_value(pcd->words_y));
gnc_prefs_set_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_NUMBER,
gtk_spin_button_get_value(pcd->number_x),
gtk_spin_button_get_value(pcd->number_y));
gnc_prefs_set_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_NOTES,
gtk_spin_button_get_value(pcd->notes_x),
gtk_spin_button_get_value(pcd->notes_y));
gnc_prefs_set_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_MEMO,
gtk_spin_button_get_value(pcd->memo_x),
gtk_spin_button_get_value(pcd->memo_y));
gnc_prefs_set_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_ADDRESS,
gtk_spin_button_get_value(pcd->address_x),
gtk_spin_button_get_value(pcd->address_y));
gnc_prefs_set_coords(GNC_PREFS_GROUP, GNC_PREF_SPLITS_AMOUNT,
gtk_spin_button_get_value(pcd->splits_amount_x),
gtk_spin_button_get_value(pcd->splits_amount_y));
gnc_prefs_set_coords(GNC_PREFS_GROUP, GNC_PREF_SPLITS_MEMO,
gtk_spin_button_get_value(pcd->splits_memo_x),
gtk_spin_button_get_value(pcd->splits_memo_y));
gnc_prefs_set_coords(GNC_PREFS_GROUP, GNC_PREF_SPLITS_ACCOUNT,
gtk_spin_button_get_value(pcd->splits_account_x),
gtk_spin_button_get_value(pcd->splits_account_y));
gnc_prefs_set_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_TRANSLATION,
gtk_spin_button_get_value(pcd->translation_x),
gtk_spin_button_get_value(pcd->translation_y));
gnc_prefs_set_float(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_ROTATION,
gtk_spin_button_get_value(pcd->check_rotation));
active = gtk_combo_box_get_active(GTK_COMBO_BOX(pcd->units_combobox));
gnc_prefs_set_int(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_UNITS, active);
}
static void
gnc_ui_print_restore_dialog(PrintCheckDialog *pcd)
{
GtkTreeModel *model;
GtkTreeIter iter;
gchar *format, *guid;
gdouble x, y;
gint active;
/* Options page */
guid = gnc_prefs_get_string (GNC_PREFS_GROUP, GNC_PREF_CHECK_FORMAT_GUID);
if (!(guid && *guid))
gtk_combo_box_set_active(GTK_COMBO_BOX(pcd->format_combobox), 0);
else if (strcmp(guid, "custom") == 0)
{
gtk_combo_box_set_active(GTK_COMBO_BOX(pcd->format_combobox),
pcd->format_max - 1);
}
else
{
model = gtk_combo_box_get_model(GTK_COMBO_BOX(pcd->format_combobox));
if (find_existing_format(GTK_LIST_STORE(model), guid, &iter))
{
gtk_combo_box_set_active_iter(GTK_COMBO_BOX(pcd->format_combobox), &iter);
}
else
{
gtk_combo_box_set_active(GTK_COMBO_BOX(pcd->format_combobox), 0);
}
}
g_free (guid);
active = gnc_prefs_get_int(GNC_PREFS_GROUP, GNC_PREF_CHECK_POSITION);
/* If the check format used last time no longer exists, then the saved check
position may be invalid. If so set it to the first position. */
if (active < 0 || active > pcd->position_max)
active = 0;
gtk_combo_box_set_active(GTK_COMBO_BOX(pcd->position_combobox), active);
active = gnc_prefs_get_int(GNC_PREFS_GROUP, GNC_PREF_FIRST_PAGE_COUNT);
gtk_spin_button_set_value(pcd->first_page_count, (gdouble) active);
active = gnc_prefs_get_int(GNC_PREFS_GROUP, GNC_PREF_DATE_FORMAT);
gnc_date_format_set_format(GNC_DATE_FORMAT(pcd->date_format), active);
if (active == QOF_DATE_FORMAT_CUSTOM)
{
format = gnc_prefs_get_string (GNC_PREFS_GROUP, GNC_PREF_DATE_FORMAT_USER);
if (format && *format)
{
gnc_date_format_set_custom(GNC_DATE_FORMAT(pcd->date_format), format);
g_free(format);
}
}
/* Custom format page */
gnc_prefs_get_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_PAYEE, &x, &y);
gtk_spin_button_set_value(pcd->payee_x, x);
gtk_spin_button_set_value(pcd->payee_y, y);
gnc_prefs_get_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_DATE, &x, &y);
gtk_spin_button_set_value(pcd->date_x, x);
gtk_spin_button_set_value(pcd->date_y, y);
gnc_prefs_get_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_WORDS, &x, &y);
gtk_spin_button_set_value(pcd->words_x, x);
gtk_spin_button_set_value(pcd->words_y, y);
gnc_prefs_get_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_NUMBER, &x, &y);
gtk_spin_button_set_value(pcd->number_x, x);
gtk_spin_button_set_value(pcd->number_y, y);
gnc_prefs_get_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_ADDRESS, &x, &y);
gtk_spin_button_set_value(pcd->address_x, x);
gtk_spin_button_set_value(pcd->address_y, y);
gnc_prefs_get_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_NOTES, &x, &y);
gtk_spin_button_set_value(pcd->notes_x, x);
gtk_spin_button_set_value(pcd->notes_y, y);
gnc_prefs_get_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_MEMO, &x, &y);
gtk_spin_button_set_value(pcd->memo_x, x);
gtk_spin_button_set_value(pcd->memo_y, y);
gnc_prefs_get_coords(GNC_PREFS_GROUP, GNC_PREF_SPLITS_AMOUNT, &x, &y);
gtk_spin_button_set_value(pcd->splits_amount_x, x);
gtk_spin_button_set_value(pcd->splits_amount_y, y);
gnc_prefs_get_coords(GNC_PREFS_GROUP, GNC_PREF_SPLITS_MEMO, &x, &y);
gtk_spin_button_set_value(pcd->splits_memo_x, x);
gtk_spin_button_set_value(pcd->splits_memo_y, y);
gnc_prefs_get_coords(GNC_PREFS_GROUP, GNC_PREF_SPLITS_ACCOUNT, &x, &y);
gtk_spin_button_set_value(pcd->splits_account_x, x);
gtk_spin_button_set_value(pcd->splits_account_y, y);
gnc_prefs_get_coords(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_TRANSLATION, &x, &y);
gtk_spin_button_set_value(pcd->translation_x, x);
gtk_spin_button_set_value(pcd->translation_y, y);
x = gnc_prefs_get_float(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_ROTATION);
gtk_spin_button_set_value(pcd->check_rotation, x);
active = gnc_prefs_get_int(GNC_PREFS_GROUP, GNC_PREF_CUSTOM_UNITS);
gtk_combo_box_set_active(GTK_COMBO_BOX(pcd->units_combobox), active);
}
static gdouble
pcd_get_custom_multip(PrintCheckDialog *pcd)
{
gint selected;
selected = gtk_combo_box_get_active(GTK_COMBO_BOX(pcd->units_combobox));
switch (selected)
{
default:
return 72.0; /* inches */
case 1:
return 28.346; /* cm */
case 2:
return 2.8346; /* mm */
case 3:
return 1.0; /* points */
}
}
/* This function saves a coordinate pair into a check description file. It
* extracts the values from the spin buttons, adjusts them according to the
* unit multiplier (inches, pixels, etc), then adds them to the gKeyFile.
*/
static void
pcd_key_file_save_xy (GKeyFile *key_file, const gchar *group_name,
const gchar *key_name, gdouble multip,
GtkSpinButton *spin0, GtkSpinButton *spin1)
{
gdouble dd[2];
dd[0] = multip * gtk_spin_button_get_value(spin0);
dd[1] = multip * gtk_spin_button_get_value(spin1);
/* Clip the numbers to three decimal places. */
dd[0] = round(dd[0] * 1000) / 1000;
dd[1] = round(dd[1] * 1000) / 1000;
g_key_file_set_double_list(key_file, group_name, key_name, dd, 2);
}
/* This function saves the information about a single printed item into a
* check description file. It uses a helper function to extracts and save the
* item coordinates.
*/
static void
pcd_key_file_save_item_xy (GKeyFile *key_file, int index,
CheckItemType type, gdouble multip,
GtkSpinButton *spin0, GtkSpinButton *spin1)
{
gchar *key;
key = g_strdup_printf("Type_%d", index);
g_key_file_set_string(key_file, KF_GROUP_ITEMS, key,
CheckItemTypeasString(type));
g_free(key);
key = g_strdup_printf("Coords_%d", index);
pcd_key_file_save_xy(key_file, KF_GROUP_ITEMS, key, multip, spin0, spin1);
g_free(key);
}
/* This function saves all of the information from the custom check dialog
* into a check description file.
*/
static void
pcd_save_custom_data(PrintCheckDialog *pcd, const gchar *title)
{
GKeyFile *key_file;
GError *error = NULL;
GtkWidget *dialog;
gdouble multip;
gint i = 1;
GncGUID guid;
char buf[GUID_ENCODING_LENGTH+1];
gchar *filename, *pathname;
multip = pcd_get_custom_multip(pcd);
key_file = g_key_file_new();
guid_replace(&guid);
guid_to_string_buff(&guid, buf);
g_key_file_set_string(key_file, KF_GROUP_TOP, KF_KEY_GUID, buf);
g_key_file_set_string(key_file, KF_GROUP_TOP, KF_KEY_TITLE, title);
g_key_file_set_boolean(key_file, KF_GROUP_TOP, KF_KEY_SHOW_GRID, FALSE);
g_key_file_set_boolean(key_file, KF_GROUP_TOP, KF_KEY_SHOW_BOXES, FALSE);
g_key_file_set_double(key_file, KF_GROUP_TOP, KF_KEY_ROTATION,
gtk_spin_button_get_value(pcd->check_rotation));
pcd_key_file_save_xy(key_file, KF_GROUP_TOP, KF_KEY_TRANSLATION, multip,
pcd->translation_x, pcd->translation_y);
pcd_key_file_save_item_xy(key_file, i++, PAYEE, multip,
pcd->payee_x, pcd->payee_y);
pcd_key_file_save_item_xy(key_file, i++, DATE, multip,
pcd->date_x, pcd->date_y);
pcd_key_file_save_item_xy(key_file, i++, AMOUNT_WORDS, multip,
pcd->words_x, pcd->words_y);
pcd_key_file_save_item_xy(key_file, i++, AMOUNT_NUMBER, multip,
pcd->number_x, pcd->number_y);
pcd_key_file_save_item_xy(key_file, i++, ADDRESS, multip,
pcd->address_x, pcd->address_y);
pcd_key_file_save_item_xy(key_file, i++, NOTES, multip,
pcd->notes_x, pcd->notes_y);
pcd_key_file_save_item_xy(key_file, i++, MEMO, multip,
pcd->memo_x, pcd->memo_y);
pcd_key_file_save_item_xy(key_file, i++, SPLITS_AMOUNT, multip,
pcd->splits_amount_x, pcd->splits_amount_y);
pcd_key_file_save_item_xy(key_file, i++, SPLITS_MEMO, multip,
pcd->splits_memo_x, pcd->splits_memo_y);
pcd_key_file_save_item_xy(key_file, i++, SPLITS_ACCOUNT, multip,
pcd->splits_account_x, pcd->splits_account_y);
filename = g_strconcat(title, CHECK_NAME_EXTENSION, NULL);
pathname = g_build_filename(gnc_userdata_dir(), CHECK_FMT_DIR,
filename, NULL);
if (gnc_key_file_save_to_file(pathname, key_file, &error))
{
if (!gnc_prefs_get_bool(GNC_PREFS_GROUP, GNC_PREF_PRINT_DATE_FMT))
/* Reload the format combo box and reselect the "custom" entry */
initialize_format_combobox(pcd);
gtk_combo_box_set_active(GTK_COMBO_BOX(pcd->format_combobox),
pcd->format_max - 1);
}
else
{
dialog = gtk_message_dialog_new(GTK_WINDOW(pcd->dialog),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE, "%s",
_("Cannot save check format file."));
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
_("Cannot open file %s"),
_(error->message));
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
g_error_free(error);
}
g_free(pathname);
g_free(filename);
}
/* This function makes the OK button active iff a title has been entered.
*/
void
gnc_check_format_title_changed (GtkEditable *editable, GtkWidget *ok_button)
{
const gchar *text;
gboolean sensitive;
text = gtk_entry_get_text(GTK_ENTRY(editable));
sensitive = text && *text;
gtk_widget_set_sensitive(ok_button, sensitive);
}
/* This function is called when the user clicks the "save format" button in
* the check printing dialog. It presents another dialog to the user to get
* the filename for saving the data.
*/
void
gnc_print_check_save_button_clicked(GtkButton *unused, PrintCheckDialog *pcd)
{
GtkWidget *dialog, *entry, *button;
GtkBuilder *builder;
gchar *title;
builder = gtk_builder_new();
gnc_builder_add_from_file (builder, "dialog-print-check.glade", "format_title_dialog");
/* Get a title for the new check format. */
dialog = GTK_WIDGET(gtk_builder_get_object (builder, "format_title_dialog"));
entry = GTK_WIDGET(gtk_builder_get_object (builder, "format_title"));
button = GTK_WIDGET(gtk_builder_get_object (builder, "ok_button"));
gnc_check_format_title_changed(GTK_EDITABLE(entry), button);
gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, pcd);
gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(pcd->dialog));
if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_OK)
{
gtk_widget_destroy(dialog);
g_object_unref(G_OBJECT(builder));
return;
}
title = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
gtk_widget_destroy (dialog);
g_object_unref(G_OBJECT(builder));
pcd_save_custom_data(pcd, title);
g_free(title);
}
/* This function is an auxiliary debugging function for converting an array of
* doubles into a printable string.
*/
static gchar *
doubles_to_string(gdouble *dd, gint len)
{
GString *str;
gint i;
str = g_string_new_len(NULL, 50);
for (i = 0; i < len; i++)
g_string_append_printf(str, "%f ", dd[i]);
return g_string_free(str, FALSE);
}
/* This function reads in the information describing the placement for each
* item to be printed on a check. This information is all relative to the
* upper left hand corner of a "check". See the format_read_multicheck_info()
* function for determining if there are multiple checks on a single page of
* paper. This data is build into a linked list and saved as part of the check
* format information. These items will be printed in the same order they are
* read, meaning that items listed later in the date file can be printed over
* top of items that appear earlier in the file.
*/
static GSList *
format_read_item_placement(const gchar *file,
GKeyFile *key_file, check_format_t *format)
{
check_item_t *data = NULL;
GError *error = NULL;
GSList *list = NULL;
gchar *key, *value, *name;
int item_num;
gboolean bval;
gdouble *dd;
gsize dd_len;
/* Read until failure. */
for (item_num = 1;; item_num++)
{
/* Create the per-item data structure */
data = g_new0(check_item_t, 1);
if (NULL == data)
return list;
/* Get the item type */
key = g_strdup_printf("%s_%d", KF_KEY_TYPE, item_num);
value = g_key_file_get_string(key_file, KF_GROUP_ITEMS, key, &error);
if (error)
{
if ((error->domain == G_KEY_FILE_ERROR)
&& (error->code == G_KEY_FILE_ERROR_KEY_NOT_FOUND))
{
/* This is the expected exit from this function. */
goto cleanup;
}
goto failed;
}
DEBUG("Check file %s, group %s, key %s, value: %s",
file, KF_GROUP_ITEMS, key, value);
g_free(key);
/* Convert the type from a string to an enum, ignoring case. */
name = g_utf8_strup(value, -1);
data->type = CheckItemTypefromString(name);
g_free(name);
g_free(value);
/* Get the item location */
key = g_strdup_printf("%s_%d", KF_KEY_COORDS, item_num);