add a method to show caret for the uneditable textbox

When an uneditable textbox has a caret, user can select the text and copy
it.
This commit is contained in:
Jinhao 2016-09-03 07:46:43 +08:00
parent 0f951d6b3e
commit d6cb631bc2
6 changed files with 41 additions and 16 deletions

View File

@ -118,7 +118,13 @@ namespace nana{ namespace widgets
const attributes & attr() const; const attributes & attr() const;
bool multi_lines(bool); bool multi_lines(bool);
void editable(bool);
/// Enables/disables the editability of text_editor
/**
* @param enable Indicates whether to enable or disable the editability
* @param enable_cart Indicates whether to show or hide the caret when the text_editor is not editable. It is ignored if enable is false.
*/
void editable(bool enable, bool enable_caret);
void enable_background(bool); void enable_background(bool);
void enable_background_counterpart(bool); void enable_background_counterpart(bool);
@ -255,6 +261,7 @@ namespace nana{ namespace widgets
bool line_wrapped{false}; bool line_wrapped{false};
bool multi_lines{true}; bool multi_lines{true};
bool editable{true}; bool editable{true};
bool enable_caret{ true }; ///< Indicates whether to show or hide caret when text_editor is not editable
bool enable_background{true}; bool enable_background{true};
}attributes_; }attributes_;

View File

@ -184,6 +184,9 @@ namespace nana
bool editable() const; bool editable() const;
textbox& editable(bool); textbox& editable(bool);
/// Enables the caret if the textbox current is not editable
textbox& enable_caret();
void set_accept(std::function<bool(wchar_t)>); void set_accept(std::function<bool(wchar_t)>);
textbox& tip_string(::std::string); textbox& tip_string(::std::string);

View File

@ -170,7 +170,7 @@ namespace nana
{ {
if(editor_) if(editor_)
{ {
editor_->editable(enb); editor_->editable(enb, false);
editor_->show_caret(enb); editor_->show_caret(enb);
if (!enb) if (!enb)
{ {

View File

@ -1507,6 +1507,9 @@ namespace nana{ namespace widgets
bool text_editor::respond_char(const arg_keyboard& arg) //key is a character of ASCII code bool text_editor::respond_char(const arg_keyboard& arg) //key is a character of ASCII code
{ {
if (!API::window_enabled(window_))
return false;
char_type key = arg.key; char_type key = arg.key;
switch (key) switch (key)
{ {
@ -1518,7 +1521,7 @@ namespace nana{ namespace widgets
return true; return true;
} }
if (attributes_.editable && API::window_enabled(window_) && (!impl_->capacities.pred_acceptive || impl_->capacities.pred_acceptive(key))) if (attributes_.editable && (!impl_->capacities.pred_acceptive || impl_->capacities.pred_acceptive(key)))
{ {
switch (key) switch (key)
{ {
@ -1700,9 +1703,10 @@ namespace nana{ namespace widgets
return true; return true;
} }
void text_editor::editable(bool v) void text_editor::editable(bool enable, bool enable_caret)
{ {
attributes_.editable = v; attributes_.editable = enable;
attributes_.enable_caret = (enable || enable_caret);
} }
void text_editor::enable_background(bool enb) void text_editor::enable_background(bool enb)
@ -1815,12 +1819,12 @@ namespace nana{ namespace widgets
bool text_editor::mouse_move(bool left_button, const point& scrpos) bool text_editor::mouse_move(bool left_button, const point& scrpos)
{ {
cursor cur = cursor::iterm; cursor cur = cursor::iterm;
if(((!hit_text_area(scrpos)) && (!text_area_.captured)) || !attributes_.editable || !API::window_enabled(window_)) if(((!hit_text_area(scrpos)) && (!text_area_.captured)) || !attributes_.enable_caret || !API::window_enabled(window_))
cur = cursor::arrow; cur = cursor::arrow;
API::window_cursor(window_, cur); API::window_cursor(window_, cur);
if(!attributes_.editable) if(!attributes_.enable_caret)
return false; return false;
if(left_button) if(left_button)
@ -1838,7 +1842,7 @@ namespace nana{ namespace widgets
bool text_editor::mouse_pressed(const arg_mouse& arg) bool text_editor::mouse_pressed(const arg_mouse& arg)
{ {
if(!attributes_.editable) if(!attributes_.enable_caret)
return false; return false;
if (event_code::mouse_down == arg.evt_code) if (event_code::mouse_down == arg.evt_code)
@ -1857,7 +1861,9 @@ namespace nana{ namespace widgets
if (this->hit_select_area(impl_->capacities.behavior->screen_to_caret(arg.pos), true)) if (this->hit_select_area(impl_->capacities.behavior->screen_to_caret(arg.pos), true))
{ {
select_.mode_selection = selection::mode::move_selected; //The selected of text can be moved only if it is editable
if (attributes_.editable)
select_.mode_selection = selection::mode::move_selected;
} }
else else
{ {
@ -2007,7 +2013,7 @@ namespace nana{ namespace widgets
reset_caret_pixels(); reset_caret_pixels();
} }
if(!attributes_.editable) if(!attributes_.enable_caret)
visible = false; visible = false;
caret->visible(visible); caret->visible(visible);
@ -2103,9 +2109,6 @@ namespace nana{ namespace widgets
bool text_editor::move_select() bool text_editor::move_select()
{ {
if (! attributes_.editable)
return false;
if(hit_select_area(points_.caret, true) || (select_.b == points_.caret)) if(hit_select_area(points_.caret, true) || (select_.b == points_.caret))
{ {
points_.caret = select_.b; points_.caret = select_.b;
@ -3206,6 +3209,9 @@ namespace nana{ namespace widgets
bool text_editor::_m_move_select(bool record_undo) bool text_editor::_m_move_select(bool record_undo)
{ {
if (!attributes_.editable)
return false;
nana::upoint caret = points_.caret; nana::upoint caret = points_.caret;
const auto text = _m_make_select_string(); const auto text = _m_make_select_string();
if (!text.empty()) if (!text.empty())
@ -3457,7 +3463,7 @@ namespace nana{ namespace widgets
const bool text_selected = (sbegin == text_ptr->c_str() && send == text_ptr->c_str() + text_ptr->size()); const bool text_selected = (sbegin == text_ptr->c_str() && send == text_ptr->c_str() + text_ptr->size());
//The text is not selected or the whole line text is selected //The text is not selected or the whole line text is selected
if (!focused || (!sbegin || !send) || text_selected || !attributes_.editable) if (!focused || (!sbegin || !send) || text_selected || !attributes_.enable_caret)
{ {
for (auto & ent : reordered) for (auto & ent : reordered)
{ {

View File

@ -607,7 +607,7 @@ namespace nana
internal_scope_guard lock; internal_scope_guard lock;
auto editor = get_drawer_trigger().impl()->editor(); auto editor = get_drawer_trigger().impl()->editor();
if (editor) if (editor)
editor->editable(accept); editor->editable(accept, false);
} }
bool spinbox::editable() const bool spinbox::editable() const

View File

@ -415,7 +415,16 @@ namespace drawerbase {
internal_scope_guard lock; internal_scope_guard lock;
auto editor = get_drawer_trigger().editor(); auto editor = get_drawer_trigger().editor();
if(editor) if(editor)
editor->editable(able); editor->editable(able, false);
return *this;
}
textbox& textbox::enable_caret()
{
internal_scope_guard lock;
auto editor = get_drawer_trigger().editor();
if (editor)
editor->editable(editor->attr().editable, true);
return *this; return *this;
} }