#pragma once #include "core/xframe_core.h" //region TFrame class TFrame { public: struct Rect { int x; int y; int w; int h; }; 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; int border_width_ = 4; bool show_caption_ = true; // Colours exposed by the getter/setter pass; defaults are the old hardcoded values. Srgb disabled_text_color_ = {140, 140, 140}; public: std::function on_click; TFrame(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_ = "TFrame"; text_ = "Frame"; } ~TFrame() {} 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_; } Srgb get_disabled_text_color() { return disabled_text_color_; } void set_disabled_text_color(Srgb c) { disabled_text_color_ = c; } void set_disabled_text_color_rgb(int r, int g, int b) { disabled_text_color_ = Srgb{r, g, b}; } SStyle style() { return styles_->resolve(style_name_); } //region Frame specifics void set_border_width(int w) { if (w < 0) { w = 0; } border_width_ = w; } int get_border_width() { return border_width_; } void set_show_caption(bool s) { show_caption_ = s; } bool get_show_caption() { return show_caption_; } // Inner rect available to whatever is placed inside the frame: // the bounds inset by border_width_ on every side, plus room // for the caption along the top when it is shown. Rect client_rect() { Rect r; r.x = x1_ + border_width_; r.y = y1_ + border_width_; if (show_caption_ && !text_.empty()) { r.y = y1_ + border_width_ + 14; } r.w = width() - 2 * border_width_; r.h = y2_ - r.y - border_width_; if (r.w < 0) { r.w = 0; } if (r.h < 0) { r.h = 0; } return r; } bool contains_client(int x, int y) { Rect r = client_rect(); return x >= r.x && x <= r.x + r.w && y >= r.y && y <= r.y + r.h; } //endregion void draw() { if (!visible_) { return; } SStyle st = style(); if (!enabled_) { st.text_color_ = styles_->get_color_Srgb(disabled_text_color_); } // Fill of the style fill colour inside the frame. XSetForeground(display_, gc_, st.fillColor_); XFillRectangle(display_, window_, gc_, x1_, y1_, width(), height()); // Plain square border, a single line in the line colour. // The top edge drops a little when a caption is shown, so // the caption can sit on it like a group box caption. int top = y1_; if (show_caption_ && !text_.empty()) { top = y1_ + 8; } XSetForeground(display_, gc_, st.lineColor_); xf::set_line_width(display_, gc_, 1); XDrawRectangle(display_, window_, gc_, x1_, top, width() - 1, y2_ - top - 1); if (show_caption_ && !text_.empty() && st.font_) { int tw = xf::text_width(st, text_); XSetForeground(display_, gc_, st.fillColor_); XFillRectangle(display_, window_, gc_, x1_ + 8, y1_, tw + 8, 16); xf::draw_text_left(display_, window_, gc_, st, x1_ + 12, y1_, y1_ + 16, text_); } if (focused_) { Rect r = client_rect(); XSetForeground(display_, gc_, st.lineColor_); if (r.w > 4 && r.h > 4) { XDrawRectangle(display_, window_, gc_, r.x + 1, r.y + 1, r.w - 3, r.h - 3); } } XFlush(display_); } bool on_button_press(int x, int y, unsigned int button) { if (!contains(x, y) || !enabled_ || button != 1) { return false; } focused_ = true; return true; } bool on_button_release(int x, int y, unsigned int button) { if (button != 1) { return false; } if (contains(x, y) && enabled_ && on_click) { on_click(); return true; } return false; } bool on_motion(int x, int y) { (void)x; (void)y; return false; } bool on_key(KeySym key, const char* str) { (void)str; if (!focused_ || !enabled_) { return false; } if (key == XK_Return || key == XK_space) { if (on_click) { on_click(); } return true; } return false; } }; //endregion