2 * RichEdit - Caret and selection functions.
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Phil Krylov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
28 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs);
30 void ME_GetSelection(ME_TextEditor *editor, int *from, int *to)
32 *from = ME_GetCursorOfs(editor, 0);
33 *to = ME_GetCursorOfs(editor, 1);
43 int ME_GetTextLength(ME_TextEditor *editor)
45 return ME_CharOfsFromRunOfs(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun), 0);
49 int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how)
53 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
55 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
58 length = ME_GetTextLength(editor);
60 if ((editor->styleFlags & ES_MULTILINE)
61 && (how->flags & GTL_USECRLF)
62 && !editor->bEmulateVersion10) /* Ignore GTL_USECRLF flag in 1.0 emulation */
63 length += editor->nParagraphs - 1;
65 if (how->flags & GTL_NUMBYTES)
69 if (how->codepage == 1200)
71 if (how->flags & GTL_PRECISE)
72 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
73 if (GetCPInfo(how->codepage, &cpinfo))
74 return length * cpinfo.MaxCharSize;
75 ERR("Invalid codepage %u\n", how->codepage);
82 int ME_SetSelection(ME_TextEditor *editor, int from, int to)
85 const int len = ME_GetTextLength(editor);
87 /* all negative values are effectively the same */
94 if (from == 0 && to == -1)
96 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
97 editor->pCursors[1].nOffset = 0;
98 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
99 editor->pCursors[0].nOffset = 0;
100 ME_InvalidateSelection(editor);
101 ME_ClearTempStyle(editor);
105 /* if both values are equal and also out of bound, that means to */
106 /* put the selection at the end of the text */
107 if ((from == to) && (to < 0 || to > len))
113 /* if from is negative and to is positive then selection is */
114 /* deselected and caret moved to end of the current selection */
118 ME_GetSelection(editor, &start, &end);
119 editor->pCursors[1] = editor->pCursors[0];
121 ME_ClearTempStyle(editor);
125 /* adjust to if it's a negative value */
129 /* flip from and to if they are reversed */
137 /* after fiddling with the values, we find from > len && to > len */
140 /* special case with to too big */
147 editor->pCursors[1].pRun = editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
148 editor->pCursors[1].nOffset = editor->pCursors[0].nOffset = 0;
149 ME_InvalidateSelection(editor);
150 ME_ClearTempStyle(editor);
154 ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
155 ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);
161 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
162 int *x, int *y, int *height)
164 ME_DisplayItem *pCursorRun = pCursor->pRun;
165 ME_DisplayItem *pSizeRun = pCursor->pRun;
167 assert(height && x && y);
168 assert(!(ME_GetParagraph(pCursorRun)->member.para.nFlags & MEPF_REWRAP));
169 assert(pCursor->pRun);
170 assert(pCursor->pRun->type == diRun);
172 if (pCursorRun->type == diRun) {
173 ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
176 HDC hDC = ITextHost_TxGetDC(editor->texthost);
178 ME_DisplayItem *run = pCursorRun;
179 ME_DisplayItem *para = NULL;
182 ME_InitContext(&c, editor, hDC);
184 if (!pCursor->nOffset)
186 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrParagraph);
188 if (prev->type == diRun)
191 assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
192 para = ME_FindItemBack(row, diParagraph);
194 assert(para->type == diParagraph);
195 if (editor->bCaretAtEnd && !pCursor->nOffset &&
196 run == ME_FindItemFwd(row, diRun))
198 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
200 if (tmp->type == diRun)
202 row = ME_FindItemBack(tmp, diStartRow);
203 pSizeRun = run = tmp;
205 assert(run->type == diRun);
206 sz = ME_GetRunSize(&c, ¶->member.para,
207 &run->member.run, ME_StrLen(run->member.run.strText),
208 row->member.row.nLMargin);
211 if (pCursor->nOffset) {
212 sz = ME_GetRunSize(&c, ¶->member.para, &run->member.run, pCursor->nOffset,
213 row->member.row.nLMargin);
216 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
217 *x = c.rcView.left + run->member.run.pt.x + sz.cx - editor->horz_si.nPos;
218 *y = c.rcView.top + para->member.para.pt.y + row->member.row.nBaseline
219 + run->member.run.pt.y - pSizeRun->member.run.nAscent - editor->vert_si.nPos;
220 ME_DestroyContext(&c);
224 *height = 10; /* FIXME use global font */
231 ME_MoveCaret(ME_TextEditor *editor)
235 if (ME_WrapMarkedParagraphs(editor))
236 ME_UpdateScrollBar(editor);
237 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
238 if(editor->bHaveFocus && !ME_IsSelection(editor))
240 x = min(x, editor->rcFormat.right-1);
241 ITextHost_TxCreateCaret(editor->texthost, NULL, 0, height);
242 ITextHost_TxSetCaretPos(editor->texthost, x, y);
247 void ME_ShowCaret(ME_TextEditor *ed)
250 if(ed->bHaveFocus && !ME_IsSelection(ed))
251 ITextHost_TxShowCaret(ed->texthost, TRUE);
254 void ME_HideCaret(ME_TextEditor *ed)
256 if(!ed->bHaveFocus || ME_IsSelection(ed))
258 ITextHost_TxShowCaret(ed->texthost, FALSE);
263 BOOL ME_InternalDeleteText(ME_TextEditor *editor, int nOfs, int nChars,
268 int totalChars = nChars;
269 ME_DisplayItem *start_para;
272 /* Prevent deletion past last end of paragraph run. */
273 ME_DisplayItem *pTextEnd = editor->pBuffer->pLast;
274 int nMaxChars = pTextEnd->member.para.prev_para->member.para.nCharOfs;
275 nMaxChars += ME_FindItemBack(pTextEnd, diRun)->member.run.nCharOfs;
277 nChars = min(nChars, nMaxChars);
280 ME_CursorFromCharOfs(editor, nOfs, &c);
281 start_para = ME_GetParagraph(c.pRun);
285 ME_ProtectPartialTableDeletion(editor, nOfs, &nChars);
293 ME_CursorFromCharOfs(editor, nOfs+nChars, &c);
295 nOfs+nChars == (c.pRun->member.run.nCharOfs
296 + ME_GetParagraph(c.pRun)->member.para.nCharOfs))
298 /* We aren't deleting anything in this run, so we will go back to the
299 * last run we are deleting text in. */
300 c.pRun = ME_FindItemBack(c.pRun, diRun);
301 if (c.pRun->member.run.nFlags & MERF_ENDPARA)
302 c.nOffset = c.pRun->member.run.nCR + c.pRun->member.run.nLF;
304 c.nOffset = c.pRun->member.run.strText->nLen;
306 run = &c.pRun->member.run;
307 if (run->nFlags & MERF_ENDPARA) {
308 int eollen = run->nCR + run->nLF;
309 BOOL keepFirstParaFormat;
311 if (!ME_FindItemFwd(c.pRun, diParagraph))
315 keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
317 if (!editor->bEmulateVersion10) /* v4.1 */
319 ME_DisplayItem *next_para = ME_FindItemFwd(c.pRun, diParagraphOrEnd);
320 ME_DisplayItem *this_para = next_para->member.para.prev_para;
322 /* The end of paragraph before a table row is only deleted if there
323 * is nothing else on the line before it. */
324 if (this_para == start_para &&
325 next_para->member.para.nFlags & MEPF_ROWSTART)
327 /* If the paragraph will be empty, then it should be deleted, however
328 * it still might have text right now which would inherit the
329 * MEPF_STARTROW property if we joined it right now.
330 * Instead we will delete it after the preceding text is deleted. */
331 if (nOfs > this_para->member.para.nCharOfs) {
332 /* Skip this end of line. */
333 nChars -= (eollen < nChars) ? eollen : nChars;
336 keepFirstParaFormat = TRUE;
339 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun), keepFirstParaFormat);
340 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
341 ME_CheckCharOffsets(editor);
342 nChars -= (eollen < nChars) ? eollen : nChars;
348 int nCharsToDelete = min(nChars, c.nOffset);
351 c.nOffset -= nCharsToDelete;
353 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
356 /* nChars is the number of characters that should be deleted from the
357 PRECEDING runs (these BEFORE cursor.pRun)
358 nCharsToDelete is a number of chars to delete from THIS run */
359 nChars -= nCharsToDelete;
360 shift -= nCharsToDelete;
361 TRACE("Deleting %d (remaning %d) chars at %d in '%s' (%d)\n",
362 nCharsToDelete, nChars, c.nOffset,
363 debugstr_w(run->strText->szData), run->strText->nLen);
365 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
367 /* undo = reinsert whole run */
368 /* nOfs is a character offset (from the start of the document
369 to the current (deleted) run */
370 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
372 pUndo->di.member.run.nCharOfs = nOfs+nChars;
376 /* undo = reinsert partial run */
377 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
379 ME_DestroyString(pUndo->di.member.run.strText);
380 pUndo->di.member.run.nCharOfs = nOfs+nChars;
381 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
384 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
385 TRACE("Shift value: %d\n", shift);
386 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
388 /* update cursors (including c) */
389 for (i=-1; i<editor->nCursors; i++) {
390 ME_Cursor *pThisCur = editor->pCursors + i;
391 if (i == -1) pThisCur = &c;
392 if (pThisCur->pRun == cursor.pRun) {
393 if (pThisCur->nOffset > cursor.nOffset) {
394 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
395 pThisCur->nOffset = cursor.nOffset;
397 pThisCur->nOffset -= nCharsToDelete;
398 assert(pThisCur->nOffset >= 0);
399 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
401 if (pThisCur->nOffset == ME_StrVLen(run->strText))
403 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
404 assert(pThisCur->pRun->type == diRun);
405 pThisCur->nOffset = 0;
410 /* c = updated data now */
412 if (c.pRun == cursor.pRun)
413 ME_SkipAndPropagateCharOffset(c.pRun, shift);
415 ME_PropagateCharOffset(c.pRun, shift);
417 if (!ME_StrVLen(cursor.pRun->member.run.strText))
419 TRACE("Removing useless run\n");
420 ME_Remove(cursor.pRun);
421 ME_DestroyDisplayItem(cursor.pRun);
426 ME_CheckCharOffsets(editor);
434 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
436 assert(nCursor>=0 && nCursor<editor->nCursors);
437 /* text operations set modified state */
438 editor->nModifyStep = 1;
439 return ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars,
443 static ME_DisplayItem *
444 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
445 const WCHAR *str, int len, ME_Style *style,
448 ME_Cursor *p = &editor->pCursors[nCursor];
450 editor->bCaretAtEnd = FALSE;
452 assert(p->pRun->type == diRun);
454 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
458 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
460 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
465 if (ME_IsSelection(editor))
466 ME_DeleteSelection(editor);
468 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
470 di->member.run.ole_obj = ALLOC_OBJ(*reo);
471 ME_CopyReObject(di->member.run.ole_obj, reo);
472 ME_SendSelChange(editor);
476 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
478 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
483 if (ME_IsSelection(editor))
484 ME_DeleteSelection(editor);
486 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
488 ME_SendSelChange(editor);
492 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
493 const WCHAR *str, int len, ME_Style *style)
499 /* FIXME really HERE ? */
500 if (ME_IsSelection(editor))
501 ME_DeleteSelection(editor);
503 /* FIXME: is this too slow? */
504 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
505 oldLen = ME_GetTextLength(editor);
507 /* text operations set modified state */
508 editor->nModifyStep = 1;
512 assert(nCursor>=0 && nCursor<editor->nCursors);
516 /* grow the text limit to fit our text */
517 if(editor->nTextLimit < oldLen +len)
518 editor->nTextLimit = oldLen + len;
523 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
524 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
526 if (pos-str < len && *pos == '\t') { /* handle tabs */
530 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
532 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
541 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
542 if (!editor->bEmulateVersion10 && pos-str < len-2 && pos[0] == '\r' && pos[1] == '\r' && pos[2] == '\n') {
546 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
548 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
557 if (pos-str < len) { /* handle EOLs */
558 ME_DisplayItem *tp, *end_run;
563 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
564 p = &editor->pCursors[nCursor];
566 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
567 p = &editor->pCursors[nCursor];
569 tmp_style = ME_GetInsertStyle(editor, nCursor);
570 /* ME_SplitParagraph increases style refcount */
572 /* Encode and fill number of CR and LF according to emulation mode */
573 if (editor->bEmulateVersion10) {
576 /* We have to find out how many consecutive \r are there, and if there
577 is a \n terminating the run of \r's. */
578 numCR = 0; numLF = 0;
580 while (tpos-str < len && *tpos == '\r') {
584 if (tpos-str >= len) {
585 /* Reached end of text without finding anything but '\r' */
589 numCR = 1; numLF = 0;
590 } else if (*tpos == '\n') {
591 /* The entire run of \r's plus the one \n is one single line break */
595 /* Found some other content past the run of \r's */
597 numCR = 1; numLF = 0;
600 if(pos-str < len && *pos =='\r')
602 if(pos-str < len && *pos =='\n')
604 numCR = 1; numLF = 0;
606 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, numCR, numLF, 0);
607 p->pRun = ME_FindItemFwd(tp, diRun);
608 end_run = ME_FindItemBack(tp, diRun);
609 ME_ReleaseStyle(end_run->member.run.style);
610 end_run->member.run.style = tmp_style;
619 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
626 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
628 ME_DisplayItem *pRun = pCursor->pRun;
632 if (!pCursor->nOffset)
635 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
642 if (pRun->member.para.prev_para->type == diTextStart)
644 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
645 /* every paragraph ought to have at least one run */
646 assert(pRun && pRun->type == diRun);
647 assert(pRun->member.run.nFlags & MERF_ENDPARA);
650 assert(pRun->type != diRun && pRun->type != diParagraph);
653 } while (RUN_IS_HIDDEN(&pRun->member.run) ||
654 pRun->member.run.nFlags & MERF_HIDDEN);
655 pCursor->pRun = pRun;
656 if (pRun->member.run.nFlags & MERF_ENDPARA)
657 pCursor->nOffset = 0;
659 pCursor->nOffset = pRun->member.run.strText->nLen;
662 if (pCursor->nOffset)
663 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
668 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
670 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
672 if (new_ofs < pRun->member.run.strText->nLen)
674 pCursor->nOffset = new_ofs;
679 pRun = ME_FindItemFwd(pRun, diRun);
680 } while (pRun && (RUN_IS_HIDDEN(&pRun->member.run) ||
681 pRun->member.run.nFlags & MERF_HIDDEN));
684 pCursor->pRun = pRun;
685 pCursor->nOffset = 0;
694 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
696 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
697 int nOffset = cursor->nOffset;
701 /* Backward movement */
704 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
705 nOffset, WB_MOVEWORDLEFT);
708 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
709 if (pOtherRun->type == diRun)
711 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
712 pOtherRun->member.run.strText->nLen - 1,
714 && !(pRun->member.run.nFlags & MERF_ENDPARA)
715 && !(cursor->pRun == pRun && cursor->nOffset == 0)
716 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
720 nOffset = pOtherRun->member.run.strText->nLen;
722 else if (pOtherRun->type == diParagraph)
724 if (cursor->pRun == pRun && cursor->nOffset == 0)
726 /* Skip empty start of table row paragraph */
727 if (pOtherRun->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART)
728 pOtherRun = pOtherRun->member.para.prev_para;
729 /* Paragraph breaks are treated as separate words */
730 if (pOtherRun->member.para.prev_para->type == diTextStart)
733 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
741 /* Forward movement */
742 BOOL last_delim = FALSE;
746 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
747 nOffset, WB_ISDELIMITER))
749 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
750 nOffset, WB_MOVEWORDRIGHT);
751 if (nOffset < pRun->member.run.strText->nLen)
753 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
754 if (pOtherRun->type == diRun)
756 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
757 nOffset - 1, WB_ISDELIMITER);
761 else if (pOtherRun->type == diParagraph)
763 if (pOtherRun->member.para.nFlags & MEPF_ROWSTART)
764 pOtherRun = pOtherRun->member.para.next_para;
765 if (cursor->pRun == pRun)
766 pRun = ME_FindItemFwd(pOtherRun, diRun);
772 if (cursor->pRun == pRun)
780 cursor->nOffset = nOffset;
786 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
788 /* pCursor[0] is the end of the selection
789 * pCursor[1] is the start of the selection (or the position selection anchor)
790 * pCursor[2] and [3] are the selection anchors that are backed up
791 * so they are kept when the selection changes for drag selection.
794 editor->nSelectionType = selectionType;
795 switch(selectionType)
800 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
801 editor->pCursors[1] = editor->pCursors[0];
802 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
807 ME_DisplayItem *pItem;
808 ME_DIType fwdSearchType, backSearchType;
809 if (selectionType == stParagraph) {
810 backSearchType = diParagraph;
811 fwdSearchType = diParagraphOrEnd;
813 backSearchType = diStartRow;
814 fwdSearchType = diStartRowOrParagraphOrEnd;
816 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
818 if (pItem->type == diTextEnd)
819 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
821 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
822 editor->pCursors[0].nOffset = 0;
824 pItem = ME_FindItemBack(pItem, backSearchType);
825 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
826 editor->pCursors[1].nOffset = 0;
830 /* Select everything with cursor anchored from the start of the text */
831 editor->nSelectionType = stDocument;
832 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
833 editor->pCursors[1].nOffset = 0;
834 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
835 editor->pCursors[0].nOffset = 0;
839 /* Store the anchor positions for extending the selection. */
840 editor->pCursors[2] = editor->pCursors[0];
841 editor->pCursors[3] = editor->pCursors[1];
844 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
846 ME_Cursor *pCursor = &editor->pCursors[nCursor];
847 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
848 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
851 /* Helper function for ME_FindPixelPos to find paragraph within tables */
852 static ME_DisplayItem* ME_FindPixelPosInTableRow(int x, int y,
853 ME_DisplayItem *para)
855 ME_DisplayItem *cell, *next_cell;
856 assert(para->member.para.nFlags & MEPF_ROWSTART);
857 cell = para->member.para.next_para->member.para.pCell;
860 /* find the cell we are in */
861 while ((next_cell = cell->member.cell.next_cell) != NULL) {
862 if (x < next_cell->member.cell.pt.x)
864 para = ME_FindItemFwd(cell, diParagraph);
865 /* Found the cell, but there might be multiple paragraphs in
866 * the cell, so need to search down the cell for the paragraph. */
867 while (cell == para->member.para.pCell) {
868 if (y < para->member.para.pt.y + para->member.para.nHeight)
870 if (para->member.para.nFlags & MEPF_ROWSTART)
871 return ME_FindPixelPosInTableRow(x, y, para);
875 para = para->member.para.next_para;
877 /* Past the end of the cell, so go back to the last cell paragraph */
878 return para->member.para.prev_para;
882 /* Return table row delimiter */
883 para = ME_FindItemFwd(cell, diParagraph);
884 assert(para->member.para.nFlags & MEPF_ROWEND);
885 assert(para->member.para.pFmt->dwMask & PFM_TABLEROWDELIMITER);
886 assert(para->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER);
890 static BOOL ME_ReturnFoundPos(ME_TextEditor *editor, ME_DisplayItem *found,
891 ME_Cursor *result, int rx, BOOL isExact)
894 assert(found->type == diRun);
895 if ((found->member.run.nFlags & MERF_ENDPARA) || rx < 0)
897 result->pRun = found;
898 result->nOffset = ME_CharFromPointCursor(editor, rx, &found->member.run);
899 if (editor->pCursors[0].nOffset == found->member.run.strText->nLen && rx)
901 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
907 /* Finds the run and offset from the pixel position.
909 * x & y are pixel positions in virtual coordinates into the rich edit control,
910 * so client coordinates must first be adjusted by the scroll position.
912 * returns TRUE if the result was exactly under the cursor, otherwise returns
913 * FALSE, and result is set to the closest position to the coordinates.
915 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
916 ME_Cursor *result, BOOL *is_eol)
918 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
919 ME_DisplayItem *last = NULL;
923 x -= editor->rcFormat.left;
924 y -= editor->rcFormat.top;
930 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
932 assert(p->type == diParagraph);
933 if (y < p->member.para.pt.y + p->member.para.nHeight)
935 if (p->member.para.nFlags & MEPF_ROWSTART)
936 p = ME_FindPixelPosInTableRow(x, y, p);
937 y -= p->member.para.pt.y;
938 p = ME_FindItemFwd(p, diStartRow);
940 } else if (p->member.para.nFlags & MEPF_ROWSTART) {
941 p = ME_GetTableRowEnd(p);
945 for (; p != editor->pBuffer->pLast; )
948 assert(p->type == diStartRow);
949 if (y < p->member.row.pt.y + p->member.row.nHeight)
951 p = ME_FindItemFwd(p, diRun);
954 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
955 if (pp->type != diStartRow)
957 p = ME_FindItemFwd(p, diRun);
962 if (p == editor->pBuffer->pLast)
964 /* The position is below the last paragraph, so the last row will be used
965 * rather than the end of the text, so the x position will be used to
966 * determine the offset closest to the pixel position. */
968 p = ME_FindItemBack(p, diStartRow);
970 p = ME_FindItemFwd(p, diRun);
974 p = editor->pBuffer->pLast;
977 for (; p != editor->pBuffer->pLast; p = p->next)
982 rx = x - p->member.run.pt.x;
983 if (rx < p->member.run.nWidth)
984 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
988 p = ME_FindItemFwd(p, diRun);
989 if (is_eol) *is_eol = 1;
990 rx = 0; /* FIXME not sure */
991 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
996 rx = 0; /* FIXME not sure */
998 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
1003 result->pRun = ME_FindItemBack(p, diRun);
1004 result->nOffset = 0;
1005 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
1010 /* Returns the character offset closest to the pixel position
1012 * x & y are pixel positions in client coordinates.
1014 * isExact will be set to TRUE if the run is directly under the pixel
1015 * position, FALSE if it not, unless isExact is set to NULL.
1017 int ME_CharFromPos(ME_TextEditor *editor, int x, int y, BOOL *isExact)
1023 ITextHost_TxGetClientRect(editor->texthost, &rc);
1024 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
1025 if (isExact) *isExact = FALSE;
1028 x += editor->horz_si.nPos;
1029 y += editor->vert_si.nPos;
1030 bResult = ME_FindPixelPos(editor, x, y, &cursor, NULL);
1031 if (isExact) *isExact = bResult;
1032 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
1033 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
1038 /* Extends the selection with a word, line, or paragraph selection type.
1040 * The selection is anchored by editor->pCursors[2-3] such that the text
1041 * between the anchors will remain selected, and one end will be extended.
1043 * editor->pCursors[0] should have the position to extend the selection to
1044 * before this function is called.
1046 * Nothing will be done if editor->nSelectionType equals stPosition.
1048 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1050 ME_Cursor tmp_cursor;
1051 int curOfs, anchorStartOfs, anchorEndOfs;
1052 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1054 curOfs = ME_GetCursorOfs(editor, 0);
1055 anchorStartOfs = ME_GetCursorOfs(editor, 3);
1056 anchorEndOfs = ME_GetCursorOfs(editor, 2);
1058 tmp_cursor = editor->pCursors[0];
1059 editor->pCursors[0] = editor->pCursors[2];
1060 editor->pCursors[1] = editor->pCursors[3];
1061 if (curOfs < anchorStartOfs)
1063 /* Extend the left side of selection */
1064 editor->pCursors[1] = tmp_cursor;
1065 if (editor->nSelectionType == stWord)
1066 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1069 ME_DisplayItem *pItem;
1070 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1071 diStartRowOrParagraph:diParagraph);
1072 pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
1073 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
1074 editor->pCursors[1].nOffset = 0;
1077 else if (curOfs >= anchorEndOfs)
1079 /* Extend the right side of selection */
1080 editor->pCursors[0] = tmp_cursor;
1081 if (editor->nSelectionType == stWord)
1082 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
1085 ME_DisplayItem *pItem;
1086 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1087 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
1088 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
1089 if (pItem->type == diTextEnd)
1090 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
1092 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
1093 editor->pCursors[0].nOffset = 0;
1098 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1100 ME_Cursor tmp_cursor;
1101 int is_selection = 0;
1104 editor->nUDArrowX = -1;
1106 x += editor->horz_si.nPos;
1107 y += editor->vert_si.nPos;
1109 tmp_cursor = editor->pCursors[0];
1110 is_selection = ME_IsSelection(editor);
1111 is_shift = GetKeyState(VK_SHIFT) < 0;
1113 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
1115 if (x >= editor->rcFormat.left || is_shift)
1119 editor->pCursors[1] = editor->pCursors[0];
1121 if (x >= editor->rcFormat.left)
1122 ME_SelectByType(editor, stWord);
1124 ME_SelectByType(editor, stParagraph);
1125 } else if (clickNum % 2 == 0) {
1126 ME_SelectByType(editor, stWord);
1128 ME_SelectByType(editor, stParagraph);
1133 editor->nSelectionType = stPosition;
1134 editor->pCursors[1] = editor->pCursors[0];
1136 else if (!is_selection)
1138 editor->nSelectionType = stPosition;
1139 editor->pCursors[1] = tmp_cursor;
1141 else if (editor->nSelectionType != stPosition)
1143 ME_ExtendAnchorSelection(editor);
1149 ME_SelectByType(editor, stLine);
1150 } else if (clickNum % 2 == 0 || is_shift) {
1151 ME_SelectByType(editor, stParagraph);
1153 ME_SelectByType(editor, stDocument);
1156 ME_InvalidateSelection(editor);
1157 ITextHost_TxShowCaret(editor->texthost, FALSE);
1158 ME_ShowCaret(editor);
1159 ME_ClearTempStyle(editor);
1160 ME_SendSelChange(editor);
1163 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1165 ME_Cursor tmp_cursor;
1167 if (editor->nSelectionType == stDocument)
1169 x += editor->horz_si.nPos;
1170 y += editor->vert_si.nPos;
1172 tmp_cursor = editor->pCursors[0];
1173 /* FIXME: do something with the return value of ME_FindPixelPos */
1174 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
1176 ME_InvalidateSelection(editor);
1177 editor->pCursors[0] = tmp_cursor;
1178 ME_ExtendAnchorSelection(editor);
1180 if (editor->nSelectionType != stPosition &&
1181 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1183 /* The scroll the cursor towards the other end, since it was the one
1184 * extended by ME_ExtendAnchorSelection */
1185 ME_EnsureVisible(editor, &editor->pCursors[1]);
1187 ME_EnsureVisible(editor, &editor->pCursors[0]);
1190 ME_InvalidateSelection(editor);
1191 ITextHost_TxShowCaret(editor->texthost, FALSE);
1192 ME_ShowCaret(editor);
1193 ME_SendSelChange(editor);
1196 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
1197 int x, int *pOffset, int *pbCaretAtEnd)
1199 ME_DisplayItem *pNext, *pLastRun;
1200 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
1201 assert(pNext->type == diRun);
1203 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
1204 if (pOffset) *pOffset = 0;
1206 int run_x = pNext->member.run.pt.x;
1207 int width = pNext->member.run.nWidth;
1212 if (x >= run_x && x < run_x+width)
1214 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
1215 ME_String *s = pNext->member.run.strText;
1223 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
1224 } while(pNext && pNext->type == diRun);
1226 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
1228 pNext = ME_FindItemFwd(pNext, diRun);
1229 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
1236 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1238 ME_DisplayItem *pRun = pCursor->pRun;
1241 if (editor->nUDArrowX != -1)
1242 x = editor->nUDArrowX;
1244 if (editor->bCaretAtEnd)
1246 pRun = ME_FindItemBack(pRun, diRun);
1248 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1251 x = pRun->member.run.pt.x;
1252 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
1254 editor->nUDArrowX = x;
1261 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1263 ME_DisplayItem *pRun = pCursor->pRun;
1264 ME_DisplayItem *pItem, *pOldPara, *pNewPara;
1265 int x = ME_GetXForArrow(editor, pCursor);
1267 if (editor->bCaretAtEnd && !pCursor->nOffset)
1268 pRun = ME_FindItemBack(pRun, diRun);
1271 pOldPara = ME_GetParagraph(pRun);
1274 /* start of this row */
1275 pItem = ME_FindItemBack(pRun, diStartRow);
1277 /* start of the previous row */
1278 pItem = ME_FindItemBack(pItem, diStartRow);
1280 return; /* row not found - ignore */
1281 pNewPara = ME_GetParagraph(pItem);
1282 if (pOldPara->member.para.nFlags & MEPF_ROWEND ||
1283 (pOldPara->member.para.pCell &&
1284 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1286 /* Brought out of a cell */
1287 pNewPara = ME_GetTableRowStart(pOldPara)->member.para.prev_para;
1288 if (pNewPara->type == diTextStart)
1289 return; /* At the top, so don't go anywhere. */
1290 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1292 if (pNewPara->member.para.nFlags & MEPF_ROWEND)
1294 /* Brought into a table row */
1295 ME_Cell *cell = &ME_FindItemBack(pNewPara, diCell)->member.cell;
1296 while (x < cell->pt.x && cell->prev_cell)
1297 cell = &cell->prev_cell->member.cell;
1298 if (cell->next_cell) /* else - we are still at the end of the row */
1299 pItem = ME_FindItemBack(cell->next_cell, diStartRow);
1304 /* start of the next row */
1305 pItem = ME_FindItemFwd(pRun, diStartRow);
1307 return; /* row not found - ignore */
1308 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1310 pNewPara = ME_GetParagraph(pItem);
1311 if (pOldPara->member.para.nFlags & MEPF_ROWSTART ||
1312 (pOldPara->member.para.pCell &&
1313 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1315 /* Brought out of a cell */
1316 pNewPara = ME_GetTableRowEnd(pOldPara)->member.para.next_para;
1317 if (pNewPara->type == diTextEnd)
1318 return; /* At the bottom, so don't go anywhere. */
1319 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1321 if (pNewPara->member.para.nFlags & MEPF_ROWSTART)
1323 /* Brought into a table row */
1324 ME_DisplayItem *cell = ME_FindItemFwd(pNewPara, diCell);
1325 while (cell->member.cell.next_cell &&
1326 x >= cell->member.cell.next_cell->member.cell.pt.x)
1327 cell = cell->member.cell.next_cell;
1328 pItem = ME_FindItemFwd(cell, diStartRow);
1333 /* row not found - ignore */
1336 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1337 assert(pCursor->pRun);
1338 assert(pCursor->pRun->type == diRun);
1341 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1343 ME_DisplayItem *p = ME_FindItemFwd(editor->pBuffer->pFirst, diStartRow);
1345 if (editor->vert_si.nPos < p->member.row.nHeight)
1347 pCursor->pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
1348 pCursor->nOffset = 0;
1349 editor->bCaretAtEnd = FALSE;
1350 /* Native clears seems to clear this x value on page up at the top
1351 * of the text, but not on page down at the end of the text.
1352 * Doesn't make sense, but we try to be bug for bug compatible. */
1353 editor->nUDArrowX = -1;
1355 ME_DisplayItem *pRun = pCursor->pRun;
1356 ME_DisplayItem *pLast;
1357 int x, y, ys, yd, yp, yprev;
1358 int yOldScrollPos = editor->vert_si.nPos;
1360 x = ME_GetXForArrow(editor, pCursor);
1361 if (!pCursor->nOffset && editor->bCaretAtEnd)
1362 pRun = ME_FindItemBack(pRun, diRun);
1364 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1365 assert(p->type == diStartRow);
1366 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1367 yprev = ys = y = yp + p->member.row.pt.y;
1369 ME_ScrollUp(editor, editor->sizeWindow.cy);
1370 /* Only move the cursor by the amount scrolled. */
1371 yd = y + editor->vert_si.nPos - yOldScrollPos;
1375 p = ME_FindItemBack(p, diStartRowOrParagraph);
1378 if (p->type == diParagraph) { /* crossing paragraphs */
1379 if (p->member.para.prev_para == NULL)
1381 yp = p->member.para.prev_para->member.para.pt.y;
1384 y = yp + p->member.row.pt.y;
1391 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset,
1392 &editor->bCaretAtEnd);
1394 assert(pCursor->pRun);
1395 assert(pCursor->pRun->type == diRun);
1398 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1400 ME_DisplayItem *pLast;
1403 /* Find y position of the last row */
1404 pLast = editor->pBuffer->pLast;
1405 y = pLast->member.para.prev_para->member.para.pt.y
1406 + ME_FindItemBack(pLast, diStartRow)->member.row.pt.y;
1408 x = ME_GetXForArrow(editor, pCursor);
1410 if (editor->vert_si.nPos >= y - editor->sizeWindow.cy)
1412 pCursor->pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
1413 pCursor->nOffset = 0;
1414 editor->bCaretAtEnd = FALSE;
1416 ME_DisplayItem *pRun = pCursor->pRun;
1418 int ys, yd, yp, yprev;
1419 int yOldScrollPos = editor->vert_si.nPos;
1421 if (!pCursor->nOffset && editor->bCaretAtEnd)
1422 pRun = ME_FindItemBack(pRun, diRun);
1424 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1425 assert(p->type == diStartRow);
1426 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1427 yprev = ys = y = yp + p->member.row.pt.y;
1429 /* For native richedit controls:
1430 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1431 * v4.1 can scroll past this position here. */
1432 ME_ScrollDown(editor, editor->sizeWindow.cy);
1433 /* Only move the cursor by the amount scrolled. */
1434 yd = y + editor->vert_si.nPos - yOldScrollPos;
1438 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1441 if (p->type == diParagraph) {
1442 yp = p->member.para.pt.y;
1445 y = yp + p->member.row.pt.y;
1452 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset,
1453 &editor->bCaretAtEnd);
1455 assert(pCursor->pRun);
1456 assert(pCursor->pRun->type == diRun);
1459 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1461 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1462 ME_WrapMarkedParagraphs(editor);
1464 ME_DisplayItem *pRun;
1465 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1466 pRow = ME_FindItemBack(pRow, diStartRow);
1470 pRun = ME_FindItemFwd(pRow, diRun);
1472 pCursor->pRun = pRun;
1473 pCursor->nOffset = 0;
1476 editor->bCaretAtEnd = FALSE;
1479 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1481 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1483 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1485 pCursor->pRun = pRun;
1486 pCursor->nOffset = 0;
1491 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1493 ME_DisplayItem *pRow;
1495 if (editor->bCaretAtEnd && !pCursor->nOffset)
1498 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1500 if (pRow->type == diStartRow) {
1501 /* FIXME WTF was I thinking about here ? */
1502 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1504 pCursor->pRun = pRun;
1505 pCursor->nOffset = 0;
1506 editor->bCaretAtEnd = 1;
1509 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1510 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1511 pCursor->nOffset = 0;
1512 editor->bCaretAtEnd = FALSE;
1515 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1517 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1519 p = ME_FindItemBack(p, diRun);
1521 assert(p->member.run.nFlags & MERF_ENDPARA);
1523 pCursor->nOffset = 0;
1524 editor->bCaretAtEnd = FALSE;
1527 BOOL ME_IsSelection(ME_TextEditor *editor)
1529 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1532 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1534 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1542 void ME_DeleteSelection(ME_TextEditor *editor)
1545 ME_GetSelection(editor, &from, &to);
1546 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1549 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1551 return ME_GetInsertStyle(editor, 0);
1554 void ME_SendSelChange(ME_TextEditor *editor)
1558 if (!(editor->nEventMask & ENM_SELCHANGE))
1561 sc.nmhdr.code = EN_SELCHANGE;
1562 ME_GetSelection(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1563 sc.seltyp = SEL_EMPTY;
1564 if (sc.chrg.cpMin != sc.chrg.cpMax)
1565 sc.seltyp |= SEL_TEXT;
1566 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1567 sc.seltyp |= SEL_MULTICHAR;
1568 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1569 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1570 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1571 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1572 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1574 ME_ClearTempStyle(editor);
1576 editor->notified_cr = sc.chrg;
1577 ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
1582 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1585 ME_Cursor *p = &editor->pCursors[nCursor];
1586 ME_Cursor tmp_curs = *p;
1587 BOOL success = FALSE;
1589 ME_CheckCharOffsets(editor);
1592 editor->bCaretAtEnd = 0;
1594 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1596 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1599 editor->bCaretAtEnd = 0;
1601 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1603 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1606 ME_MoveCursorLines(editor, &tmp_curs, -1);
1609 ME_MoveCursorLines(editor, &tmp_curs, +1);
1612 ME_ArrowPageUp(editor, &tmp_curs);
1615 ME_ArrowPageDown(editor, &tmp_curs);
1619 ME_ArrowCtrlHome(editor, &tmp_curs);
1621 ME_ArrowHome(editor, &tmp_curs);
1622 editor->bCaretAtEnd = 0;
1627 ME_ArrowCtrlEnd(editor, &tmp_curs);
1629 ME_ArrowEnd(editor, &tmp_curs);
1634 editor->pCursors[1] = tmp_curs;
1637 ME_InvalidateSelection(editor);
1639 ITextHost_TxShowCaret(editor->texthost, FALSE);
1640 ME_EnsureVisible(editor, &tmp_curs);
1641 ME_ShowCaret(editor);
1642 ME_SendSelChange(editor);