File: gd.tcl

package info (click to toggle)
mc 4.1.35-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 6,924 kB
  • ctags: 9,665
  • sloc: ansic: 84,273; tcl: 1,779; makefile: 1,266; sh: 864; perl: 262; awk: 148; sed: 93; csh: 1
file content (752 lines) | stat: -rw-r--r-- 19,046 bytes parent folder | download | duplicates (2)
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
#
# gd: the built in Midnight Commander GUI designer
# (C) 1996 the Free Software Foundation
# See the file COPYING for details
#
# Author: Miguel de Icaza
#

set min_width  10
set min_height 10
set dragging 0
set new_dialog 1

proc reset_counters {} {
    global dialog_rows
    global dialog_columns
    global frame_count
    global text_count 
    global line_count

    set dialog_rows 4
    set dialog_columns 4
    set frame_count 0
    set text_count 0
    set line_count 0
}

#
# create a division
# 
# what = { row, column }
# if visible then allow the user to add columns and make them visibles
#
proc create_division {root index what visible} {
    global dialog_columns
    global dialog_rows

    set cn [expr $index*2]

    if {$what == "row"} { 
	set owhat "column"
	set width height
	set stick we
    } else { 
	set owhat "row"
	set width width 
	set stick ns
    }
    set c \$dialog_${owhat}s

    if {$visible} {
	frame $root.$what@$cn -$width 3  -back gray -relief sunken -borderwidth 4
	bind $root.$what@$cn <Enter> "$root.$what@$cn configure -back red"
	bind $root.$what@$cn <Leave> "$root.$what@$cn configure -back gray"
	bind $root.$what@$cn <ButtonRelease-1> "new_division $root $index $what"
    } else {
	frame $root.$what@$cn -$width 3
    }
    grid $root.$what@$cn -$what $cn -$owhat 0 -${what}span 1 -${owhat}span [expr $c*2] -sticky $stick
}

proc create_column {root column visible} {
    create_division $root $column column $visible
}

proc create_row {root row visible} {
    create_division $root $row row $visible
}

proc column_space {root column} {
    global min_width

    grid columnconfigure $root [expr $column*2+1] -minsize $min_width
}

proc row_space {root row} {
    global min_height

    grid rowconfigure $root [expr $row*2+1] -minsize $min_height
}

#
# When inserting a column or row, move all of the widgets after
# the insertion point
#
proc move_childs {root index what} {
    global components

    set pix [expr $index*2]
    
    foreach i $components {
	set info [grid info $root.$i]
	set idx [lsearch $info -$what]
	if {$idx >= 0} {
	    incr idx
	    set cp [lindex $info $idx]
	    if {$cp >= $pix} {
		grid $root.$i -$what [expr $cp+2]
	    }
	}
    }
}

#
# Update the separators spans after a column or row has been added
#
proc reconfig_spans {root} {
    global dialog_rows
    global dialog_columns
 
    for {set i 0} {$i <= $dialog_rows} {incr i} {
	set j [expr $i*2]
	grid $root.row@$j -columnspan [expr $dialog_columns*2]
    }
    for {set i 0} {$i <= $dialog_columns} {incr i} {
	set j [expr $i*2]
	grid $root.column@$j -rowspan [expr $dialog_rows*2]
    }
}

proc new_division {root index what} {
    global dialog_columns
    global dialog_rows

    set var [incr dialog_${what}s]

    create_$what $root $var 1
    ${what}_space $root $var
    reconfig_spans $root
    move_childs $root $index $what
}

proc create_gui_canvas {frame} {
    if {$frame == "."} { set base "" } else { set base $frame }
    set bw $base.widgets
    catch "frame $bw"
    grid   $bw -column 1 -row 1 -sticky nwse -padx 2 -pady 2 -ipady 12
}

proc create_workspace {frame} {
    global dialog_rows
    global dialog_columns
    global env
    global components

    puts "Create_workspace llamado"

    if {$frame == "."} { set base "" } else { set base $frame }
    set bw $base.widgets

    # If user wants to edit this, then the workspace has been already created.
    if ![string compare .$env(MC_EDIT) $frame] {
	return 0
    }

    create_gui_canvas $frame

    $bw  configure -relief sunken -borderwidth 2
    canvas $base.h -back white -height 8 -relief sunken -borderwidth 2
    canvas $base.v -back white -width 8 -relief sunken -borderwidth 2

    grid   $bw -column 1 -row 1 -sticky nwse -padx 2 -pady 2 -ipady 12
    grid   $base.h -column 1 -row 0 -sticky we
    grid   $base.v -column 0 -row 1 -sticky ns

    for {set col 0} {$col <= $dialog_columns} {incr col} {
	column_space $bw $col
	create_column $bw $col 1
    }
    for {set row 0} {$row <= $dialog_rows} {incr row} {
	row_space $bw $row
	create_row $bw $row 1
    }
}

