ui/motif/graphics.c

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
child 97
1a786201465f
permissions
-rw-r--r--

added drawing area (Motif)

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

mercurial