annotate lisp/prim/dialog.el @ 12:bcdc7deadc19 r19-15b7

Import from CVS: tag r19-15b7
author cvs
date Mon, 13 Aug 2007 08:48:16 +0200
parents 376386a54a3c
children 0293115a14e9
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; along with XEmacs; see the file COPYING. If not, write to the Free
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;;; Synched up with: Not in FSF.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 (defun yes-or-no-p-dialog-box (prompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 "Ask user a \"y or n\" question with a popup dialog box.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 Returns t if answer is \"yes\".
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 Takes one argument, which is the string to display to ask the question."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 (let ((echo-keystrokes 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 (popup-dialog-box
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;; "Non-violent language please!" says Robin.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 (cons prompt '(["Yes" yes t] ["No" no t] nil ["Cancel" abort t])))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ; (cons prompt '(["Yes" yes t] ["No" no t] nil ["Abort" abort t])))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 (catch 'ynp-done
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 (while t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 (setq event (next-command-event event))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 (cond ((and (misc-user-event-p event) (eq (event-object event) 'yes))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 (throw 'ynp-done t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ((and (misc-user-event-p event) (eq (event-object event) 'no))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 (throw 'ynp-done nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ((and (misc-user-event-p event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 (or (eq (event-object event) 'abort)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 (eq (event-object event) 'menu-no-selection-hook)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 (signal 'quit nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 ((button-release-event-p event) ;; don't beep twice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 (beep)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 (message "please answer the dialog box")))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (defun yes-or-no-p-maybe-dialog-box (prompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 "Ask user a yes-or-no question. Return t if answer is yes.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 The question is asked with a dialog box or the minibuffer, as appropriate.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 Takes one argument, which is the string to display to ask the question.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 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
54 The user must confirm the answer with RET,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 and can edit it until it as been confirmed."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 (if (should-use-dialog-box-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 (yes-or-no-p-dialog-box prompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 (yes-or-no-p-minibuf prompt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 (defun y-or-n-p-maybe-dialog-box (prompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 "Ask user a \"y or n\" question. Return t if answer is \"y\".
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 Takes one argument, which is the string to display to ask the question.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 The question is asked with a dialog box or the minibuffer, as appropriate.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 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
65 No confirmation of the answer is requested; a single character is enough.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 Also accepts Space to mean yes, or Delete to mean no."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 (if (should-use-dialog-box-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (yes-or-no-p-dialog-box prompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (y-or-n-p-minibuf prompt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (if (fboundp 'popup-dialog-box)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (fset 'yes-or-no-p 'yes-or-no-p-maybe-dialog-box)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (fset 'y-or-n-p 'y-or-n-p-maybe-dialog-box)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 ;; this is call-compatible with the horribly-named FSF Emacs function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 ;; `x-popup-dialog'. I refuse to use that name.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 (defun get-dialog-box-response (position contents)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 ;; by Stig@hackvan.com
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 ;; modified by pez@atlantic2.sbi.com
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 "Pop up a dialog box and return user's selection.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 POSITION specifies which frame to use.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 This is normally an event or a window or frame.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 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
85 The dialog box appears in the middle of the specified frame.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 CONTENTS specifies the alternatives to display in the dialog box.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 It is a list of the form (TITLE ITEM1 ITEM2...).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 Each ITEM is a cons cell (STRING . VALUE).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 The return value is VALUE from the chosen item.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 An ITEM may also be just a string--that makes a nonselectable item.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 An ITEM may also be nil--that means to put all preceding items
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 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
95 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 ((eventp position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (select-frame (event-frame position)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 ((framep position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (select-frame position))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 ((windowp position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (select-window position)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (let ((dbox (cons (car contents)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (mapcar #'(lambda (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 ((null x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 ((stringp x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 `[,x 'ignore nil]) ;this will never get
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 ;selected
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 `[,(car x) (throw 'result ',(cdr x)) t])))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (cdr contents))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 )))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (catch 'result
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (popup-dialog-box dbox)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (dispatch-event (next-command-event)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (defun message-box (fmt &rest args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 "Display a message, in a dialog box if possible.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 If the selected device has no dialog-box support, use the echo area.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 The arguments are the same as to `format'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 If the only argument is nil, clear any existing message; let the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 minibuffer contents show."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 (if (and (null fmt) (null args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (clear-message nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (let ((str (apply 'format fmt args)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (if (device-on-window-system-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (get-dialog-box-response nil (list str (cons "OK" t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (display-message 'message str))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 str)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (defun message-or-box (fmt &rest args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 "Display a message in a dialog box or in the echo area.\n\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 If this command was invoked with the mouse, use a dialog box.\n\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 Otherwise, use the echo area.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 The arguments are the same as to `format'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 If the only argument is nil, clear any existing message; let the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 minibuffer contents show."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (if (should-use-dialog-box-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (apply 'message-box fmt args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (apply 'message fmt args)))