22c52f82b2b35e0384a266207747a02d71dd12f3
[clfswm.git] / src / clfswm-expose-mode.lisp
blob22c52f82b2b35e0384a266207747a02d71dd12f3
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)))))
91 (defun expose-create-window (ex-child)
92 (let ((child (expose-child-child ex-child)))
93 (with-current-child (child)
94 (let* ((string (format nil "~A"
95 (if *expose-show-window-title*
96 (ensure-printable (child-fullname child))
97 "")))
98 (width (if *expose-show-window-title*
99 (min (* (xlib:max-char-width *expose-font*) (+ (length string) 2))
100 (- (child-width child) 4))
101 (* (xlib:max-char-width *expose-font*) 3)))
102 (height (* (xlib:font-ascent *expose-font*) 3)))
103 (with-placement (*expose-mode-placement* x y width height)
104 (let* ((window (xlib:create-window :parent *root*
105 :x x :y y
106 :width width :height height
107 :background (get-color *expose-background*)
108 :border-width *border-size*
109 :border (get-color *expose-border*)
110 :colormap (xlib:screen-default-colormap *screen*)
111 :event-mask '(:exposure :key-press)))
112 (gc (xlib:create-gcontext :drawable window
113 :foreground (get-color *expose-foreground*)
114 :background (get-color *expose-background*)
115 :font *expose-font*
116 :line-style :solid)))
117 (setf (window-transparency window) *expose-transparency*)
118 (map-window window)
119 (setf (expose-child-window ex-child) window
120 (expose-child-gc ex-child) gc
121 (expose-child-string ex-child) string)))))))
126 (defun expose-query-key-press-hook (code state)
127 (declare (ignore code state))
128 (expose-draw-letter)
129 (when (and *expose-direct-select* (<= (length *expose-child-list*) 26))
130 (leave-query-mode :return)))
132 (defun expose-query-button-press-hook (code state x y)
133 (declare (ignore state))
134 (when (= code 1)
135 (setf *expose-selected-child* (find-child-under-mouse x y)))
136 (leave-query-mode :click))
139 (defun expose-init ()
140 (setf *expose-font* (xlib:open-font *display* *expose-font-string*)
141 *expose-child-list* (expose-associate-keys)
142 *expose-selected-child* nil
143 *query-string* "")
144 (xlib:warp-pointer *root* (truncate (/ (screen-width) 2))
145 (truncate (/ (screen-height) 2)))
146 (add-hook *query-key-press-hook* 'expose-query-key-press-hook)
147 (add-hook *query-button-press-hook* 'expose-query-button-press-hook))
149 (defun expose-present-windows ()
150 (dolist (ex-child *expose-child-list*)
151 (let ((child (expose-child-child ex-child)))
152 (when (frame-p child)
153 (setf (frame-data-slot child :old-layout) (frame-layout child)
154 (frame-layout child) #'tile-space-layout))))
155 (show-all-children t))
157 (defun expose-unpresent-windows ()
158 (dolist (ex-child *expose-child-list*)
159 (let ((child (expose-child-child ex-child)))
160 (when (frame-p child)
161 (setf (frame-layout child) (frame-data-slot child :old-layout)
162 (frame-data-slot child :old-layout) nil)))))
164 (defun expose-mode-display-accel-windows ()
165 (with-all-root-child (root)
166 (with-all-children-reversed (root child)
167 (let ((ex-child (find child *expose-child-list* :test #'child-equal-p :key #'expose-child-child)))
168 (when ex-child
169 (if (or (frame-p (expose-child-child ex-child))
170 (managed-window-p (expose-child-child ex-child)
171 (find-parent-frame (expose-child-child ex-child) *root-frame*)))
172 (expose-create-window ex-child)
173 (hide-child (expose-child-child ex-child)))))))
174 (expose-draw-letter))
177 (defun expose-find-child-from-letters (letters)
178 (find letters *expose-child-list* :test #'string-equal :key #'expose-child-key))
180 (defun expose-select-child ()
181 (let ((*query-mode-placement* *expose-query-placement*))
182 (multiple-value-bind (letters return)
183 (query-string "Which child ?")
184 (let ((ex-child (case return
185 (:return (expose-find-child-from-letters letters))
186 (:click *expose-selected-child*))))
187 (when ex-child
188 (expose-child-child ex-child))))))
191 (defun expose-restore-windows ()
192 (remove-hook *query-key-press-hook* 'expose-query-key-press-hook)
193 (remove-hook *query-button-press-hook* 'expose-query-button-press-hook)
194 (dolist (ex-child *expose-child-list*)
195 (awhen (expose-child-gc ex-child)
196 (xlib:free-gcontext it))
197 (awhen (expose-child-window ex-child)
198 (xlib:destroy-window it)))
199 (when *expose-font*
200 (xlib:close-font *expose-font*))
201 (expose-unpresent-windows))
203 (defun expose-focus-child (child)
204 (let ((parent (typecase child
205 (xlib:window (find-parent-frame child))
206 (frame child))))
207 (when (and child parent)
208 (change-root (find-root parent) parent)
209 (setf (current-child) child)
210 (focus-all-children child parent t))))
212 (defun expose-do-main ()
213 (stop-button-event)
214 (expose-init)
215 (expose-present-windows)
216 (expose-mode-display-accel-windows)
217 (let ((child (expose-select-child)))
218 (expose-restore-windows)
219 child))
221 (defun expose-windows-mode ()
222 "Present all windows in currents roots (An expose like)"
223 (awhen (expose-do-main)
224 (expose-focus-child it))
225 (show-all-children)
229 (defun expose-all-windows-mode ()
230 "Present all windows in all frames (An expose like)"
231 (let ((child nil))
232 (with-saved-root-list ()
233 (dolist (root (get-root-list))
234 (change-root root (root-original root)))
235 (setf child (expose-do-main)))
236 (when child
237 (expose-focus-child child)))
238 (show-all-children)