| 1 | ;; Common Lisp bindings for GTK+ v2.0 |
| 2 | ;; Copyright (C) 1999-2001 Espen S. Johnsen <espen@users.sourceforge.org> |
| 3 | ;; |
| 4 | ;; This library is free software; you can redistribute it and/or |
| 5 | ;; modify it under the terms of the GNU Lesser General Public |
| 6 | ;; License as published by the Free Software Foundation; either |
| 7 | ;; version 2 of the License, or (at your option) any later version. |
| 8 | ;; |
| 9 | ;; This library is distributed in the hope that it will be useful, |
| 10 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | ;; Lesser General Public License for more details. |
| 13 | ;; |
| 14 | ;; You should have received a copy of the GNU Lesser General Public |
| 15 | ;; License along with this library; if not, write to the Free Software |
| 16 | ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | |
| 18 | ;; $Id: gtkobject.lisp,v 1.19 2004-11-06 21:39:58 espen Exp $ |
| 19 | |
| 20 | |
| 21 | (in-package "GTK") |
| 22 | |
| 23 | |
| 24 | ;;;; Misc utils |
| 25 | |
| 26 | ; (defun name-to-string (name) |
| 27 | ; (substitute #\_ #\- (string-downcase (string name)))) |
| 28 | |
| 29 | ; (defun string-to-name (name &optional (package "KEYWORD")) |
| 30 | ; (intern (substitute #\- #\_ (string-upcase name)) package)) |
| 31 | |
| 32 | |
| 33 | |
| 34 | ;;;; Superclass for the gtk class hierarchy |
| 35 | |
| 36 | (eval-when (:compile-toplevel :load-toplevel :execute) |
| 37 | (init-types-in-library |
| 38 | #.(concatenate 'string (pkg-config:pkg-variable "gtk+-2.0" "libdir") |
| 39 | "/libgtk-x11-2.0.so") |
| 40 | :ignore ("gtk_window_get_type_hint")) |
| 41 | |
| 42 | (defclass %object (gobject) |
| 43 | () |
| 44 | (:metaclass gobject-class) |
| 45 | (:alien-name "GtkObject"))) |
| 46 | |
| 47 | |
| 48 | (defmethod initialize-instance ((object %object) &rest initargs &key signal) |
| 49 | (declare (ignore signal)) |
| 50 | (call-next-method) |
| 51 | (reference-foreign (class-of object) (proxy-location object)) |
| 52 | (dolist (signal-definition (get-all initargs :signal)) |
| 53 | (apply #'signal-connect object signal-definition))) |
| 54 | |
| 55 | (defmethod initialize-instance :around ((object %object) &rest initargs) |
| 56 | (declare (ignore initargs)) |
| 57 | (call-next-method) |
| 58 | (%object-sink object)) |
| 59 | |
| 60 | (defbinding %object-sink () nil |
| 61 | (object %object)) |
| 62 | |
| 63 | ;;;; Main loop, timeouts and idle functions |
| 64 | |
| 65 | (declaim (inline events-pending-p main-iteration)) |
| 66 | |
| 67 | (defbinding (events-pending-p "gtk_events_pending") () boolean) |
| 68 | |
| 69 | (defbinding get-current-event () gdk:event) |
| 70 | |
| 71 | (defbinding main-do-event () nil |
| 72 | (event gdk:event)) |
| 73 | |
| 74 | (defbinding main () nil) |
| 75 | |
| 76 | (defbinding main-level () int) |
| 77 | |
| 78 | (defbinding main-quit () nil) |
| 79 | |
| 80 | (defbinding main-iteration-do (&optional (blocking t)) boolean |
| 81 | (blocking boolean)) |
| 82 | |
| 83 | (defun main-iterate-all (&rest args) |
| 84 | (declare (ignore args)) |
| 85 | (when (events-pending-p) |
| 86 | (main-iteration-do nil) |
| 87 | (main-iterate-all))) |
| 88 | |
| 89 | |
| 90 | ;;;; Metaclass for child classes |
| 91 | |
| 92 | (defvar *container-to-child-class-mappings* (make-hash-table)) |
| 93 | |
| 94 | (eval-when (:compile-toplevel :load-toplevel :execute) |
| 95 | (defclass child-class (virtual-slots-class) |
| 96 | ()) |
| 97 | |
| 98 | (defclass direct-child-slot-definition (direct-virtual-slot-definition) |
| 99 | ((pname :reader slot-definition-pname :initarg :pname))) |
| 100 | |
| 101 | (defclass effective-child-slot-definition (effective-virtual-slot-definition) |
| 102 | ((pname :reader slot-definition-pname :initarg :pname))) |
| 103 | |
| 104 | |
| 105 | (defmethod shared-initialize ((class child-class) names &key container) |
| 106 | (call-next-method) |
| 107 | (setf |
| 108 | (gethash (find-class (first container)) *container-to-child-class-mappings*) |
| 109 | class)) |
| 110 | |
| 111 | (defmethod direct-slot-definition-class ((class child-class) &rest initargs) |
| 112 | (case (getf initargs :allocation) |
| 113 | (:property (find-class 'direct-child-slot-definition)) |
| 114 | (t (call-next-method)))) |
| 115 | |
| 116 | (defmethod effective-slot-definition-class ((class child-class) &rest initargs) |
| 117 | (case (getf initargs :allocation) |
| 118 | (:property (find-class 'effective-child-slot-definition)) |
| 119 | (t (call-next-method)))) |
| 120 | |
| 121 | (defmethod compute-effective-slot-definition-initargs ((class child-class) direct-slotds) |
| 122 | (if (eq (most-specific-slot-value direct-slotds 'allocation) :property) |
| 123 | (nconc |
| 124 | (list :pname (most-specific-slot-value direct-slotds 'pname)) |
| 125 | (call-next-method)) |
| 126 | (call-next-method))) |
| 127 | |
| 128 | (progn |
| 129 | (declaim (optimize (ext:inhibit-warnings 3))) |
| 130 | (defun %container-child-get-property (parent child pname gvalue)) |
| 131 | (defun %container-child-set-property (parent child pname gvalue))) |
| 132 | |
| 133 | |
| 134 | (defmethod initialize-internal-slot-functions ((slotd effective-child-slot-definition)) |
| 135 | (let* ((type (slot-definition-type slotd)) |
| 136 | (pname (slot-definition-pname slotd)) |
| 137 | (type-number (find-type-number type))) |
| 138 | (unless (slot-boundp slotd 'reader-function) |
| 139 | (setf |
| 140 | (slot-value slotd 'reader-function) |
| 141 | #'(lambda (object) |
| 142 | (with-slots (parent child) object |
| 143 | (let ((gvalue (gvalue-new type-number))) |
| 144 | (%container-child-get-property parent child pname gvalue) |
| 145 | (unwind-protect |
| 146 | (funcall (reader-function type) gvalue +gvalue-value-offset+) |
| 147 | (gvalue-free gvalue t))))))) |
| 148 | |
| 149 | (unless (slot-boundp slotd 'writer-function) |
| 150 | (setf |
| 151 | (slot-value slotd 'writer-function) |
| 152 | #'(lambda (value object) |
| 153 | (with-slots (parent child) object |
| 154 | (let ((gvalue (gvalue-new type-number))) |
| 155 | (funcall (writer-function type) value gvalue +gvalue-value-offset+) |
| 156 | (%container-child-set-property parent child pname gvalue) |
| 157 | ;; (funcall |
| 158 | ;; (destroy-function type) |
| 159 | ;; gvalue +gvalue-value-offset+) |
| 160 | (gvalue-free gvalue t) |
| 161 | value))))) |
| 162 | |
| 163 | (unless (slot-boundp slotd 'boundp-function) |
| 164 | (setf |
| 165 | (slot-value slotd 'boundp-function) |
| 166 | #'(lambda (object) |
| 167 | (declare (ignore object)) |
| 168 | t)))) |
| 169 | (call-next-method))) |
| 170 | |
| 171 | |
| 172 | (defmethod pcl::add-reader-method ((class child-class) generic-function slot-name) |
| 173 | (add-method |
| 174 | generic-function |
| 175 | (make-instance 'standard-method |
| 176 | :specializers (list (find-class 'widget)) |
| 177 | :lambda-list '(widget) |
| 178 | :function #'(lambda (args next-methods) |
| 179 | (declare (ignore next-methods)) |
| 180 | (child-slot-value (first args) slot-name))))) |
| 181 | |
| 182 | (defmethod pcl::add-writer-method |
| 183 | ((class child-class) generic-function slot-name) |
| 184 | (add-method |
| 185 | generic-function |
| 186 | (make-instance 'standard-method |
| 187 | :specializers (list (find-class t) (find-class 'widget)) |
| 188 | :lambda-list '(value widget) |
| 189 | :function #'(lambda (args next-methods) |
| 190 | (declare (ignore next-methods)) |
| 191 | (destructuring-bind (value widget) args |
| 192 | (setf |
| 193 | (child-slot-value widget slot-name) |
| 194 | value)))))) |
| 195 | |
| 196 | |
| 197 | (defmethod validate-superclass ((class child-class) (super pcl::standard-class)) |
| 198 | ;(subtypep (class-name super) 'container-child) |
| 199 | t) |
| 200 | |
| 201 | |
| 202 | (defclass container-child () |
| 203 | ((parent :initarg :parent :type container) |
| 204 | (child :initarg :child :type widget))) |
| 205 | |
| 206 | |
| 207 | ;;;; |
| 208 | |
| 209 | (defbinding %container-class-list-child-properties () pointer |
| 210 | (class pointer) |
| 211 | (n-properties unsigned-int :out)) |
| 212 | |
| 213 | (defun query-container-class-child-properties (type-number) |
| 214 | (let ((class (type-class-ref type-number))) |
| 215 | (multiple-value-bind (array length) |
| 216 | (%container-class-list-child-properties class) |
| 217 | (unwind-protect |
| 218 | (map-c-vector 'list #'identity array 'param length) |
| 219 | (deallocate-memory array))))) |
| 220 | |
| 221 | (defun default-container-child-name (container-class) |
| 222 | (intern (format nil "~A-CHILD" container-class))) |
| 223 | |
| 224 | (defun expand-container-type (type &optional options) |
| 225 | (let* ((class (type-from-number type)) |
| 226 | (super (supertype type)) |
| 227 | (child-class (default-container-child-name class))) |
| 228 | `(progn |
| 229 | ,(expand-gobject-type type options) |
| 230 | (defclass ,child-class (,(default-container-child-name super)) |
| 231 | ,(slot-definitions child-class |
| 232 | (query-container-class-child-properties type) nil) |
| 233 | (:metaclass child-class) |
| 234 | (:container ,class))))) |
| 235 | |
| 236 | |
| 237 | (register-derivable-type 'container "GtkContainer" 'expand-container-type) |