cocoa: Implements support for receiving dropped files into forms

git-svn-id: trunk@49725 -
This commit is contained in:
sekelsenmat 2015-08-29 07:27:27 +00:00
parent 6621c5c9ce
commit 83d51bb80f
2 changed files with 59 additions and 1 deletions

View File

@ -402,6 +402,9 @@ type
procedure scrollWheel(event: NSEvent); override;
procedure sendEvent(event: NSEvent); override;
function lclIsHandle: Boolean; override;
// NSDraggingDestinationCategory
function draggingEntered(sender: NSDraggingInfoProtocol): NSDragOperation; override;
function performDragOperation(sender: NSDraggingInfoProtocol): Boolean; override;
end;
{ TCocoaCustomControl }
@ -1433,6 +1436,59 @@ begin
inherited sendEvent(event);
end;
function TCocoaWindow.draggingEntered(sender: NSDraggingInfoProtocol): NSDragOperation;
var
lTarget: TCustomForm = nil;
begin
Result := NSDragOperationNone;
if (callback <> nil) and (callback.GetTarget() <> nil) and (callback.GetTarget() is TCustomForm) then
lTarget := TCustomForm(callback.GetTarget());
if (lTarget <> nil) and (lTarget.OnDropFiles <> nil) then
begin
Result := sender.draggingSourceOperationMask();
end;
end;
function TCocoaWindow.performDragOperation(sender: NSDraggingInfoProtocol): Boolean;
var
draggedURLs{, lClasses}: NSArray;
lFiles: array of string;
i: Integer;
pboard: NSPasteboard;
lNSStr: NSString;
//lClass: pobjc_class;
begin
Result := False;
pboard := sender.draggingPasteboard();
// Multiple strings
draggedURLs := pboard.propertyListForType(NSFilenamesPboardType);
SetLength(lFiles, draggedURLs.count);
for i := 0 to draggedURLs.count-1 do
begin
lNSStr := NSString(draggedURLs.objectAtIndex(i));
lFiles[i] := NSStringToString(lNSStr);
end;
// Multiple URLs -> Results in strange URLs with file:// protocol
{if pboard.types.containsObject(NSURLPboardType) then
begin
lClass := NSURL.classClass;
lClasses := NSArray.arrayWithObjects_count(@lClass, 1);
draggedURLs := pboard.readObjectsForClasses_options(lClasses, nil);
SetLength(lFiles, draggedURLs.count);
for i := 0 to draggedURLs.count-1 do
begin
lNSStr := NSURL(draggedURLs.objectAtIndex(i)).absoluteString;
lFiles[i] := NSStringToString(lNSStr);
end;
end;}
if (Length(lFiles) > 0) and (callback <> nil) and (callback.GetTarget() <> nil) then
TCustomForm(callback.GetTarget()).IntfDropFiles(lFiles);
Result := True;
end;
{ TCocoaScrollView }
function TCocoaScrollView.lclIsHandle: Boolean;

View File

@ -478,7 +478,6 @@ var
ns.release;
win.setAcceptsMouseMovedEvents(True);
cnt.callback := TCocoaWindow(win).callback;
cnt.callback.IsOpaque:=true;
win.setContentView(cnt);
@ -490,6 +489,9 @@ var
else
NSWindow(AParams.WndParent).addChildWindow_ordered(win, NSWindowAbove);
end;
// support for drag & drop
win.registerForDraggedTypes(NSArray.arrayWithObjects_count(@NSFilenamesPboardType, 1));
end
else
begin