Color My Word--Open Color Palette

Cubist at Cubist at
Wed Apr 2 13:20:01 EST 2003

sez bornstein at
>Let's say you have an unlocked text field that you want to allow a user
>to format. Place some buttons next to it labeled "Bold", "Italic" 
>"Plain", etc. You can also provide popups for font and font size. Your
>users simply selects any text that they want to operate on and click the
>buttons or popups.
>The basic code to operate on the selected text looks like this:
>on mouseup
>-- italicize the selected text
>  if the selection is not empty the
>    put the selectedchunk into sc
>    -- see if the text already has some style info
>    put the textstyle of sc into currentStyle
>    if currentStyle is empty then put "plain" into currentstyle
>    -- add italics to any existing style
>    put currentStyle&comma&"italic" into newStyle
>    set the textStyle of sc to newStyle
>  end if
>end mouseup
>This will work fine, even if the text field is not locked.
   Some comments...
   One: If the selected text has no style assigned to it, the textstyle won't 
be empty -- it'll be "plain".
   Two: If the selected text has a mixture of styles -- for instance, if 
three words are selected, and precisely one of the three words is boldfaced 
-- the results may not be what you wanted. Try this out: Put whatever you 
like in a standard text field; apply boldface to word 2 (and *only* word 2) 
of that text; try this code...

  put the textstyle of word 1 to 3 of field "testbed" into Fred
  put ",italic" after Fred
  set the textstyle of word 1 to 3 of field "testbed" to Fred

   ...and watch as the boldfacing on word 2 evaporates. Note that the 
resulting behavior is exactly what you get when you apply a text style from 
the STYLE submenu of the TEXT menu. Perhaps it might be worth someone's while 
to write a style-applying handler that does not display this behavior?
   Three: If the selected text is all the same style, this will work just 
fine regardless of whether that style is "plain" or "bold,underline" or what:

   put "italic" into item (1 + the number of items in TheStyle) of TheStyle

   Fortunately, the MC engine is smart enough that if you set some text to 
the style "plain,italic", it comes out as "italic", so you don't need to 
worry about it.
   Four: In a word processor, if all of the selected text is italics, 
selecting "italics" should *remove* that style from the selected text. This 
code doesn't do that. Again -- perhaps someone might want to write a 
style-applying handler that does things differently?

>Fonts and font sizes are a little different but not very hard. Let me 
>know if you want more details.
   The "mixed styles" thing applies here, too, but you probably *want* fonts 
and sizes to behave this way.

   Hope this helps...

More information about the use-livecode mailing list