fix newline issue of text_editor

This commit is contained in:
Jinhao 2015-07-12 10:56:51 +08:00
parent b1ed1f0452
commit f762589975

View File

@ -2534,8 +2534,34 @@ namespace nana{ namespace widgets
return true;
}
std::size_t eat_endl(const wchar_t* str, std::size_t pos)
{
auto ch = str[pos];
if (0 == ch)
return pos;
const wchar_t * endlstr;
switch (ch)
{
case L'\n':
endlstr = L"\n\r";
break;
case L'\r':
endlstr = L"\r\n";
break;
default:
return pos;
}
if (std::memcmp(str + pos, endlstr, sizeof(wchar_t) * 2) == 0)
return pos + 2;
return pos + 1;
}
bool text_editor::_m_resolve_text(const nana::string& text, std::vector<std::pair<std::size_t, std::size_t>> & lines)
{
auto const text_str = text.data();
std::size_t begin = 0;
while (true)
{
@ -2548,13 +2574,23 @@ namespace nana{ namespace widgets
}
lines.emplace_back(begin, pos);
begin = text.find_first_not_of(STR("\r\n"), pos + 1);
pos = eat_endl(text_str, pos);
begin = text.find_first_not_of(STR("\r\n"), pos);
//The number of new lines minus one
const auto chp_end = text.data() + (begin == text.npos ? text.size() : begin);
for (auto chp = text.data() + (pos + 1); chp != chp_end; ++chp)
if (*chp == '\n')
const auto chp_end = text_str + (begin == text.npos ? text.size() : begin);
for (auto chp = text_str + pos; chp != chp_end; ++chp)
{
auto eats = eat_endl(chp, 0);
if (eats)
{
lines.emplace_back(0, 0);
chp += (eats - 1);
}
}
if (text.npos == begin)
{
@ -2760,19 +2796,12 @@ namespace nana{ namespace widgets
auto ent_end = (ent.end < str_end ? ent.end : str_end);
auto ent_pixels = std::accumulate(glyphs + (ent_begin - str), glyphs + (ent_end - str), unsigned{});
if (ent.scheme->bgcolor.invisible())
canvas.set_color(_m_bgcolor());
else
canvas.set_color(ent.scheme->bgcolor);
canvas.set_color(ent.scheme->bgcolor.invisible() ? _m_bgcolor() : ent.scheme->bgcolor);
canvas.set_text_color(ent.scheme->fgcolor.invisible() ? fgcolor : ent.scheme->fgcolor);
canvas.rectangle(true);
if (ent.scheme->fgcolor.invisible())
canvas.set_text_color(fgcolor);
else
canvas.set_text_color(ent.scheme->fgcolor);
ent_pos.x += ent_off;
if (rtl)
{
//draw the whole text if it is a RTL text, because Arbic language is transformable.
@ -2797,7 +2826,6 @@ namespace nana{ namespace widgets
if (if_mask && mask_char_)
mask_str.reset(new nana::string(str.size(), mask_char_));
auto & linestr = (if_mask && mask_char_ ? *mask_str : str);
unicode_bidi bidi;