462
|
1 ;;; dialog-gtk.el --- Dialog-box support for XEmacs w/GTK primitives
|
|
2
|
|
3 ;; Copyright (C) 2000 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Maintainer: William M. Perry <wmperry@gnu.org>
|
|
6 ;; Keywords: extensions, internal, dumped
|
|
7
|
|
8 ;; This file is part of XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;; any later version.
|
|
14
|
|
15 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
|
21 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
22 ;; Free Software Foundation, 59 Temple Place - Suite 330,
|
|
23 ;; Boston, MA 02111-1307, USA.
|
|
24
|
|
25 ;;; Synched up with: Not in FSF.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; This file is dumped with XEmacs (when dialog boxes are compiled in).
|
|
30
|
|
31 (require 'cl)
|
|
32 (require 'gtk-password-dialog)
|
|
33 (require 'gtk-file-dialog)
|
|
34
|
502
|
35 (globally-declare-fboundp
|
|
36 '(gtk-signal-connect
|
|
37 gtk-main-quit gtk-window-set-transient-for
|
|
38 gtk-widget-show-all gtk-main gtk-color-selection-dialog-new
|
|
39 gtk-color-selection-dialog-ok-button gtk-widget-hide-all
|
|
40 gtk-color-selection-get-color
|
|
41 gtk-color-selection-dialog-colorsel
|
|
42 gtk-color-selection-dialog-cancel-button gtk-widget-show-now
|
|
43 gtk-widget-grab-focus gtk-widget-destroy gtk-dialog-new
|
|
44 gtk-window-set-title gtk-container-set-border-width
|
|
45 gtk-box-set-spacing gtk-dialog-vbox gtk-container-add
|
|
46 gtk-label-new gtk-button-new-with-label
|
2081
|
47 gtk-widget-set-sensitive gtk-widget-show gtk-dialog-action-area
|
|
48 gtk-label-parse-uline gtk-widget-add-accelerator gtk-accel-group-new))
|
|
49
|
|
50 (defun gtk-popup-convert-underscores (str)
|
|
51 ;; Convert the XEmacs button accelerator representation to Gtk mnemonic
|
|
52 ;; form. If no accelerator has been provided, put one at the start of the
|
|
53 ;; string (this mirrors the behaviour under X). This algorithm is also found
|
|
54 ;; in menubar-gtk.c:convert_underscores().
|
|
55 (let ((new-str (string))
|
|
56 (i 0)
|
|
57 (found-accel nil))
|
|
58 (while (< i (length str))
|
|
59 (let ((c (aref str i)))
|
|
60 (cond ((eq c ?%)
|
|
61 (setq i (1+ i))
|
|
62 (if (and (not (eq (aref str i) ?_)) (not (eq (aref str i) ?%)))
|
|
63 (setq i (1- i)))
|
|
64 (setq found-accel 1)
|
|
65 )
|
|
66 ((eq c ?_)
|
|
67 (setq new-str (concat new-str "_")))
|
|
68 ))
|
|
69 (setq new-str (concat new-str (string (aref str i))))
|
|
70 (setq i (1+ i))
|
|
71 )
|
|
72 (if found-accel new-str (concat "_" new-str))
|
|
73 ))
|
502
|
74
|
462
|
75 (defun popup-builtin-open-dialog (keys)
|
|
76 ;; Allowed keywords are:
|
|
77 ;;
|
|
78 ;; :initial-filename fname
|
|
79 ;; :initial-directory dir
|
|
80 ;; :filter-list (filter-desc filter ...)
|
|
81 ;; :directory t/nil
|
|
82 ;; :title string
|
|
83 ;; :allow-multi-select t/nil
|
|
84 ;; :create-prompt-on-nonexistent t/nil
|
|
85 ;; :overwrite-prompt t/nil
|
|
86 ;; :file-must-exist t/nil
|
|
87 ;; :no-network-button t/nil
|
|
88 ;; :no-read-only-return t/nil
|
|
89 (let ((initial-filename (plist-get keys :initial-filename))
|
|
90 (clicked-ok nil)
|
622
|
91 (widget nil)
|
|
92 filename)
|
462
|
93 (setq widget (gtk-file-dialog-new
|
|
94 :directory (plist-get keys :directory)
|
|
95 :callback `(lambda (f)
|
|
96 (setq clicked-ok t
|
|
97 filename f))
|
|
98 :initial-directory (or (plist-get keys :initial-directory nil)
|
|
99 (if initial-filename
|
|
100 (file-name-directory initial-filename)
|
|
101 default-directory))
|
|
102 :filter-list (plist-to-alist
|
|
103 (plist-get keys :filter-list nil))
|
|
104 :file-must-exist (plist-get keys :file-must-exist nil)))
|
|
105
|
|
106 (gtk-signal-connect widget 'destroy (lambda (obj data) (gtk-main-quit)))
|
|
107
|
|
108 (gtk-window-set-transient-for widget (frame-property nil 'shell-widget))
|
|
109 (gtk-widget-show-all widget)
|
|
110 (gtk-main)
|
|
111 (if (not clicked-ok)
|
608
|
112 (signal 'quit nil)
|
|
113 filename)))
|
462
|
114
|
|
115 (defalias 'popup-builtin-save-as-dialog 'popup-builtin-open-dialog)
|
|
116
|
|
117 (defun popup-builtin-color-dialog (keys)
|
|
118 ;; Allowed keys:
|
|
119 ;; :initial-color COLOR
|
502
|
120 (let (;(initial-color (or (plist-get keys :initial-color) "white"))
|
462
|
121 (title (or (plist-get keys :title "Select color...")))
|
|
122 (dialog nil)
|
|
123 (clicked-ok nil)
|
|
124 (color nil))
|
|
125 (setq dialog (gtk-color-selection-dialog-new title))
|
|
126 (gtk-signal-connect
|
|
127 (gtk-color-selection-dialog-ok-button dialog) 'clicked
|
|
128 (lambda (button colorsel)
|
|
129 (gtk-widget-hide-all dialog)
|
|
130 (setq color (gtk-color-selection-get-color colorsel)
|
|
131 clicked-ok t)
|
|
132 (gtk-main-quit))
|
|
133 (gtk-color-selection-dialog-colorsel dialog))
|
|
134
|
|
135 (gtk-signal-connect
|
|
136 (gtk-color-selection-dialog-cancel-button dialog) 'clicked
|
|
137 (lambda (&rest ignored)
|
|
138 (gtk-main-quit)))
|
|
139
|
|
140 (put dialog 'modal t)
|
|
141 (put dialog 'type 'dialog)
|
|
142 (gtk-window-set-transient-for dialog (frame-property nil 'shell-widget))
|
|
143
|
|
144 (unwind-protect
|
|
145 (progn
|
|
146 (gtk-widget-show-now dialog)
|
|
147 (gtk-main))
|
|
148 '(gtk-widget-destroy dialog))
|
|
149 (if (not clicked-ok)
|
|
150 (signal 'quit nil))
|
|
151 ;; Need to convert from (R G B A) to #rrggbb
|
|
152 (format "#%02x%02x%02x"
|
|
153 (* 256 (nth 0 color))
|
|
154 (* 256 (nth 1 color))
|
|
155 (* 256 (nth 2 color)))))
|
|
156
|
|
157 (defun popup-builtin-password-dialog (keys)
|
|
158 ;; Format is (default callback :keyword value)
|
|
159 ;; Allowed keywords are:
|
|
160 ;;
|
|
161 ;; :title string
|
|
162 :; :prompt string
|
|
163 ;; :default string
|
|
164 ;; :verify boolean
|
|
165 ;; :verify-prompt string
|
|
166 (let* ((default (plist-get keys :default))
|
|
167 (dialog nil)
|
|
168 (clicked-ok nil)
|
|
169 (passwd nil)
|
|
170 (info nil)
|
|
171 (generic-cb (lambda (x)
|
|
172 (setq clicked-ok t
|
|
173 passwd x))))
|
|
174
|
|
175 ;; Convert the descriptor to keywords and create the dialog
|
|
176 (setq info (copy-list keys)
|
|
177 info (plist-put info :callback generic-cb)
|
|
178 info (plist-put info :default default)
|
|
179 dialog (apply 'gtk-password-dialog-new info))
|
|
180
|
|
181 ;; Clicking any button or closing the box exits the main loop.
|
|
182 (gtk-signal-connect (gtk-password-dialog-ok-button dialog)
|
|
183 'clicked
|
|
184 (lambda (&rest ignored)
|
|
185 (gtk-main-quit)))
|
|
186
|
|
187 (gtk-signal-connect (gtk-password-dialog-cancel-button dialog)
|
|
188 'clicked
|
|
189 (lambda (&rest ignored)
|
|
190 (gtk-main-quit)))
|
|
191
|
|
192 (gtk-signal-connect dialog
|
|
193 'delete-event
|
|
194 (lambda (&rest ignored)
|
|
195 (gtk-main-quit)))
|
|
196
|
|
197 (gtk-widget-grab-focus (gtk-password-dialog-entry-widget dialog))
|
|
198
|
|
199 ;; Make us modal...
|
|
200 (put dialog 'modal t)
|
|
201 (gtk-window-set-transient-for dialog (frame-property nil 'shell-widget))
|
|
202
|
|
203 ;; Realize the damn thing & wait for some action...
|
|
204 (gtk-widget-show-all dialog)
|
|
205 (gtk-main)
|
|
206
|
|
207 (if (not clicked-ok)
|
|
208 (signal 'quit nil))
|
|
209
|
|
210 (gtk-widget-destroy dialog)
|
|
211 passwd))
|
|
212
|
|
213 (defun popup-builtin-question-dialog (keys)
|
|
214 ;; Allowed keywords:
|
|
215 ;; :question STRING
|
|
216 ;; :buttons BUTTONDESC
|
|
217 (let ((title (or (plist-get keys :title) "Question"))
|
|
218 (buttons-descr (plist-get keys :buttons))
|
|
219 (question (or (plist-get keys :question) "Question goes here..."))
|
|
220 (dialog nil) ; GtkDialog
|
|
221 (buttons nil) ; List of GtkButton objects
|
|
222 (activep t)
|
707
|
223 (callback nil)
|
462
|
224 (flushrightp nil)
|
707
|
225 (length nil)
|
2081
|
226 (label nil)
|
|
227 (gui-button nil)
|
|
228 (accel-group (gtk-accel-group-new))
|
|
229 (accel-key nil)
|
462
|
230 (errp t))
|
|
231 (if (not buttons-descr)
|
|
232 (error 'syntax-error
|
|
233 "Dialog descriptor must supply at least one button"))
|
|
234
|
|
235 ;; Do the basics - create the dialog, set the window title, and
|
|
236 ;; add the label asking the question.
|
|
237 (unwind-protect
|
|
238 (progn
|
|
239 (setq dialog (gtk-dialog-new))
|
|
240 (gtk-window-set-title dialog title)
|
|
241 (gtk-container-set-border-width dialog 3)
|
|
242 (gtk-box-set-spacing (gtk-dialog-vbox dialog) 5)
|
|
243 (gtk-container-add (gtk-dialog-vbox dialog) (gtk-label-new question))
|
|
244
|
|
245 ;; Create the buttons.
|
|
246 (mapc (lambda (button)
|
|
247 ;; Handle flushright buttons
|
|
248 (if (null button)
|
|
249 (setq flushrightp t)
|
|
250
|
|
251 ;; More sanity checking first of all.
|
|
252 (if (not (vectorp button))
|
|
253 (error "Button descriptor is not a vector: %S" button))
|
|
254
|
707
|
255 (setq length (length button))
|
|
256
|
|
257 (cond
|
|
258 ((= length 1) ; [ "name" ]
|
|
259 (setq callback nil
|
|
260 activep nil))
|
|
261 ((= length 2) ; [ "name" callback ]
|
|
262 (setq callback (aref button 1)
|
|
263 activep t))
|
|
264 ((and (or (= length 3) (= length 4))
|
|
265 (not (keywordp (aref button 2))))
|
|
266 ;; [ "name" callback active-p ] or
|
|
267 ;; [ "name" callback active-p suffix ]
|
|
268 ;; We ignore the 'suffix' entry, because that is
|
|
269 ;; what the X code does.
|
|
270 (setq callback (aref button 1)
|
|
271 activep (aref button 2)))
|
|
272 (t ; 100% keyword specification
|
|
273 (let ((plist (cdr (mapcar 'identity button))))
|
|
274 (setq activep (plist-get plist :active)
|
|
275 callback (plist-get plist :callback)))))
|
462
|
276
|
2081
|
277 ;; Create the label and determine what the mnemonic key is.
|
|
278 (setq label (gtk-label-new ""))
|
|
279 (setq accel-key (gtk-label-parse-uline label
|
|
280 (gtk-popup-convert-underscores (aref button 0))))
|
|
281 ;; Place the label in the button.
|
|
282 (gtk-misc-set-alignment label 0.5 0.5)
|
|
283 (setq gui-button (gtk-button-new))
|
|
284 (gtk-container-add gui-button label)
|
|
285 ;; Add ALT-mnemonic to the dialog's accelerator group.
|
|
286 (gtk-widget-add-accelerator gui-button "clicked" accel-group
|
|
287 accel-key
|
|
288 8 ; GDK_MOD1_MASK
|
|
289 4 ; GTK_ACCEL_LOCKED
|
|
290 )
|
|
291
|
|
292 (push gui-button buttons)
|
462
|
293 (gtk-widget-set-sensitive (car buttons) (eval activep))
|
|
294
|
|
295 ;; Apply the callback
|
|
296 (gtk-signal-connect
|
|
297 (car buttons) 'clicked
|
|
298 (lambda (button data)
|
|
299 (push (make-event 'misc-user
|
|
300 (list 'object (car data)
|
|
301 'function
|
|
302 (if (symbolp (car data))
|
|
303 'call-interactively
|
|
304 'eval)))
|
|
305 unread-command-events)
|
|
306 (gtk-main-quit)
|
|
307 t)
|
707
|
308 (cons callback dialog))
|
462
|
309
|
|
310 (gtk-widget-show (car buttons))
|
|
311 (funcall (if flushrightp 'gtk-box-pack-end 'gtk-box-pack-start)
|
|
312 (gtk-dialog-action-area dialog) (car buttons)
|
|
313 nil t 2)))
|
|
314 buttons-descr)
|
|
315
|
|
316 ;; Make sure they can't close it with the window manager
|
|
317 (gtk-signal-connect dialog 'delete-event (lambda (&rest ignored) t))
|
|
318 (gtk-window-set-transient-for dialog (frame-property nil 'shell-widget))
|
|
319 (put dialog 'type 'dialog)
|
|
320 (put dialog 'modal t)
|
2081
|
321 ;; Make the dialog listen for global mnemonic keys/
|
|
322 (gtk-window-add-accel-group dialog accel-group)
|
|
323
|
462
|
324 (gtk-widget-show-all dialog)
|
|
325 (gtk-main)
|
|
326 (gtk-widget-destroy dialog)
|
|
327 (setq errp nil))
|
|
328 (if (not errp)
|
|
329 ;; Nothing, we successfully showed the dialog
|
|
330 nil
|
|
331 ;; We need to destroy all the widgets, just in case.
|
|
332 (mapc 'gtk-widget-destroy buttons)
|
|
333 (gtk-widget-destroy dialog)))))
|
|
334
|
|
335 (defun gtk-make-dialog-box-internal (type keys)
|
|
336 (case type
|
|
337 (file
|
|
338 (popup-builtin-open-dialog keys))
|
|
339 (password
|
|
340 (popup-builtin-password-dialog keys))
|
|
341 (question
|
|
342 (popup-builtin-question-dialog keys))
|
|
343 (color
|
|
344 (popup-builtin-color-dialog keys))
|
|
345 (find
|
|
346 )
|
|
347 (font
|
|
348 )
|
|
349 (replace
|
|
350 )
|
|
351 (mswindows-message
|
|
352 ;; This should really be renamed!
|
|
353 )
|
|
354 (print
|
|
355 )
|
|
356 (page-setup
|
|
357 )
|
|
358 (print-setup
|
|
359 )
|
|
360 (default
|
|
361 (error "Unknown type of dialog: %S" type))))
|
|
362
|
|
363 (provide 'dialog-gtk)
|