Improves the controller of the statusitem example to be a separate objective-c class.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@378 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
sekelsenmat 2008-03-14 01:05:09 +00:00
parent 43d2119108
commit 14130ff8dd
4 changed files with 219 additions and 65 deletions

View File

@ -7,7 +7,7 @@
}
unit controller;
{$mode delphi}
{$mode delphi}{$STATIC ON}
interface
@ -22,12 +22,13 @@ type
public
{ Extra binding functions }
constructor Create; override;
function getClass: objc.id; override;
procedure AddMethods;
{ Objective-c Methods }
class procedure doShowStatusitem(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl;
class procedure doHideStatusitem(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl;
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;
class function applicationShouldTerminateAfterLastWindowClosed(_self: objc.id;
_cmd: SEL; theApplication: objc.id): cbool; cdecl;
_cmd: SEL; theApplication: objc.id): cbool; cdecl; static;
{ Other helper functions }
function GetResourcesDir: string;
function CreateButton(AView: NSView; ATitle: shortstring;
@ -40,6 +41,8 @@ type
end;
const
Str_TMyController = 'TMyController';
Str_doShowStatusitem = 'doShowStatusitem:';
Str_doHideStatusitem = 'doHideStatusitem:';
Str_applicationShouldTerminateAfterLastWindowClosed = 'applicationShouldTerminateAfterLastWindowClosed:';
@ -72,6 +75,8 @@ constructor TMyController.Create;
var
fileName: CFStringRef;
begin
if not CreateClassDefinition(Str_TMyController, Str_NSObject) then WriteLn('Failed to create objc class');
inherited Create;
AddMethods();
@ -82,6 +87,11 @@ begin
image := NSImage.initWithContentsOfFile(fileName);
end;
function TMyController.getClass: objc.id;
begin
Result := objc_getClass(Str_TMyController);
end;
{ Objective-c Methods }
class procedure TMyController.doShowStatusitem(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl;

View File

@ -26,15 +26,15 @@
<LaunchingApplication PathPlusParams="/usr/X11R6/bin/xterm -T 'Lazarus Run Output' -e $(LazarusDir)/tools/runwait.sh $(TargetCmdLine)"/>
</local>
</RunParams>
<Units Count="37">
<Units Count="49">
<Unit0>
<Filename Value="statusitem.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="statusitem"/>
<CursorPos X="18" Y="36"/>
<TopLine Value="1"/>
<CursorPos X="11" Y="60"/>
<TopLine Value="49"/>
<EditorIndex Value="0"/>
<UsageCount Value="44"/>
<UsageCount Value="50"/>
<Loaded Value="True"/>
</Unit0>
<Unit1>
@ -52,33 +52,30 @@
</Unit2>
<Unit3>
<Filename Value="../../appkit/NSApplication.inc"/>
<CursorPos X="3" Y="140"/>
<CursorPos X="50" Y="133"/>
<TopLine Value="130"/>
<EditorIndex Value="5"/>
<UsageCount Value="13"/>
<Loaded Value="True"/>
<UsageCount Value="15"/>
</Unit3>
<Unit4>
<Filename Value="../../appkit/NSAlert.inc"/>
<CursorPos X="1" Y="12"/>
<TopLine Value="1"/>
<UsageCount Value="24"/>
<UsageCount Value="28"/>
<Loaded Value="True"/>
</Unit4>
<Unit5>
<Filename Value="../../appkit/NSStatusItem.inc"/>
<CursorPos X="1" Y="129"/>
<TopLine Value="113"/>
<EditorIndex Value="2"/>
<UsageCount Value="17"/>
<EditorIndex Value="5"/>
<UsageCount Value="21"/>
<Loaded Value="True"/>
</Unit5>
<Unit6>
<Filename Value="../../appkit/AppKit.inc"/>
<CursorPos X="10" Y="39"/>
<CursorPos X="32" Y="27"/>
<TopLine Value="21"/>
<EditorIndex Value="8"/>
<UsageCount Value="15"/>
<UsageCount Value="19"/>
<Loaded Value="True"/>
</Unit6>
<Unit7>
@ -162,19 +159,16 @@
<Unit19>
<Filename Value="../../appkit/appkit.pas"/>
<UnitName Value="appkit"/>
<CursorPos X="10" Y="39"/>
<CursorPos X="32" Y="27"/>
<TopLine Value="21"/>
<EditorIndex Value="8"/>
<UsageCount Value="23"/>
<UsageCount Value="27"/>
<Loaded Value="True"/>
</Unit19>
<Unit20>
<Filename Value="../../appkit/NSWindow.inc"/>
<CursorPos X="17" Y="99"/>
<CursorPos X="39" Y="95"/>
<TopLine Value="92"/>
<EditorIndex Value="4"/>
<UsageCount Value="19"/>
<Loaded Value="True"/>
<UsageCount Value="21"/>
</Unit20>
<Unit21>
<Filename Value="/System/Library/Frameworks/AppKit.framework/Versions/C/Headers/NSWindow.h"/>
@ -222,17 +216,17 @@
</Unit27>
<Unit28>
<Filename Value="../../foundation/NSObject.inc"/>
<CursorPos X="48" Y="7"/>
<TopLine Value="1"/>
<UsageCount Value="13"/>
<CursorPos X="15" Y="210"/>
<TopLine Value="231"/>
<EditorIndex Value="3"/>
<UsageCount Value="14"/>
<Loaded Value="True"/>
</Unit28>
<Unit29>
<Filename Value="../../appkit/NSStatusBar.inc"/>
<CursorPos X="10" Y="39"/>
<CursorPos X="58" Y="25"/>
<TopLine Value="21"/>
<EditorIndex Value="8"/>
<UsageCount Value="19"/>
<Loaded Value="True"/>
<UsageCount Value="21"/>
</Unit29>
<Unit30>
<Filename Value="../../foundation/NSAutoreleasePool.inc"/>
@ -242,26 +236,26 @@
</Unit30>
<Unit31>
<Filename Value="../../../objc/objc-class.inc"/>
<CursorPos X="45" Y="161"/>
<CursorPos X="11" Y="168"/>
<TopLine Value="159"/>
<UsageCount Value="12"/>
<EditorIndex Value="4"/>
<UsageCount Value="13"/>
<Loaded Value="True"/>
</Unit31>
<Unit32>
<Filename Value="../../../objc/objc.inc"/>
<CursorPos X="10" Y="82"/>
<CursorPos X="65" Y="75"/>
<TopLine Value="73"/>
<EditorIndex Value="6"/>
<UsageCount Value="14"/>
<Loaded Value="True"/>
<UsageCount Value="16"/>
</Unit32>
<Unit33>
<Filename Value="controller.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="controller"/>
<CursorPos X="6" Y="105"/>
<TopLine Value="92"/>
<CursorPos X="1" Y="92"/>
<TopLine Value="86"/>
<EditorIndex Value="1"/>
<UsageCount Value="28"/>
<UsageCount Value="34"/>
<Loaded Value="True"/>
</Unit33>
<Unit34>
@ -273,43 +267,121 @@
</Unit34>
<Unit35>
<Filename Value="../../appkit/NSImage.inc"/>
<CursorPos X="1" Y="185"/>
<CursorPos X="39" Y="172"/>
<TopLine Value="169"/>
<EditorIndex Value="3"/>
<UsageCount Value="26"/>
<Loaded Value="True"/>
<UsageCount Value="28"/>
</Unit35>
<Unit36>
<Filename Value="/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h"/>
<CursorPos X="14" Y="35"/>
<CursorPos X="26" Y="120"/>
<TopLine Value="113"/>
<EditorIndex Value="7"/>
<UsageCount Value="13"/>
<Loaded Value="True"/>
<UsageCount Value="15"/>
<SyntaxHighlighter Value="C++"/>
</Unit36>
<Unit37>
<Filename Value="../../../lazarus/ide/compileroptions.pp"/>
<UnitName Value="CompilerOptions"/>
<CursorPos X="17" Y="456"/>
<TopLine Value="450"/>
<UsageCount Value="10"/>
</Unit37>
<Unit38>
<Filename Value="../../../lazarus/ide/buildlazdialog.pas"/>
<ComponentName Value="ConfigureBuildLazarusDlg"/>
<HasResources Value="True"/>
<UnitName Value="BuildLazDialog"/>
<CursorPos X="39" Y="430"/>
<TopLine Value="428"/>
<UsageCount Value="10"/>
</Unit38>
<Unit39>
<Filename Value="controller.s"/>
<CursorPos X="25" Y="6"/>
<TopLine Value="1"/>
<UsageCount Value="10"/>
<SyntaxHighlighter Value="None"/>
</Unit39>
<Unit40>
<Filename Value="../../foundation/foundation.pas"/>
<UnitName Value="foundation"/>
<CursorPos X="16" Y="10"/>
<TopLine Value="1"/>
<EditorIndex Value="2"/>
<UsageCount Value="11"/>
<Loaded Value="True"/>
</Unit40>
<Unit41>
<Filename Value="../../../lazarus/ide/uniteditor.pp"/>
<ComponentName Value="SourceNotebook"/>
<HasResources Value="True"/>
<UnitName Value="UnitEditor"/>
<CursorPos X="35" Y="5"/>
<TopLine Value="1"/>
<UsageCount Value="10"/>
</Unit41>
<Unit42>
<Filename Value="../../../fpc/rtl/objpas/sysutils/syspchh.inc"/>
<CursorPos X="44" Y="20"/>
<TopLine Value="16"/>
<UsageCount Value="10"/>
</Unit42>
<Unit43>
<Filename Value="../../../fpc/rtl/i386/strings.inc"/>
<CursorPos X="53" Y="15"/>
<TopLine Value="10"/>
<UsageCount Value="10"/>
</Unit43>
<Unit44>
<Filename Value="../../../objc/objc-runtime.inc"/>
<CursorPos X="21" Y="175"/>
<TopLine Value="167"/>
<UsageCount Value="10"/>
</Unit44>
<Unit45>
<Filename Value="/usr/local/share/fpcsrc/packages/extra/univint/FPCMacOSAll.pas"/>
<UnitName Value="FPCMacOSAll"/>
<CursorPos X="15" Y="15543"/>
<TopLine Value="15540"/>
<UsageCount Value="10"/>
</Unit45>
<Unit46>
<Filename Value="/usr/local/share/fpcsrc/packages/base/dbus/dbus-memory.inc"/>
<CursorPos X="12" Y="23"/>
<TopLine Value="19"/>
<UsageCount Value="10"/>
</Unit46>
<Unit47>
<Filename Value="../../../fpc/packages/extra/univint/FPCMacOSAll.pas"/>
<UnitName Value="FPCMacOSAll"/>
<CursorPos X="36" Y="15573"/>
<TopLine Value="15572"/>
<UsageCount Value="10"/>
</Unit47>
<Unit48>
<Filename Value="/usr/local/share/fpcsrc/rtl/unix/baseunix.pp"/>
<UnitName Value="baseunix"/>
<CursorPos X="22" Y="76"/>
<TopLine Value="71"/>
<UsageCount Value="10"/>
</Unit48>
</Units>
<JumpHistory Count="5" HistoryIndex="4">
<JumpHistory Count="4" HistoryIndex="3">
<Position1>
<Filename Value="/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h"/>
<Caret Line="1" Column="1" TopLine="1"/>
<Filename Value="../../foundation/NSObject.inc"/>
<Caret Line="262" Column="13" TopLine="246"/>
</Position1>
<Position2>
<Filename Value="controller.pas"/>
<Caret Line="71" Column="17" TopLine="69"/>
<Filename Value="../../foundation/NSObject.inc"/>
<Caret Line="215" Column="31" TopLine="206"/>
</Position2>
<Position3>
<Filename Value="controller.pas"/>
<Caret Line="10" Column="14" TopLine="1"/>
<Filename Value="../../foundation/NSObject.inc"/>
<Caret Line="216" Column="55" TopLine="206"/>
</Position3>
<Position4>
<Filename Value="controller.pas"/>
<Caret Line="65" Column="69" TopLine="55"/>
<Filename Value="../../foundation/NSObject.inc"/>
<Caret Line="207" Column="8" TopLine="195"/>
</Position4>
<Position5>
<Filename Value="statusitem.pas"/>
<Caret Line="70" Column="13" TopLine="25"/>
</Position5>
</JumpHistory>
</ProjectOptions>
<CompilerOptions>
@ -331,6 +403,8 @@
<Verbosity>
<ShowSummary Value="True"/>
</Verbosity>
<CustomOptions Value="-St
"/>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>

View File

@ -61,6 +61,7 @@ FOUNDATION_EXPORT unsigned NSExtraRefCount(id object);}
function getClass: objc.id; virtual;
{ Class creation methods }
procedure AddMethod(aName, aParameters: string; aPointer: Pointer);
function CreateClassDefinition(const name, superclassName: string): Boolean;
public
{+ (void)load;
@ -185,7 +186,7 @@ end;
destructor NSObject.Destroy;
begin
release;
if Handle <> nil then release;
end;
function NSObject.getClass: objc.id;
@ -206,6 +207,75 @@ begin
class_addMethods(ClassId, method_list);
end;
function NSObject.CreateClassDefinition(const name, superclassName: string): Boolean;
var
meta_class, super_class, new_class, root_class: Pobjc_class;
begin
// Ensure that the superclass exists and that someone
// hasn't already implemented a class with the same name
//
super_class := Pobjc_class(objc_lookUpClass (PChar(superclassName)));
if (super_class = nil) then Exit(False);
if (objc_lookUpClass (PChar(name)) <> nil) then Exit(False);
// Find the root class
//
root_class := super_class;
while ( root_class^.super_class <> nil ) do
begin
root_class := root_class^.super_class;
end;
// Allocate space for the class and its metaclass
//
new_class := CFAllocatorAllocate(kCFAllocatorMalloc, 2 * SizeOf(objc_class), 0);
meta_class := @new_class[1];
// setup class
new_class^.isa := meta_class;
new_class^.info := CLS_CLASS;
meta_class^.info := CLS_META;
// Create a copy of the class name.
// For efficiency, we have the metaclass and the class itself
// to share this copy of the name, but this is not a requirement
// imposed by the runtime.
//
new_class^.name := CFAllocatorAllocate(kCFAllocatorMalloc, Length(name) + 1, 0);
SysUtils.strcopy(new_class^.name, PChar(name));
meta_class^.name := new_class^.name;
// Allocate empty method lists.
// We can add methods later.
//
new_class^.methodLists := CFAllocatorAllocate(kCFAllocatorMalloc, sizeof(Pobjc_method_list), 0);
new_class^.methodLists^ := Pointer(-1);
meta_class^.methodLists := CFAllocatorAllocate(kCFAllocatorMalloc, sizeof(Pobjc_method_list), 0);
meta_class^.methodLists^ := Pointer(-1);
// Connect the class definition to the class hierarchy:
// Connect the class to the superclass.
// Connect the metaclass to the metaclass of the superclass.
// Connect the metaclass of the metaclass to the metaclass of the root class.
//
new_class^.super_class := super_class;
meta_class^.super_class := super_class^.isa;
meta_class^.isa := Pointer(root_class^.isa);
// Set the sizes of the class and the metaclass.
//
new_class^.instance_size := super_class^.instance_size;
meta_class^.instance_size := meta_class^.super_class^.instance_size;
// Finally, register the class with the runtime.
//
objc_addClass( new_class );
Result := True;
end;
{*************** Basic protocols ***************}
function NSObject.retain: objc.id;

View File

@ -1,13 +1,13 @@
unit foundation;
{$ifdef fpc}
{$mode delphi}
{$mode objfpc}{$H+}
{$packrecords c}
{$endif}
interface
uses ctypes, objc, FPCMacOSAll;
uses SysUtils, ctypes, objc, FPCMacOSAll;
{$define HEADER}
{$include Foundation.inc}