chiark / gitweb /
Added bindigns to GdkAtom
[clg] / gtk / gtkobject.lisp
CommitLineData
560af5c5 1;; Common Lisp bindings for GTK+ v2.0
56460319 2;; Copyright (C) 1999-2001 Espen S. Johnsen <espen@users.sourceforge.org>
560af5c5 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
c289d084 18;; $Id: gtkobject.lisp,v 1.21 2004-12-20 20:09:53 espen Exp $
560af5c5 19
20
21(in-package "GTK")
22
8bf63d9f 23
560af5c5 24;;;; Misc utils
25
f86d391e 26; (defun name-to-string (name)
27; (substitute #\_ #\- (string-downcase (string name))))
560af5c5 28
f86d391e 29; (defun string-to-name (name &optional (package "KEYWORD"))
30; (intern (substitute #\- #\_ (string-upcase name)) package))
560af5c5 31
32
560af5c5 33
34;;;; Superclass for the gtk class hierarchy
35
36(eval-when (:compile-toplevel :load-toplevel :execute)
9adccb27 37 (init-types-in-library
38 #.(concatenate 'string (pkg-config:pkg-variable "gtk+-2.0" "libdir")
39 "/libgtk-x11-2.0.so")
18eed2a0 40 :ignore ("gtk_window_get_type_hint"))
f86d391e 41
42 (defclass %object (gobject)
560af5c5 43 ()
560af5c5 44 (:metaclass gobject-class)
45 (:alien-name "GtkObject")))
46
47
9adccb27 48(defmethod initialize-instance ((object %object) &rest initargs &key signal)
49 (declare (ignore signal))
560af5c5 50 (call-next-method)
9adccb27 51 (reference-foreign (class-of object) (proxy-location object))
18eed2a0 52 (dolist (signal-definition (get-all initargs :signal))
53 (apply #'signal-connect object signal-definition)))
560af5c5 54
9adccb27 55(defmethod initialize-instance :around ((object %object) &rest initargs)
560af5c5 56 (declare (ignore initargs))
57 (call-next-method)
18eed2a0 58 (%object-sink object))
560af5c5 59
f86d391e 60(defbinding %object-sink () nil
61 (object %object))
560af5c5 62
aace61f5 63;;;; Main loop, timeouts and idle functions
560af5c5 64
65(declaim (inline events-pending-p main-iteration))
66
f86d391e 67(defbinding (events-pending-p "gtk_events_pending") () boolean)
560af5c5 68
f86d391e 69(defbinding get-current-event () gdk:event)
aace61f5 70
f86d391e 71(defbinding main-do-event () nil
560af5c5 72 (event gdk:event))
73
f86d391e 74(defbinding main () nil)
560af5c5 75
f86d391e 76(defbinding main-level () int)
560af5c5 77
f86d391e 78(defbinding main-quit () nil)
560af5c5 79
f86d391e 80(defbinding main-iteration-do (&optional (blocking t)) boolean
560af5c5 81 (blocking boolean))
82
83(defun main-iterate-all (&rest args)
84 (declare (ignore args))
85 (when (events-pending-p)
f86d391e 86 (main-iteration-do nil)
560af5c5 87 (main-iterate-all)))
88
560af5c5 89
560af5c5 90;;;; Metaclass for child classes
f86d391e 91
92(defvar *container-to-child-class-mappings* (make-hash-table))
560af5c5 93
94(eval-when (:compile-toplevel :load-toplevel :execute)
9adccb27 95 (defclass child-class (virtual-slots-class)
1047e159 96 ())
560af5c5 97
f86d391e 98 (defclass direct-child-slot-definition (direct-virtual-slot-definition)
1047e159 99 ((pname :reader slot-definition-pname :initarg :pname)))
560af5c5 100
1047e159 101 (defclass effective-child-slot-definition (effective-virtual-slot-definition)
102 ((pname :reader slot-definition-pname :initarg :pname)))
560af5c5 103
104
1047e159 105(defmethod shared-initialize ((class child-class) names &key container)
560af5c5 106 (call-next-method)
f86d391e 107 (setf
108 (gethash (find-class (first container)) *container-to-child-class-mappings*)
109 class))
560af5c5 110
1047e159 111(defmethod direct-slot-definition-class ((class child-class) &rest initargs)
560af5c5 112 (case (getf initargs :allocation)
18eed2a0 113 (:property (find-class 'direct-child-slot-definition))
560af5c5 114 (t (call-next-method))))
115
1047e159 116(defmethod effective-slot-definition-class ((class child-class) &rest initargs)
560af5c5 117 (case (getf initargs :allocation)
18eed2a0 118 (:property (find-class 'effective-child-slot-definition))
560af5c5 119 (t (call-next-method))))
560af5c5 120
1047e159 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
b030f2d9 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
1047e159 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)))
d24bc5ad 138 (setf
139 (slot-value slotd 'getter)
140 #'(lambda (object)
141 (with-slots (parent child) object
142 (let ((gvalue (gvalue-new type-number)))
143 (%container-child-get-property parent child pname gvalue)
144 (unwind-protect
145 (funcall (reader-function type) gvalue +gvalue-value-offset+)
146 (gvalue-free gvalue t))))))
1047e159 147
d24bc5ad 148 (setf
149 (slot-value slotd 'setter)
150 #'(lambda (value object)
151 (with-slots (parent child) object
152 (let ((gvalue (gvalue-new type-number)))
153 (funcall (writer-function type) value gvalue +gvalue-value-offset+)
154 (%container-child-set-property parent child pname gvalue)
155 (gvalue-free gvalue t)
156 value)))))
157
1047e159 158 (call-next-method)))
560af5c5 159
160
161(defmethod pcl::add-reader-method ((class child-class) generic-function slot-name)
162 (add-method
163 generic-function
164 (make-instance 'standard-method
c289d084 165 :specializers (list (find-class 'widget))
166 :lambda-list '(widget)
167 :function #'(lambda (args next-methods)
168 (declare (ignore next-methods))
169 (child-property-value (first args) slot-name)))))
560af5c5 170
171(defmethod pcl::add-writer-method
172 ((class child-class) generic-function slot-name)
173 (add-method
174 generic-function
175 (make-instance 'standard-method
c289d084 176 :specializers (list (find-class t) (find-class 'widget))
177 :lambda-list '(value widget)
178 :function #'(lambda (args next-methods)
179 (declare (ignore next-methods))
180 (destructuring-bind (value widget) args
181 (setf (child-property-value widget slot-name) value))))))
560af5c5 182
183
184(defmethod validate-superclass ((class child-class) (super pcl::standard-class))
18eed2a0 185 ;(subtypep (class-name super) 'container-child)
186 t)
560af5c5 187
188
f86d391e 189(defclass container-child ()
190 ((parent :initarg :parent :type container)
191 (child :initarg :child :type widget)))
192
193
194;;;;
195
18eed2a0 196(defbinding %container-class-list-child-properties () pointer
197 (class pointer)
198 (n-properties unsigned-int :out))
f86d391e 199
18eed2a0 200(defun query-container-class-child-properties (type-number)
201 (let ((class (type-class-ref type-number)))
202 (multiple-value-bind (array length)
203 (%container-class-list-child-properties class)
204 (unwind-protect
9adccb27 205 (map-c-vector 'list #'identity array 'param length)
18eed2a0 206 (deallocate-memory array)))))
f86d391e 207
208(defun default-container-child-name (container-class)
209 (intern (format nil "~A-CHILD" container-class)))
210
1047e159 211(defun expand-container-type (type &optional options)
212 (let* ((class (type-from-number type))
213 (super (supertype type))
214 (child-class (default-container-child-name class)))
18eed2a0 215 `(progn
1047e159 216 ,(expand-gobject-type type options)
217 (defclass ,child-class (,(default-container-child-name super))
218 ,(slot-definitions child-class
219 (query-container-class-child-properties type) nil)
18eed2a0 220 (:metaclass child-class)
221 (:container ,class)))))
222
1047e159 223
18eed2a0 224(register-derivable-type 'container "GtkContainer" 'expand-container-type)