annotate lisp/prim/dialog.el @ 183:e121b013d1f0 r20-3b18

Import from CVS: tag r20-3b18
author cvs
date Mon, 13 Aug 2007 09:54:23 +0200
parents 131b0175ea99
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;; Dialog-box support.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 ;; Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 ;; This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;; XEmacs is distributed in the hope that it will be useful, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;; You should have received a copy of the GNU General Public License
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 0
diff changeset
17 ;; along with XEmacs; see the file COPYING. If not, write to the
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 16
diff changeset
18 ;; Free Software Foundation, 59 Temple Place - Suite 330,
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 0
diff changeset
19 ;; Boston, MA 02111-1307, USA.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ;;; Synched up with: Not in FSF.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 (defun yes-or-no-p-dialog-box (prompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 "Ask user a \"y or n\" question with a popup dialog box.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 Returns t if answer is \"yes\".
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 Takes one argument, which is the string to display to ask the question."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 (let ((echo-keystrokes 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 (popup-dialog-box
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;; "Non-violent language please!" says Robin.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 (cons prompt '(["Yes" yes t] ["No" no t] nil ["Cancel" abort t])))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ; (cons prompt '(["Yes" yes t] ["No" no t] nil ["Abort" abort t])))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 (catch 'ynp-done
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 (while t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 (setq event (next-command-event event))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 (cond ((and (misc-user-event-p event) (eq (event-object event) 'yes))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 (throw 'ynp-done t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ((and (misc-user-event-p event) (eq (event-object event) 'no))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 (throw 'ynp-done nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ((and (misc-user-event-p event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 (or (eq (event-object event) 'abort)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 (eq (event-object event) 'menu-no-selection-hook)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 (signal 'quit nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ((button-release-event-p event) ;; don't beep twice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 (beep)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 (message "please answer the dialog box")))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 (defun yes-or-no-p-maybe-dialog-box (prompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 "Ask user a yes-or-no question. Return t if answer is yes.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 The question is asked with a dialog box or the minibuffer, as appropriate.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 Takes one argument, which is the string to display to ask the question.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 It should end in a space; `yes-or-no-p' adds `(yes or no) ' to it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 The user must confirm the answer with RET,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 and can edit it until it as been confirmed."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 (if (should-use-dialog-box-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 (yes-or-no-p-dialog-box prompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 (yes-or-no-p-minibuf prompt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 (defun y-or-n-p-maybe-dialog-box (prompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 "Ask user a \"y or n\" question. Return t if answer is \"y\".
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 Takes one argument, which is the string to display to ask the question.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 The question is asked with a dialog box or the minibuffer, as appropriate.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 It should end in a space; `y-or-n-p' adds `(y or n) ' to it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 No confirmation of the answer is requested; a single character is enough.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 Also accepts Space to mean yes, or Delete to mean no."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (if (should-use-dialog-box-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (yes-or-no-p-dialog-box prompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 (y-or-n-p-minibuf prompt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (if (fboundp 'popup-dialog-box)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (fset 'yes-or-no-p 'yes-or-no-p-maybe-dialog-box)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 (fset 'y-or-n-p 'y-or-n-p-maybe-dialog-box)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 ;; this is call-compatible with the horribly-named FSF Emacs function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 ;; `x-popup-dialog'. I refuse to use that name.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (defun get-dialog-box-response (position contents)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 ;; by Stig@hackvan.com
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 ;; modified by pez@atlantic2.sbi.com
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 "Pop up a dialog box and return user's selection.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 POSITION specifies which frame to use.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 This is normally an event or a window or frame.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 If POSITION is t or nil, it means to use the frame the mouse is on.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 The dialog box appears in the middle of the specified frame.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 CONTENTS specifies the alternatives to display in the dialog box.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 It is a list of the form (TITLE ITEM1 ITEM2...).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 Each ITEM is a cons cell (STRING . VALUE).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 The return value is VALUE from the chosen item.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 An ITEM may also be just a string--that makes a nonselectable item.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 An ITEM may also be nil--that means to put all preceding items
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 on the left of the dialog box and all following items on the right."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 ((eventp position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (select-frame (event-frame position)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 ((framep position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (select-frame position))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 ((windowp position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (select-window position)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (let ((dbox (cons (car contents)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (mapcar #'(lambda (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 ((null x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 ((stringp x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 `[,x 'ignore nil]) ;this will never get
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 ;selected
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 `[,(car x) (throw 'result ',(cdr x)) t])))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (cdr contents))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 )))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (catch 'result
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (popup-dialog-box dbox)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (dispatch-event (next-command-event)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (defun message-box (fmt &rest args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 "Display a message, in a dialog box if possible.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 If the selected device has no dialog-box support, use the echo area.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 The arguments are the same as to `format'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 If the only argument is nil, clear any existing message; let the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 minibuffer contents show."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (if (and (null fmt) (null args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 (clear-message nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (let ((str (apply 'format fmt args)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (if (device-on-window-system-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (get-dialog-box-response nil (list str (cons "OK" t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 (display-message 'message str))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 str)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (defun message-or-box (fmt &rest args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 "Display a message in a dialog box or in the echo area.\n\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 If this command was invoked with the mouse, use a dialog box.\n\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 Otherwise, use the echo area.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 The arguments are the same as to `format'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 If the only argument is nil, clear any existing message; let the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 minibuffer contents show."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (if (should-use-dialog-box-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (apply 'message-box fmt args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (apply 'message fmt args)))