From b258e1fc82ef08aefa0c6ed50ee9153a27c778ac Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 20 Apr 2015 22:34:31 -0400 Subject: [PATCH] Began implementing menus on Mac OS X. --- darwin/GNUmakeinc.mk | 1 + darwin/init.m | 3 ++ darwin/menu.m | 70 ++++++++++++++++++++++++++++++++++++++++++ darwin/parent.m | 2 +- darwin/uipriv_darwin.h | 10 ++++-- darwin/util.m | 5 +++ darwin/window.m | 2 +- 7 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 darwin/menu.m diff --git a/darwin/GNUmakeinc.mk b/darwin/GNUmakeinc.mk index a6afd997..8190ffce 100644 --- a/darwin/GNUmakeinc.mk +++ b/darwin/GNUmakeinc.mk @@ -6,6 +6,7 @@ OSMFILES = \ init.m \ label.m \ main.m \ + menu.m \ newcontrol.m \ parent.m \ tab.m \ diff --git a/darwin/init.m b/darwin/init.m index 4f14a0a6..c188d5d8 100644 --- a/darwin/init.m +++ b/darwin/init.m @@ -56,6 +56,9 @@ const char *uiInit(uiInitOptions *o) [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; [NSApp setDelegate:[uiAppDelegate new]]; + if (options.Menu != NULL) + [NSApp setMainMenu:makeMenubar()]; + // we can use a stock NSView for this destroyedControlsView = [[NSView alloc] initWithFrame:NSZeroRect]; diff --git a/darwin/menu.m b/darwin/menu.m new file mode 100644 index 00000000..74fdf135 --- /dev/null +++ b/darwin/menu.m @@ -0,0 +1,70 @@ +// 20 april 2015 +#import "uipriv_darwin.h" + +struct menuConfig { + BOOL hasQuit; + BOOL hasPreferences; + BOOL hasAbout; +}; + +void appendMenuItem(NSMenu *menu, const uiMenuItem *item, struct menuConfig *menuConfig) +{ + NSMenuItem *mitem; + + if (item->Name == uiMenuItemQuit) { + // TODO verify type + menuConfig->hasQuit = YES; + return; + } + if (item->Name == uiMenuItemPreferences) { + // TODO verify type + menuConfig->hasPreferences = YES; + return; + } + if (item->Name == uiMenuItemAbout) { + // TODO verify type + menuConfig->hasAbout = YES; + return; + } + if (item->Name == uiMenuItemSeparator) { + // TODO verify type + [menu addItem:[NSMenuItem separatorItem]]; + return; + } + switch (item->Type) { + case uiMenuItemTypeCommand: + case uiMenuItemTypeCheckbox: + mitem = [[NSMenuItem alloc] initWithTitle:toNSString(item->Name) action:NULL keyEquivalent:@""]; + [menu addItem:mitem]; + return; + } +} + +NSMenuItem *makeMenu(const char *name, const uiMenuItem *items, struct menuConfig *menuConfig) +{ + NSMenuItem *menubarItem; + NSMenu *menu; + const uiMenuItem *i; + + menubarItem = [[NSMenuItem alloc] initWithTitle:toNSString(name) action:NULL keyEquivalent:@""]; + menu = [[NSMenu alloc] initWithTitle:toNSString(name)]; + for (i = items; i->Name != NULL; i++) + appendMenuItem(menu, i, menuConfig); + [menubarItem setSubmenu:menu]; + return menubarItem; +} + +NSMenu *makeMenubar(void) +{ + NSMenu *menubar; + struct menuConfig menuConfig; + const uiMenu *m; + + menubar = [[NSMenu alloc] initWithTitle:@""]; + + memset(&menuConfig, 0, sizeof (struct menuConfig)); + for (m = options.Menu; m->Name != NULL; m++) + [menubar addItem:makeMenu(m->Name, m->Items, &menuConfig)]; + + return menubar; +} diff --git a/darwin/parent.m b/darwin/parent.m index 629c8f0b..100475cf 100644 --- a/darwin/parent.m +++ b/darwin/parent.m @@ -28,7 +28,7 @@ uiLogObjCClassAllocations { if (self->mainControl != NULL) { // we have to do this before we can destroy controls - uiControlSetParent(p->mainControl, NULL); + uiControlSetParent(self->mainControl, NULL); uiControlDestroy(self->mainControl); self->mainControl = NULL; } diff --git a/darwin/uipriv_darwin.h b/darwin/uipriv_darwin.h index fd0cb980..239fd05a 100644 --- a/darwin/uipriv_darwin.h +++ b/darwin/uipriv_darwin.h @@ -26,17 +26,21 @@ #define VIEW(c) uiControlHandle(uiControl(c)) -// init_darwin.m +// init.m extern NSView *destroyedControlsView; -// util_darwin.m +// util.m extern void setStandardControlFont(NSControl *); extern void disableAutocorrect(NSTextView *); +// parent.m // These are based on measurements from Interface Builder. // These seem to be based on Auto Layout constants, but I don't see an API that exposes these... #define macXMargin 20 #define macYMargin 20 -// entry_darwin.m +// entry.m extern void finishNewTextField(NSTextField *, BOOL); + +// menu.m +extern NSMenu *makeMenubar(void); diff --git a/darwin/util.m b/darwin/util.m index 906a0ead..6861147e 100644 --- a/darwin/util.m +++ b/darwin/util.m @@ -18,3 +18,8 @@ void disableAutocorrect(NSTextView *tv) [tv setAutomaticLinkDetectionEnabled:NO]; [tv setSmartInsertDeleteEnabled:NO]; } + +void complain(const char *fmt, ...) +{ + // TODO +} diff --git a/darwin/window.m b/darwin/window.m index 8bfea308..850ce61b 100644 --- a/darwin/window.m +++ b/darwin/window.m @@ -116,7 +116,7 @@ static void windowSetMargined(uiWindow *ww, int margined) uiParentUpdate(D.content); } -uiWindow *uiNewWindow(const char *title, int width, int height) +uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar) { uiWindowDelegate *d;