dragdrop question

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
marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am

dragdrop question

Post by marksmithhfx » Mon Nov 21, 2011 1:19 am

Hi, just trying to figure out how I can capture and process text after it is dragged and dropped onto a text field. I created a text field and put a dragdrop handler in it:

on dragdrop
put return & "dragdrop detected" after msg
put the number of lines of field "text" into fld "count"
pass dragdrop
end dragdrop

And, the msg shows up in the msg box indicating detection. However the line count only includes text already in the field, and not the new text just dropped in. Is there a way to detect and process the dropped text?

Thanks... I've attached a simple demo stack showing the problem I am having.

-- Mark
Attachments
test dragdrop.livecode.zip
(1.14 KiB) Downloaded 256 times
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

cbodell
Posts: 61
Joined: Wed Nov 09, 2011 7:27 pm

Re: dragdrop question

Post by cbodell » Mon Nov 21, 2011 3:24 am

Hi Mark,

Firstly, upon dragDrop, you have the option to invoke code before the dragDrop actually takes place. After your code, the engine takes over and completes the process.

Your Code:

Code: Select all

on dragDrop
   put return & "dragdrop detected" after msg
   put the number of lines of fld "text" into fld "count"
   [u]pass dragDrop[/u] <-- Part of your problem
end dragDrop
Your New & Improved Code:

Code: Select all

on dragDrop
   if the dragData["text"] is not empty then put "DragDrop Detected!" <-- Optional Line
   put the dragData["text"] after me
   put the number of lines of me into fld "count"
end dragDrop
Do not add a pass command, because then the engine will add the text to the field when it takes over after passing of the handler.

Hope it Helped!

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am

Re: dragdrop question

Post by marksmithhfx » Mon Nov 21, 2011 5:03 am

cbodell wrote:Hi Mark,

Do not add a pass command, because then the engine will add the text to the field when it takes over after passing of the handler.

Hope it Helped!
Brilliant! Thanks Chris. I was also able to solve another problem I was working on. I wanted the field to be able to handle styled text:

Code: Select all

on dragdrop
   -- put return & "dragdrop detected" after msg -- debugging
   put the htmltext of fld "text" into temp
   put the dragdata["html"] after temp
   -- put return & temp after msg -- debugging
   set the htmltext of fld "text" to  temp
   select after me
   put the number of lines of me into fld "count"
end dragdrop
It works great. Just wondering if there is an easier way to do this (or if this is pretty much the way it is usually done?)
Thanks again,

-- Mark
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

cbodell
Posts: 61
Joined: Wed Nov 09, 2011 7:27 pm

Re: dragdrop question

Post by cbodell » Mon Nov 21, 2011 5:51 am

Your welcome,

Im not sure there is an easier way, that's the way i would go about it.
To save a few words of code, you could refer to the field as "me" instead of fld "text" but it wont speed for efficiency. Just a reminder

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am

Re: dragdrop question

Post by marksmithhfx » Mon Nov 21, 2011 6:11 am

cbodell wrote:Your welcome,

To save a few words of code, you could refer to the field as "me" instead of fld "text" but it wont speed for efficiency. Just a reminder
Yes, I'm still struggling with that one. For some reason it just doesn't come naturally for me :D
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am

Re: dragdrop question

Post by marksmithhfx » Mon Nov 21, 2011 6:24 am

Ok, here's my final "drag styled text into a text field" working version (sometimes we have to take pleasure in small accomplishments :D :

Code: Select all

on dragdrop
   put the htmltext of me into temp
   put the dragdata["html"] after temp
   set the htmltext of me to  temp
   select after me
end dragdrop
After all that I'm tempted to comment "I guess its all about me!" but that would just be a bad joke :D
Thanks for all you help.
-- Mark
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

Post Reply