Began implementing menus on Mac OS X.

This commit is contained in:
Pietro Gagliardi 2015-04-20 22:34:31 -04:00
parent f303f066e2
commit b258e1fc82
7 changed files with 88 additions and 5 deletions

View File

@ -6,6 +6,7 @@ OSMFILES = \
init.m \
label.m \
main.m \
menu.m \
newcontrol.m \
parent.m \
tab.m \

View File

@ -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];

70
darwin/menu.m Normal file
View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);

View File

@ -18,3 +18,8 @@ void disableAutocorrect(NSTextView *tv)
[tv setAutomaticLinkDetectionEnabled:NO];
[tv setSmartInsertDeleteEnabled:NO];
}
void complain(const char *fmt, ...)
{
// TODO
}

View File

@ -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;