Debian Codegraph @

tclpkg/tcldot/demo/doted.tcl

raw
1 #!/bin/sh
2 # next line is a comment in tcl \
3 exec wish "$0" ${1+"$@"}
4
5 package require Tcldot
6
7 # doted - dot/gv graph editor - John Ellson ([email protected])
8 #
9 # Usage: doted <file.gv>
10 #
11 # doted displays the graph described in the input file and allows
12 # the user to add/delete nodes/edges, to modify their attributes,
13 # and to save the result.
14
15 global saveFill tk_library modified fileName printCommand g
16
17 # as the mouse moves over an object change its shading
18 proc mouse_anyenter {c} {
19 global tk_library saveFill
20 set item [string range [lindex [$c gettags current] 0] 1 end]
21 set saveFill [list $item [lindex [$c itemconfigure 1$item -fill] 4]]
22 $c itemconfigure 1$item -fill black \
23 -stipple @$tk_library/demos/images/gray25.xbm
24 }
25
26 # as the mouse moves out of an object restore its shading
27 proc mouse_anyleave {c} {
28 global saveFill
29 $c itemconfigure 1[lindex $saveFill 0] \
30 -fill [lindex $saveFill 1] -stipple {}
31 }
32
33 # if b1 is pressed over the background then start a node,
34 # if b1 is pressed over a node then start an edge
35 proc mouse_b1_press {c x y} {
36 global startObj graphtype
37 set x [$c canvasx $x]
38 set y [$c canvasy $y]
39 foreach item [$c find overlapping $x $y $x $y] {
40 foreach tag [$c gettags $item] {
41 if {[string first "node" $tag] == 1} {
42 set item [string range $tag 1 end]
43 if {[string equal $graphtype digraph]} {
44 set startObj [$c create line $x $y $x $y \
45 -tag $item -fill red -arrow last]
46 } {
47 set startObj [$c create line $x $y $x $y \
48 -tag $item -fill red]
49 }
50 return
51 }
52 }
53 }
54 set startObj [$c create oval [expr $x - 10] [expr $y - 10] \
55 [expr $x + 10] [expr $y + 10] -fill red -outline black]
56 }
57
58 # if node started by b1_press then move it,
59 # else extend edge
60 proc mouse_b1_motion {c x y} {
61 global startObj
62 set pos [$c coords $startObj]
63 if {[$c type $startObj] == "line"} {
64 $c coords $startObj [lindex $pos 0] [lindex $pos 1] \
65 [$c canvasx $x] [$c canvasy $y]
66 } {
67 $c move $startObj [expr [$c canvasx $x] - [lindex $pos 0] - 10] \
68 [expr [$c canvasy $y] - [lindex $pos 1] - 10]
69 }
70 }
71
72 # complete node or edge construction.
73 proc mouse_b1_release {c x y} {
74 global startObj modified g
75 set x [$c canvasx $x]
76 set y [$c canvasy $y]
77 set t [$c type $startObj]
78 if {$t == "line"} {
79 set tail [lindex [$c gettags $startObj] 0]
80 foreach item [$c find overlapping $x $y $x $y] {
81 foreach tag [$c gettags $item] {
82 set head [string range $tag 1 end]
83 if {[string first "node" $head] == 0} {
84 set e [$tail addedge $head]
85 $c dtag $startObj $tail
86 $c addtag 1$e withtag $startObj
87 $c itemconfigure $startObj -fill black
88 set modified 1
89 set startObj {}
90 return
91 }
92 }
93 }
94 # if we get here then edge isn't terminating on a node
95 $c delete $startObj
96 } {
97 set n [$g addnode]
98 $c addtag 1$n withtag $startObj
99 $c itemconfigure $startObj -fill white
100 set modified 1
101 }
102 set startObj {}
103 }
104
105 proc loadFileByName {c name} {
106 global modified
107 if {$modified} {
108 confirm "Current graph has been modified. Shall I overwrite it?" \
109 "loadFileByNameDontAsk $c $name"
110 } {
111 loadFileByNameDontAsk $c $name
112 }
113 }
114
115 proc loadFileByNameDontAsk {c name} {
116 global fileName g
117 $g delete
118 $c delete all
119 set modified 0
120 if {[string first / $name] == 0} {
121 set fileName $name
122 } {
123 if {[pwd] == "/"} {
124 set fileName /$name
125 } {
126 set fileName [pwd]/$name
127 }
128 }
129 if {[catch {open $fileName r} f]} {
130 warning "Unable to open file: $fileName"
131 }
132 if {[catch {dotread $f} g]} {
133 warning "Invalid .gv file: $fileName"
134 close $f
135 }
136 close $f
137 $g layout
138 eval [$g render]
139 $c configure -scrollregion [$c bbox all]
140 }
141
142 proc resize_canvas {c w h} {
143 $c configure -scrollregion [$c bbox all]
144 }
145
146 proc update_entry {w x y} {
147 $w.entry delete 0 end
148 $w.entry insert end [$w.l.list get @$x,$y]
149 }
150
151 # doesn't work well with window managers that position initial window
152 # on the left because then all popups get obscured
153 #
154 #proc positionWindow {w} {
155 # set pos [split [wm geometry .] +]
156 # set x [expr [lindex $pos 1] - 350]
157 # set y [expr [lindex $pos 2] + 20]
158 # wm geometry $w +$x+$y
159 #}
160
161 proc loadFile {c} {
162 global fileName
163
164 set types {
165 {{GV Graph Files} {.gv}}
166 {{DOT Graph Files} {.dot}}
167 {{All Files} *}
168 }
169 set fn [tk_getOpenFile \
170 -defaultextension .gv \
171 -filetypes $types \
172 -initialfile $fileName]
173 if {[string length $fn]} {
174 loadFileByName $c $fn
175 }
176 }
177
178 proc saveFile {type} {
179 global fileName
180 if {$fileName == {}} {
181 saveFileAs $type
182 } {
183 saveFileByName $fileName $type
184 }
185 }
186
187 proc saveFileByName {name type} {
188 global fileName
189 if {$name != $fileName && [file exists $name]} {
190 confirm "File exists. Shall I overwrite it?" \
191 "saveFileByNameDontAsk $name $type"
192 } {
193 saveFileByNameDontAsk $name $type
194 }
195 }
196
197 proc saveFileByNameDontAsk {name type} {
198 global modified fileName g
199 if {[catch {open $name w} f]} {
200 warning "Unable to open file for write:\n$name; return"
201 }
202 if {$type == "gv"} {
203 set type canon
204 set fileName $name
205 set modified 0
206 }
207 $g write $f $type
208 close $f
209 message "Graph written to:\n$name"
210 }
211
212 proc saveFileAs {type} {
213 global fileName
214
215 set cmap {{{CMAP Image Map Files} {.cmap}} {{All Files} *}}
216 set dot {{{DOT Graph Files} {.dot}} {{All Files} *}}
217 set fig {{{FIG Image Files} {.fig}} {{All Files} *}}
218 set gif {{{GIF Image Files} {.gif}} {{All Files} *}}
219 set gv {{{GV Graph Files} {.gv}} {{All Files} *}}
220 set jpg {{{JPG Image Files} {.jpg}} {{All Files} *}}
221 set mif {{{MIF Image Files} {.mif}} {{All Files} *}}
222 set pcl {{{PCL Image Files} {.pcl}} {{All Files} *}}
223 set pdf {{{PDF Image Files} {.pdf}} {{All Files} *}}
224 set png {{{PNG Image Files} {.png}} {{All Files} *}}
225 set ps {{{PostScript Files} {.ps}} {{All Files} *}}
226 set svg {{{SVG Image Files} {.svg}} {{All Files} *}}
227 set tiff {{{TIFF Image Files} {.tiff}} {{All Files} *}}
228 set vml {{{VML Image Files} {.vml}} {{All Files} *}}
229 set vtx {{{VTX Image Files} {.vtx}} {{All Files} *}}
230
231 set fn [tk_getSaveFile \
232 -defaultextension .$type \
233 -filetypes [set $type] \
234 -initialdir [file dirname $fileName] \
235 -initialfile [file tail [file rootname $fileName]].$type]
236 if {[string length $fn]} {
237 saveFileByNameDontAsk $fn $type
238 }
239 }
240
241 proc print {} {
242 global g printCommand
243 if {[catch {open "| $printCommand &" w} f]} {
244 warning "Unable to open pipe to printer command:\n$printCommand; return"
245 }
246 $g write $f ps
247 close $f
248 message "Graph printed to:\n$printCommand"
249 }
250
251 proc setPrinterCommand {w} {
252 global printCommand
253 set printCommand [$w.printCommand get]
254 message "Printer command changed to:\n$printCommand"
255 destroy $w
256 }
257
258 proc printSetup {} {
259 global printCommand
260 set w .printer
261 catch {destroy $w}
262 toplevel $w
263 # positionWindow $w
264 wm title $w "Printer"
265 wm iconname $w "Printer"
266 label $w.message -text "Printer command:"
267 frame $w.spacer -height 3m -width 20
268 entry $w.printCommand
269 $w.printCommand insert end $printCommand
270 bind $w.printCommand <Return> "setPrinterCommand $w"
271 frame $w.buttons
272 button $w.buttons.confirm -text OK -command "setPrinterCommand $w"
273 button $w.buttons.cancel -text Cancel -command "destroy $w"
274 pack $w.buttons.confirm $w.buttons.cancel -side left -expand 1
275 pack $w.message $w.spacer $w.printCommand -side top -anchor w
276 pack $w.buttons -side bottom -expand y -fill x -pady 2m
277 }
278
279 proc confirm {msg cmd} {
280 set w .confirm
281 catch {destroy $w}
282 toplevel $w
283 # positionWindow $w
284 wm title $w "Confirm"
285 wm iconname $w "Confirm"
286 label $w.message -text "\n$msg\n"
287 frame $w.spacer -height 3m -width 20
288 frame $w.buttons
289 button $w.buttons.confirm -text OK -command "$cmd; destroy $w"
290 button $w.buttons.cancel -text Cancel -command "destroy $w"
291 pack $w.buttons.confirm $w.buttons.cancel -side left -expand 1
292 pack $w.message $w.spacer -side top -anchor w
293 pack $w.buttons -side bottom -expand y -fill x -pady 2m
294 }
295
296 proc message {m} {
297 set w .message
298 catch {destroy $w}
299 toplevel $w
300 # positionWindow $w
301 wm title $w "Message"
302 wm iconname $w "Message"
303 label $w.message -text "\n$m\n"
304 pack $w.message -side top -anchor w
305 update
306 after 2000 "destroy $w"
307 }
308
309 proc warning {m} {
310 set w .warning
311 catch {destroy $w}
312 toplevel $w
313 # positionWindow $w
314 wm title $w "Warning"
315 wm iconname $w "Warning"
316 label $w.message -text "\nWarning:\n\n$m"
317 pack $w.message -side top -anchor w
318 update
319 after 2000 "destroy $w"
320 }
321
322 proc setoneattribute {w d a s} {
323 set aa [$w.e$a.a get]
324 if {$aa == {}} {
325 error "no attribute name set"
326 } {
327 set v [$w.e$a.v get]
328 eval $s $aa $v
329 }
330 if {$a == {}} {
331 destroy $w.e
332 addEntryPair $w $d $aa $v $s
333 addEntryPair $w d {} {} $s
334 }
335 }
336
337 proc addEntryPair {w d a v s} {
338 pack [frame $w.e$a] -side top
339 pack [entry $w.e$a.a] [entry $w.e$a.v] -side left
340 if {$a != {}} {
341 $w.e$a.a insert end $a
342 $w.e$a.a configure -state disabled -relief flat
343 $w.e$a.v insert end $v
344 if {$d != "d"} {
345 $w.e$a.v configure -state disabled -relief flat
346 }
347 }
348 bind $w.e$a.a <Return> "focus $w.e$a.v"
349 bind $w.e$a.v <Return> [list setoneattribute $w $d $a $s]
350 pack $w.e$a -side top
351 focus $w.e$a.a
352 }
353
354 proc deleteobj {c o} {
355 if {[string first "node" $o] == 0} {
356 foreach e [$o listedges] {
357 $c delete 1$e
358 $c delete 0$e
359 $e delete
360 }
361 }
362 $c delete 1$o
363 $c delete 0$o
364 $o delete
365 }
366
367 # open a requestor for object $o,
368 # deletable if $d is not null,
369 # command to list attribute in $l
370 # command to query attributes in $q
371 # command to set attributes in $s
372 proc setAttributesWidget {c o d l q s} {
373 set w .attributes
374 catch {destroy $w}
375 toplevel $w
376 # positionWindow $w
377 wm title $w "[$o showname] Attributes"
378 wm iconname $w "Attributes"
379 foreach a [eval $l] {
380 if {[catch {eval $q $a} v]} {set v {}}
381 addEntryPair $w $d $a $v $s
382 }
383 addEntryPair $w d {} {} $s
384 frame $w.spacer -height 3m -width 20
385 frame $w.buttons
386 if {$d == "d"} {
387 button $w.buttons.delete -text Delete -command "deleteobj $c $o; destroy $w"
388 pack $w.buttons.delete -side left -expand 1
389 }
390 button $w.buttons.dismiss -text OK -command "destroy $w"
391 pack $w.buttons.dismiss -side left -expand 1
392 pack $w.buttons -side bottom -expand y -fill x -pady 2m
393 }
394
395 # open a requestor according to the type of graph object $obj, to allow the user to read and set attributions
396 proc setAttributes {c obj} {
397 global g
398 if {$obj == {}} {
399 set obj [string range [lindex [$c gettags current] 0] 1 end]
400 }
401 set type [string range $obj 0 3]
402 if {$type == "node" || $type == "edge"} {
403 if {[string length $obj] > 4} {
404 setAttributesWidget $c $obj d \
405 "$obj listattributes" \
406 "$obj queryattributes" \
407 "$obj setattributes"
408 } {
409 setAttributesWidget $c $obj {} \
410 "$g list[set type]attributes" \
411 "$g query[set type]attributes" \
412 "$g set[set type]attributes"
413 }
414 } {
415 setAttributesWidget $c $g {} \
416 "$g listattributes" \
417 "$g queryattributes" \
418 "$g setattributes"
419 }
420 }
421
422 # unconditionally remove any old graph and canvas contents, the create a new graph of $type
423 proc newGraphDontAsk {c type} {
424 global modified g graphtype
425 set graphtype $type
426 $c delete all
427 set modified 0
428 if {[info exists g]} {$g delete}
429 set g [dotnew $type]
430 }
431
432 # upon confirmation, remove any old graph and canvas contents, the create a new graph of $type
433 proc newGraph {c type} {
434 global modified
435 if {$modified} {
436 confirm "Current graph has been modified. Shall I continue?" \
437 "newGraphDontAsk $c $type"
438 } {
439 newGraphDontAsk $c $type
440 }
441 }
442
443 # generate a new graph layout and update rendering on the canvas
444 # this proc is attached to the green button to the lower right of the window
445 proc layout {c} {
446 global g
447 $c delete all
448 $g layout
449 eval [$g render]
450 $c configure -scrollregion [$c bbox all]
451 }
452
453 # generate a help window with $msg as the contents
454 proc help {msg} {
455 set w .help
456 catch {destroy $w}
457 toplevel $w
458 # positionWindow $w
459 wm title $w "DotEd Help"
460 wm iconname $w "DotEd"
461 frame $w.menu -relief raised -bd 2
462 pack $w.menu -side top -fill x
463 label $w.msg \
464 -font -Adobe-helvetica-medium-r-normal--*-140-*-*-*-*-*-* \
465 -wraplength 4i -justify left -text $msg
466 pack $w.msg -side top
467 frame $w.buttons
468 pack $w.buttons -side bottom -expand y -fill x -pady 2m
469 button $w.buttons.dismiss -text Dismiss -command "destroy $w"
470 pack $w.buttons.dismiss -side left -expand 1
471 }
472
473 # proc that supports zoom in/out events
474 proc zoom {c fact} {
475 upvar #0 $c data
476 set x [$c canvasx [expr {[winfo pointerx $c] - [winfo rootx $c]}]]
477 set y [$c canvasy [expr {[winfo pointery $c] - [winfo rooty $c]}]]
478 $c scale all $x $y $fact $fact
479 set data(zdepth) [expr {$data(zdepth) * $fact}]
480 after cancel $data(idle)
481 set data(idle) [after idle "zoomupdate $c"]
482 }
483
484 # update all text strings after zoom operation is complete
485 proc zoomupdate {c} {
486 upvar #0 $c data
487 # adjust fonts
488 foreach {i} [$c find all] {
489 if { ! [string equal [$c type $i] text]} {continue}
490 set fontsize 0
491 # get original fontsize and text from tags
492 # if they were previously recorded
493 foreach {tag} [$c gettags $i] {
494 scan $tag {_f%d} fontsize
495 scan $tag "_t%\[^\0\]" text
496 }
497 # if not, then record current fontsize and text
498 # and use them
499 set font [$c itemcget $i -font]
500 if {!$fontsize} {
501 set text [$c itemcget $i -text]
502 if {[llength $font] < 2} {
503 #new font API
504 set fontsize [font actual $font -size]
505 } {
506 #old font API
507 set fontsize [lindex $font 1]
508 }
509 $c addtag _f$fontsize withtag $i
510 $c addtag _t$text withtag $i
511 }
512 # scale font
513 set newsize [expr {int($fontsize * $data(zdepth))}]
514 if {abs($newsize) >= 4} {
515 if {[llength $font] < 2} {
516 #new font api
517 font configure $font -size $newsize
518 } {
519 #old font api
520 lreplace $font 1 1 $newsize
521 }
522 $c itemconfigure $i -font $font -text $text
523 } {
524 # suppress text if too small
525 $c itemconfigure $i -text {}
526 }
527 }
528 set bbox [$c bbox all]
529 if {[llength $bbox]} {
530 $c configure -scrollregion $bbox
531 } {
532 $c configure -scrollregion [list -4 -4 \
533 [expr {[winfo width $c]-4}] \
534 [expr {[winfo height $c]-4}]]
535 }
536 }
537
538 #--------------------------------------------------------------------------
539 set help_about "DotEd - Dot Graph Editor
540 Copyright (C) 1995 AT&T Bell Labs
541 (C) 1996 Lucent Technologies
542
543 Written by: John Ellson ([email protected])
544 and: Stephen North ([email protected])
545
546 DotEd provides for the graphical editing of
547 directed graphs. Once a graph has been manually
548 entered then the dot layout algorithm can be applied
549 by clicking on the button in the lower right corner
550 of the window."
551
552 set help_mouse "Button-1: When the cursor is over the
553 background Button-1-Press will start a node,
554 Button-1-Motion (dragging the mouse with
555 Button-1 still down) will move it and
556 Button-1-Release will complete the node
557 insertion into the graph.
558
559 When the cursor is over an existing node
560 then Button-1-Press will start an edge from
561 that node. Button-1-Motion will extend the
562 edge and Button-1-Release over a different
563 node will complete the edge.
564
565 Button-2: Button-2-Motion (click and drag) will
566 reposition the canvas under the window.
567
568 Button-3: When Button-3 is clicked over a
569 node or edge the attribute editor will
570 be opened on that object.
571
572 Scrollwheel: Zooms canvas in/out.
573
574 Once a graph has been manually entered then
575 the dot layout algorithm can be applied by
576 clicking on the button in the lower right
577 corner of the window."
578
579 #--------------------------------------------------------------------------
580
581 #initialize some globals
582 set startObj {}
583 set saveFill {}
584 set modified 0
585 set fileName {no_name}
586 set printCommand {lpr}
587 set zfact 1.1
588
589 # create main window
590 wm title . "DotEd"
591 wm iconname . "DotEd"
592 wm minsize . 120 100
593 wm geometry . 400x300
594 frame .m -relief raised -borderwidth 1
595 frame .a
596 frame .b
597 set c [canvas .a.c \
598 -cursor crosshair \
599 -xscrollcommand ".b.h set" \
600 -yscrollcommand ".a.v set" \
601 -width 0 \
602 -height 0 \
603 -borderwidth 0]
604 scrollbar .b.h \
605 -orient horiz \
606 -relief sunken \
607 -command "$c xview"
608 scrollbar .a.v \
609 -relief sunken \
610 -command "$c yview"
611 button .b.layout \
612 -width [.a.v cget -width] \
613 -height [.b.h cget -width] \
614 -foreground green \
615 -activeforeground green\
616 -bitmap @$tk_library/demos/images/gray25.xbm \
617 -command "layout $c"
618
619 # initialize zoom state
620 set [set c](zdepth) 1.0
621 set [set c](idle) {}
622
623 # create graph structure and set global "g"
624 newGraphDontAsk $c digraph
625
626 # canvas bindings
627 bind $c <Configure> "resize_canvas $c %w %h"
628 bind $c <ButtonPress-1> "mouse_b1_press $c %x %y"
629 bind $c <B1-Motion> "mouse_b1_motion $c %x %y"
630 bind $c <ButtonRelease-1> "mouse_b1_release $c %x %y"
631 bind $c <Button-2> "$c scan mark %x %y"
632 bind $c <B2-Motion> "$c scan dragto %x %y 1"
633 bind $c <Button-3> "setAttributes $c {}"
634 bind $c <Button-4> "zoom $c $zfact"
635 bind $c <Button-5> "zoom $c [expr {1.0/$zfact}]"
636
637 # canvas item bindings
638 $c bind all <Any-Enter> "mouse_anyenter $c"
639 $c bind all <Any-Leave> "mouse_anyleave $c"
640
641 menubutton .m.file -text "File" -underline 0 -menu .m.file.m
642 menu .m.file.m
643 .m.file.m add command -label "Load ..." -underline 0 \
644 -command "loadFile $c"
645 .m.file.m add command -label "New - directed" -underline 0 \
646 -command "newGraph $c digraph"
647 .m.file.m add command -label "New - undirected" -underline 6 \
648 -command "newGraph $c graph"
649 .m.file.m add command -label "Save" -underline 0 \
650 -command "saveFile gv"
651 .m.file.m add command -label "Save As ..." -underline 5 \
652 -command "saveFileAs gv"
653 .m.file.m add separator
654 .m.file.m add cascade -label "Export" -underline 1 \
655 -menu .m.file.m.export
656 menu .m.file.m.export
657 .m.file.m.export add command -label "CMAP ..." -underline 0 \
658 -command "saveFileAs cmap"
659 .m.file.m.export add command -label "FIG ..." -underline 0 \
660 -command "saveFileAs fig"
661 .m.file.m.export add command -label "GIF ..." -underline 0 \
662 -command "saveFileAs gif"
663 .m.file.m.export add command -label "MIF ..." -underline 0 \
664 -command "saveFileAs mif"
665 .m.file.m.export add command -label "PDF ..." -underline 0 \
666 -command "saveFileAs pdf"
667 .m.file.m.export add command -label "PNG ..." -underline 0 \
668 -command "saveFileAs png"
669 .m.file.m.export add command -label "PS ..." -underline 0 \
670 -command "saveFileAs ps"
671 .m.file.m.export add command -label "SVG ..." -underline 0 \
672 -command "saveFileAs svg"
673 .m.file.m.export add command -label "TIFF ..." -underline 0 \
674 -command "saveFileAs tiff"
675 .m.file.m.export add command -label "VML ..." -underline 0 \
676 -command "saveFileAs vml"
677 .m.file.m.export add command -label "VTX ..." -underline 0 \
678 -command "saveFileAs vtx"
679 .m.file.m add separator
680 .m.file.m add command -label "Print Setup ..." -underline 0 \
681 -command "printSetup"
682 .m.file.m add command -label "Print" -underline 0 \
683 -command "print"
684 .m.file.m add separator
685 .m.file.m add command -label "Exit" -underline 0 -command "exit"
686 menubutton .m.graph -text "Graph" -underline 0 -menu .m.graph.m
687 menu .m.graph.m
688 .m.graph.m add command -label "Graph Attributes" -underline 0 \
689 -command "setAttributes $c graph"
690 .m.graph.m add command -label "Node Attributes" -underline 0 \
691 -command "setAttributes $c node"
692 .m.graph.m add command -label "Edge Attributes" -underline 0 \
693 -command "setAttributes $c edge"
694 menubutton .m.help -text "Help" -underline 0 -menu .m.help.m
695 menu .m.help.m
696 .m.help.m add command -label "About DotEd" -underline 0 \
697 -command {help $help_about}
698 .m.help.m add command -label "Mouse Operations" -underline 0 \
699 -command {help $help_mouse}
700
701 pack append .m .m.file {left} .m.graph {left} .m.help {right}
702 pack append .a $c {left expand fill} .a.v {right filly}
703 pack append .b .b.h {left expand fillx} .b.layout {right}
704 pack append . .m {top fillx} .a {expand fill} .b {bottom fillx}
705 tk_menuBar .m.file .m.graph .m.help
706
707 if {$argc} {loadFileByNameDontAsk $c [lindex $argv 0]}