/
options.scm
2198 lines (2004 loc) · 81.4 KB
/
options.scm
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
;; Scheme code for supporting options
;;
;; 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
(define-module (gnucash app-utils options))
(eval-when (compile load eval expand)
(load-extension "libgnc-app-utils" "scm_init_sw_app_utils_module"))
(use-modules (gnucash core-utils))
(use-modules (gnucash engine))
(use-modules (sw_app_utils))
(use-modules (gnucash app-utils date-utilities))
(use-modules (gnucash utilities))
(use-modules (srfi srfi-1))
(use-modules (ice-9 regex))
(export gnc:color->html)
(export gnc:color-option->hex-string)
(export gnc:color-option->html)
(export gnc:currency-accounting-option-get-curr-doc-string)
(export gnc:currency-accounting-option-get-default-curr)
(export gnc:currency-accounting-option-get-default-policy)
(export gnc:currency-accounting-option-get-gain-loss-account-doc-string)
(export gnc:currency-accounting-option-get-policy-doc-string)
(export gnc:currency-accounting-option-selected-currency)
(export gnc:currency-accounting-option-selected-gain-loss-account)
(export gnc:currency-accounting-option-selected-method)
(export gnc:currency-accounting-option-selected-policy)
(export gnc:date-option-absolute-time)
(export gnc:date-option-get-subtype)
(export gnc:date-option-relative-time)
(export gnc:date-option-show-time?)
(export gnc:date-option-value-type)
(export gnc:dateformat-get-format)
(export gnc:generate-restore-forms)
(export gnc:get-rd-option-data-rd-list)
(export gnc:get-rd-option-data-show-time)
(export gnc:get-rd-option-data-subtype)
(export gnc:lookup-option)
(export gnc:make-account-list-limited-option)
(export gnc:make-account-list-option)
(export gnc:make-account-sel-limited-option)
(export gnc:make-account-sel-option)
(export gnc:make-budget-option)
(export gnc:make-color-option)
(export gnc:make-commodity-option)
(export gnc:make-complex-boolean-option)
(export gnc:make-currency-option)
(export gnc:make-date-option)
(export gnc:make-dateformat-option)
(export gnc:make-font-option)
(export gnc:make-internal-option)
(export gnc:make-list-option)
(export gnc:make-multichoice-callback-option)
(export gnc:make-multichoice-option)
(export gnc:make-number-plot-size-option)
(export gnc:make-number-range-option)
(export gnc:make-option)
(export gnc:make-pixmap-option)
(export gnc:make-query-option)
(export gnc:make-radiobutton-callback-option)
(export gnc:make-radiobutton-option)
(export gnc:make-simple-boolean-option)
(export gnc:make-string-option)
(export gnc:make-text-option)
(export gnc:multichoice-list-lookup)
(export gnc:new-options)
(export gnc:option-data)
(export gnc:option-data-fns)
(export gnc:option-default-getter)
(export gnc:option-default-value)
(export gnc:option-documentation)
(export gnc:option-generate-restore-form)
(export gnc:option-get-value)
(export gnc:option-getter)
(export gnc:option-index-get-name)
(export gnc:option-index-get-value)
(export gnc:option-kvp->scm)
(export gnc:option-make-internal!)
(export gnc:option-name)
(export gnc:option-number-of-indices)
(export gnc:option-scm->kvp)
(export gnc:option-section)
(export gnc:option-set-changed-callback)
(export gnc:option-set-default-value)
(export gnc:option-set-value)
(export gnc:option-setter)
(export gnc:option-sort-tag)
(export gnc:option-strings-getter)
(export gnc:option-type)
(export gnc:option-value)
(export gnc:option-value-get-index)
(export gnc:option-value-validator)
(export gnc:option-widget-changed-proc)
(export gnc:options-clear-changes)
(export gnc:options-copy-values)
(export gnc:options-for-each)
(export gnc:options-for-each-general)
(export gnc:options-get-default-section)
(export gnc:options-kvp->scm)
(export gnc:options-make-date-interval!)
(export gnc:options-make-end-date!)
(export gnc:options-register-c-callback)
(export gnc:options-register-callback)
(export gnc:options-run-callbacks)
(export gnc:options-scm->kvp)
(export gnc:options-set-default-section)
(export gnc:options-touch)
(export gnc:options-unregister-callback-id)
(export gnc:plot-size-option-value)
(export gnc:plot-size-option-value-type)
(export gnc:register-option)
(export gnc:restore-form-generator)
(export gnc:send-options)
(export gnc:set-option-kvp->scm)
(export gnc:set-option-scm->kvp)
(export gnc:unregister-option)
(export gnc:value->string)
(export gnc:*option-name-trading-accounts*)
(export gnc:*option-name-book-currency*)
(export gnc:*option-section-accounts*)
(export gnc:*option-name-default-gains-policy*)
(export gnc:*option-name-default-gain-loss-account*)
(define gnc:*option-section-accounts* OPTION-SECTION-ACCOUNTS)
(define gnc:*option-name-trading-accounts* OPTION-NAME-TRADING-ACCOUNTS)
(define gnc:*option-name-currency-accounting* OPTION-NAME-CURRENCY-ACCOUNTING)
(define gnc:*option-name-book-currency* OPTION-NAME-BOOK-CURRENCY)
(define gnc:*option-name-default-gains-policy* OPTION-NAME-DEFAULT-GAINS-POLICY)
(define gnc:*option-name-default-gain-loss-account*
OPTION-NAME-DEFAULT-GAINS-LOSS-ACCT-GUID)
(define (gnc:option-get-value book category key)
(define acc (if (pair? key) cons list))
(qof-book-get-option book (acc category key)))
(define (rpterror-earlier type newoption fallback)
;; Translators: the 3 ~a below refer to (1) option type (2) unknown
;; new option name, (3) fallback option name. The order is
;; important, and must not be changed.
(let* ((template (N_ "This report was saved using a later version of \
GnuCash. One of the newer ~a options '~a' is not available, fallback to \
the option '~a'."))
(console-msg (format #f template type newoption fallback))
(ui-msg (format #f (G_ template) type newoption fallback)))
(gnc:gui-warn console-msg ui-msg)))
(define (gnc:make-option
;; The category of this option
section
name
;; The sort-tag determines the relative ordering of options in
;; this category. It is used by the gui for display.
sort-tag
type
documentation-string
getter
;; The setter is responsible for ensuring that the value is valid.
setter
default-getter
;; Restore form generator should generate an ascii representation
;; of a function taking one argument. The argument will be an
;; option. The function should restore the option to the original
;; value.
generate-restore-form
;; the scm->kvp and kvp->scm functions should save and load
;; the option to the book. The arguments to these function will be
;; a book and a base key-path list for this option.
scm->kvp
kvp->scm
;; Validation func should accept a value and return (#t value)
;; on success, and (#f "failure-message") on failure. If #t,
;; the supplied value will be used by the gui to set the option.
value-validator
;;; free-form storage depending on type.
option-data
;; If this is a "multiple choice" type of option,
;; this should be a vector of the following five functions:
;;
;; Function 1: taking no arguments, giving the number of choices
;;
;; Function 2: taking one argument, a non-negative integer, that
;; returns the scheme value (usually a symbol) matching the
;; nth choice
;;
;; Function 3: taking one argument, a non-negative integer,
;; that returns the string matching the nth choice
;;
;; Function 4: #f, this was the individual tool tip and not used now
;;
;; Function 5: giving a possible value and returning the index
;; if an option doesn't use these, this should just be a #f
option-data-fns
;; This function should return a list of all the strings
;; in the option other than the section, name, (define
;; (list-lookup list item) and documentation-string that
;; might be displayed to the user (and thus should be
;; translated).
strings-getter
;; This function will be called when the GUI representation
;; of the option is changed. This will normally occur before
;; the setter is called, because setters are only called when
;; the user selects "OK" or "Apply". Therefore, this
;; callback shouldn't be used to make changes to the actual
;; options database.
option-widget-changed-proc)
(let ((changed-callback #f))
(vector section
name
sort-tag
type
documentation-string
getter
(lambda args
(apply setter args)
(if changed-callback (changed-callback)))
default-getter
generate-restore-form
scm->kvp
kvp->scm
value-validator
option-data
option-data-fns
(lambda (callback) (set! changed-callback callback))
strings-getter
option-widget-changed-proc)))
(define (gnc:option-section option)
(vector-ref option 0))
(define (gnc:option-name option)
(vector-ref option 1))
(define (gnc:option-sort-tag option)
(vector-ref option 2))
(define (gnc:option-type option)
(vector-ref option 3))
(define (gnc:option-documentation option)
(vector-ref option 4))
(define (gnc:option-getter option)
(vector-ref option 5))
(define (gnc:option-setter option)
(vector-ref option 6))
(define (gnc:option-default-getter option)
(vector-ref option 7))
(define (gnc:option-generate-restore-form option)
(vector-ref option 8))
(define (gnc:option-scm->kvp option)
(vector-ref option 9))
(define (gnc:set-option-scm->kvp option v)
(vector-set! option 9 v))
(define (gnc:option-kvp->scm option)
(vector-ref option 10))
(define (gnc:set-option-kvp->scm option v)
(vector-set! option 10 v))
(define (gnc:option-value-validator option)
(vector-ref option 11))
(define (gnc:option-data option)
(vector-ref option 12))
(define (gnc:option-data-fns option)
(vector-ref option 13))
(define (gnc:option-set-changed-callback option callback)
(let ((cb-setter (vector-ref option 14)))
(cb-setter callback)))
(define (gnc:option-strings-getter option)
(vector-ref option 15))
(define (gnc:option-widget-changed-proc option)
(vector-ref option 16))
(define (gnc:option-value option)
(let ((getter (gnc:option-getter option)))
(getter)))
(define (gnc:option-set-value option value)
(let ((setter (gnc:option-setter option)))
(setter value)))
(define (gnc:option-index-get-name option index)
(let* ((option-data-fns (gnc:option-data-fns option))
(name-fn (vector-ref option-data-fns 2)))
(name-fn index)))
(define (gnc:option-index-get-value option index)
(let* ((option-data-fns (gnc:option-data-fns option))
(name-fn (vector-ref option-data-fns 1)))
(name-fn index)))
(define (gnc:option-value-get-index option value)
(let* ((option-data-fns (gnc:option-data-fns option))
(name-fn (vector-ref option-data-fns 4)))
(name-fn value)))
(define (gnc:option-number-of-indices option)
(let* ((option-data-fns (gnc:option-data-fns option))
(name-fn (vector-ref option-data-fns 0)))
(name-fn)))
(define (gnc:option-default-value option)
(let ((getter (gnc:option-default-getter option)))
(getter)))
;; Attention: this function can only be used with restrictions
;; - only during option generation, not in arbitrary code
;; - only for option types for which no conversion is required
;; between default-value and value. In the various gnc:make-option
;; functions below this is ok when
;; 1. there's (value default-value) in the let* call
;; 2. default-getter is set to (lambda() default-value)
(define (gnc:option-set-default-value option default-value)
(vector-set! option 7 (lambda() default-value))
(gnc:option-set-value option default-value))
(define (gnc:restore-form-generator value->string)
(lambda ()
(string-append "(lambda (o) (if o (gnc:option-set-value o "
(value->string) ")))")))
(define (gnc:value->string value)
(format #f "~s" value))
(define (gnc:make-string-option
section
name
sort-tag
documentation-string
default-value)
(let* ((value default-value)
(value->string (lambda () (gnc:value->string value))))
(gnc:make-option
section name sort-tag 'string documentation-string
(lambda () value)
(lambda (x) (set! value x))
(lambda () default-value)
(gnc:restore-form-generator value->string)
(lambda (b p) (qof-book-set-option b value p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(set! value v))))
(lambda (x)
(cond ((string? x)(list #t x))
(else (list #f "string-option: not a string"))))
#f #f #f #f)))
(define (gnc:make-text-option
section
name
sort-tag
documentation-string
default-value)
(let* ((value default-value)
(value->string (lambda () (gnc:value->string value))))
(gnc:make-option
section name sort-tag 'text documentation-string
(lambda () value)
(lambda (x) (set! value x))
(lambda () default-value)
(gnc:restore-form-generator value->string)
(lambda (b p) (qof-book-set-option b value p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(set! value v))))
(lambda (x)
(cond ((string? x)(list #t x))
(else (list #f "text-option: not a string"))))
#f #f #f #f)))
;;; font options store fonts as strings a la the X Logical
;;; Font Description. You should always provide a default
;;; value, as currently there seems to be no way to go from
;;; an actual font to a logical font description, and thus
;;; there is no way for the gui to pick a default value.
(define (gnc:make-font-option
section
name
sort-tag
documentation-string
default-value)
(let* ((value default-value)
(value->string (lambda () (gnc:value->string value))))
(gnc:make-option
section
name
sort-tag
'font
documentation-string
(lambda () value)
(lambda (x) (set! value x))
(lambda () default-value)
(gnc:restore-form-generator value->string)
(lambda (b p) (qof-book-set-option b value p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(set! value v))))
(lambda (x)
(cond ((string? x)(list #t x))
(else (list #f "font-option: not a string"))))
#f #f #f #f)))
;; currency options use a specialized widget for entering currencies
;; in the GUI implementation.
(define (gnc:make-currency-option
section
name
sort-tag
documentation-string
default-value)
(define (currency->scm currency)
(if (string? currency)
currency
(gnc-commodity-get-mnemonic currency)))
(define (scm->currency currency)
(if (string? currency)
(gnc-commodity-table-lookup
(gnc-commodity-table-get-table (gnc-get-current-book))
(GNC-COMMODITY-NS-CURRENCY) currency)
currency))
(let* ((value (currency->scm default-value))
(value->string (lambda () (gnc:value->string value))))
(gnc:make-option
section name sort-tag 'currency documentation-string
(lambda () (scm->currency value))
(lambda (x) (set! value (currency->scm x)))
(lambda () (scm->currency default-value))
(gnc:restore-form-generator value->string)
(lambda (b p) (qof-book-set-option b value p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(set! value v))))
(lambda (x) (list #t x))
#f #f #f #f)))
;; budget option
;; TODO: need to double-check this proc (dates back to r11545 or eariler)
;;
;; Always takes/returns a budget
;; Stores the GUID in the KVP
;;
(define (gnc:make-budget-option
section
name
sort-tag
documentation-string)
(define (convert-to-guid item)
(if (string? item) item (gncBudgetGetGUID item)))
(define (convert-to-budget item)
(if (string? item) (gnc-budget-lookup item (gnc-get-current-book)) item))
(let* ((initial-budget (gnc-budget-get-default (gnc-get-current-book)))
(selection-budget (convert-to-guid initial-budget)))
(gnc:make-option
section
name
sort-tag
'budget
documentation-string
;; getter -- Return a budget pointer
(lambda ()
(convert-to-budget selection-budget))
;; setter -- takes a budget
(lambda (x)
(set! selection-budget (convert-to-guid x)))
;; default-getter
;; Default now is #f so saving is independent of book-level default
(lambda ()
#f)
;; generate-restore-form
;; "return 'ascii represention of a function'
;; that will set the option passed as its lone parameter
;; to the value it was when the picker was first displayed"
;;
;; *This* is used to restore reports, not the KVP -- and is stored as text
;; This does not run in closure with direct access to the option's
;; internal variables, so the setter generally gets used
(lambda ()
(string-append
"(lambda (option) "
"(if option ((gnc:option-setter option) "
"(gnc-budget-lookup "
(gnc:value->string selection-budget)
" (gnc-get-current-book)))))"))
;; scm->kvp -- commit the change
;; b -- book; p -- key-path
(lambda (b p)
(qof-book-set-option b selection-budget p))
;; kvp->scm -- get the stored value
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(set! selection-budget (convert-to-guid
(gnc-budget-lookup v (gnc-get-current-book)))))))
;; value-validator -- returns (#t value) or (#f "failure message")
;; As no user-generated input, this legacy hard-wire is probably ok
(lambda (x)
(list #t x))
;; option-data
#f
;; option-data-fns -- used for multi-pick (this isn't one), or #f
;; Vector of five functions
;; 1) () => number of choices
;; 2) (n) => key for the nth choice
;; 3) (n) => string for the nth choice
;; 4) (n) => description for the nth choice
;; 5) (key) => n (assuming this is the reverse key lookup)
#f
;; strings-getter -- list of all translatable strings in the option
#f
;; options-widget-changed-proc -- callback for what it sounds like
#f
))) ;; completes gnc:make-budget-option
;; commodity options use a specialized widget for entering commodities
;; in the GUI implementation.
(define (gnc:make-commodity-option
section
name
sort-tag
documentation-string
default-value)
(define (commodity->scm commodity)
(if (string? commodity)
(list 'commodity-scm
(GNC-COMMODITY-NS-CURRENCY)
commodity)
(list 'commodity-scm
(gnc-commodity-get-namespace commodity)
(gnc-commodity-get-mnemonic commodity))))
(define (scm->commodity scm)
(gnc-commodity-table-lookup
(gnc-commodity-table-get-table (gnc-get-current-book))
(cadr scm) (caddr scm)))
(let* ((value (commodity->scm default-value))
(value->string (lambda ()
(string-append "'" (gnc:value->string value)))))
(gnc:make-option
section name sort-tag 'commodity documentation-string
(lambda () (scm->commodity value))
(lambda (x) (if (and (pair? x) (eqv? (car x) 'commodity-scm))
(set! value x)
(set! value (commodity->scm x))))
(lambda () default-value)
(gnc:restore-form-generator value->string)
(lambda (b p)
(qof-book-set-option b (cadr value) (append p '("ns")))
(qof-book-set-option b (caddr value) (append p '("monic"))))
(lambda (b p)
(let ((ns (qof-book-get-option b (append p '("ns"))))
(monic (qof-book-get-option b (append p '("monic")))))
(if (and ns monic (string? ns) (string? monic))
(set! value (list 'commodity-scm ns monic)))))
(lambda (x) (list #t x))
#f #f #f #f)))
(define (gnc:make-simple-boolean-option
section
name
sort-tag
documentation-string
default-value)
(gnc:make-complex-boolean-option section
name
sort-tag
documentation-string
default-value
#f
#f))
;; Complex boolean options are the same as simple boolean options (see
;; above), with the addition of two function arguments. (If both of
;; them are #f, you have exactly a simple-boolean-option.) Both
;; functions should expect one boolean argument. When the option's
;; value is changed, the function option-widget-changed-cb will be
;; called with the new option value at the time that the GUI widget
;; representing the option is changed, and the function
;; setter-function-called-cb will be called when the option's setter
;; is called (that is, when the user selects "OK" or "Apply").
;; The option-widget-changed-cb is tested for procedurehood before
;; it is called, so it is not validated to be a procedure here.
;; However, since there could be an option-widget-changed-cb but not
;; a setter-function-called-cb, the procedurehood of the
;; setter-function-called-cb is checked here.
(define (gnc:make-complex-boolean-option
section
name
sort-tag
documentation-string
default-value
setter-function-called-cb
option-widget-changed-cb)
(let* ((value default-value)
(value->string (lambda () (gnc:value->string value))))
(gnc:make-option
section name sort-tag 'boolean documentation-string
(lambda () value)
(lambda (x) (set! value x)
(if (procedure? setter-function-called-cb)
(setter-function-called-cb x)))
(lambda () default-value)
(gnc:restore-form-generator value->string)
(lambda (b p) (qof-book-set-option b
;; As no boolean KvpValue exists, as a workaround
;; we store the string "t" for TRUE and "f" for
;; FALSE in a string KvpValue.
(if value "t" "f")
p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
;; As no boolean KvpValue exists, as a workaround we store
;; the string "t" for TRUE and "f" for FALSE.
(cond ((equal? v "t") (set! v #t))
((equal? v "f") (set! v #f)))
(if (and v (boolean? v) (not (equal? v default-value)))
(set! value v))))
(lambda (x)
(if (boolean? x)
(list #t x)
(list #f "boolean-option: not a boolean")))
#f #f #f (and option-widget-changed-cb
(lambda (x) (option-widget-changed-cb x))))))
(define (gnc:make-pixmap-option
section name sort-tag doc-string
default-value)
(let* ((value default-value))
(gnc:make-option
section name sort-tag 'pixmap doc-string
(lambda () value)
(lambda (x) (set! value x))
(lambda () default-value)
(gnc:restore-form-generator (lambda () (gnc:value->string value)))
#f
#f
(lambda (x)
(if (string? x)
(begin
(list #t x))
(begin
(list #f "pixmap-option: not a string"))))
#f #f #f #f)))
;; show-time is boolean
;; subtype should be one of 'relative 'absolute or 'both
;; if subtype is 'absolute then relative-date-list should be #f
;; relative-date-list should be the list of relative dates permitted
;; gnc:all-relative-dates contains a list of all relative dates.
(define (gnc:make-date-option
section
name
sort-tag
documentation-string
default-getter
show-time
subtype
relative-date-list)
(define (date-legal date)
(and (pair? date)
(or
(and (eq? 'relative (car date)) (symbol? (cdr date)))
(and (eq? 'absolute (car date))
(or (and (pair? (cdr date)) ; we can still accept
(exact? (cadr date)) ; old-style timepairs
(exact? (cddr date)))
(and (number? (cdr date))
(exact? (cdr date))))))))
(define (maybe-convert-to-time64 date)
;; compatibility shim. this is triggered only when date is type
;; (cons 'absolute (cons sec nsec)) - we'll convert to
;; (cons 'absolute sec). this shim must always be kept for gnucash
;; to reload saved reports, or reload reports launched at startup,
;; which had been saved as timepairs.
(if (pair? (cdr date))
(cons (car date) (cadr date))
date))
(define (list-lookup full-list item)
(or (list-index (lambda (i) (eq? i item)) full-list)
(begin
(rpterror-earlier "date" item (car full-list))
0)))
(if show-time
(issue-deprecation-warning
(format #f "Date options with time of day values are deprecated and will be removed in GnuCash 5.")))
(let* ((value (default-getter))
(value->string (lambda ()
(string-append "'" (gnc:value->string value)))))
(gnc:make-option
section name sort-tag 'date documentation-string
(lambda () value)
(lambda (date)
(if (date-legal date)
(set! value (maybe-convert-to-time64 date))
(gnc:error "Illegal date value set:" date)))
default-getter
(gnc:restore-form-generator value->string)
(lambda (b p)
(qof-book-set-option b (symbol->string (car value))
(append p '("type")))
(qof-book-set-option b
(if (symbol? (cdr value))
(symbol->string (cdr value))
(cdr value))
(append p '("value"))))
(lambda (b p)
(let ((t (qof-book-get-option b (append p '("type"))))
(v (qof-book-get-option b (append p '("value")))))
(if (and t v (string? t))
(set! value (cons (string->symbol t)
(if (string? v) (string->symbol v) v))))))
(lambda (date)
(if (date-legal date)
(list #t date)
(list #f "date-option: illegal date")))
(vector subtype show-time relative-date-list)
(vector (lambda () (length relative-date-list))
(lambda (x) (list-ref relative-date-list x))
(lambda (x) (gnc:get-relative-date-string
(list-ref relative-date-list x)))
(lambda (x) (gnc:get-relative-date-desc
(list-ref relative-date-list x)))
(lambda (x) (list-lookup relative-date-list x)))
#f
#f)))
(define (gnc:get-rd-option-data-subtype option-data)
(vector-ref option-data 0))
(define (gnc:get-rd-option-data-show-time option-data)
(vector-ref option-data 1))
(define (gnc:get-rd-option-data-rd-list option-data)
(vector-ref option-data 2))
(define (gnc:date-option-get-subtype option)
(if (eq? (gnc:option-type option) 'date)
(gnc:get-rd-option-data-subtype (gnc:option-data option))
(gnc:error "Not a date option")))
(define (gnc:date-option-show-time? option)
(if (eq? (gnc:option-type option) 'date)
(gnc:get-rd-option-data-show-time (gnc:option-data option))
(gnc:error "Not a date option")))
(define (gnc:date-option-value-type option-value)
(car option-value))
(define (gnc:date-option-absolute-time option-value)
(if (eq? (car option-value) 'absolute)
(cdr option-value)
(gnc:get-absolute-from-relative-date (cdr option-value))))
(define (gnc:date-option-relative-time option-value)
(if (eq? (car option-value) 'absolute)
#f
(cdr option-value)))
;; Just like gnc:make-account-list-limited-option except it
;; does not limit the types of accounts that are available
;; to the user.
(define (gnc:make-account-list-option
section
name
sort-tag
documentation-string
default-getter
value-validator
multiple-selection)
(gnc:make-account-list-limited-option
section name sort-tag documentation-string
default-getter value-validator multiple-selection '()))
;; account-list options use the option-data as a pair; the car is
;; a boolean value, the cdr is a list of account-types. If the boolean is
;; true, the gui should allow the user to select multiple accounts.
;; If the cdr is an empty list, then all account types are shown.
;; Internally, values are always a list of guids. Externally, both
;; guids and account pointers may be used to set the value of the
;; option. The option always returns a list of account pointers.
(define (gnc:make-account-list-limited-option
section
name
sort-tag
documentation-string
default-getter
value-validator
multiple-selection
acct-type-list)
(define (convert-to-guid item)
(if (string? item)
item
(gncAccountGetGUID item)))
(define (convert-to-account item)
(if (string? item)
(xaccAccountLookup item (gnc-get-current-book))
item))
(let* ((option (map convert-to-guid (default-getter)))
(option-set #f)
(getter (lambda () (map convert-to-account
(if option-set
option
(default-getter)))))
(value->string (lambda ()
(string-append
"'" (gnc:value->string (if option-set option #f)))))
(validator
(if (not value-validator)
(lambda (account-list) (list #t account-list))
(lambda (account-list)
(value-validator (map convert-to-account account-list))))))
(gnc:make-option
section name sort-tag 'account-list documentation-string getter
(lambda (account-list)
(if (or (not account-list) (null? account-list))
(set! account-list (default-getter)))
(set! account-list
(filter (lambda (x) (if (string? x)
(xaccAccountLookup
x (gnc-get-current-book))
x)) account-list))
(let* ((result (validator account-list))
(valid (car result))
(value (cadr result)))
(if valid
(begin
(set! option (map convert-to-guid value))
(set! option-set #t))
(gnc:error "Illegal account list value set"))))
(lambda () (map convert-to-account (default-getter)))
(gnc:restore-form-generator value->string)
(lambda (b p)
(when option-set
(qof-book-set-option b (length option) (append p '("len")))
(let loop ((option option) (idx 0))
(unless (null? option)
(qof-book-set-option
b (car option) (append p (list (format #f "acc~a" idx))))
(loop (cdr option) (1+ idx))))))
(lambda (b p)
(let ((len (qof-book-get-option b (append p '("len")))))
(when (and len (integer? len))
(set! option
(map
(lambda (idx)
(qof-book-get-option b (append p (list (format #f "acc~a" idx)))))
(iota len)))
(set! option-set #t))))
validator
(cons multiple-selection acct-type-list) #f #f #f)))
;; Just like gnc:make-account-sel-limited-option except it
;; does not limit the types of accounts that are available
;; to the user.
(define (gnc:make-account-sel-option
section
name
sort-tag
documentation-string
default-getter
value-validator)
(gnc:make-account-sel-limited-option
section name sort-tag documentation-string
default-getter value-validator '()))
;; account-sel options use the option-data as a pair; the car is
;; ignored, the cdr is a list of account-types. If the cdr is an empty
;; list, then all account types are shown. Internally, the value is
;; always a guid. Externally, both guids and account pointers may be
;; used to set the value of the option. The option always returns the
;; "current" account pointer.
(define (gnc:make-account-sel-limited-option
section
name
sort-tag
documentation-string
default-getter
value-validator
acct-type-list)
(define (convert-to-guid item)
(if (string? item)
item
(gncAccountGetGUID item)))
(define (convert-to-account item)
(if (string? item)
(xaccAccountLookup item (gnc-get-current-book))
item))
(define (find-first-account)
(define (find-first account-list)
(if (null? account-list)
'()
(let* ((this-account (car account-list))
(account-type (xaccAccountGetType this-account)))
(if (if (null? acct-type-list)
#t
(member account-type acct-type-list))
this-account
(find-first (cdr account-list))))))
(let* ((current-root (gnc-get-current-root-account))
(account-list (gnc-account-get-descendants-sorted current-root)))
(find-first account-list)))
(define (get-default)
(if default-getter
(default-getter)
(find-first-account)))
(let* ((option (convert-to-guid (get-default)))
(option-set #f)
(getter (lambda () (convert-to-account
(if option-set
option
(get-default)))))
(value->string (lambda ()
(string-append
(gnc:value->string (if option-set option #f)))))
(validator
(if (not value-validator)
(lambda (account) (list #t account))
(lambda (account)
(value-validator (convert-to-account account))))))
(gnc:make-option
section name sort-tag 'account-sel documentation-string getter
(lambda (account)
(if (or (not account) (null? account)) (set! account (get-default)))
(set! account (convert-to-account account))
(let* ((result (validator account))
(valid (car result))
(value (cadr result)))
(if valid
(begin
(set! option (convert-to-guid value))
(set! option-set #t))
(gnc:error "Illegal account value set"))))
(lambda () (convert-to-account (get-default)))
(gnc:restore-form-generator value->string)
(lambda (b p) (qof-book-set-option b option p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(set! option v))))
validator
(cons #f acct-type-list) #f #f #f)))
(define (gnc:multichoice-list-lookup full-lst item)
(or (list-index (lambda (i) (eq? (vector-ref i 0) item)) full-lst)
(begin