Recent

Author Topic: Cocoa Drag and Drop  (Read 5120 times)

ydk2

  • Newbie
  • Posts: 5
Cocoa Drag and Drop
« on: April 07, 2014, 02:04:44 pm »
I try make working DnD on Cocoa for now i have:
In "CocoaInt":
from line 52:

Code: [Select]
  { TCocoaAppDelegate }

  TCocoaAppDelegate = objcclass(NSObject, NSApplicationDelegateProtocol)
    function applicationShouldTerminate(sender: NSApplication): NSApplicationTerminateReply; message 'applicationShouldTerminate:';
  end;

to:

Code: [Select]
  { TCocoaAppDelegate }

  TCocoaAppDelegate = objcclass(NSObject, NSApplicationDelegateProtocol)
    function openFiles(sender: NSApplication; filenames: NSArray): Boolean; message 'application:openFiles:';
    function applicationShouldTerminate(sender: NSApplication): NSApplicationTerminateReply; message 'applicationShouldTerminate:';
  end;
 


in "cocoaobject.inc"
from line 453:
Code: [Select]
{ TCocoaAppDelegate }

function TCocoaAppDelegate.applicationShouldTerminate(sender: NSApplication): NSApplicationTerminateReply;
begin
  Result := NSTerminateNow;
end;

to:
Code: [Select]
{ TCocoaAppDelegate }

function TCocoaAppDelegate.openFiles(sender: NSApplication; filenames: NSArray): Boolean;
var
   i: Longint;
   args: array of String;
begin
//if gOpenFilesProc <> nil then
//begin
if filenames.count > 0 then
begin
SetLength(args, filenames.count);
    for i := 0 to filenames.count-1 do
    begin
        args[i] := NSStringToString(NSString(filenames.objectAtIndex(i)));
    end;
    Application.IntfDropFiles(args);
    //end;
    result := true; // Assume success
end
else
   result := false; // No proc - failed!
end;

function TCocoaAppDelegate.applicationShouldTerminate(sender: NSApplication): NSApplicationTerminateReply;
begin
  Result := NSTerminateNow;
end;


And in Main form of project I use:

Code: [Select]
procedure TMainForm.OnApplicationDropFiles(Sender: TObject; const FileNames: array of String);
var
i:Longint;
begin
        //ShowMessage('Files dropped');
  for i := 0 to Length(FileNames)-1 do
        begin
Dialogs.ShowMessage(FileNames[i]);
        end;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
  Application.AddOnDropFilesHandler(@OnApplicationDropFiles);
end;

and this working for me on Application icon if anybody can check on other OS-es and told me if this solusion works.

  Edit: I use Lazazus 1.2.0 OSX Lion 10.7.5 and compile with target x86_64
« Last Edit: April 09, 2014, 01:17:48 am by ydk2 »

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Cocoa Drag and Drop
« Reply #1 on: February 09, 2015, 11:10:58 am »
Oh, you should really post patches in the BTS, like explained here: http://wiki.lazarus.freepascal.org/Creating_A_Patch

Or else they can get lost in time&space

 

TinyPortal © 2005-2018