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
//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 }
//APPKIT_EXTERN int NSApplicationMain(int argc, const char *argv[]);
@ -94,6 +85,17 @@ APPKIT_EXTERN NSString *NSApplicationWillUpdateNotification;
APPKIT_EXTERN NSString *NSApplicationWillTerminateNotification;
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}
{$ifdef CLASSES}
@ -116,8 +118,8 @@ APPKIT_EXTERN NSString *NSApplicationDidChangeScreenParametersNotification; }
public
constructor sharedApplication;
{- (void)setDelegate:(id)anObject;
- (id)delegate;
procedure setDelegate(anObject: NSObject);
{- (id)delegate;
- (NSGraphicsContext*)context;
- (void)hide:(id)sender;
- (void)unhide:(id)sender;
@ -372,6 +374,16 @@ begin
Handle := objc_msgSend(ClassId, sel_registerName(PChar(Str_sharedApplication)), []);
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;
begin
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
AUTHORS: Felipe Monteiro de Carvalho
}
unit actions;
unit controller;
{$mode objfpc}{$H+}
@ -16,9 +16,9 @@ uses
type
{ TMyActionList }
{ TMyController }
TMyActionList = class(NSObject)
TMyController = class(NSObject)
public
{ Extra binding functions }
constructor Create; override;
@ -30,11 +30,18 @@ type
end;
{ Objective-c Methods }
procedure doShowStatusitem(param1: objc.id; param2: SEL; param3: objc.id); cdecl;
procedure doHideStatusitem(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; sender: objc.id); cdecl;
function applicationShouldTerminateAfterLastWindowClosed(param1: objc.id;
param2: SEL; theApplication: objc.id): cbool; cdecl;
var
actionList: TMyActionList;
myController: TMyController;
const
Str_doShowStatusitem = 'doShowStatusitem:';
Str_doHideStatusitem = 'doHideStatusitem:';
Str_applicationShouldTerminateAfterLastWindowClosed = 'applicationShouldTerminateAfterLastWindowClosed:';
{ Other helper functions }
function GetResourcesDir: string;
@ -44,10 +51,10 @@ function CreateButton(AView: NSView; ATitle: shortstring;
implementation
{ TMyActionList }
{ TMyController }
{ Adds methods to the class }
procedure TMyActionList.AddMethods;
procedure TMyController.AddMethods;
begin
{ Parameters string:
@ -55,11 +62,13 @@ begin
followed by self and _cmd (@ = id and : = SEL),
and on the end "sender" (@ = id) }
AddMethod('doShowStatusitem:', 'v@:@', @doShowStatusitem);
AddMethod('doHideStatusitem:', 'v@:@', @doHideStatusitem);
AddMethod(Str_doShowStatusItem, 'v@:@', @doShowStatusitem);
AddMethod(Str_doHideStatusitem, 'v@:@', @doHideStatusitem);
AddMethod(Str_applicationShouldTerminateAfterLastWindowClosed, 'b@:@',
@applicationShouldTerminateAfterLastWindowClosed);
end;
constructor TMyActionList.Create;
constructor TMyController.Create;
var
fileName: CFStringRef;
begin
@ -69,28 +78,33 @@ begin
bar := NSStatusBar.systemStatusBar();
fileName := CFStringCreateWithPascalString(nil,
GetResourcesDir + 'icon.ico', kCFStringEncodingUTF8);
fileName := CFStringCreateWithPascalString(nil, GetResourcesDir + 'icon.ico', kCFStringEncodingUTF8);
image := NSImage.initWithContentsOfFile(fileName);
end;
{ 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
if actionList.item <> nil then Exit;
if myController.item <> nil then Exit;
actionList.item := actionList.bar.statusItemWithLength(NSSquareStatusItemLength);
actionList.item.retain();
actionList.item.setImage(actionList.image);
myController.item := myController.bar.statusItemWithLength(NSSquareStatusItemLength);
myController.item.retain();
myController.item.setImage(myController.image);
end;
procedure doHideStatusitem(param1: objc.id; param2: SEL; param3: objc.id); cdecl;
procedure doHideStatusitem(param1: objc.id; param2: SEL; sender: objc.id); cdecl;
begin
if actionList.item = nil then Exit;
if myController.item = nil then Exit;
actionList.item.Free;
actionList.item := nil;
myController.item.Free;
myController.item := nil;
end;
function applicationShouldTerminateAfterLastWindowClosed(param1: objc.id;
param2: SEL; theApplication: objc.id): cbool; cdecl;
begin
Result := objc.YES;
end;
{ Other helper functions }

View File

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

View File

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