chiark / gitweb /
gffi/basic-types.lisp: clisp already defines a `byte' type'.
[clg] / gtk / gtkwidget.lisp
1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2006 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: gtkwidget.lisp,v 1.32 2008-11-25 22:17:08 espen Exp $
24
25 (in-package "GTK")
26
27 #-debug-ref-counting
28 (defmethod print-object ((widget widget) stream)
29   (if (and 
30        (proxy-valid-p widget) 
31        (slot-boundp widget 'name) (not (zerop (length (widget-name widget)))))
32       (print-unreadable-object (widget stream :type t :identity nil)
33         (format stream "~S at 0x~X" 
34          (widget-name widget) (pointer-address (foreign-location widget))))
35     (call-next-method)))
36
37 (defmethod shared-initialize ((widget widget) names &key (visible nil visible-p))
38   (declare (ignore names))
39   (when (and visible-p (not visible)) ; widget explicit set as not visible
40     (setf (user-data widget 'hidden-p) t)
41     (signal-connect widget 'show 
42      #'(lambda () 
43          (unset-user-data widget 'hidden-p))
44      :remove t))
45   (call-next-method))
46
47 (defmethod shared-initialize :after ((widget widget) names &key parent visible)
48   (declare (ignore names))
49   (when visible
50     (widget-show widget))
51   (when parent
52     (when (slot-boundp widget 'parent)
53       (container-remove (widget-parent widget) widget))
54     (destructuring-bind (parent &rest args)  (mklist parent)
55       (apply #'container-add parent widget args))))
56
57 (defmethod slot-unbound ((class gobject-class) (object widget) 
58                          (slot (eql 'child-properties)))
59   (cond
60    ((slot-boundp object 'parent)
61     (with-slots (parent child-properties) object
62       (setf child-properties
63        (make-instance (find-child-class (class-of parent))
64         :parent parent :child object))))
65    ((call-next-method))))
66
67
68 (defparameter *widget-display-as-default-in-signal-handler-p* t)
69
70 (defmethod compute-signal-function ((widget widget) signal function object args)
71   (let ((wrapper
72          (if (eq object :parent)
73              #'(lambda (widget &rest emission-args)
74                  (let ((all-args (nconc 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                           (apply #'signal-emit widget signal emission-args))
82                       :remove t))))
83            (call-next-method))))
84     (if *widget-display-as-default-in-signal-handler-p*
85         #'(lambda (widget &rest args)
86             (let ((display (when (slot-boundp widget 'window)
87                              (gdk:drawable-display (widget-window widget)))))
88               (gdk:with-default-display (display)
89                 (apply wrapper widget args))))
90       wrapper)))
91
92
93       
94 (defun child-property-value (widget slot)
95   (slot-value (widget-child-properties widget) slot))
96
97 (defun (setf child-property-value) (value widget slot)
98   (setf (slot-value (widget-child-properties widget) slot) value))
99
100 (defmacro with-child-properties (slots widget &body body)
101   `(with-slots ,slots (widget-child-properties ,widget)
102      ,@body))
103
104
105 ;;; Bindings
106
107 (defbinding widget-destroy () nil
108   (widget widget))
109
110 (defbinding widget-unparent () nil
111   (widget widget))
112
113 (defbinding widget-show () nil
114   (widget widget))
115
116 (defbinding widget-show-now () nil
117   (widget widget))
118
119 (defbinding widget-hide () nil
120   (widget widget))
121
122 (defun widget-hidden-p (widget)
123   "Return T if WIDGET has been explicit hidden during construction."
124   (user-data widget 'hidden-p))
125
126 (defbinding widget-show-all () nil
127   (widget widget))
128
129 (defbinding widget-hide-all () nil
130   (widget widget))
131
132 (defbinding widget-map () nil
133   (widget widget))
134
135 (defbinding widget-unmap () nil
136   (widget widget))
137
138 (defbinding widget-realize () nil
139   (widget widget))
140
141 (defbinding widget-unrealize () nil
142   (widget widget))
143
144 (defbinding widget-queue-draw () nil
145   (widget widget))
146
147 (defbinding widget-queue-resize () nil
148   (widget widget))
149
150 (defbinding widget-queue-resize-no-redraw () nil
151   (widget widget))
152
153 (defbinding widget-size-request
154     (widget &optional (requisition (make-instance 'requisition))) nil
155   (widget widget)
156   (requisition requisition :in/return))
157
158 (defbinding widget-get-child-requisition 
159     (widget &optional (requisition (make-instance 'requisition))) nil
160   (widget widget)
161   (requisition requisition :in/return))
162
163 (defbinding widget-size-allocate () nil
164   (widget widget)
165   (allocation allocation))
166
167 (defbinding widget-add-accelerator
168     (widget signal accel-group key modifiers flags) nil
169   (widget widget)
170   ((signal-name-to-string signal) string)
171   (accel-group accel-group)
172   ((gdk:keyval-from-name key) unsigned-int)
173   (modifiers gdk:modifier-type)
174   (flags accel-flags))
175
176 (defbinding widget-remove-accelerator
177     (widget accel-group key modifiers) nil
178   (widget widget)
179   (accel-group accel-group)
180   ((gdk:keyval-from-name key) unsigned-int)
181   (modifiers gdk:modifier-type))
182
183 (defbinding widget-set-accel-path () nil
184   (widget widget)
185   (accel-path string)
186   (accel-group accel-group))
187
188 (defbinding widget-list-accel-closures () (glist pointer)
189   (widget widget))
190
191 (defbinding widget-can-activate-accel-p () boolean
192   (widget widget)
193   (signal-id unsigned-int))
194
195 (defbinding widget-event () boolean
196   (widget widget)
197   (event gdk:event))
198
199 (defbinding widget-activate () boolean
200   (widget widget))
201
202 (defbinding widget-reparent () nil
203   (widget widget)
204   (new-parent widget))
205
206 (defbinding %widget-intersect () boolean
207   (widget widget)
208   (area gdk:rectangle)
209   (intersection (or null gdk:rectangle)))
210
211 (defun widget-intersection (widget area)
212   (let ((intersection (make-instance 'gdk:rectangle)))
213     (when (%widget-intersect widget area intersection)
214       intersection)))
215
216 (defun widget-intersect-p (widget area)
217   (%widget-intersect widget area nil))
218
219 (defbinding widget-grab-focus () nil
220   (widget widget))
221
222 (defbinding widget-grab-default () nil
223   (widget widget))
224
225 (defbinding widget-add-events () nil
226   (widget widget)
227   (events gdk:event-mask))
228
229 (defbinding widget-get-toplevel () widget
230   (widget widget))
231
232 (defbinding widget-get-ancestor (widget type) widget
233   (widget widget)
234   ((find-type-number type) type-number))
235
236 (defbinding widget-get-pointer () nil
237   (widget widget)
238   (x int :out)
239   (y int :out))
240
241 (defbinding widget-is-ancestor-p () boolean
242   (widget widget)
243   (ancestor widget))
244
245 (defbinding widget-translate-coordinates () boolean
246   (src-widget widget)
247   (dest-widget widget)
248   (src-x int) (src-y int)
249   (set-x int :out) (dest-y int :out))
250
251 (defun widget-hide-on-delete (widget)
252   "Utility function; intended to be connected to the DELETE-EVENT
253 signal on a WINDOW. The function calls WIDGET-HIDE on its
254 argument, then returns T. If connected to DELETE-EVENT, the
255 result is that clicking the close button for a window (on the window
256 frame, top right corner usually) will hide but not destroy the
257 window. By default, GTK+ destroys windows when DELETE-EVENT is
258 received."
259   (widget-hide widget)
260   t)
261   
262 (defbinding widget-ensure-style () nil
263   (widget widget))
264
265 (defbinding widget-reset-rc-styles () nil
266   (widget widget))
267
268 (defbinding widget-push-colormap () nil
269   (colormap gdk:colormap))
270
271 (defbinding widget-pop-colormap () nil)
272
273 (defbinding %widget-set-default-colormap () nil
274   (colormap gdk:colormap))
275
276 (defun (setf widget-default-colormap) (colormap)
277   (%widget-set-default-colormap colormap)
278   colormap)
279
280 (defbinding (widget-default-style "gtk_widget_get_default_style") () style)
281
282 (defbinding (widget-default-colromap "gtk_widget_get_default_colormap") 
283     () gdk:colormap)
284
285 (defbinding (widget-default-visual "gtk_widget_get_default_visual") 
286     () gdk:visual)
287
288 (defbinding (widget-default-direction "gtk_widget_get_default_direction")
289     () text-direction)
290
291 (defbinding %widget-set-default-direction () nil
292   (direction text-direction))
293
294 (defun (setf widget-default-direction) (direction)
295   (%widget-set-default-direction direction)
296   direction)
297
298 (defbinding widget-shape-combine-mask () nil
299   (widget widget)
300   (shape-mask (or null gdk:bitmap))
301   (x-offset int)
302   (y-offset int))
303
304 (defun widget-path (widget)
305   (let ((subpath (list (if (and 
306                             (slot-boundp widget 'name) 
307                             (not (zerop (length (widget-name widget)))))
308                            (widget-name widget)
309                          (type-of widget)))))
310     (if (slot-boundp widget 'parent)
311         (nconc (widget-path (widget-parent widget)) subpath)
312       subpath)))
313
314 (defun widget-class-path (widget)
315     (let ((subpath (list (type-of widget))))
316   (if (slot-boundp widget 'parent)
317       (nconc (widget-class-path (widget-parent widget)) subpath)
318     subpath)))
319
320
321 (defun widget-path-lookup (path &optional (root (nreverse (window-list-toplevels))) (error-p t))
322   (let ((component (first path)))
323     (loop
324      for widget in (mklist root)
325      do (when (or
326                (and 
327                 (stringp component) (slot-boundp widget 'name) 
328                 (string= component (widget-name widget)))
329                (and
330                 (symbolp component) (typep widget component)))
331           (cond
332            ((endp (rest path)) (return widget))
333            ((typep widget 'container)
334             (let ((descendant (widget-path-lookup (rest path) (container-children widget) nil)))
335               (when descendant
336                 (return descendant))))))))
337   (when error-p
338     (error "Widget not found: ~A" path)))
339
340
341 (defun widget-find (name &optional (root (nreverse (window-list-toplevels))) (error-p t))
342   "Search for a widget with the given name. ROOT should be a container
343 widget or a list of containers."
344   (loop
345    for widget in (mklist root)
346    do (cond
347        ((and (slot-boundp widget 'name) (string= name (widget-name widget)))
348         (return-from widget-find widget))
349        ((typep widget 'container)
350         (let ((descendant (widget-find name (container-children widget) nil)))
351           (when descendant
352             (return-from widget-find descendant))))))
353   (when error-p
354     (error "Widget not found: ~A" name)))
355
356
357 (defbinding widget-modify-style () nil
358   (widget widget)
359   (style rc-style))
360
361 (defbinding widget-get-modifier-style () rc-style
362   (widget widget))
363
364 (defbinding widget-modify-fg () nil
365   (widget widget)
366   (state state-type)
367   (color gdk:color))
368
369 (defbinding widget-modify-bg () nil
370   (widget widget)
371   (state state-type)
372   (color gdk:color))
373
374 (defbinding widget-modify-text () nil
375   (widget widget)
376   (state state-type)
377   (color gdk:color))
378
379 (defbinding widget-modify-base () nil
380   (widget widget)
381   (state state-type)
382   (color gdk:color))
383
384 (defbinding widget-modify-font (widget font-desc) nil
385   (widget widget)
386   ((etypecase font-desc
387      (pango:font-description font-desc)
388      (string (pango:font-description-from-string font-desc)))
389    pango:font-description))
390
391 (defbinding widget-create-pango-context () pango:context
392   (widget widget))
393
394 (defbinding widget-get-pango-context () pango:context
395   (widget widget))
396
397 (defbinding widget-create-pango-layout (widget &optional text) pango:layout
398   (widget widget)
399   (text (or string null)))
400
401 (defbinding widget-render-icon (widget stock-id &optional size detail) 
402     gdk:pixbuf
403   (widget widget)
404   (stock-id string)
405   ((or size -1) (or icon-size int))
406   (detail (or null string)))
407
408 (defbinding widget-push-composite-child () nil)
409
410 (defbinding widget-pop-composite-child () nil)
411
412 (defbinding widget-queue-draw-area () nil
413   (widget widget)
414   (x int) (y int) (width int) (height int))
415
416 (defbinding widget-reset-shapes () nil
417   (widget widget))
418
419 (defbinding widget-set-double-buffered () nil
420   (widget widget)
421   (double-buffered boolean))
422
423 ;; (defbinding widget-set-redraw-on-allocate () nil
424 ;;   (widget widget)
425 ;;   (redraw-on-allocate boolean))
426
427 (defbinding widget-set-scroll-adjustments () boolean
428   (widget widget)
429   (hadjustment (or null adjustment))
430   (vadjustment (or null adjustment)))
431
432 (defbinding widget-mnemonic-activate () boolean
433   (widget widget)
434   (group-cycling boolean))
435
436 (defbinding widget-class-find-style-property (class name) param
437   ((type-class-peek class) pointer)
438   (name string))
439
440 (defbinding widget-class-list-style-properties (class)
441     (vector (copy-of param) n-properties)
442   ((type-class-peek class) pointer)
443   (n-properties unsigned-int :out))
444
445 (defbinding widget-region-intersect () pointer ;gdk:region
446   (widget widget)
447   (region pointer)) ;gdk:region))
448
449 (defbinding widget-send-expose () boolean
450   (widget widget)
451   (event gdk:event))
452
453 (defbinding %widget-style-get-property () nil
454   (widget widget)
455   (name string)
456   (value gvalue))
457
458 (defun style-property-value (widget style)
459   (let* ((name (string-downcase style))
460          (param (widget-class-find-style-property (class-of widget) name)))
461     (if (not param)
462         (error "~A has no such style property: ~A" widget style)
463       (with-gvalue (gvalue (param-value-type param))
464         (%widget-style-get-property widget (string-downcase style) gvalue)))))
465
466 (defbinding widget-get-accessible () atk:object
467   (widget widget))
468
469 (defbinding widget-child-focus () boolean
470   (widget widget)
471   (direction direction-type))
472
473 (defbinding widget-child-notify () nil
474   (widget widget)
475   (child-property string))
476
477 (defbinding widget-freeze-child-notify () nil
478   (widget widget))
479
480 (defbinding widget-get-clipboard () clipboard
481   (widget widget)
482   (selection int #|gdk:atom|#))
483
484 (defbinding widget-get-display () gdk:display
485   (widget widget))
486
487 (defbinding widget-get-root-window () gdk:window
488   (widget widget))
489
490 (defbinding widget-get-screen () gdk:screen
491   (widget widget))
492
493 (defbinding widget-has-screen-p () boolean
494   (widget widget))
495
496 (defbinding %widget-get-size-request () nil
497   (widget widget)
498   (width int :out)
499   (height int :out))
500
501 (defun widget-get-size-request (widget)
502   (multiple-value-bind (width height) (%widget-get-size-request widget)
503      (values (unless (= width -1) width) (unless (= height -1) height))))
504
505 (defbinding widget-set-size-request (widget width height) nil
506   (widget widget)
507   ((or width -1) int)
508   ((or height -1) int))
509
510 (defbinding widget-thaw-child-notify () nil
511   (widget widget))
512
513 (defbinding widget-list-mnemonic-labels () (glist widget)
514   (widget widget))
515
516 (defbinding widget-add-mnemonic-label () nil
517   (widget widget)
518   (label widget))
519
520 (defbinding widget-remove-mnemonic-label () nil
521   (widget widget)
522   (label widget))
523
524 #?(pkg-exists-p "gtk+-2.0" :atleast-version "2.14.0")
525 (defbinding widget-get-snapshot () gdk:pixmap
526   (widget widget)
527   (clip-rect (or null gdk:rectangle)))
528
529
530 ;;; Additional bindings and functions
531
532 (defbinding %widget-flags () int
533   (widget widget))
534
535 (defun widget-flags (widget)
536   (let ((flags (%widget-flags widget)))
537     (nconc 
538      (int-to-object-flags flags)
539      (int-to-widget-flags flags))))
540
541 (defun widget-mapped-p (widget)
542   (find :mapped (widget-flags widget)))
543
544 (defun widget-realized-p (widget)
545   (find :realized (widget-flags widget)))
546
547 (defbinding widget-get-size-allocation () nil
548   (widget widget)
549   (width int :out)
550   (height int :out))
551
552 (defbinding get-event-widget () widget
553   (event gdk:event))
554
555 (defun (setf widget-cursor) (cursor-type widget)
556   (warn "(SETF WIDGET-CURSOR) is deprecated, use WIDGET-SET-CURSOR instead")
557   (widget-set-cursor widget cursor-type))
558
559 (defun widget-set-cursor (widget cursor &rest args)
560   (gdk:window-set-cursor (widget-window widget) 
561    (apply #'gdk:ensure-cursor cursor args)))
562
563 (defbinding %widget-get-parent-window () gdk:window
564   (widget widget))
565
566 (defun %widget-parent-window (widget)
567   (when (slot-boundp widget 'parent)
568     (%widget-get-parent-window widget)))