richedit: Replace offsets arg with ME_Cursor for ME_InternalDeleteText.

Offsets are still used within the function, but this patch reduces the
use of it at the entry to the function.
This commit is contained in:
Dylan Smith 2009-08-13 08:44:18 -04:00 committed by Alexandre Julliard
parent 8f0dfaba30
commit bd4704280d
6 changed files with 62 additions and 51 deletions

View File

@ -281,23 +281,22 @@ void ME_HideCaret(ME_TextEditor *ed)
} }
} }
BOOL ME_InternalDeleteText(ME_TextEditor *editor, int nOfs, int nChars, BOOL ME_InternalDeleteText(ME_TextEditor *editor, ME_Cursor *start,
BOOL bForce) int nChars, BOOL bForce)
{ {
ME_Cursor c; ME_Cursor c = *start;
int nOfs = ME_GetCursorOfs(start);
int shift = 0; int shift = 0;
int totalChars = nChars; int totalChars = nChars;
ME_DisplayItem *start_para; ME_DisplayItem *start_para;
/* Prevent deletion past last end of paragraph run. */ /* Prevent deletion past last end of paragraph run. */
nChars = min(nChars, ME_GetTextLength(editor) - nOfs); nChars = min(nChars, ME_GetTextLength(editor) - nOfs);
ME_CursorFromCharOfs(editor, nOfs, &c);
start_para = c.pPara; start_para = c.pPara;
if (!bForce) if (!bForce)
{ {
ME_ProtectPartialTableDeletion(editor, nOfs, &nChars); ME_ProtectPartialTableDeletion(editor, &c, &nChars);
if (nChars == 0) if (nChars == 0)
return FALSE; return FALSE;
} }
@ -444,11 +443,11 @@ BOOL ME_InternalDeleteText(ME_TextEditor *editor, int nOfs, int nChars,
} }
BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars) BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
{ {
assert(nCursor>=0 && nCursor<editor->nCursors); assert(nCursor>=0 && nCursor<editor->nCursors);
/* text operations set modified state */ /* text operations set modified state */
editor->nModifyStep = 1; editor->nModifyStep = 1;
return ME_InternalDeleteText(editor, ME_GetCursorOfs(&editor->pCursors[nCursor]), return ME_InternalDeleteText(editor, &editor->pCursors[nCursor],
nChars, FALSE); nChars, FALSE);
} }

View File

