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: gdk.lisp,v 1.33 2007/01/14 23:24:11 espen Exp $
30 (defbinding (gdk-init "gdk_parse_args") () nil
31 "Initializes the library without opening the display."
39 (defbinding %display-open () display
40 (display-name (or null string)))
42 (defun display-open (&optional display-name)
43 (let ((display (%display-open display-name)))
44 (unless (display-get-default)
45 (display-set-default display))
48 (defbinding %display-get-n-screens () int
51 (defbinding %display-get-screen () screen
55 (defun display-screens (&optional (display (display-get-default)))
57 for i from 0 below (%display-get-n-screens display)
58 collect (%display-get-screen display i)))
60 (defbinding display-get-default-screen
61 (&optional (display (display-get-default))) screen
64 (defbinding display-beep (&optional (display (display-get-default))) nil
67 (defbinding display-sync (&optional (display (display-get-default))) nil
70 (defbinding display-flush (&optional (display (display-get-default))) nil
73 (defbinding display-close (&optional (display (display-get-default))) nil
76 (defbinding display-get-event
77 (&optional (display (display-get-default))) event
80 (defbinding display-peek-event
81 (&optional (display (display-get-default))) event
84 (defbinding display-put-event
85 (event &optional (display (display-get-default))) event
89 (defbinding (display-connection-number "clg_gdk_connection_number")
90 (&optional (display (display-get-default))) int
97 (defbinding display-get-default () display)
99 (defbinding (display-manager "gdk_display_manager_get") () display-manager)
101 (defbinding (display-set-default "gdk_display_manager_set_default_display")
103 ((display-manager) display-manager)
107 ;;; Primitive graphics structures (points, rectangles and regions)
109 (defbinding %rectangle-intersect () boolean
114 (defun rectangle-intersect (src1 src2 &optional (dest (make-instance 'rectangle)))
115 "Calculates the intersection of two rectangles. It is allowed for DEST to be the same as either SRC1 or SRC2. DEST is returned if the to rectangles intersect, otherwise NIL"
116 (when (%rectangle-intersect src1 src2 dest)
119 (defbinding rectangle-union (src1 src2 &optional (dest (make-instance 'rectangle))) nil
120 "Calculates the union of two rectangles. The union of rectangles SRC1 and SRC2 is the smallest rectangle which includes both SRC1 and SRC2 within it. It is allowed for DEST to be the same as either SRC1 or SRC2."
123 (dest rectangle :in/return))
125 (defun ensure-rectangle (rectangle)
127 (rectangle rectangle)
128 (vector (make-instance 'rectangle
129 :x (aref rectangle 0) :y (aref rectangle 1)
130 :width (aref rectangle 2) :height (aref rectangle 3)))))
133 (defbinding %region-new () pointer)
135 (defbinding %region-polygon () pointer
136 (points (vector (inlined point)))
138 (fill-rule fill-rule))
140 (defbinding %region-rectangle () pointer
141 (rectangle rectangle))
143 (defbinding %region-copy () pointer
146 (defbinding %region-destroy () nil
149 (defmethod allocate-foreign ((region region) &key rectangle polygon fill-rule)
150 (declare (ignore initargs))
152 ((and rectangle polygon)
153 (error "Only one of the keyword arguments :RECTANGLE and :POLYGON can be specified"))
154 (rectangle (%region-rectangle (ensure-rectangle rectangle)))
155 (polygon (%region-polygon polygon (length polygon) fill-rule))
158 (defun ensure-region (region)
161 ((or rectangle vector)
162 (make-instance 'region :rectangle (ensure-rectangle region)))))
164 (defbinding region-get-clipbox (region &optional (rectangle (make-instance 'rectangle))) nil
166 (rectangle rectangle :in/return))
168 (defbinding %region-get-rectangles () nil
170 (rectangles pointer :out)
171 (n-rectangles int :out))
173 (defun region-get-rectangles (region)
174 "Obtains the area covered by the region as a list of rectangles."
175 (multiple-value-bind (location length) (%region-get-rectangles region)
177 (map-c-vector 'list #'identity location 'point length :get)
178 (deallocate-memory location))))
180 (defbinding region-empty-p () boolean
183 (defbinding region-equal-p () boolean
187 (defbinding region-point-in-p () boolean
192 (defbinding region-rect-in (region rectangle) overlap-type
194 ((ensure-rectangle rectangle) rectangle))
196 (defbinding region-offset () nil
201 (defbinding region-shrink () nil
206 (defbinding region-intersect (source1 source2) nil
208 ((ensure-region source2) region))
210 (defbinding region-union (source1 source2) nil
212 ((ensure-region source2) region))
214 (defbinding region-subtract (source1 source2) nil
216 ((ensure-region source2) region))
218 (defbinding region-xor (source1 source2) nil
220 ((ensure-region source2) region))
225 (defbinding (events-pending-p "gdk_events_pending") () boolean)
227 (defbinding event-get () event)
229 (defbinding event-peek () event)
231 (defbinding event-get-graphics-expose () event
234 (defbinding event-put () event
237 ;(defbinding event-handler-set () ...)
239 (defbinding set-show-events () nil
240 (show-events boolean))
242 (defbinding get-show-events () boolean)
245 ;;; Miscellaneous functions
247 (defbinding screen-width () int
250 (defbinding screen-height () int
253 (defbinding screen-width-mm () int
256 (defbinding screen-height-mm () int
260 (defbinding pointer-grab
261 (window &key owner-events events confine-to cursor time) grab-status
263 (owner-events boolean)
265 (confine-to (or null window))
266 (cursor (or null cursor))
267 ((or time 0) (unsigned 32)))
269 (defbinding (pointer-ungrab "gdk_display_pointer_ungrab")
270 (&optional time (display (display-get-default))) nil
272 ((or time 0) (unsigned 32)))
274 (defbinding (pointer-is-grabbed-p "gdk_display_pointer_is_grabbed")
275 (&optional (display (display-get-default))) boolean
278 (defbinding keyboard-grab (window &key owner-events time) grab-status
280 (owner-events boolean)
281 ((or time 0) (unsigned 32)))
283 (defbinding (keyboard-ungrab "gdk_display_keyboard_ungrab")
284 (&optional time (display (display-get-default))) nil
286 ((or time 0) (unsigned 32)))
290 (defbinding atom-intern (atom-name &optional only-if-exists) atom
291 ((string atom-name) string)
292 (only-if-exists boolean))
294 (defbinding atom-name () string
301 (defbinding visual-get-best-depth () int)
303 (defbinding visual-get-best-type () visual-type)
305 (defbinding visual-get-system () visual)
308 (defbinding (%visual-get-best-with-nothing "gdk_visual_get_best") () visual)
310 (defbinding %visual-get-best-with-depth () visual
313 (defbinding %visual-get-best-with-type () visual
316 (defbinding %visual-get-best-with-both () visual
320 (defun visual-get-best (&key depth type)
322 ((and depth type) (%visual-get-best-with-both depth type))
323 (depth (%visual-get-best-with-depth depth))
324 (type (%visual-get-best-with-type type))
325 (t (%visual-get-best-with-nothing))))
327 ;(defbinding query-depths ..)
329 ;(defbinding query-visual-types ..)
331 (defbinding list-visuals () (glist visual))
336 (defbinding window-destroy () nil
339 (defbinding window-at-pointer () window
343 (defbinding window-show () nil
346 (defbinding window-show-unraised () nil
349 (defbinding window-hide () nil
352 (defbinding window-is-visible-p () boolean
355 (defbinding window-is-viewable-p () boolean
358 (defbinding window-withdraw () nil
361 (defbinding window-iconify () nil
364 (defbinding window-deiconify () nil
367 (defbinding window-stick () nil
370 (defbinding window-unstick () nil
373 (defbinding window-maximize () nil
376 (defbinding window-unmaximize () nil
379 (defbinding window-fullscreen () nil
382 (defbinding window-unfullscreen () nil
385 (defbinding window-set-keep-above () nil
389 (defbinding window-set-keep-below () nil
393 (defbinding window-move () nil
398 (defbinding window-resize () nil
403 (defbinding window-move-resize () nil
410 (defbinding window-scroll () nil
415 (defbinding window-reparent () nil
421 (defbinding window-clear () nil
424 (defbinding %window-clear-area () nil
426 (x int) (y int) (width int) (height int))
428 (defbinding %window-clear-area-e () nil
430 (x int) (y int) (width int) (height int))
432 (defun window-clear-area (window x y width height &optional expose)
434 (%window-clear-area-e window x y width height)
435 (%window-clear-area window x y width height)))
437 (defbinding window-raise () nil
440 (defbinding window-lower () nil
443 (defbinding window-focus () nil
445 (timestamp unsigned-int))
447 (defbinding window-register-dnd () nil
450 (defbinding window-begin-resize-drag () nil
456 (timestamp unsigned-int))
458 (defbinding window-begin-move-drag () nil
463 (timestamp unsigned-int))
465 ;; Probably not needed
466 ;; (defbinding window-constrain-size () nil ..
468 (defbinding window-begin-paint-region (window region) nil
470 ((ensure-region region) region))
472 (defbinding window-end-paint () nil
475 (defmacro with-window-paint ((window region) &body body)
477 (window-begin-paint-region ,window ,region)
480 (window-end-paint ,window))))
482 ;; TODO: create wrapper function and use gdk_window_invalidate_maybe_recurse
483 ;; if last arg is a function
484 (defbinding window-invalidate-region (window region invalidate-children-p) nil
486 ((ensure-region region) region)
487 (invalidate-children-p boolean))
489 (defbinding window-get-update-area () region
492 (defbinding window-freeze-updates () nil
495 (defbinding window-thaw-updates () nil
498 (defbinding window-process-all-updates () nil)
500 (defbinding window-process-updates () nil
502 (update-children-p boolean))
504 (defbinding window-set-debug-updates () nil
507 (defbinding window-enable-synchronized-configure () nil
510 (defbinding window-configure-finished () nil
513 ;; Deprecated, use gobject user data mechanism
514 (defbinding window-set-user-data () nil
518 (defbinding window-set-override-redirect () nil
520 (override-redirect-p boolean))
522 (defbinding window-set-accept-focus () nil
524 (accept-focus-p boolean))
526 (defbinding window-set-focus-on-map () nil
528 (focus-on-map-p boolean))
531 ; (defbinding window-add-filter () nil
532 ; (defbinding window-remove-filter () nil
534 ;; New code should use window-shape-combine
535 (defbinding window-shape-combine-mask () nil
541 (defbinding %window-shape-combine-region () nil
543 (region (or null region))
547 (defun window-shape-combine (window shape offset-x offset-y)
549 (nil (%window-shape-combine-region window nil 0 0)
550 (region (%window-shape-combine-region window shape offset-x offset-y))
551 (bitmask (window-shape-combine-mask window shape offset-x offset-y)))))
553 (defbinding window-set-child-shapes () nil
556 (defbinding window-merge-child-shapes () nil
559 #?(pkg-exists-p "gtk+-2.0" :atleast-version "2.10.0")
561 (defbinding %window-input-shape-combine-mask () nil
567 (defbinding %window-input-shape-combine-region () nil
569 (region (or null region))
573 (defun window-input-shape-combine (window shape x y)
575 (nil (%window-input-shape-combine-region window nil 0 0)
576 (region (%window-input-shape-combine-region window shape x y))
577 (bitmask (%window-input-shape-combine-mask window shape x y)))))
579 (defbinding window-set-child-input-shapes () nil
582 (defbinding window-merge-child-input-shapes () nil
585 (defbinding window-set-static-gravities () boolean
587 (use-static-p boolean))
589 (defbinding window-set-title () nil
593 (defbinding window-set-background () nil
597 (defbinding window-set-back-pixmap (window pixmap &optional parent-relative-p) nil
599 (pixmap (or null pixmap))
600 (parent-relative-p boolean))
602 (defbinding window-set-cursor () nil
604 (cursor (or null cursor)))
606 (defbinding window-get-geometry () nil
614 ;(defbinding window-set-geometry-hints () nil
616 (defbinding window-set-icon-list () nil
618 (icons (glist pixbufs)))
620 (defbinding window-set-skip-taskbar-hint () nil
622 (skip-taskbar-p boolean))
624 (defbinding window-set-skip-pager-hint () nil
626 (skip-pager-p boolean))
628 #?(pkg-exists-p "gtk+-2.0" :atleast-version "2.8.0")
629 (defbinding window-set-urgency-hint () nil
633 (defbinding window-get-position () nil
638 (defbinding window-get-root-origin () nil
643 (defbinding window-get-frame-extents (window &optional (extents (make-instance 'rect))) nil
645 (extents rectangle :in/return))
647 (defbinding window-get-origin () nil ; this may not work as
648 (window window) ; an int is actually returned
652 (defbinding window-get-pointer () window
656 (mask modifier-type :out))
658 ;(defbinding window-set-icon () nil
660 (defbinding window-set-icon-name () nil
664 (defbinding window-set-transient-for () nil
668 (defbinding window-set-role () nil
672 (defbinding %window-get-decorations () boolean
674 (decorations wm-decoration :out))
676 (defun %window-decorations-getter (window)
677 (nth-value 1 (%window-get-decorations window)))
679 (defun %window-decorations-boundp (window)
680 (%window-get-decorations window))
682 (defbinding %window-get-toplevels () (glist window))
684 (defun window-get-toplevels (&optional screen)
686 (error "Not implemented")
687 (%window-get-toplevels)))
689 (defbinding %get-default-root-window () window)
691 (defun get-root-window (&optional display)
693 (error "Not implemented")
694 (%get-default-root-window)))
702 (defbinding drag-status () nil
703 (context drag-context)
705 (time (unsigned 32)))
714 (defbinding rgb-init () nil)
721 (defmethod allocate-foreign ((cursor cursor) &key source mask fg bg
722 (x 0) (y 0) (display (display-get-default)))
724 (keyword (%cursor-new-for-display display source))
725 (pixbuf (%cursor-new-from-pixbuf display source x y))
726 (pixmap (%cursor-new-from-pixmap source mask
727 (or fg (ensure-color #(0.0 0.0 0.0)))
728 (or bg (ensure-color #(1.0 1.0 1.0))) x y))
729 (pathname (%cursor-new-from-pixbuf display (pixbuf-load source) x y))))
731 (defun ensure-cursor (cursor &rest args)
732 (if (typep cursor 'cursor)
734 (apply #'make-instance 'cursor :source cursor args)))
736 (defbinding %cursor-new-for-display () pointer
738 (cursor-type cursor-type))
740 (defbinding %cursor-new-from-pixmap () pointer
747 (defbinding %cursor-new-from-pixbuf () pointer
752 (defbinding %cursor-ref () pointer
755 (defbinding %cursor-unref () nil
761 (defbinding %pixmap-new () pointer
762 (window (or null window))
767 (defmethod allocate-foreign ((pximap pixmap) &key width height depth window)
768 (%pixmap-new window width height depth))
770 (defun pixmap-new (width height depth &key window)
771 (warn "PIXMAP-NEW is deprecated, use (make-instance 'pixmap ...) instead")
772 (make-instance 'pixmap :width width :height height :depth depth :window window))
774 (defbinding %pixmap-colormap-create-from-xpm () pixmap
775 (window (or null window))
776 (colormap (or null colormap))
778 (color (or null color))
781 (defbinding %pixmap-colormap-create-from-xpm-d () pixmap
782 (window (or null window))
783 (colormap (or null colormap))
785 (color (or null color))
786 (data (vector string)))
788 ;; Deprecated, use pixbufs instead
789 (defun pixmap-create (source &key color window colormap)
791 (if (not (or window colormap))
794 (multiple-value-bind (pixmap mask)
796 ((or string pathname)
797 (%pixmap-colormap-create-from-xpm window colormap color source))
799 (%pixmap-colormap-create-from-xpm-d window colormap color source)))
800 (values pixmap mask))))
805 (defbinding colormap-get-system () colormap)
807 (defbinding %color-copy () pointer
810 (defmethod allocate-foreign ((color color) &rest initargs)
811 (declare (ignore color initargs))
812 ;; Color structs are allocated as memory chunks by gdk, and since
813 ;; there is no gdk_color_new we have to use this hack to get a new
815 (with-memory (location #.(foreign-size (find-class 'color)))
816 (%color-copy location)))
818 (defun %scale-value (value)
821 (float (truncate (* value 65535)))))
823 (defmethod initialize-instance ((color color) &key (red 0.0) (green 0.0) (blue 0.0))
825 (with-slots ((%red red) (%green green) (%blue blue)) color
827 %red (%scale-value red)
828 %green (%scale-value green)
829 %blue (%scale-value blue))))
831 (defbinding %color-parse () boolean
833 (color color :in/return))
835 (defun color-parse (spec &optional (color (make-instance 'color)))
836 (multiple-value-bind (succeeded-p color) (%color-parse spec color)
839 (error "Parsing color specification ~S failed." spec))))
841 (defun ensure-color (color)
845 (string (color-parse color))
847 (make-instance 'color
848 :red (svref color 0) :green (svref color 1) :blue (svref color 2)))))
852 ;;; Drawable -- all the draw- functions are deprecated and will be
853 ;;; removed, use cairo for drawing instead.
855 (defbinding drawable-get-size () nil
860 (defbinding (drawable-width "gdk_drawable_get_size") () nil
865 (defbinding (drawable-height "gdk_drawable_get_size") () nil
870 ;; (defbinding drawable-get-clip-region () region
871 ;; (drawable drawable))
873 ;; (defbinding drawable-get-visible-region () region
874 ;; (drawable drawable))
876 (defbinding draw-point () nil
877 (drawable drawable) (gc gc)
880 (defbinding %draw-points () nil
881 (drawable drawable) (gc gc)
885 (defbinding draw-line () nil
886 (drawable drawable) (gc gc)
890 (defbinding draw-pixbuf
891 (drawable gc pixbuf src-x src-y dest-x dest-y &optional
892 width height (dither :none) (x-dither 0) (y-dither 0)) nil
893 (drawable drawable) (gc (or null gc))
895 (src-x int) (src-y int)
896 (dest-x int) (dest-y int)
897 ((or width -1) int) ((or height -1) int)
899 (x-dither int) (y-dither int))
901 (defbinding draw-rectangle () nil
902 (drawable drawable) (gc gc)
905 (width int) (height int))
907 (defbinding draw-arc () nil
908 (drawable drawable) (gc gc)
911 (width int) (height int)
912 (angle1 int) (angle2 int))
914 (defbinding %draw-layout () nil
915 (drawable drawable) (gc gc)
918 (layout pango:layout))
920 (defbinding %draw-layout-with-colors () nil
921 (drawable drawable) (gc gc)
924 (layout pango:layout)
925 (foreground (or null color))
926 (background (or null color)))
928 (defun draw-layout (drawable gc font x y layout &optional foreground background)
929 (if (or foreground background)
930 (%draw-layout-with-colors drawable gc font x y layout foreground background)
931 (%draw-layout drawable gc font x y layout)))
933 (defbinding draw-drawable
934 (drawable gc src src-x src-y dest-x dest-y &optional width height) nil
935 (drawable drawable) (gc gc)
937 (src-x int) (src-y int)
938 (dest-x int) (dest-y int)
939 ((or width -1) int) ((or height -1) int))
941 (defbinding draw-image
942 (drawable gc image src-x src-y dest-x dest-y &optional width height) nil
943 (drawable drawable) (gc gc)
945 (src-x int) (src-y int)
946 (dest-x int) (dest-y int)
947 ((or width -1) int) ((or height -1) int))
949 (defbinding drawable-get-image () image
952 (width int) (height int))
954 (defbinding drawable-copy-to-image
955 (drawable src-x src-y width height &optional image dest-x dest-y) image
957 (image (or null image))
958 (src-x int) (src-y int)
959 ((if image dest-x 0) int)
960 ((if image dest-y 0) int)
961 (width int) (height int))
966 (defbinding keyval-name () string
967 (keyval unsigned-int))
969 (defbinding %keyval-from-name () unsigned-int
972 (defun keyval-from-name (name)
973 "Returns the keysym value for the given key name or NIL if it is not a valid name."
974 (let ((keyval (%keyval-from-name name)))
975 (unless (zerop keyval)
978 (defbinding keyval-to-upper () unsigned-int
979 (keyval unsigned-int))
981 (defbinding keyval-to-lower () unsigned-int
982 (keyval unsigned-int))
984 (defbinding (keyval-is-upper-p "gdk_keyval_is_upper") () boolean
985 (keyval unsigned-int))
987 (defbinding (keyval-is-lower-p "gdk_keyval_is_lower") () boolean
988 (keyval unsigned-int))
990 ;;; Cairo interaction
992 #?(pkg-exists-p "gtk+-2.0" :atleast-version "2.8.0")
994 (defbinding cairo-create () cairo:context
997 (defmacro with-cairo-context ((cr drawable) &body body)
998 `(let ((,cr (cairo-create ,drawable)))
1001 (invalidate-instance ,cr t))))
1003 (defbinding cairo-set-source-color () nil
1007 (defbinding cairo-set-source-pixbuf () nil
1013 (defbinding cairo-rectangle () nil
1015 (rectangle rectangle))
1017 ;; (defbinding cairo-region () nil
1018 ;; (cr cairo:context)
1024 ;;; Multi-threading support
1028 (defvar *global-lock* (sb-thread:make-mutex :name "global GDK lock"))
1029 (let ((recursive-level 0))
1030 (defun threads-enter ()
1031 (if (eq (sb-thread:mutex-value *global-lock*) sb-thread:*current-thread*)
1032 (incf recursive-level)
1033 (sb-thread:get-mutex *global-lock*)))
1035 (defun threads-leave (&optional flush-p)
1037 ((zerop recursive-level)
1040 (sb-thread:release-mutex *global-lock*))
1041 (t (decf recursive-level)))))
1043 (define-callback %enter-fn nil ()
1046 (define-callback %leave-fn nil ()
1049 (defbinding threads-set-lock-functions (&optional) nil
1050 (%enter-fn callback)
1051 (%leave-fn callback))
1053 (defmacro with-global-lock (&body body)
1058 (threads-leave t)))))