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