ClickItem challenge

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Fermin
Posts: 152
Joined: Fri Jun 05, 2015 10:44 pm

The clicked item

Post by Fermin » Thu Dec 24, 2020 2:38 am

If a line containing several items is clicked, is there any way to know which one was clicked?
Is there a command or function that does this?
Thank you very much.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10340
Joined: Wed May 06, 2009 2:28 pm

Re: The clicked item

Post by dunbarx » Thu Dec 24, 2020 2:52 am

Hi.

Sure. Several. Check out the "clickText", the "clickChunk", the clickLine, etc. Regarding your actual question, let's say you have a field with lots of items in it. Lock that field and put this into the field script:

Code: Select all

on mouseUp
   answer the number of items of char 1 to (word 2 of the clickChunk) + 1 of me 
end mouseUp
Do you see how this works? See why the "+ 1" is a good idea?

Craig

Fermin
Posts: 152
Joined: Fri Jun 05, 2015 10:44 pm

Re: The clicked item

Post by Fermin » Thu Dec 24, 2020 11:29 am

Thank you, Craig, although I don't think I've made myself clear, for example, I have this text in a field:

Oasis,Wonderwall,19900000,Fsm,1097
Doobie Brothers,Long Train Runnin',19600000,Gm,825
Led Zeppelin,Whole wotta love,19600000,Em,1034
ZZ Top,La Grange,19600000,A,597

So, if I click on the word "Grange" I need LiveCode to tell me that I have clicked on item 2. (I already get the line with clickLine)
That is, find out the position of the item relative to a line, not to the whole field.
... and a cordial greeting and my best wishes for this ('strange') Christmas

PS. I'm going to see if itemOffset helps me at all.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10116
Joined: Fri Feb 19, 2010 10:17 am

Re: The clicked item

Post by richmond62 » Thu Dec 24, 2020 12:02 pm

Screenshot 2020-12-24 at 13.00.18.png
-

Code: Select all

on mouseDown
   put the clickLine into LYNE
   put LYNE into fld "fREZ"
   put the clickChunk into TEKST
   put (": " & TEKST) after fld "fREZ"
end mouseDown
The main problem as far as I can see is that there is no term clickWord in LiveCode.

As you can see, clickChunk does not return the word.

The other problem is that terms such as selected word don't make sense, nor do statements like this:

pseudo code

Code: Select all

