1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2006 Espen S. Johnsen <espen@users.sf.net>
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:
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
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.
23 ;; $Id: gtkwidget.lisp,v 1.31 2008/05/06 00:04:42 espen Exp $
28 (defmethod print-object ((widget widget) stream)
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"
34 (widget-name widget) (pointer-address (foreign-location widget))))
37 (defmethod shared-initialize ((widget widget) names &key (visible nil visible-p))
38 (declare (ignore names))
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
43 (unset-user-data widget 'hidden-p))
47 (defmethod shared-initialize :after ((widget widget) names &key parent visible)
48 (declare (ignore names))
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))))
57 (defmethod slot-unbound ((class gobject-class) (object widget)
58 (slot (eql 'child-properties)))
60 ((slot-boundp object 'parent)
61 (with-slots (parent child-properties) object
62 (setf child-properties
63 (make-instance (find-child-class (class-of parent))
64 :parent parent :child object))))
65 ((call-next-method))))
68 (defparameter *widget-display-as-default-in-signal-handler-p* t)
70 (defmethod compute-signal-function ((widget widget) signal function object args)
72 (if (eq object :parent)
73 #'(lambda (widget &rest emission-args)
74 (let ((all-args (nconc emission-args args)))
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))
81 (apply #'signal-emit widget signal emission-args))
84 (if *widget-display-as-default-in-signal-handler-p*
85 #'(lambda (widget &rest args)
86 (let ((display (when (slot-boundp widget 'window)
87 (gdk:drawable-display (widget-window widget)))))
88 (gdk:with-default-display (display)
89 (apply wrapper widget args))))
94 (defun child-property-value (widget slot)
95 (slot-value (widget-child-properties widget) slot))
97 (defun (setf child-property-value) (value widget slot)
98 (setf (slot-value (widget-child-properties widget) slot) value))
100 (defmacro with-child-properties (slots widget &body body)
101 `(with-slots ,slots (widget-child-properties ,widget)
107 (defbinding widget-destroy () nil
110 (defbinding widget-unparent () nil
113 (defbinding widget-show () nil
116 (defbinding widget-show-now () nil
119 (defbinding widget-hide () nil
122 (defun widget-hidden-p (widget)
123 "Return T if WIDGET has been explicit hidden during construction."
124 (user-data widget 'hidden-p))
126 (defbinding widget-show-all () nil
129 (defbinding widget-hide-all () nil
132 (defbinding widget-map () nil
135 (defbinding widget-unmap () nil
138 (defbinding widget-realize () nil
141 (defbinding widget-unrealize () nil
144 (defbinding widget-queue-draw () nil
147 (defbinding widget-queue-resize () nil
150 (defbinding widget-queue-resize-no-redraw () nil
153 (defbinding widget-size-request
154 (widget &optional (requisition (make-instance 'requisition))) nil
156 (requisition requisition :in/return))
158 (defbinding widget-get-child-requisition
159 (widget &optional (requisition (make-instance 'requisition))) nil
161 (requisition requisition :in/return))
163 (defbinding widget-size-allocate () nil
165 (allocation allocation))
167 (defbinding widget-add-accelerator
168 (widget signal accel-group key modifiers flags) nil
170 ((signal-name-to-string signal) string)
171 (accel-group accel-group)
172 ((gdk:keyval-from-name key) unsigned-int)
173 (modifiers gdk:modifier-type)
176 (defbinding widget-remove-accelerator
177 (widget accel-group key modifiers) nil
179 (accel-group accel-group)
180 ((gdk:keyval-from-name key) unsigned-int)
181 (modifiers gdk:modifier-type))
183 (defbinding widget-set-accel-path () nil
186 (accel-group accel-group))
188 (defbinding widget-list-accel-closures () (glist pointer)
191 (defbinding widget-can-activate-accel-p () boolean
193 (signal-id unsigned-int))
195 (defbinding widget-event () boolean
199 (defbinding widget-activate () boolean
202 (defbinding widget-reparent () nil
206 (defbinding %widget-intersect () boolean
209 (intersection (or null gdk:rectangle)))
211 (defun widget-intersection (widget area)
212 (let ((intersection (make-instance 'gdk:rectangle)))
213 (when (%widget-intersect widget area intersection)
216 (defun widget-intersect-p (widget area)
217 (%widget-intersect widget area nil))
219 (defbinding widget-grab-focus () nil
222 (defbinding widget-grab-default () nil
225 (defbinding widget-add-events () nil
227 (events gdk:event-mask))
229 (defbinding widget-get-toplevel () widget
232 (defbinding widget-get-ancestor (widget type) widget
234 ((find-type-number type) type-number))
236 (defbinding widget-get-pointer () nil
241 (defbinding widget-is-ancestor-p () boolean
245 (defbinding widget-translate-coordinates () boolean
248 (src-x int) (src-y int)
249 (set-x int :out) (dest-y int :out))
251 (defun widget-hide-on-delete (widget)
252 "Utility function; intended to be connected to the DELETE-EVENT
253 signal on a WINDOW. The function calls WIDGET-HIDE on its
254 argument, then returns T. If connected to DELETE-EVENT, the
255 result is that clicking the close button for a window (on the window
256 frame, top right corner usually) will hide but not destroy the
257 window. By default, GTK+ destroys windows when DELETE-EVENT is
262 (defbinding widget-ensure-style () nil
265 (defbinding widget-reset-rc-styles () nil
268 (defbinding widget-push-colormap () nil
269 (colormap gdk:colormap))
271 (defbinding widget-pop-colormap () nil)
273 (defbinding %widget-set-default-colormap () nil
274 (colormap gdk:colormap))
276 (defun (setf widget-default-colormap) (colormap)
277 (%widget-set-default-colormap colormap)
280 (defbinding (widget-default-style "gtk_widget_get_default_style") () style)
282 (defbinding (widget-default-colromap "gtk_widget_get_default_colormap")
285 (defbinding (widget-default-visual "gtk_widget_get_default_visual")
288 (defbinding (widget-default-direction "gtk_widget_get_default_direction")
291 (defbinding %widget-set-default-direction () nil
292 (direction text-direction))
294 (defun (setf widget-default-direction) (direction)
295 (%widget-set-default-direction direction)
298 (defbinding widget-shape-combine-mask () nil
300 (shape-mask (or null gdk:bitmap))
304 (defun widget-path (widget)
305 (let ((subpath (list (if (and
306 (slot-boundp widget 'name)
307 (not (zerop (length (widget-name widget)))))
310 (if (slot-boundp widget 'parent)
311 (nconc (widget-path (widget-parent widget)) subpath)
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)
321 (defun widget-path-lookup (path &optional (root (nreverse (window-list-toplevels))) (error-p t))
322 (let ((component (first path)))
324 for widget in (mklist root)
327 (stringp component) (slot-boundp widget 'name)
328 (string= component (widget-name widget)))
330 (symbolp component) (typep widget component)))
332 ((endp (rest path)) (return widget))
333 ((typep widget 'container)
334 (let ((descendant (widget-path-lookup (rest path) (container-children widget) nil)))
336 (return descendant))))))))
338 (error "Widget not found: ~A" path)))
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
343 widget or a list of containers."
345 for widget in (mklist root)
347 ((and (slot-boundp widget 'name) (string= name (widget-name widget)))
348 (return-from widget-find widget))
349 ((typep widget 'container)
350 (let ((descendant (widget-find name (container-children widget) nil)))
352 (return-from widget-find descendant))))))
354 (error "Widget not found: ~A" name)))
357 (defbinding widget-modify-style () nil
361 (defbinding widget-get-modifier-style () rc-style
364 (defbinding widget-modify-fg () nil
369 (defbinding widget-modify-bg () nil
374 (defbinding widget-modify-text () nil
379 (defbinding widget-modify-base () nil
384 (defbinding widget-modify-font (widget font-desc) nil
386 ((etypecase font-desc
387 (pango:font-description font-desc)
388 (string (pango:font-description-from-string font-desc)))
389 pango:font-description))
391 (defbinding widget-create-pango-context () pango:context
394 (defbinding widget-get-pango-context () pango:context
397 (defbinding widget-create-pango-layout (widget &optional text) pango:layout
399 (text (or string null)))
401 (defbinding widget-render-icon (widget stock-id &optional size detail)
405 ((or size -1) (or icon-size int))
406 (detail (or null string)))
408 (defbinding widget-push-composite-child () nil)
410 (defbinding widget-pop-composite-child () nil)
412 (defbinding widget-queue-draw-area () nil
414 (x int) (y int) (width int) (height int))
416 (defbinding widget-reset-shapes () nil
419 (defbinding widget-set-double-buffered () nil
421 (double-buffered boolean))
423 ;; (defbinding widget-set-redraw-on-allocate () nil
425 ;; (redraw-on-allocate boolean))
427 (defbinding widget-set-scroll-adjustments () boolean
429 (hadjustment (or null adjustment))
430 (vadjustment (or null adjustment)))
432 (defbinding widget-mnemonic-activate () boolean
434 (group-cycling boolean))
436 (defbinding widget-class-find-style-property (class name) param
437 ((type-class-peek class) pointer)
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))
445 (defbinding widget-region-intersect () pointer ;gdk:region
447 (region pointer)) ;gdk:region))
449 (defbinding widget-send-expose () boolean
453 (defbinding %widget-style-get-property () nil
458 (defun style-property-value (widget style)
459 (let* ((name (string-downcase style))
460 (param (widget-class-find-style-property (class-of widget) name)))
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)))))
466 (defbinding widget-get-accessible () atk:object
469 (defbinding widget-child-focus () boolean
471 (direction direction-type))
473 (defbinding widget-child-notify () nil
475 (child-property string))
477 (defbinding widget-freeze-child-notify () nil
480 (defbinding widget-get-clipboard () clipboard
482 (selection int #|gdk:atom|#))
484 (defbinding widget-get-display () gdk:display
487 (defbinding widget-get-root-window () gdk:window
490 (defbinding widget-get-screen () gdk:screen
493 (defbinding widget-has-screen-p () boolean
496 (defbinding %widget-get-size-request () nil
501 (defun widget-get-size-request (widget)
502 (multiple-value-bind (width height) (%widget-get-size-request widget)
503 (values (unless (= width -1) width) (unless (= height -1) height))))
505 (defbinding widget-set-size-request (widget width height) nil
508 ((or height -1) int))
510 (defbinding widget-thaw-child-notify () nil
513 (defbinding widget-list-mnemonic-labels () (glist widget)
516 (defbinding widget-add-mnemonic-label () nil
520 (defbinding widget-remove-mnemonic-label () nil
525 ;;; Additional bindings and functions
527 (defbinding %widget-flags () int
530 (defun widget-flags (widget)
531 (let ((flags (%widget-flags widget)))
533 (int-to-object-flags flags)
534 (int-to-widget-flags flags))))
536 (defun widget-mapped-p (widget)
537 (find :mapped (widget-flags widget)))
539 (defun widget-realized-p (widget)
540 (find :realized (widget-flags widget)))
542 (defbinding widget-get-size-allocation () nil
547 (defbinding get-event-widget () widget
550 (defun (setf widget-cursor) (cursor-type widget)
551 (warn "(SETF WIDGET-CURSOR) is deprecated, use WIDGET-SET-CURSOR instead")
552 (widget-set-cursor widget cursor-type))
554 (defun widget-set-cursor (widget cursor &rest args)
555 (gdk:window-set-cursor (widget-window widget)
556 (apply #'gdk:ensure-cursor cursor args)))
558 (defbinding %widget-get-parent-window () gdk:window
561 (defun %widget-parent-window (widget)
562 (when (slot-boundp widget 'parent)
563 (%widget-get-parent-window widget)))