forked from slyrus/mcclim-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gadgets.lisp
2717 lines (2377 loc) · 117 KB
/
gadgets.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; -*- 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]>
;;; 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)
;;;; Notes
;; There is STANDARD-GADGET in this file but not in the spec, where
;; from? Lispworks?
;; The spec says ORIENTED-GADGET-MIXIN, we call it ORIENTED-GADGET and
;; later define ORIENTED-GADGET-MIXIN with the remark "Try to be
;; compatible with Lispworks' CLIM."
;;
;; This makes me suspect, that either "ORIENTED-GADGET-MIXIN" in the
;; spec is a typo, or all other classes like e.g. ACTION-GADGET should
;; really be named e.g. ACTION-GADGET-MIXIN. Also that would make more
;; sense to me. --GB
;; We have: LABELLED-GADGET, the spec has LABELLED-GADGET-MIXIN. Typo?
;; Compatibility?
;; Why is there GADGET-LABEL-TEXT-STYLE? The spec says, that just the
;; pane's text-style should be borrowed.
;; RANGE-GADGET / RANGE-GADGET-MIXIN: same thing as with
;; ORIENTED-GADGET-MIXIN.
;; Why is there no (SETF GADGET-RANGE*) in the spec? Omission?
;; I would like to make COMPOSE-LABEL-SPACE and DRAW-LABEL* into some
;; sort of label protocol, so that application programmers can
;; programm their own sort of labels alleviateing the need for
;; something like a drawn button gadget.
;;
;; Q: Can we make it so that a mixin class can override another mixin
;; class?
;;
;; All the programmer should need to do is e.g.
;;
;; (defclass pattern-label-mixin ()
;; (pattern :initarg :pattern))
;;
;; (defmethod compose-label-space ((me pattern-label-mixin))
;; (with-slots (pattern) me
;; (make-space-requirement :width (pattern-width pattern)
;; :height (pattern-height pattern))))
;;
;; (defmethod draw-label ((me pattern-label-mixin) x1 y1 x2 y2)
;; (with-slots (pattern) me
;; (draw-design me (transform-region (make-translation-transformation x1 y1)
;; pattern))))
;;
;; (defclass patterned-button (pattern-label-mixin push-button-pane)
;; ())
;;
;; But then this probably is backwards. Specifing that :LABEL can be
;; another pane probably is much easier and would still allow for the
;; backend to choose the concrete widget class for us.
;;
;; --GB
;; - Should RADIO-BOX-PANE and CHECK-BOX-PANE use rack or box layout?
;; - :CHOICES initarg to RADIO-BOX and CHECK-BOX is from Franz' user
;; guide.
;;;; TODO
;; - the scroll-bar needs more work:
;; . dragging should not change the value, the value should only
;; be changed after releasing the mouse.
;; . it should arm/disarm
;; . it should be deactivatable
;; - the slider needs a total overhaul
;; - TEXT-FILED, TEXT-AREA dito
;; - GADGET-COLOR-MIXIN is currently kind of dangling, we should reuse
;; it for effective-gadget-foreground et al.
;; - The color of a 3Dish border should be derived from a gadget's
;; background.
;; - It seems that 3D-BORDER-MIXIN is only used for the scroll-bar, so
;; remove it
;; - Somehow engrafting the push button's medium does not work. The
;; text-style initarg does not make it to the sheets medium.
;; - make NIL a valid label, and take it into account when applying
;; spacing.
;;;; --------------------------------------------------------------------------
;;;;
;;;; 30.3 Basic Gadget Classes
;;;;
;;; XXX I'm not sure that *application-frame* should be rebound like this. What
;;; about gadgets in accepting-values windows? An accepting-values window
;;; shouldn't be bound to *application-frame*. -- moore
(defun invoke-callback (pane callback &rest more-arguments)
(when callback
(let ((*application-frame* (pane-frame pane)))
(apply callback pane more-arguments))))
;;
;; gadget sub-classes
;;
;;
;; gadget's colors
;;
(defclass gadget-color-mixin ()
((normal :type color
:initform +gray80+
:initarg :normal
:accessor gadget-normal-color)
(highlighted :type color
:initform +gray85+
:initarg :highlighted
:accessor gadget-highlighted-color)
(pushed-and-highlighted :type color
:initform +gray75+
:initarg :pushed-and-highlighted
:accessor gadget-pushed-and-highlighted-color)
(current-color :type color
:accessor gadget-current-color))
(:documentation "This class define the gadgets colors."))
(defmethod initialize-instance :after ((gadget gadget-color-mixin) &rest args)
(declare (ignore args))
(setf (slot-value gadget 'current-color) (gadget-normal-color gadget)))
(defmethod (setf gadget-current-color) :after (color (gadget gadget-color-mixin))
(declare (ignore color))
(dispatch-repaint gadget (sheet-region gadget)))
(defclass basic-gadget (permanent-medium-sheet-output-mixin
;; sheet-leaf-mixin ; <- this cannot go here...
gadget-color-mixin
;; These are inherited from pane, via
;; clim-sheet-input-mixin and clim-repainting-mixin
;; immediate-sheet-input-mixin
;; immediate-repainting-mixin
basic-pane
gadget)
())
;; Where is this standard-gadget from? --GB
(defclass standard-gadget (basic-gadget)
())
(defgeneric armed-callback (gadget client gadget-id)
(:argument-precedence-order client gadget-id gadget))
(defgeneric disarmed-callback (gadget client gadget-id)
(:argument-precedence-order client gadget-id gadget))
;; "The default methods (on standard-gadget) call the function stored
;; in gadget-armed-callback or gadget-disarmed-callback with one argument,
;; the gadget."
(defmethod armed-callback ((gadget basic-gadget) client gadget-id)
(declare (ignore client gadget-id))
(invoke-callback gadget (gadget-armed-callback gadget)))
(defmethod disarmed-callback ((gadget basic-gadget) client gadget-id)
(declare (ignore client gadget-id))
(invoke-callback gadget (gadget-disarmed-callback gadget)))
;;
;; arming and disarming gadgets
;;
;; Redrawing is supposed to be handled on an :AFTER method on arm- and
;; disarm-callback.
(defmethod arm-gadget ((gadget basic-gadget) &optional (value t))
(with-slots (armed) gadget
(unless (eql armed value)
(setf armed value)
(if value
(armed-callback gadget (gadget-client gadget) (gadget-id gadget))
(disarmed-callback gadget (gadget-client gadget) (gadget-id gadget))))))
(defmethod disarm-gadget ((gadget basic-gadget))
(arm-gadget gadget nil))
;;;
;;; Activation
;;;
(defgeneric activate-gadget (gadget))
(defgeneric deactivate-gadget (gadget))
(defgeneric note-gadget-activated (client gadget))
(defgeneric note-gadget-deactivated (client gadget))
(defmethod activate-gadget ((gadget gadget))
(with-slots (active-p) gadget
(unless active-p
(setf active-p t)
(note-gadget-activated (gadget-client gadget) gadget))))
(defmethod deactivate-gadget ((gadget gadget))
(with-slots (active-p) gadget
(when active-p
(setf active-p nil)
(note-gadget-deactivated (gadget-client gadget) gadget))))
(defmethod note-gadget-activated (client (gadget gadget))
(declare (ignore client))
;; Default: do nothing
)
(defmethod note-gadget-deactivated (client (gadget gadget))
(declare (ignore client))
;; Default: do nothing
)
;;;
;;; Value-gadget
;;;
(defclass value-gadget (standard-gadget)
((value :initarg :value
:reader gadget-value)
(value-changed-callback :initarg :value-changed-callback
:initform nil
:reader gadget-value-changed-callback)))
(defmethod (setf gadget-value) (value (gadget value-gadget) &key invoke-callback)
(setf (slot-value gadget 'value) value)
(when invoke-callback
(value-changed-callback gadget
(gadget-client gadget)
(gadget-id gadget)
value)))
(defgeneric value-changed-callback (gadget client gadget-id value)
(:argument-precedence-order client gadget-id value gadget))
(defmethod value-changed-callback ((gadget value-gadget) client gadget-id value)
(declare (ignore client gadget-id))
(invoke-callback gadget (gadget-value-changed-callback gadget) value))
;;;
;;; Action-gadget
;;;
(defclass action-gadget (standard-gadget)
((activate-callback :initarg :activate-callback
:initform nil
:reader gadget-activate-callback)))
(defgeneric activate-callback (action-gadget client gadget-id)
(:argument-precedence-order client gadget-id action-gadget))
(defmethod activate-callback ((gadget action-gadget) client gadget-id)
(declare (ignore client gadget-id))
(invoke-callback gadget (gadget-activate-callback gadget)))
;;;
;;; Oriented-gadget
;;;
(defclass oriented-gadget ()
((orientation :type (member :vertical :horizontal)
:initarg :orientation
:reader gadget-orientation)))
(defclass oriented-gadget-mixin (oriented-gadget)
;; Try to be compatible with Lispworks' CLIM.
())
;;;;
;;;; labelled-gadget
;;;;
(defclass labelled-gadget ()
((label :initarg :label
:initform ""
:accessor gadget-label)
#+nil
(align-x :initarg :align-x
:accessor gadget-label-align-x)
#+nil
(align-y :initarg :align-y
:accessor gadget-label-align-y)
#+nil
(text-style :initform *default-text-style*
:initarg :text-style
:accessor gadget-text-style)))
(defclass labelled-gadget-mixin (labelled-gadget)
;; Try to be compatible with Lispworks' CLIM.
())
;;;;
;;;; Range-gadget
;;;;
(defclass range-gadget ()
((min-value :initarg :min-value
:accessor gadget-min-value)
(max-value :initarg :max-value
:accessor gadget-max-value)))
(defclass range-gadget-mixin (range-gadget)
;; Try to be compatible with Lispworks' CLIM.
())
(defgeneric gadget-range (range-gadget)
(:documentation
"Returns the difference of the maximum and minimum value of RANGE-GADGET."))
(defmethod gadget-range ((gadget range-gadget))
(- (gadget-max-value gadget)
(gadget-min-value gadget)))
(defgeneric gadget-range* (range-gadget)
(:documentation
"Returns the minimum and maximum value of RANGE-GADGET as two values."))
(defmethod gadget-range* ((gadget range-gadget))
(values (gadget-min-value gadget)
(gadget-max-value gadget)))
;;;; ------------------------------------------------------------------------------------------
;;;;
;;;; 30.4 Abstract Gadget Classes
;;;;
;;; 30.4.1 The abstract push-button Gadget
(defclass push-button (labelled-gadget-mixin action-gadget)
())
;;; 30.4.2 The abstract toggle-button Gadget
(defclass toggle-button (labelled-gadget-mixin value-gadget)
()
(:documentation "The value is either t either nil"))
;;; 30.4.3 The abstract menu-button Gadget
(defclass menu-button (labelled-gadget-mixin value-gadget)
()
(:documentation "The value is a button"))
;;; 30.4.4 The abstract scroll-bar Gadget
(defgeneric drag-callback (pane client gadget-id value)
(:argument-precedence-order client gadget-id value pane))
(defgeneric scroll-to-top-callback (scroll-bar client gadget-id)
(:argument-precedence-order client gadget-id scroll-bar))
(defgeneric scroll-to-bottom-callback (scroll-bar client gadget-id)
(:argument-precedence-order client gadget-id scroll-bar))
(defgeneric scroll-up-line-callback (scroll-bar client gadget-id)
(:argument-precedence-order client gadget-id scroll-bar))
(defgeneric scroll-up-page-callback (scroll-bar client gadget-id)
(:argument-precedence-order client gadget-id scroll-bar))
(defgeneric scroll-down-line-callback (scroll-bar client gadget-id)
(:argument-precedence-order client gadget-id scroll-bar))
(defgeneric scroll-down-page-callback (scroll-bar client gadget-id)
(:argument-precedence-order client gadget-id scroll-bar))
(defclass scroll-bar (value-gadget oriented-gadget-mixin range-gadget-mixin)
((drag-callback :initarg :drag-callback
:initform nil
:reader scroll-bar-drag-callback)
(scroll-to-bottom-callback :initarg :scroll-to-bottom-callback
:initform nil
:reader scroll-bar-scroll-to-bottom-callback)
(scroll-to-top-callback :initarg :scroll-to-top-callback
:initform nil
:reader scroll-bar-scroll-to-top-callback)
(scroll-down-line-callback :initarg :scroll-down-line-callback
:initform nil
:reader scroll-bar-scroll-down-line-callback)
(scroll-up-line-callback :initarg :scroll-up-line-callback
:initform nil
:reader scroll-bar-scroll-up-line-callback)
(scroll-down-page-callback :initarg :scroll-down-page-callback
:initform nil
:reader scroll-bar-scroll-down-page-callback)
(scroll-up-page-callback :initarg :scroll-up-page-callback
:initform nil
:reader scroll-bar-scroll-up-page-callback)
(thumb-size :initarg :thumb-size :initform 1/4
:accessor scroll-bar-thumb-size
:documentation "The size of the scroll bar thumb (slug) in the
units of the gadget value. When the scroll bar is drawn the empty region of
the scroll bar and the thumb are drawn in proportion to the values of the
gadget range and thumb size."))
(:default-initargs :value 0
:min-value 0
:max-value 1
:orientation :vertical))
(defmethod drag-callback ((pane scroll-bar) client gadget-id value)
(declare (ignore client gadget-id))
(invoke-callback pane (scroll-bar-drag-callback pane) value))
(defmethod scroll-to-top-callback ((pane scroll-bar) client gadget-id)
(declare (ignore client gadget-id))
(invoke-callback pane (scroll-bar-scroll-to-top-callback pane)))
(defmethod scroll-to-bottom-callback ((pane scroll-bar) client gadget-id)
(declare (ignore client gadget-id))
(invoke-callback pane (scroll-bar-scroll-to-bottom-callback pane)))
(defmethod scroll-up-line-callback ((pane scroll-bar) client gadget-id)
(declare (ignore client gadget-id))
(invoke-callback pane (scroll-bar-scroll-up-line-callback pane)))
(defmethod scroll-up-page-callback ((pane scroll-bar) client gadget-id)
(declare (ignore client gadget-id))
(invoke-callback pane (scroll-bar-scroll-up-page-callback pane)))
(defmethod scroll-down-line-callback ((pane scroll-bar) client gadget-id)
(declare (ignore client gadget-id))
(invoke-callback pane (scroll-bar-scroll-down-line-callback pane)))
(defmethod scroll-down-page-callback ((pane scroll-bar) client gadget-id)
(declare (ignore client gadget-id))
(invoke-callback pane (scroll-bar-scroll-down-page-callback pane)))
;;; 30.4.5 The abstract slider Gadget
(defclass slider-gadget (labelled-gadget-mixin
value-gadget
oriented-gadget-mixin
range-gadget-mixin
gadget-color-mixin
;;
value-changed-repaint-mixin
)
()
(:documentation "The value is a real number, and default value for orientation is :vertical,
and must never be nil."))
;;; 30.4.6 The abstract radio-box and check-box Gadgets
;; The only real different between a RADIO-BOX and a CHECK-BOX is the
;; number of allowed selections.
(defclass radio-box (value-gadget oriented-gadget-mixin)
()
(:documentation "The value is a button")
(:default-initargs
:value nil))
;; RADIO-BOX-CURRENT-SELECTION is just a synonym for GADGET-VALUE:
(defmethod radio-box-current-selection ((radio-box radio-box))
(gadget-value radio-box))
(defmethod (setf radio-box-current-selection) (new-value (radio-box radio-box))
(setf (gadget-value radio-box) new-value))
(defmethod radio-box-selections ((pane radio-box))
(let ((v (radio-box-current-selection pane)))
(and v (list v))))
(defmethod value-changed-callback :before (value-gadget (client radio-box) gadget-id value)
(declare (ignorable value-gadget gadget-id value))
;; Note that we ignore 'value', this is because if value is non-NIL,
;; then the toggle button was turned off, which would make no
;; toggle-button turned on => constraint "always exactly one
;; selected" missed. So simply turning this toggle button on again
;; fixes it.
(unless (or (and (not value)
(not (eq (gadget-value client) value-gadget)))
(and value
(eq (gadget-value client) value-gadget)))
(setf (gadget-value client :invoke-callback t) value-gadget)))
;;;; CHECK-BOX
(defclass check-box (value-gadget oriented-gadget-mixin)
()
(:documentation "The value is a list of buttons")
(:default-initargs
:value nil
:orientation :vertical))
;; CHECK-BOX-CURRENT-SELECTION is just a synonym for GADGET-VALUE:
(defmethod check-box-current-selection ((check-box check-box))
(gadget-value check-box))
(defmethod (setf check-box-current-selection) (new-value (check-box check-box))
(setf (gadget-value check-box) new-value))
(defmethod value-changed-callback :before (value-gadget (client check-box) gadget-id value)
(declare (ignorable gadget-id))
(if value
(setf (gadget-value client :invoke-callback t)
(adjoin value-gadget (gadget-value client)))
(setf (gadget-value client :invoke-callback t)
(remove value-gadget (gadget-value client)))))
(defmethod (setf gadget-value) :after (buttons (check-box check-box) &key invoke-callback)
;; this is silly, but works ...
(dolist (c (sheet-children check-box))
(unless (eq (not (null (member c buttons)))
(not (null (gadget-value c))))
(setf (gadget-value c :invoke-callback invoke-callback) (member c buttons)) )))
(defmacro with-radio-box ((&rest options
&key (type :one-of) (orientation :vertical) &allow-other-keys)
&body body)
(let ((contents (gensym "CONTENTS-"))
(selected-p (gensym "SELECTED-P-"))
(initial-selection (gensym "INITIAL-SELECTION-")))
`(let ((,contents nil)
(,selected-p nil)
(,initial-selection nil))
(declare (special ,selected-p))
(flet ((make-pane (type &rest options)
(cond ((eq type 'toggle-button)
(let ((pane (apply #'make-pane type
:value ,selected-p
:indicator-type ',type
options)))
(push pane ,contents)
(when ,selected-p
(push pane ,initial-selection))))
(t
(error "oops")))))
(macrolet ((radio-box-current-selection (subform)
`(let ((,',selected-p t))
(declare (special ,',selected-p))
,(cond ((stringp subform)
`(make-pane 'toggle-button :label ,subform))
(t
subform)))))
,@(mapcar (lambda (form)
(cond ((stringp form)
`(make-pane 'toggle-button :label ,form))
(t
form)))
body)))
(make-pane ',(if (eq type :one-of)
'radio-box
'check-box)
:orientation ',orientation
:current-selection ,(if (eq type :one-of)
`(or (first ,initial-selection)
(first ,contents))
`,initial-selection)
:choices (reverse ,contents)
,@options))))
;;; 30.4.7 The abstract list-pane and option-pane Gadgets
(defclass list-pane (value-gadget)
()
(:documentation
"The instantiable class that implements an abstract list pane, that is, a gadget
whose semantics are similar to a radio box or check box, but whose visual
appearance is a list of buttons.")
(:default-initargs :value nil))
(defclass option-pane (value-gadget)
()
(:documentation
"The instantiable class that implements an abstract option pane, that is, a
gadget whose semantics are identical to a list pane, but whose visual
appearance is a single push button which, when pressed, pops up a menu of
selections."))
;;; 30.4.8 The abstract text-field Gadget
(defclass text-field (value-gadget action-gadget)
((editable-p :accessor editable-p :initarg editable-p :initform t))
(:documentation "The value is a string")
(:default-initargs :value ""))
(defmethod initialize-instance :after ((gadget text-field) &rest rest)
(unless (getf rest :normal)
(setf (slot-value gadget 'current-color) +white+
(slot-value gadget 'normal) +white+)))
;;; 30.4.9 The abstract text-editor Gadget
(defclass text-editor (text-field)
()
(:documentation "The value is a string"))
;;;; ------------------------------------------------------------------------------------------
;;;;
;;;; Mixin Classes for Concrete Gadgets
;;;;
(defclass standard-gadget-pane (;;permanent-medium-sheet-output-mixin
;;immediate-sheet-input-mixin
;;immediate-repainting-mixin
sheet-leaf-mixin
standard-gadget)
()
(:documentation
"PANE class to include in gadget pane classes."))
;;;; Redrawing mixins
(defclass arm/disarm-repaint-mixin ()
()
(:documentation
"Mixin class for gadgets, whose appearence depends on its armed state."))
(defmethod armed-callback :after ((gadget arm/disarm-repaint-mixin) client id)
(declare (ignore client id))
(dispatch-repaint gadget (or (pane-viewport-region gadget)
(sheet-region gadget))))
(defmethod disarmed-callback :after ((gadget arm/disarm-repaint-mixin) client id)
(declare (ignore client id))
(dispatch-repaint gadget (or (pane-viewport-region gadget)
(sheet-region gadget))))
(defclass value-changed-repaint-mixin ()
()
(:documentation
"Mixin class for gadgets, whose appearence depends on its value."))
(defmethod (setf gadget-value) :after (new-value (gadget value-changed-repaint-mixin)
&key &allow-other-keys)
(declare (ignore new-value))
(dispatch-repaint gadget (or (pane-viewport-region gadget)
(sheet-region gadget))))
;;;; Event handling mixins
(defclass enter/exit-arms/disarms-mixin ()
()
(:documentation
"Mixin class for gadgets which are armed when the mouse enters and
disarmed when the mouse leaves."))
(defmethod handle-event :before ((pane enter/exit-arms/disarms-mixin) (event pointer-enter-event))
(declare (ignorable event))
(arm-gadget pane))
(defmethod handle-event :after ((pane enter/exit-arms/disarms-mixin) (event pointer-exit-event))
(declare (ignorable event))
(disarm-gadget pane))
;;;; changing-label-invokes-layout-protocol-mixin
(defclass changing-label-invokes-layout-protocol-mixin ()
()
(:documentation
"Mixin class for gadgets, which want invoke the layout protocol, if the label changes."))
;;;; Common behavior on STANDARD-GADGET-PANE and BASIC-GADGET
;;
;; When a gadget is not activated, it receives no device events.
;;
(defmethod handle-event :around ((pane standard-gadget) (event device-event))
(when (gadget-active-p pane)
(call-next-method)))
;; When a gadget is deactivated, it cannot be armed.
;; Glitch: upon re-activation the mouse might happen to be in the
;; gadget and thus re-arm it immediately, that is not implemented.
(defmethod note-gadget-deactivated :after (client (gadget standard-gadget))
(declare (ignorable client))
(disarm-gadget gadget))
;;;; ------------------------------------------------------------------------------------------
;;;;
;;;; Drawing Utilities for Concrete Gadgets
;;;;
;;; Labels
(defmethod compose-label-space ((gadget labelled-gadget-mixin) &key (wider 0) (higher 0))
(with-slots (label align-x align-y) gadget
(let* ((as (text-style-ascent (pane-text-style gadget) gadget))
(ds (text-style-descent (pane-text-style gadget) gadget))
(w (+ (text-size gadget label :text-style (pane-text-style gadget)) wider))
(h (+ as ds higher)))
(make-space-requirement :width w :min-width w :max-width w
:height h :min-height h :max-height h))))
(defmethod draw-label* ((pane labelled-gadget-mixin) x1 y1 x2 y2
&key (ink +foreground-ink+))
(with-slots (align-x align-y label) pane
(let ((as (text-style-ascent (pane-text-style pane) pane))
(ds (text-style-descent (pane-text-style pane) pane))
(w (text-size pane label :text-style (pane-text-style pane))))
(draw-text* pane label
(case align-x
((:left) x1)
((:right) (- x2 w))
((:center) (/ (+ x1 x2 (- w)) 2))
(otherwise x1)) ;defensive programming
(case align-y
((:top) (+ y1 as))
((:center) (/ (+ y1 y2 (- as ds)) 2))
((:bottom) (- y2 ds))
(otherwise (/ (+ y1 y2 (- as ds)) 2))) ;defensive programming
;; Giving the text-style here shouldn't be neccessary --GB
:text-style (pane-text-style pane)
:ink ink))))
;;; 3D-ish Look
;; DRAW-BORDERED-POLYGON medium point-seq &key border-width style
;;
;; -GB
(labels ((line-hnf (x1 y1 x2 y2)
(values (- y2 y1) (- x1 x2) (- (* x1 y2) (* y1 x2))))
(line-line-intersection (x1 y1 x2 y2 x3 y3 x4 y4)
(multiple-value-bind (a1 b1 c1) (line-hnf x1 y1 x2 y2)
(multiple-value-bind (a2 b2 c2) (line-hnf x3 y3 x4 y4)
(let ((d (- (* a1 b2) (* b1 a2))))
(cond ((< (abs d) 1e-6)
nil)
(t
(values (/ (- (* b2 c1) (* b1 c2)) d)
(/ (- (* a1 c2) (* a2 c1)) d))))))))
(polygon-orientation (point-seq)
"Determines the polygon's orientation.
Returns: +1 = counter-clock-wise
-1 = clock-wise
The polygon should be clean from duplicate points or co-linear points.
If the polygon self intersects, the orientation may not be defined, this
function does not try to detect this situation and happily returns some
value."
;;
(let ((n (length point-seq)))
(let* ((min-i 0)
(min-val (point-x (elt point-seq min-i))))
;;
(loop for i from 1 below n do
(when (< (point-x (elt point-seq i)) min-val)
(setf min-val (point-x (elt point-seq i))
min-i i)))
;;
(let ((p0 (elt point-seq (mod (+ min-i -1) n)))
(p1 (elt point-seq (mod (+ min-i 0) n)))
(p2 (elt point-seq (mod (+ min-i +1) n))))
(signum (- (* (- (point-x p2) (point-x p0)) (- (point-y p1) (point-y p0)))
(* (- (point-x p1) (point-x p0)) (- (point-y p2) (point-y p0)))))))))
(clean-polygon (point-seq)
"Cleans a polygon from duplicate points and co-linear points. Furthermore
tries to bring it into counter-clock-wise orientation."
;; first step: remove duplicates
(setf point-seq
(let ((n (length point-seq)))
(loop for i from 0 below n
for p0 = (elt point-seq (mod (+ i -1) n))
for p1 = (elt point-seq (mod (+ i 0) n))
unless (and (< (abs (- (point-x p0) (point-x p1))) 10e-8)
(< (abs (- (point-y p0) (point-y p1))) 10e-8))
collect p1)))
;; second step: remove colinear points
(setf point-seq
(let ((n (length point-seq)))
(loop for i from 0 below n
for p0 = (elt point-seq (mod (+ i -1) n))
for p1 = (elt point-seq (mod (+ i 0) n))
for p2 = (elt point-seq (mod (+ i +1) n))
unless (< (abs (- (* (- (point-x p1) (point-x p0)) (- (point-y p2) (point-y p0)))
(* (- (point-x p2) (point-x p0)) (- (point-y p1) (point-y p0)))))
10e-8)
collect p1)))
;; third step: care for the orientation
(if (and (not (null point-seq))
(minusp (polygon-orientation point-seq)))
(reverse point-seq)
point-seq) ))
(defun shrink-polygon (point-seq width)
(let ((point-seq (clean-polygon point-seq)))
(let ((n (length point-seq)))
(values
point-seq
(loop for i from 0 below n
for p0 = (elt point-seq (mod (+ i -1) n))
for p1 = (elt point-seq (mod (+ i 0) n))
for p2 = (elt point-seq (mod (+ i +1) n))
collect
(let* ((dx1 (- (point-x p1) (point-x p0))) (dy1 (- (point-y p1) (point-y p0)))
(dx2 (- (point-x p2) (point-x p1))) (dy2 (- (point-y p2) (point-y p1)))
;;
(m1 (/ width (sqrt (+ (* dx1 dx1) (* dy1 dy1)))))
(m2 (/ width (sqrt (+ (* dx2 dx2) (* dy2 dy2)))))
;;
(q0 (make-point (+ (point-x p0) (* m1 dy1)) (- (point-y p0) (* m1 dx1))))
(q1 (make-point (+ (point-x p1) (* m1 dy1)) (- (point-y p1) (* m1 dx1))))
(q2 (make-point (+ (point-x p1) (* m2 dy2)) (- (point-y p1) (* m2 dx2))))
(q3 (make-point (+ (point-x p2) (* m2 dy2)) (- (point-y p2) (* m2 dx2)))) )
;;
(multiple-value-bind (x y)
(multiple-value-call #'line-line-intersection
(point-position q0) (point-position q1)
(point-position q2) (point-position q3))
(if x
(make-point x y)
(make-point 0 0)))))))))
(defun draw-bordered-polygon (medium point-seq
&key (border-width 2)
(style :inset))
(labels ((draw-pieces (outer-points inner-points dark light)
(let ((n (length outer-points)))
(dotimes (i n)
(let* ((p1 (elt outer-points (mod (+ i 0) n)))
(p2 (elt outer-points (mod (+ i +1) n)))
(q1 (elt inner-points (mod (+ i 0) n)))
(q2 (elt inner-points (mod (+ i +1) n)))
(p1* (transform-region +identity-transformation+ p1))
(p2* (transform-region +identity-transformation+ p2))
(a (mod (atan (- (point-y p2*) (point-y p1*))
(- (point-x p2*) (point-x p1*)))
(* 2 pi))))
(draw-polygon medium (list p1 q1 q2 p2)
:ink
(if (<= (* 1/4 pi) a (* 5/4 pi))
dark light)))))))
(let ((light *3d-light-color*)
(dark *3d-dark-color*))
;;
(ecase style
(:solid
(multiple-value-call #'draw-pieces (shrink-polygon point-seq border-width)
+black+ +black+))
(:inset
(multiple-value-call #'draw-pieces (shrink-polygon point-seq border-width)
dark light))
(:outset
(multiple-value-call #'draw-pieces (shrink-polygon point-seq border-width)
light dark))
;;
;; Mickey Mouse is the trademark of the Walt Disney Company.
;;
(:mickey-mouse-outset
(multiple-value-bind (outer-points inner-points) (shrink-polygon point-seq border-width)
(declare (ignore outer-points))
(multiple-value-bind (outer-points middle-points) (shrink-polygon point-seq (/ border-width 2))
(draw-pieces outer-points middle-points +white+ +black+)
(draw-pieces middle-points inner-points light dark))))
(:mickey-mouse-inset
(multiple-value-bind (outer-points inner-points) (shrink-polygon point-seq border-width)
(declare (ignore outer-points))
(multiple-value-bind (outer-points middle-points) (shrink-polygon point-seq (/ border-width 2))
(draw-pieces outer-points middle-points dark light)
(draw-pieces middle-points inner-points +black+ +white+))))
;;
(:ridge
(multiple-value-bind (outer-points inner-points) (shrink-polygon point-seq border-width)
(declare (ignore outer-points))
(multiple-value-bind (outer-points middle-points) (shrink-polygon point-seq (/ border-width 2))
(draw-pieces outer-points middle-points light dark)
(draw-pieces middle-points inner-points dark light))))
(:groove
(multiple-value-bind (outer-points inner-points) (shrink-polygon point-seq border-width)
(declare (ignore outer-points))
(multiple-value-bind (outer-points middle-points) (shrink-polygon point-seq (/ border-width 2))
(draw-pieces outer-points middle-points dark light)
(draw-pieces middle-points inner-points light dark))))
(:double
(multiple-value-bind (outer-points inner-points) (shrink-polygon point-seq border-width)
(declare (ignore outer-points))
(multiple-value-bind (outer-points imiddle-points) (shrink-polygon point-seq (* 2/3 border-width))
(declare (ignore outer-points))
(multiple-value-bind (outer-points omiddle-points) (shrink-polygon point-seq (* 1/3 border-width))
(draw-pieces outer-points omiddle-points +black+ +black+)
(draw-pieces imiddle-points inner-points +black+ +black+))))))))) )
(defun draw-bordered-rectangle* (medium x1 y1 x2 y2 &rest options)
(apply #'draw-bordered-polygon
medium
(polygon-points (make-rectangle* x1 y1 x2 y2))
options))
(defun draw-engraved-label* (pane x1 y1 x2 y2)
(draw-label* pane (1+ x1) (1+ y1) (1+ x2) (1+ y2) :ink *3d-light-color*)
(draw-label* pane x1 y1 x2 y2 :ink *3d-dark-color*))
;;;;
;;;; 3D-BORDER-MIXIN Class
;;;;
;; 3D-BORDER-MIXIN class can be used to add a 3D-ish border to
;; panes. There are three new options:
;;
;; :border-width The width of the border
;; :border-style The border's style one of :inset, :outset, :groove, :ridge, :solid,
;; :double, :dotted, :dashed
;; [:dotted and :dashed are not yet implemented]
;;
;; :border-color The border's color
;; [Not implemented yet]
;;
;; [These options are modelled after CSS].
;;
;; When using 3D-BORDER-MIXIN, one should query the pane's inner
;; region, where drawing should take place, by PANE-INNER-REGION.
;;
;; --GB
(defclass 3D-border-mixin ()
((border-width :initarg :border-width :initform 2)
(border-style :initarg :border-style :initform :outset)
(border-color :initarg :border-color :initform "???")))
(defmethod pane-inner-region ((pane 3D-border-mixin))
(with-slots (border-width) pane
(with-bounding-rectangle* (x1 y1 x2 y2) (sheet-region pane)
(make-rectangle* (+ x1 border-width) (+ y1 border-width)
(- x2 border-width) (- y2 border-width)))))
(defmethod handle-repaint :after ((pane 3D-border-mixin) region)
(declare (ignore region))
(with-slots (border-width border-style) pane
(draw-bordered-polygon pane (polygon-points (bounding-rectangle (sheet-region pane)))
:border-width border-width
:style border-style)))
;;;; ------------------------------------------------------------------------------------------
;;;;
;;;; 30.4a Concrete Gadget Classes
;;;;
;; xxx move these!
(defparameter *3d-border-thickness* 2)
;;; Common colors:
(defmethod gadget-highlight-background ((gadget basic-gadget))
(compose-over (compose-in #|+paleturquoise+|# +white+ (make-opacity .5))
(pane-background gadget)))
(defmethod effective-gadget-foreground ((gadget basic-gadget))
(if (gadget-active-p gadget)
+foreground-ink+
(compose-over (compose-in (pane-foreground gadget)
(make-opacity .5))
(pane-background gadget))))
(defmethod effective-gadget-background ((gadget basic-gadget))
(if (slot-value gadget 'armed)
(gadget-highlight-background gadget)
(pane-background gadget)))
(defmethod effective-gadget-input-area-color ((gadget basic-gadget))
(if (gadget-active-p gadget)
+lemonchiffon+
(compose-over (compose-in +lemonchiffon+ (make-opacity .5))
(pane-background gadget))))
;;; ------------------------------------------------------------------------------------------