comparison lisp/w3/w3-widget.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;; w3-widget.el,v --- An image widget
3 ;; Author: wmperry
4 ;; Created: 1996/05/29 03:11:24
5 ;; Version: 1.14
6 ;; Keywords: faces, help, comm, news, mail, processes, mouse, hypermedia
7
8 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9 ;;; Copyright (c) 1993, 1994, 1995 by William M. Perry (wmperry@spry.com)
10 ;;;
11 ;;; This file is not part of GNU Emacs, but the same permissions apply.
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
24 ;;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;;; This is a widget that will do the best it can with an image.
30 ;;;
31 ;;; It can handle all the common occurences of images on the world wide web
32 ;;; 1. A plain image - displays either a glyph of the image, or the
33 ;;; alternative text
34 ;;; 2. A hyperlinked image - an image that is also a hypertext link to
35 ;;; another page. Displays either a glyph of the image, or the
36 ;;; alternative text. When activated with the mouse or the keyboard,
37 ;;; the 'href' property of the widget is retrieved.
38 ;;; 3. Server side imagemaps - an image that has hotzones that lead to
39 ;;; different areas. Unfortunately, we cannot tell where the links go
40 ;;; from the client - all processing is done by the server. Displays
41 ;;; either a glyph of the image, or the alternative text. When activated
42 ;;; with the mouse or the keyboard, the coordinates clicked on are
43 ;;; sent to the remote server as HREF?x,y. If the link is activated
44 ;;; by the keyboard, then 0,0 are sent as the coordinates.
45 ;;; 4. Client side imagemaps - an image that has hotzones that lead to
46 ;;; different areas. All processing is done on the client side, so
47 ;;; we can actually show a decent representation on a TTY. Displays
48 ;;; either a glyph of the image, or a drop-down-list of the destinations
49 ;;; These are either URLs (http://foo/...) or alternative text.
50 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
51
52 (require 'cl)
53 (require 'widget)
54 (require 'w3-vars)
55 (require 'w3-mouse)
56
57 (defvar w3-image-widget-keymap (make-sparse-keymap)
58 "Keymap used over glyphs in an image widget")
59
60 (define-key w3-image-widget-keymap (vector w3-mouse-button1)
61 'w3-image-widget-button-press)
62 (define-key w3-image-widget-keymap (vector w3-mouse-button2)
63 'w3-image-widget-button-press)
64
65 (define-widget 'image 'default
66 "A fairly complex image widget."
67 :convert-widget 'w3-image-widget-convert
68 :value-to-internal (lambda (widget value) value)
69 :value-to-external (lambda (widget value) value)
70 :value-set 'w3-image-widget-value-set
71 :create 'w3-image-widget-create
72 :delete 'w3-image-widget-delete
73 :value-create 'w3-image-widget-value-create
74 :value-delete 'w3-image-widget-value-delete
75 :value-get 'w3-image-widget-value-get
76 :notify 'w3-image-widget-notify
77 )
78
79 (defun w3-image-widget-convert (widget)
80 (let ((args (widget-get widget :args)))
81 (widget-put widget :args nil)
82 (while args
83 (widget-put widget (car args) (cadr args))
84 (setq args (cddr args)))
85 widget))
86
87 (defun w3-image-widget-value-get (widget)
88 (let ((children (widget-get widget :children)))
89 (and (car children)
90 (widget-apply (car children) :value-get))))
91
92 (defun w3-image-widget-create (widget)
93 ;; Create an image widget at point in the current buffer
94 (let ((where (widget-get widget 'where)))
95 (cond
96 ((null where)
97 (setq where (set-marker (make-marker) (point))))
98 ((markerp where)
99 nil)
100 ((integerp where)
101 (setq where (set-marker (make-marker) where)))
102 (t
103 (error "IMPOSSIBLE position in w3-image-widget-create: %s" where)))
104 (widget-put widget 'where where))
105 (w3-image-widget-value-create widget))
106
107 (defun w3-image-widget-value-set (widget value)
108 ;; Recreate widget with new value.
109 (save-excursion
110 (w3-image-widget-delete widget)
111 (if (w3-glyphp value)
112 (widget-put widget 'glyph value)
113 (widget-put widget :value value))
114 (widget-apply widget :create)))
115
116 (defsubst w3-image-widget-usemap (widget)
117 (let ((usemap (widget-get widget 'usemap)))
118 (if (listp usemap)
119 usemap
120 (if (and usemap (string-match "^#" usemap))
121 (setq usemap (substring usemap 1 nil)))
122 (cdr-safe (assoc usemap w3-imagemaps)))))
123
124 (defun w3-image-widget-callback (widget widget-ignore &optional event)
125 (and (widget-get widget 'href) (w3-fetch (widget-get widget 'href))))
126
127 (defun w3-image-widget-value-create (widget)
128 ;; Insert the printed representation of the value
129 (let (
130 (href (widget-get widget 'href))
131 (server-map (widget-get widget 'ismap))
132 (client-map (w3-image-widget-usemap widget))
133 (where (or (widget-get widget 'where) (point)))
134 (glyph (widget-get widget 'glyph))
135 (alt (widget-get widget 'alt))
136 (real-widget nil)
137 (invalid-glyph nil)
138 )
139
140 ;; Specifier-instance will signal an error if we have an invalid
141 ;; image specifier, which would be the case if we get screwed up
142 ;; data back from a URL somewhere.
143
144 (setq invalid-glyph (and glyph (condition-case ()
145 (if (specifier-instance
146 (glyph-image glyph))
147 nil)
148 (error t))))
149 (if (or (not glyph) invalid-glyph)
150 ;; Do a TTY or delayed image version of the image.
151 (save-excursion
152 (if (= 0 (length alt)) (setq alt nil))
153 (goto-char where)
154 (cond
155 (client-map
156 (let* ((default nil)
157 (options (mapcar
158 (function
159 (lambda (x)
160 (if (eq (aref x 0) 'default)
161 (setq default (aref x 2)))
162 (if (and (not default) (stringp (aref x 2)))
163 (setq default (aref x 2)))
164 (list 'choice-item
165 :format "%[%t%]"
166 :tag (or (aref x 3) (aref x 2))
167 :value (aref x 2)))) client-map)))
168 (setq real-widget
169 (apply 'widget-create 'choice
170 :tag (or (widget-get widget :tag) "Imagemap")
171 :notify (widget-get widget :notify)
172 :value default options))))
173 ((and server-map (stringp href))
174 (setq real-widget
175 (widget-create 'push :tag alt
176 :delete 'widget-default-delete
177 :value href
178 :notify (widget-get widget :notify))))
179 (href
180 (setq real-widget
181 (widget-create 'push :tag (or alt "Image")
182 :value href
183 :delete 'widget-default-delete
184 :notify 'w3-image-widget-callback)))
185 (alt
186 (setq real-widget (widget-create 'item :format "%v" :value alt))))
187 (if (not real-widget)
188 nil
189 (widget-put real-widget 'usemap (widget-get widget 'usemap))
190 (widget-put real-widget 'href href)
191 (widget-put real-widget 'ismap server-map)
192 (widget-put real-widget :parent widget)
193 (widget-put widget :children (list real-widget))))
194 ;;; Actually use the image
195 (let ((extent (or (widget-get widget 'extent)
196 (make-extent where where))))
197 (set-extent-endpoints extent where where)
198 (widget-put widget 'extent extent)
199 (widget-put widget :children nil)
200 (set-extent-property extent 'keymap w3-image-widget-keymap)
201 (set-extent-property extent 'begin-glyph glyph)
202 (set-extent-property extent 'help-echo (cond
203 ((and href (or client-map
204 server-map))
205 (format "%s [map]" href))
206 (href href)
207 (t nil)))
208 (set-glyph-property glyph 'widget widget)))))
209
210 (defun w3-image-widget-delete (widget)
211 ;; Remove the widget from the buffer
212 (let ((extent (widget-get widget 'extent))
213 (child (car (widget-get widget :children))))
214 (cond
215 (extent ; Remove a glyph
216 (delete-extent extent))
217 (child ; Remove a child widget
218 (widget-apply child :delete))
219 (t ; Doh! Do nothing.
220 nil))))
221
222 (if (fboundp 'mouse-event-p)
223 (fset 'w3-mouse-event-p 'mouse-event-p)
224 (fset 'w3-mouse-event-p 'ignore))
225
226 (if (fboundp 'glyphp)
227 (fset 'w3-glyphp 'glyphp)
228 (fset 'w3-glyphp 'ignore))
229
230 (defun w3-image-widget-button-press (event)
231 (interactive "@e")
232 (let* ((glyph (and event (w3-mouse-event-p event) (event-glyph event)))
233 (widget (and glyph (glyph-property glyph 'widget))))
234 (w3-image-widget-notify widget widget event)))
235
236 (defun w3-image-widget-notify (widget widget-changed &optional event)
237 ;; Happens when anything changes
238 (let* ((glyph (and event (w3-mouse-event-p event) (event-glyph event)))
239 (x (and glyph (event-glyph-x-pixel event)))
240 (y (and glyph (event-glyph-y-pixel event)))
241 (ismap (widget-get widget 'ismap))
242 (usemap (w3-image-widget-usemap widget))
243 (href (widget-get widget 'href))
244 (value (widget-value widget))
245 )
246 (cond
247 ((and glyph usemap) ; Do the client-side imagemap stuff
248 (setq href (w3-point-in-map (vector x y) usemap nil))
249 (if href
250 (w3-fetch href)
251 (message "No destination found for %d,%d" x y)))
252 ((and glyph x y ismap) ; Do the server-side imagemap stuff
253 (w3-fetch (format "%s?%d,%d" href x y)))
254 (usemap ; Dummed-down tty client side imap
255 (w3-fetch value))
256 (ismap ; Do server-side dummy imagemap for tty
257 (w3-fetch (concat href "?0,0")))
258 ((stringp href) ; Normal hyperlink
259 (w3-fetch href))
260 (t ; Huh?
261 nil))))
262
263 (provide 'w3-widget)