Refined code

refined code and implemented select all(ctrl+a) for text_editor
This commit is contained in:
cnjinhao
2014-12-21 08:15:18 +08:00
parent e64b6ec2b2
commit e5aeb8420f
7 changed files with 326 additions and 466 deletions

View File

@@ -1165,12 +1165,16 @@ namespace nana{ namespace widgets
bool text_editor::respone_keyboard(nana::char_t key, bool enterable) //key is a character of ASCII code
{
if (keyboard::end_of_text == key)
switch (key)
{
case keyboard::end_of_text:
copy();
return false;
case keyboard::select_all:
select(true);
return true;
}
if (attributes_.editable && enterable)
{
switch (key)
@@ -1577,6 +1581,7 @@ namespace nana{ namespace widgets
if(select_.b.y) --select_.b.y;
select_.b.x = static_cast<unsigned>(textbase_.getline(select_.b.y).size());
select_.mode_selection = selection::mode_method_selected;
render(true);
return true;
}
@@ -1939,6 +1944,7 @@ namespace nana{ namespace widgets
void text_editor::move_left()
{
bool do_render = false;
if(_m_cancel_select(1) == false)
{
if(points_.caret.x)
@@ -1952,23 +1958,19 @@ namespace nana{ namespace widgets
if (attributes_.line_wrapped)
adjust_y = behavior_->adjust_caret_into_screen();
bool adjust_x = _m_move_offset_x_while_over_border(-2);
if (adjust_x || adjust_y)
render(true);
do_render = (_m_move_offset_x_while_over_border(-2) || adjust_y);
}
else if(points_.caret.y)
{ //Move to previous line
points_.caret.x = static_cast<unsigned>(textbase_.getline(-- points_.caret.y).size());
if (behavior_->adjust_caret_into_screen())
render(true);
do_render = behavior_->adjust_caret_into_screen();
}
}
else
{
behavior_->adjust_caret_into_screen();
do_render = behavior_->adjust_caret_into_screen();
if (do_render)
render(true);
}
_m_scrollbar();
points_.xpos = points_.caret.x;
@@ -1976,6 +1978,7 @@ namespace nana{ namespace widgets
void text_editor::move_right()
{
bool do_render = false;
if(_m_cancel_select(2) == false)
{
nana::string lnstr = textbase_.getline(points_.caret.y);
@@ -1987,23 +1990,20 @@ namespace nana{ namespace widgets
++points_.caret.x;
#endif
bool adjust_y = (attributes_.line_wrapped && behavior_->adjust_caret_into_screen());
if (_m_move_offset_x_while_over_border(2) || adjust_y)
render(true);
do_render = (_m_move_offset_x_while_over_border(2) || adjust_y);
}
else if(textbase_.lines() && (points_.caret.y < textbase_.lines() - 1))
{ //Move to next line
points_.caret.x = 0;
++ points_.caret.y;
if (behavior_->adjust_caret_into_screen())
render(true);
do_render = behavior_->adjust_caret_into_screen();
}
}
else
{
if (behavior_->adjust_caret_into_screen())
render(true);
}
do_render = behavior_->adjust_caret_into_screen();
if (do_render)
render(true);
_m_scrollbar();
points_.xpos = points_.caret.x;

View File

@@ -38,7 +38,7 @@ namespace nana
}
else
{
graph.gradual_rectangle({ ad.fixedpos, ad.bound.x, upperblock, len }, clr_from, clr_trans, false); //deprecatd
graph.gradual_rectangle({ ad.fixedpos, ad.bound.x, upperblock, len }, clr_from, clr_trans, false);
graph.gradual_rectangle({ ad.fixedpos + static_cast<int>(upperblock), ad.bound.x, ad.block - upperblock, len }, clr_trans, clr_to, false);
}
}
@@ -69,8 +69,10 @@ namespace nana
class controller
{
public:
enum dir_t{DirHorizontal, DirVertical};
enum where_t{WhereNone, WhereBar, WhereSlider};
enum class style{horizontal, vertical};
enum class parts{none, bar, slider};
//enum dir_t{DirHorizontal, DirVertical}; //deprecated
//enum where_t{WhereNone, WhereBar, WhereSlider};
typedef drawer_trigger::graph_reference graph_reference;
@@ -83,7 +85,7 @@ namespace nana
proto_.renderer = pat::cloneable<renderer>(interior_renderer());
attr_.skdir = seekdir::bilateral;
attr_.dir = this->DirHorizontal;
attr_.dir = style::horizontal;
attr_.vcur = 0;
attr_.vmax = 10;
attr_.slider_scale = 8;
@@ -142,7 +144,7 @@ namespace nana
void vertical(bool v)
{
dir_t dir = (v ? this->DirVertical : this->DirHorizontal);
auto dir = (v ? style::vertical : style::horizontal);
if(dir != attr_.dir)
{
@@ -153,7 +155,7 @@ namespace nana
bool vertical() const
{
return (this->DirVertical == attr_.dir);
return (style::vertical == attr_.dir);
}
void vmax(unsigned m)
@@ -203,56 +205,56 @@ namespace nana
attr_.adorn_pos = attr_.pos;
}
where_t seek_where(int x, int y) const
parts seek_where(::nana::point pos) const
{
nana::rectangle r = _m_bar_area();
if(attr_.dir == this->DirVertical)
if(style::vertical == attr_.dir)
{
std::swap(x, y);
std::swap(pos.x, pos.y);
std::swap(r.width, r.height);
}
int pos = _m_slider_pos();
if(pos <= x && x < pos + static_cast<int>(attr_.slider_scale))
return WhereSlider;
int sdpos = _m_slider_pos();
if (sdpos <= pos.x && pos.x < sdpos + static_cast<int>(attr_.slider_scale))
return parts::slider;
pos = static_cast<int>(attr_.slider_scale) / 2;
sdpos = static_cast<int>(attr_.slider_scale) / 2;
if(pos <= x && x < pos + static_cast<int>(r.width))
if (sdpos <= pos.x && pos.x < sdpos + static_cast<int>(r.width))
{
if(y < r.y + static_cast<int>(r.height))
return WhereBar;
if(pos.y < r.y + static_cast<int>(r.height))
return parts::bar;
}
return WhereNone;
return parts::none;
}
//set_slider_pos
//move the slider to a position where a mouse click on WhereBar.
bool set_slider_pos(int x, int y)
bool set_slider_pos(::nana::point pos)
{
if(this->DirVertical == attr_.dir)
std::swap(x, y);
if(style::vertical == attr_.dir)
std::swap(pos.x, pos.y);
x -= _m_slider_refpos();
if(x < 0)
pos.x -= _m_slider_refpos();
if(pos.x < 0)
return false;
if(x > static_cast<int>(_m_scale()))
x = static_cast<int>(_m_scale());
if(pos.x > static_cast<int>(_m_scale()))
pos.x = static_cast<int>(_m_scale());
double pos = attr_.pos;
double dx = _m_evaluate_by_seekdir(x);
double attr_pos = attr_.pos;
double dx = _m_evaluate_by_seekdir(pos.x);
attr_.pos = dx;
attr_.adorn_pos = dx;
_m_mk_slider_value_by_pos();
return (attr_.pos != pos);
return (attr_.pos != attr_pos);
}
void set_slider_refpos(::nana::point pos)
{
if(this->DirVertical == attr_.dir)
if(style::vertical == attr_.dir)
std::swap(pos.x, pos.y);
slider_state_.trace = slider_state_.TraceCapture;
@@ -286,21 +288,21 @@ namespace nana
return (slider_state_.trace == slider_state_.TraceCapture);
}
bool move_slider(int x, int y)
bool move_slider(const ::nana::point& pos)
{
int mpos = (this->DirHorizontal == attr_.dir ? x : y);
int pos = slider_state_.snap_pos + (mpos - slider_state_.refpos.x);
int mpos = (style::horizontal == attr_.dir ? pos.x : pos.y);
int adorn_pos = slider_state_.snap_pos + (mpos - slider_state_.refpos.x);
if(pos > 0)
if (adorn_pos > 0)
{
int scale = static_cast<int>(_m_scale());
if(pos > scale)
pos = scale;
if (adorn_pos > scale)
adorn_pos = scale;
}
else
pos = 0;
adorn_pos = 0;
double dstpos = _m_evaluate_by_seekdir(pos);
double dstpos = _m_evaluate_by_seekdir(adorn_pos);
attr_.is_draw_adorn = true;
if(dstpos != attr_.pos)
@@ -312,15 +314,15 @@ namespace nana
return false;
}
bool move_adorn(int x, int y)
bool move_adorn(const ::nana::point& pos)
{
double xpos = (this->DirHorizontal == attr_.dir ? x : y);
double xpos = (style::horizontal == attr_.dir ? pos.x : pos.y);
xpos -= _m_slider_refpos();
if(xpos > static_cast<int>(_m_scale()))
xpos = static_cast<int>(_m_scale());
int pos = static_cast<int>(attr_.adorn_pos);
int adorn_pos = static_cast<int>(attr_.adorn_pos);
xpos = _m_evaluate_by_seekdir(xpos);
attr_.adorn_pos = xpos;
@@ -329,7 +331,7 @@ namespace nana
if(slider_state_.trace == slider_state_.TraceNone)
slider_state_.trace = slider_state_.TraceOver;
return (pos != static_cast<int>(xpos));
return (adorn_pos != static_cast<int>(xpos));
}
unsigned move_step(bool forward)
@@ -385,7 +387,7 @@ namespace nana
{
auto sz = other_.graph->size();
nana::rectangle r = sz;
if(this->DirHorizontal == attr_.dir)
if(style::horizontal == attr_.dir)
{
r.x = attr_.slider_scale / 2 - attr_.border;
r.width = (static_cast<int>(sz.width) > (r.x << 1) ? sz.width - (r.x << 1) : 0);
@@ -401,7 +403,7 @@ namespace nana
unsigned _m_scale() const
{
nana::rectangle r = _m_bar_area();
return ((this->DirHorizontal == attr_.dir ? r.width : r.height) - attr_.border * 2);
return ((style::horizontal == attr_.dir ? r.width : r.height) - attr_.border * 2);
}
double _m_evaluate_by_seekdir(double pos) const
@@ -465,7 +467,7 @@ namespace nana
{
renderer::bar_t bar;
bar.horizontal = (this->DirHorizontal == attr_.dir);
bar.horizontal = (style::horizontal == attr_.dir);
bar.border_size = attr_.border;
bar.r = _m_bar_area();
@@ -494,35 +496,23 @@ namespace nana
nana::string str = proto_.provider->adorn_trace(attr_.vmax, vadorn);
if(str.size())
{
nana::rectangle r;
nana::size ts = other_.graph->text_extent_size(str);
ts.width += 6;
ts.height += 2;
r.width = ts.width;
r.height = ts.height;
int x, y;
const int room = static_cast<int>(attr_.adorn_pos);
if(bar.horizontal)
{
r.y = adorn.fixedpos + static_cast<int>(adorn.block - ts.height) / 2;
if(room > static_cast<int>(ts.width + 2))
r.x = room - static_cast<int>(ts.width + 2);
else
r.x = room + 2;
r.x += this->_m_slider_refpos();
y = adorn.fixedpos + static_cast<int>(adorn.block - ts.height) / 2;
x = (room > static_cast<int>(ts.width + 2) ? room - static_cast<int>(ts.width + 2) : room + 2) + _m_slider_refpos();
}
else
{
r.x = (other_.graph->width() - ts.width) / 2;
if(room > static_cast<int>(ts.height + 2))
r.y = room - static_cast<int>(ts.height + 2);
else
r.y = room + 2;
r.y += this->_m_slider_refpos();
x = (other_.graph->width() - ts.width) / 2;
y = (room > static_cast<int>(ts.height + 2) ? room - static_cast<int>(ts.height + 2) : room + 2) + _m_slider_refpos();
}
proto_.renderer->adorn_textbox(other_.wd, *other_.graph, str, r);
proto_.renderer->adorn_textbox(other_.wd, *other_.graph, str, {x, y, ts.width, ts.height});
}
}
}
@@ -531,7 +521,7 @@ namespace nana
{
renderer::slider_t s;
s.pos = static_cast<int>(attr_.pos);
s.horizontal = (this->DirHorizontal == attr_.dir);
s.horizontal = (style::horizontal == attr_.dir);
s.scale = attr_.slider_scale;
s.border = attr_.border;
proto_.renderer->slider(other_.wd, *other_.graph, s);
@@ -553,7 +543,7 @@ namespace nana
struct attr_tag
{
seekdir skdir;
dir_t dir;
style dir;
unsigned border;
unsigned vmax;
unsigned vcur;
@@ -607,10 +597,11 @@ namespace nana
void trigger::mouse_down(graph_reference, const arg_mouse& arg)
{
controller_t::where_t what = impl_->seek_where(arg.pos.x, arg.pos.y);
if(controller_t::WhereBar == what || controller_t::WhereSlider == what)
using parts = controller_t::parts;
auto what = impl_->seek_where(arg.pos);
if(parts::bar == what || parts::slider == what)
{
bool mkdir = impl_->set_slider_pos(arg.pos.x, arg.pos.y);
bool mkdir = impl_->set_slider_pos(arg.pos);
impl_->set_slider_refpos(arg.pos);
if(mkdir)
{
@@ -635,13 +626,13 @@ namespace nana
bool mkdraw = false;
if(impl_->if_trace_slider())
{
mkdraw = impl_->move_slider(arg.pos.x, arg.pos.y);
mkdraw = impl_->move_slider(arg.pos);
}
else
{
controller_t::where_t what = impl_->seek_where(arg.pos.x, arg.pos.y);
if(controller_t::WhereNone != what)
mkdraw = impl_->move_adorn(arg.pos.x, arg.pos.y);
auto what = impl_->seek_where(arg.pos);
if(controller_t::parts::none != what)
mkdraw = impl_->move_adorn(arg.pos);
else
mkdraw = impl_->reset_adorn();
}

View File

@@ -1225,9 +1225,7 @@ namespace nana
if(pos_.y < item_pos_.y + static_cast<int>(node_r.height))
{
int logic_x = pos_.x - item_pos_.x;
int logic_y = pos_.y - item_pos_.y;
auto logic_pos = pos_ - item_pos_;
node_ = &node;
for(int comp = static_cast<int>(component::begin); comp != static_cast<int>(component::end); ++comp)
@@ -1235,7 +1233,7 @@ namespace nana
nana::rectangle r = node_r;
if(comp_placer->locate(static_cast<component>(comp), node_attr_, &r))
{
if(r.is_hit(logic_x, logic_y))
if(r.is_hit(logic_pos))
{
what_ = static_cast<component>(comp);
if(component::expender == what_ && (false == node_attr_.has_children))
@@ -1275,10 +1273,7 @@ namespace nana
nana::rectangle trigger::item_locator::text_pos() const
{
auto r = node_text_r_;
r.x += item_pos_.x;
r.y += item_pos_.y;
return r;
return{node_text_r_.x + item_pos_.x, node_text_r_.y + item_pos_.y, node_text_r_.width, node_text_r_.height};
}
//end class item_locator
@@ -1289,10 +1284,10 @@ namespace nana
typedef tree_cont_type::node_type node_type;
item_renderer(implement * impl, const nana::point& pos)
:impl_(impl), pos_(pos)
: impl_(impl), pos_(pos),
bgcolor_(impl->data.widget_ptr->bgcolor()),
fgcolor_(impl->data.widget_ptr->fgcolor())
{
bgcolor_ = impl_->data.widget_ptr->bgcolor();
fgcolor_ = impl_->data.widget_ptr->fgcolor();
}
//affect
@@ -1762,7 +1757,6 @@ namespace nana
unsigned trigger::node_width(const node_type *node) const
{
//return (static_cast<int>(impl_->data.graph->text_extent_size(node->value.second.text).width) + impl_->shape.text_offset * 2 + static_cast<unsigned>(impl_->shape.crook_pixels + impl_->shape.image_pixels));
node_attribute node_attr;
impl_->assign_node_attr(node_attr, node);
return impl_->data.comp_placer->item_width(*impl_->data.graph, node_attr);