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.26 2007/01/07 20:23:22 espen Exp $
29 (defmethod print-object ((widget widget) stream)
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"
35 (widget-name widget) (pointer-address (foreign-location widget))))
38 (defmethod shared-initialize ((widget widget) names &key (visible nil visible-p))
39 (declare (ignore names))
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
44 (unset-user-data widget 'hidden-p))
48 (defmethod shared-initialize :after ((widget widget) names &key parent visible)
49 (declare (ignore names))
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))))
58 (defmethod slot-unbound ((class gobject-class) (object widget)
59 (slot (eql 'child-properties)))
61 ((slot-boundp object 'parent)
62 (with-slots (parent child-properties) object
63 (setf child-properties
65 (gethash (class-of parent) *container-to-child-class-mappings*)
66 :parent parent :child object))))
67 ((call-next-method))))
70 (defmethod compute-signal-function ((widget widget) signal function object args)
71 (declare (ignore signal))
72 (if (eq object :parent)
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)
77 ;; Delay until parent is set
78 (signal-connect widget 'parent-set
79 #'(lambda (old-parent)
80 (declare (ignore old-parent))
81 (let ((*signal-stop-emission*
83 (warn "Ignoring emission stop in delayed signal handler"))))
84 (apply function (widget-parent widget) all-args)))
86 ; (warn "Widget has no parent -- ignoring signal")
90 (defun child-property-value (widget slot)
91 (slot-value (widget-child-properties widget) slot))
93 (defun (setf child-property-value) (value widget slot)
94 (setf (slot-value (widget-child-properties widget) slot) value))
96 (defmacro with-child-properties (slots widget &body body)
97 `(with-slots ,slots (widget-child-properties ,widget)
103 (defbinding widget-destroy () nil
106 (defbinding widget-unparent () nil
109 (defbinding widget-show () nil
112 (defbinding widget-show-now () nil
115 (defbinding widget-hide () nil
118 (defun widget-hidden-p (widget)
119 "Return T if WIDGET has been explicit hidden during construction."
120 (user-data widget 'hidden-p))
122 (defbinding widget-show-all () nil
125 (defbinding widget-hide-all () nil
128 (defbinding widget-map () nil
131 (defbinding widget-unmap () nil
134 (defbinding widget-realize () nil
137 (defbinding widget-unrealize () nil
140 (defbinding widget-queue-draw () nil
143 (defbinding widget-queue-resize () nil
146 (defbinding widget-queue-resize-no-redraw () nil
149 (defbinding widget-size-request
150 (widget &optional (requisition (make-instance 'requisition))) nil
152 (requisition requisition :in/return))
154 (defbinding widget-get-child-requisition
155 (widget &optional (requisition (make-instance 'requisition))) nil
157 (requisition requisition :in/return))
159 (defbinding widget-size-allocate () nil
161 (allocation allocation))
163 (defbinding widget-add-accelerator
164 (widget signal accel-group key modifiers flags) nil
166 ((signal-name-to-string signal) string)
167 (accel-group accel-group)
168 ((gdk:keyval-from-name key) unsigned-int)
169 (modifiers gdk:modifier-type)
172 (defbinding widget-remove-accelerator
173 (widget accel-group key modifiers) nil
175 (accel-group accel-group)
176 ((gdk:keyval-from-name key) unsigned-int)
177 (modifiers gdk:modifier-type))
179 (defbinding widget-set-accel-path () nil
182 (accel-group accel-group))
184 (defbinding widget-list-accel-closures () (glist pointer)
187 (defbinding widget-can-activate-accel-p () boolean
189 (signal-id unsigned-int))
191 (defbinding widget-event () boolean
195 (defbinding widget-activate () boolean
198 (defbinding widget-reparent () nil
202 (defbinding %widget-intersect () boolean
205 (intersection (or null gdk:rectangle)))
207 (defun widget-intersection (widget area)
208 (let ((intersection (make-instance 'gdk:rectangle)))
209 (when (%widget-intersect widget area intersection)
212 (defun widget-intersect-p (widget area)
213 (%widget-intersect widget area nil))
215 (defbinding widget-grab-focus () nil
218 (defbinding widget-grab-default () nil
221 (defbinding widget-add-events () nil
223 (events gdk:event-mask))
225 (defbinding widget-get-toplevel () widget
228 (defbinding widget-get-ancestor (widget type) widget
230 ((find-type-number type) type-number))
232 (defbinding widget-get-pointer () nil
237 (defbinding widget-is-ancestor-p () boolean
241 (defbinding widget-translate-coordinates () boolean
244 (src-x int) (src-y int)
245 (set-x int :out) (dest-y int :out))
247 (defun widget-hide-on-delete (widget)
248 "Utility function; intended to be connected to the DELETE-EVENT
249 signal on a WINDOW. The function calls WIDGET-HIDE on its
250 argument, then returns T. If connected to DELETE-EVENT, the
251 result is that clicking the close button for a window (on the window
252 frame, top right corner usually) will hide but not destroy the
253 window. By default, GTK+ destroys windows when DELETE-EVENT is
258 (defbinding widget-ensure-style () nil
261 (defbinding widget-reset-rc-styles () nil
264 (defbinding widget-push-colormap () nil
265 (colormap gdk:colormap))
267 (defbinding widget-pop-colormap () nil)
269 (defbinding %widget-set-default-colormap () nil
270 (colormap gdk:colormap))
272 (defun (setf widget-default-colormap) (colormap)
273 (%widget-set-default-colormap colormap)
276 (defbinding (widget-default-style "gtk_widget_get_default_style") () style)
278 (defbinding (widget-default-colromap "gtk_widget_get_default_colormap")
281 (defbinding (widget-default-visual "gtk_widget_get_default_visual")
284 (defbinding (widget-default-direction "gtk_widget_get_default_direction")
287 (defbinding %widget-set-default-direction () nil
288 (direction text-direction))
290 (defun (setf widget-default-direction) (direction)
291 (%widget-set-default-direction direction)
294 (defbinding widget-shape-combine-mask () nil
296 (shape-mask (or null gdk:bitmap))
300 (defun widget-path (widget)
301 (let ((subpath (list (if (and
302 (slot-boundp widget 'name)
303 (not (zerop (length (widget-name widget)))))
306 (if (slot-boundp widget 'parent)
307 (nconc (widget-path (widget-parent widget)) subpath)
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)
317 (defun widget-path-lookup (path &optional (root (nreverse (window-list-toplevels))) (error-p t))
318 (let ((component (first path)))
320 for widget in (mklist root)
323 (stringp component) (slot-boundp widget 'name)
324 (string= component (widget-name widget)))
326 (symbolp component) (typep widget component)))
328 ((endp (rest path)) (return widget))
329 ((typep widget 'container)
330 (let ((descendant (widget-path-lookup (rest path) (container-children widget) nil)))
332 (return descendant))))))))
334 (error "Widget not found: ~A" path)))
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
339 widget or a list of containers."
341 for widget in (mklist root)
343 ((and (slot-boundp widget 'name) (string= name (widget-name widget)))
344 (return-from widget-find widget))
345 ((typep widget 'container)
346 (let ((descendant (widget-find name (container-children widget) nil)))
348 (return-from widget-find descendant))))))
350 (error "Widget not found: ~A" name)))
353 (defbinding widget-modify-style () nil
357 (defbinding widget-get-modifier-style () rc-style
360 (defbinding widget-modify-fg () nil
365 (defbinding widget-modify-bg () nil
370 (defbinding widget-modify-text () nil
375 (defbinding widget-modify-base () nil
380 (defbinding widget-modify-font (widget font-desc) nil
382 ((etypecase font-desc
383 (pango:font-description font-desc)
384 (string (pango:font-description-from-string font-desc)))
385 pango:font-description))
387 (defbinding widget-create-pango-context () pango:context
390 (defbinding widget-get-pango-context () pango:context
393 (defbinding widget-create-pango-layout (widget &optional text) pango:layout
395 (text (or string null)))
397 (defbinding widget-render-icon (widget stock-id &optional size detail)
401 ((or size -1) (or icon-size int))
402 (detail (or null string)))
404 (defbinding widget-push-composite-child () nil)
406 (defbinding widget-pop-composite-child () nil)
408 (defbinding widget-queue-draw-area () nil
410 (x int) (y int) (width int) (height int))
412 (defbinding widget-reset-shapes () nil
415 ;; (defbinding widget-set-double-buffered () nil
417 ;; (double-buffered boolean))
419 ;; (defbinding widget-set-redraw-on-allocate () nil
421 ;; (redraw-on-allocate boolean))
423 (defbinding widget-set-scroll-adjustments () boolean
425 (hadjustment (or null adjustment))
426 (vadjustment (or null adjustment)))
428 (defbinding widget-mnemonic-activate () boolean
430 (group-cycling boolean))
432 (defbinding widget-class-find-style-property (class name) param
433 ((type-class-peek class) pointer)
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))
441 (defbinding widget-region-intersect () pointer ;gdk:region
443 (region pointer)) ;gdk:region))
445 (defbinding widget-send-expose () boolean
449 (defbinding %widget-style-get-property () nil
454 (defun style-property-value (widget style)
455 (let* ((name (string-downcase style))
456 (param (widget-class-find-style-property (class-of widget) name)))
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)))))
462 (defbinding widget-get-accessible () atk:object
465 (defbinding widget-child-focus () boolean
467 (direction direction-type))
469 (defbinding widget-child-notify () nil
471 (child-property string))
473 (defbinding widget-freeze-child-notify () nil
476 (defbinding widget-get-clipboard () clipboard
478 (selection int #|gdk:atom|#))
480 (defbinding widget-get-display () gdk:display
483 (defbinding widget-get-root-window () gdk:window
486 (defbinding widget-get-screen () gdk:screen
489 (defbinding widget-has-screen-p () boolean
492 (defbinding %widget-get-size-request () nil
497 (defun widget-get-size-request (widget)
498 (multiple-value-bind (width height) (%widget-get-size-request widget)
499 (values (unless (= width -1) width) (unless (= height -1) height))))
501 (defbinding widget-set-size-request (widget width height) nil
504 ((or height -1) int))
506 (defbinding widget-thaw-child-notify () nil
509 (defbinding widget-list-mnemonic-labels () (glist widget)
512 (defbinding widget-add-mnemonic-label () nil
516 (defbinding widget-remove-mnemonic-label () nil
521 ;;; Additional bindings and functions
523 (defbinding (widget-mapped-p "gtk_widget_mapped_p") () boolean
526 (defbinding widget-get-size-allocation () nil
531 (defbinding get-event-widget () widget
534 (defun (setf widget-cursor) (cursor-type widget)
535 (warn "(SETF WIDGET-CURSOR) is deprecated, use WIDGET-SET-CURSOR instead")
536 (widget-set-cursor widget cursor-type))
538 (defun widget-set-cursor (widget cursor &rest args)
539 (gdk:window-set-cursor (widget-window widget)
540 (apply #'gdk:ensure-cursor cursor args)))
542 (defbinding %widget-get-parent-window () gdk:window
545 (defun %widget-parent-window (widget)
546 (when (slot-boundp widget 'parent)
547 (%widget-get-parent-window widget)))