riched20: move some selection code from message handler to ME_SetSelection

Matt Finnicum mattfinn at gmail.com
Tue Sep 12 09:44:29 CDT 2006


The special cases handled by EM_EXSETSEL should also be handled by
EM_SETSEL, so this patch moves that code into the common
ME_SetSelection function. Before this patch, sending EM_SETSEL with
any value less than -1 would crash richedit.

Changelog: move some selection code from message handlers to ME_SetSelection
---
 dlls/riched20/caret.c  |   51 ++++++++++++++++++++++++++++++------------------
 dlls/riched20/editor.c |   27 +++----------------------
 2 files changed, 35 insertions(+), 43 deletions(-)
-------------- next part --------------
diff --git a/dlls/riched20/caret.c b/dlls/riched20/caret.c
index ce3abb7..4709cfc 100644
--- a/dlls/riched20/caret.c
+++ b/dlls/riched20/caret.c
@@ -80,39 +80,52 @@ int ME_GetTextLengthEx(ME_TextEditor *ed
 
 void ME_SetSelection(ME_TextEditor *editor, int from, int to)
 {
-  if (from == 0 && to == -1)
+  int tmp;
+  ME_InvalidateSelection(editor);
+  
+  to = max(-1, to);
+  to = min(ME_GetTextLength(editor) +1, to);
+  
+  from = max(-1, from);
+  from = min(ME_GetTextLength(editor) +1, from);
+  
+  if (from == -1 && to == -1)
   {
+    /* deselect and move caret to end of control */
+    editor->pCursors[1].pRun = editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun); 
+    editor->pCursors[1].nOffset = editor->pCursors[0].nOffset = 0; 
+    ME_ClearTempStyle(editor);
+  }
+  else if (from == 0 && to == -1)
+  {
+    /* Select all of the text */
     editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun); 
     editor->pCursors[1].nOffset = 0; 
     editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun); 
     editor->pCursors[0].nOffset = 0; 
-    ME_InvalidateSelection(editor);
-    ME_ClearTempStyle(editor);
-    return;
-  }
-  if (from == -1 && to == -1)	/*-1,-1 means put the selection at the end of the text */
-  {
-    editor->pCursors[1].pRun = editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
-    editor->pCursors[1].nOffset = editor->pCursors[0].nOffset = 0;
-    ME_InvalidateSelection(editor);
     ME_ClearTempStyle(editor);
-    return;
   }
-  if (from == -1)
+  else if (from == -1)
   {
+    /* deselect and move caret to end of current selection */
     editor->pCursors[1] = editor->pCursors[0]; 
     ME_Repaint(editor);
     ME_ClearTempStyle(editor);
-    return;
   }
-  if (from>to)
+  else
   {
-    int tmp = from;
-    from = to;
-    to = tmp;
+    if (from>to)
+    {
+      tmp = from;
+      from = to;
+      to = tmp;
+    }
+    ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
+    ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);
   }
-  ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
-  ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);  
+  
+  ME_InvalidateSelection(editor);
+  ME_SendSelChange(editor);  
 }
 
 
diff --git a/dlls/riched20/editor.c b/dlls/riched20/editor.c
index e3383f2..da71651 100644
--- a/dlls/riched20/editor.c
+++ b/dlls/riched20/editor.c
@@ -1567,10 +1567,7 @@ LRESULT WINAPI RichEditANSIWndProc(HWND 
   }
   case EM_SETSEL:
   {
-    ME_InvalidateSelection(editor);
     ME_SetSelection(editor, wParam, lParam);
-    ME_InvalidateSelection(editor);
-    ME_SendSelChange(editor);
     return 0;
   }
   case EM_SETSCROLLPOS:
@@ -1592,32 +1589,14 @@ LRESULT WINAPI RichEditANSIWndProc(HWND 
   {
     return editor->AutoURLDetect_bEnable;
   }
-  case EM_EXSETSEL:
+   case EM_EXSETSEL:
   {
     int start, end;
     CHARRANGE range = *(CHARRANGE *)lParam;
-
-    TRACE("EM_EXSETSEL (%ld,%ld)\n", range.cpMin, range.cpMax);
-
-    /* if cpMin < 0, then selection is deselected and caret moved to end of
-     * the current selection  */
-    if (range.cpMin < 0)
-    {
-        ME_GetSelection(editor, &start, &end);
-        range.cpMin = end;
-        range.cpMax = end;
-    }  
-    else if (range.cpMax > ME_GetTextLength(editor) +1)
-    {
-      range.cpMax = ME_GetTextLength(editor) + 1;
-    }
-
-    ME_InvalidateSelection(editor);
     ME_SetSelection(editor, range.cpMin, range.cpMax);
-    ME_InvalidateSelection(editor);
-    ME_SendSelChange(editor);
     
-    return range.cpMax;
+    ME_GetSelection(editor, &start, &end);
+    return end;
   }
   case EM_SHOWSCROLLBAR:
   {
-- 
1.4.2


More information about the wine-patches mailing list