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