chiark / gitweb /
Callbacks from C done properly
[clg] / gtk / gtkobject.lisp
CommitLineData
560af5c5 1;; Common Lisp bindings for GTK+ v2.0
56460319 2;; Copyright (C) 1999-2001 Espen S. Johnsen <espen@users.sourceforge.org>
560af5c5 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
d9a7b2f8 18;; $Id: gtkobject.lisp,v 1.17 2004-10-31 14:44:05 espen Exp $
560af5c5 19
20
21(in-package "GTK")
22
8bf63d9f 23
560af5c5 24;;;; Misc utils
25
f86d391e 26; (defun name-to-string (name)
27; (substitute #\_ #\- (string-downcase (string name))))
560af5c5 28
f86d391e 29; (defun string-to-name (name &optional (package "KEYWORD"))
30; (intern (substitute #\- #\_ (string-upcase name)) package))
560af5c5 31
32
560af5c5 33
34;;;; Superclass for the gtk class hierarchy
35
36(eval-when (:compile-toplevel :load-toplevel :execute)
b030f2d9 37 (init-types-in-library "libgtk-x11-2.0.so"
18eed2a0 38 :ignore ("gtk_window_get_type_hint"))
f86d391e 39
40 (defclass %object (gobject)
560af5c5 41 ()
560af5c5 42 (:metaclass gobject-class)
43 (:alien-name "GtkObject")))
44
45
1047e159 46(defmethod shared-initialize ((object %object) names &rest initargs &key signal)
47 (declare (ignore names signal))
560af5c5 48 (call-next-method)
a236ae95 49 (object-ref object) ; inc ref count before sinking
f86d391e 50 (%object-sink object)
18eed2a0 51 (dolist (signal-definition (get-all initargs :signal))
52 (apply #'signal-connect object signal-definition)))
560af5c5 53
18eed2a0 54(defmethod initialize-proxy ((object %object) &rest initargs)
560af5c5 55 (declare (ignore initargs))
56 (call-next-method)
18eed2a0 57 (%object-sink object))
560af5c5 58
f86d391e 59(defbinding %object-sink () nil
60 (object %object))
560af5c5 61
aace61f5 62;;;; Main loop, timeouts and idle functions
560af5c5 63
64(declaim (inline events-pending-p main-iteration))
65
f86d391e 66(defbinding (events-pending-p "gtk_events_pending") () boolean)
560af5c5 67
f86d391e 68(defbinding get-current-event () gdk:event)
aace61f5 69
f86d391e 70(defbinding main-do-event () nil
560af5c5 71 (event gdk:event))
72
f86d391e 73(defbinding main () nil)
560af5c5 74
f86d391e 75(defbinding main-level () int)
560af5c5 76
f86d391e 77(defbinding main-quit () nil)
560af5c5 78
f86d391e 79(defbinding main-iteration-do (&optional (blocking t)) boolean
560af5c5 80 (blocking boolean))
81
82(defun main-iterate-all (&rest args)
83 (declare (ignore args))
84 (when (events-pending-p)
f86d391e 85 (main-iteration-do nil)
560af5c5 86 (main-iterate-all)))
87
1047e159 88;;;; Initalization
89
90(defbinding (gtk-init "gtk_parse_args") () nil
91 "Initializes the library without opening the display."
92 (nil null)
93 (nil null))
94
d9a7b2f8 95(import 'gtk-system:clg-init)
1047e159 96
97(defun clg-init (&optional display)
98 "Initializes the system and starts the event handling"
99 (unless (gdk:display-get-default)
100 (gdk:gdk-init)
101 (gtk-init)
102 (prog1
103 (gdk:display-open display)
104 (system:add-fd-handler
105 (gdk:display-connection-number) :input #'main-iterate-all)
106 (setq lisp::*periodic-polling-function* #'main-iterate-all)
107 (setq lisp::*max-event-to-sec* 0)
108 (setq lisp::*max-event-to-usec* 1000))))
560af5c5 109
110
111
560af5c5 112;;;; Metaclass for child classes
f86d391e 113
114(defvar *container-to-child-class-mappings* (make-hash-table))
560af5c5 115
116(eval-when (:compile-toplevel :load-toplevel :execute)
1047e159 117 (defclass child-class (virtual-slot-class)
118 ())
560af5c5 119
f86d391e 120 (defclass direct-child-slot-definition (direct-virtual-slot-definition)
1047e159 121 ((pname :reader slot-definition-pname :initarg :pname)))
560af5c5 122
1047e159 123 (defclass effective-child-slot-definition (effective-virtual-slot-definition)
124 ((pname :reader slot-definition-pname :initarg :pname)))
560af5c5 125
126
1047e159 127(defmethod shared-initialize ((class child-class) names &key container)
560af5c5 128 (call-next-method)
f86d391e 129 (setf
130 (gethash (find-class (first container)) *container-to-child-class-mappings*)
131 class))
560af5c5 132
1047e159 133;; (defmethod initialize-instance ((slotd direct-child-slot-definition)
134;; &rest initargs &key pname)
135;; (declare (ignore initargs))
136;; (call-next-method)
137;; (if pname
138;; (setf (slot-value slotd 'pname) pname)
139;; ; ???
140;; (error "Need pname for slot with allocation :property")))
560af5c5 141
1047e159 142(defmethod direct-slot-definition-class ((class child-class) &rest initargs)
560af5c5 143 (case (getf initargs :allocation)
18eed2a0 144 (:property (find-class 'direct-child-slot-definition))
560af5c5 145 (t (call-next-method))))
146
1047e159 147(defmethod effective-slot-definition-class ((class child-class) &rest initargs)
560af5c5 148 (case (getf initargs :allocation)
18eed2a0 149 (:property (find-class 'effective-child-slot-definition))
560af5c5 150 (t (call-next-method))))
560af5c5 151
1047e159 152(defmethod compute-effective-slot-definition-initargs ((class child-class) direct-slotds)
153 (if (eq (most-specific-slot-value direct-slotds 'allocation) :property)
154 (nconc
155 (list :pname (most-specific-slot-value direct-slotds 'pname))
156 (call-next-method))
157 (call-next-method)))
158
b030f2d9 159(progn
160 (declaim (optimize (ext:inhibit-warnings 3)))
161 (defun %container-child-get-property (parent child pname gvalue))
162 (defun %container-child-set-property (parent child pname gvalue)))
163
164
1047e159 165(defmethod initialize-internal-slot-functions ((slotd effective-child-slot-definition))
166 (let* ((type (slot-definition-type slotd))
167 (pname (slot-definition-pname slotd))
168 (type-number (find-type-number type)))
169 (unless (slot-boundp slotd 'reader-function)
170 (setf
171 (slot-value slotd 'reader-function)
560af5c5 172 #'(lambda (object)
173 (with-slots (parent child) object
174 (with-gc-disabled
1047e159 175 (let ((gvalue (gvalue-new type-number)))
176 (%container-child-get-property parent child pname gvalue)
177 (unwind-protect
178 (funcall
179 (intern-reader-function type)
180 gvalue +gvalue-value-offset+)
181 (gvalue-free gvalue t))))))))
182
183 (unless (slot-boundp slotd 'writer-function)
184 (setf
185 (slot-value slotd 'writer-function)
560af5c5 186 #'(lambda (value object)
187 (with-slots (parent child) object
188 (with-gc-disabled
1047e159 189 (let ((gvalue (gvalue-new type-number)))
190 (funcall
191 (intern-writer-function type)
192 value gvalue +gvalue-value-offset+)
193 (%container-child-set-property parent child pname gvalue)
194 (funcall
195 (intern-destroy-function type)
196 gvalue +gvalue-value-offset+)
197 (gvalue-free gvalue nil)
198 value))))))
199
200 (unless (slot-boundp slotd 'boundp-function)
201 (setf
202 (slot-value slotd 'boundp-function)
203 #'(lambda (object)
204 (declare (ignore object))
205 t))))
206 (call-next-method)))
560af5c5 207
208
209(defmethod pcl::add-reader-method ((class child-class) generic-function slot-name)
210 (add-method
211 generic-function
212 (make-instance 'standard-method
213 :specializers (list (find-class 'widget))
214 :lambda-list '(widget)
215 :function #'(lambda (args next-methods)
216 (declare (ignore next-methods))
217 (child-slot-value (first args) slot-name)))))
218
219(defmethod pcl::add-writer-method
220 ((class child-class) generic-function slot-name)
221 (add-method
222 generic-function
223 (make-instance 'standard-method
224 :specializers (list (find-class t) (find-class 'widget))
225 :lambda-list '(value widget)
226 :function #'(lambda (args next-methods)
227 (declare (ignore next-methods))
228 (destructuring-bind (value widget) args
229 (setf
230 (child-slot-value widget slot-name)
231 value))))))
232
233
234(defmethod validate-superclass ((class child-class) (super pcl::standard-class))
18eed2a0 235 ;(subtypep (class-name super) 'container-child)
236 t)
560af5c5 237
238
f86d391e 239(defclass container-child ()
240 ((parent :initarg :parent :type container)
241 (child :initarg :child :type widget)))
242
243
244;;;;
245
18eed2a0 246(defbinding %container-class-list-child-properties () pointer
247 (class pointer)
248 (n-properties unsigned-int :out))
f86d391e 249
18eed2a0 250(defun query-container-class-child-properties (type-number)
251 (let ((class (type-class-ref type-number)))
252 (multiple-value-bind (array length)
253 (%container-class-list-child-properties class)
254 (unwind-protect
255 (map-c-array 'list #'identity array 'param length)
256 (deallocate-memory array)))))
f86d391e 257
258(defun default-container-child-name (container-class)
259 (intern (format nil "~A-CHILD" container-class)))
260
1047e159 261(defun expand-container-type (type &optional options)
262 (let* ((class (type-from-number type))
263 (super (supertype type))
264 (child-class (default-container-child-name class)))
18eed2a0 265 `(progn
1047e159 266 ,(expand-gobject-type type options)
267 (defclass ,child-class (,(default-container-child-name super))
268 ,(slot-definitions child-class
269 (query-container-class-child-properties type) nil)
18eed2a0 270 (:metaclass child-class)
271 (:container ,class)))))
272
1047e159 273
18eed2a0 274(register-derivable-type 'container "GtkContainer" 'expand-container-type)