1255 lines
18 KiB
C++
1255 lines
18 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
|
|
//region GtkEntry
|
|
|
|
class GtkEntry
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
Display *display_;
|
|
|
|
int screen_;
|
|
|
|
Window window_;
|
|
|
|
GC gc_;
|
|
|
|
SStyle_Manager *styles_;
|
|
|
|
int x1_, y1_, x2_, y2_;
|
|
|
|
std::string name_;
|
|
|
|
std::string text_; // the real text buffer
|
|
|
|
std::string style_name_; // "" = manager's active style
|
|
|
|
bool visible_ = true;
|
|
|
|
bool enabled_ = true;
|
|
|
|
bool focused_ = false;
|
|
|
|
std::string placeholder_;
|
|
|
|
int cursor_ = 0; // insertion point, 0..text_.size()
|
|
|
|
int sel_anchor_ = 0; // selection anchor; selection is anchor..cursor_
|
|
|
|
int scroll_px_ = 0; // horizontal scroll offset in pixels
|
|
|
|
int max_length_ = 0; // 0 = unlimited
|
|
|
|
bool editable_ = true;
|
|
|
|
bool password_ = false; // mask the text with '*' (EchoMode)
|
|
|
|
bool show_password_ = false; // reveal the real text when set
|
|
|
|
int alignment_ = 0; // 0 = left, 1 = center, 2 = right
|
|
|
|
int char_case_ = 0; // 0 = normal, 1 = upper, 2 = lower
|
|
|
|
bool numbers_only_ = false;
|
|
|
|
bool hide_selection_ = false;
|
|
|
|
bool auto_select_ = false;
|
|
|
|
bool dragging_ = false; // mouse held down, extending the selection
|
|
|
|
bool blink_on_ = true; // cursor blink phase
|
|
|
|
// Pixel x of the text boundary at char index i, from text start.
|
|
|
|
int char_x(int i)
|
|
|
|
{
|
|
|
|
if (i <= 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return xf::text_width(style(), text_.substr(0, (size_t)i));
|
|
|
|
}
|
|
|
|
int inner_width()
|
|
|
|
{
|
|
|
|
return width() - 16; // 8 px padding each side
|
|
|
|
}
|
|
|
|
void ensure_cursor_visible()
|
|
|
|
{
|
|
|
|
int cx = char_x(cursor_);
|
|
|
|
if (cx - scroll_px_ > inner_width())
|
|
|
|
{
|
|
|
|
scroll_px_ = cx - inner_width();
|
|
|
|
}
|
|
|
|
if (cx < scroll_px_)
|
|
|
|
{
|
|
|
|
scroll_px_ = cx;
|
|
|
|
}
|
|
|
|
if (scroll_px_ < 0)
|
|
|
|
{
|
|
|
|
scroll_px_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int index_at_pixel(int px) // px relative to the text start
|
|
|
|
{
|
|
|
|
int best = 0;
|
|
|
|
int best_dist = px < 0 ? -px : px;
|
|
|
|
for (int i = 1; i <= (int)text_.size(); ++i)
|
|
|
|
{
|
|
|
|
int d = char_x(i) - px;
|
|
|
|
if (d < 0)
|
|
|
|
{
|
|
|
|
d = -d;
|
|
|
|
}
|
|
|
|
if (d < best_dist)
|
|
|
|
{
|
|
|
|
best_dist = d;
|
|
|
|
best = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
std::string filter_text(const std::string& t)
|
|
|
|
{
|
|
|
|
std::string out;
|
|
|
|
for (size_t i = 0; i < t.size(); ++i)
|
|
|
|
{
|
|
|
|
char c = t[i];
|
|
|
|
if (numbers_only_ && (c < '0' || c > '9'))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (char_case_ == 1 && c >= 'a' && c <= 'z')
|
|
|
|
{
|
|
|
|
c = (char)(c - 'a' + 'A');
|
|
|
|
}
|
|
|
|
else if (char_case_ == 2 && c >= 'A' && c <= 'Z')
|
|
|
|
{
|
|
|
|
c = (char)(c - 'A' + 'a');
|
|
|
|
}
|
|
|
|
out += c;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
int text_x()
|
|
|
|
{
|
|
|
|
int tx = x1_ + 8;
|
|
|
|
if (alignment_ > 0 && !text_.empty())
|
|
|
|
{
|
|
|
|
int tw = char_x((int)text_.size());
|
|
|
|
int extra = inner_width() - tw;
|
|
|
|
if (extra > 0)
|
|
|
|
{
|
|
|
|
tx += (alignment_ == 1) ? extra / 2 : extra;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
bool has_selection()
|
|
|
|
{
|
|
|
|
return sel_anchor_ != cursor_;
|
|
|
|
}
|
|
|
|
void delete_selection()
|
|
|
|
{
|
|
|
|
int a = std::min(sel_anchor_, cursor_);
|
|
|
|
int b = std::max(sel_anchor_, cursor_);
|
|
|
|
text_.erase((size_t)a, (size_t)(b - a));
|
|
|
|
cursor_ = a;
|
|
|
|
sel_anchor_ = a;
|
|
|
|
}
|
|
|
|
// 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 selection_fill_color_{180, 205, 245};
|
|
Srgb placeholder_text_color_{150, 150, 150};
|
|
Srgb text_color_{140, 140, 140};
|
|
Srgb caret_color_{20, 20, 20};
|
|
|
|
public:
|
|
|
|
std::function<void()> on_changed;
|
|
|
|
std::function<void()> on_activate;
|
|
|
|
GtkEntry(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_ = "GtkEntry";
|
|
|
|
text_ = "Sample text";
|
|
|
|
placeholder_ = "Type here...";
|
|
|
|
cursor_ = (int)text_.size();
|
|
|
|
sel_anchor_ = cursor_;
|
|
|
|
}
|
|
|
|
~GtkEntry() {}
|
|
|
|
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_ = filter_text(t);
|
|
|
|
if (max_length_ > 0 && (int)text_.size() > max_length_)
|
|
|
|
{
|
|
|
|
text_ = text_.substr(0, (size_t)max_length_);
|
|
|
|
}
|
|
|
|
cursor_ = (int)text_.size();
|
|
|
|
sel_anchor_ = cursor_;
|
|
|
|
scroll_px_ = 0;
|
|
|
|
ensure_cursor_visible();
|
|
|
|
}
|
|
|
|
std::string get_text()
|
|
|
|
{
|
|
|
|
return text_;
|
|
|
|
}
|
|
|
|
int get_alignment()
|
|
|
|
{
|
|
|
|
return alignment_;
|
|
|
|
}
|
|
|
|
void set_alignment(int v)
|
|
|
|
{
|
|
|
|
alignment_ = v;
|
|
|
|
}
|
|
|
|
int get_char_case()
|
|
|
|
{
|
|
|
|
return char_case_;
|
|
|
|
}
|
|
|
|
void set_char_case(int v)
|
|
|
|
{
|
|
|
|
char_case_ = v;
|
|
|
|
}
|
|
|
|
bool get_numbers_only()
|
|
|
|
{
|
|
|
|
return numbers_only_;
|
|
|
|
}
|
|
|
|
void set_numbers_only(bool v)
|
|
|
|
{
|
|
|
|
numbers_only_ = v;
|
|
|
|
}
|
|
|
|
bool get_hide_selection()
|
|
|
|
{
|
|
|
|
return hide_selection_;
|
|
|
|
}
|
|
|
|
void set_hide_selection(bool v)
|
|
|
|
{
|
|
|
|
hide_selection_ = v;
|
|
|
|
}
|
|
|
|
bool get_auto_select()
|
|
|
|
{
|
|
|
|
return auto_select_;
|
|
|
|
}
|
|
|
|
void set_auto_select(bool v)
|
|
|
|
{
|
|
|
|
auto_select_ = v;
|
|
|
|
}
|
|
|
|
SStyle style()
|
|
|
|
{
|
|
|
|
return styles_->resolve(style_name_);
|
|
|
|
}
|
|
|
|
//region GtkEntry specific API
|
|
|
|
void set_placeholder_text(std::string p)
|
|
|
|
{
|
|
|
|
placeholder_ = p;
|
|
|
|
}
|
|
|
|
std::string get_placeholder_text()
|
|
|
|
{
|
|
|
|
return placeholder_;
|
|
|
|
}
|
|
|
|
void set_max_length(int m)
|
|
|
|
{
|
|
|
|
max_length_ = m;
|
|
|
|
}
|
|
|
|
int get_max_length()
|
|
|
|
{
|
|
|
|
return max_length_;
|
|
|
|
}
|
|
|
|
void set_editable(bool e)
|
|
|
|
{
|
|
|
|
editable_ = e;
|
|
|
|
}
|
|
|
|
void set_password(bool p)
|
|
|
|
{
|
|
|
|
password_ = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_password()
|
|
|
|
{
|
|
|
|
return password_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_show_password(bool s)
|
|
|
|
{
|
|
|
|
show_password_ = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_show_password()
|
|
|
|
{
|
|
|
|
return show_password_;
|
|
|
|
}
|
|
|
|
bool get_editable()
|
|
|
|
{
|
|
|
|
return editable_;
|
|
|
|
}
|
|
|
|
int get_cursor_position()
|
|
|
|
{
|
|
|
|
return cursor_;
|
|
|
|
}
|
|
|
|
void set_cursor_position(int pos)
|
|
|
|
{
|
|
|
|
cursor_ = std::max(0, std::min(pos, (int)text_.size()));
|
|
|
|
sel_anchor_ = cursor_;
|
|
|
|
ensure_cursor_visible();
|
|
|
|
}
|
|
|
|
void select_region(int start, int end)
|
|
|
|
{
|
|
|
|
sel_anchor_ = std::max(0, std::min(start, (int)text_.size()));
|
|
|
|
cursor_ = std::max(0, std::min(end, (int)text_.size()));
|
|
|
|
ensure_cursor_visible();
|
|
|
|
}
|
|
|
|
bool get_selection_bounds(int& start, int& end)
|
|
|
|
{
|
|
|
|
if (!has_selection())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
start = std::min(sel_anchor_, cursor_);
|
|
|
|
end = std::max(sel_anchor_, cursor_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
std::string get_selected_text()
|
|
|
|
{
|
|
|
|
int a = std::min(sel_anchor_, cursor_);
|
|
|
|
int b = std::max(sel_anchor_, cursor_);
|
|
|
|
return text_.substr((size_t)a, (size_t)(b - a));
|
|
|
|
}
|
|
|
|
void delete_selected_text()
|
|
|
|
{
|
|
|
|
if (has_selection() && editable_)
|
|
|
|
{
|
|
|
|
delete_selection();
|
|
|
|
if (on_changed)
|
|
|
|
{
|
|
|
|
on_changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Called by the host's timer to blink the cursor.
|
|
|
|
void tick()
|
|
|
|
{
|
|
|
|
if (focused_)
|
|
|
|
{
|
|
|
|
blink_on_ = !blink_on_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//endregion
|
|
|
|
//region GtkEntry 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_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_placeholder_text_color(Srgb c) { placeholder_text_color_ = c; }
|
|
Srgb get_placeholder_text_color() { return placeholder_text_color_; }
|
|
void set_placeholder_text_color_rgb(int r, int g, int b) { placeholder_text_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();
|
|
|
|
// Entry body: white field with the polished outline.
|
|
|
|
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_);
|
|
|
|
}
|
|
|
|
int tx = text_x();
|
|
|
|
// Selection highlight behind the text.
|
|
|
|
if (has_selection() && (focused_ || !hide_selection_))
|
|
|
|
{
|
|
|
|
int a = std::min(sel_anchor_, cursor_);
|
|
|
|
int b = std::max(sel_anchor_, cursor_);
|
|
|
|
int sx = tx + char_x(a) - scroll_px_;
|
|
|
|
int ex = tx + char_x(b) - scroll_px_;
|
|
|
|
XSetForeground(display_, gc_, styles_->get_color_rgb(selection_fill_color_.r, selection_fill_color_.g, selection_fill_color_.b));
|
|
|
|
XFillRectangle(display_, window_, gc_, sx, y1_ + 4, ex - sx, height() - 8);
|
|
|
|
}
|
|
|
|
if (text_.empty())
|
|
|
|
{
|
|
|
|
SStyle grey = st;
|
|
|
|
grey.text_color_ = styles_->get_color_rgb(placeholder_text_color_.r, placeholder_text_color_.g, placeholder_text_color_.b);
|
|
|
|
xf::draw_text_left(display_, window_, gc_, grey, tx, y1_, y2_, placeholder_);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SStyle ink = st;
|
|
|
|
if (!enabled_)
|
|
|
|
{
|
|
|
|
ink.text_color_ = styles_->get_color_rgb(text_color_.r, text_color_.g, text_color_.b);
|
|
|
|
}
|
|
|
|
// Draw the visible run of characters only.
|
|
|
|
for (int i = 0; i < (int)text_.size(); ++i)
|
|
|
|
{
|
|
|
|
int cx = tx + char_x(i) - scroll_px_;
|
|
|
|
int cw = char_x(i + 1) - char_x(i);
|
|
|
|
if (cx + cw < x1_ + 4 || cx > x2_ - 4)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::string ch = (password_ && !show_password_)
|
|
? "*"
|
|
: text_.substr((size_t)i, 1);
|
|
|
|
int baseline = y1_ + (height() + (st.font_ ? st.font_->ascent - st.font_->descent : 10)) / 2;
|
|
|
|
xf::draw_text_at(display_, window_, gc_, ink, cx, baseline, ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Blinking cursor bar.
|
|
|
|
if (focused_ && enabled_ && blink_on_)
|
|
|
|
{
|
|
|
|
int cx = tx + char_x(cursor_) - scroll_px_;
|
|
|
|
XSetForeground(display_, gc_, styles_->get_color_rgb(caret_color_.r, caret_color_.g, caret_color_.b));
|
|
|
|
XDrawLine(display_, window_, gc_, cx, y1_ + 5, cx, y2_ - 5);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
cursor_ = index_at_pixel(x - text_x() + scroll_px_);
|
|
|
|
sel_anchor_ = cursor_;
|
|
|
|
if (auto_select_)
|
|
|
|
{
|
|
|
|
sel_anchor_ = 0;
|
|
|
|
cursor_ = (int)text_.size();
|
|
|
|
}
|
|
|
|
dragging_ = true;
|
|
|
|
ensure_cursor_visible();
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
(void)y;
|
|
|
|
if (!dragging_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cursor_ = index_at_pixel(x - text_x() + scroll_px_);
|
|
|
|
ensure_cursor_visible();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool on_key(KeySym key, const char* str)
|
|
|
|
{
|
|
|
|
if (!focused_ || !enabled_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
blink_on_ = true;
|
|
|
|
if (key == XK_Return || key == XK_KP_Enter)
|
|
|
|
{
|
|
|
|
if (on_activate)
|
|
|
|
{
|
|
|
|
on_activate();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_Left)
|
|
|
|
{
|
|
|
|
if (has_selection())
|
|
|
|
{
|
|
|
|
cursor_ = std::min(sel_anchor_, cursor_);
|
|
|
|
}
|
|
|
|
else if (cursor_ > 0)
|
|
|
|
{
|
|
|
|
--cursor_;
|
|
|
|
}
|
|
|
|
sel_anchor_ = cursor_;
|
|
|
|
ensure_cursor_visible();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_Right)
|
|
|
|
{
|
|
|
|
if (has_selection())
|
|
|
|
{
|
|
|
|
cursor_ = std::max(sel_anchor_, cursor_);
|
|
|
|
}
|
|
|
|
else if (cursor_ < (int)text_.size())
|
|
|
|
{
|
|
|
|
++cursor_;
|
|
|
|
}
|
|
|
|
sel_anchor_ = cursor_;
|
|
|
|
ensure_cursor_visible();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_Home)
|
|
|
|
{
|
|
|
|
cursor_ = 0;
|
|
|
|
sel_anchor_ = cursor_;
|
|
|
|
ensure_cursor_visible();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_End)
|
|
|
|
{
|
|
|
|
cursor_ = (int)text_.size();
|
|
|
|
sel_anchor_ = cursor_;
|
|
|
|
ensure_cursor_visible();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!editable_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (key == XK_BackSpace)
|
|
|
|
{
|
|
|
|
if (has_selection())
|
|
|
|
{
|
|
|
|
delete_selection();
|
|
|
|
}
|
|
|
|
else if (cursor_ > 0)
|
|
|
|
{
|
|
|
|
text_.erase((size_t)(cursor_ - 1), 1);
|
|
|
|
--cursor_;
|
|
|
|
sel_anchor_ = cursor_;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ensure_cursor_visible();
|
|
|
|
if (on_changed)
|
|
|
|
{
|
|
|
|
on_changed();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_Delete)
|
|
|
|
{
|
|
|
|
if (has_selection())
|
|
|
|
{
|
|
|
|
delete_selection();
|
|
|
|
}
|
|
|
|
else if (cursor_ < (int)text_.size())
|
|
|
|
{
|
|
|
|
text_.erase((size_t)cursor_, 1);
|
|
|
|
sel_anchor_ = cursor_;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ensure_cursor_visible();
|
|
|
|
if (on_changed)
|
|
|
|
{
|
|
|
|
on_changed();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Printable text from XLookupString.
|
|
|
|
if (str && str[0] >= 32 && str[0] != 127)
|
|
|
|
{
|
|
|
|
if (has_selection())
|
|
|
|
{
|
|
|
|
delete_selection();
|
|
|
|
}
|
|
|
|
for (int i = 0; str[i]; ++i)
|
|
|
|
{
|
|
|
|
if (str[i] < 32 || str[i] == 127)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
char c = str[i];
|
|
|
|
if (numbers_only_ && (c < '0' || c > '9'))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (char_case_ == 1 && c >= 'a' && c <= 'z')
|
|
|
|
{
|
|
|
|
c = (char)(c - 'a' + 'A');
|
|
|
|
}
|
|
|
|
else if (char_case_ == 2 && c >= 'A' && c <= 'Z')
|
|
|
|
{
|
|
|
|
c = (char)(c - 'A' + 'a');
|
|
|
|
}
|
|
|
|
if (max_length_ > 0 && (int)text_.size() >= max_length_)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
text_.insert((size_t)cursor_, 1, c);
|
|
|
|
++cursor_;
|
|
|
|
}
|
|
|
|
sel_anchor_ = cursor_;
|
|
|
|
ensure_cursor_visible();
|
|
|
|
if (on_changed)
|
|
|
|
{
|
|
|
|
on_changed();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
//endregion
|