Change to COLOR-PARSE and some bug fixes
[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.25 2006-04-11 18:28:38 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-allocated-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 0.0) (green 0.0) (blue 0.0))
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 () boolean
533 (spec string)
534 (color color :return))
535
536 (defun color-parse (spec &optional (color (make-instance 'color)))
537 (multiple-value-bind (succeeded-p color) (%color-parse spec color)
538 (if succeeded-p
539 color
540 (error "Parsing color specification ~S failed." spec))))
541
542 (defun ensure-color (color)
543 (etypecase color
544 (null nil)
545 (color color)
546 (string (color-parse color))
547 (vector
548 (make-instance 'color
549 :red (svref color 0) :green (svref color 1) :blue (svref color 2)))))
550
551
552
553 ;;; Drawable
554
555 (defbinding drawable-get-size () nil
556 (drawable drawable)
557 (width int :out)
558 (height int :out))
559
560 (defbinding (drawable-width "gdk_drawable_get_size") () nil
561 (drawable drawable)
562 (width int :out)
563 (nil null))
564
565 (defbinding (drawable-height "gdk_drawable_get_size") () nil
566 (drawable drawable)
567 (nil null)
568 (height int :out))
569
570 ;; (defbinding drawable-get-clip-region () region
571 ;; (drawable drawable))
572
573 ;; (defbinding drawable-get-visible-region () region
574 ;; (drawable drawable))
575
576 (defbinding draw-point () nil
577 (drawable drawable) (gc gc)
578 (x int) (y int))
579
580 (defbinding %draw-points () nil
581 (drawable drawable) (gc gc)
582 (points pointer)
583 (n-points int))
584
585 ;; (defun draw-points (drawable gc &rest points)
586
587 ;; )
588
589 (defbinding draw-line () nil
590 (drawable drawable) (gc gc)
591 (x1 int) (y1 int)
592 (x2 int) (y2 int))
593
594 ;; (defbinding draw-lines (drawable gc &rest points) nil
595 ;; (drawable drawable) (gc gc)
596 ;; (points (vector point))
597 ;; ((length points) int))
598
599 (defbinding draw-pixbuf
600 (drawable gc pixbuf src-x src-y dest-x dest-y &optional
601 width height (dither :none) (x-dither 0) (y-dither 0)) nil
602 (drawable drawable) (gc (or null gc))
603 (pixbuf pixbuf)
604 (src-x int) (src-y int)
605 (dest-x int) (dest-y int)
606 ((or width -1) int) ((or height -1) int)
607 (dither rgb-dither)
608 (x-dither int) (y-dither int))
609
610 ;; (defbinding draw-segments (drawable gc &rest points) nil
611 ;; (drawable drawable) (gc gc)
612 ;; (segments (vector segments))
613 ;; ((length segments) int))
614
615 (defbinding draw-rectangle () nil
616 (drawable drawable) (gc gc)
617 (filled boolean)
618 (x int) (y int)
619 (width int) (height int))
620
621 (defbinding draw-arc () nil
622 (drawable drawable) (gc gc)
623 (filled boolean)
624 (x int) (y int)
625 (width int) (height int)
626 (angle1 int) (angle2 int))
627
628 ;; (defbinding draw-polygon (drawable gc &rest points) nil
629 ;; (drawable drawable) (gc gc)
630 ;; (points (vector point))
631 ;; ((length points) int))
632
633 ;; (defbinding draw-trapezoid (drawable gc &rest points) nil
634 ;; (drawable drawable) (gc gc)
635 ;; (points (vector point))
636 ;; ((length points) int))
637
638 ;; (defbinding %draw-layout-line () nil
639 ;; (drawable drawable) (gc gc)
640 ;; (font pango:font)
641 ;; (x int) (y int)
642 ;; (line pango:layout-line))
643
644 ;; (defbinding %draw-layout-line-with-colors () nil
645 ;; (drawable drawable) (gc gc)
646 ;; (font pango:font)
647 ;; (x int) (y int)
648 ;; (line pango:layout-line)
649 ;; (foreground (or null color))
650 ;; (background (or null color)))
651
652 ;; (defun draw-layout-line (drawable gc font x y line &optional foreground background)
653 ;; (if (or foreground background)
654 ;; (%draw-layout-line-with-colors drawable gc font x y line foreground background)
655 ;; (%draw-layout-line drawable gc font x y line)))
656
657 (defbinding %draw-layout () nil
658 (drawable drawable) (gc gc)
659 (font pango:font)
660 (x int) (y int)
661 (layout pango:layout))
662
663 (defbinding %draw-layout-with-colors () nil
664 (drawable drawable) (gc gc)
665 (font pango:font)
666 (x int) (y int)
667 (layout pango:layout)
668 (foreground (or null color))
669 (background (or null color)))
670
671 (defun draw-layout (drawable gc font x y layout &optional foreground background)
672 (if (or foreground background)
673 (%draw-layout-with-colors drawable gc font x y layout foreground background)
674 (%draw-layout drawable gc font x y layout)))
675
676 (defbinding draw-drawable
677 (drawable gc src src-x src-y dest-x dest-y &optional width height) nil
678 (drawable drawable) (gc gc)
679 (src drawable)
680 (src-x int) (src-y int)
681 (dest-x int) (dest-y int)
682 ((or width -1) int) ((or height -1) int))
683
684 (defbinding draw-image
685 (drawable gc image src-x src-y dest-x dest-y &optional width height) nil
686 (drawable drawable) (gc gc)
687 (image image)
688 (src-x int) (src-y int)
689 (dest-x int) (dest-y int)
690 ((or width -1) int) ((or height -1) int))
691
692 (defbinding drawable-get-image () image
693 (drawable drawable)
694 (x int) (y int)
695 (width int) (height int))
696
697 (defbinding drawable-copy-to-image
698 (drawable src-x src-y width height &optional image dest-x dest-y) image
699 (drawable drawable)
700 (image (or null image))
701 (src-x int) (src-y int)
702 ((if image dest-x 0) int)
703 ((if image dest-y 0) int)
704 (width int) (height int))
705
706
707 ;;; Key values
708
709 (defbinding keyval-name () string
710 (keyval unsigned-int))
711
712 (defbinding %keyval-from-name () unsigned-int
713 (name string))
714
715 (defun keyval-from-name (name)
716 "Returns the keysym value for the given key name or NIL if it is not a valid name."
717 (let ((keyval (%keyval-from-name name)))
718 (unless (zerop keyval)
719 keyval)))
720
721 (defbinding keyval-to-upper () unsigned-int
722 (keyval unsigned-int))
723
724 (defbinding keyval-to-lower () unsigned-int
725 (keyval unsigned-int))
726
727 (defbinding (keyval-is-upper-p "gdk_keyval_is_upper") () boolean
728 (keyval unsigned-int))
729
730 (defbinding (keyval-is-lower-p "gdk_keyval_is_lower") () boolean
731 (keyval unsigned-int))
732
733 ;;; Cairo interaction
734
735 #+gtk2.8
736 (progn
737 (defbinding cairo-create () cairo:context
738 (drawable drawable))
739
740 (defmacro with-cairo-context ((cr drawable) &body body)
741 `(let ((,cr (cairo-create ,drawable)))
742 (unwind-protect
743 (progn ,@body)
744 (unreference-foreign 'cairo:context (foreign-location ,cr))
745 (invalidate-instance ,cr))))
746
747 (defbinding cairo-set-source-color () nil
748 (cr cairo:context)
749 (color color))
750
751 (defbinding cairo-set-source-pixbuf () nil
752 (cr cairo:context)
753 (pixbuf pixbuf)
754 (x double-float)
755 (y double-float))
756
757 (defbinding cairo-rectangle () nil
758 (cr cairo:context)
759 (rectangle rectangle))
760
761 ;; (defbinding cairo-region () nil
762 ;; (cr cairo:context)
763 ;; (region region))
764 )