proc get_stick {root widget} {
    global props

    set a $props(stick.n.$widget)
    set b $props(stick.s.$widget)
    set c $props(stick.e.$widget)
    set d $props(stick.w.$widget)
    return "$a$b$c$d"
}

#
# Callbacks for configuring widgets, frames and extra text
# 

    proc set_stick {root widget} {
	if {$root == "."} { set base "" } else { set base $root }
	grid $base.widgets.$widget -sticky [get_stick $root $widget]
    }

    proc make_sticky_button {root window widget sval} {
	checkbutton $window.$sval -text $sval -variable props(stick.$sval.$widget) \
	    -command "set_stick $root $widget" -onvalue $sval -offvalue ""
    }

 
    #
    # Configure a widget
    #
    proc config_widget {root widget} {
	global components
	global props

	set w .config-$widget

	toplevel $w
	frame $w.f
	make_sticky_button $root $w.f $widget n 
	make_sticky_button $root $w.f $widget s 
	make_sticky_button $root $w.f $widget e 
	make_sticky_button $root $w.f $widget w 
	label $w.f.l -text "Anchor"
	pack $w.f.l $w.f.n $w.f.s $w.f.e $w.f.w
	pack $w.f
    }

    proc make_radio_button {root window widget state} {
	radiobutton $window.$state -text $state -variable frame_relief -value $state \
	    -command "$root.widgets.$widget configure -relief $state" 
	pack $window.$state
    }
    #
    # Configure a frame
    #
    proc config_frame {root widget} {
	set w .config-$widget

	toplevel $w
	make_radio_button $root $w $widget sunken
	make_radio_button $root $w $widget groove
	make_radio_button $root $w $widget ridge
	make_radio_button $root $w $widget raised
    }

    proc set_text {root widget from} {
	set text [.config-$widget.f.entry get]
	puts "Texto: $text"
	$root.widgets.$widget configure -text $text
    }

    proc config_text {root widget} {
	config_widget $root $widget
	entry .config-$widget.f.entry -text [lindex [$root.widgets.$widget configure -text] 4]
	pack .config-$widget.f.entry
	bind .config-$widget.f.entry <Return> "set_text $root $widget .config-$widget.f.entry"
    }

    proc config_line {root widget} {
	# Nothing is configurable on a line.
    }

proc reconfig_rows {root} {
    global dialog_rows
    global dialog_columns

    for {set i 0} {$i < $dialog_rows} {incr i} {
	set cn [expr $i*2]
	grid $root.row@cn -columnspan [expr $dialog_columns*2+2]
    }
}

#
# Set the column for a widget
#
proc set_widget_col {root w col} {
    global dialog_columns

    if {$root == "."} { set base "" } else { set base $root }
    if {$col >= $dialog_columns} {
	return
    }
    grid $base.widgets.$w -column [expr $col*2+1]
}

#
# Set the row for a widget
#
proc set_widget_row {root w row} {
    global dialog_rows

    if {$root == "."} { set base "" } else { set base $root }
    if {$row >= $dialog_rows} {
	return
    }
    grid $base.widgets.$w -row [expr $row*2+1]
}

#
# Set the number of spanning lines for a widget
#
proc set_span_col {root w n} {
    if {$root == "."} { set base "" } else { set base $root }
    grid $base.widgets.$w -columnspan [expr $n*2-1]
}

#
# Set the number of spanning rows for a widget
#
proc set_span_row {root w n} {
    if {$root == "."} { set base "" } else { set base $root }
    grid $base.widgets.$w -rowspan [expr $n*2-1]
}

proc set_sticky {root w s} {
    global props

    if {$root == "."} { set base "" } else { set base $root }
    grid $base.widgets.$w -sticky $s

    foreach stick_dir {n s w e} {
	if [regexp $stick_dir $s] {
	    set props(stick.$stick_dir.$w) $stick_dir
	}
    }
}

