Improves the statusitem example.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@372 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
sekelsenmat 2008-03-07 11:33:19 +00:00
parent a713a91bd9
commit b839964d8e
5 changed files with 173 additions and 79 deletions

View File

@ -0,0 +1,76 @@
{
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;
end;
{ Objective-c Methods }
procedure doShowStatusitem(param1: objc.id; param2: SEL; param3: objc.id); cdecl;
var
actionList: TMyActionList;
implementation
{ TMyActionList }
procedure TMyActionList.AddMethods;
var
method_list: Pobjc_method_list;
begin
{ Adds methods to the class }
method_list := GetMem(SizeOf(objc_method_list)); { We can't free this until the last instance is freed }
method_list^.method_count := 1;
method_list^.method_list[0].method_name := sel_registerName('doShowStatusitem:');
{ The first parameter is the result (v = void),
followed by self and _cmd (@ = id and : = SEL),
and on the end "sender" (@ = id) }
method_list^.method_list[0].method_types := 'v@:@';
method_list^.method_list[0].method_imp := IMP(@doShowStatusitem);
class_addMethods(ClassId, method_list);
end;
constructor TMyActionList.Create;
begin
inherited Create;
AddMethods();
bar := NSStatusBar.systemStatusBar();
end;
{ Objective-c Methods }
procedure doShowStatusitem(param1: objc.id; param2: SEL; param3: objc.id); cdecl;
begin
actionList.item := actionList.bar.statusItemWithLength(NSSquareStatusItemLength);
end;
end.

View File

@ -7,7 +7,7 @@
<MainUnit Value="0"/>
<IconPath Value="./"/>
<TargetFileExt Value=""/>
<ActiveEditorIndexAtStart Value="0"/>
<ActiveEditorIndexAtStart Value="1"/>
</General>
<VersionInfo>
<ProjectVersion Value=""/>
@ -26,15 +26,15 @@
<LaunchingApplication PathPlusParams="/usr/X11R6/bin/xterm -T 'Lazarus Run Output' -e $(LazarusDir)/tools/runwait.sh $(TargetCmdLine)"/>
</local>
</RunParams>
<Units Count="33">
<Units Count="34">
<Unit0>
<Filename Value="statusitem.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="statusitem"/>
<CursorPos X="3" Y="47"/>
<TopLine Value="37"/>
<CursorPos X="19" Y="26"/>
<TopLine Value="54"/>
<EditorIndex Value="0"/>
<UsageCount Value="36"/>
<UsageCount Value="37"/>
<Loaded Value="True"/>
</Unit0>
<Unit1>
@ -60,7 +60,7 @@
<Filename Value="../../appkit/NSAlert.inc"/>
<CursorPos X="1" Y="12"/>
<TopLine Value="1"/>
<UsageCount Value="19"/>
<UsageCount Value="20"/>
<Loaded Value="True"/>
</Unit4>
<Unit5>
@ -124,8 +124,10 @@
<Unit14>
<Filename Value="../../appkit/NSControl.inc"/>
<CursorPos X="26" Y="147"/>
<TopLine Value="142"/>
<TopLine Value="145"/>
<EditorIndex Value="4"/>
<UsageCount Value="24"/>
<Loaded Value="True"/>
</Unit14>
<Unit15>
<Filename Value="/System/Library/Frameworks/AppKit.framework/Versions/C/Headers/NSView.h"/>
@ -158,16 +160,16 @@
<UnitName Value="appkit"/>
<CursorPos X="13" Y="21"/>
<TopLine Value="10"/>
<EditorIndex Value="3"/>
<UsageCount Value="18"/>
<EditorIndex Value="5"/>
<UsageCount Value="19"/>
<Loaded Value="True"/>
</Unit19>
<Unit20>
<Filename Value="../../appkit/NSWindow.inc"/>
<CursorPos X="1" Y="522"/>
<TopLine Value="508"/>
<EditorIndex Value="1"/>
<UsageCount Value="14"/>
<TopLine Value="21"/>
<EditorIndex Value="2"/>
<UsageCount Value="15"/>
<Loaded Value="True"/>
</Unit20>
<Unit21>
@ -216,24 +218,24 @@
</Unit27>
<Unit28>
<Filename Value="../../foundation/NSObject.inc"/>
<CursorPos X="46" Y="102"/>
<TopLine Value="94"/>
<EditorIndex Value="2"/>
<UsageCount Value="10"/>
<CursorPos X="16" Y="7"/>
<TopLine Value="1"/>
<EditorIndex Value="3"/>
<UsageCount Value="11"/>
<Loaded Value="True"/>
</Unit28>
<Unit29>
<Filename Value="../../appkit/NSStatusBar.inc"/>
<CursorPos X="27" Y="15"/>
<TopLine Value="5"/>
<EditorIndex Value="4"/>
<UsageCount Value="14"/>
<TopLine Value="36"/>
<EditorIndex Value="6"/>
<UsageCount Value="15"/>
<Loaded Value="True"/>
</Unit29>
<Unit30>
<Filename Value="../../foundation/NSAutoreleasePool.inc"/>
<CursorPos X="33" Y="43"/>
<TopLine Value="37"/>
<CursorPos X="26" Y="19"/>
<TopLine Value="13"/>
<UsageCount Value="10"/>
</Unit30>
<Unit31>
@ -248,20 +250,78 @@
<TopLine Value="49"/>
<UsageCount Value="12"/>
</Unit32>
<Unit33>
<Filename Value="actions.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="actions"/>
<CursorPos X="3" Y="61"/>
<TopLine Value="58"/>
<EditorIndex Value="1"/>
<UsageCount Value="21"/>
<Loaded Value="True"/>
</Unit33>
</Units>
<JumpHistory Count="3" HistoryIndex="2">
<JumpHistory Count="15" HistoryIndex="14">
<Position1>
<Filename Value="statusitem.pas"/>
<Caret Line="80" Column="30" TopLine="69"/>
<Caret Line="38" Column="25" TopLine="29"/>
</Position1>
<Position2>
<Filename Value="../../appkit/NSWindow.inc"/>
<Caret Line="137" Column="17" TopLine="128"/>
<Filename Value="statusitem.pas"/>
<Caret Line="39" Column="22" TopLine="23"/>
</Position2>
<Position3>
<Filename Value="statusitem.pas"/>
<Caret Line="105" Column="15" TopLine="91"/>
<Filename Value="actions.pas"/>
<Caret Line="22" Column="1" TopLine="18"/>
</Position3>
<Position4>
<Filename Value="actions.pas"/>
<Caret Line="15" Column="41" TopLine="9"/>
</Position4>
<Position5>
<Filename Value="actions.pas"/>
<Caret Line="18" Column="33" TopLine="9"/>
</Position5>
<Position6>
<Filename Value="actions.pas"/>
<Caret Line="17" Column="31" TopLine="9"/>
</Position6>
<Position7>
<Filename Value="actions.pas"/>
<Caret Line="15" Column="59" TopLine="12"/>
</Position7>
<Position8>
<Filename Value="actions.pas"/>
<Caret Line="21" Column="3" TopLine="18"/>
</Position8>
<Position9>
<Filename Value="actions.pas"/>
<Caret Line="70" Column="1" TopLine="58"/>
</Position9>
<Position10>
<Filename Value="actions.pas"/>
<Caret Line="59" Column="12" TopLine="57"/>
</Position10>
<Position11>
<Filename Value="actions.pas"/>
<Caret Line="36" Column="1" TopLine="19"/>
</Position11>
<Position12>
<Filename Value="actions.pas"/>
<Caret Line="41" Column="14" TopLine="32"/>
</Position12>
<Position13>
<Filename Value="actions.pas"/>
<Caret Line="27" Column="1" TopLine="14"/>
</Position13>
<Position14>
<Filename Value="actions.pas"/>
<Caret Line="58" Column="15" TopLine="43"/>
</Position14>
<Position15>
<Filename Value="actions.pas"/>
<Caret Line="57" Column="1" TopLine="44"/>
</Position15>
</JumpHistory>
</ProjectOptions>
<CompilerOptions>

View File

@ -16,7 +16,7 @@ program statusitem;
{$mode delphi}
uses
objc, ctypes, FPCMacOSAll, AppKit, Foundation;
objc, ctypes, FPCMacOSAll, AppKit, Foundation, actions;
const
Str_Window_Title = 'StatusItem example project';
@ -27,45 +27,10 @@ var
MainWindow: NSWindow;
MainWindowView: NSView;
MyButton: NSButton;
bar: NSStatusBar;
item: NSStatusItem;
{ strings }
CFTitle, CFButtonText: CFStringRef;
{ sizes }
MainWindowRect, ButtonRect: NSRect;
type
TMyWindow = class(NSWindow)
public
{ Extra binding functions }
function getClass: objc.id; override;
end;
procedure doShowStatusitem(param1: objc.id; param2: SEL; param3: objc.id); cdecl;
begin
item := bar.statusItemWithLength(NSSquareStatusItemLength);
end;
function TMyWindow.getClass: objc.id;
var
method_list: Pobjc_method_list;
begin
Result := objc_getClass(Str_NSWindow);
{ Adds methods to the class }
method_list := GetMem(SizeOf(objc_method_list)); { We can't free this until the last instance is freed }
method_list.method_count := 1;
method_list.method_list[0].method_name := sel_registerName('doShowStatusitem:');
{ The first parameter is the result (v = void),
followed by self and _cmd (@ = id and : = SEL),
and on the end "sender" (@ = id) }
method_list.method_list[0].method_types := 'v@:@';
method_list.method_list[0].method_imp := IMP(@doShowStatusitem);
class_addMethods(Result, method_list);
end;
begin
{ Creates the AutoreleasePool }
pool := NSAutoreleasePool.Create;
@ -80,7 +45,7 @@ begin
MainWindowRect.size.width := 300.0;
MainWindowRect.size.height := 500.0;
MainWindow := TMyWindow.initWithContentRect(MainWindowRect,
MainWindow := NSWindow.initWithContentRect(MainWindowRect,
NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask or NSResizableWindowMask,
NSBackingStoreBuffered, NO);
MainWindowView := NSView.CreateWithHandle(MainWindow.contentView);
@ -88,6 +53,10 @@ begin
CFTitle := CFStringCreateWithPascalString(nil, Str_Window_Title, kCFStringEncodingUTF8);
MainWindow.setTitle(CFTitle);
{ Initializes the action responding object }
actionList := TMyActionList.Create();
{ Puts some buttons on the window }
CFButtonText := CFStringCreateWithPascalString(nil, Str_Show_Button, kCFStringEncodingUTF8);
@ -99,13 +68,9 @@ begin
MyButton.setStringValue(CFButtonText);
MyButton.setBezelStyle(NSRoundedBezelStyle);
MyButton.setAction(sel_registerName('doShowStatusitem:'));
MyButton.setTarget(MainWindow.Handle);
MyButton.setTarget(actionList.Handle);
MainWindowView.addSubview(MyButton);
{ Initializes the StatusBar object }
bar := NSStatusBar.systemStatusBar();
{ Enters main message loop }
MainWindow.orderFrontRegardless;

View File

@ -21,7 +21,6 @@ const
NSAutoreleasePool = class(NSObject)
public
constructor Create; override;
{ Extra binding functions }
function getClass: objc.id; override;
public
@ -45,13 +44,5 @@ begin
Result := objc_getClass(Str_NSAutoreleasePool);
end;
constructor NSAutoreleasePool.Create;
begin
{ NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; }
ClassId := getClass();
allocbuf := objc_msgSend(ClassId, sel_registerName(PChar(Str_alloc)), []);
Handle := objc_msgSend(allocbuf, sel_registerName(PChar(Str_init)), []);
end;
{$endif}

View File

@ -161,7 +161,9 @@ FOUNDATION_EXPORT unsigned NSExtraRefCount(id object);}
constructor NSObject.Create;
begin
ClassId := getClass();
allocbuf := objc_msgSend(ClassId, sel_registerName(PChar(Str_alloc)), []);
Handle := objc_msgSend(allocbuf, sel_registerName(PChar(Str_init)), []);
end;
constructor NSObject.CreateWithHandle(aHandle: objc.id);