Improves texteditor example

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@583 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
sekelsenmat
2008-10-06 02:46:47 +00:00
parent 07b9c83841
commit b6a6f21e34
12 changed files with 422 additions and 114 deletions

View File

@ -24,11 +24,11 @@ type
constructor Create; override; constructor Create; override;
procedure AddMethods; override; procedure AddMethods; override;
{ Objective-c Methods } { Objective-c Methods }
class procedure doShowStatusitem(_self: objc.id; _cmd: objc.SEL; sender: objc.id); cdecl; static; class procedure doShowStatusitem(_self: objc.id; _cmd: objc.SEL; sender: objc.id); cdecl; //static;
class procedure doHideStatusitem(_self: objc.id; _cmd: objc.SEL; sender: objc.id); cdecl; static; class procedure doHideStatusitem(_self: objc.id; _cmd: objc.SEL; sender: objc.id); cdecl; //static;
class procedure doClose(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; static; class procedure doClose(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; //static;
class function applicationShouldTerminateAfterLastWindowClosed(_self: objc.id; class function applicationShouldTerminateAfterLastWindowClosed(_self: objc.id;
_cmd: SEL; theApplication: objc.id): cbool; cdecl; static; _cmd: SEL; theApplication: objc.id): cbool; cdecl; //static;
{ Other helper functions } { Other helper functions }
function GetResourcesDir: string; function GetResourcesDir: string;
function CreateButton(AView: NSView; ATitle: shortstring; function CreateButton(AView: NSView; ATitle: shortstring;

View File

@ -0,0 +1,4 @@
{
IBClasses = ({CLASS = FirstResponder; LANGUAGE = ObjC; SUPERCLASS = NSObject; });
IBVersion = 1;
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IBDocumentLocation</key>
<string>69 85 356 240 0 0 1280 778 </string>
<key>IBEditorPositions</key>
<dict>
<key>29</key>
<string>69 330 338 44 0 0 1280 778 </string>
</dict>
<key>IBFramework Version</key>
<string>446.1</string>
<key>IBOpenObjects</key>
<array>
<integer>29</integer>
</array>
<key>IBSystem Version</key>
<string>8S2167</string>
</dict>
</plist>

Binary file not shown.

View File

@ -1,4 +1,4 @@
rm *.o
rm *.ppu
rm *.bak rm *.bak
rm *.ppu
rm *.o
rm *.compiled rm *.compiled

View File

@ -27,7 +27,6 @@ type
class procedure doClose(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; //static; class procedure doClose(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; //static;
class procedure doOpenFile(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; //static; class procedure doOpenFile(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; //static;
class procedure doSaveFile(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; //static; class procedure doSaveFile(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; //static;
class procedure doSaveFileAs(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; //static;
class function applicationShouldTerminateAfterLastWindowClosed(_self: objc.id; class function applicationShouldTerminateAfterLastWindowClosed(_self: objc.id;
_cmd: SEL; theApplication: objc.id): cbool; cdecl; //static; _cmd: SEL; theApplication: objc.id): cbool; cdecl; //static;
end; end;
@ -36,7 +35,6 @@ const
Str_doClose = 'doClose:'; Str_doClose = 'doClose:';
Str_doOpenFile = 'doOpenFile:'; Str_doOpenFile = 'doOpenFile:';
Str_doSaveFile = 'doSaveFile:'; Str_doSaveFile = 'doSaveFile:';
Str_doSaveFileAs = 'doSaveFileAs:';
Str_applicationShouldTerminateAfterLastWindowClosed = 'applicationShouldTerminateAfterLastWindowClosed:'; Str_applicationShouldTerminateAfterLastWindowClosed = 'applicationShouldTerminateAfterLastWindowClosed:';
var var
@ -64,8 +62,7 @@ begin
AddMethod(Str_doClose, 'v@:@', Pointer(doClose)); AddMethod(Str_doClose, 'v@:@', Pointer(doClose));
AddMethod(Str_doOpenFile, 'v@:@', Pointer(doOpenFile)); AddMethod(Str_doOpenFile, 'v@:@', Pointer(doOpenFile));
AddMethod(Str_doSaveFile, 'v@:@', Pointer(doSaveFile)); AddMethod(Str_doSaveFile, 'v@:@', Pointer(doSaveFile));
AddMethod(Str_doSaveFileAs, 'v@:@', Pointer(doSaveFileAs)); AddMethod(Str_applicationShouldTerminateAfterLastWindowClosed, 'B@:@',
AddMethod(Str_applicationShouldTerminateAfterLastWindowClosed, 'b@:@',
Pointer(applicationShouldTerminateAfterLastWindowClosed)); Pointer(applicationShouldTerminateAfterLastWindowClosed));
end; end;
@ -123,12 +120,6 @@ begin
end; end;
end; end;
class procedure TMyController.doSaveFileAs(_self: objc.id; _cmd: SEL;
sender: objc.id); cdecl;
begin
end;
class function TMyController.applicationShouldTerminateAfterLastWindowClosed(_self: objc.id; class function TMyController.applicationShouldTerminateAfterLastWindowClosed(_self: objc.id;
_cmd: SEL; theApplication: objc.id): cbool; cdecl; _cmd: SEL; theApplication: objc.id): cbool; cdecl;
begin begin

View File

@ -19,6 +19,7 @@ else
mkdir $appfolder/Contents mkdir $appfolder/Contents
mkdir $appfolder/Contents/MacOS mkdir $appfolder/Contents/MacOS
mkdir $appfolder/Contents/Resources mkdir $appfolder/Contents/Resources
mkdir $appfolder/Contents/Resources/English.lproj
# #
# Instead of copying executable into .app folder after each compile, # Instead of copying executable into .app folder after each compile,
# simply create a symbolic link to executable. # simply create a symbolic link to executable.
@ -27,9 +28,10 @@ else
cp imgOpen.png $appfolder/Contents/Resources/ cp imgOpen.png $appfolder/Contents/Resources/
cp imgSave.png $appfolder/Contents/Resources/ cp imgSave.png $appfolder/Contents/Resources/
cp imgClose.png $appfolder/Contents/Resources/ cp imgClose.png $appfolder/Contents/Resources/
cp -r applemenu.nib $appfolder/Contents/Resources/English.lproj/
# #
# Create PkgInfo file. # Create PkgInfo file.
echo "APPLMAG#" >$appfolder/Contents/PkgInfo echo "TXTEDIT#" >$appfolder/Contents/PkgInfo
# #
# Create information property list file (Info.plist). # Create information property list file (Info.plist).
echo '<?xml version="1.0" encoding="UTF-8"?>' >$plistfile echo '<?xml version="1.0" encoding="UTF-8"?>' >$plistfile
@ -47,11 +49,13 @@ else
echo ' <key>CFBundleInfoDictionaryVersion</key>' >>$plistfile echo ' <key>CFBundleInfoDictionaryVersion</key>' >>$plistfile
echo ' <string>6.0</string>' >>$plistfile echo ' <string>6.0</string>' >>$plistfile
echo ' <key>CFBundlePackageType</key>' >>$plistfile echo ' <key>CFBundlePackageType</key>' >>$plistfile
echo ' <string>APPL</string>' >>$plistfile echo ' <string>EDIT</string>' >>$plistfile
echo ' <key>CFBundleSignature</key>' >>$plistfile echo ' <key>CFBundleSignature</key>' >>$plistfile
echo ' <string>MAG#</string>' >>$plistfile echo ' <string>TXT#</string>' >>$plistfile
echo ' <key>CFBundleVersion</key>' >>$plistfile echo ' <key>CFBundleVersion</key>' >>$plistfile
echo ' <string>1.0</string>' >>$plistfile echo ' <string>1.0</string>' >>$plistfile
echo ' <key>NSMainNibFile</key>' >>$plistfile
echo ' <string>applemenu</string>' >>$plistfile
echo '</dict>' >>$plistfile echo '</dict>' >>$plistfile
echo '</plist>' >>$plistfile echo '</plist>' >>$plistfile
fi fi

View File

@ -62,9 +62,9 @@ end;
constructor TMyModel.Create; constructor TMyModel.Create;
begin begin
LoadImages;
ResourcesDir := GetResourcesDir; ResourcesDir := GetResourcesDir;
LoadImages;
end; end;
end. end.

View File

@ -5,6 +5,7 @@ unit mytoolbar;
interface interface
uses uses
SysUtils,
MacOSAll, objc, appkit, foundation; MacOSAll, objc, appkit, foundation;
type type
@ -16,15 +17,14 @@ type
{ Extra binding functions } { Extra binding functions }
constructor Create; override; constructor Create; override;
procedure AddMethods; override; procedure AddMethods; override;
procedure AttachEventHandlers;
{ Toolbar items }
OpenToolbarItem, SaveToolbarItem, CloseToolbarItem: NSToolbarItem;
{ Objective-c Methods } { Objective-c Methods }
// class procedure doClose(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; //static;
class function toolbarAllowedItemIdentifiers(_self: objc.id; class function toolbarAllowedItemIdentifiers(_self: objc.id;
_cmd: SEL; toolbar: objc.id {NSToolbar}): CFArrayRef; cdecl;// static; _cmd: SEL; toolbar: objc.id {NSToolbar}): CFArrayRef; cdecl;// static;
class function toolbarDefaultItemIdentifiers(_self: objc.id; class function toolbarDefaultItemIdentifiers(_self: objc.id;
_cmd: SEL; toolbar: objc.id {NSToolbar}): CFArrayRef; cdecl;// static; _cmd: SEL; toolbar: objc.id {NSToolbar}): CFArrayRef; cdecl;// static;
class function toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar
(_self: objc.id; _cmd: SEL; toolbar: objc.id;
itemIdentifier: CFStringRef; flag: CBOOL): objc.id; cdecl;// static;
end; end;
const const
@ -34,6 +34,11 @@ const
var var
OpenToolbarItemIdentifier, SaveToolbarItemIdentifier, CloseToolbarItemIdentifier: CFStringRef; OpenToolbarItemIdentifier, SaveToolbarItemIdentifier, CloseToolbarItemIdentifier: CFStringRef;
myToolbarController: TMyToolbarController;
function toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar
(_self: objc.id; _cmd: SEL; toolbar: objc.id;
itemIdentifier: CFStringRef; flag: OBJC_BOOL): objc.id; cdecl;// static;
implementation implementation
@ -54,14 +59,39 @@ begin
OpenToolbarItemIdentifier := CFStringCreateWithPascalString(nil, 'OpenID', kCFStringEncodingUTF8); OpenToolbarItemIdentifier := CFStringCreateWithPascalString(nil, 'OpenID', kCFStringEncodingUTF8);
SaveToolbarItemIdentifier := CFStringCreateWithPascalString(nil, 'SaveID', kCFStringEncodingUTF8); SaveToolbarItemIdentifier := CFStringCreateWithPascalString(nil, 'SaveID', kCFStringEncodingUTF8);
CloseToolbarItemIdentifier := CFStringCreateWithPascalString(nil, 'CloseID', kCFStringEncodingUTF8); CloseToolbarItemIdentifier := CFStringCreateWithPascalString(nil, 'CloseID', kCFStringEncodingUTF8);
{ 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);
end; end;
procedure TMyToolbarController.AddMethods; procedure TMyToolbarController.AddMethods;
begin begin
AddMethod(Str_toolbarAllowedItemIdentifiers, '@@:@', Pointer(toolbarAllowedItemIdentifiers)); AddMethod(Str_toolbarAllowedItemIdentifiers, '@@:@', Pointer(toolbarAllowedItemIdentifiers));
AddMethod(Str_toolbarDefaultItemIdentifiers, '@@:@', Pointer(toolbarDefaultItemIdentifiers)); AddMethod(Str_toolbarDefaultItemIdentifiers, '@@:@', Pointer(toolbarDefaultItemIdentifiers));
// AddMethod(Str_toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar, AddMethod(Str_toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar,
// '@@:@@L', Pointer(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:')));
end; end;
class function TMyToolbarController.toolbarAllowedItemIdentifiers(_self: objc.id; class function TMyToolbarController.toolbarAllowedItemIdentifiers(_self: objc.id;
@ -69,11 +99,14 @@ class function TMyToolbarController.toolbarAllowedItemIdentifiers(_self: objc.id
var var
toolbarItems: array[0..4] of CFStringRef; toolbarItems: array[0..4] of CFStringRef;
begin begin
WriteLn('OpenToolbarItemIdentifier: ', IntToHex(Cardinal(OpenToolbarItemIdentifier), 8));
WriteLn('SaveToolbarItemIdentifier: ', IntToHex(Cardinal(SaveToolbarItemIdentifier), 8));
WriteLn('NSToolbarSpaceItemIdentifier: ', IntToHex(Cardinal(NSToolbarSpaceItemIdentifier), 8));
WriteLn('CloseToolbarItemIdentifier: ', IntToHex(Cardinal(CloseToolbarItemIdentifier), 8));
toolbarItems[0] := OpenToolbarItemIdentifier; toolbarItems[0] := OpenToolbarItemIdentifier;
toolbarItems[1] := SaveToolbarItemIdentifier; toolbarItems[1] := SaveToolbarItemIdentifier;
// toolbarItems[2] := NSToolbarSpaceItemIdentifier; toolbarItems[2] := NSToolbarSpaceItemIdentifier;
toolbarItems[2] := CloseToolbarItemIdentifier; toolbarItems[3] := CloseToolbarItemIdentifier;
toolbarItems[3] := nil;
toolbarItems[4] := nil; toolbarItems[4] := nil;
Result := CFArrayCreate(nil, @toolbarItems[0], 4, nil); Result := CFArrayCreate(nil, @toolbarItems[0], 4, nil);
@ -85,48 +118,23 @@ begin
Result := toolbarAllowedItemIdentifiers(_self, _cmd, toolbar); Result := toolbarAllowedItemIdentifiers(_self, _cmd, toolbar);
end; end;
class function TMyToolbarController.toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar(_self: objc.id; function toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar(_self: objc.id;
_cmd: SEL; toolbar: objc.id {NSToolbar}; itemIdentifier: CFStringRef; _cmd: SEL; toolbar: objc.id {NSToolbar}; itemIdentifier: CFStringRef;
flag: CBOOL): objc.id {NSToolbarItem}; cdecl; flag: OBJC_BOOL): objc.id {NSToolbarItem}; cdecl;
var
toolbarItem: NSToolbarItem;
begin begin
WriteLn('toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar: ', IntToHex(Cardinal(itemIdentifier), 8));
with myToolbarController do
begin
if CFStringCompare(itemIdentifier, OpenToolbarItemIdentifier, kCFCompareCaseInsensitive) = kCFCompareEqualTo then if CFStringCompare(itemIdentifier, OpenToolbarItemIdentifier, kCFCompareCaseInsensitive) = kCFCompareEqualTo then
begin Result := OpenToolbarItem.autorelease
toolbarItem := NSToolbarItem.initWithItemIdentifier(itemIdentifier);
// [toolbarItem setLabel:@"Save"];
// [toolbarItem setPaletteLabel:[toolbarItem label]];
// [toolbarItem setToolTip:@"Save Your Passwords"];}
toolbarItem.setImage(myModel.imgOpen.Handle);
toolbarItem.setTarget(myController.Handle);
toolbarItem.setAction(sel_registerName(PChar('doOpenFile:')));
end
else if CFStringCompare(itemIdentifier, SaveToolbarItemIdentifier, kCFCompareCaseInsensitive) = kCFCompareEqualTo then else if CFStringCompare(itemIdentifier, SaveToolbarItemIdentifier, kCFCompareCaseInsensitive) = kCFCompareEqualTo then
begin Result := SaveToolbarItem.autorelease
toolbarItem := NSToolbarItem.initWithItemIdentifier(itemIdentifier);
toolbarItem.setImage(myModel.imgSave.Handle);
toolbarItem.setTarget(myController.Handle);
toolbarItem.setAction(sel_registerName(PChar('doSaveFile:')));
end
else if CFStringCompare(itemIdentifier, CloseToolbarItemIdentifier, kCFCompareCaseInsensitive) = kCFCompareEqualTo then else if CFStringCompare(itemIdentifier, CloseToolbarItemIdentifier, kCFCompareCaseInsensitive) = kCFCompareEqualTo then
begin Result := CloseToolbarItem.autorelease
toolbarItem := NSToolbarItem.initWithItemIdentifier(itemIdentifier);
toolbarItem.setImage(myModel.imgClose.Handle);
toolbarItem.setTarget(myController.Handle);
toolbarItem.setAction(sel_registerName(PChar('doCloseFile:')));
end
else else
begin
Result := nil; Result := nil;
Exit
end; end;
Result := ToolbarItem.autorelease;
{ ToolbarItem.Handle := 0;
ToolbarItem.Free;}
// Result := Result. [toolbarItem autorelease];
end; end;
end. end.

View File

@ -4,13 +4,14 @@
<PathDelim Value="/"/> <PathDelim Value="/"/>
<Version Value="6"/> <Version Value="6"/>
<General> <General>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/> <MainUnit Value="0"/>
<IconPath Value="./"/>
<TargetFileExt Value=""/> <TargetFileExt Value=""/>
<ActiveEditorIndexAtStart Value="7"/>
</General> </General>
<VersionInfo> <VersionInfo>
<ProjectVersion Value=""/> <ProjectVersion Value=""/>
<Language Value=""/>
<CharSet Value=""/>
</VersionInfo> </VersionInfo>
<PublishOptions> <PublishOptions>
<Version Value="2"/> <Version Value="2"/>
@ -29,33 +30,213 @@
<PackageName Value="cocoa_pkg"/> <PackageName Value="cocoa_pkg"/>
</Item1> </Item1>
</RequiredPackages> </RequiredPackages>
<Units Count="5"> <Units Count="9">
<Unit0> <Unit0>
<Filename Value="texteditor.pas"/> <Filename Value="texteditor.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="texteditor"/> <UnitName Value="texteditor"/>
<CursorPos X="15" Y="44"/>
<TopLine Value="31"/>
<EditorIndex Value="0"/>
<UsageCount Value="31"/>
<Loaded Value="True"/>
</Unit0> </Unit0>
<Unit1> <Unit1>
<Filename Value="model.pas"/> <Filename Value="model.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="model"/> <UnitName Value="model"/>
<CursorPos X="33" Y="63"/>
<TopLine Value="48"/>
<EditorIndex Value="5"/>
<UsageCount Value="31"/>
<Loaded Value="True"/>
</Unit1> </Unit1>
<Unit2> <Unit2>
<Filename Value="view.pas"/> <Filename Value="view.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="view"/> <UnitName Value="view"/>
<CursorPos X="28" Y="217"/>
<TopLine Value="204"/>
<EditorIndex Value="7"/>
<UsageCount Value="31"/>
<Loaded Value="True"/>
</Unit2> </Unit2>
<Unit3> <Unit3>
<Filename Value="controller.pas"/> <Filename Value="controller.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="controller"/> <UnitName Value="controller"/>
<CursorPos X="25" Y="30"/>
<TopLine Value="19"/>
<EditorIndex Value="6"/>
<UsageCount Value="31"/>
<Loaded Value="True"/>
</Unit3> </Unit3>
<Unit4> <Unit4>
<Filename Value="mytoolbar.pas"/> <Filename Value="mytoolbar.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="mytoolbar"/> <UnitName Value="mytoolbar"/>
<CursorPos X="1" Y="106"/>
<TopLine Value="97"/>
<EditorIndex Value="1"/>
<UsageCount Value="26"/>
<Loaded Value="True"/>
</Unit4> </Unit4>
<Unit5>
<Filename Value="../../../objc/objc.pas"/>
<UnitName Value="objc"/>
<CursorPos X="14" Y="48"/>
<TopLine Value="29"/>
<EditorIndex Value="4"/>
<UsageCount Value="12"/>
<Loaded Value="True"/>
</Unit5>
<Unit6>
<Filename Value="../../foundation/foundation.pas"/>
<UnitName Value="foundation"/>
<CursorPos X="30" Y="12"/>
<TopLine Value="1"/>
<EditorIndex Value="3"/>
<UsageCount Value="12"/>
<Loaded Value="True"/>
</Unit6>
<Unit7>
<Filename Value="../../appkit/NSToolbarItem.inc"/>
<CursorPos X="49" Y="5"/>
<TopLine Value="4"/>
<UsageCount Value="10"/>
</Unit7>
<Unit8>
<Filename Value="../../appkit/appkit.pas"/>
<UnitName Value="appkit"/>
<CursorPos X="16" Y="12"/>
<TopLine Value="1"/>
<EditorIndex Value="2"/>
<UsageCount Value="12"/>
<Loaded Value="True"/>
</Unit8>
</Units> </Units>
<JumpHistory Count="30" HistoryIndex="29">
<Position1>
<Filename Value="mytoolbar.pas"/>
<Caret Line="73" Column="1" TopLine="61"/>
</Position1>
<Position2>
<Filename Value="mytoolbar.pas"/>
<Caret Line="74" Column="1" TopLine="62"/>
</Position2>
<Position3>
<Filename Value="mytoolbar.pas"/>
<Caret Line="75" Column="1" TopLine="63"/>
</Position3>
<Position4>
<Filename Value="mytoolbar.pas"/>
<Caret Line="76" Column="1" TopLine="64"/>
</Position4>
<Position5>
<Filename Value="mytoolbar.pas"/>
<Caret Line="78" Column="1" TopLine="66"/>
</Position5>
<Position6>
<Filename Value="mytoolbar.pas"/>
<Caret Line="72" Column="1" TopLine="67"/>
</Position6>
<Position7>
<Filename Value="mytoolbar.pas"/>
<Caret Line="74" Column="92" TopLine="61"/>
</Position7>
<Position8>
<Filename Value="mytoolbar.pas"/>
<Caret Line="8" Column="12" TopLine="1"/>
</Position8>
<Position9>
<Filename Value="mytoolbar.pas"/>
<Caret Line="126" Column="30" TopLine="115"/>
</Position9>
<Position10>
<Filename Value="mytoolbar.pas"/>
<Caret Line="21" Column="55" TopLine="2"/>
</Position10>
<Position11>
<Filename Value="mytoolbar.pas"/>
<Caret Line="101" Column="111" TopLine="92"/>
</Position11>
<Position12>
<Filename Value="mytoolbar.pas"/>
<Caret Line="75" Column="5" TopLine="52"/>
</Position12>
<Position13>
<Filename Value="view.pas"/>
<Caret Line="101" Column="20" TopLine="90"/>
</Position13>
<Position14>
<Filename Value="view.pas"/>
<Caret Line="102" Column="13" TopLine="89"/>
</Position14>
<Position15>
<Filename Value="mytoolbar.pas"/>
<Caret Line="65" Column="1" TopLine="58"/>
</Position15>
<Position16>
<Filename Value="mytoolbar.pas"/>
<Caret Line="20" Column="34" TopLine="8"/>
</Position16>
<Position17>
<Filename Value="mytoolbar.pas"/>
<Caret Line="37" Column="22" TopLine="28"/>
</Position17>
<Position18>
<Filename Value="view.pas"/>
<Caret Line="99" Column="12" TopLine="80"/>
</Position18>
<Position19>
<Filename Value="view.pas"/>
<Caret Line="108" Column="1" TopLine="85"/>
</Position19>
<Position20>
<Filename Value="view.pas"/>
<Caret Line="20" Column="22" TopLine="9"/>
</Position20>
<Position21>
<Filename Value="view.pas"/>
<Caret Line="178" Column="5" TopLine="155"/>
</Position21>
<Position22>
<Filename Value="view.pas"/>
<Caret Line="181" Column="5" TopLine="158"/>
</Position22>
<Position23>
<Filename Value="view.pas"/>
<Caret Line="23" Column="27" TopLine="13"/>
</Position23>
<Position24>
<Filename Value="view.pas"/>
<Caret Line="219" Column="1" TopLine="210"/>
</Position24>
<Position25>
<Filename Value="view.pas"/>
<Caret Line="23" Column="8" TopLine="14"/>
</Position25>
<Position26>
<Filename Value="view.pas"/>
<Caret Line="218" Column="1" TopLine="209"/>
</Position26>
<Position27>
<Filename Value="view.pas"/>
<Caret Line="131" Column="1" TopLine="114"/>
</Position27>
<Position28>
<Filename Value="view.pas"/>
<Caret Line="116" Column="44" TopLine="104"/>
</Position28>
<Position29>
<Filename Value="view.pas"/>
<Caret Line="192" Column="35" TopLine="180"/>
</Position29>
<Position30>
<Filename Value="controller.pas"/>
<Caret Line="74" Column="15" TopLine="59"/>
</Position30>
</JumpHistory>
</ProjectOptions> </ProjectOptions>
<CompilerOptions> <CompilerOptions>
<Version Value="8"/> <Version Value="8"/>

View File

@ -17,8 +17,8 @@ program texteditor;
uses uses
Math, Math,
objc, ctypes, MacOSAll, AppKit, Foundation, objc, ctypes, MacOSAll, AppKit, Foundation,
controller, model, view, {$ifndef WITHOUT_PKG}cocoa_pkg,{$endif}
cocoa_pkg, mytoolbar; controller, model, view, mytoolbar;
var var
pool: NSAutoreleasePool; pool: NSAutoreleasePool;
@ -33,20 +33,28 @@ begin
{ Creates the application NSApp object } { Creates the application NSApp object }
NSApp := NSApplication.sharedApplication; NSApp := NSApplication.sharedApplication;
{ Initializes the controller, view and model objects } { Initializes the model object,
must be after the view so that the resources are loaded }
myModel := TMyModel.Create();
{ Initializes the view object }
myView := TMyView.Create;
myView.CreateUserInterface;
{ The controller needs to be the last to be creates for an unknown reason,
and we can only attach the events after the controller is created }
myController := TMyController.Create(); myController := TMyController.Create();
myModel := TMyModel.Create();
myView := TMyView.Create();
{ Creates the user interface } myView.AttachEventHandlers();
myToolbarController.AttachEventHandlers();
myView.CreateUserInterface();
myView.MainWindow.orderFrontRegardless;
{ Enters main message loop } { Enters main message loop }
myView.MainWindow.orderFrontRegardless;
NSApp.setDelegate(myController.Handle); NSApp.setDelegate(myController.Handle);
NSApp.run; NSApp.run;

View File

@ -16,27 +16,29 @@ type
TMyView = class TMyView = class
private private
{ Other helper functions } { Other helper functions }
procedure AddToMenubar(menu: NSMenu);
function CreateAppleMenu(): NSMenu;
function CreateFileMenu(): NSMenu;
procedure CreateMainMenu();
function CreateMenuItem(ATitle: shortstring): NSMenuItem;
function CreateToolbar(AOwnerView: NSView; AX, AY, AWidth, AHeight: Double): NSToolbar; function CreateToolbar(AOwnerView: NSView; AX, AY, AWidth, AHeight: Double): NSToolbar;
function CreateMainMenu(): NSMenu;
function CreateMenuItem(ATitle: shortstring;
ACallbackName: string; ACallbackClass: NSObject): NSMenuItem;
function CreateTextField(): NSTextField; function CreateTextField(): NSTextField;
public public
{ classes } { classes }
MainWindow: NSWindow; MainWindow: NSWindow;
MainWindowView: NSView; MainWindowView: NSView;
Toolbar: NSToolbar; Toolbar: NSToolbar;
ToolbarController: TMyToolbarController;
MainMenu: NSMenu;
TextField: NSTextField; TextField: NSTextField;
FileMenuItem, OpenItem, SaveItem, SaveAsItem, ExitItem: NSMenuItem; MainMenu, AppleMenu, ServicesMenu, FileMenu: NSMenu;
FileMenu: NSMenu; OpenItem, SaveItem, SaveAsItem, ExitItem: NSMenuItem;
{ strings and sizes} { strings and sizes}
CFWindowTitle, CFEmptyString: CFStringRef; CFWindowTitle, CFEmptyString: CFStringRef;
MainWindowRect: NSRect; MainWindowRect: NSRect;
MenuTitle: CFStringRef;
{ methods } { methods }
procedure CreateUserInterface(); procedure CreateUserInterface();
procedure AttachEventHandlers();
end; end;
var var
@ -82,10 +84,114 @@ begin
{ Add the main menu } { Add the main menu }
MainMenu := CreateMainMenu(); CreateMainMenu();
end;
NSApp.setAppleMenu(MainMenu.Handle); procedure TMyView.AttachEventHandlers();
NSApp.setMainMenu(MainMenu.Handle); begin
OpenItem.setTarget(myController.Handle);
OpenItem.setAction(sel_registerName(PChar('doOpenFile:')));
SaveItem.setTarget(myController.Handle);
SaveItem.setAction(sel_registerName(PChar('doSaveFile:')));
ExitItem.setTarget(myController.Handle);
ExitItem.setAction(sel_registerName(PChar('doClose:')));
end;
function TMyView.CreateAppleMenu(): NSMenu;
var
AppleMenuTitle, ServicesMenuTitle: CFStringRef;
begin
AppleMenuTitle := CFStringCreateWithPascalString(nil, 'Apple Menu', kCFStringEncodingUTF8);
ServicesMenuTitle := CFStringCreateWithPascalString(nil, 'Services', kCFStringEncodingUTF8);
{ Creates the Apple menu }
Result := NSMenu.initWithTitle(AppleMenuTitle);
{ Add the services submenu }
ServicesMenu := NSMenu.initWithTitle(ServicesMenuTitle);
NSApp.setServicesMenu(ServicesMenu.Handle);
{
NSMenuItem *menuitem;
// Create the application (Apple) menu.
NSMenu *menuApp = [[NSMenu alloc] initWithTitle: @""];
NSMenu *menuServices = [[NSMenu alloc] initWithTitle: @"Services"];
[NSApp setServicesMenu:menuServices];
menuitem = [[NSMenuItem alloc] initWithTitle:@"Preferences..." action:nil keyEquivalent:@","];
[menuApp addItem: menuitem];
SetPreferencesMenuItem(menuitem);
[menuApp addItem: [NSMenuItem separatorItem]];
menuitem = [[NSMenuItem alloc] initWithTitle: @"Services"
action:nil keyEquivalent:@""];
[menuitem setSubmenu:menuServices];
[menuApp addItem: menuitem];
[menuitem release];
[menuApp addItem: [NSMenuItem separatorItem]];
menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide"
action:@selector(hide:) keyEquivalent:@""];
[menuitem setTarget: NSApp];
[menuApp addItem: menuitem];
[menuitem release];
menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide Others"
action:@selector(hideOtherApplications:) keyEquivalent:@""];
[menuitem setTarget: NSApp];
[menuApp addItem: menuitem];
[menuitem release];
menuitem = [[NSMenuItem alloc] initWithTitle:@"Show All"
action:@selector(unhideAllApplications:) keyEquivalent:@""];
[menuitem setTarget: NSApp];
[menuApp addItem: menuitem];
[menuitem release];
[menuApp addItem: [NSMenuItem separatorItem]];
menuitem = [[NSMenuItem alloc] initWithTitle:@"Quit"
action:@selector(terminate:) keyEquivalent:@"q"];
[menuitem setTarget: NSApp];
[menuApp addItem: menuitem];
SetQuitMenuItem(menuitem);
[menuitem release];
[NSApp setAppleMenu:menuApp];
AddToMenubar(menuApp);
[menuApp release];
return PR_SUCCESS;
}
end;
function TMyView.CreateFileMenu(): NSMenu;
var
MenuTitle: CFStringRef;
begin
MenuTitle := CFStringCreateWithPascalString(nil, 'File', kCFStringEncodingUTF8);
{ Creates the file menu }
Result := NSMenu.initWithTitle(MenuTitle);
{ Adds items to it }
OpenItem := CreateMenuItem('Open');
Result.addItem(OpenItem.Handle);
SaveItem := CreateMenuItem('Save');
Result.addItem(SaveItem.Handle);
ExitItem := CreateMenuItem('Exit');
Result.addItem(ExitItem.Handle);
end;
procedure TMyView.AddToMenubar(menu: NSMenu);
var
dummyItem: NSMenuItem;
begin
dummyItem := NSMenuItem.initWithTitle_action_keyEquivalent(CFEmptyString, nil, CFEmptyString);
dummyItem.setSubmenu(menu.Handle);
MainMenu.addItem(dummyItem.Handle);
dummyItem.Free;
end; end;
{@@ {@@
@ -94,42 +200,29 @@ function TMyView.CreateToolbar(AOwnerView: NSView; AX, AY, AWidth,
AHeight: Double): NSToolbar; AHeight: Double): NSToolbar;
begin begin
Result := NSToolbar.initWithIdentifier(CFEmptyString); Result := NSToolbar.initWithIdentifier(CFEmptyString);
ToolbarController := TMyToolbarController.Create; myToolbarController := TMyToolbarController.Create;
Result.setDelegate(ToolbarController.Handle); Result.setDelegate(myToolbarController.Handle);
end; end;
{@@ {@@
} }
function TMyView.CreateMainMenu(): NSMenu; procedure TMyView.CreateMainMenu();
begin begin
MenuTitle := CFStringCreateWithPascalString(nil, 'Title', kCFStringEncodingUTF8); MainMenu := NSMenu.initWithTitle(CFEmptyString);
WriteLn('CreateMenu');
Result := NSMenu.initWithTitle(CFEmptyString);
WriteLn('Menu Created');
{ Creates the file menu } NSApp.setMainMenu(MainMenu.Handle);
FileMenu := NSMenu.initWithTitle(MenuTitle); AppleMenu := CreateAppleMenu();
FileMenuItem := NSMenuItem.initWithTitle_action_keyEquivalent(CFEmptyString, nil, CFEmptyString); NSApp.setAppleMenu(AppleMenu.Handle);
FileMenuItem.setSubmenu(FileMenu.Handle); AddToMenubar(AppleMenu);
Result.addItem(FileMenuItem.Handle);
{ Adds items to it } FileMenu := CreateFileMenu();
AddToMenubar(FileMenu);
OpenItem := CreateMenuItem('Open', 'doOpenFile:', myController);
FileMenu.addItem(OpenItem.Handle);
OpenItem := CreateMenuItem('Save', 'doSaveFile:', myController);
FileMenu.addItem(OpenItem.Handle);
OpenItem := CreateMenuItem('Save As', 'doSaveFileAs:', myController);
FileMenu.addItem(OpenItem.Handle);
OpenItem := CreateMenuItem('Exit', 'doClose:', myController);
FileMenu.addItem(OpenItem.Handle);
end; end;
{@@ {@@
} }
function TMyView.CreateMenuItem(ATitle: shortstring; function TMyView.CreateMenuItem(ATitle: shortstring): NSMenuItem;
ACallbackName: string; ACallbackClass: NSObject): NSMenuItem;
var var
ItemText: CFStringRef; ItemText: CFStringRef;
KeyText: CFStringRef; KeyText: CFStringRef;
@ -139,8 +232,6 @@ begin
WriteLn(' ItemText: ', IntToHex(Int64(ItemText), 8), ' ATitle: ', ATitle); WriteLn(' ItemText: ', IntToHex(Int64(ItemText), 8), ' ATitle: ', ATitle);
Result := NSMenuItem.initWithTitle_action_keyEquivalent(ItemText, nil, KeyText); Result := NSMenuItem.initWithTitle_action_keyEquivalent(ItemText, nil, KeyText);
Result.setTarget(ACallbackClass.Handle);
Result.setAction(sel_registerName(PChar(ACallbackName)));
end; end;
function TMyView.CreateTextField(): NSTextField; function TMyView.CreateTextField(): NSTextField;