I'm so far:
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
uses
gl, MacOSAll, CocoaAll;
type
{ MyCallback }
MyCallback = objcclass(NSObject)
public
procedure haltMenuEvent(sender: id); message 'haltMenuEvent:';
end;
var
pool : NSAutoreleasePool;
filemenu : NSMenu;
subitem : NSMenuItem;
app : NSApplication;
callback : MyCallback;
MainWindow: NSWindow;
MainView: NSView;
R: NSRect;
function NSStr(const s: AnsiString): NSString;
begin
if s='' then Result:=NSString.alloc.init
else Result:=NSString.alloc.initWithCString(@s[1]);
end;
{ MyCallback }
procedure MyCallback.haltMenuEvent(sender:id);
begin
WriteLn('HALT!');
app.stop(self);
end;
procedure initMenus;
var
root : NSMenu;
rootitem : NSMenuItem;
begin
root:=NSMenu.alloc.init;
rootitem:=root.addItemWithTitle_action_keyEquivalent(NSStr(''), nil,
NSStr(''));
filemenu:=NSMenu.alloc.initWithTitle(NSStr('File'));
rootitem.setSubmenu(filemenu);
rootitem.setEnabled(true);
subitem:=filemenu.addItemWithTitle_action_keyEquivalent(NSStr('Halt'),
nil, NSStr(''));
subitem.setEnabled(true);
app.setMainMenu(root);
end;
procedure InitCallback;
begin
callback:=MyCallback.alloc;
subitem.setAction(ObjCSelector(callback.haltMenuEvent));
subitem.setTarget(callback);
end;
begin
pool:=NSAutoreleasePool.alloc.init;
app:=NSApplication.sharedApplication;
R.origin.x := 300.0;
R.origin.y := 300.0;
R.size.width := 300.0;
R.size.height := 300.0;
MainWindow :=
NSWindow.alloc.initWithContentRect_styleMask_backing_defer(R,
NSTitledWindowMask or NSClosableWindowMask or
NSMiniaturizableWindowMask or NSResizableWindowMask,
NSBackingStoreBuffered, False);
MainWindow.orderFrontRegardless;
MainWindow.setTitle(NSSTR('Title'));
R.origin.x := 0.0;
R.origin.y := 0.0;
MainView := NSView.alloc.initWithFrame(R);
MainWindow.setContentView(MainView);
initMenus;
initCallback;
app.run;
pool.release;
end.
Is it possible to do a moin loop like the:
repeat
updatemessages; // windows style
until true;
or is there only one option to go with callbacks and
app.run;
?
[Edited to add code tags - please see
How to use the Forums.]