semi functional toolbar (Cocoa)

Sat, 26 Jul 2025 20:06:02 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 26 Jul 2025 20:06:02 +0200
changeset 677
04bcb1930fbf
parent 676
f0eea815c5ff
child 678
5a6edc92c7d9

semi functional toolbar (Cocoa)

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/MainWindow.m file | annotate | diff | comparison | revisions
ui/cocoa/Toolbar.h file | annotate | diff | comparison | revisions
ui/cocoa/Toolbar.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	Tue Jul 22 22:32:34 2025 +0200
+++ b/make/xcode/toolkit/toolkit.xcodeproj/project.pbxproj	Sat Jul 26 20:06:02 2025 +0200
@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		ED2F55AE2E34FAD800A84793 /* Toolbar.m in Sources */ = {isa = PBXBuildFile; fileRef = ED2F55AD2E34FAD800A84793 /* Toolbar.m */; };
 		ED52BFB02D86FC5D00FD8BE5 /* text.m in Sources */ = {isa = PBXBuildFile; fileRef = ED52BFAF2D86FC5D00FD8BE5 /* text.m */; };
 		ED6580EE2CFF19F900F5402F /* context.c in Sources */ = {isa = PBXBuildFile; fileRef = ED6580DD2CFF19F900F5402F /* context.c */; };
 		ED6580EF2CFF19F900F5402F /* menu.c in Sources */ = {isa = PBXBuildFile; fileRef = ED6580E12CFF19F900F5402F /* menu.c */; };
@@ -48,6 +49,8 @@
 /* End PBXBuildFile section */
 
 /* Begin PBXFileReference section */
+		ED2F55AC2E34FAD800A84793 /* Toolbar.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Toolbar.h; path = /Users/olaf/Projekte/toolkit/ui/cocoa/Toolbar.h; sourceTree = "<absolute>"; };
+		ED2F55AD2E34FAD800A84793 /* Toolbar.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = Toolbar.m; path = /Users/olaf/Projekte/toolkit/ui/cocoa/Toolbar.m; sourceTree = "<absolute>"; };
 		ED52BFAE2D86FC5D00FD8BE5 /* text.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = text.h; path = /Users/olaf/Projekte/toolkit/ui/cocoa/text.h; sourceTree = "<absolute>"; };
 		ED52BFAF2D86FC5D00FD8BE5 /* text.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = text.m; path = /Users/olaf/Projekte/toolkit/ui/cocoa/text.m; sourceTree = "<absolute>"; };
 		ED6580AC2CFF122700F5402F /* toolkit.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = toolkit.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -266,6 +269,8 @@
 		ED65812E2CFF1A7200F5402F /* cocoa */ = {
 			isa = PBXGroup;
 			children = (
+				ED2F55AC2E34FAD800A84793 /* Toolbar.h */,
+				ED2F55AD2E34FAD800A84793 /* Toolbar.m */,
 				EDB452C12E302C65006FB12D /* image.h */,
 				EDB452C22E302C65006FB12D /* image.m */,
 				ED8687E32D999CF3002F3EC2 /* menu.h */,
@@ -403,6 +408,7 @@
 				ED65815F2CFF4BF200F5402F /* WindowManager.m in Sources */,
 				ED6580F22CFF19F900F5402F /* document.c in Sources */,
 				ED65811D2CFF1A3000F5402F /* linked_list.c in Sources */,
+				ED2F55AE2E34FAD800A84793 /* Toolbar.m in Sources */,
 				ED65811E2CFF1A3000F5402F /* tree.c in Sources */,
 				ED6581202CFF1A3000F5402F /* mempool.c in Sources */,
 				ED6581212CFF1A3000F5402F /* map.c in Sources */,
--- a/make/xcode/toolkit/toolkit/main.m	Tue Jul 22 22:32:34 2025 +0200
+++ b/make/xcode/toolkit/toolkit/main.m	Sat Jul 26 20:06:02 2025 +0200
@@ -80,6 +80,12 @@
         }
     }
     
+    ui_toolbar_item("item1", .label = "Test 1");
+    ui_toolbar_item("item2", .label = "Test 2");
+    
+    ui_toolbar_add_default("item1", UI_TOOLBAR_LEFT);
+    ui_toolbar_add_default("item1", UI_TOOLBAR_RIGHT);
+    
     ui_main();
     return 0;
 }
--- a/ui/cocoa/MainWindow.m	Tue Jul 22 22:32:34 2025 +0200
+++ b/ui/cocoa/MainWindow.m	Sat Jul 26 20:06:02 2025 +0200
@@ -34,6 +34,7 @@
 
 #import "EventData.h"
 #import "menu.h"
+#import "Toolbar.h"
 
 @implementation MainWindow
 
@@ -48,6 +49,12 @@
                              backing:NSBackingStoreBuffered
                                defer:false];
     
