chiark / gitweb /
Moved object user data from gtk to glib
[clg] / gtk / gtkobject.lisp
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
18 ;; $Id: gtkobject.lisp,v 1.5 2000/08/23 21:41:10 espen Exp $
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))))
81   (funcall (get-writer-function type) value arg +arg-value-offset+)
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)
158       (let* ((type-number (arg-type arg))
159              (type (type-from-number type-number)))
160         (%arg-reset arg)
161         (setf (arg-type arg) type-number)
162         (setf (arg-value arg type) value)
163         (object-set-arg object arg)
164         (arg-free arg t))))
165   value)
166
167
168 ;;;; Callback mechanism
169
170 (defun register-callback-function (function)
171   (check-type function (or null symbol function))
172   ; We treat callbacks just as ordinary user data
173   (register-user-data function))
174
175 (defun callback-trampoline (callback-id nargs arg-array)
176   (declare (fixnum callback-id nargs))
177   (let* ((return-arg (unless (null-pointer-p arg-array)
178                        (arg-array-ref arg-array nargs)))
179          (return-type (if return-arg
180                           (type-from-number (arg-type return-arg))
181                         nil))
182          (args nil)
183          (callback-function (find-user-data callback-id)))
184     
185     (dotimes (n nargs)
186       (push (arg-value (arg-array-ref arg-array (- nargs n 1))) args))
187
188     (labels ((invoke-callback ()
189                (restart-case
190                    (unwind-protect
191                        (let ((return-value (apply callback-function args)))
192                          (when return-type
193                            (setf (return-arg-value return-arg) return-value))))
194                 
195                  (continue nil :report "Return from callback function"
196                   (when return-type
197                     (format
198                      *query-io*
199                      "Enter return value of type ~S: "
200                      return-type)
201                     (force-output *query-io*)
202                     (setf
203                      (return-arg-value return-arg)
204                      (eval (read *query-io*)))))
205                  (re-invoke nil :report "Re-invoke callback function"
206                   (invoke-callback)))))
207       (invoke-callback))))
208
209 (defvar *callback-marshal* (system:foreign-symbol-address "callback_marshal"))
210 (setq *destroy-marshal* (system:foreign-symbol-address "destroy_marshal"))
211
212 (defun after-gc-hook ()
213   (setf
214    (extern-alien "callback_trampoline" system-area-pointer)
215    (make-pointer (kernel:get-lisp-obj-address #'callback-trampoline))
216    (extern-alien "destroy_user_data" system-area-pointer)
217    (make-pointer (kernel:get-lisp-obj-address #'destroy-user-data))))
218
219 (pushnew 'after-gc-hook ext:*after-gc-hooks*)
220 (after-gc-hook)
221
222
223
224 ;;;; Main loop, timeouts and idle functions
225
226 (declaim (inline events-pending-p main-iteration))
227
228 (define-foreign ("gtk_events_pending" events-pending-p) () boolean)
229
230 (define-foreign get-current-event () gdk:event)
231
232 (define-foreign main-do-event () nil
233   (event gdk:event))
234
235 (define-foreign main () nil)
236
237 (define-foreign main-level () int)
238
239 (define-foreign main-quit () nil)
240
241 (define-foreign
242     ("gtk_main_iteration_do" main-iteration) (&optional (blocking t)) boolean
243   (blocking boolean))
244
245 (defun main-iterate-all (&rest args)
246   (declare (ignore args))
247   (when (events-pending-p)
248     (main-iteration nil)
249     (main-iterate-all)))
250
251 (define-foreign ("gtk_timeout_add_full" timeout-add)
252     (interval function) unsigned-int
253   (interval (unsigned 32))
254   (0 unsigned-long)
255   (*callback-marshal* pointer)
256   ((register-callback-function function) unsigned-long)
257   (*destroy-marshal* pointer))
258
259 (define-foreign timeout-remove () nil
260   (timeout-handler-id unsigned-int))
261   
262 (define-foreign ("gtk_idle_add_full" idle-add)
263     (function &optional (priority 200)) unsigned-int
264   (priority int)
265   (0 unsigned-long)
266   (*callback-marshal* pointer)
267   ((register-callback-function function) unsigned-long)
268   (*destroy-marshal* pointer))
269
270 (define-foreign idle-remove () nil
271   (idle-handler-id unsigned-int))
272
273
274 (system:add-fd-handler (gdk:event-poll-fd) :input #'main-iterate-all)
275 (setq lisp::*periodic-polling-function* #'main-iterate-all)
276 (setq lisp::*max-event-to-sec* 0)
277 (setq lisp::*max-event-to-usec* 1000)
278
279
280
281 ;;;; Signals
282
283 (define-foreign %signal-emit-stop () nil
284   (object object)
285   (signal-id unsigned-int))
286
287 (define-foreign %signal-emit-stop-by-name (object signal) nil
288   (object object)
289   ((name-to-string signal) string))
290
291 (defun signal-emit-stop (object signal)
292   (if (numberp signal)
293       (%signal-emit-stop object signal)
294     (%signal-emit-stop-by-name object signal)))
295
296 (define-foreign %signal-connect-full
297     (object signal function after) unsigned-int
298   (object object)
299   ((name-to-string signal) string)
300   (0 unsigned-long)
301   (*callback-marshal* pointer)
302   ((register-callback-function function) unsigned-long)
303   (*destroy-marshal* pointer)
304   (nil boolean)
305   (after boolean))
306
307 (defun signal-connect (object signal function
308                        &key after ((:object callback-object)))
309   (let* ((callback-object (if (eq callback-object t)
310                               object
311                             callback-object))
312          (callback-function
313           (if callback-object
314               #'(lambda (&rest args) (apply function callback-object args))
315             function)))
316     (%signal-connect-full object signal callback-function after)))
317
318 (define-foreign signal-disconnect () nil
319   (object object)
320   (handler unsigned-int))
321
322 (define-foreign signal-handler-block () nil
323   (object object)
324   (handler unsigned-int))
325
326 (define-foreign signal-handler-unblock () nil
327   (object object)
328   (handler unsigned-int))
329
330
331 ;;;; Metaclass used for subclasses of object
332
333 (eval-when (:compile-toplevel :load-toplevel :execute)
334   (defclass object-class (gobject-class))
335
336   (defclass direct-object-slot-definition (direct-virtual-slot-definition))
337
338   (defclass effective-object-slot-definition
339     (effective-virtual-slot-definition)))
340
341
342 (defmethod initialize-instance :after ((slotd direct-object-slot-definition)
343                                    &rest initargs &key)
344   (declare (ignore initargs))
345   (unless (slot-boundp slotd 'location)
346     (with-slots (pcl::name location pcl::class) slotd
347       (setf
348        location 
349        (format nil "~A::~A"
350         (alien-type-name (class-name pcl::class))
351         (name-to-string pcl::name))))))
352
353
354 (defmethod direct-slot-definition-class ((class object-class) initargs)
355   (case (getf initargs :allocation)
356     (:arg (find-class 'direct-object-slot-definition))
357     (t (call-next-method))))
358
359
360 (defmethod effective-slot-definition-class ((class object-class) initargs)
361   (case (getf initargs :allocation)
362     (:arg (find-class 'effective-object-slot-definition))
363     (t (call-next-method))))
364   
365
366 (defmethod compute-virtual-slot-location
367     ((class object-class) (slotd effective-object-slot-definition)
368      direct-slotds)
369   (with-slots (type) slotd
370     (let ((location (slot-definition-location (first direct-slotds)))
371           (type-number (find-type-number type))
372           (reader (get-reader-function type))
373           (writer (get-writer-function type))
374           (destroy (get-destroy-function type)))
375       (list
376        #'(lambda (object)
377            (with-gc-disabled
378              (let ((arg (arg-new type-number)))
379                (setf (arg-name arg) location)
380                (object-get-arg object arg)
381                (prog1
382                    (funcall reader arg +arg-value-offset+)
383                  (arg-free arg t t)))))
384        #'(lambda (value object)
385            (with-gc-disabled
386              (let ((arg (arg-new type-number)))
387                (setf (arg-name arg) location)
388                (funcall writer value arg +arg-value-offset+)
389                (object-set-arg object arg)
390                (funcall destroy arg +arg-value-offset+)
391                (arg-free arg nil)
392                value)))))))
393
394
395 (defmethod validate-superclass ((class object-class)
396                                 (super pcl::standard-class))
397   (subtypep (class-name super) 'object))
398   
399
400 ;;;; Metaclasses used for widgets and containers
401
402 (eval-when (:compile-toplevel :load-toplevel :execute)
403   (defclass widget-class (object-class))
404
405   (defclass container-class (widget-class)
406     (child-class)))
407
408
409 (defvar *child-to-container-class-mappings* (make-hash-table))
410
411 (defmethod shared-initialize ((class container-class) names
412                               &rest initargs &key name child-class)
413   (declare (ignore initargs))
414   (call-next-method)
415   (with-slots ((child-class-slot child-class)) class
416     (setf
417      child-class-slot
418      (or
419       (first child-class)
420       (intern (format nil "~A-CHILD" (or name (class-name class)))))
421      (gethash child-class-slot *child-to-container-class-mappings*)
422      class)))
423
424
425 (defmethod validate-superclass ((class widget-class)
426                                 (super pcl::standard-class))
427   (subtypep (class-name super) 'widget))
428
429 (defmethod validate-superclass ((class container-class)
430                                 (super pcl::standard-class))
431   (subtypep (class-name super) 'container))
432
433
434
435 ;;;; Metaclass for child classes
436
437 (eval-when (:compile-toplevel :load-toplevel :execute)
438   (defclass child-class (virtual-class))
439
440   (defclass direct-child-slot-definition (direct-virtual-slot-definition))
441
442   (defclass effective-child-slot-definition
443     (effective-virtual-slot-definition)))
444
445
446 (defmethod initialize-instance  ((slotd direct-child-slot-definition)
447                                  &rest initargs &key)
448   (declare (ignore initargs))
449   (call-next-method)
450   (unless (slot-boundp slotd 'location)
451     (with-slots (pcl::name location pcl::class) slotd
452       (setf
453        location 
454        (format nil "~A::~A"
455         (alien-type-name
456          (gethash (class-name pcl::class) *child-to-container-class-mappings*))
457         (name-to-string pcl::name))))))
458
459
460 (defmethod direct-slot-definition-class ((class child-class) initargs)
461   (case (getf initargs :allocation)
462     (:arg (find-class 'direct-child-slot-definition))
463     (t (call-next-method))))
464
465
466 (defmethod effective-slot-definition-class ((class child-class) initargs)
467   (case (getf initargs :allocation)
468     (:arg (find-class 'effective-child-slot-definition))
469     (t (call-next-method))))
470   
471
472 (defmethod compute-virtual-slot-location
473     ((class child-class) (slotd effective-child-slot-definition) direct-slotds)
474   (with-slots (type) slotd
475     (let ((location (slot-definition-location (first direct-slotds)))
476           (type-number (find-type-number type))
477           (reader (get-reader-function type))
478           (writer (get-writer-function type))
479           (destroy (get-destroy-function type)))
480       (list
481        #'(lambda (object)
482            (with-slots (parent child) object       
483              (with-gc-disabled
484                (let ((arg (arg-new type-number)))
485                  (setf (arg-name arg) location)
486                  (container-child-get-arg parent child arg)
487                  (prog1
488                      (funcall reader arg +arg-value-offset+)
489                    (arg-free arg t t))))))
490        #'(lambda (value object)
491            (with-slots (parent child) object       
492              (with-gc-disabled
493                (let ((arg (arg-new type-number)))
494                  (setf (arg-name arg) location)
495                  (funcall writer value arg +arg-value-offset+)
496                  (container-child-set-arg parent child arg)
497                  (funcall destroy arg +arg-value-offset+)
498                  (arg-free arg nil)
499                  value))))))))
500
501
502 (defmethod pcl::add-reader-method ((class child-class) generic-function slot-name)
503   (add-method
504    generic-function
505    (make-instance 'standard-method
506                   :specializers (list (find-class 'widget))
507                   :lambda-list '(widget)
508                   :function #'(lambda (args next-methods)
509                                 (declare (ignore next-methods))
510                                 (child-slot-value (first args) slot-name)))))
511
512 (defmethod pcl::add-writer-method
513     ((class child-class) generic-function slot-name)
514   (add-method
515    generic-function
516    (make-instance 'standard-method
517                   :specializers (list (find-class t) (find-class 'widget))
518                   :lambda-list '(value widget)
519                   :function #'(lambda (args next-methods)
520                                 (declare (ignore next-methods))
521                                 (destructuring-bind (value widget) args
522                                   (setf
523                                    (child-slot-value widget slot-name)
524                                    value))))))
525
526
527 (defmethod validate-superclass ((class child-class) (super pcl::standard-class))
528   (subtypep (class-name super) 'container-child))
529
530