added drawingarea (Cocoa)

Wed, 13 Jul 2016 16:40:59 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 13 Jul 2016 16:40:59 +0200
changeset 127
ce342364fad5
parent 126
f190d03dce0f
child 128
c284c15509a8

added drawingarea (Cocoa)

application/main.c file | annotate | diff | comparison | revisions
ui/cocoa/graphics.h file | annotate | diff | comparison | revisions
ui/cocoa/graphics.m file | annotate | diff | comparison | revisions
ui/cocoa/objs.mk file | annotate | diff | comparison | revisions
--- a/application/main.c	Wed Jul 13 15:35:59 2016 +0200
+++ b/application/main.c	Wed Jul 13 16:40:59 2016 +0200
@@ -63,11 +63,8 @@
     printf("scroll\n");
 }
 
-/*
+
 void draw(UiEvent *event, UiGraphics *g, void *data) {
-    double adjust = range.get(&range);
-    
-    ///*
     int width = g->width;
     int height = g->height;
     //printf("rec[%d,%d]\n", width, height);
@@ -75,29 +72,9 @@
     ui_graphics_color(g, 64, 64, 64);
     ui_draw_rect(g, 0, 0, width, height, TRUE);
     
-    if(!text) {
-        text = ui_text(g);
-        ui_text_setfont(text, "Monospace", 12);
-        ui_text_setstring(text, "Hello World");
-    }
-    int w, h;
-    ui_text_getsize(text, &w, &h);
-    //printf("ext[%d,%d]\n", w, h);
     
-    ui_graphics_color(g, 255, 255, 255);
-    ui_draw_text(g, 50, 50 + adjust, text);
-    ui_draw_line(g, 50, 55 + h, 50+w, 55 +h);
-    ui_draw_line(g, 50, 55 + h, 50, 75 +h);
-    
-    ui_draw_line(g, 0, 120, width, 120);
-    ui_draw_line(g, 200, 0, 200, height);
-    
-    ui_draw_rect(g, 250, 250, 50, 50, FALSE);
-    
-    //ui_text_free(text);
-    //*/
-    /*
 }
+/*
 
 void* model_getval(void *obj, int col) {
     if(col == 0) {
@@ -151,7 +128,7 @@
     
     UiObject *obj = ui_window("Test", NULL);
     //ui_context_closefunc(obj->ctx, window_close, NULL);
-
+    ui_drawingarea(obj, draw, NULL);
     
     ui_show(obj);
     ui_main();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/cocoa/graphics.h	Wed Jul 13 16:40:59 2016 +0200
@@ -0,0 +1,52 @@
+/*
+ * 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 "../ui/graphics.h"
+#import "toolkit.h"
+
+
+@interface UiCanvas : NSView {
+    UiObject            *object;
+    ui_drawfunc         callback;
+    void                *userdata;
+}
+
+- (UiObject*) object;
+- (void) setObject:(void*)obj;
+
+- (void*) userdata;
+- (void) setUserdata:(void*)d;
+
+- (ui_drawfunc) callback;
+- (void) setCallback: (ui_drawfunc)f;
+
+
+- (void)drawRect:(NSRect)rect;
+
+@end
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/cocoa/graphics.m	Wed Jul 13 16:40:59 2016 +0200
@@ -0,0 +1,124 @@
+/*
+ * 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);
+    }
+}
+
+
+
--- a/ui/cocoa/objs.mk	Wed Jul 13 15:35:59 2016 +0200
+++ b/ui/cocoa/objs.mk	Wed Jul 13 16:40:59 2016 +0200
@@ -38,6 +38,7 @@
 COCOAOBJ += text.o
 COCOAOBJ += resource.o
 COCOAOBJ += tree.o
+COCOAOBJ += graphics.o
 
 
 TOOLKITOBJS += $(COCOAOBJ:%=$(COCOA_OBJPRE)%)

mercurial