ui/cocoa/graphics.m

Wed, 09 Dec 2020 11:33:00 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 09 Dec 2020 11:33:00 +0100
changeset 1
fcacc15a2ef2
parent 0
804d8803eade
permissions
-rw-r--r--

add .hgignore

/*
 * 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 "graphics.h"
#import "container.h"
#import "../common/context.h"



@implementation UiCanvas

- (UiObject*) object {
    return object;
}

- (void) setObject:(void*)obj {
    object = obj;
}

- (void*) userdata {
    return userdata;
}

- (void) setUserdata:(void*)d {
    userdata = d;
}

- (ui_drawfunc) callback {
    return callback;
}
- (void) setCallback: (ui_drawfunc)f {
    callback = f;
}

- (void) drawRect:(NSRect)rect {
    UiGraphics g;
    NSRect bounds = [self bounds];
    g.width = bounds.size.width;
    g.height = bounds.size.height;
    
    UiEvent ev;
    ev.obj = object;
    ev.window = object->window;
    ev.document = object->ctx->document;
    
    callback(&ev, &g, userdata);
}

@end


UIWIDGET ui_drawingarea(UiObject *obj, ui_drawfunc f, void *userdata) {
    UiContainer *ct = uic_get_current_container(obj);
    
    NSRect frame = ct->getframe(ct);
    
    UiCanvas *canvas = [[UiCanvas alloc]initWithFrame:frame];
    [canvas setObject: obj];
    [canvas setCallback: f];
    [canvas setUserdata: userdata];
    ct->add(ct, canvas);
    
    return canvas;
}


// drawing functions
void ui_graphics_color(UiGraphics *gr, int red, int green, int blue) {
    float r = ((float)red) / 255.f;
    float g = ((float)green) / 255.f;
    float b = ((float)blue) / 255.f;
    
    NSColor *color = [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1];
    [color set];
}

void ui_draw_rect(UiGraphics *g, int x, int y, int w, int h, int fill) {
    // translate y
    y = g->height - y - h;
    
    NSRect bounds;
    bounds.origin.x = x;
    bounds.origin.y = y;
    bounds.size.width = w;
    bounds.size.height = h;
    
    if(fill) {
        NSRectFill(bounds);
    } else {
        NSFrameRect(bounds);
    }
}


mercurial