ui/gtk/draw_cairo.c

Tue, 08 Oct 2024 22:19:24 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 08 Oct 2024 22:19:24 +0200
branch
newapi
changeset 330
d615aa36c98e
parent 300
2f924f7ca901
permissions
-rw-r--r--

add tabview value/varname parameter

/*
 * 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"


#if GTK_MAJOR_VERSION >= 3
static void ui_drawingarea_draw(
        GtkDrawingArea *area,
        cairo_t *cr,
        int width,
        int height,
        gpointer data)
{
    UiCairoGraphics g;
    g.g.width = width;
    g.g.height = height;
    g.widget = GTK_WIDGET(area);
    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);
}
#endif

#if UI_GTK3
gboolean ui_drawingarea_expose(GtkWidget *w, cairo_t *cr, void *data) {
    int width = gtk_widget_get_allocated_width(w);
    int height = gtk_widget_get_allocated_height(w);
    ui_drawingarea_draw(GTK_DRAWING_AREA(w), cr, width, height, data);
    return FALSE;
}
#endif
#ifdef UI_GTK2
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) {
#if GTK_MAJOR_VERSION >= 4
    gtk_drawing_area_set_draw_func(GTK_DRAWING_AREA(widget), ui_drawingarea_draw, event, NULL);
#elif GTK_MAJOR_VERSION == 3
    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