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 | |
b030f2d9 |
18 | ;; $Id: gtkobject.lisp,v 1.13 2002-03-19 19:09:18 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) |
b030f2d9 |
37 | (init-types-in-library "libgtk-x11-2.0.so" |
18eed2a0 |
38 | :ignore ("gtk_window_get_type_hint")) |
f86d391e |
39 | |
40 | (defclass %object (gobject) |
560af5c5 |
41 | () |
560af5c5 |
42 | (:metaclass gobject-class) |
43 | (:alien-name "GtkObject"))) |
44 | |
45 | |
f86d391e |
46 | (defmethod shared-initialize ((object %object) names &rest initargs |
18eed2a0 |
47 | &allow-other-keys) |
48 | (declare (ignore names)) |
560af5c5 |
49 | (call-next-method) |
18eed2a0 |
50 | (funcall (proxy-class-copy (class-of object)) nil (proxy-location object)) ; inc ref count before sinking |
f86d391e |
51 | (%object-sink object) |
18eed2a0 |
52 | (dolist (signal-definition (get-all initargs :signal)) |
53 | (apply #'signal-connect object signal-definition))) |
560af5c5 |
54 | |
18eed2a0 |
55 | (defmethod initialize-proxy ((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 | |
560af5c5 |
63 | |
aace61f5 |
64 | ;;;; Main loop, timeouts and idle functions |
560af5c5 |
65 | |
66 | (declaim (inline events-pending-p main-iteration)) |
67 | |
f86d391e |
68 | (defbinding (events-pending-p "gtk_events_pending") () boolean) |
560af5c5 |
69 | |
f86d391e |
70 | (defbinding get-current-event () gdk:event) |
aace61f5 |
71 | |
f86d391e |
72 | (defbinding main-do-event () nil |
560af5c5 |
73 | (event gdk:event)) |
74 | |
f86d391e |
75 | (defbinding main () nil) |
560af5c5 |
76 | |
f86d391e |
77 | (defbinding main-level () int) |
560af5c5 |
78 | |
f86d391e |
79 | (defbinding main-quit () nil) |
560af5c5 |
80 | |
f86d391e |
81 | (defbinding main-iteration-do (&optional (blocking t)) boolean |
560af5c5 |
82 | (blocking boolean)) |
83 | |
84 | (defun main-iterate-all (&rest args) |
85 | (declare (ignore args)) |
86 | (when (events-pending-p) |
f86d391e |
87 | (main-iteration-do nil) |
560af5c5 |
88 | (main-iterate-all))) |
89 | |
18eed2a0 |
90 | (system:add-fd-handler (gdk:connection-number) :input #'main-iterate-all) |
560af5c5 |
91 | (setq lisp::*periodic-polling-function* #'main-iterate-all) |
92 | (setq lisp::*max-event-to-sec* 0) |
93 | (setq lisp::*max-event-to-usec* 1000) |
94 | |
95 | |
96 | |
560af5c5 |
97 | ;;;; Metaclass for child classes |
f86d391e |
98 | |
99 | (defvar *container-to-child-class-mappings* (make-hash-table)) |
560af5c5 |
100 | |
101 | (eval-when (:compile-toplevel :load-toplevel :execute) |
f86d391e |
102 | (defclass child-class (virtual-slot-class)) |
560af5c5 |
103 | |
f86d391e |
104 | (defclass direct-child-slot-definition (direct-virtual-slot-definition) |
18eed2a0 |
105 | ((pname :reader slot-definition-pname))) |
560af5c5 |
106 | |
107 | (defclass effective-child-slot-definition |
108 | (effective-virtual-slot-definition))) |
109 | |
110 | |
f86d391e |
111 | (defmethod shared-initialize ((class child-class) names &rest initargs |
112 | &key container) |
560af5c5 |
113 | (declare (ignore initargs)) |
114 | (call-next-method) |
f86d391e |
115 | (setf |
116 | (gethash (find-class (first container)) *container-to-child-class-mappings*) |
117 | class)) |
560af5c5 |
118 | |
f86d391e |
119 | (defmethod initialize-instance ((slotd direct-child-slot-definition) |
18eed2a0 |
120 | &rest initargs &key pname) |
f86d391e |
121 | (declare (ignore initargs)) |
122 | (call-next-method) |
18eed2a0 |
123 | (if pname |
124 | (setf (slot-value slotd 'pname) pname) |
125 | ; ??? |
126 | (error "Need pname for slot with allocation :property"))) |
560af5c5 |
127 | |
128 | (defmethod direct-slot-definition-class ((class child-class) initargs) |
129 | (case (getf initargs :allocation) |
18eed2a0 |
130 | (:property (find-class 'direct-child-slot-definition)) |
560af5c5 |
131 | (t (call-next-method)))) |
132 | |
560af5c5 |
133 | (defmethod effective-slot-definition-class ((class child-class) initargs) |
134 | (case (getf initargs :allocation) |
18eed2a0 |
135 | (:property (find-class 'effective-child-slot-definition)) |
560af5c5 |
136 | (t (call-next-method)))) |
560af5c5 |
137 | |
b030f2d9 |
138 | (progn |
139 | (declaim (optimize (ext:inhibit-warnings 3))) |
140 | (defun %container-child-get-property (parent child pname gvalue)) |
141 | (defun %container-child-set-property (parent child pname gvalue))) |
142 | |
143 | |
18eed2a0 |
144 | (defmethod compute-virtual-slot-accessors |
560af5c5 |
145 | ((class child-class) (slotd effective-child-slot-definition) direct-slotds) |
b030f2d9 |
146 | |
560af5c5 |
147 | (with-slots (type) slotd |
18eed2a0 |
148 | (let ((pname (slot-definition-pname (first direct-slotds))) |
149 | (type-number (find-type-number type))) |
560af5c5 |
150 | (list |
151 | #'(lambda (object) |
152 | (with-slots (parent child) object |
153 | (with-gc-disabled |
18eed2a0 |
154 | (let ((gvalue (gvalue-new type-number))) |
155 | (%container-child-get-property parent child pname gvalue) |
156 | (unwind-protect |
f86d391e |
157 | (funcall |
158 | (intern-reader-function type) |
18eed2a0 |
159 | gvalue +gvalue-value-offset+) |
160 | (gvalue-free gvalue t)))))) |
560af5c5 |
161 | #'(lambda (value object) |
162 | (with-slots (parent child) object |
163 | (with-gc-disabled |
18eed2a0 |
164 | (let ((gvalue (gvalue-new type-number))) |
165 | (funcall |
166 | (intern-writer-function type) |
167 | value gvalue +gvalue-value-offset+) |
168 | (%container-child-set-property parent child pname gvalue) |
169 | (funcall |
170 | (intern-destroy-function type) |
171 | gvalue +gvalue-value-offset+) |
172 | (gvalue-free gvalue nil) |
173 | value)))))))) |
560af5c5 |
174 | |
175 | |
176 | (defmethod pcl::add-reader-method ((class child-class) generic-function slot-name) |
177 | (add-method |
178 | generic-function |
179 | (make-instance 'standard-method |
180 | :specializers (list (find-class 'widget)) |
181 | :lambda-list '(widget) |
182 | :function #'(lambda (args next-methods) |
183 | (declare (ignore next-methods)) |
184 | (child-slot-value (first args) slot-name))))) |
185 | |
186 | (defmethod pcl::add-writer-method |
187 | ((class child-class) generic-function slot-name) |
188 | (add-method |
189 | generic-function |
190 | (make-instance 'standard-method |
191 | :specializers (list (find-class t) (find-class 'widget)) |
192 | :lambda-list '(value widget) |
193 | :function #'(lambda (args next-methods) |
194 | (declare (ignore next-methods)) |
195 | (destructuring-bind (value widget) args |
196 | (setf |
197 | (child-slot-value widget slot-name) |
198 | value)))))) |
199 | |
200 | |
201 | (defmethod validate-superclass ((class child-class) (super pcl::standard-class)) |
18eed2a0 |
202 | ;(subtypep (class-name super) 'container-child) |
203 | t) |
560af5c5 |
204 | |
205 | |
f86d391e |
206 | (defclass container-child () |
207 | ((parent :initarg :parent :type container) |
208 | (child :initarg :child :type widget))) |
209 | |
210 | |
211 | ;;;; |
212 | |
18eed2a0 |
213 | (defbinding %container-class-list-child-properties () pointer |
214 | (class pointer) |
215 | (n-properties unsigned-int :out)) |
f86d391e |
216 | |
18eed2a0 |
217 | (defun query-container-class-child-properties (type-number) |
218 | (let ((class (type-class-ref type-number))) |
219 | (multiple-value-bind (array length) |
220 | (%container-class-list-child-properties class) |
221 | (unwind-protect |
222 | (map-c-array 'list #'identity array 'param length) |
223 | (deallocate-memory array))))) |
f86d391e |
224 | |
225 | (defun default-container-child-name (container-class) |
226 | (intern (format nil "~A-CHILD" container-class))) |
227 | |
228 | (defun expand-container-type (type-number &optional slots) |
229 | (let* ((class (type-from-number type-number)) |
230 | (super (supertype type-number)) |
231 | (child-class (default-container-child-name class)) |
18eed2a0 |
232 | (expanded-child-slots |
233 | (mapcar |
234 | #'(lambda (param) |
235 | (with-slots (name flags value-type documentation) param |
236 | (let* ((slot-name (default-slot-name name)) |
237 | (slot-type (type-from-number value-type #|t|#)) |
238 | (accessor |
239 | (default-slot-accessor class slot-name slot-type))) |
240 | `(,slot-name |
241 | :allocation :property |
242 | :pname ,name |
243 | ,@(cond |
244 | ((and |
245 | (member :writable flags) |
246 | (member :readable flags)) |
247 | (list :accessor accessor)) |
248 | ((member :writable flags) |
249 | (list :writer `(setf ,accessor))) |
250 | ((member :readable flags) |
251 | (list :reader accessor))) |
252 | ,@(when (or |
253 | (member :construct flags) |
254 | (member :writable flags)) |
255 | (list :initarg (intern (string slot-name) "KEYWORD"))) |
256 | :type ,slot-type |
257 | ,@(when documentation |
258 | (list :documentation documentation)))))) |
259 | (query-container-class-child-properties type-number)))) |
260 | `(progn |
261 | ,(expand-gobject-type type-number slots) |
262 | (defclass ,child-class |
263 | (,(default-container-child-name super)) |
264 | ,expanded-child-slots |
265 | (:metaclass child-class) |
266 | (:container ,class))))) |
267 | |
268 | (register-derivable-type 'container "GtkContainer" 'expand-container-type) |