comparison lisp/prim/menubar.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children 0293115a14e9
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;; Menubar support.
2 ;; Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
3 ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
4 ;; Copyright (C) 1995, 1996 Ben Wing.
5
6 ;; This file is part of XEmacs.
7
8 ;; XEmacs is free software; you can redistribute it and/or modify it
9 ;; under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; XEmacs is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;; General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with XEmacs; see the file COPYING. If not, write to the Free
20 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 ;;; Synched up with: Not in FSF. (Completely divergent from FSF menu-bar.el)
23 ;;; Some stuff in FSF menu-bar.el is in x-menubar.el
24
25 (defvar default-menubar nil)
26
27 ;; this function is considered "part of the lexicon" by many,
28 ;; so we'll leave it here.
29 (defun kill-this-buffer () ; for the menubar
30 "Kill the current buffer."
31 (interactive)
32 (kill-buffer (current-buffer)))
33
34 (defun set-menubar-dirty-flag ()
35 "Tell XEmacs that the menubar has to be updated.
36 NOTE: XEmacs now recognizes when you set a different value for
37 `current-menubar'. You *only* need to call this function if you
38 destructively modify a part of the menubar and don't set `current-menubar'.
39 Note that all the functions that modify a menu call this automatically."
40 (setq-default current-menubar (default-value 'current-menubar)))
41
42 ;; #### shouldn't this perhaps be `copy-tree'?
43 (defun set-menubar (menubar)
44 "Set the default menubar to be MENUBAR.
45 See `current-menubar' for a description of the syntax of a menubar."
46 (check-menu-syntax menubar t)
47 (setq-default current-menubar (copy-sequence menubar)))
48
49 (defun set-buffer-menubar (menubar)
50 "Set the buffer-local menubar to be MENUBAR.
51 See `current-menubar' for a description of the syntax of a menubar."
52 (check-menu-syntax menubar t)
53 (make-local-variable 'current-menubar)
54 (setq current-menubar (copy-sequence menubar)))
55
56 (defun check-menu-syntax (menu &optional menubar-p)
57 ;; The C code does syntax checking on the value of `current-menubar',
58 ;; but it's better to do it early, before things have gotten messed up.
59 (if menubar-p
60 nil
61 (or (stringp (car menu))
62 (signal 'error
63 (list "menu name (first element) must be a string" menu)))
64 ;;(or (cdr menu) (signal 'error (list "menu is empty" menu)))
65 (setq menu (cdr menu)))
66 (let (menuitem item)
67 (while (keywordp (setq item (car menu)))
68 (or (memq item '(:config :included :filter))
69 (signal 'error
70 (list "menu keyword must be :config, :included, or :filter"
71 item)))
72 (if (or (not (cdr menu))
73 (vectorp (nth 1 menu))
74 (keywordp (nth 1 menu)))
75 (signal 'error (list "strange keyword value" item (nth 1 menu))))
76 (setq menu (nthcdr 2 menu)))
77 (while menu
78 (setq menuitem (car menu))
79 (cond
80 ((stringp menuitem)
81 (and (string-match "^\\(-+\\|=+\\):\\(.*\\)" menuitem)
82 (setq item (match-string 2 menuitem))
83 (or (member item '(;; Motif-compatible
84 "singleLine"
85 "doubleLine"
86 "singleDashedLine"
87 "doubleDashedLine"
88 "noLine"
89 "shadowEtchedIn"
90 "shadowEtchedOut"
91 "shadowEtchedInDash"
92 "shadowEtchedOutDash"
93 ;; non-Motif (Lucid menubar widget only)
94 "shadowDoubleEtchedIn"
95 "shadowDoubleEtchedOut"
96 "shadowDoubleEtchedInDash"
97 "shadowDoubleEtchedOutDash"
98 ))
99 (signal 'error (list "bogus separator style in menu item" item)))
100 ))
101 ((null menuitem)
102 (or menubar-p
103 (signal 'error (list "nil is only permitted in the top level of menubars"))))
104 ((consp menuitem)
105 (check-menu-syntax menuitem))
106 ((vectorp menuitem)
107 (let ((L (length menuitem))
108 plistp)
109 (and (< L 3)
110 (signal 'error
111 (list "button descriptors must be at least 3 long"
112 menuitem)))
113 (setq plistp (or (>= L 5) (keywordp (aref menuitem 2))))
114 (or (stringp (aref menuitem 0))
115 (signal 'error
116 (list
117 "first element of a button must be a string (the label)"
118 menuitem)))
119 (or plistp
120 (< L 4)
121 (null (aref menuitem 3))
122 (stringp (aref menuitem 3))
123 (signal 'error
124 (list
125 "fourth element of a button must be a string (the label suffix)"
126 menuitem)))
127 (if plistp
128 (let ((i 2)
129 selp
130 style
131 item)
132 (while (< i L)
133 (setq item (aref menuitem i))
134 (cond ((not (memq item '(:active :suffix :keys :style
135 :full :included :selected)))
136 (signal 'error
137 (list (if (keywordp item)
138 "unknown menu item keyword"
139 "not a keyword")
140 item menuitem)))
141 ((eq item :style)
142 (setq style (aref menuitem (1+ i)))
143 (or (memq style '(nil toggle radio button text))
144 (signal 'error (list "unknown style" style
145 menuitem))))
146 ((eq item :selected) (setq selp t))
147 )
148 (setq i (+ i (if (eq item :full) 1 2))))
149 (if (and selp (not (memq style '(toggle button radio))))
150 (signal 'error
151 (list
152 ":selected only makes sense with :style toggle, radio, or button"
153 menuitem)))
154 )))
155 )
156 (t (signal 'error (list "unrecognised menu descriptor" menuitem))))
157 (setq menu (cdr menu)))))
158
159
160 ;;; menu manipulation functions
161
162 (defun find-menu-item (menubar item-path-list &optional parent)
163 "Search MENUBAR for item given by ITEM-PATH-LIST starting from PARENT.
164 Returns (ITEM . PARENT), where PARENT is the immediate parent of
165 the item found.
166 If the item does not exist, the car of the returned value is nil.
167 If some menu in the ITEM-PATH-LIST does not exist, an error is signalled."
168 (or (listp item-path-list)
169 (signal 'wrong-type-argument (list 'listp item-path-list)))
170 (or parent (setq item-path-list (mapcar 'downcase item-path-list)))
171 (if (not (consp menubar))
172 nil
173 (let ((rest menubar)
174 result)
175 (if (stringp (car rest))
176 (setq rest (cdr rest)))
177 (while (keywordp (car rest))
178 (setq rest (cddr rest)))
179 (while rest
180 (if (and (car rest)
181 (equal (car item-path-list)
182 (downcase (if (vectorp (car rest))
183 (aref (car rest) 0)
184 (if (stringp (car rest))
185 (car rest)
186 (car (car rest)))))))
187 (setq result (car rest) rest nil)
188 (setq rest (cdr rest))))
189 (if (cdr item-path-list)
190 (if (consp result)
191 (find-menu-item (cdr result) (cdr item-path-list) result)
192 (if result
193 (signal 'error (list (gettext "not a submenu") result))
194 (signal 'error (list (gettext "no such submenu") (car item-path-list)))))
195 (cons result parent)))))
196
197 (defun add-menu-item-1 (leaf-p menu-path new-item before)
198 (if before (setq before (downcase before)))
199 (let* ((item-name (if (vectorp new-item) (aref new-item 0) (car new-item)))
200 (menubar current-menubar)
201 (menu (condition-case ()
202 (car (find-menu-item menubar menu-path))
203 (error nil)))
204 (item-found (cond ((not (listp menu))
205 (signal 'error (list (gettext "not a submenu")
206 menu-path)))
207 (menu
208 (find-menu-item (cdr menu) (list item-name)))
209 (t
210 (find-menu-item menubar (list item-name)))
211 )))
212 (or menubar
213 (error "`current-menubar' is nil: can't add menus to it."))
214 (or menu
215 (let ((rest menu-path)
216 (so-far menubar))
217 (while rest
218 ;;; (setq menu (car (find-menu-item (cdr so-far) (list (car rest)))))
219 (setq menu
220 (if (eq so-far menubar)
221 (car (find-menu-item so-far (list (car rest))))
222 (car (find-menu-item (cdr so-far) (list (car rest))))))
223 (or menu
224 (let ((rest2 so-far))
225 (while (and (cdr rest2) (car (cdr rest2)))
226 (setq rest2 (cdr rest2)))
227 (setcdr rest2
228 (nconc (list (setq menu (list (car rest))))
229 (cdr rest2)))))
230 (setq so-far menu)
231 (setq rest (cdr rest)))))
232 (if (and item-found (car item-found))
233 ;; hack the item in place.
234 (if menu
235 (nsubstitute new-item (car item-found) menu)
236 (setq current-menubar (nsubstitute new-item
237 (car item-found)
238 current-menubar)))
239 ;; OK, we have to add the whole thing...
240 ;; if BEFORE is specified, try to add it there.
241 (or menu (setq menu current-menubar))
242 (if before
243 (setq before (car (find-menu-item menu (list before)))))
244 (let ((rest menu)
245 (added-before nil))
246 (while rest
247 (if (eq before (car (cdr rest)))
248 (progn
249 (setcdr rest (cons new-item (cdr rest)))
250 (setq rest nil added-before t))
251 (setq rest (cdr rest))))
252 (if (not added-before)
253 ;; adding before the first item on the menubar itself is harder
254 (if (and (eq menu menubar) (eq before (car menu)))
255 (setq menu (cons new-item menu)
256 current-menubar menu)
257 ;; otherwise, add the item to the end.
258 (nconc menu (list new-item))))))
259 (set-menubar-dirty-flag)
260 new-item))
261
262 (defun add-menu-button (menu-path menu-leaf &optional before)
263 "Add a menu item to some menu, creating the menu first if necessary.
264 If the named item exists already, it is changed.
265 MENU-PATH identifies the menu under which the new menu item should be inserted.
266 It is a list of strings; for example, (\"File\") names the top-level \"File\"
267 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
268 MENU-LEAF is a menubar leaf node. See the documentation of `current-menubar'.
269 BEFORE, if provided, is the name of a menu item before which this item should
270 be added, if this item is not on the menu already. If the item is already
271 present, it will not be moved."
272 (add-menu-item-1 t menu-path menu-leaf before))
273
274 ;; I actually liked the old name better, but the interface has changed too
275 ;; drastically to keep it. --Stig
276 (defun add-submenu (menu-path submenu &optional before)
277 "Add a menu to the menubar or one of its submenus.
278 If the named menu exists already, it is changed.
279 MENU-PATH identifies the menu under which the new menu should be inserted.
280 It is a list of strings; for example, (\"File\") names the top-level \"File\"
281 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
282 If MENU-PATH is nil, then the menu will be added to the menubar itself.
283 SUBMENU is the new menu to add.
284 See the documentation of `current-menubar' for the syntax.
285 BEFORE, if provided, is the name of a menu before which this menu should
286 be added, if this menu is not on its parent already. If the menu is already
287 present, it will not be moved."
288 (check-menu-syntax submenu nil)
289 (add-menu-item-1 nil menu-path submenu before))
290
291 (defun purecopy-menubar (x)
292 ;; this calls purecopy on the strings, and the contents of the vectors,
293 ;; but not on the vectors themselves, or the conses - those must be
294 ;; writable.
295 (cond ((vectorp x)
296 (let ((i (length x)))
297 (while (> i 0)
298 (aset x (1- i) (purecopy (aref x (1- i))))
299 (setq i (1- i))))
300 x)
301 ((consp x)
302 (let ((rest x))
303 (while rest
304 (setcar rest (purecopy-menubar (car rest)))
305 (setq rest (cdr rest))))
306 x)
307 (t
308 (purecopy x))))
309
310 (defun delete-menu-item (path)
311 "Remove the named menu item from the menu hierarchy.
312 PATH is a list of strings which identify the position of the menu item in
313 the menu hierarchy. The documentation of `add-submenu' describes menu-paths."
314 (let* ((pair (condition-case nil (find-menu-item current-menubar path)
315 (error nil)))
316 (item (car pair))
317 (parent (or (cdr pair) current-menubar)))
318 (if (not item)
319 nil
320 ;; the menubar is the only special case, because other menus begin
321 ;; with their name.
322 (if (eq parent current-menubar)
323 (setq current-menubar (delq item parent))
324 (delq item parent))
325 (set-menubar-dirty-flag)
326 item)))
327
328 (defun relabel-menu-item (path new-name)
329 "Change the string of the specified menu item.
330 PATH is a list of strings which identify the position of the menu item in
331 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
332 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
333 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\".
334 NEW-NAME is the string that the menu item will be printed as from now on."
335 (or (stringp new-name)
336 (setq new-name (signal 'wrong-type-argument (list 'stringp new-name))))
337 (let* ((menubar current-menubar)
338 (pair (find-menu-item menubar path))
339 (item (car pair))
340 (menu (cdr pair)))
341 (or item
342 (signal 'error (list (if menu (gettext "No such menu item")
343 (gettext "No such menu"))
344 path)))
345 (if (and (consp item)
346 (stringp (car item)))
347 (setcar item new-name)
348 (aset item 0 new-name))
349 (set-menubar-dirty-flag)
350 item))
351
352 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
353 ;;
354 ;; these are all bad style. Why in the world would we put evaluable forms
355 ;; into the menubar if we didn't want people to use 'em?
356 ;; x-font-menu.el is the only known offender right now and that ought to be
357 ;; rehashed a bit.
358 ;;
359
360 (defun enable-menu-item-1 (path toggle-p on-p)
361 (let (menu item)
362 (if (and (vectorp path) (> (length path) 2)) ; limited syntax checking...
363 (setq item path)
364 (let* ((menubar current-menubar)
365 (pair (find-menu-item menubar path)))
366 (setq item (car pair)
367 menu (cdr pair))
368 (or item
369 (signal 'error (list (if menu
370 "No such menu item"
371 "No such menu")
372 path)))
373 (if (consp item)
374 (error "%S is a menu, not a menu item" path))))
375 (if (or (> (length item) 4)
376 (and (symbolp (aref item 2))
377 (= ?: (aref (symbol-name (aref item 2)) 0))))
378 ;; plist-like syntax
379 (let ((i 2)
380 (keyword (if toggle-p :selected :active))
381 (ok nil))
382 (while (< i (length item))
383 (cond ((eq (aref item i) keyword)
384 (aset item (1+ i) on-p)
385 (setq ok t)))
386 (setq i (+ i 2)))
387 (cond (ok nil)
388 (toggle-p
389 (signal 'error (list "not a toggle menu item" item)))
390 (t
391 ;; Need to copy the item to extend it, sigh...
392 (let ((cons (memq item menu))
393 (new-item (vconcat item (list keyword on-p))))
394 (if cons
395 (setcar cons (setq item new-item))
396 (if menu
397 (error "couldn't find %S on its parent?" item)
398 (error "no %S slot to set: %S" keyword item)))))))
399 ;; positional syntax
400 (if toggle-p
401 (signal 'error (list "not a toggle menu item" item))
402 (aset item 2 on-p)))
403 (set-menubar-dirty-flag)
404 item))
405
406 (defun enable-menu-item (path)
407 "Make the named menu item be selectable.
408 PATH is a list of strings which identify the position of the menu item in
409 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
410 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
411 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
412 (enable-menu-item-1 path nil t))
413
414 (defun disable-menu-item (path)
415 "Make the named menu item be unselectable.
416 PATH is a list of strings which identify the position of the menu item in
417 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
418 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
419 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
420 (enable-menu-item-1 path nil nil))
421
422 (defun select-toggle-menu-item (path)
423 "Make the named toggle- or radio-style menu item be in the `selected' state.
424 PATH is a list of strings which identify the position of the menu item in
425 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
426 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
427 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
428 (enable-menu-item-1 path t t))
429
430 (defun deselect-toggle-menu-item (path)
431 "Make the named toggle- or radio-style menu item be in the `unselected' state.
432 PATH is a list of strings which identify the position of the menu item in
433 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
434 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
435 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
436 (enable-menu-item-1 path t nil))
437
438
439 (defun get-popup-menu-response (menu-desc &optional event)
440 "Pop up the given menu and wait for a response.
441 This blocks until the response is received, and returns the misc-user
442 event that encapsulates the response. To execute it, you can do
443 (funcall (event-function response) (event-object response))
444 If no response was received, nil is returned.
445
446 MENU-DESC and EVENT are as in the call to `popup-menu'."
447 ;; partially stolen from w3
448 (let ((echo-keystrokes 0)
449 new-event)
450 (popup-menu menu-desc event)
451 (catch 'popup-done
452 (while t
453 (setq new-event (next-command-event new-event))
454 (cond ((misc-user-event-p new-event)
455 (throw 'popup-done new-event))
456 ((not (popup-up-p))
457 (setq unread-command-events (cons new-event
458 unread-command-events))
459 (throw 'popup-done nil))
460 ((button-release-event-p new-event);; don't beep twice
461 nil)
462 ((event-matches-key-specifier-p (quit-char))
463 (signal 'quit nil))
464 (t
465 (beep)
466 (message "please make a choice from the menu.")))))))
467
468 (defun popup-menu-and-execute-in-window (menu-desc event)
469 "Pop up the given menu and execute its response in EVENT's window.
470 This blocks until the response is received, temporarily selects
471 EVENT's window, and executes the command specified in the response.
472 EVENT can also be a window. See `popup-menu' for the semantics of
473 MENU-DESC."
474 (let ((response
475 (get-popup-menu-response menu-desc
476 (and (eventp event) event))))
477 (and (misc-user-event-p response)
478 (save-selected-window
479 (select-window (if (windowp event) event
480 (event-window event)))
481 (funcall (event-function response)
482 (event-object response))))))
483
484
485 (provide 'menubar)