[plt-scheme] Text field font troubles

From: Sky O'Mara (skyo at ccs.neu.edu)
Date: Sat Aug 9 15:07:38 EDT 2008

This is what I came up with to make use of change-style:

(define (insert-red-text str ed)
   (let ([red-delta (send (make-object style-delta%) set-delta- 
foreground "Red")]
         [end-before (send ed last-position)]) ;; the end of the text  
before inserting str
     (send ed insert str end-before)
     (send ed change-style red-delta end-before (send ed last- 
position))))

I hope this is a useful example. I agree that the documentation for  
the gui leaves much to be desired.

- Sky O.

On Aug 8, 2008, at 5:52 PM, praimon wrote:

> Hi,
> I'm just beginning to learn about gui, so the hint didn't help
> and I had to resort to the much more convoluted:
>
> (define (insert-red-text str ed)
>  (let* ([style (make-object style-list%)]
>         [delta (make-object style-delta%)]
>         [basic (send style basic-style)])
>    (send ed set-style-list style)
>    (send delta set-delta-foreground "Red")
>    (send ed change-style
>          (send style find-or-create-style basic delta))
>    (send ed insert str)))
>
> If I saw the intended solution, that might also answer another
> question of mine: When dealing with text, what are the
> advantages of using snips (which I don't really understand)
> in place of strings (which get automatically converted to snips
> anyway).
>
> The lack of worked-out examples in the gui manual makes
> it a difficult learning experience, so all examples presented
> here are much appreciated.
> regards,
> praimon
>
>
> On Thu, Aug 7, 2008 at 3:24 PM, Robby Findler  
> <robby at cs.uchicago.edu> wrote:
>> I think you want the change-style method of the text (using the
>> style-delta as an argument to that). See also begin-edit-sequence and
>> end-edit-sequence.
>>
>> (If that's not enough help, I can adjust your program below to get  
>> you started.)
>>
>> Robby
>>
>> On Thu, Aug 7, 2008 at 11:59 AM, Sky O'Mara <skyo at ccs.neu.edu> wrote:
>>> 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.
>>> _________________________________________________
>>> For list-related administrative tasks:
>>> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>>
>>>
>> _________________________________________________
>> For list-related administrative tasks:
>> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.