#include <stdio.h>
#include <stdlib.h>
#include "graphics.h"
#include "container.h"
#include "../common/object.h"
UIWIDGET ui_drawingarea(UiObject *obj, ui_drawfunc f,
void *userdata) {
GtkWidget *widget = gtk_drawing_area_new();
if(f) {
UiDrawEvent *event = malloc(
sizeof(UiDrawEvent));
event->obj = obj;
event->callback = f;
event->userdata = userdata;
ui_connect_draw_handler(widget, event);
}
UiContainer *ct = uic_get_current_container(obj);
ct->add(ct, widget,
TRUE);
return widget;
}
static gboolean widget_button_pressed(
GtkWidget *widget,
GdkEvent *event,
gpointer userdata)
{
UiEventData *eventdata = userdata;
UiMouseEvent me;
me.x = (
int)event->button.x;
me.y = (
int)event->button.y;
int exec =
0;
if(event->button.type ==
GDK_BUTTON_PRESS) {
exec =
1;
me.type =
UI_PRESS;
}
else if(event->button.type ==
GDK_2BUTTON_PRESS) {
exec =
1;
me.type =
UI_PRESS2;
}
if(exec) {
UiEvent e;
e.obj = eventdata->obj;
e.window = eventdata->obj->window;
e.document = eventdata->obj->ctx->document;
e.eventdata = &me;
e.intval =
0;
eventdata->callback(&e, eventdata->userdata);
}
return TRUE;
}
void ui_drawingarea_getsize(
UIWIDGET drawingarea,
int *width,
int *height) {
#ifdef UI_GTK3
*width = gtk_widget_get_allocated_width(drawingarea);
*height = gtk_widget_get_allocated_height(drawingarea);
#else
*width = drawingarea->allocation.width;
*height = drawingarea->allocation.height;
#endif
}
void ui_drawingarea_redraw(
UIWIDGET drawingarea) {
gtk_widget_queue_draw(drawingarea);
}
void ui_drawingarea_mousehandler(UiObject *obj,
UIWIDGET widget, ui_callback f,
void *u) {
gtk_widget_set_events(widget,
GDK_BUTTON_PRESS_MASK);
if(f) {
UiEventData *event = malloc(
sizeof(UiEventData));
event->obj = obj;
event->callback = f;
event->userdata = u;
g_signal_connect(
G_OBJECT(widget),
"button-press-event",
G_CALLBACK(widget_button_pressed),
event);
}
else {
}
}
UiTextLayout* ui_text(UiGraphics *g) {
UiTextLayout *layout = malloc(
sizeof(UiTextLayout));
PangoContext *pc = ui_get_pango_context(g);
layout->layout = pango_layout_new(pc);
return layout;
}
void ui_text_setstring(UiTextLayout *layout,
char *str) {
pango_layout_set_text(layout->layout, str, -
1);
}
void ui_text_setstringl(UiTextLayout *layout,
char *str,
int len) {
pango_layout_set_text(layout->layout, str, len);
}
void ui_text_setfont(UiTextLayout *layout,
char *font,
int size) {
PangoFontDescription *fontDesc;
fontDesc = pango_font_description_from_string(font);
pango_font_description_set_size(fontDesc, size *
PANGO_SCALE);
pango_layout_set_font_description(layout->layout, fontDesc);
pango_font_description_free(fontDesc);
}
void ui_text_getsize(UiTextLayout *layout,
int *width,
int *height) {
pango_layout_get_size(layout->layout, width, height);
*width = *width /
PANGO_SCALE;
*height = *height /
PANGO_SCALE;
}
void ui_text_setwidth(UiTextLayout *layout,
int width) {
pango_layout_set_width(layout->layout, width *
PANGO_SCALE);
pango_layout_set_ellipsize(layout->layout,
PANGO_ELLIPSIZE_END);
}
void ui_text_free(UiTextLayout *text) {
g_object_unref(text->layout);
free(text);
}