-
Notifications
You must be signed in to change notification settings - Fork 0
/
boilermatic.tcl
executable file
Β·1331 lines (1184 loc) Β· 49.9 KB
/
boilermatic.tcl
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
#!/usr/bin/env tclsh
###
# Copyright 2013 Matthew Harvey
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###
##############################################################################
# Dependencies
package require Tk
package require Ttk
##############################################################################
# Platform specific formatting details are contained in this namespace.
namespace eval platform {
# INTERFACE
# No parameters.
# Returns a list of alternating options/values to be used with Tk
# (not Ttk) widgets to adjust background colour to account for certain
# platform-specific peculiarities.
# Expand returned list with {*} and pass to widget creation command.
namespace export get_background_options
# No parameters.
# Returns a list of alternating options/values to be used with Tk text
# widget to adjust for certain platform-specific peculiarities.
# Expend returned list with {*} and pass to text widget creation command.
namespace export get_text_widget_options
# IMPLEMENTATION DETAILS
variable background [list]
variable text_widget_options [list]
if {[string equal $tcl_platform(os) Darwin]} {
# OSX
variable darwin_background_color "#ececec"
set background [list -background $darwin_background_color]
set text_widget_options \
[list -borderwidth 2 -relief sunken -highlightbackground \
$darwin_background_color -highlightcolor $darwin_background_color]
. configure {*}$background ;# Yes that's a dot - the topmost window.
}
proc get_background_options {} {
variable background
return $background
}
proc get_text_widget_options {} {
variable text_widget_options
return $text_widget_options
}
}
##############################################################################
# Standard widths and padding details are managed in this namespace.
namespace eval sizing {
# INTERFACE
# No parameters.
# Returns an alternating option/value list specifying our standard width
# for a "narrow" widget.
# Expand returned list with {*} and pass to widget creation command.
namespace export get_narrow_option
# No parameters.
# Returns an alternating option/value list specifying our standard width
# for a "wide" widget.
# Expand returned list with {*} and pass to widget creation command.
namespace export get_wide_option
# No parameters
# Returns a list of alternating options/values specifying our standard
# padding values for widgets passed to "grid" command.
# Expand returned list with {*} and pass to grid command.
namespace export get_padding_options
# IMPLEMENTATION DETAILS
variable narrow_width 8
proc get_narrow_option {} {
variable narrow_width
return [list -width $narrow_width]
}
proc get_wide_option {} {
variable narrow_width
return [list -width [expr {$narrow_width * 3}]]
}
proc get_padding_options {} {
return [list -padx 10 -pady 10]
}
}
###############################################################################
# Options configured by the user via the contents of the .boilermatic directory
# are managed here in this namespace.
namespace eval configuration {
# INTERFACE
# Call to get the legal notice string (which will include appropriate
# comment-out symbols added).
namespace export get_legal_notice
# Call to return the filepath of the legal notice configuration
# file (or empty string if this file not found).
namespace export get_legal_notice_filepath
# IMPLEMENTATION DETAILS
variable legal_notice
variable is_initialized 0
proc get_config_dir {starting_dir} {
set dir [file normalize $starting_dir]
while {1} {
set tmp [file dirname $dir]
set conf_path [file join $dir ".boilermatic"]
if {[file exists $conf_path] && [file isdirectory $conf_path]} {
return $conf_path
}
if {[string equal $tmp $dir]} {
# We're at root
return ""
}
set dir $tmp
}
}
proc get_legal_notice_filepath {{starting_dir [pwd]}} {
set dir [get_config_dir [file normalize $starting_dir]]
if {[string equal $dir ""]} {
return ""
}
set fp [file join $dir "legal_notice"]
if {[file exists $fp] && [file isfile $fp]} {
return $fp
}
return ""
}
proc ensure_initialized {} {
variable is_initialized
if {!$is_initialized} {
set legal_notice_filepath [get_legal_notice_filepath [pwd]]
if {[string equal $legal_notice_filepath ""]} {
variable legal_notice ""
} else {
variable legal_notice ""
set infile [open $legal_notice_filepath r]
set lines_read 0
while {[gets $infile line] >= 0} {
incr lines_read
if {$lines_read == 1} {
append legal_notice "/*\n"
}
append legal_notice " * ${line}\n"
}
if {$lines_read > 0} {
append legal_notice " */\n"
}
close $infile
}
variable is_initialized 1
}
}
proc get_legal_notice {} {
ensure_initialized
variable legal_notice
return $legal_notice
}
}
##############################################################################
# Data associated with the various widgets is managed in this namespace.
# These data represent the "underlying data model" the details of which
# are configured by the user via the GUI.
namespace eval widget_contents {
# INTERFACE
# Call this procedure with no arguments to initialize data prior
# to setting up the GUI.
namespace export initialize
# Call with no arguments to return name of the C++ class for which
# code will be generated.
namespace export get_class_name
# Call with no arguments to return name of the C++ header file
# to be created.
namespace export get_header_name
# Call with no arguments.
# Returns 1 if user would like source (".cpp") file created; otherwise,
# returns 0.
namespace export get_whether_source_file_enabled
# Call with no arguments to return name of C++ source (".cpp") file
# to be created.
namespace export get_source_file_name
# Call with no arguments to return name of C++ preprocessor symbol
# to be used as header guard for ordinary header.
namespace export get_header_guard
# Call with no arguments to return path of directory in which
# C++ header will be saved.
namespace export get_header_directory
# Call with no arguments to return path of directory in which
# C++ source file will be saved.
namespace export get_source_file_directory
# Call with no arguments to return a dict.
# For each key/value pair in the dict, the "key"
# is the text of the C++ special function declaration as it will appear in
# the generated C++ source code, and the
# "value" is an access specifier ("public", "protected" or "private").
# The dict will only includes special member functions which for which the
# user has requested code to be generated.
namespace export get_special_member_function_details
# Call with no arguments.
# Returns a list of names of every special member function that may be
# configured by the user via the GUI. The "names" for this purpose
# are user-presentable strings, e.g. "Copy constructor".
namespace export get_special_member_function_labels
# Call with no arguments.
# Returns as a list of strings the "implementation specifiers" from
# which the user may select for each of the special member
# functions: e.g. "Custom".
namespace export get_implementation_specifiers
# Call with no arguments.
# Returns a list of access specifiers from the the user may
# select for each of the special member functions.
namespace export get_access_specifiers
# Call with no arguments.
# Returns 1 if the user has selected for the generated C++
# destructor to be virtual, otherwise returns 0.
namespace export get_whether_destructor_virtual
# Call with no argumets.
# Returns a list of strings, each of which describes a convention
# for indenting C++ code, from which the user may select.
namespace export get_indentation_styles
# Call with no arguments.
# Returns 1 if adding legal notice; otherwise, returs 0
namespace export get_whether_legal_notice_enabled
# Call with no arguments.
# Returns the actual string which will effect the indentation
# style currently selected by the user. E.g. " " (four spaces).
namespace export get_indentation_string
# Call with no arguments.
# Returns selected command for adding files to a version control system
# (VCS), as a list; returns an empty list if VCS command not
# selected.
namespace export get_vcs_command
# Call with no arguments to return a list of strings each of which
# is the symbol for a C++ namespace to be generated.
namespace export get_cpp_namespaces
# Call with no arguments to set the text of the header guard symbol.
# This is generated on the basis of the filename stem currently
# stored in the GUI, together with random digits appended to ensure
# the C++ header guard symbol will be (almost certainly) unique.
namespace export configure_header_guard
# RESTRICTED INTERFACE - TO BE ACCESSED BY ASSOCIATED WIDGETS ONLY
variable class_name
variable filename_stem
variable header_guard
variable source_file_enabled ;# boolean
variable header_directory
variable source_file_directory
variable should_generate_special_member_function ;# array
variable access_specifiers ;# list
variable implementation_spec ;# array
variable access_spec ;# array
variable destructor_is_virtual ;# boolean
variable should_generate_legal_notice ;# boolean
variable indentation_style ;# string
variable vcs_command ;# list
variable always_true ;# boolean - hack
proc set_cpp_namespaces {text_widget_contents} {
set raw_string $text_widget_contents
set raw_list [split $raw_string \n]
set tmp [list]
foreach elem $raw_list {
set trimmed_elem [string trim $elem]
if {[string length $trimmed_elem] != 0} {
lappend tmp $trimmed_elem
}
}
variable cpp_namespace_list $tmp
}
# IMPLEMENTATION DETAILS
variable cpp_namespace_list
variable special_member_functions
variable implementation_specifiers
proc initialize {} {
expr {srand([clock seconds])} ;# Seed random number generator
variable class_name ""
variable filename_stem ""
variable header_guard ""
variable source_file_enabled 1
variable header_directory [pwd]
variable source_file_directory [pwd]
if {[file isdirectory [file join [pwd] src]]} {
set source_file_directory [file join [pwd] src]
}
if {[file isdirectory [file join [pwd] include]]} {
set header_directory [file join [pwd] include]
}
variable destructor_is_virtual 1
variable special_member_functions \
[ dict create \
"Default constructor" {CLASS()} \
"Copy constructor" {CLASS(CLASS const& rhs)} \
"Move constructor" {CLASS(CLASS&& rhs)} \
"Copy assignment" {CLASS& operator=(CLASS const& rhs)} \
"Move assignment" {CLASS& operator=(CLASS&& rhs)} \
"Destructor" {~CLASS()} \
]
variable implementation_specifiers {"default" "delete" "custom"}
variable access_specifiers {"public" "protected" "private"}
variable should_generate_special_member_function
variable access_spec
variable implementation_spec
foreach smf [get_special_member_function_labels] {
set should_generate_special_member_function($smf) 1
set access_spec($smf) [lindex $access_specifiers 0]
set implementation_spec($smf) \
[lindex $implementation_specifiers 0]
}
variable cpp_namespace_list [list]
variable should_generate_legal_notice \
[expr {![string equal [configuration::get_legal_notice] ""]}]
variable indentation_style "Tab" ;#TODO Not cool
variable vcs_command [list]
variable always_true 1
}
proc get_header_extension {} {
return "hpp"
}
proc get_source_file_extension {} {
return "cpp"
}
proc get_class_name {} {
variable class_name
return [string trim $class_name]
}
proc get_header_name {} {
variable filename_stem
return "[string trim ${filename_stem}].[get_header_extension]"
}
proc get_whether_source_file_enabled {} {
variable source_file_enabled
return $source_file_enabled
}
proc get_source_file_name {} {
variable filename_stem
return "[string trim ${filename_stem}].[get_source_file_extension]"
}
proc get_header_guard {} {
variable header_guard
return [string trim $header_guard]
}
proc get_header_directory {} {
variable header_directory
return $header_directory
}
proc get_source_file_directory {} {
variable source_file_directory
return $source_file_directory
}
proc get_special_member_function_details {} {
variable should_generate_special_member_function
variable implementation_spec
variable access_spec
variable special_member_functions
set ret [dict create]
set cname [widget_contents::get_class_name]
if {[string length $cname] == 0} {
return $ret;
}
foreach {smf template} $special_member_functions {
if {$should_generate_special_member_function($smf)} {
regsub -all CLASS $template $cname declaration_text
set spec $implementation_spec($smf)
if {![string equal $spec "custom"]} {
append declaration_text " = $spec"
}
append declaration_text ";"
if {[string equal $smf "Destructor"]} {
if {[widget_contents::get_whether_destructor_virtual]} {
set declaration_text "virtual $declaration_text"
}
}
dict append ret $declaration_text $access_spec($smf)
}
incr r
}
return $ret
}
proc get_special_member_function_labels {} {
variable special_member_functions
return [dict keys $special_member_functions]
}
proc get_implementation_specifiers {} {
variable implementation_specifiers
return $implementation_specifiers
}
proc get_access_specifiers {} {
variable access_specifiers
return $access_specifiers
}
proc get_whether_destructor_virtual {} {
variable destructor_is_virtual
return $destructor_is_virtual
}
proc get_indentation_map {} {
set ret \
[ dict create \
"Tab" "\t"\
"2 spaces" { }\
"4 spaces" { }\
"8 spaces" { }\
]
return $ret
}
proc get_indentation_styles {} {
return [dict keys [get_indentation_map]]
}
proc get_whether_legal_notice_enabled {} {
variable should_generate_legal_notice
return $should_generate_legal_notice
}
proc get_indentation_string {} {
variable indentation_style
dict get [get_indentation_map] $indentation_style
}
proc get_vcs_command {} {
variable vcs_command
return $vcs_command
}
proc get_cpp_namespaces {} {
variable cpp_namespace_list
return $cpp_namespace_list
}
proc configure_header_guard {} {
variable header_guard
variable filename_stem
set header_guard [string trim $header_guard]
set fname_stem $filename_stem
if {[string equal $header_guard ""]} {
if {![string equal $fname_stem ""]} {
set tmp "GUARD_${fname_stem}_[get_header_extension]_"
set rand_num [expr rand()]
# Get rid of punctuation from the random number
regsub -all {[^0-9]} $rand_num {} rand_num
# Get rid of initial zero from random number
if {[string equal [string range $rand_num 0 0] "0"]} {
set rand_num [string range $rand_num 1 end]
}
# Shorten rand_num if it's very long and we can shorten
# it without losing too much randomness.
set column_limit 80
set expected_columns \
[string length "#ifndef ${tmp}${rand_num}"]
if {$expected_columns > $column_limit} {
if {[string length $rand_num] > 8} {
set rand_num [string range $rand_num end-7 end]
}
}
append tmp $rand_num
set header_guard $tmp
}
}
}
# Given a class name in Pascal case, returns an underscore-separated,
# all-lower-case filename stem based on that class name.
proc generate_filename_stem {class_name} {
set filename_subelements [list]
set current_subelement ""
foreach c [split $class_name {}] {
if {[string is upper $c]} {
lappend filename_subelements $current_subelement
if \
{([llength $current_subelement] > 0) && \
([string range $current_subelement end end] == "_") \
} {
# Do nothing, as we don't want two underscores in a row
} else {
lappend filename_subelements "_"
}
set current_subelement ""
}
append current_subelement [string tolower $c]
}
lappend filename_subelements $current_subelement
set ret [join $filename_subelements {}]
set ret [string trim $ret "_"]
return $ret
}
proc configure_filename_stem {p_class_name} {
variable filename_stem
if {![string equal $p_class_name ""] && \
[string equal $filename_stem ""]} {
set filename_stem [generate_filename_stem $p_class_name]
}
}
}
##############################################################################
# Configuration of the graphical user interface is managed in this namespace.
namespace eval gui {
# INTERFACE
# Call with no arguments to create the main window and populate it
# with all the widgets required for the application.
# This procedure in turn will do all other initialization required
# for the application.
namespace export setup_widgets
# IMPLEMENTATION DETAILS
# Window manager configuration
proc configure_main_window_title {title} {
wm title . $title
}
# Procedure to create and position a label and correponding ttk::entry
# widget.
# Parameters:
# label_text - text of the label appearing next to the widget;
# associated_variable - variable bound to the contents of the ttk::entry
# widget.
# row_num - row number in which grid geometry manager will position
# the widget and its label.
#
# Returns the path of the new ttk::entry widget.
proc setup_entry_widget {label_text associated_variable row_num} {
set l [label ".${row_num}_entry_label" -text $label_text \
{*}[platform::get_background_options]]
set e [ttk::entry ".${row_num}_entry" -textvariable \
$associated_variable {*}[sizing::get_wide_option] -cursor xterm]
grid $l -row $row_num -column 0 {*}[sizing::get_padding_options] \
-sticky e
grid $e -row $row_num -column 1 {*}[sizing::get_padding_options] \
-sticky we -columnspan 3
return $e
}
# Here follow several procedures for creating a widget or set of widgets
# and positioning them via the gridder on the main window. These
# procedures generally follow a pattern where they must be passed a row
# number - determining where they will be positioned on the grid - and
# return a row number, being the next available row for further widgets
# to be positioned.
# Setup entry widget for class name; return the next available row number.
proc setup_class_name_widget {row_num} {
set path [setup_entry_widget "Class name" \
widget_contents::class_name $row_num]
focus $path ;# Cursor starts here
# Setting the class name entry should cause the value of filename_stem
# to be configured, if not already.
bind $path <FocusOut> {
widget_contents::configure_filename_stem [string trim [%W get]]
widget_contents::configure_header_guard
}
return [incr row_num]
}
# Setup entry widget for filename stem; return the next available row
# number.
proc setup_filename_stem_widget {row_num} {
set path [setup_entry_widget "Filename stem" \
widget_contents::filename_stem $row_num]
# Setting the filename stem should cause the header guard symbol to be
# configured if not already.
bind $path <FocusOut> widget_contents::configure_header_guard
return [incr row_num]
}
# Setup entry widget for header guard; return the next available row
# number.
proc setup_header_guard_widget {row_num} {
set path [setup_entry_widget "Header guard" \
widget_contents::header_guard $row_num]
return [incr row_num]
}
# Returns widget paths to be used for a row of widgets for configuring
# header or source file directory (pass "header" or "source_file" to
# procedure).
proc directory_ctrl_paths {file_type} {
set ret [list]
for {set i 0} {$i != 3} {incr i} {
lappend ret .${file_type}_directory_widget_${i}
}
return $ret
}
# Toggle whether the widgets for controlling the source file saving
# directory, are enabled or disabled.
proc toggle_source_file_dir_widgets_enabled {} {
if [::widget_contents::get_whether_source_file_enabled] {
set s "normal"
} else {
set s "disabled"
}
foreach path [directory_ctrl_paths source_file] {
$path configure -state $s
}
}
# Setup widgets for selecting whether to create header and whether
# to create source file.
proc setup_file_creation_toggle_widgets {row_num} {
# Setup widget for selecting whether or not to create a header
# (".hpp" file). Note this is always selected and the user cannot
# change this. It is provided for consistency with the source file
# creation widget, which the user CAN toggle on or off.
# Note the code in cpp_code_generation namespace just assumes this
# will always be selected.
ttk::checkbutton .header_creation_checkbutton \
-text "Create header?" \
-state disabled \
-variable ::widget_contents::always_true
grid .header_creation_checkbutton -row $row_num -column 1 \
-sticky w {*}[sizing::get_padding_options]
# Setup widget for selecting whether or not to create a source
# (".cpp") file.
ttk::checkbutton .source_file_creation_checkbutton \
-text "Create source file?" \
-variable ::widget_contents::source_file_enabled \
-command "{*}[list gui::toggle_source_file_dir_widgets_enabled]"
grid .source_file_creation_checkbutton -row $row_num -column 2 \
-sticky w {*}[sizing::get_padding_options]
return [incr row_num]
}
# Procedure to create a dialog box whereby the user chooses
# a directory. The dialog box starts at the directory stored
# in associated_variable, and the directory eventually chosen by the
# user is stored back into associated_variable.
proc configure_directory {associated_variable} {
upvar #0 $associated_variable var
set tmp [tk_chooseDirectory -initialdir $associated_variable]
if {![string equal $tmp ""]} {
set var $tmp
}
}
# Create file-saving-location widgets and return the next available
# row number.
proc setup_directory_widgets {row_num} {
proc create_directory_choice_ctrl \
{paths label_text var row_num} {
namespace import ::platform::get_background_options
namespace import ::sizing::get_padding_options
set l [label [lindex $paths 0] -text $label_text \
{*}[get_background_options]]
set t [label [lindex $paths 1] -textvariable $var \
{*}[get_background_options]]
set b [ttk::button [lindex $paths 2] \
-text "Browse..." \
-command "::gui::configure_directory $var"]
grid $l -row $row_num -column 0 {*}[get_padding_options] \
-sticky e
grid $t -row $row_num -column 1 {*}[get_padding_options] \
-sticky w -columnspan 2
grid $b -row $row_num -column 3 {*}[get_padding_options] \
-sticky we
}
create_directory_choice_ctrl \
[directory_ctrl_paths header] \
"Save header in directory" \
widget_contents::header_directory \
$row_num
incr row_num
create_directory_choice_ctrl \
[directory_ctrl_paths source_file] \
"Save source file in directory" \
widget_contents::source_file_directory \
$row_num
incr row_num
return [incr row_num]
}
# Set up widgets for user to configure generated C++ special member
# functions.
# Returns next available row
proc setup_special_member_function_widgets {row_num} {
foreach smf [widget_contents::get_special_member_function_labels] {
label .label($smf) \
-text $smf {*}[platform::get_background_options]
ttk::checkbutton .checkbutton($smf) \
-text "Declare?" -variable \
::widget_contents::should_generate_special_member_function($smf)
ttk::combobox .implementation_combobox($smf) \
-values [widget_contents::get_implementation_specifiers] \
-textvariable ::widget_contents::implementation_spec($smf) \
-state readonly
ttk::combobox .access_combobox($smf) \
-values [widget_contents::get_access_specifiers] \
-textvariable ::widget_contents::access_spec($smf) \
-state readonly
grid .label($smf) -row $row_num -column 0 -sticky e \
{*}[sizing::get_padding_options]
grid .checkbutton($smf) -row $row_num -column 1 -sticky w \
{*}[sizing::get_padding_options]
grid .implementation_combobox($smf) -row $row_num -column 2 \
-sticky w {*}[sizing::get_padding_options]
grid .access_combobox($smf) -row $row_num -column 3 \
-sticky w {*}[sizing::get_padding_options]
# Unchecking the checkbutton means application will not create
# C++ code for that special member function. The comboboxes for
# that special member function therefore become
# irrelevant and should be disabled. For the checkbutton for the
# Destructor, for "Make destructor virtual?" checkbutton should be
# disabled also. We do that here.
.checkbutton($smf) configure -command \
"{*}[list ::gui::refresh_combobox_states $smf \
[string equal $smf Destructor]]"
incr row_num
}
return $row_num
}
# Create widget for user to configure whether the destructor should be
# virtual.
# Returns next available row.
proc setup_destructor_virtuality_widget {row_num} {
ttk::checkbutton .destructor_virtuality_checkbutton \
-text "Make destructor virtual?" \
-variable ::widget_contents::destructor_is_virtual
grid .destructor_virtuality_checkbutton -row $row_num -column 1 \
-sticky w {*}[sizing::get_padding_options]
return [incr row_num]
}
# Create widget controlling C++ namespaces to be generated.
# Returns next available row.
proc setup_namespace_widget {row_num} {
label .namespace_label_0 -text "Enclosing namespaces" \
{*}[platform::get_background_options]
grid .namespace_label_0 -row $row_num -column 0 -sticky ne \
{*}[sizing::get_padding_options]
label .namespace_label_1 -text "(one per line, outermost first)" \
{*}[platform::get_background_options]
grid .namespace_label_1 -row [expr {$row_num + 1}] -column 0 \
-sticky ne {*}[sizing::get_padding_options]
text .namespace_box -height 6 -width 40 \
{*}[platform::get_text_widget_options]
grid .namespace_box -row $row_num -column 1 -columnspan 2 -rowspan 4 \
-sticky we {*}[sizing::get_padding_options]
bind .namespace_box <FocusOut> {
widget_contents::set_cpp_namespaces [.namespace_box get -- 1.0 end]
}
return [incr row_num 5]
}
# Create widget for user to select whether to add legal notice
proc setup_legal_notice_widget {row_num} {
ttk::checkbutton .legal_notice_checkbutton \
-text "Generate legal notice?" \
-variable ::widget_contents::should_generate_legal_notice
grid .legal_notice_checkbutton -row $row_num -column 1 \
-columnspan 4 \
-sticky w {*}[sizing::get_padding_options]
if {![widget_contents::get_whether_legal_notice_enabled]} {
.legal_notice_checkbutton state "disabled"
}
return [incr row_num]
}
# Create widget controlling indentation style. Returns next available row.
proc setup_indentation_widget {row_num} {
label .indentation_label -text "Indentation style" \
{*}[platform::get_background_options]
grid .indentation_label -row $row_num -column 0 -sticky e \
{*}[sizing::get_padding_options]
ttk::combobox .indentation_combobox -values \
[widget_contents::get_indentation_styles] \
-textvariable ::widget_contents::indentation_style -state readonly
grid .indentation_combobox -row $row_num -column 1 -sticky w \
{*}[sizing::get_padding_options]
return [incr row_num]
}
# Create widgets controlling whether to add the newly created files to
# version control, and if so, the shell command with which to to do (e.g.
# "svn add" or etc.). Returns next available row.
proc setup_vcs_widgets {row_num} {
label .vcs_label -text "Add files to version control?" \
{*}[platform::get_background_options]
grid .vcs_label -row $row_num -column 0 -sticky e \
{*}[sizing::get_padding_options]
set vcs_widgets_data \
[ dict create \
"" "Do not add" \
"git add" "Run \"git add\"" \
"svn add" "Run \"svn add\"" \
]
set i 1
foreach {command description} $vcs_widgets_data {
set rb \
[ ttk::radiobutton .vcs_radiobutton_${i} \
-variable ::widget_contents::vcs_command \
-value $command -text $description \
]
grid $rb -row $row_num -column $i -sticky w \
{*}[sizing::get_padding_options]
incr i
}
return [incr row_num]
}
# Create buttons to cancel, and to proceed with generating code. Returns
# next variable row.
proc setup_cancel_and_generate_buttons {row_num} {
ttk::button .cancel_button -text "Cancel" \
{*}[sizing::get_narrow_option] -command exit
grid .cancel_button -row $row_num -column 0 -sticky swe \
{*}[sizing::get_padding_options]
ttk::button .generate_button -text "Generate" \
{*}[sizing::get_narrow_option] \
-command {
::cpp_code_generation::generate \
[widget_contents::get_class_name] \
[widget_contents::get_header_guard] \
[widget_contents::get_header_name] \
[widget_contents::get_whether_source_file_enabled] \
[widget_contents::get_header_directory] \
[widget_contents::get_source_file_name] \
[widget_contents::get_source_file_directory] \
[widget_contents::get_special_member_function_details] \
[widget_contents::get_cpp_namespaces] \
[expr { \
[widget_contents::get_whether_legal_notice_enabled]? \
[configuration::get_legal_notice]: \
"" \
} \
] \
[widget_contents::get_indentation_string] \
[widget_contents::get_vcs_command] \
}
grid .generate_button -row $row_num -column 3 -sticky swe \
{*}[sizing::get_padding_options]
return [incr row_num]
}
# Setup all the widget on the top window.
proc setup_widgets {} {
# Hide GUI till all widgets created
wm withdraw .
configure_main_window_title \
"Boilermatic: C++ boilerplate code generator"
widget_contents::initialize
set widget_setup_procedures [\
list setup_class_name_widget \
setup_filename_stem_widget \
setup_header_guard_widget \
setup_file_creation_toggle_widgets \
setup_directory_widgets \
setup_special_member_function_widgets \
setup_destructor_virtuality_widget \
setup_namespace_widget \
setup_legal_notice_widget \
setup_indentation_widget \
setup_vcs_widgets \
setup_cancel_and_generate_buttons \
]
set row 0
foreach procedure $widget_setup_procedures {
set row [$procedure $row]
}
# Make column 1 expand when window resized
grid columnconfigure . 1 -weight 1
# Make all the rows expand when window resized
set max_row [lindex [grid size .] 1]
for {set i 0} {$i != $max_row } {incr i} {
grid rowconfigure . $i -weight 1
}
# Display GUI
wm deiconify .
}
# Refresh states of comboboxes for a particular C++ special member
# function passed to smf, according as whether each should be normal or
# disabled. Pass 1 to refresh_destructor_virtuality_checkbutton_state if
# we want to refresh the state of that too.
proc refresh_combobox_states \
{smf {refresh_destructor_virtuality_checkbutton_state 0}} {
variable ::widget_contents::should_generate_special_member_function
set s \
[expr {$should_generate_special_member_function($smf)? \
"!disabled": "disabled"}]
.implementation_combobox($smf) state $s
.access_combobox($smf) state $s
if {$refresh_destructor_virtuality_checkbutton_state} {
.destructor_virtuality_checkbutton state $s
}
}
}
##############################################################################
# Data validation procedures are contained in this namespace.
namespace eval validation {
# INTERFACE
# Validates a string purporting to be a valid C++ identifier (which
# might be used as e.g. a class name or a preprocessor symbol).
# Parameters:
# target - string being validated
# msg - a variable. If the target is invalid, a user-friendly error
# message will be placed in this variable; otherwise, it will be
# untouched.
# target_desc - a user-friendly descriptor of the nature of target,
# which will be incorporated into any error message placed in msg.
# E.g. "class name".
# Returns 1 if target is valid, otherwise returns 0.
namespace export validate_cpp_identifier
# Validates a string purporting to be a valid filename.
# target - string being validated
# msg - a variable. If the target is invalid, a user-friendly error
# message will be placed in this variable; otherwise, it will be
# untouched.
# Returns 1 if the target is valid, otherwise returns 0.
# This function may assess as invalid strings that may actually be
# accepted by certain platforms as valid filenames. It is conservative
# however and errs on the side of rejecting filenames that might
# be problematic (even if not strictly unusable) on some platforms.
namespace export validate_filename
# Validates a list of stings purporting to be valid C++ namespace
# identifiers.
# Parameters:
# target - a (Tcl) list of strings to be validated
# msg - a variable. If (any of the strings in the) the target is
# invalid, a user-friendly error
# message will be placed in this variable; otherwise, it will be
# untouched.
# Returns 1 if all the strings in target are valid C++ namespace
# identifiers, otherwise returns 0;
# however, if target is not a (Tcl) list then this function may raise an
# error, rather than returning 0.