ui/cocoa/Toolbar.m

changeset 678
5a6edc92c7d9
parent 677
04bcb1930fbf
child 687
d43b4fcd3d8c
--- a/ui/cocoa/Toolbar.m	Sat Jul 26 20:06:02 2025 +0200
+++ b/ui/cocoa/Toolbar.m	Sun Jul 27 17:32:20 2025 +0200
@@ -27,9 +27,12 @@
  */
 
 #import "Toolbar.h"
+#import "EventData.h"
+#import <objc/runtime.h>
 
 #include "../common/toolbar.h"
 
+
 void ui_toolbar_init(void) {
     
 }
@@ -95,10 +98,10 @@
     switch(item->type) {
         default: return nil;
         case UI_TOOLBAR_ITEM: {
-            NSToolbarItem *tbItem = [[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier];
-            [tbItem setLabel: @"Test"];
-            return tbItem;
-            break;
+            return ui_nstoolbaritem_create_item(_obj, (UiToolbarItem*)item, itemIdentifier);
+        }
+        case UI_TOOLBAR_TOGGLEITEM: {
+            return ui_nstoolbaritem_create_toggle(_obj, (UiToolbarToggleItem*)item, itemIdentifier);
         }
     }
     
@@ -106,3 +109,39 @@
 }
 
 @end
+
+NSToolbarItem* ui_nstoolbaritem_create_item(UiObject *obj, UiToolbarItem *item, NSString *identifier) {
+    NSToolbarItem *tbItem = [[NSToolbarItem alloc] initWithItemIdentifier: identifier];
+    
+    NSButton *button = [[NSButton alloc] init];
+    tbItem.view = button;
+    
+    if(item->args.label) {
+        NSString *label = [[NSString alloc] initWithUTF8String:item->args.label];
+        [tbItem setLabel:label];
+        if(!item->args.icon) {
+            button.title = label;
+        }
+    }
+    if(item->args.icon) {
+        button.image = [NSImage imageNamed: [[NSString alloc] initWithUTF8String:item->args.icon]];
+    }
+    
+    if(item->args.onclick) {
+        EventData *event = [[EventData alloc] init:item->args.onclick userdata:item->args.onclickdata];
+        event.obj = obj;
+        button.target = event;
+        button.action = @selector(handleEvent:);
+        objc_setAssociatedObject(button, "eventdata", event, OBJC_ASSOCIATION_RETAIN);
+    }
+    return tbItem;
+}
+
+NSToolbarItem* ui_nstoolbaritem_create_toggle(UiObject *obj, UiToolbarToggleItem *item, NSString *identifier) {
+    NSToolbarItem *tbItem = [[NSToolbarItem alloc] initWithItemIdentifier: identifier];
+    if(item->args.label) {
+        NSString *label = [[NSString alloc] initWithUTF8String:item->args.label];
+        [tbItem setLabel:label];
+    }
+    return tbItem;
+}

mercurial