+    if(uic_toolbar_isenabled()) {
+        UiToolbar *toolbar = [[UiToolbar alloc]initWithObject:obj];
+        [self setToolbar:toolbar];
+    }
+    
+    
     // create a vertical stackview as default container
     BoxContainer *vbox = [[BoxContainer alloc] init:NSUserInterfaceLayoutOrientationVertical spacing:0];
     //GridLayout *vbox = [[GridLayout alloc] init];
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/cocoa/Toolbar.h	Sat Jul 26 20:06:02 2025 +0200
@@ -0,0 +1,59 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2025 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 "toolkit.h"
+#import "../common/toolbar.h"
+
+/*
+ * UiToolbarDelegate
+ */
+@interface UiToolbarDelegate : NSObject <NSToolbarDelegate> {
+    NSMutableArray<NSString*> *allowedItems;
+    NSMutableArray<NSString*> *defaultItems;
+}
+
+- (UiToolbarDelegate*) init;
+
+@end
+
+/*
+ * UiToolbar
+ */
+@interface UiToolbar : NSToolbar <NSToolbarDelegate> {
+    NSMutableArray<NSString*> *allowedItems;
+    NSMutableArray<NSString*> *defaultItems;
+}
+
+@property UiObject *obj;
+
+- (UiToolbar*) initWithObject:(UiObject*)object;
+
+@end
+
+
+void ui_toolbar_init(void);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/cocoa/Toolbar.m	Sat Jul 26 20:06:02 2025 +0200
@@ -0,0 +1,108 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2025 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 "Toolbar.h"
+
+#include "../common/toolbar.h"
+
+void ui_toolbar_init(void) {
+    
+}
+
+
+
+/* ---------------------      UiToolbar      --------------------- */
+
+@implementation UiToolbar
+
+- (UiToolbar*) initWithObject:(UiObject*)object {
+    self = [super initWithIdentifier:@"UiToolbar"];
+    _obj = object;
+    
+    allowedItems = [[NSMutableArray alloc]initWithCapacity:16];
+    defaultItems = [[NSMutableArray alloc]initWithCapacity:16];
+    
+    CxMap *toolbarItems = uic_get_toolbar_items();
+    CxMapIterator i = cxMapIteratorKeys(toolbarItems);
+    cx_foreach(CxHashKey *, key, i) {
+        NSString *s = [[NSString alloc]initWithBytes:key->data length:key->len encoding:NSUTF8StringEncoding];
+        [allowedItems addObject:s];
+    }
+    [allowedItems addObject: NSToolbarFlexibleSpaceItemIdentifier];
+    [allowedItems addObject: NSToolbarSpaceItemIdentifier];
+    
+    CxList *tbitems[3];
+    tbitems[0] =  uic_get_toolbar_defaults(UI_TOOLBAR_LEFT);
+    tbitems[1] =  uic_get_toolbar_defaults(UI_TOOLBAR_CENTER);
+    tbitems[2] =  uic_get_toolbar_defaults(UI_TOOLBAR_RIGHT);
+    for(int t=0;t<3;t++) {
+        CxIterator iter = cxListIterator(tbitems[t]);
+        cx_foreach(char *, name, iter) {
+            NSString *s = [[NSString alloc] initWithUTF8String:name];
+            [defaultItems addObject:s];
+        }
+    }
+    
+    [self setDelegate:self];
+    [self setAllowsUserCustomization:YES];
+    return self;
+}
+
+// implementation of NSToolbarDelegate methods
+
+- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar {
+    return allowedItems;
+}
+
+- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
+    return defaultItems;
+}
+
+- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
+  itemForItemIdentifier:(NSString *)itemIdentifier
+  willBeInsertedIntoToolbar:(BOOL)flag {
+    CxMap *items = uic_get_toolbar_items();
+    UiToolbarItemI *item = cxMapGet(items, itemIdentifier.UTF8String);
+    if(!item) {
+        return nil;
+    }
+    
+    switch(item->type) {
+        default: return nil;
+        case UI_TOOLBAR_ITEM: {
+            NSToolbarItem *tbItem = [[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier];
+            [tbItem setLabel: @"Test"];
+            return tbItem;
+            break;
+        }
+    }
+    
+    return nil;
+}
+
+@end
--- a/ui/cocoa/objs.mk	Tue Jul 22 22:32:34 2025 +0200
+++ b/ui/cocoa/objs.mk	Sat Jul 26 20:06:02 2025 +0200
@@ -41,6 +41,7 @@
 COCOAOBJ += button.o
 COCOAOBJ += text.o
 COCOAOBJ += menu.o
+COCOAOBJ += Toolbar.o
 
 TOOLKITOBJS += $(COCOAOBJ:%=$(COCOA_OBJPRE)%)
 TOOLKITSOURCE += $(COCOAOBJ:%.o=cocoa/%.m)
--- a/ui/cocoa/toolkit.m	Tue Jul 22 22:32:34 2025 +0200
+++ b/ui/cocoa/toolkit.m	Sat Jul 26 20:06:02 2025 +0200
@@ -35,6 +35,7 @@
 #include "../common/threadpool.h"
 
 #import "menu.h"
+#import "Toolbar.h"
 
 #import "AppDelegate.h"
 
@@ -70,6 +71,8 @@
     [NSApplication sharedApplication];
     //[NSBundle loadNibNamed:@"MainMenu" owner:NSApp ];
     //[[NSBundle mainBundle] loadNibNamed:@"MainMenu" owner:NSApp topLevelObjects:&topLevelObjects];
+    
+    ui_toolbar_init();
 }
 
 const char* ui_appname() {

mercurial