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