#pragma once #include "core/xframe_core.h" //region GtkEventBox class GtkEventBox { private: Display *display_; int screen_; Window window_; GC gc_; SStyle_Manager *styles_; int x1_, y1_, x2_, y2_; std::string name_; std::string text_; std::string style_name_; // "" = manager's active style bool visible_ = true; bool enabled_ = true; bool focused_ = false; bool above_child_ = false; bool visible_window_ = true; bool design_time_ = true; std::string last_event_; int event_count_ = 0; // Getter/setter pass: colours formerly hardcoded in draw()/helpers. Srgb very_faint_color_{245, 245, 248}; Srgb border_color_{200, 200, 205}; Srgb text_color_{150, 150, 155}; Srgb no_window_color_{170, 170, 175}; public: std::function on_click; GtkEventBox(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height) { display_ = display; screen_ = screen; window_ = window; gc_ = gc; styles_ = styles; x1_ = x; y1_ = y; x2_ = x + width; y2_ = y + height; name_ = "GtkEventBox"; text_ = "EventBox"; last_event_ = ""; } ~GtkEventBox() {} void set_bounds(int x, int y, int width, int height) { x1_ = x; y1_ = y; x2_ = x + width; y2_ = y + height; } int width() { return x2_ - x1_; } int height() { return y2_ - y1_; } bool contains(int x, int y) { return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; } void set_visible(bool v) { visible_ = v; } bool get_visible() { return visible_; } void set_enabled(bool e) { enabled_ = e; } bool get_enabled() { return enabled_; } void set_style_name(std::string s) { style_name_ = s; } void set_text(std::string t) { text_ = t; } std::string get_text() { return text_; } SStyle style() { return styles_->resolve(style_name_); } void set_above_child(bool a) { above_child_ = a; } bool get_above_child() { return above_child_; } void set_visible_window(bool v) { visible_window_ = v; } bool get_visible_window() { return visible_window_; } void set_design_time(bool d) { design_time_ = d; } std::string get_last_event() { return last_event_; } int get_event_count() { return event_count_; } //region GtkEventBox colours (getter/setter pass: formerly hardcoded in draw()/helpers) void set_very_faint_color(Srgb c) { very_faint_color_ = c; } Srgb get_very_faint_color() { return very_faint_color_; } void set_very_faint_color_rgb(int r, int g, int b) { very_faint_color_ = Srgb{r, g, b}; } void set_border_color(Srgb c) { border_color_ = c; } Srgb get_border_color() { return border_color_; } void set_border_color_rgb(int r, int g, int b) { border_color_ = Srgb{r, g, b}; } void set_text_color(Srgb c) { text_color_ = c; } Srgb get_text_color() { return text_color_; } void set_text_color_rgb(int r, int g, int b) { text_color_ = Srgb{r, g, b}; } void set_no_window_color(Srgb c) { no_window_color_ = c; } Srgb get_no_window_color() { return no_window_color_; } void set_no_window_color_rgb(int r, int g, int b) { no_window_color_ = Srgb{r, g, b}; } //endregion void draw() { if (!visible_) { return; } SStyle st = style(); if (visible_window_) { // Very faint fill so the catch area is just barely visible. XSetForeground(display_, gc_, styles_->get_color_rgb(very_faint_color_.r, very_faint_color_.g, very_faint_color_.b)); XFillRectangle(display_, window_, gc_, x1_, y1_, width(), height()); XSetForeground(display_, gc_, styles_->get_color_rgb(border_color_.r, border_color_.g, border_color_.b)); XDrawRectangle(display_, window_, gc_, x1_, y1_, width() - 1, height() - 1); SStyle label = st; label.text_color_ = styles_->get_color_rgb(text_color_.r, text_color_.g, text_color_.b); xf::draw_text_centered(display_, window_, gc_, label, x1_, y1_, x2_, y2_, text_); } else if (design_time_) { // No window of its own: just a dotted outline at design time. XSetForeground(display_, gc_, styles_->get_color_rgb(no_window_color_.r, no_window_color_.g, no_window_color_.b)); for (int x = x1_; x < x2_; x += 6) { XDrawLine(display_, window_, gc_, x, y1_, std::min(x + 3, x2_), y1_); XDrawLine(display_, window_, gc_, x, y2_ - 1, std::min(x + 3, x2_), y2_ - 1); } for (int y = y1_; y < y2_; y += 6) { XDrawLine(display_, window_, gc_, x1_, y, x1_, std::min(y + 3, y2_)); XDrawLine(display_, window_, gc_, x2_ - 1, y, x2_ - 1, std::min(y + 3, y2_)); } } XFlush(display_); } bool on_button_press(int x, int y, unsigned int button) { if (!contains(x, y) || !enabled_ || button != 1) { return false; } focused_ = true; last_event_ = "press " + std::to_string(x) + "," + std::to_string(y); event_count_++; if (on_click) { on_click(); } return true; } bool on_button_release(int x, int y, unsigned int button) { if (!contains(x, y) || button != 1) { return false; } last_event_ = "release " + std::to_string(x) + "," + std::to_string(y); event_count_++; return true; } bool on_motion(int x, int y) { if (!contains(x, y)) { return false; } last_event_ = "motion " + std::to_string(x) + "," + std::to_string(y); return true; } bool on_key(KeySym key, const char* str) { (void)str; if (!focused_ || !enabled_) { return false; } last_event_ = "key " + std::to_string((int)key); event_count_++; return true; } }; //endregion