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.
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 ) 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?
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.
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.
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?