richedit: Fixups to move over to reliance on CR and LF counters.
authorAlex Villacís Lasso <a_villacis@palosanto.com>
Sat, 26 Apr 2008 20:26:58 +0000 (15:26 -0500)
committerAlexandre Julliard <julliard@winehq.org>
Tue, 29 Apr 2008 12:54:05 +0000 (14:54 +0200)
Text streamout now honors CR and LF counters.
Tests to pin down required EM_STREAMOUT behavior.

dlls/riched20/editor.c
dlls/riched20/para.c
dlls/riched20/run.c
dlls/riched20/tests/editor.c
dlls/riched20/writer.c
dlls/riched32/tests/editor.c

index dcb21d156c974d5dff5eba59998f582fd2f4a39d..7a0dce70bd7ac581a9ff8d42e07e9dc02c1cbd70 100644 (file)
@@ -3278,6 +3278,9 @@ LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM
     
     editor->bEmulateVersion10 = TRUE;
     editor->pBuffer->pLast->member.para.nCharOfs = 2;
+    assert(editor->pBuffer->pLast->prev->type == diRun);
+    assert(editor->pBuffer->pLast->prev->member.run.nFlags & MERF_ENDPARA);
+    editor->pBuffer->pLast->prev->member.run.nLF = 1;
   }
   return result;
 }
index 6f5291080ea5867e91b2d4948a1e70e04c0874b1..8380a79b425bb165178d0c059b110fef91d2a399 100644 (file)
@@ -73,6 +73,8 @@ void ME_MakeFirstParagraph(ME_TextEditor *editor)
   
   run = ME_MakeRun(style, ME_MakeString(wszParagraphSign), MERF_ENDPARA);
   run->member.run.nCharOfs = 0;
+  run->member.run.nCR = 1;
+  run->member.run.nLF = (editor->bEmulateVersion10) ? 1 : 0;
 
   ME_InsertBefore(text->pLast, para);
   ME_InsertBefore(text->pLast, run);
index e193bd22bc8c51e3a0c06db322ed67348c0ec1d1..c49eb16e67043ef5d76e8d18eb474264808a2fe8 100644 (file)
@@ -127,8 +127,10 @@ void ME_CheckCharOffsets(ME_TextEditor *editor)
           p->member.run.nFlags,
           p->member.run.style->fmt.dwMask & p->member.run.style->fmt.dwEffects);
         assert(ofs == p->member.run.nCharOfs);
-        if (p->member.run.nFlags & MERF_ENDPARA)
-          ofs += (editor->bEmulateVersion10 ? 2 : 1);
+        if (p->member.run.nFlags & MERF_ENDPARA) {
+          assert(p->member.run.nCR + p->member.run.nLF > 0);
+          ofs += p->member.run.nCR + p->member.run.nLF;
+        }
         else
           ofs += ME_StrLen(p->member.run.strText);
         break;
@@ -209,10 +211,12 @@ void ME_RunOfsFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem **
         }
         *ppRun = pNext;
       }
-      /* the handling of bEmulateVersion10 may be a source of many bugs, I'm afraid */
-      eollen = (editor->bEmulateVersion10 ? 2 : 1);
+      /* Recover proper character length of this line break */
+      eollen = (*ppRun)->member.run.nCR + (*ppRun)->member.run.nLF;
       if (nCharOfs >= nParaOfs + (*ppRun)->member.run.nCharOfs &&
         nCharOfs < nParaOfs + (*ppRun)->member.run.nCharOfs + eollen) {
+        /* FIXME: Might cause problems when actually requiring an offset in the
+           middle of a run that is considered a single line break */
         *pOfs = 0;
         return;
       }
index 554d7bad084ad2c380a6c46fc52a0575f68a957d..d78c7f6e9b36dcf20ba7088adf67cf629903d37f 100644 (file)
@@ -1125,6 +1125,60 @@ static void test_WM_SETTEXT()
   DestroyWindow(hwndRichEdit);
 }
 
