added drawing area (Gtk)

Sun, 29 Nov 2015 17:22:15 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 29 Nov 2015 17:22:15 +0100
changeset 95
29f5cd5f5367
parent 94
d51e334c1439
child 96
93785a7bda56

added drawing area (Gtk)

application/main.c file | annotate | diff | comparison | revisions
make/configure_gtk2.sh file | annotate | diff | comparison | revisions
make/configure_gtk2legacy.sh file | annotate | diff | comparison | revisions
make/configure_gtk3.sh file | annotate | diff | comparison | revisions
ui/gtk/draw_cairo.c file | annotate | diff | comparison | revisions
ui/gtk/draw_cairo.h file | annotate | diff | comparison | revisions
ui/gtk/draw_gdk.c file | annotate | diff | comparison | revisions
ui/gtk/draw_gdk.h file | annotate | diff | comparison | revisions
ui/gtk/graphics.c file | annotate | diff | comparison | revisions
ui/gtk/graphics.h file | annotate | diff | comparison | revisions
ui/gtk/objs.mk file | annotate | diff | comparison | revisions
ui/gtk/toolkit.c file | annotate | diff | comparison | revisions
ui/ui/graphics.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
ui/ui/ui.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sat Oct 10 15:29:31 2015 +0200
+++ b/application/main.c	Sun Nov 29 17:22:15 2015 +0100
@@ -44,6 +44,22 @@
     fflush(stdout);
 }
 
