ui/cocoa/toolbar.m

Thu, 15 Feb 2024 21:33:08 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 15 Feb 2024 21:33:08 +0100
branch
newapi
changeset 262
81778b9589d0
parent 129
5babf09f5f19
permissions
-rw-r--r--

port progressbar to new API (GTK)

/*
 * 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 <inttypes.h>
#import <stdarg.h>

#import "toolbar.h"
#import "window.h"
#import "stock.h"


static UiToolbarDelegate* toolbar_delegate;

/* ---------------------      UiToolbarStockItem     --------------------- */

@implementation UiToolbarStockItem

- (UiToolbarStockItem*) initWithIdentifier:(char*)identifier
                                   stockID:(char*)sid
                                  callback:(ui_callback)f
                                  userdata:(void*)data
{
    name = identifier;
    stockid = sid;
    callback = f;
    userdata = data;
    groups = NULL;
    isToggleButton = NO;
    return self;
}

- (void) setIsToggleButton:(BOOL)t {
    isToggleButton = t;
}

- (void) addGroup:(int)group {
    groups = ucx_list_append(groups, (void*)(intptr_t)group);
}


- (NSToolbarItem *) createItem:(NSToolbar*)toolbar
                    identifier:(NSString*)identifier
                        object:(UiObject*)obj
{
    UiStockItem *s = ui_get_stock_item(stockid);
    if(s == nil) {
        printf("cannot find stock item\n");
        return nil;
    }
    
    NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier:
                            identifier] autorelease];
    //[item setLabel:[s label]];
    //[item setPaletteLabel:[s label]];
    [item setLabel:s->label];
    [item setPaletteLabel:@"Operation"];
    
    // create button ...
    NSRect frame = NSMakeRect(0, 0, 40, 22);
    //NSSearchField *sf = [[NSSearchField alloc]initWithFrame:frame];
    NSButton *button = [[NSButton alloc]initWithFrame:frame];
    //[button setImage:[s buttonImage]];
    //[button setImage:[NSImage imageNamed: NSImageNameAddTemplate]];
    if(s->image) {
        [button setImage:s->image];
    } else {
        [button setImage:[NSImage imageNamed: NSImageNameRemoveTemplate]];
    }
    [button setBezelStyle: NSTexturedRoundedBezelStyle];
    
    // event
    EventWrapper *event = [[EventWrapper alloc]
                           initWithData:userdata callback:callback];
    if(isToggleButton) {
        [button setButtonType: NSPushOnPushOffButton];
        [button setAction:@selector(handleToggleEvent:)];
    } else {
        [button setAction:@selector(handleEvent:)];
    }
    [button setTarget:event];
    
    if(groups) {
        uic_add_group_widget(obj->ctx, item, groups);
    }
    
    [item setView:button];
    return item;
}

- (UcxList*) groups {
    return groups;
}

@end


/* ---------------------      UiToolbarItem     --------------------- */

@implementation UiToolbarItem

- (UiToolbarItem*) initWithIdentifier:(char*)identifier
                                     label:(char*)lbl
                                  callback:(ui_callback)f
                                  userdata:(void*)data
{
    name = identifier;
    label = lbl;
    callback = f;
    userdata = data;
    groups = NULL;
    isToggleButton = NO;
    return self;
}

- (void) setIsToggleButton:(BOOL)t {
    isToggleButton = t;
}

- (void) addGroup:(int)group {
    groups = ucx_list_append(groups, (void*)(intptr_t)group);
}


- (NSToolbarItem *) createItem:(NSToolbar*)toolbar
                    identifier:(NSString*)identifier
                        object:(UiObject*)obj
{
    NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier:
                            identifier] autorelease];
    //[item setLabel:[s label]];
    //[item setPaletteLabel:[s label]];
    NSString *l = [[NSString alloc]initWithUTF8String:label];
    [item setLabel:l];
    [item setPaletteLabel:@"Operation"];
    
    // create button ...
    NSRect frame = NSMakeRect(0, 0, 40, 22);
    //NSSearchField *sf = [[NSSearchField alloc]initWithFrame:frame];
    NSButton *button = [[NSButton alloc]initWithFrame:frame];
    //[button setImage:[s buttonImage]];
    //[button setImage:[NSImage imageNamed: NSImageNameAddTemplate]];
    
    // TODO: image
    [button setImage:[NSImage imageNamed: NSImageNameRemoveTemplate]];
    
    [button setBezelStyle: NSTexturedRoundedBezelStyle];
    
    // event
    EventWrapper *event = [[EventWrapper alloc]
                           initWithData:userdata callback:callback];
    if(isToggleButton) {
        [button setButtonType: NSPushOnPushOffButton];
        [button setAction:@selector(handleToggleEvent:)];
    } else {
        [button setAction:@selector(handleEvent:)];
    }
    [button setTarget:event];
    
    if(groups) {
        uic_add_group_widget(obj->ctx, item, groups);
    }
    
    [item setView:button];
    return item;
}

- (UcxList*) groups {
    return groups;
}

@end


/* ---------------------      UiToolbarDelegate      --------------------- */

@implementation UiToolbarDelegate

- (UiToolbarDelegate*) init {
    allowedItems = [[NSMutableArray alloc]initWithCapacity: 16];
    defaultItems = [[NSMutableArray alloc]initWithCapacity: 16];
    items = [[NSMutableDictionary alloc] init];
    return self;
}

- (void) addDefault:(NSString*)identifier {
    [defaultItems addObject: identifier];
}

- (void) addItem: (NSString*) identifier
            item: (NSObject<UiToolItem>*) item
{
    [allowedItems addObject: identifier];
    [items setObject: item forKey:identifier];
}

/*
- (void) addStockItem:(char*)name
              stockID:(char*)sid
             callback:(ui_callback)f
                 data:(void*)userdata
{
    UiToolbarStockItem *item = [[UiToolbarStockItem alloc]initWithData:name
                                                               stockID:sid callback:f data:userdata];
    
    NSString *s = [[NSString alloc]initWithUTF8String:name];
    [allowedItems addObject: s];
    [items setObject: item forKey:s];
}
*/

// implementation of NSToolbarDelegate methods
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar {
    NSMutableArray *i = [[NSMutableArray alloc]
                         initWithCapacity:[allowedItems count] + 3];
    [i addObject: NSToolbarFlexibleSpaceItemIdentifier];
    [i addObject: NSToolbarSpaceItemIdentifier];
    [i addObject: NSToolbarSeparatorItemIdentifier];
    for(id item in allowedItems) {
        [i addObject: item];
    }
    
    return i;
}

- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
    return defaultItems;
}

- (NSToolbarItem *) toolbar:(NSToolbar*)toolbar
         itemForItemIdentifier:(NSString*)identifier
     willBeInsertedIntoToolbar:(BOOL)flag
{
    Protocol *item = @protocol(UiToolItem);
    item = [items objectForKey: identifier];
    
    // get UiObject from toolbar
    UiObject *obj = [(UiToolbar*)toolbar object];
    
    // create new NSToolbarItem
    return [item createItem:toolbar identifier:identifier object:obj];
}

@end


@implementation UiToolbar

- (UiToolbar*) initWithObject:(UiObject*)object {
    [self initWithIdentifier: @"MainToolbar"];
    obj = object;
    return self;
}

- (UiObject*) object {
    return obj;
}

@end


/* ---------------------          functions          --------------------- */

void ui_toolbar_init() {
    toolbar_delegate = [[UiToolbarDelegate alloc]init];
}

void ui_toolitem(char *name, char *label, ui_callback f, void *udata) {
    UiToolbarItem *item = [[UiToolbarItem alloc]
                                initWithIdentifier: name
                                label: label
                                callback: f
                                userdata: udata];
    
    NSString *identifier = [[NSString alloc]initWithUTF8String:name];
    [toolbar_delegate addItem: identifier item: item];
}

void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *udata) {
    ui_toolitem_stgr(name, stockid, f, udata, -1);
}

void ui_toolitem_stgr(char *name, char *stockid, ui_callback f, void *udata, ...) {
    va_list ap;
    va_start(ap, udata);
    ui_toolbar_stock_button(name, stockid, NO, f, udata, ap);
    va_end(ap);
}

void ui_toolitem_toggle_st(char *name, char *stockid, ui_callback f, void *udata) {
    ui_toolitem_toggle_stgr(name, stockid, f, udata, -1);
}

void ui_toolitem_toggle_stgr(char *name, char *stockid, ui_callback f, void *udata, ...) {
    va_list ap;
    va_start(ap, udata);
    ui_toolbar_stock_button(name, stockid, YES, f, udata, ap);
    va_end(ap);
}


void ui_toolbar_stock_button(char *name, char *stockid, BOOL toggle, ui_callback f, void *udata, va_list ap) {
    UiToolbarStockItem *item = [[UiToolbarStockItem alloc]
                                initWithIdentifier: name
                                stockID: stockid
                                callback: f
                                userdata: udata];
    [item setIsToggleButton: toggle];
    NSString *identifier = [[NSString alloc]initWithUTF8String:name];
    [toolbar_delegate addItem: identifier item: item];
    
    // add groups
    int group;
    while((group = va_arg(ap, int)) != -1) {
        [item addGroup: group];
    }
}


void ui_toolbar_add_default(char *name) {
    NSString *identifier = [[NSString alloc]initWithUTF8String:name];
    [toolbar_delegate addDefault: identifier];
}

NSToolbar* ui_create_toolbar(UiObject *obj) {
    UiToolbar *toolbar = [[UiToolbar alloc] initWithObject:obj];
    [toolbar setDelegate: toolbar_delegate];
    [toolbar setAllowsUserCustomization: true];
    return toolbar;
}

mercurial