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 ((GetWindowLongW(editor->hWnd, GWL_STYLE) & 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 = GetDC(editor->hWnd);
178 ME_DisplayItem *run = pCursorRun;
179 ME_DisplayItem *para = NULL;
182 ME_InitContext(&c, editor, hDC);
184 if (!pCursor->nOffset && !editor->bCaretAtEnd)
186 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrStartRow);
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 = run->member.run.pt.x + sz.cx;
218 *y = para->member.para.nYPos + row->member.row.nBaseline + pSizeRun->member.run.pt.y - pSizeRun->member.run.nAscent - ME_GetYScrollPos(editor);
220 ME_DestroyContext(&c, editor->hWnd);
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)
240 CreateCaret(editor->hWnd, NULL, 0, height);
246 void ME_ShowCaret(ME_TextEditor *ed)
253 void ME_HideCaret(ME_TextEditor *ed)
262 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
271 ME_CursorFromCharOfs(editor, nOfs, &c);
272 run = &c.pRun->member.run;
273 if (run->nFlags & MERF_ENDPARA) {
274 int eollen = run->nCR + run->nLF;
276 if (!ME_FindItemFwd(c.pRun, diParagraph))
280 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
281 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
282 ME_CheckCharOffsets(editor);
283 nChars -= (eollen < nChars) ? eollen : nChars;
289 int nIntendedChars = nChars;
290 int nCharsToDelete = nChars;
294 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
297 ME_StrRelPos(run->strText, loc, &nChars);
298 /* nChars is the number of characters that should be deleted from the
299 FOLLOWING runs (these AFTER cursor.pRun)
300 nCharsToDelete is a number of chars to delete from THIS run */
301 nCharsToDelete -= nChars;
302 shift -= nCharsToDelete;
303 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
304 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
305 debugstr_w(run->strText->szData), run->strText->nLen);
307 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
309 /* undo = reinsert whole run */
310 /* nOfs is a character offset (from the start of the document
311 to the current (deleted) run */
312 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
314 pUndo->di.member.run.nCharOfs = nOfs;
318 /* undo = reinsert partial run */
319 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
321 ME_DestroyString(pUndo->di.member.run.strText);
322 pUndo->di.member.run.nCharOfs = nOfs;
323 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
326 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
327 TRACE("Shift value: %d\n", shift);
328 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
330 /* update cursors (including c) */
331 for (i=-1; i<editor->nCursors; i++) {
332 ME_Cursor *pThisCur = editor->pCursors + i;
333 if (i == -1) pThisCur = &c;
334 if (pThisCur->pRun == cursor.pRun) {
335 if (pThisCur->nOffset > cursor.nOffset) {
336 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
337 pThisCur->nOffset = cursor.nOffset;
339 pThisCur->nOffset -= nCharsToDelete;
340 assert(pThisCur->nOffset >= 0);
341 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
343 if (pThisCur->nOffset == ME_StrVLen(run->strText))
345 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
346 assert(pThisCur->pRun->type == diRun);
347 pThisCur->nOffset = 0;
352 /* c = updated data now */
354 if (c.pRun == cursor.pRun)
355 ME_SkipAndPropagateCharOffset(c.pRun, shift);
357 ME_PropagateCharOffset(c.pRun, shift);
359 if (!ME_StrVLen(cursor.pRun->member.run.strText))
361 TRACE("Removing useless run\n");
362 ME_Remove(cursor.pRun);
363 ME_DestroyDisplayItem(cursor.pRun);
368 ME_CheckCharOffsets(editor);
375 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
378 assert(nCursor>=0 && nCursor<editor->nCursors);
379 /* text operations set modified state */
380 editor->nModifyStep = 1;
381 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
384 static ME_DisplayItem *
385 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
386 const WCHAR *str, int len, ME_Style *style,
389 ME_Cursor *p = &editor->pCursors[nCursor];
391 editor->bCaretAtEnd = FALSE;
393 assert(p->pRun->type == diRun);
395 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
399 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
401 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
406 if (ME_IsSelection(editor))
407 ME_DeleteSelection(editor);
409 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
411 di->member.run.ole_obj = ALLOC_OBJ(*reo);
412 ME_CopyReObject(di->member.run.ole_obj, reo);
413 ME_SendSelChange(editor);
417 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
419 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
424 if (ME_IsSelection(editor))
425 ME_DeleteSelection(editor);
427 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
429 ME_SendSelChange(editor);
433 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
436 ME_DisplayItem *p, *run;
437 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
439 p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
442 while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
444 if (run->member.run.nFlags & MERF_CELL)
446 assert(run->member.run.pCell->next);
447 p->member.run.pCell = run->member.run.pCell->next;
451 assert(run->type == diParagraph);
452 assert(run->member.para.bTable);
453 assert(run->member.para.pCells);
454 p->member.run.pCell = run->member.para.pCells;
458 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
459 const WCHAR *str, int len, ME_Style *style)
465 /* FIXME really HERE ? */
466 if (ME_IsSelection(editor))
467 ME_DeleteSelection(editor);
469 /* FIXME: is this too slow? */
470 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
471 oldLen = ME_GetTextLength(editor);
473 /* text operations set modified state */
474 editor->nModifyStep = 1;
478 assert(nCursor>=0 && nCursor<editor->nCursors);
482 /* grow the text limit to fit our text */
483 if(editor->nTextLimit < oldLen +len)
484 editor->nTextLimit = oldLen + len;
489 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
490 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
492 if (pos-str < len && *pos == '\t') { /* handle tabs */
496 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
498 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
507 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
508 if (!editor->bEmulateVersion10 && pos-str < len-2 && pos[0] == '\r' && pos[1] == '\r' && pos[2] == '\n') {
512 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
514 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
523 if (pos-str < len) { /* handle EOLs */
524 ME_DisplayItem *tp, *end_run;
529 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
530 p = &editor->pCursors[nCursor];
532 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
533 p = &editor->pCursors[nCursor];
535 tmp_style = ME_GetInsertStyle(editor, nCursor);
536 /* ME_SplitParagraph increases style refcount */
538 /* Encode and fill number of CR and LF according to emulation mode */
539 if (editor->bEmulateVersion10) {
542 /* We have to find out how many consecutive \r are there, and if there
543 is a \n terminating the run of \r's. */
544 numCR = 0; numLF = 0;
546 while (tpos-str < len && *tpos == '\r') {
550 if (tpos-str >= len) {
551 /* Reached end of text without finding anything but '\r' */
555 numCR = 1; numLF = 0;
556 } else if (*tpos == '\n') {
557 /* The entire run of \r's plus the one \n is one single line break */
561 /* Found some other content past the run of \r's */
563 numCR = 1; numLF = 0;
566 if(pos-str < len && *pos =='\r')
568 if(pos-str < len && *pos =='\n')
570 numCR = 1; numLF = 0;
572 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, numCR, numLF);
573 p->pRun = ME_FindItemFwd(tp, diRun);
574 end_run = ME_FindItemBack(tp, diRun);
575 ME_ReleaseStyle(end_run->member.run.style);
576 end_run->member.run.style = tmp_style;
585 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
592 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
594 ME_DisplayItem *pRun = pCursor->pRun;
598 if (!pCursor->nOffset)
601 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
608 if (pRun->member.para.prev_para->type == diTextStart)
610 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
611 /* every paragraph ought to have at least one run */
612 assert(pRun && pRun->type == diRun);
613 assert(pRun->member.run.nFlags & MERF_ENDPARA);
616 assert(pRun->type != diRun && pRun->type != diParagraph);
619 } while (RUN_IS_HIDDEN(&pRun->member.run));
620 pCursor->pRun = pRun;
621 if (pRun->member.run.nFlags & MERF_ENDPARA)
622 pCursor->nOffset = 0;
624 pCursor->nOffset = pRun->member.run.strText->nLen;
627 if (pCursor->nOffset)
628 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
633 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
635 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
637 if (new_ofs < pRun->member.run.strText->nLen)
639 pCursor->nOffset = new_ofs;
644 pRun = ME_FindItemFwd(pRun, diRun);
645 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
648 pCursor->pRun = pRun;
649 pCursor->nOffset = 0;
658 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
660 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
661 int nOffset = cursor->nOffset;
665 /* Backward movement */
668 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
669 nOffset, WB_MOVEWORDLEFT);
672 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
673 if (pOtherRun->type == diRun)
675 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
676 pOtherRun->member.run.strText->nLen - 1,
678 && !(pRun->member.run.nFlags & MERF_ENDPARA)
679 && !(cursor->pRun == pRun && cursor->nOffset == 0)
680 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
684 nOffset = pOtherRun->member.run.strText->nLen;
686 else if (pOtherRun->type == diParagraph)
688 if (cursor->pRun == pRun && cursor->nOffset == 0)
690 /* Paragraph breaks are treated as separate words */
691 if (pOtherRun->member.para.prev_para->type == diTextStart)
693 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
701 /* Forward movement */
702 BOOL last_delim = FALSE;
706 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
707 nOffset, WB_ISDELIMITER))
709 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
710 nOffset, WB_MOVEWORDRIGHT);
711 if (nOffset < pRun->member.run.strText->nLen)
713 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
714 if (pOtherRun->type == diRun)
716 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
717 nOffset - 1, WB_ISDELIMITER);
721 else if (pOtherRun->type == diParagraph)
723 if (cursor->pRun == pRun)
724 pRun = ME_FindItemFwd(pOtherRun, diRun);
730 if (cursor->pRun == pRun)
738 cursor->nOffset = nOffset;
744 ME_SelectWord(ME_TextEditor *editor)
746 if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
747 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
748 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
749 ME_InvalidateSelection(editor);
750 ME_SendSelChange(editor);
754 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
756 ME_Cursor *pCursor = &editor->pCursors[nCursor];
757 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
758 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
761 static void ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
763 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
764 ME_DisplayItem *last = NULL;
771 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
773 assert(p->type == diParagraph);
774 if (y < p->member.para.nYPos + p->member.para.nHeight)
776 y -= p->member.para.nYPos;
777 p = ME_FindItemFwd(p, diStartRow);
782 for (; p != editor->pBuffer->pLast; )
785 assert(p->type == diStartRow);
786 if (y < p->member.row.nYPos + p->member.row.nHeight)
788 p = ME_FindItemFwd(p, diRun);
791 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
792 if (pp->type != diStartRow)
794 p = ME_FindItemFwd(p, diRun);
799 for (; p != editor->pBuffer->pLast; p = p->next)
804 rx = x - p->member.run.pt.x;
805 if (rx < p->member.run.nWidth)
808 assert(p->type == diRun);
809 if ((p->member.run.nFlags & MERF_ENDPARA) || rx < 0)
812 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
813 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
815 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
822 p = ME_FindItemFwd(p, diRun);
823 if (is_eol) *is_eol = 1;
824 rx = 0; /* FIXME not sure */
828 rx = 0; /* FIXME not sure */
835 result->pRun = ME_FindItemBack(p, diRun);
837 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
842 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
847 GetClientRect(editor->hWnd, &rc);
848 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
850 y += ME_GetYScrollPos(editor);
851 ME_FindPixelPos(editor, x, y, &cursor, NULL);
852 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
853 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
857 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
859 ME_Cursor tmp_cursor;
860 int is_selection = 0;
862 editor->nUDArrowX = -1;
864 y += ME_GetYScrollPos(editor);
866 tmp_cursor = editor->pCursors[0];
867 is_selection = ME_IsSelection(editor);
869 if (x >= editor->selofs)
871 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
872 if (GetKeyState(VK_SHIFT)>=0)
874 editor->pCursors[1] = editor->pCursors[0];
876 else if (!is_selection) {
877 editor->pCursors[1] = tmp_cursor;
881 ME_InvalidateSelection(editor);
882 HideCaret(editor->hWnd);
883 ME_MoveCaret(editor);
884 ShowCaret(editor->hWnd);
885 ME_ClearTempStyle(editor);
886 ME_SendSelChange(editor);
890 ME_DisplayItem *pRow;
894 /* Set pCursors[0] to beginning of line */
895 ME_FindPixelPos(editor, x, y, &editor->pCursors[1], &editor->bCaretAtEnd);
896 /* Set pCursors[1] to end of line */
897 pRow = ME_FindItemFwd(editor->pCursors[1].pRun, diStartRowOrParagraphOrEnd);
899 /* pCursor[0] is the position where the cursor will be drawn,
900 * pCursor[1] is the other end of the selection range
901 * pCursor[2] and [3] are backups of [0] and [1] so I
902 * don't have to look them up again
905 if (pRow->type == diStartRow) {
906 /* FIXME WTF was I thinking about here ? */
907 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
909 editor->pCursors[0].pRun = pRun;
910 editor->pCursors[0].nOffset = 0;
911 editor->bCaretAtEnd = 1;
913 editor->pCursors[0].pRun = ME_FindItemBack(pRow, diRun);
914 assert(editor->pCursors[0].pRun && editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA);
915 editor->pCursors[0].nOffset = 0;
916 editor->bCaretAtEnd = 0;
918 editor->pCursors[2] = editor->pCursors[0];
919 editor->pCursors[3] = editor->pCursors[1];
920 ME_InvalidateSelection(editor);
921 HideCaret(editor->hWnd);
922 ME_MoveCaret(editor);
923 ShowCaret(editor->hWnd);
924 ME_ClearTempStyle(editor);
925 ME_SendSelChange(editor);
929 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
931 ME_Cursor tmp_cursor;
933 y += ME_GetYScrollPos(editor);
935 tmp_cursor = editor->pCursors[0];
936 /* FIXME: do something with the return value of ME_FindPixelPos */
937 if (!editor->linesel)
938 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
939 else ME_FindPixelPos(editor, (y > editor->sely) * editor->rcFormat.right, y, &tmp_cursor, &editor->bCaretAtEnd);
941 if (!memcmp(&tmp_cursor, editor->pCursors, sizeof(tmp_cursor)))
944 ME_InvalidateSelection(editor);
945 if (!editor->linesel)
946 editor->pCursors[0] = tmp_cursor;
947 else if (!memcmp(&tmp_cursor, editor->pCursors+2, sizeof(tmp_cursor)) ||
948 !memcmp(&tmp_cursor, editor->pCursors+3, sizeof(tmp_cursor)))
950 editor->pCursors[0] = editor->pCursors[2];
951 editor->pCursors[1] = editor->pCursors[3];
953 else if (y < editor->sely)
955 editor->pCursors[0] = tmp_cursor;
956 editor->pCursors[1] = editor->pCursors[2];
960 editor->pCursors[0] = tmp_cursor;
961 editor->pCursors[1] = editor->pCursors[3];
964 HideCaret(editor->hWnd);
965 ME_MoveCaret(editor);
966 ME_InvalidateSelection(editor);
967 ShowCaret(editor->hWnd);
968 ME_SendSelChange(editor);
969 SendMessageW(editor->hWnd, EM_SCROLLCARET, 0, 0);
972 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
973 int x, int *pOffset, int *pbCaretAtEnd)
975 ME_DisplayItem *pNext, *pLastRun;
976 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
977 assert(pNext->type == diRun);
979 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
980 if (pOffset) *pOffset = 0;
982 int run_x = pNext->member.run.pt.x;
983 int width = pNext->member.run.nWidth;
988 if (x >= run_x && x < run_x+width)
990 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
991 ME_String *s = pNext->member.run.strText;
999 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
1000 } while(pNext && pNext->type == diRun);
1002 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
1004 pNext = ME_FindItemFwd(pNext, diRun);
1005 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
1012 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1014 ME_DisplayItem *pRun = pCursor->pRun;
1017 if (editor->nUDArrowX != -1)
1018 x = editor->nUDArrowX;
1020 if (editor->bCaretAtEnd)
1022 pRun = ME_FindItemBack(pRun, diRun);
1024 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1027 x = pRun->member.run.pt.x;
1028 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
1030 editor->nUDArrowX = x;
1037 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1039 ME_DisplayItem *pRun = pCursor->pRun;
1040 ME_DisplayItem *pItem;
1041 int x = ME_GetXForArrow(editor, pCursor);
1043 if (editor->bCaretAtEnd && !pCursor->nOffset)
1044 pRun = ME_FindItemBack(pRun, diRun);
1049 /* start of this row */
1050 pItem = ME_FindItemBack(pRun, diStartRow);
1052 /* start of the previous row */
1053 pItem = ME_FindItemBack(pItem, diStartRow);
1057 /* start of the next row */
1058 pItem = ME_FindItemFwd(pRun, diStartRow);
1059 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1064 /* row not found - ignore */
1067 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1068 assert(pCursor->pRun);
1069 assert(pCursor->pRun->type == diRun);
1073 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1075 ME_DisplayItem *pRun = pCursor->pRun;
1076 ME_DisplayItem *pLast, *p;
1077 int x, y, ys, yd, yp, yprev;
1078 ME_Cursor tmp_curs = *pCursor;
1080 x = ME_GetXForArrow(editor, pCursor);
1081 if (!pCursor->nOffset && editor->bCaretAtEnd)
1082 pRun = ME_FindItemBack(pRun, diRun);
1084 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1085 assert(p->type == diStartRow);
1086 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1087 yprev = ys = y = yp + p->member.row.nYPos;
1088 yd = y - editor->sizeWindow.cy;
1092 p = ME_FindItemBack(p, diStartRowOrParagraph);
1095 if (p->type == diParagraph) { /* crossing paragraphs */
1096 if (p->member.para.prev_para == NULL)
1098 yp = p->member.para.prev_para->member.para.nYPos;
1101 y = yp + p->member.row.nYPos;
1108 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1109 ME_UpdateSelection(editor, &tmp_curs);
1110 if (yprev < editor->sizeWindow.cy)
1112 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
1117 ME_ScrollUp(editor, ys-yprev);
1119 assert(pCursor->pRun);
1120 assert(pCursor->pRun->type == diRun);
1123 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1124 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1125 In such a situation, clicking the scrollbar restores its position back to the
1126 normal range (ie. sets it to (doclength-screenheight)). */
1128 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1130 ME_DisplayItem *pRun = pCursor->pRun;
1131 ME_DisplayItem *pLast, *p;
1132 int x, y, ys, yd, yp, yprev;
1133 ME_Cursor tmp_curs = *pCursor;
1135 x = ME_GetXForArrow(editor, pCursor);
1136 if (!pCursor->nOffset && editor->bCaretAtEnd)
1137 pRun = ME_FindItemBack(pRun, diRun);
1139 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1140 assert(p->type == diStartRow);
1141 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1142 yprev = ys = y = yp + p->member.row.nYPos;
1143 yd = y + editor->sizeWindow.cy;
1147 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1150 if (p->type == diParagraph) {
1151 yp = p->member.para.nYPos;
1154 y = yp + p->member.row.nYPos;
1161 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1162 ME_UpdateSelection(editor, &tmp_curs);
1163 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
1165 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1170 ME_ScrollUp(editor,ys-yprev);
1172 assert(pCursor->pRun);
1173 assert(pCursor->pRun->type == diRun);
1176 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1178 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1179 ME_WrapMarkedParagraphs(editor);
1181 ME_DisplayItem *pRun;
1182 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1183 pRow = ME_FindItemBack(pRow, diStartRow);
1187 pRun = ME_FindItemFwd(pRow, diRun);
1189 pCursor->pRun = pRun;
1190 pCursor->nOffset = 0;
1193 editor->bCaretAtEnd = FALSE;
1196 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1198 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1200 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1202 pCursor->pRun = pRun;
1203 pCursor->nOffset = 0;
1208 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1210 ME_DisplayItem *pRow;
1212 if (editor->bCaretAtEnd && !pCursor->nOffset)
1215 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1217 if (pRow->type == diStartRow) {
1218 /* FIXME WTF was I thinking about here ? */
1219 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1221 pCursor->pRun = pRun;
1222 pCursor->nOffset = 0;
1223 editor->bCaretAtEnd = 1;
1226 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1227 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1228 pCursor->nOffset = 0;
1229 editor->bCaretAtEnd = FALSE;
1232 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1234 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1236 p = ME_FindItemBack(p, diRun);
1238 assert(p->member.run.nFlags & MERF_ENDPARA);
1240 pCursor->nOffset = 0;
1241 editor->bCaretAtEnd = FALSE;
1244 BOOL ME_IsSelection(ME_TextEditor *editor)
1246 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1249 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1251 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1259 BOOL ME_UpdateSelection(ME_TextEditor *editor, const ME_Cursor *pTempCursor)
1261 ME_Cursor old_anchor = editor->pCursors[1];
1263 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1265 /* any selection was present ? if so, it's no more, repaint ! */
1266 editor->pCursors[1] = editor->pCursors[0];
1267 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1274 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1276 editor->pCursors[1] = *pTempCursor;
1285 void ME_DeleteSelection(ME_TextEditor *editor)
1288 ME_GetSelection(editor, &from, &to);
1289 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1292 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1294 return ME_GetInsertStyle(editor, 0);
1297 void ME_SendSelChange(ME_TextEditor *editor)
1301 if (!(editor->nEventMask & ENM_SELCHANGE))
1304 sc.nmhdr.hwndFrom = editor->hWnd;
1305 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1306 sc.nmhdr.code = EN_SELCHANGE;
1307 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1308 sc.seltyp = SEL_EMPTY;
1309 if (sc.chrg.cpMin != sc.chrg.cpMax)
1310 sc.seltyp |= SEL_TEXT;
1311 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1312 sc.seltyp |= SEL_MULTICHAR;
1313 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1314 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1315 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1316 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1317 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1319 ME_ClearTempStyle(editor);
1321 editor->notified_cr = sc.chrg;
1322 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1327 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1330 ME_Cursor *p = &editor->pCursors[nCursor];
1331 ME_Cursor tmp_curs = *p;
1332 BOOL success = FALSE;
1334 ME_CheckCharOffsets(editor);
1335 editor->nUDArrowX = -1;
1338 editor->bCaretAtEnd = 0;
1340 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1342 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1345 editor->bCaretAtEnd = 0;
1347 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1349 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1352 ME_MoveCursorLines(editor, &tmp_curs, -1);
1355 ME_MoveCursorLines(editor, &tmp_curs, +1);
1358 ME_ArrowPageUp(editor, &tmp_curs);
1361 ME_ArrowPageDown(editor, &tmp_curs);
1365 ME_ArrowCtrlHome(editor, &tmp_curs);
1367 ME_ArrowHome(editor, &tmp_curs);
1368 editor->bCaretAtEnd = 0;
1373 ME_ArrowCtrlEnd(editor, &tmp_curs);
1375 ME_ArrowEnd(editor, &tmp_curs);
1380 editor->pCursors[1] = tmp_curs;
1383 ME_InvalidateSelection(editor);
1385 HideCaret(editor->hWnd);
1386 ME_EnsureVisible(editor, tmp_curs.pRun);
1387 ME_ShowCaret(editor);
1388 ME_SendSelChange(editor);