Setting more than 1 textStyle

Cubist at aol.com Cubist at aol.com
Sat Jan 1 15:49:05 EST 2005


   A number of "setting multiple text-styles at once" solutions have been 
posted to the list, but there's a problem in all of them. Namely, text-styles are 
supposed to be a toggleable thing. If you select Bold for plain text, it gets 
bolded; but if you select Bold for text that *is* bold, it turns plain again! 
And there's the whole problem of "when the text-style of the selected text is 
'mixed', what do I do?"...

on ChangeStyle SelText,NuStyle
  # SelText is the selected text -- char 5 to 29 of field "Fred" or whatever
  # NuStyle is the newly-selected text style that the user wants to put on 
that text

  if NuStyle = "plain" then
    # nuke it all
    set the textStyle of SelText to "plain"
    exit to top
  end if

  put the textStyle of SelText into OldStyle
  if OldStyle = "mixed" then
    # this bit will work, but its obvious goal is problematic...
    set the textStyle of SelText to NuStyle
  else
    if the number of items in OldStyle = 1 then
      # only one text-style, huzzah!
      switch OldStyle
      case NuStyle
        # user has selected the existing style -- toggle it
        set the textStyle of SelText to "plain"
        break
      case "plain"
        # it's virgin text
        set the textStyle of SelText to NuStyle
        break
      default
        # the user has selected a different style than is there -- toggle it!
        set the textStyle of SelText to (OldStyle & "," & NuStyle)
      end switch
    else
      # okay, multiple text-styles
      # is NuStyle one of the text-styles that's already on the selected text?
      put 0 into Kounter
      repeat for each item II in OldStyle
        add 1 to Kounter
        if II = NuStyle then exit repeat
      end repeat

      if Kounter > 0 then
        # the newly-selected style is already there -- turn it off!
        delete item Kounter of OldStyle
        set the textStyle of SelText to OldStyle
      else
        # the newly-selected style is, well, NEW -- turn it on
        set the textStyle of SelText to (OldStyle & "," & NuStyle)
      end if
    end if
  end if
end ChangeStyle

   I am not happy with the bit that handles "mixed" text-style; it just plows 
everything under, burying it all beneath the newly-selected style. Sure, it 
works and it's obvious, but... it's not what you'd call elegant, okay?
  What should happen in situations like when *all* of the selected text is 
Italic, but some of the selected text has Bold *in addition to* Italic? At 
minimum, it should check to see what styles apply to *all* the selected text, and 
not eliminate any whole-selection styles that the user didn't select... (pause 
to let readers parse that sentence) ...but what happens when the user selects 
Italic, and (let us say) 15% of the selected text is Italic? Do you make every 
character Italic (in addition to whatever other text-styles whichever 
character might have), do you remove Italic from whichever characters have it, or 
what?
   My own first impulse is to go with a "50% rule" -- count the text-styles 
of individual characters, and if more than 50% of the selected text has a given 
style, treat it as tho the whole thing has it; otherwise, treat it as tho the 
whole thing doesn't have that style. What do the rest of you think?


More information about the use-livecode mailing list