2008-10-06 13:26:48 +00:00
|
|
|
{
|
|
|
|
mytoolbar.pas
|
|
|
|
|
|
|
|
Toolbar controller class. Creates and manages the toolbar.
|
|
|
|
|
|
|
|
This example project is released under public domain
|
|
|
|
|
|
|
|
AUTHORS: Felipe Monteiro de Carvalho
|
|
|
|
}
|
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
|
|
|
{ Toolbar items }
|
|
|
|
OpenToolbarItem, SaveToolbarItem, CloseToolbarItem: NSToolbarItem;
|
2008-09-29 00:47:50 +00:00
|
|
|
{ Objective-c Methods }
|
|
|
|
class function toolbarAllowedItemIdentifiers(_self: objc.id;
|
2008-10-06 13:26:48 +00:00
|
|
|
_cmd: SEL; toolbar: objc.id {NSToolbar}): CFArrayRef; cdecl; {$ifndef VER2_2_2}static;{$endif}
|
2008-09-29 00:47:50 +00:00
|
|
|
class function toolbarDefaultItemIdentifiers(_self: objc.id;
|
2008-10-06 13:26:48 +00:00
|
|
|
_cmd: SEL; toolbar: objc.id {NSToolbar}): CFArrayRef; cdecl; {$ifndef VER2_2_2}static;{$endif}
|
2008-09-29 00:47:50 +00:00
|
|
|
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;
|
2008-10-06 13:26:48 +00:00
|
|
|
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);
|
2008-10-06 13:26:48 +00:00
|
|
|
OpenToolbarItem.setToolTip(CFStringCreateWithPascalString(nil, 'Open', kCFStringEncodingUTF8));
|
2008-10-06 02:46:47 +00:00
|
|
|
OpenToolbarItem.setImage(myModel.imgOpen.Handle);
|
2008-10-08 11:28:15 +00:00
|
|
|
OpenToolbarItem.setTarget(myController.Handle);
|
|
|
|
OpenToolbarItem.setAction(sel_registerName(PChar('doOpenFile:')));
|
2008-10-06 02:46:47 +00:00
|
|
|
|
|
|
|
SaveToolbarItem := NSToolbarItem.initWithItemIdentifier(SaveToolbarItemIdentifier);
|
2008-10-06 13:26:48 +00:00
|
|
|
SaveToolbarItem.setToolTip(CFStringCreateWithPascalString(nil, 'Save', kCFStringEncodingUTF8));
|
2008-10-06 02:46:47 +00:00
|
|
|
SaveToolbarItem.setImage(myModel.imgSave.Handle);
|
2008-10-08 11:28:15 +00:00
|
|
|
SaveToolbarItem.setTarget(myController.Handle);
|
|
|
|
SaveToolbarItem.setAction(sel_registerName(PChar('doSaveFile:')));
|
2008-10-06 02:46:47 +00:00
|
|
|
|
|
|
|
CloseToolbarItem := NSToolbarItem.initWithItemIdentifier(CloseToolbarItemIdentifier);
|
2008-10-06 13:26:48 +00:00
|
|
|
CloseToolbarItem.setToolTip(CFStringCreateWithPascalString(nil, 'Exit', kCFStringEncodingUTF8));
|
2008-10-06 02:46:47 +00:00
|
|
|
CloseToolbarItem.setImage(myModel.imgClose.Handle);
|
2008-10-08 11:28:15 +00:00
|
|
|
CloseToolbarItem.setTarget(myController.Handle);
|
|
|
|
CloseToolbarItem.setAction(sel_registerName(PChar('doClose:')));
|
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;
|
|
|
|
|
2008-10-06 13:26:48 +00:00
|
|
|
{@@
|
|
|
|
Lists all of the items in the toolbar by their identifiers.
|
|
|
|
This method is necessary to implement a toolbar.
|
|
|
|
}
|
2008-09-29 00:47:50 +00:00
|
|
|
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 13:26:48 +00:00
|
|
|
{ WriteLn('OpenToolbarItemIdentifier: ', IntToHex(Cardinal(OpenToolbarItemIdentifier), 8));
|
2008-10-06 02:46:47 +00:00
|
|
|
WriteLn('SaveToolbarItemIdentifier: ', IntToHex(Cardinal(SaveToolbarItemIdentifier), 8));
|
|
|
|
WriteLn('NSToolbarSpaceItemIdentifier: ', IntToHex(Cardinal(NSToolbarSpaceItemIdentifier), 8));
|
2008-10-06 13:26:48 +00:00
|
|
|
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;
|
|
|
|
|
2008-10-06 13:26:48 +00:00
|
|
|
{@@
|
|
|
|
Returns which items are available by default in the toolbar.
|
|
|
|
Other items exist in the toolbar but are optional.
|
|
|
|
We simply return the same items from toolbarAllowedItemIdentifiers.
|
|
|
|
}
|
2008-09-29 00:47:50 +00:00
|
|
|
class function TMyToolbarController.toolbarDefaultItemIdentifiers(_self: objc.id;
|
|
|
|
_cmd: SEL; toolbar: objc.id): CFArrayRef; cdecl;
|
|
|
|
begin
|
|
|
|
Result := toolbarAllowedItemIdentifiers(_self, _cmd, toolbar);
|
|
|
|
end;
|
|
|
|
|
2008-10-06 13:26:48 +00:00
|
|
|
{@@
|
|
|
|
Returns the NSToolbarItem for each item.
|
|
|
|
Note that we should return an item in our cache instead of creating a new one
|
|
|
|
everytime this routine is called.
|
|
|
|
}
|
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 13:26:48 +00:00
|
|
|
flag: objc.BOOL): objc.id {NSToolbarItem}; cdecl;
|
2008-09-29 00:47:50 +00:00
|
|
|
begin
|
2008-10-06 13:26:48 +00:00
|
|
|
// WriteLn('toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar: ', IntToHex(Cardinal(itemIdentifier), 8));
|
2008-10-06 02:46:47 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|