chiark / gitweb /
Removed circular object references in signal handler closures
[clg] / gtk / gtkwidget.lisp
CommitLineData
112ac1d3 1;; Common Lisp bindings for GTK+ v2.x
6ac8ef47 2;; Copyright 2000-2006 Espen S. Johnsen <espen@users.sf.net>
560af5c5 3;;
112ac1d3 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:
560af5c5 11;;
112ac1d3 12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
560af5c5 14;;
112ac1d3 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.
560af5c5 22
d1b6a54e 23;; $Id: gtkwidget.lisp,v 1.31 2008-05-06 00:04:42 espen Exp $
560af5c5 24
25(in-package "GTK")
26
71267721 27#-debug-ref-counting
28(defmethod print-object ((widget widget) stream)
29 (if (and
30 (proxy-valid-p widget)
31 (slot-boundp widget 'name) (not (zerop (length (widget-name widget)))))
32 (print-unreadable-object (widget stream :type t :identity nil)
33 (format stream "~S at 0x~X"
6ac8ef47 34 (widget-name widget) (pointer-address (foreign-location widget))))
71267721 35 (call-next-method)))
36
fa60e0a2 37(defmethod shared-initialize ((widget widget) names &key (visible nil visible-p))
6ac8ef47 38 (declare (ignore names))
fa60e0a2 39 (when (and visible-p (not visible)) ; widget explicit set as not visible
40 (setf (user-data widget 'hidden-p) t)
41 (signal-connect widget 'show
42 #'(lambda ()
43 (unset-user-data widget 'hidden-p))
44 :remove t))
45 (call-next-method))
e5b416f0 46
a457d472 47(defmethod shared-initialize :after ((widget widget) names &key parent visible)
fa60e0a2 48 (declare (ignore names))
a457d472 49 (when visible
50 (widget-show widget))
fa60e0a2 51 (when parent
52 (when (slot-boundp widget 'parent)
53 (container-remove (widget-parent widget) widget))
54 (destructuring-bind (parent &rest args) (mklist parent)
55 (apply #'container-add parent widget args))))
560af5c5 56
8e947895 57(defmethod slot-unbound ((class gobject-class) (object widget)
58 (slot (eql 'child-properties)))
560af5c5 59 (cond
8e947895 60 ((slot-boundp object 'parent)
c289d084 61 (with-slots (parent child-properties) object
8e947895 62 (setf child-properties
3494da2a 63 (make-instance (find-child-class (class-of parent))
560af5c5 64 :parent parent :child object))))
8e947895 65 ((call-next-method))))
66
560af5c5 67
f5c99598 68(defparameter *widget-display-as-default-in-signal-handler-p* t)
69
4c371500 70(defmethod compute-signal-function ((widget widget) signal function object args)
f5c99598 71 (let ((wrapper
72 (if (eq object :parent)
d1b6a54e 73 #'(lambda (widget &rest emission-args)
74 (let ((all-args (nconc emission-args args)))
f5c99598 75 (if (slot-boundp widget 'parent)
76 (apply function (widget-parent widget) all-args)
77 ;; Delay until parent is set
78 (signal-connect widget 'parent-set
79 #'(lambda (old-parent)
80 (declare (ignore old-parent))
d1b6a54e 81 (apply #'signal-emit widget signal emission-args))
f5c99598 82 :remove t))))
83 (call-next-method))))
84 (if *widget-display-as-default-in-signal-handler-p*
d1b6a54e 85 #'(lambda (widget &rest args)
f5c99598 86 (let ((display (when (slot-boundp widget 'window)
87 (gdk:drawable-display (widget-window widget)))))
88 (gdk:with-default-display (display)
d1b6a54e 89 (apply wrapper widget args))))
f5c99598 90 wrapper)))
91
92
03802c3c 93
c289d084 94(defun child-property-value (widget slot)
95 (slot-value (widget-child-properties widget) slot))
560af5c5 96
c289d084 97(defun (setf child-property-value) (value widget slot)
98 (setf (slot-value (widget-child-properties widget) slot) value))
560af5c5 99
c289d084 100(defmacro with-child-properties (slots widget &body body)
101 `(with-slots ,slots (widget-child-properties ,widget)
560af5c5 102 ,@body))
103
1de3a418 104
1de3a418 105;;; Bindings
106
0d270bd9 107(defbinding widget-destroy () nil
560af5c5 108 (widget widget))
109
0d270bd9 110(defbinding widget-unparent () nil
560af5c5 111 (widget widget))
112
0d270bd9 113(defbinding widget-show () nil
560af5c5 114 (widget widget))
115
0d270bd9 116(defbinding widget-show-now () nil
560af5c5 117 (widget widget))
118
0d270bd9 119(defbinding widget-hide () nil
560af5c5 120 (widget widget))
121
fa60e0a2 122(defun widget-hidden-p (widget)
123 "Return T if WIDGET has been explicit hidden during construction."
124 (user-data widget 'hidden-p))
125
0d270bd9 126(defbinding widget-show-all () nil
560af5c5 127 (widget widget))
128
0d270bd9 129(defbinding widget-hide-all () nil
560af5c5 130 (widget widget))
131
0d270bd9 132(defbinding widget-map () nil
560af5c5 133 (widget widget))
134
0d270bd9 135(defbinding widget-unmap () nil
560af5c5 136 (widget widget))
137
0d270bd9 138(defbinding widget-realize () nil
560af5c5 139 (widget widget))
140
0d270bd9 141(defbinding widget-unrealize () nil
560af5c5 142 (widget widget))
143
1de3a418 144(defbinding widget-queue-draw () nil
145 (widget widget))
146
147(defbinding widget-queue-resize () nil
148 (widget widget))
149
8e947895 150(defbinding widget-queue-resize-no-redraw () nil
151 (widget widget))
152
153(defbinding widget-size-request
154 (widget &optional (requisition (make-instance 'requisition))) nil
1de3a418 155 (widget widget)
6ac8ef47 156 (requisition requisition :in/return))
1de3a418 157
8e947895 158(defbinding widget-get-child-requisition
159 (widget &optional (requisition (make-instance 'requisition))) nil
1de3a418 160 (widget widget)
6ac8ef47 161 (requisition requisition :in/return))
1de3a418 162
163(defbinding widget-size-allocate () nil
164 (widget widget)
165 (allocation allocation))
166
0d270bd9 167(defbinding widget-add-accelerator
560af5c5 168 (widget signal accel-group key modifiers flags) nil
169 (widget widget)
71267721 170 ((signal-name-to-string signal) string)
560af5c5 171 (accel-group accel-group)
172 ((gdk:keyval-from-name key) unsigned-int)
173 (modifiers gdk:modifier-type)
174 (flags accel-flags))
175
0d270bd9 176(defbinding widget-remove-accelerator
560af5c5 177 (widget accel-group key modifiers) nil
178 (widget widget)
179 (accel-group accel-group)
180 ((gdk:keyval-from-name key) unsigned-int)
181 (modifiers gdk:modifier-type))
182
8e947895 183(defbinding widget-set-accel-path () nil
560af5c5 184 (widget widget)
1de3a418 185 (accel-path string)
186 (accel-group accel-group))
560af5c5 187
8e947895 188(defbinding widget-list-accel-closures () (glist pointer)
189 (widget widget))
190
191(defbinding widget-can-activate-accel-p () boolean
192 (widget widget)
193 (signal-id unsigned-int))
194
195(defbinding widget-event () boolean
560af5c5 196 (widget widget)
197 (event gdk:event))
198
0d270bd9 199(defbinding widget-activate () boolean
560af5c5 200 (widget widget))
201
0d270bd9 202(defbinding widget-reparent () nil
560af5c5 203 (widget widget)
204 (new-parent widget))
205
1de3a418 206(defbinding %widget-intersect () boolean
207 (widget widget)
208 (area gdk:rectangle)
853ec10e 209 (intersection (or null gdk:rectangle)))
560af5c5 210
1de3a418 211(defun widget-intersection (widget area)
212 (let ((intersection (make-instance 'gdk:rectangle)))
213 (when (%widget-intersect widget area intersection)
214 intersection)))
560af5c5 215
1de3a418 216(defun widget-intersect-p (widget area)
853ec10e 217 (%widget-intersect widget area nil))
aace61f5 218
1de3a418 219(defbinding widget-grab-focus () nil
aace61f5 220 (widget widget))
221
1de3a418 222(defbinding widget-grab-default () nil
223 (widget widget))
560af5c5 224
0d270bd9 225(defbinding widget-add-events () nil
560af5c5 226 (widget widget)
227 (events gdk:event-mask))
228
1de3a418 229(defbinding widget-get-toplevel () widget
560af5c5 230 (widget widget))
231
1de3a418 232(defbinding widget-get-ancestor (widget type) widget
560af5c5 233 (widget widget)
234 ((find-type-number type) type-number))
235
1de3a418 236(defbinding widget-get-pointer () nil
560af5c5 237 (widget widget)
238 (x int :out)
239 (y int :out))
240
8e947895 241(defbinding widget-is-ancestor-p () boolean
560af5c5 242 (widget widget)
243 (ancestor widget))
244
1de3a418 245(defbinding widget-translate-coordinates () boolean
246 (src-widget widget)
247 (dest-widget widget)
248 (src-x int) (src-y int)
249 (set-x int :out) (dest-y int :out))
250
251(defun widget-hide-on-delete (widget)
252 "Utility function; intended to be connected to the DELETE-EVENT
8e947895 253signal on a WINDOW. The function calls WIDGET-HIDE on its
1de3a418 254argument, then returns T. If connected to DELETE-EVENT, the
255result is that clicking the close button for a window (on the window
256frame, top right corner usually) will hide but not destroy the
257window. By default, GTK+ destroys windows when DELETE-EVENT is
258received."
259 (widget-hide widget)
260 t)
261
0d270bd9 262(defbinding widget-ensure-style () nil
560af5c5 263 (widget widget))
264
0d270bd9 265(defbinding widget-reset-rc-styles () nil
560af5c5 266 (widget widget))
267
0d270bd9 268(defbinding widget-push-colormap () nil
560af5c5 269 (colormap gdk:colormap))
270
0d270bd9 271(defbinding widget-pop-colormap () nil)
560af5c5 272
8e947895 273(defbinding %widget-set-default-colormap () nil
560af5c5 274 (colormap gdk:colormap))
275
8e947895 276(defun (setf widget-default-colormap) (colormap)
277 (%widget-set-default-colormap colormap)
278 colormap)
279
280(defbinding (widget-default-style "gtk_widget_get_default_style") () style)
560af5c5 281
8e947895 282(defbinding (widget-default-colromap "gtk_widget_get_default_colormap")
283 () gdk:colormap)
560af5c5 284
8e947895 285(defbinding (widget-default-visual "gtk_widget_get_default_visual")
286 () gdk:visual)
1de3a418 287
8e947895 288(defbinding (widget-default-direction "gtk_widget_get_default_direction")
289 () text-direction)
1de3a418 290
8e947895 291(defbinding %widget-set-default-direction () nil
292 (direction text-direction))
293
294(defun (setf widget-default-direction) (direction)
295 (%widget-set-default-direction direction)
296 direction)
1de3a418 297
0d270bd9 298(defbinding widget-shape-combine-mask () nil
560af5c5 299 (widget widget)
8e947895 300 (shape-mask (or null gdk:bitmap))
560af5c5 301 (x-offset int)
302 (y-offset int))
303
2b18bfaf 304(defun widget-path (widget)
305 (let ((subpath (list (if (and
306 (slot-boundp widget 'name)
307 (not (zerop (length (widget-name widget)))))
308 (widget-name widget)
309 (type-of widget)))))
310 (if (slot-boundp widget 'parent)
311 (nconc (widget-path (widget-parent widget)) subpath)
312 subpath)))
313
314(defun widget-class-path (widget)
315 (let ((subpath (list (type-of widget))))
316 (if (slot-boundp widget 'parent)
317 (nconc (widget-class-path (widget-parent widget)) subpath)
318 subpath)))
319
320
321(defun widget-path-lookup (path &optional (root (nreverse (window-list-toplevels))) (error-p t))
322 (let ((component (first path)))
323 (loop
324 for widget in (mklist root)
325 do (when (or
326 (and
327 (stringp component) (slot-boundp widget 'name)
328 (string= component (widget-name widget)))
329 (and
330 (symbolp component) (typep widget component)))
331 (cond
332 ((endp (rest path)) (return widget))
333 ((typep widget 'container)
334 (let ((descendant (widget-path-lookup (rest path) (container-children widget) nil)))
335 (when descendant
336 (return descendant))))))))
337 (when error-p
338 (error "Widget not found: ~A" path)))
339
340
341(defun widget-find (name &optional (root (nreverse (window-list-toplevels))) (error-p t))
342 "Search for a widget with the given name. ROOT should be a container
343widget or a list of containers."
344 (loop
345 for widget in (mklist root)
346 do (cond
347 ((and (slot-boundp widget 'name) (string= name (widget-name widget)))
80722817 348 (return-from widget-find widget))
2b18bfaf 349 ((typep widget 'container)
350 (let ((descendant (widget-find name (container-children widget) nil)))
351 (when descendant
80722817 352 (return-from widget-find descendant))))))
2b18bfaf 353 (when error-p
354 (error "Widget not found: ~A" name)))
1de3a418 355
1de3a418 356
357(defbinding widget-modify-style () nil
358 (widget widget)
359 (style rc-style))
360
8e947895 361(defbinding widget-get-modifier-style () rc-style
1de3a418 362 (widget widget))
363
8e947895 364(defbinding widget-modify-fg () nil
1de3a418 365 (widget widget)
366 (state state-type)
367 (color gdk:color))
368
8e947895 369(defbinding widget-modify-bg () nil
1de3a418 370 (widget widget)
371 (state state-type)
372 (color gdk:color))
373
374(defbinding widget-modify-text () nil
375 (widget widget)
376 (state state-type)
377 (color gdk:color))
378
379(defbinding widget-modify-base () nil
380 (widget widget)
381 (state state-type)
382 (color gdk:color))
383
fbc09061 384(defbinding widget-modify-font (widget font-desc) nil
1de3a418 385 (widget widget)
fbc09061 386 ((etypecase font-desc
387 (pango:font-description font-desc)
388 (string (pango:font-description-from-string font-desc)))
389 pango:font-description))
1de3a418 390
391(defbinding widget-create-pango-context () pango:context
392 (widget widget))
393
394(defbinding widget-get-pango-context () pango:context
395 (widget widget))
396
397(defbinding widget-create-pango-layout (widget &optional text) pango:layout
398 (widget widget)
399 (text (or string null)))
400
8e947895 401(defbinding widget-render-icon (widget stock-id &optional size detail)
402 gdk:pixbuf
1de3a418 403 (widget widget)
404 (stock-id string)
8e947895 405 ((or size -1) (or icon-size int))
406 (detail (or null string)))
1de3a418 407
408(defbinding widget-push-composite-child () nil)
409
410(defbinding widget-pop-composite-child () nil)
411
412(defbinding widget-queue-draw-area () nil
413 (widget widget)
414 (x int) (y int) (width int) (height int))
415
416(defbinding widget-reset-shapes () nil
417 (widget widget))
418
cf34dadb 419(defbinding widget-set-double-buffered () nil
420 (widget widget)
421 (double-buffered boolean))
1de3a418 422
8e947895 423;; (defbinding widget-set-redraw-on-allocate () nil
424;; (widget widget)
425;; (redraw-on-allocate boolean))
1de3a418 426
427(defbinding widget-set-scroll-adjustments () boolean
428 (widget widget)
8e947895 429 (hadjustment (or null adjustment))
430 (vadjustment (or null adjustment)))
1de3a418 431
432(defbinding widget-mnemonic-activate () boolean
433 (widget widget)
434 (group-cycling boolean))
435
8e947895 436(defbinding widget-class-find-style-property (class name) param
437 ((type-class-peek class) pointer)
438 (name string))
439
440(defbinding widget-class-list-style-properties (class)
441 (vector (copy-of param) n-properties)
442 ((type-class-peek class) pointer)
443 (n-properties unsigned-int :out))
444
1de3a418 445(defbinding widget-region-intersect () pointer ;gdk:region
446 (widget widget)
447 (region pointer)) ;gdk:region))
448
8e947895 449(defbinding widget-send-expose () boolean
1de3a418 450 (widget widget)
451 (event gdk:event))
452
8e947895 453(defbinding %widget-style-get-property () nil
454 (widget widget)
455 (name string)
456 (value gvalue))
457
458(defun style-property-value (widget style)
459 (let* ((name (string-downcase style))
460 (param (widget-class-find-style-property (class-of widget) name)))
461 (if (not param)
462 (error "~A has no such style property: ~A" widget style)
463 (with-gvalue (gvalue (param-value-type param))
464 (%widget-style-get-property widget (string-downcase style) gvalue)))))
465
1de3a418 466(defbinding widget-get-accessible () atk:object
467 (widget widget))
468
469(defbinding widget-child-focus () boolean
470 (widget widget)
471 (direction direction-type))
472
473(defbinding widget-child-notify () nil
474 (widget widget)
475 (child-property string))
476
477(defbinding widget-freeze-child-notify () nil
478 (widget widget))
479
8e947895 480(defbinding widget-get-clipboard () clipboard
481 (widget widget)
482 (selection int #|gdk:atom|#))
483
484(defbinding widget-get-display () gdk:display
485 (widget widget))
486
487(defbinding widget-get-root-window () gdk:window
488 (widget widget))
489
490(defbinding widget-get-screen () gdk:screen
491 (widget widget))
492
493(defbinding widget-has-screen-p () boolean
494 (widget widget))
495
1de3a418 496(defbinding %widget-get-size-request () nil
497 (widget widget)
498 (width int :out)
499 (height int :out))
500
501(defun widget-get-size-request (widget)
502 (multiple-value-bind (width height) (%widget-get-size-request widget)
9adccb27 503 (values (unless (= width -1) width) (unless (= height -1) height))))
1de3a418 504
505(defbinding widget-set-size-request (widget width height) nil
506 (widget widget)
507 ((or width -1) int)
508 ((or height -1) int))
509
510(defbinding widget-thaw-child-notify () nil
511 (widget widget))
512
8e947895 513(defbinding widget-list-mnemonic-labels () (glist widget)
514 (widget widget))
515
516(defbinding widget-add-mnemonic-label () nil
517 (widget widget)
518 (label widget))
519
520(defbinding widget-remove-mnemonic-label () nil
521 (widget widget)
522 (label widget))
523
1de3a418 524
525;;; Additional bindings and functions
526
7166a443 527(defbinding %widget-flags () int
560af5c5 528 (widget widget))
529
7166a443 530(defun widget-flags (widget)
531 (let ((flags (%widget-flags widget)))
532 (nconc
533 (int-to-object-flags flags)
534 (int-to-widget-flags flags))))
535
536(defun widget-mapped-p (widget)
537 (find :mapped (widget-flags widget)))
538
539(defun widget-realized-p (widget)
540 (find :realized (widget-flags widget)))
541
1de3a418 542(defbinding widget-get-size-allocation () nil
543 (widget widget)
544 (width int :out)
545 (height int :out))
546
547(defbinding get-event-widget () widget
548 (event gdk:event))
549
550(defun (setf widget-cursor) (cursor-type widget)
25d755bb 551 (warn "(SETF WIDGET-CURSOR) is deprecated, use WIDGET-SET-CURSOR instead")
552 (widget-set-cursor widget cursor-type))
553
554(defun widget-set-cursor (widget cursor &rest args)
555 (gdk:window-set-cursor (widget-window widget)
556 (apply #'gdk:ensure-cursor cursor args)))
8e947895 557
558(defbinding %widget-get-parent-window () gdk:window
559 (widget widget))
560
561(defun %widget-parent-window (widget)
562 (when (slot-boundp widget 'parent)
563 (%widget-get-parent-window widget)))