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