text_part(0),
slider(0),
got_key_press(false),
- cursor_blink(true)
+ cursor_blink(true),
+ selection_active(false),
+ selection_pos(0)
{
input_type = INPUT_TEXT;
set_text(t);
bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
}
+ else if(part.get_name()=="selection")
+ {
+ if(!selection_active)
+ return;
+
+ const Graphic *graphic = part.get_graphic(state);
+ if(!text_part || !graphic || !graphic->get_texture())
+ return;
+
+ unsigned start = selection_pos;
+ unsigned end = edit_pos;
+ if(start>end)
+ swap(start, end);
+
+ unsigned row, col;
+ text.offset_to_coords(start, row, col);
+ unsigned end_row, end_col;
+ text.offset_to_coords(end, end_row, end_col);
+
+ if(end_row<first_row || row>=first_row+visible_rows)
+ return;
+
+ if(row<first_row)
+ {
+ row = first_row;
+ col = 0;
+ }
+
+ if(end_row>=first_row+visible_rows)
+ {
+ end_row = first_row+visible_rows-1;
+ end_col = text.get_line_length(end_row);
+ }
+
+ while(row<=end_row)
+ {
+ unsigned ec = (row==end_row ? end_col : text.get_line_length(row));
+ if(ec>col)
+ {
+ Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
+ Geometry egeom = text.coords_to_geometry(*text_part, geom, first_row, row, ec);
+
+ GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
+ bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
+ graphic->build(egeom.x-rgeom.x, part.get_geometry().h, bld);
+ }
+
+ ++row;
+ col = 0;
+ }
+ }
else if(part.get_name()=="slider")
{
if(multiline)
Widget::touch_press(x, y, finger);
}
-bool Entry::key_press(unsigned key, unsigned)
+bool Entry::key_press(unsigned key, unsigned mod)
{
got_key_press = true;
if(key==Input::KEY_BACKSPACE)
{
- if(edit_pos>0)
+ if(selection_active)
+ erase_selection();
+ else if(edit_pos>0)
{
text.erase(--edit_pos, 1);
check_view_range();
rebuild();
}
}
+ else if(key==Input::KEY_DELETE)
+ {
+ if(selection_active)
+ erase_selection();
+ else
+ text.erase(edit_pos, 1);
+ }
else if(key==Input::KEY_ENTER && multiline)
{
text.insert(edit_pos++, "\n");
{
unsigned row, col;
text.offset_to_coords(edit_pos, row, col);
- set_edit_position(text.coords_to_offset(row, text.get_line_length(row)));
+ set_edit_position(text.coords_to_offset(row, text.get_line_length(row)), mod==MOD_SHIFT);
}
else if(key==Input::KEY_HOME)
{
unsigned row, col;
text.offset_to_coords(edit_pos, row, col);
- set_edit_position(text.coords_to_offset(row, 0));
+ set_edit_position(text.coords_to_offset(row, 0), mod==MOD_SHIFT);
}
+ else if(key==Input::KEY_LEFT && mod==MOD_SHIFT)
+ move_edit_position(NAV_LEFT, true);
+ else if(key==Input::KEY_RIGHT && mod==MOD_SHIFT)
+ move_edit_position(NAV_RIGHT, true);
+ else if(key==Input::KEY_UP && mod==MOD_SHIFT && multiline)
+ move_edit_position(NAV_UP, true);
+ else if(key==Input::KEY_DOWN && mod==MOD_SHIFT && multiline)
+ move_edit_position(NAV_DOWN, true);
else
return false;
bool Entry::character(wchar_t ch)
{
- if(got_key_press && ch>=' ')
+ if(got_key_press && ch>=' ' && ch!=0x7F)
{
+ if(selection_active)
+ erase_selection();
text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
++edit_pos;
rebuild();
bool Entry::navigate(Navigation nav)
{
- if(nav==NAV_LEFT)
- {
- if(edit_pos>0)
- set_edit_position(edit_pos-1);
- }
- else if(nav==NAV_RIGHT)
- {
- if(edit_pos<text.size())
- set_edit_position(edit_pos+1);
- }
- else if(nav==NAV_DOWN && multiline)
- {
- unsigned row, col;
- text.offset_to_coords(edit_pos, row, col);
- set_edit_position(text.coords_to_offset(row+1, col));
- }
- else if(nav==NAV_UP && multiline)
- {
- unsigned row, col;
- text.offset_to_coords(edit_pos, row, col);
- set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
- }
+ if(nav==NAV_LEFT || nav==NAV_RIGHT || ((nav==NAV_DOWN || nav==NAV_UP) && multiline))
+ move_edit_position(nav, false);
else if(nav==NAV_ACCEPT && !signal_enter.empty())
signal_enter.emit();
else
check_cursor_blink();
}
-void Entry::set_edit_position(unsigned ep)
+void Entry::move_edit_position(Navigation nav, bool select)
{
+ if(nav==NAV_LEFT)
+ {
+ if(edit_pos>0)
+ set_edit_position(edit_pos-1, select);
+ }
+ else if(nav==NAV_RIGHT)
+ {
+ if(edit_pos<text.size())
+ set_edit_position(edit_pos+1, select);
+ }
+ else if(nav==NAV_DOWN)
+ {
+ unsigned row, col;
+ text.offset_to_coords(edit_pos, row, col);
+ set_edit_position(text.coords_to_offset(row+1, col), select);
+ }
+ else if(nav==NAV_UP)
+ {
+ unsigned row, col;
+ text.offset_to_coords(edit_pos, row, col);
+ set_edit_position((row>0 ? text.coords_to_offset(row-1, col) : 0), select);
+ }
+}
+
+void Entry::set_edit_position(unsigned ep, bool select)
+{
+ if(select && !selection_active)
+ selection_pos = edit_pos;
+ selection_active = select;
+
edit_pos = ep;
check_view_range();
rebuild();
}
+void Entry::erase_selection()
+{
+ if(!selection_active)
+ return;
+
+ unsigned start = selection_pos;
+ unsigned end = edit_pos;
+ if(start>end)
+ swap(start, end);
+
+ text.erase(start, end-start);
+ set_edit_position(start, false);
+}
+
void Entry::check_cursor_blink()
{
cursor_blink = (state&FOCUS);