| 1 |
#if defined(Hiro_Canvas) |
| 2 |
|
| 3 |
namespace hiro { |
| 4 |
|
| 5 |
//GTK3 |
| 6 |
static auto Canvas_draw(GtkWidget* widget, cairo_t* context, pCanvas* p) -> s32 { |
| 7 |
p->_onDraw(context); |
| 8 |
return true; |
| 9 |
} |
| 10 |
|
| 11 |
//GTK2 |
| 12 |
static auto Canvas_expose(GtkWidget* widget, GdkEventExpose* event, pCanvas* p) -> s32 { |
| 13 |
p->_onExpose(event); |
| 14 |
return true; |
| 15 |
} |
| 16 |
|
| 17 |
static auto Canvas_keyPress(GtkWidget* widget, GdkEventKey* event, pViewport* p) -> s32 { |
| 18 |
//canvass that have been set focusable are intended for games. |
| 19 |
//to prevent arrow keys, tab, etc from losing focus on the game viewport, block key propagation here. |
| 20 |
if(p->self().focusable()) return true; |
| 21 |
|
| 22 |
//for all other cases, allow the key to propagate. |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
static auto Canvas_mousePress(GtkWidget* widget, GdkEventButton* event, pCanvas* p) -> s32 { |
| 27 |
//gtk_widget_set_focus_on_click() is a GTK 3.2+ feature. |
| 28 |
//implement this functionality manually for GTK 2.0+ compatibility. |
| 29 |
if(event->button == 1 && p->self().focusable()) gtk_widget_grab_focus(widget); |
| 30 |
|
| 31 |
return Widget_mousePress(widget, event, p); |
| 32 |
} |
| 33 |
|
| 34 |
auto pCanvas::construct() -> void { |
| 35 |
gtkWidget = gtk_drawing_area_new(); |
| 36 |
gtk_widget_add_events(gtkWidget, |
| 37 |
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_EXPOSURE_MASK | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_POINTER_MOTION_MASK); |
| 38 |
|
| 39 |
_rasterize(); |
| 40 |
_redraw(); |
| 41 |
|
| 42 |
//todo: need to work around GTK+ library bug: |
| 43 |
//after calling destruct(), construct() with state.droppable == true; |
| 44 |
//GTK+ will throw SIGBUS inside g_signal_connect_data() on one of the below connections |
| 45 |
|
| 46 |
g_signal_connect(G_OBJECT(gtkWidget), "button-press-event", G_CALLBACK(Canvas_mousePress), (gpointer)this); |
| 47 |
g_signal_connect(G_OBJECT(gtkWidget), "button-release-event", G_CALLBACK(Widget_mouseRelease), (gpointer)this); |
| 48 |
g_signal_connect(G_OBJECT(gtkWidget), "drag-data-received", G_CALLBACK(Widget_drop), (gpointer)this); |
| 49 |
#if HIRO_GTK==2 |
| 50 |
g_signal_connect(G_OBJECT(gtkWidget), "expose-event", G_CALLBACK(Canvas_expose), (gpointer)this); |
| 51 |
#elif HIRO_GTK==3 |
| 52 |
g_signal_connect(G_OBJECT(gtkWidget), "draw", G_CALLBACK(Canvas_draw), (gpointer)this); |
| 53 |
#endif |
| 54 |
g_signal_connect(G_OBJECT(gtkWidget), "key-press-event", G_CALLBACK(Canvas_keyPress), (gpointer)this); |
| 55 |
g_signal_connect(G_OBJECT(gtkWidget), "enter-notify-event", G_CALLBACK(Widget_mouseEnter), (gpointer)this); |
| 56 |
g_signal_connect(G_OBJECT(gtkWidget), "leave-notify-event", G_CALLBACK(Widget_mouseLeave), (gpointer)this); |
| 57 |
g_signal_connect(G_OBJECT(gtkWidget), "motion-notify-event", G_CALLBACK(Widget_mouseMove), (gpointer)this); |
| 58 |
|
| 59 |
pWidget::construct(); |
| 60 |
} |
| 61 |
|
| 62 |
auto pCanvas::destruct() -> void { |
| 63 |
_release(); |
| 64 |
if(gtkWidget) gtk_widget_destroy(gtkWidget), gtkWidget = nullptr; |
| 65 |
gtkParent = nullptr; |
| 66 |
} |
| 67 |
|
| 68 |
auto pCanvas::handle() const -> uintptr { |
| 69 |
#if defined(DISPLAY_WINDOWS) |
| 70 |
return (uintptr)GDK_WINDOW_HWND(gtk_widget_get_window(gtkWidget)); |
| 71 |
#endif |
| 72 |
|
| 73 |
#if defined(DISPLAY_XORG) |
| 74 |
return (uintptr)GDK_WINDOW_XID(gtk_widget_get_window(gtkWidget)); |
| 75 |
#endif |
| 76 |
|
| 77 |
return (uintptr)nullptr; |
| 78 |
} |
| 79 |
|
| 80 |
auto pCanvas::minimumSize() const -> Size { |
| 81 |
if(auto& icon = state().icon) return {(s32)icon.width(), (s32)icon.height()}; |
| 82 |
return {0, 0}; |
| 83 |
} |
| 84 |
|
| 85 |
auto pCanvas::setAlignment(Alignment) -> void { |
| 86 |
update(); |
| 87 |
} |
| 88 |
|
| 89 |
auto pCanvas::setColor(Color color) -> void { |
| 90 |
update(); |
| 91 |
} |
| 92 |
|
| 93 |
auto pCanvas::setFocusable(bool focusable) -> void { |
| 94 |
gtk_widget_set_can_focus(gtkWidget, focusable); |
| 95 |
} |
| 96 |
|
| 97 |
auto pCanvas::setGeometry(Geometry geometry) -> void { |
| 98 |
update(); |
| 99 |
pWidget::setGeometry(geometry); |
| 100 |
} |
| 101 |
|
| 102 |
auto pCanvas::setGradient(Gradient gradient) -> void { |
| 103 |
update(); |
| 104 |
} |
| 105 |
|
| 106 |
auto pCanvas::setIcon(const image& icon) -> void { |
| 107 |
update(); |
| 108 |
} |
| 109 |
|
| 110 |
auto pCanvas::update() -> void { |
| 111 |
_rasterize(); |
| 112 |
_redraw(); |
| 113 |
} |
| 114 |
|
| 115 |
auto pCanvas::_onDraw(cairo_t* context) -> void { |
| 116 |
if(!surface) return; |
| 117 |
|
| 118 |
s32 sx = 0, sy = 0, dx = 0, dy = 0; |
| 119 |
s32 width = surfaceWidth, height = surfaceHeight; |
| 120 |
auto geometry = pSizable::state().geometry; |
| 121 |
auto alignment = state().alignment ? state().alignment : Alignment{0.5, 0.5}; |
| 122 |
|
| 123 |
if(width <= geometry.width()) { |
| 124 |
sx = 0; |
| 125 |
dx = (geometry.width() - width) * alignment.horizontal(); |
| 126 |
} else { |
| 127 |
sx = (width - geometry.width()) * alignment.horizontal(); |
| 128 |
dx = 0; |
| 129 |
} |
| 130 |
|
| 131 |
if(height <= geometry.height()) { |
| 132 |
sy = 0; |
| 133 |
dy = (geometry.height() - height) * alignment.vertical(); |
| 134 |
} else { |
| 135 |
sy = (height - geometry.height()) * alignment.vertical(); |
| 136 |
dy = 0; |
| 137 |
} |
| 138 |
|
| 139 |
cairo_set_source_rgba(context, 0.0, 0.0, 0.0, 0.0); |
| 140 |
cairo_paint(context); |
| 141 |
gdk_cairo_set_source_pixbuf(context, surface, dx - sx, dy - sy); |
| 142 |
cairo_rectangle(context, dx, dy, width, height); |
| 143 |
cairo_paint(context); |
| 144 |
} |
| 145 |
|
| 146 |
auto pCanvas::_onExpose(GdkEventExpose* expose) -> void { |
| 147 |
if(!surface) return; |
| 148 |
|
| 149 |
cairo_t* context = gdk_cairo_create(gtk_widget_get_window(gtkWidget)); |
| 150 |
_onDraw(context); |
| 151 |
cairo_destroy(context); |
| 152 |
} |
| 153 |
|
| 154 |
auto pCanvas::_rasterize() -> void { |
| 155 |
s32 width = 0; |
| 156 |
s32 height = 0; |
| 157 |
|
| 158 |
if(auto& icon = state().icon) { |
| 159 |
width = icon.width(); |
| 160 |
height = icon.height(); |
| 161 |
} else { |
| 162 |
width = pSizable::state().geometry.width(); |
| 163 |
height = pSizable::state().geometry.height(); |
| 164 |
} |
| 165 |
if(width <= 0 || height <= 0) return; |
| 166 |
|
| 167 |
if(width != surfaceWidth || height != surfaceHeight) _release(); |
| 168 |
surfaceWidth = width; |
| 169 |
surfaceHeight = height; |
| 170 |
|
| 171 |
if(!surface) surface = gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, width, height); |
| 172 |
auto buffer = (u32*)gdk_pixbuf_get_pixels(surface); |
| 173 |
|
| 174 |
if(auto& icon = state().icon) { |
| 175 |
memory::copy<u32>(buffer, state().icon.data(), width * height); |
| 176 |
} else if(auto& gradient = state().gradient) { |
| 177 |
auto& colors = gradient.state.colors; |
| 178 |
image fill; |
| 179 |
fill.allocate(width, height); |
| 180 |
fill.gradient(colors[0].value(), colors[1].value(), colors[2].value(), colors[3].value()); |
| 181 |
memory::copy(buffer, fill.data(), fill.size()); |
| 182 |
} else { |
| 183 |
u32 color = state().color.value(); |
| 184 |
for(auto n : range(width * height)) buffer[n] = color; |
| 185 |
} |
| 186 |
|
| 187 |
//ARGB -> ABGR conversion |
| 188 |
for(auto n : range(width * height)) { |
| 189 |
u32 color = *buffer; |
| 190 |
color = (color & 0xff00ff00) | ((color & 0xff0000) >> 16) | ((color & 0x0000ff) << 16); |
| 191 |
*buffer++ = color; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
auto pCanvas::_redraw() -> void { |
| 196 |
if(gtk_widget_get_realized(gtkWidget)) { |
| 197 |
gdk_window_invalidate_rect(gtk_widget_get_window(gtkWidget), nullptr, true); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
auto pCanvas::_release() -> void { |
| 202 |
if(surface) { |
| 203 |
g_object_unref(surface); |
| 204 |
surface = nullptr; |
| 205 |
} |
| 206 |
surfaceWidth = 0; |
| 207 |
surfaceHeight = 0; |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
#endif |