File: generate.tcl

package info (click to toggle)
dotfile 1%3A2.4-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,472 kB
  • ctags: 523
  • sloc: tcl: 14,072; sh: 918; makefile: 177; lisp: 18; ansic: 7
file content (1072 lines) | stat: -rw-r--r-- 32,687 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
### Copyright (C) 1995-1997 Jesper K. Pedersen
### 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, write to the Free Software
### Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


############################################################
# This function generates only the actual page
############################################################
proc regenerate {} {
  global setup __pathProgsNames __editInfo module
  
  set page $__editInfo(name)
  if {$page == ""} return

  ### reading the other parts of the files
  foreach elm $setup(dotfile) {

    ### testing that the file exists
    if {![file exists [lindex $elm 2]]} {
      tk_dialog .error "Error" "The file [lindex $elm 2] didn't exist. To regenerate, you first have to generat all pages!" error 0 OK
      return
    }
    set FILE [open [lindex $elm 2] r]
    set line [gets $FILE]
    set before([lindex $elm 0]) ""
    set after([lindex $elm 0]) ""
    
    ### Removing the "auto generated" message
    while {![eof $FILE] && ([string match "[lindex $elm 3]*" $line] ||
                          [string match "\#!*" $line]) } {
      set line [gets $FILE]
    }
    
    ### Reading every thing up til the actual section
    while {![eof $FILE] && ![string match "[lindex $elm 3]---------->[string range $__pathProgsNames($page) 1 [string length $__pathProgsNames($page)]]<----------[lindex $elm 4]" $line]} {
      append before([lindex $elm 0]) $line\n
      set line [gets $FILE]
    }

    if {[eof $FILE]} {
      tk_dialog .error "Error" "The file [lindex $elm 2] didn't seems to be a file generated from The Dotfile Generator. To regenerate, you first have to generat all pages!" error 0 OK
      close $FILE
      return
    }
    set line [gets $FILE]
    
    ### Ignoring the page
    while {![eof $FILE] && ![regexp "^[lindex $elm 3]---------->.*<----------[lindex $elm 4]" $line]} {
      set line [gets $FILE]
    }

    if {![eof $FILE]} {
      ### Reading the rest of the page
      while {![eof $FILE]} {
        append after([lindex $elm 0]) $line\n
        set line [gets $FILE]
      }
    }

    close $FILE
  }

  set oldSetup $setup(whatToGenerate)
  set setup(whatToGenerate) one

  generate

  ### merging the files
  foreach elm $setup(dotfile) {
    set FILE [open [lindex $elm 2] r]

    ### Removing the "auto generated" message
    set line [gets $FILE]
    while {![eof $FILE] && ([string match "[lindex $elm 3]*" $line] ||
                          [string match "\#!*" $line])} {
      set line [gets $FILE]
    }
    set text [read $FILE]
    close $FILE

    set FILE [open [lindex $elm 2] w]

    ### printing the overall description
    if {![string match "\#!*" $module(overAllDesc)]} {
      set edesc "[lindex $elm 3] $module(overAllDesc)"
    } else {
      set edesc $module(overAllDesc)
    }
    regsub -all -- "\n" $edesc  " [lindex $elm 4]\n[lindex $elm 3] " edesc
    set edesc "$edesc [lindex $elm 4]"
    eval puts $FILE \"$edesc\"

    ### printing the lines in the file.
    puts $FILE [string trimright $before([lindex $elm 0]) "\n"]\n
    puts $FILE $text
    puts $FILE [string trimright $after([lindex $elm 0]) "\n"]\n
    close $FILE
  }

  set setup(whatToGenerate) $oldSetup
}