+static void test_EM_STREAMOUT(void)
+{
+  HWND hwndRichEdit = new_richedit(NULL);
+  int r;
+  EDITSTREAM es;
+  char buf[1024] = {0};
+  char * p;
+
+  const char * TestItem1 = "TestSomeText";
+  const char * TestItem2 = "TestSomeText\r";
+  const char * TestItem3 = "TestSomeText\r\n";
+
+  SendMessage(hwndRichEdit, WM_SETTEXT, 0, (LPARAM) TestItem1);
+  p = buf;
+  es.dwCookie = (DWORD_PTR)&p;
+  es.dwError = 0;
+  es.pfnCallback = test_WM_SETTEXT_esCallback;
+  memset(buf, 0, sizeof(buf));
+  SendMessage(hwndRichEdit, EM_STREAMOUT,
+              (WPARAM)(SF_TEXT), (LPARAM)&es);
+  r = strlen(buf);
+  ok(r == 12, "streamed text length is %d, expecting 12\n", r);
+  ok(strcmp(buf, TestItem1) == 0,
+        "streamed text different, got %s\n", buf);
+
+  SendMessage(hwndRichEdit, WM_SETTEXT, 0, (LPARAM) TestItem2);
+  p = buf;
+  es.dwCookie = (DWORD_PTR)&p;
+  es.dwError = 0;
+  es.pfnCallback = test_WM_SETTEXT_esCallback;
+  memset(buf, 0, sizeof(buf));
+  SendMessage(hwndRichEdit, EM_STREAMOUT,
+              (WPARAM)(SF_TEXT), (LPARAM)&es);
+  r = strlen(buf);
+  /* Here again, \r gets converted to \r\n, like WM_GETTEXT */
+  ok(r == 14, "streamed text length is %d, expecting 14\n", r);
+  ok(strcmp(buf, TestItem3) == 0,
+        "streamed text different from, got %s\n", buf);
+  SendMessage(hwndRichEdit, WM_SETTEXT, 0, (LPARAM) TestItem3);
+  p = buf;
+  es.dwCookie = (DWORD_PTR)&p;
+  es.dwError = 0;
+  es.pfnCallback = test_WM_SETTEXT_esCallback;
+  memset(buf, 0, sizeof(buf));
+  SendMessage(hwndRichEdit, EM_STREAMOUT,
+              (WPARAM)(SF_TEXT), (LPARAM)&es);
+  r = strlen(buf);
+  ok(r == 14, "streamed text length is %d, expecting 14\n", r);
+  ok(strcmp(buf, TestItem3) == 0,
+        "streamed text different, got %s\n", buf);
+
+  DestroyWindow(hwndRichEdit);
+}
+
 static void test_EM_SETTEXTEX(void)
 {
   HWND hwndRichEdit = new_richedit(NULL);
@@ -2952,6 +3006,7 @@ START_TEST( editor )
   test_EM_EXSETSEL();
   test_WM_PASTE();
   test_EM_STREAMIN();
+  test_EM_STREAMOUT();
   test_EM_StreamIn_Undo();
   test_EM_FORMATRANGE();
   test_unicode_conversions();
index 30e9c936e1bf2647104b7d912ae20fcc3507f75e..ce9e8537d7f49dffc1ef8f9694380fe738d83f54 100644 (file)
@@ -711,9 +711,9 @@ ME_StreamOutRTF(ME_TextEditor *editor, ME_OutStream *pStream, int nStart, int nC
             if (!ME_StreamOutPrint(pStream, "\r\n\\par"))
               return FALSE;
           }
-          nChars--;
-          if (editor->bEmulateVersion10 && nChars)
-            nChars--;
+          /* Skip as many characters as required by current line break */
+          nChars -= (p->member.run.nCR <= nChars) ? p->member.run.nCR : nChars;
+          nChars -= (p->member.run.nLF <= nChars) ? p->member.run.nLF : nChars;
         } else if (p->member.run.nFlags & MERF_ENDROW) {
           if (!ME_StreamOutPrint(pStream, "\\line \r\n"))
             return FALSE;
@@ -775,10 +775,39 @@ ME_StreamOutText(ME_TextEditor *editor, ME_OutStream *pStream, int nStart, int n
     if (item->member.run.nFlags & MERF_ENDPARA) {
       static const WCHAR szEOL[2] = { '\r', '\n' };
       
-      if (dwFormat & SF_UNICODE)
-        success = ME_StreamOutMove(pStream, (const char *)szEOL, sizeof(szEOL));
-      else
-        success = ME_StreamOutMove(pStream, "\r\n", 2);
+      if (!editor->bEmulateVersion10) {
+        /* richedit 2.0 - all line breaks are \r\n */
+        if (dwFormat & SF_UNICODE)
+          success = ME_StreamOutMove(pStream, (const char *)szEOL, sizeof(szEOL));
+        else
+          success = ME_StreamOutMove(pStream, "\r\n", 2);
+        assert(nLen == 1);
+      } else {
+        int i; int tnLen;
+
+        /* richedit 1.0 - need to honor actual \r and \n amounts */
+        nLen = item->member.run.nCR + item->member.run.nLF;
+        if (nLen > nChars)
+          nLen = nChars;
+        tnLen = nLen;
+
+        i = 0;
+        while (tnLen > 0 && i < item->member.run.nCR) {
+          if (dwFormat & SF_UNICODE)
+            success = ME_StreamOutMove(pStream, (const char *)(&szEOL[0]), sizeof(WCHAR));
+          else
+            success = ME_StreamOutMove(pStream, "\r", 1);
+          tnLen--; i++;
+        }
+        i = 0;
+        while (tnLen > 0 && i < item->member.run.nLF) {
+          if (dwFormat & SF_UNICODE)
+            success = ME_StreamOutMove(pStream, (const char *)(&szEOL[1]), sizeof(WCHAR));
+          else
+            success = ME_StreamOutMove(pStream, "\n", 1);
+          tnLen--; i++;
+        }
+      }
     } else {
       if (dwFormat & SF_UNICODE)
         success = ME_StreamOutMove(pStream, (const char *)(item->member.run.strText->szData + nStart),
@@ -800,8 +829,6 @@ ME_StreamOutText(ME_TextEditor *editor, ME_OutStream *pStream, int nStart, int n
     }
     
     nChars -= nLen;
-    if (editor->bEmulateVersion10 && nChars && item->member.run.nFlags & MERF_ENDPARA)
-      nChars--;
     nStart = 0;
     item = ME_FindItemFwd(item, diRun);
   }
index c8b71812997b946317a6986a09c94a335c2198a8..4bf35aba52defc83703a6631ee838c46715ec571 100644 (file)
@@ -284,6 +284,74 @@ static void test_EM_STREAMIN(void)
   DestroyWindow(hwndRichEdit);
 }
 
+static DWORD CALLBACK test_WM_SETTEXT_esCallback(DWORD_PTR dwCookie,
+                                         LPBYTE pbBuff,
+                                         LONG cb,
+                                         LONG *pcb)
+{
+  char** str = (char**)dwCookie;
+  *pcb = cb;
+  if (*pcb > 0) {
+    memcpy(*str, pbBuff, *pcb);
+    *str += *pcb;
+  }
+  return 0;
+}
+
+static void test_EM_STREAMOUT(void)
+{
+  HWND hwndRichEdit = new_richedit(NULL);
+  int r;
+  EDITSTREAM es;
+  char buf[1024] = {0};
+  char * p;
+
+  const char * TestItem1 = "TestSomeText";
+  const char * TestItem2 = "TestSomeText\r";
+  const char * TestItem3 = "TestSomeText\r\n";
+
+  SendMessage(hwndRichEdit, WM_SETTEXT, 0, (LPARAM) TestItem1);
+  p = buf;
+  es.dwCookie = (DWORD_PTR)&p;
+  es.dwError = 0;
+  es.pfnCallback = test_WM_SETTEXT_esCallback;
+  memset(buf, 0, sizeof(buf));
+  SendMessage(hwndRichEdit, EM_STREAMOUT,
+              (WPARAM)(SF_TEXT), (LPARAM)&es);
+  r = strlen(buf);
+  ok(r == 12, "streamed text length is %d, expecting 12\n", r);
+  ok(strcmp(buf, TestItem1) == 0,
+        "streamed text different, got %s\n", buf);
+
+  SendMessage(hwndRichEdit, WM_SETTEXT, 0, (LPARAM) TestItem2);
+  p = buf;
+  es.dwCookie = (DWORD_PTR)&p;
+  es.dwError = 0;
+  es.pfnCallback = test_WM_SETTEXT_esCallback;
+  memset(buf, 0, sizeof(buf));
+  SendMessage(hwndRichEdit, EM_STREAMOUT,
+              (WPARAM)(SF_TEXT), (LPARAM)&es);
+  r = strlen(buf);
+  todo_wine { /* Currently fails because of solitary \r mangling */
+  ok(r == 13, "streamed text length is %d, expecting 13\n", r);
+  ok(strcmp(buf, TestItem2) == 0,
+        "streamed text different, got %s\n", buf);
+  }
+  SendMessage(hwndRichEdit, WM_SETTEXT, 0, (LPARAM) TestItem3);
+  p = buf;
+  es.dwCookie = (DWORD_PTR)&p;
+  es.dwError = 0;
+  es.pfnCallback = test_WM_SETTEXT_esCallback;
+  memset(buf, 0, sizeof(buf));
+  SendMessage(hwndRichEdit, EM_STREAMOUT,
+              (WPARAM)(SF_TEXT), (LPARAM)&es);
+  r = strlen(buf);
+  ok(r == 14, "streamed text length is %d, expecting 14\n", r);
+  ok(strcmp(buf, TestItem3) == 0,
+        "streamed text different, got %s\n", buf);
+
+  DestroyWindow(hwndRichEdit);
+}
 
 START_TEST( editor )
 {
@@ -298,6 +366,7 @@ START_TEST( editor )
   test_WM_SETTEXT();
   test_WM_GETTEXTLENGTH();
   test_EM_STREAMIN();
+  test_EM_STREAMOUT();
 
   /* Set the environment variable WINETEST_RICHED32 to keep windows
    * responsive and open for 30 seconds. This is useful for debugging.