drag & drop external objects

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: heatherlaine, Klaus, FourthWorld, kevinmiller

Post Reply
stanier25
Posts: 6
Joined: Sun Nov 14, 2010 4:59 pm

drag & drop external objects

Post by stanier25 » Tue May 15, 2018 12:39 pm

Hi I'm struggling with getting drag & drop to work. I'm building an app for OSX and need it to be able to handle multiple types of content dragged onto it from external sources - selected text, files, emails etc.

I can do this successfully using an unlocked field and can handle the dragdrop message and get the drag data.

However, I can't seem to do this with a locked field or any other object type (ideally I'd like to do it when content is dragged onto any part of the card).

When dragging items from the finder onto my app, dragging over an unlocked field results in the mouse pointer displaying a "+" sign, whereas dragging over a locked field or anywhere else results in the pointer displaying a no entry sign suggesting the finder/OSX doesn't recognise that area of the screen as somewhere that can receive drags.

The Livecode dictionary suggests you can do this with objects other than unlocked fields and states:

"To accept drops to a locked field or a control other than a field, handle the dragDrop message, using the dragData property to determine what data is being dragged"

...but no dragdrop message appears to be being sent to any object other than an unlocked field.

Any ideas what I might be doing wrong?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5383
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: drag & drop external objects

Post by dunbarx » Tue May 15, 2018 2:00 pm

Hi.

One kluge might be to set the lockText of the field of interest to "false" upon mouseEnter, and reset it on mouseLeave.

Craig Newman

Klaus
Posts: 10101
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: drag & drop external objects

Post by Klaus » Tue May 15, 2018 2:29 pm

Hi stanier25,

you are not doing anything wrong!

LC fully supports "Drag'n'Drop", but only drag and drop of TEXT is handled automatically, that's why you see the CROSS cursor when dragging the mouse over an editable field in LC. Drag some selected text form any text editor onto a field in LC and drop it!

All other D'n'D functionality needs to be scripted in LC. Examples:
To accept drag and drop in the complete stach on all objects, add this to the stack script:

Code: Select all

on dragenter
   set the dragaction to "copy"
end dragenter
This takes care of changing the cursor to CROSS (drag/drop) when over ANY object in the stack.
If you only want that a specific LC object accepts D'n'D, do not put this into the stack script but into the script of that namely object. Same for "dragdrop", see below.

Now you also need to take care of the "dragdrop" message, which is sent when the user, well, finally DROPS something in LC. You could check -> the dragdata["files"] and decide what to do with the dropped files.


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5383
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: drag & drop external objects

Post by dunbarx » Tue May 15, 2018 3:02 pm

Klaus.

I rarely drag or drop anything. But I tried to drag some text from this very forum over to a field in a LC card, and with my kluge, all works fine, locked or unlocked.

But with a dragEnter handler in the card script I cannot get the drag to "take". It does not matter what the insides of the dragEnter handler are.

The cursor does indeed change to a "cross", but the text does not drop.

Without that handler, in an unlocked field, all is well.

I am using the commandKey to allow drag from the external application. Is that an issue?

Craig

Klaus
Posts: 10101
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: drag & drop external objects

Post by Klaus » Tue May 15, 2018 3:15 pm

When scritping "on dragenter" in the stack (or wherever), you OVERWRITE the automatism of LC and thus have to take action by yourself, even when just dropping some text!

Holding the CMD-key should not do anything harmful.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5383
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: drag & drop external objects

Post by dunbarx » Tue May 15, 2018 3:33 pm

Klaus.
When scritping "on dragenter" in the stack (or wherever), you OVERWRITE the automatism of LC and thus have to take action by yourself, even when just dropping some text!
I get that. I used your example. It prevents the drop.

Craig

Klaus
Posts: 10101
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: drag & drop external objects

Post by Klaus » Tue May 15, 2018 4:13 pm

dunbarx wrote:
Tue May 15, 2018 3:33 pm
When scritping "on dragenter" in the stack (or wherever), you OVERWRITE the automatism of LC and thus have to take action by yourself, even when just dropping some text!
I get that. I used your example. It prevents the drop.
It actually does not prevent the drop, it just doesn't handle it! 8)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5383
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: drag & drop external objects

Post by dunbarx » Tue May 15, 2018 4:27 pm

Hmmm.

So without a dragEnter handler of any kind, I can drag text into an unlocked field. But with a dragEnter handler of any kind, I cannot.

I tried the example handler in the dictionary:

Code: Select all

on dragEnter -- show a green outline around the drop target
  set the borderColor of the target to "green"
end dragEnter
and what do you think? Nothing happens.

This is an odd way to think about:
It actually does not prevent the drop, it just doesn't handle it!
For a newbie like me, can you post a simple example of a dragEnter hander that does indeed allow me to drop text into a field? And any other functionality that might ride along would be a bonus.

Craig

Klaus
Posts: 10101
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: drag & drop external objects

Post by Klaus » Tue May 15, 2018 4:43 pm

Hi Craig,

uncheck "3d" for the field and the handler will work! ;-)
Use something like this in the STACK or CARD script to handle the "former automatism" of text dropping in LC:

Code: Select all

on dragenter
   set the dragaction to "copy"
   set the borderColor of the target to green
end dragenter

on dragdrop
   if the dragdata["text"] <> EMPTY then
      set the text of the target to the dragdata["text"]
   end if
end dragdrop
You can of course finetune this ad infinitum, check if is a field at all, if there is already text in the field etc., but this is it in general...


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5383
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: drag & drop external objects

Post by dunbarx » Tue May 15, 2018 7:00 pm

Klaus.

OK, thanks. I see. Like you said, you have to handle it.

What threw me was that without any "drag"-like handler at all, the process worked just fine.

Craig

stanier25
Posts: 6
Joined: Sun Nov 14, 2010 4:59 pm

Re: drag & drop external objects

Post by stanier25 » Tue May 15, 2018 7:27 pm

Many thanks for such quick responses.

Setting the drag action in the stack script did, indeed, solve the issue and I can now happily drag external content onto objects other than unlocked field. Interestingly, the one object that doesn't respond is the card itself but that doesn't matter as you can simply use a graphic or locked field to cover the whole of the card area to get the same effect.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”