2013-03-15 13:11:33 +00:00
|
|
|
namespace phoenix {
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
static gboolean Canvas_expose(GtkWidget* widget, GdkEvent* event, pCanvas* self) {
|
|
|
|
cairo_t* context = gdk_cairo_create(gtk_widget_get_window(widget));
|
2011-08-06 14:03:52 +00:00
|
|
|
cairo_set_source_surface(context, self->surface, 0, 0);
|
|
|
|
cairo_paint(context);
|
|
|
|
cairo_destroy(context);
|
|
|
|
return true;
|
2011-04-27 08:57:31 +00:00
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
static gboolean Canvas_mouseLeave(GtkWidget* widget, GdkEventButton* event, pCanvas* self) {
|
2012-01-26 06:50:09 +00:00
|
|
|
if(self->canvas.onMouseLeave) self->canvas.onMouseLeave();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
static gboolean Canvas_mouseMove(GtkWidget* widget, GdkEventButton* event, pCanvas* self) {
|
|
|
|
if(self->canvas.onMouseMove) self->canvas.onMouseMove({(signed)event->x, (signed)event->y});
|
2012-01-26 06:50:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
static gboolean Canvas_mousePress(GtkWidget* widget, GdkEventButton* event, pCanvas* self) {
|
2012-01-26 06:50:09 +00:00
|
|
|
if(self->canvas.onMousePress) switch(event->button) {
|
|
|
|
case 1: self->canvas.onMousePress(Mouse::Button::Left); break;
|
|
|
|
case 2: self->canvas.onMousePress(Mouse::Button::Middle); break;
|
|
|
|
case 3: self->canvas.onMousePress(Mouse::Button::Right); break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
static gboolean Canvas_mouseRelease(GtkWidget* widget, GdkEventButton* event, pCanvas* self) {
|
2012-01-26 06:50:09 +00:00
|
|
|
if(self->canvas.onMouseRelease) switch(event->button) {
|
|
|
|
case 1: self->canvas.onMouseRelease(Mouse::Button::Left); break;
|
|
|
|
case 2: self->canvas.onMouseRelease(Mouse::Button::Middle); break;
|
|
|
|
case 3: self->canvas.onMouseRelease(Mouse::Button::Right); break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
void pCanvas::setSize(const Size& size) {
|
2011-11-04 11:57:54 +00:00
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, canvas.state.width, canvas.state.height);
|
2011-04-27 08:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pCanvas::update() {
|
2011-11-04 11:57:54 +00:00
|
|
|
memcpy(cairo_image_surface_get_data(surface), canvas.state.data, canvas.state.width * canvas.state.height * sizeof(uint32_t));
|
2011-04-27 08:57:31 +00:00
|
|
|
if(gtk_widget_get_realized(gtkWidget) == false) return;
|
2013-03-15 13:11:33 +00:00
|
|
|
gdk_window_invalidate_rect(gtk_widget_get_window(gtkWidget), nullptr, true);
|
2011-04-27 08:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pCanvas::constructor() {
|
2011-11-04 11:57:54 +00:00
|
|
|
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, canvas.state.width, canvas.state.height);
|
|
|
|
memcpy(cairo_image_surface_get_data(surface), canvas.state.data, canvas.state.width * canvas.state.height * sizeof(uint32_t));
|
2011-04-27 08:57:31 +00:00
|
|
|
gtkWidget = gtk_drawing_area_new();
|
|
|
|
gtk_widget_set_double_buffered(gtkWidget, false);
|
2012-01-26 06:50:09 +00:00
|
|
|
gtk_widget_add_events(gtkWidget,
|
|
|
|
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_EXPOSURE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_POINTER_MOTION_MASK);
|
|
|
|
g_signal_connect(G_OBJECT(gtkWidget), "button_press_event", G_CALLBACK(Canvas_mousePress), (gpointer)this);
|
|
|
|
g_signal_connect(G_OBJECT(gtkWidget), "button_release_event", G_CALLBACK(Canvas_mouseRelease), (gpointer)this);
|
2011-08-06 14:03:52 +00:00
|
|
|
g_signal_connect(G_OBJECT(gtkWidget), "expose_event", G_CALLBACK(Canvas_expose), (gpointer)this);
|
2012-01-26 06:50:09 +00:00
|
|
|
g_signal_connect(G_OBJECT(gtkWidget), "leave_notify_event", G_CALLBACK(Canvas_mouseLeave), (gpointer)this);
|
|
|
|
g_signal_connect(G_OBJECT(gtkWidget), "motion_notify_event", G_CALLBACK(Canvas_mouseMove), (gpointer)this);
|
2011-04-27 08:57:31 +00:00
|
|
|
}
|
2011-09-05 03:48:23 +00:00
|
|
|
|
|
|
|
void pCanvas::destructor() {
|
|
|
|
gtk_widget_destroy(gtkWidget);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pCanvas::orphan() {
|
|
|
|
destructor();
|
|
|
|
constructor();
|
|
|
|
}
|
2013-03-15 13:11:33 +00:00
|
|
|
|
|
|
|
}
|