@ -1015,7 +1015,8 @@ void ME_RTFSpecialCharHook(RTF_Info *info)
info->editor->pCursors[1].nOffset = 0; info->editor->pCursors[1].nOffset = 0;
nOfs = ME_GetCursorOfs(&info->editor->pCursors[1]); nOfs = ME_GetCursorOfs(&info->editor->pCursors[1]);
nChars = ME_GetCursorOfs(&info->editor->pCursors[0]) - nOfs; nChars = ME_GetCursorOfs(&info->editor->pCursors[0]) - nOfs;
ME_InternalDeleteText(info->editor, nOfs, nChars, TRUE); ME_InternalDeleteText(info->editor, &info->editor->pCursors[1],
nChars, TRUE);
} }
para = ME_InsertTableRowEndFromCursor(info->editor); para = ME_InsertTableRowEndFromCursor(info->editor);
@ -1389,15 +1390,18 @@ static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stre
int nEventMask = editor->nEventMask; int nEventMask = editor->nEventMask;
ME_InStream inStream; ME_InStream inStream;
BOOL invalidRTF = FALSE; BOOL invalidRTF = FALSE;
ME_Cursor *selStart, *selEnd;
TRACE("stream==%p editor==%p format==0x%X\n", stream, editor, format); TRACE("stream==%p editor==%p format==0x%X\n", stream, editor, format);
editor->nEventMask = 0; editor->nEventMask = 0;
ME_GetSelectionOfs(editor, &from, &to); ME_GetSelectionOfs(editor, &from, &to);
if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) { if (format & SFF_SELECTION && editor->mode & TM_RICHTEXT)
{
ME_GetSelection(editor, &selStart, &selEnd);
style = ME_GetSelectionInsertStyle(editor); style = ME_GetSelectionInsertStyle(editor);
ME_InternalDeleteText(editor, from, to-from, FALSE); ME_InternalDeleteText(editor, selStart, to - from, FALSE);
/* Don't insert text at the end of the table row */ /* Don't insert text at the end of the table row */
if (!editor->bEmulateVersion10) { /* v4.1 */ if (!editor->bEmulateVersion10) { /* v4.1 */
@ -1422,12 +1426,12 @@ static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stre
ME_IsInTable(editor->pCursors[0].pRun)) ME_IsInTable(editor->pCursors[0].pRun))
return 0; return 0;
} }
} } else {
else {
style = editor->pBuffer->pDefaultStyle; style = editor->pBuffer->pDefaultStyle;
ME_AddRefStyle(style); ME_AddRefStyle(style);
ME_SetSelection(editor, 0, 0); ME_SetSelection(editor, 0, 0);
ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE); ME_InternalDeleteText(editor, &editor->pCursors[1],
ME_GetTextLength(editor), FALSE);
from = to = 0; from = to = 0;
ME_ClearTempStyle(editor); ME_ClearTempStyle(editor);
ME_SetDefaultParaFormat(editor->pCursors[0].pPara->member.para.pFmt); ME_SetDefaultParaFormat(editor->pCursors[0].pPara->member.para.pFmt);
@ -1518,7 +1522,7 @@ static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stre
editor->pCursors[1].nOffset = 0; editor->pCursors[1].nOffset = 0;
nOfs = ME_GetCursorOfs(&editor->pCursors[1]); nOfs = ME_GetCursorOfs(&editor->pCursors[1]);
nChars = ME_GetCursorOfs(&editor->pCursors[0]) - nOfs; nChars = ME_GetCursorOfs(&editor->pCursors[0]) - nOfs;
ME_InternalDeleteText(editor, nOfs, nChars, TRUE); ME_InternalDeleteText(editor, &editor->pCursors[1], nChars, TRUE);
if (parser.tableDef) if (parser.tableDef)
parser.tableDef->tableRowStart = NULL; parser.tableDef->tableRowStart = NULL;
} }
@ -1544,17 +1548,18 @@ static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stre
are converted according to the standard rules: \r for 2.0, \r\n for 1.0 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
*/ */
if (stripLastCR) { if (stripLastCR) {
int newfrom, newto; int newto;
ME_GetSelectionOfs(editor, &newfrom, &newto); ME_GetSelection(editor, &selStart, &selEnd);
newto = ME_GetCursorOfs(selEnd);
if (newto > to + (editor->bEmulateVersion10 ? 1 : 0)) { if (newto > to + (editor->bEmulateVersion10 ? 1 : 0)) {
WCHAR lastchar[3] = {'\0', '\0'}; WCHAR lastchar[3] = {'\0', '\0'};
int linebreakSize = editor->bEmulateVersion10 ? 2 : 1; int linebreakSize = editor->bEmulateVersion10 ? 2 : 1;
ME_Cursor linebreakCursor; ME_Cursor linebreakCursor = *selEnd;
ME_CursorFromCharOfs(editor, newto - linebreakSize, &linebreakCursor); ME_MoveCursorChars(editor, &linebreakCursor, -linebreakSize);
ME_GetTextW(editor, lastchar, 2, &linebreakCursor, linebreakSize, 0); ME_GetTextW(editor, lastchar, 2, &linebreakCursor, linebreakSize, 0);
if (lastchar[0] == '\r' && (lastchar[1] == '\n' || lastchar[1] == '\0')) { if (lastchar[0] == '\r' && (lastchar[1] == '\n' || lastchar[1] == '\0')) {
ME_InternalDeleteText(editor, newto - linebreakSize, linebreakSize, FALSE); ME_InternalDeleteText(editor, &linebreakCursor, linebreakSize, FALSE);
} }
} }
} }
@ -2284,7 +2289,7 @@ ME_KeyDown(ME_TextEditor *editor, WORD nKey)
result = ME_Copy(editor, selStart, nChars); result = ME_Copy(editor, selStart, nChars);
if (result && nKey == 'X') if (result && nKey == 'X')
{ {
ME_InternalDeleteText(editor, nOfs, nChars, FALSE); ME_InternalDeleteText(editor, selStart, nChars, FALSE);
ME_CommitUndo(editor); ME_CommitUndo(editor);
ME_UpdateRepaint(editor); ME_UpdateRepaint(editor);
} }
@ -3222,11 +3227,13 @@ LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
bSelection = (pStruct->flags & ST_SELECTION) != 0; bSelection = (pStruct->flags & ST_SELECTION) != 0;
if (bSelection) { if (bSelection) {
ME_GetSelectionOfs(editor, &from, &to); int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
style = ME_GetSelectionInsertStyle(editor); style = ME_GetSelectionInsertStyle(editor);
ME_InternalDeleteText(editor, from, to - from, FALSE); ME_InternalDeleteText(editor, &editor->pCursors[nStartCursor], to - from, FALSE);
} else { } else {
ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE); ME_Cursor start;
ME_SetCursorToStart(editor, &start);
ME_InternalDeleteText(editor, &start, ME_GetTextLength(editor), FALSE);
style = editor->pBuffer->pDefaultStyle; style = editor->pBuffer->pDefaultStyle;
} }
@ -3417,23 +3424,23 @@ LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
case WM_CLEAR: case WM_CLEAR:
{ {
int from, to; int from, to;
ME_GetSelectionOfs(editor, &from, &to); int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
ME_InternalDeleteText(editor, from, to-from, FALSE); ME_InternalDeleteText(editor, &editor->pCursors[nStartCursor], to-from, FALSE);
ME_CommitUndo(editor); ME_CommitUndo(editor);
ME_UpdateRepaint(editor); ME_UpdateRepaint(editor);
return 0; return 0;
} }
case EM_REPLACESEL: case EM_REPLACESEL:
{ {
int from, to; int from, to, nStartCursor;
ME_Style *style; ME_Style *style;
LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL; LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL;
size_t len = wszText ? lstrlenW(wszText) : 0; size_t len = wszText ? lstrlenW(wszText) : 0;
TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText)); TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
ME_GetSelectionOfs(editor, &from, &to); nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
style = ME_GetSelectionInsertStyle(editor); style = ME_GetSelectionInsertStyle(editor);
ME_InternalDeleteText(editor, from, to-from, FALSE); ME_InternalDeleteText(editor, &editor->pCursors[nStartCursor], to-from, FALSE);
ME_InsertTextFromCursor(editor, 0, wszText, len, style); ME_InsertTextFromCursor(editor, 0, wszText, len, style);
ME_ReleaseStyle(style); ME_ReleaseStyle(style);
/* drop temporary style if line end */ /* drop temporary style if line end */
@ -3482,8 +3489,9 @@ LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
} }
case WM_SETTEXT: case WM_SETTEXT:
{ {
ME_Cursor updateLinksStart; ME_Cursor cursor;
ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE); ME_SetCursorToStart(editor, &cursor);
ME_InternalDeleteText(editor, &cursor, ME_GetTextLength(editor), FALSE);
if (lParam) if (lParam)
{ {
TRACE("WM_SETTEXT lParam==%lx\n",lParam); TRACE("WM_SETTEXT lParam==%lx\n",lParam);
@ -3517,8 +3525,8 @@ LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
} }
else else
TRACE("WM_SETTEXT - NULL\n"); TRACE("WM_SETTEXT - NULL\n");
ME_SetCursorToStart(editor, &updateLinksStart); ME_SetCursorToStart(editor, &cursor);
ME_UpdateLinkAttribute(editor, &updateLinksStart, INT_MAX); ME_UpdateLinkAttribute(editor, &cursor, INT_MAX);
ME_SetSelection(editor, 0, 0); ME_SetSelection(editor, 0, 0);
editor->nModifyStep = 0; editor->nModifyStep = 0;
ME_CommitUndo(editor); ME_CommitUndo(editor);
@ -3541,14 +3549,13 @@ LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
case WM_CUT: case WM_CUT:
case WM_COPY: case WM_COPY:
{ {
int nOfs, nChars; int nFrom, nTo, nStartCur = ME_GetSelectionOfs(editor, &nFrom, &nTo);
int nStartCur = ME_GetSelectionOfs(editor, &nOfs, &nChars); int nChars = nTo - nFrom;
ME_Cursor *selStart = &editor->pCursors[nStartCur]; ME_Cursor *selStart = &editor->pCursors[nStartCur];
nChars -= nOfs;
if (ME_Copy(editor, selStart, nChars) && msg == WM_CUT) if (ME_Copy(editor, selStart, nChars) && msg == WM_CUT)
{ {
ME_InternalDeleteText(editor, nOfs, nChars, FALSE); ME_InternalDeleteText(editor, selStart, nChars, FALSE);
ME_CommitUndo(editor); ME_CommitUndo(editor);
ME_UpdateRepaint(editor); ME_UpdateRepaint(editor);
} }

View File

@ -177,7 +177,7 @@ BOOL ME_IsSelection(ME_TextEditor *editor);
void ME_DeleteSelection(ME_TextEditor *editor); void ME_DeleteSelection(ME_TextEditor *editor);
void ME_SendSelChange(ME_TextEditor *editor); void ME_SendSelChange(ME_TextEditor *editor);
void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor); void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor);
BOOL ME_InternalDeleteText(ME_TextEditor *editor, int nOfs, int nChars, BOOL bForce); BOOL ME_InternalDeleteText(ME_TextEditor *editor, ME_Cursor *start, int nChars, BOOL bForce);
int ME_GetTextLength(ME_TextEditor *editor); int ME_GetTextLength(ME_TextEditor *editor);
int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how); int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how);
ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor); ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor);
@ -264,7 +264,7 @@ ME_DisplayItem *ME_InsertTableRowEndFromCursor(ME_TextEditor *editor);
ME_DisplayItem *ME_GetTableRowEnd(ME_DisplayItem *para); ME_DisplayItem *ME_GetTableRowEnd(ME_DisplayItem *para);
ME_DisplayItem *ME_GetTableRowStart(ME_DisplayItem *para); ME_DisplayItem *ME_GetTableRowStart(ME_DisplayItem *para);
void ME_CheckTablesForCorruption(ME_TextEditor *editor); void ME_CheckTablesForCorruption(ME_TextEditor *editor);
void ME_ProtectPartialTableDeletion(ME_TextEditor *editor, int nOfs,int *nChars); void ME_ProtectPartialTableDeletion(ME_TextEditor *editor, ME_Cursor *c, int *nChars);
ME_DisplayItem* ME_AppendTableRow(ME_TextEditor *editor, ME_DisplayItem *table_row); ME_DisplayItem* ME_AppendTableRow(ME_TextEditor *editor, ME_DisplayItem *table_row);
void ME_TabPressedInTable(ME_TextEditor *editor, BOOL bSelectedRow); void ME_TabPressedInTable(ME_TextEditor *editor, BOOL bSelectedRow);
void ME_MoveCursorFromTableRowStartParagraph(ME_TextEditor *editor); void ME_MoveCursorFromTableRowStartParagraph(ME_TextEditor *editor);

