chiark / gitweb /
Added PRINT-OBJECT method for widget class and a small bug fix
[clg] / gtk / gtkobject.lisp
1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 1999-2005 Espen S. Johnsen <espen@users.sf.net>
3 ;;
4 ;; Permission is hereby granted, free of charge, to any person obtaining
5 ;; a copy of this software and associated documentation files (the
6 ;; "Software"), to deal in the Software without restriction, including
7 ;; without limitation the rights to use, copy, modify, merge, publish,
8 ;; distribute, sublicense, and/or sell copies of the Software, and to
9 ;; permit persons to whom the Software is furnished to do so, subject to
10 ;; the following conditions:
11 ;;
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
14 ;;
15 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 ;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 ;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23 ;; $Id: gtkobject.lisp,v 1.32 2006-02-26 15:22:07 espen Exp $
24
25
26 (in-package "GTK")
27
28
29 ;;;; Superclass for the gtk class hierarchy
30
31 (eval-when (:compile-toplevel :load-toplevel :execute)
32   (init-types-in-library 
33    #.(concatenate 'string (pkg-config:pkg-variable "gtk+-2.0" "libdir") 
34                           "/libgtk-x11-2.0.so"))
35
36   (defclass %object (gobject)
37     ()
38     (:metaclass gobject-class)
39     (:gtype |gtk_object_get_type|)))
40
41
42 (defmethod initialize-instance ((object %object) &rest initargs &key signal)
43   (declare (ignore signal))
44   (call-next-method)
45   (dolist (signal-definition (get-all initargs :signal))
46     (apply #'signal-connect object signal-definition)))
47
48 (defmethod initialize-instance :around ((object %object) &rest initargs)
49   (declare (ignore initargs))
50   (call-next-method)
51   ;; Add a temorary reference which will be removed when the object is
52   ;; sinked
53   (reference-foreign (class-of object) (foreign-location object))
54   (%object-sink object))
55
56 (defbinding %object-sink () nil
57   (object %object))
58
59 ;;;; Main loop and event handling
60
61 (declaim (inline events-pending-p main-iteration))
62
63 (defbinding events-pending-p () boolean)
64
65 (defbinding get-current-event () gdk:event)
66
67 (defbinding main-do-event () nil
68   (event gdk:event))
69
70 (defbinding main () nil)
71
72 (defbinding main-level () int)
73
74 (defbinding main-quit () nil)
75
76 (defbinding main-iteration-do (&optional (blocking t)) boolean
77   (blocking boolean))
78
79 (defun main-iterate-all (&rest args)
80   (declare (ignore args))
81   (loop
82    while (events-pending-p)
83    do (main-iteration-do nil)))
84
85
86 ;;;; Metaclass for child classes
87  
88 (defvar *container-to-child-class-mappings* (make-hash-table))
89
90 (eval-when (:compile-toplevel :load-toplevel :execute)
91   (defclass child-class (virtual-slots-class)
92     ())
93
94   (defclass direct-child-slot-definition (direct-virtual-slot-definition)
95     ((pname :reader slot-definition-pname :initarg :pname)))
96
97   (defclass effective-child-slot-definition (effective-virtual-slot-definition)
98     ((pname :reader slot-definition-pname :initarg :pname)))
99
100
101 (defmethod shared-initialize ((class child-class) names &key container)
102   (call-next-method)
103   (setf
104    (gethash (find-class (first container)) *container-to-child-class-mappings*)
105     class))
106
107 (defmethod direct-slot-definition-class ((class child-class) &rest initargs)
108   (case (getf initargs :allocation)
109     (:property (find-class 'direct-child-slot-definition))
110     (t (call-next-method))))
111
112 (defmethod effective-slot-definition-class ((class child-class) &rest initargs)
113   (case (getf initargs :allocation)
114     (:property (find-class 'effective-child-slot-definition))
115     (t (call-next-method))))
116
117 (defmethod compute-effective-slot-definition-initargs ((class child-class) direct-slotds)
118   (if (eq (slot-definition-allocation (first direct-slotds)) :property)
119       (nconc 
120        (list :pname (most-specific-slot-value direct-slotds 'pname))
121        (call-next-method))
122     (call-next-method)))
123
124 (progn
125   #+cmu(declaim (optimize (inhibit-warnings 3)))
126   #+sbcl(declaim (muffle-conditions compiler-note))
127   (defun %container-child-get-property (parent child pname gvalue))
128   (defun %container-child-set-property (parent child pname gvalue)))
129
130
131 (defmethod initialize-internal-slot-functions ((slotd effective-child-slot-definition))
132   (let ((type (slot-definition-type slotd))
133         (pname (slot-definition-pname slotd)))
134     (setf 
135      (slot-value slotd 'getter)
136      #'(lambda (object)
137          (with-slots (parent child) object         
138            (let ((gvalue (gvalue-new type)))
139              (%container-child-get-property parent child pname gvalue)
140              (unwind-protect
141                  (funcall (reader-function type) gvalue +gvalue-value-offset+)
142                (gvalue-free gvalue t))))))
143     
144     (setf 
145      (slot-value slotd 'setter)
146      #'(lambda (value object)
147          (with-slots (parent child) object         
148            (let ((gvalue (gvalue-new type)))
149              (funcall (writer-function type) value gvalue +gvalue-value-offset+)
150              (%container-child-set-property parent child pname gvalue)
151              (gvalue-free gvalue t)
152              value)))))
153  
154   (call-next-method)))
155
156
157 (defmethod add-reader-method ((class child-class) generic-function slot-name)
158   (add-method
159    generic-function
160    (make-instance 'standard-method
161     :specializers (list (find-class 'widget))
162     :lambda-list '(widget)
163     :function #'(lambda (args next-methods)
164                   (declare (ignore next-methods))
165                   (child-property-value (first args) slot-name)))))
166
167 (defmethod add-writer-method
168     ((class child-class) generic-function slot-name)
169   (add-method
170    generic-function
171    (make-instance 'standard-method
172     :specializers (list (find-class t) (find-class 'widget))
173     :lambda-list '(value widget)
174     :function #'(lambda (args next-methods)
175                   (declare (ignore next-methods))
176                   (destructuring-bind (value widget) args
177                     (setf (child-property-value widget slot-name) value))))))
178
179
180 (defmethod validate-superclass ((class child-class) (super standard-class))
181   ;(subtypep (class-name super) 'container-child)
182   t)
183
184
185 (defclass container-child ()
186   ((parent :initarg :parent :type container)
187    (child :initarg :child :type widget)))
188
189
190 ;;;;
191
192 (defbinding %container-class-list-child-properties () pointer
193   (class pointer)
194   (n-properties unsigned-int :out))
195
196 (defun query-container-class-child-properties (type-number)
197   (let ((class (type-class-ref type-number)))
198     (multiple-value-bind (array length)
199         (%container-class-list-child-properties class)
200       (unwind-protect
201           (map-c-vector 'list #'identity array 'param length)
202         (deallocate-memory array)))))
203
204 (defun default-container-child-name (container-class)
205   (intern (format nil "~A-CHILD" container-class)))
206
207 (defun expand-container-type (type forward-p options)
208   (let* ((class (type-from-number type))
209          (super (supertype type))
210          (child-class (default-container-child-name class)))
211     (if forward-p 
212         (expand-gobject-type type t options)
213       `(progn
214          ,(expand-gobject-type type nil options)
215          (defclass ,child-class (,(default-container-child-name super))
216            ,(slot-definitions child-class 
217              (query-container-class-child-properties type) nil)
218            (:metaclass child-class)
219            (:container ,class))))))
220
221
222 (register-derivable-type 'container "GtkContainer" 'expand-container-type 'gobject-dependencies)