Simple editor for diagrams

Arjen Markus (20 february 2009) When drawing a diagram using the Diagrams package in Tklib, I always use an edit-run-view cycle. As this is slightly annoying (edit the code, restart wish and see if the resulting diagram is to my liking), I thought a simple editor might be called for.

The script below is the barest program I could come up with to illustrate such an application. Just type in small diagram like:

box A
arrow "" 40
circle B
direction SE

in the text widget in the lower part and then press the Show diagram button, as a demonstration of the idea.

The blue circle and arrow show the "current position" and the direction - two properties used to position the next item in the diagram.

Next step: decorate the program with a proper menubar, perhaps hypertext help. Another idea: add an "animated" display (pause between in the handling of the diagram commands, with proper visual feedback so that you can see what line is responsible for what part of the diagram).

# editdiag.tcl --
#     Interactive diagram editor
# - Properly reset the diagram stuff
# - Nicer GUI
source draw_diagram.tcl
package require Diagrams 0.3
namespace import ::Diagrams::*

# ShowDiagram --
#     Draw the diagram via the commands entered in the text widget
proc ShowDiagram {} {

    .c delete all

    array set ::Diagrams::state $::original_state

    drawin .c
    eval [.t get 1.0 end]

    foreach {key x y} [currentpos] {break}

    .c create oval [expr {$x-3}] [expr {$y-3}] [expr {$x+3}] [expr {$y+3}] \
        -outline blue -fill white

    color blue
    arrow "" 40

# main --
#     Bring up the main window
grid [canvas .c -height 400 -width 600 -background white] -sticky news
grid [label .l -text "Type the commands for the diagram:"] -sticky w
grid [text .t -height 15] -sticky news
grid [button .b -text "Show diagram" -command ShowDiagram]

# Store the current state for resetting the diagram
set original_state [array get ::Diagrams::state]