Callbacks from C done properly
[clg] / gtk / gtkcontainer.lisp
1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 2000 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: gtkcontainer.lisp,v 1.10 2004-11-01 00:08:50 espen Exp $
19
20 (in-package "GTK")
21
22 (defmethod shared-initialize ((container container) names &rest initargs
23 &key child children child-args)
24 (declare (ignore child))
25 (call-next-method)
26 (dolist (child (append children (get-all initargs :child)))
27 (apply #'container-add container (append (mklist child) child-args))))
28
29
30 (defbinding %container-add () nil
31 (container container)
32 (widget widget))
33
34 (defmethod container-add ((container container) (widget widget) &rest args)
35 (%container-add container widget)
36 (when args
37 (setf
38 (slot-value widget 'child-slots)
39 (apply
40 #'make-instance
41 (gethash (class-of container) *container-to-child-class-mappings*)
42 :parent container :child widget args))))
43
44
45 (defbinding %container-remove () nil
46 (container container)
47 (widget widget))
48
49 (defmethod container-remove ((container container) (widget widget))
50 (%container-remove container widget)
51 (slot-makunbound widget 'child-slots))
52
53
54 (defbinding %container-child-get-property () nil
55 (container container)
56 (child widget)
57 (property-name string)
58 (value gvalue))
59
60 (defbinding %container-child-set-property () nil
61 (container container)
62 (child widget)
63 (property-name string)
64 (value gvalue))
65
66
67 (defbinding container-check-resize () nil
68 (container container))
69
70 (def-callback %foreach-callback (c-call:void (widget system-area-pointer)
71 (callback-id c-call:unsigned-int))
72 (invoke-callback callback-id nil (ensure-proxy-instance 'widget widget nil)))
73
74 (defbinding %container-foreach (container callback-id) nil
75 (container container)
76 ((callback %foreach-callback) pointer)
77 (callback-id unsigned-int))
78
79 (defun container-foreach (container function)
80 (let ((callback-id (register-callback-function function)))
81 (unwind-protect
82 (%container-foreach container callback-id)
83 (destroy-user-data callback-id))))
84
85 (defun map-container (seqtype func container)
86 (case seqtype
87 ((nil)
88 (container-foreach container func)
89 nil)
90 (list
91 (let ((list nil))
92 (container-foreach
93 container
94 #'(lambda (child)
95 (push (funcall func child) list)))
96 (nreverse list)))
97 (t
98 (let ((seq (make-sequence seqtype (container-length container)))
99 (index 0))
100 (container-foreach
101 container
102 #'(lambda (child)
103 (setf (elt seq index) (funcall func child))
104 (incf index)))
105 seq))))
106
107 (defmacro do-container ((var container &optional (result nil)) &body body)
108 (let ((continue (make-symbol "CONTINUE")))
109 `(let ((,continue t))
110 (container-foreach
111 ,container
112 #'(lambda (,var)
113 (when ,continue
114 (setq ,continue nil)
115 (block nil
116 ,@body
117 (setq ,continue t)))))
118 ,result)))
119
120 ;; (defbinding %container-get-children () (glist widget)
121 ;; (container container))
122
123 (defmethod container-children ((container container))
124 ;; (%container-get-children container)
125 (map-container 'list #'identity container))
126
127 (defmethod (setf container-children) (children (container container))
128 (dolist (child (container-children container))
129 (container-remove container child))
130 (dolist (child children)
131 (container-add container child))
132 children)
133
134 (defun container-length (container)
135 (let ((n 0))
136 (container-foreach container
137 #'(lambda (child)
138 (declare (ignore child))
139 (incf n)))
140 n))
141
142 (defbinding container-resize-children () nil
143 (container container))
144
145 (defbinding container-propagate-expose () nil
146 (container container)
147 (child widget)
148 (event gdk:expose-event))
149
150
151 (defbinding %container-get-focus-chain () boolean
152 (container container)
153 (focusable-widgets (glist widget) :out))
154
155 (defun container-focus-chain (container)
156 (multiple-value-bind (chain-set-p focusable-widgets)
157 (%container-get-focus-chain container)
158 (and chain-set-p focusable-widgets)))
159
160 (defbinding %container-set-focus-chain () nil
161 (container container)
162 (focusable-widgets (glist widget)))
163
164 (defbinding %container-unset-focus-chain () nil
165 (container container))
166
167 (defun (setf container-focus-chain) (focusable-widgets container)
168 (if (null focusable-widgets)
169 (%container-unset-focus-chain container)
170 (%container-set-focus-chain container focusable-widgets)))