Debian Codegraph @

tclpkg/tcldot/examples/neatoed

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