*** empty log message ***
[bpt/emacs.git] / lisp / tree-widget.el
CommitLineData
3212eb61
DP
1;;; tree-widget.el --- Tree widget
2
3bcf793c 3;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3212eb61
DP
4
5;; Author: David Ponce <david@dponce.com>
6;; Maintainer: David Ponce <david@dponce.com>
7;; Created: 16 Feb 2001
8;; Keywords: extensions
9
10;; This file is part of GNU Emacs
11
12;; This program is free software; you can redistribute it and/or
13;; modify it under the terms of the GNU General Public License as
14;; published by the Free Software Foundation; either version 2, or (at
15;; your option) any later version.
16
17;; This program is distributed in the hope that it will be useful, but
18;; WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20;; General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with this program; see the file COPYING. If not, write to
086add15
LK
24;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
3212eb61
DP
26
27;;; Commentary:
28;;
29;; This library provide a tree widget useful to display data
30;; structures organized in a hierarchical order.
31;;
32;; The following properties are specific to the tree widget:
33;;
f2cb69d5
DP
34;; :open
35;; Set to non-nil to expand the tree. By default the tree is
36;; collapsed.
3212eb61 37;;
f2cb69d5
DP
38;; :node
39;; Specify the widget used to represent the value of a tree node.
40;; By default this is an `item' widget which displays the
41;; tree-widget :tag property value if defined, or a string
42;; representation of the tree-widget value.
3212eb61 43;;
f2cb69d5
DP
44;; :keep
45;; Specify a list of properties to keep when the tree is collapsed
46;; so they can be recovered when the tree is expanded. This
47;; property can be used in child widgets too.
3212eb61 48;;
f2cb69d5
DP
49;; :expander (obsoletes :dynargs)
50;; Specify a function to be called to dynamically provide the
51;; tree's children in response to an expand request. This function
52;; will be passed the tree widget and must return a list of child
53;; widgets.
3212eb61 54;;
f2cb69d5
DP
55;; *Please note:* Child widgets returned by the :expander function
56;; are stored in the :args property of the tree widget. To speed
57;; up successive expand requests, the :expander function is not
58;; called again when the :args value is non-nil. To refresh child
59;; values, it is necessary to set the :args property to nil, then
60;; redraw the tree.
3212eb61 61;;
0cfce69f
DP
62;; :open-icon (default `tree-widget-open-icon')
63;; :close-icon (default `tree-widget-close-icon')
64;; :empty-icon (default `tree-widget-empty-icon')
65;; :leaf-icon (default `tree-widget-leaf-icon')
66;; Those properties define the icon widgets associated to tree
67;; nodes. Icon widgets must derive from the `tree-widget-icon'
68;; widget. The :tag and :glyph-name property values are
69;; respectively used when drawing the text and graphic
70;; representation of the tree. The :tag value must be a string
71;; that represent a node icon, like "[+]" for example. The
72;; :glyph-name value must the name of an image found in the current
73;; theme, like "close" for example (see also the variable
74;; `tree-widget-theme').
3212eb61 75;;
0cfce69f
DP
76;; :guide (default `tree-widget-guide')
77;; :end-guide (default `tree-widget-end-guide')
78;; :no-guide (default `tree-widget-no-guide')
79;; :handle (default `tree-widget-handle')
80;; :no-handle (default `tree-widget-no-handle')
81;; Those properties define `item'-like widgets used to draw the
82;; tree guide lines. The :tag property value is used when drawing
83;; the text representation of the tree. The graphic look and feel
84;; is given by the images named "guide", "no-guide", "end-guide",
85;; "handle", and "no-handle" found in the current theme (see also
86;; the variable `tree-widget-theme').
3212eb61 87;;
0cfce69f 88;; These are the default :tag values for icons, and guide lines:
3212eb61 89;;
0cfce69f
DP
90;; open-icon "[-]"
91;; close-icon "[+]"
92;; empty-icon "[X]"
93;; leaf-icon ""
94;; guide " |"
95;; no-guide " "
96;; end-guide " `"
97;; handle "-"
98;; no-handle " "
99;;
100;; The text representation of a tree looks like this:
101;;
102;; [-] 1 (open-icon :node)
103;; |-[+] 1.0 (guide+handle+close-icon :node)
104;; |-[X] 1.1 (guide+handle+empty-icon :node)
105;; `-[-] 1.2 (end-guide+handle+open-icon :node)
106;; |- 1.2.1 (no-guide+no-handle+guide+handle+leaf-icon leaf)
107;; `- 1.2.2 (no-guide+no-handle+end-guide+handle+leaf-icon leaf)
3212eb61 108;;
f2cb69d5
DP
109;; By default, images will be used instead of strings to draw a
110;; nice-looking tree. See the `tree-widget-image-enable',
111;; `tree-widget-themes-directory', and `tree-widget-theme' options for
112;; more details.
3212eb61
DP
113
114;;; History:
115;;
116
117;;; Code:
118(eval-when-compile (require 'cl))
119(require 'wid-edit)
120\f
121;;; Customization
122;;
123(defgroup tree-widget nil
f2cb69d5 124 "Customization support for the Tree Widget library."
bf247b6e 125 :version "22.1"
3212eb61
DP
126 :group 'widgets)
127
128(defcustom tree-widget-image-enable
129 (not (or (featurep 'xemacs) (< emacs-major-version 21)))
f2cb69d5 130 "*Non-nil means that tree-widget will try to use images."
3212eb61
DP
131 :type 'boolean
132 :group 'tree-widget)
133
01c5577a
DP
134(defvar tree-widget-themes-load-path
135 '(load-path
136 (let ((dir (if (fboundp 'locate-data-directory)
137 (locate-data-directory "tree-widget") ;; XEmacs
138 data-directory)))
139 (and dir (list dir (expand-file-name "images" dir))))
140 )
141 "List of locations where to search for the themes sub-directory.
142Each element is an expression that will be evaluated to return a
143single directory or a list of directories to search.
144
145The default is to search in the `load-path' first, then in the
146\"images\" sub directory in the data directory, then in the data
147directory.
148The data directory is the value of the variable `data-directory' on
149Emacs, and what `(locate-data-directory \"tree-widget\")' returns on
150XEmacs.")
151
3212eb61 152(defcustom tree-widget-themes-directory "tree-widget"
f2cb69d5 153 "*Name of the directory where to look up for image themes.
3212eb61 154When nil use the directory where the tree-widget library is located.
f2cb69d5 155When a relative name is specified, try to locate that sub directory in
01c5577a 156the locations specified in `tree-widget-themes-load-path'.
f2cb69d5 157The default is to use the \"tree-widget\" relative name."
3212eb61
DP
158 :type '(choice (const :tag "Default" "tree-widget")
159 (const :tag "With the library" nil)
160 (directory :format "%{%t%}:\n%v"))
161 :group 'tree-widget)
162
163(defcustom tree-widget-theme nil
f2cb69d5
DP
164 "*Name of the theme where to look up for images.
165It must be a sub directory of the directory specified in variable
0cfce69f
DP
166`tree-widget-themes-directory'. The default theme is \"default\".
167When an image is not found in a theme, it is searched in the default
168theme.
169
170A complete theme must at least contain images with these file names
171with a supported extension (see also `tree-widget-image-formats'):
f2cb69d5 172
f2cb69d5
DP
173\"guide\"
174 A vertical guide line.
175\"no-guide\"
176 An invisible vertical guide line.
177\"end-guide\"
178 End of a vertical guide line.
179\"handle\"
0cfce69f 180 Horizontal guide line that joins the vertical guide line to an icon.
f2cb69d5 181\"no-handle\"
0cfce69f
DP
182 An invisible handle.
183
184Plus images whose name is given by the :glyph-name property of the
185icon widgets used to draw the tree. By default these images are used:
186
187\"open\"
188 Icon associated to an expanded tree.
189\"close\"
190 Icon associated to a collapsed tree.
191\"empty\"
192 Icon associated to an expanded tree with no child.
193\"leaf\"
194 Icon associated to a leaf node."
3212eb61
DP
195 :type '(choice (const :tag "Default" nil)
196 (string :tag "Name"))
197 :group 'tree-widget)
198
199(defcustom tree-widget-image-properties-emacs
200 '(:ascent center :mask (heuristic t))
f2cb69d5 201 "*Default properties of Emacs images."
3212eb61
DP
202 :type 'plist
203 :group 'tree-widget)
204
205(defcustom tree-widget-image-properties-xemacs
206 nil
f2cb69d5 207 "*Default properties of XEmacs images."
3212eb61
DP
208 :type 'plist
209 :group 'tree-widget)
0cfce69f
DP
210
211(defcustom tree-widget-space-width 0.5
212 "Amount of space between an icon image and a node widget.
213Must be a valid space :width display property."
214 :group 'tree-widget
215 :type 'sexp)
3212eb61
DP
216\f
217;;; Image support
218;;
f2cb69d5 219(eval-and-compile ;; Emacs/XEmacs compatibility stuff
3212eb61
DP
220 (cond
221 ;; XEmacs
222 ((featurep 'xemacs)
223 (defsubst tree-widget-use-image-p ()
224 "Return non-nil if image support is currently enabled."
225 (and tree-widget-image-enable
226 widget-glyph-enable
227 (console-on-window-system-p)))
228 (defsubst tree-widget-create-image (type file &optional props)
f2cb69d5
DP
229 "Create an image of type TYPE from FILE, and return it.
230Give the image the specified properties PROPS."
3212eb61
DP
231 (apply 'make-glyph `([,type :file ,file ,@props])))
232 (defsubst tree-widget-image-formats ()
f2cb69d5 233 "Return the alist of image formats/file name extensions.
3212eb61
DP
234See also the option `widget-image-file-name-suffixes'."
235 (delq nil
236 (mapcar
237 #'(lambda (fmt)
238 (and (valid-image-instantiator-format-p (car fmt)) fmt))
239 widget-image-file-name-suffixes)))
240 )
f2cb69d5 241 ;; Emacs
3212eb61
DP
242 (t
243 (defsubst tree-widget-use-image-p ()
244 "Return non-nil if image support is currently enabled."
245 (and tree-widget-image-enable
246 widget-image-enable
247 (display-images-p)))
248 (defsubst tree-widget-create-image (type file &optional props)
f2cb69d5
DP
249 "Create an image of type TYPE from FILE, and return it.
250Give the image the specified properties PROPS."
3212eb61
DP
251 (apply 'create-image `(,file ,type nil ,@props)))
252 (defsubst tree-widget-image-formats ()
f2cb69d5 253 "Return the alist of image formats/file name extensions.
01c5577a 254See also the option `widget-image-conversion'."
3212eb61
DP
255 (delq nil
256 (mapcar
257 #'(lambda (fmt)
258 (and (image-type-available-p (car fmt)) fmt))
259 widget-image-conversion)))
260 ))
261 )
262
263;; Buffer local cache of theme data.
264(defvar tree-widget--theme nil)
265
266(defsubst tree-widget-theme-name ()
267 "Return the current theme name, or nil if no theme is active."
268 (and tree-widget--theme (aref tree-widget--theme 0)))
269
270(defsubst tree-widget-set-theme (&optional name)
271 "In the current buffer, set the theme to use for images.
f2cb69d5
DP
272The current buffer must be where the tree widget is drawn.
273Optional argument NAME is the name of the theme to use. It defaults
3212eb61 274to the value of the variable `tree-widget-theme'.
f2cb69d5 275Does nothing if NAME is already the current theme."
3212eb61 276 (or name (setq name (or tree-widget-theme "default")))
f2cb69d5 277 (unless (string-equal name (tree-widget-theme-name))
3212eb61
DP
278 (set (make-local-variable 'tree-widget--theme)
279 (make-vector 4 nil))
280 (aset tree-widget--theme 0 name)))
281
01c5577a
DP
282(defun tree-widget--locate-sub-directory (name path)
283 "Locate the sub-directory NAME in PATH.
284Return the absolute name of the directory found, or nil if not found."
285 (let (dir elt)
286 (while (and (not dir) (consp path))
287 (setq elt (condition-case nil (eval (car path)) (error nil))
288 path (cdr path))
289 (cond
290 ((stringp elt)
291 (setq dir (expand-file-name name elt))
292 (or (file-accessible-directory-p dir)
293 (setq dir nil)))
294 ((and elt (not (equal elt (car path))))
295 (setq dir (tree-widget--locate-sub-directory name elt)))))
296 dir))
297
3212eb61
DP
298(defun tree-widget-themes-directory ()
299 "Locate the directory where to search for a theme.
300It is defined in variable `tree-widget-themes-directory'.
301Return the absolute name of the directory found, or nil if the
302specified directory is not accessible."
303 (let ((found (aref tree-widget--theme 1)))
01c5577a
DP
304 (cond
305 ;; The directory was not found.
306 ((eq found 'void)
307 (setq found nil))
308 ;; The directory is available in the cache.
309 (found)
310 ;; Use the directory where this library is located.
311 ((null tree-widget-themes-directory)
312 (setq found (locate-library "tree-widget"))
313 (when found
314 (setq found (file-name-directory found))
3212eb61 315 (or (file-accessible-directory-p found)
01c5577a
DP
316 (setq found nil))))
317 ;; Check accessibility of absolute directory name.
318 ((file-name-absolute-p tree-widget-themes-directory)
319 (setq found (expand-file-name tree-widget-themes-directory))
320 (or (file-accessible-directory-p found)
321 (setq found nil)))
322 ;; Locate a sub-directory in `tree-widget-themes-load-path'.
323 (t
324 (setq found (tree-widget--locate-sub-directory
325 tree-widget-themes-directory
326 tree-widget-themes-load-path))))
327 ;; Store the result in the cache for later use.
328 (aset tree-widget--theme 1 (or found 'void))
329 found))
3212eb61
DP
330
331(defsubst tree-widget-set-image-properties (props)
332 "In current theme, set images properties to PROPS."
333 (aset tree-widget--theme 2 props))
334
335(defun tree-widget-image-properties (file)
f2cb69d5
DP
336 "Return the properties of an image in current theme.
337FILE is the absolute file name of an image.
338
339If there is a \"tree-widget-theme-setup\" library in the theme
340directory, where is located FILE, load it to setup theme images
341properties. Typically it should contain something like this:
3212eb61
DP
342
343 (tree-widget-set-image-properties
344 (if (featurep 'xemacs)
345 '(:ascent center)
346 '(:ascent center :mask (heuristic t))
347 ))
348
0cfce69f
DP
349When there is no \"tree-widget-theme-setup\" library in the current
350theme directory, load the one from the default theme, if available.
f2cb69d5
DP
351Default global properties are provided for respectively Emacs and
352XEmacs in the variables `tree-widget-image-properties-emacs', and
3212eb61
DP
353`tree-widget-image-properties-xemacs'."
354 ;; If properties are in the cache, use them.
f2cb69d5
DP
355 (let ((plist (aref tree-widget--theme 2)))
356 (unless plist
357 ;; Load tree-widget-theme-setup if available.
358 (load (expand-file-name "tree-widget-theme-setup"
359 (file-name-directory file)) t t)
360 ;; If properties have been setup, use them.
361 (unless (setq plist (aref tree-widget--theme 2))
0cfce69f
DP
362 ;; Try from the default theme.
363 (load (expand-file-name "../default/tree-widget-theme-setup"
364 (file-name-directory file)) t t)
365 ;; If properties have been setup, use them.
366 (unless (setq plist (aref tree-widget--theme 2))
367 ;; By default, use supplied global properties.
368 (setq plist (if (featurep 'xemacs)
369 tree-widget-image-properties-xemacs
370 tree-widget-image-properties-emacs))
371 ;; Setup the cache.
372 (tree-widget-set-image-properties plist))))
f2cb69d5
DP
373 plist))
374
375(defconst tree-widget--cursors
01c5577a
DP
376 ;; Pointer shapes when the mouse pointer is over inactive
377 ;; tree-widget images. This feature works since Emacs 22, and
378 ;; ignored on older versions, and XEmacs.
f2cb69d5 379 '(
f2cb69d5
DP
380 ("guide" . arrow)
381 ("no-guide" . arrow)
382 ("end-guide" . arrow)
383 ("handle" . arrow)
384 ("no-handle" . arrow)
385 ))
386
387(defun tree-widget-lookup-image (name)
388 "Look up in current theme for an image with NAME.
389Search first in current theme, then in default theme (see also the
390variable `tree-widget-theme').
391Return the first image found having a supported format, or nil if not
392found."
393 (let ((default-directory (tree-widget-themes-directory)))
394 (when default-directory
395 (let (file (theme (tree-widget-theme-name)))
396 (catch 'found
397 (dolist (dir (if (string-equal theme "default")
398 '("default") (list theme "default")))
399 (dolist (fmt (tree-widget-image-formats))
400 (dolist (ext (cdr fmt))
401 (setq file (expand-file-name (concat name ext) dir))
402 (and
403 (file-readable-p file)
404 (file-regular-p file)
405 (throw
406 'found
407 (tree-widget-create-image
408 (car fmt) file
409 ;; Add the pointer shape
410 (cons :pointer
411 (cons
0cfce69f
DP
412 (or (cdr (assoc name tree-widget--cursors))
413 'hand)
f2cb69d5
DP
414 (tree-widget-image-properties file)))))))))
415 nil)))))
3212eb61
DP
416
417(defun tree-widget-find-image (name)
418 "Find the image with NAME in current theme.
419NAME is an image file name sans extension.
f2cb69d5 420Return the image found, or nil if not found."
3212eb61
DP
421 (when (tree-widget-use-image-p)
422 ;; Ensure there is an active theme.
423 (tree-widget-set-theme (tree-widget-theme-name))
f2cb69d5
DP
424 (let ((image (assoc name (aref tree-widget--theme 3))))
425 ;; The image NAME is found in the cache.
426 (if image
427 (cdr image)
428 ;; Search the image in current, and default themes.
429 (prog1
430 (setq image (tree-widget-lookup-image name))
431 ;; Store image reference in the cache for later use.
432 (push (cons name image) (aref tree-widget--theme 3))))
433 )))
3212eb61
DP
434\f
435;;; Widgets
436;;
437(defvar tree-widget-button-keymap
f2cb69d5
DP
438 (let ((km (make-sparse-keymap)))
439 (if (boundp 'widget-button-keymap)
440 ;; XEmacs
441 (progn
442 (set-keymap-parent km widget-button-keymap)
443 (define-key km [button1] 'widget-button-click))
444 ;; Emacs
445 (set-keymap-parent km widget-keymap)
446 (define-key km [down-mouse-1] 'widget-button-click))
447 km)
448 "Keymap used inside node buttons.
449Handle mouse button 1 click on buttons.")
3212eb61 450
0cfce69f
DP
451(define-widget 'tree-widget-icon 'push-button
452 "Basic widget other tree-widget icons are derived from."
3212eb61
DP
453 :format "%[%t%]"
454 :button-keymap tree-widget-button-keymap ; XEmacs
455 :keymap tree-widget-button-keymap ; Emacs
0cfce69f
DP
456 :create 'tree-widget-icon-create
457 :action 'tree-widget-icon-action
458 :help-echo 'tree-widget-icon-help-echo
3212eb61
DP
459 )
460
0cfce69f
DP
461(define-widget 'tree-widget-open-icon 'tree-widget-icon
462 "Icon for an expanded tree-widget node."
463 :tag "[-]"
464 :glyph-name "open"
3212eb61
DP
465 )
466
0cfce69f
DP
467(define-widget 'tree-widget-empty-icon 'tree-widget-icon
468 "Icon for an expanded tree-widget node with no child."
469 :tag "[X]"
470 :glyph-name "empty"
3212eb61
DP
471 )
472
0cfce69f
DP
473(define-widget 'tree-widget-close-icon 'tree-widget-icon
474 "Icon for a collapsed tree-widget node."
475 :tag "[+]"
476 :glyph-name "close"
3212eb61
DP
477 )
478
0cfce69f
DP
479(define-widget 'tree-widget-leaf-icon 'tree-widget-icon
480 "Icon for a tree-widget leaf node."
481 :tag ""
482 :glyph-name "leaf"
483 :button-face 'default
3212eb61
DP
484 )
485
486(define-widget 'tree-widget-guide 'item
f2cb69d5 487 "Vertical guide line."
3212eb61
DP
488 :tag " |"
489 ;;:tag-glyph (tree-widget-find-image "guide")
490 :format "%t"
491 )
492
493(define-widget 'tree-widget-end-guide 'item
f2cb69d5 494 "End of a vertical guide line."
3212eb61
DP
495 :tag " `"
496 ;;:tag-glyph (tree-widget-find-image "end-guide")
497 :format "%t"
498 )
499
500(define-widget 'tree-widget-no-guide 'item
f2cb69d5 501 "Invisible vertical guide line."
3212eb61
DP
502 :tag " "
503 ;;:tag-glyph (tree-widget-find-image "no-guide")
504 :format "%t"
505 )
506
507(define-widget 'tree-widget-handle 'item
f2cb69d5 508 "Horizontal guide line that joins a vertical guide line to a node."
0cfce69f 509 :tag "-"
3212eb61
DP
510 ;;:tag-glyph (tree-widget-find-image "handle")
511 :format "%t"
512 )
513
514(define-widget 'tree-widget-no-handle 'item
f2cb69d5 515 "Invisible handle."
3212eb61
DP
516 :tag " "
517 ;;:tag-glyph (tree-widget-find-image "no-handle")
518 :format "%t"
519 )
520
521(define-widget 'tree-widget 'default
522 "Tree widget."
523 :format "%v"
524 :convert-widget 'widget-types-convert-widget
525 :value-get 'widget-value-value-get
f2cb69d5 526 :value-delete 'widget-children-value-delete
3212eb61 527 :value-create 'tree-widget-value-create
0cfce69f
DP
528 :action 'tree-widget-action
529 :help-echo 'tree-widget-help-echo
530 :open-icon 'tree-widget-open-icon
531 :close-icon 'tree-widget-close-icon
532 :empty-icon 'tree-widget-empty-icon
533 :leaf-icon 'tree-widget-leaf-icon
f2cb69d5
DP
534 :guide 'tree-widget-guide
535 :end-guide 'tree-widget-end-guide
536 :no-guide 'tree-widget-no-guide
537 :handle 'tree-widget-handle
538 :no-handle 'tree-widget-no-handle
3212eb61
DP
539 )
540\f
541;;; Widget support functions
542;;
543(defun tree-widget-p (widget)
f2cb69d5 544 "Return non-nil if WIDGET is a tree-widget."
3212eb61
DP
545 (let ((type (widget-type widget)))
546 (while (and type (not (eq type 'tree-widget)))
547 (setq type (widget-type (get type 'widget-type))))
548 (eq type 'tree-widget)))
549
f2cb69d5
DP
550(defun tree-widget-node (widget)
551 "Return WIDGET's :node child widget.
552If not found, setup an `item' widget as default.
553Signal an error if the :node widget is a tree-widget.
554WIDGET is, or derives from, a tree-widget."
3212eb61 555 (let ((node (widget-get widget :node)))
f2cb69d5
DP
556 (if node
557 ;; Check that the :node widget is not a tree-widget.
558 (and (tree-widget-p node)
559 (error "Invalid tree-widget :node %S" node))
560 ;; Setup an item widget as default :node.
3212eb61
DP
561 (setq node `(item :tag ,(or (widget-get widget :tag)
562 (widget-princ-to-string
563 (widget-value widget)))))
564 (widget-put widget :node node))
565 node))
566
3212eb61 567(defun tree-widget-keep (arg widget)
f2cb69d5 568 "Save in ARG the WIDGET's properties specified by :keep."
3212eb61
DP
569 (dolist (prop (widget-get widget :keep))
570 (widget-put arg prop (widget-get widget prop))))
571
572(defun tree-widget-children-value-save (widget &optional args node)
573 "Save WIDGET children values.
f2cb69d5
DP
574WIDGET is, or derives from, a tree-widget.
575Children properties and values are saved in ARGS if non-nil, else in
576WIDGET's :args property value. Properties and values of the
577WIDGET's :node sub-widget are saved in NODE if non-nil, else in
578WIDGET's :node sub-widget."
579 (let ((args (cons (or node (widget-get widget :node))
580 (or args (widget-get widget :args))))
581 (children (widget-get widget :children))
3212eb61
DP
582 arg child)
583 (while (and args children)
584 (setq arg (car args)
585 args (cdr args)
586 child (car children)
587 children (cdr children))
588 (if (tree-widget-p child)
589;;;; The child is a tree node.
590 (progn
591 ;; Backtrack :args and :node properties.
592 (widget-put arg :args (widget-get child :args))
f2cb69d5 593 (widget-put arg :node (widget-get child :node))
3212eb61
DP
594 ;; Save :open property.
595 (widget-put arg :open (widget-get child :open))
596 ;; The node is open.
597 (when (widget-get child :open)
598 ;; Save the widget value.
599 (widget-put arg :value (widget-value child))
600 ;; Save properties specified in :keep.
601 (tree-widget-keep arg child)
602 ;; Save children.
603 (tree-widget-children-value-save
604 child (widget-get arg :args) (widget-get arg :node))))
605;;;; Another non tree node.
f2cb69d5 606 ;; Save the widget value.
3212eb61
DP
607 (widget-put arg :value (widget-value child))
608 ;; Save properties specified in :keep.
f2cb69d5 609 (tree-widget-keep arg child)))))
0cfce69f
DP
610\f
611;;; Widget creation
612;;
613(defvar tree-widget-before-create-icon-functions nil
614 "Hooks run before to create a tree-widget icon.
615Each function is passed the icon widget not yet created.
616The value of the icon widget :node property is a tree :node widget or
617a leaf node widget, not yet created.
618This hook can be used to dynamically change properties of the icon and
619associated node widgets. For example, to dynamically change the look
620and feel of the tree-widget by changing the values of the :tag
621and :glyph-name properties of the icon widget.
622This hook should be local in the buffer setup to display widgets.")
623
624(defun tree-widget-icon-create (icon)
625 "Create the ICON widget."
626 (run-hook-with-args 'tree-widget-before-create-icon-functions icon)
627 (widget-put icon :tag-glyph
628 (tree-widget-find-image (widget-get icon :glyph-name)))
629 ;; Ensure there is at least one char to display the image.
630 (and (widget-get icon :tag-glyph)
631 (equal "" (or (widget-get icon :tag) ""))
632 (widget-put icon :tag " "))
633 (widget-default-create icon)
634 ;; Insert space between the icon and the node widget.
635 (insert-char ? 1)
636 (put-text-property
637 (1- (point)) (point)
638 'display (list 'space :width tree-widget-space-width)))
3212eb61 639
3212eb61 640(defun tree-widget-value-create (tree)
f2cb69d5
DP
641 "Create the TREE tree-widget."
642 (let* ((node (tree-widget-node tree))
643 (flags (widget-get tree :tree-widget--guide-flags))
61194c8d 644 (indent (widget-get tree :indent))
f2cb69d5
DP
645 ;; Setup widget's image support. Looking up for images, and
646 ;; setting widgets' :tag-glyph is done here, to allow to
647 ;; dynamically change the image theme.
648 (widget-image-enable (tree-widget-use-image-p)) ; Emacs
649 (widget-glyph-enable widget-image-enable) ; XEmacs
3212eb61 650 children buttons)
66c20a82 651 (and indent (not (widget-get tree :parent))
61194c8d 652 (insert-char ?\ indent))
3212eb61 653 (if (widget-get tree :open)
f2cb69d5 654;;;; Expanded node.
3bcf793c 655 (let ((args (widget-get tree :args))
f2cb69d5
DP
656 (xpandr (or (widget-get tree :expander)
657 (widget-get tree :dynargs)))
f2cb69d5
DP
658 (guide (widget-get tree :guide))
659 (noguide (widget-get tree :no-guide))
660 (endguide (widget-get tree :end-guide))
661 (handle (widget-get tree :handle))
662 (nohandle (widget-get tree :no-handle))
3bcf793c
DP
663 (guidi (tree-widget-find-image "guide"))
664 (noguidi (tree-widget-find-image "no-guide"))
665 (endguidi (tree-widget-find-image "end-guide"))
666 (handli (tree-widget-find-image "handle"))
0cfce69f 667 (nohandli (tree-widget-find-image "no-handle")))
f2cb69d5
DP
668 ;; Request children at run time, when not already done.
669 (when (and (not args) xpandr)
670 (setq args (mapcar 'widget-convert (funcall xpandr tree)))
671 (widget-put tree :args args))
4ff094a8
DP
672 ;; Defer the node widget creation after icon creation.
673 (widget-put tree :node (widget-convert node))
0cfce69f 674 ;; Create the icon widget for the expanded tree.
3212eb61 675 (push (widget-create-child-and-convert
38d0d987
DP
676 tree (widget-get tree (if args :open-icon :empty-icon))
677 ;; Pass the node widget to child.
678 :node (widget-get tree :node))
3212eb61 679 buttons)
0cfce69f 680 ;; Create the tree node widget.
4ff094a8
DP
681 (push (widget-create-child tree (widget-get tree :node))
682 children)
0cfce69f
DP
683 ;; Update the icon :node with the created node widget.
684 (widget-put (car buttons) :node (car children))
685 ;; Create the tree children.
3212eb61 686 (while args
0cfce69f
DP
687 (setq node (car args)
688 args (cdr args))
3bcf793c 689 (and indent (insert-char ?\ indent))
f2cb69d5 690 ;; Insert guide lines elements from previous levels.
3bcf793c 691 (dolist (f (reverse flags))
3212eb61
DP
692 (widget-create-child-and-convert
693 tree (if f guide noguide)
694 :tag-glyph (if f guidi noguidi))
695 (widget-create-child-and-convert
f2cb69d5
DP
696 tree nohandle :tag-glyph nohandli))
697 ;; Insert guide line element for this level.
3212eb61
DP
698 (widget-create-child-and-convert
699 tree (if args guide endguide)
700 :tag-glyph (if args guidi endguidi))
701 ;; Insert the node handle line
702 (widget-create-child-and-convert
703 tree handle :tag-glyph handli)
0cfce69f
DP
704 (if (tree-widget-p node)
705 ;; Create a sub-tree node.
706 (push (widget-create-child-and-convert
707 tree node :tree-widget--guide-flags
708 (cons (if args t) flags))
709 children)
710 ;; Create the icon widget for a leaf node.
3212eb61 711 (push (widget-create-child-and-convert
0cfce69f
DP
712 tree (widget-get tree :leaf-icon)
713 ;; At this point the node widget isn't yet created.
714 :node (setq node (widget-convert
715 node :tree-widget--guide-flags
716 (cons (if args t) flags)))
717 :tree-widget--leaf-flag t)
718 buttons)
719 ;; Create the leaf node widget.
720 (push (widget-create-child tree node) children)
721 ;; Update the icon :node with the created node widget.
722 (widget-put (car buttons) :node (car children)))))
f2cb69d5 723;;;; Collapsed node.
4ff094a8
DP
724 ;; Defer the node widget creation after icon creation.
725 (widget-put tree :node (widget-convert node))
0cfce69f 726 ;; Create the icon widget for the collapsed tree.
3212eb61 727 (push (widget-create-child-and-convert
38d0d987
DP
728 tree (widget-get tree :close-icon)
729 ;; Pass the node widget to child.
730 :node (widget-get tree :node))
3212eb61 731 buttons)
0cfce69f 732 ;; Create the tree node widget.
4ff094a8
DP
733 (push (widget-create-child tree (widget-get tree :node))
734 children)
0cfce69f
DP
735 ;; Update the icon :node with the created node widget.
736 (widget-put (car buttons) :node (car children)))
737 ;; Save widget children and buttons. The tree-widget :node child
738 ;; is the first element in :children.
3212eb61 739 (widget-put tree :children (nreverse children))
0cfce69f
DP
740 (widget-put tree :buttons buttons)))
741\f
742;;; Widget callbacks
743;;
744(defsubst tree-widget-leaf-node-icon-p (icon)
745 "Return non-nil if ICON is a leaf node icon.
746That is, if its :node property value is a leaf node widget."
747 (widget-get icon :tree-widget--leaf-flag))
748
749(defun tree-widget-icon-action (icon &optional event)
750 "Handle the ICON widget :action.
751If ICON :node is a leaf node it handles the :action. The tree-widget
752parent of ICON handles the :action otherwise.
753Pass the received EVENT to :action."
754 (let ((node (widget-get icon (if (tree-widget-leaf-node-icon-p icon)
755 :node :parent))))
756 (widget-apply node :action event)))
757
758(defun tree-widget-icon-help-echo (icon)
759 "Return the help-echo string of ICON.
760If ICON :node is a leaf node it handles the :help-echo. The tree-widget
761parent of ICON handles the :help-echo otherwise."
762 (let* ((node (widget-get icon (if (tree-widget-leaf-node-icon-p icon)
763 :node :parent)))
764 (help-echo (widget-get node :help-echo)))
765 (if (functionp help-echo)
766 (funcall help-echo node)
767 help-echo)))
768
769(defvar tree-widget-after-toggle-functions nil
770 "Hooks run after toggling a tree-widget expansion.
771Each function is passed a tree-widget. If the value of the :open
772property is non-nil the tree has been expanded, else collapsed.
773This hook should be local in the buffer setup to display widgets.")
774
775(defun tree-widget-action (tree &optional event)
776 "Handle the :action of the TREE tree-widget.
777That is, toggle expansion of the TREE tree-widget.
778Ignore the EVENT argument."
779 (let ((open (not (widget-get tree :open))))
780 (or open
781 ;; Before to collapse the node, save children values so next
782 ;; open can recover them.
783 (tree-widget-children-value-save tree))
784 (widget-put tree :open open)
785 (widget-value-set tree open)
786 (run-hook-with-args 'tree-widget-after-toggle-functions tree)))
787
788(defun tree-widget-help-echo (tree)
789 "Return the help-echo string of the TREE tree-widget."
790 (if (widget-get tree :open)
791 "Collapse node"
792 "Expand node"))
3212eb61
DP
793
794(provide 'tree-widget)
795
f2cb69d5 796;; arch-tag: c3a1ada2-1663-41dc-9d16-2479ed8320e8
3212eb61 797;;; tree-widget.el ends here