| 1 |
#if defined(Hiro_Label) |
| 2 |
|
| 3 |
namespace hiro { |
| 4 |
|
| 5 |
static auto Label_draw(GtkWidget* widget, cairo_t* context, pLabel* p) -> s32 { |
| 6 |
auto color = p->state().backgroundColor; |
| 7 |
if(auto window = p->self().parentWindow(true)) { |
| 8 |
if(!color) color = window->backgroundColor(); |
| 9 |
} |
| 10 |
|
| 11 |
if(color) { |
| 12 |
f64 red = (f64)color.red() / 255.0; |
| 13 |
f64 green = (f64)color.green() / 255.0; |
| 14 |
f64 blue = (f64)color.blue() / 255.0; |
| 15 |
f64 alpha = (f64)color.alpha() / 255.0; |
| 16 |
|
| 17 |
if(gdk_screen_is_composited(gdk_screen_get_default()) |
| 18 |
&& gdk_screen_get_rgba_visual(gdk_screen_get_default()) |
| 19 |
) { |
| 20 |
cairo_set_source_rgba(context, red, green, blue, alpha); |
| 21 |
} else { |
| 22 |
cairo_set_source_rgb(context, red, green, blue); |
| 23 |
} |
| 24 |
|
| 25 |
cairo_set_operator(context, CAIRO_OPERATOR_SOURCE); |
| 26 |
cairo_paint(context); |
| 27 |
} else { |
| 28 |
#if HIRO_GTK==3 |
| 29 |
auto style = gtk_widget_get_style_context(widget); |
| 30 |
if(auto tabFrame = p->self().parentTabFrame(true)) { |
| 31 |
if(auto self = tabFrame->self()) style = gtk_widget_get_style_context(self->gtkWidget); |
| 32 |
} |
| 33 |
GtkAllocation allocation; |
| 34 |
gtk_widget_get_allocation(widget, &allocation); |
| 35 |
gtk_render_background(style, context, 0, 0, allocation.width, allocation.height); |
| 36 |
#endif |
| 37 |
} |
| 38 |
|
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
static auto Label_expose(GtkWidget* widget, GdkEvent* event, pLabel* p) -> s32 { |
| 43 |
cairo_t* context = gdk_cairo_create(gtk_widget_get_window(widget)); |
| 44 |
Label_draw(widget, context, p); |
| 45 |
cairo_destroy(context); |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
static auto Label_mouseEnter(GtkWidget* widget, GdkEventButton* event, pCanvas* p) -> s32 { |
| 50 |
Widget_mouseEnter(widget, event, p); |
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
static auto Label_mouseLeave(GtkWidget* widget, GdkEventButton* event, pCanvas* p) -> s32 { |
| 55 |
Widget_mouseLeave(widget, event, p); |
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
static auto Label_mousePress(GtkWidget* widget, GdkEventButton* event, pLabel* p) -> s32 { |
| 60 |
switch(event->button) { |
| 61 |
case 1: p->self().doMousePress(Mouse::Button::Left); break; |
| 62 |
case 2: p->self().doMousePress(Mouse::Button::Middle); break; |
| 63 |
case 3: p->self().doMousePress(Mouse::Button::Right); break; |
| 64 |
} |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
static auto Label_mouseRelease(GtkWidget* widget, GdkEventButton* event, pLabel* p) -> s32 { |
| 69 |
switch(event->button) { |
| 70 |
case 1: p->self().doMouseRelease(Mouse::Button::Left); break; |
| 71 |
case 2: p->self().doMouseRelease(Mouse::Button::Middle); break; |
| 72 |
case 3: p->self().doMouseRelease(Mouse::Button::Right); break; |
| 73 |
} |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
auto pLabel::construct() -> void { |
| 78 |
gtkWidget = gtk_event_box_new(); |
| 79 |
subWidget = gtk_label_new(""); |
| 80 |
gtk_widget_show(subWidget); |
| 81 |
gtk_container_add(GTK_CONTAINER(gtkWidget), subWidget); |
| 82 |
|
| 83 |
setAlignment(state().alignment); |
| 84 |
setBackgroundColor(state().backgroundColor); |
| 85 |
setForegroundColor(state().foregroundColor); |
| 86 |
setText(state().text); |
| 87 |
|
| 88 |
g_signal_connect(G_OBJECT(gtkWidget), "button-press-event", G_CALLBACK(Label_mousePress), (gpointer)this); |
| 89 |
g_signal_connect(G_OBJECT(gtkWidget), "button-release-event", G_CALLBACK(Label_mouseRelease), (gpointer)this); |
| 90 |
#if HIRO_GTK==2 |
| 91 |
g_signal_connect(G_OBJECT(subWidget), "expose-event", G_CALLBACK(Label_expose), (gpointer)this); |
| 92 |
#elif HIRO_GTK==3 |
| 93 |
g_signal_connect(G_OBJECT(subWidget), "draw", G_CALLBACK(Label_draw), (gpointer)this); |
| 94 |
#endif |
| 95 |
g_signal_connect(G_OBJECT(gtkWidget), "enter-notify-event", G_CALLBACK(Label_mouseEnter), (gpointer)this); |
| 96 |
g_signal_connect(G_OBJECT(gtkWidget), "leave-notify-event", G_CALLBACK(Label_mouseLeave), (gpointer)this); |
| 97 |
|
| 98 |
pWidget::construct(); |
| 99 |
} |
| 100 |
|
| 101 |
auto pLabel::destruct() -> void { |
| 102 |
gtk_widget_destroy(subWidget); |
| 103 |
gtk_widget_destroy(gtkWidget); |
| 104 |
} |
| 105 |
|
| 106 |
auto pLabel::minimumSize() const -> Size { |
| 107 |
auto size = pFont::size(self().font(true), state().text); |
| 108 |
return {size.width(), size.height()}; |
| 109 |
} |
| 110 |
|
| 111 |
auto pLabel::setAlignment(Alignment alignment) -> void { |
| 112 |
if(!alignment) alignment = {0.0, 0.5}; |
| 113 |
gtk_misc_set_alignment(GTK_MISC(subWidget), alignment.horizontal(), alignment.vertical()); |
| 114 |
auto justify = GTK_JUSTIFY_CENTER; |
| 115 |
if(alignment.horizontal() < 0.333) justify = GTK_JUSTIFY_LEFT; |
| 116 |
if(alignment.horizontal() > 0.666) justify = GTK_JUSTIFY_RIGHT; |
| 117 |
gtk_label_set_justify(GTK_LABEL(subWidget), justify); |
| 118 |
} |
| 119 |
|
| 120 |
auto pLabel::setBackgroundColor(Color color) -> void { |
| 121 |
//GTK3 will paint the wrong background color when a label is inside of a TabFrame |
| 122 |
//this is caused by the GtkEventBox wrapper that is required to paint custom backgrounds |
| 123 |
//handle background painting via "draw" or "expose-event" signals instead |
| 124 |
} |
| 125 |
|
| 126 |
auto pLabel::setForegroundColor(Color color) -> void { |
| 127 |
auto gdkColor = CreateColor(color); |
| 128 |
gtk_widget_modify_fg(subWidget, GTK_STATE_NORMAL, color ? &gdkColor : nullptr); |
| 129 |
} |
| 130 |
|
| 131 |
auto pLabel::setText(const string& text) -> void { |
| 132 |
gtk_label_set_text(GTK_LABEL(subWidget), text); |
| 133 |
} |
| 134 |
|
| 135 |
auto pLabel::setVisible(bool visible) -> void { |
| 136 |
setBackgroundColor(state().backgroundColor); |
| 137 |
return pWidget::setVisible(visible); |
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
#endif |