home *** CD-ROM | disk | FTP | other *** search
Wrap
;;;; ---------------------------------------------------------------------- ;;;; PURPOSE: color palette (demo for drag&drop facilities) ;;;; ;;;; AUTHOR: Michael J. McLennan Phone: (215)770-2842 ;;;; AT&T Bell Laboratories E-mail: aluxpo!mmc@att.com ;;;; ;;;; Rewritten for STk by Erick Gallesio ;;;; Creation date: 6-Jul-1994 09:53 ;;;; Last file update: 27-Dec-1995 00:06 ;;;; ---------------------------------------------------------------------- ;;;; Copyright (c) 1993 AT&T All Rights Reserved ;;;; ====================================================================== (set! *load-path* (cons ".." *load-path*)) (require "blt") (require "dd-protocol") (define DragDrop (make-hash-table)) (define Red 0) (define Green 0) (define Blue 0) ;;;; ---------------------------------------------------------------------- ;;;; Routines for packaging token windows... ;;;; ---------------------------------------------------------------------- (define (package-color color win) (when (null? (winfo 'children win)) (pack (label (& win ".label") :text "Color")) :side "top") (let* ((rgb (winfo 'rgb *root* color)) (r (quotient (car rgb) 256)) (g (quotient (cadr rgb) 256)) (b (quotient (caddr rgb) 256))) (tk-set! (string->widget (& win ".label")) :background color) (tk-set! (string->widget (& win ".label")) :foreground (if (> (+ r g b) 384) "black" "white"))) color) (define (package-number num win) (when (null? (winfo 'children win)) (pack (label (& win ".label") :text "")) :side "top") (tk-set! (string->widget (& win ".label")) :text (format #f "Number: ~A" (* num 1))) num) (define (package-text text win) (when (null? (winfo 'children win)) (pack (label (& win ".label") :text "" :width 30)) :side "top") (tk-set! (string->widget (& win ".label")) :text (format #f "Text: ~A" text)) text) ;;;; ---------------------------------------------------------------------- ;;;; Actions to handle color data... ;;;; ---------------------------------------------------------------------- (define (hexa n) (string-append (number->string (quotient n 16) 16) (number->string (modulo n 16) 16))) (define (set-color . args) (let ((rgb (winfo 'rgb *root* (hash-table-get DragDrop 'color "")))) (if (or (null? args) (eq? (car args) 'red)) (set-red (quotient (car rgb) 256))) (if (or (null? args) (eq? args 'green)) (set-green (quotient (cadr rgb) 256))) (if (or (null? args) (eq? args 'blue)) (set-blue (quotient (caddr rgb) 256))))) (define (bg-color win) (when (widget? win) (let* ((rgb (winfo 'rgb *root* (hash-table-get DragDrop 'color ""))) (newR (quotient (car rgb) 256)) (newG (quotient (cadr rgb) 256)) (newB (quotient (caddr rgb) 256)) (actR (- newR 20)) (actG (- newG 20)) (actB (- newB 20)) (ncolor (string-append "#" (hexa newR) (hexa newG) (hexa newB))) (acolor (string-append "#" (hexa actR) (hexa actG) (hexa actB))) (children (winfo 'children win)) (win-name (widget->string win))) (if (and (not (string-find? "sample" win-name)) (not (string=? win-name "*root*"))) (catch (begin (tk-set! win :background ncolor) (tk-set! win :activebackground acolor)))) (for-each (lambda (x) (if (winfo 'exists x) (bg-color x))) (if (list? children) children (list children)))))) (define (fg-color win) (when (widget? win) (let* ((rgb (winfo 'rgb *root* (hash-table-get DragDrop 'color ""))) (newR (quotient (car rgb) 256)) (newG (quotient (cadr rgb) 256)) (newB (quotient (caddr rgb) 256)) (actR (- newR 20)) (actG (- newG 20)) (actB (- newB 20)) (ncolor (string-append "#" (hexa newR) (hexa newG) (hexa newB))) (acolor (string-append "#" (hexa actR) (hexa actG) (hexa actB))) (children (winfo 'children win)) (win-name (widget->string win))) (if (and (not (string-find? "sample" win-name)) (not (string=? win-name "*root*"))) (catch (begin (tk-set! win :foreground ncolor) (tk-set! win :activeforeground acolor)))) (for-each (lambda (x) (if (winfo 'exists x) (fg-color x))) (if (list? children) children (list children)))))) ;;;; ---------------------------------------------------------------------- ;;;; Setting color samples... ;;;; ---------------------------------------------------------------------- (define (update-main-sample) (let ((color (string-append "#" (hexa Red) (hexa Green) (hexa Blue)))) (tk-set! .sample :background color) (tk-set! .sample :foreground (if (> (+ Red Green Blue) 384) "black" "white")))) (define (set-red val) (set! Red val) (.red.cntl 'set val) (tk-set! .red.sample :background (string-append "#" (hexa val) "0000")) (update-main-sample)) (define (set-green val) (set! Green val) (.green.cntl 'set val) (tk-set! .green.sample :background (string-append "#00" (hexa val) "00")) (update-main-sample)) (define (set-blue val) (set! Blue val) (.blue.cntl 'set val) (tk-set! .blue.sample :background (string-append "#0000" (hexa val))) (update-main-sample)) ;;;; ---------------------------------------------------------------------- ;;;; Main application window... ;;;; ---------------------------------------------------------------------- (label '.sample :text "Color" :borderwidth 3 :relief 'raised) (blt_drag&drop 'source .sample 'config :packagecmd (lambda (w) (package-color (format #f "#~A~A~A" (hexa Red) (hexa Green) (hexa Blue)) w))) (blt_drag&drop 'source .sample 'handler 'color 'dd-send-color) (blt_drag&drop 'target .sample 'handler 'color 'set-color) (message '.explanation :font "-Adobe-times-medium-r-normal--*-120*" :aspect 200 :text "Press the third mouse button over a slider or a color sample and drag the token window around. When the token becomes raised, it is over a target window. Release the mouse button to drop the token and transfer information. If the transfer fails, a \"no\" symbol is drawn on the token window. Try the following: - Drop a number from one slider onto another - Drop a color sample onto the Foreground/Background targets - Drop one of the slider color samples onto the main sample - Drop tokens from one palette application onto another") ;;;; ;;;; Color value entry... ;;;; (frame '.value :borderwidth 3) (label '.value.l :text "Color Value:") (entry '.value.e :borderwidth 2 :relief "sunken" :bg "white") (pack .value.l :side "left") (pack .value.e :side "left" :expand #t :fill 'x) (blt_drag&drop 'source .value.e 'config :packagecmd (lambda (w) (package-color (.value.e 'get) w))) (blt_drag&drop 'source .value.e 'handler 'color 'dd-send-color) (blt_drag&drop 'target .value.e 'handler 'number '(begin (.value.e 'delete 0 'end) (.value.e 'insert 0 (hash-table-get DragDrop 'number))) 'color '(begin (.value.e 'delete 0 'end) (.value.e 'insert 0 (hash-table-get DragDrop 'color)))) (bind .value.e "<Key-Return>" '(hash-table-put! DragDrop 'color (.value.e 'get))) ;;;; ;;;; Red slider... ;;;; (frame '.red :borderwidth 3 :relief "raised") (scale '.red.cntl :label "Red" :orient "horiz" :from 0 :to 255 :command 'set-red) (frame '.red.sample :width 20 :height 20 :borderwidth 3 :relief "raised") (pack .red.cntl :side "left" :expand #t :fill 'x) (pack .red.sample :side "right" :fill 'y) (blt_drag&drop 'source '.red.sample 'config :packagecmd (lambda (w) (package-color (format #f "#~A0000" (hexa Red)) w))) (blt_drag&drop 'source '.red.sample 'handler 'color 'dd-send-color) (blt_drag&drop 'target '.red.sample 'handler 'number '(set-red (hash-table-get DragDrop 'number)) 'color '(set-color 'red)) (blt_drag&drop 'source '.red.cntl 'config :packagecmd (lambda (w) (package-number [.red.cntl 'get] w))) (blt_drag&drop 'source '.red.cntl 'handler 'number 'dd-send-number) (blt_drag&drop 'target .red.cntl 'handler 'number '(set-red (hash-table-get DragDrop 'number)) 'color '(set-color 'red)) ;;;; ;;;; Green slider... ;;;; (frame '.green :borderwidth 3 :relief "raised") (scale '.green.cntl :label "Green" :orient "horiz" :from 0 :to 255 :command 'set-green) (frame '.green.sample :width 20 :height 20 :borderwidth 3 :relief "raised") (pack .green.cntl :side "left" :expand #t :fill 'x) (pack .green.sample :side "right" :fill 'y) (blt_drag&drop 'source '.green.sample 'config :packagecmd (lambda (w) (package-color (format #f "#00~A00" (hexa Green)) w))) (blt_drag&drop 'source '.green.sample 'handler 'color 'dd-send-color) (blt_drag&drop 'target '.green.sample 'handler 'number '(set-green (hash-table-get DragDrop 'number)) 'color '(set-color 'green)) (blt_drag&drop 'source '.green.cntl 'config :packagecmd (lambda (w) (package-number [.green.cntl 'get] w))) (blt_drag&drop 'source '.green.cntl 'handler 'number 'dd-send-number) (blt_drag&drop 'target .green.cntl 'handler 'number '(set-green (hash-table-get DragDrop 'number)) 'color '(set-color 'green)) ;;;; ;;;; Blue slider... ;;;; (frame '.blue :borderwidth 3 :relief "raised") (scale '.blue.cntl :label "Blue" :orient "horiz" :from 0 :to 255 :command 'set-blue) (frame '.blue.sample :width 20 :height 20 :borderwidth 3 :relief "raised") (pack .blue.cntl :side "left" :expand #t :fill 'x) (pack .blue.sample :side "right" :fill 'y) (blt_drag&drop 'source '.blue.sample 'config :packagecmd (lambda (w) (package-color (format #f "#0000~A" (hexa Blue)) w))) (blt_drag&drop 'source '.blue.sample 'handler 'color 'dd-send-color) (blt_drag&drop 'target '.blue.sample 'handler 'number '(set-blue (hash-table-get DragDrop 'number)) 'color '(set-color 'blue)) (blt_drag&drop 'source '.blue.cntl 'config :packagecmd (lambda (w) (package-number [.blue.cntl 'get] w))) (blt_drag&drop 'source '.blue.cntl 'handler 'number 'dd-send-number) (blt_drag&drop 'target .blue.cntl 'handler 'number '(set-blue (hash-table-get DragDrop 'number)) 'color '(set-color 'blue)) ;;;; ;;;; Foreground/Background color inputs... ;;;; (frame '.inputs) (label '.inputs.bg :text "Background" :borderwidth 3 :relief 'groove) (label '.inputs.fg :text "Foreground" :borderwidth 3 :relief 'groove) (button '.inputs.quit :text "Quit" :borderwidth 3 :command "exit") (blt_drag&drop 'target .inputs.bg 'handler 'color '(bg-color *root*)) (blt_drag&drop 'target .inputs.fg 'handler 'color '(fg-color *root*)) (pack .inputs.fg .inputs.bg :side "left" :padx 5 :pady 5) (pack .inputs.quit :side "right" :padx 5 :pady 5) (pack 'append *root* .sample "top expand fillx filly" .explanation "top expand fillx filly" .value "top fillx" .red "top fill" .green "top fill" .blue "top fill" .inputs "top fillx") (wm 'minsize *root* 200 200) (wm 'maxsize *root* 1000 1000) (set-red 0) (set-green 0) (set-blue 0)