+(defbinding %tree-path-free () nil
+ (location pointer))
+
+(defbinding %tree-path-get-indices () pointer
+ (location pointer))
+
+(defbinding %tree-path-get-depth () int
+ (location pointer))
+
+(defun %make-tree-path (path)
+ (let ((c-vector (make-c-vector 'int (length path) :content path))
+ (location (allocate-memory (+ (size-of 'int) (size-of 'pointer)))))
+ (funcall (writer-function 'int) (length path) location)
+ (funcall (writer-function 'pointer) c-vector location (size-of 'int))
+ location))
+
+(defun %tree-path-to-vector (location)
+ (let ((indices (%tree-path-get-indices location))
+ (depth (%tree-path-get-depth location)))
+ (if (null-pointer-p indices)
+ #()
+ (map-c-vector 'vector #'identity indices 'int depth))))
+
+(defmacro %with-tree-path ((var path) &body body)
+ (let ((vector-offset (+ (size-of 'int) (size-of 'pointer))))
+ `(with-memory (,var (+ ,(size-of 'int) ,(size-of 'pointer) (* ,(size-of 'int) (length ,path))))
+ (funcall (writer-function 'int) (length ,path) ,var)
+ (setf (ref-pointer ,var ,(size-of 'int)) (pointer+ ,var ,vector-offset))
+ (make-c-vector 'int (length ,path) :content ,path :location (pointer+ ,var ,vector-offset))
+ ,@body)))
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+ (define-type-method alien-type ((type tree-path))
+ (declare (ignore type))
+ (alien-type 'pointer))
+
+ (define-type-method size-of ((type tree-path) &key inlined)
+ (assert-not-inlined type inlined)
+ (size-of 'pointer))
+
+ (define-type-method alien-arg-wrapper ((type tree-path) var path style form &optional copy-in-p)
+ (declare (ignore type))
+ (cond
+ ((and (in-arg-p style) copy-in-p)
+ `(with-pointer (,var (%make-tree-path ,path))
+ ,form))
+ ((and (in-arg-p style) (not (out-arg-p style)))
+ `(%with-tree-path (,var ,path)
+ ,form))
+ ((and (in-arg-p style) (out-arg-p style))
+ (let ((tree-path (make-symbol "SYMBOL")))
+ `(%with-tree-path (,tree-path ,path)
+ (with-pointer (,var ,tree-path)
+ ,form))))
+ ((and (out-arg-p style) (not (in-arg-p style)))
+ `(with-pointer (,var)
+ ,form))))
+
+ (define-type-method to-alien-form ((type tree-path) path &optional copy-p)
+ (declare (ignore type copy-p))
+ `(%make-tree-path ,path))
+
+ (define-type-method from-alien-form ((type tree-path) location &key (ref :free))
+ (declare (ignore type))
+ `(prog1
+ (%tree-path-to-vector ,location)
+ ,(when (eq ref :free)
+ `(%tree-path-free ,location)))))
+
+(define-type-method to-alien-function ((type tree-path) &optional copy-p)
+ (declare (ignore type))
+ #'%make-tree-path
+ (unless copy-p
+ #'(lambda (tree-path location)
+ (declare (ignore tree-path))
+ (%tree-path-free location))))
+
+(define-type-method from-alien-function ((type tree-path) &key (ref :free))
+ (declare (ignore type))
+ (if (eq ref :free)
+ #'(lambda (location)
+ (prog1
+ (%tree-path-to-vector location)
+ (%tree-path-free location)))
+ #'(lambda (location)
+ (%tree-path-to-vector location))))
+
+(define-type-method writer-function ((type tree-path) &key temp inlined)
+ (declare (ignore temp))
+ (assert-not-inlined type inlined)
+ (let ((writer (writer-function 'pointer)))
+ #'(lambda (path location &optional (offset 0))
+ (funcall writer (%make-tree-path path) location offset))))
+
+(define-type-method reader-function ((type tree-path) &key ref inlined)
+ (declare (ignore ref))
+ (assert-not-inlined type inlined)
+ #'(lambda (location &optional (offset 0))
+ (%tree-path-to-vector (ref-pointer location offset))))
+
+(define-type-method destroy-function ((type tree-path) &key temp inlined)
+ (declare (ignore temp))
+ (assert-not-inlined type inlined)
+ #'(lambda (location &optional (offset 0))
+ (%tree-path-free (ref-pointer location offset))))
+
+
+(defbinding %tree-row-reference-new () pointer
+ (model tree-model)
+ (path tree-path))
+
+(defmethod allocate-foreign ((reference tree-row-reference) &key model path)
+ (%tree-row-reference-new model path))
+
+(defbinding tree-row-reference-get-path () tree-path
+ (reference tree-row-reference))
+
+(defbinding (tree-row-reference-valid-p "gtk_tree_row_reference_valid") () boolean
+ (reference tree-row-reference))
+
+
+(defbinding tree-model-get-column-type () gtype ;type-number
+ (tree-model tree-model)
+ (index int))
+
+(defbinding tree-model-get-iter
+ (model path &optional (iter (make-instance 'tree-iter))) boolean
+ (model tree-model)
+ (iter tree-iter :in/return)
+ (path tree-path))
+
+(defbinding tree-model-get-path () tree-path
+ (tree-model tree-model)
+ (iter tree-iter))
+
+(defbinding %tree-model-get-value () nil
+ (tree-model tree-model)
+ (iter tree-iter)
+ (column int)
+ (gvalue gvalue))
+
+(defgeneric tree-model-value (model row column))
+
+(defmethod tree-model-value ((model tree-model) row column)
+ (let ((index (column-index model column))
+ (iter (etypecase row
+ (tree-iter row)
+ (tree-path (multiple-value-bind (valid iter)
+ (tree-model-get-iter model row)
+ (if valid
+ iter
+ (error "Invalid tree path: ~A" row)))))))
+ (with-gvalue (gvalue)
+ (%tree-model-get-value model iter index gvalue))))
+
+(defbinding tree-model-iter-next () boolean
+ (tree-model tree-model)
+ (iter tree-iter :in/return))
+
+(defbinding tree-model-iter-children
+ (tree-model parent &optional (iter (make-instance 'tree-iter))) boolean
+ (tree-model tree-model)
+ (iter tree-iter :in/return)
+ (parent (or null tree-iter)))
+
+(defbinding (tree-model-iter-has-child-p "gtk_tree_model_iter_has_child")
+ () boolean
+ (tree-model tree-model)
+ (iter tree-iter))
+
+(defbinding tree-model-iter-n-children () int
+ (tree-model tree-model)
+ (iter tree-iter))
+
+(defbinding tree-model-iter-nth-child
+ (tree-model parent n &optional (iter (make-instance 'tree-iter))) boolean
+ (tree-model tree-model)
+ (iter tree-iter :in/return)
+ (parent (or null tree-iter))
+ (n int))
+
+(defbinding tree-model-iter-parent
+ (tree-model child &optional (iter (make-instance 'tree-iter))) boolean
+ (tree-model tree-model)
+ (iter tree-iter :in/return)
+ (child tree-iter))
+
+(define-callback-marshal %tree-model-foreach-callback boolean
+ (tree-model tree-path tree-iter))
+
+(defbinding %tree-model-foreach (tree-model callback-id) nil
+ (tree-model tree-model)
+ (%tree-model-foreach-callback callback)
+ (callback-id unsigned-int))
+
+(defun tree-model-foreach (model function)
+ (with-callback-function (id function)
+ (%tree-model-foreach model id)))
+
+(defbinding tree-model-row-changed () nil
+ (tree-model tree-model)
+ (path tree-path)
+ (iter tree-iter))
+
+(defbinding tree-model-row-inserted () nil
+ (tree-model tree-model)
+ (path tree-path)
+ (iter tree-iter))
+
+(defbinding tree-model-row-has-child-toggled () nil
+ (tree-model tree-model)
+ (path tree-path)
+ (iter tree-iter))
+
+(defbinding tree-model-row-deleted () nil
+ (tree-model tree-model)
+ (path tree-path)
+ (iter tree-iter))
+
+(defbinding tree-model-rows-reordered () nil
+ (tree-model tree-model)
+ (path tree-path)
+ (iter tree-iter)
+ (new-order int))
+
+
+(defun column-types (model columns)
+ (declare (ignore model))
+ (map 'vector
+ #'(lambda (column)
+ (find-type-number (first (mklist column))))
+ columns))
+
+(defun column-index (model column)
+ (or
+ (etypecase column
+ (number column)
+ (symbol (position column (user-data model 'column-names)))
+ (string (position column (user-data model 'column-names)
+ :test #'string=)))
+ (error "~A has no column ~S" model column)))
+
+(defun column-name (model index)
+ (svref (user-data model 'column-names) index))
+
+(defun tree-model-column-value-setter (model column)
+ (let ((setters (or
+ (user-data model 'column-setters)
+ (setf
+ (user-data model 'column-setters)
+ (make-array (tree-model-n-columns model)
+ :initial-element nil)))))
+ (let ((index (column-index model column)))
+ (or
+ (svref setters index)
+ (setf
+ (svref setters index)
+ (let ((setter
+ (mkbinding (column-setter-name model)
+ nil (type-of model) 'tree-iter 'int
+ (tree-model-get-column-type model index)
+ 'int)))
+ #'(lambda (value iter)
+ (funcall setter model iter index value -1))))))))
+
+(defun tree-model-row-setter (model)
+ (or
+ (user-data model 'row-setter)
+ (progn
+ ;; This will create any missing column setter
+ (loop
+ for i from 0 below (tree-model-n-columns model)
+ do (tree-model-column-value-setter model i))
+ (let ((setters (user-data model 'column-setters)))
+ (setf
+ (user-data model 'row-setter)
+ #'(lambda (row iter)
+ (map nil #'(lambda (value setter)
+ (funcall setter value iter))
+ row setters)))))))
+
+(defgeneric (setf tree-model-value) (value model row column))
+
+(defmethod (setf tree-model-value) (value (model tree-model) row column)
+ (let ((iter (etypecase row
+ (tree-iter row)
+ (tree-path (multiple-value-bind (valid iter)
+ (tree-model-get-iter model row)
+ (if valid
+ iter
+ (error "Invalid tree path: ~A" row)))))))
+ (funcall (tree-model-column-value-setter model column) value iter)
+ value))
+
+(defun (setf tree-model-row-data) (data model iter)
+ (funcall (tree-model-row-setter model) data iter)
+ data)
+
+(defun %tree-model-set (model iter data)
+ (etypecase data
+ (vector (setf (tree-model-row-data model iter) data))
+ (cons
+ (loop
+ as (column value . rest) = data then rest
+ do (setf (tree-model-value model iter column) value)
+ while rest))))
+
+
+;;; Tree Selection
+
+(define-callback-marshal %tree-selection-callback boolean
+ (tree-selection tree-model tree-path (path-currently-selected boolean)))
+
+(defbinding tree-selection-set-select-function (selection function) nil
+ (selection tree-selection)
+ (%tree-selection-callback callback)
+ ((register-callback-function function) unsigned-int)
+ (user-data-destroy-callback callback))
+
+(defbinding tree-selection-get-selected
+ (selection &optional (iter (make-instance 'tree-iter))) boolean
+ (selection tree-selection)
+ (nil null)
+ (iter tree-iter :in/return))
+
+(define-callback-marshal %tree-selection-foreach-callback nil (tree-model tree-path tree-iter))
+
+(defbinding %tree-selection-selected-foreach (tree-selection callback-id) nil
+ (tree-selection tree-selection)
+ (%tree-selection-foreach-callback callback)
+ (callback-id unsigned-int))
+
+(defun tree-selection-selected-foreach (selection function)
+ (with-callback-function (id function)
+ (%tree-selection-selected-foreach selection id)))
+
+(defbinding tree-selection-get-selected-rows () (glist tree-path)
+ (tree-selection tree-selection)
+ (nil null))
+
+(defbinding tree-selection-count-selected-rows () int
+ (tree-selection tree-selection))
+
+(defbinding %tree-selection-select-path () nil
+ (tree-selection tree-selection)
+ (tree-path tree-path))
+
+(defbinding %tree-selection-unselect-path () nil
+ (tree-selection tree-selection)
+ (tree-path tree-path))
+
+(defbinding %tree-selection-path-is-selected () boolean
+ (tree-selection tree-selection)
+ (tree-path tree-path))
+
+(defbinding %tree-selection-select-iter () nil
+ (tree-selection tree-selection)
+ (tree-path tree-path))
+
+(defbinding %tree-selection-unselect-iter () nil
+ (tree-selection tree-selection)
+ (tree-path tree-path))
+
+(defbinding %tree-selection-iter-is-selected () boolean
+ (tree-selection tree-selection)
+ (tree-path tree-path))
+
+(defun tree-selection-select (selection row)
+ (etypecase row
+ (tree-path (%tree-selection-select-path selection row))
+ (tree-iter (%tree-selection-select-iter selection row))))
+
+(defun tree-selection-unselect (selection row)
+ (etypecase row
+ (tree-path (%tree-selection-unselect-path selection row))
+ (tree-iter (%tree-selection-unselect-iter selection row))))
+
+(defun tree-selection-is-selected-p (selection row)
+ (etypecase row
+ (tree-path (%tree-selection-path-is-selected selection row))
+ (tree-iter (%tree-selection-iter-is-selected selection row))))
+
+(defbinding tree-selection-select-all () nil
+ (tree-selection tree-selection))
+
+(defbinding tree-selection-unselect-all () nil
+ (tree-selection tree-selection))
+
+(defbinding tree-selection-select-range () nil
+ (tree-selection tree-selection)
+ (start tree-path)
+ (end tree-path))
+
+(defbinding tree-selection-unselect-range () nil
+ (tree-selection tree-selection)
+ (start tree-path)
+ (end tree-path))
+
+
+;;; Tree Sortable
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+ (define-enum-type sort-column (:default -1) (:unsorted -2))
+ (define-enum-type sort-order (:before -1) (:equal 0) (:after 1)))
+
+
+(define-callback-marshal %tree-iter-compare-callback (or int sort-order)
+ (tree-model (a tree-iter) (b tree-iter)))
+
+(defbinding tree-sortable-sort-column-changed () nil
+ (sortable tree-sortable))
+
+(defbinding %tree-sortable-get-sort-column-id () boolean
+ (sortable tree-sortable)
+ (column int :out)
+ (order sort-type :out))
+
+(defun tree-sortable-get-sort-column (sortable)
+ (multiple-value-bind (special-p column order)
+ (%tree-sortable-get-sort-column-id sortable)
+ (values
+ (if special-p
+ (int-to-sort-order column)
+ (column-name sortable column))
+ order)))
+
+(defbinding (tree-sortable-set-sort-column
+ "gtk_tree_sortable_set_sort_column_id")
+ (sortable column order) nil
+ (sortable tree-sortable)
+ ((etypecase column
+ ((or integer sort-column) column)
+ (symbol (column-index sortable column)))
+ (or sort-column int))
+ (order sort-type))
+
+(defbinding %tree-sortable-set-sort-func (sortable column function) nil
+ (sortable tree-sortable)
+ ((column-index sortable column) int)
+ (%tree-iter-compare-callback callback)
+ ((register-callback-function function) unsigned-int)
+ (user-data-destroy-callback callback))
+
+(defbinding %tree-sortable-set-default-sort-func () nil
+ (sortable tree-sortable)
+ (compare-func (or null callback))
+ (callback-id unsigned-int)
+ (destroy-func (or null callback)))
+
+(defun tree-sortable-set-sort-func (sortable column function)
+ "Sets the comparison function used when sorting to be FUNCTION. If
+the current sort column of SORTABLE is the same as COLUMN,
+then the model will sort using this function."
+ (cond
+ ((and (eq column :default) (not function))
+ (%tree-sortable-set-default-sort-func sortable nil 0 nil))
+ ((eq column :default)
+ (%tree-sortable-set-default-sort-func sortable
+ %tree-iter-compare-callback
+ (register-callback-function function)
+ user-data-destroy-callback))
+ ((%tree-sortable-set-sort-func sortable column function))))
+
+(defbinding tree-sortable-has-default-sort-func-p () boolean
+ (sortable tree-sortable))