Started the stub menu code.

This commit is contained in:
Pietro Gagliardi 2015-04-28 14:17:28 -04:00
parent b7272f680f
commit 855bca9b0c
4 changed files with 124 additions and 77 deletions

View File

@ -55,65 +55,7 @@ NSMenuItem *makeMenu(const char *name, const uiMenuItem *items, struct menuConfi
return menubarItem; return menubarItem;
} }
// Cocoa constructs the default application menu by hand for each program; that's what MainMenu.[nx]ib does
// TODO investigate setAppleMenu:
static void buildApplicationMenu(NSMenu *menubar)
{
NSString *appName;
NSMenuItem *appMenuItem;
NSMenu *appMenu;
NSMenuItem *item;
NSString *title;
appName = [[NSProcessInfo processInfo] processName];
appMenuItem = [[NSMenuItem alloc] initWithTitle:appName action:NULL keyEquivalent:@""];
appMenu = [[NSMenu alloc] initWithTitle:appName];
[appMenuItem setSubmenu:appMenu];
[menubar addItem:appMenuItem];
// first is About
title = [@"About " stringByAppendingString:appName];
item = [[NSMenuItem alloc] initWithTitle:title action:NULL keyEquivalent:@""];
[item setEnabled:NO];
[appMenu addItem:item];
aboutItem = item;
[appMenu addItem:[NSMenuItem separatorItem]];
// next is Preferences
item = [[NSMenuItem alloc] initWithTitle:@"Preferences…" action:NULL keyEquivalent:@","];
[item setEnabled:NO];
[appMenu addItem:item];
preferencesItem = item;
[appMenu addItem:[NSMenuItem separatorItem]];
// next is Services
item = [[NSMenuItem alloc] initWithTitle:@"Services" action:NULL keyEquivalent:@""];
// TODO build this part
[appMenu addItem:[NSMenuItem separatorItem]];
// next are the three hiding options
title = [@"Hide " stringByAppendingString:appName];
item = [[NSMenuItem alloc] initWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
// TODO set target for all three of these? the .xib file says they go to -1 ("First Responder", which sounds wrong...)
[appMenu addItem:item];
item = [[NSMenuItem alloc] initWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
[item setKeyEquivalentModifierMask:(NSAlternateKeyMask | NSCommandKeyMask)];
[appMenu addItem:item];
item = [[NSMenuItem alloc] initWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
[appMenu addItem:item];
[appMenu addItem:[NSMenuItem separatorItem]];
// and finally Quit
// DON'T use @selector(terminate:) as the action; we handle termination ourselves (TODO figure out how)
title = [@"Quit " stringByAppendingString:appName];
item = [[NSMenuItem alloc] initWithTitle:title action:NULL keyEquivalent:@"q"];
[appMenu addItem:item];
quitItem = item;
}
NSMenu *makeMenubar(void) NSMenu *makeMenubar(void)
{ {

View File

@ -21,10 +21,7 @@
@end @end
@interface uiAppDelegate : NSObject <NSApplicationDelegate> @implementation appDelegate
@end
@implementation uiAppDelegate
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)app - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)app
{ {
@ -54,10 +51,11 @@ const char *uiInit(uiInitOptions *o)
// don't check for a NO return; something (launch services?) causes running from application bundles to always return NO when asking to change activation policy, even if the change is to the same activation policy! // don't check for a NO return; something (launch services?) causes running from application bundles to always return NO when asking to change activation policy, even if the change is to the same activation policy!
// see https://github.com/andlabs/ui/issues/6 // see https://github.com/andlabs/ui/issues/6
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp setDelegate:[uiAppDelegate new]]; [NSApp setDelegate:[appDelegate new]];
// always do this so we always have an application menu // always do this so we always have an application menu
[NSApp setMainMenu:makeMenubar()]; appDelegate().menuManager = [menuManager new];
[NSApp setMainMenu:[appDelegate().menuManager makeMenubar]];
// we can use a stock NSView for this // we can use a stock NSView for this
destroyedControlsView = [[NSView alloc] initWithFrame:NSZeroRect]; destroyedControlsView = [[NSView alloc] initWithFrame:NSZeroRect];

99
new/darwin/menu.m Normal file
View File

@ -0,0 +1,99 @@
// 28 april 2015
#import "uipriv_darwin.h"
@implementation menuManager
// TODO allocation logger
- (id)init
{
self = [super init];
if (self)
self->items = [NSMutableDictionary new];
return self;
}
- (void)dealloc
{
[self->items release];
[super dealloc];
}
- (IBAction)onClicked:(id)sender
{
// TODO
}
// Cocoa constructs the default application menu by hand for each program; that's what MainMenu.[nx]ib does
// TODO investigate setAppleMenu:
- (void)buildApplicationMenu:(NSMenu *)menubar
{
NSString *appName;
NSMenuItem *appMenuItem;
NSMenu *appMenu;
NSMenuItem *item;
NSString *title;
appName = [[NSProcessInfo processInfo] processName];
appMenuItem = [[NSMenuItem alloc] initWithTitle:appName action:NULL keyEquivalent:@""];
appMenu = [[NSMenu alloc] initWithTitle:appName];
[appMenuItem setSubmenu:appMenu];
[menubar addItem:appMenuItem];
// first is About
title = [@"About " stringByAppendingString:appName];
item = [[NSMenuItem alloc] initWithTitle:title action:@selector(onClicked:) keyEquivalent:@""];
[item setTarget:self];
[item setEnabled:NO];
[appMenu addItem:item];
self.aboutItem = item;
[appMenu addItem:[NSMenuItem separatorItem]];
// next is Preferences
item = [[NSMenuItem alloc] initWithTitle:@"Preferences…" action:@selector(onClicked:) keyEquivalent:@","];
[item setTarget:self];
[item setEnabled:NO];
[appMenu addItem:item];
self.preferencesItem = item;
[appMenu addItem:[NSMenuItem separatorItem]];
// next is Services
item = [[NSMenuItem alloc] initWithTitle:@"Services" action:NULL keyEquivalent:@""];
// TODO build this part
[appMenu addItem:[NSMenuItem separatorItem]];
// next are the three hiding options
title = [@"Hide " stringByAppendingString:appName];
item = [[NSMenuItem alloc] initWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
// TODO set target for all three of these? the .xib file says they go to -1 ("First Responder", which sounds wrong...)
[appMenu addItem:item];
item = [[NSMenuItem alloc] initWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
[item setKeyEquivalentModifierMask:(NSAlternateKeyMask | NSCommandKeyMask)];
[appMenu addItem:item];
item = [[NSMenuItem alloc] initWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
[appMenu addItem:item];
[appMenu addItem:[NSMenuItem separatorItem]];
// and finally Quit
// DON'T use @selector(terminate:) as the action; we handle termination ourselves (TODO figure out how)
title = [@"Quit " stringByAppendingString:appName];
item = [[NSMenuItem alloc] initWithTitle:title action:@selector(onClicked:) keyEquivalent:@"q"];
[item setTarget:self];
[appMenu addItem:item];
self.quitItem = item;
}
- (NSMenu *)makeMenubar
{
NSMenu *menubar;
menubar = [[NSMenu alloc] initWithTitle:@""];
[self buildApplicationMenu:menubar];
return menubar;
}
@end

View File

@ -24,23 +24,31 @@
fprintf(stderr, "%p free\n", self); \ fprintf(stderr, "%p free\n", self); \
} }
#define VIEW(c) uiControlHandle(uiControl(c))
// init.m
extern NSView *destroyedControlsView;
// util.m
extern void setStandardControlFont(NSControl *);
extern void disableAutocorrect(NSTextView *);
// parent.m
// These are based on measurements from Interface Builder. // 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... // These seem to be based on Auto Layout constants, but I don't see an API that exposes these...
#define macXMargin 20 #define macXMargin 20
#define macYMargin 20 #define macYMargin 20
// menu.m
@interface menuManager : NSObject {
NSMutableDictionary *items;
}
@property NSMenuItem *quitItem;
@property NSMenuItem *preferencesItem;
@property NSMenuItem *aboutItem;
- (IBAction)onMenuItemClicked:(id)sender;
- (NSMenu *)makeMenubar;
@end
// init.m
@interface appDelegate : NSObject <NSApplicationDelegate>
@property menuManager *menuManager;
@end
#define appDelegate() ((appDelegate *) [NSApp delegate])
// util.m
extern void setStandardControlFont(NSControl *);
extern void disableAutocorrect(NSTextView *);
// entry.m // entry.m
extern void finishNewTextField(NSTextField *, BOOL); extern void finishNewTextField(NSTextField *, BOOL);
// menu.m
extern NSMenu *makeMenubar(void);