Files

680 lines
9.5 KiB
C++

#pragma once
#include "core/xframe_core.h"
//region GtkLabel
class GtkLabel
{
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;
double xalign_ = 0.0; // 0 = left, 0.5 = centre, 1 = right
double yalign_ = 0.5;
bool wrap_ = false;
bool selectable_ = false;
bool transparent_ = false; // labels never fill a background; stored for the model
bool show_accel_char_ = true;
std::string focus_control_;
int selection_start_ = -1; // character offsets when selectable_
int selection_end_ = -1;
// Getter/setter pass: colours formerly hardcoded in draw()/helpers.
Srgb text_color_{140, 140, 140};
Srgb fill_color_{160, 190, 230};
public:
GtkLabel(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_ = "GtkLabel";
text_ = "Label";
}
~GtkLabel() {}
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;
selection_start_ = -1;
selection_end_ = -1;
}
std::string get_text()
{
return text_;
}
SStyle style()
{
return styles_->resolve(style_name_);
}
//region GtkLabel API
void set_xalign(double a)
{
xalign_ = std::max(0.0, std::min(1.0, a));
}
double get_xalign()
{
return xalign_;
}
void set_yalign(double a)
{
yalign_ = std::max(0.0, std::min(1.0, a));
}
double get_yalign()
{
return yalign_;
}
void set_wrap(bool w)
{
wrap_ = w;
}
bool get_wrap()
{
return wrap_;
}
void set_selectable(bool s)
{
selectable_ = s;
if (!s)
{
selection_start_ = -1;
selection_end_ = -1;
}
}
bool get_selectable()
{
return selectable_;
}
std::string get_selected_text()
{
if (selection_start_ < 0 || selection_end_ <= selection_start_)
{
return "";
}
return text_.substr((size_t)selection_start_, (size_t)(selection_end_ - selection_start_));
}
std::string display_text()
{
if (!show_accel_char_)
{
return text_;
}
std::string out;
for (size_t i = 0; i < text_.size(); ++i)
{
if (text_[i] == '&' && i + 1 < text_.size() && text_[i + 1] == '&')
{
out += '&';
++i;
}
else if (text_[i] == '&')
{
continue;
}
else
{
out += text_[i];
}
}
return out;
}
// Split text_ into lines that fit the widget width (word wrap).
std::vector<std::string> wrap_lines(const SStyle& st)
{
std::vector<std::string> lines;
std::string dt = display_text();
if (!wrap_)
{
lines.push_back(dt);
return lines;
}
std::string line;
size_t pos = 0;
while (pos <= dt.size())
{
size_t sp = dt.find(' ', pos);
std::string word = dt.substr(pos, sp == std::string::npos ? std::string::npos : sp - pos);
std::string trial = line.empty() ? word : line + " " + word;
if (!line.empty() && xf::text_width(st, trial) > width() - 4)
{
lines.push_back(line);
line = word;
}
else
{
line = trial;
}
if (sp == std::string::npos)
{
break;
}
pos = sp + 1;
}
lines.push_back(line);
return lines;
}
//endregion
bool get_transparent()
{
return transparent_;
}
void set_transparent(bool v)
{
transparent_ = v;
}
bool get_show_accel_char()
{
return show_accel_char_;
}
void set_show_accel_char(bool v)
{
show_accel_char_ = v;
}
std::string get_focus_control()
{
return focus_control_;
}
void set_focus_control(std::string v)
{
focus_control_ = v;
}
//region GtkLabel 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}; }
//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);
}
std::vector<std::string> lines = wrap_lines(st);
int line_h = st.font_ ? (st.font_->ascent + st.font_->descent + 2) : 14;
int block_h = line_h * (int)lines.size();
int top = y1_ + (int)((height() - block_h) * yalign_);
for (size_t i = 0; i < lines.size(); ++i)
{
int ly1 = top + (int)i * line_h;
int ly2 = ly1 + line_h;
if (ly2 > y2_)
{
break;
}
int tw = xf::text_width(st, lines[i]);
int tx = x1_ + 2 + (int)((width() - 4 - tw) * xalign_);
// Selection highlight behind the selected characters.
if (selectable_ && selection_start_ >= 0 && selection_end_ > selection_start_ && lines.size() == 1)
{
std::string before = text_.substr(0, (size_t)selection_start_);
std::string sel = get_selected_text();
int sx = tx + xf::text_width(st, before);
int sw = xf::text_width(st, sel);
XSetForeground(display_, gc_, styles_->get_color_rgb(fill_color_.r, fill_color_.g, fill_color_.b));
XFillRectangle(display_, window_, gc_, sx, ly1 + 1, sw, line_h - 2);
}
xf::draw_text_at(display_, window_, gc_, st, tx, ly1 + (st.font_ ? st.font_->ascent : 11), lines[i]);
}
XFlush(display_);
}
bool on_button_press(int x, int y, unsigned int button)
{
if (!contains(x, y) || button != 1)
{
return false;
}
focused_ = true;
if (selectable_)
{
// Start a selection at the start of the text for the demo:
// dragging extends it in on_motion via character hit test.
selection_start_ = char_at(x);
selection_end_ = selection_start_;
return true;
}
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)
{
if (selectable_ && selection_start_ >= 0 && contains(x, y))
{
selection_end_ = char_at(x);
if (selection_end_ < selection_start_)
{
int swap = selection_start_;
selection_start_ = selection_end_;
selection_end_ = swap;
}
return true;
}
return false;
}
bool on_key(KeySym key, const char* str)
{
(void)key;
(void)str;
return false;
}
private:
// Which character index sits under pixel x (single-line labels).
int char_at(int x)
{
SStyle st = style();
int tw = xf::text_width(st, text_);
int tx = x1_ + 2 + (int)((width() - 4 - tw) * xalign_);
int best = 0;
int best_dist = 1 << 30;
for (size_t i = 0; i <= text_.size(); ++i)
{
int cx = tx + xf::text_width(st, text_.substr(0, i));
int d = std::abs(cx - x);
if (d < best_dist)
{
best_dist = d;
best = (int)i;
}
}
return best;
}
};
//endregion