add basic code structure for cocoa menu implementation

2 days ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 30 Mar 2025 17:54:17 +0200 (2 days ago)
changeset 541
63289f61fb7f
parent 540
d8b86f66721c
child 542
af268fbe676a

add basic code structure for cocoa menu implementation

make/xcode/toolkit/toolkit.xcodeproj/project.pbxproj file | annotate | diff | comparison | revisions
make/xcode/toolkit/toolkit/main.m file | annotate | diff | comparison | revisions
ui/cocoa/appdelegate.m file | annotate | diff | comparison | revisions
ui/cocoa/menu.h file | annotate | diff | comparison | revisions
ui/cocoa/menu.m file | annotate | diff | comparison | revisions
ui/cocoa/objs.mk file | annotate | diff | comparison | revisions
ui/cocoa/toolkit.m file | annotate | diff | comparison | revisions
--- a/make/xcode/toolkit/toolkit.xcodeproj/project.pbxproj	Sun Mar 30 17:06:27 2025 +0200
+++ b/make/xcode/toolkit/toolkit.xcodeproj/project.pbxproj	Sun Mar 30 17:54:17 2025 +0200
@@ -43,6 +43,7 @@
 		ED6581462CFF3BCE00F5402F /* GridLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = ED6581402CFF3BCE00F5402F /* GridLayout.m */; };
 		ED65815C2CFF3EE900F5402F /* MainWindow.m in Sources */ = {isa = PBXBuildFile; fileRef = ED65815B2CFF3EE900F5402F /* MainWindow.m */; };
 		ED65815F2CFF4BF200F5402F /* WindowManager.m in Sources */ = {isa = PBXBuildFile; fileRef = ED65815E2CFF4BF200F5402F /* WindowManager.m */; };
