Hello,

I am still trying to get the application menu to work. I have even
tryed to add a very simple nib file to the project. I simply added the
nib file to the bundle and set NSMainNibFile, but it doesn't show the
applications menu. It doesn't matter if I comment the code which
attempts to generate the application menu or not, it never shows
anything inside it.

Any ideas how to insert a menu in a project which is otherwise
completely written in code?

Here is my code:

type

  { TMyView }

  TMyView = class
  private
    { Other helper functions }
    procedure AddToMenubar(menu: NSMenu);

    function CreateAppleMenu(): NSMenu;
    function CreateFileMenu(): NSMenu;
    procedure CreateMainMenu();
    function CreateMenuItem(ATitle: shortstring): NSMenuItem;

    function CreateToolbar(AOwnerView: NSView; AX, AY, AWidth,
AHeight: Double): NSToolbar;
    function CreateTextField(): NSTextField;
  public
    { classes }
    MainWindow: NSWindow;
    MainWindowView: NSView;
    Toolbar: NSToolbar;
    TextField: NSTextField;
    MainMenu, AppleMenu, ServicesMenu, FileMenu: NSMenu;
    OpenItem, SaveItem, SaveAsItem, ExitItem: NSMenuItem;
    { strings and sizes}
    CFWindowTitle, CFEmptyString: CFStringRef;
    MainWindowRect: NSRect;
    { methods }
    procedure CreateUserInterface();
    procedure AttachEventHandlers();
  end;

var
  myView: TMyView;

const
  Str_Window_Title = 'Text Editor';

implementation

uses controller, model;

{@@
}
procedure TMyView.CreateUserInterface();
begin
  CFEmptyString := CFStringCreateWithPascalString(nil, '',
kCFStringEncodingUTF8);

  { Creates the main window }

  MainWindowRect.origin.x := 300.0;
  MainWindowRect.origin.y := 300.0;
  MainWindowRect.size.width := 300.0;
  MainWindowRect.size.height := 500.0;

  MainWindow :=
NSWindow.initWithContentRect_styleMask_backing_defer(MainWindowRect,
    NSTitledWindowMask or NSClosableWindowMask or
NSMiniaturizableWindowMask or NSResizableWindowMask,
    NSBackingStoreBuffered, LongBool(NO));
  MainWindowView := NSView.CreateWithHandle(MainWindow.contentView);

  CFWindowTitle := CFStringCreateWithPascalString(nil,
Str_Window_Title, kCFStringEncodingUTF8);
  MainWindow.setTitle(CFWindowTitle);

  { Adds the toolbar and it's buttons }

  Toolbar := CreateToolbar(MainWindowView, 0,
MainWindowRect.size.height - 50, MainWindowRect.size.width, 50);

  MainWindow.setToolbar(Toolbar.Handle);

  { Add the text area }

  TextField := CreateTextField();

  { Add the main menu }

  CreateMainMenu();
end;

procedure TMyView.AttachEventHandlers();
begin
  OpenItem.setTarget(myController.Handle);
  OpenItem.setAction(sel_registerName(PChar('doOpenFile:')));

  SaveItem.setTarget(myController.Handle);
  SaveItem.setAction(sel_registerName(PChar('doSaveFile:')));

  ExitItem.setTarget(myController.Handle);
  ExitItem.setAction(sel_registerName(PChar('doClose:')));
end;

function TMyView.CreateAppleMenu(): NSMenu;
var
  AppleMenuTitle, ServicesMenuTitle: CFStringRef;
begin
  AppleMenuTitle := CFStringCreateWithPascalString(nil, 'Apple Menu',
kCFStringEncodingUTF8);
  ServicesMenuTitle := CFStringCreateWithPascalString(nil, 'Services',
kCFStringEncodingUTF8);

  { Creates the Apple menu }

  Result := NSMenu.initWithTitle(AppleMenuTitle);

  { Add the services submenu }

  ServicesMenu := NSMenu.initWithTitle(ServicesMenuTitle);
  NSApp.setServicesMenu(ServicesMenu.Handle);
end;

function TMyView.CreateFileMenu(): NSMenu;
var
  MenuTitle: CFStringRef;
begin
  MenuTitle := CFStringCreateWithPascalString(nil, 'File',
kCFStringEncodingUTF8);

  { Creates the file menu }

  Result := NSMenu.initWithTitle(MenuTitle);

  { Adds items to it }

  OpenItem := CreateMenuItem('Open');
  Result.addItem(OpenItem.Handle);
  SaveItem := CreateMenuItem('Save');
  Result.addItem(SaveItem.Handle);
  ExitItem := CreateMenuItem('Exit');
  Result.addItem(ExitItem.Handle);
end;

procedure TMyView.AddToMenubar(menu: NSMenu);
var
  dummyItem: NSMenuItem;
begin
  dummyItem := NSMenuItem.initWithTitle_action_keyEquivalent(CFEmptyString,
nil, CFEmptyString);
  dummyItem.setSubmenu(menu.Handle);
  MainMenu.addItem(dummyItem.Handle);
  dummyItem.Free;
end;


-- 
Felipe Monteiro de Carvalho
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to