Files

983 lines
13 KiB
C++

#pragma once
#include "core/xframe_core.h"
//region GtkComboBoxText
class GtkComboBoxText
{
private:
Display *display_;
int screen_;
Window window_;
GC gc_;
SStyle_Manager *styles_;
int x1_, y1_, x2_, y2_;
std::string name_;
std::string text_; // mirrors the active row's text
std::string style_name_; // "" = manager's active style
bool visible_ = true;
bool enabled_ = true;
bool focused_ = false;
std::vector<std::string> texts_;
int active_ = -1;
bool open_ = false;
int hover_ = -1;
int top_row_ = 0;
bool pressed_ = false;
int style_ = 0; // 0 = DropDown, 1 = Simple, 2 = DropDownList
int drop_down_count_ = 8;
int drop_down_width_ = 0; // 0 = same width as the widget
bool sorted_ = false;
bool read_only_ = false; // stored; the widget has no text editing
int max_length_ = 0; // stored; the widget has no text editing
bool auto_complete_ = false; // stored; the widget has no text editing
int char_case_ = 0; // stored; the widget has no text editing
int row_height()
{
return 20;
}
int max_rows()
{
return drop_down_count_;
}
int popup_width()
{
return drop_down_width_ > 0 ? drop_down_width_ : width();
}
int list_y1()
{
return y2_ + 2;
}
int shown_rows()
{
return std::min(max_rows(), (int)texts_.size());
}
bool in_list(int x, int y)
{
return open_ && x >= x1_ && x <= x1_ + popup_width() && y >= list_y1() && y <= list_y1() + shown_rows() * row_height();
}
void choose(int i)
{
if (i < 0 || i >= (int)texts_.size())
{
return;
}
bool was = (active_ != i);
active_ = i;
text_ = texts_[(size_t)i];
open_ = false;
if (was && on_changed)
{
on_changed();
}
}
// Getter/setter pass: colours formerly hardcoded in draw()/helpers.
Srgb text_color_{140, 140, 140};
Srgb fill_color_{255, 255, 255};
Srgb selection_fill_color_{195, 215, 245};
public:
std::function<void()> on_changed;
GtkComboBoxText(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_ = "GtkComboBoxText";
append_text("Small");
append_text("Medium");
append_text("Large");
append_text("Extra Large");
set_active(1);
}
~GtkComboBoxText() {}
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)
{
if (!visible_)
{
return false;
}
if (x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_)
{
return true;
}
return in_list(x, y);
}
void set_visible(bool v)
{
visible_ = v;
}
bool get_visible()
{
return visible_;
}
void set_enabled(bool e)
{
enabled_ = e;
if (!e)
{
open_ = false;
}
}
bool get_enabled()
{
return enabled_;
}
void set_style_name(std::string s)
{
style_name_ = s;
}
void set_text(std::string t)
{
set_active_text(t);
}
std::string get_text()
{
return get_active_text();
}
SStyle style()
{
return styles_->resolve(style_name_);
}
//region GtkComboBoxText specific API
void append_text(std::string t)
{
texts_.push_back(t);
if (active_ < 0)
{
active_ = 0;
text_ = t;
}
}
void prepend_text(std::string t)
{
texts_.insert(texts_.begin(), t);
if (active_ >= 0)
{
++active_;
}
else
{
active_ = 0;
text_ = t;
}
}
void insert_text(int position, std::string t)
{
position = std::max(0, std::min(position, (int)texts_.size()));
texts_.insert(texts_.begin() + position, t);
if (active_ >= position)
{
++active_;
}
}
void remove_text(int position)
{
if (position < 0 || position >= (int)texts_.size())
{
return;
}
texts_.erase(texts_.begin() + position);
if (active_ == position)
{
active_ = std::min(position, (int)texts_.size() - 1);
}
else if (active_ > position)
{
--active_;
}
text_ = active_ >= 0 ? texts_[(size_t)active_] : "";
}
void remove_all()
{
texts_.clear();
active_ = -1;
text_ = "";
open_ = false;
}
// Live-apply for the Inspector `items` prop (Phase 1): replace
// the whole list from the pipe-separated canonical form. The
// generator keeps emitting append_text() calls; this setter is
// for the live canvas widget.
void set_items(const std::string& items)
{
remove_all();
size_t start = 0;
while (start <= items.size()) {
size_t bar = items.find('|', start);
std::string one = items.substr(start, bar == std::string::npos ? bar : bar - start);
if (!one.empty()) append_text(one);
if (bar == std::string::npos) break;
start = bar + 1;
}
if (sorted_)
{
std::sort(texts_.begin(), texts_.end());
}
}
int get_style()
{
return style_;
}
void set_style(int v)
{
style_ = v;
}
int get_drop_down_count()
{
return drop_down_count_;
}
void set_drop_down_count(int v)
{
drop_down_count_ = v < 1 ? 1 : v;
}
int get_drop_down_width()
{
return drop_down_width_;
}
void set_drop_down_width(int v)
{
drop_down_width_ = v < 0 ? 0 : v;
}
bool get_sorted()
{
return sorted_;
}
void set_sorted(bool v)
{
sorted_ = v;
if (sorted_)
{
std::sort(texts_.begin(), texts_.end());
}
}
bool get_read_only()
{
return read_only_;
}
void set_read_only(bool v)
{
read_only_ = v;
}
int get_max_length()
{
return max_length_;
}
void set_max_length(int v)
{
max_length_ = v < 0 ? 0 : v;
}
bool get_auto_complete()
{
return auto_complete_;
}
void set_auto_complete(bool v)
{
auto_complete_ = v;
}
int get_char_case()
{
return char_case_;
}
void set_char_case(int v)
{
char_case_ = v;
}
std::string get_items()
{
std::string out;
for (size_t i = 0; i < texts_.size(); ++i) {
if (i) out += "|";
out += texts_[i];
}
return out;
}
int get_row_count()
{
return (int)texts_.size();
}
void set_active(int index)
{
if (index >= -1 && index < (int)texts_.size())
{
active_ = index;
text_ = index >= 0 ? texts_[(size_t)index] : "";
}
}
int get_active()
{
return active_;
}
std::string get_active_text()
{
if (active_ < 0 || active_ >= (int)texts_.size())
{
return "";
}
return texts_[(size_t)active_];
}
void set_active_text(std::string t)
{
for (int i = 0; i < (int)texts_.size(); ++i)
{
if (texts_[(size_t)i] == t)
{
active_ = i;
text_ = t;
return;
}
}
}
bool get_popup_open()
{
return open_;
}
//endregion
//region GtkComboBoxText colours (getter/setter pass: formerly hardcoded in draw()/helpers)
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_fill_color(Srgb c) { fill_color_ = c; }
Srgb get_fill_color() { return fill_color_; }
void set_fill_color_rgb(int r, int g, int b) { fill_color_ = Srgb{r, g, b}; }
void set_selection_fill_color(Srgb c) { selection_fill_color_ = c; }
Srgb get_selection_fill_color() { return selection_fill_color_; }
void set_selection_fill_color_rgb(int r, int g, int b) { selection_fill_color_ = Srgb{r, g, b}; }
//endregion
void draw()
{
if (!visible_)
{
return;
}
SStyle st = style();
if (!enabled_)
{
st.text_color_ = styles_->get_color_rgb(text_color_.r, text_color_.g, text_color_.b);
}
if (pressed_ || open_)
{
unsigned long swap = st.fillColor_;
st.fillColor_ = st.outlineColor_;
st.outlineColor_ = swap;
}
xf::draw_polished(display_, window_, gc_, st, x1_, y1_, x2_, y2_);
xf::draw_text_left(display_, window_, gc_, st, x1_ + 8, y1_, y2_, get_active_text());
XSetForeground(display_, gc_, st.lineColor_);
XDrawLine(display_, window_, gc_, x2_ - 24, y1_ + 3, x2_ - 24, y2_ - 3);
xf::draw_triangle_down(display_, window_, gc_, st.text_color_, x2_ - 12, (y1_ + y2_) / 2 - 3, 6);
if (focused_)
{
XSetForeground(display_, gc_, st.lineColor_);
XDrawRectangle(display_, window_, gc_, x1_ + 3, y1_ + 3, width() - 6, height() - 6);
}
if (open_)
{
int ly = list_y1();
int lh = shown_rows() * row_height();
XSetForeground(display_, gc_, styles_->get_color_rgb(fill_color_.r, fill_color_.g, fill_color_.b));
XFillRectangle(display_, window_, gc_, x1_, ly, popup_width(), lh);
XSetForeground(display_, gc_, st.lineColor_);
XDrawRectangle(display_, window_, gc_, x1_, ly, popup_width(), lh);
for (int row = 0; row < shown_rows(); ++row)
{
int idx = top_row_ + row;
if (idx >= (int)texts_.size())
{
break;
}
int ry = ly + row * row_height();
if (idx == hover_ || idx == active_)
{
XSetForeground(display_, gc_, styles_->get_color_rgb(selection_fill_color_.r, selection_fill_color_.g, selection_fill_color_.b));
XFillRectangle(display_, window_, gc_, x1_ + 1, ry + 1, popup_width() - 2, row_height() - 1);
}
xf::draw_text_left(display_, window_, gc_, st, x1_ + 8, ry, ry + row_height(), texts_[(size_t)idx]);
}
}
XFlush(display_);
}
bool on_button_press(int x, int y, unsigned int button)
{
if (!enabled_ || button != 1)
{
return false;
}
if (in_list(x, y))
{
choose(top_row_ + (y - list_y1()) / row_height());
return true;
}
if (x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_)
{
focused_ = true;
pressed_ = true;
open_ = !open_;
return true;
}
if (open_)
{
open_ = false;
}
return false;
}
bool on_button_release(int x, int y, unsigned int button)
{
(void)x;
(void)y;
if (button != 1 || !pressed_)
{
return false;
}
pressed_ = false;
return true;
}
bool on_motion(int x, int y)
{
if (!open_ || !in_list(x, y))
{
return false;
}
int idx = top_row_ + (y - list_y1()) / row_height();
if (idx == hover_)
{
return false;
}
hover_ = idx;
return true;
}
bool on_key(KeySym key, const char* str)
{
(void)str;
if (!focused_ || !enabled_)
{
return false;
}
if (key == XK_Down && active_ + 1 < (int)texts_.size())
{
choose(active_ + 1);
return true;
}
if (key == XK_Up && active_ > 0)
{
choose(active_ - 1);
return true;
}
if (key == XK_Return || key == XK_KP_Enter || key == XK_space)
{
open_ = !open_;
return true;
}
if (key == XK_Escape)
{
open_ = false;
return true;
}
return false;
}
};
//endregion