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);
27 void ME_SetCursorToStart(ME_TextEditor *editor, ME_Cursor *cursor)
29 cursor->pPara = editor->pBuffer->pFirst->member.para.next_para;
30 cursor->pRun = ME_FindItemFwd(cursor->pPara, diRun);
34 static void ME_SetCursorToEnd(ME_TextEditor *editor, ME_Cursor *cursor)
36 cursor->pPara = editor->pBuffer->pLast->member.para.prev_para;
37 cursor->pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
42 int ME_GetSelectionOfs(ME_TextEditor *editor, int *from, int *to)
44 *from = ME_GetCursorOfs(&editor->pCursors[0]);
45 *to = ME_GetCursorOfs(&editor->pCursors[1]);
57 int ME_GetSelection(ME_TextEditor *editor, ME_Cursor **from, ME_Cursor **to)
59 if (ME_GetCursorOfs(&editor->pCursors[0]) < ME_GetCursorOfs(&editor->pCursors[1]))
61 *from = &editor->pCursors[0];
62 *to = &editor->pCursors[1];
65 *from = &editor->pCursors[1];
66 *to = &editor->pCursors[0];
71 int ME_GetTextLength(ME_TextEditor *editor)
74 ME_SetCursorToEnd(editor, &cursor);
75 return ME_GetCursorOfs(&cursor);
79 int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how)
83 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
85 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
88 length = ME_GetTextLength(editor);
90 if ((editor->styleFlags & ES_MULTILINE)
91 && (how->flags & GTL_USECRLF)
92 && !editor->bEmulateVersion10) /* Ignore GTL_USECRLF flag in 1.0 emulation */
93 length += editor->nParagraphs - 1;
95 if (how->flags & GTL_NUMBYTES ||
96 (how->flags & GTL_PRECISE && /* GTL_PRECISE seems to imply GTL_NUMBYTES */
97 !(how->flags & GTL_NUMCHARS))) /* unless GTL_NUMCHARS is given */
101 if (how->codepage == 1200)
103 if (how->flags & GTL_PRECISE)
104 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
105 if (GetCPInfo(how->codepage, &cpinfo))
106 return length * cpinfo.MaxCharSize;
107 ERR("Invalid codepage %u\n", how->codepage);
114 int ME_SetSelection(ME_TextEditor *editor, int from, int to)
116 int selectionEnd = 0;
117 const int len = ME_GetTextLength(editor);
119 /* all negative values are effectively the same */
126 if (from == 0 && to == -1)
128 ME_SetCursorToStart(editor, &editor->pCursors[1]);
129 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
130 ME_InvalidateSelection(editor);
131 ME_ClearTempStyle(editor);
135 /* if both values are equal and also out of bound, that means to */
136 /* put the selection at the end of the text */
137 if ((from == to) && (to < 0 || to > len))
143 /* if from is negative and to is positive then selection is */
144 /* deselected and caret moved to end of the current selection */
148 ME_GetSelectionOfs(editor, &start, &end);
151 editor->pCursors[1] = editor->pCursors[0];
154 ME_ClearTempStyle(editor);
158 /* adjust to if it's a negative value */
162 /* flip from and to if they are reversed */
170 /* after fiddling with the values, we find from > len && to > len */
173 /* special case with to too big */
180 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
181 editor->pCursors[1] = editor->pCursors[0];
182 ME_InvalidateSelection(editor);
183 ME_ClearTempStyle(editor);
187 ME_CursorFromCharOfs(editor, from, &editor->pCursors[1]);
188 editor->pCursors[0] = editor->pCursors[1];
189 ME_MoveCursorChars(editor, &editor->pCursors[0], to - from);
190 /* Selection is not allowed in the middle of an end paragraph run. */
191 if (editor->pCursors[1].pRun->member.run.nFlags & MERF_ENDPARA)
192 editor->pCursors[1].nOffset = 0;
193 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA)
194 editor->pCursors[0].nOffset = 0;
200 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
201 int *x, int *y, int *height)
204 ME_DisplayItem *run = pCursor->pRun;
205 ME_DisplayItem *para = pCursor->pPara;
206 ME_DisplayItem *pSizeRun = run;
210 assert(height && x && y);
211 assert(~para->member.para.nFlags & MEPF_REWRAP);
212 assert(run && run->type == diRun);
213 assert(para && para->type == diParagraph);
215 row = ME_FindItemBack(run, diStartRowOrParagraph);
216 assert(row && row->type == diStartRow);
218 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
220 if (!pCursor->nOffset)
222 ME_DisplayItem *prev = ME_FindItemBack(run, diRunOrParagraph);
224 if (prev->type == diRun)
227 if (editor->bCaretAtEnd && !pCursor->nOffset &&
228 run == ME_FindItemFwd(row, diRun))
230 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
232 if (tmp->type == diRun)
234 row = ME_FindItemBack(tmp, diStartRow);
235 pSizeRun = run = tmp;
237 assert(run->type == diRun);
238 sz = ME_GetRunSize(&c, ¶->member.para,
239 &run->member.run, run->member.run.strText->nLen,
240 row->member.row.nLMargin);
243 if (pCursor->nOffset) {
244 sz = ME_GetRunSize(&c, ¶->member.para, &run->member.run,
245 pCursor->nOffset, row->member.row.nLMargin);
248 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
249 *x = c.rcView.left + run->member.run.pt.x + sz.cx - editor->horz_si.nPos;
250 *y = c.rcView.top + para->member.para.pt.y + row->member.row.nBaseline
251 + run->member.run.pt.y - pSizeRun->member.run.nAscent
252 - editor->vert_si.nPos;
253 ME_DestroyContext(&c);
259 ME_MoveCaret(ME_TextEditor *editor)
263 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
264 if(editor->bHaveFocus && !ME_IsSelection(editor))
266 x = min(x, editor->rcFormat.right-1);
267 ITextHost_TxCreateCaret(editor->texthost, NULL, 0, height);
268 ITextHost_TxSetCaretPos(editor->texthost, x, y);
273 void ME_ShowCaret(ME_TextEditor *ed)
276 if(ed->bHaveFocus && !ME_IsSelection(ed))
277 ITextHost_TxShowCaret(ed->texthost, TRUE);
280 void ME_HideCaret(ME_TextEditor *ed)
282 if(!ed->bHaveFocus || ME_IsSelection(ed))
284 ITextHost_TxShowCaret(ed->texthost, FALSE);
289 BOOL ME_InternalDeleteText(ME_TextEditor *editor, ME_Cursor *start,
290 int nChars, BOOL bForce)
292 ME_Cursor c = *start;
293 int nOfs = ME_GetCursorOfs(start);
295 int totalChars = nChars;
296 ME_DisplayItem *start_para;
298 /* Prevent deletion past last end of paragraph run. */
299 nChars = min(nChars, ME_GetTextLength(editor) - nOfs);
300 start_para = c.pPara;
304 ME_ProtectPartialTableDeletion(editor, &c, &nChars);
312 ME_CursorFromCharOfs(editor, nOfs+nChars, &c);
314 nOfs+nChars == (c.pRun->member.run.nCharOfs
315 + c.pPara->member.para.nCharOfs))
317 /* We aren't deleting anything in this run, so we will go back to the
318 * last run we are deleting text in. */
319 ME_PrevRun(&c.pPara, &c.pRun);
320 c.nOffset = c.pRun->member.run.strText->nLen;
322 run = &c.pRun->member.run;
323 if (run->nFlags & MERF_ENDPARA) {
324 int eollen = c.pRun->member.run.strText->nLen;
325 BOOL keepFirstParaFormat;
327 if (!ME_FindItemFwd(c.pRun, diParagraph))
331 keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
333 if (!editor->bEmulateVersion10) /* v4.1 */
335 ME_DisplayItem *next_para = ME_FindItemFwd(c.pRun, diParagraphOrEnd);
336 ME_DisplayItem *this_para = next_para->member.para.prev_para;
338 /* The end of paragraph before a table row is only deleted if there
339 * is nothing else on the line before it. */
340 if (this_para == start_para &&
341 next_para->member.para.nFlags & MEPF_ROWSTART)
343 /* If the paragraph will be empty, then it should be deleted, however
344 * it still might have text right now which would inherit the
345 * MEPF_STARTROW property if we joined it right now.
346 * Instead we will delete it after the preceding text is deleted. */
347 if (nOfs > this_para->member.para.nCharOfs) {
348 /* Skip this end of line. */
349 nChars -= (eollen < nChars) ? eollen : nChars;
352 keepFirstParaFormat = TRUE;
355 ME_JoinParagraphs(editor, c.pPara, keepFirstParaFormat);
356 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
357 ME_CheckCharOffsets(editor);
358 nChars -= (eollen < nChars) ? eollen : nChars;
365 int nCharsToDelete = min(nChars, c.nOffset);
368 c.nOffset -= nCharsToDelete;
370 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
373 /* nChars is the number of characters that should be deleted from the
374 PRECEDING runs (these BEFORE cursor.pRun)
375 nCharsToDelete is a number of chars to delete from THIS run */
376 nChars -= nCharsToDelete;
377 shift -= nCharsToDelete;
378 TRACE("Deleting %d (remaning %d) chars at %d in '%s' (%d)\n",
379 nCharsToDelete, nChars, c.nOffset,
380 debugstr_w(run->strText->szData), run->strText->nLen);
382 undo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
385 /* nOfs is a character offset (from the start of the document
386 to the current (deleted) run */
387 undo->di.member.run.nCharOfs = nOfs + nChars;
388 undo->di.member.run.strText = ME_MakeStringN(run->strText->szData + c.nOffset, nCharsToDelete);
391 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
392 TRACE("Shift value: %d\n", shift);
393 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
395 /* update cursors (including c) */
396 for (i=-1; i<editor->nCursors; i++) {
397 ME_Cursor *pThisCur = editor->pCursors + i;
398 if (i == -1) pThisCur = &c;
399 if (pThisCur->pRun == cursor.pRun) {
400 if (pThisCur->nOffset > cursor.nOffset) {
401 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
402 pThisCur->nOffset = cursor.nOffset;
404 pThisCur->nOffset -= nCharsToDelete;
405 assert(pThisCur->nOffset >= 0);
406 assert(pThisCur->nOffset <= run->strText->nLen);
408 if (pThisCur->nOffset == run->strText->nLen)
410 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
411 assert(pThisCur->pRun->type == diRun);
412 pThisCur->nOffset = 0;
417 /* c = updated data now */
419 if (c.pRun == cursor.pRun)
420 ME_SkipAndPropagateCharOffset(c.pRun, shift);
422 ME_PropagateCharOffset(c.pRun, shift);
424 if (!cursor.pRun->member.run.strText->nLen)
426 TRACE("Removing useless run\n");
427 ME_Remove(cursor.pRun);
428 ME_DestroyDisplayItem(cursor.pRun);
433 ME_CheckCharOffsets(editor);
441 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
443 assert(nCursor>=0 && nCursor<editor->nCursors);
444 /* text operations set modified state */
445 editor->nModifyStep = 1;
446 return ME_InternalDeleteText(editor, &editor->pCursors[nCursor],
450 static ME_DisplayItem *
451 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
452 const WCHAR *str, int len, ME_Style *style,
455 ME_Cursor *p = &editor->pCursors[nCursor];
457 editor->bCaretAtEnd = FALSE;
459 assert(p->pRun->type == diRun);
461 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
465 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
467 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
472 if (ME_IsSelection(editor))
473 ME_DeleteSelection(editor);
475 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
477 di->member.run.ole_obj = ALLOC_OBJ(*reo);
478 ME_CopyReObject(di->member.run.ole_obj, reo);
479 ME_ReleaseStyle(pStyle);
483 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
485 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
489 if (ME_IsSelection(editor))
490 ME_DeleteSelection(editor);
492 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
494 ME_ReleaseStyle(pStyle);
498 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
499 const WCHAR *str, int len, ME_Style *style)
505 /* FIXME really HERE ? */
506 if (ME_IsSelection(editor))
507 ME_DeleteSelection(editor);
509 /* FIXME: is this too slow? */
510 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
511 oldLen = ME_GetTextLength(editor);
513 /* text operations set modified state */
514 editor->nModifyStep = 1;
518 assert(nCursor>=0 && nCursor<editor->nCursors);
522 /* grow the text limit to fit our text */
523 if(editor->nTextLimit < oldLen +len)
524 editor->nTextLimit = oldLen + len;
530 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
531 while(pos - str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
534 if (pos != str) { /* handle text */
535 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
536 } else if (*pos == '\t') { /* handle tabs */
538 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
540 } else { /* handle EOLs */
541 ME_DisplayItem *tp, *end_run;
545 /* Find number of CR and LF in end of paragraph run */
548 if (len > 1 && pos[1] == '\n')
550 else if (len > 2 && pos[1] == '\r' && pos[2] == '\n')
555 assert(*pos == '\n');
560 if (!editor->bEmulateVersion10 && eol_len == 3)
562 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
564 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
568 if (!editor->bEmulateVersion10) {
570 eol_str = ME_MakeStringN(&cr, 1);
572 eol_str = ME_MakeStringN(str, eol_len);
575 p = &editor->pCursors[nCursor];
577 ME_SplitRunSimple(editor, p);
578 tmp_style = ME_GetInsertStyle(editor, nCursor);
579 /* ME_SplitParagraph increases style refcount */
580 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, eol_str, 0);
581 p->pRun = ME_FindItemFwd(tp, diRun);
583 end_run = ME_FindItemBack(tp, diRun);
584 ME_ReleaseStyle(end_run->member.run.style);
585 end_run->member.run.style = tmp_style;
594 /* Move the cursor nRelOfs characters (either forwards or backwards)
596 * returns the actual number of characters moved.
598 int ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
600 cursor->nOffset += nRelOfs;
601 if (cursor->nOffset < 0)
603 cursor->nOffset += cursor->pRun->member.run.nCharOfs;
604 if (cursor->nOffset >= 0)
606 /* new offset in the same paragraph */
608 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
609 } while (cursor->nOffset < cursor->pRun->member.run.nCharOfs);
610 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
614 cursor->nOffset += cursor->pPara->member.para.nCharOfs;
615 if (cursor->nOffset <= 0)
617 /* moved to the start of the text */
618 nRelOfs -= cursor->nOffset;
619 ME_SetCursorToStart(editor, cursor);
623 /* new offset in a previous paragraph */
625 cursor->pPara = cursor->pPara->member.para.prev_para;
626 } while (cursor->nOffset < cursor->pPara->member.para.nCharOfs);
627 cursor->nOffset -= cursor->pPara->member.para.nCharOfs;
629 cursor->pRun = ME_FindItemBack(cursor->pPara->member.para.next_para, diRun);
630 while (cursor->nOffset < cursor->pRun->member.run.nCharOfs) {
631 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
633 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
634 } else if (cursor->nOffset >= cursor->pRun->member.run.strText->nLen) {
635 ME_DisplayItem *next_para;
638 new_offset = ME_GetCursorOfs(cursor);
639 next_para = cursor->pPara->member.para.next_para;
640 if (new_offset < next_para->member.para.nCharOfs)
642 /* new offset in the same paragraph */
644 cursor->nOffset -= cursor->pRun->member.run.strText->nLen;
645 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
646 } while (cursor->nOffset >= cursor->pRun->member.run.strText->nLen);
650 if (new_offset >= ME_GetTextLength(editor))
652 /* new offset at the end of the text */
653 ME_SetCursorToEnd(editor, cursor);
654 nRelOfs -= new_offset - ME_GetTextLength(editor);
658 /* new offset in a following paragraph */
660 cursor->pPara = next_para;
661 next_para = next_para->member.para.next_para;
662 } while (new_offset >= next_para->member.para.nCharOfs);
664 cursor->nOffset = new_offset - cursor->pPara->member.para.nCharOfs;
665 cursor->pRun = ME_FindItemFwd(cursor->pPara, diRun);
666 while (cursor->nOffset >= cursor->pRun->member.run.strText->nLen)
668 cursor->nOffset -= cursor->pRun->member.run.strText->nLen;
669 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
671 } /* else new offset is in the same run */
677 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
679 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
680 ME_DisplayItem *pPara = cursor->pPara;
681 int nOffset = cursor->nOffset;
685 /* Backward movement */
688 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
689 nOffset, WB_MOVEWORDLEFT);
692 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
693 if (pOtherRun->type == diRun)
695 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
696 pOtherRun->member.run.strText->nLen - 1,
698 && !(pRun->member.run.nFlags & MERF_ENDPARA)
699 && !(cursor->pRun == pRun && cursor->nOffset == 0)
700 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
704 nOffset = pOtherRun->member.run.strText->nLen;
706 else if (pOtherRun->type == diParagraph)
708 if (cursor->pRun == pRun && cursor->nOffset == 0)
711 /* Skip empty start of table row paragraph */
712 if (pPara->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART)
713 pPara = pPara->member.para.prev_para;
714 /* Paragraph breaks are treated as separate words */
715 if (pPara->member.para.prev_para->type == diTextStart)
718 pRun = ME_FindItemBack(pPara, diRun);
719 pPara = pPara->member.para.prev_para;
727 /* Forward movement */
728 BOOL last_delim = FALSE;
732 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
733 nOffset, WB_ISDELIMITER))
735 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
736 nOffset, WB_MOVEWORDRIGHT);
737 if (nOffset < pRun->member.run.strText->nLen)
739 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
740 if (pOtherRun->type == diRun)
742 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
743 nOffset - 1, WB_ISDELIMITER);
747 else if (pOtherRun->type == diParagraph)
749 if (pOtherRun->member.para.nFlags & MEPF_ROWSTART)
750 pOtherRun = pOtherRun->member.para.next_para;
751 if (cursor->pRun == pRun) {
753 pRun = ME_FindItemFwd(pPara, diRun);
760 if (cursor->pRun == pRun)
767 cursor->pPara = pPara;
769 cursor->nOffset = nOffset;
775 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
777 /* pCursor[0] is the end of the selection
778 * pCursor[1] is the start of the selection (or the position selection anchor)
779 * pCursor[2] and [3] are the selection anchors that are backed up
780 * so they are kept when the selection changes for drag selection.
783 editor->nSelectionType = selectionType;
784 switch(selectionType)
789 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
790 editor->pCursors[1] = editor->pCursors[0];
791 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
796 ME_DisplayItem *pItem;
797 ME_DIType fwdSearchType, backSearchType;
798 if (selectionType == stParagraph) {
799 backSearchType = diParagraph;
800 fwdSearchType = diParagraphOrEnd;
802 backSearchType = diStartRow;
803 fwdSearchType = diStartRowOrParagraphOrEnd;
805 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
807 if (pItem->type == diTextEnd)
808 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
810 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
811 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
812 editor->pCursors[0].nOffset = 0;
814 pItem = ME_FindItemBack(pItem, backSearchType);
815 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
816 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
817 editor->pCursors[1].nOffset = 0;
821 /* Select everything with cursor anchored from the start of the text */
822 editor->nSelectionType = stDocument;
823 ME_SetCursorToStart(editor, &editor->pCursors[1]);
824 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
828 /* Store the anchor positions for extending the selection. */
829 editor->pCursors[2] = editor->pCursors[0];
830 editor->pCursors[3] = editor->pCursors[1];
833 int ME_GetCursorOfs(const ME_Cursor *cursor)
835 return cursor->pPara->member.para.nCharOfs
836 + cursor->pRun->member.run.nCharOfs + cursor->nOffset;
839 /* Helper function for ME_FindPixelPos to find paragraph within tables */
840 static ME_DisplayItem* ME_FindPixelPosInTableRow(int x, int y,
841 ME_DisplayItem *para)
843 ME_DisplayItem *cell, *next_cell;
844 assert(para->member.para.nFlags & MEPF_ROWSTART);
845 cell = para->member.para.next_para->member.para.pCell;
848 /* find the cell we are in */
849 while ((next_cell = cell->member.cell.next_cell) != NULL) {
850 if (x < next_cell->member.cell.pt.x)
852 para = ME_FindItemFwd(cell, diParagraph);
853 /* Found the cell, but there might be multiple paragraphs in
854 * the cell, so need to search down the cell for the paragraph. */
855 while (cell == para->member.para.pCell) {
856 if (y < para->member.para.pt.y + para->member.para.nHeight)
858 if (para->member.para.nFlags & MEPF_ROWSTART)
859 return ME_FindPixelPosInTableRow(x, y, para);
863 para = para->member.para.next_para;
865 /* Past the end of the cell, so go back to the last cell paragraph */
866 return para->member.para.prev_para;
870 /* Return table row delimiter */
871 para = ME_FindItemFwd(cell, diParagraph);
872 assert(para->member.para.nFlags & MEPF_ROWEND);
873 assert(para->member.para.pFmt->dwMask & PFM_TABLEROWDELIMITER);
874 assert(para->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER);
878 static BOOL ME_ReturnFoundPos(ME_TextEditor *editor, ME_DisplayItem *found,
879 ME_Cursor *result, int rx, BOOL isExact)
882 assert(found->type == diRun);
883 if ((found->member.run.nFlags & MERF_ENDPARA) || rx < 0)
885 result->pRun = found;
886 result->nOffset = ME_CharFromPointCursor(editor, rx, &found->member.run);
887 if (result->nOffset == found->member.run.strText->nLen && rx)
889 result->pRun = ME_FindItemFwd(result->pRun, diRun);
892 result->pPara = ME_GetParagraph(result->pRun);
896 /* Finds the run and offset from the pixel position.
898 * x & y are pixel positions in virtual coordinates into the rich edit control,
899 * so client coordinates must first be adjusted by the scroll position.
901 * returns TRUE if the result was exactly under the cursor, otherwise returns
902 * FALSE, and result is set to the closest position to the coordinates.
904 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
905 ME_Cursor *result, BOOL *is_eol)
907 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
908 ME_DisplayItem *last = NULL;
912 x -= editor->rcFormat.left;
913 y -= editor->rcFormat.top;
919 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
921 assert(p->type == diParagraph);
922 if (y < p->member.para.pt.y + p->member.para.nHeight)
924 if (p->member.para.nFlags & MEPF_ROWSTART)
925 p = ME_FindPixelPosInTableRow(x, y, p);
926 y -= p->member.para.pt.y;
927 p = ME_FindItemFwd(p, diStartRow);
929 } else if (p->member.para.nFlags & MEPF_ROWSTART) {
930 p = ME_GetTableRowEnd(p);
934 for (; p != editor->pBuffer->pLast; )
937 assert(p->type == diStartRow);
938 if (y < p->member.row.pt.y + p->member.row.nHeight)
940 p = ME_FindItemFwd(p, diRun);
943 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
944 if (pp->type != diStartRow)
946 p = ME_FindItemFwd(p, diRun);
951 if (p == editor->pBuffer->pLast)
953 /* The position is below the last paragraph, so the last row will be used
954 * rather than the end of the text, so the x position will be used to
955 * determine the offset closest to the pixel position. */
957 p = ME_FindItemBack(p, diStartRow);
959 p = ME_FindItemFwd(p, diRun);
963 p = editor->pBuffer->pLast;
966 for (; p != editor->pBuffer->pLast; p = p->next)
971 rx = x - p->member.run.pt.x;
972 if (rx < p->member.run.nWidth)
973 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
977 p = ME_FindItemFwd(p, diRun);
978 if (is_eol) *is_eol = 1;
979 rx = 0; /* FIXME not sure */
980 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
985 rx = 0; /* FIXME not sure */
987 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
992 result->pRun = ME_FindItemBack(p, diRun);
993 result->pPara = ME_GetParagraph(result->pRun);
995 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
1000 /* Sets the cursor to the position closest to the pixel position
1002 * x & y are pixel positions in client coordinates.
1004 * isExact will be set to TRUE if the run is directly under the pixel
1005 * position, FALSE if it not, unless isExact is set to NULL.
1007 * return FALSE if outside client area and the cursor is not set,
1008 * otherwise TRUE is returned.
1010 BOOL ME_CharFromPos(ME_TextEditor *editor, int x, int y,
1011 ME_Cursor *cursor, BOOL *isExact)
1016 ITextHost_TxGetClientRect(editor->texthost, &rc);
1017 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
1018 if (isExact) *isExact = FALSE;
1021 x += editor->horz_si.nPos;
1022 y += editor->vert_si.nPos;
1023 bResult = ME_FindPixelPos(editor, x, y, cursor, NULL);
1024 if (isExact) *isExact = bResult;
1030 /* Extends the selection with a word, line, or paragraph selection type.
1032 * The selection is anchored by editor->pCursors[2-3] such that the text
1033 * between the anchors will remain selected, and one end will be extended.
1035 * editor->pCursors[0] should have the position to extend the selection to
1036 * before this function is called.
1038 * Nothing will be done if editor->nSelectionType equals stPosition.
1040 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1042 ME_Cursor tmp_cursor;
1043 int curOfs, anchorStartOfs, anchorEndOfs;
1044 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1046 curOfs = ME_GetCursorOfs(&editor->pCursors[0]);
1047 anchorStartOfs = ME_GetCursorOfs(&editor->pCursors[3]);
1048 anchorEndOfs = ME_GetCursorOfs(&editor->pCursors[2]);
1050 tmp_cursor = editor->pCursors[0];
1051 editor->pCursors[0] = editor->pCursors[2];
1052 editor->pCursors[1] = editor->pCursors[3];
1053 if (curOfs < anchorStartOfs)
1055 /* Extend the left side of selection */
1056 editor->pCursors[1] = tmp_cursor;
1057 if (editor->nSelectionType == stWord)
1058 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1061 ME_DisplayItem *pItem;
1062 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1063 diStartRowOrParagraph:diParagraph);
1064 pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
1065 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
1066 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
1067 editor->pCursors[1].nOffset = 0;
1070 else if (curOfs >= anchorEndOfs)
1072 /* Extend the right side of selection */
1073 editor->pCursors[0] = tmp_cursor;
1074 if (editor->nSelectionType == stWord)
1075 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
1078 ME_DisplayItem *pItem;
1079 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1080 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
1081 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
1082 if (pItem->type == diTextEnd)
1083 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
1085 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
1086 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
1087 editor->pCursors[0].nOffset = 0;
1092 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1094 ME_Cursor tmp_cursor;
1095 int is_selection = 0;
1098 editor->nUDArrowX = -1;
1100 x += editor->horz_si.nPos;
1101 y += editor->vert_si.nPos;
1103 tmp_cursor = editor->pCursors[0];
1104 is_selection = ME_IsSelection(editor);
1105 is_shift = GetKeyState(VK_SHIFT) < 0;
1107 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
1109 if (x >= editor->rcFormat.left || is_shift)
1113 editor->pCursors[1] = editor->pCursors[0];
1115 if (x >= editor->rcFormat.left)
1116 ME_SelectByType(editor, stWord);
1118 ME_SelectByType(editor, stParagraph);
1119 } else if (clickNum % 2 == 0) {
1120 ME_SelectByType(editor, stWord);
1122 ME_SelectByType(editor, stParagraph);
1127 editor->nSelectionType = stPosition;
1128 editor->pCursors[1] = editor->pCursors[0];
1130 else if (!is_selection)
1132 editor->nSelectionType = stPosition;
1133 editor->pCursors[1] = tmp_cursor;
1135 else if (editor->nSelectionType != stPosition)
1137 ME_ExtendAnchorSelection(editor);
1143 ME_SelectByType(editor, stLine);
1144 } else if (clickNum % 2 == 0 || is_shift) {
1145 ME_SelectByType(editor, stParagraph);
1147 ME_SelectByType(editor, stDocument);
1150 ME_InvalidateSelection(editor);
1151 ITextHost_TxShowCaret(editor->texthost, FALSE);
1152 ME_ShowCaret(editor);
1153 ME_ClearTempStyle(editor);
1154 ME_SendSelChange(editor);
1157 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1159 ME_Cursor tmp_cursor;
1161 if (editor->nSelectionType == stDocument)
1163 x += editor->horz_si.nPos;
1164 y += editor->vert_si.nPos;
1166 tmp_cursor = editor->pCursors[0];
1167 /* FIXME: do something with the return value of ME_FindPixelPos */
1168 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
1170 ME_InvalidateSelection(editor);
1171 editor->pCursors[0] = tmp_cursor;
1172 ME_ExtendAnchorSelection(editor);
1174 if (editor->nSelectionType != stPosition &&
1175 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1177 /* The scroll the cursor towards the other end, since it was the one
1178 * extended by ME_ExtendAnchorSelection */
1179 ME_EnsureVisible(editor, &editor->pCursors[1]);
1181 ME_EnsureVisible(editor, &editor->pCursors[0]);
1184 ME_InvalidateSelection(editor);
1185 ITextHost_TxShowCaret(editor->texthost, FALSE);
1186 ME_ShowCaret(editor);
1187 ME_SendSelChange(editor);
1190 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
1191 int x, int *pOffset, int *pbCaretAtEnd)
1193 ME_DisplayItem *pNext, *pLastRun;
1194 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
1195 assert(pNext->type == diRun);
1196 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
1197 if (pOffset) *pOffset = 0;
1199 int run_x = pNext->member.run.pt.x;
1200 int width = pNext->member.run.nWidth;
1205 if (x >= run_x && x < run_x+width)
1207 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
1208 ME_String *s = pNext->member.run.strText;
1216 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
1217 } while(pNext && pNext->type == diRun);
1219 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
1221 pNext = ME_FindItemFwd(pNext, diRun);
1222 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
1229 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1231 ME_DisplayItem *pRun = pCursor->pRun;
1234 if (editor->nUDArrowX != -1)
1235 x = editor->nUDArrowX;
1237 if (editor->bCaretAtEnd)
1239 pRun = ME_FindItemBack(pRun, diRun);
1241 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1244 x = pRun->member.run.pt.x;
1245 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
1247 editor->nUDArrowX = x;
1254 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1256 ME_DisplayItem *pRun = pCursor->pRun;
1257 ME_DisplayItem *pOldPara = pCursor->pPara;
1258 ME_DisplayItem *pItem, *pNewPara;
1259 int x = ME_GetXForArrow(editor, pCursor);
1261 if (editor->bCaretAtEnd && !pCursor->nOffset)
1262 if (!ME_PrevRun(&pOldPara, &pRun))
1267 /* start of this row */
1268 pItem = ME_FindItemBack(pRun, diStartRow);
1270 /* start of the previous row */
1271 pItem = ME_FindItemBack(pItem, diStartRow);
1273 return; /* row not found - ignore */
1274 pNewPara = ME_GetParagraph(pItem);
1275 if (pOldPara->member.para.nFlags & MEPF_ROWEND ||
1276 (pOldPara->member.para.pCell &&
1277 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1279 /* Brought out of a cell */
1280 pNewPara = ME_GetTableRowStart(pOldPara)->member.para.prev_para;
1281 if (pNewPara->type == diTextStart)
1282 return; /* At the top, so don't go anywhere. */
1283 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1285 if (pNewPara->member.para.nFlags & MEPF_ROWEND)
1287 /* Brought into a table row */
1288 ME_Cell *cell = &ME_FindItemBack(pNewPara, diCell)->member.cell;
1289 while (x < cell->pt.x && cell->prev_cell)
1290 cell = &cell->prev_cell->member.cell;
1291 if (cell->next_cell) /* else - we are still at the end of the row */
1292 pItem = ME_FindItemBack(cell->next_cell, diStartRow);
1297 /* start of the next row */
1298 pItem = ME_FindItemFwd(pRun, diStartRow);
1300 return; /* row not found - ignore */
1301 pNewPara = ME_GetParagraph(pItem);
1302 if (pOldPara->member.para.nFlags & MEPF_ROWSTART ||
1303 (pOldPara->member.para.pCell &&
1304 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1306 /* Brought out of a cell */
1307 pNewPara = ME_GetTableRowEnd(pOldPara)->member.para.next_para;
1308 if (pNewPara->type == diTextEnd)
1309 return; /* At the bottom, so don't go anywhere. */
1310 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1312 if (pNewPara->member.para.nFlags & MEPF_ROWSTART)
1314 /* Brought into a table row */
1315 ME_DisplayItem *cell = ME_FindItemFwd(pNewPara, diCell);
1316 while (cell->member.cell.next_cell &&
1317 x >= cell->member.cell.next_cell->member.cell.pt.x)
1318 cell = cell->member.cell.next_cell;
1319 pItem = ME_FindItemFwd(cell, diStartRow);
1324 /* row not found - ignore */
1327 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1328 pCursor->pPara = ME_GetParagraph(pCursor->pRun);
1329 assert(pCursor->pRun);
1330 assert(pCursor->pRun->type == diRun);
1333 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1335 ME_DisplayItem *p = ME_FindItemFwd(editor->pBuffer->pFirst, diStartRow);
1337 if (editor->vert_si.nPos < p->member.row.nHeight)
1339 ME_SetCursorToStart(editor, pCursor);
1340 editor->bCaretAtEnd = FALSE;
1341 /* Native clears seems to clear this x value on page up at the top
1342 * of the text, but not on page down at the end of the text.
1343 * Doesn't make sense, but we try to be bug for bug compatible. */
1344 editor->nUDArrowX = -1;
1346 ME_DisplayItem *pRun = pCursor->pRun;
1347 ME_DisplayItem *pLast;
1349 int yOldScrollPos = editor->vert_si.nPos;
1351 x = ME_GetXForArrow(editor, pCursor);
1352 if (!pCursor->nOffset && editor->bCaretAtEnd)
1353 pRun = ME_FindItemBack(pRun, diRun);
1355 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1356 assert(p->type == diStartRow);
1357 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1358 y = yp + p->member.row.pt.y;
1360 ME_ScrollUp(editor, editor->sizeWindow.cy);
1361 /* Only move the cursor by the amount scrolled. */
1362 yd = y + editor->vert_si.nPos - yOldScrollPos;
1366 p = ME_FindItemBack(p, diStartRowOrParagraph);
1369 if (p->type == diParagraph) { /* crossing paragraphs */
1370 if (p->member.para.prev_para == NULL)
1372 yp = p->member.para.prev_para->member.para.pt.y;
1375 y = yp + p->member.row.pt.y;
1381 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset,
1382 &editor->bCaretAtEnd);
1383 pCursor->pPara = ME_GetParagraph(pCursor->pRun);
1385 assert(pCursor->pRun);
1386 assert(pCursor->pRun->type == diRun);
1389 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1391 ME_DisplayItem *pLast;
1394 /* Find y position of the last row */
1395 pLast = editor->pBuffer->pLast;
1396 y = pLast->member.para.prev_para->member.para.pt.y
1397 + ME_FindItemBack(pLast, diStartRow)->member.row.pt.y;
1399 x = ME_GetXForArrow(editor, pCursor);
1401 if (editor->vert_si.nPos >= y - editor->sizeWindow.cy)
1403 ME_SetCursorToEnd(editor, pCursor);
1404 editor->bCaretAtEnd = FALSE;
1406 ME_DisplayItem *pRun = pCursor->pRun;
1409 int yOldScrollPos = editor->vert_si.nPos;
1411 if (!pCursor->nOffset && editor->bCaretAtEnd)
1412 pRun = ME_FindItemBack(pRun, diRun);
1414 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1415 assert(p->type == diStartRow);
1416 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1417 y = yp + p->member.row.pt.y;
1419 /* For native richedit controls:
1420 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1421 * v4.1 can scroll past this position here. */
1422 ME_ScrollDown(editor, editor->sizeWindow.cy);
1423 /* Only move the cursor by the amount scrolled. */
1424 yd = y + editor->vert_si.nPos - yOldScrollPos;
1428 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1431 if (p->type == diParagraph) {
1432 yp = p->member.para.pt.y;
1435 y = yp + p->member.row.pt.y;
1441 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset,
1442 &editor->bCaretAtEnd);
1443 pCursor->pPara = ME_GetParagraph(pCursor->pRun);
1445 assert(pCursor->pRun);
1446 assert(pCursor->pRun->type == diRun);
1449 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1451 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1453 ME_DisplayItem *pRun;
1454 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1455 pRow = ME_FindItemBack(pRow, diStartRow);
1459 pRun = ME_FindItemFwd(pRow, diRun);
1461 pCursor->pRun = pRun;
1462 assert(pCursor->pPara == ME_GetParagraph(pRun));
1463 pCursor->nOffset = 0;
1466 editor->bCaretAtEnd = FALSE;
1469 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1471 ME_SetCursorToStart(editor, pCursor);
1472 editor->bCaretAtEnd = FALSE;
1475 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1477 ME_DisplayItem *pRow;
1479 if (editor->bCaretAtEnd && !pCursor->nOffset)
1482 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1484 if (pRow->type == diStartRow) {
1485 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1487 pCursor->pRun = pRun;
1488 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1489 pCursor->nOffset = 0;
1490 editor->bCaretAtEnd = TRUE;
1493 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1494 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1495 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1496 pCursor->nOffset = 0;
1497 editor->bCaretAtEnd = FALSE;
1500 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1502 ME_SetCursorToEnd(editor, pCursor);
1503 editor->bCaretAtEnd = FALSE;
1506 BOOL ME_IsSelection(ME_TextEditor *editor)
1508 return editor->pCursors[0].pRun != editor->pCursors[1].pRun ||
1509 editor->pCursors[0].nOffset != editor->pCursors[1].nOffset;
1512 void ME_DeleteSelection(ME_TextEditor *editor)
1515 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
1516 ME_DeleteTextAtCursor(editor, nStartCursor, to - from);
1519 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1521 return ME_GetInsertStyle(editor, 0);
1524 void ME_SendSelChange(ME_TextEditor *editor)
1528 if (!(editor->nEventMask & ENM_SELCHANGE))
1531 sc.nmhdr.hwndFrom = NULL;
1532 sc.nmhdr.idFrom = 0;
1533 sc.nmhdr.code = EN_SELCHANGE;
1534 ME_GetSelectionOfs(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1535 sc.seltyp = SEL_EMPTY;
1536 if (sc.chrg.cpMin != sc.chrg.cpMax)
1537 sc.seltyp |= SEL_TEXT;
1538 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* what were RICHEDIT authors thinking ? */
1539 sc.seltyp |= SEL_MULTICHAR;
1540 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1541 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1542 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1543 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1544 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1546 ME_ClearTempStyle(editor);
1548 editor->notified_cr = sc.chrg;
1549 ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
1554 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1557 ME_Cursor *p = &editor->pCursors[nCursor];
1558 ME_Cursor tmp_curs = *p;
1559 BOOL success = FALSE;
1561 ME_CheckCharOffsets(editor);
1564 editor->bCaretAtEnd = 0;
1566 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1568 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1571 editor->bCaretAtEnd = 0;
1573 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1575 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1578 ME_MoveCursorLines(editor, &tmp_curs, -1);
1581 ME_MoveCursorLines(editor, &tmp_curs, +1);
1584 ME_ArrowPageUp(editor, &tmp_curs);
1587 ME_ArrowPageDown(editor, &tmp_curs);
1591 ME_ArrowCtrlHome(editor, &tmp_curs);
1593 ME_ArrowHome(editor, &tmp_curs);
1594 editor->bCaretAtEnd = 0;
1599 ME_ArrowCtrlEnd(editor, &tmp_curs);
1601 ME_ArrowEnd(editor, &tmp_curs);
1606 editor->pCursors[1] = tmp_curs;
1609 ME_InvalidateSelection(editor);
1611 ITextHost_TxShowCaret(editor->texthost, FALSE);
1612 ME_EnsureVisible(editor, &tmp_curs);
1613 ME_ShowCaret(editor);
1614 ME_SendSelChange(editor);