#
# Start a drag
#
proc drag {root w x y} {
    global dragging
    global root_x
    global root_y

    if {$root == "."} { set base "" } else { set base $root }

    if {!$dragging} {
	set dragging 1
	button $base.widgets.drag -text "$w"
    } 
    place $base.widgets.drag -x [expr $x-$root_x] -y [expr $y-$root_y]
}

#
# Drop action
#
proc drop {root w x y} {
    global root_x
    global root_y

    global dragging

    if {$root == "."} { set base "" } else { set base $root }
    set pos [grid location $base.widgets [expr $x-$root_x] [expr $y-$root_y]]
    
    set col [expr [lindex $pos 0]/2]
    set row [expr [lindex $pos 1]/2]
    set_widget_row $root $w $row 
    set_widget_col $root $w $col
    set dragging 0
    catch "destroy $root.widgets.drag"
}

#
# Setup before the drag
#
proc button_press {root} {
    global root_x
    global root_y
    
    if {$root == "."} { set base "" } else { set base $root }
    set root_x [expr [winfo rootx $base.widgets]]
    set root_y [expr [winfo rooty $base.widgets]]
}

#
# Extract a value from a {key value ...} list returned by Tk
#
proc extract_parameter {parameters key} {
    return [lindex $parameters [expr [lsearch $parameters $key]+1]]
}

#
# Return the value of a variable stored in the props() array
#
proc get_prop {root win} {
    global props

    return $props($root.props.$win)
}

#
# Save the layout as defined by the user
#
proc save_gui {root dlg} {
    global dialog_columns
    global dialog_rows
    global components
    global frame_count
    global text_count
    global line_count

    if {$root == "."} { set base "" } else { set base $root }

    set file [open "gui$dlg.tcl" w]

    puts $file "set props($dlg.columns) $dialog_columns"
    puts $file "set props($dlg.rows)    $dialog_rows"
    puts $file "set props($dlg.frames)  $frame_count"
    puts $file "set props($dlg.texts)   $text_count"
    puts $file "set props($dlg.lines)   $line_count"

    set cnum [llength $components]
    puts $file "set props($dlg.components)   \"$components\""
    puts $file "set props($dlg.count)   $cnum"

    # 1. dump components

    foreach i $components {
	set winfo [grid info $base.widgets.$i]

	puts $file "set props($dlg.props.$i) \"$winfo\""
    }
    
    # 2. dump frames
    for {set i 0} {$i < $frame_count} {incr i} {
	set winfo [grid info $base.widgets.frame$i]
	set relief [lindex [$base.widgets.frame$i configure -relief] end]

	puts $file "set props($dlg.frame$i) \"$winfo\""
	puts $file "set props($dlg.relief.frame$i) $relief"
    }

    # 3. dump texts
    for {set i 0} {$i < $text_count} {incr i} {
	set winfo [grid info $base.widgets.text$i]
	set text  [lindex [$base.widgets.text$i configure -text] end]
	puts $file "set props($dlg.text$i) \"$winfo\""
	puts $file "set props($dlg.text.text$i) \"$text\""
    }

    # 4. dump lines
    for {set i 0} {$i < $line_count} {incr i} {
	set winfo [grid info $base.widgets.line$i]
	puts $file "set props($dlg.line$i) \"$winfo\""
    }
    close $file
}

#
# Setup the bindings for a given widget to make it drag and droppable
#
proc make_draggable {root wn short} {
    bind $wn <ButtonPress-1> "button_press $root; update idletasks"
    bind $wn <B1-Motion> "drag $root $short %X %Y; update idletasks"
    bind $wn <ButtonRelease-1> "drop $root $short %X %Y; update idletasks"
}

#
# root, window name, what = { frame, text, widget }
#
proc make_config_button {root i what} {
    if {$root == "."} { set base "" } else { set base $root } 

    frame .gui-widgets.$i
    button .gui-widgets.$i.button -command "config_$what $root $i " -text "$i"

    set    spans [grid info $base.widgets.$i] 
    scale  .gui-widgets.$i.scale-x -orient horizontal -from 1 -to 10 -label "span-x" \
	-command "set_span_col $root $i"
    scale  .gui-widgets.$i.scale-y -orient horizontal -from 1 -to 10 -label "span-y" \
	-command "set_span_row $root $i"

    .gui-widgets.$i.scale-y set [expr 1+([lindex $spans [expr 1+[lsearch $spans -rowspan]]]-1)/2]
    .gui-widgets.$i.scale-x set [expr 1+([lindex $spans [expr 1+[lsearch $spans -columnspan]]]-1)/2]
    pack   .gui-widgets.$i.button .gui-widgets.$i.scale-x .gui-widgets.$i.scale-y -side left
    pack   .gui-widgets.$i -side top
}

