Statistics
| Branch: | Revision:

root / src / tkenv / canvlbl.tcl @ fbe00e73

History | View | Annotate | Download (2.54 KB)

1
#=================================================================
2
#  CANVLBL.TCL - part of
3
#
4
#                     OMNeT++/OMNEST
5
#            Discrete System Simulation in C++
6
#
7
#=================================================================
8

    
9
#----------------------------------------------------------------#
10
#  Copyright (C) 1992-2008 Andras Varga
11
#
12
#  This file is distributed WITHOUT ANY WARRANTY. See the file
13
#  `license' for details on this and other legal matters.
14
#----------------------------------------------------------------#
15

    
16

    
17
# editCanvasLabel --
18
#
19
# Start editing a text item on a canvas. Uses an entry widget.
20
#
21
# This is a general-purpose proc, it doesn't know anything
22
# about internal GNED's data structures.
23
#
24
#  c    canvas
25
#  id   tag or id of text item
26
#  cmd  command to eval (with the edited string as extra arg)
27
#       when editing is committed. Canvas text item will be set to
28
#       the return value of the proc.
29
#
30
proc editCanvasLabel {c id cmd} {
31
    global tmp fonts
32

    
33
    set id [$c find withtag $id]
34
    set xy [$c coords $id]
35
    set txt  [$c itemcget $id -text]
36
    regexp {^'(.*)'$} $txt match txt ;# strip quotes (apostrophes)
37
    set font $fonts(normal)
38
    set anch [$c itemcget $id -anchor]
39
    set tmp(canvasentry_orig) $txt
40
    $c itemconfig $id -text ""
41
    catch {destroy $c.e}
42
    entry $c.e -bd 1 -relief solid -font $font
43
    $c.e insert end $txt
44
    $c.e select range 0 end
45
    set eid [$c create window [lindex $xy 0] [expr [lindex $xy 1]-1] -window $c.e -anchor $anch]
46
    resizeCanvasEntry $c.e
47
    bind $c.e <FocusOut> [list commitCanvasEntry $c $id $c.e $eid $cmd]
48
    bind $c.e <Return> [list commitCanvasEntry $c $id $c.e $eid $cmd]
49
    bind $c.e <Escape> "cancelCanvasEntry $c $id $c.e $eid"
50
    bind $c.e <KeyPress> "after idle \"resizeCanvasEntry $c.e\""
51
    focus $c.e
52
}
53

    
54

    
55
# resizeCanvasEntry --
56
#
57
# Private proc for editCanvasLabel
58
#
59
proc resizeCanvasEntry {w} {
60
    # winfo exists is needed because TAB key takes away focus and destroys $w
61
    if [winfo exists $w] {
62
       set txt [$w get]
63
       $w config -width [expr [string length $txt]+1]
64
    }
65
}
66

    
67

    
68
# commitCanvasEntry --
69
#
70
# Private proc for editCanvasLabel
71
#
72
proc commitCanvasEntry {c id e eid cmd} {
73
    # let $cmd modify the text from the entry
74
    set txt [$e get]
75
    $c delete $eid
76
    destroy $e
77
    set txt [eval $cmd [list $txt]]
78
    $c itemconfig $id -text $txt
79
}
80

    
81

    
82
# cancelCanvasEntry --
83
#
84
# Private proc for editCanvasLabel
85
#
86
proc cancelCanvasEntry {c id e eid} {
87
    global tmp
88
    $c itemconfig $id -text $tmp(canvasentry_orig)
89
    $c delete $eid
90
    destroy $e
91
}
92