Re-registering custom signals and class closures when loading saved images
[clg] / gtk / gtkwidget.lisp
CommitLineData
55212af1 1;; Common Lisp bindings for GTK+ v2.x
aa032564 2;; Copyright 2000-2006 Espen S. Johnsen <espen@users.sf.net>
0d07716f 3;;
55212af1 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:
0d07716f 11;;
55212af1 12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
0d07716f 14;;
55212af1 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.
0d07716f 22
bdfd0166 23;; $Id: gtkwidget.lisp,v 1.32 2008/11/25 22:17:08 espen Exp $
0d07716f 24
25(in-package "GTK")
26
7af3f90b 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"
aa032564 34 (widget-name widget) (pointer-address (foreign-location widget))))
7af3f90b 35 (call-next-method)))
36
d0cc9e86 37(defmethod shared-initialize ((widget widget) names &key (visible nil visible-p))
aa032564 38 (declare (ignore names))
d0cc9e86 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))
dd392521 46
9d5e4733 47(defmethod shared-initialize :after ((widget widget) names &key parent visible)
d0cc9e86 48 (declare (ignore names))
9d5e4733 49 (when visible
50 (widget-show widget))
d0cc9e86 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))))
0d07716f 56
03272be4 57(defmethod slot-unbound ((class gobject-class) (object widget)
58 (slot (eql 'child-properties)))
0d07716f 59 (cond
03272be4 60 ((slot-boundp object 'parent)
c66e7b94 61 (with-slots (parent child-properties) object
03272be4 62 (setf child-properties
f0c03460 63 (make-instance (find-child-class (class-of parent))
0d07716f 64 :parent parent :child object))))
03272be4 65 ((call-next-method))))
66
0d07716f 67
6b465328 68(defparameter *widget-display-as-default-in-signal-handler-p* t)
69
a68868d1 70(defmethod compute-signal-function ((widget widget) signal function object args)
6b465328 71 (let ((wrapper
72 (if (eq object :parent)
bedf7b84 73 #'(lambda (widget &rest emission-args)
74 (let ((all-args (nconc emission-args args)))
6b465328 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))
bedf7b84 81 (apply #'signal-emit widget signal emission-args))
6b465328 82 :remove t))))
83 (call-next-method))))
84 (if *widget-display-as-default-in-signal-handler-p*
bedf7b84 85 #'(lambda (widget &rest args)
6b465328 86 (let ((display (when (slot-boundp widget 'window)
87 (gdk:drawable-display (widget-window widget)))))
88 (gdk:with-default-display (display)
bedf7b84 89 (apply wrapper widget args))))
6b465328 90 wrapper)))
91
92
2519d4ca 93
c66e7b94 94(defun child-property-value (widget slot)
95 (slot-value (widget-child-properties widget) slot))
0d07716f 96
c66e7b94 97(defun (setf child-property-value) (value widget slot)
98 (setf (slot-value (widget-child-properties widget) slot) value))
0d07716f 99
c66e7b94 100(defmacro with-child-properties (slots widget &body body)
101 `(with-slots ,slots (widget-child-properties ,widget)
0d07716f 102 ,@body))
103
860e6a2e 104
860e6a2e 105;;; Bindings
106
08aad4db 107(defbinding widget-destroy () nil
0d07716f 108 (widget widget))
109
08aad4db 110(defbinding widget-unparent () nil
0d07716f 111 (widget widget))
112
08aad4db 113(defbinding widget-show () nil
0d07716f 114 (widget widget))
115
08aad4db 116(defbinding widget-show-now () nil
0d07716f 117 (widget widget))
118
08aad4db 119(defbinding widget-hide () nil
0d07716f 120 (widget widget))
121
d0cc9e86 122(defun widget-hidden-p (widget)
123 "Return T if WIDGET has been explicit hidden during construction."
124 (user-data widget 'hidden-p))
125
08aad4db 126(defbinding widget-show-all () nil
0d07716f 127 (widget widget))
128
08aad4db 129(defbinding widget-hide-all () nil
0d07716f 130 (widget widget))
131
08aad4db 132(defbinding widget-map () nil
0d07716f 133 (widget widget))
134
08aad4db 135(defbinding widget-unmap () nil
0d07716f 136 (widget widget))
137
08aad4db 138(defbinding widget-realize () nil
0d07716f 139 (widget widget))
140
08aad4db 141(defbinding widget-unrealize () nil
0d07716f 142 (widget widget))
143
860e6a2e 144(defbinding widget-queue-draw () nil
145 (widget widget))
146
147(defbinding widget-queue-resize () nil
148 (widget widget))
149
03272be4 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
860e6a2e 155 (widget widget)
aa032564 156 (requisition requisition :in/return))
860e6a2e 157
03272be4 158(defbinding widget-get-child-requisition
159 (widget &optional (requisition (make-instance 'requisition))) nil
860e6a2e 160 (widget widget)
aa032564 161 (requisition requisition :in/return))
860e6a2e 162
163(defbinding widget-size-allocate () nil
164 (widget widget)
165 (allocation allocation))
166
08aad4db 167(defbinding widget-add-accelerator
0d07716f 168 (widget signal accel-group key modifiers flags) nil
169 (widget widget)
7af3f90b 170 ((signal-name-to-string signal) string)
0d07716f 171 (accel-group accel-group)
172 ((gdk:keyval-from-name key) unsigned-int)
173 (modifiers gdk:modifier-type)
174 (flags accel-flags))
175
08aad4db 176(defbinding widget-remove-accelerator
0d07716f 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
03272be4 183(defbinding widget-set-accel-path () nil
0d07716f 184 (widget widget)
860e6a2e 185 (accel-path string)
186 (accel-group accel-group))
0d07716f 187
03272be4 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
0d07716f 196 (widget widget)
197 (event gdk:event))
198
08aad4db 199(defbinding widget-activate () boolean
0d07716f 200 (widget widget))
201
08aad4db 202(defbinding widget-reparent () nil
0d07716f 203 (widget widget)
204 (new-parent widget))
205
860e6a2e 206(defbinding %widget-intersect () boolean
207 (widget widget)
208 (area gdk:rectangle)
ecdb2ae2 209 (intersection (or null gdk:rectangle)))
0d07716f 210
860e6a2e 211(defun widget-intersection (widget area)
212 (let ((intersection (make-instance 'gdk:rectangle)))
213 (when (%widget-intersect widget area intersection)
214 intersection)))
0d07716f 215
860e6a2e 216(defun widget-intersect-p (widget area)
ecdb2ae2 217 (%widget-intersect widget area nil))
13148f19 218
860e6a2e 219(defbinding widget-grab-focus () nil
13148f19 220 (widget widget))
221
860e6a2e 222(defbinding widget-grab-default () nil
223 (widget widget))
0d07716f 224
08aad4db 225(defbinding widget-add-events () nil
0d07716f 226 (widget widget)
227 (events gdk:event-mask))
228
860e6a2e 229(defbinding widget-get-toplevel () widget
0d07716f 230 (widget widget))
231
860e6a2e 232(defbinding widget-get-ancestor (widget type) widget
0d07716f 233 (widget widget)
234 ((find-type-number type) type-number))
235
860e6a2e 236(defbinding widget-get-pointer () nil
0d07716f 237 (widget widget)
238 (x int :out)
239 (y int :out))
240
03272be4 241(defbinding widget-is-ancestor-p () boolean
0d07716f 242 (widget widget)
243 (ancestor widget))
244
860e6a2e 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
03272be4 253signal on a WINDOW. The function calls WIDGET-HIDE on its
860e6a2e 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
08aad4db 262(defbinding widget-ensure-style () nil
0d07716f 263 (widget widget))
264
08aad4db 265(defbinding widget-reset-rc-styles () nil
0d07716f 266 (widget widget))
267
08aad4db 268(defbinding widget-push-colormap () nil
0d07716f 269 (colormap gdk:colormap))
270
08aad4db 271(defbinding widget-pop-colormap () nil)
0d07716f 272
03272be4 273(defbinding %widget-set-default-colormap () nil
0d07716f 274 (colormap gdk:colormap))
275
03272be4 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)
0d07716f 281
03272be4 282(defbinding (widget-default-colromap "gtk_widget_get_default_colormap")
283 () gdk:colormap)
0d07716f 284
03272be4 285(defbinding (widget-default-visual "gtk_widget_get_default_visual")
286 () gdk:visual)
860e6a2e 287
03272be4 288(defbinding (widget-default-direction "gtk_widget_get_default_direction")
289 () text-direction)
860e6a2e 290
03272be4 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)
860e6a2e 297
08aad4db 298(defbinding widget-shape-combine-mask () nil
0d07716f 299 (widget widget)
03272be4 300 (shape-mask (or null gdk:bitmap))
0d07716f 301 (x-offset int)
302 (y-offset int))
303
46158111 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)))
9afb775d 348 (return-from widget-find widget))
46158111 349 ((typep widget 'container)
350 (let ((descendant (widget-find name (container-children widget) nil)))
351 (when descendant
9afb775d 352 (return-from widget-find descendant))))))
46158111 353 (when error-p
354 (error "Widget not found: ~A" name)))
860e6a2e 355
860e6a2e 356
357(defbinding widget-modify-style () nil
358 (widget widget)
359 (style rc-style))
360
03272be4 361(defbinding widget-get-modifier-style () rc-style
860e6a2e 362 (widget widget))
363
03272be4 364(defbinding widget-modify-fg () nil
860e6a2e 365 (widget widget)
366 (state state-type)
367 (color gdk:color))
368
03272be4 369(defbinding widget-modify-bg () nil
860e6a2e 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
b22f5374 384(defbinding widget-modify-font (widget font-desc) nil
860e6a2e 385 (widget widget)
b22f5374 386 ((etypecase font-desc
387 (pango:font-description font-desc)
388 (string (pango:font-description-from-string font-desc)))
389 pango:font-description))
860e6a2e 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
03272be4 401(defbinding widget-render-icon (widget stock-id &optional size detail)
402 gdk:pixbuf
860e6a2e 403 (widget widget)
404 (stock-id string)
03272be4 405 ((or size -1) (or icon-size int))
406 (detail (or null string)))
860e6a2e 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
44cba00e 419(defbinding widget-set-double-buffered () nil
420 (widget widget)
421 (double-buffered boolean))
860e6a2e 422
03272be4 423;; (defbinding widget-set-redraw-on-allocate () nil
424;; (widget widget)
425;; (redraw-on-allocate boolean))
860e6a2e 426
427(defbinding widget-set-scroll-adjustments () boolean
428 (widget widget)
03272be4 429 (hadjustment (or null adjustment))
430 (vadjustment (or null adjustment)))
860e6a2e 431
432(defbinding widget-mnemonic-activate () boolean
433 (widget widget)
434 (group-cycling boolean))
435
03272be4 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
860e6a2e 445(defbinding widget-region-intersect () pointer ;gdk:region
446 (widget widget)
447 (region pointer)) ;gdk:region))
448
03272be4 449(defbinding widget-send-expose () boolean
860e6a2e 450 (widget widget)
451 (event gdk:event))
452
03272be4 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
860e6a2e 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
03272be4 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
860e6a2e 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)
6baf860c 503 (values (unless (= width -1) width) (unless (= height -1) height))))
860e6a2e 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
03272be4 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
bdfd0166 524#?(pkg-exists-p "gtk+-2.0" :atleast-version "2.14.0")
525(defbinding widget-get-snapshot () gdk:pixmap
526 (widget widget)
527 (clip-rect (or null gdk:rectangle)))
528
860e6a2e 529
530;;; Additional bindings and functions
531
83147967 532(defbinding %widget-flags () int
0d07716f 533 (widget widget))
534
83147967 535(defun widget-flags (widget)
536 (let ((flags (%widget-flags widget)))
537 (nconc
538 (int-to-object-flags flags)
539 (int-to-widget-flags flags))))
540
541(defun widget-mapped-p (widget)
542 (find :mapped (widget-flags widget)))
543
544(defun widget-realized-p (widget)
545 (find :realized (widget-flags widget)))
546
860e6a2e 547(defbinding widget-get-size-allocation () nil
548 (widget widget)
549 (width int :out)
550 (height int :out))
551
552(defbinding get-event-widget () widget
553 (event gdk:event))
554
555(defun (setf widget-cursor) (cursor-type widget)
6091b3e8 556 (warn "(SETF WIDGET-CURSOR) is deprecated, use WIDGET-SET-CURSOR instead")
557 (widget-set-cursor widget cursor-type))
558
559(defun widget-set-cursor (widget cursor &rest args)
560 (gdk:window-set-cursor (widget-window widget)
561 (apply #'gdk:ensure-cursor cursor args)))
03272be4 562
563(defbinding %widget-get-parent-window () gdk:window
564 (widget widget))
565
566(defun %widget-parent-window (widget)
567 (when (slot-boundp widget 'parent)
568 (%widget-get-parent-window widget)))