Added autogeneration of Menus via newEditMenu/newFormatMenu

This commit is contained in:
Armando Rivera 2021-08-03 22:24:15 -04:00
parent 27ebd03c48
commit 40311dab75
2 changed files with 117 additions and 1 deletions

View File

@ -4,4 +4,8 @@ import NSFunctions
proc newMenu*(title: cstring): ID {.cdecl, importc: "createMenu".}
proc newMenuItem*(parent: ID, caption, key: cstring, `callback`:ACTION) {.cdecl, importc: "createMenuItem".}
proc newMenuSeparator*(parent: ID) {.cdecl, importc: "createMenuSeparator".}
proc newMenuSeparator*(parent: ID) {.cdecl, importc: "createMenuSeparator".}
proc setSystemAction*(widget: ID, title, actionName: cstring) {.cdecl, importc: "setSystemAction".}
proc newFileMenu*() {.cdecl, importc: "createFileMenu".}
proc newEditMenu*() {.cdecl, importc: "createEditMenu".}
proc newFormatMenu*() {.cdecl, importc: "createFormatMenu".}

View File

@ -27,6 +27,7 @@
[item setTitle:Title];
[item setTarget:item];
[item setKeyEquivalent: key];
[item setIdentifier: Title];
if (callback != NULL) {
[item setMenuItemAction: callback];
@ -44,6 +45,100 @@
[widget setMenuAction:func];
}
- (id) createFileMenu {
NSMenuItem *newMenuItem = [[NSMenuItem alloc] initWithTitle:@"File" action:NULL keyEquivalent:@""];
NSMenu *fileMenu = [[NSMenu alloc] initWithTitle:@"File"];
id newMenu =[[[NSMenuItem alloc] initWithTitle:@"New"
action:@selector(newDocument:)
keyEquivalent:@"n"]
autorelease];
[fileMenu addItem:newMenu];
[newMenuItem setSubmenu: fileMenu];
[[[NSApplication sharedApplication] mainMenu] addItem: newMenuItem];
}
- (id) createEditMenu {
NSString *appName = [[NSProcessInfo processInfo] processName];
NSMenuItem *newMenuItem = [[NSMenuItem alloc] initWithTitle:@"Edit" action:NULL keyEquivalent:@""];
NSMenu *editMenu = [[NSMenu alloc] initWithTitle:@"Edit"];
id undoMenu =[[[NSMenuItem alloc] initWithTitle:@"Undo"
action:@selector(undo:)
keyEquivalent:@"z"]
autorelease];
[editMenu addItem:undoMenu];
// ;
id redoMenu =[[[NSMenuItem alloc] initWithTitle:@"Redo"
action:@selector(redo:)
keyEquivalent:@"Z"]
autorelease];
[editMenu addItem:redoMenu];
[editMenu addItem: [NSMenuItem separatorItem]];
id cutMenu =[[[NSMenuItem alloc] initWithTitle:@"Cut"
action:@selector(cut:)
keyEquivalent:@"x"]
autorelease];
[editMenu addItem:cutMenu];
id copyMenu =[[[NSMenuItem alloc] initWithTitle:@"Copy"
action:@selector(copy:)
keyEquivalent:@"c"]
autorelease];
[editMenu addItem:copyMenu];
id pasteMenu =[[[NSMenuItem alloc] initWithTitle:@"Paste"
action:@selector(paste:)
keyEquivalent:@"v"]
autorelease];
[editMenu addItem:pasteMenu];
id pasteAndMatchMenu =[[[NSMenuItem alloc] initWithTitle:@"Paste and Match Style"
action:@selector(pastAsPlainText:)
keyEquivalent:@""]
autorelease];
[editMenu addItem:pasteAndMatchMenu];
id selectAllMenu =[[[NSMenuItem alloc] initWithTitle:@"Select All"
action:@selector(selectAll:)
keyEquivalent:@"a"]
autorelease];
[editMenu addItem:selectAllMenu];
// [newMenuItem setSubmenu: selectAllMenu];
// [editMenu addItem: [NSMenuItem separatorItem]];
[newMenuItem setSubmenu: editMenu];
[[[NSApplication sharedApplication] mainMenu] addItem: newMenuItem];
}
- (id) createFormatMenu{
NSFontManager *fontManager = [NSFontManager sharedFontManager];
[fontManager setDelegate: self];
NSMenu *fontMenu = [fontManager fontMenu:YES];
// [[[NSApplication sharedApplication] mainMenu] addItem: fontManager];
NSMenuItem *newMenuItem = [[NSMenuItem alloc] initWithTitle:@"Format" action:NULL keyEquivalent:@""];
NSMenu *blah = [[NSMenu alloc] initWithTitle: @"Format"];
[newMenuItem setSubmenu: blah];
[newMenuItem setSubmenu: fontMenu];
[[[NSApplication sharedApplication] mainMenu] addItem: newMenuItem];
}
@end
@ -62,3 +157,20 @@ void createMenuItem(id parent, const char *caption, const char *key, ACTION call
void createMenuSeparator(id parent){
[parent addSeparator];
}
id createFileMenu() {
id widget = [CocoaMenu new];
[widget createFileMenu];
return widget;
}
id createFormatMenu() {
id widget = [CocoaMenu new];
[widget createFormatMenu];
return widget;
}
id createEditMenu() {
id widget = [CocoaMenu new];
[widget createEditMenu];
return widget;
}