#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
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 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);
}