Mercurial > hg > xemacs-beta
annotate lisp/gtk-file-dialog.el @ 5594:cc8ea7ed4286
Mention when compiled functions are built-in, help.el.
2011-11-09 Aidan Kehoe <kehoea@parhasard.net>
* help.el (describe-function-1):
When printing compiled functions, mention whether they're
built-in.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Wed, 09 Nov 2011 12:51:10 +0000 |
parents | 308d34e9f07d |
children | bbe4146603db |
rev | line source |
---|---|
462 | 1 ;;; gtk-file-dialog.el --- A nicer file selection dialog 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 | |
7 | |
8 ;; This file is part of XEmacs. | |
9 | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
502
diff
changeset
|
10 ;; XEmacs is free software: you can redistribute it and/or modify it |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
502
diff
changeset
|
11 ;; under the terms of the GNU General Public License as published by the |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
502
diff
changeset
|
12 ;; Free Software Foundation, either version 3 of the License, or (at your |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
502
diff
changeset
|
13 ;; option) any later version. |
462 | 14 |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
502
diff
changeset
|
15 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
502
diff
changeset
|
16 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
502
diff
changeset
|
17 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
502
diff
changeset
|
18 ;; for more details. |
462 | 19 |
20 ;; You should have received a copy of the GNU General Public License | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
502
diff
changeset
|
21 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>. |
462 | 22 |
23 ;;; Synched up with: Not in FSF. | |
24 | |
25 ;;; Commentary: | |
26 | |
27 ;; The default GTK file selection dialog is not sufficient for our | |
28 ;; needs. Limitations include: | |
29 ;; | |
30 ;; - not derived from GtkDialog | |
31 ;; - no support for filters based on file types | |
32 ;; - no support for setting an initial directory | |
33 ;; - no way to tell it 'file must exist' | |
34 ;; - no easy way to tell it to look at directories only | |
35 ;; - ugly as sin | |
36 ;; | |
37 ;; This attempts to rectify the situation. | |
38 | |
502 | 39 (globally-declare-fboundp |
40 '(gtk-clist-clear | |
41 gtk-clist-freeze gtk-clist-append | |
42 gtk-clist-thaw gtk-combo-set-popdown-strings gtk-dialog-new | |
43 gtk-dialog-vbox gtk-dialog-action-area gtk-window-set-title | |
44 gtk-button-new-with-label gtk-container-add gtk-signal-connect | |
45 gtk-entry-get-text gtk-widget-destroy gtk-combo-new | |
46 gtk-combo-disable-activate gtk-box-pack-start gtk-combo-entry | |
47 gtk-hbox-new gtk-clist-new-with-titles gtk-scrolled-window-new | |
48 gtk-widget-set-usize gtk-clist-get-text gtk-entry-set-text | |
49 gtk-button-clicked gtk-option-menu-new gtk-menu-new | |
50 gtk-label-new gtk-menu-item-new-with-label gtk-menu-append | |
51 gtk-widget-show gtk-option-menu-set-menu gtk-box-pack-end | |
52 gtk-entry-new gtk-widget-set-sensitive gtk-widget-realize)) | |
53 | |
462 | 54 (defun gtk-file-dialog-fill-file-list (dialog dir) |
55 (if (not dir) | |
56 (setq dir (get dialog 'x-file-dialog-current-dir nil))) | |
57 | |
58 (put dialog 'x-file-dialog-current-dir dir) | |
59 | |
60 (let ((list (get dialog 'x-file-dialog-files-list nil)) | |
502 | 61 ;(remotep (file-remote-p dir)) |
62 ) | |
462 | 63 (if (not list) |
64 nil | |
65 (gtk-clist-clear list) | |
66 (gtk-clist-freeze list) | |
67 ;; NOTE: Current versions of efs / ange-ftp do not honor the | |
68 ;; files-only flag to directory-files, but actually DOING these | |
69 ;; checks is hideously expensive. Leave it turned off for now. | |
502 | 70 (mapc #'(lambda (f) |
71 (if (or t ; Lets just wait for EFS to | |
72 ;(not remotep) ; fix itself, shall we? | |
73 ;(not (file-directory-p (expand-file-name f dir))) | |
74 ) | |
75 (gtk-clist-append list (list f)))) | |
462 | 76 (directory-files dir nil |
77 (get dialog 'x-file-dialog-active-filter nil) | |
78 nil t)) | |
79 (gtk-clist-thaw list)))) | |
80 | |
81 (defun gtk-file-dialog-fill-directory-list (dialog dir) | |
82 (let ((subdirs (directory-files dir nil nil nil 5)) | |
502 | 83 ;(remotep (file-remote-p dir)) |
84 ;(selected-dir (get dialog 'x-file-dialog-current-dir "/")) | |
462 | 85 (directory-list (get dialog 'x-file-dialog-directory-list))) |
86 | |
87 (gtk-clist-freeze directory-list) | |
88 (gtk-clist-clear directory-list) | |
89 | |
90 (while subdirs | |
91 (if (equal "." (car subdirs)) | |
92 nil | |
93 ;; NOTE: Current versions of efs / ange-ftp do not honor the | |
94 ;; files-only flag to directory-files, but actually DOING these | |
95 ;; checks is hideously expensive. Leave it turned off for now. | |
96 (if (or t ; Lets just wait for EFS to | |
502 | 97 ;(not remotep) ; fix itself, shall we? |
98 ;(file-directory-p (expand-file-name (car subdirs) dir)) | |
99 ) | |
462 | 100 (gtk-clist-append directory-list (list (car subdirs))))) |
101 (pop subdirs)) | |
102 (gtk-clist-thaw directory-list))) | |
103 | |
104 (defun gtk-file-dialog-update-dropdown (dialog dir) | |
105 (let ((combo-box (get dialog 'x-file-dialog-select-list)) | |
106 (components (reverse | |
107 (delete "" | |
108 (split-string dir | |
109 (concat "[" (char-to-string directory-sep-char) "]"))))) | |
110 (entries nil)) | |
111 | |
112 (while components | |
113 (push (concat "/" (mapconcat 'identity (reverse components) | |
114 (char-to-string directory-sep-char))) | |
115 entries) | |
116 (pop components)) | |
117 (push (expand-file-name "." "~/") entries) | |
118 (gtk-combo-set-popdown-strings combo-box (nreverse entries)))) | |
119 | |
120 (defun gtk-file-dialog-select-directory (dialog dir) | |
121 (gtk-file-dialog-fill-directory-list dialog dir) | |
122 (gtk-file-dialog-fill-file-list dialog dir) | |
123 (gtk-file-dialog-update-dropdown dialog dir)) | |
124 | |
125 (defun gtk-file-dialog-new (&rest keywords) | |
126 "Create a XEmacs file selection dialog. | |
127 Optional keyword arguments allowed: | |
128 | |
129 :title The title of the dialog | |
130 :initial-directory Initial directory to show | |
131 :filter-list List of filter descriptions and filters | |
132 :file-must-exist Whether the file must exist or not | |
133 :directory Look for a directory instead | |
134 :callback Function to call with one arg, the selection | |
135 " | |
136 (let* ((dialog (gtk-dialog-new)) | |
137 (vbox (gtk-dialog-vbox dialog)) | |
138 (dir (plist-get keywords :initial-directory default-directory)) | |
139 (button-area (gtk-dialog-action-area dialog)) | |
502 | 140 ;(initializing-gtk-file-dialog t) |
462 | 141 (select-box nil) |
142 button hbox) | |
143 | |
144 (put dialog 'type 'dialog) | |
145 | |
146 (gtk-window-set-title dialog (plist-get keywords :title "Select a file...")) | |
147 | |
148 (setq button (gtk-button-new-with-label "OK")) | |
149 (gtk-container-add button-area button) | |
150 (gtk-signal-connect button 'clicked | |
151 (lambda (button dialog) | |
152 (funcall | |
153 (get dialog 'x-file-dialog-callback 'ignore) | |
154 (gtk-entry-get-text | |
155 (get dialog 'x-file-dialog-entry nil))) | |
156 (gtk-widget-destroy dialog)) | |
157 dialog) | |
158 (put dialog 'x-file-dialog-ok-button button) | |
159 | |
160 (setq button (gtk-button-new-with-label "Cancel")) | |
161 (gtk-container-add button-area button) | |
162 (gtk-signal-connect button 'clicked | |
163 (lambda (button dialog) | |
164 (gtk-widget-destroy dialog)) dialog) | |
165 | |
166 (put dialog 'x-file-dialog-cancel-button button) | |
167 (put dialog 'x-file-dialog-callback (plist-get keywords :callback 'ignore)) | |
168 (put dialog 'x-file-dialog-construct-args keywords) | |
169 (put dialog 'x-file-dialog-current-dir dir) | |
170 | |
171 ;; Dropdown list of directories... | |
172 (setq select-box (gtk-combo-new)) | |
173 (gtk-combo-disable-activate select-box) | |
174 (gtk-box-pack-start vbox select-box nil nil 5) | |
175 (put dialog 'x-file-dialog-select-list select-box) | |
176 | |
177 ;; Hitting return in the entry will change dirs... | |
178 (gtk-signal-connect (gtk-combo-entry select-box) 'activate | |
179 (lambda (entry dialog) | |
180 (gtk-file-dialog-select-directory dialog | |
181 (gtk-entry-get-text entry))) | |
182 dialog) | |
183 | |
184 ;; Start laying out horizontally... | |
185 (setq hbox (gtk-hbox-new nil 0)) | |
186 (gtk-box-pack-start vbox hbox t t 5) | |
187 | |
188 ;; Directory listing | |
189 (let ((directories (gtk-clist-new-with-titles 1 '("Directories"))) | |
502 | 190 (scrolled (gtk-scrolled-window-new nil nil))) |
462 | 191 (gtk-container-add scrolled directories) |
192 (gtk-widget-set-usize scrolled 200 300) | |
193 (gtk-box-pack-start hbox scrolled t t 0) | |
194 (put dialog 'x-file-dialog-directory-list directories) | |
195 (put dialog 'x-file-dialog-directory-scrolled scrolled) | |
196 | |
197 (gtk-signal-connect directories 'select-row | |
198 (lambda (list row column event dialog) | |
199 (let ((dir (expand-file-name | |
200 (gtk-clist-get-text | |
201 (get dialog 'x-file-dialog-directory-list) | |
202 row column) | |
203 (get dialog 'x-file-dialog-current-dir)))) | |
204 (if (and (misc-user-event-p event) | |
205 (event-function event)) | |
206 (gtk-file-dialog-select-directory dialog dir) | |
207 (gtk-entry-set-text | |
208 (get dialog 'x-file-dialog-entry) | |
209 dir)))) | |
210 dialog) | |
211 ) | |
212 | |
213 (if (plist-get keywords :directory nil) | |
214 ;; Directory listings only do not need the file or filters buttons. | |
215 nil | |
216 ;; File listing | |
217 (let ((list (gtk-clist-new-with-titles 1 '("Files"))) | |
218 (scrolled (gtk-scrolled-window-new nil nil))) | |
219 (gtk-container-add scrolled list) | |
220 (gtk-widget-set-usize scrolled 200 300) | |
221 (gtk-box-pack-start hbox scrolled t t 0) | |
222 | |
223 (gtk-signal-connect list 'select-row | |
224 (lambda (list row column event dialog) | |
225 (gtk-entry-set-text | |
226 (get dialog 'x-file-dialog-entry nil) | |
227 (expand-file-name | |
228 (gtk-clist-get-text list row column) | |
229 (get dialog 'x-file-dialog-current-dir nil))) | |
230 (if (and (misc-user-event-p event) | |
231 (event-function event)) | |
232 ;; Got a double or triple click event... | |
233 (gtk-button-clicked | |
234 (get dialog 'x-file-dialog-ok-button nil)))) | |
235 dialog) | |
236 | |
237 (put dialog 'x-file-dialog-files-list list)) | |
238 | |
239 ;; Filters | |
240 (if (not (plist-get keywords :filter-list nil)) | |
241 ;; Don't need to bother packing this | |
242 nil | |
243 (setq hbox (gtk-hbox-new nil 0)) | |
244 (gtk-box-pack-start vbox hbox nil nil 0) | |
245 | |
246 (let ((label nil) | |
247 (options (plist-get keywords :filter-list nil)) | |
248 (omenu nil) | |
249 (menu nil) | |
250 (item nil)) | |
251 (setq omenu (gtk-option-menu-new) | |
252 menu (gtk-menu-new) | |
253 label (gtk-label-new "Filter: ")) | |
254 | |
255 (put dialog 'x-file-dialog-active-filter (cdr (car options))) | |
256 (mapc (lambda (o) | |
257 (setq item (gtk-menu-item-new-with-label (car o))) | |
258 (gtk-signal-connect item 'activate | |
259 (lambda (obj data) | |
260 (put (car data) 'x-file-dialog-active-filter (cdr data)) | |
261 (gtk-file-dialog-fill-file-list (car data) nil)) | |
262 (cons dialog (cdr o))) | |
263 (gtk-menu-append menu item) | |
264 (gtk-widget-show item)) options) | |
265 (gtk-option-menu-set-menu omenu menu) | |
266 (gtk-box-pack-end hbox omenu nil nil 0) | |
267 (gtk-box-pack-end hbox label nil nil 0)))) | |
268 | |
269 ;; Entry | |
270 (let ((entry (gtk-entry-new))) | |
271 (if (plist-get keywords :directory nil) | |
272 nil | |
273 (gtk-box-pack-start vbox entry nil nil 0)) | |
274 (if (plist-get keywords :file-must-exist nil) | |
275 (progn | |
276 (gtk-widget-set-sensitive (get dialog 'x-file-dialog-ok-button nil) nil) | |
277 (gtk-signal-connect entry 'changed | |
278 (lambda (entry dialog) | |
279 (gtk-widget-set-sensitive | |
280 (get dialog 'x-file-dialog-ok-button) | |
281 (file-exists-p (gtk-entry-get-text entry)))) | |
282 dialog))) | |
283 (put dialog 'x-file-dialog-entry entry)) | |
284 | |
285 (gtk-widget-realize dialog) | |
286 | |
287 | |
288 ;; Populate the file list if necessary | |
289 (gtk-file-dialog-select-directory dialog dir) | |
290 dialog)) | |
291 | |
292 (provide 'gtk-file-dialog) |