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

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Robertel
Posts: 10
Joined: Sat Nov 02, 2013 6:21 am

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

Post by Robertel » Fri Dec 13, 2013 5:00 am

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
Last edited by Robertel on Sat Dec 14, 2013 12:14 am, edited 1 time in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: constructing a text tool palette--seeking advice

Post by FourthWorld » Fri Dec 13, 2013 6:30 am

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: constructing a text tool palette--seeking advice

Post by MaxV » Fri Dec 13, 2013 11:30 am

I think that you don't need to indicate card when it's the current shown card of that stack.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Robertel
Posts: 10
Joined: Sat Nov 02, 2013 6:21 am

Re: constructing a text tool palette--seeking advice

Post by Robertel » Fri Dec 13, 2013 6:15 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: constructing a text tool palette--seeking advice

Post by FourthWorld » Fri Dec 13, 2013 7:27 pm

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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Robertel
Posts: 10
Joined: Sat Nov 02, 2013 6:21 am

Re: constructing a text tool palette--seeking advice

Post by Robertel » Fri Dec 13, 2013 7:44 pm

Richard,
Now that's one swell foop! Exactly what I was hoping for. Many thanks!

Robert

Post Reply