398b331de239b5cb8e0b3a3f44de293ceb6374e6
[clg] / gdk / gdk.lisp
1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
3 ;;
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:
11 ;;
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
14 ;;
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.
22
23 ;; $Id: gdk.lisp,v 1.24 2006-04-10 18:38:42 espen Exp $
24
25
26 (in-package "GDK")
27
28 ;;; Initialization
29
30 (defbinding (gdk-init "gdk_parse_args") () nil
31 "Initializes the library without opening the display."
32 (nil null)
33 (nil null))
34
35
36
37 ;;; Display
38
39 (defbinding %display-open () display
40 (display-name (or null string)))
41
42 (defun display-open (&optional display-name)
43 (let ((display (%display-open display-name)))
44 (unless (display-get-default)
45 (display-set-default display))
46 display))
47
48 (defbinding %display-get-n-screens () int
49 (display display))
50
51 (defbinding %display-get-screen () screen
52 (display display)
53 (screen-num int))
54
55 (defun display-screens (&optional (display (display-get-default)))
56 (loop
57 for i from 0 below (%display-get-n-screens display)
58 collect (%display-get-screen display i)))
59
60 (defbinding display-get-default-screen
61 (&optional (display (display-get-default))) screen
62 (display display))
63
64 (defbinding display-beep (&optional (display (display-get-default))) nil
65 (display display))
66
67 (defbinding display-sync (&optional (display (display-get-default))) nil
68 (display display))
69
70 (defbinding display-flush (&optional (display (display-get-default))) nil
71 (display display))
72
73 (defbinding display-close (&optional (display (display-get-default))) nil
74 (display display))
75
76 (defbinding display-get-event
77 (&optional (display (display-get-default))) event
78 (display display))
79
80 (defbinding display-peek-event
81 (&optional (display (display-get-default))) event
82 (display display))
83
84 (defbinding display-put-event
85 (event &optional (display (display-get-default))) event
86 (display display)
87 (event event))
88
89 (defbinding (display-connection-number "clg_gdk_connection_number")
90 (&optional (display (display-get-default))) int
91 (display display))
92
93
94
95 ;;; Display manager
96
97 (defbinding display-get-default () display)
98
99 (defbinding (display-manager "gdk_display_manager_get") () display-manager)
100
101 (defbinding (display-set-default "gdk_display_manager_set_default_display")
102 (display) nil
103 ((display-manager) display-manager)
104 (display display))
105
106
107
108 ;;; Events
109
110 (defbinding (events-pending-p "gdk_events_pending") () boolean)
111
112 (defbinding event-get () event)
113
114 (defbinding event-peek () event)
115
116 (defbinding event-get-graphics-expose () event
117 (window window))
118
119 (defbinding event-put () event
120 (event event))
121
122 ;(defbinding event-handler-set () ...)
123
124 (defbinding set-show-events () nil
125 (show-events boolean))
126
127 (defbinding get-show-events () boolean)
128
129
130 ;;; Miscellaneous functions
131
132 (defbinding screen-width () int)
133 (defbinding screen-height () int)
134
135 (defbinding screen-width-mm () int)
136 (defbinding screen-height-mm () int)
137
138 (defbinding pointer-grab
139 (window &key owner-events events confine-to cursor time) grab-status
140 (window window)
141 (owner-events boolean)
142 (events event-mask)
143 (confine-to (or null window))
144 (cursor (or null cursor))
145 ((or time 0) (unsigned 32)))
146
147 (defbinding (pointer-ungrab "gdk_display_pointer_ungrab")
148 (&optional time (display (display-get-default))) nil
149 (display display)
150 ((or time 0) (unsigned 32)))
151
152 (defbinding (pointer-is-grabbed-p "gdk_display_pointer_is_grabbed")
153 (&optional (display (display-get-default))) boolean)
154
155 (defbinding keyboard-grab (window &key owner-events time) grab-status
156 (window window)
157 (owner-events boolean)
158 ((or time 0) (unsigned 32)))
159
160 (defbinding (keyboard-ungrab "gdk_display_keyboard_ungrab")
161 (&optional time (display (display-get-default))) nil
162 (display display)
163 ((or time 0) (unsigned 32)))
164
165
166
167 (defbinding atom-intern (atom-name &optional only-if-exists) atom
168 ((string atom-name) string)
169 (only-if-exists boolean))
170
171 (defbinding atom-name () string
172 (atom atom))
173
174
175
176 ;;; Visuals
177
178 (defbinding visual-get-best-depth () int)
179
180 (defbinding visual-get-best-type () visual-type)
181
182 (defbinding visual-get-system () visual)
183
184
185 (defbinding (%visual-get-best-with-nothing "gdk_visual_get_best") () visual)
186
187 (defbinding %visual-get-best-with-depth () visual
188 (depth int))
189
190 (defbinding %visual-get-best-with-type () visual
191 (type visual-type))
192
193 (defbinding %visual-get-best-with-both () visual
194 (depth int)
195 (type visual-type))
196
197 (defun visual-get-best (&key depth type)
198 (cond
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))))
203
204 ;(defbinding query-depths ..)
205
206 ;(defbinding query-visual-types ..)
207
208 (defbinding list-visuals () (glist visual))
209
210
211 ;;; Windows
212
213 (defbinding window-destroy () nil
214 (window window))
215
216
217 (defbinding window-at-pointer () window
218 (x int :out)
219 (y int :out))
220
221 (defbinding window-show () nil
222 (window window))
223
224 (defbinding window-show-unraised () nil
225 (window window))
226
227 (defbinding window-hide () nil
228 (window window))
229
230 (defbinding window-is-visible-p () boolean
231 (window window))
232
233 (defbinding window-is-viewable-p () boolean
234 (window window))
235
236 (defbinding window-withdraw () nil
237 (window window))
238
239 (defbinding window-iconify () nil
240 (window window))
241
242 (defbinding window-deiconify () nil
243 (window window))
244
245 (defbinding window-stick () nil
246 (window window))
247
248 (defbinding window-unstick () nil
249 (window window))
250
251 (defbinding window-maximize () nil
252 (window window))
253
254 (defbinding window-unmaximize () nil
255 (window window))
256
257 (defbinding window-fullscreen () nil
258 (window window))
259
260 (defbinding window-unfullscreen () nil
261 (window window))
262
263 (defbinding window-set-keep-above () nil
264 (window window)
265 (setting boolean))
266
267 (defbinding window-set-keep-below () nil
268 (window window)
269 (setting boolean))
270
271 (defbinding window-move () nil
272 (window window)
273 (x int)
274 (y int))
275
276 (defbinding window-resize () nil
277 (window window)
278 (width int)
279 (height int))
280
281 (defbinding window-move-resize () nil
282 (window window)
283 (x int)
284 (y int)
285 (width int)
286 (height int))
287
288 (defbinding window-scroll () nil
289 (window window)
290 (dx int)
291 (dy int))
292
293 (defbinding window-reparent () nil
294 (window window)
295 (new-parent window)
296 (x int)
297 (y int))
298
299 (defbinding window-clear () nil
300 (window window))
301
302 (defbinding %window-clear-area () nil
303 (window window)
304 (x int) (y int) (width int) (height int))
305
306 (defbinding %window-clear-area-e () nil
307 (window window)
308 (x int) (y int) (width int) (height int))
309
310 (defun window-clear-area (window x y width height &optional expose)
311 (if expose
312 (%window-clear-area-e window x y width height)
313 (%window-clear-area window x y width height)))
314
315 (defbinding window-raise () nil
316 (window window))
317
318 (defbinding window-lower () nil
319 (window window))
320
321 (defbinding window-focus () nil
322 (window window)
323 (timestamp unsigned-int))
324
325 (defbinding window-register-dnd () nil
326 (window window))
327
328 (defbinding window-begin-resize-drag () nil
329 (window window)
330 (edge window-edge)
331 (button int)
332 (root-x int)
333 (root-y int)
334 (timestamp unsigned-int))
335
336 (defbinding window-begin-move-drag () nil
337 (window window)
338 (button int)
339 (root-x int)
340 (root-y int)
341 (timestamp unsigned-int))
342
343 ;; komplett så langt
344
345 (defbinding window-set-user-data () nil
346 (window window)
347 (user-data pointer))
348
349 (defbinding window-set-override-redirect () nil
350 (window window)
351 (override-redirect boolean))
352
353 ; (defbinding window-add-filter () nil
354
355 ; (defbinding window-remove-filter () nil
356
357 (defbinding window-shape-combine-mask () nil
358 (window window)
359 (shape-mask bitmap)
360 (offset-x int)
361 (offset-y int))
362
363 (defbinding window-set-child-shapes () nil
364 (window window))
365
366 (defbinding window-merge-child-shapes () nil
367 (window window))
368
369
370 (defbinding window-set-static-gravities () boolean
371 (window window)
372 (use-static boolean))
373
374 ; (defbinding add-client-message-filter ...
375
376 (defbinding window-set-cursor () nil
377 (window window)
378 (cursor (or null cursor)))
379
380 (defbinding window-get-pointer () window
381 (window window)
382 (x int :out)
383 (y int :out)
384 (mask modifier-type :out))
385
386 (defbinding %window-get-toplevels () (glist window))
387
388 (defun window-get-toplevels (&optional screen)
389 (if screen
390 (error "Not implemented")
391 (%window-get-toplevels)))
392
393 (defbinding %get-default-root-window () window)
394
395 (defun get-root-window (&optional display)
396 (if display
397 (error "Not implemented")
398 (%get-default-root-window)))
399
400
401
402 ;;; Drag and Drop
403
404 ;; Destination side
405
406 (defbinding drag-status () nil
407 (context drag-context)
408 (action drag-action)
409 (time (unsigned 32)))
410
411
412
413
414
415
416 ;;
417
418 (defbinding rgb-init () nil)
419
420
421
422
423 ;;; Cursor
424
425 (defmethod allocate-foreign ((cursor cursor) &key type mask fg bg
426 (x 0) (y 0) (display (display-get-default)))
427 (etypecase type
428 (keyword (%cursor-new-for-display display type))
429 (pixbuf (%cursor-new-from-pixbuf display type x y))
430 (pixmap (%cursor-new-from-pixmap type mask fg bg x y))))
431
432
433 (defbinding %cursor-new-for-display () pointer
434 (display display)
435 (cursor-type cursor-type))
436
437 (defbinding %cursor-new-from-pixmap () pointer
438 (source pixmap)
439 (mask bitmap)
440 (foreground color)
441 (background color)
442 (x int) (y int))
443
444 (defbinding %cursor-new-from-pixbuf () pointer
445 (display display)
446 (pixbuf pixbuf)
447 (x int) (y int))
448
449 (defbinding %cursor-ref () pointer
450 (location pointer))
451
452 (defbinding %cursor-unref () nil
453 (location pointer))
454
455 (defmethod reference-foreign ((class (eql (find-class 'cursor))) location)
456 (declare (ignore class))
457 (%cursor-ref location))
458
459 (defmethod unreference-foreign ((class (eql (find-class 'cursor))) location)
460 (declare (ignore class))
461 (%cursor-unref location))
462
463
464 ;;; Pixmaps
465
466 (defbinding pixmap-new (width height depth &key window) pixmap
467 (width int)
468 (height int)
469 (depth int)
470 (window (or null window)))
471
472 (defbinding %pixmap-colormap-create-from-xpm () pixmap
473 (window (or null window))
474 (colormap (or null colormap))
475 (mask bitmap :out)
476 (color (or null color))
477 (filename string))
478
479 (defbinding %pixmap-colormap-create-from-xpm-d () pixmap
480 (window (or null window))
481 (colormap (or null colormap))
482 (mask bitmap :out)
483 (color (or null color))
484 (data (vector string)))
485
486 (defun pixmap-create (source &key color window colormap)
487 (let ((window
488 (if (not (or window colormap))
489 (get-root-window)
490 window)))
491 (multiple-value-bind (pixmap mask)
492 (etypecase source
493 ((or string pathname)
494 (%pixmap-colormap-create-from-xpm
495 window colormap color (namestring (truename source))))
496 ((vector string)
497 (%pixmap-colormap-create-from-xpm-d window colormap color source)))
498 ;; (unreference-instance pixmap)
499 ;; (unreference-instance mask)
500 (values pixmap mask))))
501
502
503
504 ;;; Color
505
506 (defbinding %color-copy () pointer
507 (location pointer))
508
509 (defmethod allocate-foreign ((color color) &rest initargs)
510 (declare (ignore color initargs))
511 ;; Color structs are allocated as memory chunks by gdk, and since
512 ;; there is no gdk_color_new we have to use this hack to get a new
513 ;; color chunk
514 (with-memory (location #.(foreign-size (find-class 'color)))
515 (%color-copy location)))
516
517 (defun %scale-value (value)
518 (etypecase value
519 (integer value)
520 (float (truncate (* value 65535)))))
521
522 (defmethod initialize-instance ((color color) &rest initargs
523 &key red green blue)
524 (declare (ignore initargs))
525 (call-next-method)
526 (with-slots ((%red red) (%green green) (%blue blue)) color
527 (setf
528 %red (%scale-value red)
529 %green (%scale-value green)
530 %blue (%scale-value blue))))
531
532 (defbinding color-parse (spec &optional (make-instance 'color)) boolean
533 (spec string)
534 (color color :return))
535
536 (defun ensure-color (color)
537 (etypecase color
538 (null nil)
539 (color color)
540 (vector
541 (make-instance 'color
542 :red (svref color 0) :green (svref color 1) :blue (svref color 2)))
543 (string
544 (multiple-value-bind (succeeded-p color) (parse-color color)
545 (if succeeded-p
546 color
547 (error "Parsing color specification ~S failed." color))))))
548
549
550
551 ;;; Drawable
552
553 (defbinding drawable-get-size () nil
554 (drawable drawable)
555 (width int :out)
556 (height int :out))
557
558 (defbinding (drawable-width "gdk_drawable_get_size") () nil
559 (drawable drawable)
560 (width int :out)
561 (nil null))
562
563 (defbinding (drawable-height "gdk_drawable_get_size") () nil
564 (drawable drawable)
565 (nil null)
566 (height int :out))
567
568 ;; (defbinding drawable-get-clip-region () region
569 ;; (drawable drawable))
570
571 ;; (defbinding drawable-get-visible-region () region
572 ;; (drawable drawable))
573
574 (defbinding draw-point () nil
575 (drawable drawable) (gc gc)
576 (x int) (y int))
577
578 (defbinding %draw-points () nil
579 (drawable drawable) (gc gc)
580 (points pointer)
581 (n-points int))
582
583 ;; (defun draw-points (drawable gc &rest points)
584
585 ;; )
586
587 (defbinding draw-line () nil
588 (drawable drawable) (gc gc)
589 (x1 int) (y1 int)
590 (x2 int) (y2 int))
591
592 ;; (defbinding draw-lines (drawable gc &rest points) nil
593 ;; (drawable drawable) (gc gc)
594 ;; (points (vector point))
595 ;; ((length points) int))
596
597 (defbinding draw-pixbuf
598 (drawable gc pixbuf src-x src-y dest-x dest-y &optional
599 width height (dither :none) (x-dither 0) (y-dither 0)) nil
600 (drawable drawable) (gc (or null gc))
601 (pixbuf pixbuf)
602 (src-x int) (src-y int)
603 (dest-x int) (dest-y int)
604 ((or width -1) int) ((or height -1) int)
605 (dither rgb-dither)
606 (x-dither int) (y-dither int))
607
608 ;; (defbinding draw-segments (drawable gc &rest points) nil
609 ;; (drawable drawable) (gc gc)
610 ;; (segments (vector segments))
611 ;; ((length segments) int))
612
613 (defbinding draw-rectangle () nil
614 (drawable drawable) (gc gc)
615 (filled boolean)
616 (x int) (y int)
617 (width int) (height int))
618
619 (defbinding draw-arc () nil
620 (drawable drawable) (gc gc)
621 (filled boolean)
622 (x int) (y int)
623 (width int) (height int)
624 (angle1 int) (angle2 int))
625
626 ;; (defbinding draw-polygon (drawable gc &rest points) nil
627 ;; (drawable drawable) (gc gc)
628 ;; (points (vector point))
629 ;; ((length points) int))
630
631 ;; (defbinding draw-trapezoid (drawable gc &rest points) nil
632 ;; (drawable drawable) (gc gc)
633 ;; (points (vector point))
634 ;; ((length points) int))
635
636 ;; (defbinding %draw-layout-line () nil
637 ;; (drawable drawable) (gc gc)
638 ;; (font pango:font)
639 ;; (x int) (y int)
640 ;; (line pango:layout-line))
641
642 ;; (defbinding %draw-layout-line-with-colors () nil
643 ;; (drawable drawable) (gc gc)
644 ;; (font pango:font)
645 ;; (x int) (y int)
646 ;; (line pango:layout-line)
647 ;; (foreground (or null color))
648 ;; (background (or null color)))
649
650 ;; (defun draw-layout-line (drawable gc font x y line &optional foreground background)
651 ;; (if (or foreground background)
652 ;; (%draw-layout-line-with-colors drawable gc font x y line foreground background)
653 ;; (%draw-layout-line drawable gc font x y line)))
654
655 (defbinding %draw-layout () nil
656 (drawable drawable) (gc gc)
657 (font pango:font)
658 (x int) (y int)
659 (layout pango:layout))
660
661 (defbinding %draw-layout-with-colors () nil
662 (drawable drawable) (gc gc)
663 (font pango:font)
664 (x int) (y int)
665 (layout pango:layout)
666 (foreground (or null color))
667 (background (or null color)))
668
669 (defun draw-layout (drawable gc font x y layout &optional foreground background)
670 (if (or foreground background)
671 (%draw-layout-with-colors drawable gc font x y layout foreground background)
672 (%draw-layout drawable gc font x y layout)))
673
674 (defbinding draw-drawable
675 (drawable gc src src-x src-y dest-x dest-y &optional width height) nil
676 (drawable drawable) (gc gc)
677 (src drawable)
678 (src-x int) (src-y int)
679 (dest-x int) (dest-y int)
680 ((or width -1) int) ((or height -1) int))
681
682 (defbinding draw-image
683 (drawable gc image src-x src-y dest-x dest-y &optional width height) nil
684 (drawable drawable) (gc gc)
685 (image image)
686 (src-x int) (src-y int)
687 (dest-x int) (dest-y int)
688 ((or width -1) int) ((or height -1) int))
689
690 (defbinding drawable-get-image () image
691 (drawable drawable)
692 (x int) (y int)
693 (width int) (height int))
694
695 (defbinding drawable-copy-to-image
696 (drawable src-x src-y width height &optional image dest-x dest-y) image
697 (drawable drawable)
698 (image (or null image))
699 (src-x int) (src-y int)
700 ((if image dest-x 0) int)
701 ((if image dest-y 0) int)
702 (width int) (height int))
703
704
705 ;;; Key values
706
707 (defbinding keyval-name () string
708 (keyval unsigned-int))
709
710 (defbinding %keyval-from-name () unsigned-int
711 (name string))
712
713 (defun keyval-from-name (name)
714 "Returns the keysym value for the given key name or NIL if it is not a valid name."
715 (let ((keyval (%keyval-from-name name)))
716 (unless (zerop keyval)
717 keyval)))
718
719 (defbinding keyval-to-upper () unsigned-int
720 (keyval unsigned-int))
721
722 (defbinding keyval-to-lower () unsigned-int
723 (keyval unsigned-int))
724
725 (defbinding (keyval-is-upper-p "gdk_keyval_is_upper") () boolean
726 (keyval unsigned-int))
727
728 (defbinding (keyval-is-lower-p "gdk_keyval_is_lower") () boolean
729 (keyval unsigned-int))
730
731 ;;; Cairo interaction
732
733 #+gtk2.8
734 (progn
735 (defbinding cairo-create () cairo:context
736 (drawable drawable))
737
738 (defmacro with-cairo-context ((cr drawable) &body body)
739 `(let ((,cr (cairo-create ,drawable)))
740 (unwind-protect
741 (progn ,@body)
742 (unreference-foreign 'cairo:context (foreign-location ,cr))
743 (invalidate-instance ,cr))))
744
745 (defbinding cairo-set-source-color () nil
746 (cr cairo:context)
747 (color color))
748
749 (defbinding cairo-set-source-pixbuf () nil
750 (cr cairo:context)
751 (pixbuf pixbuf)
752 (x double-float)
753 (y double-float))
754
755 (defbinding cairo-rectangle () nil
756 (cr cairo:context)
757 (rectangle rectangle))
758
759 ;; (defbinding cairo-region () nil
760 ;; (cr cairo:context)
761 ;; (region region))
762 )