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)
99 if (how->codepage == 1200)
101 if (how->flags & GTL_PRECISE)
102 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
103 if (GetCPInfo(how->codepage, &cpinfo))
104 return length * cpinfo.MaxCharSize;
105 ERR("Invalid codepage %u\n", how->codepage);
112 int ME_SetSelection(ME_TextEditor *editor, int from, int to)
114 int selectionEnd = 0;
115 const int len = ME_GetTextLength(editor);
117 /* all negative values are effectively the same */
124 if (from == 0 && to == -1)
126 ME_SetCursorToStart(editor, &editor->pCursors[1]);
127 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
128 ME_InvalidateSelection(editor);
129 ME_ClearTempStyle(editor);
133 /* if both values are equal and also out of bound, that means to */
134 /* put the selection at the end of the text */
135 if ((from == to) && (to < 0 || to > len))
141 /* if from is negative and to is positive then selection is */
142 /* deselected and caret moved to end of the current selection */
146 ME_GetSelectionOfs(editor, &start, &end);
147 editor->pCursors[1] = editor->pCursors[0];
149 ME_ClearTempStyle(editor);
153 /* adjust to if it's a negative value */
157 /* flip from and to if they are reversed */
165 /* after fiddling with the values, we find from > len && to > len */
168 /* special case with to too big */
175 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
176 editor->pCursors[1] = editor->pCursors[0];
177 ME_InvalidateSelection(editor);
178 ME_ClearTempStyle(editor);
182 ME_CursorFromCharOfs(editor, from, &editor->pCursors[1]);
183 editor->pCursors[0] = editor->pCursors[1];
184 ME_MoveCursorChars(editor, &editor->pCursors[0], to - from);
185 /* Selection is not allowed in the middle of an end paragraph run. */
186 if (editor->pCursors[1].pRun->member.run.nFlags & MERF_ENDPARA)
187 editor->pCursors[1].nOffset = 0;
188 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA)
189 editor->pCursors[0].nOffset = 0;
195 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
196 int *x, int *y, int *height)
199 ME_DisplayItem *run = pCursor->pRun;
200 ME_DisplayItem *para = pCursor->pPara;
201 ME_DisplayItem *pSizeRun = run;
205 assert(height && x && y);
206 assert(~para->member.para.nFlags & MEPF_REWRAP);
207 assert(run && run->type == diRun);
208 assert(para && para->type == diParagraph);
210 row = ME_FindItemBack(run, diStartRowOrParagraph);
211 assert(row && row->type == diStartRow);
213 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
215 if (!pCursor->nOffset)
217 ME_DisplayItem *prev = ME_FindItemBack(run, diRunOrParagraph);
219 if (prev->type == diRun)
222 if (editor->bCaretAtEnd && !pCursor->nOffset &&
223 run == ME_FindItemFwd(row, diRun))
225 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
227 if (tmp->type == diRun)
229 row = ME_FindItemBack(tmp, diStartRow);
230 pSizeRun = run = tmp;
232 assert(run->type == diRun);
233 sz = ME_GetRunSize(&c, ¶->member.para,
234 &run->member.run, run->member.run.strText->nLen,
235 row->member.row.nLMargin);
238 if (pCursor->nOffset) {
239 sz = ME_GetRunSize(&c, ¶->member.para, &run->member.run,
240 pCursor->nOffset, row->member.row.nLMargin);
243 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
244 *x = c.rcView.left + run->member.run.pt.x + sz.cx - editor->horz_si.nPos;
245 *y = c.rcView.top + para->member.para.pt.y + row->member.row.nBaseline
246 + run->member.run.pt.y - pSizeRun->member.run.nAscent
247 - editor->vert_si.nPos;
248 ME_DestroyContext(&c);
254 ME_MoveCaret(ME_TextEditor *editor)
258 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
259 if(editor->bHaveFocus && !ME_IsSelection(editor))
261 x = min(x, editor->rcFormat.right-1);
262 ITextHost_TxCreateCaret(editor->texthost, NULL, 0, height);
263 ITextHost_TxSetCaretPos(editor->texthost, x, y);
268 void ME_ShowCaret(ME_TextEditor *ed)
271 if(ed->bHaveFocus && !ME_IsSelection(ed))
272 ITextHost_TxShowCaret(ed->texthost, TRUE);
275 void ME_HideCaret(ME_TextEditor *ed)
277 if(!ed->bHaveFocus || ME_IsSelection(ed))
279 ITextHost_TxShowCaret(ed->texthost, FALSE);
284 BOOL ME_InternalDeleteText(ME_TextEditor *editor, ME_Cursor *start,
285 int nChars, BOOL bForce)
287 ME_Cursor c = *start;
288 int nOfs = ME_GetCursorOfs(start);
290 int totalChars = nChars;
291 ME_DisplayItem *start_para;
293 /* Prevent deletion past last end of paragraph run. */
294 nChars = min(nChars, ME_GetTextLength(editor) - nOfs);
295 start_para = c.pPara;
299 ME_ProtectPartialTableDeletion(editor, &c, &nChars);
307 ME_CursorFromCharOfs(editor, nOfs+nChars, &c);
309 nOfs+nChars == (c.pRun->member.run.nCharOfs
310 + c.pPara->member.para.nCharOfs))
312 /* We aren't deleting anything in this run, so we will go back to the
313 * last run we are deleting text in. */
314 ME_PrevRun(&c.pPara, &c.pRun);
315 c.nOffset = c.pRun->member.run.strText->nLen;
317 run = &c.pRun->member.run;
318 if (run->nFlags & MERF_ENDPARA) {
319 int eollen = c.pRun->member.run.strText->nLen;
320 BOOL keepFirstParaFormat;
322 if (!ME_FindItemFwd(c.pRun, diParagraph))
326 keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
328 if (!editor->bEmulateVersion10) /* v4.1 */
330 ME_DisplayItem *next_para = ME_FindItemFwd(c.pRun, diParagraphOrEnd);
331 ME_DisplayItem *this_para = next_para->member.para.prev_para;
333 /* The end of paragraph before a table row is only deleted if there
334 * is nothing else on the line before it. */
335 if (this_para == start_para &&
336 next_para->member.para.nFlags & MEPF_ROWSTART)
338 /* If the paragraph will be empty, then it should be deleted, however
339 * it still might have text right now which would inherit the
340 * MEPF_STARTROW property if we joined it right now.
341 * Instead we will delete it after the preceding text is deleted. */
342 if (nOfs > this_para->member.para.nCharOfs) {
343 /* Skip this end of line. */
344 nChars -= (eollen < nChars) ? eollen : nChars;
347 keepFirstParaFormat = TRUE;
350 ME_JoinParagraphs(editor, c.pPara, keepFirstParaFormat);
351 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
352 ME_CheckCharOffsets(editor);
353 nChars -= (eollen < nChars) ? eollen : nChars;
359 int nCharsToDelete = min(nChars, c.nOffset);
362 c.nOffset -= nCharsToDelete;
364 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
367 /* nChars is the number of characters that should be deleted from the
368 PRECEDING runs (these BEFORE cursor.pRun)
369 nCharsToDelete is a number of chars to delete from THIS run */
370 nChars -= nCharsToDelete;
371 shift -= nCharsToDelete;
372 TRACE("Deleting %d (remaning %d) chars at %d in '%s' (%d)\n",
373 nCharsToDelete, nChars, c.nOffset,
374 debugstr_w(run->strText->szData), run->strText->nLen);
376 if (!c.nOffset && run->strText->nLen == nCharsToDelete)
378 /* undo = reinsert whole run */
379 /* nOfs is a character offset (from the start of the document
380 to the current (deleted) run */
381 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
383 pUndo->di.member.run.nCharOfs = nOfs+nChars;
387 /* undo = reinsert partial run */
388 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
390 ME_DestroyString(pUndo->di.member.run.strText);
391 pUndo->di.member.run.nCharOfs = nOfs+nChars;
392 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
395 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
396 TRACE("Shift value: %d\n", shift);
397 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
399 /* update cursors (including c) */
400 for (i=-1; i<editor->nCursors; i++) {
401 ME_Cursor *pThisCur = editor->pCursors + i;
402 if (i == -1) pThisCur = &c;
403 if (pThisCur->pRun == cursor.pRun) {
404 if (pThisCur->nOffset > cursor.nOffset) {
405 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
406 pThisCur->nOffset = cursor.nOffset;
408 pThisCur->nOffset -= nCharsToDelete;
409 assert(pThisCur->nOffset >= 0);
410 assert(pThisCur->nOffset <= run->strText->nLen);
412 if (pThisCur->nOffset == run->strText->nLen)
414 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
415 assert(pThisCur->pRun->type == diRun);
416 pThisCur->nOffset = 0;
421 /* c = updated data now */
423 if (c.pRun == cursor.pRun)
424 ME_SkipAndPropagateCharOffset(c.pRun, shift);
426 ME_PropagateCharOffset(c.pRun, shift);
428 if (!cursor.pRun->member.run.strText->nLen)
430 TRACE("Removing useless run\n");
431 ME_Remove(cursor.pRun);
432 ME_DestroyDisplayItem(cursor.pRun);
437 ME_CheckCharOffsets(editor);
445 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
447 assert(nCursor>=0 && nCursor<editor->nCursors);
448 /* text operations set modified state */
449 editor->nModifyStep = 1;
450 return ME_InternalDeleteText(editor, &editor->pCursors[nCursor],
454 static ME_DisplayItem *
455 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
456 const WCHAR *str, int len, ME_Style *style,
459 ME_Cursor *p = &editor->pCursors[nCursor];
461 editor->bCaretAtEnd = FALSE;
463 assert(p->pRun->type == diRun);
465 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
469 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
471 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
476 if (ME_IsSelection(editor))
477 ME_DeleteSelection(editor);
479 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
481 di->member.run.ole_obj = ALLOC_OBJ(*reo);
482 ME_CopyReObject(di->member.run.ole_obj, reo);
483 ME_ReleaseStyle(pStyle);
487 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
489 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
494 if (ME_IsSelection(editor))
495 ME_DeleteSelection(editor);
497 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
499 ME_ReleaseStyle(pStyle);
503 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
504 const WCHAR *str, int len, ME_Style *style)
510 /* FIXME really HERE ? */
511 if (ME_IsSelection(editor))
512 ME_DeleteSelection(editor);
514 /* FIXME: is this too slow? */
515 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
516 oldLen = ME_GetTextLength(editor);
518 /* text operations set modified state */
519 editor->nModifyStep = 1;
523 assert(nCursor>=0 && nCursor<editor->nCursors);
527 /* grow the text limit to fit our text */
528 if(editor->nTextLimit < oldLen +len)
529 editor->nTextLimit = oldLen + len;
535 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
536 while(pos - str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
539 if (pos != str) { /* handle text */
540 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
541 } else if (*pos == '\t') { /* handle tabs */
543 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
545 } else { /* handle EOLs */
546 ME_DisplayItem *tp, *end_run;
550 /* Find number of CR and LF in end of paragraph run */
553 if (len > 1 && pos[1] == '\n')
555 else if (len > 2 && pos[1] == '\r' && pos[2] == '\n')
560 assert(*pos == '\n');
565 if (!editor->bEmulateVersion10 && eol_len == 3)
567 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
569 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
573 if (!editor->bEmulateVersion10) {
575 eol_str = ME_MakeStringN(&cr, 1);
577 eol_str = ME_MakeStringN(str, eol_len);
580 p = &editor->pCursors[nCursor];
582 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
583 p = &editor->pCursors[nCursor];
585 tmp_style = ME_GetInsertStyle(editor, nCursor);
586 /* ME_SplitParagraph increases style refcount */
587 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, eol_str, 0);
588 p->pRun = ME_FindItemFwd(tp, diRun);
590 end_run = ME_FindItemBack(tp, diRun);
591 ME_ReleaseStyle(end_run->member.run.style);
592 end_run->member.run.style = tmp_style;
601 /* Move the cursor nRelOfs characters (either forwards or backwards)
603 * returns the actual number of characters moved.
605 int ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
607 cursor->nOffset += nRelOfs;
608 if (cursor->nOffset < 0)
610 cursor->nOffset += cursor->pRun->member.run.nCharOfs;
611 if (cursor->nOffset >= 0)
613 /* new offset in the same paragraph */
615 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
616 } while (cursor->nOffset < cursor->pRun->member.run.nCharOfs);
617 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
621 cursor->nOffset += cursor->pPara->member.para.nCharOfs;
622 if (cursor->nOffset <= 0)
624 /* moved to the start of the text */
625 nRelOfs -= cursor->nOffset;
626 ME_SetCursorToStart(editor, cursor);
630 /* new offset in a previous paragraph */
632 cursor->pPara = cursor->pPara->member.para.prev_para;
633 } while (cursor->nOffset < cursor->pPara->member.para.nCharOfs);
634 cursor->nOffset -= cursor->pPara->member.para.nCharOfs;
636 cursor->pRun = ME_FindItemBack(cursor->pPara->member.para.next_para, diRun);
637 while (cursor->nOffset < cursor->pRun->member.run.nCharOfs) {
638 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
640 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
641 } else if (cursor->nOffset >= cursor->pRun->member.run.strText->nLen) {
642 ME_DisplayItem *next_para;
645 new_offset = ME_GetCursorOfs(cursor);
646 next_para = cursor->pPara->member.para.next_para;
647 if (new_offset < next_para->member.para.nCharOfs)
649 /* new offset in the same paragraph */
651 cursor->nOffset -= cursor->pRun->member.run.strText->nLen;
652 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
653 } while (cursor->nOffset >= cursor->pRun->member.run.strText->nLen);
657 if (new_offset >= ME_GetTextLength(editor))
659 /* new offset at the end of the text */
660 ME_SetCursorToEnd(editor, cursor);
661 nRelOfs -= new_offset - ME_GetTextLength(editor);
665 /* new offset in a following paragraph */
667 cursor->pPara = next_para;
668 next_para = next_para->member.para.next_para;
669 } while (new_offset >= next_para->member.para.nCharOfs);
671 cursor->nOffset = new_offset - cursor->pPara->member.para.nCharOfs;
672 cursor->pRun = ME_FindItemFwd(cursor->pPara, diRun);
673 while (cursor->nOffset >= cursor->pRun->member.run.strText->nLen)
675 cursor->nOffset -= cursor->pRun->member.run.strText->nLen;
676 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
678 } /* else new offset is in the same run */
684 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
686 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
687 ME_DisplayItem *pPara = cursor->pPara;
688 int nOffset = cursor->nOffset;
692 /* Backward movement */
695 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
696 nOffset, WB_MOVEWORDLEFT);
699 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
700 if (pOtherRun->type == diRun)
702 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
703 pOtherRun->member.run.strText->nLen - 1,
705 && !(pRun->member.run.nFlags & MERF_ENDPARA)
706 && !(cursor->pRun == pRun && cursor->nOffset == 0)
707 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
711 nOffset = pOtherRun->member.run.strText->nLen;
713 else if (pOtherRun->type == diParagraph)
715 if (cursor->pRun == pRun && cursor->nOffset == 0)
718 /* Skip empty start of table row paragraph */
719 if (pPara->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART)
720 pPara = pPara->member.para.prev_para;
721 /* Paragraph breaks are treated as separate words */
722 if (pPara->member.para.prev_para->type == diTextStart)
725 pRun = ME_FindItemBack(pPara, diRun);
733 /* Forward movement */
734 BOOL last_delim = FALSE;
738 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
739 nOffset, WB_ISDELIMITER))
741 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
742 nOffset, WB_MOVEWORDRIGHT);
743 if (nOffset < pRun->member.run.strText->nLen)
745 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
746 if (pOtherRun->type == diRun)
748 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
749 nOffset - 1, WB_ISDELIMITER);
753 else if (pOtherRun->type == diParagraph)
755 if (pOtherRun->member.para.nFlags & MEPF_ROWSTART)
756 pOtherRun = pOtherRun->member.para.next_para;
757 if (cursor->pRun == pRun) {
759 pRun = ME_FindItemFwd(pPara, diRun);
766 if (cursor->pRun == pRun)
773 cursor->pPara = pPara;
775 cursor->nOffset = nOffset;
781 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
783 /* pCursor[0] is the end of the selection
784 * pCursor[1] is the start of the selection (or the position selection anchor)
785 * pCursor[2] and [3] are the selection anchors that are backed up
786 * so they are kept when the selection changes for drag selection.
789 editor->nSelectionType = selectionType;
790 switch(selectionType)
795 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
796 editor->pCursors[1] = editor->pCursors[0];
797 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
802 ME_DisplayItem *pItem;
803 ME_DIType fwdSearchType, backSearchType;
804 if (selectionType == stParagraph) {
805 backSearchType = diParagraph;
806 fwdSearchType = diParagraphOrEnd;
808 backSearchType = diStartRow;
809 fwdSearchType = diStartRowOrParagraphOrEnd;
811 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
813 if (pItem->type == diTextEnd)
814 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
816 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
817 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
818 editor->pCursors[0].nOffset = 0;
820 pItem = ME_FindItemBack(pItem, backSearchType);
821 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
822 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
823 editor->pCursors[1].nOffset = 0;
827 /* Select everything with cursor anchored from the start of the text */
828 editor->nSelectionType = stDocument;
829 ME_SetCursorToStart(editor, &editor->pCursors[1]);
830 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
834 /* Store the anchor positions for extending the selection. */
835 editor->pCursors[2] = editor->pCursors[0];
836 editor->pCursors[3] = editor->pCursors[1];
839 int ME_GetCursorOfs(const ME_Cursor *cursor)
841 return cursor->pPara->member.para.nCharOfs
842 + cursor->pRun->member.run.nCharOfs + cursor->nOffset;
845 /* Helper function for ME_FindPixelPos to find paragraph within tables */
846 static ME_DisplayItem* ME_FindPixelPosInTableRow(int x, int y,
847 ME_DisplayItem *para)
849 ME_DisplayItem *cell, *next_cell;
850 assert(para->member.para.nFlags & MEPF_ROWSTART);
851 cell = para->member.para.next_para->member.para.pCell;
854 /* find the cell we are in */
855 while ((next_cell = cell->member.cell.next_cell) != NULL) {
856 if (x < next_cell->member.cell.pt.x)
858 para = ME_FindItemFwd(cell, diParagraph);
859 /* Found the cell, but there might be multiple paragraphs in
860 * the cell, so need to search down the cell for the paragraph. */
861 while (cell == para->member.para.pCell) {
862 if (y < para->member.para.pt.y + para->member.para.nHeight)
864 if (para->member.para.nFlags & MEPF_ROWSTART)
865 return ME_FindPixelPosInTableRow(x, y, para);
869 para = para->member.para.next_para;
871 /* Past the end of the cell, so go back to the last cell paragraph */
872 return para->member.para.prev_para;
876 /* Return table row delimiter */
877 para = ME_FindItemFwd(cell, diParagraph);
878 assert(para->member.para.nFlags & MEPF_ROWEND);
879 assert(para->member.para.pFmt->dwMask & PFM_TABLEROWDELIMITER);
880 assert(para->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER);
884 static BOOL ME_ReturnFoundPos(ME_TextEditor *editor, ME_DisplayItem *found,
885 ME_Cursor *result, int rx, BOOL isExact)
888 assert(found->type == diRun);
889 if ((found->member.run.nFlags & MERF_ENDPARA) || rx < 0)
891 result->pRun = found;
892 result->nOffset = ME_CharFromPointCursor(editor, rx, &found->member.run);
893 if (result->nOffset == found->member.run.strText->nLen && rx)
895 result->pRun = ME_FindItemFwd(result->pRun, diRun);
898 result->pPara = ME_GetParagraph(result->pRun);
902 /* Finds the run and offset from the pixel position.
904 * x & y are pixel positions in virtual coordinates into the rich edit control,
905 * so client coordinates must first be adjusted by the scroll position.
907 * returns TRUE if the result was exactly under the cursor, otherwise returns
908 * FALSE, and result is set to the closest position to the coordinates.
910 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
911 ME_Cursor *result, BOOL *is_eol)
913 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
914 ME_DisplayItem *last = NULL;
918 x -= editor->rcFormat.left;
919 y -= editor->rcFormat.top;
925 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
927 assert(p->type == diParagraph);
928 if (y < p->member.para.pt.y + p->member.para.nHeight)
930 if (p->member.para.nFlags & MEPF_ROWSTART)
931 p = ME_FindPixelPosInTableRow(x, y, p);
932 y -= p->member.para.pt.y;
933 p = ME_FindItemFwd(p, diStartRow);
935 } else if (p->member.para.nFlags & MEPF_ROWSTART) {
936 p = ME_GetTableRowEnd(p);
940 for (; p != editor->pBuffer->pLast; )
943 assert(p->type == diStartRow);
944 if (y < p->member.row.pt.y + p->member.row.nHeight)
946 p = ME_FindItemFwd(p, diRun);
949 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
950 if (pp->type != diStartRow)
952 p = ME_FindItemFwd(p, diRun);
957 if (p == editor->pBuffer->pLast)
959 /* The position is below the last paragraph, so the last row will be used
960 * rather than the end of the text, so the x position will be used to
961 * determine the offset closest to the pixel position. */
963 p = ME_FindItemBack(p, diStartRow);
965 p = ME_FindItemFwd(p, diRun);
969 p = editor->pBuffer->pLast;
972 for (; p != editor->pBuffer->pLast; p = p->next)
977 rx = x - p->member.run.pt.x;
978 if (rx < p->member.run.nWidth)
979 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
983 p = ME_FindItemFwd(p, diRun);
984 if (is_eol) *is_eol = 1;
985 rx = 0; /* FIXME not sure */
986 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
991 rx = 0; /* FIXME not sure */
993 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
998 result->pRun = ME_FindItemBack(p, diRun);
999 result->pPara = ME_GetParagraph(result->pRun);
1000 result->nOffset = 0;
1001 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
1006 /* Sets the cursor to the position closest to the pixel position
1008 * x & y are pixel positions in client coordinates.
1010 * isExact will be set to TRUE if the run is directly under the pixel
1011 * position, FALSE if it not, unless isExact is set to NULL.
1013 * return FALSE if outside client area and the cursor is not set,
1014 * otherwise TRUE is returned.
1016 BOOL ME_CharFromPos(ME_TextEditor *editor, int x, int y,
1017 ME_Cursor *cursor, BOOL *isExact)
1022 ITextHost_TxGetClientRect(editor->texthost, &rc);
1023 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
1024 if (isExact) *isExact = FALSE;
1027 x += editor->horz_si.nPos;
1028 y += editor->vert_si.nPos;
1029 bResult = ME_FindPixelPos(editor, x, y, cursor, NULL);
1030 if (isExact) *isExact = bResult;
1036 /* Extends the selection with a word, line, or paragraph selection type.
1038 * The selection is anchored by editor->pCursors[2-3] such that the text
1039 * between the anchors will remain selected, and one end will be extended.
1041 * editor->pCursors[0] should have the position to extend the selection to
1042 * before this function is called.
1044 * Nothing will be done if editor->nSelectionType equals stPosition.
1046 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1048 ME_Cursor tmp_cursor;
1049 int curOfs, anchorStartOfs, anchorEndOfs;
1050 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1052 curOfs = ME_GetCursorOfs(&editor->pCursors[0]);
1053 anchorStartOfs = ME_GetCursorOfs(&editor->pCursors[3]);
1054 anchorEndOfs = ME_GetCursorOfs(&editor->pCursors[2]);
1056 tmp_cursor = editor->pCursors[0];
1057 editor->pCursors[0] = editor->pCursors[2];
1058 editor->pCursors[1] = editor->pCursors[3];
1059 if (curOfs < anchorStartOfs)
1061 /* Extend the left side of selection */
1062 editor->pCursors[1] = tmp_cursor;
1063 if (editor->nSelectionType == stWord)
1064 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1067 ME_DisplayItem *pItem;
1068 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1069 diStartRowOrParagraph:diParagraph);
1070 pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
1071 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
1072 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
1073 editor->pCursors[1].nOffset = 0;
1076 else if (curOfs >= anchorEndOfs)
1078 /* Extend the right side of selection */
1079 editor->pCursors[0] = tmp_cursor;
1080 if (editor->nSelectionType == stWord)
1081 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
1084 ME_DisplayItem *pItem;
1085 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1086 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
1087 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
1088 if (pItem->type == diTextEnd)
1089 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
1091 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
1092 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
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 *pOldPara = pCursor->pPara;
1265 ME_DisplayItem *pItem, *pNewPara;
1266 int x = ME_GetXForArrow(editor, pCursor);
1268 if (editor->bCaretAtEnd && !pCursor->nOffset)
1269 if (!ME_PrevRun(&pOldPara, &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 pNewPara = ME_GetParagraph(pItem);
1309 if (pOldPara->member.para.nFlags & MEPF_ROWSTART ||
1310 (pOldPara->member.para.pCell &&
1311 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1313 /* Brought out of a cell */
1314 pNewPara = ME_GetTableRowEnd(pOldPara)->member.para.next_para;
1315 if (pNewPara->type == diTextEnd)
1316 return; /* At the bottom, so don't go anywhere. */
1317 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1319 if (pNewPara->member.para.nFlags & MEPF_ROWSTART)
1321 /* Brought into a table row */
1322 ME_DisplayItem *cell = ME_FindItemFwd(pNewPara, diCell);
1323 while (cell->member.cell.next_cell &&
1324 x >= cell->member.cell.next_cell->member.cell.pt.x)
1325 cell = cell->member.cell.next_cell;
1326 pItem = ME_FindItemFwd(cell, diStartRow);
1331 /* row not found - ignore */
1334 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1335 pCursor->pPara = ME_GetParagraph(pCursor->pRun);
1336 assert(pCursor->pRun);
1337 assert(pCursor->pRun->type == diRun);
1340 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1342 ME_DisplayItem *p = ME_FindItemFwd(editor->pBuffer->pFirst, diStartRow);
1344 if (editor->vert_si.nPos < p->member.row.nHeight)
1346 ME_SetCursorToStart(editor, pCursor);
1347 editor->bCaretAtEnd = FALSE;
1348 /* Native clears seems to clear this x value on page up at the top
1349 * of the text, but not on page down at the end of the text.
1350 * Doesn't make sense, but we try to be bug for bug compatible. */
1351 editor->nUDArrowX = -1;
1353 ME_DisplayItem *pRun = pCursor->pRun;
1354 ME_DisplayItem *pLast;
1355 int x, y, ys, yd, yp, yprev;
1356 int yOldScrollPos = editor->vert_si.nPos;
1358 x = ME_GetXForArrow(editor, pCursor);
1359 if (!pCursor->nOffset && editor->bCaretAtEnd)
1360 pRun = ME_FindItemBack(pRun, diRun);
1362 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1363 assert(p->type == diStartRow);
1364 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1365 yprev = ys = y = yp + p->member.row.pt.y;
1367 ME_ScrollUp(editor, editor->sizeWindow.cy);
1368 /* Only move the cursor by the amount scrolled. */
1369 yd = y + editor->vert_si.nPos - yOldScrollPos;
1373 p = ME_FindItemBack(p, diStartRowOrParagraph);
1376 if (p->type == diParagraph) { /* crossing paragraphs */
1377 if (p->member.para.prev_para == NULL)
1379 yp = p->member.para.prev_para->member.para.pt.y;
1382 y = yp + p->member.row.pt.y;
1389 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset,
1390 &editor->bCaretAtEnd);
1391 pCursor->pPara = ME_GetParagraph(pCursor->pRun);
1393 assert(pCursor->pRun);
1394 assert(pCursor->pRun->type == diRun);
1397 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1399 ME_DisplayItem *pLast;
1402 /* Find y position of the last row */
1403 pLast = editor->pBuffer->pLast;
1404 y = pLast->member.para.prev_para->member.para.pt.y
1405 + ME_FindItemBack(pLast, diStartRow)->member.row.pt.y;
1407 x = ME_GetXForArrow(editor, pCursor);
1409 if (editor->vert_si.nPos >= y - editor->sizeWindow.cy)
1411 ME_SetCursorToEnd(editor, pCursor);
1412 editor->bCaretAtEnd = FALSE;
1414 ME_DisplayItem *pRun = pCursor->pRun;
1416 int ys, yd, yp, yprev;
1417 int yOldScrollPos = editor->vert_si.nPos;
1419 if (!pCursor->nOffset && editor->bCaretAtEnd)
1420 pRun = ME_FindItemBack(pRun, diRun);
1422 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1423 assert(p->type == diStartRow);
1424 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1425 yprev = ys = y = yp + p->member.row.pt.y;
1427 /* For native richedit controls:
1428 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1429 * v4.1 can scroll past this position here. */
1430 ME_ScrollDown(editor, editor->sizeWindow.cy);
1431 /* Only move the cursor by the amount scrolled. */
1432 yd = y + editor->vert_si.nPos - yOldScrollPos;
1436 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1439 if (p->type == diParagraph) {
1440 yp = p->member.para.pt.y;
1443 y = yp + p->member.row.pt.y;
1450 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset,
1451 &editor->bCaretAtEnd);
1452 pCursor->pPara = ME_GetParagraph(pCursor->pRun);
1454 assert(pCursor->pRun);
1455 assert(pCursor->pRun->type == diRun);
1458 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1460 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1462 ME_DisplayItem *pRun;
1463 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1464 pRow = ME_FindItemBack(pRow, diStartRow);
1468 pRun = ME_FindItemFwd(pRow, diRun);
1470 pCursor->pRun = pRun;
1471 assert(pCursor->pPara == ME_GetParagraph(pRun));
1472 pCursor->nOffset = 0;
1475 editor->bCaretAtEnd = FALSE;
1478 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1480 ME_SetCursorToStart(editor, pCursor);
1481 editor->bCaretAtEnd = FALSE;
1484 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1486 ME_DisplayItem *pRow;
1488 if (editor->bCaretAtEnd && !pCursor->nOffset)
1491 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1493 if (pRow->type == diStartRow) {
1494 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1496 pCursor->pRun = pRun;
1497 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1498 pCursor->nOffset = 0;
1499 editor->bCaretAtEnd = TRUE;
1502 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1503 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1504 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1505 pCursor->nOffset = 0;
1506 editor->bCaretAtEnd = FALSE;
1509 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1511 ME_SetCursorToEnd(editor, pCursor);
1512 editor->bCaretAtEnd = FALSE;
1515 BOOL ME_IsSelection(ME_TextEditor *editor)
1517 return editor->pCursors[0].pRun != editor->pCursors[1].pRun ||
1518 editor->pCursors[0].nOffset != editor->pCursors[1].nOffset;
1521 void ME_DeleteSelection(ME_TextEditor *editor)
1524 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
1525 ME_DeleteTextAtCursor(editor, nStartCursor, to - from);
1528 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1530 return ME_GetInsertStyle(editor, 0);
1533 void ME_SendSelChange(ME_TextEditor *editor)
1537 if (!(editor->nEventMask & ENM_SELCHANGE))
1540 sc.nmhdr.hwndFrom = NULL;
1541 sc.nmhdr.idFrom = 0;
1542 sc.nmhdr.code = EN_SELCHANGE;
1543 ME_GetSelectionOfs(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1544 sc.seltyp = SEL_EMPTY;
1545 if (sc.chrg.cpMin != sc.chrg.cpMax)
1546 sc.seltyp |= SEL_TEXT;
1547 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* what were RICHEDIT authors thinking ? */
1548 sc.seltyp |= SEL_MULTICHAR;
1549 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1550 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1551 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1552 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1553 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1555 ME_ClearTempStyle(editor);
1557 editor->notified_cr = sc.chrg;
1558 ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
1563 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1566 ME_Cursor *p = &editor->pCursors[nCursor];
1567 ME_Cursor tmp_curs = *p;
1568 BOOL success = FALSE;
1570 ME_CheckCharOffsets(editor);
1573 editor->bCaretAtEnd = 0;
1575 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1577 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1580 editor->bCaretAtEnd = 0;
1582 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1584 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1587 ME_MoveCursorLines(editor, &tmp_curs, -1);
1590 ME_MoveCursorLines(editor, &tmp_curs, +1);
1593 ME_ArrowPageUp(editor, &tmp_curs);
1596 ME_ArrowPageDown(editor, &tmp_curs);
1600 ME_ArrowCtrlHome(editor, &tmp_curs);
1602 ME_ArrowHome(editor, &tmp_curs);
1603 editor->bCaretAtEnd = 0;
1608 ME_ArrowCtrlEnd(editor, &tmp_curs);
1610 ME_ArrowEnd(editor, &tmp_curs);
1615 editor->pCursors[1] = tmp_curs;
1618 ME_InvalidateSelection(editor);
1620 ITextHost_TxShowCaret(editor->texthost, FALSE);
1621 ME_EnsureVisible(editor, &tmp_curs);
1622 ME_ShowCaret(editor);
1623 ME_SendSelChange(editor);