0
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
59
|
3 |
All Rights Reserved
|
0
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
TextCollector subclass:#Workspace
|
|
14 |
instanceVariableNames:'doItAction
|
59
|
15 |
codeStartPosition
|
|
16 |
errorFgColor errorBgColor'
|
|
17 |
classVariableNames:'DefaultErrorForegroundColor DefaultErrorBackgroundColor'
|
0
|
18 |
poolDictionaries:''
|
|
19 |
category:'Interface-Workspace'
|
|
20 |
!
|
|
21 |
|
|
22 |
Workspace comment:'
|
5
|
23 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
59
|
24 |
All Rights Reserved
|
0
|
25 |
|
123
|
26 |
$Header: /cvs/stx/stx/libwidg/Workspace.st,v 1.21 1995-05-07 01:58:55 claus Exp $
|
0
|
27 |
'!
|
|
28 |
|
32
|
29 |
!Workspace class methodsFor:'documentation'!
|
|
30 |
|
38
|
31 |
copyright
|
|
32 |
"
|
|
33 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
59
|
34 |
All Rights Reserved
|
38
|
35 |
|
|
36 |
This software is furnished under a license and may be used
|
|
37 |
only in accordance with the terms of that license and with the
|
|
38 |
inclusion of the above copyright notice. This software may not
|
|
39 |
be provided or otherwise made available to, or used by, any
|
|
40 |
other person. No title to or ownership of the software is
|
|
41 |
hereby transferred.
|
|
42 |
"
|
|
43 |
!
|
|
44 |
|
|
45 |
version
|
|
46 |
"
|
123
|
47 |
$Header: /cvs/stx/stx/libwidg/Workspace.st,v 1.21 1995-05-07 01:58:55 claus Exp $
|
38
|
48 |
"
|
|
49 |
!
|
|
50 |
|
32
|
51 |
documentation
|
|
52 |
"
|
|
53 |
a view for editable text which can evaluate expressions.
|
38
|
54 |
I.e. its basically a view for editable text, with added
|
|
55 |
'doIt', 'printIt' and 'inspectIt' functions on the popup-menu.
|
|
56 |
The action to be performed on doIt is defined by a block,
|
58
|
57 |
which can be defined by the owner of this view.
|
60
|
58 |
(thus you can put a workspace into more complex widgets, and
|
|
59 |
control what should happen on 'doIt').
|
58
|
60 |
|
38
|
61 |
A useful default action is setup, which simply evaluates the
|
|
62 |
selection as a smalltalk expression. (but, a lisp or prolog
|
|
63 |
view could define its own action ...)
|
32
|
64 |
|
123
|
65 |
|
|
66 |
Caveat:
|
|
67 |
in this version, Workspace does not yet support doIt in MVC setups.
|
|
68 |
For now, simulate this by setting the doItAction, to notify the
|
|
69 |
model manually about the doIt.
|
|
70 |
|
|
71 |
|
32
|
72 |
instance variables:
|
|
73 |
|
121
|
74 |
doItAction <Block> block to evaluate for doIt
|
123
|
75 |
|
121
|
76 |
codeStartPosition temporary
|
|
77 |
|
|
78 |
errorFgColor <Color> fg-Color to be used when highlighting errors
|
|
79 |
|
|
80 |
errorBgColor <Color> bg-Color to be used when highlighting errors
|
58
|
81 |
|
121
|
82 |
|
|
83 |
styleSheet values:
|
|
84 |
|
|
85 |
codeErrorSelectionForegroundColor fg color to highlight errors
|
123
|
86 |
(default: selection fg)
|
121
|
87 |
codeErrorSelectionBackgroundColor bg color to highlight errors
|
123
|
88 |
(default: selection bg)
|
32
|
89 |
"
|
|
90 |
! !
|
|
91 |
|
0
|
92 |
!Workspace class methodsFor:'getting a System Workspace'!
|
|
93 |
|
32
|
94 |
open
|
0
|
95 |
"launch a new workspace"
|
|
96 |
|
23
|
97 |
|scrollable scr topView workspace f|
|
0
|
98 |
|
|
99 |
topView := StandardSystemView label:'Workspace' minExtent:(100 @ 100).
|
23
|
100 |
|
|
101 |
scrollable := true.
|
|
102 |
scrollable ifTrue:[
|
59
|
103 |
scr := HVScrollableView "ScrollableView" for:self in:topView.
|
|
104 |
scr origin:(0.0 @ 0.0) corner:(1.0 @ 1.0).
|
|
105 |
workspace := scr scrolledView.
|
23
|
106 |
] ifFalse:[
|
59
|
107 |
workspace := super origin:(0.0 @ 0.0) corner:(1.0 @ 1.0) in:topView.
|
|
108 |
workspace level:0.
|
23
|
109 |
].
|
0
|
110 |
|
58
|
111 |
"adjust topViews extent according to my font"
|
0
|
112 |
f := workspace font.
|
|
113 |
topView extent:((f widthOf:'x') * 40) @ (f height * 10).
|
7
|
114 |
topView open.
|
0
|
115 |
^ topView
|
|
116 |
|
58
|
117 |
"
|
|
118 |
Workspace open
|
|
119 |
"
|
0
|
120 |
! !
|
|
121 |
|
59
|
122 |
!Workspace class methodsFor:'defaults'!
|
|
123 |
|
|
124 |
updateStyleCache
|
60
|
125 |
DefaultErrorForegroundColor := StyleSheet colorAt:'codeErrorSelectionForegroundColor'.
|
|
126 |
DefaultErrorBackgroundColor := StyleSheet colorAt:'codeErrorSelectionBackgroundColor'.
|
59
|
127 |
! !
|
|
128 |
|
0
|
129 |
!Workspace methodsFor:'initialize / release'!
|
|
130 |
|
|
131 |
initialize
|
|
132 |
super initialize.
|
3
|
133 |
|
32
|
134 |
showMatchingParenthesis := true.
|
|
135 |
|
58
|
136 |
self initializeDoITAction.
|
59
|
137 |
!
|
58
|
138 |
|
59
|
139 |
initStyle
|
|
140 |
super initStyle.
|
|
141 |
|
|
142 |
errorFgColor := DefaultErrorForegroundColor.
|
|
143 |
errorFgColor isNil ifTrue:[errorFgColor := selectionFgColor].
|
|
144 |
errorBgColor := DefaultErrorBackgroundColor.
|
|
145 |
errorBgColor isNil ifTrue:[
|
|
146 |
device hasColors ifTrue:[
|
|
147 |
errorBgColor := Color red
|
|
148 |
] ifFalse:[
|
|
149 |
errorBgColor := selectionBgColor
|
|
150 |
]
|
|
151 |
].
|
0
|
152 |
!
|
|
153 |
|
97
|
154 |
editMenu
|
119
|
155 |
|m sub idx|
|
38
|
156 |
|
97
|
157 |
m := super editMenu.
|
0
|
158 |
|
38
|
159 |
"
|
58
|
160 |
workspaces support #doIt, #printIt and #inspectIt
|
|
161 |
add them after paste.
|
38
|
162 |
"
|
97
|
163 |
idx := m indexOf:#paste.
|
119
|
164 |
idx ~~ 0 ifTrue:[
|
|
165 |
m addLabels:(resources array:#('-' 'doIt' 'printIt' 'inspectIt'))
|
|
166 |
selectors:#(nil doIt printIt inspectIt)
|
|
167 |
after:idx.
|
|
168 |
].
|
32
|
169 |
|
97
|
170 |
self hasSelection ifFalse:[
|
121
|
171 |
m disable:#printIt; disable:#doIt; disable:#inspectIt
|
97
|
172 |
].
|
|
173 |
|
|
174 |
^ m.
|
0
|
175 |
!
|
|
176 |
|
58
|
177 |
initializeDoITAction
|
|
178 |
"set up the block to be evaluated for doIts.
|
|
179 |
This is done here in a separate method to allow easier
|
|
180 |
redefinition in subclasses"
|
|
181 |
|
|
182 |
doItAction := [:theCode |
|
59
|
183 |
Compiler
|
|
184 |
evaluate:theCode
|
|
185 |
in:nil
|
|
186 |
receiver:nil
|
|
187 |
notifying:self
|
|
188 |
logged:true
|
|
189 |
ifFail:nil
|
32
|
190 |
].
|
0
|
191 |
! !
|
|
192 |
|
|
193 |
!Workspace methodsFor:'accessing'!
|
|
194 |
|
|
195 |
doItAction:aBlock
|
|
196 |
"define the action to be performed when 'doIt' is selected"
|
|
197 |
|
|
198 |
doItAction := aBlock
|
|
199 |
!
|
|
200 |
|
59
|
201 |
doItAction
|
|
202 |
"return the action to be performed when 'doIt' is selected"
|
0
|
203 |
|
59
|
204 |
^ doItAction
|
0
|
205 |
! !
|
|
206 |
|
|
207 |
!Workspace methodsFor:'error handling'!
|
|
208 |
|
59
|
209 |
highlightingErrorPosition:relPos to:relEndPos do:aBlock
|
|
210 |
"evaluate aBlock while some selection is shown highlighted with error colors."
|
58
|
211 |
|
59
|
212 |
|absPosition fg bg|
|
0
|
213 |
|
58
|
214 |
"
|
|
215 |
change color of selection & hide cursor
|
|
216 |
"
|
0
|
217 |
fg := selectionFgColor.
|
|
218 |
bg := selectionBgColor.
|
|
219 |
selectionBgColor := errorBgColor.
|
|
220 |
selectionFgColor := errorFgColor.
|
58
|
221 |
self hideCursor.
|
0
|
222 |
|
58
|
223 |
"
|
|
224 |
select the text - relEndPos may be nil in which case the whole line is selected
|
|
225 |
we have to adjust the positions given by the compiler, since they
|
|
226 |
are relative to the texts start (the compiler did stream-read the code).
|
|
227 |
"
|
0
|
228 |
codeStartPosition isNil ifTrue:[codeStartPosition := 1].
|
|
229 |
absPosition := codeStartPosition + relPos - 1.
|
|
230 |
relEndPos isNil ifTrue:[
|
59
|
231 |
self selectLineWhereCharacterPosition:absPosition
|
0
|
232 |
] ifFalse:[
|
59
|
233 |
self selectFromCharacterPosition:absPosition to:(codeStartPosition + relEndPos - 1)
|
0
|
234 |
].
|
|
235 |
self makeSelectionVisible.
|
|
236 |
|
|
237 |
device synchronizeOutput.
|
58
|
238 |
|
59
|
239 |
aBlock valueNowOrOnUnwindDo:[
|
|
240 |
"
|
|
241 |
undo selection color change and show cursor again
|
|
242 |
"
|
|
243 |
selectionFgColor := fg.
|
|
244 |
selectionBgColor := bg.
|
|
245 |
self showCursor.
|
0
|
246 |
].
|
|
247 |
!
|
|
248 |
|
116
|
249 |
warning:aString position:relPos to:relEndPos from:aCompiler
|
32
|
250 |
"compiler notifies us of a warning - same behavior as error"
|
0
|
251 |
|
95
|
252 |
self error:aString position:relPos to:relEndPos asWarning:true
|
0
|
253 |
!
|
|
254 |
|
116
|
255 |
error:aString position:relPos to:relEndPos from:aCompiler
|
59
|
256 |
"compiler notifies us of an error; hilight the error (relPos to relEndPos)
|
|
257 |
and show a Box asking for continue/abort."
|
|
258 |
|
95
|
259 |
^ self error:aString position:relPos to:relEndPos asWarning:false
|
|
260 |
!
|
|
261 |
|
|
262 |
error:aString position:relPos to:relEndPos asWarning:asWarning
|
|
263 |
"compiler notifies us of an error; hilight the error (relPos to relEndPos)
|
|
264 |
and show a Box asking for continue/abort."
|
|
265 |
|
59
|
266 |
|answer|
|
|
267 |
|
|
268 |
self highlightingErrorPosition:relPos to:relEndPos do:[
|
|
269 |
|box|
|
|
270 |
|
|
271 |
"
|
|
272 |
ask if we should abort or continue
|
|
273 |
"
|
|
274 |
box := YesNoBox title:aString
|
|
275 |
yesText:(resources string:'continue')
|
|
276 |
noText:(resources string:'abort').
|
|
277 |
|
95
|
278 |
asWarning ifTrue:[
|
|
279 |
box label:'Warning'
|
|
280 |
] ifFalse:[
|
|
281 |
box label:'Error'.
|
|
282 |
].
|
|
283 |
box form:(WarningBox iconBitmap).
|
59
|
284 |
answer := box confirm.
|
|
285 |
].
|
|
286 |
|
|
287 |
"
|
|
288 |
do the abort if we have to
|
|
289 |
"
|
|
290 |
answer ifFalse:[
|
|
291 |
Object abortSignal raise.
|
|
292 |
].
|
|
293 |
^ false
|
|
294 |
!
|
|
295 |
|
116
|
296 |
correctableError:aString position:relPos to:relEndPos from:aCompiler
|
32
|
297 |
"compiler notifies us of a correctable error;
|
0
|
298 |
hilight the error (relPos to relEndPos) and show a Box asking for continue/correct/abort;
|
|
299 |
this method should return true to the compiler if user wants the error
|
|
300 |
to be corrected; false otherwise"
|
|
301 |
|
59
|
302 |
|action|
|
0
|
303 |
|
59
|
304 |
self highlightingErrorPosition:relPos to:relEndPos do:[
|
95
|
305 |
action := OptionBox
|
|
306 |
request:aString
|
|
307 |
label:'Error'
|
|
308 |
form:(WarningBox iconBitmap)
|
|
309 |
buttonLabels:#('abort' 'correct' 'continue')
|
|
310 |
values:#(#abort #correct #continue).
|
59
|
311 |
].
|
58
|
312 |
|
59
|
313 |
action == #abort ifTrue:[
|
|
314 |
Object abortSignal raise.
|
|
315 |
^ false
|
|
316 |
].
|
|
317 |
^ action == #correct
|
119
|
318 |
! !
|
|
319 |
|
|
320 |
!Workspace methodsFor:'compiler interface'!
|
|
321 |
|
|
322 |
wantChangeLog
|
|
323 |
"sent by the compiler to ask if a changeLog entry should
|
|
324 |
be written. Return true here."
|
|
325 |
|
|
326 |
^ true
|
32
|
327 |
!
|
|
328 |
|
|
329 |
currentSourceCode
|
|
330 |
"special interface to compiler - called by parser
|
|
331 |
to get the updated source code after a corrected error"
|
|
332 |
|
|
333 |
^ self contents
|
0
|
334 |
! !
|
|
335 |
|
|
336 |
!Workspace methodsFor:'execution'!
|
|
337 |
|
|
338 |
doItWithValueDo:aBlock
|
|
339 |
"helper for doIt, printIt and inspectIt. Evaluate the selection and,
|
|
340 |
if all went well, evaluate the argument, aBlock with the value."
|
|
341 |
|
59
|
342 |
|code value selLine selCol endLine endCol cLine cCol cleanUp|
|
0
|
343 |
|
|
344 |
code := self selection.
|
|
345 |
code notNil ifTrue:[
|
59
|
346 |
codeStartPosition := self characterPositionOfSelection.
|
|
347 |
|
|
348 |
"
|
|
349 |
remember selection for later - if there is an error,
|
|
350 |
the notification method will highlight it.
|
|
351 |
thus destroying the current selection
|
|
352 |
"
|
|
353 |
selLine := selectionStartLine.
|
|
354 |
selCol := selectionStartCol.
|
|
355 |
endLine := selectionEndLine.
|
|
356 |
endCol := selectionEndCol.
|
|
357 |
cCol := cursorCol.
|
|
358 |
cLine := cursorLine.
|
58
|
359 |
|
97
|
360 |
"
|
|
361 |
cleanup: restore previous selection and cursor positions
|
|
362 |
"
|
59
|
363 |
cleanUp := [
|
|
364 |
self selectFromLine:selLine col:selCol toLine:endLine col:endCol.
|
|
365 |
cLine notNil ifTrue:[
|
|
366 |
self cursorLine:cLine col:cCol
|
66
|
367 |
].
|
59
|
368 |
].
|
0
|
369 |
|
97
|
370 |
"
|
|
371 |
perform the action.
|
|
372 |
Be careful to release the reference to the value;
|
|
373 |
otherwise, we could keep lots of garbage from being freed
|
|
374 |
until the view gets closed
|
|
375 |
"
|
60
|
376 |
self topView withCursor:Cursor execute do:[
|
97
|
377 |
[
|
|
378 |
AbortSignal handle:[:ex |
|
|
379 |
ex return
|
|
380 |
] do:[
|
59
|
381 |
value := doItAction value:code asString.
|
97
|
382 |
cleanUp value. cleanUp := nil.
|
59
|
383 |
aBlock value:value.
|
97
|
384 |
]
|
|
385 |
] valueNowOrOnUnwindDo:[
|
|
386 |
cleanUp notNil ifTrue:[cleanUp value. cleanUp := nil].
|
|
387 |
value := nil
|
59
|
388 |
]
|
|
389 |
].
|
0
|
390 |
]
|
|
391 |
!
|
|
392 |
|
|
393 |
doIt
|
|
394 |
"user selected 'doIt' from menu; show a wait-cursor, evaluate the code
|
|
395 |
and finally restore cursor; return result of evaluation"
|
|
396 |
|
|
397 |
self doItWithValueDo:[:result | ]
|
|
398 |
!
|
|
399 |
|
|
400 |
inspectIt
|
|
401 |
"user selected 'inspectIt' from menu; use doIt to evaluate the code
|
|
402 |
and start an inspector on the result"
|
|
403 |
|
|
404 |
self doItWithValueDo:[:result | result inspect]
|
|
405 |
!
|
|
406 |
|
|
407 |
printIt
|
|
408 |
"user selected 'printIt' from menu; use doIt to evaluate the code
|
|
409 |
and insert result of evaluation into my text"
|
|
410 |
|
|
411 |
self doItWithValueDo:[:result |
|
59
|
412 |
self cursorLine:selectionEndLine col:(selectionEndCol + 1).
|
|
413 |
self insertSelectedStringAtCursor:(result displayString "printString")
|
0
|
414 |
]
|
|
415 |
! !
|
|
416 |
|
|
417 |
!Workspace methodsFor:'events'!
|
|
418 |
|
|
419 |
keyPress:key x:x y:y
|
60
|
420 |
|cmd commands|
|
|
421 |
|
32
|
422 |
(key == #DoIt) ifTrue:[^ self doIt].
|
23
|
423 |
(key == #InspectIt) ifTrue:[^ self inspectIt].
|
79
|
424 |
(key == #PrintIt) ifTrue:[
|
|
425 |
readOnly ifFalse:[self printIt].
|
|
426 |
^ self
|
|
427 |
].
|
38
|
428 |
|
|
429 |
"
|
60
|
430 |
Ctrl-Fn or Cmd-Fn evaluates a key-sequence
|
|
431 |
(I added Ctrl-Fn, because some windowmanagers already use cmd-fn)
|
38
|
432 |
see TextView>>keyPress:x:y:
|
|
433 |
"
|
95
|
434 |
(#(CmdF1 CmdF2 CmdF3 CmdF4 CmdF5 CmdF6 CmdF7 CmdF8 CmdF9
|
|
435 |
Cmdf1 Cmdf2 Cmdf3 Cmdf4 Cmdf5 Cmdf6 Cmdf7 Cmdf8 Cmdf9
|
|
436 |
CtrlF1 CtrlF2 CtrlF3 CtrlF4 CtrlF5 CtrlF6 CtrlF7 CtrlF8 CtrlF9
|
|
437 |
Ctrlf1 Ctrlf2 Ctrlf3 Ctrlf4 Ctrlf5 Ctrlf6 Ctrlf7 Ctrlf8 Ctrlf9
|
|
438 |
) includes:key) ifTrue:[
|
|
439 |
commands := Smalltalk at:#FunctionKeySequences ifAbsent:[nil].
|
|
440 |
commands notNil ifTrue:[
|
|
441 |
cmd := commands at:(key copyLast:2) ifAbsent:[nil].
|
|
442 |
cmd notNil ifTrue:[
|
|
443 |
Parser evaluate:cmd asString
|
|
444 |
receiver:self
|
|
445 |
notifying:nil
|
|
446 |
compile:false.
|
|
447 |
^ self
|
59
|
448 |
]
|
|
449 |
]
|
38
|
450 |
].
|
|
451 |
|
0
|
452 |
super keyPress:key x:x y:y
|
38
|
453 |
|
|
454 |
"
|
|
455 |
example uses of funtion keys:
|
|
456 |
|
|
457 |
to set tab-distance to 4-cols,
|
|
458 |
select the following:
|
|
459 |
|
|
460 |
self setTab4
|
|
461 |
|
|
462 |
then, press shift-F2 to define the sequence;
|
95
|
463 |
press cmd-F2 to execute it
|
38
|
464 |
voila: you have set 4-tabs.
|
95
|
465 |
(some window managers (MWM, 4DWM have cmd-Fn redefined; use Ctrl-Fn then).
|
38
|
466 |
|
|
467 |
to switch back, perform the same procedure with:
|
|
468 |
|
59
|
469 |
self setTab8
|
38
|
470 |
|
60
|
471 |
Within the expression, 'self' is bound to the view. Thus, you can do
|
|
472 |
all kinds of fancy things.
|
|
473 |
For example:
|
95
|
474 |
if you like a browser to come up on the selection when pressing Cmd-F3:
|
60
|
475 |
select:
|
38
|
476 |
|
59
|
477 |
|sel|
|
|
478 |
sel := self selection asString withoutSeparators.
|
|
479 |
(Smalltalk includesKey:sel asSymbol) ifTrue:[
|
|
480 |
(Smalltalk at:sel asSymbol) isClass ifTrue:[
|
|
481 |
SystemBrowser browseClass:(Smalltalk at:sel asSymbol)
|
|
482 |
]
|
|
483 |
]
|
38
|
484 |
|
|
485 |
then, press shift-F3 to define the command.
|
|
486 |
press cmd-F3 to execute it (select some classname before).
|
60
|
487 |
(notice: on the Indy, Cmd-F3 is already used by the window manager)
|
38
|
488 |
|
95
|
489 |
if you like a file-include command on Cmd-F4:
|
38
|
490 |
select:
|
|
491 |
|
59
|
492 |
|sel|
|
|
493 |
sel := self selection.
|
|
494 |
sel notNil ifTrue:[
|
|
495 |
sel := sel asString withoutSeparators.
|
|
496 |
s := FileStream readonlyFileNamed:sel.
|
|
497 |
s notNil ifTrue:[
|
|
498 |
self paste:(s contents asString).
|
|
499 |
s close
|
|
500 |
]
|
|
501 |
]
|
38
|
502 |
|
|
503 |
this will paste the contents of the file at the current cusor position.
|
60
|
504 |
(select above expression, press Shift-F4,
|
|
505 |
then select any filename and press Cmd-F4)
|
|
506 |
try it here: /etc/passwd
|
38
|
507 |
"
|
0
|
508 |
! !
|