X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/clg/blobdiff_plain/2a8752b025e5b22396c063505279a35e08dc3778..cc74b2c987edc3e2bc85413e92f33e2690a32b1c:/gtk/gtktree.lisp diff --git a/gtk/gtktree.lisp b/gtk/gtktree.lisp index 5bbc67a..9b408f5 100644 --- a/gtk/gtktree.lisp +++ b/gtk/gtktree.lisp @@ -1,21 +1,26 @@ -;; Common Lisp bindings for GTK+ v2.0 -;; Copyright (C) 1999-2001 Espen S. Johnsen +;; Common Lisp bindings for GTK+ v2.x +;; Copyright 2004-2005 Espen S. Johnsen ;; -;; This library is free software; you can redistribute it and/or -;; modify it under the terms of the GNU Lesser General Public -;; License as published by the Free Software Foundation; either -;; version 2 of the License, or (at your option) any later version. +;; Permission is hereby granted, free of charge, to any person obtaining +;; a copy of this software and associated documentation files (the +;; "Software"), to deal in the Software without restriction, including +;; without limitation the rights to use, copy, modify, merge, publish, +;; distribute, sublicense, and/or sell copies of the Software, and to +;; permit persons to whom the Software is furnished to do so, subject to +;; the following conditions: ;; -;; This library is distributed in the hope that it will be useful, -;; but WITHOUT ANY WARRANTY; without even the implied warranty of -;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -;; Lesser General Public License for more details. +;; The above copyright notice and this permission notice shall be +;; included in all copies or substantial portions of the Software. ;; -;; You should have received a copy of the GNU Lesser General Public -;; License along with this library; if not, write to the Free Software -;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -;; $Id: gtktree.lisp,v 1.2 2004-11-15 19:24:03 espen Exp $ +;; $Id: gtktree.lisp,v 1.13 2006-02-08 22:21:07 espen Exp $ (in-package "GTK") @@ -54,14 +59,14 @@ (defbinding cell-layout-add-attribute (cell-layout cell attribute column) nil (column int)) (def-callback-marshal %cell-layout-data-func - (nil cell-layout cell-renderer tree-model tree-iter)) + (nil cell-layout cell-renderer tree-model (copy-of tree-iter))) (defbinding cell-layout-set-cell-data-func (cell-layout cell function) nil (cell-layout cell-layout) (cell cell-renderer) - ((callback %cell-layout-data-func) pointer) + (%cell-layout-data-func callback) ((register-callback-function function) unsigned-int) - ((callback %destroy-user-data) pointer)) + (user-data-destroy-func callback)) (defbinding cell-layout-clear-attributes () nil (cell-layout cell-layout) @@ -93,10 +98,26 @@ (defbinding %list-store-set-column-types () nil ((length columns) unsigned-int) (columns (vector gtype))) -(defbinding list-store-remove () boolean +(defbinding %list-store-remove () boolean (list-store list-store) (tree-iter tree-iter)) +(defun list-store-remove (store row) + (etypecase row + (tree-iter + (%list-store-remove store row)) + (tree-path + (multiple-value-bind (valid iter) (tree-model-get-iter store row) + (if valid + (%list-store-remove store iter) + (error "~A not poiniting to a valid iterator in ~A" row store)))) + (tree-row-reference + (let ((path (tree-row-reference-get-path row))) + (if path + (list-store-remove store path) + (error "~A not valid" row)))))) + + (defbinding %list-store-insert () nil (list-store list-store) (tree-iter tree-iter) @@ -192,12 +213,12 @@ (defun %make-tree-path (path) (funcall (writer-function 'pointer) c-vector location (size-of 'int)) location)) -(defun %tree-path-to-vector (location &optional (destroy-p t)) - (prog1 - (map-c-vector 'vector #'identity (%tree-path-get-indices location) - 'int (%tree-path-get-depth location)) - (when destroy-p - (%tree-path-free 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)))) (eval-when (:compile-toplevel :load-toplevel :execute) (defmethod alien-type ((type (eql 'tree-path)) &rest args) @@ -212,25 +233,58 @@ (defmethod to-alien-form (path (type (eql 'tree-path)) &rest args) (declare (ignore type args)) `(%make-tree-path ,path)) - (defmethod to-alien-function ((type (eql 'tree-path)) &rest args) - (declare (ignore type args)) - #'%make-tree-path) - (defmethod from-alien-form (location (type (eql 'tree-path)) &rest args) (declare (ignore type args)) - `(%tree-path-to-vector ,location)) + `(let ((location ,location)) + (prog1 + (%tree-path-to-vector location) + (%tree-path-free location)))) - (defmethod from-alien-function ((type (eql 'tree-path)) &rest args) + (defmethod copy-from-alien-form (location (type (eql 'tree-path)) &rest args) (declare (ignore type args)) - #'%tree-path-to-vector) + `(%tree-path-to-vector ,location)) (defmethod cleanup-form (location (type (eql 'tree-path)) &rest args) (declare (ignore type args)) - `(%tree-path-free ,location)) + `(%tree-path-free ,location))) + +(defmethod to-alien-function ((type (eql 'tree-path)) &rest args) + (declare (ignore type args)) + #'%make-tree-path) - (defmethod cleanup-function ((type (eql 'tree-path)) &rest args) - (declare (ignore type args)) - #'%tree-path-free)) +(defmethod from-alien-function ((type (eql 'tree-path)) &rest args) + (declare (ignore type args)) + #'(lambda (location) + (prog1 + (%tree-path-to-vector location) + (%tree-path-free location)))) + +(defmethod copy-from-alien-function ((type (eql 'tree-path)) &rest args) + (declare (ignore type args)) + #'%tree-path-to-vector) + +(defmethod cleanup-function ((type (eql 'tree-path)) &rest args) + (declare (ignore type args)) + #'%tree-path-free) + +(defmethod writer-function ((type (eql 'tree-path)) &rest args) + (declare (ignore type args)) + (let ((writer (writer-function 'pointer))) + #'(lambda (path location &optional (offset 0)) + (funcall writer (%make-tree-path path) location offset)))) + +(defmethod reader-function ((type (eql 'tree-path)) &rest args) + (declare (ignore type args)) + (let ((reader (reader-function 'pointer))) + #'(lambda (location &optional (offset 0) weak-p) + (declare (ignore weak-p)) + (%tree-path-to-vector (funcall reader location offset))))) + +(defmethod destroy-function ((type (eql 'tree-path)) &rest args) + (declare (ignore type args)) + (let ((reader (reader-function 'pointer))) + #'(lambda (location &optional (offset 0)) + (%tree-path-free (funcall reader location offset))))) (defbinding %tree-row-reference-new () pointer @@ -238,9 +292,8 @@ (defbinding %tree-row-reference-new () pointer (path tree-path)) (defmethod initialize-instance ((reference tree-row-reference) &key model path) - (declare (ignore initargs)) (setf - (slot-value reference 'location) + (foreign-location reference) (%tree-row-reference-new model path)) (call-next-method)) @@ -251,7 +304,7 @@ (defbinding (tree-row-reference-valid-p "gtk_tree_row_reference_valid") () boole (reference tree-row-reference)) -(defbinding tree-model-get-column-type () type-number +(defbinding tree-model-get-column-type () gtype ;type-number (tree-model tree-model) (index int)) @@ -271,9 +324,16 @@ (defbinding %tree-model-get-value () nil (column int) (gvalue gvalue)) -(defun tree-model-get-column-value (model iter column) - (let ((index (column-index model column))) - (with-gvalue (gvalue (tree-model-get-column-type model index)) +(defun tree-model-value (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 @@ -296,7 +356,7 @@ (defbinding tree-model-iter-n-children () int (iter tree-iter)) (defbinding tree-model-iter-nth-child - (tree-model parent &optional (iter (make-instance 'tree-iter))) boolean + (tree-model parent n &optional (iter (make-instance 'tree-iter))) boolean (tree-model tree-model) (iter tree-iter :return) (parent (or null tree-iter)) @@ -308,16 +368,12 @@ (defbinding tree-model-iter-parent (iter tree-iter :return) (child tree-iter)) -(defbinding tree-model-get-string-from-iter () string - (tree-model tree-model) - (iter tree-iter)) - (def-callback-marshal %tree-model-foreach-func - (boolean tree-model tree-path tree-iter)) + (boolean tree-model (path (copy-of tree-path)) (iter (copy-of tree-iter)))) (defbinding %tree-model-foreach () nil (tree-model tree-model) - ((callback %tree-model-foreach-func) pointer) + ((progn %tree-model-foreach-func) callback) (callback-id unsigned-int)) (defun tree-model-foreach (model function) @@ -366,6 +422,9 @@ (defun column-index (model column) :test #'string=))) (error "~A has no column ~S" model column))) +(defun column-name (model index) + (svref (object-data model 'column-names) index)) + (defun tree-model-column-value-setter (model column) (let ((setters (or (object-data model 'column-setters) @@ -381,7 +440,8 @@ (defun tree-model-column-value-setter (model column) (let ((setter (mkbinding (column-setter-name model) nil (type-of model) 'tree-iter 'int - (type-from-number (tree-model-get-column-type model index)) +; (type-from-number (tree-model-get-column-type model index)) + (tree-model-get-column-type model index) 'int))) #'(lambda (value iter) (funcall setter model iter index value -1)))))))) @@ -402,9 +462,16 @@ (defun tree-model-row-setter (model) (funcall setter value iter)) row setters))))))) -(defun (setf tree-model-column-value) (value model iter column) - (funcall (tree-model-column-value-setter model column) value iter) - value) +(defun (setf tree-model-value) (value 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) @@ -416,10 +483,168 @@ (defun %tree-model-set (model iter data) (cons (loop as (column value . rest) = data then rest - do (setf (tree-model-column-value model iter column) value) + do (setf (tree-model-value model iter column) value) while rest)))) +;;; Tree Selection + +(def-callback-marshal %tree-selection-func (boolean tree-selection tree-model (path (copy-of tree-path)) (path-currently-selected boolean))) + +(defbinding tree-selection-set-select-function (selection function) nil + (selection tree-selection) + (%tree-selection-func callback) + ((register-callback-function function) unsigned-int) + (user-data-destroy-func callback)) + +(defbinding tree-selection-get-selected + (selection &optional (iter (make-instance 'tree-iter))) boolean + (selection tree-selection) + (nil null) + (iter tree-iter :return)) + +(def-callback-marshal %tree-selection-foreach-func (nil tree-model (path (copy-of tree-path)) (iter (copy-of tree-iter)))) + +(defbinding %tree-selection-selected-foreach () nil + (tree-selection tree-selection) + ((progn %tree-selection-foreach-func) 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))) + + +(def-callback-marshal %tree-iter-compare-func + ((or int sort-order) tree-model (a (copy-of tree-iter)) (b (copy-of 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-func callback) + ((register-callback-function function) unsigned-int) + (user-data-destroy-func callback)) + +(defbinding %tree-sortable-set-default-sort-func () nil + (sortable tree-sortable) + (compare-func (or null pointer)) + (callback-id unsigned-int) + (destroy-func (or null pointer))) + +(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 + (callback %tree-iter-compare-func) + (register-callback-function function) + (callback user-data-destroy-func))) + ((%tree-sortable-set-sort-func sortable column function)))) + +(defbinding tree-sortable-has-default-sort-func-p () boolean + (sortable tree-sortable)) + + ;;; Tree Store (defbinding %tree-store-set-column-types () nil @@ -460,7 +685,7 @@ (defbinding %tree-store-insert-before () nil (parent (or null tree-iter)) (sibling (or null tree-iter))) -(defun tree-store-insert-after +(defun tree-store-insert-before (store parent sibling &optional data (iter (make-instance 'tree-iter))) (%tree-store-insert-before store iter parent sibling) (when data (%tree-model-set store iter data)) @@ -537,16 +762,14 @@ (defbinding tree-store-move-after () nil ;;; Tree View -(defmethod initialize-instance ((tree-view tree-view) &key column) +(defmethod initialize-instance ((tree-view tree-view) &rest initargs + &key column) (call-next-method) (mapc #'(lambda (column) (tree-view-append-column tree-view column)) (get-all initargs :column))) -(defbinding tree-view-get-selection () tree-selection - (tree-view tree-view)) - (defbinding tree-view-columns-autosize () nil (tree-view tree-view)) @@ -558,7 +781,7 @@ (defbinding tree-view-remove-column () int (tree-view tree-view) (tree-view-column tree-view-column)) -(defbinding tree-view-insert-column (view columnd position) int +(defbinding tree-view-insert-column (view column position) int (view tree-view) (column tree-view-column) ((if (eq position :end) -1 position) int)) @@ -629,11 +852,11 @@ (defbinding tree-view-collapse-row () nil (tree-view tree-view) (path tree-path)) -(def-callback-marshal %tree-view-mapping-func (nil tree-view tree-path)) +(def-callback-marshal %tree-view-mapping-func (nil tree-view (path (copy-of tree-path)))) (defbinding %tree-view-map-expanded-rows () nil (tree-view tree-view) - ((callback %tree-view-mapping-func) pointer) + ((progn %tree-view-mapping-func) callback) (callback-id unsigned-int)) (defun map-expanded-rows (function tree-view) @@ -673,5 +896,127 @@ (defbinding tree-view-get-visible-rect () nil ;; and many more functions which we'll add later -;;; Tree View Column +;;;; Icon View + +#+gtk2.6 +(progn + (defbinding icon-view-get-path-at-pos () tree-path + (icon-view icon-view) + (x int) (y int)) + + (def-callback-marshal %icon-view-foreach-func + (nil icon-view (path (copy-of tree-path)))) + + (defbinding %icon-view-selected-foreach () tree-path + (icon-view icon-view) + ((progn %icon-view-foreach-func) callback) + (callback-id unsigned-int)) + + (defun icon-view-foreach (icon-view function) + (with-callback-function (id function) + (%icon-view-selected-foreach icon-view id))) + (defbinding icon-view-select-path () nil + (icon-view icon-view) + (path tree-path)) + + (defbinding icon-view-unselect-path () nil + (icon-view icon-view) + (path tree-path)) + + (defbinding icon-view-path-is-selected-p () boolean + (icon-view icon-view) + (path tree-path)) + + (defbinding icon-view-get-selected-items () (glist tree-path) + (icon-view icon-view)) + + (defbinding icon-view-select-all () nil + (icon-view icon-view)) + + (defbinding icon-view-unselect-all () nil + (icon-view icon-view)) + + (defbinding icon-view-item-activated () nil + (icon-view icon-view) + (path tree-path)) + + (defbinding %icon-view-set-text-column (column icon-view) nil + (icon-view icon-view) + ((if (integerp column) + column + (column-index (icon-view-model icon-view) column)) int)) + + (defbinding %icon-view-set-markup-column (column icon-view) nil + (icon-view icon-view) + ((if (integerp column) + column + (column-index (icon-view-model icon-view) column)) int)) + + (defbinding %icon-view-set-pixbuf-column (column icon-view) nil + (icon-view icon-view) + ((if (integerp column) + column + (column-index (icon-view-model icon-view) column)) int))) + +#+gtk2.8 +(progn + (defbinding icon-view-get-item-at-pos () boolean + (icon-view icon-view) + (x int) + (y int) + (tree-path tree-path :out) + (cell cell-renderer :out)) + + (defbinding icon-view-set-cursor (icon-view path &key cell start-editing) nil + (icon-view icon-view) + (path tree-path) + (cell (or null cell-renderer)) + (start-editing boolean)) + + (defbinding icon-view-get-cursor () boolean + (icon-view icon-view) + (path tree-path :out) + (cell cell-renderer :out)) + + (defbinding icon-view-get-dest-item-at-pos () boolean + (icon-view icon-view) + (drag-x int) + (drag-y int) + (tree-path tree-path :out) + (pos drop-position :out)) + + (defbinding icon-view-create-drag-icon () gdk:pixmap + (icon-view icon-view) + (tree-path tree-path)) + + (defbinding icon-view-scroll-to-path (icon-view tree-path &key row-align column-align) nil + (icon-view icon-view) + (tree-path tree-path) + ((or row-align column-align) boolean) + (row-align single-float) + (column-align single-float)) + + (defbinding icon-view-get-visible-range () boolean + (icon-view icon-view) + (start-path tree-path :out) + (end-path tree-path :out)) + +;; (defbinding icon-view-enable-model-drag-source () nil +;; (icon-view icon-view) +;; (start-button-mask gdk:modifier-type) +;; (targets (vector target-entry)) +;; ((length targets) unsigned-int) +;; (actions gdk:drag-action)) + +;; (defbinding icon-view-enable-model-drag-dest () nil +;; (icon-view icon-view) +;; (targets (vector target-entry)) +;; ((length targets) unsigned-int) +;; (actions gdk:drag-action)) + + (defbinding icon-view-unset-model-drag-source () nil + (icon-view icon-view)) + + (defbinding icon-view-unset-model-drag-dest () nil + (icon-view icon-view)))