Changed to MIT license
[clg] / gtk / gtkwidget.lisp
CommitLineData
55212af1 1;; Common Lisp bindings for GTK+ v2.x
2;; Copyright 2000-2005 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
55212af1 23;; $Id: gtkwidget.lisp,v 1.18 2005/04/23 16:48:52 espen Exp $
0d07716f 24
25(in-package "GTK")
26
27
d0cc9e86 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))
dd392521 36
9d5e4733 37(defmethod shared-initialize :after ((widget widget) names &key parent visible)
d0cc9e86 38 (declare (ignore names))
9d5e4733 39 (when visible
40 (widget-show widget))
d0cc9e86 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))))
0d07716f 46
03272be4 47(defmethod slot-unbound ((class gobject-class) (object widget)
48 (slot (eql 'child-properties)))
0d07716f 49 (cond
03272be4 50 ((slot-boundp object 'parent)
c66e7b94 51 (with-slots (parent child-properties) object
03272be4 52 (setf child-properties
53 (make-instance
54 (gethash (class-of parent) *container-to-child-class-mappings*)
0d07716f 55 :parent parent :child object))))
03272be4 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)))
0d07716f 64
d0cc9e86 65(defmethod compute-signal-function ((widget widget) signal function object)
66 (if (eq object :parent)
2519d4ca 67 #'(lambda (&rest args)
68 (if (slot-boundp widget 'parent)
69 (apply function (widget-parent widget) (rest args))
03272be4 70 ;; Delay until parent is set
2519d4ca 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
c66e7b94 83(defun child-property-value (widget slot)
84 (slot-value (widget-child-properties widget) slot))
0d07716f 85
c66e7b94 86(defun (setf child-property-value) (value widget slot)
87 (setf (slot-value (widget-child-properties widget) slot) value))
0d07716f 88
c66e7b94 89(defmacro with-child-properties (slots widget &body body)
90 `(with-slots ,slots (widget-child-properties ,widget)
0d07716f 91 ,@body))
92
860e6a2e 93
860e6a2e 94;;; Bindings
95
08aad4db 96(defbinding widget-destroy () nil
0d07716f 97 (widget widget))
98
08aad4db 99(defbinding widget-unparent () nil
0d07716f 100 (widget widget))
101
08aad4db 102(defbinding widget-show () nil
0d07716f 103 (widget widget))
104
08aad4db 105(defbinding widget-show-now () nil
0d07716f 106 (widget widget))
107
08aad4db 108(defbinding widget-hide () nil
0d07716f 109 (widget widget))
110
d0cc9e86 111(defun widget-hidden-p (widget)
112 "Return T if WIDGET has been explicit hidden during construction."
113 (user-data widget 'hidden-p))
114
08aad4db 115(defbinding widget-show-all () nil
0d07716f 116 (widget widget))
117
08aad4db 118(defbinding widget-hide-all () nil
0d07716f 119 (widget widget))
120
08aad4db 121(defbinding widget-map () nil
0d07716f 122 (widget widget))
123
08aad4db 124(defbinding widget-unmap () nil
0d07716f 125 (widget widget))
126
08aad4db 127(defbinding widget-realize () nil
0d07716f 128 (widget widget))
129
08aad4db 130(defbinding widget-unrealize () nil
0d07716f 131 (widget widget))
132
860e6a2e 133(defbinding widget-queue-draw () nil
134 (widget widget))
135
136(defbinding widget-queue-resize () nil
137 (widget widget))
138
03272be4 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
860e6a2e 144 (widget widget)
03272be4 145 (requisition requisition :return))
860e6a2e 146
03272be4 147(defbinding widget-get-child-requisition
148 (widget &optional (requisition (make-instance 'requisition))) nil
860e6a2e 149 (widget widget)
03272be4 150 (requisition requisition :return))
860e6a2e 151
152(defbinding widget-size-allocate () nil
153 (widget widget)
154 (allocation allocation))
155
08aad4db 156(defbinding widget-add-accelerator
0d07716f 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
08aad4db 165(defbinding widget-remove-accelerator
0d07716f 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
03272be4 172(defbinding widget-set-accel-path () nil
0d07716f 173 (widget widget)
860e6a2e 174 (accel-path string)
175 (accel-group accel-group))
0d07716f 176
03272be4 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
0d07716f 185 (widget widget)
186 (event gdk:event))
187
08aad4db 188(defbinding widget-activate () boolean
0d07716f 189 (widget widget))
190
08aad4db 191(defbinding widget-reparent () nil
0d07716f 192 (widget widget)
193 (new-parent widget))
194
860e6a2e 195(defbinding %widget-intersect () boolean
196 (widget widget)
197 (area gdk:rectangle)
ecdb2ae2 198 (intersection (or null gdk:rectangle)))
0d07716f 199
860e6a2e 200(defun widget-intersection (widget area)
201 (let ((intersection (make-instance 'gdk:rectangle)))
202 (when (%widget-intersect widget area intersection)
203 intersection)))
0d07716f 204
860e6a2e 205(defun widget-intersect-p (widget area)
ecdb2ae2 206 (%widget-intersect widget area nil))
13148f19 207
860e6a2e 208(defbinding widget-grab-focus () nil
13148f19 209 (widget widget))
210
860e6a2e 211(defbinding widget-grab-default () nil
212 (widget widget))
0d07716f 213
08aad4db 214(defbinding widget-add-events () nil
0d07716f 215 (widget widget)
216 (events gdk:event-mask))
217
860e6a2e 218(defbinding widget-get-toplevel () widget
0d07716f 219 (widget widget))
220
860e6a2e 221(defbinding widget-get-ancestor (widget type) widget
0d07716f 222 (widget widget)
223 ((find-type-number type) type-number))
224
860e6a2e 225(defbinding widget-get-pointer () nil
0d07716f 226 (widget widget)
227 (x int :out)
228 (y int :out))
229
03272be4 230(defbinding widget-is-ancestor-p () boolean
0d07716f 231 (widget widget)
232 (ancestor widget))
233
860e6a2e 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
03272be4 242signal on a WINDOW. The function calls WIDGET-HIDE on its
860e6a2e 243argument, then returns T. If connected to DELETE-EVENT, the
244result is that clicking the close button for a window (on the window
245frame, top right corner usually) will hide but not destroy the
246window. By default, GTK+ destroys windows when DELETE-EVENT is
247received."
248 (widget-hide widget)
249 t)
250
08aad4db 251(defbinding widget-ensure-style () nil
0d07716f 252 (widget widget))
253
08aad4db 254(defbinding widget-reset-rc-styles () nil
0d07716f 255 (widget widget))
256
08aad4db 257(defbinding widget-push-colormap () nil
0d07716f 258 (colormap gdk:colormap))
259
08aad4db 260(defbinding widget-pop-colormap () nil)
0d07716f 261
03272be4 262(defbinding %widget-set-default-colormap () nil
0d07716f 263 (colormap gdk:colormap))
264
03272be4 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)
0d07716f 270
03272be4 271(defbinding (widget-default-colromap "gtk_widget_get_default_colormap")
272 () gdk:colormap)
0d07716f 273
03272be4 274(defbinding (widget-default-visual "gtk_widget_get_default_visual")
275 () gdk:visual)
860e6a2e 276
03272be4 277(defbinding (widget-default-direction "gtk_widget_get_default_direction")
278 () text-direction)
860e6a2e 279
03272be4 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)
860e6a2e 286
08aad4db 287(defbinding widget-shape-combine-mask () nil
0d07716f 288 (widget widget)
03272be4 289 (shape-mask (or null gdk:bitmap))
0d07716f 290 (x-offset int)
291 (y-offset int))
292
860e6a2e 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
03272be4 309(defbinding widget-get-modifier-style () rc-style
860e6a2e 310 (widget widget))
311
03272be4 312(defbinding widget-modify-fg () nil
860e6a2e 313 (widget widget)
314 (state state-type)
315 (color gdk:color))
316
03272be4 317(defbinding widget-modify-bg () nil
860e6a2e 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
03272be4 347(defbinding widget-render-icon (widget stock-id &optional size detail)
348 gdk:pixbuf
860e6a2e 349 (widget widget)
350 (stock-id string)
03272be4 351 ((or size -1) (or icon-size int))
352 (detail (or null string)))
860e6a2e 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
03272be4 365;; (defbinding widget-set-double-buffered () nil
366;; (widget widget)
367;; (double-buffered boolean))
860e6a2e 368
03272be4 369;; (defbinding widget-set-redraw-on-allocate () nil
370;; (widget widget)
371;; (redraw-on-allocate boolean))
860e6a2e 372
373(defbinding widget-set-scroll-adjustments () boolean
374 (widget widget)
03272be4 375 (hadjustment (or null adjustment))
376 (vadjustment (or null adjustment)))
860e6a2e 377
378(defbinding widget-mnemonic-activate () boolean
379 (widget widget)
380 (group-cycling boolean))
381
03272be4 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
860e6a2e 391(defbinding widget-region-intersect () pointer ;gdk:region
392 (widget widget)
393 (region pointer)) ;gdk:region))
394
03272be4 395(defbinding widget-send-expose () boolean
860e6a2e 396 (widget widget)
397 (event gdk:event))
398
03272be4 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
860e6a2e 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
03272be4 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
860e6a2e 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)
6baf860c 449 (values (unless (= width -1) width) (unless (= height -1) height))))
860e6a2e 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
03272be4 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
860e6a2e 470
471;;; Additional bindings and functions
472
ecdb2ae2 473(defbinding (widget-mapped-p "gtk_widget_mapped_p") () boolean
0d07716f 474 (widget widget))
475
860e6a2e 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)
b7f08643 485 (let ((cursor (make-instance 'gdk:cursor :type cursor-type)))
860e6a2e 486 (gdk:window-set-cursor (widget-window widget) cursor)))
03272be4 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)))