File: font-selector.lisp

package info (click to toggle)
cl-mcclim 0.9.6.dfsg.cvs20100315-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 6,624 kB
  • ctags: 13,512
  • sloc: lisp: 106,015; makefile: 47; sh: 15
file content (167 lines) | stat: -rw-r--r-- 5,909 bytes parent folder | download | duplicates (2)
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
;;; -*- Mode: Lisp; show-trailing-whitespace: t; indent-tabs: nil -*-

;;; A font selection dialog.

#|

(clim-demo::select-font)

(clim-demo::select-font
 :port (clim:find-port :server-path (list :ps :stream *standard-output*)))

|#

;;;  (c) 2006 David Lichteblau (david@lichteblau.com)

;;; 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-demo)

(defun select-font (&key (port (find-port)))
  (let ((frame
	 (make-application-frame 'font-selector :font-selector-port port)))
    (run-frame-top-level frame)
    (font-selector-text-style frame)))

(define-application-frame font-selector ()
    ((font-selector-port :initarg :font-selector-port
                         :initform (find-port)
                         :accessor font-selector-port)
     (font-selector-text-style :accessor font-selector-text-style))
  (:menu-bar nil)
  (:panes
   (canvas :application
	   :height 150
	   :scroll-bars nil
           :display-time t
           :display-function 'display-font-preview)
   (family
    (make-pane 'list-pane
               :items nil
               :name-key #'font-family-name
               :value-changed-callback 'family-changed))
   (face (make-pane 'list-pane
                    :items nil
                    :name-key #'font-face-name
                    :value-changed-callback 'face-changed))
   (size (make-pane 'list-pane
                    :items nil
                    :value-changed-callback 'size-changed)))
  (:layouts
   (default
       (vertically (:height 400 :width 600)
	 (horizontally ()
	   (labelling (:label "Family") (scrolling () family))
	   (labelling (:label "Face") (scrolling () face))
	   (labelling (:label "Size") (scrolling () size)))
	 canvas
	 (horizontally ()
	   +fill+
	   (make-pane 'push-button
		      :label "OK"
		      :activate-callback
		      (lambda (ignore)
			ignore
			(frame-exit *application-frame*)))
	   (make-pane 'push-button
		      :label "Cancel"
		      :activate-callback
		      (lambda (ignore)
			ignore
			(setf (font-selector-text-style *application-frame*)
			      nil)
			(frame-exit *application-frame*))))))))

(defmethod generate-panes :after (fm (frame font-selector))
  (reset-list-pane (find-pane-named frame 'family)
                       (port-all-font-families
			(font-selector-port *application-frame*))))

(defun family-changed (pane value)
  (declare (ignore pane))
  (let* ((face-list (find-pane-named *application-frame* 'face))
	 (old-face (and (slot-boundp face-list 'climi::value)
			(gadget-value face-list)))
	 (new-faces (font-family-all-faces value)))
    (reset-list-pane face-list new-faces)
    (when old-face
      (setf (gadget-value face-list :invoke-callback t)
	    (or (find (font-face-name old-face)
                      new-faces
                      :key #'font-face-name
                      :test #'equal)
                (first new-faces))))))

(defun face-changed (pane value)
  (declare (ignore pane))
  (let ((sizes (if value (font-face-all-sizes value) nil)))
    (reset-list-pane (find-pane-named *application-frame* 'size)
		     sizes
		     (or (position-if (lambda (x) (>= x 20)) sizes) 0))))

(defun size-changed (pane value)
  (declare (ignore pane))
  (setf (font-selector-text-style *application-frame*)
	(let ((face
	       (gadget-value (find-pane-named *application-frame* 'face))))
	  (if (and face value)
	      (font-face-text-style face value)
	      nil)))
  (display-font-preview *application-frame*
		  (frame-standard-output *application-frame*)))

(defun reset-list-pane (pane items &optional (index 0))
  (setf (climi::list-pane-items pane :invoke-callback nil) items)
  (setf (gadget-value pane :invoke-callback t)
	(or (and (slot-boundp pane 'climi::value) (gadget-value pane))
	    (let ((values (climi::generic-list-pane-item-values pane)))
	      (if (plusp (length values))
		  (elt values index)
		  nil)))))

(defmethod display-font-preview (frame stream)
  (window-clear stream)
  (let* ((pane-width (rectangle-width (sheet-region stream)))
         (pane-height (rectangle-height (sheet-region stream)))
         (str "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
         (style (font-selector-text-style frame))
	 (ok nil))
    (cond
      ((not (eq (port frame) (font-selector-port frame)))
	(setf str (format nil
			  "Cannot preview font for ~A"
			  (font-selector-port frame)))
	(setf style (make-text-style :sans-serif :italic :normal)))
      ((null style)
	(setf str "Error: Text style is null")
	(setf style (make-text-style :sans-serif :italic :normal)))
      (t
	(setf ok t)))
    (multiple-value-bind (width height final-x final-y baseline)
        (text-size stream str :text-style style)
      (declare (ignore final-x final-y))
      (let* ((x1 (/ (- pane-width width) 2))
             (y1 (/ (- pane-height height) 2))
             (y2 (+ y1 height))
             (ybase (+ y1 baseline)))
        (when ok
	  (draw-line* stream 0 ybase pane-width ybase :ink +green+)
	  (draw-line* stream 0 y1 pane-width y1 :ink +blue+)
	  (draw-line* stream 0 y2 pane-width y2 :ink +blue+))
        (handler-case
	    (draw-text* stream str x1 ybase :text-style style)
	  (error (c)
	    (princ c)))))))