ui/gtk/draw_cairo.c

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 2017 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);
    
    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;
}
#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_canvas_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_line(UiGraphics *g, int x1, int y1, int x2, int y2) {
    UiCairoGraphics *gr = (UiCairoGraphics*)g;
    cairo_set_line_width(gr->cr, 1);
    cairo_move_to(gr->cr, (double)x1 + 0.5, (double)y1 + 0.5);
    cairo_line_to(gr->cr, (double)x2 + 0.5, (double)y2 + 0.5);
    cairo_stroke(gr->cr);
}

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 + 0.5, y + 0.5 , 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; 
    cairo_move_to(gr->cr, x, y);
    pango_cairo_show_layout(gr->cr, text->layout);
}

mercurial