2008-03-07 11:33:19 +00:00
|
|
|
{
|
2008-03-11 23:52:48 +00:00
|
|
|
controller.pas
|
2008-03-07 11:33:19 +00:00
|
|
|
|
|
|
|
This example project is released under public domain
|
|
|
|
|
|
|
|
AUTHORS: Felipe Monteiro de Carvalho
|
|
|
|
}
|
2008-03-11 23:52:48 +00:00
|
|
|
unit controller;
|
2008-03-07 11:33:19 +00:00
|
|
|
|
2008-03-14 01:05:09 +00:00
|
|
|
{$mode delphi}{$STATIC ON}
|
2008-03-07 11:33:19 +00:00
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, foundation, objc, appkit, FPCMacOSAll;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
2008-03-11 23:52:48 +00:00
|
|
|
{ TMyController }
|
2008-03-07 11:33:19 +00:00
|
|
|
|
2008-03-11 23:52:48 +00:00
|
|
|
TMyController = class(NSObject)
|
2008-03-07 11:33:19 +00:00
|
|
|
public
|
|
|
|
{ Extra binding functions }
|
|
|
|
constructor Create; override;
|
2008-05-09 19:51:36 +00:00
|
|
|
class function getClass: objc.id; override;
|
2008-03-14 01:40:16 +00:00
|
|
|
procedure AddMethods; override;
|
2008-03-12 01:29:11 +00:00
|
|
|
{ Objective-c Methods }
|
2008-03-14 01:05:09 +00:00
|
|
|
class procedure doShowStatusitem(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; static;
|
|
|
|
class procedure doHideStatusitem(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; static;
|
2008-05-09 19:51:36 +00:00
|
|
|
class procedure doClose(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; static;
|
2008-03-12 01:29:11 +00:00
|
|
|
class function applicationShouldTerminateAfterLastWindowClosed(_self: objc.id;
|
2008-03-14 01:05:09 +00:00
|
|
|
_cmd: SEL; theApplication: objc.id): cbool; cdecl; static;
|
2008-03-12 01:29:11 +00:00
|
|
|
{ Other helper functions }
|
|
|
|
function GetResourcesDir: string;
|
|
|
|
function CreateButton(AView: NSView; ATitle: shortstring;
|
|
|
|
AX, AY, AWidth, AHeight: Double;
|
|
|
|
ACallbackName: string; ACallbackClass: NSObject): NSButton;
|
2008-03-07 11:33:19 +00:00
|
|
|
{ Fields }
|
|
|
|
bar: NSStatusBar;
|
|
|
|
item: NSStatusItem;
|
2008-03-10 11:01:03 +00:00
|
|
|
image: NSImage;
|
2008-03-07 11:33:19 +00:00
|
|
|
end;
|
|
|
|
|
2008-03-11 23:52:48 +00:00
|
|
|
const
|
2008-03-14 01:05:09 +00:00
|
|
|
Str_TMyController = 'TMyController';
|
|
|
|
|
2008-03-11 23:52:48 +00:00
|
|
|
Str_doShowStatusitem = 'doShowStatusitem:';
|
|
|
|
Str_doHideStatusitem = 'doHideStatusitem:';
|
2008-05-09 19:51:36 +00:00
|
|
|
Str_doClose = 'doClose:';
|
2008-03-11 23:52:48 +00:00
|
|
|
Str_applicationShouldTerminateAfterLastWindowClosed = 'applicationShouldTerminateAfterLastWindowClosed:';
|
2008-03-07 11:33:19 +00:00
|
|
|
|
2008-03-12 01:29:11 +00:00
|
|
|
var
|
|
|
|
myController: TMyController;
|
2008-03-10 11:51:50 +00:00
|
|
|
|
2008-05-09 19:51:36 +00:00
|
|
|
{ classes }
|
|
|
|
pool: NSAutoreleasePool;
|
|
|
|
MainWindow: NSWindow;
|
|
|
|
MainWindowView: NSView;
|
|
|
|
{ strings and sizes}
|
|
|
|
CFTitle: CFStringRef;
|
|
|
|
MainWindowRect: NSRect;
|
|
|
|
|
2008-03-07 11:33:19 +00:00
|
|
|
implementation
|
|
|
|
|
2008-03-11 23:52:48 +00:00
|
|
|
{ TMyController }
|
2008-03-07 11:33:19 +00:00
|
|
|
|
2008-03-12 01:29:11 +00:00
|
|
|
{@@
|
|
|
|
Adds methods to the class
|
|
|
|
|
|
|
|
Details of the parameters string:
|
|
|
|
|
|
|
|
The first parameter is the result (v = void),
|
|
|
|
followed by self and _cmd (@ = id and : = SEL),
|
|
|
|
and on the end "sender" (@ = id)
|
|
|
|
}
|
2008-03-11 23:52:48 +00:00
|
|
|
procedure TMyController.AddMethods;
|
2008-03-07 11:33:19 +00:00
|
|
|
begin
|
2008-03-12 01:29:11 +00:00
|
|
|
AddMethod(Str_doShowStatusItem, 'v@:@', Pointer(doShowStatusitem));
|
|
|
|
AddMethod(Str_doHideStatusitem, 'v@:@', Pointer(doHideStatusitem));
|
2008-05-09 19:51:36 +00:00
|
|
|
AddMethod(Str_doClose, 'v@:@', Pointer(doClose));
|
2008-03-11 23:52:48 +00:00
|
|
|
AddMethod(Str_applicationShouldTerminateAfterLastWindowClosed, 'b@:@',
|
2008-03-12 01:29:11 +00:00
|
|
|
Pointer(applicationShouldTerminateAfterLastWindowClosed));
|
2008-03-07 11:33:19 +00:00
|
|
|
end;
|
|
|
|
|
2008-03-11 23:52:48 +00:00
|
|
|
constructor TMyController.Create;
|
2008-03-10 11:01:03 +00:00
|
|
|
var
|
|
|
|
fileName: CFStringRef;
|
2008-03-07 11:33:19 +00:00
|
|
|
begin
|
2008-04-02 23:03:56 +00:00
|
|
|
{ The class is registered on the Objective-C runtime before the NSObject constructor is called }
|
2008-03-14 01:05:09 +00:00
|
|
|
if not CreateClassDefinition(Str_TMyController, Str_NSObject) then WriteLn('Failed to create objc class');
|
|
|
|
|
2008-03-07 11:33:19 +00:00
|
|
|
inherited Create;
|
|
|
|
|
|
|
|
AddMethods();
|
|
|
|
|
|
|
|
bar := NSStatusBar.systemStatusBar();
|
2008-03-10 11:01:03 +00:00
|
|
|
|
2008-03-11 23:52:48 +00:00
|
|
|
fileName := CFStringCreateWithPascalString(nil, GetResourcesDir + 'icon.ico', kCFStringEncodingUTF8);
|
2008-03-10 11:01:03 +00:00
|
|
|
image := NSImage.initWithContentsOfFile(fileName);
|
2008-03-07 11:33:19 +00:00
|
|
|
end;
|
|
|
|
|
2008-05-09 19:51:36 +00:00
|
|
|
class function TMyController.getClass: objc.id;
|
2008-03-14 01:05:09 +00:00
|
|
|
begin
|
|
|
|
Result := objc_getClass(Str_TMyController);
|
|
|
|
end;
|
|
|
|
|
2008-03-07 11:33:19 +00:00
|
|
|
{ Objective-c Methods }
|
|
|
|
|
2008-03-12 01:29:11 +00:00
|
|
|
class procedure TMyController.doShowStatusitem(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl;
|
2008-03-07 11:33:19 +00:00
|
|
|
begin
|
2008-03-11 23:52:48 +00:00
|
|
|
if myController.item <> nil then Exit;
|
2008-03-10 11:01:03 +00:00
|
|
|
|
2008-06-06 01:32:06 +00:00
|
|
|
myController.item := NSStatusItem.CreateWithHandle(myController.bar.statusItemWithLength(NSSquareStatusItemLength));
|
2008-03-11 23:52:48 +00:00
|
|
|
myController.item.retain();
|
|
|
|
myController.item.setImage(myController.image);
|
2008-03-07 11:33:19 +00:00
|
|
|
end;
|
|
|
|
|
2008-03-12 01:29:11 +00:00
|
|
|
class procedure TMyController.doHideStatusitem(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl;
|
2008-03-10 11:51:50 +00:00
|
|
|
begin
|
2008-03-11 23:52:48 +00:00
|
|
|
if myController.item = nil then Exit;
|
2008-03-10 11:51:50 +00:00
|
|
|
|
2008-03-11 23:52:48 +00:00
|
|
|
myController.item.Free;
|
|
|
|
myController.item := nil;
|
|
|
|
end;
|
|
|
|
|
2008-05-09 19:51:36 +00:00
|
|
|
class procedure TMyController.doClose(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl;
|
|
|
|
begin
|
|
|
|
MainWindow.close;
|
|
|
|
end;
|
|
|
|
|
2008-03-12 01:29:11 +00:00
|
|
|
class function TMyController.applicationShouldTerminateAfterLastWindowClosed(_self: objc.id;
|
|
|
|
_cmd: SEL; theApplication: objc.id): cbool; cdecl;
|
2008-03-11 23:52:48 +00:00
|
|
|
begin
|
|
|
|
Result := objc.YES;
|
2008-03-10 11:51:50 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
{ Other helper functions }
|
|
|
|
|
2008-03-12 01:29:11 +00:00
|
|
|
function TMyController.GetResourcesDir: string;
|
2008-03-10 11:51:50 +00:00
|
|
|
const
|
|
|
|
BundleResourcesDirectory = '/Contents/Resources/';
|
|
|
|
var
|
|
|
|
pathRef: CFURLRef;
|
|
|
|
pathCFStr: CFStringRef;
|
|
|
|
pathStr: shortstring;
|
|
|
|
begin
|
|
|
|
// Under Mac OS X we need to get the location of the bundle
|
|
|
|
pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
|
|
|
|
pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
|
|
|
|
CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
|
|
|
|
CFRelease(pathRef);
|
|
|
|
CFRelease(pathCFStr);
|
|
|
|
|
|
|
|
Result := pathStr + BundleResourcesDirectory;
|
|
|
|
end;
|
|
|
|
|
2008-03-12 01:29:11 +00:00
|
|
|
function TMyController.CreateButton(AView: NSView; ATitle: shortstring;
|
2008-03-10 11:51:50 +00:00
|
|
|
AX, AY, AWidth, AHeight: Double;
|
|
|
|
ACallbackName: string; ACallbackClass: NSObject): NSButton;
|
|
|
|
var
|
|
|
|
CFButtonText: CFStringRef;
|
|
|
|
ButtonRect: NSRect;
|
|
|
|
begin
|
|
|
|
CFButtonText := CFStringCreateWithPascalString(nil, ATitle, kCFStringEncodingUTF8);
|
|
|
|
ButtonRect.origin.x := AX;
|
|
|
|
ButtonRect.origin.y := AY;
|
|
|
|
ButtonRect.size.width := AWidth;
|
|
|
|
ButtonRect.size.height := AHeight;
|
|
|
|
Result := NSButton.initWithFrame(ButtonRect);
|
|
|
|
Result.setTitle(CFButtonText);
|
|
|
|
Result.setBezelStyle(NSRoundedBezelStyle);
|
|
|
|
Result.setAction(sel_registerName(PChar(ACallbackName)));
|
|
|
|
Result.setTarget(ACallbackClass.Handle);
|
|
|
|
AView.addSubview(Result);
|
|
|
|
end;
|
|
|
|
|
2008-03-07 11:33:19 +00:00
|
|
|
end.
|
|
|
|
|