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