chiark / gitweb /
Changes in widget initialization
[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.12 2001-11-12 22:33:08 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-1.3.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
47                               &allow-other-keys)
48   (declare (ignore names))
49   (call-next-method)
50   (funcall (proxy-class-copy (class-of object)) nil (proxy-location object)) ; inc ref count before sinking
51   (%object-sink object)
52   (dolist (signal-definition (get-all initargs :signal))
53     (apply #'signal-connect object signal-definition)))
54
55 (defmethod initialize-proxy ((object %object) &rest initargs)
56   (declare (ignore initargs))
57   (call-next-method)
58   (%object-sink object))
59
60 (defbinding %object-sink () nil
61   (object %object))
62
63
64 ;;;; Main loop, timeouts and idle functions
65
66 (declaim (inline events-pending-p main-iteration))
67
68 (defbinding (events-pending-p "gtk_events_pending") () boolean)
69
70 (defbinding get-current-event () gdk:event)
71
72 (defbinding main-do-event () nil
73   (event gdk:event))
74
75 (defbinding main () nil)
76
77 (defbinding main-level () int)
78
79 (defbinding main-quit () nil)
80
81 (defbinding main-iteration-do (&optional (blocking t)) boolean
82   (blocking boolean))
83
84 (defun main-iterate-all (&rest args)
85   (declare (ignore args))
86   (when (events-pending-p)
87     (main-iteration-do nil)
88     (main-iterate-all)))
89
90 (system:add-fd-handler (gdk:connection-number) :input #'main-iterate-all)
91 (setq lisp::*periodic-polling-function* #'main-iterate-all)
92 (setq lisp::*max-event-to-sec* 0)
93 (setq lisp::*max-event-to-usec* 1000)
94
95
96
97 ;;;; Metaclass for child classes
98  
99 (defvar *container-to-child-class-mappings* (make-hash-table))
100
101 (eval-when (:compile-toplevel :load-toplevel :execute)
102   (defclass child-class (virtual-slot-class))
103
104   (defclass direct-child-slot-definition (direct-virtual-slot-definition)
105     ((pname :reader slot-definition-pname)))
106
107   (defclass effective-child-slot-definition
108     (effective-virtual-slot-definition)))
109
110
111 (defmethod shared-initialize ((class child-class) names &rest initargs
112                               &key container)
113   (declare (ignore initargs))
114   (call-next-method)
115   (setf
116    (gethash (find-class (first container)) *container-to-child-class-mappings*)
117     class))
118
119 (defmethod initialize-instance  ((slotd direct-child-slot-definition)
120                                  &rest initargs &key pname)
121   (declare (ignore initargs))
122   (call-next-method)
123   (if pname
124       (setf (slot-value slotd 'pname) pname)
125     ; ???
126     (error "Need pname for slot with allocation :property")))
127
128 (defmethod direct-slot-definition-class ((class child-class) initargs)
129   (case (getf initargs :allocation)
130     (:property (find-class 'direct-child-slot-definition))
131     (t (call-next-method))))
132
133 (defmethod effective-slot-definition-class ((class child-class) initargs)
134   (case (getf initargs :allocation)
135     (:property (find-class 'effective-child-slot-definition))
136     (t (call-next-method))))
137
138 (defmethod compute-virtual-slot-accessors
139     ((class child-class) (slotd effective-child-slot-definition) direct-slotds)
140   (with-slots (type) slotd
141     (let ((pname (slot-definition-pname (first direct-slotds)))
142           (type-number (find-type-number type)))
143       (list
144        #'(lambda (object)
145            (with-slots (parent child) object       
146              (with-gc-disabled
147                (let ((gvalue (gvalue-new type-number)))
148                  (%container-child-get-property parent child pname gvalue)
149                  (unwind-protect
150                      (funcall
151                       (intern-reader-function type)
152                       gvalue +gvalue-value-offset+)
153                    (gvalue-free gvalue t))))))
154        #'(lambda (value object)
155            (with-slots (parent child) object       
156              (with-gc-disabled
157               (let ((gvalue (gvalue-new type-number)))
158                 (funcall
159                  (intern-writer-function type)
160                  value gvalue +gvalue-value-offset+)
161                 (%container-child-set-property parent child pname gvalue)
162                 (funcall
163                  (intern-destroy-function type)
164                  gvalue +gvalue-value-offset+)
165                 (gvalue-free gvalue nil)
166                 value))))))))
167
168
169 (defmethod pcl::add-reader-method ((class child-class) generic-function slot-name)
170   (add-method
171    generic-function
172    (make-instance 'standard-method
173                   :specializers (list (find-class 'widget))
174                   :lambda-list '(widget)
175                   :function #'(lambda (args next-methods)
176                                 (declare (ignore next-methods))
177                                 (child-slot-value (first args) slot-name)))))
178
179 (defmethod pcl::add-writer-method
180     ((class child-class) generic-function slot-name)
181   (add-method
182    generic-function
183    (make-instance 'standard-method
184                   :specializers (list (find-class t) (find-class 'widget))
185                   :lambda-list '(value widget)
186                   :function #'(lambda (args next-methods)
187                                 (declare (ignore next-methods))
188                                 (destructuring-bind (value widget) args
189                                   (setf
190                                    (child-slot-value widget slot-name)
191                                    value))))))
192
193
194 (defmethod validate-superclass ((class child-class) (super pcl::standard-class))
195   ;(subtypep (class-name super) 'container-child)
196   t)
197
198
199 (defclass container-child ()
200   ((parent :initarg :parent :type container)
201    (child :initarg :child :type widget)))
202
203
204 ;;;;
205
206 (defbinding %container-class-list-child-properties () pointer
207   (class pointer)
208   (n-properties unsigned-int :out))
209
210 (defun query-container-class-child-properties (type-number)
211   (let ((class (type-class-ref type-number)))
212     (multiple-value-bind (array length)
213         (%container-class-list-child-properties class)
214       (unwind-protect
215           (map-c-array 'list #'identity array 'param length)
216         (deallocate-memory array)))))
217
218 (defun default-container-child-name (container-class)
219   (intern (format nil "~A-CHILD" container-class)))
220
221 (defun expand-container-type (type-number &optional slots)
222   (let* ((class (type-from-number type-number))
223          (super (supertype type-number))
224          (child-class (default-container-child-name class))
225          (expanded-child-slots
226           (mapcar
227            #'(lambda (param)
228                (with-slots (name flags value-type documentation) param
229                  (let* ((slot-name (default-slot-name name))
230                         (slot-type (type-from-number value-type #|t|#))
231                         (accessor
232                          (default-slot-accessor class slot-name slot-type)))
233                    `(,slot-name
234                      :allocation :property
235                      :pname ,name
236                      ,@(cond
237                         ((and
238                           (member :writable flags)
239                           (member :readable flags))
240                          (list :accessor accessor))
241                         ((member :writable flags)
242                          (list :writer `(setf ,accessor)))
243                         ((member :readable flags)
244                          (list :reader accessor)))
245                      ,@(when (or
246                               (member :construct flags)
247                               (member :writable flags))
248                          (list :initarg (intern (string slot-name) "KEYWORD")))
249                      :type ,slot-type
250                      ,@(when documentation
251                          (list :documentation documentation))))))
252            (query-container-class-child-properties type-number))))
253     `(progn
254        ,(expand-gobject-type type-number slots)
255        (defclass ,child-class
256          (,(default-container-child-name super))
257          ,expanded-child-slots
258          (:metaclass child-class)
259          (:container ,class)))))
260
261 (register-derivable-type 'container "GtkContainer" 'expand-container-type)