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