1324 lines
20 KiB
C++
1324 lines
20 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
|
|
//region GtkTextView
|
|
|
|
class GtkTextView
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
Display *display_;
|
|
|
|
int screen_;
|
|
|
|
Window window_;
|
|
|
|
GC gc_;
|
|
|
|
SStyle_Manager *styles_;
|
|
|
|
int x1_, y1_, x2_, y2_;
|
|
|
|
std::string name_;
|
|
|
|
std::string text_; // the whole buffer; lines split on '\n'
|
|
|
|
std::string style_name_; // "" = manager's active style
|
|
|
|
bool visible_ = true;
|
|
|
|
bool enabled_ = true;
|
|
|
|
bool focused_ = false;
|
|
|
|
int cursor_ = 0; // byte index into text_
|
|
|
|
bool cursor_visible_ = true; // show the caret when focused
|
|
|
|
int top_line_ = 0; // first visible display line
|
|
|
|
bool editable_ = true;
|
|
|
|
bool wrap_ = true;
|
|
|
|
int max_length_ = 0; // 0 = unlimited
|
|
|
|
int alignment_ = 0; // 0 = left, 1 = center, 2 = right
|
|
|
|
int scroll_bars_ = 0; // 0 = none, 1 = horizontal, 2 = vertical, 3 = both (stored; no scrollbars are drawn)
|
|
|
|
bool want_tabs_ = true;
|
|
|
|
bool want_returns_ = true;
|
|
|
|
bool hide_selection_ = false; // stored; no selection is drawn
|
|
|
|
bool blink_on_ = true;
|
|
|
|
int pad_ = 6;
|
|
|
|
int line_height()
|
|
|
|
{
|
|
|
|
SStyle st = style();
|
|
|
|
if (st.font_)
|
|
|
|
{
|
|
|
|
return st.font_->ascent + st.font_->descent + 4;
|
|
|
|
}
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
int visible_rows()
|
|
|
|
{
|
|
|
|
return std::max(1, (height() - 2 * pad_) / line_height());
|
|
|
|
}
|
|
|
|
// Build the display lines as (start, length) pairs into text_,
|
|
// wrapping each logical line at the inner pixel width.
|
|
|
|
void build_lines(std::vector<std::pair<int, int>>& out)
|
|
|
|
{
|
|
|
|
out.clear();
|
|
|
|
SStyle st = style();
|
|
|
|
int inner = width() - 2 * pad_ - 4;
|
|
|
|
int n = (int)text_.size();
|
|
|
|
int line_start = 0;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
int nl = (int)text_.find('\n', (size_t)line_start);
|
|
|
|
int line_end = (nl == -1) ? n : nl;
|
|
|
|
if (!wrap_ || inner <= 10)
|
|
|
|
{
|
|
|
|
out.push_back({line_start, line_end - line_start});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int seg_start = line_start;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
int seg_len = line_end - seg_start;
|
|
|
|
if (xf::text_width(st, text_.substr((size_t)seg_start, (size_t)seg_len)) <= inner)
|
|
|
|
{
|
|
|
|
out.push_back({seg_start, seg_len});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Find the longest fitting prefix, prefer a space break.
|
|
|
|
int fit = 0;
|
|
|
|
int last_space = -1;
|
|
|
|
for (int i = 1; i <= seg_len; ++i)
|
|
|
|
{
|
|
|
|
if (xf::text_width(st, text_.substr((size_t)seg_start, (size_t)i)) > inner)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fit = i;
|
|
|
|
if (text_[(size_t)(seg_start + i - 1)] == ' ')
|
|
|
|
{
|
|
|
|
last_space = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fit <= 0)
|
|
|
|
{
|
|
|
|
fit = 1;
|
|
|
|
}
|
|
|
|
int brk = (last_space > 0) ? last_space : fit;
|
|
|
|
out.push_back({seg_start, brk});
|
|
|
|
seg_start += brk;
|
|
|
|
if (seg_start >= line_end)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nl == -1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
line_start = nl + 1;
|
|
|
|
if (line_start > n)
|
|
|
|
{
|
|
|
|
out.push_back({n, 0});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (out.empty())
|
|
|
|
{
|
|
|
|
out.push_back({0, 0});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Display line index that contains the cursor.
|
|
|
|
int cursor_line(const std::vector<std::pair<int, int>>& lines)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < (int)lines.size(); ++i)
|
|
|
|
{
|
|
|
|
int s = lines[(size_t)i].first;
|
|
|
|
int e = s + lines[(size_t)i].second;
|
|
|
|
if (cursor_ >= s && cursor_ <= e)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (int)lines.size() - 1;
|
|
|
|
}
|
|
|
|
int line_tx(const SStyle& st, const std::string& line_text)
|
|
|
|
{
|
|
|
|
if (alignment_ == 0)
|
|
|
|
{
|
|
|
|
return x1_ + pad_;
|
|
|
|
}
|
|
|
|
int tw = xf::text_width(st, line_text);
|
|
|
|
int extra = width() - 2 * pad_ - tw;
|
|
|
|
if (extra <= 0)
|
|
|
|
{
|
|
|
|
return x1_ + pad_;
|
|
|
|
}
|
|
|
|
return x1_ + pad_ + (alignment_ == 1 ? extra / 2 : extra);
|
|
|
|
}
|
|
|
|
void ensure_cursor_visible()
|
|
|
|
{
|
|
|
|
std::vector<std::pair<int, int>> lines;
|
|
|
|
build_lines(lines);
|
|
|
|
int cl = cursor_line(lines);
|
|
|
|
if (cl < top_line_)
|
|
|
|
{
|
|
|
|
top_line_ = cl;
|
|
|
|
}
|
|
|
|
if (cl >= top_line_ + visible_rows())
|
|
|
|
{
|
|
|
|
top_line_ = cl - visible_rows() + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void changed()
|
|
|
|
{
|
|
|
|
if (on_changed)
|
|
|
|
{
|
|
|
|
on_changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Getter/setter pass: colours formerly hardcoded in draw()/helpers.
|
|
Srgb fill_color_{255, 255, 255};
|
|
Srgb disabled_fill_color_{235, 235, 235};
|
|
Srgb focus_ring_color_{60, 120, 200};
|
|
Srgb text_color_{140, 140, 140};
|
|
Srgb caret_color_{20, 20, 20};
|
|
|
|
public:
|
|
|
|
std::function<void()> on_changed;
|
|
|
|
GtkTextView(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_ = "GtkTextView";
|
|
|
|
text_ = "The quick brown fox jumps over the lazy dog.\n"
|
|
|
|
"Pack my box with five dozen liquor jugs.\n"
|
|
|
|
"\n"
|
|
|
|
"Type here: lines wrap at the window edge,\n"
|
|
|
|
"the arrow keys walk the cursor, and the\n"
|
|
|
|
"view scrolls to keep the cursor in sight.";
|
|
|
|
cursor_ = 0;
|
|
|
|
}
|
|
|
|
~GtkTextView() {}
|
|
|
|
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;
|
|
|
|
if (max_length_ > 0 && (int)text_.size() > max_length_)
|
|
|
|
{
|
|
|
|
text_ = text_.substr(0, (size_t)max_length_);
|
|
|
|
}
|
|
|
|
cursor_ = 0;
|
|
|
|
top_line_ = 0;
|
|
|
|
}
|
|
|
|
std::string get_text()
|
|
|
|
{
|
|
|
|
return text_;
|
|
|
|
}
|
|
|
|
SStyle style()
|
|
|
|
{
|
|
|
|
return styles_->resolve(style_name_);
|
|
|
|
}
|
|
|
|
//region GtkTextView specific API
|
|
|
|
void set_editable(bool e)
|
|
|
|
{
|
|
|
|
editable_ = e;
|
|
|
|
}
|
|
|
|
bool get_editable()
|
|
|
|
{
|
|
|
|
return editable_;
|
|
|
|
}
|
|
|
|
void set_wrap_mode(bool w)
|
|
|
|
{
|
|
|
|
wrap_ = w;
|
|
|
|
}
|
|
|
|
bool get_wrap_mode()
|
|
|
|
{
|
|
|
|
return wrap_;
|
|
|
|
}
|
|
|
|
int get_cursor_position()
|
|
|
|
{
|
|
|
|
return cursor_;
|
|
|
|
}
|
|
|
|
void set_cursor_position(int pos)
|
|
|
|
{
|
|
|
|
cursor_ = std::max(0, std::min(pos, (int)text_.size()));
|
|
|
|
ensure_cursor_visible();
|
|
|
|
}
|
|
|
|
void set_cursor_visible(bool v)
|
|
|
|
{
|
|
|
|
cursor_visible_ = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_cursor_visible()
|
|
|
|
{
|
|
|
|
return cursor_visible_;
|
|
|
|
}
|
|
|
|
int get_max_length()
|
|
|
|
{
|
|
|
|
return max_length_;
|
|
|
|
}
|
|
|
|
void set_max_length(int v)
|
|
|
|
{
|
|
|
|
max_length_ = v < 0 ? 0 : v;
|
|
|
|
}
|
|
|
|
int get_alignment()
|
|
|
|
{
|
|
|
|
return alignment_;
|
|
|
|
}
|
|
|
|
void set_alignment(int v)
|
|
|
|
{
|
|
|
|
alignment_ = v;
|
|
|
|
}
|
|
|
|
int get_scroll_bars()
|
|
|
|
{
|
|
|
|
return scroll_bars_;
|
|
|
|
}
|
|
|
|
void set_scroll_bars(int v)
|
|
|
|
{
|
|
|
|
scroll_bars_ = v;
|
|
|
|
}
|
|
|
|
bool get_want_tabs()
|
|
|
|
{
|
|
|
|
return want_tabs_;
|
|
|
|
}
|
|
|
|
void set_want_tabs(bool v)
|
|
|
|
{
|
|
|
|
want_tabs_ = v;
|
|
|
|
}
|
|
|
|
bool get_want_returns()
|
|
|
|
{
|
|
|
|
return want_returns_;
|
|
|
|
}
|
|
|
|
void set_want_returns(bool v)
|
|
|
|
{
|
|
|
|
want_returns_ = v;
|
|
|
|
}
|
|
|
|
bool get_hide_selection()
|
|
|
|
{
|
|
|
|
return hide_selection_;
|
|
|
|
}
|
|
|
|
void set_hide_selection(bool v)
|
|
|
|
{
|
|
|
|
hide_selection_ = v;
|
|
|
|
}
|
|
|
|
void get_cursor_line_col(int& line, int& col)
|
|
|
|
{
|
|
|
|
line = 0;
|
|
|
|
col = cursor_;
|
|
|
|
for (int i = 0; i < cursor_ && i < (int)text_.size(); ++i)
|
|
|
|
{
|
|
|
|
if (text_[(size_t)i] == '\n')
|
|
|
|
{
|
|
|
|
++line;
|
|
|
|
col = cursor_ - i - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void insert_at_cursor(const std::string& s)
|
|
|
|
{
|
|
|
|
if (!editable_)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string ins = s;
|
|
|
|
if (max_length_ > 0 && (int)text_.size() + (int)ins.size() > max_length_)
|
|
|
|
{
|
|
|
|
ins = ins.substr(0, (size_t)std::max(0, max_length_ - (int)text_.size()));
|
|
|
|
}
|
|
|
|
text_.insert((size_t)cursor_, ins);
|
|
|
|
cursor_ += (int)ins.size();
|
|
|
|
ensure_cursor_visible();
|
|
|
|
changed();
|
|
|
|
}
|
|
|
|
int get_line_count()
|
|
|
|
{
|
|
|
|
std::vector<std::pair<int, int>> lines;
|
|
|
|
build_lines(lines);
|
|
|
|
return (int)lines.size();
|
|
|
|
}
|
|
|
|
void scroll_to_top()
|
|
|
|
{
|
|
|
|
top_line_ = 0;
|
|
|
|
}
|
|
|
|
void tick()
|
|
|
|
{
|
|
|
|
if (focused_)
|
|
|
|
{
|
|
|
|
blink_on_ = !blink_on_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//endregion
|
|
|
|
//region GtkTextView colours (getter/setter pass: formerly hardcoded in draw()/helpers)
|
|
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_disabled_fill_color(Srgb c) { disabled_fill_color_ = c; }
|
|
Srgb get_disabled_fill_color() { return disabled_fill_color_; }
|
|
void set_disabled_fill_color_rgb(int r, int g, int b) { disabled_fill_color_ = Srgb{r, g, b}; }
|
|
void set_focus_ring_color(Srgb c) { focus_ring_color_ = c; }
|
|
Srgb get_focus_ring_color() { return focus_ring_color_; }
|
|
void set_focus_ring_color_rgb(int r, int g, int b) { focus_ring_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_caret_color(Srgb c) { caret_color_ = c; }
|
|
Srgb get_caret_color() { return caret_color_; }
|
|
void set_caret_color_rgb(int r, int g, int b) { caret_color_ = Srgb{r, g, b}; }
|
|
//endregion
|
|
|
|
void draw()
|
|
|
|
{
|
|
|
|
if (!visible_)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SStyle st = style();
|
|
|
|
SStyle body = st;
|
|
|
|
body.fillColor_ = styles_->get_color_rgb(fill_color_.r, fill_color_.g, fill_color_.b);
|
|
|
|
if (!enabled_)
|
|
|
|
{
|
|
|
|
body.fillColor_ = styles_->get_color_rgb(disabled_fill_color_.r, disabled_fill_color_.g, disabled_fill_color_.b);
|
|
|
|
}
|
|
|
|
xf::fill_rounded_rect(display_, window_, gc_, body, x1_, y1_, x2_, y2_);
|
|
|
|
if (focused_)
|
|
|
|
{
|
|
|
|
SStyle ring = st;
|
|
|
|
ring.lineColor_ = styles_->get_color_rgb(focus_ring_color_.r, focus_ring_color_.g, focus_ring_color_.b);
|
|
|
|
ring.lineWidth_ = 2;
|
|
|
|
xf::draw_rounded_outline(display_, window_, gc_, ring, x1_, y1_, x2_, y2_);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xf::draw_rounded_outline(display_, window_, gc_, st, x1_, y1_, x2_, y2_);
|
|
|
|
}
|
|
|
|
std::vector<std::pair<int, int>> lines;
|
|
|
|
build_lines(lines);
|
|
|
|
int lh = line_height();
|
|
|
|
int rows = visible_rows();
|
|
|
|
SStyle ink = st;
|
|
|
|
if (!enabled_)
|
|
|
|
{
|
|
|
|
ink.text_color_ = styles_->get_color_rgb(text_color_.r, text_color_.g, text_color_.b);
|
|
|
|
}
|
|
|
|
int ascent = st.font_ ? st.font_->ascent : 11;
|
|
|
|
for (int row = 0; row < rows; ++row)
|
|
|
|
{
|
|
|
|
int li = top_line_ + row;
|
|
|
|
if (li >= (int)lines.size())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int s = lines[(size_t)li].first;
|
|
|
|
int len = lines[(size_t)li].second;
|
|
|
|
int baseline = y1_ + pad_ + row * lh + ascent;
|
|
|
|
std::string lt = text_.substr((size_t)s, (size_t)len);
|
|
|
|
xf::draw_text_at(display_, window_, gc_, ink, line_tx(st, lt), baseline, lt);
|
|
|
|
}
|
|
|
|
// Cursor bar on its display line.
|
|
|
|
if (cursor_visible_ && focused_ && enabled_ && blink_on_)
|
|
|
|
{
|
|
|
|
int cl = cursor_line(lines);
|
|
|
|
int row = cl - top_line_;
|
|
|
|
if (row >= 0 && row < rows)
|
|
|
|
{
|
|
|
|
int s = lines[(size_t)cl].first;
|
|
|
|
int cx = line_tx(st, text_.substr((size_t)s, (size_t)lines[(size_t)cl].second)) + xf::text_width(st, text_.substr((size_t)s, (size_t)(cursor_ - s)));
|
|
|
|
int ty = y1_ + pad_ + row * lh;
|
|
|
|
XSetForeground(display_, gc_, styles_->get_color_rgb(caret_color_.r, caret_color_.g, caret_color_.b));
|
|
|
|
XDrawLine(display_, window_, gc_, cx, ty + 1, cx, ty + lh - 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XFlush(display_);
|
|
|
|
}
|
|
|
|
bool on_button_press(int x, int y, unsigned int button)
|
|
|
|
{
|
|
|
|
if (!contains(x, y) || !enabled_ || button != 1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
focused_ = true;
|
|
|
|
blink_on_ = true;
|
|
|
|
std::vector<std::pair<int, int>> lines;
|
|
|
|
build_lines(lines);
|
|
|
|
int row = (y - y1_ - pad_) / line_height();
|
|
|
|
int li = std::max(0, std::min(top_line_ + row, (int)lines.size() - 1));
|
|
|
|
int s = lines[(size_t)li].first;
|
|
|
|
int len = lines[(size_t)li].second;
|
|
|
|
int px = x - x1_ - pad_;
|
|
|
|
int best = 0;
|
|
|
|
int best_dist = 1 << 30;
|
|
|
|
SStyle st = style();
|
|
|
|
px = x - line_tx(st, text_.substr((size_t)s, (size_t)len));
|
|
|
|
for (int i = 0; i <= len; ++i)
|
|
|
|
{
|
|
|
|
int d = xf::text_width(st, text_.substr((size_t)s, (size_t)i)) - px;
|
|
|
|
if (d < 0)
|
|
|
|
{
|
|
|
|
d = -d;
|
|
|
|
}
|
|
|
|
if (d < best_dist)
|
|
|
|
{
|
|
|
|
best_dist = d;
|
|
|
|
best = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cursor_ = s + best;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (!focused_ || !enabled_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
blink_on_ = true;
|
|
|
|
if (key == XK_Left)
|
|
|
|
{
|
|
|
|
if (cursor_ > 0)
|
|
|
|
{
|
|
|
|
--cursor_;
|
|
|
|
}
|
|
|
|
ensure_cursor_visible();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_Right)
|
|
|
|
{
|
|
|
|
if (cursor_ < (int)text_.size())
|
|
|
|
{
|
|
|
|
++cursor_;
|
|
|
|
}
|
|
|
|
ensure_cursor_visible();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_Home)
|
|
|
|
{
|
|
|
|
while (cursor_ > 0 && text_[(size_t)(cursor_ - 1)] != '\n')
|
|
|
|
{
|
|
|
|
--cursor_;
|
|
|
|
}
|
|
|
|
ensure_cursor_visible();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_End)
|
|
|
|
{
|
|
|
|
while (cursor_ < (int)text_.size() && text_[(size_t)cursor_] != '\n')
|
|
|
|
{
|
|
|
|
++cursor_;
|
|
|
|
}
|
|
|
|
ensure_cursor_visible();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_Up || key == XK_Down)
|
|
|
|
{
|
|
|
|
// Move by logical line, keeping the column where possible.
|
|
|
|
int line, col;
|
|
|
|
get_cursor_line_col(line, col);
|
|
|
|
int target = (key == XK_Up) ? line - 1 : line + 1;
|
|
|
|
// Find the start of the target logical line.
|
|
|
|
int pos = 0;
|
|
|
|
int cur = 0;
|
|
|
|
while (cur < target && pos < (int)text_.size())
|
|
|
|
{
|
|
|
|
if (text_[(size_t)pos] == '\n')
|
|
|
|
{
|
|
|
|
++cur;
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
if (cur == target)
|
|
|
|
{
|
|
|
|
int end = pos;
|
|
|
|
while (end < (int)text_.size() && text_[(size_t)end] != '\n')
|
|
|
|
{
|
|
|
|
++end;
|
|
|
|
}
|
|
|
|
cursor_ = std::min(pos + col, end);
|
|
|
|
ensure_cursor_visible();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_Page_Up)
|
|
|
|
{
|
|
|
|
top_line_ = std::max(0, top_line_ - visible_rows());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_Page_Down)
|
|
|
|
{
|
|
|
|
std::vector<std::pair<int, int>> lines;
|
|
|
|
build_lines(lines);
|
|
|
|
top_line_ = std::min(std::max(0, (int)lines.size() - visible_rows()), top_line_ + visible_rows());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!editable_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (key == XK_Return || key == XK_KP_Enter)
|
|
|
|
{
|
|
|
|
if (!want_returns_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (max_length_ > 0 && (int)text_.size() >= max_length_)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
text_.insert((size_t)cursor_, 1, '\n');
|
|
|
|
++cursor_;
|
|
|
|
ensure_cursor_visible();
|
|
|
|
changed();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_BackSpace)
|
|
|
|
{
|
|
|
|
if (cursor_ > 0)
|
|
|
|
{
|
|
|
|
text_.erase((size_t)(cursor_ - 1), 1);
|
|
|
|
--cursor_;
|
|
|
|
ensure_cursor_visible();
|
|
|
|
changed();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_Delete)
|
|
|
|
{
|
|
|
|
if (cursor_ < (int)text_.size())
|
|
|
|
{
|
|
|
|
text_.erase((size_t)cursor_, 1);
|
|
|
|
changed();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_Tab)
|
|
|
|
{
|
|
|
|
if (!want_tabs_)
|
|
|
|
{
|
|
|
|
return false; // let Tab move focus, like the real widget
|
|
|
|
}
|
|
|
|
if (max_length_ > 0 && (int)text_.size() >= max_length_)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
text_.insert((size_t)cursor_, 1, '\t');
|
|
|
|
++cursor_;
|
|
|
|
ensure_cursor_visible();
|
|
|
|
changed();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (str && str[0] >= 32 && str[0] != 127)
|
|
|
|
{
|
|
|
|
for (int i = 0; str[i]; ++i)
|
|
|
|
{
|
|
|
|
if (str[i] < 32 || str[i] == 127)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (max_length_ > 0 && (int)text_.size() >= max_length_)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
text_.insert((size_t)cursor_, 1, str[i]);
|
|
|
|
++cursor_;
|
|
|
|
}
|
|
|
|
ensure_cursor_visible();
|
|
|
|
changed();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
//endregion
|