From df68cfa3adbdbf04842f584698aa8b55e88c534a Mon Sep 17 00:00:00 2001 From: Jinhao Date: Tue, 10 May 2016 15:52:06 +0800 Subject: [PATCH] a bool param to determines whether the caret move to end of text --- .../nana/gui/detail/inner_fwd_implement.hpp | 14 +++---- .../gui/widgets/skeletons/text_editor.hpp | 10 ++++- include/nana/gui/widgets/textbox.hpp | 12 ++++-- source/gui/widgets/combox.cpp | 4 +- source/gui/widgets/skeletons/text_editor.cpp | 41 +++++++++++++------ source/gui/widgets/spinbox.cpp | 6 +-- source/gui/widgets/textbox.cpp | 9 ++-- 7 files changed, 61 insertions(+), 35 deletions(-) diff --git a/include/nana/gui/detail/inner_fwd_implement.hpp b/include/nana/gui/detail/inner_fwd_implement.hpp index 659bc9a9..a1bc1aaf 100644 --- a/include/nana/gui/detail/inner_fwd_implement.hpp +++ b/include/nana/gui/detail/inner_fwd_implement.hpp @@ -112,21 +112,19 @@ namespace nana{ struct root_misc { - typedef basic_window core_window_t; - - core_window_t * window; + basic_window * window; nana::paint::graphics root_graph; shortkey_container shortkeys; struct condition_rep { - bool ignore_tab{ false }; //ignore tab when the focus is changed by pressing tab. - core_window_t* pressed{nullptr}; //The handle to a window which has been pressed by pressing left button of mouse. - core_window_t* pressed_by_space{ nullptr }; //The handle to a window which has been pressed by pressing spacebar. - core_window_t* hovered{nullptr}; //the latest window that mouse moved + bool ignore_tab{ false }; //ignore tab when the focus is changed by TAB key. + basic_window* pressed{ nullptr }; //The handle to a window which has been pressed by mouse left button. + basic_window* pressed_by_space{ nullptr }; //The handle to a window which has been pressed by SPACEBAR key. + basic_window* hovered{ nullptr }; //the latest window that mouse moved }condition; - root_misc(core_window_t * wd, unsigned width, unsigned height) + root_misc(basic_window * wd, unsigned width, unsigned height) : window(wd), root_graph({ width, height }) {} diff --git a/include/nana/gui/widgets/skeletons/text_editor.hpp b/include/nana/gui/widgets/skeletons/text_editor.hpp index 0dad496e..92b6a5ac 100644 --- a/include/nana/gui/widgets/skeletons/text_editor.hpp +++ b/include/nana/gui/widgets/skeletons/text_editor.hpp @@ -42,6 +42,12 @@ namespace nana{ namespace widgets using command = EnumCommand; using container = std::deque < std::unique_ptr> >; + void clear() + { + commands_.clear(); + pos_ = 0; + } + void max_steps(std::size_t maxs) { max_steps_ = maxs; @@ -58,7 +64,7 @@ namespace nana{ namespace widgets { enabled_ = enb; if (!enb) - commands_.clear(); + clear(); } bool enabled() const @@ -204,7 +210,7 @@ namespace nana{ namespace widgets unsigned screen_lines() const; bool getline(std::size_t pos, ::std::wstring&) const; - void text(std::wstring); + void text(std::wstring, bool end_caret); std::wstring text() const; /// Sets caret position through text coordinate. diff --git a/include/nana/gui/widgets/textbox.hpp b/include/nana/gui/widgets/textbox.hpp index e385b633..13b4eb9b 100644 --- a/include/nana/gui/widgets/textbox.hpp +++ b/include/nana/gui/widgets/textbox.hpp @@ -1,7 +1,7 @@ /** * A Textbox Implementation * Nana C++ Library(http://www.nanapro.org) - * Copyright(C) 2003-2015 Jinhao(cnjinhao@hotmail.com) + * Copyright(C) 2003-2016 Jinhao(cnjinhao@hotmail.com) * * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at @@ -138,8 +138,14 @@ namespace nana /// @param generator generates text for identing a line. If it is empty, textbox indents the line according to last line. textbox& indention(bool, std::function generator = {}); - //A workaround for reset, explicit default constructor syntax, because VC2013 incorrectly treats {} as {0}. - textbox& reset(const std::string& = std::string()); ///< discard the old text and set a new text + /// Discards the old text and set a new text. It also clears the filename/edited flags and undo command. + /// A workaround for reset, explicit default constructor syntax, because VC2013 incorrectly treats {} as {0}. + /* + * @param text A new text replaces the old text. + * @param end_caret Indicates whether to position the caret to the end of text. + * @return a reference of *this. + */ + textbox& reset(const std::string& text = std::string(), bool end_caret = true); ///< discard the old text and set a new text /// The file of last store operation. std::string filename() const; diff --git a/source/gui/widgets/combox.cpp b/source/gui/widgets/combox.cpp index 2e1d4546..374a9baa 100644 --- a/source/gui/widgets/combox.cpp +++ b/source/gui/widgets/combox.cpp @@ -351,7 +351,7 @@ namespace nana if (calc_where(*graph_, pos.x, pos.y)) state_.button_state = element_state::normal; - editor_->text(::nana::charset(items_[index]->item_text, ::nana::unicode::utf8)); + editor_->text(::nana::charset(items_[index]->item_text, ::nana::unicode::utf8), false); _m_draw_push_button(widget_->enabled()); _m_draw_image(); @@ -993,7 +993,7 @@ namespace nana auto editor = _m_impl().editor(); if (editor) - editor->text(to_wstring(str)); + editor->text(to_wstring(str), false); API::refresh_window(*this); } diff --git a/source/gui/widgets/skeletons/text_editor.cpp b/source/gui/widgets/skeletons/text_editor.cpp index 90b4bdce..a1d9d1e2 100644 --- a/source/gui/widgets/skeletons/text_editor.cpp +++ b/source/gui/widgets/skeletons/text_editor.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include namespace nana{ namespace widgets @@ -442,11 +441,8 @@ namespace nana{ namespace widgets editor_._m_get_scrollbar_size(); - auto x = points.caret.x; auto& lnstr = textbase.getline(points.caret.y); - - if (x > lnstr.size()) x = static_cast(lnstr.size()); - + const auto x = (points.caret.x > lnstr.size() ? static_cast(lnstr.size()) : points.caret.x); auto const text_w = editor_._m_pixels_by_char(lnstr, x); unsigned area_w = editor_._m_text_area().width; @@ -515,11 +511,12 @@ namespace nana{ namespace widgets const wchar_t* begin; const wchar_t* end; unsigned pixels; - - text_section() + /* + text_section() //deprecated { throw std::runtime_error("text_section default construction is forbidden."); } + */ text_section(const wchar_t* ptr, const wchar_t* endptr) : begin(ptr), end(endptr) @@ -1808,12 +1805,34 @@ namespace nana{ namespace widgets return true; } - void text_editor::text(std::wstring str) + void text_editor::text(std::wstring str, bool end_caret) { + undo_.clear(); + textbase_.erase_all(); _m_reset(); behavior_->pre_calc_lines(width_pixels()); - put(std::move(str)); + if (!end_caret) + { + auto undo_ptr = std::unique_ptr{ new undo_input_text(*this, str) }; + undo_ptr->set_caret_pos(); + + _m_put(std::move(str)); + + undo_.push(std::move(undo_ptr)); + + if (graph_) + { + behavior_->adjust_caret_into_screen(); + reset_caret(); + render(API::is_focus_ready(window_)); + _m_scrollbar(); + + points_.xpos = 0; + } + } + else + put(std::move(str)); } std::wstring text_editor::text() const @@ -2324,7 +2343,6 @@ namespace nana{ namespace widgets behavior_->adjust_caret_into_screen(); render(true); _m_scrollbar(); - } void text_editor::move_ns(bool to_north) @@ -2965,9 +2983,6 @@ namespace nana{ namespace widgets nana::size text_editor::_m_text_extent_size(const char_type* str, size_type n) const { - if (!graph_) - return{}; - if(mask_char_) { std::wstring maskstr; diff --git a/source/gui/widgets/spinbox.cpp b/source/gui/widgets/spinbox.cpp index 4dc0dd70..a4105075 100644 --- a/source/gui/widgets/spinbox.cpp +++ b/source/gui/widgets/spinbox.cpp @@ -422,9 +422,9 @@ namespace nana return; if (API::is_focus_ready(editor_->window_handle())) - editor_->text(to_wstring(range_->value())); + editor_->text(to_wstring(range_->value()), false); else - editor_->text(to_wstring(modifier_.prefix + range_->value() + modifier_.suffix)); + editor_->text(to_wstring(modifier_.prefix + range_->value() + modifier_.suffix), false); _m_draw_spins(spin_stated_); } @@ -697,7 +697,7 @@ namespace nana auto editor = get_drawer_trigger().impl()->editor(); if (editor) { - editor->text(to_wstring(text)); + editor->text(to_wstring(text), false); API::refresh_window(*this); } } diff --git a/source/gui/widgets/textbox.cpp b/source/gui/widgets/textbox.cpp index adb1a93a..12f42872 100644 --- a/source/gui/widgets/textbox.cpp +++ b/source/gui/widgets/textbox.cpp @@ -254,13 +254,14 @@ namespace drawerbase { return *this; } - textbox& textbox::reset(const std::string& str) + textbox& textbox::reset(const std::string& str, bool end_caret) { internal_scope_guard lock; auto editor = get_drawer_trigger().editor(); if (editor) - { - editor->text(to_wstring(str)); + { + editor->text(to_wstring(str), end_caret); + editor->textbase().reset(); API::update_window(this->handle()); } @@ -622,7 +623,7 @@ namespace drawerbase { auto editor = get_drawer_trigger().editor(); if (editor) { - editor->text(to_wstring(str)); + editor->text(to_wstring(str), false); API::update_window(this->handle()); } }