#
# Create a new border (these are widgets not known by mc)
#
proc new_border {root} {
    global frame_count
    if {$root == "."} { set base "" } else { set base $root }

    set short frame$frame_count
    set wn    $base.widgets.$short
    incr frame_count

    # create the frame
    frame $wn  -relief sunken -borderwidth 2 
    grid $wn -row 1 -column 1  -columnspan 1 -rowspan 1 -sticky wens -padx 2 -pady 2
    lower $wn

    # drag and dropability
    make_draggable $root $wn $short

    # configurability
    make_config_button $root $short frame
} 

#
# Create a new line separator (these are widgets not known by mc)
#
proc new_line {root} {
    global line_count
    if {$root == "."} { set base "" } else { set base $root }

    set short line$line_count
    set wn $base.widgets.$short
    incr line_count

    # create the line
    frame $wn -height 3 -bd 1 -relief sunken
    grid  $wn -row 1 -column 1 -columnspan 1 -rowspan 1 -sticky wens -padx 2 -pady 2
    lower $wn

    # drag and dropability
    make_draggable $root $wn $short
    
    # configurability
    make_config_button $root $short line
}

#
# Create a new text (these are widgets not known by mc)
#
proc new_text {root} {
    global text_count

    if {$root == "."} { set base "" } else { set base $root }

    set short text$text_count
    set wn    $base.widgets.$short
    incr text_count

    label $wn -text "Text..."
    grid $wn -row 1 -column 1 -columnspan 1 -rowspan 1 
    make_draggable $root $wn $short
    make_config_button $root $short text
}

#
# Start up the GUI designer
#

proc gui_design {root components} {
    global props
    global new_dialog

    # May be created in layout_with_grid if reconfiguring
    catch {toplevel .gui-widgets}

    if {$root == "."} {
	set base ""
    } else {
	set base $root
    }

    if {$new_dialog} {
	reset_counters
    }
    # Work around Tk 4.1 bug
    frame $base.widgets.bug-work-around
    grid $base.widgets.bug-work-around -row 60 -column 60

    foreach i $components {
	set def_layout [catch "get_prop $root $i" val]
	if {$def_layout} {
	    set_widget_col $root $i 0
	    set_widget_row $root $i 0
	} 
	make_draggable $root $base.widgets.$i $i
	make_config_button $root $i widget
    }
    frame .gui-widgets.buttons
    button .gui-widgets.buttons.save -text "Save to: gui$root.tcl" -command "save_gui $root $root"
    button .gui-widgets.buttons.abort -text "abort" -command "exit"
    button .gui-widgets.buttons.newf -text "New border" -command "new_border $root"
    button .gui-widgets.buttons.newl -text "New line"   -command "new_line $root"
    button .gui-widgets.buttons.newt -text "New text"   -command "new_text $root"

    pack\
	.gui-widgets.buttons.save \
	.gui-widgets.buttons.abort \
	.gui-widgets.buttons.newf \
	.gui-widgets.buttons.newt \
	-side left -expand y
    pack .gui-widgets.buttons
}

#
# Attempt to layout a grided dialog.  If something fails, return 0
# to give the application the chance to run the GUI designer
#
proc layout_with_grid {dialog count} {
    global props
    global components
    global min_width
    global min_height
    global env
    global dialog_columns
    global dialog_rows
    global frame_count
    global text_count
    global line_count
    global new_dialog

    set expr "set saved_count \$props(.\$dialog.count)"

    set new_dialog 1
    if [catch "eval $expr"] {
	puts "Calling editor, reason: count"
	return 0
    }
    set bw .$dialog.widgets

    if {$saved_count != $count} {
	puts "Calling editor, reason: more widgets"
	return 0
    } 

    set new_dialog 0

    # Check if the user wants to modify this dialog
    if ![string compare $env(MC_EDIT) $dialog] {
	set modify_dialog 1
	toplevel .gui-widgets
    } else {
	set modify_dialog 0
    }
    
    # First, hack around the crash problem of Tk 4.2 beta 1
    frame .$dialog.widgets.work-around
    grid .$dialog.widgets.work-around -row 60 -column 60

    set dialog_columns $props(.$dialog.columns)
    set dialog_rows    $props(.$dialog.rows)
    for {set i 0} {$i <= $dialog_columns} {incr i} {
	column_space $bw $i
	create_column $bw $i $modify_dialog
    }
    for {set i 0} {$i <= $dialog_rows} {incr i} {
	row_space $bw $i
	create_row $bw $i $modify_dialog
    }
    grid .$dialog.widgets -column 0 -row 0 -ipadx 8 -ipady 8 -sticky nswe

    # 1. Load the borders (first, because they may cover other widgets)
    set frame_count $props(.$dialog.frames)
    for {set i 0} {$i < $frame_count} {incr i} {
	frame .$dialog.widgets.frame$i -relief $props(.$dialog.relief.frame$i) -borderwidth 2
	eval grid .$dialog.widgets.frame$i "$props(.$dialog.frame$i)"
	if {$modify_dialog} {
	    lower .$dialog.widgets.frame$i
	    make_draggable .$dialog .$dialog.widgets.frame$i frame$i 
	    make_config_button .$dialog frame$i frame	    
	}
    }

    # 1.1 Load the lines (before texts, since they may cover other widgets)
    if {![catch {set line_count $props(.$dialog.lines)}]} {
	for {set i 0} {$i < $line_count} {incr i} {
	    frame .$dialog.widgets.line$i -relief sunken -bd 1 -height 3
	    eval grid .$dialog.widgets.line$i "$props(.$dialog.line$i)"
	    if {$modify_dialog} {
		lower .$dialog.widgets.line$i
		make_draggable .$dialog .$dialog.widgets.line$i line$i 
		make_config_button .$dialog line$i line
	    }
	}
    }

    # 2. Load the components
    foreach i $components {
	eval grid .$dialog.widgets.$i "$props(.$dialog.props.$i)"
	raise .$dialog.widgets.$i 
    }

    # 3 . Load the texts
    set text_count $props(.$dialog.texts)
    for {set i 0} {$i < $text_count} {incr i} {
	label .$dialog.widgets.text$i -text $props(.$dialog.text.text$i)
	eval grid .$dialog.widgets.text$i "$props(.$dialog.text$i)"
	raise .$dialog.widgets.text$i 
	if {$modify_dialog} {
	    make_draggable .$dialog .$dialog.widgets.text$i text$i 
	#    make_config_button .$dialog text$i text
	}
    }

    if {$modify_dialog} { 
	puts "Calling editor, reason: modify_dialog set"
	return 0 
    }
    return 1
}

#
# For testing the GUI builder.  Not used by the Midnight Commander
#
proc mc_create_buttons {root} {
    if {$root == "."} { set base "" } else { set base $root }

    button $base.widgets.button#1 -text "Oki\ndoki\nmy\friends"
    button $base.widgets.button#2 -text "Cancel"
    entry  $base.widgets.entry#1
    radiobutton $base.widgets.radio#1 -text "Primera opcion" 
    radiobutton $base.widgets.radio#2 -text "Segunda opcion"
    radiobutton $base.widgets.radio#3 -text "Tercera opcion"
}

proc test_gui {} {
    global components
    button .a -text "A" 
    pack .a
    
    toplevel .hola

    create_gui_canvas .hola

    set components {button#1 button#2 entry#1 radio#1 radio#2 radio#3}
    mc_create_buttons .hola

    if [layout_with_grid hola 6] {
	puts corriendo
    } else {
	create_workspace .hola
	gui_design .hola $components
    }
}

# initialize
reset_counters

if ![info exists env(MC_EDIT)] {
    set env(MC_EDIT) non-existant-toplevel-never-hit
}

if [catch {set x $mc_running}] { set mc_running 0 }

if {$use_separate_gui_files} {
    if [catch "glob gui.*.tcl" files] {
	set files ""
    }

    foreach i $files {
	puts "loading $i..."
	source $i
    }
} else {
    source $LIBDIR/gui.tcl
}

if {$mc_running == 0} {
    test_gui 
}