chiark / gitweb /
Modifer-type manually defined
[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.27 2005-04-23 16:48:52 espen Exp $
24
25
26 (in-package "GTK")
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
39 ;;;; Superclass for the gtk class hierarchy
40
41 (eval-when (:compile-toplevel :load-toplevel :execute)
42   (init-types-in-library 
43    #.(concatenate 'string (pkg-config:pkg-variable "gtk+-2.0" "libdir") 
44                           "/libgtk-x11-2.0.so"))
45
46   (defclass %object (gobject)
47     ()
48     (:metaclass gobject-class)
49     (:gtype |gtk_object_get_type|)))
50
51
52 (defmethod initialize-instance ((object %object) &rest initargs &key signal)
53   (declare (ignore signal))
54   (call-next-method)
55   (reference-foreign (class-of object) (proxy-location object))
56   (dolist (signal-definition (get-all initargs :signal))
57     (apply #'signal-connect object signal-definition)))
58
59 (defmethod initialize-instance :around ((object %object) &rest initargs)
60   (declare (ignore initargs))
61   (call-next-method)
62   (%object-sink object))
63
64 (defbinding %object-sink () nil
65   (object %object))
66
67 ;;;; Main loop and event handling
68
69 (declaim (inline events-pending-p main-iteration))
70
71 (defbinding events-pending-p () boolean)
72
73 (defbinding get-current-event () gdk:event)
74
75 (defbinding main-do-event () nil
76   (event gdk:event))
77
78 (defbinding main () nil)
79
80 (defbinding main-level () int)
81
82 (defbinding main-quit () nil)
83
84 (defbinding main-iteration-do (&optional (blocking t)) boolean
85   (blocking boolean))
86
87 (defun main-iterate-all (&rest args)
88   (declare (ignore args))
89   (loop
90    while (events-pending-p)
91    do (main-iteration-do nil)))
92
93
94 ;;;; Metaclass for child classes
95  
96 (defvar *container-to-child-class-mappings* (make-hash-table))
97
98 (eval-when (:compile-toplevel :load-toplevel :execute)
99   (defclass child-class (virtual-slots-class)
100     ())
101
102   (defclass direct-child-slot-definition (direct-virtual-slot-definition)
103     ((pname :reader slot-definition-pname :initarg :pname)))
104
105   (defclass effective-child-slot-definition (effective-virtual-slot-definition)
106     ((pname :reader slot-definition-pname :initarg :pname)))
107
108
109 (defmethod shared-initialize ((class child-class) names &key container)
110   (call-next-method)
111   (setf
112    (gethash (find-class (first container)) *container-to-child-class-mappings*)
113     class))
114
115 (defmethod direct-slot-definition-class ((class child-class) &rest initargs)
116   (case (getf initargs :allocation)
117     (:property (find-class 'direct-child-slot-definition))
118     (t (call-next-method))))
119
120 (defmethod effective-slot-definition-class ((class child-class) &rest initargs)
121   (case (getf initargs :allocation)
122     (:property (find-class 'effective-child-slot-definition))
123     (t (call-next-method))))
124
125 (defmethod compute-effective-slot-definition-initargs ((class child-class) direct-slotds)
126   (if (eq (most-specific-slot-value direct-slotds 'allocation) :property)
127       (nconc 
128        (list :pname (most-specific-slot-value direct-slotds 'pname))
129        (call-next-method))
130     (call-next-method)))
131
132 (progn
133   #+cmu(declaim (optimize (inhibit-warnings 3)))
134   #+sbcl(declaim (muffle-conditions compiler-note))
135   (defun %container-child-get-property (parent child pname gvalue))
136   (defun %container-child-set-property (parent child pname gvalue)))
137
138
139 (defmethod initialize-internal-slot-functions ((slotd effective-child-slot-definition))
140   (let ((type (slot-definition-type slotd))
141         (pname (slot-definition-pname slotd)))
142     (setf 
143      (slot-value slotd 'getter)
144      #'(lambda (object)
145          (with-slots (parent child) object         
146            (let ((gvalue (gvalue-new type)))
147              (%container-child-get-property parent child pname gvalue)
148              (unwind-protect
149                  (funcall (reader-function type) gvalue +gvalue-value-offset+)
150                (gvalue-free gvalue t))))))
151     
152     (setf 
153      (slot-value slotd 'setter)
154      #'(lambda (value object)
155          (with-slots (parent child) object         
156            (let ((gvalue (gvalue-new type)))
157              (funcall (writer-function type) value gvalue +gvalue-value-offset+)
158              (%container-child-set-property parent child pname gvalue)
159              (gvalue-free gvalue t)
160              value)))))
161  
162   (call-next-method)))
163
164
165 (defmethod add-reader-method ((class child-class) generic-function slot-name)
166   (add-method
167    generic-function
168    (make-instance 'standard-method
169     :specializers (list (find-class 'widget))
170     :lambda-list '(widget)
171     :function #'(lambda (args next-methods)
172                   (declare (ignore next-methods))
173                   (child-property-value (first args) slot-name)))))
174
175 (defmethod add-writer-method
176     ((class child-class) generic-function slot-name)
177   (add-method
178    generic-function
179    (make-instance 'standard-method
180     :specializers (list (find-class t) (find-class 'widget))
181     :lambda-list '(value widget)
182     :function #'(lambda (args next-methods)
183                   (declare (ignore next-methods))
184                   (destructuring-bind (value widget) args
185                     (setf (child-property-value widget slot-name) value))))))
186
187
188 (defmethod validate-superclass ((class child-class) (super standard-class))
189   ;(subtypep (class-name super) 'container-child)
190   t)
191
192
193 (defclass container-child ()
194   ((parent :initarg :parent :type container)
195    (child :initarg :child :type widget)))
196
197
198 ;;;;
199
200 (defbinding %container-class-list-child-properties () pointer
201   (class pointer)
202   (n-properties unsigned-int :out))
203
204 (defun query-container-class-child-properties (type-number)
205   (let ((class (type-class-ref type-number)))
206     (multiple-value-bind (array length)
207         (%container-class-list-child-properties class)
208       (unwind-protect
209           (map-c-vector 'list #'identity array 'param length)
210         (deallocate-memory array)))))
211
212 (defun default-container-child-name (container-class)
213   (intern (format nil "~A-CHILD" container-class)))
214
215 (defun expand-container-type (type forward-p options)
216   (let* ((class (type-from-number type))
217          (super (supertype type))
218          (child-class (default-container-child-name class)))
219     (if forward-p 
220         (expand-gobject-type type t options)
221       `(progn
222          ,(expand-gobject-type type nil options)
223          (defclass ,child-class (,(default-container-child-name super))
224            ,(slot-definitions child-class 
225              (query-container-class-child-properties type) nil)
226            (:metaclass child-class)
227            (:container ,class))))))
228
229
230 (register-derivable-type 'container "GtkContainer" 'expand-container-type 'gobject-dependencies)