2008-03-07 11:33:19 +00:00
|
|
|
{
|
|
|
|
actions.pas
|
|
|
|
|
|
|
|
This example project is released under public domain
|
|
|
|
|
|
|
|
AUTHORS: Felipe Monteiro de Carvalho
|
|
|
|
}
|
|
|
|
unit actions;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, foundation, objc, appkit, FPCMacOSAll;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
{ TMyActionList }
|
|
|
|
|
|
|
|
TMyActionList = class(NSObject)
|
|
|
|
public
|
|
|
|
{ Extra binding functions }
|
|
|
|
constructor Create; override;
|
|
|
|
procedure AddMethods;
|
|
|
|
{ Fields }
|
|
|
|
bar: NSStatusBar;
|
|
|
|
item: NSStatusItem;
|
2008-03-10 11:01:03 +00:00
|
|
|
image: NSImage;
|
2008-03-07 11:33:19 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
{ Objective-c Methods }
|
|
|
|
procedure doShowStatusitem(param1: objc.id; param2: SEL; param3: objc.id); cdecl;
|
2008-03-10 11:51:50 +00:00
|
|
|
procedure doHideStatusitem(param1: objc.id; param2: SEL; param3: objc.id); cdecl;
|
2008-03-07 11:33:19 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
actionList: TMyActionList;
|
|
|
|
|
2008-03-10 11:51:50 +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
|
|
|
implementation
|
|
|
|
|
|
|
|
{ TMyActionList }
|
|
|
|
|
2008-03-10 11:51:50 +00:00
|
|
|
{ Adds methods to the class }
|
2008-03-07 11:33:19 +00:00
|
|
|
procedure TMyActionList.AddMethods;
|
|
|
|
begin
|
2008-03-10 11:51:50 +00:00
|
|
|
{ Parameters string:
|
|
|
|
|
|
|
|
The first parameter is the result (v = void),
|
2008-03-07 11:33:19 +00:00
|
|
|
followed by self and _cmd (@ = id and : = SEL),
|
|
|
|
and on the end "sender" (@ = id) }
|
2008-03-10 11:51:50 +00:00
|
|
|
|
|
|
|
AddMethod('doShowStatusitem:', 'v@:@', @doShowStatusitem);
|
|
|
|
AddMethod('doHideStatusitem:', 'v@:@', @doHideStatusitem);
|
2008-03-07 11:33:19 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TMyActionList.Create;
|
2008-03-10 11:01:03 +00:00
|
|
|
var
|
|
|
|
fileName: CFStringRef;
|
2008-03-07 11:33:19 +00:00
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
|
|
|
|
AddMethods();
|
|
|
|
|
|
|
|
bar := NSStatusBar.systemStatusBar();
|
2008-03-10 11:01:03 +00:00
|
|
|
|
|
|
|
fileName := CFStringCreateWithPascalString(nil,
|
2008-03-10 11:51:50 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
{ Objective-c Methods }
|
|
|
|
|
|
|
|
procedure doShowStatusitem(param1: objc.id; param2: SEL; param3: objc.id); cdecl;
|
|
|
|
begin
|
2008-03-10 11:51:50 +00:00
|
|
|
if actionList.item <> nil then Exit;
|
2008-03-10 11:01:03 +00:00
|
|
|
|
2008-03-10 11:51:50 +00:00
|
|
|
actionList.item := actionList.bar.statusItemWithLength(NSSquareStatusItemLength);
|
2008-03-10 11:01:03 +00:00
|
|
|
actionList.item.retain();
|
|
|
|
actionList.item.setImage(actionList.image);
|
2008-03-07 11:33:19 +00:00
|
|
|
end;
|
|
|
|
|
2008-03-10 11:51:50 +00:00
|
|
|
procedure doHideStatusitem(param1: objc.id; param2: SEL; param3: objc.id); cdecl;
|
|
|
|
begin
|
|
|
|
if actionList.item = nil then Exit;
|
|
|
|
|
|
|
|
actionList.item.Free;
|
|
|
|
actionList.item := nil;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ Other helper functions }
|
|
|
|
|
|
|
|
function GetResourcesDir: string;
|
|
|
|
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;
|
|
|
|
|
|
|
|
function CreateButton(AView: NSView; ATitle: shortstring;
|
|
|
|
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.
|
|
|
|
|