2
|
1 ;;; w3-widget.el --- An image widget
|
0
|
2 ;; Author: wmperry
|
30
|
3 ;; Created: 1997/03/05 23:37:58
|
|
4 ;; Version: 1.20
|
14
|
5 ;; Keywords: faces, images
|
0
|
6
|
|
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2
|
8 ;;; Copyright (c) 1993 - 1996 by William M. Perry (wmperry@cs.indiana.edu)
|
16
|
9 ;;; Copyright (c) 1996, 1997 Free Software Foundation, Inc.
|
0
|
10 ;;;
|
14
|
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
|
14
|
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
|
14
|
56 (defvar widget-image-keymap (make-sparse-keymap)
|
0
|
57 "Keymap used over glyphs in an image widget")
|
|
58
|
14
|
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
|
30
|
75 (defvar widget-image-inaudible-p nil
|
|
76 "*Whether to make images inaudible or not.")
|
|
77
|
14
|
78 (define-key widget-image-keymap (vector widget-mouse-button1)
|
|
79 'widget-image-button-press)
|
|
80 (define-key widget-image-keymap (vector widget-mouse-button2)
|
|
81 'widget-image-button-press)
|
0
|
82
|
|
83 (define-widget 'image 'default
|
|
84 "A fairly complex image widget."
|
14
|
85 :convert-widget 'widget-image-convert
|
0
|
86 :value-to-internal (lambda (widget value) value)
|
|
87 :value-to-external (lambda (widget value) value)
|
14
|
88 :value-set 'widget-image-value-set
|
|
89 :create 'widget-image-create
|
|
90 :delete 'widget-image-delete
|
|
91 :value-create 'widget-image-value-create
|
|
92 :value-delete 'widget-image-value-delete
|
|
93 :value-get 'widget-image-value-get
|
|
94 :notify 'widget-image-notify
|
0
|
95 )
|
|
96
|
14
|
97 (defun widget-image-convert (widget)
|
0
|
98 (let ((args (widget-get widget :args)))
|
|
99 (widget-put widget :args nil)
|
|
100 (while args
|
|
101 (widget-put widget (car args) (cadr args))
|
|
102 (setq args (cddr args)))
|
|
103 widget))
|
|
104
|
14
|
105 (defun widget-image-value-get (widget)
|
0
|
106 (let ((children (widget-get widget :children)))
|
|
107 (and (car children)
|
|
108 (widget-apply (car children) :value-get))))
|
|
109
|
14
|
110 (defun widget-image-create (widget)
|
0
|
111 ;; Create an image widget at point in the current buffer
|
|
112 (let ((where (widget-get widget 'where)))
|
|
113 (cond
|
|
114 ((null where)
|
|
115 (setq where (set-marker (make-marker) (point))))
|
|
116 ((markerp where)
|
|
117 nil)
|
|
118 ((integerp where)
|
|
119 (setq where (set-marker (make-marker) where)))
|
|
120 (t
|
14
|
121 (error "IMPOSSIBLE position in widget-image-create: %s" where)))
|
0
|
122 (widget-put widget 'where where))
|
14
|
123 (widget-image-value-create widget))
|
0
|
124
|
14
|
125 (defun widget-image-value-set (widget value)
|
0
|
126 ;; Recreate widget with new value.
|
|
127 (save-excursion
|
14
|
128 (widget-image-delete widget)
|
|
129 (if (widget-glyphp value)
|
0
|
130 (widget-put widget 'glyph value)
|
|
131 (widget-put widget :value value))
|
30
|
132 (put-text-property (point)
|
|
133 (progn
|
|
134 (widget-apply widget :create)
|
|
135 (point))
|
|
136 'inaudible
|
|
137 widget-image-inaudible-p)))
|
0
|
138
|
14
|
139 (defsubst widget-image-usemap (widget)
|
0
|
140 (let ((usemap (widget-get widget 'usemap)))
|
|
141 (if (listp usemap)
|
|
142 usemap
|
|
143 (if (and usemap (string-match "^#" usemap))
|
|
144 (setq usemap (substring usemap 1 nil)))
|
|
145 (cdr-safe (assoc usemap w3-imagemaps)))))
|
|
146
|
14
|
147 (defun widget-image-callback (widget widget-ignore &optional event)
|
30
|
148 (and (widget-get widget 'href) (w3-fetch (widget-get widget 'href) (widget-get widget 'target))))
|
14
|
149
|
|
150 (defmacro widget-image-create-subwidget (&rest args)
|
|
151 (` (widget-create (,@ args)
|
|
152 :parent widget
|
|
153 :help-echo 'widget-image-summarize
|
|
154 'usemap (widget-get widget 'usemap)
|
|
155 'href href
|
|
156 'src (widget-get widget 'src)
|
|
157 'ismap server-map)))
|
|
158
|
20
|
159 (defun widget-image-emacspeak-tty-imagemap (usemap)
|
|
160 (let* ((default nil)
|
|
161 (href nil)
|
|
162 (tag nil)
|
|
163 (options (delete
|
|
164 nil
|
|
165 (mapcar
|
|
166 (function
|
|
167 (lambda (x)
|
|
168 (if (eq (aref x 0) 'default)
|
|
169 (setq default (aref x 2)))
|
|
170 (if (and (not default) (stringp (aref x 2)))
|
|
171 (setq default (aref x 2)))
|
|
172 (setq tag (or (aref x 3) (aref x 2))
|
|
173 href (aref x 2))
|
|
174 (and (stringp tag)
|
|
175 (stringp href)
|
|
176 (list 'a
|
|
177 (list
|
|
178 (cons 'href href)
|
|
179 (cons
|
|
180 'class
|
|
181 (list
|
|
182 (if (url-have-visited-url href)
|
|
183 ":visited" ":link"))))
|
|
184 (list tag)))))
|
|
185 usemap))))
|
|
186 (w3-display-node (list 'table '((border . "1"))
|
|
187 (w3-display-chop-into-table
|
|
188 (list nil nil options) 3)))))
|
|
189
|
14
|
190 (defun widget-image-value-create (widget)
|
0
|
191 ;; Insert the printed representation of the value
|
|
192 (let (
|
|
193 (href (widget-get widget 'href))
|
|
194 (server-map (widget-get widget 'ismap))
|
14
|
195 (client-map (widget-image-usemap widget))
|
0
|
196 (where (or (widget-get widget 'where) (point)))
|
|
197 (glyph (widget-get widget 'glyph))
|
|
198 (alt (widget-get widget 'alt))
|
|
199 (real-widget nil)
|
|
200 (invalid-glyph nil)
|
|
201 )
|
|
202
|
|
203 ;; Specifier-instance will signal an error if we have an invalid
|
|
204 ;; image specifier, which would be the case if we get screwed up
|
|
205 ;; data back from a URL somewhere.
|
|
206
|
|
207 (setq invalid-glyph (and glyph (condition-case ()
|
|
208 (if (specifier-instance
|
|
209 (glyph-image glyph))
|
|
210 nil)
|
|
211 (error t))))
|
|
212 (if (or (not glyph) invalid-glyph)
|
|
213 ;; Do a TTY or delayed image version of the image.
|
|
214 (save-excursion
|
|
215 (if (= 0 (length alt)) (setq alt nil))
|
|
216 (goto-char where)
|
|
217 (cond
|
|
218 (client-map
|
20
|
219 (if (featurep 'emacspeak)
|
|
220 (widget-image-emacspeak-tty-imagemap client-map)
|
|
221 (let* ((default nil)
|
|
222 (href nil)
|
|
223 (tag nil)
|
|
224 (options (mapcar
|
|
225 (function
|
|
226 (lambda (x)
|
|
227 (if (eq (aref x 0) 'default)
|
|
228 (setq default (aref x 2)))
|
|
229 (if (and (not default) (stringp (aref x 2)))
|
|
230 (setq default (aref x 2)))
|
|
231 (list 'choice-item
|
|
232 :format "%[%t%]"
|
|
233 :tag (or (aref x 3) (aref x 2))
|
|
234 :value (aref x 2)))) client-map)))
|
|
235 (setq real-widget
|
|
236 (apply 'widget-create 'menu-choice
|
|
237 :tag (or (widget-get widget :tag) "Imagemap")
|
|
238 :notify (widget-get widget :notify)
|
|
239 :action (widget-get widget :action)
|
|
240 :value default
|
|
241 :parent widget
|
|
242 :help-echo 'widget-image-summarize
|
|
243 options)))))
|
0
|
244 ((and server-map (stringp href))
|
|
245 (setq real-widget
|
14
|
246 (widget-image-create-subwidget
|
|
247 'push-button :tag alt
|
|
248 :delete 'widget-default-delete
|
|
249 :value href
|
|
250 :action (widget-get widget :action)
|
|
251 :notify (widget-get widget :notify))))
|
0
|
252 (href
|
|
253 (setq real-widget
|
14
|
254 (widget-image-create-subwidget
|
|
255 'push-button :tag (or alt "Image")
|
|
256 :value href
|
|
257 :delete 'widget-default-delete
|
|
258 :action (widget-get widget :action)
|
|
259 :notify 'widget-image-callback)))
|
0
|
260 (alt
|
2
|
261 (setq real-widget
|
14
|
262 (widget-image-create-subwidget
|
|
263 'push-button :tag alt :format "%[%t%]"
|
|
264 :delete 'widget-default-delete
|
|
265 :action (widget-get widget :action)
|
|
266 :notify 'widget-image-callback))))
|
0
|
267 (if (not real-widget)
|
|
268 nil
|
|
269 (widget-put widget :children (list real-widget))))
|
|
270 ;;; Actually use the image
|
|
271 (let ((extent (or (widget-get widget 'extent)
|
|
272 (make-extent where where))))
|
|
273 (set-extent-endpoints extent where where)
|
|
274 (widget-put widget 'extent extent)
|
|
275 (widget-put widget :children nil)
|
14
|
276 (set-extent-property extent 'keymap widget-image-keymap)
|
0
|
277 (set-extent-property extent 'begin-glyph glyph)
|
|
278 (set-extent-property extent 'help-echo (cond
|
|
279 ((and href (or client-map
|
|
280 server-map))
|
|
281 (format "%s [map]" href))
|
|
282 (href href)
|
|
283 (t nil)))
|
|
284 (set-glyph-property glyph 'widget widget)))))
|
|
285
|
14
|
286 (defun widget-image-delete (widget)
|
0
|
287 ;; Remove the widget from the buffer
|
|
288 (let ((extent (widget-get widget 'extent))
|
|
289 (child (car (widget-get widget :children))))
|
|
290 (cond
|
|
291 (extent ; Remove a glyph
|
|
292 (delete-extent extent))
|
|
293 (child ; Remove a child widget
|
|
294 (widget-apply child :delete))
|
|
295 (t ; Doh! Do nothing.
|
|
296 nil))))
|
|
297
|
|
298 (if (fboundp 'mouse-event-p)
|
14
|
299 (fset 'widget-mouse-event-p 'mouse-event-p)
|
|
300 (fset 'widget-mouse-event-p 'ignore))
|
0
|
301
|
|
302 (if (fboundp 'glyphp)
|
14
|
303 (fset 'widget-glyphp 'glyphp)
|
|
304 (fset 'widget-glyphp 'ignore))
|
0
|
305
|
14
|
306 (defun widget-image-button-press (event)
|
0
|
307 (interactive "@e")
|
14
|
308 (let* ((glyph (and event (widget-mouse-event-p event) (event-glyph event)))
|
0
|
309 (widget (and glyph (glyph-property glyph 'widget))))
|
14
|
310 (widget-image-notify widget widget event)))
|
|
311
|
|
312 (defun widget-image-usemap-default (usemap)
|
|
313 (let ((rval (and usemap (car usemap))))
|
|
314 (while usemap
|
|
315 (if (equal (aref (car usemap) 0) "default")
|
|
316 (setq rval (car usemap)
|
|
317 usemap nil))
|
|
318 (setq usemap (cdr usemap)))
|
|
319 rval))
|
0
|
320
|
14
|
321 (defun widget-image-summarize (widget)
|
|
322 (if (widget-get widget :parent)
|
|
323 (setq widget (widget-get widget :parent)))
|
|
324 (let* ((ismap (widget-get widget 'ismap))
|
|
325 (usemap (widget-image-usemap widget))
|
|
326 (href (widget-get widget 'href))
|
|
327 (alt (widget-get widget 'alt))
|
|
328 (value (widget-value widget))
|
|
329 (i nil))
|
|
330 (cond
|
|
331 (usemap
|
|
332 (setq i (length usemap)
|
|
333 usemap (widget-image-usemap-default usemap))
|
|
334 ;; Perhaps we should do something here with showing the # of entries
|
|
335 ;; in the imagemap as well as the default href? Could get too long.
|
|
336 (format "Client side imagemap: %s" value))
|
|
337 (ismap
|
|
338 (format "Server side imagemap: %s" href))
|
|
339 ((stringp href) ; Normal hyperlink
|
|
340 (format "Image hyperlink: %s" href))
|
|
341 ((stringp alt) ; Alternate message was specified
|
|
342 (format "Image: %s" alt))
|
|
343 ((stringp value)
|
|
344 (format "Image: %s" value))
|
|
345 (t ; Huh?
|
|
346 "A very confused image widget."))))
|
|
347
|
16
|
348 (defvar widget-image-auto-retrieve 'ask
|
|
349 "*Whether to automatically retrieve the source of an image widget
|
|
350 if it is not an active hyperlink or imagemap.
|
|
351 If `nil', don't do anything.
|
|
352 If `t', automatically retrieve the source.
|
|
353 Any other value means ask the user each time.")
|
|
354
|
14
|
355 (defun widget-image-notify (widget widget-changed &optional event)
|
0
|
356 ;; Happens when anything changes
|
14
|
357 (let* ((glyph (and event (widget-mouse-event-p event) (event-glyph event)))
|
0
|
358 (x (and glyph (event-glyph-x-pixel event)))
|
|
359 (y (and glyph (event-glyph-y-pixel event)))
|
|
360 (ismap (widget-get widget 'ismap))
|
14
|
361 (usemap (widget-image-usemap widget))
|
0
|
362 (href (widget-get widget 'href))
|
16
|
363 (img-src (or (widget-get widget 'src)
|
|
364 (and widget-changed (widget-get widget-changed 'src))))
|
0
|
365 (value (widget-value widget))
|
30
|
366 (target (widget-get widget 'target))
|
0
|
367 )
|
|
368 (cond
|
|
369 ((and glyph usemap) ; Do the client-side imagemap stuff
|
|
370 (setq href (w3-point-in-map (vector x y) usemap nil))
|
16
|
371 (if (stringp href)
|
30
|
372 (w3-fetch href target)
|
0
|
373 (message "No destination found for %d,%d" x y)))
|
|
374 ((and glyph x y ismap) ; Do the server-side imagemap stuff
|
30
|
375 (w3-fetch (format "%s?%d,%d" href x y) target))
|
0
|
376 (usemap ; Dummed-down tty client side imap
|
16
|
377 (let ((choices (mapcar (function
|
|
378 (lambda (entry)
|
|
379 (cons
|
|
380 (or (aref entry 3) (aref entry 2))
|
20
|
381 (aref entry 2)))) usemap))
|
16
|
382 (choice nil))
|
20
|
383 (setq choice (completing-read "Imagemap: " choices nil t)
|
|
384 choice (cdr-safe (assoc choice choices)))
|
30
|
385 (and (stringp choice) (w3-fetch choice target))))
|
0
|
386 (ismap ; Do server-side dummy imagemap for tty
|
30
|
387 (w3-fetch (concat href "?0,0") target))
|
0
|
388 ((stringp href) ; Normal hyperlink
|
30
|
389 (w3-fetch href target))
|
16
|
390 ((stringp img-src)
|
|
391 (cond
|
|
392 ((null widget-image-auto-retrieve) nil)
|
|
393 ((eq t widget-image-auto-retrieve)
|
|
394 (w3-fetch img-src))
|
|
395 ((funcall url-confirmation-func
|
|
396 (format "Retrieve image (%s)?"
|
|
397 (url-truncate-url-for-viewing img-src)))
|
|
398 (w3-fetch img-src))))
|
0
|
399 (t ; Huh?
|
|
400 nil))))
|
|
401
|
|
402 (provide 'w3-widget)
|