iceman50

Untitled

Jan 12th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. void RichTextBox::addText(const RichTextBox::TextItem::List& texts) {
  2.     if(texts.empty())
  3.         return;
  4.  
  5.     dwt::Point scrollPos;
  6.     ::SendMessage(handle(), EM_GETSCROLLPOS, 0, reinterpret_cast<LPARAM>(&scrollPos));
  7.  
  8.     bool scroll = scrollIsAtEnd();
  9.     if(!scroll)
  10.         ::SendMessage(handle(), WM_SETREDRAW, FALSE, 0);
  11.  
  12.     std::pair<int, int> cr = getCaretPosRange();
  13.     unsigned charsRemoved = 0;
  14.  
  15.     {
  16.         size_t len = 0;
  17.         std::for_each(texts.begin(), texts.end(), [&len](const TextItem& ti) { len += ti.text.size(); } );
  18.  
  19.         int multipler = 1;
  20.         size_t limit = ::SendMessage(handle(), EM_GETLIMITTEXT, 0, 0);
  21.  
  22.         if(length() + len > limit) {
  23.             if(scroll)
  24.                 ::SendMessage(handle(), WM_SETREDRAW, FALSE, 0);
  25.  
  26.             if(len >= limit) {
  27.                 charsRemoved = length();
  28.             } else {
  29.                 while (charsRemoved < len)
  30.                     charsRemoved = ::SendMessage(handle(), EM_LINEINDEX, sendMessage(EM_LINEFROMCHAR, multipler++ * limit / 10), 0);
  31.             }
  32.  
  33.             scrollPos.y -= posFromChar(charsRemoved).y;
  34.             ::SendMessage(handle(), EM_SETSEL, 0, charsRemoved);
  35.             ::SendMessage(handle(), EM_REPLACESEL, static_cast<WPARAM>(TRUE), reinterpret_cast<LPARAM>(_T("")));
  36.  
  37.             if(scroll)
  38.                 ::SendMessage(handle(), WM_SETREDRAW, TRUE, 0);
  39.         }
  40.     }
  41.  
  42.     ::SendMessage(handle(), EM_SETSEL, -1, -1);
  43.     SETTEXTEX config = { ST_SELECTION, CP_ACP };
  44.  
  45.     for(TextItem::IterC i = texts.begin(); i != texts.end(); ++i) {
  46.         if(i->text.empty())
  47.             continue; // nothing to do here
  48.  
  49.         if(i->check) {
  50.             // do keywords here
  51. //          continue;
  52.         }
  53.  
  54.         string line = escapeUnicode(_T("{\\urtf1\n") + rtfEscape(Text::toT(Text::toDOS(i->text))) + _T("}\n"));
  55.  
  56.         ::SendMessage(handle(), EM_SETCHARFORMAT, SCF_SELECTION, reinterpret_cast<LPARAM>(&i->cf));
  57.         ::SendMessage(handle(), EM_SETTEXTEX, reinterpret_cast<WPARAM>(&config), reinterpret_cast<LPARAM>(line.c_str()));
  58.     }
  59.  
  60.     ::SendMessage(handle(), EM_SETSEL, cr.first-charsRemoved, cr.second-charsRemoved);
  61.  
  62.     if(scroll) {
  63.         const std::pair<int, int> sel = getCaretPosRange();
  64.         ::SendMessage(handle(), EM_SETSEL, length(), -1);
  65.         ::SendMessage(handle(), EM_SCROLLCARET, 0, 0);
  66.         ::SendMessage(handle(), EM_SETSEL, sel.first, sel.second);
  67.         ::SendMessage(handle(), WM_VSCROLL, SB_BOTTOM, 0);
  68.     } else {
  69.         ::SendMessage(handle(), EM_SETSCROLLPOS, 0, reinterpret_cast<LPARAM>(&scrollPos));
  70.     }
  71.  
  72.     if(!scroll)
  73.         ::SendMessage(handle(), WM_SETREDRAW, TRUE, 0);
  74.  
  75.     redraw();
  76. }
Advertisement
Add Comment
Please, Sign In to add comment