chiark / gitweb /
Updated for CMUCL 19a and glib-2.4. Lots of improvements
[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.16 2004/10/31 12:05:52 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 "libgtk-x11-2.0.so"
38    :ignore ("gtk_window_get_type_hint"))
39
40   (defclass %object (gobject)
41     ()
42     (:metaclass gobject-class)
43     (:alien-name "GtkObject")))
44
45
46 (defmethod shared-initialize ((object %object) names &rest initargs &key signal)
47   (declare (ignore names signal))
48   (call-next-method)
49   (object-ref object) ; inc ref count before sinking
50   (%object-sink object)
51   (dolist (signal-definition (get-all initargs :signal))
52     (apply #'signal-connect object signal-definition)))
53
54 (defmethod initialize-proxy ((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, timeouts and idle functions
63
64 (declaim (inline events-pending-p main-iteration))
65
66 (defbinding (events-pending-p "gtk_events_pending") () 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   (when (events-pending-p)
85     (main-iteration-do nil)
86     (main-iterate-all)))
87
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
95
96 (defun clg-init (&optional display)
97   "Initializes the system and starts the event handling"
98   (unless (gdk:display-get-default)
99     (gdk:gdk-init)
100     (gtk-init)
101     (prog1
102         (gdk:display-open display)
103       (system:add-fd-handler 
104        (gdk:display-connection-number) :input #'main-iterate-all)
105       (setq lisp::*periodic-polling-function* #'main-iterate-all)
106       (setq lisp::*max-event-to-sec* 0)
107       (setq lisp::*max-event-to-usec* 1000))))
108
109
110
111 ;;;; Metaclass for child classes
112  
113 (defvar *container-to-child-class-mappings* (make-hash-table))
114
115 (eval-when (:compile-toplevel :load-toplevel :execute)
116   (defclass child-class (virtual-slot-class)
117     ())
118
119   (defclass direct-child-slot-definition (direct-virtual-slot-definition)
120     ((pname :reader slot-definition-pname :initarg :pname)))
121
122   (defclass effective-child-slot-definition (effective-virtual-slot-definition)
123     ((pname :reader slot-definition-pname :initarg :pname)))
124
125
126 (defmethod shared-initialize ((class child-class) names &key container)
127   (call-next-method)
128   (setf
129    (gethash (find-class (first container)) *container-to-child-class-mappings*)
130     class))
131
132 ;; (defmethod initialize-instance  ((slotd direct-child-slot-definition)
133 ;;                               &rest initargs &key pname)
134 ;;   (declare (ignore initargs))
135 ;;   (call-next-method)
136 ;;   (if pname
137 ;;       (setf (slot-value slotd 'pname) pname)
138 ;;     ; ???
139 ;;     (error "Need pname for slot with allocation :property")))
140
141 (defmethod direct-slot-definition-class ((class child-class) &rest initargs)
142   (case (getf initargs :allocation)
143     (:property (find-class 'direct-child-slot-definition))
144     (t (call-next-method))))
145
146 (defmethod effective-slot-definition-class ((class child-class) &rest initargs)
147   (case (getf initargs :allocation)
148     (:property (find-class 'effective-child-slot-definition))
149     (t (call-next-method))))
150
151 (defmethod compute-effective-slot-definition-initargs ((class child-class) direct-slotds)
152   (if (eq (most-specific-slot-value direct-slotds 'allocation) :property)
153       (nconc 
154        (list :pname (most-specific-slot-value direct-slotds 'pname))
155        (call-next-method))
156     (call-next-method)))
157
158 (progn
159   (declaim (optimize (ext:inhibit-warnings 3)))
160   (defun %container-child-get-property (parent child pname gvalue))
161   (defun %container-child-set-property (parent child pname gvalue)))
162
163
164 (defmethod initialize-internal-slot-functions ((slotd effective-child-slot-definition))
165   (let* ((type (slot-definition-type slotd))
166          (pname (slot-definition-pname slotd))
167          (type-number (find-type-number type)))
168     (unless (slot-boundp slotd 'reader-function)
169       (setf 
170        (slot-value slotd 'reader-function)
171        #'(lambda (object)
172            (with-slots (parent child) object       
173              (with-gc-disabled
174                  (let ((gvalue (gvalue-new type-number)))
175                    (%container-child-get-property parent child pname gvalue)
176                    (unwind-protect
177                         (funcall
178                          (intern-reader-function type)
179                          gvalue +gvalue-value-offset+)
180                      (gvalue-free gvalue t))))))))
181     
182     (unless (slot-boundp slotd 'writer-function)
183       (setf 
184        (slot-value slotd 'writer-function)
185        #'(lambda (value object)
186            (with-slots (parent child) object       
187              (with-gc-disabled
188                  (let ((gvalue (gvalue-new type-number)))
189                    (funcall
190                     (intern-writer-function type)
191                     value gvalue +gvalue-value-offset+)
192                    (%container-child-set-property parent child pname gvalue)
193                    (funcall
194                     (intern-destroy-function type)
195                     gvalue +gvalue-value-offset+)
196                    (gvalue-free gvalue nil)
197                    value))))))
198     
199     (unless (slot-boundp slotd 'boundp-function)
200       (setf 
201        (slot-value slotd 'boundp-function)
202        #'(lambda (object)
203            (declare (ignore object))
204            t))))
205   (call-next-method)))
206
207
208 (defmethod pcl::add-reader-method ((class child-class) generic-function slot-name)
209   (add-method
210    generic-function
211    (make-instance 'standard-method
212                   :specializers (list (find-class 'widget))
213                   :lambda-list '(widget)
214                   :function #'(lambda (args next-methods)
215                                 (declare (ignore next-methods))
216                                 (child-slot-value (first args) slot-name)))))
217
218 (defmethod pcl::add-writer-method
219     ((class child-class) generic-function slot-name)
220   (add-method
221    generic-function
222    (make-instance 'standard-method
223                   :specializers (list (find-class t) (find-class 'widget))
224                   :lambda-list '(value widget)
225                   :function #'(lambda (args next-methods)
226                                 (declare (ignore next-methods))
227                                 (destructuring-bind (value widget) args
228                                   (setf
229                                    (child-slot-value widget slot-name)
230                                    value))))))
231
232
233 (defmethod validate-superclass ((class child-class) (super pcl::standard-class))
234   ;(subtypep (class-name super) 'container-child)
235   t)
236
237
238 (defclass container-child ()
239   ((parent :initarg :parent :type container)
240    (child :initarg :child :type widget)))
241
242
243 ;;;;
244
245 (defbinding %container-class-list-child-properties () pointer
246   (class pointer)
247   (n-properties unsigned-int :out))
248
249 (defun query-container-class-child-properties (type-number)
250   (let ((class (type-class-ref type-number)))
251     (multiple-value-bind (array length)
252         (%container-class-list-child-properties class)
253       (unwind-protect
254           (map-c-array 'list #'identity array 'param length)
255         (deallocate-memory array)))))
256
257 (defun default-container-child-name (container-class)
258   (intern (format nil "~A-CHILD" container-class)))
259
260 (defun expand-container-type (type &optional options)
261   (let* ((class (type-from-number type))
262          (super (supertype type))
263          (child-class (default-container-child-name class)))
264     `(progn
265        ,(expand-gobject-type type options)
266        (defclass ,child-class (,(default-container-child-name super))
267          ,(slot-definitions child-class 
268            (query-container-class-child-properties type) nil)
269          (:metaclass child-class)
270          (:container ,class)))))
271
272
273 (register-derivable-type 'container "GtkContainer" 'expand-container-type)