forked from slyrus/mcclim-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text-editor-gadget.lisp
402 lines (351 loc) · 17.5 KB
/
text-editor-gadget.lisp
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
;;; -*- Mode: Lisp; Package: CLIM-INTERNALS -*-
;;; (c) copyright 2000 by
;;; Arthur Lemmens ([email protected]),
;;; Iban Hatchondo ([email protected])
;;; and Julien Boninfante ([email protected])
;;; (c) copyright 2001 by
;;; Lionel Salabartan ([email protected])
;;; (c) copyright 2001 by Michael McDonald ([email protected])
;;; (c) copyright 2001 by Gilbert Baumann <[email protected]>
;;; (c) copyright 2006 by Troels Henriksen ([email protected])
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Library General Public
;;; License as published by the Free Software Foundation; either
;;; version 2 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU Library General Public
;;; License along with this library; if not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA 02111-1307 USA.
;;; This file contains the concrete implementation of the text-field
;;; and text-editor gadgets. It is loaded rather late, because it
;;; requires Drei. Half of the complexity here is about working around
;;; annoying Goatee quirks, generalising it to three editor substrates
;;; is nontrivial.
(in-package :clim-internals)
;;; The text editor gadget(s) is implemented as a class implementing
;;; the text editor gadget protocol, but containing an editor
;;; substrate object that takes care of the actual editing logic,
;;; redisplay, etc. The substrates need to be gadgets themselves and
;;; are defined here.
(defparameter *default-text-field-text-style*
(make-text-style :fix :roman :normal))
(defclass editor-substrate-mixin (value-gadget)
((activation-gestures :reader activation-gestures
:initarg :activation-gestures)
(user :reader user-gadget
:initarg :user-gadget
:documentation "The editor gadget using this editor substrate."
:initform (error "Editor substrates must have a user.")))
(:documentation "A mixin class for text editor gadget substrates.")
(:default-initargs :activation-gestures '()))
(defmethod gadget-id ((gadget editor-substrate-mixin))
(gadget-id (user-gadget gadget)))
(defmethod (setf gadget-id) (value (gadget editor-substrate-mixin))
(setf (gadget-id (user-gadget gadget)) value))
(defmethod gadget-client ((gadget editor-substrate-mixin))
(gadget-client (user-gadget gadget)))
(defmethod (setf gadget-client) (value (gadget editor-substrate-mixin))
(setf (gadget-client (user-gadget gadget)) value))
(defmethod gadget-armed-callback ((gadget editor-substrate-mixin))
(gadget-armed-callback (user-gadget gadget)))
(defmethod gadget-disarmed-callback ((gadget editor-substrate-mixin))
(gadget-disarmed-callback (user-gadget gadget)))
(defclass text-field-substrate-mixin (editor-substrate-mixin)
()
(:documentation "A mixin class for editor substrates used for text field gadgets."))
(defclass text-editor-substrate-mixin (editor-substrate-mixin)
((ncolumns :reader text-editor-ncolumns
:initarg :ncolumns
:initform nil
:type (or null integer))
(nlines :reader text-editor-nlines
:initarg :nlines
:initform nil
:type (or null integer)))
(:documentation "A mixin class for editor substrates used for text editor gadgets."))
;;; Now, define the Drei substrate.
(defclass drei-editor-substrate (drei:drei-gadget-pane
editor-substrate-mixin)
()
(:metaclass esa-utils:modual-class)
(:documentation "A class for Drei-based editor substrates."))
(defmethod (setf gadget-value) :after (value (gadget drei-editor-substrate)
&key invoke-callback)
(declare (ignore invoke-callback))
;; Hm! I wonder if this can cause trouble. I think not.
(drei:display-drei gadget))
(defclass drei-text-field-substrate (text-field-substrate-mixin
drei-editor-substrate)
()
(:metaclass esa-utils:modual-class)
(:documentation "The class for Drei-based text field substrates."))
(defmethod drei:handle-gesture ((drei drei-text-field-substrate) gesture)
(if (with-activation-gestures ((activation-gestures drei))
(activation-gesture-p gesture))
(activate-callback drei (gadget-client drei) (gadget-id drei))
(call-next-method)))
(defmethod compose-space ((pane drei-text-field-substrate) &key width height)
(declare (ignore width height))
(with-sheet-medium (medium pane)
(let ((as (text-style-ascent (medium-text-style medium) medium))
(ds (text-style-descent (medium-text-style medium) medium))
(w (text-size medium (gadget-value pane))))
(let ((width w)
(height (+ as ds)))
(make-space-requirement :height height :max-height height :min-height height
:min-width width :width width)))))
(defclass drei-text-editor-substrate (text-editor-substrate-mixin
drei-editor-substrate)
()
(:metaclass esa-utils:modual-class)
(:documentation "The class for Drei-based text editor substrates."))
(defmethod compose-space ((pane drei-text-editor-substrate) &key width height)
(with-sheet-medium (medium pane)
(let* ((text-style (medium-text-style medium))
(line-height (+ (text-style-height text-style medium)
(stream-vertical-spacing pane)))
(column-width (text-style-width text-style medium)))
(with-accessors ((ncolumns text-editor-ncolumns)
(nlines text-editor-nlines)) pane
(apply #'space-requirement-combine* #'(lambda (req1 req2)
(or req2 req1))
(call-next-method)
(let ((width (if ncolumns
(+ (* ncolumns column-width))
width))
(height (if nlines
(+ (* nlines line-height))
height)))
(list
:width width :max-width width :min-width width
:height height :max-height height :min-height height)))))))
(defmethod allocate-space ((pane drei-text-editor-substrate) w h)
(resize-sheet pane w h))
;;; Now, define the Goatee substrate.
(defclass goatee-editor-substrate (editor-substrate-mixin
text-field
clim-stream-pane)
((area :accessor area
:initform nil
:documentation "The Goatee area used for text editing.")
;; This hack is necessary because the Goatee editing area is not
;; created until the first redisplay... yuck.
(value :documentation "The initial value for the Goatee area."))
(:default-initargs
:text-style *default-text-field-text-style*))
(defmethod initialize-instance :after ((pane goatee-editor-substrate) &rest rest)
(declare (ignore rest))
(setf (medium-text-style (sheet-medium pane))
(slot-value pane 'text-style)))
;; Is there really a benefit to waiting until the first painting to
;; create the goatee instance? Why not use INITIALIZE-INSTANCE?
(defmethod handle-repaint :before ((pane goatee-editor-substrate) region)
(declare (ignore region))
(unless (area pane)
(multiple-value-bind (cx cy)
(stream-cursor-position pane)
(setf (cursor-visibility (stream-text-cursor pane)) nil)
(setf (area pane) (make-instance 'goatee:simple-screen-area
:area-stream pane
:x-position cx
:y-position cy
:initial-contents (slot-value pane 'value))))
(stream-add-output-record pane (area pane))))
;;; This implements click-to-focus-keyboard-and-pass-click-through
;;; behaviour.
(defmethod handle-event :before
((gadget goatee-editor-substrate) (event pointer-button-press-event))
(let ((previous (stream-set-input-focus gadget)))
(when (and previous (typep previous 'gadget))
(disarmed-callback previous (gadget-client previous) (gadget-id previous)))
(armed-callback gadget (gadget-client gadget) (gadget-id gadget))))
(defmethod armed-callback :after ((gadget goatee-editor-substrate) client id)
(declare (ignore client id))
(handle-repaint gadget +everywhere+) ;FIXME: trigger initialization
(let ((cursor (cursor (area gadget))))
(letf (((cursor-state cursor) nil))
(setf (cursor-appearance cursor) :solid))))
(defmethod disarmed-callback :after ((gadget goatee-editor-substrate) client id)
(declare (ignore client id))
(handle-repaint gadget +everywhere+) ;FIXME: trigger initialization
(let ((cursor (cursor (area gadget))))
(letf (((cursor-state cursor) nil))
(setf (cursor-appearance cursor) :hollow))))
(defmethod handle-event
((gadget goatee-editor-substrate) (event key-press-event))
(let ((gesture (convert-to-gesture event))
(*activation-gestures* (activation-gestures gadget)))
(when (activation-gesture-p gesture)
(activate-callback gadget (gadget-client gadget) (gadget-id gadget))
(return-from handle-event t))
(goatee:execute-gesture-command gesture
(area gadget)
goatee::*simple-area-gesture-table*)
(let ((new-value (goatee::buffer-string (goatee::buffer (area gadget)))))
(unless (string= (gadget-value gadget) new-value)
(setf (slot-value gadget 'value) new-value)
(value-changed-callback gadget
(gadget-client gadget)
(gadget-id gadget)
new-value)))))
(defmethod (setf gadget-value) :after (new-value (gadget goatee-editor-substrate)
&key invoke-callback)
(declare (ignore invoke-callback))
(let* ((area (area gadget))
(buffer (goatee::buffer area))
(start (goatee::buffer-start buffer))
(end (goatee::buffer-end buffer)))
(goatee::delete-region buffer start end)
(goatee::insert buffer new-value :position start)
(goatee::redisplay-area area)))
#+nil
(defmethod handle-repaint ((pane goatee-editor-substrate) region)
(declare (ignore region))
(with-special-choices (pane)
(with-sheet-medium (medium pane)
(multiple-value-bind (x1 y1 x2 y2) (bounding-rectangle* (sheet-region pane))
(display-gadget-background pane (gadget-current-color pane) 0 0 (- x2 x1) (- y2 y1))
(draw-text* pane (gadget-value pane)
x1
(+ y1 (text-style-ascent (medium-text-style medium) medium))
:align-x :left
:align-y :baseline)))))
(defclass goatee-text-field-substrate (text-field-substrate-mixin
goatee-editor-substrate)
()
(:documentation "The class for Goatee-based text field substrates."))
(defmethod compose-space ((pane goatee-text-field-substrate) &key width height)
(declare (ignore width height))
(with-sheet-medium (medium pane)
(let ((as (text-style-ascent (medium-text-style medium) medium))
(ds (text-style-descent (medium-text-style medium) medium))
(w (text-size medium (gadget-value pane))))
(let ((width w)
(height (+ as ds)))
(make-space-requirement :width width :height height
:max-width width :max-height height
:min-width width :min-height height)))))
(defclass goatee-text-editor-substrate (text-editor-substrate-mixin
goatee-editor-substrate)
()
(:documentation "The class for Goatee-based text field substrates."))
(defmethod compose-space ((pane goatee-text-editor-substrate) &key width height)
(with-sheet-medium (medium pane)
(let* ((text-style (medium-text-style medium))
(line-height (+ (text-style-height text-style medium)
(stream-vertical-spacing pane)))
(column-width (text-style-width text-style medium)))
(with-accessors ((ncolumns text-editor-ncolumns)
(nlines text-editor-nlines)) pane
(apply #'space-requirement-combine* #'(lambda (req1 req2)
(or req2 req1))
(call-next-method)
(let ((width (if ncolumns
(+ (* ncolumns column-width))
width))
(height (if nlines
(+ (* nlines line-height))
height)))
(list :width width :max-width width :min-width width
:height height :max-height height :min-height height)))))))
(defmethod allocate-space ((pane goatee-text-editor-substrate) w h)
(resize-sheet pane w h))
(defun make-text-field-substrate (user &rest args)
"Create an appropriate text field gadget editing substrate object."
(let* ((substrate (apply #'make-pane (if *use-goatee*
'goatee-text-field-substrate
'drei-text-field-substrate)
:user-gadget user args))
(sheet substrate))
(values substrate sheet)))
(defun make-text-editor-substrate (user &rest args &key scroll-bars value
&allow-other-keys)
"Create an appropriate text editor gadget editing substrate
object. Returns two values, the first is the substrate object,
the second is the sheet that should be adopted by the user
gadget."
(let* ((minibuffer (when (and (not *use-goatee*) scroll-bars)
(make-pane 'drei::drei-minibuffer-pane)))
(substrate (apply #'make-pane (if *use-goatee*
'goatee-text-editor-substrate
'drei-text-editor-substrate)
:user-gadget user
:minibuffer minibuffer args))
(sheet (if scroll-bars
(scrolling (:scroll-bars scroll-bars)
substrate)
substrate)))
(if *use-goatee*
(setf (slot-value substrate 'value) value)
(setf (gadget-value substrate) value))
(values substrate (if minibuffer
(vertically ()
sheet
minibuffer)
sheet))))
;;; The class for using these substrates in the gadgets.
(defclass editor-substrate-user-mixin (value-gadget)
((substrate :accessor substrate
:documentation "The editing substrate used for this
text field."))
(:documentation "A mixin class for creating gadgets using
editor substrates."))
(defmethod gadget-value ((gadget editor-substrate-user-mixin))
(gadget-value (substrate gadget)))
(defmethod (setf gadget-value) (value (gadget editor-substrate-user-mixin)
&key invoke-callback)
(declare (ignore invoke-callback))
(setf (gadget-value (substrate gadget)) value))
;;; ------------------------------------------------------------------------------------------
;;; 30.4.8 The concrete text-field Gadget
(defclass text-field-pane (text-field
vrack-pane editor-substrate-user-mixin)
((activation-gestures :accessor activation-gestures
:initarg :activation-gestures
:documentation "A list of gestures that
cause the activate callback to be called."))
(:default-initargs
:activation-gestures *standard-activation-gestures*))
(defmethod initialize-instance :after ((object text-field-pane)
&key id client armed-callback
disarmed-callback
activation-gestures activate-callback
value value-changed-callback)
;; Make an editor substrate object for the gadget.
(let ((substrate (make-text-field-substrate
object :id id :client client :armed-callback armed-callback
:disarmed-callback disarmed-callback
:activation-gestures activation-gestures
:activate-callback activate-callback
:value value
:value-changed-callback value-changed-callback)))
(setf (substrate object) substrate)
(sheet-adopt-child object substrate)))
;;; ------------------------------------------------------------------------------------------
;;; 30.4.9 The concrete text-editor Gadget
(defclass text-editor-pane (text-editor
vrack-pane editor-substrate-user-mixin)
()
(:default-initargs :activation-gestures '()))
(defmethod initialize-instance :after ((object text-editor-pane)
&key id client armed-callback
disarmed-callback
activation-gestures scroll-bars
ncolumns nlines value)
;; Make an editor substrate object for the gadget.
(multiple-value-bind (substrate sheet)
(make-text-editor-substrate object
:id id :client client :armed-callback armed-callback
:disarmed-callback disarmed-callback
:activation-gestures activation-gestures
:scroll-bars scroll-bars
:ncolumns ncolumns :nlines nlines
:value value)
(setf (substrate object) substrate)
(sheet-adopt-child object sheet)))