[plt-scheme] Changing a snip's style

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Tue Jun 10 00:16:57 EDT 2008

Hi everyone,

I just wanted to make sure I'm understanding the snip style system.  As I 
understand it now, snips really don't get to control their own styles: 
they're really at the mercy of whatever editor they're embedded in.  For 
example, in the following example code:

#lang scheme/base
(require scheme/gui/base

(define f (new frame% [label ""] [width 100] [height 100]))
(define e (new text%))
(define c (new editor-canvas% [parent f] [editor e]))
(send f show #t)

(define a-snip (new editor-snip%))
(send e insert a-snip)

;; Successfully changes the color of the editor snip's border to Green.
(define (t1)
   (define d (new style-delta%))
   (send d set-delta-foreground "Green")
   (send e change-style d 0 'end))

;; Unsuccessfully tries to change the editor snip's border to Red.
(define (t2)
   (define d (new style-delta%))
   (define a-style (send a-snip get-style))
   (send a-style get-delta d)
   (send d set-delta-foreground "Red")
   (send a-style set-delta d)
   (send a-snip set-style a-style))

t1 seems to work, but t2 does not.  I'm assuming that the state of snip 
styles really lives in editors, as the documentation for snip%'s set-style 
has the mysterious sentence: "The snip's style may be changed by the 
system without calling this method."

Is my understanding accurate?  It's really not clear from the GUI 


The documentation should make it more clear that editors will be happy to 
override the style of a snip when the snip gets inserted.  I've had to 
fight the snip style system for a few days, just trying to get the styles 
to stick to snips without touching editors.  After that didn't work, I 
decided to stop fighting and embrace editors, and things are working now.

I've written the following helper function to make it easier for me to 
change snip styles.  Would this be welcome in PLaneT, or is it too simple?

;; change-snip-style: snip% style-delta% -> void
;; Tries to change the delta of a snip that's already inserted into
;; an editor.  If no editor's attached, this has no effect.
(define (change-snip-style a-snip a-delta)
   (when (send a-snip get-admin)
     (let* ([editor (send (send a-snip get-admin) get-editor)])
         [(is-a? editor text%)
          (let* ([start-pos (send editor get-snip-position a-snip)]
                 [end-pos (+ start-pos (send a-snip get-count))])
            (send editor change-style a-delta start-pos end-pos))]
         [(is-a? editor pasteboard%)
          (send editor change-style a-delta a-snip)]))))

Posted on the users mailing list.