Files

545 lines
6.9 KiB
C++

#pragma once
#include "core/xframe_core.h"
//region GtkRadioButton
class GtkRadioButton
{
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 active_ = false;
// Shared group list, owned by the caller. Every button in the
// group points at the same vector; turning one on turns the
// rest off.
std::vector<GtkRadioButton*> *group_ = nullptr;
int group_id_ = 0; // Inspector group id; the generator wires buttons
// sharing an id into one shared group vector.
// Colours exposed by the getter/setter pass; defaults are the old hardcoded values.
Srgb disabled_text_color_ = {140, 140, 140};
Srgb disc_color_ = {255, 255, 255};
public:
std::function<void()> on_click;
std::function<void(bool)> on_toggled;
GtkRadioButton(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_ = "GtkRadioButton";
text_ = "Option";
}
~GtkRadioButton() {}
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};
}
Srgb get_disc_color()
{
return disc_color_;
}
void set_disc_color(Srgb c)
{
disc_color_ = c;
}
void set_disc_color_rgb(int r, int g, int b)
{
disc_color_ = Srgb{r, g, b};
}
SStyle style()
{
return styles_->resolve(style_name_);
}
//region Group and active state
void set_group(std::vector<GtkRadioButton*> *group)
{
group_ = group;
if (group_)
{
bool found = false;
for (auto *b : *group_)
{
if (b == this)
{
found = true;
}
}
if (!found)
{
group_->push_back(this);
}
}
}
std::vector<GtkRadioButton*> *get_group()
{
return group_;
}
int get_group_id()
{
return group_id_;
}
void set_group_id(int v)
{
group_id_ = v;
}
void set_active(bool a)
{
if (a && group_)
{
// Turning on: every other member of the group goes off.
for (auto *b : *group_)
{
if (b != this && b->active_)
{
b->active_ = false;
if (b->on_toggled)
{
b->on_toggled(false);
}
}
}
}
if (a == active_)
{
return;
}
active_ = a;
if (on_toggled)
{
on_toggled(active_);
}
}
bool get_active()
{
return active_;
}
//endregion
void draw()
{
if (!visible_)
{
return;
}
SStyle st = style();
if (!enabled_)
{
st.text_color_ = styles_->get_color_Srgb(disabled_text_color_);
}
// The indicator: a circle of radius 8 at the left.
int r = 8;
int cx = x1_ + 2 + r;
int cy = y1_ + height() / 2;
XSetForeground(display_, gc_, styles_->get_color_Srgb(disc_color_));
XFillArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64);
XSetForeground(display_, gc_, st.lineColor_);
xf::set_line_width(display_, gc_, 1);
XDrawArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64);
if (active_)
{
int dr = 4;
XSetForeground(display_, gc_, st.text_color_);
XFillArc(display_, window_, gc_, cx - dr, cy - dr, 2 * dr, 2 * dr, 0, 360 * 64);
}
xf::draw_text_left(display_, window_, gc_, st, cx + r + 6, y1_, y2_, text_);
if (focused_)
{
XSetForeground(display_, gc_, st.lineColor_);
XDrawRectangle(display_, window_, gc_, x1_, y1_, width(), height());
}
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) && !active_)
{
// A radio button never turns itself off by clicking.
set_active(true);
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) && !active_)
{
set_active(true);
if (on_click)
{
on_click();
}
return true;
}
return false;
}
};
//endregion