Page 1 of 1

constructing a text tool palette--seeking advice[solved]

Posted: Fri Dec 13, 2013 5:00 am
by Robertel
I have a main stack with a field on each card and I would like to selectively modify text style, size, or color of selected text chunks in these fields. I would like to do this using a substack, a text tool palette.

As a first step towards constructing this, I have a field message handler in the main stack that triggers on selectionChange. For example, I might select a word in a field, planning to make it bold. This handler assigns values to two global variables, gCardID and gFieldID, identifying where this selection resides. After making a text selection, I then go to the substack, the text tool palette, and click a button. This button, "Bold", has the following handler:

Code: Select all

global gCardID,gFieldID--the field and card IDs of where selected chunk resides
on mouseUp
   local tSelectedChunk,tStackName
   put quote & the mainstack of this stack & quote into tStackName
   put (the selectedChunk of field ID gFieldID of cd ID gCardID of stack value(tStackName)) into tSelectedChunk
   do "set the textStyle[" & quote & "bold" & quote & "] of "& tSelectedChunk & " of cd ID " & gCardID & " of stack " & tStackName & " to true"
end mouseUp
This works, but seems overwrought, and I suspect there are simpler, more elegant ways to do this? The simpler, more elegant ways I tried didn't work. :D I want to minimize coding in the main stack, so that this text tool palette could be easily employed with other stacks with little effort. Any suggestions appreciated. Thanks.
Robert

Re: constructing a text tool palette--seeking advice

Posted: Fri Dec 13, 2013 6:30 am
by FourthWorld
In 99% of the cases where you find "do" a temptation, there's a simpler, faster, and more robust method.

The key here is our good friend "the long ID". Rather than store both the field and card IDs to construct an absolute reference on the fly, the long ID of an object will give you an absolute reference in one swell foop.

Re: constructing a text tool palette--seeking advice

Posted: Fri Dec 13, 2013 11:30 am
by MaxV
I think that you don't need to indicate card when it's the current shown card of that stack.

Re: constructing a text tool palette--seeking advice

Posted: Fri Dec 13, 2013 6:15 pm
by Robertel
Thanks for the help. I learned something from both comments. I'm still unable to eliminate the "do", however. No doubt a syntax deficiency on my part. I think the difficulty that I'm having boils down to the following
Using Richard's suggestion and switching to the long ID of the field in the main stack where the selected chunk resides, the expression

Code: Select all

the selectedChunk of gLongID 
evaluates to , for example,
char 7 to 11 of field 1
where the evaluated expression only refers to the field and not the main stack where the field is located. Hence, the following expression fails because the selectedChunk can't be found.

Code: Select all

set the textStyle["bold"] of the selectedChunk of gLongID  to true--fails because no stack info
I've tried a number of variations on the above statement in an attempt to include the additional necessary information stored in gLongID, but without success. The only way I've found that works, unfortunately, is a "do" statement similar to that in my original post.

Robert

Re: constructing a text tool palette--seeking advice

Posted: Fri Dec 13, 2013 7:27 pm
by FourthWorld
Remember that a selection can only happen in a window that has focus, so any commands acting on things like the selection should work, e.g.:

Code: Select all

if the selectedChunk is not empty then
    set the textStyle of the selection to "bold"
end if

Re: constructing a text tool palette--seeking advice

Posted: Fri Dec 13, 2013 7:44 pm
by Robertel
Richard,
Now that's one swell foop! Exactly what I was hoping for. Many thanks!

Robert