#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
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 pango_cairo_create_context(gr->cr);
}
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);
}