summaryrefslogtreecommitdiff
path: root/itcl/itk/demos/itkedit
blob: b20aef7265cb1138412b86279d44a32102fd984e (plain)
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
#!/bin/sh
#\
exec itkwish $0
# ======================================================================
# Simple text editor built with [incr Widgets]
# ----------------------------------------------------------------------
#   AUTHOR:  Michael J. McLennan
#    CLASS:  Object-Oriented Programming with [incr Tcl]
# ======================================================================
package require Iwidgets 2.1

option add *edit.width 5i startupFile
option add *edit.height 4i startupFile
option add *Fileselectiondialog.width 4i startupFile
option add *Fileselectiondialog.height 5i startupFile

# ----------------------------------------------------------------------
set FileWindows 0

# ----------------------------------------------------------------------
#  Dialog boxes
# ----------------------------------------------------------------------
messagedialog .notice -title "itkedit: Notice" \
    -bitmap info -buttonboxpos e -modality application
.notice hide OK
.notice hide Help
.notice buttonconfigure Cancel -text "Dismiss"

messagedialog .confirm -title "itkedit: Confirm" \
    -bitmap questhead -modality application
.confirm hide Help
.confirm buttonconfigure OK -text "Yes"
.confirm buttonconfigure Cancel -text "No"

fileselectiondialog .files -title "itkedit: Files" \
    -childsitepos s -modality application
.files hide Help

set PaneMenu "[.files childsite].panes"
optionmenu $PaneMenu -labeltext "Edit Window:"
pack $PaneMenu -pady 6

# ----------------------------------------------------------------------
# USAGE:  file_load
#
# Initiates the process of loading a new text file for editing.
# Pops up a Fileselectiondialog, allowing the user to select a
# file for editing.  If the user pushes the "load" button, the
# file is loaded.
# ----------------------------------------------------------------------
proc file_load {} {
    global FileName PaneMenu

    .files buttonconfigure OK -text "Load"
    if {[.files activate]} {
        set fname [.files get]
        set cmd {
            set fid [open $fname r]
            set text [read $fid]
            close $fid
        }
        if {[catch $cmd err] != 0} {
            .notice configure -bitmap error \
                -text "Cannot load file \"$fname\":\n$err"
            .notice activate
            return
        }

        set pane [$PaneMenu get]
        set win [.edit childsite $pane]
        clear_text $win
        $win.text insert end $text
        $win.text configure -labeltext "file: $fname"

        set FileName($win) $fname
    }
}

# ----------------------------------------------------------------------
# USAGE:  file_save_as
#
# Initiates the process of saving the current text into a particular
# file.  Pops up a Fileselectiondialog, allowing the user to select
# a file for saving.  If the user pushes the "save" button, the
# file is saved.
# ----------------------------------------------------------------------
proc file_save_as {} {
    global FileName PaneMenu

    .files buttonconfigure OK -text "Save"
    if {[.files activate]} {
        set pane [$PaneMenu get]
        set win [.edit childsite $pane]

        set FileName($win) [.files get]

        file_save $win
    }
}

# ----------------------------------------------------------------------
# USAGE:  file_save <win>
#
# Saves the context of <win> into its associated file.  Does the
# dirty work to finish the file_save_as operation.
# ----------------------------------------------------------------------
proc file_save {win} {
    global FileName FileChanged

    set cmd {
        set fid [open $FileName($win) w]
        puts $fid [$win.text get 1.0 end]
        close $fid
        set FileChanged($win) 0
        $win.text configure -labeltext "file: $FileName($win)"
    }
    if {[catch $cmd err] != 0} {
        .notice configure -bitmap error \
            -text "Cannot save file \"$FileName($win)\":\n$err"
        .notice activate
    }
}

# ----------------------------------------------------------------------
# USAGE:  clear_text ?<win>?
#
# Clears the text area associated with <win>, making sure to save
# any pending changes.  If no <win> is specified, then all text
# areas are cleared.
# ----------------------------------------------------------------------
proc clear_text {{areas ""}} {
    global FileName FileChanged FileWindows

    if {$areas == ""} {
        for {set i 0} {$i < $FileWindows} {incr i} {
            set pane "area #[expr $i+1]"
            lappend areas [.edit childsite $pane]
        }
    }

    foreach win $areas {
        if {$FileChanged($win)} {
            set fname [file tail $FileName($win)]
            .confirm configure -text "File \"$fname\" has changed.\nSave changes?"
            if {[.confirm activate]} {
                file_save $win
            }
        }
        $win.text delete 1.0 end
        set FileChanged($win) 0
    }
}

# ----------------------------------------------------------------------
# USAGE:  split_view
#
# Adds another editing pane to the current editor.
# ----------------------------------------------------------------------
proc split_view {} {
    global FileName FileChanged FileWindows PaneMenu

    set pane "area #[incr FileWindows]"
    .edit add $pane -minimum 100
    $PaneMenu insert end $pane

    set win [.edit childsite $pane]

    set FileName($win) untitled.txt
    set FileChanged($win) 0

    scrolledtext $win.text -wrap none -labeltext "file: $FileName($win)" \
        -hscrollmode none -vscrollmode dynamic -visibleitems 1x1
    pack $win.text -expand yes -fill both

    bind [$win.text component text] <KeyPress> "
        set FileChanged($win) 1
    "
}

frame .mbar -borderwidth 2 -relief raised
pack .mbar -side top -fill x

# ----------------------------------------------------------------------
#  FILE menu
# ----------------------------------------------------------------------
menubutton .mbar.file -text "File" -underline 0 -menu .mbar.file.menu
pack .mbar.file -side left -padx 4

menu .mbar.file.menu
.mbar.file.menu add command -label "Load..." \
    -accelerator "  ^L" -underline 0 -command file_load
bind . <Control-KeyPress-l> { .mbar.file.menu invoke "Load..." }

.mbar.file.menu add command -label "Save As..." \
    -accelerator "  ^S" -underline 0 -command file_save_as
bind . <Control-KeyPress-s> { .mbar.file.menu invoke "Save As..." }

.mbar.file.menu add separator
.mbar.file.menu add command -label "Quit" \
    -accelerator "  ^Q" -underline 0 -command {clear_text; exit}
bind . <Control-KeyPress-q> { .mbar.file.menu invoke Quit }

# ----------------------------------------------------------------------
#  VIEW menu
# ----------------------------------------------------------------------
menubutton .mbar.view -text "View" -underline 0 -menu .mbar.view.menu
pack .mbar.view -side left -padx 4

menu .mbar.view.menu
.mbar.view.menu add command -label "Split" \
    -underline 0 -command split_view

# ----------------------------------------------------------------------
#  Editor
# ----------------------------------------------------------------------
panedwindow .edit -orient horizontal
pack .edit -expand yes -fill both

split_view

wm title . "itkedit"
wm protocol . WM_DELETE_WINDOW { .mbar.file.menu invoke Quit }

after idle {
    update idletasks
    wm minsize . [winfo reqwidth .] [winfo reqheight .]
}