ui/cocoa/Toolbar.m

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
child 678
5a6edc92c7d9
permissions
-rw-r--r--

semi functional toolbar (Cocoa)

/*
 * 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

mercurial