| 1 |
#if defined(Hiro_Widget) |
| 2 |
|
| 3 |
namespace hiro { |
| 4 |
|
| 5 |
static auto Widget_drop(GtkWidget* widget, GdkDragContext* context, s32 x, s32 y, |
| 6 |
GtkSelectionData* data, u32 type, u32 timestamp, pWidget* p) -> void { |
| 7 |
if(!p->state().droppable) return; |
| 8 |
auto paths = DropPaths(data); |
| 9 |
if(!paths.empty()) p->self().doDrop(paths); |
| 10 |
} |
| 11 |
|
| 12 |
static auto Widget_mouseEnter(GtkWidget* widget, GdkEventButton* event, pWidget* p) -> s32 { |
| 13 |
if(auto cursor = p->gdkMouseCursor) { |
| 14 |
if(auto window = gtk_widget_get_parent_window(widget)) { |
| 15 |
gdk_window_set_cursor(window, cursor); |
| 16 |
} |
| 17 |
} |
| 18 |
p->self().doMouseEnter(); |
| 19 |
return true; |
| 20 |
} |
| 21 |
|
| 22 |
static auto Widget_mouseLeave(GtkWidget* widget, GdkEventButton* event, pWidget* p) -> s32 { |
| 23 |
if(auto cursor = p->gdkMouseCursor) { |
| 24 |
if(auto window = gtk_widget_get_parent_window(widget)) { |
| 25 |
gdk_window_set_cursor(window, nullptr); |
| 26 |
} |
| 27 |
} |
| 28 |
p->self().doMouseLeave(); |
| 29 |
return true; |
| 30 |
} |
| 31 |
|
| 32 |
static auto Widget_mouseMove(GtkWidget* widget, GdkEventButton* event, pWidget* p) -> s32 { |
| 33 |
p->self().doMouseMove({(s32)event->x, (s32)event->y}); |
| 34 |
return true; |
| 35 |
} |
| 36 |
|
| 37 |
static auto Widget_mousePress(GtkWidget* widget, GdkEventButton* event, pWidget* p) -> s32 { |
| 38 |
switch(event->button) { |
| 39 |
case 1: p->self().doMousePress(Mouse::Button::Left); break; |
| 40 |
case 2: p->self().doMousePress(Mouse::Button::Middle); break; |
| 41 |
case 3: p->self().doMousePress(Mouse::Button::Right); break; |
| 42 |
} |
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
static auto Widget_mouseRelease(GtkWidget* widget, GdkEventButton* event, pWidget* p) -> s32 { |
| 47 |
switch(event->button) { |
| 48 |
case 1: p->self().doMouseRelease(Mouse::Button::Left); break; |
| 49 |
case 2: p->self().doMouseRelease(Mouse::Button::Middle); break; |
| 50 |
case 3: p->self().doMouseRelease(Mouse::Button::Right); break; |
| 51 |
} |
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
auto pWidget::construct() -> void { |
| 56 |
if(!gtkWidget) return; |
| 57 |
if(auto window = self().parentWindow(true)) { |
| 58 |
if(window->self()) window->self()->_append(self()); |
| 59 |
setDroppable(self().droppable()); |
| 60 |
setEnabled(self().enabled(true)); |
| 61 |
setFocusable(self().focusable()); |
| 62 |
setFont(self().font(true)); |
| 63 |
setMouseCursor(self().mouseCursor()); |
| 64 |
setToolTip(self().toolTip()); |
| 65 |
setVisible(self().visible(true)); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
auto pWidget::destruct() -> void { |
| 70 |
} |
| 71 |
|
| 72 |
auto pWidget::container(mWidget& widget) -> GtkWidget* { |
| 73 |
return nullptr; |
| 74 |
} |
| 75 |
|
| 76 |
auto pWidget::focused() const -> bool { |
| 77 |
if(!gtkWidget) return false; |
| 78 |
return gtk_widget_has_focus(gtkWidget); |
| 79 |
} |
| 80 |
|
| 81 |
auto pWidget::setDroppable(bool droppable) -> void { |
| 82 |
if(!gtkWidget) return; |
| 83 |
if(droppable) { |
| 84 |
gtk_drag_dest_set(gtkWidget, GTK_DEST_DEFAULT_ALL, nullptr, 0, GDK_ACTION_COPY); |
| 85 |
gtk_drag_dest_add_uri_targets(gtkWidget); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
auto pWidget::setEnabled(bool enabled) -> void { |
| 90 |
if(!gtkWidget) return; |
| 91 |
gtk_widget_set_sensitive(gtkWidget, enabled); |
| 92 |
} |
| 93 |
|
| 94 |
auto pWidget::setFocusable(bool focusable) -> void { |
| 95 |
//virtual overload: only configurable for Canvas and Viewport |
| 96 |
} |
| 97 |
|
| 98 |
auto pWidget::setFocused() -> void { |
| 99 |
if(!gtkWidget) return; |
| 100 |
gtk_widget_grab_focus(gtkWidget); |
| 101 |
} |
| 102 |
|
| 103 |
auto pWidget::setFont(const Font& font) -> void { |
| 104 |
if(!gtkWidget) return; |
| 105 |
return pFont::setFont(gtkWidget, font); |
| 106 |
} |
| 107 |
|
| 108 |
auto pWidget::setGeometry(Geometry geometry) -> void { |
| 109 |
if(!gtkWidget) return; |
| 110 |
if(gtkParent) gtk_fixed_move(GTK_FIXED(gtkParent), gtkWidget, geometry.x(), geometry.y()); |
| 111 |
if(geometry.width() < 1) geometry.setWidth (1); |
| 112 |
if(geometry.height() < 1) geometry.setHeight(1); |
| 113 |
gtk_widget_set_size_request(gtkWidget, geometry.width(), geometry.height()); |
| 114 |
if(0 && gtk_widget_get_realized(gtkWidget)) { |
| 115 |
static bool locked = false; |
| 116 |
if(!locked) { |
| 117 |
locked = true; |
| 118 |
auto time = chrono::millisecond(); |
| 119 |
while(chrono::millisecond() - time < 20) { |
| 120 |
GtkAllocation allocation; |
| 121 |
gtk_widget_get_allocation(gtkWidget, &allocation); |
| 122 |
if(allocation.width == geometry.width() && allocation.height == geometry.height()) break; |
| 123 |
gtk_main_iteration_do(false); |
| 124 |
} |
| 125 |
locked = false; |
| 126 |
} |
| 127 |
} |
| 128 |
pSizable::setGeometry(geometry); |
| 129 |
//this is needed to prevent some repainting issues (specifically with a Label which has a background color set for it) |
| 130 |
gtk_widget_queue_draw(gtkWidget); |
| 131 |
} |
| 132 |
|
| 133 |
auto pWidget::setMouseCursor(const MouseCursor& mouseCursor) -> void { |
| 134 |
//TODO: this should be freed, yet this code seems to break the cursor assignment completely ... |
| 135 |
//if(gdkMouseCursor) { |
| 136 |
// gdk_cursor_unref(gdkMouseCursor); |
| 137 |
// gdkMouseCursor = nullptr; |
| 138 |
//} |
| 139 |
|
| 140 |
if(mouseCursor) { |
| 141 |
string name; |
| 142 |
if(mouseCursor.name() == MouseCursor::Hand) name = "hand1"; |
| 143 |
if(mouseCursor.name() == MouseCursor::HorizontalResize) name = "sb_h_double_arrow"; |
| 144 |
if(mouseCursor.name() == MouseCursor::VerticalResize) name = "sb_v_double_arrow"; |
| 145 |
if(name) { |
| 146 |
gdkMouseCursor = gdk_cursor_new_from_name(gdk_display_get_default(), name); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
auto pWidget::setToolTip(const string& toolTip) -> void { |
| 152 |
gtk_widget_set_tooltip_text(gtkWidget, toolTip); |
| 153 |
} |
| 154 |
|
| 155 |
auto pWidget::setVisible(bool visible) -> void { |
| 156 |
if(!gtkWidget) return; |
| 157 |
gtk_widget_set_visible(gtkWidget, visible); |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
#endif |