chiark / gitweb /
Moved callback mechanism and signal system from gtk to glib
[clg] / gtk / gtkobject.lisp
1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 1999-2000 Espen S. Johnsen <esj@ostud.cs.uit.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.7 2000-11-09 20:30:16 espen Exp $
19
20
21 (in-package "GTK")
22
23
24 ;;;; Initializing
25
26 (setf (alien-type-name 'pointer) "gpointer")
27
28
29 ;;;; Misc utils
30
31 (defun name-to-string (name)
32   (substitute #\_ #\- (string-downcase (string name))))
33
34 (defun string-to-name (name &optional (package "KEYWORD"))
35   (intern (substitute #\- #\_ (string-upcase name)) package))
36
37
38 ;;;; Argument stuff
39
40 (deftype arg () 'pointer)
41
42 (defconstant +arg-type-offset+ 0)
43 (defconstant +arg-name-offset+ 4)
44 (defconstant +arg-value-offset+ 8)
45 (defconstant +arg-size+ 16)
46
47 (define-foreign arg-new () arg
48   (type type-number))
49
50 (define-foreign %arg-free () nil
51   (arg arg)
52   (free-contents boolean))
53
54 (defun arg-free (arg free-contents &optional alien)
55   (cond
56    (alien (%arg-free arg free-contents))
57    (t
58     (unless (null-pointer-p arg)
59       (when free-contents
60         (funcall
61          (get-destroy-function (type-from-number (arg-type arg)))
62          arg +arg-value-offset+))
63       (deallocate-memory arg)))))
64
65 (define-foreign %arg-reset () nil
66   (arg arg))
67
68 (defun arg-name (arg)
69   (funcall (get-reader-function '(static string)) arg +arg-name-offset+))
70
71 (defun (setf arg-name) (name arg)
72   (funcall (get-writer-function '(static string)) name arg +arg-name-offset+)
73   name)
74
75 (defun arg-type (arg)
76   (system:sap-ref-32 arg +arg-type-offset+))
77
78 (defun (setf arg-type) (type arg)
79   (setf (system:sap-ref-32 arg +arg-type-offset+) type))
80
81 (defun arg-value (arg &optional (type (type-from-number (arg-type arg))))
82   (funcall (get-reader-function type) arg +arg-value-offset+))
83
84 ;; One should never call this function on an arg whose value is already set
85 (defun (setf arg-value)
86     (value arg &optional (type (type-from-number (arg-type arg))))
87   (funcall (get-writer-function type) value arg +arg-value-offset+)
88   value)
89
90 (defun (setf return-arg-value)
91     (value arg &optional (type (type-from-number (arg-type arg))))
92   ; this is probably causing a memory leak
93   (funcall (get-writer-function type) value (arg-value arg 'pointer) 0)
94   value)
95
96 (defun arg-array-ref (arg0 index)
97   (system:sap+ arg0 (* index +arg-size+)))
98
99
100 ;;;; Superclass for the gtk class hierarchy
101
102 (eval-when (:compile-toplevel :load-toplevel :execute)
103   (defclass object (gobject)
104     ()
105 ;   ((flags
106 ;     :allocation :alien
107 ;     :accessor object-flags
108 ;     :type object-flags))
109     (:metaclass gobject-class)
110     (:alien-name "GtkObject")))
111
112
113 (defmethod shared-initialize ((object object) names &rest initargs &key signals)
114   (declare (ignore initargs names))
115   (call-next-method)
116   (dolist (signal signals)
117     (apply #'signal-connect object signal)))
118
119
120 (defmethod initialize-instance :after ((object object) &rest initargs &key)
121   (declare (ignore initargs))
122   (object-default-construct object)
123   (reference-instance object)
124   (object-sink object))
125
126
127 (defmethod from-alien-initialzie-instance ((object object) &rest initargs)
128   (declare (ignore initargs))
129   (call-next-method)
130   (object-sink object))
131
132
133 (define-foreign object-default-construct () nil
134   (object object))
135
136 (define-foreign object-sink () nil
137   (object object))
138
139 (define-foreign ("gtk_object_getv" object-get-arg) () nil
140   (object object)
141   (1 unsigned-int)
142   (arg arg))
143
144 (define-foreign ("gtk_object_setv" object-set-arg) () nil
145   (object object)
146   (1 unsigned-int)
147   (arg arg))
148
149 (defun object-arg (object name)
150   (with-gc-disabled
151     (let ((arg (arg-new 0)))
152       (setf (arg-name arg) name)
153       (object-get-arg object arg)
154       (let ((type (type-from-number (arg-type arg))))
155         (prog1
156             (arg-value arg type)
157           (arg-free arg t))))))
158
159 (defun (setf object-arg) (value object name)
160   (with-gc-disabled
161     (let ((arg (arg-new 0)))
162       (setf (arg-name arg) name)
163       (object-get-arg object arg)
164       (let* ((type-number (arg-type arg))
165              (type (type-from-number type-number)))
166         (%arg-reset arg)
167         (setf (arg-type arg) type-number)
168         (setf (arg-value arg type) value)
169         (object-set-arg object arg)
170         (arg-free arg t))))
171   value)
172
173
174
175 ;;;; Main loop, timeouts and idle functions
176
177 (declaim (inline events-pending-p main-iteration))
178
179 (define-foreign ("gtk_events_pending" events-pending-p) () boolean)
180
181 (define-foreign get-current-event () gdk:event)
182
183 (define-foreign main-do-event () nil
184   (event gdk:event))
185
186 (define-foreign main () nil)
187
188 (define-foreign main-level () int)
189
190 (define-foreign main-quit () nil)
191
192 (define-foreign
193     ("gtk_main_iteration_do" main-iteration) (&optional (blocking t)) boolean
194   (blocking boolean))
195
196 (defun main-iterate-all (&rest args)
197   (declare (ignore args))
198   (when (events-pending-p)
199     (main-iteration nil)
200     (main-iterate-all)))
201
202 (define-foreign ("gtk_timeout_add_full" timeout-add)
203     (interval function) unsigned-int
204   (interval (unsigned 32))
205   (0 unsigned-long)
206   (*callback-marshal* pointer)
207   ((register-callback-function function) unsigned-long)
208   (*destroy-marshal* pointer))
209
210 (define-foreign timeout-remove () nil
211   (timeout-handler-id unsigned-int))
212   
213 (define-foreign ("gtk_idle_add_full" idle-add)
214     (function &optional (priority 200)) unsigned-int
215   (priority int)
216   (0 unsigned-long)
217   (*callback-marshal* pointer)
218   ((register-callback-function function) unsigned-long)
219   (*destroy-marshal* pointer))
220
221 (define-foreign idle-remove () nil
222   (idle-handler-id unsigned-int))
223
224
225 (system:add-fd-handler (gdk:event-poll-fd) :input #'main-iterate-all)
226 (setq lisp::*periodic-polling-function* #'main-iterate-all)
227 (setq lisp::*max-event-to-sec* 0)
228 (setq lisp::*max-event-to-usec* 1000)
229
230
231
232 ;;;; Metaclass used for subclasses of object
233
234 (eval-when (:compile-toplevel :load-toplevel :execute)
235   (defclass object-class (gobject-class))
236
237   (defclass direct-object-slot-definition (direct-virtual-slot-definition))
238
239   (defclass effective-object-slot-definition
240     (effective-virtual-slot-definition)))
241
242
243 (defmethod initialize-instance :after ((slotd direct-object-slot-definition)
244                                    &rest initargs &key)
245   (declare (ignore initargs))
246   (unless (slot-boundp slotd 'location)
247     (with-slots (pcl::name location pcl::class) slotd
248       (setf
249        location 
250        (format nil "~A::~A"
251         (alien-type-name (class-name pcl::class))
252         (name-to-string pcl::name))))))
253
254
255 (defmethod direct-slot-definition-class ((class object-class) initargs)
256   (case (getf initargs :allocation)
257     (:arg (find-class 'direct-object-slot-definition))
258     (t (call-next-method))))
259
260
261 (defmethod effective-slot-definition-class ((class object-class) initargs)
262   (case (getf initargs :allocation)
263     (:arg (find-class 'effective-object-slot-definition))
264     (t (call-next-method))))
265   
266
267 (defmethod compute-virtual-slot-location
268     ((class object-class) (slotd effective-object-slot-definition)
269      direct-slotds)
270   (with-slots (type) slotd
271     (let ((location (slot-definition-location (first direct-slotds)))
272           (type-number (find-type-number type))
273           (reader (get-reader-function type))
274           (writer (get-writer-function type))
275           (destroy (get-destroy-function type)))
276       (list
277        #'(lambda (object)
278            (with-gc-disabled
279              (let ((arg (arg-new type-number)))
280                (setf (arg-name arg) location)
281                (object-get-arg object arg)
282                (prog1
283                    (funcall reader arg +arg-value-offset+)
284                  (arg-free arg t t)))))
285        #'(lambda (value object)
286            (with-gc-disabled
287              (let ((arg (arg-new type-number)))
288                (setf (arg-name arg) location)
289                (funcall writer value arg +arg-value-offset+)
290                (object-set-arg object arg)
291                (funcall destroy arg +arg-value-offset+)
292                (arg-free arg nil)
293                value)))))))
294
295
296 (defmethod validate-superclass ((class object-class)
297                                 (super pcl::standard-class))
298   (subtypep (class-name super) 'object))
299   
300
301 ;;;; Metaclasses used for widgets and containers
302
303 (eval-when (:compile-toplevel :load-toplevel :execute)
304   (defclass widget-class (object-class))
305
306   (defclass container-class (widget-class)
307     (child-class)))
308
309
310 (defvar *child-to-container-class-mappings* (make-hash-table))
311
312 (defmethod shared-initialize ((class container-class) names
313                               &rest initargs &key name child-class)
314   (declare (ignore initargs))
315   (call-next-method)
316   (with-slots ((child-class-slot child-class)) class
317     (setf
318      child-class-slot
319      (or
320       (first child-class)
321       (intern (format nil "~A-CHILD" (or name (class-name class)))))
322      (gethash child-class-slot *child-to-container-class-mappings*)
323      class)))
324
325
326 (defmethod validate-superclass ((class widget-class)
327                                 (super pcl::standard-class))
328   (subtypep (class-name super) 'widget))
329
330 (defmethod validate-superclass ((class container-class)
331                                 (super pcl::standard-class))
332   (subtypep (class-name super) 'container))
333
334
335
336 ;;;; Metaclass for child classes
337
338 (eval-when (:compile-toplevel :load-toplevel :execute)
339   (defclass child-class (virtual-class))
340
341   (defclass direct-child-slot-definition (direct-virtual-slot-definition))
342
343   (defclass effective-child-slot-definition
344     (effective-virtual-slot-definition)))
345
346
347 (defmethod initialize-instance  ((slotd direct-child-slot-definition)
348                                  &rest initargs &key)
349   (declare (ignore initargs))
350   (call-next-method)
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
357          (gethash (class-name pcl::class) *child-to-container-class-mappings*))
358         (name-to-string pcl::name))))))
359
360
361 (defmethod direct-slot-definition-class ((class child-class) initargs)
362   (case (getf initargs :allocation)
363     (:arg (find-class 'direct-child-slot-definition))
364     (t (call-next-method))))
365
366
367 (defmethod effective-slot-definition-class ((class child-class) initargs)
368   (case (getf initargs :allocation)
369     (:arg (find-class 'effective-child-slot-definition))
370     (t (call-next-method))))
371   
372
373 (defmethod compute-virtual-slot-location
374     ((class child-class) (slotd effective-child-slot-definition) 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-slots (parent child) object       
384              (with-gc-disabled
385                (let ((arg (arg-new type-number)))
386                  (setf (arg-name arg) location)
387                  (container-child-get-arg parent child arg)
388                  (prog1
389                      (funcall reader arg +arg-value-offset+)
390                    (arg-free arg t t))))))
391        #'(lambda (value object)
392            (with-slots (parent child) object       
393              (with-gc-disabled
394                (let ((arg (arg-new type-number)))
395                  (setf (arg-name arg) location)
396                  (funcall writer value arg +arg-value-offset+)
397                  (container-child-set-arg parent child arg)
398                  (funcall destroy arg +arg-value-offset+)
399                  (arg-free arg nil)
400                  value))))))))
401
402
403 (defmethod pcl::add-reader-method ((class child-class) generic-function slot-name)
404   (add-method
405    generic-function
406    (make-instance 'standard-method
407                   :specializers (list (find-class 'widget))
408                   :lambda-list '(widget)
409                   :function #'(lambda (args next-methods)
410                                 (declare (ignore next-methods))
411                                 (child-slot-value (first args) slot-name)))))
412
413 (defmethod pcl::add-writer-method
414     ((class child-class) generic-function slot-name)
415   (add-method
416    generic-function
417    (make-instance 'standard-method
418                   :specializers (list (find-class t) (find-class 'widget))
419                   :lambda-list '(value widget)
420                   :function #'(lambda (args next-methods)
421                                 (declare (ignore next-methods))
422                                 (destructuring-bind (value widget) args
423                                   (setf
424                                    (child-slot-value widget slot-name)
425                                    value))))))
426
427
428 (defmethod validate-superclass ((class child-class) (super pcl::standard-class))
429   (subtypep (class-name super) 'container-child))
430
431