929 lines
14 KiB
C++
929 lines
14 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
|
|
//region GtkListBox
|
|
|
|
class GtkListBox
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
std::vector<std::string> rows_;
|
|
|
|
int row_height_ = 24;
|
|
|
|
int selected_row_ = 0;
|
|
|
|
bool multi_select_ = false;
|
|
|
|
bool sorted_ = false;
|
|
|
|
std::vector<int> selected_rows_; // toggled rows when multi_select_
|
|
|
|
int scroll_offset_ = 0; // first visible row index
|
|
|
|
int inset_ = 2;
|
|
|
|
void clamp_scroll()
|
|
|
|
{
|
|
|
|
int vis = visible_row_count();
|
|
|
|
if (scroll_offset_ < 0)
|
|
|
|
{
|
|
|
|
scroll_offset_ = 0;
|
|
|
|
}
|
|
|
|
int max_off = (int)rows_.size() - vis;
|
|
|
|
if (max_off < 0)
|
|
|
|
{
|
|
|
|
max_off = 0;
|
|
|
|
}
|
|
|
|
if (scroll_offset_ > max_off)
|
|
|
|
{
|
|
|
|
scroll_offset_ = max_off;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void keep_selection_visible()
|
|
|
|
{
|
|
|
|
if (selected_row_ < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int vis = visible_row_count();
|
|
|
|
if (selected_row_ < scroll_offset_)
|
|
|
|
{
|
|
|
|
scroll_offset_ = selected_row_;
|
|
|
|
}
|
|
|
|
if (selected_row_ >= scroll_offset_ + vis)
|
|
|
|
{
|
|
|
|
scroll_offset_ = selected_row_ - vis + 1;
|
|
|
|
}
|
|
|
|
clamp_scroll();
|
|
|
|
}
|
|
|
|
// Getter/setter pass: colours formerly hardcoded in draw()/helpers.
|
|
Srgb text_color_{140, 140, 140};
|
|
Srgb frame_background_color_{255, 255, 255};
|
|
Srgb separator_line_color_{225, 225, 225};
|
|
|
|
public:
|
|
|
|
std::function<void(int)> on_row_selected;
|
|
|
|
GtkListBox(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_ = "GtkListBox";
|
|
|
|
text_ = "List Box";
|
|
|
|
rows_.push_back("Apple");
|
|
|
|
rows_.push_back("Banana");
|
|
|
|
rows_.push_back("Cherry");
|
|
|
|
rows_.push_back("Dragonfruit");
|
|
|
|
rows_.push_back("Elderberry");
|
|
|
|
rows_.push_back("Fig");
|
|
|
|
rows_.push_back("Grape");
|
|
|
|
rows_.push_back("Honeydew");
|
|
|
|
rows_.push_back("Kiwi");
|
|
|
|
rows_.push_back("Lemon");
|
|
|
|
rows_.push_back("Mango");
|
|
|
|
rows_.push_back("Nectarine");
|
|
|
|
rows_.push_back("Orange");
|
|
|
|
rows_.push_back("Papaya");
|
|
|
|
}
|
|
|
|
~GtkListBox() {}
|
|
|
|
void set_bounds(int x, int y, int width, int height)
|
|
|
|
{
|
|
|
|
x1_ = x;
|
|
|
|
y1_ = y;
|
|
|
|
x2_ = x + width;
|
|
|
|
y2_ = y + height;
|
|
|
|
clamp_scroll();
|
|
|
|
}
|
|
|
|
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_);
|
|
|
|
}
|
|
|
|
int visible_row_count()
|
|
|
|
{
|
|
|
|
int n = (height() - 2 * inset_) / row_height_;
|
|
|
|
return n < 1 ? 1 : n;
|
|
|
|
}
|
|
|
|
// Rect of the row at visible slot v (0 = top visible row).
|
|
|
|
void row_rect(int v, int& rx1, int& ry1, int& rx2, int& ry2)
|
|
|
|
{
|
|
|
|
rx1 = x1_ + inset_;
|
|
|
|
ry1 = y1_ + inset_ + v * row_height_;
|
|
|
|
rx2 = x2_ - inset_;
|
|
|
|
ry2 = ry1 + row_height_;
|
|
|
|
}
|
|
|
|
void add_row(std::string label)
|
|
|
|
{
|
|
|
|
rows_.push_back(label);
|
|
|
|
}
|
|
|
|
// Live-apply for the Inspector `items` prop (Phase 1): replace
|
|
|
|
// the whole list from the pipe-separated canonical form. The
|
|
|
|
// generator keeps emitting add_row() calls; this setter is for
|
|
|
|
// the live canvas widget.
|
|
|
|
void set_items(const std::string& items)
|
|
|
|
{
|
|
|
|
rows_.clear();
|
|
|
|
selected_rows_.clear();
|
|
|
|
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()) rows_.push_back(one);
|
|
|
|
if (bar == std::string::npos) break;
|
|
|
|
start = bar + 1;
|
|
|
|
}
|
|
|
|
if (sorted_)
|
|
|
|
{
|
|
|
|
std::sort(rows_.begin(), rows_.end());
|
|
|
|
}
|
|
|
|
selected_row_ = rows_.empty() ? -1 : 0;
|
|
|
|
scroll_offset_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string get_items()
|
|
|
|
{
|
|
|
|
std::string out;
|
|
|
|
for (size_t i = 0; i < rows_.size(); ++i) {
|
|
|
|
if (i) out += "|";
|
|
|
|
out += rows_[i];
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
void remove_row(int i)
|
|
|
|
{
|
|
|
|
if (i < 0 || i >= (int)rows_.size())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rows_.erase(rows_.begin() + i);
|
|
|
|
selected_rows_.clear();
|
|
|
|
if (selected_row_ >= (int)rows_.size())
|
|
|
|
{
|
|
|
|
selected_row_ = (int)rows_.size() - 1;
|
|
|
|
}
|
|
|
|
clamp_scroll();
|
|
|
|
}
|
|
|
|
int row_count()
|
|
|
|
{
|
|
|
|
return (int)rows_.size();
|
|
|
|
}
|
|
|
|
int get_selected_row()
|
|
|
|
{
|
|
|
|
return selected_row_;
|
|
|
|
}
|
|
|
|
void set_selected_row(int i)
|
|
|
|
{
|
|
|
|
if (rows_.empty())
|
|
|
|
{
|
|
|
|
selected_row_ = -1;
|
|
|
|
selected_rows_.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (i < -1)
|
|
|
|
{
|
|
|
|
i = -1;
|
|
|
|
}
|
|
|
|
if (i >= (int)rows_.size())
|
|
|
|
{
|
|
|
|
i = (int)rows_.size() - 1;
|
|
|
|
}
|
|
|
|
selected_row_ = i;
|
|
|
|
if (multi_select_ && i >= 0 &&
|
|
std::find(selected_rows_.begin(), selected_rows_.end(), i) ==
|
|
selected_rows_.end())
|
|
selected_rows_.push_back(i);
|
|
|
|
if (!multi_select_) selected_rows_.clear();
|
|
|
|
keep_selection_visible();
|
|
|
|
}
|
|
|
|
void set_multi_select(bool v)
|
|
|
|
{
|
|
|
|
multi_select_ = v;
|
|
|
|
if (!v) selected_rows_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_multi_select()
|
|
|
|
{
|
|
|
|
return multi_select_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_selected(int idx)
|
|
|
|
{
|
|
|
|
if (idx == selected_row_) return true;
|
|
|
|
return multi_select_ &&
|
|
|
|
std::find(selected_rows_.begin(), selected_rows_.end(), idx) !=
|
|
|
|
selected_rows_.end();
|
|
|
|
}
|
|
|
|
std::string get_row(int i)
|
|
|
|
{
|
|
|
|
if (i < 0 || i >= (int)rows_.size())
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return rows_[i];
|
|
|
|
}
|
|
|
|
//region GtkListBox 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_frame_background_color(Srgb c) { frame_background_color_ = c; }
|
|
Srgb get_frame_background_color() { return frame_background_color_; }
|
|
void set_frame_background_color_rgb(int r, int g, int b) { frame_background_color_ = Srgb{r, g, b}; }
|
|
void set_separator_line_color(Srgb c) { separator_line_color_ = c; }
|
|
Srgb get_separator_line_color() { return separator_line_color_; }
|
|
void set_separator_line_color_rgb(int r, int g, int b) { separator_line_color_ = Srgb{r, g, b}; }
|
|
//endregion
|
|
//region GtkListBox properties (getter/setter pass)
|
|
void set_inset(int v) { inset_ = v; }
|
|
int get_inset() { return inset_; }
|
|
void set_row_height(int v) { row_height_ = v; }
|
|
|
|
bool get_sorted()
|
|
|
|
{
|
|
|
|
return sorted_;
|
|
|
|
}
|
|
|
|
void set_sorted(bool v)
|
|
|
|
{
|
|
|
|
sorted_ = v;
|
|
|
|
if (sorted_)
|
|
|
|
{
|
|
|
|
std::sort(rows_.begin(), rows_.end());
|
|
|
|
selected_row_ = rows_.empty() ? -1 : 0;
|
|
|
|
selected_rows_.clear();
|
|
|
|
scroll_offset_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
int get_row_height() { return row_height_; }
|
|
//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);
|
|
|
|
}
|
|
|
|
clamp_scroll();
|
|
|
|
// Frame + background.
|
|
|
|
XSetForeground(display_, gc_, styles_->get_color_rgb(frame_background_color_.r, frame_background_color_.g, frame_background_color_.b));
|
|
|
|
XFillRectangle(display_, window_, gc_, x1_, y1_, width(), height());
|
|
|
|
XSetForeground(display_, gc_, st.lineColor_);
|
|
|
|
xf::set_line_width(display_, gc_, (int)st.lineWidth_);
|
|
|
|
XDrawRectangle(display_, window_, gc_, x1_, y1_, width(), height());
|
|
|
|
int vis = visible_row_count();
|
|
|
|
for (int v = 0; v < vis; ++v)
|
|
|
|
{
|
|
|
|
int idx = scroll_offset_ + v;
|
|
|
|
if (idx >= (int)rows_.size())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int rx1, ry1, rx2, ry2;
|
|
|
|
row_rect(v, rx1, ry1, rx2, ry2);
|
|
|
|
if (is_selected(idx))
|
|
|
|
{
|
|
|
|
// Selected row: highlighted fill, swapped text colour.
|
|
|
|
XSetForeground(display_, gc_, st.outlineColor_);
|
|
|
|
XFillRectangle(display_, window_, gc_, rx1, ry1, rx2 - rx1, ry2 - ry1);
|
|
|
|
SStyle sel = st;
|
|
|
|
sel.text_color_ = styles_->get_color_rgb(frame_background_color_.r, frame_background_color_.g, frame_background_color_.b);
|
|
|
|
xf::draw_text_left(display_, window_, gc_, sel, rx1 + 8, ry1, ry2, rows_[idx]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xf::draw_text_left(display_, window_, gc_, st, rx1 + 8, ry1, ry2, rows_[idx]);
|
|
|
|
}
|
|
|
|
// Separator line under the row.
|
|
|
|
XSetForeground(display_, gc_, styles_->get_color_rgb(separator_line_color_.r, separator_line_color_.g, separator_line_color_.b));
|
|
|
|
XDrawLine(display_, window_, gc_, rx1, ry2 - 1, rx2, ry2 - 1);
|
|
|
|
}
|
|
|
|
if (focused_)
|
|
|
|
{
|
|
|
|
XSetForeground(display_, gc_, st.lineColor_);
|
|
|
|
XDrawRectangle(display_, window_, gc_, x1_ + 1, y1_ + 1, width() - 2, height() - 2);
|
|
|
|
}
|
|
|
|
XFlush(display_);
|
|
|
|
}
|
|
|
|
bool on_button_press(int x, int y, unsigned int button)
|
|
|
|
{
|
|
|
|
if (!contains(x, y) || !enabled_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
focused_ = true;
|
|
|
|
// Mouse wheel scrolls the row window.
|
|
|
|
if (button == 4)
|
|
|
|
{
|
|
|
|
scroll_offset_ -= 1;
|
|
|
|
clamp_scroll();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (button == 5)
|
|
|
|
{
|
|
|
|
scroll_offset_ += 1;
|
|
|
|
clamp_scroll();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (button != 1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (y < y1_ + inset_)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
int v = (y - y1_ - inset_) / row_height_;
|
|
|
|
int idx = scroll_offset_ + v;
|
|
|
|
if (idx >= 0 && idx < (int)rows_.size())
|
|
|
|
{
|
|
|
|
if (multi_select_)
|
|
|
|
{
|
|
|
|
auto it = std::find(selected_rows_.begin(),
|
|
selected_rows_.end(), idx);
|
|
|
|
if (it != selected_rows_.end())
|
|
|
|
selected_rows_.erase(it);
|
|
|
|
else
|
|
|
|
selected_rows_.push_back(idx);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
selected_rows_.clear();
|
|
|
|
}
|
|
|
|
selected_row_ = idx;
|
|
|
|
if (on_row_selected)
|
|
|
|
{
|
|
|
|
on_row_selected(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
(void)x;
|
|
|
|
(void)y;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool on_key(KeySym key, const char* str)
|
|
|
|
{
|
|
|
|
(void)str;
|
|
|
|
if (!focused_ || !enabled_ || rows_.empty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (key == XK_Down)
|
|
|
|
{
|
|
|
|
if (selected_row_ < (int)rows_.size() - 1)
|
|
|
|
{
|
|
|
|
selected_row_ += 1;
|
|
|
|
}
|
|
|
|
keep_selection_visible();
|
|
|
|
if (on_row_selected && selected_row_ >= 0)
|
|
|
|
{
|
|
|
|
on_row_selected(selected_row_);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_Up)
|
|
|
|
{
|
|
|
|
if (selected_row_ > 0)
|
|
|
|
{
|
|
|
|
selected_row_ -= 1;
|
|
|
|
}
|
|
|
|
keep_selection_visible();
|
|
|
|
if (on_row_selected && selected_row_ >= 0)
|
|
|
|
{
|
|
|
|
on_row_selected(selected_row_);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_Page_Down)
|
|
|
|
{
|
|
|
|
selected_row_ += visible_row_count();
|
|
|
|
if (selected_row_ >= (int)rows_.size())
|
|
|
|
{
|
|
|
|
selected_row_ = (int)rows_.size() - 1;
|
|
|
|
}
|
|
|
|
keep_selection_visible();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_Page_Up)
|
|
|
|
{
|
|
|
|
selected_row_ -= visible_row_count();
|
|
|
|
if (selected_row_ < 0)
|
|
|
|
{
|
|
|
|
selected_row_ = 0;
|
|
|
|
}
|
|
|
|
keep_selection_visible();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
//endregion
|