This repository has been archived by the owner on Mar 13, 2023. It is now read-only.
/
clim-fig.lisp
424 lines (394 loc) · 19 KB
/
clim-fig.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
;;; -*- Mode: Lisp; Package: CLIM-DEMO -*-
;;; (c) copyright 2001 by
;;; Arnaud Rouanet (rouanet@emi.u-bordeaux.fr)
;;; Lionel Salabartan (salabart@emi.u-bordeaux.fr)
;;; 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.
(in-package :CLIM-INTERNALS)
(defclass canvas-pane (application-pane)
((first-point-x :initform nil)
(first-point-y :initform nil)
(canvas-pixmap :initform nil)
(selected-object :initform nil)))
(defun set-status-line (string)
(setf (gadget-value (slot-value *application-frame* 'clim-demo::status))
string))
#+nil
(progn
(defmethod handle-event :around ((pane t) (event t))
(let ((*application-frame* (pane-frame pane)))
(call-next-method pane event)))
(defmethod dispatch-event ((pane canvas-pane) event)
(handle-event pane event))
)
(defmethod handle-event ((pane canvas-pane) (event pointer-button-press-event))
(when (= (pointer-event-button event) +pointer-left-button+)
(with-slots (first-point-x first-point-y canvas-pixmap) pane
(let ((pixmap-width (round (bounding-rectangle-width (sheet-region pane))))
(pixmap-height (round (bounding-rectangle-height (sheet-region pane)))))
(setq first-point-x (pointer-event-x event))
(setq first-point-y (pointer-event-y event))
(with-slots (clim-demo::line-style clim-demo::current-color clim-demo::drawing-mode) *application-frame*
(case clim-demo::drawing-mode
(:point
(draw-point* pane first-point-x first-point-y
:ink clim-demo::current-color
:line-style clim-demo::line-style))))
(setf canvas-pixmap (allocate-pixmap pane pixmap-width pixmap-height))
(copy-to-pixmap pane 0 0 pixmap-width pixmap-height canvas-pixmap))))
(when (= (pointer-event-button event) +pointer-right-button+)
(with-slots (first-point-x first-point-y selected-object) pane
(setq first-point-x (pointer-event-x event)
first-point-y (pointer-event-y event))
(setf selected-object (search-selected-object (climi::pane-output-history pane)
first-point-x
first-point-y)))))
(defun search-selected-object (record x y)
(let ((selected-objects))
(map-over-output-records-containing-position
#'(lambda (x)
(push x selected-objects))
record x y)
(first selected-objects)))
(defun signum-1 (value)
(if (zerop value)
1
(signum value)))
(defmethod handle-event ((pane canvas-pane) (event pointer-motion-event))
(with-slots (first-point-x first-point-y canvas-pixmap) pane
(when (and first-point-x first-point-y canvas-pixmap)
(let* ((x (pointer-event-x event))
(y (pointer-event-y event))
(radius-x (- x first-point-x))
(radius-y (- y first-point-y))
(pixmap-width (round (bounding-rectangle-width (sheet-region pane))))
(pixmap-height (round (bounding-rectangle-height (sheet-region pane)))))
(with-slots (clim-demo::current-color clim-demo::drawing-mode clim-demo::constrict-mode) *application-frame*
(set-status-line (format nil "~:(~A~) from (~D,~D) to (~D,~D)~%"
clim-demo::drawing-mode
(round first-point-x) (round first-point-y)
(round x) (round y)))
(with-output-recording-options (pane :record nil)
(copy-from-pixmap canvas-pixmap 0 0 pixmap-width pixmap-height pane 0 0)
(when clim-demo::constrict-mode
(let ((radius-max (max (abs radius-x) (abs radius-y))))
(setf radius-x (* (signum-1 radius-x) radius-max)
radius-y (* (signum-1 radius-y) radius-max)
x (+ first-point-x radius-x)
y (+ first-point-y radius-y))))
(case clim-demo::drawing-mode
(:line
(when clim-demo::constrict-mode
(if (= (- (pointer-event-x event) first-point-x) radius-x)
(setf y first-point-y)
(setf x first-point-x)))
(draw-line* pane first-point-x first-point-y x y
:ink clim-demo::current-color :line-thickness 1))
(:arrow
(when clim-demo::constrict-mode
(if (= (- (pointer-event-x event) first-point-x) radius-x)
(setf y first-point-y)
(setf x first-point-x)))
(draw-arrow* pane first-point-x first-point-y x y
:ink clim-demo::current-color :line-thickness 1
:to-head t :head-width 20 :head-length 20))
(:rectangle
(draw-rectangle* pane first-point-x first-point-y x y :filled nil
:ink clim-demo::current-color :line-thickness 1))
(:ellipse
(draw-ellipse* pane first-point-x first-point-y radius-x 0 0 radius-y :filled nil
:ink clim-demo::current-color :line-thickness 1)))))))))
(defmethod handle-event ((pane canvas-pane) (event pointer-button-release-event))
(when (= (pointer-event-button event) +pointer-left-button+)
(with-slots (first-point-x first-point-y canvas-pixmap) pane
(let ((pixmap-width (round (bounding-rectangle-width (sheet-region pane))))
(pixmap-height (round (bounding-rectangle-height (sheet-region pane)))))
(set-status-line " ")
(when (and first-point-x first-point-y canvas-pixmap)
(copy-from-pixmap canvas-pixmap 0 0 pixmap-width pixmap-height pane 0 0)
(deallocate-pixmap canvas-pixmap)
(setf canvas-pixmap nil)
(with-slots (clim-demo::line-style
clim-demo::current-color
clim-demo::fill-mode
clim-demo::constrict-mode) *application-frame*
(let* ((x (pointer-event-x event))
(y (pointer-event-y event))
(radius-x (- x first-point-x))
(radius-y (- y first-point-y)))
(when clim-demo::constrict-mode
(let ((radius-max (max (abs radius-x) (abs radius-y))))
(setf radius-x (* (signum-1 radius-x) radius-max)
radius-y (* (signum-1 radius-y) radius-max)
x (+ first-point-x radius-x)
y (+ first-point-y radius-y))))
(case (clim-demo::clim-fig-drawing-mode *application-frame*)
(:line
(when clim-demo::constrict-mode
(if (= (- (pointer-event-x event) first-point-x) radius-x)
(setf y first-point-y)
(setf x first-point-x)))
(draw-line* pane first-point-x first-point-y x y
:ink clim-demo::current-color
:line-style clim-demo::line-style))
(:arrow
(when clim-demo::constrict-mode
(if (= (- (pointer-event-x event) first-point-x) radius-x)
(setf y first-point-y)
(setf x first-point-x)))
(with-new-output-record (pane)
(draw-arrow* pane first-point-x first-point-y x y
:ink clim-demo::current-color
:line-style clim-demo::line-style
:to-head t :head-width 20 :head-length 20)))
(:rectangle
(draw-rectangle* pane first-point-x first-point-y x y :filled clim-demo::fill-mode
:ink clim-demo::current-color
:line-style clim-demo::line-style))
(:ellipse
(draw-ellipse* pane first-point-x first-point-y radius-x 0 0 radius-y
:filled clim-demo::fill-mode
:ink clim-demo::current-color :line-style clim-demo::line-style))))
(setf (clim-demo::clim-fig-redo-list *application-frame*) nil))
(setf first-point-x nil
first-point-y nil)))))
(when (= (pointer-event-button event) +pointer-right-button+)
(with-slots (first-point-x first-point-y selected-object) pane
(when selected-object
(multiple-value-bind (old-x old-y) (output-record-position selected-object)
(setf (output-record-position selected-object)
(values (+ old-x (- (pointer-event-x event) first-point-x))
(+ old-y (- (pointer-event-y event) first-point-y)))))
(with-output-recording-options (*standard-output* :record nil)
(with-bounding-rectangle* (x1 y1 x2 y2) (sheet-region pane)
(draw-rectangle* pane x1 y1 x2 y2 :ink +background-ink+))
(replay-output-record (stream-current-output-record *standard-output*)
*standard-output*))
(setf selected-object nil
first-point-x nil
first-point-y nil)))))
(in-package :clim-demo)
(defun clim-fig ()
(loop for port in climi::*all-ports*
do (destroy-port port))
(setq climi::*all-ports* nil)
;(run-frame-top-level (make-application-frame 'clim-fig))
(setq frame (make-application-frame 'clim-fig))
(setq fm (frame-manager frame))
(setq port (climi::frame-manager-port fm))
(setq pane (first (frame-panes frame)))
(setq medium (sheet-medium pane))
(setq graft (graft frame))
(setq vbox (climi::frame-pane frame))
(unless clim-sys:*multiprocessing-p*
(run-frame-top-level frame)))
(defun make-colored-button (color &key width height)
(make-pane 'push-button-pane
:label " "
:activate-callback
#'(lambda (gadget)
(setf (clim-fig-current-color (gadget-client gadget))
color))
:width width :height height
:normal color :pushed-and-highlighted color
:highlighted color))
(defun make-drawing-mode-button (label mode &key width height)
(make-pane 'push-button-pane
:label label
:activate-callback
#'(lambda (gadget)
(setf (clim-fig-drawing-mode (gadget-client gadget))
mode))
:width width :height height))
(defun make-merged-line-style (line-style &key unit thickness joint-shape cap-shape
(dashes nil dashes-p))
(make-line-style :unit (or unit
(line-style-unit line-style))
:thickness (or thickness
(line-style-thickness line-style))
:joint-shape (or joint-shape
(line-style-joint-shape line-style))
:cap-shape (or cap-shape
(line-style-cap-shape line-style))
:dashes (if dashes-p
dashes
(line-style-dashes line-style))))
(define-command com-exit ()
(throw 'exit nil))
(define-command com-undo ()
(let* ((output-history (clim-fig-output-record *application-frame*))
(record (first (last (output-record-children output-history)))))
(if record
(with-output-recording-options (*standard-output* :record nil)
(with-bounding-rectangle* (x1 y1 x2 y2) record
(draw-rectangle* *standard-output* x1 y1 x2 y2 :ink +background-ink+)
(delete-output-record record output-history)
(push record (clim-fig-redo-list *application-frame*))
(replay-output-record output-history *standard-output*
(make-rectangle* x1 y1 x2 y2))))
(beep))))
(define-command com-redo ()
(let* ((output-history (clim-fig-output-record *application-frame*))
(record (pop (clim-fig-redo-list *application-frame*))))
(if record
(with-output-recording-options (*standard-output* :record nil)
(with-bounding-rectangle* (x1 y1 x2 y2) record
(draw-rectangle* *standard-output* x1 y1 x2 y2 :ink +background-ink+)
(add-output-record record output-history)
(replay-output-record output-history *standard-output*
(make-rectangle* x1 y1 x2 y2))))
(beep))))
(define-command com-clear ()
(setf (clim-demo::clim-fig-redo-list *application-frame*)
(append (output-record-children (clim-fig-output-record *application-frame*))
(clim-demo::clim-fig-redo-list *application-frame*)))
(window-clear *standard-output*))
(make-command-table 'file-command-table
:errorp nil
:menu '(("Exit" :command com-exit)))
(make-command-table 'edit-command-table
:errorp nil
:menu '(("Undo" :command com-undo)
("Redo" :command com-redo)
("Clear" :command com-clear)))
(make-command-table 'menubar-command-table
:errorp nil
:menu '(("File" :menu file-command-table)
("Edit" :menu edit-command-table)))
(define-application-frame clim-fig ()
((drawing-mode :initform :line :accessor clim-fig-drawing-mode)
(output-record :accessor clim-fig-output-record)
(redo-list :initform nil :accessor clim-fig-redo-list)
(current-color :initform +black+ :accessor clim-fig-current-color)
(line-style :initform (make-line-style) :accessor clim-fig-line-style)
(fill-mode :initform nil :accessor clim-fig-fill-mode)
(constrict-mode :initform nil :accessor clim-fig-constrict-mode)
(status :initform nil :accessor clim-fig-status))
(:panes
(canvas :canvas)
(menu-bar (climi::make-menu-bar 'menubar-command-table :height 25))
(line-width-slider :slider
:label "Line Width"
:value 1
:min-value 1
:max-value 100
:value-changed-callback
#'(lambda (gadget value)
(declare (ignore gadget))
(with-slots (line-style) *application-frame*
(setf line-style
(make-merged-line-style line-style
:thickness (round value)))))
:show-value-p t
:decimal-places 0
:height 50
:orientation :horizontal)
(round-shape-toggle :toggle-button
:label "Round Cap/Joint"
:value nil
:value-changed-callback
#'(lambda (gadget value)
(declare (ignore gadget))
(with-slots (line-style) *application-frame*
(let ((cap-shape (if value
:round
:butt))
(joint-shape (if value
:round
:miter)))
(setf line-style
(make-merged-line-style line-style
:cap-shape cap-shape
:joint-shape joint-shape))))))
(fill-mode-toggle :toggle-button
:label "Fill"
:value nil
:value-changed-callback
#'(lambda (gadget value)
(declare (ignore gadget))
(setf (clim-fig-fill-mode *application-frame*) value)))
(constrict-toggle :toggle-button
:label "Constrict"
:value nil
:value-changed-callback
#'(lambda (gadget value)
(declare (ignore gadget))
(setf (clim-fig-constrict-mode *application-frame*) value)))
;; Drawing modes
(point-button (make-drawing-mode-button "Point" :point))
(line-button (make-drawing-mode-button "Line" :line))
(arrow-button (make-drawing-mode-button "Arrow" :arrow))
(rectangle-button (make-drawing-mode-button "Rectangle" :rectangle))
(ellipse-button (make-drawing-mode-button "Ellipse" :ellipse))
;; Colors
(black-button (make-colored-button +black+))
(blue-button (make-colored-button +blue+))
(green-button (make-colored-button +green+))
(cyan-button (make-colored-button +cyan+))
(red-button (make-colored-button +red+))
(magenta-button (make-colored-button +magenta+))
(yellow-button (make-colored-button +yellow+))
(white-button (make-colored-button +white+))
(turquoise-button (make-colored-button +turquoise+))
(grey-button (make-colored-button +grey+))
(brown-button (make-colored-button +brown+))
(orange-button (make-colored-button +orange+))
(undo :push-button
:label "Undo"
:activate-callback #'(lambda (x)
(declare (ignore x))
(com-undo)))
(redo :push-button
:label "Redo"
:activate-callback #'(lambda (x)
(declare (ignore x))
(com-redo)))
(clear :push-button
:label "Clear"
:activate-callback #'(lambda (x)
(declare (ignore x))
(com-clear)))
(status :text-field :value "CLIM Fig"))
(:layouts
(default
(vertically ()
menu-bar
(horizontally ()
(vertically (:width 150)
(tabling (:height 60)
(list black-button blue-button green-button cyan-button)
(list red-button magenta-button yellow-button white-button)
(list turquoise-button grey-button brown-button orange-button))
line-width-slider
round-shape-toggle
(horizontally () fill-mode-toggle constrict-toggle)
point-button line-button arrow-button
ellipse-button rectangle-button)
(scrolling (:width 600 :height 400) canvas))
(horizontally (:height 30) clear undo redo)
status)))
(:top-level (clim-fig-frame-top-level)))
(defmethod clim-fig-frame-top-level ((frame application-frame) &key)
(let ((*standard-input* (frame-standard-input frame))
(*standard-output* (frame-standard-output frame))
(*query-io* (frame-query-io frame)))
(setf (slot-value frame 'output-record)
(stream-current-output-record *standard-output*)
(slot-value frame 'status)
(find-if #'(lambda (pane) (typep pane 'text-field-pane))
(frame-panes frame)))
(catch 'exit
(clim-extensions:simple-event-loop))
(frame-exit frame)))