fix bug when an invalid pos for textbox.move_caret

This commit is contained in:
Jinhao 2016-08-27 09:40:49 +08:00
parent b13f0a4ce5
commit cf99551924
3 changed files with 24 additions and 11 deletions

View File

@ -219,7 +219,7 @@ namespace nana{ namespace widgets
* @param pos the text position
* @param reset indicates whether to reset the text position by the pos. If this parameter is true, the text position is set by pos. If the parameter is false, it only moves the UI caret to the specified position.
*/
void move_caret(const upoint& pos, bool reset = false);
bool move_caret(const upoint& pos, bool reset = false);
void move_caret_end();
void reset_caret_pixels() const;
void reset_caret();

View File

@ -341,6 +341,9 @@ namespace nana{ namespace widgets
text_ptr = &mask_str;
}
if (pos.x > text_ptr->size())
pos.x = text_ptr->size();
pos.x = editor_._m_pixels_by_char(*text_ptr, pos.x) + editor_.text_area_.area.x;
int pos_y = static_cast<int>((pos.y - editor_.points_.offset.y) * editor_.line_height() + editor_._m_text_top_base());
@ -1840,18 +1843,18 @@ namespace nana{ namespace widgets
//move_caret
//Set caret position through text coordinate
void text_editor::move_caret(const upoint& crtpos, bool reset_caret)
{
if (reset_caret)
points_.caret = crtpos;
if (!API::is_focus_ready(window_))
return;
bool text_editor::move_caret(const upoint& crtpos, bool reset_caret)
{
const unsigned line_pixels = line_height();
auto pos = this->behavior_->caret_to_screen(crtpos);
const int line_bottom = pos.y + static_cast<int>(line_pixels);
if (reset_caret)
points_.caret = this->behavior_->screen_to_caret(pos);
if (!API::is_focus_ready(window_))
return false;
bool visible = false;
if (hit_text_area(pos) && (line_bottom > text_area_.area.y))
{
@ -1868,6 +1871,16 @@ namespace nana{ namespace widgets
caret_->visible(visible);
if(visible)
caret_->position(pos);
//Adjust the caret into screen when the caret position is modified by this function
if (reset_caret && (!hit_text_area(pos)))
{
behavior_->adjust_caret_into_screen();
render(true);
caret_->visible(true);
return true;
}
return false;
}
void text_editor::move_caret_end()

View File

@ -344,8 +344,8 @@ namespace drawerbase {
{
auto editor = get_drawer_trigger().editor();
internal_scope_guard lock;
if (editor)
editor->move_caret(pos, true);
if (editor && editor->move_caret(pos, true))
API::refresh_window(handle());
return *this;
}