gtk-basic.stk 8.46 KB
Newer Older
eg's avatar
eg committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
;;;;
;;;; gtk-basic.stk	-- Basic classes for GTK+ support
;;;; 
;;;; Copyright  2000-2002 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
;;;; 
;;;; 
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
;;;; 
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
;;;; USA.
;;;; 
;;;;           Author: Erick Gallesio [eg@unice.fr]
;;;;    Creation date: 16-Aug-2000 09:21 (eg)
;;;; Last file update:  8-Apr-2002 13:06 (eg)
;;;;

;;;;======================================================================
;;;; 
;;;; 				    Utilities 
;;;;
;;;;======================================================================

;; HACK: Set the %before-exit-hook to %gtk-main so that GUI program don't stop
;; when they are loaded
(define %before-exit-hook (symbol-value '%gtk-main (current-module)))

(define (%gtk-slot-ro slot)
  (lambda (o v)
    (error "slot `~S' is read-only in ~S" slot o)))

(define (%gtk-active-slot-ro slot)
  (lambda (o v)
    (when (slot-bound? o slot) (error "slot `~S' is read-only in ~S" slot o))
    #t))

(define (%gtk-slot-init-only name)
  (let ((keyword (make-keyword name)))
    (lambda (o v)
      (when (slot-bound? o name)
	(error "slot `~S' is read-only in ~S" name o))
      ;; This is the init form. Do the initialization
      (%widget-plist (slot-ref o 'wid) keyword v))))


(define (%gtk-bad-orientation value)
  (error "value ~ is a bad orientation (should be HORIZONTAL or VERTICAL)"
	 value))

;;;
;;; Tooltips
;;;
(define tooltips-configure 
  (let ((enabled #t)
	(time    #f))
    (lambda (:rest l :key (enable #f enable?) (delay 1000 delay?))
      (cond
       (enable?     (%tooltip-configure (if enable 0 1))
		    (set! enabled enable))
       (delay?      (%tooltip-configure 2 delay)
		    (set! time delay))
       ((null? l)  `(:enable ,enabled :delay ,time))
       (else 	    (error 'tooltips-configure "bad option ~S" l))))))

; initialize tooltips
(tooltips-configure :delay 500)


;;;;======================================================================
;;;; 
;;;; 			M e t a c l a s s e s
;;;; 
;;;;======================================================================

(define-class <gtk-virtual-slots-metaclass> (<class>)
  ())


(define-method compute-get-n-set ((class <gtk-virtual-slots-metaclass>) slot)
  (case (slot-definition-allocation slot)
    ((:gtk-arg)
       (let ((gtk-name (make-keyword (key-get (cdr slot) :gtk-name 
					      (slot-definition-name slot))))
	     (s-name   (key-get (cdr slot) :gtk-widget 'wid)))
	 (list (lambda (o)   (%gtk-arg-get  (slot-ref o s-name) gtk-name))
	       (lambda (o v) (%gtk-arg-set! (slot-ref o s-name) gtk-name v)))))
    ((:gtk-signal)
       (let* ((str    (key-get (cdr slot)
			       :signal-name
			       (symbol->string (slot-definition-name slot))))
	      (name   (string->symbol str))
	      (key    (make-keyword name))
	      (widget (key-get (cdr slot) :gtk-widget 'wid)))
	 (list
	    (lambda (o) (%widget-plist (slot-ref o 'wid) key))
	    (lambda (o v)
	      (let ((old (%widget-plist (slot-ref o 'wid) key))
		    (who (slot-ref o widget)))
		;; Eventually disconnect old command handler
		(when (and old (not (eq? old (void))))
		  (delete-callback! who name old))
		;; Connect new handler (if not #f)
		(when v
		  (install-callback! who name v))
		;; Retain the new handler in widget plist
		(%widget-plist (slot-ref o 'wid) key v))))))
	 
;//    ((:gtk-propagated)
;//     	 (let ((prop   	  (key-get (cdr slot) :propagate-to #f))
;//	       (s-name     (slot-definition-name slot))
;//	       (the-reader (lambda (s default)
;//			     (let ((s (if (pair? s) s (list s default))))
;//			       `(slot-ref (slot-ref o ',(car s)) ',(cadr s)))))
;//	       (the-writer (lambda (s default)
;//			     (let ((s (if (pair? s) s (list s default))))
;//			       `(slot-set! (slot-ref o ',(car s)) ',(cadr s) v)))))
;//	   (unless prop 
;//	     (error "propagation not specified for slot ~s" s-name))
;//	   (unless (pair? prop) 
;//	     (error "bad propagation list for slot ~s" s-name))
;//	   
;//	   (list 
;//	    (make-closure `(lambda (o) ,(the-reader (car prop) s-name)))
;//	    (make-closure `(lambda (o v)
;//			     ,@(map (lambda (item) (the-writer item s-name)) 
;//				    prop))))))
    (else 
       (next-method))))


;;;; ======================================================================
;;;;
;;;; 			The class given to the destroyed widgets
;;;;
;;;; ======================================================================
(define-class <destroyed-object> () ())


;;;;======================================================================
;;;; 
;;;; 		   B a s i c   A b s t r a c t   C l a s s e s 
;;;; 
;;;;======================================================================

(include "object.stk")
(include "widget.stk")



;======================================================================
(define-class <gtk-misc> (<gtk-widget>)
  ((x-align	:accessor     x-align
		:init-keyword :x-align
		:allocation   :gtk-arg
		:gtk-name     "xalign")
   (y-align	:accessor     y-align
		:init-keyword :y-align
		:allocation   :gtk-arg
		:gtk-name     "yalign")
   (x-pad	:accessor     x-pad
		:init-keyword :x-pad
		:allocation   :gtk-arg
		:gtk-name     "xpad")
   (y-pad	:accessor     y-pad
		:init-keyword :y-pad
		:allocation   :gtk-arg
		:gtk-name     "ypad")))


;======================================================================
;; The virtual accessors used for adjustments  use the primitive %adj-ctrl.
;; This primitive accesses different values of an adjustment with an index. 
;; To make things faster, this index is a small integer with the following 
;; conventions:
;;    0: to access the lower bound of the scale (from)
;;    1: for the upper bound			(to)
;;    2: for the value				(value)
;;    3: for the step increment			(increment)
;;    4: for the page increment			(big-increment)
;;    5: for the page size 			(unused for now)
;;
;; Note that the adjustment widget must be present in a slot named %adj

(define-class <gtk-adjustement> (<gtk-object>)
  ((%adj)	  ;; reference on the adjustment object
   (from	  :accessor	from
		  :init-keyword	:from
		  :allocation	:virtual
		  :slot-ref	(lambda (o)
				  (%adj-ctrl (slot-ref o '%adj) 0))
		  :slot-set!	(lambda (o v)
				  (%adj-ctrl (slot-ref o '%adj) 0 v)))
   (to		  :accessor	to
		  :init-keyword	:to
		  :allocation	:virtual
		  :slot-ref	(lambda (o)
				  (%adj-ctrl (slot-ref o '%adj) 1))
		  :slot-set!	(lambda (o v)
				 (%adj-ctrl (slot-ref o '%adj) 1 v)))
   (value	  :accessor	value
		  :init-keyword	:value
		  :allocation	:virtual
		  :slot-ref	(lambda (o)
				  (%adj-ctrl (slot-ref o '%adj) 2))
		  :slot-set!	(lambda (o v)
				  (%adj-ctrl (slot-ref o '%adj) 2 v)))
   (increment	  :accessor	increment
		  :init-keyword	:increment
		  :allocation	:virtual
		  :slot-ref	(lambda (o)
				  (%adj-ctrl (slot-ref o '%adj) 3))
		  :slot-set!	(lambda (o v)
				  (%adj-ctrl (slot-ref o '%adj) 3 v)))
   (big-increment :accessor	big-increment
		  :init-keyword	:big-increment
		  :allocation	:virtual
		  :slot-ref	(lambda (o)
				  (%adj-ctrl (slot-ref o '%adj) 4))
		  :slot-set!	(lambda (o v)
				  (%adj-ctrl (slot-ref o '%adj) 4 v)))))

;;;; ======================================================================
;;;;
;;;; 		   < g t k - e d i t a b l e >   w i d g e t
;;;;
;;;; ======================================================================
(define-class <gtk-editable> (<gtk-widget>)
  (
;;// FIXME:
;;// text-position has been deleted since it is confusing with text-point
;;// Wait the new text-widget to decide what to do with it/
;;//   (text-position	:accessor	text-position
;;//			:init-keyword	:text-position
;;//			:allocation	:gtk-arg
;;//			:gtk-name	"text_position")
   (value		:accessor	value
			:init-keyword	:value
			:allocation	:virtual
			:slot-ref	(lambda (o)
					  (%gtk-editable-get-or-set 
					       (slot-ref o 'wid)))
			:slot-set!	(lambda (o v)
					  (%gtk-editable-get-or-set 
					       (slot-ref o 'wid) v)))))


;LocalWords:  tooltips wid