2
|
1 ;;; w3-widget.el --- An image widget
|
0
|
2 ;; Author: wmperry
|
82
|
3 ;; Created: 1997/01/17 22:09:43
|
|
4 ;; Version: 1.16
|
80
|
5 ;; Keywords: faces, images
|
0
|
6
|
|
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2
|
8 ;;; Copyright (c) 1993 - 1996 by William M. Perry (wmperry@cs.indiana.edu)
|
82
|
9 ;;; Copyright (c) 1996, 1997 Free Software Foundation, Inc.
|
0
|
10 ;;;
|
80
|
11 ;;; This file is part of GNU Emacs.
|
0
|
12 ;;;
|
|
13 ;;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
14 ;;; it under the terms of the GNU General Public License as published by
|
|
15 ;;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;;; any later version.
|
|
17 ;;;
|
|
18 ;;; GNU Emacs is distributed in the hope that it will be useful,
|
|
19 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 ;;; GNU General Public License for more details.
|
|
22 ;;;
|
|
23 ;;; You should have received a copy of the GNU General Public License
|
80
|
24 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
25 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
26 ;;; Boston, MA 02111-1307, USA.
|
0
|
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
28
|
|
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
30 ;;; This is a widget that will do the best it can with an image.
|
|
31 ;;;
|
|
32 ;;; It can handle all the common occurences of images on the world wide web
|
|
33 ;;; 1. A plain image - displays either a glyph of the image, or the
|
|
34 ;;; alternative text
|
|
35 ;;; 2. A hyperlinked image - an image that is also a hypertext link to
|
|
36 ;;; another page. Displays either a glyph of the image, or the
|
|
37 ;;; alternative text. When activated with the mouse or the keyboard,
|
|
38 ;;; the 'href' property of the widget is retrieved.
|
|
39 ;;; 3. Server side imagemaps - an image that has hotzones that lead to
|
|
40 ;;; different areas. Unfortunately, we cannot tell where the links go
|
|
41 ;;; from the client - all processing is done by the server. Displays
|
|
42 ;;; either a glyph of the image, or the alternative text. When activated
|
|
43 ;;; with the mouse or the keyboard, the coordinates clicked on are
|
|
44 ;;; sent to the remote server as HREF?x,y. If the link is activated
|
|
45 ;;; by the keyboard, then 0,0 are sent as the coordinates.
|
|
46 ;;; 4. Client side imagemaps - an image that has hotzones that lead to
|
|
47 ;;; different areas. All processing is done on the client side, so
|
|
48 ;;; we can actually show a decent representation on a TTY. Displays
|
|
49 ;;; either a glyph of the image, or a drop-down-list of the destinations
|
|
50 ;;; These are either URLs (http://foo/...) or alternative text.
|
|
51 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
52
|
|
53 (require 'cl)
|
|
54 (require 'widget)
|
|
55
|
80
|
56 (defvar widget-image-keymap (make-sparse-keymap)
|
0
|
57 "Keymap used over glyphs in an image widget")
|
|
58
|
80
|
59 (defconst widget-mouse-button1 nil)
|
|
60 (defconst widget-mouse-button2 nil)
|
|
61 (defconst widget-mouse-button3 nil)
|
|
62
|
|
63 (if (string-match "XEmacs" (emacs-version))
|
|
64 (if (featurep 'mouse)
|
|
65 (setq widget-mouse-button1 'button1
|
|
66 widget-mouse-button2 'button2
|
|
67 widget-mouse-button3 'button3)
|
|
68 (setq widget-mouse-button1 'return
|
|
69 widget-mouse-button2 'return
|
|
70 widget-mouse-button3 'return))
|
|
71 (setq widget-mouse-button1 'mouse-1
|
|
72 widget-mouse-button2 'mouse-2
|
|
73 widget-mouse-button3 'mouse-3))
|
|
74
|
|
75 (define-key widget-image-keymap (vector widget-mouse-button1)
|
|
76 'widget-image-button-press)
|
|
77 (define-key widget-image-keymap (vector widget-mouse-button2)
|
|
78 'widget-image-button-press)
|
0
|
79
|
|
80 (define-widget 'image 'default
|
|
81 "A fairly complex image widget."
|
80
|
82 :convert-widget 'widget-image-convert
|
0
|
83 :value-to-internal (lambda (widget value) value)
|
|
84 :value-to-external (lambda (widget value) value)
|
80
|
85 :value-set 'widget-image-value-set
|
|
86 :create 'widget-image-create
|
|
87 :delete 'widget-image-delete
|
|
88 :value-create 'widget-image-value-create
|
|
89 :value-delete 'widget-image-value-delete
|
|
90 :value-get 'widget-image-value-get
|
|
91 :notify 'widget-image-notify
|
0
|
92 )
|
|
93
|
80
|
94 (defun widget-image-convert (widget)
|
0
|
95 (let ((args (widget-get widget :args)))
|
|
96 (widget-put widget :args nil)
|
|
97 (while args
|
|
98 (widget-put widget (car args) (cadr args))
|
|
99 (setq args (cddr args)))
|
|
100 widget))
|
|
101
|
80
|
102 (defun widget-image-value-get (widget)
|
0
|
103 (let ((children (widget-get widget :children)))
|
|
104 (and (car children)
|
|
105 (widget-apply (car children) :value-get))))
|
|
106
|
80
|
107 (defun widget-image-create (widget)
|
0
|
108 ;; Create an image widget at point in the current buffer
|
|
109 (let ((where (widget-get widget 'where)))
|
|
110 (cond
|
|
111 ((null where)
|
|
112 (setq where (set-marker (make-marker) (point))))
|
|
113 ((markerp where)
|
|
114 nil)
|
|
115 ((integerp where)
|
|
116 (setq where (set-marker (make-marker) where)))
|
|
117 (t
|
80
|
118 (error "IMPOSSIBLE position in widget-image-create: %s" where)))
|
0
|
119 (widget-put widget 'where where))
|
80
|
120 (widget-image-value-create widget))
|
0
|
121
|
80
|
122 (defun widget-image-value-set (widget value)
|
0
|
123 ;; Recreate widget with new value.
|
|
124 (save-excursion
|
80
|
125 (widget-image-delete widget)
|
|
126 (if (widget-glyphp value)
|
0
|
127 (widget-put widget 'glyph value)
|
|
128 (widget-put widget :value value))
|
70
|
129 (widget-apply widget :create)))
|
0
|
130
|
80
|
131 (defsubst widget-image-usemap (widget)
|
0
|
132 (let ((usemap (widget-get widget 'usemap)))
|
|
133 (if (listp usemap)
|
|
134 usemap
|
|
135 (if (and usemap (string-match "^#" usemap))
|
|
136 (setq usemap (substring usemap 1 nil)))
|
|
137 (cdr-safe (assoc usemap w3-imagemaps)))))
|
|
138
|
80
|
139 (defun widget-image-callback (widget widget-ignore &optional event)
|
70
|
140 (and (widget-get widget 'href) (w3-fetch (widget-get widget 'href))))
|
80
|
141
|
|
142 (defmacro widget-image-create-subwidget (&rest args)
|
|
143 (` (widget-create (,@ args)
|
|
144 :parent widget
|
|
145 :help-echo 'widget-image-summarize
|
|
146 'usemap (widget-get widget 'usemap)
|
|
147 'href href
|
|
148 'src (widget-get widget 'src)
|
|
149 'ismap server-map)))
|
|
150
|
|
151 (defun widget-image-value-create (widget)
|
0
|
152 ;; Insert the printed representation of the value
|
|
153 (let (
|
|
154 (href (widget-get widget 'href))
|
|
155 (server-map (widget-get widget 'ismap))
|
80
|
156 (client-map (widget-image-usemap widget))
|
0
|
157 (where (or (widget-get widget 'where) (point)))
|
|
158 (glyph (widget-get widget 'glyph))
|
|
159 (alt (widget-get widget 'alt))
|
|
160 (real-widget nil)
|
|
161 (invalid-glyph nil)
|
|
162 )
|
|
163
|
|
164 ;; Specifier-instance will signal an error if we have an invalid
|
|
165 ;; image specifier, which would be the case if we get screwed up
|
|
166 ;; data back from a URL somewhere.
|
|
167
|
|
168 (setq invalid-glyph (and glyph (condition-case ()
|
|
169 (if (specifier-instance
|
|
170 (glyph-image glyph))
|
|
171 nil)
|
|
172 (error t))))
|
|
173 (if (or (not glyph) invalid-glyph)
|
|
174 ;; Do a TTY or delayed image version of the image.
|
|
175 (save-excursion
|
|
176 (if (= 0 (length alt)) (setq alt nil))
|
|
177 (goto-char where)
|
|
178 (cond
|
|
179 (client-map
|
44
|
180 (let* ((default nil)
|
|
181 (options (mapcar
|
|
182 (function
|
|
183 (lambda (x)
|
|
184 (if (eq (aref x 0) 'default)
|
|
185 (setq default (aref x 2)))
|
|
186 (if (and (not default) (stringp (aref x 2)))
|
|
187 (setq default (aref x 2)))
|
|
188 (list 'choice-item
|
|
189 :format "%[%t%]"
|
|
190 :tag (or (aref x 3) (aref x 2))
|
|
191 :value (aref x 2)))) client-map)))
|
|
192 (setq real-widget
|
80
|
193 (apply 'widget-create 'menu-choice
|
44
|
194 :tag (or (widget-get widget :tag) "Imagemap")
|
|
195 :notify (widget-get widget :notify)
|
80
|
196 :action (widget-get widget :action)
|
|
197 :value default
|
|
198 :parent widget
|
|
199 :help-echo 'widget-image-summarize
|
|
200 options))))
|
0
|
201 ((and server-map (stringp href))
|
|
202 (setq real-widget
|
80
|
203 (widget-image-create-subwidget
|
|
204 'push-button :tag alt
|
|
205 :delete 'widget-default-delete
|
|
206 :value href
|
|
207 :action (widget-get widget :action)
|
|
208 :notify (widget-get widget :notify))))
|
0
|
209 (href
|
|
210 (setq real-widget
|
80
|
211 (widget-image-create-subwidget
|
|
212 'push-button :tag (or alt "Image")
|
|
213 :value href
|
|
214 :delete 'widget-default-delete
|
|
215 :action (widget-get widget :action)
|
|
216 :notify 'widget-image-callback)))
|
0
|
217 (alt
|
2
|
218 (setq real-widget
|
80
|
219 (widget-image-create-subwidget
|
|
220 'push-button :tag alt :format "%[%t%]"
|
|
221 :delete 'widget-default-delete
|
|
222 :action (widget-get widget :action)
|
|
223 :notify 'widget-image-callback))))
|
0
|
224 (if (not real-widget)
|
|
225 nil
|
|
226 (widget-put widget :children (list real-widget))))
|
|
227 ;;; Actually use the image
|
|
228 (let ((extent (or (widget-get widget 'extent)
|
|
229 (make-extent where where))))
|
|
230 (set-extent-endpoints extent where where)
|
|
231 (widget-put widget 'extent extent)
|
|
232 (widget-put widget :children nil)
|
80
|
233 (set-extent-property extent 'keymap widget-image-keymap)
|
0
|
234 (set-extent-property extent 'begin-glyph glyph)
|
|
235 (set-extent-property extent 'help-echo (cond
|
|
236 ((and href (or client-map
|
|
237 server-map))
|
|
238 (format "%s [map]" href))
|
|
239 (href href)
|
|
240 (t nil)))
|
|
241 (set-glyph-property glyph 'widget widget)))))
|
|
242
|
80
|
243 (defun widget-image-delete (widget)
|
0
|
244 ;; Remove the widget from the buffer
|
|
245 (let ((extent (widget-get widget 'extent))
|
|
246 (child (car (widget-get widget :children))))
|
|
247 (cond
|
|
248 (extent ; Remove a glyph
|
|
249 (delete-extent extent))
|
|
250 (child ; Remove a child widget
|
|
251 (widget-apply child :delete))
|
|
252 (t ; Doh! Do nothing.
|
|
253 nil))))
|
|
254
|
|
255 (if (fboundp 'mouse-event-p)
|
80
|
256 (fset 'widget-mouse-event-p 'mouse-event-p)
|
|
257 (fset 'widget-mouse-event-p 'ignore))
|
0
|
258
|
|
259 (if (fboundp 'glyphp)
|
80
|
260 (fset 'widget-glyphp 'glyphp)
|
|
261 (fset 'widget-glyphp 'ignore))
|
0
|
262
|
80
|
263 (defun widget-image-button-press (event)
|
70
|
264 (interactive "@e")
|
80
|
265 (let* ((glyph (and event (widget-mouse-event-p event) (event-glyph event)))
|
70
|
266 (widget (and glyph (glyph-property glyph 'widget))))
|
80
|
267 (widget-image-notify widget widget event)))
|
|
268
|
|
269 (defun widget-image-usemap-default (usemap)
|
|
270 (let ((rval (and usemap (car usemap))))
|
|
271 (while usemap
|
|
272 (if (equal (aref (car usemap) 0) "default")
|
|
273 (setq rval (car usemap)
|
|
274 usemap nil))
|
|
275 (setq usemap (cdr usemap)))
|
|
276 rval))
|
14
|
277
|
80
|
278 (defun widget-image-summarize (widget)
|
|
279 (if (widget-get widget :parent)
|
|
280 (setq widget (widget-get widget :parent)))
|
|
281 (let* ((ismap (widget-get widget 'ismap))
|
|
282 (usemap (widget-image-usemap widget))
|
|
283 (href (widget-get widget 'href))
|
|
284 (alt (widget-get widget 'alt))
|
|
285 (value (widget-value widget))
|
|
286 (i nil))
|
|
287 (cond
|
|
288 (usemap
|
|
289 (setq i (length usemap)
|
|
290 usemap (widget-image-usemap-default usemap))
|
|
291 ;; Perhaps we should do something here with showing the # of entries
|
|
292 ;; in the imagemap as well as the default href? Could get too long.
|
|
293 (format "Client side imagemap: %s" value))
|
|
294 (ismap
|
|
295 (format "Server side imagemap: %s" href))
|
|
296 ((stringp href) ; Normal hyperlink
|
|
297 (format "Image hyperlink: %s" href))
|
|
298 ((stringp alt) ; Alternate message was specified
|
|
299 (format "Image: %s" alt))
|
|
300 ((stringp value)
|
|
301 (format "Image: %s" value))
|
|
302 (t ; Huh?
|
|
303 "A very confused image widget."))))
|
|
304
|
82
|
305 (defvar widget-image-auto-retrieve 'ask
|
|
306 "*Whether to automatically retrieve the source of an image widget
|
|
307 if it is not an active hyperlink or imagemap.
|
|
308 If `nil', don't do anything.
|
|
309 If `t', automatically retrieve the source.
|
|
310 Any other value means ask the user each time.")
|
|
311
|
80
|
312 (defun widget-image-notify (widget widget-changed &optional event)
|
0
|
313 ;; Happens when anything changes
|
80
|
314 (let* ((glyph (and event (widget-mouse-event-p event) (event-glyph event)))
|
0
|
315 (x (and glyph (event-glyph-x-pixel event)))
|
|
316 (y (and glyph (event-glyph-y-pixel event)))
|
|
317 (ismap (widget-get widget 'ismap))
|
80
|
318 (usemap (widget-image-usemap widget))
|
0
|
319 (href (widget-get widget 'href))
|
82
|
320 (img-src (or (widget-get widget 'src)
|
|
321 (and widget-changed (widget-get widget-changed 'src))))
|
70
|
322 (value (widget-value widget))
|
0
|
323 )
|
|
324 (cond
|
|
325 ((and glyph usemap) ; Do the client-side imagemap stuff
|
|
326 (setq href (w3-point-in-map (vector x y) usemap nil))
|
82
|
327 (if (stringp href)
|
70
|
328 (w3-fetch href)
|
0
|
329 (message "No destination found for %d,%d" x y)))
|
|
330 ((and glyph x y ismap) ; Do the server-side imagemap stuff
|
70
|
331 (w3-fetch (format "%s?%d,%d" href x y)))
|
0
|
332 (usemap ; Dummed-down tty client side imap
|
82
|
333 (let ((choices (mapcar (function
|
|
334 (lambda (entry)
|
|
335 (cons
|
|
336 (or (aref entry 3) (aref entry 2))
|
|
337 (aref entry 3)))) usemap))
|
|
338 (choice nil))
|
|
339 (setq choice (completing-read "Imagemap: " choices nil t))
|
|
340 (and (stringp choice) (w3-fetch choice))))
|
0
|
341 (ismap ; Do server-side dummy imagemap for tty
|
70
|
342 (w3-fetch (concat href "?0,0")))
|
0
|
343 ((stringp href) ; Normal hyperlink
|
70
|
344 (w3-fetch href))
|
82
|
345 ((stringp img-src)
|
|
346 (cond
|
|
347 ((null widget-image-auto-retrieve) nil)
|
|
348 ((eq t widget-image-auto-retrieve)
|
|
349 (w3-fetch img-src))
|
|
350 ((funcall url-confirmation-func
|
|
351 (format "Retrieve image (%s)?"
|
|
352 (url-truncate-url-for-viewing img-src)))
|
|
353 (w3-fetch img-src))))
|
0
|
354 (t ; Huh?
|
|
355 nil))))
|
|
356
|
|
357 (provide 'w3-widget)
|