4D Write Pro Drag and Drop

I want to be able to drag a picture from the desktop and drop it onto a 4D Write Pro document and have the On Drop event trigger so I can prompt the user as to what layer (inline with text, anchored behind text or anchored in front of text) the picture should be placed.

I’ve found that drag and drop in 4D Write Pro only works if the “Automatic Drop” Action checkbox is checked. That is fine and dragging a picture from the desktop and dropping onto the 4D Write Pro document will insert the picture inline with the text at the insertion point selected when dropped. However, the On Drop form event is not triggered (nor is On Begin Drag Over) when “Automatic Drop” action checkbox is checked.

I need to have the “Automatic Drop” and "Automatic Drag” Action checkboxes checked because I want ‘normal’ drag and drop in the 4D Write document to continue to work.

Can I have my cake and eat it too? Have I missed something important?

If not, we will need to train users how to use the 4D Write Pro Contextual Menu to change the picture layer, but a lot of users don’t know to right-click. Alternatively, we can continue to use a picture variable on the form which accepts the drop of the picture. A little clunky, but users are familiar with it.

BTW, the 4D Demo database https://blog.4d.com/insert-pictures-4d-write-pro/ was very helpful in digging through the details pertaining to this issue.

This is under v17R4.

Thanks for any comments.

Tom Benedict

Hello Tom

first of all, thank’s for the expression “Can I have my cake and eat it too?” I did not know it.
In french we say “having the butter and the money for the butter”.

Well, in english as in french, the answer is “no” (sorry).

“Automatic drop” means “let 4d do the job, don’t bother me with events”
Same worth for “automatic drag”.
No magic here; Either you let 4D Do the job either you do it (all of it) by yourself.


BTW, I’ll do my best to give you a small databse sample where events are managed. You’re not the only one asking for it, so i hope I will succeed so i can help you and other guys :slight_smile:

Regards

Roland Lannuzel

ok…here is a more complete answer and i really hope it will help :slight_smile:

First of all, you must disable auto drag and auto drop, but don’t worry, we’ll make it work again easyly with a couple of lines of code. This will allow to manage drag and drop pictures as well. Either from files (D&D from desktop) or variables and fields (direct D&D from 4D other fields)

So we have to manage TWO events :

#1) on begin drag over
#2) on drop

#1: on begin drag over, the only thing you have to do is memorise the selection beeing dragged; This is REALLY easy. You can use “Form” to memorise, or the paste board or a variable, but Form is the most clean way to do it :

<code 4D>
: (Form event=On Begin Drag Over)
// use Form.wpDrag as a pasteboard for what is being dragged from the area
Form.wpDrag:=WP Selection range(*;“WParea”)

</code 4D>

#2) on drop

you must manage three cases

  • drag a picture file from the desktop
  • drag a picture file from a container (var of field)
  • drag text (that may content pict, but you don’t have to care)

<code 4D>
$file:=Get file from pasteboard(1)
If (Is picture file($file))
READ PICTURE FILE($file;$picture)
Else
GET PICTURE FROM PASTEBOARD($picture)
End if

	If (Picture size($picture)#0)
		// manage picture

	Else   // NOT a picture file
		If (Form.wpDrag#Null)
			// manage text
		End if
	End if

</code 4D>

If it’s a picture, insert your own dialog to decide where and how the picture should be inserted in the document.
You will use WP INSERT PICTURE to insert inline picture and WP Add picture to insert a picture in backgroud or foreground. In both cases, WP SET ATTRIBUTES can be used to set sizes, positions and ratios.

If it’s a text just get the drop position

<code 4D>
$rangeDrop:=WP Text range(WParea;Drop position;Drop position)

$doc:=WP New(Form.wpDrag)

// insert this doc at the drop position
WP INSERT DOCUMENT($rangeDrop;$doc;wk replace)

</code 4D>

To be honnest, you have to manage a couple of little other things, but not a big deal.
It took me about an hour to create the sample database that I join to this post.

Disclaimer : Not fully tested, not compiled, and created using v17R6 :slight_smile:

https://forums.4d.com/4DBB_Main/x_User/4028/files/31002946.zip

Regards,

Roland

Thank you Roland for the comprehensive response.

I have implemented the code you provided.

My only challenge is in using the Form command to memorize the selection being dragged. I have not used Form before, and it appears that it only works when the current form is a dialog or subform. So I guess I need to create my own object. But it seems unusual that a 4D Write Pro document would be in a dialog or a subform, so maybe I am misunderstanding something?

Tom Benedict

One thing I’ve noticed is that when dragging text, or an inline picture, with Automatic Drag and Drop the insertion point continues to appear as you drag, indicating the drop location in the text. Without Automatic Drag and Drop, there is no visible insertion point. I haven’t found a way to simulate this yet.

Also, “Undo” a move takes three tries to complete which users will no expect.

Tom

Hello

About the insertion point, that’s an issue i have noticed too (we’ll check if we can do something about that)

About “undos” we would need a “rollback point” or something like that (that we don’t have)
Each “undo” matches the execution of a single command. I don’t have a solution yet about that. Sorry :-/

About “Form”: you may also use the pasteboard or any other variable. I used Form to provide an example as fast as possible.

regards

Roland

Thanks for the confirmation Roland. I will continue to watch for 4D Write Pro improvements.

Tom