diff --git a/dlls/riched20/caret.c b/dlls/riched20/caret.c index ce3abb7438d..a58d9f0710b 100644 --- a/dlls/riched20/caret.c +++ b/dlls/riched20/caret.c @@ -78,41 +78,81 @@ int ME_GetTextLengthEx(ME_TextEditor *editor, GETTEXTLENGTHEX *how) } -void ME_SetSelection(ME_TextEditor *editor, int from, int to) +int ME_SetSelection(ME_TextEditor *editor, int from, int to) { + int selectionEnd = 0; + const int len = ME_GetTextLength(editor); + + /* all negative values are effectively the same */ + if (from < 0) + from = -1; + if (to < 0) + to = -1; + + /* select all */ if (from == 0 && to == -1) { - editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun); + 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; + editor->pCursors[0].nOffset = 0; ME_InvalidateSelection(editor); ME_ClearTempStyle(editor); - return; + return len + 1; } - if (from == -1 && to == -1) /*-1,-1 means put the selection at the end of the text */ + + /* if both values are equal and also out of bound, that means to */ + /* put the selection at the end of the text */ + if ((from == to) && (to < 0 || to > len)) + { + selectionEnd = 1; + } + else + { + /* if from is negative and to is positive then selection is */ + /* deselected and caret moved to end of the current selection */ + if (from < 0) + { + int start, end; + ME_GetSelection(editor, &start, &end); + editor->pCursors[1] = editor->pCursors[0]; + ME_Repaint(editor); + ME_ClearTempStyle(editor); + return end; + } + + /* adjust to if it's a negative value */ + if (to < 0) + to = len + 1; + + /* flip from and to if they are reversed */ + if (from>to) + { + int tmp = from; + from = to; + to = tmp; + } + + /* after fiddling with the values, we find from > len && to > len */ + if (from > len) + selectionEnd = 1; + /* special case with to too big */ + else if (to > len) + to = len + 1; + } + + if (selectionEnd) { 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) - { - editor->pCursors[1] = editor->pCursors[0]; - ME_Repaint(editor); - ME_ClearTempStyle(editor); - return; - } - if (from>to) - { - int tmp = from; - from = to; - to = tmp; + return len; } + 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, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset); + return to; } diff --git a/dlls/riched20/editor.c b/dlls/riched20/editor.c index 254bbb74145..738a6fc006e 100644 --- a/dlls/riched20/editor.c +++ b/dlls/riched20/editor.c @@ -1615,70 +1615,17 @@ LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lP } case EM_EXSETSEL: { - int start, end; - int swap; + int end; CHARRANGE range = *(CHARRANGE *)lParam; TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax); - /* all negative values are effectively the same */ - if (range.cpMin < 0) - range.cpMin = -1; - if (range.cpMax < 0) - range.cpMax = -1; - - if (range.cpMin != range.cpMax) - { - /* if cpMin is negative and cpMax is positive 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 - { - /* adjust cpMax if it's a negative value */ - if (range.cpMax < 0) - range.cpMax = ME_GetTextLength(editor) + 1; - - /* flip cpMin and cpMax if they are reversed */ - if (range.cpMin > range.cpMax) - { - swap = range.cpMin; - range.cpMin = range.cpMax; - range.cpMax = swap; - } - - /* special case with cpMin too big */ - if (range.cpMin > ME_GetTextLength(editor)) - { - range.cpMin = ME_GetTextLength(editor); - range.cpMax = ME_GetTextLength(editor); - } - /* special case with cpMax too big */ - else if (range.cpMax > ME_GetTextLength(editor)) - range.cpMax = ME_GetTextLength(editor) + 1; - } - } - else - { - /* special case with cpMin == cpMax */ - /* make sure both values are within bounds */ - if (range.cpMax < 0 || range.cpMax > ME_GetTextLength(editor)) - { - range.cpMin = ME_GetTextLength(editor); - range.cpMax = range.cpMin; - } - } - ME_InvalidateSelection(editor); - ME_SetSelection(editor, range.cpMin, range.cpMax); + end = ME_SetSelection(editor, range.cpMin, range.cpMax); ME_InvalidateSelection(editor); ME_SendSelChange(editor); - return range.cpMax; + return end; } case EM_SHOWSCROLLBAR: { diff --git a/dlls/riched20/editor.h b/dlls/riched20/editor.h index 87742c55140..ec56e3fd286 100644 --- a/dlls/riched20/editor.h +++ b/dlls/riched20/editor.h @@ -178,7 +178,7 @@ void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt); void ME_SetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *mod); /* caret.c */ -void ME_SetSelection(ME_TextEditor *editor, int from, int to); +int ME_SetSelection(ME_TextEditor *editor, int from, int to); void ME_SelectWord(ME_TextEditor *editor); void ME_HideCaret(ME_TextEditor *ed); void ME_ShowCaret(ME_TextEditor *ed);