+		ED8687E52D999CF3002F3EC2 /* menu.m in Sources */ = {isa = PBXBuildFile; fileRef = ED8687E42D999CF3002F3EC2 /* menu.m */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXFileReference section */
@@ -140,6 +141,8 @@
 		ED65815B2CFF3EE900F5402F /* MainWindow.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = MainWindow.m; path = /Users/olaf/Projekte/toolkit/ui/cocoa/MainWindow.m; sourceTree = "<absolute>"; };
 		ED65815D2CFF4BF200F5402F /* WindowManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = WindowManager.h; path = /Users/olaf/Projekte/toolkit/ui/cocoa/WindowManager.h; sourceTree = "<absolute>"; };
 		ED65815E2CFF4BF200F5402F /* WindowManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = WindowManager.m; path = /Users/olaf/Projekte/toolkit/ui/cocoa/WindowManager.m; sourceTree = "<absolute>"; };
+		ED8687E32D999CF3002F3EC2 /* menu.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = menu.h; path = ../../../ui/cocoa/menu.h; sourceTree = SOURCE_ROOT; };
+		ED8687E42D999CF3002F3EC2 /* menu.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = menu.m; path = ../../../ui/cocoa/menu.m; sourceTree = SOURCE_ROOT; };
 /* End PBXFileReference section */
 
 /* Begin PBXFileSystemSynchronizedRootGroup section */
@@ -260,6 +263,8 @@
 		ED65812E2CFF1A7200F5402F /* cocoa */ = {
 			isa = PBXGroup;
 			children = (
+				ED8687E32D999CF3002F3EC2 /* menu.h */,
+				ED8687E42D999CF3002F3EC2 /* menu.m */,
 				ED52BFAE2D86FC5D00FD8BE5 /* text.h */,
 				ED52BFAF2D86FC5D00FD8BE5 /* text.m */,
 				ED65815D2CFF4BF200F5402F /* WindowManager.h */,
@@ -418,6 +423,7 @@
 				ED6580F32CFF19F900F5402F /* object.c in Sources */,
 				ED6580F42CFF19F900F5402F /* toolbar.c in Sources */,
 				ED6580F52CFF19F900F5402F /* types.c in Sources */,
+				ED8687E52D999CF3002F3EC2 /* menu.m in Sources */,
 				ED6580F62CFF19F900F5402F /* properties.c in Sources */,
 				ED6580F72CFF19F900F5402F /* ucx_properties.c in Sources */,
 			);
--- a/make/xcode/toolkit/toolkit/main.m	Sun Mar 30 17:06:27 2025 +0200
+++ b/make/xcode/toolkit/toolkit/main.m	Sun Mar 30 17:54:17 2025 +0200
@@ -55,6 +55,26 @@
 int main(int argc, char * argv[]) {
     ui_init("app1", argc, argv);
     ui_onstartup(application_startup, NULL);
+    
+    // menu
+    ui_menu("File") {
+        ui_menuitem(.label = "Test");
+    }
+    
+    ui_menu("Edit") {
+        ui_menuitem(.label = "Undo");
+        ui_menu_toggleitem(.label = "Checkbox");
+        ui_menuseparator();
+        ui_menu_radioitem(.label = "Option 1", .varname = "menuoption");
+        ui_menu_radioitem(.label = "Option 2", .varname = "menuoption");
+        ui_menu_radioitem(.label = "Option 3", .varname = "menuoption");
+        ui_menu_radioitem(.label = "Option 4", .varname = "menuoption");
+        ui_menuseparator();
+        ui_menu("Submenu") {
+            ui_menuitem(.label = "Subitem");
+        }
+    }
+    
     ui_main();
     return 0;
 }
--- a/ui/cocoa/appdelegate.m	Sun Mar 30 17:06:27 2025 +0200
+++ b/ui/cocoa/appdelegate.m	Sun Mar 30 17:54:17 2025 +0200
@@ -29,10 +29,12 @@
 #import "AppDelegate.h"
 
 #import "toolkit.h"
+#import "menu.h"
 
 @implementation AppDelegate
 
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
+    ui_menu_init();
     ui_cocoa_onstartup();
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/cocoa/menu.h	Sun Mar 30 17:54:17 2025 +0200
@@ -0,0 +1,44 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "../ui/menu.h"
+#import "toolkit.h"
+
+#import "../common/menu.h"
+
+void ui_menu_init(void);
+
+typedef void(*ui_menu_add_f)(NSMenu*, int, UiMenuItemI*);
+
+void add_menu_widget(NSMenu *parent, int i, UiMenuItemI *item);
+void add_menuitem_widget(NSMenu *parent, int i, UiMenuItemI *item);
+void add_menuseparator_widget(NSMenu *parent, int i, UiMenuItemI *item);
+void add_checkitem_widget(NSMenu *parent, int i, UiMenuItemI *item);
+void add_radioitem_widget(NSMenu *parent, int index, UiMenuItemI *item);
+void add_checkitemnv_widget(NSMenu *parent, int i, UiMenuItemI *item);
+void add_menuitem_list_widget(NSMenu *parent, int i, UiMenuItemI *item);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/cocoa/menu.m	Sun Mar 30 17:54:17 2025 +0200
@@ -0,0 +1,109 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <stdio.h>
+#import <stdlib.h>
+#import <string.h>
+#import <stdarg.h>
+
+#import "menu.h"
+#import "window.h"
+
+static ui_menu_add_f createMenuItem[] = {
+    /* UI_MENU                 */ add_menu_widget,
+    /* UI_MENU_ITEM            */ add_menuitem_widget,
+    /* UI_MENU_CHECK_ITEM      */ add_checkitem_widget,
+    /* UI_MENU_RADIO_ITEM      */ add_radioitem_widget,
+    /* UI_MENU_ITEM_LIST       */ add_menuitem_list_widget,
+    /* UI_MENU_CHECKITEM_LIST  */ add_menuitem_list_widget,
+    /* UI_MENU_RADIOITEM_LIST  */ add_menuitem_list_widget,
+    /* UI_MENU_SEPARATOR       */ add_menuseparator_widget
+};
+
+static void add_menu_items(NSMenu *parent, int i, UiMenu *menu) {
+    UiMenuItemI *it = menu->items_begin;
+    int index = 0;
+    while(it) {
+        createMenuItem[it->type](parent, index, it);
+        it = it->next;
+        index++;
+    }
+}
+
+void add_menu_widget(NSMenu *parent, int i, UiMenuItemI *item) {
+    UiMenu *it = (UiMenu*)item;
+    NSString *str = [[NSString alloc] initWithUTF8String:it->label];
+    NSMenu *menu = [[NSMenu alloc] initWithTitle: str];
+    NSMenuItem *menuItem = [parent addItemWithTitle:str action:nil keyEquivalent:@""];
+    [parent setSubmenu:menu forItem:menuItem];
+    
+    add_menu_items(menu, i, it);
+}
+
+void add_menuitem_widget(NSMenu *parent, int i, UiMenuItemI *item) {
+    UiMenuItem *it = (UiMenuItem*)item;
+    NSString *str = [[NSString alloc] initWithUTF8String:it->label];
+    NSMenuItem *menuItem = [parent addItemWithTitle:str action:nil keyEquivalent:@""];
+}
+
+void add_menuseparator_widget(NSMenu *parent, int i, UiMenuItemI *item) {
+    
+}
+
+void add_checkitem_widget(NSMenu *parent, int i, UiMenuItemI *item) {
+    
+}
+
+void add_radioitem_widget(NSMenu *parent, int index, UiMenuItemI *item) {
+    
+}
+
+void add_checkitemnv_widget(NSMenu *parent, int i, UiMenuItemI *item) {
+    
+}
+
+void add_menuitem_list_widget(NSMenu *parent, int i, UiMenuItemI *item) {
+    
+}
+
+
+void ui_menu_init(void) {
+    UiMenu *menus_begin = uic_get_menu_list();
+    UiMenu *ls = menus_begin;
+    while(ls) {
+        if(ls->item.type == UI_MENU) {
+            NSString *str = [[NSString alloc] initWithUTF8String:ls->label];
+            NSMenu *menu = [[NSMenu alloc] initWithTitle: str];
+            NSMenuItem *menuItem = [[NSApp mainMenu] addItemWithTitle:str action:nil keyEquivalent:@""];
+            [[NSApp mainMenu] setSubmenu:menu forItem:menuItem];
+            
+            add_menu_items(menu, 0, ls);
+        }
+        ls = (UiMenu*)ls->item.next;
+    }
+}
--- a/ui/cocoa/objs.mk	Sun Mar 30 17:06:27 2025 +0200
+++ b/ui/cocoa/objs.mk	Sun Mar 30 17:54:17 2025 +0200
@@ -40,6 +40,7 @@
 COCOAOBJ += Container.o
 COCOAOBJ += button.o
 COCOAOBJ += text.o
+COCOAOBJ += menu.o
 
 TOOLKITOBJS += $(COCOAOBJ:%=$(COCOA_OBJPRE)%)
 TOOLKITSOURCE += $(COCOAOBJ:%.o=cocoa/%.m)
--- a/ui/cocoa/toolkit.m	Sun Mar 30 17:06:27 2025 +0200
+++ b/ui/cocoa/toolkit.m	Sun Mar 30 17:54:17 2025 +0200
@@ -34,6 +34,8 @@
 #include "../common/toolbar.h"
 #include "../common/threadpool.h"
 
+#import "menu.h"
+
 #import "AppDelegate.h"
 
 static const char *application_name;

mercurial