Forum > Cocoa

Cocoa Drag and Drop

(1/1)

ydk2:
I try make working DnD on Cocoa for now i have:
In "CocoaInt":
from line 52:


--- Code: ---  { TCocoaAppDelegate }

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

to:


--- Code: ---  { TCocoaAppDelegate }

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


in "cocoaobject.inc"
from line 453:

--- Code: ---{ TCocoaAppDelegate }

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

to:

--- Code: ---{ 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;
--- End code ---


And in Main form of project I use:


--- Code: ---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;
--- End code ---

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

felipemdc:
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

Navigation

[0] Message Index

Go to full version