use UTF-8 for string representation

This commit is contained in:
Jinhao
2015-12-29 01:26:19 +08:00
parent 443b3dcd87
commit 48254b6555
22 changed files with 116 additions and 124 deletions

View File

@@ -6,9 +6,9 @@ namespace nana{ namespace audio
namespace detail
{
//class audio_stream
bool audio_stream::open(const nana::string& file)
bool audio_stream::open(const std::string& file)
{
fs_.open(static_cast<std::string>(charset(file)), std::ios::binary);
fs_.open(to_osmbstr(file), std::ios::binary);
if(fs_)
{
wave_spec::master_riff_chunk riff;

View File

@@ -17,7 +17,7 @@ namespace nana{ namespace audio
: impl_(new implementation)
{}
player::player(const nana::string& file)
player::player(const std::string& file)
: impl_(new implementation)
{
open(file);
@@ -28,7 +28,7 @@ namespace nana{ namespace audio
delete impl_;
}
bool player::open(const nana::string& file)
bool player::open(const std::string& file)
{
if(impl_->stream.open(file))
{

View File

@@ -108,7 +108,7 @@ namespace nana
_m_icon(ico);
}
void prompt(const nana::string& text)
void prompt(const std::string& text)
{
if(text.size())
{
@@ -376,23 +376,15 @@ namespace nana
sstream_.clear();
}
msgbox & msgbox::operator<<(const nana::string& str)
msgbox & msgbox::operator<<(const std::wstring& str)
{
#if defined(NANA_UNICODE)
sstream_<<static_cast<std::string>(nana::charset(str));
#else
sstream_<<str;
#endif
sstream_ << to_osmbstr(to_utf8(str));
return *this;
}
msgbox & msgbox::operator<<(const nana::char_t* str)
msgbox & msgbox::operator<<(const wchar_t* str)
{
#if defined(NANA_UNICODE)
sstream_<<static_cast<std::string>(nana::charset(str));;
#else
sstream_<<str;
#endif
sstream_ << to_osmbstr(to_utf8(str));
return *this;
}

View File

@@ -299,10 +299,10 @@ namespace nana
impl_->native_handle = nullptr;
}
void notifier::text(const nana::string& str)
void notifier::text(const std::string& str)
{
#if defined(NANA_WINDOWS)
NOTIFYICONDATA icon_data;
NOTIFYICONDATAW icon_data;
memset(&icon_data, 0, sizeof icon_data);
icon_data.cbSize = sizeof icon_data;
icon_data.hWnd = reinterpret_cast<HWND>(impl_->native_handle);
@@ -310,18 +310,18 @@ namespace nana
icon_data.uFlags = NIF_MESSAGE | NIF_TIP;
icon_data.uCallbackMessage = nana::detail::messages::tray;
strcpy(icon_data.szTip, str.data());
std::wcscpy(icon_data.szTip, to_wstring(str).c_str());
::Shell_NotifyIcon(impl_->icon_added ? NIM_MODIFY : NIM_ADD, &icon_data);
impl_->icon_added = true;
#endif
}
void notifier::icon(const nana::string& icon_file)
void notifier::icon(const std::string& icon_file)
{
#if defined(NANA_WINDOWS)
auto pre_icon = impl_->icon_handle;
auto ico = (HICON)::LoadImage(0, icon_file.data(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
auto ico = (HICON)::LoadImageW(0, to_wstring(icon_file).data(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
if (ico)
{
impl_->icon_handle = ico;
@@ -333,10 +333,10 @@ namespace nana
#endif
}
void notifier::insert_icon(const nana::string& icon_file)
void notifier::insert_icon(const std::string& icon_file)
{
#if defined(NANA_WINDOWS)
auto icon = (HICON)::LoadImage(0, icon_file.data(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
auto icon = (HICON)::LoadImage(0, to_wstring(icon_file).data(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
impl_->icons.push_back(icon);
#endif
}

View File

@@ -336,14 +336,14 @@ namespace API
//@param, text: the text is transformed.
//@param, shortkey: the character which indicates a short key.
//@param, skpos: retrives the shortkey position if it is not a null_ptr;
nana::string transform_shortkey_text(nana::string text, nana::string::value_type &shortkey, nana::string::size_type *skpos)
std::string transform_shortkey_text(std::string text, wchar_t &shortkey, std::string::size_type *skpos)
{
shortkey = 0;
nana::string::size_type off = 0;
std::string::size_type off = 0;
while(true)
{
nana::string::size_type pos = text.find_first_of('&', off);
if(pos != nana::string::npos)
auto pos = text.find_first_of('&', off);
if(pos != std::wstring::npos)
{
text.erase(pos, 1);
if(shortkey == 0 && pos < text.length())

View File

@@ -212,11 +212,9 @@ namespace nana{ namespace drawerbase
void trigger::_m_draw_title(graph_reference graph, bool enabled)
{
std::wstring text = ::nana::charset(wdg_->caption(), ::nana::unicode::utf8);
std::wstring::value_type shortkey;
std::wstring::size_type shortkey_pos;
std::wstring str = API::transform_shortkey_text(text, shortkey, &shortkey_pos);
wchar_t shortkey;
std::string::size_type shortkey_pos;
std::wstring str = to_wstring(API::transform_shortkey_text(wdg_->caption(), shortkey, &shortkey_pos));
nana::size ts = graph.text_extent_size(str);
nana::size gsize = graph.size();
@@ -237,7 +235,7 @@ namespace nana{ namespace drawerbase
unsigned omitted_pixels = gsize.width - icon_sz.width;
std::size_t txtlen = str.size();
const nana::char_t* txtptr = str.c_str();
const auto txtptr = str.c_str();
if(ts.width)
{
nana::paint::text_renderer tr(graph);
@@ -513,14 +511,14 @@ namespace nana{ namespace drawerbase
{
API::unregister_shortkey(handle());
std::wstring wtext = ::nana::to_wstring(text);
native_string_type ntext = std::move(text);
wchar_t shortkey;
API::transform_shortkey_text(wtext, shortkey, 0);
API::transform_shortkey_text(to_utf8(ntext), shortkey, nullptr);
if (shortkey)
API::register_shortkey(handle(), shortkey);
base_type::_m_caption(std::move(text));
base_type::_m_caption(std::move(ntext));
}
//end class button
}//end namespace nana

View File

@@ -79,7 +79,7 @@ namespace nana
flags.checked = false;
}
menu_item_type::menu_item_type(nana::string text, const event_fn_t& f)
menu_item_type::menu_item_type(std::string text, const event_fn_t& f)
: text(std::move(text)), functor(f)
{
flags.enabled = true;
@@ -148,7 +148,7 @@ namespace nana
img.stretch(rectangle{ img.size() }, graph, rectangle{ pos, ::nana::size(image_px, image_px) });
}
void item_text(graph_reference graph, const nana::point& pos, const nana::string& text, unsigned text_pixels, const attr& at)
void item_text(graph_reference graph, const nana::point& pos, const std::wstring& text, unsigned text_pixels, const attr& at)
{
graph.set_text_color(at.enabled ? colors::black : colors::gray_border);
nana::paint::text_renderer tr(graph);
@@ -240,7 +240,7 @@ namespace nana
return root_;
}
void insert(std::size_t pos, nana::string&& text, const event_fn_t& fn)
void insert(std::size_t pos, std::string&& text, const event_fn_t& fn)
{
if(pos < root_.items.size())
root_.items.emplace(root_.items.begin() + pos, std::move(text), std::ref(fn));
@@ -382,9 +382,9 @@ namespace nana
renderer->item(*graph_, item_r, attr);
//Draw text, the text is transformed from orignal for hotkey character
nana::char_t hotkey;
nana::string::size_type hotkey_pos;
nana::string text = API::transform_shortkey_text(m.text, hotkey, &hotkey_pos);
wchar_t hotkey;
std::string::size_type hotkey_pos;
auto text = to_wstring(API::transform_shortkey_text(m.text, hotkey, &hotkey_pos));
if (m.image.empty() == false)
renderer->item_image(graph, nana::point(item_r.x + 5, item_r.y + static_cast<int>(item_h_px - image_px) / 2 - 1), image_px, m.image);
@@ -1108,7 +1108,7 @@ namespace nana
delete impl_;
}
auto menu::append(const nana::string& text, const menu::event_fn_t& f) -> item_proxy
auto menu::append(const std::string& text, const menu::event_fn_t& f) -> item_proxy
{
impl_->mbuilder.data().items.emplace_back(text, f);
return item_proxy(size() - 1, impl_->mbuilder.data().items.back());

View File

@@ -30,11 +30,11 @@ namespace nana
{
struct item_type
{
item_type(const ::nana::string& text, unsigned long shortkey)
item_type(const ::std::wstring& text, unsigned long shortkey)
: text(text), shortkey(shortkey)
{}
::nana::string text;
::std::wstring text;
unsigned long shortkey;
::nana::menu menu_obj;
::nana::point pos;
@@ -52,7 +52,7 @@ namespace nana
delete i;
}
void append(const ::nana::string& text, unsigned long shortkey)
void append(const ::std::wstring& text, unsigned long shortkey)
{
if(shortkey && shortkey < 0x61) shortkey += (0x61 - 0x41);
cont_.push_back(new item_type(text, shortkey));
@@ -122,7 +122,7 @@ namespace nana
graph_.rectangle(r.pare_off(1), true, body);
}
void item_renderer::caption(const point& pos, const nana::string& text)
void item_renderer::caption(const point& pos, const std::wstring& text)
{
graph_.string(pos, text, colors::black);
}
@@ -138,10 +138,10 @@ namespace nana
delete items_;
}
nana::menu* trigger::push_back(const ::nana::string& text)
nana::menu* trigger::push_back(const ::std::wstring& text)
{
::nana::char_t shkey;
API::transform_shortkey_text(text, shkey, nullptr);
wchar_t shkey;
API::transform_shortkey_text(to_utf8(text), shkey, nullptr);
if(shkey)
API::register_shortkey(widget_->handle(), shkey);
@@ -187,9 +187,9 @@ namespace nana
for (auto i : items_->cont())
{
//Transform the text if it contains the hotkey character
::nana::char_t hotkey;
::nana::string::size_type hotkey_pos;
auto text = API::transform_shortkey_text(i->text, hotkey, &hotkey_pos);
wchar_t hotkey;
::std::wstring::size_type hotkey_pos;
auto text = API::transform_shortkey_text(to_utf8(i->text), hotkey, &hotkey_pos);
nana::size text_s = graph.text_extent_size(text);
@@ -212,11 +212,11 @@ namespace nana
//Draw text, the text is transformed from orignal for hotkey character
int text_top_off = (item_s.height - text_s.height) / 2;
ird.caption({ item_pos.x + 8, item_pos.y + text_top_off }, text);
ird.caption({ item_pos.x + 8, item_pos.y + text_top_off }, to_wstring(text));
if (hotkey)
{
unsigned off_w = (hotkey_pos ? graph.text_extent_size(text, static_cast<unsigned>(hotkey_pos)).width : 0);
unsigned off_w = (hotkey_pos ? graph.text_extent_size(text.c_str(), static_cast<unsigned>(hotkey_pos)).width : 0);
nana::size hotkey_size = graph.text_extent_size(text.c_str() + hotkey_pos, 1);
unsigned ascent, descent, inleading;
@@ -617,9 +617,9 @@ namespace nana
});
}
menu& menubar::push_back(const nana::string& text)
menu& menubar::push_back(const std::string& text)
{
return *(get_drawer_trigger().push_back(text));
return *(get_drawer_trigger().push_back(to_wstring(text)));
}
menu& menubar::at(std::size_t index) const

View File

@@ -1,6 +1,6 @@
/*
* A Tabbar Implementation
* Copyright(C) 2003-2013 Jinhao(cnjinhao@hotmail.com)
* Copyright(C) 2003-2015 Jinhao(cnjinhao@hotmail.com)
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
@@ -788,7 +788,7 @@ namespace nana
};
for(auto & m : list_)
menulister_.append(m.text, fn);
menulister_.append(to_utf8(m.text), fn);
auto r = toolbox_.area(toolbox_.ButtonList, basis_.graph->height());
r.x += _m_toolbox_pos();
@@ -1006,8 +1006,10 @@ namespace nana
nana::size ts = basis_.graph->text_extent_size(item.text);
basis_.graph->set_text_color(m.fgcolor.invisible() ? fgcolor : m.fgcolor);
nana::paint::text_renderer tr(*basis_.graph);
std::wstring wtext = to_wstring(item.text);
tr.render({ m.r.x + 24, m.r.y + static_cast<int>(m.r.height - ts.height) / 2 },
item.text.c_str(), item.text.length(), basis_.item_pixels - 24 - 18, true);
wtext.c_str(), wtext.length(), basis_.item_pixels - 24 - 18, true);
}
}

View File

@@ -639,7 +639,7 @@ namespace nana
nana::paint::image* image(const node_type* node)
{
const nana::string& idstr = node->value.second.img_idstr;
const std::string& idstr = node->value.second.img_idstr;
if(idstr.size())
{
auto i = shape.image_table.find(idstr);

View File

@@ -1,7 +1,7 @@
/*
* Platform Implementation
* Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2003-2014 Jinhao(cnjinhao@hotmail.com)
* Copyright(C) 2003-2015 Jinhao(cnjinhao@hotmail.com)
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
@@ -129,7 +129,7 @@ namespace detail
pixbuf.paste(nana::rectangle(r.x, 0, r.width, r.height), dw, point{r.x, r.y});
}
nana::size raw_text_extent_size(drawable_type dw, const nana::char_t* text, std::size_t len)
nana::size raw_text_extent_size(drawable_type dw, const wchar_t* text, std::size_t len)
{
if(nullptr == dw || nullptr == text || 0 == len) return nana::size();
#if defined(NANA_WINDOWS)
@@ -138,7 +138,7 @@ namespace detail
return nana::size(size.cx, size.cy);
#elif defined(NANA_X11)
#if defined(NANA_UNICODE)
std::string utf8str = nana::charset(nana::string(text, len));
std::string utf8str = to_utf8(text);
XGlyphInfo ext;
XftFont * fs = reinterpret_cast<XftFont*>(dw->font->handle);
::XftTextExtentsUtf8(nana::detail::platform_spec::instance().open_display(), fs,
@@ -180,12 +180,6 @@ namespace detail
#elif defined(NANA_X11)
auto disp = ::nana::detail::platform_spec::instance().open_display();
#if defined(NANA_UNICODE)
/*
std::string utf8str = nana::charset(nana::string(str, len));
XftFont * fs = reinterpret_cast<XftFont*>(dw->font->handle);
::XftDrawStringUtf8(dw->xftdraw, &(dw->xft_fgcolor), fs, pos.x, pos.y + fs->ascent,
reinterpret_cast<XftChar8*>(const_cast<char*>(utf8str.c_str())), utf8str.size());
*/
auto fs = reinterpret_cast<XftFont*>(dw->font->handle);
//Fixed missing array declaration by dareg

View File

@@ -365,30 +365,35 @@ namespace paint
::nana::size graphics::text_extent_size(const ::std::string& text) const
{
throw_not_utf8(text);
return text_extent_size(static_cast<std::wstring>(::nana::charset(text, ::nana::unicode::utf8)));
return text_extent_size(to_wstring(text));
}
nana::size graphics::text_extent_size(const nana::char_t* text) const
::nana::size graphics::text_extent_size(const char* text, std::size_t len) const
{
return text_extent_size(text, nana::strlen(text));
return text_extent_size(std::string(text, text + len));
}
nana::size graphics::text_extent_size(const nana::string& text) const
nana::size graphics::text_extent_size(const wchar_t* text) const
{
return text_extent_size(text, std::wcslen(text));
}
nana::size graphics::text_extent_size(const std::wstring& text) const
{
return text_extent_size(text.c_str(), static_cast<unsigned>(text.length()));
}
nana::size graphics::text_extent_size(const nana::char_t* str, std::size_t len) const
nana::size graphics::text_extent_size(const wchar_t* str, std::size_t len) const
{
return detail::text_extent_size(handle_, str, len);
}
nana::size graphics::text_extent_size(const nana::string& str, std::size_t len) const
nana::size graphics::text_extent_size(const std::wstring& str, std::size_t len) const
{
return detail::text_extent_size(handle_, str.c_str(), len);
}
nana::size graphics::glyph_extent_size(const nana::char_t * str, std::size_t len, std::size_t begin, std::size_t end) const
nana::size graphics::glyph_extent_size(const wchar_t * str, std::size_t len, std::size_t begin, std::size_t end) const
{
if(len < end) end = len;
if (nullptr == handle_ || nullptr == str || 0 == len || begin >= end) return{};
@@ -414,12 +419,12 @@ namespace paint
return sz;
}
nana::size graphics::glyph_extent_size(const nana::string& str, std::size_t len, std::size_t begin, std::size_t end) const
nana::size graphics::glyph_extent_size(const std::wstring& str, std::size_t len, std::size_t begin, std::size_t end) const
{
return glyph_extent_size(str.c_str(), len, begin, end);
}
bool graphics::glyph_pixels(const nana::char_t * str, std::size_t len, unsigned* pxbuf) const
bool graphics::glyph_pixels(const wchar_t * str, std::size_t len, unsigned* pxbuf) const
{
if(nullptr == handle_ || nullptr == handle_->context || nullptr == str || nullptr == pxbuf) return false;
if(len == 0) return true;
@@ -966,12 +971,12 @@ namespace paint
string(pos, text_utf8);
}
void graphics::string(nana::point pos, const char_t* str, std::size_t len)
void graphics::string(nana::point pos, const wchar_t* str, std::size_t len)
{
if (handle_ && str && len)
{
const nana::char_t * end = str + len;
const nana::char_t * i = std::find(str, end, '\t');
auto const end = str + len;
auto i = std::find(str, end, '\t');
#if defined(NANA_LINUX) || defined(NANA_MACOS)
handle_->update_text_color();
#endif
@@ -1008,17 +1013,17 @@ namespace paint
}
}
void graphics::string(const nana::point& pos, const char_t* str)
void graphics::string(const nana::point& pos, const wchar_t* str)
{
string(pos, str, nana::strlen(str));
string(pos, str, std::wcslen(str));
}
void graphics::string(const nana::point& pos, const nana::string& str)
void graphics::string(const nana::point& pos, const std::wstring& str)
{
string(pos, str.data(), str.size());
}
void graphics::string(const point& pos, const ::nana::string& text, const color& clr)
void graphics::string(const point& pos, const ::std::wstring& text, const color& clr)
{
set_text_color(clr);
string(pos, text.data(), text.size());

View File

@@ -98,7 +98,7 @@ namespace system
}
//open an url through a default browser
void open_url(const nana::string& url)
void open_url(const std::wstring& url)
{
if(url.empty())
return;