Commit 57c2580a authored by Huw Davies's avatar Huw Davies Committed by Alexandre Julliard

riched20: Simplify the cursor from char offset function.

parent 1994d21d
...@@ -201,7 +201,7 @@ int set_selection_cursors(ME_TextEditor *editor, int from, int to) ...@@ -201,7 +201,7 @@ int set_selection_cursors(ME_TextEditor *editor, int from, int to)
return len; return len;
} }
ME_CursorFromCharOfs(editor, from, &editor->pCursors[1]); cursor_from_char_ofs( editor, from, &editor->pCursors[1] );
editor->pCursors[0] = editor->pCursors[1]; editor->pCursors[0] = editor->pCursors[1];
ME_MoveCursorChars(editor, &editor->pCursors[0], to - from, FALSE); ME_MoveCursorChars(editor, &editor->pCursors[0], to - from, FALSE);
/* Selection is not allowed in the middle of an end paragraph run. */ /* Selection is not allowed in the middle of an end paragraph run. */
...@@ -326,7 +326,7 @@ BOOL ME_InternalDeleteText(ME_TextEditor *editor, ME_Cursor *start, ...@@ -326,7 +326,7 @@ BOOL ME_InternalDeleteText(ME_TextEditor *editor, ME_Cursor *start,
while(nChars > 0) while(nChars > 0)
{ {
ME_Run *run; ME_Run *run;
ME_CursorFromCharOfs(editor, nOfs+nChars, &c); cursor_from_char_ofs( editor, nOfs + nChars, &c );
if (!c.nOffset && if (!c.nOffset &&
nOfs+nChars == (c.pRun->member.run.nCharOfs nOfs+nChars == (c.pRun->member.run.nCharOfs
+ c.pPara->member.para.nCharOfs)) + c.pPara->member.para.nCharOfs))
......
...@@ -1788,7 +1788,7 @@ static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stre ...@@ -1788,7 +1788,7 @@ static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stre
/* put the cursor at the top */ /* put the cursor at the top */
if (!(format & SFF_SELECTION)) if (!(format & SFF_SELECTION))
set_selection_cursors(editor, 0, 0); set_selection_cursors(editor, 0, 0);
ME_CursorFromCharOfs(editor, from, &start); cursor_from_char_ofs( editor, from, &start );
ME_UpdateLinkAttribute(editor, &start, to - from); ME_UpdateLinkAttribute(editor, &start, to - from);
} }
...@@ -1917,12 +1917,11 @@ ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCH ...@@ -1917,12 +1917,11 @@ ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCH
/* If possible, find the character before where the search starts */ /* If possible, find the character before where the search starts */
if ((flags & FR_WHOLEWORD) && nMin) if ((flags & FR_WHOLEWORD) && nMin)
{ {
ME_CursorFromCharOfs(editor, nMin - 1, &cursor); cursor_from_char_ofs( editor, nMin - 1, &cursor );
wLastChar = *get_text( &cursor.pRun->member.run, cursor.nOffset ); wLastChar = *get_text( &cursor.pRun->member.run, cursor.nOffset );
ME_MoveCursorChars(editor, &cursor, 1, FALSE); ME_MoveCursorChars(editor, &cursor, 1, FALSE);
} else {
ME_CursorFromCharOfs(editor, nMin, &cursor);
} }
else cursor_from_char_ofs( editor, nMin, &cursor );
while (cursor.pRun && ME_GetCursorOfs(&cursor) + nLen <= nMax) while (cursor.pRun && ME_GetCursorOfs(&cursor) + nLen <= nMax)
{ {
...@@ -1993,12 +1992,11 @@ ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCH ...@@ -1993,12 +1992,11 @@ ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCH
/* If possible, find the character after where the search ends */ /* If possible, find the character after where the search ends */
if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1) if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
{ {
ME_CursorFromCharOfs(editor, nMax + 1, &cursor); cursor_from_char_ofs( editor, nMax + 1, &cursor );
wLastChar = *get_text( &cursor.pRun->member.run, cursor.nOffset ); wLastChar = *get_text( &cursor.pRun->member.run, cursor.nOffset );
ME_MoveCursorChars(editor, &cursor, -1, FALSE); ME_MoveCursorChars(editor, &cursor, -1, FALSE);
} else {
ME_CursorFromCharOfs(editor, nMax, &cursor);
} }
else cursor_from_char_ofs( editor, nMax, &cursor );
while (cursor.pRun && ME_GetCursorOfs(&cursor) - nLen >= nMin) while (cursor.pRun && ME_GetCursorOfs(&cursor) - nLen >= nMin)
{ {
...@@ -3009,7 +3007,7 @@ static LONG ME_GetSelectionType(ME_TextEditor *editor) ...@@ -3009,7 +3007,7 @@ static LONG ME_GetSelectionType(ME_TextEditor *editor)
{ {
ME_Cursor cursor; ME_Cursor cursor;
ME_CursorFromCharOfs(editor, start + i, &cursor); cursor_from_char_ofs( editor, start + i, &cursor );
if (cursor.pRun->member.run.reobj) if (cursor.pRun->member.run.reobj)
object_count++; object_count++;
else else
...@@ -4205,7 +4203,7 @@ LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam, ...@@ -4205,7 +4203,7 @@ LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
nEnd = textlength; nEnd = textlength;
if (nStart >= nEnd) return 0; if (nStart >= nEnd) return 0;
ME_CursorFromCharOfs(editor, nStart, &start); cursor_from_char_ofs( editor, nStart, &start );
return ME_GetTextRange(editor, rng->lpstrText, &start, nEnd - nStart, unicode); return ME_GetTextRange(editor, rng->lpstrText, &start, nEnd - nStart, unicode);
} }
case EM_GETLINE: case EM_GETLINE:
...@@ -4313,7 +4311,7 @@ LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam, ...@@ -4313,7 +4311,7 @@ LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
{ {
ME_DisplayItem *item, *item_end; ME_DisplayItem *item, *item_end;
int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0; int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
ME_DisplayItem *para, *run; ME_Cursor cursor;
if (wParam > ME_GetTextLength(editor)) if (wParam > ME_GetTextLength(editor))
return 0; return 0;
...@@ -4322,19 +4320,20 @@ LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam, ...@@ -4322,19 +4320,20 @@ LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n"); FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
return 0; return 0;
} }
ME_RunOfsFromCharOfs(editor, wParam, &para, &run, NULL); cursor_from_char_ofs( editor, wParam, &cursor );
item = ME_RowStart(run); item = ME_RowStart( cursor.pRun );
nThisLineOfs = ME_CharOfsFromRunOfs(editor, para, ME_FindItemFwd(item, diRun), 0); nThisLineOfs = ME_CharOfsFromRunOfs( editor, cursor.pPara, ME_FindItemFwd( item, diRun ), 0 );
item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd); item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
if (item_end->type == diStartRow) { if (item_end->type == diStartRow)
nNextLineOfs = ME_CharOfsFromRunOfs(editor, para, ME_FindItemFwd(item_end, diRun), 0); nNextLineOfs = ME_CharOfsFromRunOfs(editor, cursor.pPara, ME_FindItemFwd( item_end, diRun ), 0);
} else { else
{
ME_DisplayItem *endRun = ME_FindItemBack(item_end, diRun); ME_DisplayItem *endRun = ME_FindItemBack(item_end, diRun);
assert(endRun && endRun->member.run.nFlags & MERF_ENDPARA); assert(endRun && endRun->member.run.nFlags & MERF_ENDPARA);
nNextLineOfs = item_end->member.para.nCharOfs - endRun->member.run.len; nNextLineOfs = item_end->member.para.nCharOfs - endRun->member.run.len;
} }
nChars = nNextLineOfs - nThisLineOfs; nChars = nNextLineOfs - nThisLineOfs;
TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars); TRACE("EM_LINELENGTH(%ld)==%d\n", wParam, nChars);
return nChars; return nChars;
} }
case EM_EXLIMITTEXT: case EM_EXLIMITTEXT:
...@@ -4424,8 +4423,8 @@ LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam, ...@@ -4424,8 +4423,8 @@ LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
} }
case EM_POSFROMCHAR: case EM_POSFROMCHAR:
{ {
ME_DisplayItem *pPara, *pRun; ME_Cursor cursor;
int nCharOfs, nOffset, nLength; int nCharOfs, nLength;
POINTL pt = {0,0}; POINTL pt = {0,0};
nCharOfs = wParam; nCharOfs = wParam;
...@@ -4436,11 +4435,11 @@ LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam, ...@@ -4436,11 +4435,11 @@ LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
nCharOfs = min(nCharOfs, nLength); nCharOfs = min(nCharOfs, nLength);
nCharOfs = max(nCharOfs, 0); nCharOfs = max(nCharOfs, 0);
ME_RunOfsFromCharOfs(editor, nCharOfs, &pPara, &pRun, &nOffset); cursor_from_char_ofs( editor, nCharOfs, &cursor );
assert(pRun->type == diRun); pt.y = cursor.pRun->member.run.pt.y;
pt.y = pRun->member.run.pt.y; pt.x = cursor.pRun->member.run.pt.x +
pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset, TRUE); ME_PointFromChar( editor, &cursor.pRun->member.run, cursor.nOffset, TRUE );
pt.y += pPara->member.para.pt.y + editor->rcFormat.top; pt.y += cursor.pPara->member.para.pt.y + editor->rcFormat.top;
pt.x += editor->rcFormat.left; pt.x += editor->rcFormat.left;
pt.x -= editor->horz_si.nPos; pt.x -= editor->horz_si.nPos;
......
...@@ -118,6 +118,8 @@ ME_DisplayItem *ME_FindRowWithNumber(ME_TextEditor *editor, int nRow) DECLSPEC_H ...@@ -118,6 +118,8 @@ ME_DisplayItem *ME_FindRowWithNumber(ME_TextEditor *editor, int nRow) DECLSPEC_H
int ME_RowNumberFromCharOfs(ME_TextEditor *editor, int nOfs) DECLSPEC_HIDDEN; int ME_RowNumberFromCharOfs(ME_TextEditor *editor, int nOfs) DECLSPEC_HIDDEN;
/* run.c */ /* run.c */
void cursor_from_char_ofs( ME_TextEditor *editor, int char_ofs, ME_Cursor *cursor ) DECLSPEC_HIDDEN;
ME_Run *run_create( ME_Style *s, int nFlags ) DECLSPEC_HIDDEN; ME_Run *run_create( ME_Style *s, int nFlags ) DECLSPEC_HIDDEN;
ME_Run *run_insert( ME_TextEditor *editor, ME_Cursor *cursor, ME_Run *run_insert( ME_TextEditor *editor, ME_Cursor *cursor,
ME_Style *style, const WCHAR *str, int len, int flags ) DECLSPEC_HIDDEN; ME_Style *style, const WCHAR *str, int len, int flags ) DECLSPEC_HIDDEN;
...@@ -138,8 +140,6 @@ ME_Run *run_split( ME_TextEditor *editor, ME_Cursor *cursor ) DECLSPEC_HIDDEN; ...@@ -138,8 +140,6 @@ ME_Run *run_split( ME_TextEditor *editor, ME_Cursor *cursor ) DECLSPEC_HIDDEN;
void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run) DECLSPEC_HIDDEN; void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run) DECLSPEC_HIDDEN;
SIZE ME_GetRunSizeCommon(ME_Context *c, const ME_Paragraph *para, ME_Run *run, int nLen, SIZE ME_GetRunSizeCommon(ME_Context *c, const ME_Paragraph *para, ME_Run *run, int nLen,
int startx, int *pAscent, int *pDescent) DECLSPEC_HIDDEN; int startx, int *pAscent, int *pDescent) DECLSPEC_HIDDEN;
void ME_CursorFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_Cursor *pCursor) DECLSPEC_HIDDEN;
void ME_RunOfsFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem **ppPara, ME_DisplayItem **ppRun, int *pOfs) DECLSPEC_HIDDEN;
int ME_CharOfsFromRunOfs(ME_TextEditor *editor, const ME_DisplayItem *pPara, const ME_DisplayItem *pRun, int nOfs) DECLSPEC_HIDDEN; int ME_CharOfsFromRunOfs(ME_TextEditor *editor, const ME_DisplayItem *pPara, const ME_DisplayItem *pRun, int nOfs) DECLSPEC_HIDDEN;
void ME_SkipAndPropagateCharOffset(ME_DisplayItem *p, int shift) DECLSPEC_HIDDEN; void ME_SkipAndPropagateCharOffset(ME_DisplayItem *p, int shift) DECLSPEC_HIDDEN;
void ME_SetCharFormat(ME_TextEditor *editor, ME_Cursor *start, ME_Cursor *end, CHARFORMAT2W *pFmt) DECLSPEC_HIDDEN; void ME_SetCharFormat(ME_TextEditor *editor, ME_Cursor *start, ME_Cursor *end, CHARFORMAT2W *pFmt) DECLSPEC_HIDDEN;
......
...@@ -444,7 +444,7 @@ static HRESULT get_textfont_prop_for_pos(const IRichEditOleImpl *reole, int pos, ...@@ -444,7 +444,7 @@ static HRESULT get_textfont_prop_for_pos(const IRichEditOleImpl *reole, int pos,
fmt.cbSize = sizeof(fmt); fmt.cbSize = sizeof(fmt);
fmt.dwMask = textfont_prop_masks[propid][0]; fmt.dwMask = textfont_prop_masks[propid][0];
ME_CursorFromCharOfs(reole->editor, pos, &from); cursor_from_char_ofs( reole->editor, pos, &from );
to = from; to = from;
ME_MoveCursorChars(reole->editor, &to, 1, FALSE); ME_MoveCursorChars(reole->editor, &to, 1, FALSE);
ME_GetCharFormat(reole->editor, &from, &to, &fmt); ME_GetCharFormat(reole->editor, &from, &to, &fmt);
...@@ -662,7 +662,8 @@ static void textrange_set_font(ITextRange *range, ITextFont *font) ...@@ -662,7 +662,8 @@ static void textrange_set_font(ITextRange *range, ITextFont *font)
fmt.wWeight = value; fmt.wWeight = value;
} }
if (fmt.dwMask) { if (fmt.dwMask)
{
const IRichEditOleImpl *reole = get_range_reole(range); const IRichEditOleImpl *reole = get_range_reole(range);
ME_Cursor from, to; ME_Cursor from, to;
LONG start, end; LONG start, end;
...@@ -670,8 +671,8 @@ static void textrange_set_font(ITextRange *range, ITextFont *font) ...@@ -670,8 +671,8 @@ static void textrange_set_font(ITextRange *range, ITextFont *font)
ITextRange_GetStart(range, &start); ITextRange_GetStart(range, &start);
ITextRange_GetEnd(range, &end); ITextRange_GetEnd(range, &end);
ME_CursorFromCharOfs(reole->editor, start, &from); cursor_from_char_ofs( reole->editor, start, &from );
ME_CursorFromCharOfs(reole->editor, end, &to); cursor_from_char_ofs( reole->editor, end, &to );
ME_SetCharFormat(reole->editor, &from, &to, &fmt); ME_SetCharFormat(reole->editor, &from, &to, &fmt);
} }
} }
...@@ -828,8 +829,8 @@ static HRESULT set_textfont_prop(ITextFontImpl *font, enum textfont_prop_id prop ...@@ -828,8 +829,8 @@ static HRESULT set_textfont_prop(ITextFontImpl *font, enum textfont_prop_id prop
ITextRange_GetStart(font->range, &start); ITextRange_GetStart(font->range, &start);
ITextRange_GetEnd(font->range, &end); ITextRange_GetEnd(font->range, &end);
ME_CursorFromCharOfs(reole->editor, start, &from); cursor_from_char_ofs( reole->editor, start, &from );
ME_CursorFromCharOfs(reole->editor, end, &to); cursor_from_char_ofs( reole->editor, end, &to );
ME_SetCharFormat(reole->editor, &from, &to, &fmt); ME_SetCharFormat(reole->editor, &from, &to, &fmt);
return S_OK; return S_OK;
...@@ -1349,12 +1350,15 @@ IRichEditOle_fnGetClipboardData(IRichEditOle *me, CHARRANGE *lpchrg, ...@@ -1349,12 +1350,15 @@ IRichEditOle_fnGetClipboardData(IRichEditOle *me, CHARRANGE *lpchrg,
TRACE("(%p,%p,%d)\n",This, lpchrg, reco); TRACE("(%p,%p,%d)\n",This, lpchrg, reco);
if(!lplpdataobj) if(!lplpdataobj)
return E_INVALIDARG; return E_INVALIDARG;
if(!lpchrg) { if(!lpchrg)
{
int nFrom, nTo, nStartCur = ME_GetSelectionOfs(This->editor, &nFrom, &nTo); int nFrom, nTo, nStartCur = ME_GetSelectionOfs(This->editor, &nFrom, &nTo);
start = This->editor->pCursors[nStartCur]; start = This->editor->pCursors[nStartCur];
nChars = nTo - nFrom; nChars = nTo - nFrom;
} else { }
ME_CursorFromCharOfs(This->editor, lpchrg->cpMin, &start); else
{
cursor_from_char_ofs( This->editor, lpchrg->cpMin, &start );
nChars = lpchrg->cpMax - lpchrg->cpMin; nChars = lpchrg->cpMax - lpchrg->cpMin;
} }
return ME_GetDataObject(This->editor, &start, nChars, lplpdataobj); return ME_GetDataObject(This->editor, &start, nChars, lplpdataobj);
...@@ -1385,7 +1389,7 @@ IRichEditOle_fnGetObject(IRichEditOle *me, LONG iob, ...@@ -1385,7 +1389,7 @@ IRichEditOle_fnGetObject(IRichEditOle *me, LONG iob,
ME_Cursor cursor; ME_Cursor cursor;
TRACE("character offset: %d\n", lpreobject->cp); TRACE("character offset: %d\n", lpreobject->cp);
ME_CursorFromCharOfs(This->editor, lpreobject->cp, &cursor); cursor_from_char_ofs( This->editor, lpreobject->cp, &cursor );
if (!cursor.pRun->member.run.reobj) if (!cursor.pRun->member.run.reobj)
return E_INVALIDARG; return E_INVALIDARG;
else else
...@@ -1647,8 +1651,8 @@ static HRESULT WINAPI ITextRange_fnGetText(ITextRange *me, BSTR *str) ...@@ -1647,8 +1651,8 @@ static HRESULT WINAPI ITextRange_fnGetText(ITextRange *me, BSTR *str)
} }
editor = This->child.reole->editor; editor = This->child.reole->editor;
ME_CursorFromCharOfs(editor, This->start, &start); cursor_from_char_ofs( editor, This->start, &start );
ME_CursorFromCharOfs(editor, This->end, &end); cursor_from_char_ofs( editor, This->end, &end );
length = This->end - This->start; length = This->end - This->start;
*str = SysAllocStringLen(NULL, length); *str = SysAllocStringLen(NULL, length);
...@@ -1676,12 +1680,14 @@ static HRESULT WINAPI ITextRange_fnSetText(ITextRange *me, BSTR str) ...@@ -1676,12 +1680,14 @@ static HRESULT WINAPI ITextRange_fnSetText(ITextRange *me, BSTR str)
editor = This->child.reole->editor; editor = This->child.reole->editor;
/* delete only where's something to delete */ /* delete only where's something to delete */
if (This->start != This->end) { if (This->start != This->end)
ME_CursorFromCharOfs(editor, This->start, &cursor); {
cursor_from_char_ofs( editor, This->start, &cursor );
ME_InternalDeleteText(editor, &cursor, This->end - This->start, FALSE); ME_InternalDeleteText(editor, &cursor, This->end - This->start, FALSE);
} }
if (!str || !*str) { if (!str || !*str)
{
/* will update this range as well */ /* will update this range as well */
textranges_update_ranges(This->child.reole, This->start, This->end, RANGE_UPDATE_DELETE); textranges_update_ranges(This->child.reole, This->start, This->end, RANGE_UPDATE_DELETE);
return S_OK; return S_OK;
...@@ -1690,7 +1696,7 @@ static HRESULT WINAPI ITextRange_fnSetText(ITextRange *me, BSTR str) ...@@ -1690,7 +1696,7 @@ static HRESULT WINAPI ITextRange_fnSetText(ITextRange *me, BSTR str)
/* it's safer not to rely on stored BSTR length */ /* it's safer not to rely on stored BSTR length */
len = lstrlenW(str); len = lstrlenW(str);
cursor = editor->pCursors[0]; cursor = editor->pCursors[0];
ME_CursorFromCharOfs(editor, This->start, &editor->pCursors[0]); cursor_from_char_ofs( editor, This->start, &editor->pCursors[0] );
style = ME_GetInsertStyle(editor, 0); style = ME_GetInsertStyle(editor, 0);
ME_InsertTextFromCursor(editor, 0, str, len, style); ME_InsertTextFromCursor(editor, 0, str, len, style);
ME_ReleaseStyle(style); ME_ReleaseStyle(style);
...@@ -1729,7 +1735,7 @@ static HRESULT WINAPI ITextRange_fnGetChar(ITextRange *me, LONG *pch) ...@@ -1729,7 +1735,7 @@ static HRESULT WINAPI ITextRange_fnGetChar(ITextRange *me, LONG *pch)
return E_INVALIDARG; return E_INVALIDARG;
editor = This->child.reole->editor; editor = This->child.reole->editor;
ME_CursorFromCharOfs(editor, This->start, &cursor); cursor_from_char_ofs( editor, This->start, &cursor );
return range_GetChar(editor, &cursor, pch); return range_GetChar(editor, &cursor, pch);
} }
...@@ -2213,18 +2219,16 @@ static HRESULT textrange_endof(ITextRange *range, ME_TextEditor *editor, LONG un ...@@ -2213,18 +2219,16 @@ static HRESULT textrange_endof(ITextRange *range, ME_TextEditor *editor, LONG un
{ {
moved = 0; moved = 0;
new_end = old_end; new_end = old_end;
if (old_end == 0) { if (old_end == 0)
{
ME_Cursor cursor; ME_Cursor cursor;
ME_CursorFromCharOfs(editor, old_end, &cursor); cursor_from_char_ofs( editor, old_end, &cursor );
moved = ME_MoveCursorChars(editor, &cursor, 1, TRUE); moved = ME_MoveCursorChars(editor, &cursor, 1, TRUE);
new_end = old_end + moved; new_end = old_end + moved;
} else {
if (extend == tomMove) {
if (old_start != old_end) {
moved = 1;
}
}
} }
else if (extend == tomMove && old_start != old_end)
moved = 1;
ITextRange_SetEnd(range, new_end); ITextRange_SetEnd(range, new_end);
if (extend == tomMove) if (extend == tomMove)
ITextRange_SetStart(range, new_end); ITextRange_SetStart(range, new_end);
...@@ -2275,32 +2279,36 @@ static HRESULT textrange_move(ITextRange *range, ME_TextEditor *editor, LONG uni ...@@ -2275,32 +2279,36 @@ static HRESULT textrange_move(ITextRange *range, ME_TextEditor *editor, LONG uni
{ {
ME_Cursor cursor; ME_Cursor cursor;
if (count > 0) { if (count > 0)
ME_CursorFromCharOfs(editor, old_end, &cursor); {
cursor_from_char_ofs( editor, old_end, &cursor );
move_by = count; move_by = count;
if (old_start != old_end) if (old_start != old_end)
--move_by; --move_by;
} else { }
ME_CursorFromCharOfs(editor, old_start, &cursor); else
{
cursor_from_char_ofs( editor, old_start, &cursor );
move_by = count; move_by = count;
if (old_start != old_end) if (old_start != old_end)
++move_by; ++move_by;
} }
moved = ME_MoveCursorChars(editor, &cursor, move_by, FALSE); moved = ME_MoveCursorChars(editor, &cursor, move_by, FALSE);
if (count > 0) { if (count > 0)
{
new_end = old_end + moved; new_end = old_end + moved;
new_start = new_end; new_start = new_end;
if (old_start != old_end) if (old_start != old_end)
++moved; ++moved;
} else { }
else
{
new_start = old_start + moved; new_start = old_start + moved;
new_end = new_start; new_end = new_start;
if (old_start != old_end) if (old_start != old_end)
--moved; --moved;
} }
if (delta) { if (delta) *delta = moved;
*delta = moved;
}
break; break;
} }
default: default:
...@@ -2348,7 +2356,7 @@ static HRESULT textrange_movestart(ITextRange *range, ME_TextEditor *editor, LON ...@@ -2348,7 +2356,7 @@ static HRESULT textrange_movestart(ITextRange *range, ME_TextEditor *editor, LON
ME_Cursor cursor; ME_Cursor cursor;
LONG moved; LONG moved;
ME_CursorFromCharOfs(editor, old_start, &cursor); cursor_from_char_ofs( editor, old_start, &cursor );
moved = ME_MoveCursorChars(editor, &cursor, count, FALSE); moved = ME_MoveCursorChars(editor, &cursor, count, FALSE);
new_start = old_start + moved; new_start = old_start + moved;
new_end = old_end; new_end = old_end;
...@@ -2404,7 +2412,7 @@ static HRESULT textrange_moveend(ITextRange *range, ME_TextEditor *editor, LONG ...@@ -2404,7 +2412,7 @@ static HRESULT textrange_moveend(ITextRange *range, ME_TextEditor *editor, LONG
ME_Cursor cursor; ME_Cursor cursor;
LONG moved; LONG moved;
ME_CursorFromCharOfs(editor, old_end, &cursor); cursor_from_char_ofs( editor, old_end, &cursor );
moved = ME_MoveCursorChars(editor, &cursor, count, TRUE); moved = ME_MoveCursorChars(editor, &cursor, count, TRUE);
new_start = old_start; new_start = old_start;
new_end = old_end + moved; new_end = old_end + moved;
...@@ -2700,11 +2708,11 @@ static HRESULT WINAPI ITextRange_fnScrollIntoView(ITextRange *me, LONG value) ...@@ -2700,11 +2708,11 @@ static HRESULT WINAPI ITextRange_fnScrollIntoView(ITextRange *me, LONG value)
switch (value) switch (value)
{ {
case tomStart: case tomStart:
ME_CursorFromCharOfs(editor, This->start, &cursor); cursor_from_char_ofs( editor, This->start, &cursor );
ME_GetCursorCoordinates(editor, &cursor, &x, &y, &height); ME_GetCursorCoordinates(editor, &cursor, &x, &y, &height);
break; break;
case tomEnd: case tomEnd:
ME_CursorFromCharOfs(editor, This->end, &cursor); cursor_from_char_ofs( editor, This->end, &cursor );
ME_GetCursorCoordinates(editor, &cursor, &x, &y, &height); ME_GetCursorCoordinates(editor, &cursor, &x, &y, &height);
break; break;
default: default:
......
...@@ -202,58 +202,38 @@ int ME_CharOfsFromRunOfs(ME_TextEditor *editor, const ME_DisplayItem *pPara, ...@@ -202,58 +202,38 @@ int ME_CharOfsFromRunOfs(ME_TextEditor *editor, const ME_DisplayItem *pPara,
} }
/****************************************************************************** /******************************************************************************
* ME_CursorFromCharOfs * cursor_from_char_ofs
* *
* Converts a character offset (relative to the start of the document) to * Converts a character offset (relative to the start of the document) to
* a cursor structure (which contains a run and a position relative to that * a cursor structure (which contains a run and a position relative to that
* run). * run).
*/ */
void ME_CursorFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_Cursor *pCursor) void cursor_from_char_ofs( ME_TextEditor *editor, int char_ofs, ME_Cursor *cursor )
{ {
ME_RunOfsFromCharOfs(editor, nCharOfs, &pCursor->pPara, ME_Paragraph *para;
&pCursor->pRun, &pCursor->nOffset); ME_Run *run;
}
/****************************************************************************** char_ofs = min( max( char_ofs, 0 ), ME_GetTextLength( editor ) );
* ME_RunOfsFromCharOfs
*
* Find a run and relative character offset given an absolute character offset
* (absolute offset being an offset relative to the start of the document).
* Kind of a "global to local" offset conversion.
*/
void ME_RunOfsFromCharOfs(ME_TextEditor *editor,
int nCharOfs,
ME_DisplayItem **ppPara,
ME_DisplayItem **ppRun,
int *pOfs)
{
ME_DisplayItem *item, *next_item;
nCharOfs = max(nCharOfs, 0); /* Find the paragraph at the offset. */
nCharOfs = min(nCharOfs, ME_GetTextLength(editor)); for (para = editor_first_para( editor );
para_next( para )->nCharOfs <= char_ofs;
para = para_next( para ))
;
/* Find the paragraph at the offset. */ char_ofs -= para->nCharOfs;
next_item = editor->pBuffer->pFirst->member.para.next_para;
do { /* Find the run at the offset. */
item = next_item; for (run = para_first_run( para );
next_item = item->member.para.next_para; run_next( run ) && run_next( run )->nCharOfs <= char_ofs;
} while (next_item->member.para.nCharOfs <= nCharOfs); run = run_next( run ))
assert(item->type == diParagraph); ;
nCharOfs -= item->member.para.nCharOfs;
if (ppPara) *ppPara = item; char_ofs -= run->nCharOfs;
/* Find the run at the offset. */ cursor->pPara = para_get_di( para );
next_item = ME_FindItemFwd(item, diRun); cursor->pRun = run_get_di( run );
do { cursor->nOffset = char_ofs;
item = next_item;
next_item = ME_FindItemFwd(item, diRunOrParagraphOrEnd);
} while (next_item->type == diRun &&
next_item->member.run.nCharOfs <= nCharOfs);
assert(item->type == diRun);
nCharOfs -= item->member.run.nCharOfs;
if (ppRun) *ppRun = item;
if (pOfs) *pOfs = nCharOfs;
} }
/****************************************************************************** /******************************************************************************
......
...@@ -269,7 +269,7 @@ DECLSPEC_HIDDEN HRESULT __thiscall fnTextSrv_TxGetText(ITextServices *iface, BST ...@@ -269,7 +269,7 @@ DECLSPEC_HIDDEN HRESULT __thiscall fnTextSrv_TxGetText(ITextServices *iface, BST
if (bstr == NULL) if (bstr == NULL)
return E_OUTOFMEMORY; return E_OUTOFMEMORY;
ME_CursorFromCharOfs(This->editor, 0, &start); cursor_from_char_ofs( This->editor, 0, &start );
ME_GetTextW(This->editor, bstr, length, &start, INT_MAX, FALSE, FALSE); ME_GetTextW(This->editor, bstr, length, &start, INT_MAX, FALSE, FALSE);
*pbstrText = bstr; *pbstrText = bstr;
} else { } else {
......
...@@ -336,7 +336,7 @@ static void ME_PlayUndoItem(ME_TextEditor *editor, struct undo_item *undo) ...@@ -336,7 +336,7 @@ static void ME_PlayUndoItem(ME_TextEditor *editor, struct undo_item *undo)
{ {
ME_Cursor tmp; ME_Cursor tmp;
ME_DisplayItem *para; ME_DisplayItem *para;
ME_CursorFromCharOfs(editor, undo->u.set_para_fmt.pos, &tmp); cursor_from_char_ofs( editor, undo->u.set_para_fmt.pos, &tmp );
para = ME_FindItemBack(tmp.pRun, diParagraph); para = ME_FindItemBack(tmp.pRun, diParagraph);
add_undo_set_para_fmt( editor, &para->member.para ); add_undo_set_para_fmt( editor, &para->member.para );
para->member.para.fmt = undo->u.set_para_fmt.fmt; para->member.para.fmt = undo->u.set_para_fmt.fmt;
...@@ -347,7 +347,7 @@ static void ME_PlayUndoItem(ME_TextEditor *editor, struct undo_item *undo) ...@@ -347,7 +347,7 @@ static void ME_PlayUndoItem(ME_TextEditor *editor, struct undo_item *undo)
case undo_set_char_fmt: case undo_set_char_fmt:
{ {
ME_Cursor start, end; ME_Cursor start, end;
ME_CursorFromCharOfs(editor, undo->u.set_char_fmt.pos, &start); cursor_from_char_ofs( editor, undo->u.set_char_fmt.pos, &start );
end = start; end = start;
ME_MoveCursorChars(editor, &end, undo->u.set_char_fmt.len, FALSE); ME_MoveCursorChars(editor, &end, undo->u.set_char_fmt.len, FALSE);
ME_SetCharFormat(editor, &start, &end, &undo->u.set_char_fmt.fmt); ME_SetCharFormat(editor, &start, &end, &undo->u.set_char_fmt.fmt);
...@@ -356,7 +356,7 @@ static void ME_PlayUndoItem(ME_TextEditor *editor, struct undo_item *undo) ...@@ -356,7 +356,7 @@ static void ME_PlayUndoItem(ME_TextEditor *editor, struct undo_item *undo)
case undo_insert_run: case undo_insert_run:
{ {
ME_Cursor tmp; ME_Cursor tmp;
ME_CursorFromCharOfs(editor, undo->u.insert_run.pos, &tmp); cursor_from_char_ofs( editor, undo->u.insert_run.pos, &tmp );
run_insert( editor, &tmp, undo->u.insert_run.style, run_insert( editor, &tmp, undo->u.insert_run.style,
undo->u.insert_run.str, undo->u.insert_run.len, undo->u.insert_run.str, undo->u.insert_run.len,
undo->u.insert_run.flags ); undo->u.insert_run.flags );
...@@ -365,14 +365,14 @@ static void ME_PlayUndoItem(ME_TextEditor *editor, struct undo_item *undo) ...@@ -365,14 +365,14 @@ static void ME_PlayUndoItem(ME_TextEditor *editor, struct undo_item *undo)
case undo_delete_run: case undo_delete_run:
{ {
ME_Cursor tmp; ME_Cursor tmp;
ME_CursorFromCharOfs(editor, undo->u.delete_run.pos, &tmp); cursor_from_char_ofs( editor, undo->u.delete_run.pos, &tmp );
ME_InternalDeleteText(editor, &tmp, undo->u.delete_run.len, TRUE); ME_InternalDeleteText(editor, &tmp, undo->u.delete_run.len, TRUE);
break; break;
} }
case undo_join_paras: case undo_join_paras:
{ {
ME_Cursor tmp; ME_Cursor tmp;
ME_CursorFromCharOfs(editor, undo->u.join_paras.pos, &tmp); cursor_from_char_ofs( editor, undo->u.join_paras.pos, &tmp );
ME_JoinParagraphs(editor, tmp.pPara, TRUE); ME_JoinParagraphs(editor, tmp.pPara, TRUE);
break; break;
} }
...@@ -383,7 +383,7 @@ static void ME_PlayUndoItem(ME_TextEditor *editor, struct undo_item *undo) ...@@ -383,7 +383,7 @@ static void ME_PlayUndoItem(ME_TextEditor *editor, struct undo_item *undo)
BOOL bFixRowStart; BOOL bFixRowStart;
int paraFlags = undo->u.split_para.flags & (MEPF_ROWSTART|MEPF_CELL|MEPF_ROWEND); int paraFlags = undo->u.split_para.flags & (MEPF_ROWSTART|MEPF_CELL|MEPF_ROWEND);
ME_CursorFromCharOfs(editor, undo->u.split_para.pos, &tmp); cursor_from_char_ofs( editor, undo->u.split_para.pos, &tmp );
if (tmp.nOffset) run_split( editor, &tmp ); if (tmp.nOffset) run_split( editor, &tmp );
this_para = tmp.pPara; this_para = tmp.pPara;
bFixRowStart = this_para->member.para.nFlags & MEPF_ROWSTART; bFixRowStart = this_para->member.para.nFlags & MEPF_ROWSTART;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment