ui/cocoa/toolbar.m

Fri, 16 May 2014 19:20:17 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 16 May 2014 19:20:17 +0200
changeset 44
a1571777eff2
parent 25
78ae3efe463f
child 49
a80ba8741be6
permissions
-rw-r--r--

optimized selection callback (Motif)

/*
 * 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;
    return self;
}

- (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];
    [item setView:button];
    
    // event
    EventWrapper *event = [[EventWrapper alloc]
                           initWithData:userdata callback:callback];
    [button setAction:@selector(handleEvent:)];
    [button setTarget:event];
    
    if(groups) {
        uic_add_group_widget(obj->ctx, item, groups);
    }
    
    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_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, ...) {
    UiToolbarStockItem *item = [[UiToolbarStockItem alloc]
                                initWithIdentifier: name
                                stockID: stockid
                                callback: f
                                userdata: udata];
    NSString *identifier = [[NSString alloc]initWithUTF8String:name];
    [toolbar_delegate addItem: identifier item: item];
    
    // add groups
    va_list ap;
    va_start(ap, udata);
    int group;
    while((group = va_arg(ap, int)) != -1) {
        [item addGroup: group];
    }
    va_end(ap);
}

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