Text drag and drop


#1

Hi all,

is it possible to receive dropped text via Juce? For example I can select some text from this web page and drag it into the editor: [dragged]Disable BBCode[/dragged]

I tried DragAndDropTarget, but as far as I understand this is only for drag and drop between Juce components. FileDragAndDropTarget of course only works for files.

Chris


#2

No, that’s not something I’ve implemented. I guess it’s possible, but I’ve not investigated.


#3

Hi Jules,

I tried on Windows and OS X to implement a text drag and drop target. Since you already implemented a file target it wasn’t to hard to tweak that to also receive text.

On Winows this is done using CF_TEXT instead of CF_HDROP for the format description:

// added in juce_win32_Windowing.cpp: line ~1890
FORMATETC format_text = { CF_TEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };

if (pDataObject->QueryGetData(&format_text) == S_OK)
{
	if(pDataObject->GetData(&format_text, &medium) == S_OK)
	{
		char* data = (char*) GlobalLock(medium.hGlobal);

                // only print string:
		DBG(data);

		GlobalUnlock(medium.hGlobal);

	}
}

On OS X the supported drag types have to include NSStringPboardType (which is already there but commented):

// changed in juce_mac_NSViewComponentPeer.mm: line ~600

    return [NSArray arrayWithObjects: NSFilenamesPboardType, NSFilesPromisePboardType, NSStringPboardType, nil];


// juce_mac_NSViewComponentPeer.mm: line ~1503


if (files.size()==0) // there surely is a better way to distinguish between file and text drop
{
	NSString* s = [pasteBoard stringForType: NSStringPboardType];

// Print string

        String jucestr(nsStringToJuce(s));
        if (jucestr.size()>0)
        {
	        DBG(jucestr);
        }

}

One problem I have on OS X is that the drop event (type==2) doesn’t get called. I don’t know much about OS X messaging (and probably hadn’t come that far without the file drop stuff to start from), so I have no idea why this won’t happen.

In general it doesn’t seem to hard to receive a text drag and drop (didn’t try on linux due to a lack of knowledge :slight_smile: ) but of course this also should somehow get passed to a component; as you can see I don’t do anything with the text. I don’t know the Juce internals enough to produce a solution for that.

Therefore, could you implement an additional TextDragAndDropTarget class that receives this messages?

Chris


#4

Hi Jules,

I implemented text drag and drop for Windows and OS X (don’t have a linux environment). I attached the changes I need to make to the current tip and a simple example window with an Texteditor that shows any text dropped.
It is implemented via a new class TextDragAndDropTarget which is mainly a copy of FileDragAndDropTarget.

I first implemented this on an older Juce version (a local copy with modification I use) and than transferred the changes to the tip. I’ve tested it with the Juce tip’s modifications on Windows but not on OS X. Of course I tested on OS X with my local juce copy and I’m not expecting problems.

Would you consider adding this to Juce?

Chris


#5

A little push.

Chris


#6

Yes, thanks, I did see this but haven’t had a chance to look into it yet! Will do soon!


#7

+1


#8

Just having a look at this now… Just a couple of things to mention:

I’ve really no idea what your win32 utf conversion code is supposed to be doing… What format is it converting from? And what’s going on in this line?

utf8[n++] = 0xc2 + (data[i] > 0xbf);

…adding a bool to a char, really?

And in case you’ve done the same thing elsewhere in your own code, I should warn you that this kind of thing is very very dangerous:

ScopedPointer<unsigned char> utf8(new unsigned char[length*2]);

ScopedPointer uses delete(), not delete[], so this will trash your heap. Always use a HeapBlock, which is designed for exactly this purpose!


#9

Re: utf encoding: doesn’t matter, I used utf-16 instead.


#10

I found the utf8 encoding code somewhere on stackoverflow (Link) and it seemed to work. I’m not that much into text encodings so I didn’t investigate further.

Thanks for the notice about ScopedPointer. I indeed do this on other ocassions, though I never ran into crashes or observed strange problems.

Chris


#11

Reviving an ancient thread here… but I was about to try and add this to my app so I can drop a file onto a TextEditor to populate it with the file’s path… Was this ever implemented?

Thanks,

Rail


#12

Never mind… simple enough deriving a class from TextEditor and FileDragAndDropTarget.

Rail