#pragma once #include "core/xframe_core.h" //region TButton class TButton { 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 pressed_ = false; bool default_ = false; int modal_result_ = 0; public: std::function on_click; TButton(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_ = "TButton"; text_ = "OK"; } ~TButton() {} 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_default(bool d) { default_ = d; } bool get_default() { return default_; } void set_modal_result(int r) { modal_result_ = r; } int get_modal_result() { return modal_result_; } bool get_pressed() { return pressed_; } void click() { if (enabled_ && on_click) { on_click(); } } void draw() { if (!visible_) { return; } SStyle st = style(); if (!enabled_) { st.text_color_ = styles_->get_color_rgb(140, 140, 140); } if (pressed_) { // Sunken look: swap fill and outline while held down. unsigned long swap = st.fillColor_; st.fillColor_ = st.outlineColor_; st.outlineColor_ = swap; } xf::draw_polished(display_, window_, gc_, st, x1_, y1_, x2_, y2_); if (default_) { XSetForeground(display_, gc_, st.lineColor_); xf::set_line_width(display_, gc_, 2); XDrawRectangle(display_, window_, gc_, x1_ + 2, y1_ + 2, width() - 4, height() - 4); xf::set_line_width(display_, gc_, 1); } int tx = pressed_ ? 1 : 0; int ty = pressed_ ? 1 : 0; xf::draw_text_centered(display_, window_, gc_, st, x1_ + tx, y1_ + ty, x2_ + tx, y2_ + ty, text_); if (focused_) { XSetForeground(display_, gc_, st.lineColor_); XDrawRectangle(display_, window_, gc_, x1_ + 4, y1_ + 4, width() - 8, height() - 8); } XFlush(display_); } bool on_button_press(int x, int y, unsigned int button) { if (!contains(x, y) || !enabled_ || button != 1) { return false; } pressed_ = true; focused_ = true; return true; } bool on_button_release(int x, int y, unsigned int button) { if (!pressed_ || button != 1) { return false; } pressed_ = false; if (contains(x, y) && on_click) { on_click(); } return true; } 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