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