Recent

Author Topic: create window OS X  (Read 1355 times)

Key-Real

  • Full Member
  • ***
  • Posts: 185
create window OS X
« on: April 18, 2021, 12:58:23 pm »
I'm so far:

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. {$modeswitch objectivec1}
  3.  
  4. uses
  5.    gl, MacOSAll, CocoaAll;
  6.  
  7. type
  8.    { MyCallback }
  9.    MyCallback = objcclass(NSObject)
  10.    public
  11.      procedure haltMenuEvent(sender: id); message 'haltMenuEvent:';
  12.    end;
  13.  
  14. var
  15.    pool     : NSAutoreleasePool;
  16.    filemenu : NSMenu;
  17.    subitem  : NSMenuItem;
  18.    app      : NSApplication;
  19.    callback : MyCallback;
  20.    MainWindow: NSWindow;
  21.    MainView: NSView;
  22.    R: NSRect;
  23.  
  24. function NSStr(const s: AnsiString): NSString;
  25. begin
  26.    if s='' then Result:=NSString.alloc.init
  27.    else Result:=NSString.alloc.initWithCString(@s[1]);
  28. end;
  29.  
  30. { MyCallback }
  31.  
  32. procedure MyCallback.haltMenuEvent(sender:id);
  33. begin
  34.    WriteLn('HALT!');
  35.    app.stop(self);
  36. end;
  37.  
  38. procedure initMenus;
  39. var
  40.    root      : NSMenu;
  41.    rootitem  : NSMenuItem;
  42. begin
  43.    root:=NSMenu.alloc.init;
  44.    rootitem:=root.addItemWithTitle_action_keyEquivalent(NSStr(''), nil,
  45. NSStr(''));
  46.    filemenu:=NSMenu.alloc.initWithTitle(NSStr('File'));
  47.    rootitem.setSubmenu(filemenu);
  48.    rootitem.setEnabled(true);
  49. subitem:=filemenu.addItemWithTitle_action_keyEquivalent(NSStr('Halt'),
  50. nil, NSStr(''));
  51.    subitem.setEnabled(true);
  52.    app.setMainMenu(root);
  53. end;
  54.  
  55. procedure InitCallback;
  56. begin
  57.    callback:=MyCallback.alloc;
  58.    subitem.setAction(ObjCSelector(callback.haltMenuEvent));
  59.    subitem.setTarget(callback);
  60. end;
  61.  
  62. begin
  63.    pool:=NSAutoreleasePool.alloc.init;
  64.    app:=NSApplication.sharedApplication;
  65.  
  66.      R.origin.x := 300.0;
  67.      R.origin.y := 300.0;
  68.      R.size.width := 300.0;
  69.      R.size.height := 300.0;
  70.  
  71.      MainWindow :=
  72. NSWindow.alloc.initWithContentRect_styleMask_backing_defer(R,
  73.      NSTitledWindowMask or NSClosableWindowMask or
  74. NSMiniaturizableWindowMask or NSResizableWindowMask,
  75.      NSBackingStoreBuffered, False);
  76.  
  77.      MainWindow.orderFrontRegardless;
  78.  
  79.      MainWindow.setTitle(NSSTR('Title'));
  80.  
  81.      R.origin.x := 0.0;
  82.      R.origin.y := 0.0;
  83.      MainView := NSView.alloc.initWithFrame(R);
  84.      MainWindow.setContentView(MainView);
  85.  
  86.    initMenus;
  87.    initCallback;
  88.    app.run;
  89.    pool.release;
  90. end.

Is it possible to do a moin loop like the:

Code: Pascal  [Select][+][-]
  1. repeat
  2.  updatemessages;  // windows style
  3. 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.]
« Last Edit: May 09, 2021, 01:20:25 am by trev »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: create window OS X
« Reply #1 on: April 18, 2021, 02:08:47 pm »
Please, use code tags ...
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018