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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
29 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs);
32 void ME_GetSelection(ME_TextEditor *editor, int *from, int *to)
34 *from = ME_GetCursorOfs(editor, 0);
35 *to = ME_GetCursorOfs(editor, 1);
45 int ME_GetTextLength(ME_TextEditor *editor)
47 return ME_CharOfsFromRunOfs(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun), 0);
51 int ME_GetTextLengthEx(ME_TextEditor *editor, GETTEXTLENGTHEX *how)
55 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
57 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
60 length = ME_GetTextLength(editor);
62 if (how->flags & GTL_USECRLF)
63 length += editor->nParagraphs;
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 void ME_SetSelection(ME_TextEditor *editor, int from, int to)
84 if (from == 0 && to == -1)
86 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
87 editor->pCursors[1].nOffset = 0;
88 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
89 editor->pCursors[0].nOffset = 0;
91 ME_ClearTempStyle(editor);
96 editor->pCursors[1] = editor->pCursors[0];
98 ME_ClearTempStyle(editor);
107 ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
108 ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);
113 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
114 int *x, int *y, int *height)
116 ME_DisplayItem *pCursorRun = pCursor->pRun;
117 ME_DisplayItem *pSizeRun = pCursor->pRun;
119 assert(!pCursor->nOffset || !editor->bCaretAtEnd);
120 assert(height && x && y);
122 if (pCursorRun->type == diRun) {
123 ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
126 HDC hDC = GetDC(editor->hWnd);
128 ME_DisplayItem *run = pCursorRun;
129 ME_DisplayItem *para;
132 ME_InitContext(&c, editor, hDC);
134 if (!pCursor->nOffset && !editor->bCaretAtEnd)
136 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrStartRow);
137 if (prev->type == diRun)
140 assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
141 para = ME_FindItemBack(row, diParagraph);
142 if (editor->bCaretAtEnd && !pCursor->nOffset &&
143 run == ME_FindItemFwd(row, diRun))
145 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
146 if (tmp->type == diRun)
148 row = ME_FindItemBack(tmp, diStartRow);
149 pSizeRun = run = tmp;
150 sz = ME_GetRunSize(&c, ¶->member.para, &run->member.run, ME_StrLen(run->member.run.strText));
153 if (pCursor->nOffset && !(run->member.run.nFlags & MERF_SKIPPED)) {
154 sz = ME_GetRunSize(&c, ¶->member.para, &run->member.run, pCursor->nOffset);
157 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
158 *x = run->member.run.pt.x + sz.cx;
159 *y = para->member.para.nYPos + row->member.row.nBaseline + pSizeRun->member.run.pt.y - pSizeRun->member.run.nAscent - ME_GetYScrollPos(editor);
161 ME_DestroyContext(&c);
162 ReleaseDC(editor->hWnd, hDC);
166 *height = 10; /* FIXME use global font */
173 ME_MoveCaret(ME_TextEditor *editor)
177 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
178 CreateCaret(editor->hWnd, NULL, 0, height);
183 void ME_ShowCaret(ME_TextEditor *ed)
189 void ME_HideCaret(ME_TextEditor *ed)
195 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
204 ME_CursorFromCharOfs(editor, nOfs, &c);
205 run = &c.pRun->member.run;
206 if (run->nFlags & MERF_ENDPARA) {
207 if (!ME_FindItemFwd(c.pRun, diParagraph))
211 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
212 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
213 ME_CheckCharOffsets(editor);
215 if (editor->bEmulateVersion10 && nChars)
222 int nIntendedChars = nChars;
223 int nCharsToDelete = nChars;
227 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
230 ME_StrRelPos(run->strText, loc, &nChars);
231 /* nChars is the number of characters that should be deleted from the
232 FOLLOWING runs (these AFTER cursor.pRun)
233 nCharsToDelete is a number of chars to delete from THIS run */
234 nCharsToDelete -= nChars;
235 shift -= nCharsToDelete;
236 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
237 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
238 debugstr_w(run->strText->szData), run->strText->nLen);
240 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
242 /* undo = reinsert whole run */
243 /* nOfs is a character offset (from the start of the document
244 to the current (deleted) run */
245 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
247 pUndo->di.member.run.nCharOfs = nOfs;
251 /* undo = reinsert partial run */
252 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
254 ME_DestroyString(pUndo->di.member.run.strText);
255 pUndo->di.member.run.nCharOfs = nOfs;
256 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
259 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
260 TRACE("Shift value: %d\n", shift);
261 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
263 /* update cursors (including c) */
264 for (i=-1; i<editor->nCursors; i++) {
265 ME_Cursor *pThisCur = editor->pCursors + i;
266 if (i == -1) pThisCur = &c;
267 if (pThisCur->pRun == cursor.pRun) {
268 if (pThisCur->nOffset > cursor.nOffset) {
269 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
270 pThisCur->nOffset = cursor.nOffset;
272 pThisCur->nOffset -= nCharsToDelete;
273 assert(pThisCur->nOffset >= 0);
274 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
276 if (pThisCur->nOffset == ME_StrVLen(run->strText))
278 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
279 assert(pThisCur->pRun->type == diRun);
280 pThisCur->nOffset = 0;
285 /* c = updated data now */
287 if (c.pRun == cursor.pRun)
288 ME_SkipAndPropagateCharOffset(c.pRun, shift);
290 ME_PropagateCharOffset(c.pRun, shift);
292 if (!ME_StrVLen(cursor.pRun->member.run.strText))
294 TRACE("Removing useless run\n");
295 ME_Remove(cursor.pRun);
296 ME_DestroyDisplayItem(cursor.pRun);
301 ME_CheckCharOffsets(editor);
308 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
311 assert(nCursor>=0 && nCursor<editor->nCursors);
312 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
315 static const WCHAR wszSpace[] = {' ', 0};
317 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
318 void ME_InsertGraphicsFromCursor(ME_TextEditor *editor, int nCursor)
320 ME_Cursor *pCursor = &editor->pCursors[nCursor];
321 ME_DisplayItem *pItem = NULL;
322 ME_DisplayItem *pNewRun = NULL;
323 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
327 if (ME_IsSelection(editor))
328 ME_DeleteSelection(editor);
330 pUndo = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
332 pUndo->nStart = pCursor->nOffset + pCursor->pRun->member.run.nCharOfs + ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs;
335 if (pCursor->nOffset)
337 ME_SplitRunSimple(editor, pCursor->pRun, pCursor->nOffset);
339 pItem = pCursor->pRun;
340 pNewRun = ME_MakeRun(pStyle, ME_MakeStringN(wszSpace, 1), MERF_GRAPHICS);
341 pNewRun->member.run.nCharOfs = pCursor->pRun->member.run.nCharOfs;
342 ME_InsertBefore(pCursor->pRun, pNewRun);
343 ME_PropagateCharOffset(pItem, 1);
344 ME_CheckCharOffsets(editor);
345 ME_SendSelChange(editor);
350 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
351 const WCHAR *str, int len, ME_Style *style,
354 ME_Cursor *p = &editor->pCursors[nCursor];
356 editor->bCaretAtEnd = FALSE;
358 assert(p->pRun->type == diRun);
360 ME_InsertRunAtCursor(editor, p, style, str, len, flags);
364 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
365 const WCHAR *str, int len, ME_Style *style)
372 /* FIXME really HERE ? */
373 if (ME_IsSelection(editor))
374 ME_DeleteSelection(editor);
376 assert(nCursor>=0 && nCursor<editor->nCursors);
382 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
383 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
385 if (pos-str < len && *pos == '\t') { /* handle tabs */
389 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
391 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
400 if (pos-str < len) { /* handle EOLs */
401 ME_DisplayItem *tp, *end_run;
404 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
405 p = &editor->pCursors[nCursor];
407 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
408 p = &editor->pCursors[nCursor];
410 tmp_style = ME_GetInsertStyle(editor, nCursor);
411 /* ME_SplitParagraph increases style refcount */
412 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
413 p->pRun = ME_FindItemFwd(tp, diRun);
414 end_run = ME_FindItemBack(tp, diRun);
415 ME_ReleaseStyle(end_run->member.run.style);
416 end_run->member.run.style = tmp_style;
418 if(pos-str < len && *pos =='\r')
420 if(pos-str < len && *pos =='\n')
428 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
435 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
437 ME_DisplayItem *pRun = pCursor->pRun;
441 if (!pCursor->nOffset)
444 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
451 if (pRun->member.para.prev_para->type == diTextStart)
453 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
454 /* every paragraph ought to have at least one run */
455 assert(pRun && pRun->type == diRun);
456 assert(pRun->member.run.nFlags & MERF_ENDPARA);
459 assert(pRun->type != diRun && pRun->type != diParagraph);
462 } while (RUN_IS_HIDDEN(&pRun->member.run));
463 pCursor->pRun = pRun;
464 if (pRun->member.run.nFlags & MERF_ENDPARA)
465 pCursor->nOffset = 0;
467 pCursor->nOffset = pRun->member.run.strText->nLen;
470 if (pCursor->nOffset)
471 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
476 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
478 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
480 if (new_ofs < pRun->member.run.strText->nLen)
482 pCursor->nOffset = new_ofs;
487 pRun = ME_FindItemFwd(pRun, diRun);
488 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
491 pCursor->pRun = pRun;
492 pCursor->nOffset = 0;
501 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
503 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
504 int nOffset = cursor->nOffset;
508 /* Backward movement */
511 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
512 nOffset, WB_MOVEWORDLEFT);
515 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
516 if (pOtherRun->type == diRun)
518 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
519 pOtherRun->member.run.strText->nLen - 1,
521 && !(pRun->member.run.nFlags & MERF_ENDPARA)
522 && !(cursor->pRun == pRun && cursor->nOffset == 0)
523 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
527 nOffset = pOtherRun->member.run.strText->nLen;
529 else if (pOtherRun->type == diParagraph)
531 if (cursor->pRun == pRun && cursor->nOffset == 0)
533 /* Paragraph breaks are treated as separate words */
534 if (pOtherRun->member.para.prev_para->type == diTextStart)
536 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
544 /* Forward movement */
545 BOOL last_delim = FALSE;
549 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
550 nOffset, WB_ISDELIMITER))
552 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
553 nOffset, WB_MOVEWORDRIGHT);
554 if (nOffset < pRun->member.run.strText->nLen)
556 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
557 if (pOtherRun->type == diRun)
559 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
560 nOffset - 1, WB_ISDELIMITER);
564 else if (pOtherRun->type == diParagraph)
566 if (cursor->pRun == pRun)
567 pRun = ME_FindItemFwd(pOtherRun, diRun);
573 if (cursor->pRun == pRun)
581 cursor->nOffset = nOffset;
587 ME_SelectWord(ME_TextEditor *editor)
589 if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
590 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
591 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
592 ME_InvalidateSelection(editor);
593 ME_SendSelChange(editor);
597 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
599 ME_Cursor *pCursor = &editor->pCursors[nCursor];
601 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
602 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
605 int ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
607 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
613 while(p != editor->pBuffer->pLast)
615 if (p->type == diParagraph)
617 int ry = y - p->member.para.nYPos;
620 result->pRun = ME_FindItemFwd(p, diRun);
624 if (ry >= p->member.para.nHeight)
626 p = p->member.para.next_para;
629 p = ME_FindItemFwd(p, diStartRow);
633 if (p->type == diStartRow)
635 int ry = y - p->member.row.nYPos;
638 if (ry >= p->member.row.nHeight)
640 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
641 if (p->type != diStartRow)
645 p = ME_FindItemFwd(p, diRun);
648 if (p->type == diRun)
651 rx = x - p->member.run.pt.x;
654 if (rx >= p->member.run.nWidth) /* not this run yet... find next item */
659 if (p->type == diRun)
661 rx = x - p->member.run.pt.x;
662 goto continue_search;
664 if (p->type == diStartRow)
666 p = ME_FindItemFwd(p, diRun);
669 rx = 0; /* FIXME not sure */
672 if (p->type == diParagraph || p->type == diTextEnd)
674 rx = 0; /* FIXME not sure */
682 if (p->member.run.nFlags & MERF_ENDPARA)
685 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
686 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
688 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
697 result->pRun = ME_FindItemBack(p, diRun);
699 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
705 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
710 GetClientRect(editor->hWnd, &rc);
711 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
713 ME_FindPixelPos(editor, x, y, &cursor, NULL);
714 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
715 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
719 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
721 ME_Cursor tmp_cursor;
722 int is_selection = 0;
724 editor->nUDArrowX = -1;
726 y += ME_GetYScrollPos(editor);
728 tmp_cursor = editor->pCursors[0];
729 is_selection = ME_IsSelection(editor);
731 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
733 if (GetKeyState(VK_SHIFT)>=0)
735 editor->pCursors[1] = editor->pCursors[0];
740 editor->pCursors[1] = tmp_cursor;
744 ME_InvalidateSelection(editor);
745 HideCaret(editor->hWnd);
746 ME_MoveCaret(editor);
747 ShowCaret(editor->hWnd);
748 ME_ClearTempStyle(editor);
749 ME_SendSelChange(editor);
752 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
754 ME_Cursor tmp_cursor;
756 y += ME_GetYScrollPos(editor);
758 tmp_cursor = editor->pCursors[0];
759 if (!ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd))
762 if (tmp_cursor.pRun == editor->pCursors[0].pRun &&
763 tmp_cursor.nOffset == editor->pCursors[0].nOffset)
766 ME_InvalidateSelection(editor);
767 editor->pCursors[0] = tmp_cursor;
768 HideCaret(editor->hWnd);
769 ME_MoveCaret(editor);
770 ME_InvalidateSelection(editor);
771 ShowCaret(editor->hWnd);
772 ME_SendSelChange(editor);
775 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
776 int x, int *pOffset, int *pbCaretAtEnd)
778 ME_DisplayItem *pNext, *pLastRun;
779 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
780 assert(pNext->type == diRun);
782 *pbCaretAtEnd = FALSE;
784 int run_x = pNext->member.run.pt.x;
785 int width = pNext->member.run.nWidth;
788 if (pOffset) *pOffset = 0;
791 if (x >= run_x && x < run_x+width)
793 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
794 ME_String *s = pNext->member.run.strText;
802 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
803 } while(pNext && pNext->type == diRun);
805 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
807 pNext = ME_FindItemFwd(pNext, diRun);
808 if (pbCaretAtEnd) *pbCaretAtEnd = 1;
809 if (pOffset) *pOffset = 0;
812 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
813 if (pOffset) *pOffset = 0;
818 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
820 ME_DisplayItem *pRun = pCursor->pRun;
823 if (editor->nUDArrowX != -1)
824 x = editor->nUDArrowX;
826 if (editor->bCaretAtEnd)
828 pRun = ME_FindItemBack(pRun, diRun);
830 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
833 x = pRun->member.run.pt.x;
834 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
836 editor->nUDArrowX = x;
843 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
845 ME_DisplayItem *pRun = pCursor->pRun;
846 ME_DisplayItem *pItem;
847 int x = ME_GetXForArrow(editor, pCursor);
849 if (editor->bCaretAtEnd && !pCursor->nOffset)
850 pRun = ME_FindItemBack(pRun, diRun);
855 /* start of this row */
856 pItem = ME_FindItemBack(pRun, diStartRow);
858 /* start of the previous row */
859 pItem = ME_FindItemBack(pItem, diStartRow);
863 /* start of the next row */
864 pItem = ME_FindItemFwd(pRun, diStartRow);
865 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
870 /* row not found - ignore */
873 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
874 assert(pCursor->pRun);
875 assert(pCursor->pRun->type == diRun);
879 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
881 ME_DisplayItem *pRun = pCursor->pRun;
882 ME_DisplayItem *pLast, *p;
883 int x, y, ys, yd, yp, yprev;
884 ME_Cursor tmp_curs = *pCursor;
886 x = ME_GetXForArrow(editor, pCursor);
887 if (!pCursor->nOffset && editor->bCaretAtEnd)
888 pRun = ME_FindItemBack(pRun, diRun);
890 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
891 assert(p->type == diStartRow);
892 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
893 yprev = ys = y = yp + p->member.row.nYPos;
894 yd = y - editor->sizeWindow.cy;
898 p = ME_FindItemBack(p, diStartRowOrParagraph);
901 if (p->type == diParagraph) { /* crossing paragraphs */
902 if (p->member.para.prev_para == NULL)
904 yp = p->member.para.prev_para->member.para.nYPos;
907 y = yp + p->member.row.nYPos;
914 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
915 ME_UpdateSelection(editor, &tmp_curs);
916 if (yprev < editor->sizeWindow.cy)
918 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
922 ME_Scroll(editor, 0, ys-yprev);
925 assert(pCursor->pRun);
926 assert(pCursor->pRun->type == diRun);
929 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
930 of pixels, even if it makes the scroll bar position exceed its normal maximum.
931 In such a situation, clicking the scrollbar restores its position back to the
932 normal range (ie. sets it to (doclength-screenheight)). */
934 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
936 ME_DisplayItem *pRun = pCursor->pRun;
937 ME_DisplayItem *pLast, *p;
938 int x, y, ys, yd, yp, yprev;
939 ME_Cursor tmp_curs = *pCursor;
941 x = ME_GetXForArrow(editor, pCursor);
942 if (!pCursor->nOffset && editor->bCaretAtEnd)
943 pRun = ME_FindItemBack(pRun, diRun);
945 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
946 assert(p->type == diStartRow);
947 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
948 yprev = ys = y = yp + p->member.row.nYPos;
949 yd = y + editor->sizeWindow.cy;
953 p = ME_FindItemFwd(p, diStartRowOrParagraph);
956 if (p->type == diParagraph) {
957 yp = p->member.para.nYPos;
960 y = yp + p->member.row.nYPos;
967 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
968 ME_UpdateSelection(editor, &tmp_curs);
969 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
971 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
975 ME_Scroll(editor, 0, ys-yprev);
978 assert(pCursor->pRun);
979 assert(pCursor->pRun->type == diRun);
982 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
984 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
986 ME_DisplayItem *pRun;
987 if (editor->bCaretAtEnd && !pCursor->nOffset) {
988 pRow = ME_FindItemBack(pRow, diStartRow);
992 pRun = ME_FindItemFwd(pRow, diRun);
994 pCursor->pRun = pRun;
995 pCursor->nOffset = 0;
998 editor->bCaretAtEnd = FALSE;
1001 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1003 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1005 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1007 pCursor->pRun = pRun;
1008 pCursor->nOffset = 0;
1013 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1015 ME_DisplayItem *pRow;
1017 if (editor->bCaretAtEnd && !pCursor->nOffset)
1020 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1022 if (pRow->type == diStartRow) {
1023 /* FIXME WTF was I thinking about here ? */
1024 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1026 pCursor->pRun = pRun;
1027 pCursor->nOffset = 0;
1028 editor->bCaretAtEnd = 1;
1031 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1032 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1033 pCursor->nOffset = 0;
1034 editor->bCaretAtEnd = FALSE;
1037 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1039 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1041 p = ME_FindItemBack(p, diRun);
1043 assert(p->member.run.nFlags & MERF_ENDPARA);
1045 pCursor->nOffset = 0;
1046 editor->bCaretAtEnd = FALSE;
1049 BOOL ME_IsSelection(ME_TextEditor *editor)
1051 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1054 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1056 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1064 BOOL ME_UpdateSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
1066 ME_Cursor old_anchor = editor->pCursors[1];
1068 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1070 /* any selection was present ? if so, it's no more, repaint ! */
1071 editor->pCursors[1] = editor->pCursors[0];
1072 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1079 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1081 editor->pCursors[1] = *pTempCursor;
1090 void ME_DeleteSelection(ME_TextEditor *editor)
1093 ME_GetSelection(editor, &from, &to);
1094 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1097 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1103 ME_GetSelection(editor, &from, &to);
1104 ME_CursorFromCharOfs(editor, from, &c);
1106 style = c.pRun->member.run.style;
1107 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1110 style = ME_GetInsertStyle(editor, 0);
1114 void ME_SendSelChange(ME_TextEditor *editor)
1118 ME_ClearTempStyle(editor);
1120 if (!(editor->nEventMask & ENM_SELCHANGE))
1123 sc.nmhdr.hwndFrom = editor->hWnd;
1124 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1125 sc.nmhdr.code = EN_SELCHANGE;
1126 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1127 sc.seltyp = SEL_EMPTY;
1128 if (sc.chrg.cpMin != sc.chrg.cpMax)
1129 sc.seltyp |= SEL_TEXT;
1130 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1131 sc.seltyp |= SEL_MULTICHAR;
1132 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1137 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1140 ME_Cursor *p = &editor->pCursors[nCursor];
1141 ME_Cursor tmp_curs = *p;
1142 BOOL success = FALSE;
1144 if (ME_IsSelection(editor) && !extend)
1145 ME_InvalidateSelection(editor);
1147 editor->nUDArrowX = -1;
1150 editor->bCaretAtEnd = 0;
1152 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1154 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1157 editor->bCaretAtEnd = 0;
1159 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1161 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1164 ME_MoveCursorLines(editor, &tmp_curs, -1);
1167 ME_MoveCursorLines(editor, &tmp_curs, +1);
1170 ME_ArrowPageUp(editor, &tmp_curs);
1173 ME_ArrowPageDown(editor, &tmp_curs);
1177 ME_ArrowCtrlHome(editor, &tmp_curs);
1179 ME_ArrowHome(editor, &tmp_curs);
1180 editor->bCaretAtEnd = 0;
1185 ME_ArrowCtrlEnd(editor, &tmp_curs);
1187 ME_ArrowEnd(editor, &tmp_curs);
1192 editor->pCursors[1] = tmp_curs;
1195 if (ME_IsSelection(editor))
1196 ME_InvalidateSelection(editor);
1197 HideCaret(editor->hWnd);
1198 ME_EnsureVisible(editor, tmp_curs.pRun);
1199 ME_ShowCaret(editor);
1200 ME_SendSelChange(editor);