-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.lisp
352 lines (302 loc) · 11.6 KB
/
menu.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
;;; -*- Mode: Lisp; Package: ECLIPSE-INTERNALS -*-
;;; $Id: menu.lisp,v 1.8 2004-11-30 23:48:10 ihatchondo Exp $
;;;
;;; This file is part of Eclipse
;;; Copyright (C) 2000, 2001, 2002 Iban HATCHONDO, Robert STRANDH
;;; contact : [email protected]
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License
;;; as published by the Free Software Foundation.
;;;
;;; This program 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 General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;;; Pop-up menu
(in-package :ECLIPSE-INTERNALS)
(defvar *gctxt*)
(defvar *black*)
(defvar *white*)
(defvar *background1*)
(defvar *background2*)
(defvar *menu-item-margin* 2)
(defvar *default-menu-height* 20)
(defvar *max-char-width* 7)
(defvar *menu-color*
(xlib:make-color :red 0.8 :blue 0.8 :green 0.8)
"The menu background color")
(defvar *menu-hilighted-color*
(xlib:make-color :red 0.85 :blue 0.85 :green 0.85)
"The menu entry color hilighted (e.g: when the mouse passes over it)")
(defgeneric menu-root (item))
(defgeneric menu-root-application-window (item))
(defgeneric arm (item))
(defgeneric arm-branch (item))
(defgeneric disarm (item))
(defgeneric destroy-substructure (item))
(defmethod destroy-substructure ((item null)) nil)
(declaim (inline get-max-item-width))
(defun get-max-item-width (items)
(* *max-char-width*
(+ 3 (loop for item in items maximize (length (slot-value item 'text))))))
(defun make-raise-window (window)
(when window
(multiple-value-bind (width height) (drawable-sizes window)
(setf (xlib:gcontext-foreground *gctxt*) *background2*)
(xlib:draw-rectangle window *gctxt* 0 0 width height t)
(setf (xlib:gcontext-foreground *gctxt*) *white*)
(xlib:draw-lines window *gctxt* (list 1 height 1 1 1 1 width 1))
(setf (xlib:gcontext-foreground *gctxt*) *black*)
(xlib:draw-lines window *gctxt*
(list 1 (1- height) (1- width) (1- height)
(1- width) (1- height) (1- width) 0)))))
(defun highlight (item)
(declare (inline draw-glyphs))
(with-slots (window armed text) item
(when window
(let ((height (xlib:drawable-height window)))
(make-raise-window window)
(setf (xlib:gcontext-foreground *gctxt*) *black*)
(draw-glyphs window *gctxt* 7 (floor height 3/2) text)))))
(defun make-background-pixmap (window width height)
(let ((pix (xlib:create-pixmap
:drawable window
:width width
:height height
:depth (xlib:drawable-depth window))))
(make-raise-window pix)
pix))
;;; menu-widget
(defclass menu-widget ()
((armed :initform nil)))
;;; menu-item
(defclass menu-item (menu-widget)
((window :initform nil)
(client :initform nil :initarg :client)
(text :initarg :text)))
(defun realize-menu-items (parent width items
&key map
(x *menu-item-margin*) (y *menu-item-margin*))
(mapc #'(lambda (item)
(with-slots (window) item
(setf window
(xlib:create-window
:parent parent
:x x :y y
:width width
:height *default-menu-height*
:background *background1*
:override-redirect :on
:event-mask '(:exposure
:button-press
:button-release
:enter-window
:leave-window
:key-press
:key-release
:owner-grab-button)))
(save-widget window item)
(incf y *default-menu-height*)
(when map (xlib:map-window window))))
items))
(defmethod menu-root ((item menu-item))
(menu-root (slot-value item 'client)))
(defmethod menu-root-application-window ((item menu-item))
(menu-root-application-window (slot-value item 'client)))
(defmethod arm ((item menu-item))
(with-slots (client armed) item
(unless armed
(arm client)
(mapc #'disarm (slot-value client 'items))
(setf (slot-value item 'armed) t)
(event-process (make-instance 'exposure) item))))
(defmethod arm-branch ((item menu-item))
(with-slots (client armed) item
(if armed
(mapc #'destroy-substructure (slot-value item 'items))
(progn
(arm client)
(mapc #'destroy-substructure (slot-value client 'items))
(arm item)))))
(defmethod event-process ((event exposure) (item menu-item))
(declare (inline draw-glyphs))
(with-slots (window armed client text) item
(when window
(multiple-value-bind (width height) (drawable-sizes window)
(setf (xlib:gcontext-foreground *gctxt*) *background1*)
(xlib:draw-rectangle window *gctxt* 0 0 width height t)
(setf (xlib:gcontext-foreground *gctxt*) *black*)
(draw-glyphs window *gctxt* 7 (floor height 3/2) text)))))
(defmethod event-process ((event leave-notify) (item menu-item))
(when (eql :ungrab (event-mode event))
(destroy-substructure (menu-root item))))
(defmethod event-process ((event enter-notify) (item menu-item))
(when (slot-value (slot-value item 'client) 'armed)
(arm-branch item)))
(defmethod event-process ((event button-press) (item menu-item))
(arm-branch item))
;;; menu-leaf-item
(defclass menu-leaf-item (menu-item)
((callback :initform nil :initarg :callback :reader menu-leaf-callback)))
(defun make-menu-leaf-item (text parent callback)
(make-instance 'menu-leaf-item :text text :callback callback :client parent))
(defmethod destroy-substructure ((item menu-leaf-item))
(setf (slot-value item 'armed) nil))
(defmethod disarm ((item menu-item))
(setf (slot-value item 'armed) nil)
(event-process (make-instance 'exposure) item))
(defmethod event-process :after ((event exposure) (item menu-leaf-item))
(with-slots (armed) item
(when armed
(highlight item))))
(defmethod event-process ((event button-press) (item menu-leaf-item)))
(defmethod event-process ((event button-release) (item menu-leaf-item))
(with-slots (armed callback) item
(when armed
(when callback (funcall (the function callback)))
(destroy-substructure (menu-root item)))))
(defmethod event-process ((event leave-notify) (item menu-leaf-item))
(disarm item))
;;; sub-menu
(defclass sub-menu (menu-item)
((items :initarg :items)
(item-container :initform nil)
(bottomp :initform nil :initarg :bottomp)
(has-substructure :initform nil)))
(defun make-sub-menu (item parent &optional bottomp)
(let ((sub-menu (make-instance 'sub-menu
:text (car item)
:client parent
:bottomp bottomp)))
(setf (slot-value sub-menu 'items)
(mapcar #'(lambda (item)
(if (or (null (cdr item)) (functionp (cdr item)))
(make-menu-leaf-item (car item) sub-menu (cdr item))
(make-sub-menu item sub-menu)))
(cdr item)))
sub-menu))
(defun make-substructure (sub-menu)
(with-slots (window items has-substructure bottomp item-container) sub-menu
(let ((root-window (menu-root-application-window sub-menu))
(subheight (* *default-menu-height* (length items)))
(subwidth (+ (get-max-item-width items) (* 2 *menu-item-margin*))))
(multiple-value-bind (x y)
(xlib:translate-coordinates window 0 0 root-window)
(if bottomp
(incf y (xlib:drawable-height window))
(incf x (xlib:drawable-width window)))
(incf subheight (* 2 *menu-item-margin*))
(if (< (xlib:drawable-width root-window) (+ x subwidth))
(decf x (+ subwidth (xlib:drawable-width window) *menu-item-margin*))
(incf x *menu-item-margin*))
(when (< (xlib:drawable-height root-window) (+ y subheight))
(decf y (- subheight *default-menu-height*)))
(setf item-container (xlib:create-window
:parent root-window
:x x :y y
:width subwidth
:height subheight
:override-redirect :on
:background (make-background-pixmap
root-window
subwidth
subheight))))
(save-widget item-container sub-menu)
(decf subwidth (* 2 *menu-item-margin*))
(realize-menu-items item-container subwidth items))))
(defmethod destroy-substructure ((item sub-menu))
(with-slots (items armed has-substructure window item-container) item
(when has-substructure
(mapc #'(lambda (item)
(destroy-substructure item)
(clear-widget (slot-value item 'window))
(setf (slot-value item 'window) nil))
items)
(clear-widget item-container)
(xlib:destroy-window item-container))
(setf armed nil
has-substructure nil
item-container nil)))
(defmethod arm-branch :after ((sub-menu sub-menu))
(with-slots (has-substructure item-container window) sub-menu
(unless has-substructure
(make-substructure sub-menu)
(xlib:map-window item-container)
(xlib:map-subwindows item-container)
(setf has-substructure t))))
(defmethod event-process :after ((event exposure) (item sub-menu))
(with-slots (armed has-substructure) item
(when (or armed has-substructure)
(highlight item))))
(defmethod event-process ((event button-release) (item sub-menu))
(with-slots (armed) item
(when armed
(destroy-substructure (menu-root item)))))
(defmethod event-process ((event leave-notify) (sub-menu sub-menu))
(declare (ignorable sub-menu))
nil)
;;; pop-up menu
(defclass pop-up-menu (menu-widget)
((window :initform nil)
(client :initform nil :initarg :client)
(items :initarg :items)))
(defun make-pop-up (application &rest menu-pop-up-items)
(let ((pop-up (make-instance 'pop-up-menu)))
(with-slots (client items) pop-up
(setf client application
items
(mapcar #'(lambda (item)
(if (or (null (cdr item)) (functionp (cdr item)))
(make-menu-leaf-item (car item) pop-up (cdr item))
(make-sub-menu item pop-up)))
menu-pop-up-items)))
pop-up))
(defun realize-pop-up (pop-up-menu x y)
(with-slots (window menu-present armed items) pop-up-menu
(when window (destroy-substructure pop-up-menu))
(let ((root-window (menu-root-application-window pop-up-menu))
(subheight (* *default-menu-height* (length items)))
(subwidth (get-max-item-width items)))
(when (< (xlib:drawable-width root-window) (+ x subwidth))
(decf x subwidth))
(when (< (xlib:drawable-height root-window) (+ y subheight))
(decf y subheight))
(setf window (xlib:create-window
:parent root-window
:x x :y y
:width (+ subwidth (* 2 *menu-item-margin*))
:height (+ subheight (* 2 *menu-item-margin*))
:override-redirect :on
:background (make-background-pixmap
root-window
(+ subwidth (* 2 *menu-item-margin*))
(+ subheight (* 2 *menu-item-margin*))))
armed t)
(save-widget window pop-up-menu)
(xlib:map-window window)
(realize-menu-items window subwidth items :map t))))
(defmethod destroy-substructure ((pop-up-menu pop-up-menu))
(with-slots (window items) pop-up-menu
(when window
(mapc #'(lambda (item)
(destroy-substructure item)
(clear-widget (slot-value item 'window))
(setf (slot-value item 'window) nil))
items)
(xlib:destroy-window window)
(clear-widget window)
(setf (slot-value pop-up-menu 'armed) nil
window nil))))
(defmethod arm ((widget pop-up-menu))
(setf (slot-value widget 'armed) t))
(defmethod disarm ((widget pop-up-menu))
(setf (slot-value widget 'armed) nil))
(defmethod menu-root ((widget pop-up-menu))
widget)
(defmethod menu-root-application-window ((widget pop-up-menu))
(slot-value (slot-value widget 'client) 'window))