Skip to content

Commit

Permalink
Merge pull request #1067 from iPlug2/graphics/external-drag
Browse files Browse the repository at this point in the history
Add IGraphics::InitiateExternalFileDragDrop()
  • Loading branch information
olilarkin committed Mar 16, 2024
2 parents 876c85a + c412910 commit 759d9fd
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 1 deletion.
6 changes: 6 additions & 0 deletions IGraphics/IGraphics.h
Expand Up @@ -865,6 +865,12 @@ class IGraphics
* @return /c true on success */
virtual bool SetFilePathInClipboard(const char* path) { return false; }

/** Initiate an drag-n-drop operation of an existing file, to be dropped outside of the current window
* @param path A CString that contains a path to a file on disk
* @param iconBounds The area where the icon should appear
* @return /c true on success */
virtual bool InitiateExternalFileDragDrop(const char* path, const IRECT& iconBounds) { return false; };

/** Call this if you modify control tool tips at runtime. \todo explain */
virtual void UpdateTooltips() = 0;

Expand Down
2 changes: 2 additions & 0 deletions IGraphics/Platforms/IGraphicsMac.h
Expand Up @@ -69,6 +69,8 @@ class IGraphicsMac final : public IGRAPHICS_DRAW_CLASS
bool SetTextInClipboard(const char* str) override;
bool SetFilePathInClipboard(const char* path) override;

bool InitiateExternalFileDragDrop(const char* path, const IRECT& iconBounds) override API_AVAILABLE(macos(10.13));

float MeasureText(const IText& text, const char* str, IRECT& bounds) const override;

EUIAppearance GetUIAppearance() const override;
Expand Down
22 changes: 22 additions & 0 deletions IGraphics/Platforms/IGraphicsMac.mm
Expand Up @@ -662,6 +662,28 @@ static int GetSystemVersion()
return (bool)success;
}

bool IGraphicsMac::InitiateExternalFileDragDrop(const char* path, const IRECT& iconBounds)
{
NSPasteboardItem* pasteboardItem = [[NSPasteboardItem alloc] init];
NSURL* fileURL = [NSURL fileURLWithPath: [NSString stringWithUTF8String: path]];
[pasteboardItem setString:fileURL.absoluteString forType:NSPasteboardTypeFileURL];

NSDraggingItem* draggingItem = [[NSDraggingItem alloc] initWithPasteboardWriter:pasteboardItem];
NSRect draggingFrame = ToNSRect(this, iconBounds);
NSImage* iconImage = [[NSWorkspace sharedWorkspace] iconForFile:fileURL.path];
[iconImage setSize:NSMakeSize(64, 64)];
[draggingItem setDraggingFrame:draggingFrame contents: iconImage];

IGRAPHICS_VIEW* view = (IGRAPHICS_VIEW*) mView;
NSDraggingSession* draggingSession = [view beginDraggingSessionWithItems:@[draggingItem] event:[NSApp currentEvent] source: view];
draggingSession.animatesToStartingPositionsOnCancelOrFail = YES;
draggingSession.draggingFormation = NSDraggingFormationNone;

ReleaseMouseCapture();

return true;
}

EUIAppearance IGraphicsMac::GetUIAppearance() const
{
if (@available(macOS 10.14, *)) {
Expand Down
3 changes: 2 additions & 1 deletion IGraphics/Platforms/IGraphicsMac_view.h
Expand Up @@ -113,7 +113,7 @@ using namespace igraphics;
#define VIEW_BASE NSView
#endif

@interface IGRAPHICS_VIEW : VIEW_BASE <NSTextFieldDelegate/*, WKScriptMessageHandler*/>
@interface IGRAPHICS_VIEW : VIEW_BASE <NSTextFieldDelegate, NSDraggingSource/*, WKScriptMessageHandler*/>
{
CVDisplayLinkRef mDisplayLink;
dispatch_source_t mDisplaySource;
Expand Down Expand Up @@ -174,6 +174,7 @@ using namespace igraphics;
//drag-and-drop
- (NSDragOperation) draggingEntered: (id <NSDraggingInfo>) sender;
- (BOOL) performDragOperation: (id<NSDraggingInfo>) sender;
- (NSDragOperation)draggingSession:(NSDraggingSession*) session sourceOperationMaskForDraggingContext:(NSDraggingContext)context;
//
- (void) setMouseCursor: (ECursor) cursorType;
@end
Expand Down
5 changes: 5 additions & 0 deletions IGraphics/Platforms/IGraphicsMac_view.mm
Expand Up @@ -1285,6 +1285,11 @@ - (BOOL) performDragOperation: (id<NSDraggingInfo>) sender
return YES;
}

- (NSDragOperation)draggingSession:(NSDraggingSession*) session sourceOperationMaskForDraggingContext:(NSDraggingContext)context
{
return NSDragOperationCopy;
}

#ifdef IGRAPHICS_METAL
- (void) frameDidChange:(NSNotification*) pNotification
{
Expand Down
25 changes: 25 additions & 0 deletions IGraphics/Platforms/IGraphicsWin.cpp
Expand Up @@ -16,6 +16,7 @@

#include "IPlugParameter.h"
#include "IGraphicsWin.h"
#include "IGraphicsWin_dnd.h"
#include "IPopupMenuControl.h"
#include "IPlugPaths.h"

Expand Down Expand Up @@ -1953,6 +1954,30 @@ bool IGraphicsWin::SetFilePathInClipboard(const char* path)
return result;
}

bool IGraphicsWin::InitiateExternalFileDragDrop(const char* path, const IRECT& /*iconBounds*/)
{
using namespace DragAndDropHelpers;
OleInitialize(nullptr);

FORMATETC format = { CF_HDROP, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };

DataObject* dataObj = new DataObject(&format, path);
DropSource* dropSource = new DropSource();

DWORD dropEffect;
HRESULT ret = DoDragDrop(dataObj, dropSource, DROPEFFECT_COPY, &dropEffect);
bool success = SUCCEEDED(ret);

dataObj->Release();
dropSource->Release();

OleUninitialize();

ReleaseMouseCapture();

return success;
}

static HFONT GetHFont(const char* fontName, int weight, bool italic, bool underline, DWORD quality = DEFAULT_QUALITY, bool enumerate = false)
{
HDC hdc = GetDC(NULL);
Expand Down
2 changes: 2 additions & 0 deletions IGraphics/Platforms/IGraphicsWin.h
Expand Up @@ -77,6 +77,8 @@ class IGraphicsWin final : public IGRAPHICS_DRAW_CLASS
bool SetTextInClipboard(const char* str) override;
bool SetFilePathInClipboard(const char* path) override;

bool InitiateExternalFileDragDrop(const char* path, const IRECT& iconBounds) override;

bool PlatformSupportsMultiTouch() const override;

static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
Expand Down

0 comments on commit 759d9fd

Please sign in to comment.