Changed to MIT license
[clg] / gtk / gtkwidget.lisp
1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
3 ;;
4 ;; Permission is hereby granted, free of charge, to any person obtaining
5 ;; a copy of this software and associated documentation files (the
6 ;; "Software"), to deal in the Software without restriction, including
7 ;; without limitation the rights to use, copy, modify, merge, publish,
8 ;; distribute, sublicense, and/or sell copies of the Software, and to
9 ;; permit persons to whom the Software is furnished to do so, subject to
10 ;; the following conditions:
11 ;;
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
14 ;;
15 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 ;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 ;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23 ;; $Id: gtkwidget.lisp,v 1.18 2005-04-23 16:48:52 espen Exp $
24
25 (in-package "GTK")
26
27
28 (defmethod shared-initialize ((widget widget) names &key (visible nil visible-p))
29 (when (and visible-p (not visible)) ; widget explicit set as not visible
30 (setf (user-data widget 'hidden-p) t)
31 (signal-connect widget 'show
32 #'(lambda ()
33 (unset-user-data widget 'hidden-p))
34 :remove t))
35 (call-next-method))
36
37 (defmethod shared-initialize :after ((widget widget) names &key parent visible)
38 (declare (ignore names))
39 (when visible
40 (widget-show widget))
41 (when parent
42 (when (slot-boundp widget 'parent)
43 (container-remove (widget-parent widget) widget))
44 (destructuring-bind (parent &rest args) (mklist parent)
45 (apply #'container-add parent widget args))))
46
47 (defmethod slot-unbound ((class gobject-class) (object widget)
48 (slot (eql 'child-properties)))
49 (cond
50 ((slot-boundp object 'parent)
51 (with-slots (parent child-properties) object
52 (setf child-properties
53 (make-instance
54 (gethash (class-of parent) *container-to-child-class-mappings*)
55 :parent parent :child object))))
56 ((call-next-method))))
57
58 (defmethod slot-boundp-using-class ((class gobject-class) (object widget) slot)
59 (or
60 (and
61 (eq (slot-definition-name slot) 'child-properties)
62 (slot-boundp object 'parent))
63 (call-next-method)))
64
65 (defmethod compute-signal-function ((widget widget) signal function object)
66 (if (eq object :parent)
67 #'(lambda (&rest args)
68 (if (slot-boundp widget 'parent)
69 (apply function (widget-parent widget) (rest args))
70 ;; Delay until parent is set
71 (signal-connect widget 'parent-set
72 #'(lambda (old-parent)
73 (declare (ignore old-parent))
74 (let ((*signal-stop-emission*
75 #'(lambda ()
76 (warn "Ignoring emission stop in delayed signal handler"))))
77 (apply function (widget-parent widget) (rest args))))
78 :remove t)
79 ; (warn "Widget has no parent -- ignoring signal")
80 ))
81 (call-next-method)))
82
83 (defun child-property-value (widget slot)
84 (slot-value (widget-child-properties widget) slot))
85
86 (defun (setf child-property-value) (value widget slot)
87 (setf (slot-value (widget-child-properties widget) slot) value))
88
89 (defmacro with-child-properties (slots widget &body body)
90 `(with-slots ,slots (widget-child-properties ,widget)
91 ,@body))
92
93
94 ;;; Bindings
95
96 (defbinding widget-destroy () nil
97 (widget widget))
98
99 (defbinding widget-unparent () nil
100 (widget widget))
101
102 (defbinding widget-show () nil
103 (widget widget))
104
105 (defbinding widget-show-now () nil
106 (widget widget))
107
108 (defbinding widget-hide () nil
109 (widget widget))
110
111 (defun widget-hidden-p (widget)
112 "Return T if WIDGET has been explicit hidden during construction."
113 (user-data widget 'hidden-p))
114
115 (defbinding widget-show-all () nil
116 (widget widget))
117
118 (defbinding widget-hide-all () nil
119 (widget widget))
120
121 (defbinding widget-map () nil
122 (widget widget))
123
124 (defbinding widget-unmap () nil
125 (widget widget))
126
127 (defbinding widget-realize () nil
128 (widget widget))
129
130 (defbinding widget-unrealize () nil
131 (widget widget))
132
133 (defbinding widget-queue-draw () nil
134 (widget widget))
135
136 (defbinding widget-queue-resize () nil
137 (widget widget))
138
139 (defbinding widget-queue-resize-no-redraw () nil
140 (widget widget))
141
142 (defbinding widget-size-request
143 (widget &optional (requisition (make-instance 'requisition))) nil
144 (widget widget)
145 (requisition requisition :return))
146
147 (defbinding widget-get-child-requisition
148 (widget &optional (requisition (make-instance 'requisition))) nil
149 (widget widget)
150 (requisition requisition :return))
151
152 (defbinding widget-size-allocate () nil
153 (widget widget)
154 (allocation allocation))
155
156 (defbinding widget-add-accelerator
157 (widget signal accel-group key modifiers flags) nil
158 (widget widget)
159 ((name-to-string signal) string)
160 (accel-group accel-group)
161 ((gdk:keyval-from-name key) unsigned-int)
162 (modifiers gdk:modifier-type)
163 (flags accel-flags))
164
165 (defbinding widget-remove-accelerator
166 (widget accel-group key modifiers) nil
167 (widget widget)
168 (accel-group accel-group)
169 ((gdk:keyval-from-name key) unsigned-int)
170 (modifiers gdk:modifier-type))
171
172 (defbinding widget-set-accel-path () nil
173 (widget widget)
174 (accel-path string)
175 (accel-group accel-group))
176
177 (defbinding widget-list-accel-closures () (glist pointer)
178 (widget widget))
179
180 (defbinding widget-can-activate-accel-p () boolean
181 (widget widget)
182 (signal-id unsigned-int))
183
184 (defbinding widget-event () boolean
185 (widget widget)
186 (event gdk:event))
187
188 (defbinding widget-activate () boolean
189 (widget widget))
190
191 (defbinding widget-reparent () nil
192 (widget widget)
193 (new-parent widget))
194
195 (defbinding %widget-intersect () boolean
196 (widget widget)
197 (area gdk:rectangle)
198 (intersection (or null gdk:rectangle)))
199
200 (defun widget-intersection (widget area)
201 (let ((intersection (make-instance 'gdk:rectangle)))
202 (when (%widget-intersect widget area intersection)
203 intersection)))
204
205 (defun widget-intersect-p (widget area)
206 (%widget-intersect widget area nil))
207
208 (defbinding widget-grab-focus () nil
209 (widget widget))
210
211 (defbinding widget-grab-default () nil
212 (widget widget))
213
214 (defbinding widget-add-events () nil
215 (widget widget)
216 (events gdk:event-mask))
217
218 (defbinding widget-get-toplevel () widget
219 (widget widget))
220
221 (defbinding widget-get-ancestor (widget type) widget
222 (widget widget)
223 ((find-type-number type) type-number))
224
225 (defbinding widget-get-pointer () nil
226 (widget widget)
227 (x int :out)
228 (y int :out))
229
230 (defbinding widget-is-ancestor-p () boolean
231 (widget widget)
232 (ancestor widget))
233
234 (defbinding widget-translate-coordinates () boolean
235 (src-widget widget)
236 (dest-widget widget)
237 (src-x int) (src-y int)
238 (set-x int :out) (dest-y int :out))
239
240 (defun widget-hide-on-delete (widget)
241 "Utility function; intended to be connected to the DELETE-EVENT
242 signal on a WINDOW. The function calls WIDGET-HIDE on its
243 argument, then returns T. If connected to DELETE-EVENT, the
244 result is that clicking the close button for a window (on the window
245 frame, top right corner usually) will hide but not destroy the
246 window. By default, GTK+ destroys windows when DELETE-EVENT is
247 received."
248 (widget-hide widget)
249 t)
250
251 (defbinding widget-ensure-style () nil
252 (widget widget))
253
254 (defbinding widget-reset-rc-styles () nil
255 (widget widget))
256
257 (defbinding widget-push-colormap () nil
258 (colormap gdk:colormap))
259
260 (defbinding widget-pop-colormap () nil)
261
262 (defbinding %widget-set-default-colormap () nil
263 (colormap gdk:colormap))
264
265 (defun (setf widget-default-colormap) (colormap)
266 (%widget-set-default-colormap colormap)
267 colormap)
268
269 (defbinding (widget-default-style "gtk_widget_get_default_style") () style)
270
271 (defbinding (widget-default-colromap "gtk_widget_get_default_colormap")
272 () gdk:colormap)
273
274 (defbinding (widget-default-visual "gtk_widget_get_default_visual")
275 () gdk:visual)
276
277 (defbinding (widget-default-direction "gtk_widget_get_default_direction")
278 () text-direction)
279
280 (defbinding %widget-set-default-direction () nil
281 (direction text-direction))
282
283 (defun (setf widget-default-direction) (direction)
284 (%widget-set-default-direction direction)
285 direction)
286
287 (defbinding widget-shape-combine-mask () nil
288 (widget widget)
289 (shape-mask (or null gdk:bitmap))
290 (x-offset int)
291 (y-offset int))
292
293 (defbinding widget-path () nil
294 (widget widget)
295 (path-length int :out)
296 (path string :out)
297 (reverse-path string :out))
298
299 (defbinding widget-class-path () nil
300 (widget widget)
301 (path-length int :out)
302 (path string :out)
303 (reverse-path string :out))
304
305 (defbinding widget-modify-style () nil
306 (widget widget)
307 (style rc-style))
308
309 (defbinding widget-get-modifier-style () rc-style
310 (widget widget))
311
312 (defbinding widget-modify-fg () nil
313 (widget widget)
314 (state state-type)
315 (color gdk:color))
316
317 (defbinding widget-modify-bg () nil
318 (widget widget)
319 (state state-type)
320 (color gdk:color))
321
322 (defbinding widget-modify-text () nil
323 (widget widget)
324 (state state-type)
325 (color gdk:color))
326
327 (defbinding widget-modify-base () nil
328 (widget widget)
329 (state state-type)
330 (color gdk:color))
331
332 (defbinding widget-modify-font () nil
333 (widget widget)
334 (state state-type)
335 (font-desc pango:font-description))
336
337 (defbinding widget-create-pango-context () pango:context
338 (widget widget))
339
340 (defbinding widget-get-pango-context () pango:context
341 (widget widget))
342
343 (defbinding widget-create-pango-layout (widget &optional text) pango:layout
344 (widget widget)
345 (text (or string null)))
346
347 (defbinding widget-render-icon (widget stock-id &optional size detail)
348 gdk:pixbuf
349 (widget widget)
350 (stock-id string)
351 ((or size -1) (or icon-size int))
352 (detail (or null string)))
353
354 (defbinding widget-push-composite-child () nil)
355
356 (defbinding widget-pop-composite-child () nil)
357
358 (defbinding widget-queue-draw-area () nil
359 (widget widget)
360 (x int) (y int) (width int) (height int))
361
362 (defbinding widget-reset-shapes () nil
363 (widget widget))
364
365 ;; (defbinding widget-set-double-buffered () nil
366 ;; (widget widget)
367 ;; (double-buffered boolean))
368
369 ;; (defbinding widget-set-redraw-on-allocate () nil
370 ;; (widget widget)
371 ;; (redraw-on-allocate boolean))
372
373 (defbinding widget-set-scroll-adjustments () boolean
374 (widget widget)
375 (hadjustment (or null adjustment))
376 (vadjustment (or null adjustment)))
377
378 (defbinding widget-mnemonic-activate () boolean
379 (widget widget)
380 (group-cycling boolean))
381
382 (defbinding widget-class-find-style-property (class name) param
383 ((type-class-peek class) pointer)
384 (name string))
385
386 (defbinding widget-class-list-style-properties (class)
387 (vector (copy-of param) n-properties)
388 ((type-class-peek class) pointer)
389 (n-properties unsigned-int :out))
390
391 (defbinding widget-region-intersect () pointer ;gdk:region
392 (widget widget)
393 (region pointer)) ;gdk:region))
394
395 (defbinding widget-send-expose () boolean
396 (widget widget)
397 (event gdk:event))
398
399 (defbinding %widget-style-get-property () nil
400 (widget widget)
401 (name string)
402 (value gvalue))
403
404 (defun style-property-value (widget style)
405 (let* ((name (string-downcase style))
406 (param (widget-class-find-style-property (class-of widget) name)))
407 (if (not param)
408 (error "~A has no such style property: ~A" widget style)
409 (with-gvalue (gvalue (param-value-type param))
410 (%widget-style-get-property widget (string-downcase style) gvalue)))))
411
412 (defbinding widget-get-accessible () atk:object
413 (widget widget))
414
415 (defbinding widget-child-focus () boolean
416 (widget widget)
417 (direction direction-type))
418
419 (defbinding widget-child-notify () nil
420 (widget widget)
421 (child-property string))
422
423 (defbinding widget-freeze-child-notify () nil
424 (widget widget))
425
426 (defbinding widget-get-clipboard () clipboard
427 (widget widget)
428 (selection int #|gdk:atom|#))
429
430 (defbinding widget-get-display () gdk:display
431 (widget widget))
432
433 (defbinding widget-get-root-window () gdk:window
434 (widget widget))
435
436 (defbinding widget-get-screen () gdk:screen
437 (widget widget))
438
439 (defbinding widget-has-screen-p () boolean
440 (widget widget))
441
442 (defbinding %widget-get-size-request () nil
443 (widget widget)
444 (width int :out)
445 (height int :out))
446
447 (defun widget-get-size-request (widget)
448 (multiple-value-bind (width height) (%widget-get-size-request widget)
449 (values (unless (= width -1) width) (unless (= height -1) height))))
450
451 (defbinding widget-set-size-request (widget width height) nil
452 (widget widget)
453 ((or width -1) int)
454 ((or height -1) int))
455
456 (defbinding widget-thaw-child-notify () nil
457 (widget widget))
458
459 (defbinding widget-list-mnemonic-labels () (glist widget)
460 (widget widget))
461
462 (defbinding widget-add-mnemonic-label () nil
463 (widget widget)
464 (label widget))
465
466 (defbinding widget-remove-mnemonic-label () nil
467 (widget widget)
468 (label widget))
469
470
471 ;;; Additional bindings and functions
472
473 (defbinding (widget-mapped-p "gtk_widget_mapped_p") () boolean
474 (widget widget))
475
476 (defbinding widget-get-size-allocation () nil
477 (widget widget)
478 (width int :out)
479 (height int :out))
480
481 (defbinding get-event-widget () widget
482 (event gdk:event))
483
484 (defun (setf widget-cursor) (cursor-type widget)
485 (let ((cursor (make-instance 'gdk:cursor :type cursor-type)))
486 (gdk:window-set-cursor (widget-window widget) cursor)))
487
488 (defbinding %widget-get-parent-window () gdk:window
489 (widget widget))
490
491 (defun %widget-parent-window (widget)
492 (when (slot-boundp widget 'parent)
493 (%widget-get-parent-window widget)))