refactor drawing of shortkey underline

This commit is contained in:
Jinhao 2017-08-27 10:32:56 +08:00
parent 4e18d81f90
commit ec9ec611b4
5 changed files with 28 additions and 50 deletions

View File

@ -106,6 +106,8 @@ namespace API
* This function will copy the drawer surface into system window after the event process finished.
*/
void lazy_refresh();
void draw_shortkey_underline(paint::graphics&, const std::string& text, wchar_t shortkey, std::size_t shortkey_position, const point& text_pos, const color&);
}//end namespace dev
/// Returns the widget pointer of the specified window.

View File

@ -365,6 +365,24 @@ namespace API
{
restrict::bedrock.thread_context_lazy_refresh();
}
void draw_shortkey_underline(paint::graphics& graph, const std::string& text, wchar_t shortkey, std::size_t shortkey_position, const point& text_pos, const color& line_color)
{
if (shortkey)
{
auto off_x = (shortkey_position ? graph.text_extent_size(text.c_str(), shortkey_position).width : 0);
auto key_px = static_cast<int>(graph.text_extent_size(&shortkey, 1).width);
unsigned ascent, descent, inleading;
graph.text_metrics(ascent, descent, inleading);
int x = text_pos.x + static_cast<int>(off_x);
int y = text_pos.y + static_cast<int>(ascent + 2);
graph.line({ x, y }, {x + key_px - 1, y}, line_color);
}
}
}//end namespace dev

View File

@ -34,12 +34,7 @@ namespace nana{ namespace drawerbase
return{};
wchar_t shortkey;
std::string::size_type shortkey_pos;
auto str = to_wstring(API::transform_shortkey_text(trigger_->wdg_->caption(), shortkey, &shortkey_pos));
auto text_sz = graph.text_extent_size(str);
return size{ text_sz.width, text_sz.height };
return graph.text_extent_size(API::transform_shortkey_text(trigger_->wdg_->caption(), shortkey, nullptr));
}
size extension() const override
@ -261,28 +256,15 @@ namespace nana{ namespace drawerbase
++pos.y;
}
graph.palette(true, attr_.focus_color && attr_.focused ? ::nana::color(colors::blue) : attr_.fgcolor);
auto text_color = (attr_.focus_color && attr_.focused ? ::nana::color(colors::blue) : attr_.fgcolor);
graph.palette(true, text_color);
if (attr_.omitted)
tr.render(pos, txtptr, txtlen, omitted_pixels, true);
else
graph.bidi_string(pos, txtptr, txtlen);
if(shortkey)
{
unsigned off_w = (shortkey_pos ? graph.text_extent_size(mbstr.c_str(), shortkey_pos).width : 0);
wchar_t keystr[2] = {nana::utf::char_at(mbstr.c_str() + shortkey_pos, 0, 0), 0};
auto shortkey_size = graph.text_extent_size(keystr, 1);
unsigned ascent, descent, inleading;
graph.text_metrics(ascent, descent, inleading);
pos.x += off_w;
pos.y += static_cast<int>(ascent + 2);
graph.line(pos, point{ pos.x + static_cast<int>(shortkey_size.width) - 1, pos.y }, colors::black);
}
API::dev::draw_shortkey_underline(graph, mbstr, shortkey, shortkey_pos, pos, text_color);
}
else
{

View File

@ -441,23 +441,9 @@ namespace nana
renderer->item_text(graph, nana::point(item_r.x + 40, item_r.y + text_top_off), text, strpixels, attr);
if (hotkey)
{
item_ptr->hotkey = hotkey;
if (item_ptr->flags.enabled)
{
auto off_px = (hotkey_pos ? graph.text_extent_size(text.c_str(), hotkey_pos).width : 0);
auto hotkey_px = graph.text_extent_size(text.c_str() + hotkey_pos, 1).width;
unsigned ascent, descent, inleading;
graph.text_metrics(ascent, descent, inleading);
int x = item_r.x + 40 + off_px;
int y = item_r.y + text_top_off + ascent + 1;
graph_->line({ x, y }, { x + static_cast<int>(hotkey_px)-1, y }, colors::black);
}
}
item_ptr->hotkey = hotkey;
if (hotkey && item_ptr->flags.enabled)
API::dev::draw_shortkey_underline(*graph_, text, hotkey, hotkey_pos, {item_r.x + 40, item_r.y + text_top_off}, colors::black);
if (item_ptr->linked.menu_ptr)
renderer->sub_arrow(graph, nana::point(graph_->width() - 20, item_r.y), item_h_px, attr);

View File

@ -212,17 +212,7 @@ namespace nana
int text_top_off = (item_s.height - text_s.height) / 2;
ird.caption({ item_pos.x + 8, item_pos.y + text_top_off }, to_nstring(text));
if (hotkey)
{
unsigned off_w = (hotkey_pos ? graph.text_extent_size(text.c_str(), hotkey_pos).width : 0);
auto hotkey_size = graph.text_extent_size(nana::to_wstring(text.c_str() + hotkey_pos), 1);
unsigned ascent, descent, inleading;
graph.text_metrics(ascent, descent, inleading);
int x = item_pos.x + 8 + off_w;
int y = item_pos.y + text_top_off + ascent + 1;
graph.line({ x, y }, { x + static_cast<int>(hotkey_size.width) - 1, y }, ird.scheme_ptr()->text_fgcolor);
}
API::dev::draw_shortkey_underline(graph, text, hotkey, hotkey_pos, { item_pos.x + 8, item_pos.y + text_top_off }, ird.scheme_ptr()->text_fgcolor);
item_pos.x += i->size.width;
++index;