added drawing area (Motif)

Sun, 29 Nov 2015 20:03:06 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 29 Nov 2015 20:03:06 +0100
changeset 96
93785a7bda56
parent 95
29f5cd5f5367
child 97
1a786201465f

added drawing area (Motif)

ui/motif/graphics.c file | annotate | diff | comparison | revisions
ui/motif/graphics.h file | annotate | diff | comparison | revisions
ui/motif/objs.mk file | annotate | diff | comparison | revisions
ui/ui/graphics.h file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/motif/graphics.c	Sun Nov 29 20:03:06 2015 +0100
@@ -0,0 +1,192 @@
+/*
+ * 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.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "graphics.h"
+
+#include "container.h"
+
+static void ui_drawingarea_expose(Widget widget, XtPointer u, XtPointer c) {
+    UiDrawEvent *drawevent = u;
+    XmDrawingAreaCallbackStruct *cbs = (XmDrawingAreaCallbackStruct *)c;
+    XEvent *event = cbs->event;
+    Display *dpy = XtDisplay(widget);
+    
+    UiEvent ev;
+    ev.obj = drawevent->obj;
+    ev.window = drawevent->obj->window;
+    ev.document = drawevent->obj->ctx->document;
+    
+    XtVaGetValues(
+            widget,
+            XmNwidth,
+            &drawevent->gr.g.width,
+            XmNheight,
+            &drawevent->gr.g.height,
+            NULL);
+    
+    XGCValues gcvals;
+    gcvals.foreground = BlackPixelOfScreen(XtScreen(widget));
+    drawevent->gr.gc = XCreateGC(dpy, XtWindow(widget), (GCForeground), &gcvals);
+    
+    drawevent->callback(&ev, &drawevent->gr.g, drawevent->userdata);
+}
+
+UIWIDGET ui_drawingarea(UiObject *obj, ui_drawfunc f, void *userdata) {
+    UiContainer *ct = uic_get_current_container(obj);
+    
+    int n = 0;
+    Arg args[16];
+    
+    Widget parent = ct->prepare(ct, args, &n, TRUE);
+    Widget drawingarea = XmCreateDrawingArea(parent, "drawingarea", args, n);
+    
+    if(f) {
+        UiDrawEvent *event = malloc(sizeof(UiDrawEvent));
+        event->obj = obj;
+        event->callback = f;
+        event->userdata = userdata;
+        
+        event->gr.display = XtDisplay(drawingarea);
+        event->gr.widget = drawingarea;
+        
+        Colormap colormap;
+        XtVaGetValues(drawingarea, XmNcolormap, &colormap, NULL);    
+        event->gr.colormap = colormap;
+        
+        XtAddCallback(
+                drawingarea,
+                XmNexposeCallback,
+                ui_drawingarea_expose,
+                event);
+    }
+    
+    XtManageChild(drawingarea);
+    return drawingarea;
+}
+
+
+/* -------------------- text layout functions -------------------- */
+UiTextLayout* ui_text(UiGraphics *g) {
+    UiTextLayout *text = malloc(sizeof(UiTextLayout));
+    text->text = NULL;
+    text->length = 0;
+    text->widget = ((UiXlibGraphics*)g)->widget;
+    text->fontset = NULL;
+    return text;
+}
+
+static void create_default_fontset(UiTextLayout *layout) {
+    char **missing = NULL;
+    int num_missing = 0;
+    char *def = NULL;
+    Display *dpy = XtDisplay(layout->widget);
+    XFontSet fs = XCreateFontSet(
+        dpy,
+        "-dt-interface system-medium-r-normal-s*utf*:,"
+                "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-1,"
+                "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-10,"
+                "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-15,"
+                "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-2,"
+                "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-3,"
+                "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-4,"
+                "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-5,"
+                "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-9,"
+                "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-e,"
+                "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-r,"
+                "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-ru,"
+                "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-u,"
+                "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-uni,"
+                "-misc-fixed-medium-r-normal--14-130-75-75-c-140-jisx0208",
+        &missing, &num_missing, &def);
+    layout->fontset = fs;
+}
+
+void ui_text_free(UiTextLayout *text) {
+    // TODO
+}
+
+void ui_text_setstring(UiTextLayout *layout, char *str) {
+    layout->text = str;
+    layout->length = strlen(str);
+}
+
+void ui_text_setstringl(UiTextLayout *layout, char *str, int len) {
+    layout->text = str;
+    layout->length = len;
+}
+
+void ui_text_setfont(UiTextLayout *layout, char *font, int size) {
+    create_default_fontset(layout);//TODO
+}
+
+void ui_text_getsize(UiTextLayout *layout, int *width, int *height) {
+    // TODO
+}
+
+void ui_text_setwidth(UiTextLayout *layout, int width) {
+    // TODO
+}
+
+
+/* -------------------- drawing functions -------------------- */
+
+void ui_graphics_color(UiGraphics *g, int red, int green, int blue) {
+    UiXlibGraphics *gr = (UiXlibGraphics*)g;
+    XColor color;
+    color.flags= DoRed | DoGreen | DoBlue; 
+    color.red = red * 257;
+    color.green = green * 257;
+    color.blue = blue * 257;
+    XAllocColor(gr->display, gr->colormap, &color);
+    XSetForeground(gr->display, gr->gc, color.pixel);
+}
+
+void ui_draw_rect(UiGraphics *g, int x, int y, int w, int h, int fill) {
+    UiXlibGraphics *gr = (UiXlibGraphics*)g;
+    if(fill) {
+        XFillRectangle(gr->display, XtWindow(gr->widget), gr->gc, x, y, w, h);
+    } else {
+        XDrawRectangle(gr->display, XtWindow(gr->widget), gr->gc, x, y, w, h);
+    }
+}
+
+void ui_draw_text(UiGraphics *g, int x, int y, UiTextLayout *text) {
+    UiXlibGraphics *gr = (UiXlibGraphics*)g;
+    XmbDrawString(
+            gr->display,
+            XtWindow(gr->widget),
+            text->fontset,
+            gr->gc,
+            x,
+            y,
+            text->text,
+            text->length);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/motif/graphics.h	Sun Nov 29 20:03:06 2015 +0100
@@ -0,0 +1,67 @@
+/*
+ * 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 GRAPHICS_H
+#define	GRAPHICS_H
+
+#include "../ui/graphics.h"
+#include "toolkit.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+typedef struct UiXlibGraphics {
+    UiGraphics g;
+    Display    *display;
+    Widget     widget;
+    Colormap   colormap;
+    GC         gc;
+} UiXlibGraphics;
+
+typedef struct UiDrawEvent {
+    ui_drawfunc    callback;
+    UiObject       *obj;
+    void           *userdata;
+    UiXlibGraphics gr;
+} UiDrawEvent;
+
+struct UiTextLayout {
+    char     *text;
+    size_t   length;
+    Widget   widget;
+    XFontSet fontset;
+};
+
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* GRAPHICS_H */
+
--- a/ui/motif/objs.mk	Sun Nov 29 17:22:15 2015 +0100
+++ b/ui/motif/objs.mk	Sun Nov 29 20:03:06 2015 +0100
@@ -40,6 +40,7 @@
 MOTIFOBJ += text.o
 MOTIFOBJ += list.o
 MOTIFOBJ += tree.o
+MOTIFOBJ += graphics.o
 
 TOOLKITOBJS += $(MOTIFOBJ:%=$(MOTIF_OBJPRE)%)
 TOOLKITSOURCE += $(MOTIFOBJ:%.o=motif/%.c)
--- a/ui/ui/graphics.h	Sun Nov 29 17:22:15 2015 +0100
+++ b/ui/ui/graphics.h	Sun Nov 29 20:03:06 2015 +0100
@@ -26,8 +26,8 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef GRAPHICS_H
-#define	GRAPHICS_H
+#ifndef UI_GRAPHICS_H
+#define	UI_GRAPHICS_H
 
 #include "toolkit.h"
 
@@ -67,5 +67,5 @@
 }
 #endif
 
-#endif	/* GRAPHICS_H */
+#endif	/* UI_GRAPHICS_H */
 

mercurial