914 lines
15 KiB
C++
914 lines
15 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
|
|
//region GtkListView
|
|
|
|
class GtkListView
|
|
|
|
{
|
|
|
|
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> items_;
|
|
|
|
std::vector<bool> selected_;
|
|
|
|
int anchor_row_ = -1;
|
|
|
|
int scroll_row_ = 0;
|
|
|
|
int row_height_ = 22;
|
|
|
|
// Phase 3 (§2.17): ViewStyle / GridLines / RowSelect /
|
|
// MultiSelect / SortType.
|
|
int view_style_ = 0; // 0 report, 1 icon, 2 small icon, 3 list
|
|
|
|
bool grid_lines_ = false;
|
|
|
|
bool row_select_ = true;
|
|
|
|
bool multi_select_ = false;
|
|
|
|
int sort_type_ = 0; // 0 none, 1 ascending, 2 descending
|
|
|
|
bool dragging_ = false;
|
|
|
|
int band_x0_ = 0;
|
|
|
|
int band_y0_ = 0;
|
|
|
|
int band_x1_ = 0;
|
|
|
|
int band_y1_ = 0;
|
|
|
|
int visible_rows()
|
|
|
|
{
|
|
|
|
return std::max(1, (height() - 4) / row_height_);
|
|
|
|
}
|
|
|
|
int row_at(int y)
|
|
|
|
{
|
|
|
|
if (y < y1_ + 2)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int index = scroll_row_ + (y - y1_ - 2) / row_height_;
|
|
|
|
if (index < 0 || index >= (int)items_.size())
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
// Getter/setter pass: colours formerly hardcoded in draw()/helpers.
|
|
Srgb selection_fill_color_{70, 110, 180};
|
|
Srgb band_fill_color_{150, 180, 230};
|
|
Srgb text_color_{255, 255, 255};
|
|
|
|
public:
|
|
|
|
std::function<void()> on_selection_changed;
|
|
|
|
GtkListView(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_ = "GtkListView";
|
|
|
|
text_ = "ListView";
|
|
|
|
add_item("Alpha");
|
|
|
|
add_item("Bravo");
|
|
|
|
add_item("Charlie");
|
|
|
|
add_item("Delta");
|
|
|
|
add_item("Echo");
|
|
|
|
add_item("Foxtrot");
|
|
|
|
add_item("Golf");
|
|
|
|
add_item("Hotel");
|
|
|
|
if (!selected_.empty())
|
|
|
|
{
|
|
|
|
selected_[0] = true;
|
|
|
|
anchor_row_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~GtkListView() {}
|
|
|
|
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 List specific API
|
|
|
|
void add_item(std::string item)
|
|
|
|
{
|
|
|
|
items_.push_back(item);
|
|
|
|
selected_.push_back(false);
|
|
|
|
}
|
|
|
|
void clear_items()
|
|
|
|
{
|
|
|
|
items_.clear();
|
|
|
|
selected_.clear();
|
|
|
|
anchor_row_ = -1;
|
|
|
|
scroll_row_ = 0;
|
|
|
|
}
|
|
|
|
int item_count()
|
|
|
|
{
|
|
|
|
return (int)items_.size();
|
|
|
|
}
|
|
|
|
std::string get_item(int index)
|
|
|
|
{
|
|
|
|
if (index >= 0 && index < (int)items_.size())
|
|
|
|
{
|
|
|
|
return items_[index];
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
void select_row(int index)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < selected_.size(); ++i)
|
|
|
|
{
|
|
|
|
selected_[i] = false;
|
|
|
|
}
|
|
|
|
if (index >= 0 && index < (int)selected_.size())
|
|
|
|
{
|
|
|
|
selected_[index] = true;
|
|
|
|
anchor_row_ = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::vector<int> get_selected_rows()
|
|
|
|
{
|
|
|
|
std::vector<int> out;
|
|
|
|
for (size_t i = 0; i < selected_.size(); ++i)
|
|
|
|
{
|
|
|
|
if (selected_[i])
|
|
|
|
{
|
|
|
|
out.push_back((int)i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
int get_selected_row()
|
|
|
|
{
|
|
|
|
std::vector<int> rows = get_selected_rows();
|
|
|
|
return rows.empty() ? -1 : rows[0];
|
|
|
|
}
|
|
|
|
void set_selected_row(int r)
|
|
|
|
{
|
|
|
|
select_row(r);
|
|
|
|
}
|
|
|
|
// Phase 3 (§2.17): rebuild items from "Row 1;Row 2" text.
|
|
|
|
void set_items(const std::string& items)
|
|
|
|
{
|
|
|
|
clear_items();
|
|
|
|
size_t pos = 0;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
size_t semi = items.find(';', pos);
|
|
|
|
add_item(items.substr(pos, semi == std::string::npos ? semi : semi - pos));
|
|
|
|
if (semi == std::string::npos) break;
|
|
|
|
pos = semi + 1;
|
|
|
|
}
|
|
|
|
if (!selected_.empty())
|
|
|
|
{
|
|
|
|
selected_[0] = true;
|
|
|
|
anchor_row_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void set_scroll_row(int row)
|
|
|
|
{
|
|
|
|
scroll_row_ = std::max(0, row);
|
|
|
|
}
|
|
|
|
//endregion
|
|
|
|
//region GtkListView colours (getter/setter pass: formerly hardcoded in draw()/helpers)
|
|
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}; }
|
|
void set_band_fill_color(Srgb c) { band_fill_color_ = c; }
|
|
Srgb get_band_fill_color() { return band_fill_color_; }
|
|
void set_band_fill_color_rgb(int r, int g, int b) { band_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}; }
|
|
//endregion
|
|
//region GtkListView properties (getter/setter pass)
|
|
void set_row_height(int v) { row_height_ = v; }
|
|
int get_row_height() { return row_height_; }
|
|
void set_selected(const std::vector<bool>& v) { selected_ = v; }
|
|
const std::vector<bool>& get_selected() { return selected_; }
|
|
|
|
//region GtkListView view properties (Phase 3, §2.17)
|
|
void set_view_style(int v) { view_style_ = v; }
|
|
int get_view_style() { return view_style_; }
|
|
void set_grid_lines(bool v) { grid_lines_ = v; }
|
|
bool get_grid_lines() { return grid_lines_; }
|
|
void set_row_select(bool v) { row_select_ = v; }
|
|
bool get_row_select() { return row_select_; }
|
|
void set_multi_select(bool v) { multi_select_ = v; }
|
|
bool get_multi_select() { return multi_select_; }
|
|
void set_sort_type(int s)
|
|
{
|
|
sort_type_ = s;
|
|
if (sort_type_ == 0) return;
|
|
std::vector<std::pair<std::string, bool>> pairs;
|
|
for (size_t i = 0; i < items_.size(); ++i)
|
|
{
|
|
pairs.push_back({items_[i], selected_[i]});
|
|
}
|
|
std::sort(pairs.begin(), pairs.end(),
|
|
[this](const std::pair<std::string, bool>& a,
|
|
const std::pair<std::string, bool>& b) {
|
|
return sort_type_ == 1 ? a.first < b.first
|
|
: a.first > b.first;
|
|
});
|
|
for (size_t i = 0; i < pairs.size(); ++i)
|
|
{
|
|
items_[i] = pairs[i].first;
|
|
selected_[i] = pairs[i].second;
|
|
}
|
|
}
|
|
int get_sort_type() { return sort_type_; }
|
|
//endregion
|
|
// Icon grid (1) and multi-column list (3) views.
|
|
void draw_alt_view(unsigned long sel_fill)
|
|
{
|
|
SStyle st = style();
|
|
if (view_style_ == 1)
|
|
{
|
|
int cw = 76, ch = 60;
|
|
int cols = std::max(1, (width() - 4) / cw);
|
|
for (int i = scroll_row_; i < (int)items_.size(); ++i)
|
|
{
|
|
int pos = i - scroll_row_;
|
|
int cx = x1_ + 2 + (pos % cols) * cw;
|
|
int cy = y1_ + 2 + (pos / cols) * ch;
|
|
if (cy + ch > y2_ - 2) break;
|
|
if (selected_[i])
|
|
{
|
|
XSetForeground(display_, gc_, sel_fill);
|
|
XFillRectangle(display_, window_, gc_, cx, cy, cw - 2, ch - 2);
|
|
}
|
|
XSetForeground(display_, gc_, st.lineColor_);
|
|
XDrawRectangle(display_, window_, gc_, cx + cw / 2 - 12, cy + 4, 24, 24);
|
|
xf::draw_text_centered(display_, window_, gc_, st, cx, cy + 30, cx + cw - 2, cy + ch, items_[i]);
|
|
}
|
|
return;
|
|
}
|
|
int col_w = 150;
|
|
int cols = std::max(1, (width() - 4) / col_w);
|
|
int rows_per_col = std::max(1, (height() - 4) / row_height_);
|
|
for (int i = scroll_row_; i < (int)items_.size(); ++i)
|
|
{
|
|
int pos = i - scroll_row_;
|
|
int col = pos / rows_per_col;
|
|
int row = pos % rows_per_col;
|
|
if (col >= cols) break;
|
|
int cx = x1_ + 2 + col * col_w;
|
|
int ry = y1_ + 2 + row * row_height_;
|
|
if (selected_[i])
|
|
{
|
|
XSetForeground(display_, gc_, sel_fill);
|
|
XFillRectangle(display_, window_, gc_, cx, ry, col_w - 2, row_height_);
|
|
}
|
|
xf::draw_text_left(display_, window_, gc_, st, cx + 6, ry, ry + row_height_, items_[i]);
|
|
}
|
|
}
|
|
//endregion
|
|
|
|
void draw()
|
|
|
|
{
|
|
|
|
if (!visible_)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SStyle st = style();
|
|
|
|
xf::draw_polished(display_, window_, gc_, st, x1_, y1_, x2_, y2_);
|
|
|
|
unsigned long sel_fill = styles_->get_color_rgb(selection_fill_color_.r, selection_fill_color_.g, selection_fill_color_.b);
|
|
|
|
unsigned long band_fill = styles_->get_color_rgb(band_fill_color_.r, band_fill_color_.g, band_fill_color_.b);
|
|
|
|
// Phase 3 (§2.17): icon / list views branch out of the report loop.
|
|
|
|
if (view_style_ == 1 || view_style_ == 3)
|
|
|
|
{
|
|
|
|
draw_alt_view(sel_fill);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for (int i = scroll_row_; i < (int)items_.size(); ++i)
|
|
|
|
{
|
|
|
|
int ry = y1_ + 2 + (i - scroll_row_) * row_height_;
|
|
|
|
if (ry + row_height_ > y2_ - 2)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SStyle row_style = st;
|
|
|
|
if (selected_[i])
|
|
|
|
{
|
|
|
|
XSetForeground(display_, gc_, sel_fill);
|
|
|
|
XFillRectangle(display_, window_, gc_, x1_ + 2, ry, width() - 4, row_height_);
|
|
|
|
row_style.text_color_ = styles_->get_color_rgb(text_color_.r, text_color_.g, text_color_.b);
|
|
|
|
}
|
|
|
|
if (view_style_ == 2)
|
|
|
|
{
|
|
|
|
XSetForeground(display_, gc_, st.lineColor_);
|
|
|
|
XDrawRectangle(display_, window_, gc_, x1_ + 8, ry + (row_height_ - 14) / 2, 14, 14);
|
|
|
|
xf::draw_text_left(display_, window_, gc_, row_style, x1_ + 26, ry, ry + row_height_, items_[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xf::draw_text_left(display_, window_, gc_, row_style, x1_ + 8, ry, ry + row_height_, items_[i]);
|
|
|
|
}
|
|
|
|
if (grid_lines_)
|
|
|
|
{
|
|
|
|
XSetForeground(display_, gc_, st.lineColor_);
|
|
|
|
XDrawLine(display_, window_, gc_, x1_ + 2, ry + row_height_, x2_ - 2, ry + row_height_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Rubber band while dragging.
|
|
|
|
if (dragging_)
|
|
|
|
{
|
|
|
|
int bx = std::min(band_x0_, band_x1_);
|
|
|
|
int by = std::min(band_y0_, band_y1_);
|
|
|
|
int bw = std::abs(band_x1_ - band_x0_);
|
|
|
|
int bh = std::abs(band_y1_ - band_y0_);
|
|
|
|
XSetForeground(display_, gc_, band_fill);
|
|
|
|
XDrawRectangle(display_, window_, gc_, bx, by, bw, bh);
|
|
|
|
}
|
|
|
|
if (focused_)
|
|
|
|
{
|
|
|
|
XSetForeground(display_, gc_, st.lineColor_);
|
|
|
|
XDrawRectangle(display_, window_, gc_, x1_ + 2, y1_ + 2, width() - 4, height() - 4);
|
|
|
|
}
|
|
|
|
XFlush(display_);
|
|
|
|
}
|
|
|
|
bool on_button_press(int x, int y, unsigned int button)
|
|
|
|
{
|
|
|
|
if (!contains(x, y) || !enabled_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
focused_ = true;
|
|
|
|
if (button == 4)
|
|
|
|
{
|
|
|
|
scroll_row_ = std::max(0, scroll_row_ - 3);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (button == 5)
|
|
|
|
{
|
|
|
|
scroll_row_ += 3;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (button != 1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int row = row_at(y);
|
|
|
|
// Phase 3 (§2.17): multi-select toggles; single-select replaces.
|
|
|
|
if (multi_select_ && row >= 0 && selected_[row])
|
|
|
|
{
|
|
|
|
selected_[row] = false;
|
|
|
|
anchor_row_ = row;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!multi_select_)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < selected_.size(); ++i)
|
|
|
|
{
|
|
|
|
selected_[i] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (row >= 0)
|
|
|
|
{
|
|
|
|
selected_[row] = true;
|
|
|
|
anchor_row_ = row;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dragging_ = true;
|
|
|
|
band_x0_ = x;
|
|
|
|
band_y0_ = y;
|
|
|
|
band_x1_ = x;
|
|
|
|
band_y1_ = y;
|
|
|
|
if (on_selection_changed)
|
|
|
|
{
|
|
|
|
on_selection_changed();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool on_button_release(int x, int y, unsigned int button)
|
|
|
|
{
|
|
|
|
(void)x;
|
|
|
|
(void)y;
|
|
|
|
if (button != 1 || !dragging_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
dragging_ = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool on_motion(int x, int y)
|
|
|
|
{
|
|
|
|
if (!dragging_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
band_x1_ = x;
|
|
|
|
band_y1_ = y;
|
|
|
|
// Select every row the band touches.
|
|
|
|
int top = std::min(band_y0_, band_y1_);
|
|
|
|
int bottom = std::max(band_y0_, band_y1_);
|
|
|
|
for (size_t i = 0; i < selected_.size(); ++i)
|
|
|
|
{
|
|
|
|
selected_[i] = false;
|
|
|
|
}
|
|
|
|
for (int ry = top; ry <= bottom; ry += 4)
|
|
|
|
{
|
|
|
|
int row = row_at(ry);
|
|
|
|
if (row >= 0)
|
|
|
|
{
|
|
|
|
selected_[row] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int last = row_at(bottom);
|
|
|
|
if (last >= 0)
|
|
|
|
{
|
|
|
|
selected_[last] = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool on_key(KeySym key, const char* str)
|
|
|
|
{
|
|
|
|
(void)str;
|
|
|
|
if (!focused_ || !enabled_ || items_.empty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int current = anchor_row_ < 0 ? 0 : anchor_row_;
|
|
|
|
if (key == XK_Down && current < (int)items_.size() - 1)
|
|
|
|
{
|
|
|
|
select_row(current + 1);
|
|
|
|
}
|
|
|
|
else if (key == XK_Up && current > 0)
|
|
|
|
{
|
|
|
|
select_row(current - 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (anchor_row_ < scroll_row_)
|
|
|
|
{
|
|
|
|
scroll_row_ = anchor_row_;
|
|
|
|
}
|
|
|
|
if (anchor_row_ >= scroll_row_ + visible_rows())
|
|
|
|
{
|
|
|
|
scroll_row_ = anchor_row_ - visible_rows() + 1;
|
|
|
|
}
|
|
|
|
if (on_selection_changed)
|
|
|
|
{
|
|
|
|
on_selection_changed();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
//endregion
|