chiark / gitweb /
Bugfix in (setf object-arg)
[clg] / gtk / gtkobject.lisp
CommitLineData
560af5c5 1;; Common Lisp bindings for GTK+ v2.0
2;; Copyright (C) 1999-2000 Espen S. Johnsen <espejohn@online.no>
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
5881f841 18;; $Id: gtkobject.lisp,v 1.3 2000-08-16 17:30:36 espen Exp $
560af5c5 19
20
21(in-package "GTK")
22
23;;;; Misc utils
24
25(defun name-to-string (name)
26 (substitute #\_ #\- (string-downcase (string name))))
27
28(defun string-to-name (name &optional (package "KEYWORD"))
29 (intern (substitute #\- #\_ (string-upcase name)) package))
30
31
32;;;; Argument stuff
33
34(deftype arg () 'pointer)
35
36(defconstant +arg-type-offset+ 0)
37(defconstant +arg-name-offset+ 4)
38(defconstant +arg-value-offset+ 8)
39(defconstant +arg-size+ 16)
40
41(define-foreign arg-new () arg
42 (type type-number))
43
44(define-foreign %arg-free () nil
45 (arg arg)
46 (free-contents boolean))
47
48(defun arg-free (arg free-contents &optional alien)
49 (cond
50 (alien (%arg-free arg free-contents))
51 (t
52 (unless (null-pointer-p arg)
53 (when free-contents
54 (funcall
55 (get-destroy-function (type-from-number (arg-type arg)))
56 arg +arg-value-offset+))
57 (deallocate-memory arg)))))
58
59(define-foreign %arg-reset () nil
60 (arg arg))
61
62(defun arg-name (arg)
63 (funcall (get-reader-function '(static string)) arg +arg-name-offset+))
64
65(defun (setf arg-name) (name arg)
66 (funcall (get-writer-function '(static string)) name arg +arg-name-offset+)
67 name)
68
69(defun arg-type (arg)
70 (system:sap-ref-32 arg +arg-type-offset+))
71
72(defun (setf arg-type) (type arg)
73 (setf (system:sap-ref-32 arg +arg-type-offset+) type))
74
75(defun arg-value (arg &optional (type (type-from-number (arg-type arg))))
76 (funcall (get-reader-function type) arg +arg-value-offset+))
77
78;; One should never call this function on an arg whose value is already set
79(defun (setf arg-value)
80 (value arg &optional (type (type-from-number (arg-type arg))))
a250ff35 81 (funcall (get-writer-function type) value arg +arg-value-offset+)
560af5c5 82 value)
83
84(defun (setf return-arg-value)
85 (value arg &optional (type (type-from-number (arg-type arg))))
86 ; this is probably causing a memory leak
87 (funcall (get-writer-function type) value (arg-value arg 'pointer) 0)
88 value)
89
90(defun arg-array-ref (arg0 index)
91 (system:sap+ arg0 (* index +arg-size+)))
92
93
94;;;; Superclass for the gtk class hierarchy
95
96(eval-when (:compile-toplevel :load-toplevel :execute)
97 (defclass object (gobject)
98 ()
99; ((flags
100; :allocation :alien
101; :accessor object-flags
102; :type object-flags))
103 (:metaclass gobject-class)
104 (:alien-name "GtkObject")))
105
106
107(defmethod shared-initialize ((object object) names &rest initargs &key signals)
108 (declare (ignore initargs names))
109 (call-next-method)
110 (dolist (signal signals)
111 (apply #'signal-connect object signal)))
112
113
114(defmethod initialize-instance :after ((object object) &rest initargs &key)
115 (declare (ignore initargs))
116 (object-default-construct object)
117 (reference-instance object)
118 (object-sink object))
119
120
121(defmethod from-alien-initialzie-instance ((object object) &rest initargs)
122 (declare (ignore initargs))
123 (call-next-method)
124 (object-sink object))
125
126
127(define-foreign object-default-construct () nil
128 (object object))
129
130(define-foreign object-sink () nil
131 (object object))
132
133(define-foreign ("gtk_object_getv" object-get-arg) () nil
134 (object object)
135 (1 unsigned-int)
136 (arg arg))
137
138(define-foreign ("gtk_object_setv" object-set-arg) () nil
139 (object object)
140 (1 unsigned-int)
141 (arg arg))
142
143(defun object-arg (object name)
144 (with-gc-disabled
145 (let ((arg (arg-new 0)))
146 (setf (arg-name arg) name)
147 (object-get-arg object arg)
148 (let ((type (type-from-number (arg-type arg))))
149 (prog1
150 (arg-value arg type)
151 (arg-free arg t))))))
152
153(defun (setf object-arg) (value object name)
154 (with-gc-disabled
155 (let ((arg (arg-new 0)))
156 (setf (arg-name arg) name)
157 (object-get-arg object arg)
5881f841 158 (let* ((type-number (arg-type arg))
159 (type (type-from-number type-number)))
560af5c5 160 (%arg-reset arg)
5881f841 161 (setf (arg-type arg) type-number)
560af5c5 162 (setf (arg-value arg type) value)
163 (object-set-arg object arg)
164 (arg-free arg t))))
165 value)
166
167
168;;;; Callback and user data mechanism
169
170(declaim (fixnum *user-data-count*))
171
172(defvar *user-data* (make-hash-table))
173(defvar *user-data-count* 0)
174
175(defun register-user-data (object &optional destroy-function)
176 (check-type destroy-function (or null symbol function))
177; (incf *user-data-count*)
178 (setq *user-data-count* (the fixnum (1+ *user-data-count*)))
179 (setf
180 (gethash *user-data-count* *user-data*)
181 (cons object destroy-function))
182 *user-data-count*)
183
184
185(defun find-user-data (id)
186 (check-type id fixnum)
187 (multiple-value-bind (user-data p) (gethash id *user-data*)
188 (values (car user-data) p)))
189
190
191(defun register-callback-function (function)
192 (check-type function (or null symbol function))
193 ; We treat callbacks just as ordinary user data
194 (register-user-data function))
195
196
197(defun callback-trampoline (callback-id nargs arg-array)
198 (declare (fixnum callback-id nargs))
199 (let* ((return-arg (unless (null-pointer-p arg-array)
200 (arg-array-ref arg-array nargs)))
201 (return-type (if return-arg
202 (type-from-number (arg-type return-arg))
203 nil))
204 (args nil)
205 (callback-function (find-user-data callback-id)))
206
207 (dotimes (n nargs)
208 (push (arg-value (arg-array-ref arg-array (- nargs n 1))) args))
209
210 (labels ((invoke-callback ()
211 (restart-case
212 (unwind-protect
213 (let ((return-value (apply callback-function args)))
214 (when return-type
215 (setf (return-arg-value return-arg) return-value))))
216
217 (continue nil :report "Return from callback function"
218 (when return-type
219 (format
220 *query-io*
221 "Enter return value of type ~S: "
222 return-type)
223 (force-output *query-io*)
224 (setf
225 (return-arg-value return-arg)
226 (eval (read *query-io*)))))
227 (re-invoke nil :report "Re-invoke callback function"
228 (invoke-callback)))))
229 (invoke-callback))))
230
231
232(defun destroy-user-data (id)
233 (check-type id fixnum)
234 (let ((user-data (gethash id *user-data*)))
235 (when (cdr user-data)
236 (funcall (cdr user-data) (car user-data))))
237 (remhash id *user-data*))
238
239
240(defvar *callback-marshal* (system:foreign-symbol-address "callback_marshal"))
241(defvar *destroy-marshal* (system:foreign-symbol-address "destroy_marshal"))
242
243(defun after-gc-hook ()
244 (setf
245 (extern-alien "callback_trampoline" system-area-pointer)
246 (make-pointer (kernel:get-lisp-obj-address #'callback-trampoline))
247 (extern-alien "destroy_user_data" system-area-pointer)
248 (make-pointer (kernel:get-lisp-obj-address #'destroy-user-data))))
249
250(pushnew 'after-gc-hook ext:*after-gc-hooks*)
251(after-gc-hook)
252
253
254
255;;;; Main loop
256
257(declaim (inline events-pending-p main-iteration))
258
259(define-foreign ("gtk_events_pending" events-pending-p) () boolean)
260
261(define-foreign main-do-event () nil
262 (event gdk:event))
263
264(define-foreign main () nil)
265
266(define-foreign main-level () int)
267
268(define-foreign main-quit () nil)
269
270(define-foreign
271 ("gtk_main_iteration_do" main-iteration) (&optional (blocking t)) boolean
272 (blocking boolean))
273
274(defun main-iterate-all (&rest args)
275 (declare (ignore args))
276 (when (events-pending-p)
277 (main-iteration nil)
278 (main-iterate-all)))
279
280(system:add-fd-handler (gdk:event-poll-fd) :input #'main-iterate-all)
281(setq lisp::*periodic-polling-function* #'main-iterate-all)
282(setq lisp::*max-event-to-sec* 0)
283(setq lisp::*max-event-to-usec* 1000)
284
285
286
287;;;; Signals
288
289(define-foreign %signal-emit-stop () nil
290 (object object)
291 (signal-id unsigned-int))
292
293(define-foreign %signal-emit-stop-by-name (object signal) nil
294 (object object)
295 ((name-to-string signal) string))
296
297(defun signal-emit-stop (object signal)
298 (if (numberp signal)
299 (%signal-emit-stop object signal)
300 (%signal-emit-stop-by-name object signal)))
301
302(define-foreign %signal-connect-full
303 (object signal function after) unsigned-int
304 (object object)
305 ((name-to-string signal) string)
306 (0 unsigned-long)
307 (*callback-marshal* pointer)
308 ((register-callback-function function) unsigned-long)
309 (*destroy-marshal* pointer)
310 (nil boolean)
311 (after boolean))
312
313(defun signal-connect (object signal function
314 &key after ((:object callback-object)))
315 (let* ((callback-object (if (eq callback-object t)
316 object
317 callback-object))
318 (callback-function
319 (if callback-object
320 #'(lambda (&rest args) (apply function callback-object args))
321 function)))
322 (%signal-connect-full object signal callback-function after)))
323
324(define-foreign signal-disconnect () nil
325 (object object)
326 (handler unsigned-int))
327
328(define-foreign signal-handler-block () nil
329 (object object)
330 (handler unsigned-int))
331
332(define-foreign signal-handler-unblock () nil
333 (object object)
334 (handler unsigned-int))
335
336
337;;;; Metaclass used for subclasses of object
338
339(eval-when (:compile-toplevel :load-toplevel :execute)
340 (defclass object-class (gobject-class))
341
342 (defclass direct-object-slot-definition (direct-virtual-slot-definition))
343
344 (defclass effective-object-slot-definition
345 (effective-virtual-slot-definition)))
346
347
348(defmethod initialize-instance :after ((slotd direct-object-slot-definition)
349 &rest initargs &key)
350 (declare (ignore initargs))
351 (unless (slot-boundp slotd 'location)
352 (with-slots (pcl::name location pcl::class) slotd
353 (setf
354 location
355 (format nil "~A::~A"
356 (alien-type-name (class-name pcl::class))
357 (name-to-string pcl::name))))))
358
359
360(defmethod direct-slot-definition-class ((class object-class) initargs)
361 (case (getf initargs :allocation)
362 (:arg (find-class 'direct-object-slot-definition))
363 (t (call-next-method))))
364
365
366(defmethod effective-slot-definition-class ((class object-class) initargs)
367 (case (getf initargs :allocation)
368 (:arg (find-class 'effective-object-slot-definition))
369 (t (call-next-method))))
370
371
372(defmethod compute-virtual-slot-location
373 ((class object-class) (slotd effective-object-slot-definition)
374 direct-slotds)
375 (with-slots (type) slotd
376 (let ((location (slot-definition-location (first direct-slotds)))
377 (type-number (find-type-number type))
378 (reader (get-reader-function type))
379 (writer (get-writer-function type))
380 (destroy (get-destroy-function type)))
381 (list
382 #'(lambda (object)
383 (with-gc-disabled
384 (let ((arg (arg-new type-number)))
385 (setf (arg-name arg) location)
386 (object-get-arg object arg)
387 (prog1
388 (funcall reader arg +arg-value-offset+)
389 (arg-free arg t t)))))
390 #'(lambda (value object)
391 (with-gc-disabled
392 (let ((arg (arg-new type-number)))
393 (setf (arg-name arg) location)
394 (funcall writer value arg +arg-value-offset+)
395 (object-set-arg object arg)
396 (funcall destroy arg +arg-value-offset+)
397 (arg-free arg nil)
398 value)))))))
399
400
401(defmethod validate-superclass ((class object-class)
402 (super pcl::standard-class))
403 (subtypep (class-name super) 'object))
404
405
406;;;; Metaclasses used for widgets and containers
407
408(eval-when (:compile-toplevel :load-toplevel :execute)
409 (defclass widget-class (object-class))
410
411 (defclass container-class (widget-class)
412 (child-class)))
413
414
415(defvar *child-to-container-class-mappings* (make-hash-table))
416
417(defmethod shared-initialize ((class container-class) names
418 &rest initargs &key name child-class)
419 (declare (ignore initargs))
420 (call-next-method)
421 (with-slots ((child-class-slot child-class)) class
422 (setf
423 child-class-slot
424 (or
425 (first child-class)
426 (intern (format nil "~A-CHILD" (or name (class-name class)))))
427 (gethash child-class-slot *child-to-container-class-mappings*)
428 class)))
429
430
431(defmethod validate-superclass ((class widget-class)
432 (super pcl::standard-class))
433 (subtypep (class-name super) 'widget))
434
435(defmethod validate-superclass ((class container-class)
436 (super pcl::standard-class))
437 (subtypep (class-name super) 'container))
438
439
440
441;;;; Metaclass for child classes
442
443(eval-when (:compile-toplevel :load-toplevel :execute)
444 (defclass child-class (virtual-class))
445
446 (defclass direct-child-slot-definition (direct-virtual-slot-definition))
447
448 (defclass effective-child-slot-definition
449 (effective-virtual-slot-definition)))
450
451
452(defmethod initialize-instance ((slotd direct-child-slot-definition)
453 &rest initargs &key)
454 (declare (ignore initargs))
455 (call-next-method)
456 (unless (slot-boundp slotd 'location)
457 (with-slots (pcl::name location pcl::class) slotd
458 (setf
459 location
460 (format nil "~A::~A"
461 (alien-type-name
462 (gethash (class-name pcl::class) *child-to-container-class-mappings*))
463 (name-to-string pcl::name))))))
464
465
466(defmethod direct-slot-definition-class ((class child-class) initargs)
467 (case (getf initargs :allocation)
468 (:arg (find-class 'direct-child-slot-definition))
469 (t (call-next-method))))
470
471
472(defmethod effective-slot-definition-class ((class child-class) initargs)
473 (case (getf initargs :allocation)
474 (:arg (find-class 'effective-child-slot-definition))
475 (t (call-next-method))))
476
477
478(defmethod compute-virtual-slot-location
479 ((class child-class) (slotd effective-child-slot-definition) direct-slotds)
480 (with-slots (type) slotd
481 (let ((location (slot-definition-location (first direct-slotds)))
482 (type-number (find-type-number type))
483 (reader (get-reader-function type))
484 (writer (get-writer-function type))
485 (destroy (get-destroy-function type)))
486 (list
487 #'(lambda (object)
488 (with-slots (parent child) object
489 (with-gc-disabled
490 (let ((arg (arg-new type-number)))
491 (setf (arg-name arg) location)
492 (container-child-get-arg parent child arg)
493 (prog1
494 (funcall reader arg +arg-value-offset+)
495 (arg-free arg t t))))))
496 #'(lambda (value object)
497 (with-slots (parent child) object
498 (with-gc-disabled
499 (let ((arg (arg-new type-number)))
500 (setf (arg-name arg) location)
501 (funcall writer value arg +arg-value-offset+)
502 (container-child-set-arg parent child arg)
503 (funcall destroy arg +arg-value-offset+)
504 (arg-free arg nil)
505 value))))))))
506
507
508(defmethod pcl::add-reader-method ((class child-class) generic-function slot-name)
509 (add-method
510 generic-function
511 (make-instance 'standard-method
512 :specializers (list (find-class 'widget))
513 :lambda-list '(widget)
514 :function #'(lambda (args next-methods)
515 (declare (ignore next-methods))
516 (child-slot-value (first args) slot-name)))))
517
518(defmethod pcl::add-writer-method
519 ((class child-class) generic-function slot-name)
520 (add-method
521 generic-function
522 (make-instance 'standard-method
523 :specializers (list (find-class t) (find-class 'widget))
524 :lambda-list '(value widget)
525 :function #'(lambda (args next-methods)
526 (declare (ignore next-methods))
527 (destructuring-bind (value widget) args
528 (setf
529 (child-slot-value widget slot-name)
530 value))))))
531
532
533(defmethod validate-superclass ((class child-class) (super pcl::standard-class))
534 (subtypep (class-name super) 'container-child))
535
536