void text_entry::try_delete_selection() { if (!_is_read_only) { if (get_selection_length() > 0) { _text.erase(get_selection_begin(), get_selection_length()); _caret_pos += std::min(0, _selection_pos - _caret_pos); _selection_pos = _caret_pos; make_caret_visible(); handle_text_changed(); } } }
void ttext_box::delete_selection() { if(get_selection_length() == 0) { return; } // If we have a negative range change it to a positive range. // This makes the rest of the algoritms easier. int len = get_selection_length(); unsigned start = get_selection_start(); if(len < 0) { len = -len; start -= len; } utf8::string tmp = get_value(); set_value(utf8::erase(tmp, start, len)); set_cursor(start, false); }
void ttext_box::update_canvas() { /***** Gather the info *****/ // Set the cursor info. const unsigned start = get_selection_start(); const int length = get_selection_length(); set_maximum_length(max_input_length_); PangoEllipsizeMode ellipse_mode = PANGO_ELLIPSIZE_NONE; if(!can_wrap()) { if((start + length) > (get_length() / 2)) { ellipse_mode = PANGO_ELLIPSIZE_START; } else { ellipse_mode = PANGO_ELLIPSIZE_END; } } set_ellipse_mode(ellipse_mode); // Set the selection info unsigned start_offset = 0; unsigned end_offset = 0; if(length == 0) { // No nothing. } else if(length > 0) { start_offset = get_cursor_position(start).x; end_offset = get_cursor_position(start + length).x; } else { start_offset = get_cursor_position(start + length).x; end_offset = get_cursor_position(start).x; } /***** Set in all canvases *****/ const int max_width = get_text_maximum_width(); const int max_height = get_text_maximum_height(); for(auto & tmp : canvas()) { tmp.set_variable("text", variant(get_value())); tmp.set_variable("text_x_offset", variant(text_x_offset_)); tmp.set_variable("text_y_offset", variant(text_y_offset_)); tmp.set_variable("text_maximum_width", variant(max_width)); tmp.set_variable("text_maximum_height", variant(max_height)); tmp.set_variable("cursor_offset", variant(get_cursor_position(start + length).x)); tmp.set_variable("selection_offset", variant(start_offset)); tmp.set_variable("selection_width", variant(end_offset - start_offset)); tmp.set_variable("text_wrap_mode", variant(ellipse_mode)); } }
void tpassword_box::pre_function() { // ttext_box::set_value() will reset the selection, // we therefore have to remember it size_t selection_start = get_selection_start(); size_t selection_length = get_selection_length(); // Tell ttext_box the actual input of this box ttext_box::set_value(real_value_); // Restore the selection set_selection_start(selection_start); set_selection_length(selection_length); }
void text_entry::handle_delete_key_down() { if (!_is_read_only) { if (get_selection_length() > 0) { try_delete_selection(); } else { if (!_text.empty() && _caret_pos < uint_to_int(_text.length())) { _text.erase(_caret_pos, 1); handle_text_changed(); } } } }
void text_entry::handle_backspace_key_down() { if (!_is_read_only) { if (get_selection_length() > 0) { try_delete_selection(); } else { if (!_text.empty() && _caret_pos > 0) { _text.erase(_caret_pos - 1, 1); move_caret_left(1, false); handle_text_changed(); } } } }
void tpassword_box::handle_key_delete(SDLMod /*modifier*/, bool& handled) { pre_function(); // Copy & paste from ttext_::handle_key_delete() DBG_GUI_E << LOG_SCOPE_HEADER << '\n'; handled = true; if(get_selection_length() != 0) { delete_selection(); } else if (get_selection_start() < get_text_length(text())) { delete_char(false); } post_function(); }
void text_entry::move_caret_left(int count, bool should_select) { for (int i = 0; i < count; ++i) { if (!should_select && get_selection_length() > 0) { _selection_pos = _caret_pos; } else { if (_caret_pos > 0) { _caret_pos--; if (!should_select) { _selection_pos = _caret_pos; } make_caret_visible(); } } } }
void tpassword_box::post_function() { // See above size_t selection_start = get_selection_start(); size_t selection_length = get_selection_length(); // Get the input back and make ttext_box forget it real_value_ = get_value(); ttext_box::set_value(std::string(get_text_length(real_value_), '*')); // See above set_selection_start(selection_start); set_selection_length(selection_length); // Why do the selection functions not update // the canvas? update_canvas(); set_dirty(true); }
void text_entry::move_caret_right(int count, bool should_select) { for (int i = 0; i < count; ++i) { if (!should_select && get_selection_length() > 0) { _selection_pos = _caret_pos; } else { int text_length = uint_to_int(_text.length()); if (_caret_pos < text_length) { _caret_pos++; if (!should_select) { _selection_pos = _caret_pos; } make_caret_visible(); } else if (_caret_pos > text_length) { //this can happen if pasting in huge strings and causing the append to fail out, wiping out the string. _caret_pos = text_length; } } } }
void text_entry::copy_text_to_clipboard(clipboard& clipboard) { clipboard.set_clipboard_text(_text.substr(get_selection_begin(), get_selection_length())); }
int text_entry::get_selection_end() const { return get_selection_begin() + get_selection_length(); }