diff --git a/include/nana/gui/programming_interface.hpp b/include/nana/gui/programming_interface.hpp index 0a1d55d8..4d1931dc 100644 --- a/include/nana/gui/programming_interface.hpp +++ b/include/nana/gui/programming_interface.hpp @@ -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. diff --git a/source/gui/programming_interface.cpp b/source/gui/programming_interface.cpp index 6f91756e..6121d95d 100644 --- a/source/gui/programming_interface.cpp +++ b/source/gui/programming_interface.cpp @@ -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(graph.text_extent_size(&shortkey, 1).width); + + unsigned ascent, descent, inleading; + graph.text_metrics(ascent, descent, inleading); + + int x = text_pos.x + static_cast(off_x); + int y = text_pos.y + static_cast(ascent + 2); + + graph.line({ x, y }, {x + key_px - 1, y}, line_color); + + } + } }//end namespace dev diff --git a/source/gui/widgets/button.cpp b/source/gui/widgets/button.cpp index 01de4f7c..a01d001b 100644 --- a/source/gui/widgets/button.cpp +++ b/source/gui/widgets/button.cpp @@ -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(ascent + 2); - - graph.line(pos, point{ pos.x + static_cast(shortkey_size.width) - 1, pos.y }, colors::black); - } + API::dev::draw_shortkey_underline(graph, mbstr, shortkey, shortkey_pos, pos, text_color); } else { diff --git a/source/gui/widgets/menu.cpp b/source/gui/widgets/menu.cpp index bdf2cba1..36dbb7de 100644 --- a/source/gui/widgets/menu.cpp +++ b/source/gui/widgets/menu.cpp @@ -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(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); diff --git a/source/gui/widgets/menubar.cpp b/source/gui/widgets/menubar.cpp index 0fc114b9..4ed24c67 100644 --- a/source/gui/widgets/menubar.cpp +++ b/source/gui/widgets/menubar.cpp @@ -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(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;