ClickItem challenge

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

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

Re: The clicked item

Post by dunbarx » Sat Dec 26, 2020 7:19 pm

Richmond.

I am sure you actually know this, but the clickChunk does indeed know what the "clickWord" is:

Code: Select all

answer char word 2 of the clickChunk to word 4 of the clickChunk of me
Craig

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

Re: The clicked item

Post by dunbarx » Sat Dec 26, 2020 7:58 pm

The challenge is to find, in a line in a field, which item of "xx,xx,xx,xx,xx,xx,xx" was clicked on.

My earlier optimistic assertion that the clickLoc might work may be in trouble. Not a problem with a fixed-width font like Courier. I can get the clickCharLoc, the formattedWidth of the line, and so know the pixel distance of that char from the left side of the field. In a fixed-width font, I can find the item that char lives in.

But not sure how to handle the far more common dynamic-width fonts. I do not want to have to know the pixel width of each char in such fonts, and am not even sure that would be viable in any case. I am trying to find a way forward using all the chunk gadgets, and location and size properties available in LC.

Hmmm. I may start a new challenge thread. My very first.

Craig

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

ClickItem challenge

Post by dunbarx » Sat Dec 26, 2020 8:03 pm

There is no "clickItem" function in LC.

This came from another thread:
http://forums.livecode.com/viewtopic.php?f=19&t=35159

The challenge is to find, in a line in a field, which item of "xx,xx,xx,xx,xx,xx,xx" was clicked on.

Not a problem with a fixed-width font like Courier. I can get the clickCharLoc, the formattedWidth of the line, and so know the pixel distance of that char from the left side of the field. In a fixed-width font, I can find the item that char lives in.

But not sure how to handle the far more common dynamic-width fonts. I do not want to have to know the pixel width of each char in such fonts, and am not even sure that would be viable in any case. I am trying to find a way forward using the chunk gadgets, and location and size properties available in LC.

Craig

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: ClickItem challenge

Post by jmburnod » Sat Dec 26, 2020 10:25 pm

Hi Craig,
If i understand you correctly, this should work

Code: Select all

on mouseup
   put word 2 of the clickchunk into tStart
   put 0 into tNbItems
   repeat with i = tStart down to 1
      if char i of fld 1 = "," then add 1 to tNbItems
   end repeat
   put tNbItems+1
end mouseup
Best
Jean-Marc
https://alternatic.ch

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

Re: The clicked item

Post by richmond62 » Sat Dec 26, 2020 10:40 pm

I am sure you actually know this
Yes, I do, but it doesn't do a blind bit of good in the OP's case.

If I click on a word in a sentence in the line of a listField ALL I can get back is the line clicked.

So, knowing that is in this case as valuable as knowing that the moon is not made of cheese.

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

Re: ClickItem challenge

Post by FourthWorld » Sat Dec 26, 2020 10:57 pm

Original thread posted in the "MacOS" forum (it's not platform-specific) has been merged with this duplicate thread here. Now people know where to post replies, and future readers can find complete discussions.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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 » Sun Dec 27, 2020 1:06 am

richmond62 wrote:
Sat Dec 26, 2020 10:40 pm
I am sure you actually know this
Yes, I do, but it doesn't do a blind bit of good in the OP's case.

If I click on a word in a sentence in the line of a listField ALL I can get back is the line clicked.

So, knowing that is in this case as valuable as knowing that the moon is not made of cheese.
The handler I posted finds the clicked item in a list field.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: ClickItem challenge

Post by richmond62 » Sun Dec 27, 2020 10:45 am

Aksherlly, I can't help seeing this whole discussion as slightly redundant
as ALL one has to do is deselect the list behavior thing in the field's Prop. palette,
and 'Bingo' the whole thang disappears in a puff of smoke:

Code: Select all

on mouseDown
   put empty into fld "fREZ"
   put ((the clickLine) & " : ") into fld "fREZ"
   put the clickText after fld "fREZ"
end mouseDown
-
listFieldGone.jpg
Attachments
Clicketty Click Toooooo.livecode.zip
Here's the stack.
(1.13 KiB) Downloaded 225 times

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: ClickItem challenge

Post by jmburnod » Sun Dec 27, 2020 11:13 am

A better version of my first try

Code: Select all

on mouseup
   put word 2 of the clickchunk into tStart
   put word 2 of the clickline into tNoLi
   if tStart = empty then exit mouseup
   put 0 into tNbItems
   repeat with i = tStart down to 1
      if char i of fld 1 = cr then exit repeat
      if char i of fld 1 = "," then add 1 to tNbItems
   end repeat
   add 1 to tNbItems
   put "You clicked item" && tNbItems & "=" & item tNbItems of line tNoLi of fld "fText" into fld "fDev"
end mouseup
stGetClickedItem.zip
(1.3 KiB) Downloaded 210 times
Jean-Marc
https://alternatic.ch

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: ClickItem challenge

Post by bn » Sun Dec 27, 2020 11:54 am

This works with locked fields and list fields

Code: Select all

on mouseUp
   put the clickCharChunk into tTemp
   if tTemp is empty then
      put "nothing clicked"
      beep
      exit mouseUp
   end if
   put word 2 of tTemp into tTemp
   put the number of lines of char 1 to tTemp of me into tLineNo
   put the number of chars of line 1 to tLineNo -1 of me into tPrevChars
   if tPrevChars > 0 then add 1 to tPrevChars -- add return at end of line
   put the number of items of char 1 to tTemp - tPrevChars of line tLineNo of me into tItemNo
   put "Item " &  tItemNo & " of line " & tLineNo
end mouseUp
Kind regards
Bernd

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

Re: ClickItem challenge

Post by dunbarx » Sun Dec 27, 2020 6:25 pm

Thanks, Bernd.

You know, I got stuck in my thinking. I should have stepped back a bit and simply counted forward, collecting commas along the way, like you did.

Craig

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

Re: ClickItem challenge

Post by dunbarx » Mon Dec 28, 2020 1:14 am

So with blinders off, thanks to others, I think this finds, within a line, the item number in any kind of field, listField or not:

Code: Select all

on mouseUp
   put word 2 of the clickCharChunk into x
   put word 2 of the clickline into lineClickedOn
   put char 1 to word 2 of the clickCharChunk of me into tText
   put the number of chars of line 1 to (lineClickedOn - 1) of tText + 1 into upperCount
   answer the number of items of char 1 to  x - upperCount of the last line of tText
end mouseUp
A little analysis, or a kick from your buddies, is worth a million lines of code.

Craig

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

Re: ClickItem challenge

Post by Fermin » Sun Jan 03, 2021 2:56 am

Oh, how clueless I am!... Until now I didn't see that there was a second page of comments. The last post I saw, at the end of the first page was: [Post by dunbarx " Fri Dec 25, 2020 4:07 pm]
Thank you very much for all those answers. I have tried the latest code from Craig [Post by dunbarx "Mon Dec 28, 2020 12:14 am] and it seems to work very well. And I see that it basically relies on the 'clickline' and 'clickCharChunk' commands as I did in the code I uploaded in [Post by Fermin " Fri Dec 25, 2020 1:19 am]. But I'm going to investigate Craig's work because I find it more direct and professional.
Thank you, again... And you, 2021, please be good to us!

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

Post Reply