3 (define-vtable ("GtkTreeModel" c-gtk-tree-model)
4 (:skip parent-instance g-type-interface)
6 (:skip tree-model-row-changed :pointer)
7 (:skip tree-model-row-inserted :pointer)
8 (:skip tree-model-row-has-child-toggled :pointer)
9 (:skip tree-model-row-deleted :pointer)
10 (:skip tree-model-rows-reordered :pointer)
12 (tree-model-get-flags-impl tree-model-get-flags-cb tree-model-flags (tree-model g-object))
13 (tree-model-get-n-columns-impl tree-model-get-n-columns-cb :int (tree-model g-object))
14 (tree-model-get-column-type-impl tree-model-get-column-type-cb g-type-designator (tree-model g-object) (index :int))
15 (tree-model-get-iter-impl tree-model-get-iter-cb :boolean (tree-model g-object) (iter (g-boxed-foreign tree-iter)) (path (g-boxed-foreign tree-path)))
16 (tree-model-get-path-impl tree-model-get-path-cb (g-boxed-foreign tree-path :return) (tree-model g-object) (iter (g-boxed-foreign tree-iter)))
17 (tree-model-get-value-impl tree-model-get-value-cb :void (tree-model g-object) (iter (g-boxed-foreign tree-iter)) (n :int) (value (:pointer g-value)))
18 (tree-model-iter-next-impl tree-model-iter-next-cb :boolean (tree-model g-object) (iter (g-boxed-foreign tree-iter)))
19 (tree-model-iter-children-impl tree-model-iter-children-cb :boolean (tree-model g-object) (iter (g-boxed-foreign tree-iter)) (parent (g-boxed-foreign tree-iter)))
20 (tree-model-iter-has-child-impl tree-model-iter-has-child-cb :boolean (tree-model g-object) (iter (g-boxed-foreign tree-iter)))
21 (tree-model-iter-n-children-impl tree-model-iter-n-children-cb :int (tree-model g-object) (iter (g-boxed-foreign tree-iter)))
22 (tree-model-iter-nth-child-impl tree-model-iter-nth-child-cb :boolean (tree-model g-object) (iter (g-boxed-foreign tree-iter)) (parent (g-boxed-foreign tree-iter)) (n :int))
23 (tree-model-iter-parent-impl tree-model-iter-parent-cb :boolean (tree-model g-object) (iter (g-boxed-foreign tree-iter)) (child (g-boxed-foreign tree-iter)))
24 (tree-model-ref-node-impl tree-model-ref-node-cb :void (tree-model g-object) (iter (g-boxed-foreign tree-iter)))
25 (tree-model-unref-node-impl tree-model-unref-node-cb :void (tree-model g-object) (iter (g-boxed-foreign tree-iter))))
27 ; TODO: GtkTreeSortable
29 ; TODO: GtkTreeModelSort
31 ; TODO: GtkTreeModelFilter
34 (defclass array-list-store (tree-model)
35 ((items :initform (make-array 0 :adjustable t :fill-pointer t) :reader store-items)
36 (columns-getters :initform (make-array 0 :adjustable t :fill-pointer t) :reader store-getters)
37 (columns-types :initform (make-array 0 :adjustable t :fill-pointer t) :reader store-types))
38 (:metaclass gobject-class)
39 (:g-type-name . "LispArrayListStore"))
41 (export 'array-list-store)
43 (register-object-type-implementation "LispArrayListStore" array-list-store "GObject" ("GtkTreeModel") nil)
45 (defun store-items-count (store)
46 (length (store-items store)))
48 (export 'store-items-count)
50 (defun store-item (store index)
51 (aref (store-items store) index))
55 (defun store-add-item (store item)
56 (vector-push-extend item (store-items store))
57 (let* ((path (make-instance 'tree-path))
58 (iter (make-tree-iter)))
59 (setf (tree-path-indices path) (list (1- (length (store-items store)))))
60 (setf (tree-iter-stamp iter) 0 (tree-iter-user-data iter) (1- (length (store-items store))))
61 (emit-signal store "row-inserted" path iter)))
63 (export 'store-add-item)
65 (defun store-remove-item (store item &key (test 'eq))
66 (with-slots (items) store
67 (let ((index (position item items :test test)))
68 (unless index (error "No such item~%~A~%in list-store~%~A" item store))
69 (setf items (delete item items :test test))
70 (let ((path (make-instance 'tree-path)))
71 (setf (tree-path-indices path) (list index))
72 (emit-signal store "row-deleted" path)))))
74 (export 'store-remove-item)
76 (defun store-add-column (store type getter)
77 (vector-push-extend type (store-types store))
78 (vector-push-extend getter (store-getters store))
79 (1- (length (store-types store))))
81 (export 'store-add-column)
83 (defmethod tree-model-get-flags-impl ((model array-list-store))
86 (defmethod tree-model-get-n-columns-impl ((model array-list-store))
87 (length (store-types model)))
89 (defmethod tree-model-get-column-type-impl ((tree-model array-list-store) index)
90 (aref (store-types tree-model) index))
92 (defmethod tree-model-get-iter-impl ((model array-list-store) iter path)
93 (let ((indices (tree-path-indices path)))
94 (when (and (= 1 (length indices))
95 (< (first indices) (length (store-items model))))
96 (setf (tree-iter-stamp iter) 0 (tree-iter-user-data iter) (first indices))
99 (defmethod tree-model-ref-node-impl ((model array-list-store) iter)
100 (declare (ignorable model iter)))
101 (defmethod tree-model-unref-node-impl ((model array-list-store) iter)
102 (declare (ignorable model iter)))
104 (defmethod tree-model-iter-next-impl ((model array-list-store) iter)
105 (let ((n (tree-iter-user-data iter)))
106 (when (< n (1- (length (store-items model))))
107 (setf (tree-iter-user-data iter) (1+ n))
110 (defmethod tree-model-iter-nth-child-impl ((model array-list-store) iter parent n)
111 (declare (ignorable parent))
112 (setf (tree-iter-stamp iter) 0
113 (tree-iter-user-data iter) n)
116 (defmethod tree-model-iter-n-children-impl ((model array-list-store) iter)
118 (length (store-items model))
121 (defmethod tree-model-get-path-impl ((model array-list-store) iter)
122 (let ((path (make-instance 'tree-path)))
123 (setf (tree-path-indices path) (list (tree-iter-user-data iter)))
126 (defmethod tree-model-iter-has-child-impl ((model array-list-store) iter)
127 (declare (ignorable iter))
130 (defgeneric tree-model-item (model iter-or-path))
132 (defmethod tree-model-item ((model array-list-store) (iter tree-iter))
133 (let ((n-row (tree-iter-user-data iter)))
134 (aref (store-items model) n-row)))
136 (defmethod tree-model-item ((model array-list-store) (path tree-path))
137 (let ((n-row (first (tree-path-indices path))))
138 (aref (store-items model) n-row)))
140 (export 'tree-model-item)
142 (defmethod tree-model-get-value-impl ((model array-list-store) iter n value)
143 (let ((n-row (tree-iter-user-data iter)))
145 (funcall (aref (store-getters model) n)
146 (aref (store-items model) n-row))
147 (aref (store-types model) n))))
149 (defcfun (tree-model-flags "gtk_tree_model_get_flags") tree-model-flags
150 (tree-model g-object))
152 (export 'tree-model-flags)
154 (defcfun (tree-model-n-columns "gtk_tree_model_get_n_columns") :int
155 (tree-model g-object))
157 (export 'tree-model-flags)
159 (defcfun (tree-model-column-type "gtk_tree_model_get_column_type") g-type-designator
160 (tree-model g-object)
163 (export 'tree-model-column-type)
165 (defcfun (tree-model-set-iter-to-path "gtk_tree_model_get_iter") :boolean
166 (tree-model g-object)
167 (iter (g-boxed-foreign tree-iter))
168 (path (g-boxed-foreign tree-path)))
170 (defun tree-model-iter-by-path (tree-model tree-path)
171 (let ((iter (make-tree-iter)))
172 (if (tree-model-set-iter-to-path tree-model iter tree-path)
176 (export 'tree-model-iter-by-path)
178 (defcfun (tree-model-set-iter-from-string "gtk_tree_model_get_iter_from_string") :boolean
179 (tree-model g-object)
180 (iter (g-boxed-foreign tree-iter))
181 (path-string :string))
183 (defun tree-model-iter-from-string (tree-model path-string)
184 (let ((iter (make-tree-iter)))
185 (if (tree-model-set-iter-from-string tree-model iter path-string)
189 (export 'tree-model-iter-from-string)
191 (defcfun (tree-model-set-iter-to-first "gtk_tree_model_get_iter_first") :boolean
193 (iter (g-boxed-foreign tree-iter)))
195 (defun tree-model-iter-first (tree-model)
196 (let ((iter (make-tree-iter)))
197 (if (tree-model-set-iter-to-first tree-model iter)
201 (export 'tree-model-iter-first)
203 (defcfun (tree-model-path "gtk_tree_model_get_path") (g-boxed-foreign tree-path :return)
204 (tree-model g-object)
205 (iter (g-boxed-foreign tree-iter)))
207 (export 'tree-model-path)
209 (defcfun gtk-tree-model-get-value :void
211 (iter (g-boxed-foreign tree-iter))
213 (value (:pointer g-value)))
215 (defun tree-model-value (tree-model iter column)
216 (with-foreign-object (v 'g-value)
218 (gtk-tree-model-get-value tree-model iter column v)
219 (prog1 (parse-g-value v)
222 (export 'tree-model-value)
224 (defcfun (tree-model-iter-next "gtk_tree_model_iter_next") :boolean
225 (tree-model g-object)
226 (iter (g-boxed-foreign tree-iter)))
228 (export 'tree-model-iter-next)
230 (defcfun gtk-tree-model-iter-children :boolean
231 (tree-model g-object)
232 (iter (g-boxed-foreign tree-iter))
233 (parent (g-boxed-foreign tree-iter)))
235 (defun tree-model-iter-first-child (tree-model parent)
236 (let ((iter (make-tree-iter)))
237 (if (gtk-tree-model-iter-children tree-model iter parent)
241 (export 'tree-model-iter-first-child)
243 (defcfun (tree-model-iter-has-child "gtk_tree_model_iter_has_child") :boolean
244 (tree-model g-object)
245 (iter (g-boxed-foreign tree-iter)))
247 (export 'tree-model-iter-has-child)
249 (defcfun (tree-model-iter-n-children "gtk_tree_model_iter_n_children") :int
250 (tree-model g-object)
251 (iter (g-boxed-foreign tree-iter)))
253 (export 'tree-model-iter-n-children)
255 (defcfun gtk-tree-model-iter-nth-child :boolean
256 (tree-model g-object)
257 (iter (g-boxed-foreign tree-iter))
258 (parent (g-boxed-foreign tree-iter))
261 (defun tree-model-iter-nth-child (tree-model parent n)
262 (let ((iter (make-tree-iter)))
263 (if (gtk-tree-model-iter-nth-child tree-model iter parent n)
267 (export 'tree-model-iter-nth-child)
269 (defcfun gtk-tree-model-iter-parent :boolean
270 (tree-model g-object)
271 (iter (g-boxed-foreign tree-iter))
272 (parent (g-boxed-foreign tree-iter)))
274 (defun tree-model-iter-parent (tree-model iter)
275 (let ((parent (make-tree-iter)))
276 (if (gtk-tree-model-iter-parent tree-model iter parent)
280 (export 'tree-model-iter-parent)
282 (defcfun (tree-model-iter-to-string "gtk_tree_model_get_string_from_iter") (g-string :free-from-foreign t)
283 (tree-model g-object)
284 (iter (g-boxed-foreign tree-iter)))
286 (export 'tree-model-iter-to-string)
288 (defcfun (tree-model-ref-node "gtk_tree_model_ref_node") :void
289 (tree-model g-object)
290 (iter (g-boxed-foreign tree-iter)))
292 (export 'tree-model-ref-node)
294 (defcfun (tree-model-unref-node "gtk_tree_model_unref_node") :void
295 (tree-model g-object)
296 (iter (g-boxed-foreign tree-iter)))
298 (export 'tree-model-unref-node)
300 (defcallback gtk-tree-model-foreach-cb :boolean ((model g-object) (path (g-boxed-foreign tree-path)) (iter (g-boxed-foreign tree-iter)) (data :pointer))
301 (let ((fn (get-stable-pointer-value data)))
303 (funcall fn model path iter)
304 (stop-tree-model-iteration () t)
305 (skip-tree-model-current () nil))))
307 (defcfun gtk-tree-model-foreach :void
312 (defun do-tree-model (model fn)
313 (with-stable-pointer (ptr fn)
314 (gtk-tree-model-foreach model (callback gtk-tree-model-foreach-cb) ptr)))
316 (export 'do-tree-model)
318 (defun array-insert-at (array element index)
319 (assert (adjustable-array-p array))
320 (adjust-array array (1+ (length array)) :fill-pointer t)
321 (iter (for i from (1- (length array)) above index)
323 (aref array (1- i))))
324 (setf (aref array index) element)
327 (defun array-remove-at (array index)
328 (assert (adjustable-array-p array))
329 (iter (for i from index below (1- (length array)))
331 (aref array (1+ i))))
332 (adjust-array array (1- (length array)) :fill-pointer t)
340 (children (make-array 0 :element-type 'tree-node :adjustable t :fill-pointer t)))
342 (defclass tree-lisp-store (tree-model)
343 ((columns-getters :initform (make-array 0 :adjustable t :fill-pointer t) :reader tree-lisp-store-getters)
344 (columns-types :initform (make-array 0 :adjustable t :fill-pointer t) :reader tree-lisp-store-types)
345 (root :initform (make-tree-node) :reader tree-lisp-store-root)
346 (id-map :initform (make-hash-table) :reader tree-lisp-store-id-map)
347 (next-id-value :initform 0 :accessor tree-lisp-store-next-id-value))
348 (:metaclass gobject-class)
349 (:g-type-name . "LispTreeStore"))
351 (defmethod initialize-instance :after ((object tree-lisp-store) &key &allow-other-keys)
352 (setf (tree-node-tree (tree-lisp-store-root object)) object))
354 (register-object-type-implementation "LispTreeStore" tree-lisp-store "GObject" ("GtkTreeModel") nil)
356 (defun map-subtree (node fn)
358 (iter (for child in-vector (tree-node-children node))
359 (map-subtree child fn)))
361 (defun clear-id (node)
364 (when (and (tree-node-id n)
366 (remhash (tree-node-id n)
367 (tree-lisp-store-id-map (tree-node-tree n))))
368 (setf (tree-node-id n) nil))))
370 (defun set-node-tree (node tree)
373 (setf (tree-node-tree n) tree))))
375 (defun tree-node-insert-at (node child index)
376 (assert (null (tree-node-parent child)))
378 (setf (tree-node-parent child) node)
379 (set-node-tree child (tree-node-tree node))
380 (array-insert-at (tree-node-children node) child index)
381 (notice-tree-node-insertion (tree-node-tree node) node child index)
384 (defun tree-node-child-at (node index)
385 (aref (tree-node-children node) index))
387 (defun tree-node-remove-at (node index)
388 (assert (<= 0 index (1- (length (tree-node-children node)))))
389 (let ((child (tree-node-child-at node index)))
391 (setf (tree-node-parent child) nil)
392 (set-node-tree child nil)
393 (array-remove-at (tree-node-children node) index)
394 (notice-tree-node-removal (tree-node-tree node) node child index)))
396 (defun tree-lisp-store-add-column (store column-type column-getter)
397 (vector-push-extend column-getter (tree-lisp-store-getters store))
398 (vector-push-extend column-type (tree-lisp-store-types store)))
400 (defmethod tree-model-get-flags-impl ((store tree-lisp-store))
403 (defmethod tree-model-get-n-columns-impl ((store tree-lisp-store))
404 (length (tree-lisp-store-getters store)))
406 (defmethod tree-model-get-column-type-impl ((store tree-lisp-store) index)
407 (aref (tree-lisp-store-types store) index))
409 (defun get-node-by-indices (root indices)
411 (get-node-by-indices (tree-node-child-at root (first indices)) (rest indices))
414 (defun get-node-by-path (tree path)
415 (let ((indices (tree-path-indices path)))
416 (get-node-by-indices (tree-lisp-store-root tree) indices)))
418 (defun get-node-path (node)
420 (for parent = (tree-node-parent node))
422 (for index = (position node (tree-node-children parent)))
425 (finally (return z))))
427 (defun tree-lisp-store-get-next-id (tree)
428 (incf (tree-lisp-store-next-id-value tree)))
430 (defun tree-lisp-store-add-id-map (tree id node)
431 (setf (gethash id (tree-lisp-store-id-map tree)) node))
433 (defun get-assigned-id (tree node)
434 (or (tree-node-id node)
435 (let ((id (tree-lisp-store-get-next-id tree)))
436 (tree-lisp-store-add-id-map tree id node)
437 (setf (tree-node-id node) id)
440 (defun get-node-by-id (tree id)
441 (gethash id (tree-lisp-store-id-map tree)))
443 (defmethod tree-model-get-iter-impl ((store tree-lisp-store) iter path)
444 (let* ((node (get-node-by-path store path))
445 (node-idx (get-assigned-id store node)))
446 (setf (tree-iter-stamp iter) 0
447 (tree-iter-user-data iter) node-idx)))
449 (defun get-node-by-iter (tree iter)
450 (get-node-by-id tree (tree-iter-user-data iter)))
452 (defmethod tree-model-get-path-impl ((store tree-lisp-store) iter)
453 (let* ((path (make-instance 'tree-path))
454 (node (get-node-by-iter store iter))
455 (indices (get-node-path node)))
456 (setf (tree-path-indices path) indices)
459 (defmethod tree-model-get-value-impl ((store tree-lisp-store) iter n value)
460 (let* ((node (get-node-by-iter store iter))
461 (getter (aref (tree-lisp-store-getters store) n))
462 (type (aref (tree-lisp-store-types store) n)))
463 (set-g-value value (funcall getter (tree-node-item node)) type)))
465 (defmethod tree-model-iter-next-impl ((store tree-lisp-store) iter)
466 (let* ((node (get-node-by-iter store iter))
467 (parent (tree-node-parent node))
468 (index (position node (tree-node-children parent))))
469 (when (< (1+ index) (length (tree-node-children parent)))
470 (setf (tree-iter-stamp iter)
472 (tree-iter-user-data iter)
473 (get-assigned-id store (tree-node-child-at parent (1+ index))))
476 (defmethod tree-model-iter-children-impl ((store tree-lisp-store) iter parent)
477 (let* ((node (if parent
478 (get-node-by-iter store parent)
479 (tree-lisp-store-root store))))
480 (when (plusp (length (tree-node-children node)))
481 (setf (tree-iter-stamp iter)
483 (tree-iter-user-data iter)
484 (get-assigned-id store (tree-node-child-at node 0)))
487 (defmethod tree-model-iter-has-child-impl ((store tree-lisp-store) iter)
488 (let ((node (get-node-by-iter store iter)))
489 (plusp (length (tree-node-children node)))))
491 (defmethod tree-model-iter-n-children-impl ((store tree-lisp-store) iter)
492 (let* ((node (if iter
493 (get-node-by-iter store iter)
494 (tree-lisp-store-root store))))
495 (length (tree-node-children node))))
497 (defmethod tree-model-iter-nth-child-impl ((store tree-lisp-store) iter parent n)
498 (let* ((node (if parent
499 (get-node-by-iter store parent)
500 (tree-lisp-store-root store)))
501 (requested-node (tree-node-child-at node n)))
502 (setf (tree-iter-stamp iter) 0
503 (tree-iter-user-data iter) (get-assigned-id store requested-node))
506 (defmethod tree-model-iter-parent-impl ((store tree-lisp-store) iter child)
507 (let ((node (get-node-by-iter store child)))
508 (when (tree-node-parent node)
509 (setf (tree-iter-stamp iter) 0
510 (tree-iter-user-data iter) (get-assigned-id store (tree-node-parent node))))))
512 (defmethod tree-model-ref-node-impl ((store tree-lisp-store) iter)
513 (declare (ignorable iter)))
515 (defmethod tree-model-unref-node-impl ((store tree-lisp-store) iter)
516 (declare (ignorable iter)))
518 (defun notice-tree-node-insertion (tree node child index)
519 (declare (ignore node index))
521 (let* ((path (make-instance 'tree-path))
522 (iter (make-tree-iter)))
523 (setf (tree-path-indices path) (get-node-path child)
524 (tree-iter-stamp iter) 0
525 (tree-iter-user-data iter) (get-assigned-id tree child))
526 (emit-signal tree "row-inserted" path iter)
527 (when (plusp (length (tree-node-children child)))
528 (emit-signal tree "row-has-child-toggled" path iter)))))
530 (defun notice-tree-node-removal (tree node child index)
531 (declare (ignore child))
533 (let ((path (make-instance 'tree-path)))
534 (setf (tree-path-indices path) (nconc (get-node-path node) (list index)))
535 (emit-signal tree "row-deleted" path))
536 (when (zerop (length (tree-node-children node)))
537 (let* ((path (make-instance 'tree-path))
538 (iter (make-tree-iter)))
539 (setf (tree-path-indices path) (get-node-path node)
540 (tree-iter-stamp iter) 0
541 (tree-iter-user-data iter) (get-assigned-id tree node))
542 (emit-signal tree "row-has-child-toggled" path iter)))))