Set expose windows and gcontexts to nil when done in expose-mode
[clfswm.git] / src / clfswm-expose-mode.lisp
blob31fbee1dd55afac21a976d00e39a6c5da21c7402
1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Expose functions - An expose like.
6 ;;; --------------------------------------------------------------------------
7 ;;;
8 ;;; (C) 2005-2013 Philippe Brochard <pbrochard@common-lisp.net>
9 ;;;
10 ;;; This program is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or
13 ;;; (at your option) any later version.
14 ;;;
15 ;;; This program is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with this program; if not, write to the Free Software
22 ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 ;;;
24 ;;; --------------------------------------------------------------------------
26 (in-package :clfswm)
28 (defparameter *expose-font* nil)
29 (defparameter *expose-selected-child* nil)
31 (defstruct expose-child child key window gc string)
33 (defun leave-expose-mode ()
34 "Leave the expose mode"
35 (throw 'exit-expose-loop nil))
37 (defun valid-expose-mode ()
38 "Valid the expose mode"
39 (throw 'exit-expose-loop t))
41 (defun mouse-leave-expose-mode (window root-x root-y)
42 "Leave the expose mode"
43 (declare (ignore window root-x root-y))
44 (throw 'exit-expose-loop nil))
46 (defun mouse-valid-expose-mode (window root-x root-y)
47 "Valid the expose mode"
48 (declare (ignore window root-x root-y))
49 (throw 'exit-expose-loop t))
52 (defun expose-associate-keys ()
53 (let* ((all nil)
54 (new nil))
55 (with-all-children-reversed (*root-frame* child)
56 (unless (child-equal-p child *root-frame*)
57 (push child all)
58 (unless (member child *expose-child-list* :test #'child-equal-p :key #'expose-child-child)
59 (push (make-expose-child :child child :key (number->letter *expose-current-number*)) new)
60 (incf *expose-current-number*))))
61 (append (remove-if-not (lambda (x) (member x all :test #'child-equal-p)) *expose-child-list*
62 :key #'expose-child-child)
63 (nreverse new))))
69 (defun expose-draw-letter ()
70 (dolist (ex-child *expose-child-list*)
71 (let ((window (expose-child-window ex-child))
72 (gc (expose-child-gc ex-child)))
73 (when (and window gc)
74 (clear-pixmap-buffer window gc)
75 (xlib:with-gcontext (gc :foreground (get-color (if (substring-equal *query-string* (expose-child-key ex-child))
76 *expose-foreground-letter*
77 *expose-foreground-letter-nok*))
78 :background (get-color (if (string-equal *query-string* (expose-child-key ex-child))
79 *expose-background-letter-match*
80 *expose-background*)))
81 (xlib:draw-image-glyphs *pixmap-buffer* gc
82 (xlib:max-char-width *expose-font*)
83 (+ (xlib:font-ascent *expose-font*) (xlib:font-descent *expose-font*))
84 (expose-child-key ex-child)))
85 (xlib:draw-glyphs *pixmap-buffer* gc
86 (xlib:max-char-width *expose-font*)
87 (+ (* 2 (xlib:font-ascent *expose-font*)) (xlib:font-descent *expose-font*) 1)
88 (expose-child-string ex-child))
89 (copy-pixmap-buffer window gc)))))
92 (defun expose-create-window (ex-child)
93 (let ((child (expose-child-child ex-child)))
94 (with-current-child (child)
95 (let* ((string (format nil "~A"
96 (if *expose-show-window-title*
97 (ensure-printable (child-fullname child))
98 "")))
99 (width (if *expose-show-window-title*
100 (min (* (xlib:max-char-width *expose-font*) (+ (length string) 2))
101 (- (child-width child) 4))
102 (* (xlib:max-char-width *expose-font*) 3)))
103 (height (* (xlib:font-ascent *expose-font*) 3)))
104 (with-placement (*expose-mode-placement* x y width height)
105 (let* ((window (xlib:create-window :parent *root*
106 :x x :y y
107 :width width :height height
108 :background (get-color *expose-background*)
109 :border-width *border-size*
110 :border (get-color *expose-border*)
111 :colormap (xlib:screen-default-colormap *screen*)
112 :event-mask '(:exposure :key-press)))
113 (gc (xlib:create-gcontext :drawable window
114 :foreground (get-color *expose-foreground*)
115 :background (get-color *expose-background*)
116 :font *expose-font*
117 :line-style :solid)))
118 (setf (window-transparency window) *expose-transparency*)
119 (map-window window)
120 (setf (expose-child-window ex-child) window
121 (expose-child-gc ex-child) gc
122 (expose-child-string ex-child) string)))))))
127 (defun expose-query-key-press-hook (code state)
128 (declare (ignore code state))
129 (expose-draw-letter)
130 (when (and *expose-direct-select* (<= (length *expose-child-list*) 26))
131 (leave-query-mode :return)))
133 (defun expose-query-button-press-hook (code state x y)
134 (declare (ignore state))
135 (when (= code 1)
136 (setf *expose-selected-child* (find-child-under-mouse x y)))
137 (leave-query-mode :click))
140 (defun expose-init ()
141 (setf *expose-font* (xlib:open-font *display* *expose-font-string*)
142 *expose-child-list* (expose-associate-keys)
143 *expose-selected-child* nil
144 *query-string* "")
145 (xlib:warp-pointer *root* (truncate (/ (screen-width) 2))
146 (truncate (/ (screen-height) 2)))
147 (add-hook *query-key-press-hook* 'expose-query-key-press-hook)
148 (add-hook *query-button-press-hook* 'expose-query-button-press-hook))
150 (defun expose-present-windows ()
151 (dolist (ex-child *expose-child-list*)
152 (let ((child (expose-child-child ex-child)))
153 (when (frame-p child)
154 (setf (frame-data-slot child :old-layout) (frame-layout child)
155 (frame-layout child) #'tile-space-layout))))
156 (show-all-children t))
158 (defun expose-unpresent-windows ()
159 (dolist (ex-child *expose-child-list*)
160 (let ((child (expose-child-child ex-child)))
161 (when (frame-p child)
162 (setf (frame-layout child) (frame-data-slot child :old-layout)
163 (frame-data-slot child :old-layout) nil)))))
165 (defun expose-mode-display-accel-windows ()
166 (with-all-root-child (root)
167 (with-all-children-reversed (root child)
168 (let ((ex-child (find child *expose-child-list* :test #'child-equal-p :key #'expose-child-child)))
169 (when ex-child
170 (if (or (frame-p (expose-child-child ex-child))
171 (managed-window-p (expose-child-child ex-child)
172 (find-parent-frame (expose-child-child ex-child) *root-frame*)))
173 (expose-create-window ex-child)
174 (hide-child (expose-child-child ex-child)))))))
175 (expose-draw-letter))
178 (defun expose-find-child-from-letters (letters)
179 (find letters *expose-child-list* :test #'string-equal :key #'expose-child-key))
181 (defun expose-select-child ()
182 (let ((*query-mode-placement* *expose-query-placement*))
183 (multiple-value-bind (letters return)
184 (query-string "Which child ?")
185 (let ((ex-child (case return
186 (:return (expose-find-child-from-letters letters))
187 (:click *expose-selected-child*))))
188 (when ex-child
189 (expose-child-child ex-child))))))
192 (defun expose-restore-windows ()
193 (remove-hook *query-key-press-hook* 'expose-query-key-press-hook)
194 (remove-hook *query-button-press-hook* 'expose-query-button-press-hook)
195 (dolist (ex-child *expose-child-list*)
196 (awhen (expose-child-gc ex-child)
197 (xlib:free-gcontext it))
198 (awhen (expose-child-window ex-child)
199 (xlib:destroy-window it))
200 (setf (expose-child-gc ex-child) nil
201 (expose-child-window ex-child) nil))
202 (when *expose-font*
203 (xlib:close-font *expose-font*))
204 (expose-unpresent-windows))
206 (defun expose-focus-child (child)
207 (let ((parent (typecase child
208 (xlib:window (find-parent-frame child))
209 (frame child))))
210 (when (and child parent)
211 (change-root (find-root parent) parent)
212 (setf (current-child) child)
213 (focus-all-children child parent t))))
215 (defun expose-do-main ()
216 (stop-button-event)
217 (expose-init)
218 (expose-present-windows)
219 (expose-mode-display-accel-windows)
220 (let ((child (expose-select-child)))
221 (expose-restore-windows)
222 child))
224 (defun expose-windows-mode ()
225 "Present all windows in currents roots (An expose like)"
226 (awhen (expose-do-main)
227 (expose-focus-child it))
228 (show-all-children)
232 (defun expose-all-windows-mode ()
233 "Present all windows in all frames (An expose like)"
234 (let ((child nil))
235 (with-saved-root-list ()
236 (dolist (root (get-root-list))
237 (change-root root (root-original root)))
238 (setf child (expose-do-main)))
239 (when child
240 (expose-focus-child child)))
241 (show-all-children)