############################################################
#           This function, generates the files          
############################################################
proc generate {} {
  global __progGenList __saveInfo __answers __shortDesc __ok 
  global __editInfo setup  module __windows __pathProgsNames
  set noSave 0
  set initProg $__editInfo(name)

  # first of all check the actual page
  if {[checkPage] == 0} return
  catch {unset __answers}

  startBusy "Generating..."

  ### Making the window for print.
  if {$setup(print)} {
    foreach elm $setup(dotfile) {
      set w .output[lindex $elm 0]
      set activeWin([lindex $elm 0]) 0
      if {[winfo exists $w]} {
        $w.text delete 1.0 end
      } else {
        toplevel $w
        wm withdraw $w
        wm title $w [lindex $elm 1]

        pack [frame $w.frame] -side bottom
        button $w.frame.ok -text OK -command "catch {destroy $w}"
        button $w.frame.help -text "Tell me how to use this!" \
            -command "help`gotoTag howToUse"
        pack $w.frame.ok $w.frame.help -padx 3m -pady 3m -ipadx 2m -ipady 1m \
            -side left
        
        text $w.text -relief raised -bd 2 -yscrollcommand "$w.scroll set" \
          -setgrid true
        scrollbar $w.scroll -relief flat -command "$w.text yview"
        pack $w.scroll -side right -fill y
        pack $w.text -side left -expand yes -fill both
        if {$setup(placeWindows)} {
          wm geometry $w 50x15-1+1
        } else {
          wm geometry $w 50x15
        }
      }
    }
  }

  ### opening the files for output
  if {$setup(file)} {
    foreach elm $setup(dotfile) {
      createDir [file dirname [lindex $elm 2]]
      set OUTPUT([lindex $elm 0]) [open [lindex $elm 2] w]
    }
  }

  foreach elm $setup(dotfile) {
    set __answers([lindex $elm 0]) ""
  }
  ### printing the overall description
  foreach elm $setup(dotfile) {
    if {![string match "\#!*" $module(overAllDesc)]} {
       set edesc "[lindex $elm 3] $module(overAllDesc)"
    } else {
      set edesc $module(overAllDesc)
    }
    regsub -all -- "\n" $edesc  " [lindex $elm 4]\n[lindex $elm 3] " edesc
    set edesc "$edesc [lindex $elm 4]"
    if {$setup(print)} {
      eval .output[lindex $elm 0].text insert end \"$edesc\n\"
    }
    if {$setup(file)} {
      eval puts $OUTPUT([lindex $elm 0]) \"$edesc\"
    }
  }

  ### decide how much to generate
  switch $setup(whatToGenerate) {
    one {
      set plist $__editInfo(name)
    }
    selected {
      set plist {}
      foreach prog $__progGenList {
       	if {$__saveInfo($prog) == 1} {
	        lappend plist $prog
	      }
      }
    }
    all {
      set plist $__progGenList
    }
  }

  if {$setup(whatToGenerate) != "one"} {
    if {[info commands pre_generate] != ""} {
      set plist "pre_generate $plist"
    }
    if {[info commands post_generate] != ""} {
      set plist "post_generate $plist"
    }
  }
  unlink $initProg top ""

  ### Creating a "please wait" window
  if {$setup(file)} {
    set fileText " in file(s) "
    foreach elm $setup(dotfile) {
      append fileText "[lindex $elm 2] "
    }
  } else {
    set fileText ""
  }
  timeWindow [llength $plist] "Generating" \
    "Please Wait\nGenerating for $module(name)$fileText"

  ### runs through all the files, which have to be saved
  foreach prog $plist {
    
    set __editInfo(name) $prog
    if {$prog != "pre_generate" && $prog != "post_generate"} {
      linkVars $prog __$prog top
      set err [catch {uplevel \#0 $__ok($prog)} errmsg]
    } else {
      set err [catch {uplevel \#0 $prog} errmsg]
    }
      
    ### saveing the page gave an error
    if {$err} {
      global errorInfo
      set info $errorInfo
      set page [string range $__pathProgsNames($prog) 1 end]
      regsub -all {/} $page "-->" page
      set what [tk_dialog .dialog "Error while generating" "an error occurred on page: \"$page\"\nWith the following error message:\n \"$errmsg\"\n. What do you want to do?" error -1 "Continue without saving this page" "Goto errornous page" "see stack trace"]
      if {$what == 1} {
        destroyTimeWindow
        set __editInfo(name) $initProg
        loadMenu $prog
        endBusy
        return
      }
      if {$what == 2} {
        set w .tkerrorTrace
        catch {destroy $w}
        toplevel $w -class ErrorTrace
        wm minsize $w 1 1
        wm title $w "Stack Trace for Error"
        wm iconname $w "Stack Trace"
        button $w.ok -text OK -command "catch {destroy $w}"
        text $w.text -relief raised -bd 2 -yscrollcommand "$w.scroll set" \
          -setgrid true -width 40 -height 10
        scrollbar $w.scroll -relief flat -command "$w.text yview"
        pack $w.ok -side bottom -padx 3m -pady 3m -ipadx 2m -ipady 1m
        pack $w.scroll -side right -fill y
        pack $w.text -side left -expand yes -fill both
        $w.text insert 0.0 $info
        $w.text mark set insert 0.0
        
        # Center the window on the screen.
        
        wm withdraw $w
        update idletasks
        set x [expr [winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
                 - [winfo vrootx [winfo parent $w]]]
        set y [expr [winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
                 - [winfo vrooty [winfo parent $w]]]
        wm geom $w +$x+$y
        wm deiconify $w

        ### reload the page
        if {$initProg != ""} {
          set __editInfo(name) $initProg
          linkVars $initProg __$initProg  top
        } else {
          set __editInfo(name) ""
        }
        endBusy
        destroyTimeWindow
        return
      }
    } else {
      ### no error occured, printing and/or saving
      foreach elm $setup(dotfile) {
        set shortdesc ""
        if {[info exists __shortDesc($prog)] && \
              $__answers([lindex $elm 0]) != ""} {
          set shortdesc "[lindex $elm 3] $__shortDesc($prog)"
          regsub -all -- "\n" $shortdesc \
            " [lindex $elm 4]\n[lindex $elm 3] " shortdesc
          set shortdesc "$shortdesc [lindex $elm 4]"
          if {$setup(print)} {
            .output[lindex $elm 0].text insert end \
                "\n$shortdesc\n"
          }
        }
        if {$setup(file)} {
          puts $OUTPUT([lindex $elm 0]) \
              "\n[lindex $elm 3]---------->[string range $__pathProgsNames($prog) 1 [string length $__pathProgsNames($prog)]]<----------[lindex $elm 4]"
          puts $OUTPUT([lindex $elm 0]) "$shortdesc"
        }
        if {$__answers([lindex $elm 0]) != ""} {
          set  activeWin([lindex $elm 0]) 1
          if {$setup(print)} {
            .output[lindex $elm 0].text insert end $__answers([lindex $elm 0])
          }
          if {$setup(file)} {
            puts $OUTPUT([lindex $elm 0]) $__answers([lindex $elm 0])
          }
        }
        set __answers([lindex $elm 0]) ""
      }
    }
      
    if {$prog != "pre_generate" && $prog != "post_generate"} {
      unlink $prog top ""
    }
    incrTimeWindow
  }
  ### closing the "please wait" window
  destroyTimeWindow
  
  ### opennig the window, and closing the files.
  if {$setup(print)} {
    foreach elm $setup(dotfile) {
      if {$activeWin([lindex $elm 0])} {
        wm deiconify .output[lindex $elm 0]
        raise .output[lindex $elm 0]
      }
    }
  }
  if {$setup(file)} {
    foreach elm $setup(dotfile) {
      close $OUTPUT([lindex $elm 0])
    }
  }

  # creating the original window
  if {$initProg != ""} {
    set __editInfo(name) $initProg
    linkVars $initProg __$initProg  top
  } else {
    set __editInfo(name) ""
  }
  endBusy

  # saveing
  if {$setup(saveOnGenerate) == 1} {
    save
  }
  

}

############################################################
#    This procedure link variables to their basic names.
############################################################
proc linkVars {function prefix parent {func ""}} {
  set errmsg "The Dotfile Generator has unfortunately meet an internal error. This is most likely due to the fact that you have upgraded the module without exporting and reimporting the savefile. If this is the case, then you should reinstall the old version of The Dotfile Generator, which saved this file, export the file (using the export menu item), reinstall this version and import the file.\n\nThis is an unfortunate behaviour (I know), and it will hopefully be fixed in the next version of TDG."
  global __editInfo __widgetArgs __children
  if {$func != ""} {
    set funcPre "$func@"
  } else {
    set funcPre ""
  }

  foreach child $__children(${function}__$parent) {
    set type $__widgetArgs(${function}__${child}__type)
    upvar \#0 ${prefix}_$child prefixChild
    upvar \#0 "$funcPre$child" ch
    switch -exact -- $type {
      checkbox -
      int   -
      float -
      label -
      textbox -
      command -
      combobox -
      entry {
        trace variable ch rw \
          "linkBasic $funcPre$child ${prefix}_$child"
        if {[catch "set ch"]} {
          tk_dialog .error "Internal Error" $errmsg error 0 OK
          exit
        }
      }
      frame {
        linkVars $function $prefix $child $func
      }
      window {
        linkVars $function $prefix $child $func
      }

      menu -
      listbox -
      radio {
        ### create the traces.
        trace variable ch(index) w \
          "linkTrace_${type}Write $function $child ${prefix}_$child {$func}"
        trace variable ch(index) r \
          "linkTrace_${type}Read $function $child ${prefix}_$child {$func}"
        trace variable ch(name) w \
          "linkTrace_${type}Write $function $child ${prefix}_$child {$func}"
        trace variable ch(name) r \
          "linkTrace_${type}Read $function $child ${prefix}_$child {$func}"
        if {[catch "set ch(index); set ch(name)"]} {
          tk_dialog .error "Internal Error" $errmsg error 0 OK
          exit
        }
      }

      extentry -
      line -
      header -
      filloutelm {
        # Nothing It just have to be here.
      }
      fillout {
        trace variable ch w \
          "linkTrace_filloutWrite $function $child ${prefix}_$child {$func}"
        trace variable ch r \
          "linkTrace_filloutRead $function $child ${prefix}_$child {$func}"
        if {$func == ""} {
          if {[catch "set ch"]} {
          tk_dialog .error "Internal Error" $errmsg error 0 OK
          exit
          }
        }
      }
      default {
        error "Unknow type $type"
      }
    }
  }
}
proc linkBasic {basename prefixname dummy arrayname operation} {
  upvar \#0 $prefixname prefixChild
  upvar \#0 $basename child
  
  if {$operation == "r"} {
    set child $prefixChild
  } else {
    set prefixChild $child
  }
}
############################################################
# This function link 'Menu'
# from bacis to prefix Ie. when the bacis element changes,
# this function change the prefix form too
# Ie. name -> $prefix_name
############################################################
proc linkTrace_menuWrite {function basename prefixname func dummy arrayname operation} {
  global __widgetArgs __editInfo


  ### first of all check whether an trace allready is in progress
  if {$__editInfo(trace)} return
  set __editInfo(trace) 1

  if {$func != ""} {
    set func $func@
  }
  set entries $__widgetArgs(${function}__${basename}__entries)
  
  upvar \#0 $prefixname prefixChild
  upvar \#0 $func$basename child

  if {$arrayname == "index"} {
    ### the index elements has been changed
    set index $child(index)
    if {$index<0 || $index>=[llength $entries]} {
      set __editInfo(trace) 0
      error "Index \"$index\" is out of range for $basename"
    }
    ### setting the variable
    set prefixChild [lindex $entries $child(index)]
    set child(name) [lindex $entries $child(index)]

  } else {
    ### the name element has been changed.
    if {[lsearch -exact $entries $child(name)] == -1} {
      set __editInfo(trace) 0
      error [concat "\"$child(name)\" isn't a valid element in Menu" \
               "\"$basename\" should be one of \"$entries\""]
    }
    ### settring the variable
    set prefixChild $child(name)
    set child(index) [lsearch -exact $entries $child(name)]
  }

  ### remove the trace flag.
  set __editInfo(trace) 0
}
############################################################
# This function link 'Menu'
# from prefix to bacis Ie. when the prefix element changes,
# this function change the bacis form too
# Ie. prefix_name -> name
############################################################
proc linkTrace_menuRead {function basename prefixname func dummy arrayname operation} {
  global __widgetArgs __editInfo

  ### first of all check whether an trace allready is in progress
  if {$__editInfo(trace)} return
  set __editInfo(trace) 1

  if {$func != ""} {
    set func $func@
  }

  set entries $__widgetArgs(${function}__${basename}__entries)

  upvar \#0 $func$basename child
  upvar \#0 $prefixname prefixChild
  set child(name) $prefixChild
  set child(index) [lsearch -exact $entries $prefixChild]

  ### remove the trace flag
  set __editInfo(trace) 0
}

############################################################
# This function link 'ListBox'
# from bacis to prefix Ie. when the bacis element changes,
# this function change the prefix form too
# Ie. name -> $prefix_name
############################################################
proc linkTrace_listboxWrite {function basename prefixname func dummy arrayname operation} {
  global __widgetArgs __editInfo

  ### first of all check whether an trace allready is in progress
  if {$__editInfo(trace)} return
  set __editInfo(trace) 1

  if {$func != ""} {
    set func $func@
  }

  set entries $__widgetArgs(${function}__${basename}__entries)
  
  upvar \#0 $prefixname prefixChild
  upvar \#0 $func$basename child

  if {$arrayname == "index"} {
    ### the index elements has been changed
    set list {}
    foreach index $child(index) {
      if {$index<0 || $index>=[llength $entries]} {
        set __editInfo(trace) 0
        error "Index \"$index\" is out of range for $basename"
      }
      lappend list [lindex $entries $index]
    }
    ### setting the variable
    set prefixChild $child(index)
    set child(name) $list

  } else {
    ### the name element has been changed.
    set list {}
    foreach elm $child(name) {
      set index [lsearch -exact $entries $elm]
      if {$index  == -1} {
        set __editInfo(trace) 0
        error [concat "\"$elm\" isn't a valid element in ListBox" \
                 "\"$basename\" should be one of \"$entries\""]
      }
      lappend list $index
    }
    
    ### settring the variable
    set prefixChild $list
    set child(index) $list
  }

  ### remove the trace flag.
  set __editInfo(trace) 0
}
############################################################
# This function link 'Listbox'
# from prefix to bacis Ie. when the prefix element changes,
# this function change the bacis form too
# Ie. prefix_name -> name
############################################################
proc linkTrace_listboxRead {function basename prefixname func dummy arrayname operation} {
  global __widgetArgs __editInfo

  ### first of all check whether an trace allready is in progress
  if {$__editInfo(trace)} return
  set __editInfo(trace) 1

  if {$func != ""} {
    set func $func@
  }

  set entries $__widgetArgs(${function}__${basename}__entries)

  upvar \#0 $prefixname prefixChild
  upvar \#0 $func$basename child

  set child(name) {}
  set child(index) {}
  foreach elm $prefixChild {
    lappend child(name) [lindex $entries $elm]
    lappend child(index) $elm
  }

  ### remove the trace flag
  set __editInfo(trace) 0
}

############################################################
# This function link 'Radio'
# from bacis to prefix Ie. when the bacis element changes,
# this function change the prefix form too
# Ie. name -> $prefix_name
############################################################
proc linkTrace_radioWrite {function basename prefixname func dummy arrayname operation} {
  global __widgetArgs __editInfo

  ### first of all check whether an trace allready is in progress
  if {$__editInfo(trace)} return
  set __editInfo(trace) 1

  if {$func != ""} {
    set func $func@
  }

  set entries $__widgetArgs(${function}__${basename}__entries)
  
  upvar \#0 $prefixname prefixChild
  upvar \#0 $func$basename child

  if {$arrayname == "index"} {
    ### the index elements has been changed
    set index $child(index)
    if {$index<0 || $index>=[llength $entries]} {
      set __editInfo(trace) 0
      error "Index \"$index\" is out of range for $basename"
    }

    ### setting the variable
    set prefixChild $index
    set child(name) [lindex $entries $index]

  } else {
    ### the name element has been changed.
    set index [lsearch -exact $entries $child(name)]
    if {$index  == -1} {
      set __editInfo(trace) 0
      error [concat "\"$child(name)\" isn't a valid element in ListBox" \
               "\"$basename\" should be one of \"$entries\""]
    }
    
    ### settring the variable
    set prefixChild $index
    set child(index) $index
  }

  ### remove the trace flag.
  set __editInfo(trace) 0
}
############################################################
# This function link 'Radio'
# from prefix to bacis Ie. when the prefix element changes,
# this function change the bacis form too
# Ie. prefix_name -> name
############################################################
proc linkTrace_radioRead {function basename prefixname func dummy arrayname operation} {
  global __widgetArgs __editInfo

  ### first of all check whether an trace allready is in progress
  if {$__editInfo(trace)} return
  set __editInfo(trace) 1

  if {$func != ""} {
    set func $func@
  }

  set entries $__widgetArgs(${function}__${basename}__entries)

  upvar \#0 $prefixname prefixChild
  upvar \#0 $func$basename child

  set child(name) [lindex $entries $prefixChild]
  set child(index) $prefixChild

  ### remove the trace flag
  set __editInfo(trace) 0
}
############################################################
# This function link 'Radio'
# from bacis to prefix Ie. when the bacis element changes,
# this function change the prefix form too
# Ie. name -> $prefix_name
############################################################
proc linkTrace_filloutWrite {function basename prefixname func dummy arrayname operation} {
  error "It is not posible to set an fillOut element, this is a readonly variable"
}
############################################################
# This function link 'Radio'
# from prefix to bacis Ie. when the prefix element changes,
# this function change the bacis form too
# Ie. prefix_name -> name
############################################################
proc linkTrace_filloutRead {function basename prefixname func dummy arrayname operation} {
  fillOutSave $function $basename $func
}

############################################################
# This function unlink the variables
# If its argument is a frame, a window or an extentry, 
# then all the children are unlinked.
############################################################
proc unlink {function name func} {
  global __widgetArgs __editInfo __children

  if {$func == ""} {
    set funcPre ""
  } else {
    set funcPre $func@
  }
  
  if {$function == ""} return
  if {$name == "top"} {
    set type frame
  } else {
    set type $__widgetArgs(${function}__${name}__type)
  }
  switch -exact -- $type {
    checkbox -
    int   -
    float -
    label -
    textbox -
    entry -
    combobox -
    menu -
    listbox -
    command -
    radio {
      uplevel \#0 "catch {unset $funcPre$name}"
    }
    frame -
    extentry -
    filloutelm -
    window {
      foreach child $__children(${function}__$name) {
        unlink $function $child $func
      }
    }
    fillout {
      uplevel \#0 "catch {unset $name}"
      foreach child $__children(${function}__$name) {
        unlink $function $child $func 
      }
    }
    line -
    header {}

    default {
      error "unlink: Unknown type: $type"
    }
  }
}
############################################################
# This function is called from the makeChange function.
# It takes care of linking the right variables, and
# no more than what is nessecary.
############################################################
proc UpdateActive {name prefix} {
  global __widgetArgs __children __activeNivau __editInfo __parent
  set function $__editInfo(name)
  set par $__parent(${function}__$name)
  if {$par == "top"} return

  ### the element is part of an extentry.
  # calculating the prefix path up to the top.
  set prefixList {}
  while {$par != "top"} {
    set prefixList "$par $prefixList"
    set par $__parent(${function}__$par)
  }

  # removeing the function name from the prefix
  regexp "^__${function}_(.*)\$" $prefix all prefix
  set prefix ${prefix}_
  set indexList {}

  # calculating the index's
  foreach pre $prefixList {
    set type $__widgetArgs(${function}__${pre}__type)
    switch $type {
      extentry {
        if {![regexp "^${pre}(\[0-9\]+)_(.*)\$" $prefix all index rest]} {
          error "extentry: Coulnd't parse \"$prefix\" with \"$pre\""
        }
        lappend indexList $index
      }
      frame -
      window {
        ### insert a dummy element, as the next foreach will read this
        ### element too.
        set rest $prefix
        lappend indexList DUMMY
      }
      fillout {
        if {![regexp "^${pre}_(.*)\$" $prefix all rest]} {
          error "fillout: Coulnd't parse \"$prefix\" with \"$pre\""
        }
      }
      filloutelm {
        if {![regexp "^(\[0-9\]+)_(.*)\$" $prefix all index rest]} {
          error "filloutelm: Coulnd't parse \"$prefix\" for $pre"
        }
        lappend indexList $index
      }
      default {
        error "UpdateActive: unknown type $type"
      }
    }
    set prefix $rest
  }
  # relinking variables
  set __changed 0
  set index 0
  set prefix "__$function"
  foreach pre $prefixList {
    set type $__widgetArgs(${function}__${pre}__type)
    switch $type {
      frame -
      window {
        # ignore!
      }
      fillout {
        append prefix "_$pre"
        continue
      }
      filloutelm {
        append prefix "_[lindex $indexList $index]"
      }
      extentry {
        append prefix "_$pre[lindex $indexList $index]"
      }
      default {
        error "wrong type: $__widgetArgs(${function}__${pre}__type)"
      }
    }
    if {!$__changed && $type != "window" && $type != "frame" &&
        (![info exists __activeNivau($pre)] ||
         $__activeNivau($pre) != [lindex $indexList $index])} {
      set __changed 1
      unlink $function $pre ""
    }
    if {$__changed && $type != "window" && $type != "frame"} {
      set __activeNivau($pre) [lindex $indexList $index]
      linkVars $function $prefix $pre
    }
    incr index
  }

  # reseting links below
  foreach child $__children(${function}__$pre) {
    set type $__widgetArgs(${function}__${child}__type)
    if {$type == "extentry" || $type == "frame" || $type == "window"} {
      resetBelow $child ""
    }
  }
}

############################################################
# This function delete the activity for extentry's
# below the on which just have been selected.
############################################################
proc resetBelow {parent func} {
  global __children __activeNivau __editInfo __widgetArgs

  if {$func == ""} {
    set function $__editInfo(name)
  } else {
    set function $func
  }

  if {$__widgetArgs(${function}__${parent}__type) == "extentry"} {
    catch "unset __activeNivau($parent)"
  }
  
  foreach child $__children(${function}__$parent) {
    set type $__widgetArgs(${function}__${child}__type)
    if {$type == "extentry" || $type == "frame"} {
      resetBelow $child $func
    }
  }
}
############################################################
#    This procedure is used to run through an extentry
############################################################
proc forevery {name proc} {
  global __widgetArgs __activeNivau __editInfo __scrollBar

  if {[regexp {^([^@]+)@([^@]+)$} $name all func name]} {
    set function $func
  } else {
    set function $__editInfo(name)
    set func ""
  }

  if {![info exists __widgetArgs(${function}__${name}__type)]} {
    error "Forevery: element \"$name\" doesn't exists"
  }
  set type $__widgetArgs(${function}__${name}__type)

  if {$type != "extentry"} {
    error "forevery may only be used on extentries. type of \"$name\" is \"$type\""
  }

  set prefix "[buildPath $name $func]_$name"
  set count [lindex $__scrollBar($prefix) 1]

  ### evaluateing proc for each prompt.
  for {set i 0} {$i < $count} {incr i} {
    unlink $function $name $func
    set __activeNivau($name) $i
    linkVars $function $prefix$i $name $func
    set result [catch {uplevel \#0 $proc} err]
    switch $result {
      1 {error $err}
      3 break
    }
  }
  resetBelow $name $func
}

############################################################
#    This procedure enables or disables widgets
############################################################
proc Enable {args} {
  foreach widget $args {
    enable_disable normal $widget
  }
}

proc Disable {args} {
  foreach widget $args {
    enable_disable disabled $widget
  }
}
proc enable_disable {mode name} {
  global  __editInfo __widgetArgs

  if {[regexp {^([^@]+)@([^@]+)$} $name all func name]} {
    set function $func
    set funcPre $func@
    
  } else {
    set function $__editInfo(name)
    set func ""
    set funcPre ""
  }

  if {![info exists __widgetArgs(${function}__${name}__type)]} {
    error "Enable/Disable called with an unknow widget: \"$name\""
  }
  set prefix [buildPath $name $func]
  changeState $function $prefix $name $mode 1
}

######################################################################
### This function changes the state of a widget and it's children
### If the widget is mapped, the element is disabled.
######################################################################
proc changeState {function prefix name mode active} {
  global __state __children __var2path __scrollBar __widgetArgs
  # check if state information exists
  if {![info exists __state(${prefix}_$name)]} {
    set __state(${prefix}_$name) "normal"
  }

  # seting the __state if it has changed.
  if {$__state(${prefix}_$name) != $mode} {
    set __state(${prefix}_$name) $mode
    ### set the widget
    if {[info exists __var2path(${prefix}_$name)] } {
      setState $function $__var2path(${prefix}_$name) $prefix $name $active
    }
    set type $__widgetArgs(${function}__${name}__type)
    if {$type == "extentry"} {
      if {![info exists __scrollBar(${prefix}_$name)]} {
        # This can happend if the extentry is inside another extentry.
        # and the inner extentry isn't packed (extentry->window->extentry)
        set __scrollBar(${prefix}_$name) {0 0}
      }
      set total [lindex $__scrollBar(${prefix}_$name) 1]
      for {set i 0} {$i < $total} {incr i} {
        foreach child $__children(${function}__$name) {
          changeState $function ${prefix}_$name$i $child $mode $active
        }
      }
    }
    if {$type == "frame" || $type == "window"} {
      foreach child $__children(${function}__$name) {
        changeState $function $prefix $child $mode $active
      }
    }
  }
}
############################################################
# Given an extentry, this function create a variable
# prefix all the way down through the active elements
# including the function as prefix.
############################################################
proc buildPath {name func} {
  global __parent __editInfo __activeNivau __widgetArgs

  if {$func != ""} {
    set function $func
  } else {
    set function $__editInfo(name)
  }
  
  set par $__parent(${function}__$name)
  # calculating the prefix path up to the top.
  set prefixList {}
  while {$par != "top"} {
    set prefixList "$par $prefixList"
    set par $__parent(${function}__$par)
  }

  # creating the prefix
  set prefix "__${function}"

  foreach pre $prefixList {
    set type $__widgetArgs(${function}__${pre}__type)
    switch $type {
      extentry {
        if {![info exists __activeNivau($pre)]} {
          error "Missing a forevery on $pre"
        }
        append prefix "_$pre$__activeNivau($pre)"
      }
      filloutelm {
        if {![info exists __activeNivau($pre)]} {
          error "Fillout page for $pre have to be visable, before you can refer to it."
        }
        append prefix "_$__activeNivau($pre)"
      }
      frame -
      window {}
      fillout {
        append prefix "_$pre"
      }
      fillout {
      }
      default {
        error "buildPath: unknown type $type"
      }
    }
  }
  return $prefix
}