Skip to content
Andrej Vodopivec edited this page Mar 19, 2015 · 3 revisions

A treeview is used to display data in a tree. Each item in the tree can have additional data shown in separate columns. The columns are specified using the :columns configuration option. The tree is in the column names "#0". How the tree and data are displayed is specified using the :show configuration option (a list which can contain "tree" and/or "headings").

Each treeview has the root item which has the id "" and is not displayed. All items in the treeview are children of other items. Toplevel items are children of the root item "".

Functions implemented for treeview:

  • (treeview-bbox tw item &optional column)
  • (treeview-children tw item)
  • (treeview-column-id tw col)
  • (treeview-column-width tw col), is setf-iable
  • (treeview-column-stretch tw col), is setf-iable
  • (treeview-column-minwidth tw col), is setf-iable
  • (treeview-column-anchor tw col), is setf-iable
  • (treeview-heading-command tw heading fun), when heading is clicked, call fun
  • (treeview-delete tw items)
  • (treeview-detach tw items)
  • (treeview-exists tw item)
  • (treeview-focus tw), is setf-iable, used to focus items
  • (treeview-heading-text tw col), is setf-iable
  • (treeview-heading-image tw col), is setf-iable
  • (treeview-heading-anchor tw col), is setf-fiable
  • (treeview-insert tw parent index &key id text image values open tags), returns the id of the new item
  • (treeview-item-id tw item)
  • (treeview-item-text tw item), is setf-ifable
  • (treeview-item-values tw item), is setf-ifable
  • (treeview-move tw item parent index)
  • (treeview-next tw item)
  • (treeview-parent tw item)
  • (treeview-prev tw item)
  • (treeview-get tw item col)
  • (treeview-set tw item col &optional val)
  • (treeview-selection tw)
  • (treeview-selection-set tw indxs)
  • (treeview-selection-add tw idxs)
  • (treeview-selection-remove tw idxs)
  • (treeview-selection-toggle tw idxs)

Treeview events:

  • <<TrewviewSelect>>
  • <<TreeviewOpen>>
  • <<TreeviewClose>>

Example:

Treeview

(defpackage :tk-user
  (:use :cl :tk)
  (:export :main))

(in-package :tk-user)

(defun main ()
  (with-tk-root (root)
    (setf (window-title root) "Treeview")

    (let* ((f (frame :parent root))
           (var (string-var))
           (tw (treeview :parent f :columns (list "col1" "col2"))))

      (pack f :expand t :fill "both")

      (setf (treeview-heading-text tw "#0") "Tree")
      (setf (treeview-column-width tw "col1") 150)
      (setf (treeview-column-width tw "col2") 150)
      (setf (treeview-heading-text tw "col1") "Column A")
      (setf (treeview-heading-text tw "col2") "Column B")
      (setf (treeview-column-anchor tw "col1") "center")
      (setf (treeview-column-anchor tw "col2") "center")

      (let ((id (treeview-insert tw "" 0 :text "Line 1" :values '("A" "B"))))
        (treeview-insert tw id 0 :text "Line 2" :values '("A1" "B1"))
        (treeview-insert tw id "end" :text "Line 3" :values '("A2")))
      (let ((id (treeview-insert tw "" 1 :text "Line 4")))
        (treeview-insert tw id 0 :values '("C" "D"))
        (treeview-selection-set tw id))

      (pack tw :expand t :fill "both")

      (pack (label :parent f :textvariable var)
            :padx 5 :pady 5 :fill "x")

      (treeview-heading-command tw "col1"
                                (lambda ()
                                  (message-box "Heading clicked" :detail "col1")))
      
      (bind-event tw "<<TreeviewSelect>>"
                  (lambda (ev)
                    (declare (ignore ev))
                    (let ((id (car (treeview-selection tw))))
                      (setf (var-value var)
                            (format nil "Selection: ID: ~a, TEXT: ~a, VALUES: (~{~a~,})"
                                    id
                                    (treeview-item-text tw id)
                                    (treeview-item-values tw id)))))))))

Tcl/Tk documentation for treeview.

Clone this wiki locally