[plt-scheme] Text field font troubles

From: Sky O'Mara (skyo at ccs.neu.edu)
Date: Thu Aug 7 12:59:22 EDT 2008

Hi,

I am using MrEd to write a program with a GUI that contains a text  
field to send feedback to the user. I would like to be able to insert  
segments of colored text into the text field, but I am running into a  
roadblock because I do not fully understand how styles work.

Here is some example code to demonstrate what I have tried so far. The  
documentation does not seem to make it much clearer than this:

#lang scheme/gui

(define main-window
   (new frame%
        [label "Window"]
        [width 500]
        [height 500]))

(define editor (new text%))

(define canvas
   (new editor-canvas%
        [parent main-window]
        [editor editor]))

;; insert-red-text : String editor<%> -> Void
;; Inserts red text into the editor.
(define (insert-red-text str ed)
   (let* ([snip (make-object string-snip% str)]
          [style (send snip get-style)]
          [delta (make-object style-delta%)]
          [newdelta (send delta set-delta-foreground "Red")])
     (send style set-delta newdelta)
     (send snip set-style style)
     (send ed insert snip)))

(send main-window show #t)

(send editor insert "Regular text...")
(insert-red-text "Red text..." editor)

The idea behind insert-red-text is to create a new snip% object,  
change its style to have a different color, and then insert the snip  
into the editor. I am not having luck getting it to work. The function  
inserts the text, but only in the default font.

Any help would be much appreciated.

- Sky O.


Posted on the users mailing list.