1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2005 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.27 2006-04-25 20:19:32 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)
110 (defbinding (events-pending-p "gdk_events_pending") () boolean)
112 (defbinding event-get () event)
114 (defbinding event-peek () event)
116 (defbinding event-get-graphics-expose () event
119 (defbinding event-put () event
122 ;(defbinding event-handler-set () ...)
124 (defbinding set-show-events () nil
125 (show-events boolean))
127 (defbinding get-show-events () boolean)
130 ;;; Miscellaneous functions
132 (defbinding screen-width () int)
133 (defbinding screen-height () int)
135 (defbinding screen-width-mm () int)
136 (defbinding screen-height-mm () int)
138 (defbinding pointer-grab
139 (window &key owner-events events confine-to cursor time) grab-status
141 (owner-events boolean)
143 (confine-to (or null window))
144 (cursor (or null cursor))
145 ((or time 0) (unsigned 32)))
147 (defbinding (pointer-ungrab "gdk_display_pointer_ungrab")
148 (&optional time (display (display-get-default))) nil
150 ((or time 0) (unsigned 32)))
152 (defbinding (pointer-is-grabbed-p "gdk_display_pointer_is_grabbed")
153 (&optional (display (display-get-default))) boolean)
155 (defbinding keyboard-grab (window &key owner-events time) grab-status
157 (owner-events boolean)
158 ((or time 0) (unsigned 32)))
160 (defbinding (keyboard-ungrab "gdk_display_keyboard_ungrab")
161 (&optional time (display (display-get-default))) nil
163 ((or time 0) (unsigned 32)))
167 (defbinding atom-intern (atom-name &optional only-if-exists) atom
168 ((string atom-name) string)
169 (only-if-exists boolean))
171 (defbinding atom-name () string
178 (defbinding visual-get-best-depth () int)
180 (defbinding visual-get-best-type () visual-type)
182 (defbinding visual-get-system () visual)
185 (defbinding (%visual-get-best-with-nothing "gdk_visual_get_best") () visual)
187 (defbinding %visual-get-best-with-depth () visual
190 (defbinding %visual-get-best-with-type () visual
193 (defbinding %visual-get-best-with-both () visual
197 (defun visual-get-best (&key depth type)
199 ((and depth type) (%visual-get-best-with-both depth type))
200 (depth (%visual-get-best-with-depth depth))
201 (type (%visual-get-best-with-type type))
202 (t (%visual-get-best-with-nothing))))
204 ;(defbinding query-depths ..)
206 ;(defbinding query-visual-types ..)
208 (defbinding list-visuals () (glist visual))
213 (defbinding window-destroy () nil
217 (defbinding window-at-pointer () window
221 (defbinding window-show () nil
224 (defbinding window-show-unraised () nil
227 (defbinding window-hide () nil
230 (defbinding window-is-visible-p () boolean
233 (defbinding window-is-viewable-p () boolean
236 (defbinding window-withdraw () nil
239 (defbinding window-iconify () nil
242 (defbinding window-deiconify () nil
245 (defbinding window-stick () nil
248 (defbinding window-unstick () nil
251 (defbinding window-maximize () nil
254 (defbinding window-unmaximize () nil
257 (defbinding window-fullscreen () nil
260 (defbinding window-unfullscreen () nil
263 (defbinding window-set-keep-above () nil
267 (defbinding window-set-keep-below () nil
271 (defbinding window-move () nil
276 (defbinding window-resize () nil
281 (defbinding window-move-resize () nil
288 (defbinding window-scroll () nil
293 (defbinding window-reparent () nil
299 (defbinding window-clear () nil
302 (defbinding %window-clear-area () nil
304 (x int) (y int) (width int) (height int))
306 (defbinding %window-clear-area-e () nil
308 (x int) (y int) (width int) (height int))
310 (defun window-clear-area (window x y width height &optional expose)
312 (%window-clear-area-e window x y width height)
313 (%window-clear-area window x y width height)))
315 (defbinding window-raise () nil
318 (defbinding window-lower () nil
321 (defbinding window-focus () nil
323 (timestamp unsigned-int))
325 (defbinding window-register-dnd () nil
328 (defbinding window-begin-resize-drag () nil
334 (timestamp unsigned-int))
336 (defbinding window-begin-move-drag () nil
341 (timestamp unsigned-int))
343 ;; komplett så langt
345 (defbinding window-set-user-data () nil
349 (defbinding window-set-override-redirect () nil
351 (override-redirect boolean))
353 ; (defbinding window-add-filter () nil
355 ; (defbinding window-remove-filter () nil
357 (defbinding window-shape-combine-mask () nil
363 (defbinding window-set-child-shapes () nil
366 (defbinding window-merge-child-shapes () nil
370 (defbinding window-set-static-gravities () boolean
372 (use-static boolean))
374 ; (defbinding add-client-message-filter ...
376 (defbinding window-set-cursor () nil
378 (cursor (or null cursor)))
380 (defbinding window-get-pointer () window
384 (mask modifier-type :out))
386 (defbinding %window-get-toplevels () (glist window))
388 (defun window-get-toplevels (&optional screen)
390 (error "Not implemented")
391 (%window-get-toplevels)))
393 (defbinding %get-default-root-window () window)
395 (defun get-root-window (&optional display)
397 (error "Not implemented")
398 (%get-default-root-window)))
406 (defbinding drag-status () nil
407 (context drag-context)
409 (time (unsigned 32)))
418 (defbinding rgb-init () nil)
425 (defmethod allocate-foreign ((cursor cursor) &key source mask fg bg
426 (x 0) (y 0) (display (display-get-default)))
428 (keyword (%cursor-new-for-display display source))
429 (pixbuf (%cursor-new-from-pixbuf display source x y))
430 (pixmap (%cursor-new-from-pixmap source mask
431 (or fg (ensure-color #(0.0 0.0 0.0)))
432 (or bg (ensure-color #(1.0 1.0 1.0))) x y))
433 (pathname (%cursor-new-from-pixbuf display (pixbuf-load source) x y))))
435 (defun ensure-cursor (cursor &rest args)
436 (if (typep cursor 'cursor)
438 (apply #'make-instance 'cursor :type cursor args)))
440 (defbinding %cursor-new-for-display () pointer
442 (cursor-type cursor-type))
444 (defbinding %cursor-new-from-pixmap () pointer
451 (defbinding %cursor-new-from-pixbuf () pointer
456 (defbinding %cursor-ref () pointer
459 (defbinding %cursor-unref () nil
462 (defmethod reference-foreign ((class (eql (find-class 'cursor))) location)
463 (declare (ignore class))
464 (%cursor-ref location))
466 (defmethod unreference-foreign ((class (eql (find-class 'cursor))) location)
467 (declare (ignore class))
468 (%cursor-unref location))
473 (defbinding pixmap-new (width height depth &key window) pixmap
477 (window (or null window)))
479 (defbinding %pixmap-colormap-create-from-xpm () pixmap
480 (window (or null window))
481 (colormap (or null colormap))
483 (color (or null color))
486 (defbinding %pixmap-colormap-create-from-xpm-d () pixmap
487 (window (or null window))
488 (colormap (or null colormap))
490 (color (or null color))
491 (data (vector string)))
493 (defun pixmap-create (source &key color window colormap)
495 (if (not (or window colormap))
498 (multiple-value-bind (pixmap mask)
500 ((or string pathname)
501 (%pixmap-colormap-create-from-xpm
502 window colormap color (namestring (truename source))))
504 (%pixmap-colormap-create-from-xpm-d window colormap color source)))
505 ;; (unreference-instance pixmap)
506 ;; (unreference-instance mask)
507 (values pixmap mask))))
513 (defbinding %color-copy () pointer
516 (defmethod allocate-foreign ((color color) &rest initargs)
517 (declare (ignore color initargs))
518 ;; Color structs are allocated as memory chunks by gdk, and since
519 ;; there is no gdk_color_new we have to use this hack to get a new
521 (with-allocated-memory (location #.(foreign-size (find-class 'color)))
522 (%color-copy location)))
524 (defun %scale-value (value)
527 (float (truncate (* value 65535)))))
529 (defmethod initialize-instance ((color color) &rest initargs
530 &key (red 0.0) (green 0.0) (blue 0.0))
531 (declare (ignore initargs))
533 (with-slots ((%red red) (%green green) (%blue blue)) color
535 %red (%scale-value red)
536 %green (%scale-value green)
537 %blue (%scale-value blue))))
539 (defbinding %color-parse () boolean
541 (color color :return))
543 (defun color-parse (spec &optional (color (make-instance 'color)))
544 (multiple-value-bind (succeeded-p color) (%color-parse spec color)
547 (error "Parsing color specification ~S failed." spec))))
549 (defun ensure-color (color)
553 (string (color-parse color))
555 (make-instance 'color
556 :red (svref color 0) :green (svref color 1) :blue (svref color 2)))))
562 (defbinding drawable-get-size () nil
567 (defbinding (drawable-width "gdk_drawable_get_size") () nil
572 (defbinding (drawable-height "gdk_drawable_get_size") () nil
577 ;; (defbinding drawable-get-clip-region () region
578 ;; (drawable drawable))
580 ;; (defbinding drawable-get-visible-region () region
581 ;; (drawable drawable))
583 (defbinding draw-point () nil
584 (drawable drawable) (gc gc)
587 (defbinding %draw-points () nil
588 (drawable drawable) (gc gc)
592 ;; (defun draw-points (drawable gc &rest points)
596 (defbinding draw-line () nil
597 (drawable drawable) (gc gc)
601 ;; (defbinding draw-lines (drawable gc &rest points) nil
602 ;; (drawable drawable) (gc gc)
603 ;; (points (vector point))
604 ;; ((length points) int))
606 (defbinding draw-pixbuf
607 (drawable gc pixbuf src-x src-y dest-x dest-y &optional
608 width height (dither :none) (x-dither 0) (y-dither 0)) nil
609 (drawable drawable) (gc (or null gc))
611 (src-x int) (src-y int)
612 (dest-x int) (dest-y int)
613 ((or width -1) int) ((or height -1) int)
615 (x-dither int) (y-dither int))
617 ;; (defbinding draw-segments (drawable gc &rest points) nil
618 ;; (drawable drawable) (gc gc)
619 ;; (segments (vector segments))
620 ;; ((length segments) int))
622 (defbinding draw-rectangle () nil
623 (drawable drawable) (gc gc)
626 (width int) (height int))
628 (defbinding draw-arc () nil
629 (drawable drawable) (gc gc)
632 (width int) (height int)
633 (angle1 int) (angle2 int))
635 ;; (defbinding draw-polygon (drawable gc &rest points) nil
636 ;; (drawable drawable) (gc gc)
637 ;; (points (vector point))
638 ;; ((length points) int))
640 ;; (defbinding draw-trapezoid (drawable gc &rest points) nil
641 ;; (drawable drawable) (gc gc)
642 ;; (points (vector point))
643 ;; ((length points) int))
645 ;; (defbinding %draw-layout-line () nil
646 ;; (drawable drawable) (gc gc)
649 ;; (line pango:layout-line))
651 ;; (defbinding %draw-layout-line-with-colors () nil
652 ;; (drawable drawable) (gc gc)
655 ;; (line pango:layout-line)
656 ;; (foreground (or null color))
657 ;; (background (or null color)))
659 ;; (defun draw-layout-line (drawable gc font x y line &optional foreground background)
660 ;; (if (or foreground background)
661 ;; (%draw-layout-line-with-colors drawable gc font x y line foreground background)
662 ;; (%draw-layout-line drawable gc font x y line)))
664 (defbinding %draw-layout () nil
665 (drawable drawable) (gc gc)
668 (layout pango:layout))
670 (defbinding %draw-layout-with-colors () nil
671 (drawable drawable) (gc gc)
674 (layout pango:layout)
675 (foreground (or null color))
676 (background (or null color)))
678 (defun draw-layout (drawable gc font x y layout &optional foreground background)
679 (if (or foreground background)
680 (%draw-layout-with-colors drawable gc font x y layout foreground background)
681 (%draw-layout drawable gc font x y layout)))
683 (defbinding draw-drawable
684 (drawable gc src src-x src-y dest-x dest-y &optional width height) nil
685 (drawable drawable) (gc gc)
687 (src-x int) (src-y int)
688 (dest-x int) (dest-y int)
689 ((or width -1) int) ((or height -1) int))
691 (defbinding draw-image
692 (drawable gc image src-x src-y dest-x dest-y &optional width height) nil
693 (drawable drawable) (gc gc)
695 (src-x int) (src-y int)
696 (dest-x int) (dest-y int)
697 ((or width -1) int) ((or height -1) int))
699 (defbinding drawable-get-image () image
702 (width int) (height int))
704 (defbinding drawable-copy-to-image
705 (drawable src-x src-y width height &optional image dest-x dest-y) image
707 (image (or null image))
708 (src-x int) (src-y int)
709 ((if image dest-x 0) int)
710 ((if image dest-y 0) int)
711 (width int) (height int))
716 (defbinding keyval-name () string
717 (keyval unsigned-int))
719 (defbinding %keyval-from-name () unsigned-int
722 (defun keyval-from-name (name)
723 "Returns the keysym value for the given key name or NIL if it is not a valid name."
724 (let ((keyval (%keyval-from-name name)))
725 (unless (zerop keyval)
728 (defbinding keyval-to-upper () unsigned-int
729 (keyval unsigned-int))
731 (defbinding keyval-to-lower () unsigned-int
732 (keyval unsigned-int))
734 (defbinding (keyval-is-upper-p "gdk_keyval_is_upper") () boolean
735 (keyval unsigned-int))
737 (defbinding (keyval-is-lower-p "gdk_keyval_is_lower") () boolean
738 (keyval unsigned-int))
740 ;;; Cairo interaction
744 (defbinding cairo-create () cairo:context
747 (defmacro with-cairo-context ((cr drawable) &body body)
748 `(let ((,cr (cairo-create ,drawable)))
751 (unreference-foreign 'cairo:context (foreign-location ,cr))
752 (invalidate-instance ,cr))))
754 (defbinding cairo-set-source-color () nil
758 (defbinding cairo-set-source-pixbuf () nil
764 (defbinding cairo-rectangle () nil
766 (rectangle rectangle))
768 ;; (defbinding cairo-region () nil
769 ;; (cr cairo:context)
774 ;;; Multi-threading support
778 (defvar *global-lock* (sb-thread:make-mutex :name "global GDK lock"))
779 (let ((recursive-level 0))
780 (defun threads-enter ()
781 (if (eq (sb-thread:mutex-value *global-lock*) sb-thread:*current-thread*)
782 (incf recursive-level)
783 (sb-thread:get-mutex *global-lock*)))
785 (defun threads-leave (&optional flush-p)
787 ((zerop recursive-level)
790 (sb-thread:release-mutex *global-lock*))
791 (t (decf recursive-level)))))
793 (define-callback %enter-fn nil ()
796 (define-callback %leave-fn nil ()
799 (defbinding threads-set-lock-functions (&optional) nil
801 (%leave-fn callback))
803 (defmacro with-global-lock (&body body)
808 (threads-leave t)))))