2008-09-29 00:47:50 +00:00
|
|
|
unit mytoolbar;
|
|
|
|
|
|
|
|
{$mode delphi}{$STATIC ON}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2008-10-06 02:46:47 +00:00
|
|
|
SysUtils,
|
2008-09-29 00:47:50 +00:00
|
|
|
MacOSAll, objc, appkit, foundation;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
{ TMyToolbarController }
|
|
|
|
|
|
|
|
TMyToolbarController = class(NSObject)
|
|
|
|
public
|
|
|
|
{ Extra binding functions }
|
|
|
|
constructor Create; override;
|
|
|
|
procedure AddMethods; override;
|
2008-10-06 02:46:47 +00:00
|
|
|
procedure AttachEventHandlers;
|
|
|
|
{ Toolbar items }
|
|
|
|
OpenToolbarItem, SaveToolbarItem, CloseToolbarItem: NSToolbarItem;
|
2008-09-29 00:47:50 +00:00
|
|
|
{ Objective-c Methods }
|
|
|
|
class function toolbarAllowedItemIdentifiers(_self: objc.id;
|
|
|
|
_cmd: SEL; toolbar: objc.id {NSToolbar}): CFArrayRef; cdecl;// static;
|
|
|
|
class function toolbarDefaultItemIdentifiers(_self: objc.id;
|
|
|
|
_cmd: SEL; toolbar: objc.id {NSToolbar}): CFArrayRef; cdecl;// static;
|
|
|
|
end;
|
|
|
|
|
|
|
|
const
|
|
|
|
Str_toolbarAllowedItemIdentifiers = 'toolbarAllowedItemIdentifiers:';
|
|
|
|
Str_toolbarDefaultItemIdentifiers = 'toolbarDefaultItemIdentifiers:';
|
|
|
|
Str_toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar = 'toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:';
|
|
|
|
|
|
|
|
var
|
|
|
|
OpenToolbarItemIdentifier, SaveToolbarItemIdentifier, CloseToolbarItemIdentifier: CFStringRef;
|
2008-10-06 02:46:47 +00:00
|
|
|
myToolbarController: TMyToolbarController;
|
|
|
|
|
|
|
|
function toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar
|
|
|
|
(_self: objc.id; _cmd: SEL; toolbar: objc.id;
|
|
|
|
itemIdentifier: CFStringRef; flag: OBJC_BOOL): objc.id; cdecl;// static;
|
2008-09-29 00:47:50 +00:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
model, controller;
|
|
|
|
|
|
|
|
{ TMyToolbar }
|
|
|
|
|
|
|
|
constructor TMyToolbarController.Create;
|
|
|
|
begin
|
|
|
|
{ The class is registered on the Objective-C runtime before the NSObject constructor is called }
|
|
|
|
if not CreateClassDefinition(ClassName(), Str_NSObject) then WriteLn('Failed to create objc class ' + ClassName());
|
|
|
|
|
|
|
|
inherited Create;
|
|
|
|
|
|
|
|
{ Prepare CFStringRefs for the constants }
|
|
|
|
|
|
|
|
OpenToolbarItemIdentifier := CFStringCreateWithPascalString(nil, 'OpenID', kCFStringEncodingUTF8);
|
|
|
|
SaveToolbarItemIdentifier := CFStringCreateWithPascalString(nil, 'SaveID', kCFStringEncodingUTF8);
|
|
|
|
CloseToolbarItemIdentifier := CFStringCreateWithPascalString(nil, 'CloseID', kCFStringEncodingUTF8);
|
2008-10-06 02:46:47 +00:00
|
|
|
|
|
|
|
{ Create toolbar items }
|
|
|
|
OpenToolbarItem := NSToolbarItem.initWithItemIdentifier(OpenToolbarItemIdentifier);
|
|
|
|
// [toolbarItem setLabel:@"Save"];
|
|
|
|
// [toolbarItem setPaletteLabel:[toolbarItem label]];
|
|
|
|
// [toolbarItem setToolTip:@"Save Your Passwords"];}
|
|
|
|
OpenToolbarItem.setImage(myModel.imgOpen.Handle);
|
|
|
|
|
|
|
|
SaveToolbarItem := NSToolbarItem.initWithItemIdentifier(SaveToolbarItemIdentifier);
|
|
|
|
SaveToolbarItem.setImage(myModel.imgSave.Handle);
|
|
|
|
|
|
|
|
CloseToolbarItem := NSToolbarItem.initWithItemIdentifier(CloseToolbarItemIdentifier);
|
|
|
|
CloseToolbarItem.setImage(myModel.imgClose.Handle);
|
2008-09-29 00:47:50 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMyToolbarController.AddMethods;
|
|
|
|
begin
|
|
|
|
AddMethod(Str_toolbarAllowedItemIdentifiers, '@@:@', Pointer(toolbarAllowedItemIdentifiers));
|
|
|
|
AddMethod(Str_toolbarDefaultItemIdentifiers, '@@:@', Pointer(toolbarDefaultItemIdentifiers));
|
2008-10-06 02:46:47 +00:00
|
|
|
AddMethod(Str_toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar,
|
|
|
|
'@@:@@c', @toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMyToolbarController.AttachEventHandlers;
|
|
|
|
begin
|
|
|
|
OpenToolbarItem.setTarget(myController.Handle);
|
|
|
|
OpenToolbarItem.setAction(sel_registerName(PChar('doOpenFile:')));
|
|
|
|
|
|
|
|
SaveToolbarItem.setTarget(myController.Handle);
|
|
|
|
SaveToolbarItem.setAction(sel_registerName(PChar('doSaveFile:')));
|
|
|
|
|
|
|
|
CloseToolbarItem.setTarget(myController.Handle);
|
|
|
|
CloseToolbarItem.setAction(sel_registerName(PChar('doClose:')));
|
2008-09-29 00:47:50 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
class function TMyToolbarController.toolbarAllowedItemIdentifiers(_self: objc.id;
|
|
|
|
_cmd: SEL; toolbar: objc.id): CFArrayRef; cdecl;
|
|
|
|
var
|
|
|
|
toolbarItems: array[0..4] of CFStringRef;
|
|
|
|
begin
|
2008-10-06 02:46:47 +00:00
|
|
|
WriteLn('OpenToolbarItemIdentifier: ', IntToHex(Cardinal(OpenToolbarItemIdentifier), 8));
|
|
|
|
WriteLn('SaveToolbarItemIdentifier: ', IntToHex(Cardinal(SaveToolbarItemIdentifier), 8));
|
|
|
|
WriteLn('NSToolbarSpaceItemIdentifier: ', IntToHex(Cardinal(NSToolbarSpaceItemIdentifier), 8));
|
|
|
|
WriteLn('CloseToolbarItemIdentifier: ', IntToHex(Cardinal(CloseToolbarItemIdentifier), 8));
|
2008-09-29 00:47:50 +00:00
|
|
|
toolbarItems[0] := OpenToolbarItemIdentifier;
|
|
|
|
toolbarItems[1] := SaveToolbarItemIdentifier;
|
2008-10-06 02:46:47 +00:00
|
|
|
toolbarItems[2] := NSToolbarSpaceItemIdentifier;
|
|
|
|
toolbarItems[3] := CloseToolbarItemIdentifier;
|
2008-09-29 00:47:50 +00:00
|
|
|
toolbarItems[4] := nil;
|
|
|
|
|
|
|
|
Result := CFArrayCreate(nil, @toolbarItems[0], 4, nil);
|
|
|
|
end;
|
|
|
|
|
|
|
|
class function TMyToolbarController.toolbarDefaultItemIdentifiers(_self: objc.id;
|
|
|
|
_cmd: SEL; toolbar: objc.id): CFArrayRef; cdecl;
|
|
|
|
begin
|
|
|
|
Result := toolbarAllowedItemIdentifiers(_self, _cmd, toolbar);
|
|
|
|
end;
|
|
|
|
|
2008-10-06 02:46:47 +00:00
|
|
|
function toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar(_self: objc.id;
|
2008-09-29 00:47:50 +00:00
|
|
|
_cmd: SEL; toolbar: objc.id {NSToolbar}; itemIdentifier: CFStringRef;
|
2008-10-06 02:46:47 +00:00
|
|
|
flag: OBJC_BOOL): objc.id {NSToolbarItem}; cdecl;
|
2008-09-29 00:47:50 +00:00
|
|
|
begin
|
2008-10-06 02:46:47 +00:00
|
|
|
WriteLn('toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar: ', IntToHex(Cardinal(itemIdentifier), 8));
|
|
|
|
|
|
|
|
with myToolbarController do
|
2008-09-29 00:47:50 +00:00
|
|
|
begin
|
2008-10-06 02:46:47 +00:00
|
|
|
if CFStringCompare(itemIdentifier, OpenToolbarItemIdentifier, kCFCompareCaseInsensitive) = kCFCompareEqualTo then
|
|
|
|
Result := OpenToolbarItem.autorelease
|
2008-09-29 00:47:50 +00:00
|
|
|
else if CFStringCompare(itemIdentifier, SaveToolbarItemIdentifier, kCFCompareCaseInsensitive) = kCFCompareEqualTo then
|
2008-10-06 02:46:47 +00:00
|
|
|
Result := SaveToolbarItem.autorelease
|
2008-09-29 00:47:50 +00:00
|
|
|
else if CFStringCompare(itemIdentifier, CloseToolbarItemIdentifier, kCFCompareCaseInsensitive) = kCFCompareEqualTo then
|
2008-10-06 02:46:47 +00:00
|
|
|
Result := CloseToolbarItem.autorelease
|
2008-09-29 00:47:50 +00:00
|
|
|
else
|
|
|
|
Result := nil;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|