Hi , I’m trying to write simple plugin that will be holding (and saving with DAW session) small file and will allow to drag in and out this file from some external app.
At the moment, DragAndDrop works when I drag a file from an external application or file system.
When I drag the file from the plug-in to the directory everything works fine.
The file is created in tempDirectory and copied to the destination directory.
The problem is that the external application doesn’t accept a file that is pulled directly from my plugin. Interestingly - if I drag a file created in tempDirectory to this application it accepts it without any problem. I have no idea where to look for the reason. Has anyone encountered such a problem?
I tried debugging the code and it looks like the files I dragged from the filesystem have the same sArray files syntax as the files I dragged from my plugin.
Here is my DragAndDrop Component code.
*
==============================================================================
RigContainer.h
Created: 20 Feb 2021 9:58:20am
Author: MusicCreator
==============================================================================
*/
#pragma once
#include <JuceHeader.h>
//==============================================================================
/*
*/
class RigContainer : public juce::Component,
public juce::DragAndDropContainer,
public juce::FileDragAndDropTarget
{
public:
RigContainer()
{
}
~RigContainer() override
{
}
void paint (juce::Graphics& g) override
{
g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));
g.setColour (juce::Colours::grey);
g.drawRect (getLocalBounds(), 1);
if (somethingIsBeingDraggedOver)
{
g.setColour(juce::Colours::red);
g.drawRect(getLocalBounds(), 3);
}
g.setColour(getLookAndFeel().findColour(juce::Label::textColourId));
g.setFont(14.0f);
g.drawFittedText(message, getLocalBounds().reduced(10, 0), juce::Justification::centred, 4);
}
void resized() override
{
}
bool RigContainer::isInterestedInFileDrag(const juce::StringArray& files) override
{
return true;
}
void RigContainer::fileDragEnter(const juce::StringArray& /*files*/, int /*x*/, int /*y*/) override
{
somethingIsBeingDraggedOver = true;
repaint();
}
void RigContainer::fileDragMove(const juce::StringArray& /*files*/, int /*x*/, int /*y*/) override
{
}
void RigContainer::fileDragExit(const juce::StringArray& /*files*/) override
{
somethingIsBeingDraggedOver = false;
repaint();
}
void RigContainer::filesDropped(const juce::StringArray& files, int /*x*/, int /*y*/) override
{
if (files.size() == 1) {
juce::String rigFileFullPath = files[0];
juce::File rigFile(rigFileFullPath);
if (rigFile.existsAsFile())
{
rigFileName = rigFile.getFileName();
juce::MemoryBlock rigMemoryBlock;
rigFile.loadFileAsData(rigMemoryBlock);
rigContentBase64 = rigMemoryBlock.toBase64Encoding();
message = { "Rig name: " + rigFileName + "\n" };
}
}
else
{
message = "Too many files dragged. Only one file is allowed. ";
}
somethingIsBeingDraggedOver = false;
repaint();
}
void RigContainer::mouseDrag(const juce::MouseEvent& inEvent) override
{
if (!rigContentBase64.isEmpty())
{
int x = 0;
int y = 0;
juce::Image dragImage = createComponentSnapshot(getBounds());
juce::MouseEvent e2(inEvent.getEventRelativeTo(this));
const juce::Point<int> p(x - e2.x, y - e2.y);
juce::File tempRigFile = juce::File::getSpecialLocation(juce::File::tempDirectory).getChildFile(rigFileName);
if (tempRigFile.existsAsFile()) { tempRigFile.deleteFile();}
if (tempRigFile.create().ok())
{
juce::MemoryBlock mb;
mb.fromBase64Encoding(rigContentBase64);
tempRigFile.replaceWithData(mb.getData(), mb.getSize());
}
performExternalDragDropOfFiles(tempRigFile.getFullPathName(), true, this, [this](void) {});
startDragging(tempRigFile.getFullPathName(), this, dragImage, true, &p);
}
}
bool RigContainer::shouldDropFilesWhenDraggedExternally(const juce::DragAndDropTarget::SourceDetails& sourceDetails, juce::StringArray& files, bool& canMoveFiles) override
{
files.add(juce::File::getSpecialLocation(juce::File::tempDirectory).getChildFile(rigFileName).getFullPathName());
canMoveFiles = true;
return true;
}
private:
juce::DrawableRectangle rect;
bool somethingIsBeingDraggedOver = false;
juce::String message{ "Drag-and-drop Rig file onto this component!\n\n"
"You can also drag-and-drop Rig from RigManager" };
juce::String rigFileName{""};
juce::String rigContentBase64{""};
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RigContainer)
};