put the trueWord of the clickChunk into TEKST
Actually the whole problem devolves to the fact that the LINE is selected rather than an individual WORD in that LINE. :(

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10116
Joined: Fri Feb 19, 2010 10:17 am

Re: The clicked item

Post by richmond62 » Thu Dec 24, 2020 12:12 pm

Ouch: this does not work either:

pseudo code

Code: Select all

put the trueword from char 36 of field 1 into TEKST
Also: clickChunk always gives you the whole line of a listField you have clicked on.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10116
Joined: Fri Feb 19, 2010 10:17 am

Re: The clicked item

Post by richmond62 » Thu Dec 24, 2020 12:36 pm

Code: Select all

on mouseDown
   put empty into fld "fREZ"
   put the clickLine into CLINE
   put fld "fLIST" into HOLDER
   set itemDelimiter to " "
   put the clickChunk into CHOPPER
   put item 2 of CHOPPER into FIRSTCUT
   put item 4 of CHOPPER into SECONDCUT
   repeat (FIRSTCUT - 1) times
      delete char 1 of HOLDER
   end repeat  
   put (SECONDCUT - FIRSTCUT) into SLICER
   repeat SLICER times
      put char 1 of HOLDER after fld "fREZ"
      delete char 1 of HOLDER
   end repeat
end mouseDown
What a load of old cobblers!

Because ALL that does is deliver the text of the line clicked.

Off for a Boozy Bath: Happy Christmas to everyone.
-
BoozyB.jpeg
BoozyB.jpeg (4.59 KiB) Viewed 7746 times
Attachments
Clicketty Click.livecode.zip
Here's the stack.
(1.23 KiB) Downloaded 237 times

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Re: The clicked item

Post by SparkOut » Thu Dec 24, 2020 3:07 pm

Hmm, best kludge I can think of at the moment is

Code: Select all

on mouseUp
   put word 2 of the clickLine into tClickLine
   put the text of me into tText
   replace cr with the itemDelimiter in tText
   put the clickText into tTextLine
   put word 2 of the clickCharChunk into tIdx
   put 0 into tLastStart
   repeat for each item tItem in tText
      put offset (tItem,tText,tLastStart) + tLastStart into tOffset
      if tOffset > 0 then
         if tOffset >= tIdx then
            put char tLastStart to tOffset of tText into tFoundItem
            put item 1 of tFoundItem into tFoundItem
            exit repeat
         else
            put tOffset into tLastStart
         end if         
      end if
   end repeat
   if tFoundItem is not empty then
      put itemOffset (tFoundItem,tTextLine) into tFoundItemNr
      answer "Found" && tFoundItem && "in item" && tFoundItemNr && "of line" && tClickLine
   else
      answer "No clicked item found"
   end if
end mouseUp

Fermin
Posts: 152
Joined: Fri Jun 05, 2015 10:44 pm

Re: The clicked item

Post by Fermin » Thu Dec 24, 2020 5:41 pm

Thanks SparkOut for the work although I think there is some small error, for example when clicking on the extreme characters of a line.
However, I think it is strange that there is not a function or at least a more direct method to find out the position of an item within a line. I have the feeling that I already had this problem long time ago and someone gave me a very interesting solution but maybe I am confused.
In any case, I will continue investigating and, of course, thank you very much for your collaboration.

Translated with www.DeepL.com/Translator (free version)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10340
Joined: Wed May 06, 2009 2:28 pm

Re: The clicked item

Post by dunbarx » Thu Dec 24, 2020 10:52 pm

This seems compact. Does it do what you want?

I took your sample text and put it in a field. This is in the field script:

Code: Select all

on mouseUp
   put the clickText into x
   put word 2 of the clickLine into tLine
  put the value of the clickLine into tText
  put offset(x,tText) into y
  answer "You clicked Item" && the number of items of char 1 to y of tText && "of line" && tLine
end mouseUp
Craig

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

Re: The clicked item

Post by jacque » Thu Dec 24, 2020 11:44 pm

Craig's script works if the field is not a list field. If it is, the clicktext returns the entire line. In that case, this returns both the text of the item clicked and the item number within the clickline. The script is in field 1, the response is put into field 2.

Code: Select all

on mouseUp
  put getItemInLine(word 2 of the clickCharChunk) into fld 2
end mouseUp

function getItemInLine pCharLoc
  put fld 1 into tText
  replace cr with comma in tText
  put the num of items in char 1 to pCharLoc of tText into tItemOffset
  put item tItemOffset of tText into tSelItem -- actual text
  put itemOffset(tSelItem,the text of the clickline) into tItemNum
  return tSelItem,tItemNum
end getItemInLine
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10340
Joined: Wed May 06, 2009 2:28 pm

Re: The clicked item

Post by dunbarx » Fri Dec 25, 2020 12:00 am

This kind of thread is the sort of thing I live for.

Craig

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

Re: The clicked item

Post by jacque » Fri Dec 25, 2020 12:16 am

dunbarx wrote:
Fri Dec 25, 2020 12:00 am
This kind of thread is the sort of thing I live for.
Yeah, they're fun, especially when the rest of the world is occupied elsewhere and you have time to tinker.

(BTW, I'd replace the word "value" with "text" in line 3 of your handler. That way the engine doesn't have to load the compiler.)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Fermin
Posts: 152
Joined: Fri Jun 05, 2015 10:44 pm

Re: The clicked item

Post by Fermin » Fri Dec 25, 2020 1:19 am

Craig's code seems to work except if I click on a space. And if there are identical words or numbers repeated on the same line I don't think you can tell exactly which one you clicked on.
Last edited by Fermin on Fri Dec 25, 2020 1:12 pm, edited 1 time in total.

Fermin
Posts: 152
Joined: Fri Jun 05, 2015 10:44 pm

Re: The clicked item

Post by Fermin » Fri Dec 25, 2020 1:35 am

I have written a code using selections. It's not too brilliant but I think it works for me. It basically consists of calculating the position of the clicked character relative to its line and then counting the commas up to that character from the beginning of the line.

--

Code: Select all

on mouseup
   put "avatar1" into elCampo
   put word 2 of the clickline into lali /* the clicked line */
   --
   select char 1 of line lali of fld elCampo /* It will serve to find out the position of the first character in the clicked line */
   put word 2 of the selectedchunk into charAlfa /* Position of the first character in the line */
   --
   put word 2 of the clickCharChunk into charAbsoluto /* Absolute position in the field of the selected character */
   put charAbsoluto - charAlfa +1 into CharInLine /* Position, relative to the line, of the character clicked */
   -- 
   if CharInLine < 1 then exit to top /* Click on empty. Exit */
   -- 
   repeat with a = 1 to CharInLine /* Count the commas (items) in the line*/
      if char a of line lali of fld elCampo is comma then add 1 to itemN
   end repeat
   --
   put itemN+1 into itemOK /* position of the item on the line */
   answer "line " & lali && " item " & itemOK
end mouseup

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10340
Joined: Wed May 06, 2009 2:28 pm

Re: The clicked item

Post by dunbarx » Fri Dec 25, 2020 4:07 pm

Craig's code seems to work except if I click on a space. And if there are identical words or numbers repeated on the same line I don't think you can tell exactly which one you clicked on.
Hmmm. Clicking on a space gives a "0" as the result of the "offset" function. A cheap way out is to simply inform the user that he has not quite clicked on an actual word.

I bet the "clickLoc" can be exploited to distinguish two identical copies of a word or item in one line. If i can get away from my family today I will experiment.

Craig

Post Reply