Implemented menu uniqueness checks on OS X. More TODOs.

This commit is contained in:
Pietro Gagliardi 2015-04-30 23:24:52 -04:00
parent 20d0c40ad4
commit 442907ad64
2 changed files with 30 additions and 11 deletions

View File

@ -3,6 +3,8 @@
// general TODO: release Objective-C objects in dealloc since we can't use ARC // general TODO: release Objective-C objects in dealloc since we can't use ARC
// TODO menu finalization
struct menu { struct menu {
uiMenu m; uiMenu m;
NSMenu *menu; NSMenu *menu;
@ -31,9 +33,13 @@ enum {
- (id)init - (id)init
{ {
self = [super init]; self = [super init];
if (self) if (self) {
self->items = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsOpaqueMemory self->items = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsOpaqueMemory
valueOptions:NSPointerFunctionsOpaqueMemory]; valueOptions:NSPointerFunctionsOpaqueMemory];
self->hasQuit = NO;
self->hasPreferences = NO;
self->hasAbout = NO;
}
return self; return self;
} }
@ -61,6 +67,23 @@ enum {
{ {
NSValue *v; NSValue *v;
switch (smi->type) {
case typeQuit:
if (self->hasQuit)
complain("attempt to add multiple Quit menu items");
self->hasQuit = YES;
break;
case typePreferences:
if (self->hasPreferences)
complain("attempt to add multiple Preferences menu items");
self->hasPreferences = YES;
break;
case typeAbout:
if (self->hasAbout)
complain("attempt to add multiple About menu items");
self->hasAbout = YES;
break;
}
v = [NSValue valueWithPointer:smi]; v = [NSValue valueWithPointer:smi];
[self->items setObject:v forKey:item]; [self->items setObject:v forKey:item];
} }
@ -243,31 +266,24 @@ uiMenuItem *menuAppendCheckItem(uiMenu *mm, const char *name)
uiMenuItem *menuAppendQuitItem(uiMenu *mm) uiMenuItem *menuAppendQuitItem(uiMenu *mm)
{ {
// TODO check multiple quit items // duplicate check is in the register:to: selector
// TODO conditionally add separator
newItem((struct menu *) mm, typeSeparator, NULL);
return newItem((struct menu *) mm, typeQuit, NULL); return newItem((struct menu *) mm, typeQuit, NULL);
} }
uiMenuItem *menuAppendPreferencesItem(uiMenu *mm) uiMenuItem *menuAppendPreferencesItem(uiMenu *mm)
{ {
// TODO check multiple preferences items // duplicate check is in the register:to: selector
// TODO conditionally add separator
newItem((struct menu *) mm, typeSeparator, NULL);
return newItem((struct menu *) mm, typePreferences, NULL); return newItem((struct menu *) mm, typePreferences, NULL);
} }
uiMenuItem *menuAppendAboutItem(uiMenu *mm) uiMenuItem *menuAppendAboutItem(uiMenu *mm)
{ {
// TODO check multiple about items // duplicate check is in the register:to: selector
// TODO conditionally add separator
newItem((struct menu *) mm, typeSeparator, NULL);
return newItem((struct menu *) mm, typeAbout, NULL); return newItem((struct menu *) mm, typeAbout, NULL);
} }
void menuAppendSeparator(uiMenu *mm) void menuAppendSeparator(uiMenu *mm)
{ {
// TODO check multiple about items
newItem((struct menu *) mm, typeSeparator, NULL); newItem((struct menu *) mm, typeSeparator, NULL);
} }

View File

@ -18,6 +18,9 @@
@interface menuManager : NSObject { @interface menuManager : NSObject {
// unfortunately NSMutableDictionary copies its keys, meaning we can't use it for pointers // unfortunately NSMutableDictionary copies its keys, meaning we can't use it for pointers
NSMapTable *items; NSMapTable *items;
BOOL hasQuit;
BOOL hasPreferences;
BOOL hasAbout;
} }
@property (strong) NSMenuItem *quitItem; @property (strong) NSMenuItem *quitItem;
@property (strong) NSMenuItem *preferencesItem; @property (strong) NSMenuItem *preferencesItem;