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(!pCursor->nOffset || !editor->bCaretAtEnd);
168 assert(height && x && y);
169 assert(!(ME_GetParagraph(pCursorRun)->member.para.nFlags & MEPF_REWRAP));
170 assert(pCursor->pRun);
171 assert(pCursor->pRun->type == diRun);
173 if (pCursorRun->type == diRun) {
174 ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
177 HDC hDC = GetDC(editor->hWnd);
179 ME_DisplayItem *run = pCursorRun;
180 ME_DisplayItem *para = NULL;
183 ME_InitContext(&c, editor, hDC);
185 if (!pCursor->nOffset && !editor->bCaretAtEnd)
187 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrStartRow);
189 if (prev->type == diRun)
192 assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
193 para = ME_FindItemBack(row, diParagraph);
195 assert(para->type == diParagraph);
196 if (editor->bCaretAtEnd && !pCursor->nOffset &&
197 run == ME_FindItemFwd(row, diRun))
199 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
201 if (tmp->type == diRun)
203 row = ME_FindItemBack(tmp, diStartRow);
204 pSizeRun = run = tmp;
206 assert(run->type == diRun);
207 sz = ME_GetRunSize(&c, ¶->member.para,
208 &run->member.run, ME_StrLen(run->member.run.strText),
209 row->member.row.nLMargin);
212 if (pCursor->nOffset && !(run->member.run.nFlags & MERF_SKIPPED)) {
213 sz = ME_GetRunSize(&c, ¶->member.para, &run->member.run, pCursor->nOffset,
214 row->member.row.nLMargin);
217 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
218 *x = run->member.run.pt.x + sz.cx;
219 *y = para->member.para.nYPos + row->member.row.nBaseline + pSizeRun->member.run.pt.y - pSizeRun->member.run.nAscent - ME_GetYScrollPos(editor);
221 ME_DestroyContext(&c, editor->hWnd);
225 *height = 10; /* FIXME use global font */
232 ME_MoveCaret(ME_TextEditor *editor)
236 if (ME_WrapMarkedParagraphs(editor))
237 ME_UpdateScrollBar(editor);
238 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
239 if(editor->bHaveFocus)
241 CreateCaret(editor->hWnd, NULL, 0, height);
247 void ME_ShowCaret(ME_TextEditor *ed)
254 void ME_HideCaret(ME_TextEditor *ed)
263 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
272 ME_CursorFromCharOfs(editor, nOfs, &c);
273 run = &c.pRun->member.run;
274 if (run->nFlags & MERF_ENDPARA) {
275 int eollen = run->nCR + run->nLF;
277 if (!ME_FindItemFwd(c.pRun, diParagraph))
281 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
282 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
283 ME_CheckCharOffsets(editor);
284 nChars -= (eollen < nChars) ? eollen : nChars;
290 int nIntendedChars = nChars;
291 int nCharsToDelete = nChars;
295 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
298 ME_StrRelPos(run->strText, loc, &nChars);
299 /* nChars is the number of characters that should be deleted from the
300 FOLLOWING runs (these AFTER cursor.pRun)
301 nCharsToDelete is a number of chars to delete from THIS run */
302 nCharsToDelete -= nChars;
303 shift -= nCharsToDelete;
304 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
305 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
306 debugstr_w(run->strText->szData), run->strText->nLen);
308 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
310 /* undo = reinsert whole run */
311 /* nOfs is a character offset (from the start of the document
312 to the current (deleted) run */
313 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
315 pUndo->di.member.run.nCharOfs = nOfs;
319 /* undo = reinsert partial run */
320 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
322 ME_DestroyString(pUndo->di.member.run.strText);
323 pUndo->di.member.run.nCharOfs = nOfs;
324 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
327 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
328 TRACE("Shift value: %d\n", shift);
329 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
331 /* update cursors (including c) */
332 for (i=-1; i<editor->nCursors; i++) {
333 ME_Cursor *pThisCur = editor->pCursors + i;
334 if (i == -1) pThisCur = &c;
335 if (pThisCur->pRun == cursor.pRun) {
336 if (pThisCur->nOffset > cursor.nOffset) {
337 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
338 pThisCur->nOffset = cursor.nOffset;
340 pThisCur->nOffset -= nCharsToDelete;
341 assert(pThisCur->nOffset >= 0);
342 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
344 if (pThisCur->nOffset == ME_StrVLen(run->strText))
346 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
347 assert(pThisCur->pRun->type == diRun);
348 pThisCur->nOffset = 0;
353 /* c = updated data now */
355 if (c.pRun == cursor.pRun)
356 ME_SkipAndPropagateCharOffset(c.pRun, shift);
358 ME_PropagateCharOffset(c.pRun, shift);
360 if (!ME_StrVLen(cursor.pRun->member.run.strText))
362 TRACE("Removing useless run\n");
363 ME_Remove(cursor.pRun);
364 ME_DestroyDisplayItem(cursor.pRun);
369 ME_CheckCharOffsets(editor);
376 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
379 assert(nCursor>=0 && nCursor<editor->nCursors);
380 /* text operations set modified state */
381 editor->nModifyStep = 1;
382 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
385 static ME_DisplayItem *
386 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
387 const WCHAR *str, int len, ME_Style *style,
390 ME_Cursor *p = &editor->pCursors[nCursor];
392 editor->bCaretAtEnd = FALSE;
394 assert(p->pRun->type == diRun);
396 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
400 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
402 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
407 if (ME_IsSelection(editor))
408 ME_DeleteSelection(editor);
410 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
412 di->member.run.ole_obj = ALLOC_OBJ(*reo);
413 ME_CopyReObject(di->member.run.ole_obj, reo);
414 ME_SendSelChange(editor);
418 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
420 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
425 if (ME_IsSelection(editor))
426 ME_DeleteSelection(editor);
428 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
430 ME_SendSelChange(editor);
434 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
437 ME_DisplayItem *p, *run;
438 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
440 p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
443 while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
445 if (run->member.run.nFlags & MERF_CELL)
447 assert(run->member.run.pCell->next);
448 p->member.run.pCell = run->member.run.pCell->next;
452 assert(run->type == diParagraph);
453 assert(run->member.para.bTable);
454 assert(run->member.para.pCells);
455 p->member.run.pCell = run->member.para.pCells;
459 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
460 const WCHAR *str, int len, ME_Style *style)
466 /* FIXME really HERE ? */
467 if (ME_IsSelection(editor))
468 ME_DeleteSelection(editor);
470 /* FIXME: is this too slow? */
471 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
472 oldLen = ME_GetTextLength(editor);
474 /* text operations set modified state */
475 editor->nModifyStep = 1;
479 assert(nCursor>=0 && nCursor<editor->nCursors);
483 /* grow the text limit to fit our text */
484 if(editor->nTextLimit < oldLen +len)
485 editor->nTextLimit = oldLen + len;
490 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
491 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
493 if (pos-str < len && *pos == '\t') { /* handle tabs */
497 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
499 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
508 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
509 if (!editor->bEmulateVersion10 && pos-str < len-2 && pos[0] == '\r' && pos[1] == '\r' && pos[2] == '\n') {
513 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
515 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
524 if (pos-str < len) { /* handle EOLs */
525 ME_DisplayItem *tp, *end_run;
530 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
531 p = &editor->pCursors[nCursor];
533 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
534 p = &editor->pCursors[nCursor];
536 tmp_style = ME_GetInsertStyle(editor, nCursor);
537 /* ME_SplitParagraph increases style refcount */
539 /* Encode and fill number of CR and LF according to emulation mode */
540 if (editor->bEmulateVersion10) {
543 /* We have to find out how many consecutive \r are there, and if there
544 is a \n terminating the run of \r's. */
545 numCR = 0; numLF = 0;
547 while (tpos-str < len && *tpos == '\r') {
551 if (tpos-str >= len) {
552 /* Reached end of text without finding anything but '\r' */
556 numCR = 1; numLF = 0;
557 } else if (*tpos == '\n') {
558 /* The entire run of \r's plus the one \n is one single line break */
562 /* Found some other content past the run of \r's */
564 numCR = 1; numLF = 0;
567 if(pos-str < len && *pos =='\r')
569 if(pos-str < len && *pos =='\n')
571 numCR = 1; numLF = 0;
573 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, numCR, numLF);
574 p->pRun = ME_FindItemFwd(tp, diRun);
575 end_run = ME_FindItemBack(tp, diRun);
576 ME_ReleaseStyle(end_run->member.run.style);
577 end_run->member.run.style = tmp_style;
586 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
593 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
595 ME_DisplayItem *pRun = pCursor->pRun;
599 if (!pCursor->nOffset)
602 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
609 if (pRun->member.para.prev_para->type == diTextStart)
611 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
612 /* every paragraph ought to have at least one run */
613 assert(pRun && pRun->type == diRun);
614 assert(pRun->member.run.nFlags & MERF_ENDPARA);
617 assert(pRun->type != diRun && pRun->type != diParagraph);
620 } while (RUN_IS_HIDDEN(&pRun->member.run));
621 pCursor->pRun = pRun;
622 if (pRun->member.run.nFlags & MERF_ENDPARA)
623 pCursor->nOffset = 0;
625 pCursor->nOffset = pRun->member.run.strText->nLen;
628 if (pCursor->nOffset)
629 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
634 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
636 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
638 if (new_ofs < pRun->member.run.strText->nLen)
640 pCursor->nOffset = new_ofs;
645 pRun = ME_FindItemFwd(pRun, diRun);
646 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
649 pCursor->pRun = pRun;
650 pCursor->nOffset = 0;
659 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
661 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
662 int nOffset = cursor->nOffset;
666 /* Backward movement */
669 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
670 nOffset, WB_MOVEWORDLEFT);
673 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
674 if (pOtherRun->type == diRun)
676 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
677 pOtherRun->member.run.strText->nLen - 1,
679 && !(pRun->member.run.nFlags & MERF_ENDPARA)
680 && !(cursor->pRun == pRun && cursor->nOffset == 0)
681 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
685 nOffset = pOtherRun->member.run.strText->nLen;
687 else if (pOtherRun->type == diParagraph)
689 if (cursor->pRun == pRun && cursor->nOffset == 0)
691 /* Paragraph breaks are treated as separate words */
692 if (pOtherRun->member.para.prev_para->type == diTextStart)
694 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
702 /* Forward movement */
703 BOOL last_delim = FALSE;
707 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
708 nOffset, WB_ISDELIMITER))
710 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
711 nOffset, WB_MOVEWORDRIGHT);
712 if (nOffset < pRun->member.run.strText->nLen)
714 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
715 if (pOtherRun->type == diRun)
717 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
718 nOffset - 1, WB_ISDELIMITER);
722 else if (pOtherRun->type == diParagraph)
724 if (cursor->pRun == pRun)
725 pRun = ME_FindItemFwd(pOtherRun, diRun);
731 if (cursor->pRun == pRun)
739 cursor->nOffset = nOffset;
745 ME_SelectWord(ME_TextEditor *editor)
747 if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
748 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
749 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
750 ME_InvalidateSelection(editor);
751 ME_SendSelChange(editor);
755 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
757 ME_Cursor *pCursor = &editor->pCursors[nCursor];
758 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
759 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
762 static void ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
764 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
765 ME_DisplayItem *last = NULL;
772 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
774 assert(p->type == diParagraph);
775 if (y < p->member.para.nYPos + p->member.para.nHeight)
777 y -= p->member.para.nYPos;
778 p = ME_FindItemFwd(p, diStartRow);
783 for (; p != editor->pBuffer->pLast; )
786 assert(p->type == diStartRow);
787 if (y < p->member.row.nYPos + p->member.row.nHeight)
789 p = ME_FindItemFwd(p, diRun);
792 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
793 if (pp->type != diStartRow)
795 p = ME_FindItemFwd(p, diRun);
800 for (; p != editor->pBuffer->pLast; p = p->next)
805 rx = x - p->member.run.pt.x;
806 if (rx < p->member.run.nWidth)
809 assert(p->type == diRun);
810 if ((p->member.run.nFlags & MERF_ENDPARA) || rx < 0)
813 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
814 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
816 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
823 p = ME_FindItemFwd(p, diRun);
824 if (is_eol) *is_eol = 1;
825 rx = 0; /* FIXME not sure */
829 rx = 0; /* FIXME not sure */
836 result->pRun = ME_FindItemBack(p, diRun);
838 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
843 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
848 GetClientRect(editor->hWnd, &rc);
849 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
851 y += ME_GetYScrollPos(editor);
852 ME_FindPixelPos(editor, x, y, &cursor, NULL);
853 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
854 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
858 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
860 ME_Cursor tmp_cursor;
861 int is_selection = 0;
863 editor->nUDArrowX = -1;
865 y += ME_GetYScrollPos(editor);
867 tmp_cursor = editor->pCursors[0];
868 is_selection = ME_IsSelection(editor);
870 if (x >= editor->selofs)
872 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
873 if (GetKeyState(VK_SHIFT)>=0)
875 editor->pCursors[1] = editor->pCursors[0];
877 else if (!is_selection) {
878 editor->pCursors[1] = tmp_cursor;
882 ME_InvalidateSelection(editor);
883 HideCaret(editor->hWnd);
884 ME_MoveCaret(editor);
885 ShowCaret(editor->hWnd);
886 ME_ClearTempStyle(editor);
887 ME_SendSelChange(editor);
891 ME_DisplayItem *pRow;
895 /* Set pCursors[0] to beginning of line */
896 ME_FindPixelPos(editor, x, y, &editor->pCursors[1], &editor->bCaretAtEnd);
897 /* Set pCursors[1] to end of line */
898 pRow = ME_FindItemFwd(editor->pCursors[1].pRun, diStartRowOrParagraphOrEnd);
900 /* pCursor[0] is the position where the cursor will be drawn,
901 * pCursor[1] is the other end of the selection range
902 * pCursor[2] and [3] are backups of [0] and [1] so I
903 * don't have to look them up again
906 if (pRow->type == diStartRow) {
907 /* FIXME WTF was I thinking about here ? */
908 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
910 editor->pCursors[0].pRun = pRun;
911 editor->pCursors[0].nOffset = 0;
912 editor->bCaretAtEnd = 1;
914 editor->pCursors[0].pRun = ME_FindItemBack(pRow, diRun);
915 assert(editor->pCursors[0].pRun && editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA);
916 editor->pCursors[0].nOffset = 0;
917 editor->bCaretAtEnd = 0;
919 editor->pCursors[2] = editor->pCursors[0];
920 editor->pCursors[3] = editor->pCursors[1];
921 ME_InvalidateSelection(editor);
922 HideCaret(editor->hWnd);
923 ME_MoveCaret(editor);
924 ShowCaret(editor->hWnd);
925 ME_ClearTempStyle(editor);
926 ME_SendSelChange(editor);
930 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
932 ME_Cursor tmp_cursor;
934 y += ME_GetYScrollPos(editor);
936 tmp_cursor = editor->pCursors[0];
937 /* FIXME: do something with the return value of ME_FindPixelPos */
938 if (!editor->linesel)
939 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
940 else ME_FindPixelPos(editor, (y > editor->sely) * editor->rcFormat.right, y, &tmp_cursor, &editor->bCaretAtEnd);
942 if (!memcmp(&tmp_cursor, editor->pCursors, sizeof(tmp_cursor)))
945 ME_InvalidateSelection(editor);
946 if (!editor->linesel)
947 editor->pCursors[0] = tmp_cursor;
948 else if (!memcmp(&tmp_cursor, editor->pCursors+2, sizeof(tmp_cursor)) ||
949 !memcmp(&tmp_cursor, editor->pCursors+3, sizeof(tmp_cursor)))
951 editor->pCursors[0] = editor->pCursors[2];
952 editor->pCursors[1] = editor->pCursors[3];
954 else if (y < editor->sely)
956 editor->pCursors[0] = tmp_cursor;
957 editor->pCursors[1] = editor->pCursors[2];
961 editor->pCursors[0] = tmp_cursor;
962 editor->pCursors[1] = editor->pCursors[3];
965 HideCaret(editor->hWnd);
966 ME_MoveCaret(editor);
967 ME_InvalidateSelection(editor);
968 ShowCaret(editor->hWnd);
969 ME_SendSelChange(editor);
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 *pbCaretAtEnd = FALSE;
981 int run_x = pNext->member.run.pt.x;
982 int width = pNext->member.run.nWidth;
985 if (pOffset) *pOffset = 0;
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 = 1;
1006 if (pOffset) *pOffset = 0;
1009 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
1010 if (pOffset) *pOffset = 0;
1015 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1017 ME_DisplayItem *pRun = pCursor->pRun;
1020 if (editor->nUDArrowX != -1)
1021 x = editor->nUDArrowX;
1023 if (editor->bCaretAtEnd)
1025 pRun = ME_FindItemBack(pRun, diRun);
1027 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1030 x = pRun->member.run.pt.x;
1031 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
1033 editor->nUDArrowX = x;
1040 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1042 ME_DisplayItem *pRun = pCursor->pRun;
1043 ME_DisplayItem *pItem;
1044 int x = ME_GetXForArrow(editor, pCursor);
1046 if (editor->bCaretAtEnd && !pCursor->nOffset)
1047 pRun = ME_FindItemBack(pRun, diRun);
1052 /* start of this row */
1053 pItem = ME_FindItemBack(pRun, diStartRow);
1055 /* start of the previous row */
1056 pItem = ME_FindItemBack(pItem, diStartRow);
1060 /* start of the next row */
1061 pItem = ME_FindItemFwd(pRun, diStartRow);
1062 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1067 /* row not found - ignore */
1070 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1071 assert(pCursor->pRun);
1072 assert(pCursor->pRun->type == diRun);
1076 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1078 ME_DisplayItem *pRun = pCursor->pRun;
1079 ME_DisplayItem *pLast, *p;
1080 int x, y, ys, yd, yp, yprev;
1081 ME_Cursor tmp_curs = *pCursor;
1083 x = ME_GetXForArrow(editor, pCursor);
1084 if (!pCursor->nOffset && editor->bCaretAtEnd)
1085 pRun = ME_FindItemBack(pRun, diRun);
1087 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1088 assert(p->type == diStartRow);
1089 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1090 yprev = ys = y = yp + p->member.row.nYPos;
1091 yd = y - editor->sizeWindow.cy;
1095 p = ME_FindItemBack(p, diStartRowOrParagraph);
1098 if (p->type == diParagraph) { /* crossing paragraphs */
1099 if (p->member.para.prev_para == NULL)
1101 yp = p->member.para.prev_para->member.para.nYPos;
1104 y = yp + p->member.row.nYPos;
1111 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1112 ME_UpdateSelection(editor, &tmp_curs);
1113 if (yprev < editor->sizeWindow.cy)
1115 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
1120 ME_ScrollUp(editor, ys-yprev);
1122 assert(pCursor->pRun);
1123 assert(pCursor->pRun->type == diRun);
1126 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1127 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1128 In such a situation, clicking the scrollbar restores its position back to the
1129 normal range (ie. sets it to (doclength-screenheight)). */
1131 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1133 ME_DisplayItem *pRun = pCursor->pRun;
1134 ME_DisplayItem *pLast, *p;
1135 int x, y, ys, yd, yp, yprev;
1136 ME_Cursor tmp_curs = *pCursor;
1138 x = ME_GetXForArrow(editor, pCursor);
1139 if (!pCursor->nOffset && editor->bCaretAtEnd)
1140 pRun = ME_FindItemBack(pRun, diRun);
1142 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1143 assert(p->type == diStartRow);
1144 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1145 yprev = ys = y = yp + p->member.row.nYPos;
1146 yd = y + editor->sizeWindow.cy;
1150 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1153 if (p->type == diParagraph) {
1154 yp = p->member.para.nYPos;
1157 y = yp + p->member.row.nYPos;
1164 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1165 ME_UpdateSelection(editor, &tmp_curs);
1166 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
1168 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1173 ME_ScrollUp(editor,ys-yprev);
1175 assert(pCursor->pRun);
1176 assert(pCursor->pRun->type == diRun);
1179 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1181 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1182 /* bCaretAtEnd doesn't make sense if the cursor isn't set at the
1183 first character of the next row */
1184 assert(!editor->bCaretAtEnd || !pCursor->nOffset);
1185 ME_WrapMarkedParagraphs(editor);
1187 ME_DisplayItem *pRun;
1188 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1189 pRow = ME_FindItemBack(pRow, diStartRow);
1193 pRun = ME_FindItemFwd(pRow, diRun);
1195 pCursor->pRun = pRun;
1196 pCursor->nOffset = 0;
1199 editor->bCaretAtEnd = FALSE;
1202 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1204 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1206 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1208 pCursor->pRun = pRun;
1209 pCursor->nOffset = 0;
1214 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1216 ME_DisplayItem *pRow;
1218 if (editor->bCaretAtEnd && !pCursor->nOffset)
1221 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1223 if (pRow->type == diStartRow) {
1224 /* FIXME WTF was I thinking about here ? */
1225 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1227 pCursor->pRun = pRun;
1228 pCursor->nOffset = 0;
1229 editor->bCaretAtEnd = 1;
1232 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1233 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1234 pCursor->nOffset = 0;
1235 editor->bCaretAtEnd = FALSE;
1238 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1240 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1242 p = ME_FindItemBack(p, diRun);
1244 assert(p->member.run.nFlags & MERF_ENDPARA);
1246 pCursor->nOffset = 0;
1247 editor->bCaretAtEnd = FALSE;
1250 BOOL ME_IsSelection(ME_TextEditor *editor)
1252 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1255 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1257 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1265 BOOL ME_UpdateSelection(ME_TextEditor *editor, const ME_Cursor *pTempCursor)
1267 ME_Cursor old_anchor = editor->pCursors[1];
1269 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1271 /* any selection was present ? if so, it's no more, repaint ! */
1272 editor->pCursors[1] = editor->pCursors[0];
1273 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1280 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1282 editor->pCursors[1] = *pTempCursor;
1291 void ME_DeleteSelection(ME_TextEditor *editor)
1294 ME_GetSelection(editor, &from, &to);
1295 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1298 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1303 ME_GetSelection(editor, &from, &to);
1306 ME_CursorFromCharOfs(editor, from, &c);
1307 style = c.pRun->member.run.style;
1308 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1311 style = ME_GetInsertStyle(editor, 0);
1315 void ME_SendSelChange(ME_TextEditor *editor)
1319 ME_ClearTempStyle(editor);
1321 if (!(editor->nEventMask & ENM_SELCHANGE))
1324 sc.nmhdr.hwndFrom = editor->hWnd;
1325 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1326 sc.nmhdr.code = EN_SELCHANGE;
1327 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1328 sc.seltyp = SEL_EMPTY;
1329 if (sc.chrg.cpMin != sc.chrg.cpMax)
1330 sc.seltyp |= SEL_TEXT;
1331 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1332 sc.seltyp |= SEL_MULTICHAR;
1333 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1334 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1335 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1336 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1337 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1339 editor->notified_cr = sc.chrg;
1340 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1345 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1348 ME_Cursor *p = &editor->pCursors[nCursor];
1349 ME_Cursor tmp_curs = *p;
1350 BOOL success = FALSE;
1352 ME_CheckCharOffsets(editor);
1353 editor->nUDArrowX = -1;
1356 editor->bCaretAtEnd = 0;
1358 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1360 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1363 editor->bCaretAtEnd = 0;
1365 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1367 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1370 ME_MoveCursorLines(editor, &tmp_curs, -1);
1373 ME_MoveCursorLines(editor, &tmp_curs, +1);
1376 ME_ArrowPageUp(editor, &tmp_curs);
1379 ME_ArrowPageDown(editor, &tmp_curs);
1383 ME_ArrowCtrlHome(editor, &tmp_curs);
1385 ME_ArrowHome(editor, &tmp_curs);
1386 editor->bCaretAtEnd = 0;
1391 ME_ArrowCtrlEnd(editor, &tmp_curs);
1393 ME_ArrowEnd(editor, &tmp_curs);
1398 editor->pCursors[1] = tmp_curs;
1401 ME_InvalidateSelection(editor);
1403 HideCaret(editor->hWnd);
1404 ME_EnsureVisible(editor, tmp_curs.pRun);
1405 ME_ShowCaret(editor);
1406 ME_SendSelChange(editor);