+void draw(UiEvent *event, UiGraphics *g, void *data) {
+    int width = g->width;
+    int height = g->height;
+    ui_graphics_color(g, 64, 64, 64);
+    ui_draw_rect(g, 0, 0, width, height, TRUE);
+    
+    UiTextLayout *text = ui_text(g);
+    ui_text_setfont(text, "Monospace", 12);
+    ui_text_setstring(text, "Hello World");
+    
+    ui_graphics_color(g, 255, 255, 255);
+    ui_draw_text(g, 50, 50, text);
+    
+    ui_text_free(text);
+}
+
 int main(int argc, char** argv) { 
     ui_init("app1", argc, argv);
     
@@ -63,48 +79,7 @@
     ui_toolbar_add_default("button2");
     
     UiObject *obj = ui_window("Test", NULL);
-    
-    ui_layout_fill(obj, FALSE);
-    ui_grid_sp(obj, 10, 2);
-    
-    ui_rlabel(obj, "Name");
-    ui_textfield(obj, NULL);
-    ui_newline(obj);
-    
-    ui_rlabel(obj, "Email");
-    ui_textfield(obj, NULL);
-    ui_button(obj, "OK", NULL, NULL);
-    ui_newline(obj);
-    
-    ui_checkbox(obj, "fuck", NULL);
-    ui_rlabel(obj, "FUCK");
-    ui_newline(obj);
-    ui_checkbox(obj, "this", NULL);
-    ui_newline(obj);
-    ui_checkbox(obj, "shit", NULL);
-    ui_newline(obj);
-    
-    ui_label(obj, "Awesome Button");
-    UIWIDGET button = ui_button(obj, "...", NULL, NULL);
-    
-    ui_end(obj);
-    
-    ui_checkbox(obj, "A", NULL);
-    ui_checkbox(obj, "B", NULL);
-    ui_checkbox(obj, "C", NULL);
-    ui_checkbox(obj, "D", NULL);
-    ui_checkbox(obj, "E", NULL);
-    ui_checkbox(obj, "F", NULL);
-    ui_space(obj);
-    
-    ui_separator(obj);
-    
-    ui_layout_fill(obj, FALSE);
-    ui_hbox(obj);
-    ui_button(obj, "Submit", NULL, NULL);
-    //ui_space(obj);
-    ui_button(obj, "Cancel", NULL, NULL);
-    ui_end(obj);
+    ui_drawingarea(obj, draw, NULL);
     
     ui_show(obj);
     ui_main();
--- a/make/configure_gtk2.sh	Sat Oct 10 15:29:31 2015 +0200
+++ b/make/configure_gtk2.sh	Sun Nov 29 17:22:15 2015 +0100
@@ -39,8 +39,7 @@
 
 TOOLKIT = gtk
 
-#GTKOBJ = draw_cairo.o
-GTKOBJ =
+GTKOBJ = draw_cairo.o
 
 __EOF__
 
--- a/make/configure_gtk2legacy.sh	Sat Oct 10 15:29:31 2015 +0200
+++ b/make/configure_gtk2legacy.sh	Sun Nov 29 17:22:15 2015 +0100
@@ -40,8 +40,7 @@
 
 TOOLKIT = gtk
 
-#GTKOBJ = draw_gdk.o
-GTKOBJ =
+GTKOBJ = draw_gdk.o
 
 __EOF__
 
--- a/make/configure_gtk3.sh	Sat Oct 10 15:29:31 2015 +0200
+++ b/make/configure_gtk3.sh	Sun Nov 29 17:22:15 2015 +0100
@@ -39,8 +39,7 @@
 
 TOOLKIT = gtk
 
-#GTKOBJ = draw_cairo.o
-GTKOBJ =
+GTKOBJ = draw_cairo.o
 
 __EOF__
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/gtk/draw_cairo.c	Sun Nov 29 17:22:15 2015 +0100
@@ -0,0 +1,120 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2013 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.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "container.h"
+
+#include "draw_cairo.h"
+
+#ifdef UI_GTK3
+gboolean ui_drawingarea_expose(GtkWidget *w, cairo_t *cr, void *data) {
+    UiCairoGraphics g;
+    g.g.width = gtk_widget_get_allocated_width(w);
+    g.g.height = gtk_widget_get_allocated_height(w);
+    g.widget = w;
+    g.cr = cr;
+    
+    UiDrawEvent *event = data;
+    UiEvent ev;
+    ev.obj = event->obj;
+    ev.window = event->obj->window;
+    ev.document = event->obj->ctx->document;
+    
+    event->callback(&ev, &g.g, event->userdata);
+    
+    return FALSE;
+}
+#else
+gboolean ui_canvas_expose(GtkWidget *w, GdkEventExpose *e, void *data) {
+    UiCairoGraphics g;
+    g.g.width = w->allocation.width;
+    g.g.height = w->allocation.height;
+    g.widget = w;
+    g.cr = gdk_cairo_create(w->window);
+    
+    UiExposeEvent *event = data;
+    event->callback(&g.g, event->document, event->data);
+    
+    return FALSE;
+}
+#endif
+
+// function from graphics.h
+
+void ui_connect_draw_handler(GtkWidget *widget, UiDrawEvent *event) {
+#ifdef UI_GTK3
+    g_signal_connect(G_OBJECT(widget),
+            "draw",
+            G_CALLBACK(ui_drawingarea_expose),
+            event);
+#else
+    g_signal_connect(G_OBJECT(widget),
+            "expose_event",
+            G_CALLBACK(ui_drawingarea_expose),
+            event);
+#endif
+}
+
+
+PangoContext *ui_get_pango_context(UiGraphics *g) {
+    UiCairoGraphics *gr = (UiCairoGraphics*)g;
+    //return gtk_widget_get_pango_context(gr->widget);
+    return pango_cairo_create_context(gr->cr);
+}
+
+
+// drawing functions
+void ui_graphics_color(UiGraphics *g, int red, int green, int blue) {
+    UiCairoGraphics *gr = (UiCairoGraphics*)g;
+    double dred = (double)red / (double)255;
+    double dgreen = (double)green / (double)255;
+    double dblue = (double)blue / (double)255;
+    cairo_set_source_rgb(gr->cr, dred, dgreen, dblue);
+}
+
+
+void ui_draw_rect(UiGraphics *g, int x, int y, int w, int h, int fill) {
+    UiCairoGraphics *gr = (UiCairoGraphics*)g;
+    cairo_set_line_width(gr->cr, 1);
+    cairo_rectangle(gr->cr, x, y, w, h);
+    if(fill) {
+        cairo_fill(gr->cr);
+    } else {
+        cairo_stroke(gr->cr);
+    }
+}
+
+void ui_draw_text(UiGraphics *g, int x, int y, UiTextLayout *text) {
+    UiCairoGraphics *gr = (UiCairoGraphics*)g; 
+    //gdk_draw_layout(gr->widget->window, gr->gc, x, y, text->layout);
+    cairo_move_to(gr->cr, x, y);
+    pango_cairo_show_layout(gr->cr, text->layout);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/gtk/draw_cairo.h	Sun Nov 29 17:22:15 2015 +0100
@@ -0,0 +1,51 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2013 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.
+ */
+
+#ifndef DRAW_CAIRO_H
+#define	DRAW_CAIRO_H
+
+#include "graphics.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+typedef struct UiCairoGraphics {
+    UiGraphics g;
+    GtkWidget  *widget;
+    cairo_t    *cr;
+} UiCairoGraphics;
+
+// ui_canvas_expose
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* DRAW_CAIRO_H */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/gtk/draw_gdk.c	Sun Nov 29 17:22:15 2015 +0100
@@ -0,0 +1,89 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2011 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.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "container.h"
+
+#include "draw_gdk.h"
+
+
+gboolean ui_drawingarea_expose(GtkWidget *w, GdkEventExpose *e, void *data) {
+    UiGdkGraphics g;
+    g.g.width = w->allocation.width;
+    g.g.height = w->allocation.height;
+    g.widget = w;
+    g.gc = gdk_gc_new(w->window);
+    
+    UiDrawEvent *event = data;
+    UiEvent ev;
+    ev.obj = event->obj;
+    ev.window = event->obj->window;
+    ev.document = event->obj->ctx->document;
+    
+    event->callback(&ev, &g.g, event->userdata);
+    
+    return FALSE;
+}
+
+// function from graphics.h
+
+void ui_connect_draw_handler(GtkWidget *widget, UiDrawEvent *event) {
+    g_signal_connect(G_OBJECT(widget),
+            "expose_event",
+            G_CALLBACK(ui_drawingarea_expose),
+            event);
+}
+
+PangoContext *ui_get_pango_context(UiGraphics *g) {
+    UiGdkGraphics *gr = (UiGdkGraphics*)g;
+    return gtk_widget_get_pango_context(gr->widget);
+}
+
+// drawing functions
+void ui_graphics_color(UiGraphics *g, int red, int green, int blue) {
+    UiGdkGraphics *gr = (UiGdkGraphics*)g;
+    GdkColor color;
+    color.red = red * 257;
+    color.green = green * 257;
+    color.blue = blue * 257;
+    gdk_gc_set_rgb_fg_color(gr->gc, &color);
+    //gdk_gc_set_rgb_bg_color(g->gc, &color);
+}
+
+
+void ui_draw_rect(UiGraphics *g, int x, int y, int w, int h, int fill) {
+    UiGdkGraphics *gr = (UiGdkGraphics*)g; 
+    gdk_draw_rectangle(gr->widget->window, gr->gc, fill, x, y, w, h);
+}
+
+void ui_draw_text(UiGraphics *g, int x, int y, UiTextLayout *text) {
+    UiGdkGraphics *gr = (UiGdkGraphics*)g; 
+    gdk_draw_layout(gr->widget->window, gr->gc, x, y, text->layout);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/gtk/draw_gdk.h	Sun Nov 29 17:22:15 2015 +0100
@@ -0,0 +1,51 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2012 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.
+ */
+
+#ifndef DRAW_GDK_H
+#define	DRAW_GDK_H
+
+#include "graphics.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+typedef struct UiGdkGraphics {
+    UiGraphics g;
+    GtkWidget  *widget;
+    GdkGC      *gc;
+} UiGdkGraphics;
+
+gboolean ui_canvas_expose(GtkWidget *w, GdkEventExpose *e, void *data);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* DRAW_GDK_H */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/gtk/graphics.c	Sun Nov 29 17:22:15 2015 +0100
@@ -0,0 +1,92 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2012 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.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "graphics.h"
+#include "container.h"
+#include "../common/object.h"
+
+UIWIDGET ui_drawingarea(UiObject *obj, ui_drawfunc f, void *userdata) {
+    GtkWidget *widget = gtk_drawing_area_new();
+    
+    if(f) {
+        UiDrawEvent *event = malloc(sizeof(UiDrawEvent));
+        event->obj = obj;
+        event->callback = f;
+        event->userdata = userdata;
+        ui_connect_draw_handler(widget, event);
+    }
+    
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->add(ct, widget, TRUE);
+    
+    return widget;
+}
+
+// text layout
+UiTextLayout* ui_text(UiGraphics *g) {
+    UiTextLayout *layout = malloc(sizeof(UiTextLayout));
+    PangoContext *pc = ui_get_pango_context(g);
+    layout->layout = pango_layout_new(pc);
+    return layout;
+}
+
+void ui_text_setstring(UiTextLayout *layout, char *str) {
+    pango_layout_set_text(layout->layout, str, -1);
+}
+
+void ui_text_setstringl(UiTextLayout *layout, char *str, int len) {
+    pango_layout_set_text(layout->layout, str, len);
+}
+
+void ui_text_setfont(UiTextLayout *layout, char *font, int size) {
+    PangoFontDescription *fontDesc;
+    fontDesc = pango_font_description_from_string(font);
+    pango_font_description_set_size(fontDesc, size * PANGO_SCALE);
+    pango_layout_set_font_description(layout->layout, fontDesc);
+    pango_font_description_free(fontDesc);
+}
+
+void ui_text_getsize(UiTextLayout *layout, int *width, int *height) {
+    pango_layout_get_size(layout->layout, width, height);
+    *width = *width / PANGO_SCALE;
+    *height = *height / PANGO_SCALE;
+}
+
+void ui_text_setwidth(UiTextLayout *layout, int width) {
+    pango_layout_set_width(layout->layout, width * PANGO_SCALE);
+    pango_layout_set_ellipsize(layout->layout, PANGO_ELLIPSIZE_END);
+    //pango_layout_set_wrap(layout->layout, PANGO_WRAP_WORD_CHAR);
+}
+
+void ui_text_free(UiTextLayout *text) {
+    g_object_unref(text->layout);
+    free(text);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/gtk/graphics.h	Sun Nov 29 17:22:15 2015 +0100
@@ -0,0 +1,58 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2012 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.
+ */
+
+#ifndef DRAWINGAREA_H
+#define	DRAWINGAREA_H
+
+#include "../ui/graphics.h"
+#include "toolkit.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+typedef struct UiDrawEvent {
+    ui_drawfunc callback;
+    UiObject    *obj;
+    void        *userdata;
+} UiDrawEvent;
+
+struct UiTextLayout {
+    PangoLayout *layout;
+};
+
+// implemented in draw_*.h
+void ui_connect_draw_handler(GtkWidget *widget, UiDrawEvent *event);
+PangoContext *ui_get_pango_context(UiGraphics *g);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* DRAWINGAREA_H */
+
--- a/ui/gtk/objs.mk	Sat Oct 10 15:29:31 2015 +0200
+++ b/ui/gtk/objs.mk	Sun Nov 29 17:22:15 2015 +0100
@@ -41,6 +41,7 @@
 GTKOBJ += model.o
 GTKOBJ += tree.o
 GTKOBJ += image.o
+GTKOBJ += graphics.o
 
 TOOLKITOBJS += $(GTKOBJ:%=$(GTK_OBJPRE)%)
 TOOLKITSOURCE += $(GTKOBJ:%.o=gtk/%.c)
--- a/ui/gtk/toolkit.c	Sat Oct 10 15:29:31 2015 +0200
+++ b/ui/gtk/toolkit.c	Sun Nov 29 17:22:15 2015 +0100
@@ -29,6 +29,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdbool.h>
 
 #include "toolkit.h"
 #include "toolbar.h"
@@ -171,3 +172,56 @@
 UiObject *ui_get_active_window() {
     return active_window;
 }
+
+
+/* -------------------- common widget functions -------------------- */
+
+static gboolean widget_button_pressed(
+        GtkWidget *widget,
+        GdkEvent *event,
+        gpointer userdata)
+{
+    UiEventData *eventdata = userdata;
+    
+    UiMouseEvent me;
+    me.x = (int)event->button.x;
+    me.y = (int)event->button.y;
+    
+    int exec = 0;
+    if(event->button.type == GDK_BUTTON_PRESS) {
+        exec = 1;
+        me.type = UI_PRESS;
+    } else if(event->button.type == GDK_2BUTTON_PRESS) {
+        exec = 1;
+        me.type = UI_PRESS2;
+    }
+    
+    if(exec) {
+        UiEvent e;
+        e.obj = eventdata->obj;
+        e.window = eventdata->obj->window;
+        e.document = eventdata->obj->ctx->document;
+        e.eventdata = &me;
+        e.intval = 0;
+        eventdata->callback(&e, eventdata->userdata);
+    }
+    return true;
+}
+
+void ui_mouse_handler(UiObject *obj, UIWIDGET widget, ui_callback f, void *u) {
+    gtk_widget_set_events(widget, GDK_BUTTON_PRESS_MASK);
+    if(f) {
+        UiEventData *event = malloc(sizeof(UiEventData));
+        event->obj = obj;
+        event->callback = f;
+        event->userdata = u;
+        
+        g_signal_connect(G_OBJECT(widget),
+                "button-press-event",
+                G_CALLBACK(widget_button_pressed),
+                event);
+    } else {
+         // TODO: warning
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/ui/graphics.h	Sun Nov 29 17:22:15 2015 +0100
@@ -0,0 +1,71 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2015 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.
+ */
+
+#ifndef GRAPHICS_H
+#define	GRAPHICS_H
+
+#include "toolkit.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+typedef struct UiGraphics      UiGraphics;
+typedef struct UiTextLayout    UiTextLayout;
+
+typedef void(*ui_drawfunc)(UiEvent*, UiGraphics*, void*);
+
+struct UiGraphics {
+    int width;
+    int height;
+};
+
+UIWIDGET ui_drawingarea(UiObject *obj, ui_drawfunc f, void *userdata);
+void ui_drawingarea_getsize(UIWIDGET drawingarea, int *width, int *height);
+void ui_drawingarea_redraw(UIWIDGET drawingarea);
+
+// text layout
+UiTextLayout* ui_text(UiGraphics *g);
+void ui_text_free(UiTextLayout *text);
+void ui_text_setstring(UiTextLayout *layout, char *str);
+void ui_text_setstringl(UiTextLayout *layout, char *str, int len);
+void ui_text_setfont(UiTextLayout *layout, char *font, int size);
+void ui_text_getsize(UiTextLayout *layout, int *width, int *height);
+void ui_text_setwidth(UiTextLayout *layout, int width);
+
+// drawing functions
+void ui_graphics_color(UiGraphics *g, int red, int green, int blue);
+void ui_draw_rect(UiGraphics *g, int x, int y, int w, int h, int fill);
+void ui_draw_text(UiGraphics *g, int x, int y, UiTextLayout *text);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* GRAPHICS_H */
+
--- a/ui/ui/toolkit.h	Sat Oct 10 15:29:31 2015 +0200
+++ b/ui/ui/toolkit.h	Sun Nov 29 17:22:15 2015 +0100
@@ -80,6 +80,7 @@
 
 typedef struct UiObject     UiObject;
 typedef struct UiEvent      UiEvent;
+typedef struct UiMouseEvent UiMouseEvent;
 typedef struct UiObserver   UiObserver;
 
 typedef struct UiInteger    UiInteger;
@@ -93,6 +94,8 @@
 
 typedef struct UiTabbedPane UiTabbedPane;
 
+enum UiMouseEventType { UI_PRESS = 0, UI_PRESS2 };
+
 #define ui_getval(val) (val).get(&(val))
 #define ui_setval(val, v) (val).set(&(val), v)
 
@@ -161,6 +164,13 @@
     int      intval;
 };
 
+struct UiMouseEvent {
+    int x;
+    int y;
+    enum UiMouseEventType type;
+    int button;
+};
+
 struct UiObserver {
     ui_callback callback;
     void *data;
@@ -325,8 +335,10 @@
 
 void ui_add_image(char *imgname, char *filename);
 
+// common widget functions
+void ui_mouse_handler(UiObject *obj, UIWIDGET widget, ui_callback f, void *u);
 
-
+// label widgets
 UIWIDGET ui_label(UiObject *obj, char *label);
 UIWIDGET ui_llabel(UiObject *obj, char *label);
 UIWIDGET ui_rlabel(UiObject *obj, char *label);
--- a/ui/ui/ui.h	Sat Oct 10 15:29:31 2015 +0200
+++ b/ui/ui/ui.h	Sun Nov 29 17:22:15 2015 +0100
@@ -38,6 +38,7 @@
 #include "text.h"
 #include "properties.h"
 #include "tree.h"
+#include "graphics.h"
 
 #endif	/* UI_H */
 

mercurial