ClickItem challenge
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
The clicked item
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.
Is there a command or function that does this?
Thank you very much.
Re: The clicked item
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:
Do you see how this works? See why the "+ 1" is a good idea?
Craig
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
Craig
Re: The clicked item
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.
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.
-
- Livecode Opensource Backer
- Posts: 10115
- Joined: Fri Feb 19, 2010 10:17 am
Re: The clicked item
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
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

-
- Livecode Opensource Backer
- Posts: 10115
- Joined: Fri Feb 19, 2010 10:17 am
Re: The clicked item
Ouch: this does not work either:
pseudo code
Also: clickChunk always gives you the whole line of a listField you have clicked on.
pseudo code
Code: Select all
put the trueword from char 36 of field 1 into TEKST
-
- Livecode Opensource Backer
- Posts: 10115
- Joined: Fri Feb 19, 2010 10:17 am
Re: The clicked item
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
Because ALL that does is deliver the text of the line clicked.
Off for a Boozy Bath: Happy Christmas to everyone.
-
- Attachments
-
- Clicketty Click.livecode.zip
- Here's the stack.
- (1.23 KiB) Downloaded 236 times
Re: The clicked item
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
Re: The clicked item
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)
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)
Re: The clicked item
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:
Craig
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
Re: The clicked item
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
HyperActive Software | http://www.hyperactivesw.com
Re: The clicked item
This kind of thread is the sort of thing I live for.
Craig
Craig
Re: The clicked item
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
HyperActive Software | http://www.hyperactivesw.com
Re: The clicked item
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.
Re: The clicked item
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
Re: The clicked item
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.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.
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