Files
Muse 7ba66af59d Restructure: move XFrame tree under widgets/ (combined IDE, part 1)
CWidgets library (components, core, palette, layout, common, icons,
tools, test-results, test-driver, tests, screenshots) moves under
widgets/ so the repo root can hold the combined IDE: designer/ +
widgets/ + docs/. Pure git mv, no content changes.
2026-11-25 15:45:12 -05:00

426 lines
5.8 KiB
C++

#pragma once
#include "core/xframe_core.h"
//region GtkLockButton
class GtkLockButton
{
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 locked_ = true; // preloaded locked, so the demo shows the closed shackle
public:
std::function<void()> on_click;
GtkLockButton(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_ = "GtkLockButton";
text_ = "Unlock";
}
~GtkLockButton() {}
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_);
}
//region Lock state
void set_locked(bool locked)
{
locked_ = locked;
// Like the real widget, the caption names the action on offer.
text_ = locked_ ? "Unlock" : "Lock";
}
bool get_locked()
{
return locked_;
}
//endregion
void draw()
{
if (!visible_)
{
return;
}
SStyle st = style();
if (!enabled_)
{
st.text_color_ = styles_->get_color_rgb(140, 140, 140);
}
if (pressed_)
{
unsigned long swap = st.fillColor_;
st.fillColor_ = st.outlineColor_;
st.outlineColor_ = swap;
}
xf::draw_polished(display_, window_, gc_, st, x1_, y1_, x2_, y2_);
int cy = y1_ + height() / 2;
//region Padlock glyph
// The body: a small gold rounded block.
SStyle body = st;
body.fillColor_ = styles_->get_color_rgb(205, 170, 75);
body.radius_ = 2;
int bx1 = x1_ + 10;
int bx2 = bx1 + 16;
int by1 = cy - 1;
int by2 = by1 + 12;
xf::fill_rounded_rect(display_, window_, gc_, body, bx1, by1, bx2, by2);
xf::draw_rounded_outline(display_, window_, gc_, body, bx1, by1, bx2, by2);
// The shackle: a half arc over the body. Locked it sits down
// on both sides; unlocked it is raised, with the right leg
// swung free, so only the left leg is drawn.
XSetForeground(display_, gc_, st.lineColor_);
xf::set_line_width(display_, gc_, 2);
if (locked_)
{
XDrawArc(display_, window_, gc_, bx1 + 2, by1 - 9, 12, 12, 0, 180 * 64);
XDrawLine(display_, window_, gc_, bx1 + 2, by1 - 3, bx1 + 2, by1);
XDrawLine(display_, window_, gc_, bx2 - 2, by1 - 3, bx2 - 2, by1);
}
else
{
XDrawArc(display_, window_, gc_, bx1 + 2, by1 - 12, 12, 12, 0, 180 * 64);
XDrawLine(display_, window_, gc_, bx1 + 2, by1 - 6, bx1 + 2, by1);
}
xf::set_line_width(display_, gc_, 1);
// The keyhole.
int kx = (bx1 + bx2) / 2;
XSetForeground(display_, gc_, styles_->get_color_rgb(40, 40, 40));
XFillArc(display_, window_, gc_, kx - 2, cy + 2, 4, 4, 0, 360 * 64);
XDrawLine(display_, window_, gc_, kx, cy + 5, kx, cy + 8);
//endregion
xf::draw_text_left(display_, window_, gc_, st, bx2 + 8, y1_, y2_, text_);
if (focused_)
{
XSetForeground(display_, gc_, st.lineColor_);
XDrawRectangle(display_, window_, gc_, x1_ + 3, y1_ + 3, width() - 6, height() - 6);
}
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))
{
set_locked(!locked_);
if (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)
{
set_locked(!locked_);
if (on_click)
{
on_click();
}
return true;
}
return false;
}
};
//endregion