a bool param to determines whether the caret move to end of text

This commit is contained in:
Jinhao 2016-05-10 15:52:06 +08:00
parent 79f484dd2e
commit df68cfa3ad
7 changed files with 61 additions and 35 deletions

View File

@ -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 })
{}

View File

@ -42,6 +42,12 @@ namespace nana{ namespace widgets
using command = EnumCommand;
using container = std::deque < std::unique_ptr<undoable_command_interface<command>> >;
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.

View File

@ -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<std::string()> 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;

View File

@ -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);
}

View File

@ -18,7 +18,6 @@
#include <numeric>
#include <cwctype>
#include <cstring>
#include <set>
#include <algorithm>
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<unsigned>(lnstr.size());
const auto x = (points.caret.x > lnstr.size() ? static_cast<decltype(points.caret.x)>(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<undo_input_text>{ 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;

View File

@ -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);
}
}

View File

@ -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());
}
}