Adds automatic closing when last window closes to statusitem example.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@376 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
sekelsenmat
2008-03-11 23:52:48 +00:00
parent 9543a52367
commit d3b0c99a9c
4 changed files with 113 additions and 69 deletions

View File

@ -45,15 +45,6 @@ const
// threading information // threading information
//typedef struct NSThreadPrivate _NSThreadPrivate; //typedef struct NSThreadPrivate _NSThreadPrivate;
const
Str_NSApplication = 'NSApplication';
Str_sharedApplication = 'sharedApplication';
Str_run = 'run';
Str_runModalForWindow = 'runModalForWindow:';
Str_orderFrontStandardAboutPanelWithOptions = 'orderFrontStandardAboutPanelWithOptions:';
{ An Application's startup function } { An Application's startup function }
//APPKIT_EXTERN int NSApplicationMain(int argc, const char *argv[]); //APPKIT_EXTERN int NSApplicationMain(int argc, const char *argv[]);
@ -94,6 +85,17 @@ APPKIT_EXTERN NSString *NSApplicationWillUpdateNotification;
APPKIT_EXTERN NSString *NSApplicationWillTerminateNotification; APPKIT_EXTERN NSString *NSApplicationWillTerminateNotification;
APPKIT_EXTERN NSString *NSApplicationDidChangeScreenParametersNotification; } APPKIT_EXTERN NSString *NSApplicationDidChangeScreenParametersNotification; }
{ Class and method name strings }
const
Str_NSApplication = 'NSApplication';
Str_sharedApplication = 'sharedApplication';
Str_setDelegate = 'setDelegate:';
Str_run = 'run';
Str_runModalForWindow = 'runModalForWindow:';
Str_orderFrontStandardAboutPanelWithOptions = 'orderFrontStandardAboutPanelWithOptions:';
{$endif} {$endif}
{$endif} {$endif}
{$ifdef CLASSES} {$ifdef CLASSES}
@ -116,8 +118,8 @@ APPKIT_EXTERN NSString *NSApplicationDidChangeScreenParametersNotification; }
public public
constructor sharedApplication; constructor sharedApplication;
{- (void)setDelegate:(id)anObject; procedure setDelegate(anObject: NSObject);
- (id)delegate; {- (id)delegate;
- (NSGraphicsContext*)context; - (NSGraphicsContext*)context;
- (void)hide:(id)sender; - (void)hide:(id)sender;
- (void)unhide:(id)sender; - (void)unhide:(id)sender;
@ -372,6 +374,16 @@ begin
Handle := objc_msgSend(ClassId, sel_registerName(PChar(Str_sharedApplication)), []); Handle := objc_msgSend(ClassId, sel_registerName(PChar(Str_sharedApplication)), []);
end; end;
procedure NSApplication.setDelegate(anObject: NSObject);
type
setDelegate_t = procedure (param1: objc.id; param2: SEL; param3: objc.id); cdecl;
var
vmethod: setDelegate_t;
begin
vmethod := setDelegate_t(@objc_msgSend);
vmethod(Handle, sel_registerName(PChar(Str_setDelegate)), anObject.Handle);
end;
procedure NSApplication.run; procedure NSApplication.run;
begin begin
objc_msgSend(Handle, sel_registerName(PChar(Str_run)), []); objc_msgSend(Handle, sel_registerName(PChar(Str_run)), []);

View File

@ -1,11 +1,11 @@
{ {
actions.pas controller.pas
This example project is released under public domain This example project is released under public domain
AUTHORS: Felipe Monteiro de Carvalho AUTHORS: Felipe Monteiro de Carvalho
} }
unit actions; unit controller;
{$mode objfpc}{$H+} {$mode objfpc}{$H+}
@ -16,9 +16,9 @@ uses
type type
{ TMyActionList } { TMyController }
TMyActionList = class(NSObject) TMyController = class(NSObject)
public public
{ Extra binding functions } { Extra binding functions }
constructor Create; override; constructor Create; override;
@ -30,11 +30,18 @@ type
end; end;
{ Objective-c Methods } { Objective-c Methods }
procedure doShowStatusitem(param1: objc.id; param2: SEL; param3: objc.id); cdecl; procedure doShowStatusitem(param1: objc.id; param2: SEL; sender: objc.id); cdecl;
procedure doHideStatusitem(param1: objc.id; param2: SEL; param3: objc.id); cdecl; procedure doHideStatusitem(param1: objc.id; param2: SEL; sender: objc.id); cdecl;
function applicationShouldTerminateAfterLastWindowClosed(param1: objc.id;
param2: SEL; theApplication: objc.id): cbool; cdecl;
var var
actionList: TMyActionList; myController: TMyController;
const
Str_doShowStatusitem = 'doShowStatusitem:';
Str_doHideStatusitem = 'doHideStatusitem:';
Str_applicationShouldTerminateAfterLastWindowClosed = 'applicationShouldTerminateAfterLastWindowClosed:';
{ Other helper functions } { Other helper functions }
function GetResourcesDir: string; function GetResourcesDir: string;
@ -44,10 +51,10 @@ function CreateButton(AView: NSView; ATitle: shortstring;
implementation implementation
{ TMyActionList } { TMyController }
{ Adds methods to the class } { Adds methods to the class }
procedure TMyActionList.AddMethods; procedure TMyController.AddMethods;
begin begin
{ Parameters string: { Parameters string:
@ -55,11 +62,13 @@ begin
followed by self and _cmd (@ = id and : = SEL), followed by self and _cmd (@ = id and : = SEL),
and on the end "sender" (@ = id) } and on the end "sender" (@ = id) }
AddMethod('doShowStatusitem:', 'v@:@', @doShowStatusitem); AddMethod(Str_doShowStatusItem, 'v@:@', @doShowStatusitem);
AddMethod('doHideStatusitem:', 'v@:@', @doHideStatusitem); AddMethod(Str_doHideStatusitem, 'v@:@', @doHideStatusitem);
AddMethod(Str_applicationShouldTerminateAfterLastWindowClosed, 'b@:@',
@applicationShouldTerminateAfterLastWindowClosed);
end; end;
constructor TMyActionList.Create; constructor TMyController.Create;
var var
fileName: CFStringRef; fileName: CFStringRef;
begin begin
@ -69,28 +78,33 @@ begin
bar := NSStatusBar.systemStatusBar(); bar := NSStatusBar.systemStatusBar();
fileName := CFStringCreateWithPascalString(nil, fileName := CFStringCreateWithPascalString(nil, GetResourcesDir + 'icon.ico', kCFStringEncodingUTF8);
GetResourcesDir + 'icon.ico', kCFStringEncodingUTF8);
image := NSImage.initWithContentsOfFile(fileName); image := NSImage.initWithContentsOfFile(fileName);
end; end;
{ Objective-c Methods } { Objective-c Methods }
procedure doShowStatusitem(param1: objc.id; param2: SEL; param3: objc.id); cdecl; procedure doShowStatusitem(param1: objc.id; param2: SEL; sender: objc.id); cdecl;
begin begin
if actionList.item <> nil then Exit; if myController.item <> nil then Exit;
actionList.item := actionList.bar.statusItemWithLength(NSSquareStatusItemLength); myController.item := myController.bar.statusItemWithLength(NSSquareStatusItemLength);
actionList.item.retain(); myController.item.retain();
actionList.item.setImage(actionList.image); myController.item.setImage(myController.image);
end; end;
procedure doHideStatusitem(param1: objc.id; param2: SEL; param3: objc.id); cdecl; procedure doHideStatusitem(param1: objc.id; param2: SEL; sender: objc.id); cdecl;
begin begin
if actionList.item = nil then Exit; if myController.item = nil then Exit;
actionList.item.Free; myController.item.Free;
actionList.item := nil; myController.item := nil;
end;
function applicationShouldTerminateAfterLastWindowClosed(param1: objc.id;
param2: SEL; theApplication: objc.id): cbool; cdecl;
begin
Result := objc.YES;
end; end;
{ Other helper functions } { Other helper functions }

View File

@ -31,10 +31,10 @@
<Filename Value="statusitem.pas"/> <Filename Value="statusitem.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="statusitem"/> <UnitName Value="statusitem"/>
<CursorPos X="1" Y="34"/> <CursorPos X="24" Y="71"/>
<TopLine Value="32"/> <TopLine Value="59"/>
<EditorIndex Value="0"/> <EditorIndex Value="0"/>
<UsageCount Value="41"/> <UsageCount Value="42"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit0> </Unit0>
<Unit1> <Unit1>
@ -52,23 +52,25 @@
</Unit2> </Unit2>
<Unit3> <Unit3>
<Filename Value="../../appkit/NSApplication.inc"/> <Filename Value="../../appkit/NSApplication.inc"/>
<CursorPos X="49" Y="107"/> <CursorPos X="3" Y="140"/>
<TopLine Value="105"/> <TopLine Value="130"/>
<UsageCount Value="11"/> <EditorIndex Value="5"/>
<UsageCount Value="12"/>
<Loaded Value="True"/>
</Unit3> </Unit3>
<Unit4> <Unit4>
<Filename Value="../../appkit/NSAlert.inc"/> <Filename Value="../../appkit/NSAlert.inc"/>
<CursorPos X="1" Y="12"/> <CursorPos X="1" Y="12"/>
<TopLine Value="1"/> <TopLine Value="1"/>
<UsageCount Value="22"/> <UsageCount Value="23"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit4> </Unit4>
<Unit5> <Unit5>
<Filename Value="../../appkit/NSStatusItem.inc"/> <Filename Value="../../appkit/NSStatusItem.inc"/>
<CursorPos X="12" Y="123"/> <CursorPos X="1" Y="129"/>
<TopLine Value="37"/> <TopLine Value="113"/>
<EditorIndex Value="2"/> <EditorIndex Value="2"/>
<UsageCount Value="15"/> <UsageCount Value="16"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit5> </Unit5>
<Unit6> <Unit6>
@ -76,7 +78,7 @@
<CursorPos X="23" Y="56"/> <CursorPos X="23" Y="56"/>
<TopLine Value="44"/> <TopLine Value="44"/>
<EditorIndex Value="9"/> <EditorIndex Value="9"/>
<UsageCount Value="13"/> <UsageCount Value="14"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit6> </Unit6>
<Unit7> <Unit7>
@ -163,15 +165,15 @@
<CursorPos X="13" Y="21"/> <CursorPos X="13" Y="21"/>
<TopLine Value="15"/> <TopLine Value="15"/>
<EditorIndex Value="8"/> <EditorIndex Value="8"/>
<UsageCount Value="21"/> <UsageCount Value="22"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit19> </Unit19>
<Unit20> <Unit20>
<Filename Value="../../appkit/NSWindow.inc"/> <Filename Value="../../appkit/NSWindow.inc"/>
<CursorPos X="1" Y="97"/> <CursorPos X="17" Y="99"/>
<TopLine Value="92"/> <TopLine Value="92"/>
<EditorIndex Value="4"/> <EditorIndex Value="4"/>
<UsageCount Value="17"/> <UsageCount Value="18"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit20> </Unit20>
<Unit21> <Unit21>
@ -220,18 +222,16 @@
</Unit27> </Unit27>
<Unit28> <Unit28>
<Filename Value="../../foundation/NSObject.inc"/> <Filename Value="../../foundation/NSObject.inc"/>
<CursorPos X="5" Y="206"/> <CursorPos X="48" Y="7"/>
<TopLine Value="192"/> <TopLine Value="1"/>
<EditorIndex Value="5"/>
<UsageCount Value="13"/> <UsageCount Value="13"/>
<Loaded Value="True"/>
</Unit28> </Unit28>
<Unit29> <Unit29>
<Filename Value="../../appkit/NSStatusBar.inc"/> <Filename Value="../../appkit/NSStatusBar.inc"/>
<CursorPos X="10" Y="61"/> <CursorPos X="10" Y="61"/>
<TopLine Value="36"/> <TopLine Value="36"/>
<EditorIndex Value="10"/> <EditorIndex Value="10"/>
<UsageCount Value="17"/> <UsageCount Value="18"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit29> </Unit29>
<Unit30> <Unit30>
@ -251,17 +251,17 @@
<CursorPos X="10" Y="82"/> <CursorPos X="10" Y="82"/>
<TopLine Value="73"/> <TopLine Value="73"/>
<EditorIndex Value="6"/> <EditorIndex Value="6"/>
<UsageCount Value="12"/> <UsageCount Value="13"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit32> </Unit32>
<Unit33> <Unit33>
<Filename Value="actions.pas"/> <Filename Value="controller.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="actions"/> <UnitName Value="controller"/>
<CursorPos X="13" Y="131"/> <CursorPos X="42" Y="92"/>
<TopLine Value="118"/> <TopLine Value="83"/>
<EditorIndex Value="1"/> <EditorIndex Value="1"/>
<UsageCount Value="25"/> <UsageCount Value="26"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit33> </Unit33>
<Unit34> <Unit34>
@ -273,10 +273,10 @@
</Unit34> </Unit34>
<Unit35> <Unit35>
<Filename Value="../../appkit/NSImage.inc"/> <Filename Value="../../appkit/NSImage.inc"/>
<CursorPos X="16" Y="15"/> <CursorPos X="1" Y="185"/>
<TopLine Value="59"/> <TopLine Value="169"/>
<EditorIndex Value="3"/> <EditorIndex Value="3"/>
<UsageCount Value="24"/> <UsageCount Value="25"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit35> </Unit35>
<Unit36> <Unit36>
@ -284,16 +284,32 @@
<CursorPos X="14" Y="35"/> <CursorPos X="14" Y="35"/>
<TopLine Value="113"/> <TopLine Value="113"/>
<EditorIndex Value="7"/> <EditorIndex Value="7"/>
<UsageCount Value="11"/> <UsageCount Value="12"/>
<Loaded Value="True"/> <Loaded Value="True"/>
<SyntaxHighlighter Value="C++"/> <SyntaxHighlighter Value="C++"/>
</Unit36> </Unit36>
</Units> </Units>
<JumpHistory Count="1" HistoryIndex="0"> <JumpHistory Count="5" HistoryIndex="4">
<Position1> <Position1>
<Filename Value="/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h"/> <Filename Value="/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h"/>
<Caret Line="1" Column="1" TopLine="1"/> <Caret Line="1" Column="1" TopLine="1"/>
</Position1> </Position1>
<Position2>
<Filename Value="../../appkit/NSApplication.inc"/>
<Caret Line="3" Column="37" TopLine="1"/>
</Position2>
<Position3>
<Filename Value="controller.pas"/>
<Caret Line="61" Column="22" TopLine="43"/>
</Position3>
<Position4>
<Filename Value="controller.pas"/>
<Caret Line="99" Column="15" TopLine="85"/>
</Position4>
<Position5>
<Filename Value="controller.pas"/>
<Caret Line="90" Column="36" TopLine="81"/>
</Position5>
</JumpHistory> </JumpHistory>
</ProjectOptions> </ProjectOptions>
<CompilerOptions> <CompilerOptions>

View File

@ -16,7 +16,7 @@ program statusitem;
{$mode delphi} {$mode delphi}
uses uses
objc, ctypes, FPCMacOSAll, AppKit, Foundation, actions; objc, ctypes, FPCMacOSAll, AppKit, Foundation, controller;
const const
Str_Window_Title = 'StatusItem example project'; Str_Window_Title = 'StatusItem example project';
@ -52,24 +52,26 @@ begin
CFTitle := CFStringCreateWithPascalString(nil, Str_Window_Title, kCFStringEncodingUTF8); CFTitle := CFStringCreateWithPascalString(nil, Str_Window_Title, kCFStringEncodingUTF8);
MainWindow.setTitle(CFTitle); MainWindow.setTitle(CFTitle);
{ Initializes the action responding object } { Initializes the controller object }
actionList := TMyActionList.Create(); myController := TMyController.Create();
{ Adds the buttons } { Adds the buttons }
CreateButton(MainWindowView, Str_Show_Button, CreateButton(MainWindowView, Str_Show_Button,
50.0, MainWindowRect.size.height - 50.0, 200.0, 25.0, 50.0, MainWindowRect.size.height - 50.0, 200.0, 25.0,
'doShowStatusitem:', actionList); Str_doShowStatusItem, myController);
CreateButton(MainWindowView, Str_Hide_Button, CreateButton(MainWindowView, Str_Hide_Button,
50.0, MainWindowRect.size.height - 100.0, 200.0, 25.0, 50.0, MainWindowRect.size.height - 100.0, 200.0, 25.0,
'doHideStatusitem:', actionList); Str_doHideStatusItem, myController);
{ Enters main message loop } { Enters main message loop }
MainWindow.orderFrontRegardless; MainWindow.orderFrontRegardless;
NSApp.setDelegate(myController);
NSApp.run; NSApp.run;
{ Releases the AutoreleasePool } { Releases the AutoreleasePool }