Select chunk in text field

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Select chunk in text field

Post by jacque » Fri Aug 10, 2012 7:12 pm

Marek,

The "get" command puts the value into the universal variable "it". In this case, the script gets the value of the the selectedchunk, so "it" contains "word x to y of field z". Using "it" is just shorter to type than this:

if word 4 of the selectedchunk > word 2 of the selectedchunk then...

Using "it" also saves the engine from having to retrieve the value twice. You wouldn't need to use "it", you could use a normal variable instead:

put the selectedchunk into tSel
if word 4 of tSel > word 2 of tSel then...

Mark is correct that my first handler didn't allow for partial word selections. To do that, we need to track the timing of the user selection. If the selection changes within the double-click interval, then it is a double click. This should work:

Code: Select all

local sClickTime

on selectionchanged
  if (the milliseconds - sClickTime) <= the doubleclickinterval then -- it's a double click
    get the selectedchunk
    if word 4 of it > word 2 of it then
      get the number of words in char 1 to (word 2 of the selectedchunk) of me
      select word it of me
    end if
  end if
  put the milliseconds into sClickTime
end selectionchanged
This handler keeps track of the time of the last user click. If the next click is within the time span of a double-click interval then it selects the entire word. Otherwise it does not interfere.

This handler will assume there has been a double-click the first time it is used if you do not initialize sClickTime when the field opens. You should probably add an openField handler that puts the milliseconds into sClickTime.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Select chunk in text field

Post by Mark » Fri Aug 10, 2012 7:42 pm

Jacque,

I really like this solution, very elegant, but unfortunately it breaks triple-clicks, which are supposed to select the entire line. Also, if you click on one word and doubleClick just a little too quickly after your first click but too late for a triple click, the first word of the clickLine is seleected instead of the clickWord. I also noticed that your script doesn't work at all if I doubleClick on the w of śmióaśw.

I was playing with your script to see if there is a way to find out how quickly after a doubleClick the third click followed but it takes me too much time right now (am very busy). This would allow you to select lines by triple clicking on them and the "w" would be the only remaining issue. Maybe you can think of a way to do it.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Select chunk in text field

Post by jacque » Mon Sep 03, 2012 7:15 pm

Just an update. After we had this discussion I reported the bug in the QCC. It was fixed quickly and should work correctly in the next release, which is due out very soon. Unicode editing should work as you'd expect now.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Select chunk in text field

Post by snm » Mon Sep 03, 2012 10:51 pm

Good news, thanks Jacque

Marek

Post Reply