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)
186 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrParagraph);
188 if (prev->type == diRun)
191 assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
192 para = ME_FindItemBack(row, diParagraph);
194 assert(para->type == diParagraph);
195 if (editor->bCaretAtEnd && !pCursor->nOffset &&
196 run == ME_FindItemFwd(row, diRun))
198 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
200 if (tmp->type == diRun)
202 row = ME_FindItemBack(tmp, diStartRow);
203 pSizeRun = run = tmp;
205 assert(run->type == diRun);
206 sz = ME_GetRunSize(&c, ¶->member.para,
207 &run->member.run, ME_StrLen(run->member.run.strText),
208 row->member.row.nLMargin);
211 if (pCursor->nOffset) {
212 sz = ME_GetRunSize(&c, ¶->member.para, &run->member.run, pCursor->nOffset,
213 row->member.row.nLMargin);
216 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
217 *x = run->member.run.pt.x + sz.cx;
218 *y = para->member.para.nYPos + row->member.row.nBaseline + run->member.run.pt.y - pSizeRun->member.run.nAscent - ME_GetYScrollPos(editor);
219 ME_DestroyContext(&c, editor->hWnd);
223 *height = 10; /* FIXME use global font */
230 ME_MoveCaret(ME_TextEditor *editor)
234 if (ME_WrapMarkedParagraphs(editor))
235 ME_UpdateScrollBar(editor);
236 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
237 if(editor->bHaveFocus)
241 GetClientRect(editor->hWnd, &rect);
242 x = min(x, rect.right-2);
243 CreateCaret(editor->hWnd, NULL, 0, height);
249 void ME_ShowCaret(ME_TextEditor *ed)
256 void ME_HideCaret(ME_TextEditor *ed)
265 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
274 ME_CursorFromCharOfs(editor, nOfs, &c);
275 run = &c.pRun->member.run;
276 if (run->nFlags & MERF_ENDPARA) {
277 int eollen = run->nCR + run->nLF;
279 if (!ME_FindItemFwd(c.pRun, diParagraph))
283 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
284 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
285 ME_CheckCharOffsets(editor);
286 nChars -= (eollen < nChars) ? eollen : nChars;
292 int nIntendedChars = nChars;
293 int nCharsToDelete = nChars;
297 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
300 ME_StrRelPos(run->strText, loc, &nChars);
301 /* nChars is the number of characters that should be deleted from the
302 FOLLOWING runs (these AFTER cursor.pRun)
303 nCharsToDelete is a number of chars to delete from THIS run */
304 nCharsToDelete -= nChars;
305 shift -= nCharsToDelete;
306 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
307 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
308 debugstr_w(run->strText->szData), run->strText->nLen);
310 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
312 /* undo = reinsert whole run */
313 /* nOfs is a character offset (from the start of the document
314 to the current (deleted) run */
315 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
317 pUndo->di.member.run.nCharOfs = nOfs;
321 /* undo = reinsert partial run */
322 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
324 ME_DestroyString(pUndo->di.member.run.strText);
325 pUndo->di.member.run.nCharOfs = nOfs;
326 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
329 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
330 TRACE("Shift value: %d\n", shift);
331 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
333 /* update cursors (including c) */
334 for (i=-1; i<editor->nCursors; i++) {
335 ME_Cursor *pThisCur = editor->pCursors + i;
336 if (i == -1) pThisCur = &c;
337 if (pThisCur->pRun == cursor.pRun) {
338 if (pThisCur->nOffset > cursor.nOffset) {
339 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
340 pThisCur->nOffset = cursor.nOffset;
342 pThisCur->nOffset -= nCharsToDelete;
343 assert(pThisCur->nOffset >= 0);
344 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
346 if (pThisCur->nOffset == ME_StrVLen(run->strText))
348 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
349 assert(pThisCur->pRun->type == diRun);
350 pThisCur->nOffset = 0;
355 /* c = updated data now */
357 if (c.pRun == cursor.pRun)
358 ME_SkipAndPropagateCharOffset(c.pRun, shift);
360 ME_PropagateCharOffset(c.pRun, shift);
362 if (!ME_StrVLen(cursor.pRun->member.run.strText))
364 TRACE("Removing useless run\n");
365 ME_Remove(cursor.pRun);
366 ME_DestroyDisplayItem(cursor.pRun);
371 ME_CheckCharOffsets(editor);
378 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
381 assert(nCursor>=0 && nCursor<editor->nCursors);
382 /* text operations set modified state */
383 editor->nModifyStep = 1;
384 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
387 static ME_DisplayItem *
388 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
389 const WCHAR *str, int len, ME_Style *style,
392 ME_Cursor *p = &editor->pCursors[nCursor];
394 editor->bCaretAtEnd = FALSE;
396 assert(p->pRun->type == diRun);
398 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
402 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
404 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
409 if (ME_IsSelection(editor))
410 ME_DeleteSelection(editor);
412 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
414 di->member.run.ole_obj = ALLOC_OBJ(*reo);
415 ME_CopyReObject(di->member.run.ole_obj, reo);
416 ME_SendSelChange(editor);
420 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
422 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
427 if (ME_IsSelection(editor))
428 ME_DeleteSelection(editor);
430 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
432 ME_SendSelChange(editor);
436 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
439 ME_DisplayItem *p, *run;
440 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
442 p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
445 while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
447 if (run->member.run.nFlags & MERF_CELL)
449 assert(run->member.run.pCell->next);
450 p->member.run.pCell = run->member.run.pCell->next;
454 assert(run->type == diParagraph);
455 assert(run->member.para.bTable);
456 assert(run->member.para.pCells);
457 p->member.run.pCell = run->member.para.pCells;
461 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
462 const WCHAR *str, int len, ME_Style *style)
468 /* FIXME really HERE ? */
469 if (ME_IsSelection(editor))
470 ME_DeleteSelection(editor);
472 /* FIXME: is this too slow? */
473 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
474 oldLen = ME_GetTextLength(editor);
476 /* text operations set modified state */
477 editor->nModifyStep = 1;
481 assert(nCursor>=0 && nCursor<editor->nCursors);
485 /* grow the text limit to fit our text */
486 if(editor->nTextLimit < oldLen +len)
487 editor->nTextLimit = oldLen + len;
492 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
493 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
495 if (pos-str < len && *pos == '\t') { /* handle tabs */
499 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
501 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
510 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
511 if (!editor->bEmulateVersion10 && pos-str < len-2 && pos[0] == '\r' && pos[1] == '\r' && pos[2] == '\n') {
515 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
517 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
526 if (pos-str < len) { /* handle EOLs */
527 ME_DisplayItem *tp, *end_run;
532 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
533 p = &editor->pCursors[nCursor];
535 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
536 p = &editor->pCursors[nCursor];
538 tmp_style = ME_GetInsertStyle(editor, nCursor);
539 /* ME_SplitParagraph increases style refcount */
541 /* Encode and fill number of CR and LF according to emulation mode */
542 if (editor->bEmulateVersion10) {
545 /* We have to find out how many consecutive \r are there, and if there
546 is a \n terminating the run of \r's. */
547 numCR = 0; numLF = 0;
549 while (tpos-str < len && *tpos == '\r') {
553 if (tpos-str >= len) {
554 /* Reached end of text without finding anything but '\r' */
558 numCR = 1; numLF = 0;
559 } else if (*tpos == '\n') {
560 /* The entire run of \r's plus the one \n is one single line break */
564 /* Found some other content past the run of \r's */
566 numCR = 1; numLF = 0;
569 if(pos-str < len && *pos =='\r')
571 if(pos-str < len && *pos =='\n')
573 numCR = 1; numLF = 0;
575 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, numCR, numLF);
576 p->pRun = ME_FindItemFwd(tp, diRun);
577 end_run = ME_FindItemBack(tp, diRun);
578 ME_ReleaseStyle(end_run->member.run.style);
579 end_run->member.run.style = tmp_style;
588 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
595 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
597 ME_DisplayItem *pRun = pCursor->pRun;
601 if (!pCursor->nOffset)
604 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
611 if (pRun->member.para.prev_para->type == diTextStart)
613 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
614 /* every paragraph ought to have at least one run */
615 assert(pRun && pRun->type == diRun);
616 assert(pRun->member.run.nFlags & MERF_ENDPARA);
619 assert(pRun->type != diRun && pRun->type != diParagraph);
622 } while (RUN_IS_HIDDEN(&pRun->member.run));
623 pCursor->pRun = pRun;
624 if (pRun->member.run.nFlags & MERF_ENDPARA)
625 pCursor->nOffset = 0;
627 pCursor->nOffset = pRun->member.run.strText->nLen;
630 if (pCursor->nOffset)
631 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
636 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
638 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
640 if (new_ofs < pRun->member.run.strText->nLen)
642 pCursor->nOffset = new_ofs;
647 pRun = ME_FindItemFwd(pRun, diRun);
648 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
651 pCursor->pRun = pRun;
652 pCursor->nOffset = 0;
661 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
663 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
664 int nOffset = cursor->nOffset;
668 /* Backward movement */
671 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
672 nOffset, WB_MOVEWORDLEFT);
675 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
676 if (pOtherRun->type == diRun)
678 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
679 pOtherRun->member.run.strText->nLen - 1,
681 && !(pRun->member.run.nFlags & MERF_ENDPARA)
682 && !(cursor->pRun == pRun && cursor->nOffset == 0)
683 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
687 nOffset = pOtherRun->member.run.strText->nLen;
689 else if (pOtherRun->type == diParagraph)
691 if (cursor->pRun == pRun && cursor->nOffset == 0)
693 /* Paragraph breaks are treated as separate words */
694 if (pOtherRun->member.para.prev_para->type == diTextStart)
696 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
704 /* Forward movement */
705 BOOL last_delim = FALSE;
709 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
710 nOffset, WB_ISDELIMITER))
712 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
713 nOffset, WB_MOVEWORDRIGHT);
714 if (nOffset < pRun->member.run.strText->nLen)
716 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
717 if (pOtherRun->type == diRun)
719 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
720 nOffset - 1, WB_ISDELIMITER);
724 else if (pOtherRun->type == diParagraph)
726 if (cursor->pRun == pRun)
727 pRun = ME_FindItemFwd(pOtherRun, diRun);
733 if (cursor->pRun == pRun)
741 cursor->nOffset = nOffset;
747 ME_SelectWord(ME_TextEditor *editor)
749 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
750 editor->pCursors[0] = editor->pCursors[1];
751 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
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 if (p == editor->pBuffer->pLast)
802 /* The position is below the last paragraph, so the last row will be used
803 * rather than the end of the text, so the x position will be used to
804 * determine the offset closest to the pixel position. */
805 p = ME_FindItemBack(p, diStartRow);
807 p = ME_FindItemFwd(p, diRun);
811 p = editor->pBuffer->pLast;
814 for (; p != editor->pBuffer->pLast; p = p->next)
819 rx = x - p->member.run.pt.x;
820 if (rx < p->member.run.nWidth)
823 assert(p->type == diRun);
824 if ((p->member.run.nFlags & MERF_ENDPARA) || rx < 0)
827 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
828 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
830 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
837 p = ME_FindItemFwd(p, diRun);
838 if (is_eol) *is_eol = 1;
839 rx = 0; /* FIXME not sure */
843 rx = 0; /* FIXME not sure */
850 result->pRun = ME_FindItemBack(p, diRun);
852 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
857 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
862 GetClientRect(editor->hWnd, &rc);
863 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
865 y += ME_GetYScrollPos(editor);
866 ME_FindPixelPos(editor, x, y, &cursor, NULL);
867 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
868 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
872 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
874 ME_Cursor tmp_cursor;
875 int is_selection = 0;
877 editor->nUDArrowX = -1;
879 y += ME_GetYScrollPos(editor);
881 tmp_cursor = editor->pCursors[0];
882 is_selection = ME_IsSelection(editor);
884 if (x >= editor->selofs)
886 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
887 if (GetKeyState(VK_SHIFT)>=0)
889 /* Shift key is not down */
890 editor->pCursors[1] = editor->pCursors[0];
892 ME_SelectWord(editor);
894 else if (!is_selection) {
895 editor->pCursors[1] = tmp_cursor;
901 ME_DisplayItem *pRow;
905 /* Set pCursors[0] to beginning of line */
906 ME_FindPixelPos(editor, x, y, &editor->pCursors[1], &editor->bCaretAtEnd);
907 /* Set pCursors[1] to end of line */
908 pRow = ME_FindItemFwd(editor->pCursors[1].pRun, diStartRowOrParagraphOrEnd);
910 /* pCursor[0] is the position where the cursor will be drawn,
911 * pCursor[1] is the other end of the selection range
912 * pCursor[2] and [3] are backups of [0] and [1] so I
913 * don't have to look them up again
916 if (pRow->type == diStartRow) {
917 /* FIXME WTF was I thinking about here ? */
918 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
920 editor->pCursors[0].pRun = pRun;
921 editor->pCursors[0].nOffset = 0;
922 editor->bCaretAtEnd = 1;
924 editor->pCursors[0].pRun = ME_FindItemBack(pRow, diRun);
925 assert(editor->pCursors[0].pRun && editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA);
926 editor->pCursors[0].nOffset = 0;
927 editor->bCaretAtEnd = 0;
929 editor->pCursors[2] = editor->pCursors[0];
930 editor->pCursors[3] = editor->pCursors[1];
932 ME_InvalidateSelection(editor);
933 HideCaret(editor->hWnd);
934 ME_MoveCaret(editor);
935 ShowCaret(editor->hWnd);
936 ME_ClearTempStyle(editor);
937 ME_SendSelChange(editor);
940 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
942 ME_Cursor tmp_cursor;
944 y += ME_GetYScrollPos(editor);
946 tmp_cursor = editor->pCursors[0];
947 /* FIXME: do something with the return value of ME_FindPixelPos */
948 if (!editor->linesel)
949 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
950 else ME_FindPixelPos(editor, (y > editor->sely) * editor->rcFormat.right, y, &tmp_cursor, &editor->bCaretAtEnd);
952 if (!memcmp(&tmp_cursor, editor->pCursors, sizeof(tmp_cursor)))
955 ME_InvalidateSelection(editor);
956 if (!editor->linesel)
957 editor->pCursors[0] = tmp_cursor;
958 else if (!memcmp(&tmp_cursor, editor->pCursors+2, sizeof(tmp_cursor)) ||
959 !memcmp(&tmp_cursor, editor->pCursors+3, sizeof(tmp_cursor)))
961 editor->pCursors[0] = editor->pCursors[2];
962 editor->pCursors[1] = editor->pCursors[3];
964 else if (y < editor->sely)
966 editor->pCursors[0] = tmp_cursor;
967 editor->pCursors[1] = editor->pCursors[2];
971 editor->pCursors[0] = tmp_cursor;
972 editor->pCursors[1] = editor->pCursors[3];
975 HideCaret(editor->hWnd);
976 ME_MoveCaret(editor);
977 ME_InvalidateSelection(editor);
978 ShowCaret(editor->hWnd);
979 ME_SendSelChange(editor);
980 SendMessageW(editor->hWnd, EM_SCROLLCARET, 0, 0);
983 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
984 int x, int *pOffset, int *pbCaretAtEnd)
986 ME_DisplayItem *pNext, *pLastRun;
987 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
988 assert(pNext->type == diRun);
990 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
991 if (pOffset) *pOffset = 0;
993 int run_x = pNext->member.run.pt.x;
994 int width = pNext->member.run.nWidth;
999 if (x >= run_x && x < run_x+width)
1001 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
1002 ME_String *s = pNext->member.run.strText;
1010 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
1011 } while(pNext && pNext->type == diRun);
1013 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
1015 pNext = ME_FindItemFwd(pNext, diRun);
1016 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
1023 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1025 ME_DisplayItem *pRun = pCursor->pRun;
1028 if (editor->nUDArrowX != -1)
1029 x = editor->nUDArrowX;
1031 if (editor->bCaretAtEnd)
1033 pRun = ME_FindItemBack(pRun, diRun);
1035 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1038 x = pRun->member.run.pt.x;
1039 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
1041 editor->nUDArrowX = x;
1048 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1050 ME_DisplayItem *pRun = pCursor->pRun;
1051 ME_DisplayItem *pItem;
1052 int x = ME_GetXForArrow(editor, pCursor);
1054 if (editor->bCaretAtEnd && !pCursor->nOffset)
1055 pRun = ME_FindItemBack(pRun, diRun);
1060 /* start of this row */
1061 pItem = ME_FindItemBack(pRun, diStartRow);
1063 /* start of the previous row */
1064 pItem = ME_FindItemBack(pItem, diStartRow);
1068 /* start of the next row */
1069 pItem = ME_FindItemFwd(pRun, diStartRow);
1070 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1075 /* row not found - ignore */
1078 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1079 assert(pCursor->pRun);
1080 assert(pCursor->pRun->type == diRun);
1084 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1086 ME_DisplayItem *pRun = pCursor->pRun;
1087 ME_DisplayItem *pLast, *p;
1088 int x, y, ys, yd, yp, yprev;
1089 ME_Cursor tmp_curs = *pCursor;
1091 x = ME_GetXForArrow(editor, pCursor);
1092 if (!pCursor->nOffset && editor->bCaretAtEnd)
1093 pRun = ME_FindItemBack(pRun, diRun);
1095 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1096 assert(p->type == diStartRow);
1097 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1098 yprev = ys = y = yp + p->member.row.nYPos;
1099 yd = y - editor->sizeWindow.cy;
1103 p = ME_FindItemBack(p, diStartRowOrParagraph);
1106 if (p->type == diParagraph) { /* crossing paragraphs */
1107 if (p->member.para.prev_para == NULL)
1109 yp = p->member.para.prev_para->member.para.nYPos;
1112 y = yp + p->member.row.nYPos;
1119 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1120 ME_UpdateSelection(editor, &tmp_curs);
1121 if (yprev < editor->sizeWindow.cy)
1123 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
1128 ME_ScrollUp(editor, ys-yprev);
1130 assert(pCursor->pRun);
1131 assert(pCursor->pRun->type == diRun);
1134 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1135 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1136 In such a situation, clicking the scrollbar restores its position back to the
1137 normal range (ie. sets it to (doclength-screenheight)). */
1139 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1141 ME_DisplayItem *pRun = pCursor->pRun;
1142 ME_DisplayItem *pLast, *p;
1143 int x, y, ys, yd, yp, yprev;
1144 ME_Cursor tmp_curs = *pCursor;
1146 x = ME_GetXForArrow(editor, pCursor);
1147 if (!pCursor->nOffset && editor->bCaretAtEnd)
1148 pRun = ME_FindItemBack(pRun, diRun);
1150 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1151 assert(p->type == diStartRow);
1152 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1153 yprev = ys = y = yp + p->member.row.nYPos;
1154 yd = y + editor->sizeWindow.cy;
1158 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1161 if (p->type == diParagraph) {
1162 yp = p->member.para.nYPos;
1165 y = yp + p->member.row.nYPos;
1172 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1173 ME_UpdateSelection(editor, &tmp_curs);
1174 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
1176 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1181 ME_ScrollUp(editor,ys-yprev);
1183 assert(pCursor->pRun);
1184 assert(pCursor->pRun->type == diRun);
1187 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1189 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1190 ME_WrapMarkedParagraphs(editor);
1192 ME_DisplayItem *pRun;
1193 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1194 pRow = ME_FindItemBack(pRow, diStartRow);
1198 pRun = ME_FindItemFwd(pRow, diRun);
1200 pCursor->pRun = pRun;
1201 pCursor->nOffset = 0;
1204 editor->bCaretAtEnd = FALSE;
1207 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1209 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1211 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1213 pCursor->pRun = pRun;
1214 pCursor->nOffset = 0;
1219 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1221 ME_DisplayItem *pRow;
1223 if (editor->bCaretAtEnd && !pCursor->nOffset)
1226 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1228 if (pRow->type == diStartRow) {
1229 /* FIXME WTF was I thinking about here ? */
1230 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1232 pCursor->pRun = pRun;
1233 pCursor->nOffset = 0;
1234 editor->bCaretAtEnd = 1;
1237 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1238 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1239 pCursor->nOffset = 0;
1240 editor->bCaretAtEnd = FALSE;
1243 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1245 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1247 p = ME_FindItemBack(p, diRun);
1249 assert(p->member.run.nFlags & MERF_ENDPARA);
1251 pCursor->nOffset = 0;
1252 editor->bCaretAtEnd = FALSE;
1255 BOOL ME_IsSelection(ME_TextEditor *editor)
1257 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1260 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1262 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1270 BOOL ME_UpdateSelection(ME_TextEditor *editor, const ME_Cursor *pTempCursor)
1272 ME_Cursor old_anchor = editor->pCursors[1];
1274 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1276 /* any selection was present ? if so, it's no more, repaint ! */
1277 editor->pCursors[1] = editor->pCursors[0];
1278 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1285 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1287 editor->pCursors[1] = *pTempCursor;
1296 void ME_DeleteSelection(ME_TextEditor *editor)
1299 ME_GetSelection(editor, &from, &to);
1300 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1303 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1305 return ME_GetInsertStyle(editor, 0);
1308 void ME_SendSelChange(ME_TextEditor *editor)
1312 if (!(editor->nEventMask & ENM_SELCHANGE))
1315 sc.nmhdr.hwndFrom = editor->hWnd;
1316 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1317 sc.nmhdr.code = EN_SELCHANGE;
1318 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1319 sc.seltyp = SEL_EMPTY;
1320 if (sc.chrg.cpMin != sc.chrg.cpMax)
1321 sc.seltyp |= SEL_TEXT;
1322 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1323 sc.seltyp |= SEL_MULTICHAR;
1324 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1325 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1326 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1327 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1328 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1330 ME_ClearTempStyle(editor);
1332 editor->notified_cr = sc.chrg;
1333 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1338 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1341 ME_Cursor *p = &editor->pCursors[nCursor];
1342 ME_Cursor tmp_curs = *p;
1343 BOOL success = FALSE;
1345 ME_CheckCharOffsets(editor);
1348 editor->bCaretAtEnd = 0;
1350 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1352 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1355 editor->bCaretAtEnd = 0;
1357 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1359 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1362 ME_MoveCursorLines(editor, &tmp_curs, -1);
1365 ME_MoveCursorLines(editor, &tmp_curs, +1);
1368 ME_ArrowPageUp(editor, &tmp_curs);
1371 ME_ArrowPageDown(editor, &tmp_curs);
1375 ME_ArrowCtrlHome(editor, &tmp_curs);
1377 ME_ArrowHome(editor, &tmp_curs);
1378 editor->bCaretAtEnd = 0;
1383 ME_ArrowCtrlEnd(editor, &tmp_curs);
1385 ME_ArrowEnd(editor, &tmp_curs);
1390 editor->pCursors[1] = tmp_curs;
1393 ME_InvalidateSelection(editor);
1395 HideCaret(editor->hWnd);
1396 ME_EnsureVisible(editor, tmp_curs.pRun);
1397 ME_ShowCaret(editor);
1398 ME_SendSelChange(editor);