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 && !ME_IsSelection(editor))
241 GetClientRect(editor->hWnd, &rect);
242 x = min(x, rect.right-2);
243 CreateCaret(editor->hWnd, NULL, 0, height);
251 void ME_ShowCaret(ME_TextEditor *ed)
254 if(ed->bHaveFocus && !ME_IsSelection(ed))
258 void ME_HideCaret(ME_TextEditor *ed)
260 if(!ed->bHaveFocus || ME_IsSelection(ed))
267 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
276 ME_CursorFromCharOfs(editor, nOfs, &c);
277 run = &c.pRun->member.run;
278 if (run->nFlags & MERF_ENDPARA) {
279 int eollen = run->nCR + run->nLF;
281 if (!ME_FindItemFwd(c.pRun, diParagraph))
285 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
286 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
287 ME_CheckCharOffsets(editor);
288 nChars -= (eollen < nChars) ? eollen : nChars;
294 int nIntendedChars = nChars;
295 int nCharsToDelete = nChars;
299 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
302 ME_StrRelPos(run->strText, loc, &nChars);
303 /* nChars is the number of characters that should be deleted from the
304 FOLLOWING runs (these AFTER cursor.pRun)
305 nCharsToDelete is a number of chars to delete from THIS run */
306 nCharsToDelete -= nChars;
307 shift -= nCharsToDelete;
308 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
309 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
310 debugstr_w(run->strText->szData), run->strText->nLen);
312 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
314 /* undo = reinsert whole run */
315 /* nOfs is a character offset (from the start of the document
316 to the current (deleted) run */
317 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
319 pUndo->di.member.run.nCharOfs = nOfs;
323 /* undo = reinsert partial run */
324 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
326 ME_DestroyString(pUndo->di.member.run.strText);
327 pUndo->di.member.run.nCharOfs = nOfs;
328 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
331 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
332 TRACE("Shift value: %d\n", shift);
333 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
335 /* update cursors (including c) */
336 for (i=-1; i<editor->nCursors; i++) {
337 ME_Cursor *pThisCur = editor->pCursors + i;
338 if (i == -1) pThisCur = &c;
339 if (pThisCur->pRun == cursor.pRun) {
340 if (pThisCur->nOffset > cursor.nOffset) {
341 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
342 pThisCur->nOffset = cursor.nOffset;
344 pThisCur->nOffset -= nCharsToDelete;
345 assert(pThisCur->nOffset >= 0);
346 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
348 if (pThisCur->nOffset == ME_StrVLen(run->strText))
350 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
351 assert(pThisCur->pRun->type == diRun);
352 pThisCur->nOffset = 0;
357 /* c = updated data now */
359 if (c.pRun == cursor.pRun)
360 ME_SkipAndPropagateCharOffset(c.pRun, shift);
362 ME_PropagateCharOffset(c.pRun, shift);
364 if (!ME_StrVLen(cursor.pRun->member.run.strText))
366 TRACE("Removing useless run\n");
367 ME_Remove(cursor.pRun);
368 ME_DestroyDisplayItem(cursor.pRun);
373 ME_CheckCharOffsets(editor);
380 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
383 assert(nCursor>=0 && nCursor<editor->nCursors);
384 /* text operations set modified state */
385 editor->nModifyStep = 1;
386 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
389 static ME_DisplayItem *
390 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
391 const WCHAR *str, int len, ME_Style *style,
394 ME_Cursor *p = &editor->pCursors[nCursor];
396 editor->bCaretAtEnd = FALSE;
398 assert(p->pRun->type == diRun);
400 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
404 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
406 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
411 if (ME_IsSelection(editor))
412 ME_DeleteSelection(editor);
414 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
416 di->member.run.ole_obj = ALLOC_OBJ(*reo);
417 ME_CopyReObject(di->member.run.ole_obj, reo);
418 ME_SendSelChange(editor);
422 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
424 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
429 if (ME_IsSelection(editor))
430 ME_DeleteSelection(editor);
432 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
434 ME_SendSelChange(editor);
438 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
441 ME_DisplayItem *p, *run;
442 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
444 p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
447 while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
449 if (run->member.run.nFlags & MERF_CELL)
451 assert(run->member.run.pCell->next);
452 p->member.run.pCell = run->member.run.pCell->next;
456 assert(run->type == diParagraph);
457 assert(run->member.para.pFmt);
458 assert(run->member.para.pFmt->dwMask & PFM_TABLE);
459 assert(run->member.para.pFmt->wEffects & PFE_TABLE);
460 assert(run->member.para.pCells);
461 p->member.run.pCell = run->member.para.pCells;
465 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
466 const WCHAR *str, int len, ME_Style *style)
472 /* FIXME really HERE ? */
473 if (ME_IsSelection(editor))
474 ME_DeleteSelection(editor);
476 /* FIXME: is this too slow? */
477 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
478 oldLen = ME_GetTextLength(editor);
480 /* text operations set modified state */
481 editor->nModifyStep = 1;
485 assert(nCursor>=0 && nCursor<editor->nCursors);
489 /* grow the text limit to fit our text */
490 if(editor->nTextLimit < oldLen +len)
491 editor->nTextLimit = oldLen + len;
496 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
497 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
499 if (pos-str < len && *pos == '\t') { /* handle tabs */
503 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
505 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
514 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
515 if (!editor->bEmulateVersion10 && pos-str < len-2 && pos[0] == '\r' && pos[1] == '\r' && pos[2] == '\n') {
519 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
521 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
530 if (pos-str < len) { /* handle EOLs */
531 ME_DisplayItem *tp, *end_run;
536 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
537 p = &editor->pCursors[nCursor];
539 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
540 p = &editor->pCursors[nCursor];
542 tmp_style = ME_GetInsertStyle(editor, nCursor);
543 /* ME_SplitParagraph increases style refcount */
545 /* Encode and fill number of CR and LF according to emulation mode */
546 if (editor->bEmulateVersion10) {
549 /* We have to find out how many consecutive \r are there, and if there
550 is a \n terminating the run of \r's. */
551 numCR = 0; numLF = 0;
553 while (tpos-str < len && *tpos == '\r') {
557 if (tpos-str >= len) {
558 /* Reached end of text without finding anything but '\r' */
562 numCR = 1; numLF = 0;
563 } else if (*tpos == '\n') {
564 /* The entire run of \r's plus the one \n is one single line break */
568 /* Found some other content past the run of \r's */
570 numCR = 1; numLF = 0;
573 if(pos-str < len && *pos =='\r')
575 if(pos-str < len && *pos =='\n')
577 numCR = 1; numLF = 0;
579 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, numCR, numLF);
580 p->pRun = ME_FindItemFwd(tp, diRun);
581 end_run = ME_FindItemBack(tp, diRun);
582 ME_ReleaseStyle(end_run->member.run.style);
583 end_run->member.run.style = tmp_style;
592 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
599 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
601 ME_DisplayItem *pRun = pCursor->pRun;
605 if (!pCursor->nOffset)
608 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
615 if (pRun->member.para.prev_para->type == diTextStart)
617 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
618 /* every paragraph ought to have at least one run */
619 assert(pRun && pRun->type == diRun);
620 assert(pRun->member.run.nFlags & MERF_ENDPARA);
623 assert(pRun->type != diRun && pRun->type != diParagraph);
626 } while (RUN_IS_HIDDEN(&pRun->member.run));
627 pCursor->pRun = pRun;
628 if (pRun->member.run.nFlags & MERF_ENDPARA)
629 pCursor->nOffset = 0;
631 pCursor->nOffset = pRun->member.run.strText->nLen;
634 if (pCursor->nOffset)
635 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
640 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
642 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
644 if (new_ofs < pRun->member.run.strText->nLen)
646 pCursor->nOffset = new_ofs;
651 pRun = ME_FindItemFwd(pRun, diRun);
652 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
655 pCursor->pRun = pRun;
656 pCursor->nOffset = 0;
665 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
667 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
668 int nOffset = cursor->nOffset;
672 /* Backward movement */
675 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
676 nOffset, WB_MOVEWORDLEFT);
679 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
680 if (pOtherRun->type == diRun)
682 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
683 pOtherRun->member.run.strText->nLen - 1,
685 && !(pRun->member.run.nFlags & MERF_ENDPARA)
686 && !(cursor->pRun == pRun && cursor->nOffset == 0)
687 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
691 nOffset = pOtherRun->member.run.strText->nLen;
693 else if (pOtherRun->type == diParagraph)
695 if (cursor->pRun == pRun && cursor->nOffset == 0)
697 /* Paragraph breaks are treated as separate words */
698 if (pOtherRun->member.para.prev_para->type == diTextStart)
700 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
708 /* Forward movement */
709 BOOL last_delim = FALSE;
713 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
714 nOffset, WB_ISDELIMITER))
716 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
717 nOffset, WB_MOVEWORDRIGHT);
718 if (nOffset < pRun->member.run.strText->nLen)
720 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
721 if (pOtherRun->type == diRun)
723 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
724 nOffset - 1, WB_ISDELIMITER);
728 else if (pOtherRun->type == diParagraph)
730 if (cursor->pRun == pRun)
731 pRun = ME_FindItemFwd(pOtherRun, diRun);
737 if (cursor->pRun == pRun)
745 cursor->nOffset = nOffset;
751 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
753 /* pCursor[0] will be the start of the selection
754 * pCursor[1] is the other end of the selection range
755 * pCursor[2] and [3] are the selection anchors that are backed up
756 * so they are kept when the selection changes for drag selection.
759 editor->nSelectionType = selectionType;
760 switch(selectionType)
765 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
766 editor->pCursors[0] = editor->pCursors[1];
767 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
772 ME_DisplayItem *pItem;
773 ME_DIType fwdSearchType, backSearchType;
774 if (selectionType == stParagraph) {
775 backSearchType = diParagraph;
776 fwdSearchType = diParagraphOrEnd;
778 backSearchType = diStartRow;
779 fwdSearchType = diStartRowOrParagraphOrEnd;
781 pItem = ME_FindItemBack(editor->pCursors[0].pRun, backSearchType);
782 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
783 editor->pCursors[0].nOffset = 0;
785 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
787 if (pItem->type == diTextEnd)
788 editor->pCursors[1].pRun = ME_FindItemBack(pItem, diRun);
790 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
791 editor->pCursors[1].nOffset = 0;
795 /* Select everything with cursor anchored from the start of the text */
796 editor->nSelectionType = stDocument;
797 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
798 editor->pCursors[1].nOffset = 0;
799 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
800 editor->pCursors[0].nOffset = 0;
804 /* Store the anchor positions for extending the selection. */
805 editor->pCursors[2] = editor->pCursors[0];
806 editor->pCursors[3] = editor->pCursors[1];
810 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
812 ME_Cursor *pCursor = &editor->pCursors[nCursor];
813 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
814 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
817 static void ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
819 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
820 ME_DisplayItem *last = NULL;
827 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
829 assert(p->type == diParagraph);
830 if (y < p->member.para.nYPos + p->member.para.nHeight)
832 y -= p->member.para.nYPos;
833 p = ME_FindItemFwd(p, diStartRow);
838 for (; p != editor->pBuffer->pLast; )
841 assert(p->type == diStartRow);
842 if (y < p->member.row.nYPos + p->member.row.nHeight)
844 p = ME_FindItemFwd(p, diRun);
847 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
848 if (pp->type != diStartRow)
850 p = ME_FindItemFwd(p, diRun);
855 if (p == editor->pBuffer->pLast)
857 /* The position is below the last paragraph, so the last row will be used
858 * rather than the end of the text, so the x position will be used to
859 * determine the offset closest to the pixel position. */
860 p = ME_FindItemBack(p, diStartRow);
862 p = ME_FindItemFwd(p, diRun);
866 p = editor->pBuffer->pLast;
869 for (; p != editor->pBuffer->pLast; p = p->next)
874 rx = x - p->member.run.pt.x;
875 if (rx < p->member.run.nWidth)
878 assert(p->type == diRun);
879 if ((p->member.run.nFlags & MERF_ENDPARA) || rx < 0)
882 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
883 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
885 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
892 p = ME_FindItemFwd(p, diRun);
893 if (is_eol) *is_eol = 1;
894 rx = 0; /* FIXME not sure */
898 rx = 0; /* FIXME not sure */
905 result->pRun = ME_FindItemBack(p, diRun);
907 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
912 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
917 GetClientRect(editor->hWnd, &rc);
918 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
920 y += ME_GetYScrollPos(editor);
921 ME_FindPixelPos(editor, x, y, &cursor, NULL);
922 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
923 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
926 /* Extends the selection with a word, line, or paragraph selection type.
928 * The selection is anchored by editor->pCursors[2-3] such that the text
929 * between the anchors will remain selected, and one end will be extended.
931 * editor->pCursors[0] should have the position to extend the selection to
932 * before this function is called.
934 * Nothing will be done if editor->nSelectionType equals stPosition.
936 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
938 ME_Cursor tmp_cursor;
939 int curOfs, anchorStartOfs, anchorEndOfs;
940 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
942 curOfs = ME_GetCursorOfs(editor, 0);
943 anchorStartOfs = ME_GetCursorOfs(editor, 2);
944 anchorEndOfs = ME_GetCursorOfs(editor, 3);
946 tmp_cursor = editor->pCursors[0];
947 editor->pCursors[0] = editor->pCursors[2];
948 editor->pCursors[1] = editor->pCursors[3];
949 if (curOfs < anchorStartOfs)
951 /* Extend the left side of selection */
952 editor->pCursors[0] = tmp_cursor;
953 if (editor->nSelectionType == stWord)
954 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
957 ME_DisplayItem *pItem;
958 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
959 diStartRowOrParagraph:diParagraph);
960 pItem = ME_FindItemBack(editor->pCursors[0].pRun, searchType);
961 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
962 editor->pCursors[0].nOffset = 0;
965 else if (curOfs >= anchorEndOfs)
967 /* Extend the right side of selection */
968 editor->pCursors[1] = tmp_cursor;
969 if (editor->nSelectionType == stWord)
970 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
973 ME_DisplayItem *pItem;
974 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
975 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
976 pItem = ME_FindItemFwd(editor->pCursors[1].pRun, searchType);
977 if (pItem->type == diTextEnd)
978 editor->pCursors[1].pRun = ME_FindItemBack(pItem, diRun);
980 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
981 editor->pCursors[1].nOffset = 0;
986 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
988 ME_Cursor tmp_cursor;
989 int is_selection = 0;
992 editor->nUDArrowX = -1;
994 y += ME_GetYScrollPos(editor);
996 tmp_cursor = editor->pCursors[0];
997 is_selection = ME_IsSelection(editor);
998 is_shift = GetKeyState(VK_SHIFT) < 0;
1000 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
1002 if (x >= editor->selofs || is_shift)
1006 editor->pCursors[1] = editor->pCursors[0];
1008 if (x >= editor->selofs)
1009 ME_SelectByType(editor, stWord);
1011 ME_SelectByType(editor, stParagraph);
1012 } else if (clickNum % 2 == 0) {
1013 ME_SelectByType(editor, stWord);
1015 ME_SelectByType(editor, stParagraph);
1020 editor->nSelectionType = stPosition;
1021 editor->pCursors[1] = editor->pCursors[0];
1023 else if (!is_selection)
1025 editor->nSelectionType = stPosition;
1026 editor->pCursors[1] = tmp_cursor;
1028 else if (editor->nSelectionType != stPosition)
1030 ME_ExtendAnchorSelection(editor);
1036 ME_SelectByType(editor, stLine);
1037 } else if (clickNum % 2 == 0 || is_shift) {
1038 ME_SelectByType(editor, stParagraph);
1040 ME_SelectByType(editor, stDocument);
1043 ME_InvalidateSelection(editor);
1044 HideCaret(editor->hWnd);
1045 ME_ShowCaret(editor);
1046 ME_ClearTempStyle(editor);
1047 ME_SendSelChange(editor);
1050 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1052 ME_Cursor tmp_cursor;
1054 if (editor->nSelectionType == stDocument)
1056 y += ME_GetYScrollPos(editor);
1058 tmp_cursor = editor->pCursors[0];
1059 /* FIXME: do something with the return value of ME_FindPixelPos */
1060 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
1062 ME_InvalidateSelection(editor);
1063 editor->pCursors[0] = tmp_cursor;
1064 ME_ExtendAnchorSelection(editor);
1066 if (editor->nSelectionType != stPosition &&
1067 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1069 /* The scroll the cursor towards the other end, since it was the one
1070 * extended by ME_ExtendAnchorSelection
1072 ME_Cursor tmpCursor = editor->pCursors[0];
1073 editor->pCursors[0] = editor->pCursors[1];
1074 editor->pCursors[1] = tmpCursor;
1075 SendMessageW(editor->hWnd, EM_SCROLLCARET, 0, 0);
1076 editor->pCursors[1] = editor->pCursors[0];
1077 editor->pCursors[0] = tmpCursor;
1079 SendMessageW(editor->hWnd, EM_SCROLLCARET, 0, 0);
1082 ME_InvalidateSelection(editor);
1083 HideCaret(editor->hWnd);
1084 ME_ShowCaret(editor);
1085 ME_SendSelChange(editor);
1088 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
1089 int x, int *pOffset, int *pbCaretAtEnd)
1091 ME_DisplayItem *pNext, *pLastRun;
1092 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
1093 assert(pNext->type == diRun);
1095 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
1096 if (pOffset) *pOffset = 0;
1098 int run_x = pNext->member.run.pt.x;
1099 int width = pNext->member.run.nWidth;
1104 if (x >= run_x && x < run_x+width)
1106 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
1107 ME_String *s = pNext->member.run.strText;
1115 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
1116 } while(pNext && pNext->type == diRun);
1118 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
1120 pNext = ME_FindItemFwd(pNext, diRun);
1121 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
1128 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1130 ME_DisplayItem *pRun = pCursor->pRun;
1133 if (editor->nUDArrowX != -1)
1134 x = editor->nUDArrowX;
1136 if (editor->bCaretAtEnd)
1138 pRun = ME_FindItemBack(pRun, diRun);
1140 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1143 x = pRun->member.run.pt.x;
1144 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
1146 editor->nUDArrowX = x;
1153 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1155 ME_DisplayItem *pRun = pCursor->pRun;
1156 ME_DisplayItem *pItem;
1157 int x = ME_GetXForArrow(editor, pCursor);
1159 if (editor->bCaretAtEnd && !pCursor->nOffset)
1160 pRun = ME_FindItemBack(pRun, diRun);
1165 /* start of this row */
1166 pItem = ME_FindItemBack(pRun, diStartRow);
1168 /* start of the previous row */
1169 pItem = ME_FindItemBack(pItem, diStartRow);
1173 /* start of the next row */
1174 pItem = ME_FindItemFwd(pRun, diStartRow);
1175 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1180 /* row not found - ignore */
1183 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1184 assert(pCursor->pRun);
1185 assert(pCursor->pRun->type == diRun);
1189 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1191 ME_DisplayItem *pRun = pCursor->pRun;
1192 ME_DisplayItem *pLast, *p;
1193 int x, y, ys, yd, yp, yprev;
1194 ME_Cursor tmp_curs = *pCursor;
1196 x = ME_GetXForArrow(editor, pCursor);
1197 if (!pCursor->nOffset && editor->bCaretAtEnd)
1198 pRun = ME_FindItemBack(pRun, diRun);
1200 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1201 assert(p->type == diStartRow);
1202 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1203 yprev = ys = y = yp + p->member.row.nYPos;
1204 yd = y - editor->sizeWindow.cy;
1208 p = ME_FindItemBack(p, diStartRowOrParagraph);
1211 if (p->type == diParagraph) { /* crossing paragraphs */
1212 if (p->member.para.prev_para == NULL)
1214 yp = p->member.para.prev_para->member.para.nYPos;
1217 y = yp + p->member.row.nYPos;
1224 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1225 ME_UpdateSelection(editor, &tmp_curs);
1226 if (yprev < editor->sizeWindow.cy)
1228 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
1233 ME_ScrollUp(editor, ys-yprev);
1235 assert(pCursor->pRun);
1236 assert(pCursor->pRun->type == diRun);
1239 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1240 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1241 In such a situation, clicking the scrollbar restores its position back to the
1242 normal range (ie. sets it to (doclength-screenheight)). */
1244 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1246 ME_DisplayItem *pRun = pCursor->pRun;
1247 ME_DisplayItem *pLast, *p;
1248 int x, y, ys, yd, yp, yprev;
1249 ME_Cursor tmp_curs = *pCursor;
1251 x = ME_GetXForArrow(editor, pCursor);
1252 if (!pCursor->nOffset && editor->bCaretAtEnd)
1253 pRun = ME_FindItemBack(pRun, diRun);
1255 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1256 assert(p->type == diStartRow);
1257 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1258 yprev = ys = y = yp + p->member.row.nYPos;
1259 yd = y + editor->sizeWindow.cy;
1263 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1266 if (p->type == diParagraph) {
1267 yp = p->member.para.nYPos;
1270 y = yp + p->member.row.nYPos;
1277 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1278 ME_UpdateSelection(editor, &tmp_curs);
1279 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
1281 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1286 ME_ScrollUp(editor,ys-yprev);
1288 assert(pCursor->pRun);
1289 assert(pCursor->pRun->type == diRun);
1292 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1294 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1295 ME_WrapMarkedParagraphs(editor);
1297 ME_DisplayItem *pRun;
1298 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1299 pRow = ME_FindItemBack(pRow, diStartRow);
1303 pRun = ME_FindItemFwd(pRow, diRun);
1305 pCursor->pRun = pRun;
1306 pCursor->nOffset = 0;
1309 editor->bCaretAtEnd = FALSE;
1312 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1314 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1316 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1318 pCursor->pRun = pRun;
1319 pCursor->nOffset = 0;
1324 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1326 ME_DisplayItem *pRow;
1328 if (editor->bCaretAtEnd && !pCursor->nOffset)
1331 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1333 if (pRow->type == diStartRow) {
1334 /* FIXME WTF was I thinking about here ? */
1335 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1337 pCursor->pRun = pRun;
1338 pCursor->nOffset = 0;
1339 editor->bCaretAtEnd = 1;
1342 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1343 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1344 pCursor->nOffset = 0;
1345 editor->bCaretAtEnd = FALSE;
1348 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1350 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1352 p = ME_FindItemBack(p, diRun);
1354 assert(p->member.run.nFlags & MERF_ENDPARA);
1356 pCursor->nOffset = 0;
1357 editor->bCaretAtEnd = FALSE;
1360 BOOL ME_IsSelection(ME_TextEditor *editor)
1362 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1365 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1367 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1375 BOOL ME_UpdateSelection(ME_TextEditor *editor, const ME_Cursor *pTempCursor)
1377 ME_Cursor old_anchor = editor->pCursors[1];
1379 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1381 /* any selection was present ? if so, it's no more, repaint ! */
1382 editor->pCursors[1] = editor->pCursors[0];
1383 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1390 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1392 editor->pCursors[1] = *pTempCursor;
1401 void ME_DeleteSelection(ME_TextEditor *editor)
1404 ME_GetSelection(editor, &from, &to);
1405 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1408 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1410 return ME_GetInsertStyle(editor, 0);
1413 void ME_SendSelChange(ME_TextEditor *editor)
1417 if (!(editor->nEventMask & ENM_SELCHANGE))
1420 sc.nmhdr.hwndFrom = editor->hWnd;
1421 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1422 sc.nmhdr.code = EN_SELCHANGE;
1423 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1424 sc.seltyp = SEL_EMPTY;
1425 if (sc.chrg.cpMin != sc.chrg.cpMax)
1426 sc.seltyp |= SEL_TEXT;
1427 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1428 sc.seltyp |= SEL_MULTICHAR;
1429 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1430 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1431 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1432 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1433 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1435 ME_ClearTempStyle(editor);
1437 editor->notified_cr = sc.chrg;
1438 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1443 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1446 ME_Cursor *p = &editor->pCursors[nCursor];
1447 ME_Cursor tmp_curs = *p;
1448 BOOL success = FALSE;
1450 ME_CheckCharOffsets(editor);
1453 editor->bCaretAtEnd = 0;
1455 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1457 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1460 editor->bCaretAtEnd = 0;
1462 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1464 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1467 ME_MoveCursorLines(editor, &tmp_curs, -1);
1470 ME_MoveCursorLines(editor, &tmp_curs, +1);
1473 ME_ArrowPageUp(editor, &tmp_curs);
1476 ME_ArrowPageDown(editor, &tmp_curs);
1480 ME_ArrowCtrlHome(editor, &tmp_curs);
1482 ME_ArrowHome(editor, &tmp_curs);
1483 editor->bCaretAtEnd = 0;
1488 ME_ArrowCtrlEnd(editor, &tmp_curs);
1490 ME_ArrowEnd(editor, &tmp_curs);
1495 editor->pCursors[1] = tmp_curs;
1498 ME_InvalidateSelection(editor);
1500 HideCaret(editor->hWnd);
1501 ME_EnsureVisible(editor, tmp_curs.pRun);
1502 ME_ShowCaret(editor);
1503 ME_SendSelChange(editor);