Files

425 lines
6.0 KiB
C++

#pragma once
#include "core/xframe_core.h"
//region GtkCheckMenuItem
class GtkCheckMenuItem
{
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 hovered_ = false;
bool checked_ = true;
bool inconsistent_ = false;
// Getter/setter pass: colours formerly hardcoded in draw()/helpers.
Srgb hot_fill_color_{70, 110, 180};
Srgb text_color_{255, 255, 255};
Srgb text_2_color_{150, 150, 150};
public:
std::function<void()> on_toggled;
GtkCheckMenuItem(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_ = "GtkCheckMenuItem";
text_ = "Show Toolbar";
}
~GtkCheckMenuItem() {}
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 Check menu item specific API
bool get_active()
{
return checked_;
}
void set_active(bool active)
{
checked_ = active;
}
void set_inconsistent(bool inconsistent)
{
inconsistent_ = inconsistent;
}
bool get_inconsistent()
{
return inconsistent_;
}
void toggle()
{
checked_ = !checked_;
inconsistent_ = false;
if (on_toggled)
{
on_toggled();
}
}
//endregion
//region GtkCheckMenuItem colours (getter/setter pass: formerly hardcoded in draw()/helpers)
void set_hot_fill_color(Srgb c) { hot_fill_color_ = c; }
Srgb get_hot_fill_color() { return hot_fill_color_; }
void set_hot_fill_color_rgb(int r, int g, int b) { hot_fill_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_text_2_color(Srgb c) { text_2_color_ = c; }
Srgb get_text_2_color() { return text_2_color_; }
void set_text_2_color_rgb(int r, int g, int b) { text_2_color_ = Srgb{r, g, b}; }
//endregion
void draw()
{
if (!visible_)
{
return;
}
SStyle st = style();
SStyle row_style = st;
if (hovered_ && enabled_)
{
unsigned long hot_fill = styles_->get_color_rgb(hot_fill_color_.r, hot_fill_color_.g, hot_fill_color_.b);
XSetForeground(display_, gc_, hot_fill);
XFillRectangle(display_, window_, gc_, x1_, y1_, width(), height());
row_style.text_color_ = styles_->get_color_rgb(text_color_.r, text_color_.g, text_color_.b);
}
else
{
XSetForeground(display_, gc_, st.fillColor_);
XFillRectangle(display_, window_, gc_, x1_, y1_, width(), height());
}
if (!enabled_)
{
row_style.text_color_ = styles_->get_color_rgb(text_2_color_.r, text_2_color_.g, text_2_color_.b);
}
// Check box zone on the left.
int box = 13;
int bx = x1_ + 8;
int by = (y1_ + y2_) / 2 - box / 2;
XSetForeground(display_, gc_, st.lineColor_);
XDrawRectangle(display_, window_, gc_, bx, by, box, box);
if (inconsistent_)
{
XSetForeground(display_, gc_, row_style.text_color_);
xf::set_line_width(display_, gc_, 2);
XDrawLine(display_, window_, gc_, bx + 3, by + box / 2, bx + box - 3, by + box / 2);
xf::set_line_width(display_, gc_, 1);
}
else if (checked_)
{
xf::draw_checkmark(display_, window_, gc_, row_style.text_color_, bx + box / 2, by + box / 2, box - 2);
}
xf::draw_text_left(display_, window_, gc_, row_style, x1_ + 30, y1_, y2_, text_);
XFlush(display_);
}
bool on_button_press(int x, int y, unsigned int button)
{
if (!contains(x, y) || !enabled_ || button != 1)
{
return false;
}
focused_ = true;
hovered_ = true;
toggle();
return true;
}
bool on_button_release(int x, int y, unsigned int button)
{
(void)x;
(void)y;
(void)button;
return false;
}
bool on_motion(int x, int y)
{
bool inside = contains(x, y);
if (inside != hovered_)
{
hovered_ = inside;
return true;
}
return false;
}
bool on_key(KeySym key, const char* str)
{
(void)str;
if (!focused_ || !enabled_)
{
return false;
}
if (key == XK_Return || key == XK_space)
{
toggle();
return true;
}
return false;
}
};
//endregion