View File

@ -275,13 +275,14 @@ BOOL ME_IsInTable(ME_DisplayItem *pItem)
} }
/* Table rows should either be deleted completely or not at all. */ /* Table rows should either be deleted completely or not at all. */
void ME_ProtectPartialTableDeletion(ME_TextEditor *editor, int nOfs,int *nChars) void ME_ProtectPartialTableDeletion(ME_TextEditor *editor, ME_Cursor *c, int *nChars)
{ {
ME_Cursor c, c2; int nOfs = ME_GetCursorOfs(c);
ME_DisplayItem *this_para, *end_para; ME_Cursor c2 = *c;
ME_CursorFromCharOfs(editor, nOfs, &c); ME_DisplayItem *this_para = c->pPara;
this_para = c.pPara; ME_DisplayItem *end_para;
ME_CursorFromCharOfs(editor, nOfs + *nChars, &c2);
ME_MoveCursorChars(editor, &c2, *nChars);
end_para = c2.pPara; end_para = c2.pPara;
if (c2.pRun->member.run.nFlags & MERF_ENDPARA) { if (c2.pRun->member.run.nFlags & MERF_ENDPARA) {
/* End offset might be in the middle of the end paragraph run. /* End offset might be in the middle of the end paragraph run.
@ -357,13 +358,13 @@ void ME_ProtectPartialTableDeletion(ME_TextEditor *editor, int nOfs,int *nChars)
this_para->member.para.pFmt->dwMask & PFM_TABLE && this_para->member.para.pFmt->dwMask & PFM_TABLE &&
this_para->member.para.pFmt->wEffects & PFE_TABLE) this_para->member.para.pFmt->wEffects & PFE_TABLE)
{ {
pRun = c.pRun; pRun = c->pRun;
/* Find the next tab or end paragraph to use as a delete boundary */ /* Find the next tab or end paragraph to use as a delete boundary */
while (!(pRun->member.run.nFlags & (MERF_TAB|MERF_ENDPARA))) while (!(pRun->member.run.nFlags & (MERF_TAB|MERF_ENDPARA)))
pRun = ME_FindItemFwd(pRun, diRun); pRun = ME_FindItemFwd(pRun, diRun);
nCharsToBoundary = pRun->member.run.nCharOfs nCharsToBoundary = pRun->member.run.nCharOfs
- c.pRun->member.run.nCharOfs - c->pRun->member.run.nCharOfs
- c.nOffset; - c->nOffset;
*nChars = min(*nChars, nCharsToBoundary); *nChars = min(*nChars, nCharsToBoundary);
} else if (end_para->member.para.pFmt->dwMask & PFM_TABLE && } else if (end_para->member.para.pFmt->dwMask & PFM_TABLE &&
end_para->member.para.pFmt->wEffects & PFE_TABLE) end_para->member.para.pFmt->wEffects & PFE_TABLE)

View File

@ -312,9 +312,11 @@ HRESULT WINAPI fnTextSrv_TxSetText(ITextServices *iface,
LPCWSTR pszText) LPCWSTR pszText)
{ {
ICOM_THIS_MULTI(ITextServicesImpl, lpVtbl, iface); ICOM_THIS_MULTI(ITextServicesImpl, lpVtbl, iface);
ME_Cursor cursor;
ME_InternalDeleteText(This->editor, 0, ME_GetTextLength(This->editor), ME_SetCursorToStart(This->editor, &cursor);
FALSE); ME_InternalDeleteText(This->editor, &cursor,
ME_GetTextLength(This->editor), FALSE);
ME_InsertTextFromCursor(This->editor, 0, pszText, -1, ME_InsertTextFromCursor(This->editor, 0, pszText, -1,
This->editor->pBuffer->pDefaultStyle); This->editor->pBuffer->pDefaultStyle);
ME_SetSelection(This->editor, 0, 0); ME_SetSelection(This->editor, 0, 0);

View File

@ -317,7 +317,9 @@ static void ME_PlayUndoItem(ME_TextEditor *editor, ME_DisplayItem *pItem)
} }
case diUndoDeleteRun: case diUndoDeleteRun:
{ {
ME_InternalDeleteText(editor, pUItem->nStart, pUItem->nLen, TRUE); ME_Cursor tmp;
ME_CursorFromCharOfs(editor, pUItem->nStart, &tmp);
ME_InternalDeleteText(editor, &tmp, pUItem->nLen, TRUE);
break; break;
} }
case diUndoJoinParagraphs: case diUndoJoinParagraphs: