title: syntax highlight richedit control not working poperly
body: <p>I am trying to implement a syntax highlight editor with a richedit, it has works well with the current selected line, but I may be missing something. The CRichEdit is my own wrapper implementation of the richedit controller, the problem seems that the text is not selected properly even though I made sure the selected range generated with the code were what I get with the EM_EXGETSEL message.
  The selection seems increasing 1 as the lines goes down, so I decided to ed_source.sendMessage(EM_LINEFROMCHAR, pos, 0) to the range which partially fix the problem except for some few lines where the coloring seems some time one or to position before and the real appropriate, so that is why I thing I may not be understanding something.</p>
  
  <pre><code>void parse(WIN::CRichEdit &amp;ed_source, bool curseline)
  {
      int pos, offset = 0;
      char delimiter[]={" \n\r(){};"}, *tok, *start;
      CStringA s;
      CString text;
      CWnd api;
  
      if(curseline){      
          ed_source.getLine(ed_source.getRow() - 1, text);
          offset = ed_source.sendMessage(EM_LINEINDEX, -1, 0);
      }else{
          text = ed_source.getCaption();
      }
  
      s = text;
      start = s.c_str();
      if(!start) return;
  
      tok = strtok(s.c_str(), delimiter);
  
      CHARRANGE cr = ed_source.getSelecteRange();
      ed_source.sendMessage(EM_HIDESELECTION, 1, 0) ;
      CHARRANGE range;
      while(tok)
      {
          int len = strlen(tok);
  
          pos = (tok - start);
          int x = ed_source.sendMessage(EM_LINEFROMCHAR, pos, 0);
          range.cpMin = offset + pos - x;
          range.cpMax = range.cpMin + len;
  
          ed_source.selectRange(range);
          if(isReserved(tok)){
  
              ed_source.setTextStyle(true, false);
              ed_source.setTextColor(keyboardColor);
          }else
              if(isType(tok)){
                  ed_source.setTextStyle(false, false);
                  ed_source.setTextColor(typeColor);
              }else {
                  ed_source.setTextStyle(false, true);
                  ed_source.setTextColor(textColor);
              }
          tok = strtok(0, delimiter);
      }
  
      ed_source.sendMessage(EM_HIDESELECTION, 0, 0) ;
      ed_source.selectRange(cr);
  }
  </code></pre>
  
  <p>just to be more specific the moment I call the above function is immediately after loading the text on it. I assumed you may want to see the implementation of some of the above functions so here they are.</p>
  
  <pre><code>CHARRANGE CRichEdit::getSelecteRange()
  {
      CHARRANGE crg = {0} ;
      sendMessage(EM_EXGETSEL, 0, (LPARAM)&amp;crg);
      return crg;
  }
  
  void CRichEdit::selectRange(const CHARRANGE &amp;cr)
  {
      sendMessage( EM_EXSETSEL, 0, (LPARAM) &amp;cr);
  }
  
  
  void CRichEdit::setTextColor(COLORREF col)
  { 
      CHARFORMAT format;
      memset(&amp;format, 0, sizeof(CHARFORMAT));
      format.cbSize       = sizeof(CHARFORMAT);
      format.dwMask       = CFM_COLOR;
      format.crTextColor  = col;
  
      sendMessage( EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &amp;format);
  }
  </code></pre>