diff --git a/include/nana/deploy.hpp b/include/nana/deploy.hpp index 9b8cf203..088f8be5 100644 --- a/include/nana/deploy.hpp +++ b/include/nana/deploy.hpp @@ -81,6 +81,35 @@ namespace nana std::wstring utf8_cast(const std::string&); std::string utf8_cast(const std::wstring&); + + const std::string& to_utf8(const std::string&); + std::string to_utf8(const std::wstring&); + + std::wstring to_wstring(const std::string& utf8_str); + const std::wstring& to_wstring(const std::wstring& wstr); + std::wstring&& to_wstring(std::wstring&& wstr); + + + namespace detail + { +#if defined(NANA_WINDOWS) + using native_string_type = std::wstring; +#else //POSIX + using native_string_type = std::string; +#endif + } + +#if defined(NANA_WINDOWS) + const detail::native_string_type to_native_string(const std::string&); + const detail::native_string_type& to_native_string(const std::wstring&); + detail::native_string_type to_native_string(int); + detail::native_string_type to_native_string(double); +#else //POSIX + const detail::native_string_type& to_native_string(const std::string&); + const detail::native_string_type to_native_string(const std::wstring&); + detail::native_string_type to_native_string(int); + detail::native_string_type to_native_string(double); +#endif } #ifndef NANA_UNICODE diff --git a/include/nana/gui/detail/basic_window.hpp b/include/nana/gui/detail/basic_window.hpp index b725b6bb..207f7e3b 100644 --- a/include/nana/gui/detail/basic_window.hpp +++ b/include/nana/gui/detail/basic_window.hpp @@ -145,7 +145,7 @@ namespace detail basic_window *parent; basic_window *owner; - ::std::string title; + native_string_type title; ::nana::detail::drawer drawer; //Self Drawer with owen graphics basic_window* root_widget; //A pointer refers to the root basic window, if the window is a root, the pointer refers to itself. paint::graphics* root_graph; //Refer to the root buffer graphics diff --git a/include/nana/gui/detail/native_window_interface.hpp b/include/nana/gui/detail/native_window_interface.hpp index 186d3e68..4ad3d1d0 100644 --- a/include/nana/gui/detail/native_window_interface.hpp +++ b/include/nana/gui/detail/native_window_interface.hpp @@ -34,6 +34,8 @@ namespace detail unsigned extra_height; //extra border size, it is useful in Windows, ignore in X11 always 0 }; + using native_string_type = ::nana::detail::native_string_type; + static nana::size primary_monitor_size(); static rectangle screen_area_from_point(const point&); static window_result create_window(native_window_type, bool nested, const rectangle&, const appearance&); @@ -65,8 +67,8 @@ namespace detail static void window_size(native_window_type, const size&); static void get_window_rect(native_window_type, rectangle&); - static void window_caption(native_window_type, const std::string&); - static std::string window_caption(native_window_type); + static void window_caption(native_window_type, const native_string_type&); + static native_string_type window_caption(native_window_type); static void capture_window(native_window_type, bool); static nana::point cursor_position(); static native_window_type get_owner_window(native_window_type); diff --git a/include/nana/gui/detail/widget_notifier_interface.hpp b/include/nana/gui/detail/widget_notifier_interface.hpp index 8439304d..81a0a28f 100644 --- a/include/nana/gui/detail/widget_notifier_interface.hpp +++ b/include/nana/gui/detail/widget_notifier_interface.hpp @@ -30,8 +30,8 @@ namespace nana virtual widget* widget_ptr() const = 0; virtual void destroy() = 0; - virtual std::string caption() = 0; - virtual void caption(std::string) = 0; + virtual ::nana::detail::native_string_type caption() = 0; + virtual void caption(::nana::detail::native_string_type) = 0; }; } } diff --git a/include/nana/gui/programming_interface.hpp b/include/nana/gui/programming_interface.hpp index 0612f664..dfa4e610 100644 --- a/include/nana/gui/programming_interface.hpp +++ b/include/nana/gui/programming_interface.hpp @@ -72,8 +72,8 @@ namespace API widget_colors* get_scheme(window); void attach_drawer(widget&, drawer_trigger&); - std::string window_caption(window) throw(); - void window_caption(window, std::string); + ::nana::detail::native_string_type window_caption(window) throw(); + void window_caption(window, ::nana::detail::native_string_type); window create_window(window, bool nested, const rectangle&, const appearance&, widget* attached); window create_widget(window, const rectangle&, widget* attached); @@ -271,7 +271,7 @@ namespace API void update_window(window); ///< Copies the off-screen buffer to the screen for immediate display. void window_caption(window, const std::string& title_utf8); - void window_caption(window, const nana::string& title); + void window_caption(window, const std::wstring& title); ::std::string window_caption(window); void window_cursor(window, cursor); diff --git a/include/nana/gui/widgets/button.hpp b/include/nana/gui/widgets/button.hpp index 955de3a3..cce1abe1 100644 --- a/include/nana/gui/widgets/button.hpp +++ b/include/nana/gui/widgets/button.hpp @@ -104,7 +104,7 @@ namespace nana{ private: //Overrides widget virtual functions void _m_complete_creation() override; - void _m_caption(std::string&&) override; + void _m_caption(native_string_type&&) override; }; }//end namespace nana #endif diff --git a/include/nana/gui/widgets/categorize.hpp b/include/nana/gui/widgets/categorize.hpp index 6d685e8c..1e97d1f5 100644 --- a/include/nana/gui/widgets/categorize.hpp +++ b/include/nana/gui/widgets/categorize.hpp @@ -238,10 +238,10 @@ namespace nana } private: //Overrides widget's virtual functions - void _m_caption(std::string&& str) override + void _m_caption(native_string_type&& str) override { - this->get_drawer_trigger().path(str); - API::dev::window_caption(*this, this->get_drawer_trigger().path()); + this->get_drawer_trigger().path(to_utf8(str)); + API::dev::window_caption(*this, to_native_string(this->get_drawer_trigger().path()) ); } }; }//end namespace nana diff --git a/include/nana/gui/widgets/combox.hpp b/include/nana/gui/widgets/combox.hpp index 3297ee18..c3033a9c 100644 --- a/include/nana/gui/widgets/combox.hpp +++ b/include/nana/gui/widgets/combox.hpp @@ -223,8 +223,8 @@ namespace nana const drawerbase::combox::drawer_impl& _m_impl() const; private: //Overrides widget's virtual functions - ::std::string _m_caption() const throw() override; - void _m_caption(::std::string&&) override; + native_string_type _m_caption() const throw() override; + void _m_caption(native_string_type&&) override; nana::any * _m_anyobj(std::size_t pos, bool alloc_if_empty) const override; }; } diff --git a/include/nana/gui/widgets/group.hpp b/include/nana/gui/widgets/group.hpp index d05a7525..79162f06 100644 --- a/include/nana/gui/widgets/group.hpp +++ b/include/nana/gui/widgets/group.hpp @@ -77,8 +77,8 @@ namespace nana{ void _m_add_child(const char* field, widget*); void _m_init(); void _m_complete_creation() override; - ::std::string _m_caption() const throw() override; - void _m_caption(::std::string&&) override; + native_string_type _m_caption() const throw() override; + void _m_caption(native_string_type&&) override; private: std::unique_ptr impl_; }; diff --git a/include/nana/gui/widgets/label.hpp b/include/nana/gui/widgets/label.hpp index 7f769f27..f7071914 100644 --- a/include/nana/gui/widgets/label.hpp +++ b/include/nana/gui/widgets/label.hpp @@ -76,7 +76,7 @@ namespace nana label& text_align(align horizontal_align, align_v vertical_align= align_v::top); private: //Overrides widget's virtual function - void _m_caption(std::string&&) override; + void _m_caption(native_string_type&&) override; }; }//end namespace nana #endif diff --git a/include/nana/gui/widgets/spinbox.hpp b/include/nana/gui/widgets/spinbox.hpp index 00fab1d9..4bc85000 100644 --- a/include/nana/gui/widgets/spinbox.hpp +++ b/include/nana/gui/widgets/spinbox.hpp @@ -106,8 +106,8 @@ namespace nana void modifier(std::wstring prefix, std::wstring suffix); void modifier(const std::string & prefix_utf8, const std::string& suffix_utf8); private: - ::std::string _m_caption() const throw(); - void _m_caption(::std::string&&); + native_string_type _m_caption() const throw(); + void _m_caption(native_string_type&&); }; //end class spinbox }//end namespace nana diff --git a/include/nana/gui/widgets/textbox.hpp b/include/nana/gui/widgets/textbox.hpp index 9d2d70bc..65c37522 100644 --- a/include/nana/gui/widgets/textbox.hpp +++ b/include/nana/gui/widgets/textbox.hpp @@ -209,8 +209,8 @@ namespace nana unsigned line_pixels() const; protected: //Overrides widget's virtual functions - ::std::string _m_caption() const throw() override; - void _m_caption(::std::string&&) override; + native_string_type _m_caption() const throw() override; + void _m_caption(native_string_type&&) override; void _m_typeface(const paint::font&) override; }; }//end namespace nana diff --git a/include/nana/gui/widgets/widget.hpp b/include/nana/gui/widgets/widget.hpp index 4dde5a06..b8304031 100644 --- a/include/nana/gui/widgets/widget.hpp +++ b/include/nana/gui/widgets/widget.hpp @@ -34,11 +34,8 @@ namespace nana class inner_widget_notifier; typedef void(*dummy_bool_type)(widget* (*)(const widget&)); public: -#if defined(NANA_WINDOWS) - using native_string_type = std::wstring; -#else //POSIX - using native_string_type = std::string; -#endif + using native_string_type = detail::native_string_type; + virtual ~widget() = default; virtual window handle() const = 0; ///< Returns the handle of window, returns 0 if window is not created. bool empty() const; ///< Determines whether the manipulator is handling a window. @@ -113,8 +110,8 @@ namespace nana virtual void _m_complete_creation(); virtual general_events& _m_get_general_events() const = 0; - virtual ::std::string _m_caption() const throw(); - virtual void _m_caption(::std::string&&); + virtual native_string_type _m_caption() const throw(); + virtual void _m_caption(native_string_type&&); virtual nana::cursor _m_cursor() const; virtual void _m_cursor(nana::cursor); virtual void _m_close(); diff --git a/source/deploy.cpp b/source/deploy.cpp index 1c8fd611..3c3b540d 100644 --- a/source/deploy.cpp +++ b/source/deploy.cpp @@ -407,6 +407,73 @@ namespace nana return ::nana::charset(text).to_bytes(::nana::unicode::utf8); } + const std::string& to_utf8(const std::string& str) + { + return str; + } + + std::string to_utf8(const std::wstring& text) + { + return ::nana::charset(text).to_bytes(::nana::unicode::utf8); + } + + std::wstring to_wstring(const std::string& utf8_str) + { + return ::nana::charset(utf8_str, ::nana::unicode::utf8); + } + + const std::wstring& to_wstring(const std::wstring& wstr) + { + return wstr; + } + + std::wstring&& to_wstring(std::wstring&& wstr) + { + return static_cast(wstr); + } + +#if defined(NANA_WINDOWS) + const detail::native_string_type to_native_string(const std::string& text) + { + return ::nana::charset(text, ::nana::unicode::utf8); + } + + const detail::native_string_type& to_native_string(const std::wstring& text) + { + return text; + } + + detail::native_string_type to_native_string(int n) + { + return std::to_wstring(n); + } + + detail::native_string_type to_native_string(double d) + { + return std::to_wstring(d); + } +#else //POSIX + const detail::native_string_type& to_native_string(const std::string& text) + { + return text; + } + + const detail::native_string_type to_native_string(const std::wstring& text) + { + return ::nana::charset(text).to_bytes(::nana::unicode::utf8); + } + + detail::native_string_type to_native_string(int n) + { + return std::to_string(n); + } + + detail::native_string_type to_native_string(double d) + { + return std::to_string(d); + } +#endif + std::size_t strlen(const char_t* str) { diff --git a/source/gui/detail/native_window_interface.cpp b/source/gui/detail/native_window_interface.cpp index e97b318b..8fbcabe1 100644 --- a/source/gui/detail/native_window_interface.cpp +++ b/source/gui/detail/native_window_interface.cpp @@ -1076,18 +1076,17 @@ namespace nana{ #endif } - void native_interface::window_caption(native_window_type wd, const std::string& title) + void native_interface::window_caption(native_window_type wd, const native_string_type& title) { #if defined(NANA_WINDOWS) - std::wstring wtext = ::nana::charset(title, ::nana::unicode::utf8); if(::GetCurrentThreadId() != ::GetWindowThreadProcessId(reinterpret_cast(wd), 0)) { - wchar_t * wstr = new wchar_t[wtext.length() + 1]; - std::wcscpy(wstr, wtext.c_str()); + wchar_t * wstr = new wchar_t[title.length() + 1]; + std::wcscpy(wstr, title.c_str()); ::PostMessage(reinterpret_cast(wd), nana::detail::messages::remote_thread_set_window_text, reinterpret_cast(wstr), 0); } else - ::SetWindowText(reinterpret_cast(wd), wtext.c_str()); + ::SetWindowText(reinterpret_cast(wd), title.c_str()); #elif defined(NANA_X11) ::XTextProperty name; char * text = const_cast(title.c_str()); @@ -1101,13 +1100,13 @@ namespace nana{ #endif } - std::string native_interface::window_caption(native_window_type wd) + auto native_interface::window_caption(native_window_type wd) -> native_string_type { #if defined(NANA_WINDOWS) int length = ::GetWindowTextLength(reinterpret_cast(wd)); if(length > 0) { - nana::string str; + native_string_type str; //One for NULL terminator which GetWindowText will write. str.resize(length+1); @@ -1116,7 +1115,7 @@ namespace nana{ //Remove the null terminator writtien by GetWindowText str.resize(length); - return ::nana::charset(str); + return str; } #elif defined(NANA_X11) nana::detail::platform_scope_guard psg; @@ -1136,7 +1135,7 @@ namespace nana{ } } #endif - return std::string(); + return native_string_type(); } void native_interface::capture_window(native_window_type wd, bool cap) diff --git a/source/gui/programming_interface.cpp b/source/gui/programming_interface.cpp index 97daa458..6f76bcbc 100644 --- a/source/gui/programming_interface.cpp +++ b/source/gui/programming_interface.cpp @@ -220,7 +220,7 @@ namespace API } } - std::string window_caption(window wd) throw() + ::nana::detail::native_string_type window_caption(window wd) throw() { auto const iwd = reinterpret_cast(wd); internal_scope_guard isg; @@ -234,7 +234,7 @@ namespace API return {}; } - void window_caption(window wd, std::string title) + void window_caption(window wd, ::nana::detail::native_string_type title) { auto const iwd = reinterpret_cast(wd); internal_scope_guard lock; @@ -823,12 +823,15 @@ namespace API auto const iwd = reinterpret_cast(wd); internal_scope_guard lock; if (restrict::wd_manager().available(iwd)) - iwd->widget_notifier->caption(title_utf8); + iwd->widget_notifier->caption(to_native_string(title_utf8)); } - void window_caption(window wd, const nana::string& title) + void window_caption(window wd, const std::wstring& title) { - window_caption(wd, static_cast(::nana::charset(title).to_bytes(::nana::unicode::utf8))); + auto const iwd = reinterpret_cast(wd); + internal_scope_guard lock; + if (restrict::wd_manager().available(iwd)) + iwd->widget_notifier->caption(to_native_string(title)); } std::string window_caption(window wd) @@ -836,7 +839,7 @@ namespace API auto const iwd = reinterpret_cast(wd); internal_scope_guard lock; if (restrict::wd_manager().available(iwd)) - return iwd->widget_notifier->caption(); + return to_utf8(iwd->widget_notifier->caption()); return{}; } diff --git a/source/gui/widgets/button.cpp b/source/gui/widgets/button.cpp index 703bf7d9..61c3f082 100644 --- a/source/gui/widgets/button.cpp +++ b/source/gui/widgets/button.cpp @@ -511,11 +511,11 @@ namespace nana{ namespace drawerbase }); } - void button::_m_caption(std::string&& text) + void button::_m_caption(native_string_type&& text) { API::unregister_shortkey(handle()); - std::wstring wtext = ::nana::charset(text, ::nana::unicode::utf8); + std::wstring wtext = ::nana::to_wstring(text); wchar_t shortkey; API::transform_shortkey_text(wtext, shortkey, 0); diff --git a/source/gui/widgets/categorize.cpp b/source/gui/widgets/categorize.cpp index 135087a9..87075b69 100644 --- a/source/gui/widgets/categorize.cpp +++ b/source/gui/widgets/categorize.cpp @@ -543,7 +543,7 @@ namespace nana { if(node) { - API::dev::window_caption(window_handle(), tree().path()); + API::dev::window_caption(window_handle(), to_native_string(tree().path())); if(evt_holder_.selected) evt_holder_.selected(node->value.second.value); } @@ -807,7 +807,7 @@ namespace nana { throw_not_utf8(str); scheme_->tree().insert(str, value); - API::dev::window_caption(scheme_->window_handle(), scheme_->tree().path()); + API::dev::window_caption(scheme_->window_handle(), to_native_string(scheme_->tree().path())); scheme_->draw(); } diff --git a/source/gui/widgets/combox.cpp b/source/gui/widgets/combox.cpp index 471ca968..ad1fa88c 100644 --- a/source/gui/widgets/combox.cpp +++ b/source/gui/widgets/combox.cpp @@ -982,22 +982,22 @@ namespace nana API::refresh_window(*this); } - std::string combox::_m_caption() const throw() + auto combox::_m_caption() const throw() -> native_string_type { internal_scope_guard lock; auto editor = _m_impl().editor(); if (editor) - return ::nana::charset(editor->text()); - return std::string(); + return to_native_string(editor->text()); + return native_string_type(); } - void combox::_m_caption(std::string&& str) + void combox::_m_caption(native_string_type&& str) { internal_scope_guard lock; auto editor = _m_impl().editor(); if (editor) - editor->text(::nana::charset(str, nana::unicode::utf8)); + editor->text(to_native_string(str)); API::refresh_window(*this); } diff --git a/source/gui/widgets/group.cpp b/source/gui/widgets/group.cpp index a7f4418a..aa4d3346 100644 --- a/source/gui/widgets/group.cpp +++ b/source/gui/widgets/group.cpp @@ -234,12 +234,12 @@ namespace nana{ _m_init(); } - ::std::string group::_m_caption() const throw() + auto group::_m_caption() const throw() -> native_string_type { - return impl_->caption.caption(); + return impl_->caption.caption_native(); } - void group::_m_caption(::std::string&& str) + void group::_m_caption(native_string_type&& str) { impl_->caption.caption(std::move(str)); impl_->update_div(); diff --git a/source/gui/widgets/label.cpp b/source/gui/widgets/label.cpp index a8b700f1..f6968dd0 100644 --- a/source/gui/widgets/label.cpp +++ b/source/gui/widgets/label.cpp @@ -807,7 +807,7 @@ namespace nana if(impl->renderer.format(f)) { window wd = *this; - impl->renderer.parse(::nana::charset(API::dev::window_caption(wd), ::nana::unicode::utf8)); + impl->renderer.parse(::nana::to_wstring(API::dev::window_caption(wd))); API::refresh_window(wd); } return *this; @@ -869,11 +869,11 @@ namespace nana return *this; } - void label::_m_caption(std::string&& str) + void label::_m_caption(native_string_type&& str) { internal_scope_guard lock; window wd = *this; - get_drawer_trigger().impl()->renderer.parse(::nana::charset(str, nana::unicode::utf8)); + get_drawer_trigger().impl()->renderer.parse(to_wstring(str)); API::dev::window_caption(wd, std::move(str)); API::refresh_window(wd); } diff --git a/source/gui/widgets/spinbox.cpp b/source/gui/widgets/spinbox.cpp index c8787785..bd1c15fc 100644 --- a/source/gui/widgets/spinbox.cpp +++ b/source/gui/widgets/spinbox.cpp @@ -677,22 +677,22 @@ namespace nana modifier(static_cast(::nana::charset(prefix_utf8, ::nana::unicode::utf8)), static_cast(::nana::charset(suffix_utf8, ::nana::unicode::utf8))); } - ::std::string spinbox::_m_caption() const throw() + auto spinbox::_m_caption() const throw() -> native_string_type { internal_scope_guard lock; auto editor = get_drawer_trigger().impl()->editor(); if (editor) - return ::nana::charset(editor->text()); - return std::string(); + return to_native_string(editor->text()); + return native_string_type(); } - void spinbox::_m_caption(::std::string&& text) + void spinbox::_m_caption(native_string_type&& text) { internal_scope_guard lock; auto editor = get_drawer_trigger().impl()->editor(); if (editor) { - editor->text(::nana::charset(text, ::nana::unicode::utf8)); + editor->text(to_wstring(text)); API::refresh_window(*this); } } diff --git a/source/gui/widgets/textbox.cpp b/source/gui/widgets/textbox.cpp index b7a6688d..26e588b7 100644 --- a/source/gui/widgets/textbox.cpp +++ b/source/gui/widgets/textbox.cpp @@ -492,7 +492,7 @@ namespace drawerbase { std::stringstream ss; int value; - ss << s; + ss << to_utf8(s); ss >> value; return value; } @@ -504,20 +504,20 @@ namespace drawerbase { std::stringstream ss; double value; - ss << s; + ss << to_utf8(s); ss >> value; return value; } textbox& textbox::from(int n) { - _m_caption(std::to_string(n)); + _m_caption(to_native_string(n)); return *this; } textbox& textbox::from(double d) { - _m_caption(std::to_string(d)); + _m_caption(to_native_string(d)); return *this; } @@ -595,23 +595,23 @@ namespace drawerbase { } //Override _m_caption for caption() - ::std::string textbox::_m_caption() const throw() + auto textbox::_m_caption() const throw() -> native_string_type { internal_scope_guard lock; auto editor = get_drawer_trigger().editor(); if (editor) - return ::nana::charset(editor->text()); + return to_native_string(editor->text()); - return std::string(); + return native_string_type(); } - void textbox::_m_caption(std::string&& str) + void textbox::_m_caption(native_string_type&& str) { internal_scope_guard lock; auto editor = get_drawer_trigger().editor(); if (editor) { - editor->text(::nana::charset(str, ::nana::unicode::utf8)); + editor->text(to_wstring(str)); API::update_window(this->handle()); } } diff --git a/source/gui/widgets/widget.cpp b/source/gui/widgets/widget.cpp index 3d180aed..a98bc289 100644 --- a/source/gui/widgets/widget.cpp +++ b/source/gui/widgets/widget.cpp @@ -41,12 +41,12 @@ namespace nana wdg_._m_notify_destroy(); } - std::string caption() override + native_string_type caption() override { return wdg_._m_caption(); } - virtual void caption(std::string text) + void caption(native_string_type text) override { wdg_._m_caption(std::move(text)); } @@ -56,33 +56,35 @@ namespace nana std::string widget::caption() const throw() { - return this->_m_caption(); + return utf8_cast(_m_caption()); } std::wstring widget::caption_wstring() const throw() { +#if defined(NANA_WINDOWS) + return _m_caption(); +#else return utf8_cast(_m_caption()); +#endif } auto widget::caption_native() const throw() -> native_string_type { -#if defined(NANA_WINDOWS) - return caption_wstring(); -#else - return caption(); -#endif + return _m_caption(); } widget& widget::caption(std::string utf8) { ::nana::throw_not_utf8(utf8); - _m_caption(std::move(utf8)); + native_string_type str = to_native_string(utf8); + _m_caption(std::move(str)); return *this; } widget& widget::caption(std::wstring text) { - _m_caption(utf8_cast(text)); + native_string_type str = to_native_string(text); + _m_caption(std::move(str)); return *this; } @@ -90,7 +92,8 @@ namespace nana { if (handle()) { - _m_caption(eval()); + native_string_type str = to_native_string(eval()); + _m_caption(std::move(str)); internationalization_parts::set_eval(handle(), std::move(eval)); } } @@ -269,12 +272,12 @@ namespace nana void widget::_m_complete_creation() {} - std::string widget::_m_caption() const throw() + auto widget::_m_caption() const throw() -> native_string_type { return API::dev::window_caption(handle()); } - void widget::_m_caption(std::string&& str) + void widget::_m_caption(native_string_type&& str) { API::dev::window_caption(handle(), std::move(str)); }