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;
441 if (!pCursor->nOffset)
443 pRun = ME_FindItemBack(pCursor->pRun, diRunOrParagraph);
448 pCursor->pRun = pRun;
449 pCursor->nOffset = pRun->member.run.strText->nLen;
452 if (pRun->member.para.prev_para->type == diTextStart)
454 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
455 /* every paragraph ought to have at least one run */
456 assert(pRun && pRun->type == diRun);
457 assert(pRun->member.run.nFlags & MERF_ENDPARA);
458 pCursor->pRun = pRun;
459 pCursor->nOffset = 0;
462 assert(pRun->type != diRun && pRun->type != diParagraph);
467 if (pCursor->nOffset)
468 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
473 if (!(pCursor->pRun->member.run.nFlags & MERF_ENDPARA))
475 int new_ofs = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
477 if (new_ofs < pCursor->pRun->member.run.strText->nLen)
479 pCursor->nOffset = new_ofs;
483 pRun = ME_FindItemFwd(pCursor->pRun, diRun);
486 pCursor->pRun = pRun;
487 pCursor->nOffset = 0;
496 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
498 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
499 int nOffset = cursor->nOffset;
503 /* Backward movement */
506 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
507 nOffset, WB_MOVEWORDLEFT);
510 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
511 if (pOtherRun->type == diRun)
513 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
514 pOtherRun->member.run.strText->nLen - 1,
516 && !(pRun->member.run.nFlags & MERF_ENDPARA)
517 && !(cursor->pRun == pRun && cursor->nOffset == 0)
518 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
522 nOffset = pOtherRun->member.run.strText->nLen;
524 else if (pOtherRun->type == diParagraph)
526 if (cursor->pRun == pRun && cursor->nOffset == 0)
528 /* Paragraph breaks are treated as separate words */
529 if (pOtherRun->member.para.prev_para->type == diTextStart)
531 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
539 /* Forward movement */
540 BOOL last_delim = FALSE;
544 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
545 nOffset, WB_ISDELIMITER))
547 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
548 nOffset, WB_MOVEWORDRIGHT);
549 if (nOffset < pRun->member.run.strText->nLen)
551 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
552 if (pOtherRun->type == diRun)
554 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
555 nOffset - 1, WB_ISDELIMITER);
559 else if (pOtherRun->type == diParagraph)
561 if (cursor->pRun == pRun)
562 pRun = ME_FindItemFwd(pOtherRun, diRun);
568 if (cursor->pRun == pRun)
576 cursor->nOffset = nOffset;
582 ME_SelectWord(ME_TextEditor *editor)
584 if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
585 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
586 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
587 ME_InvalidateSelection(editor);
588 ME_SendSelChange(editor);
592 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
594 ME_Cursor *pCursor = &editor->pCursors[nCursor];
596 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
597 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
600 int ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
602 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
608 while(p != editor->pBuffer->pLast)
610 if (p->type == diParagraph)
612 int ry = y - p->member.para.nYPos;
615 result->pRun = ME_FindItemFwd(p, diRun);
619 if (ry >= p->member.para.nHeight)
621 p = p->member.para.next_para;
624 p = ME_FindItemFwd(p, diStartRow);
628 if (p->type == diStartRow)
630 int ry = y - p->member.row.nYPos;
633 if (ry >= p->member.row.nHeight)
635 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
636 if (p->type != diStartRow)
640 p = ME_FindItemFwd(p, diRun);
643 if (p->type == diRun)
646 rx = x - p->member.run.pt.x;
649 if (rx >= p->member.run.nWidth) /* not this run yet... find next item */
654 if (p->type == diRun)
656 rx = x - p->member.run.pt.x;
657 goto continue_search;
659 if (p->type == diStartRow)
661 p = ME_FindItemFwd(p, diRun);
664 rx = 0; /* FIXME not sure */
667 if (p->type == diParagraph || p->type == diTextEnd)
669 rx = 0; /* FIXME not sure */
677 if (p->member.run.nFlags & MERF_ENDPARA)
680 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
681 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
683 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
692 result->pRun = ME_FindItemBack(p, diRun);
694 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
700 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
705 GetClientRect(editor->hWnd, &rc);
706 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
708 ME_FindPixelPos(editor, x, y, &cursor, NULL);
709 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
710 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
714 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
716 ME_Cursor tmp_cursor;
717 int is_selection = 0;
719 editor->nUDArrowX = -1;
721 y += ME_GetYScrollPos(editor);
723 tmp_cursor = editor->pCursors[0];
724 is_selection = ME_IsSelection(editor);
726 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
728 if (GetKeyState(VK_SHIFT)>=0)
730 editor->pCursors[1] = editor->pCursors[0];
735 editor->pCursors[1] = tmp_cursor;
739 ME_InvalidateSelection(editor);
740 HideCaret(editor->hWnd);
741 ME_MoveCaret(editor);
742 ShowCaret(editor->hWnd);
743 ME_ClearTempStyle(editor);
744 ME_SendSelChange(editor);
747 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
749 ME_Cursor tmp_cursor;
751 y += ME_GetYScrollPos(editor);
753 tmp_cursor = editor->pCursors[0];
754 if (!ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd))
757 if (tmp_cursor.pRun == editor->pCursors[0].pRun &&
758 tmp_cursor.nOffset == editor->pCursors[0].nOffset)
761 HideCaret(editor->hWnd);
762 ME_InvalidateSelection(editor);
763 ME_MoveCaret(editor);
764 ShowCaret(editor->hWnd);
765 ME_SendSelChange(editor);
768 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
769 int x, int *pOffset, int *pbCaretAtEnd)
771 ME_DisplayItem *pNext, *pLastRun;
772 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
773 assert(pNext->type == diRun);
775 *pbCaretAtEnd = FALSE;
777 int run_x = pNext->member.run.pt.x;
778 int width = pNext->member.run.nWidth;
781 if (pOffset) *pOffset = 0;
784 if (x >= run_x && x < run_x+width)
786 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
787 ME_String *s = pNext->member.run.strText;
795 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
796 } while(pNext && pNext->type == diRun);
798 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
800 pNext = ME_FindItemFwd(pNext, diRun);
801 if (pbCaretAtEnd) *pbCaretAtEnd = 1;
802 if (pOffset) *pOffset = 0;
805 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
806 if (pOffset) *pOffset = 0;
811 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
813 ME_DisplayItem *pRun = pCursor->pRun;
816 if (editor->nUDArrowX != -1)
817 x = editor->nUDArrowX;
819 if (editor->bCaretAtEnd)
821 pRun = ME_FindItemBack(pRun, diRun);
823 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
826 x = pRun->member.run.pt.x;
827 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
829 editor->nUDArrowX = x;
836 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
838 ME_DisplayItem *pRun = pCursor->pRun;
839 ME_DisplayItem *pItem;
840 int x = ME_GetXForArrow(editor, pCursor);
842 if (editor->bCaretAtEnd && !pCursor->nOffset)
843 pRun = ME_FindItemBack(pRun, diRun);
848 /* start of this row */
849 pItem = ME_FindItemBack(pRun, diStartRow);
851 /* start of the previous row */
852 pItem = ME_FindItemBack(pItem, diStartRow);
856 /* start of the next row */
857 pItem = ME_FindItemFwd(pRun, diStartRow);
858 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
863 /* row not found - ignore */
866 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
867 assert(pCursor->pRun);
868 assert(pCursor->pRun->type == diRun);
872 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
874 ME_DisplayItem *pRun = pCursor->pRun;
875 ME_DisplayItem *pLast, *p;
876 int x, y, ys, yd, yp, yprev;
877 ME_Cursor tmp_curs = *pCursor;
879 x = ME_GetXForArrow(editor, pCursor);
880 if (!pCursor->nOffset && editor->bCaretAtEnd)
881 pRun = ME_FindItemBack(pRun, diRun);
883 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
884 assert(p->type == diStartRow);
885 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
886 yprev = ys = y = yp + p->member.row.nYPos;
887 yd = y - editor->sizeWindow.cy;
891 p = ME_FindItemBack(p, diStartRowOrParagraph);
894 if (p->type == diParagraph) { /* crossing paragraphs */
895 if (p->member.para.prev_para == NULL)
897 yp = p->member.para.prev_para->member.para.nYPos;
900 y = yp + p->member.row.nYPos;
907 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
908 ME_UpdateSelection(editor, &tmp_curs);
909 if (yprev < editor->sizeWindow.cy)
911 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
915 ME_Scroll(editor, 0, ys-yprev);
918 assert(pCursor->pRun);
919 assert(pCursor->pRun->type == diRun);
922 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
923 of pixels, even if it makes the scroll bar position exceed its normal maximum.
924 In such a situation, clicking the scrollbar restores its position back to the
925 normal range (ie. sets it to (doclength-screenheight)). */
927 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
929 ME_DisplayItem *pRun = pCursor->pRun;
930 ME_DisplayItem *pLast, *p;
931 int x, y, ys, yd, yp, yprev;
932 ME_Cursor tmp_curs = *pCursor;
934 x = ME_GetXForArrow(editor, pCursor);
935 if (!pCursor->nOffset && editor->bCaretAtEnd)
936 pRun = ME_FindItemBack(pRun, diRun);
938 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
939 assert(p->type == diStartRow);
940 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
941 yprev = ys = y = yp + p->member.row.nYPos;
942 yd = y + editor->sizeWindow.cy;
946 p = ME_FindItemFwd(p, diStartRowOrParagraph);
949 if (p->type == diParagraph) {
950 yp = p->member.para.nYPos;
953 y = yp + p->member.row.nYPos;
960 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
961 ME_UpdateSelection(editor, &tmp_curs);
962 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
964 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
968 ME_Scroll(editor, 0, ys-yprev);
971 assert(pCursor->pRun);
972 assert(pCursor->pRun->type == diRun);
975 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
977 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
979 ME_DisplayItem *pRun;
980 if (editor->bCaretAtEnd && !pCursor->nOffset) {
981 pRow = ME_FindItemBack(pRow, diStartRow);
985 pRun = ME_FindItemFwd(pRow, diRun);
987 pCursor->pRun = pRun;
988 pCursor->nOffset = 0;
991 editor->bCaretAtEnd = FALSE;
994 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
996 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
998 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1000 pCursor->pRun = pRun;
1001 pCursor->nOffset = 0;
1006 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1008 ME_DisplayItem *pRow;
1010 if (editor->bCaretAtEnd && !pCursor->nOffset)
1013 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1015 if (pRow->type == diStartRow) {
1016 /* FIXME WTF was I thinking about here ? */
1017 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1019 pCursor->pRun = pRun;
1020 pCursor->nOffset = 0;
1021 editor->bCaretAtEnd = 1;
1024 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1025 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1026 pCursor->nOffset = 0;
1027 editor->bCaretAtEnd = FALSE;
1030 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1032 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1034 p = ME_FindItemBack(p, diRun);
1036 assert(p->member.run.nFlags & MERF_ENDPARA);
1038 pCursor->nOffset = 0;
1039 editor->bCaretAtEnd = FALSE;
1042 BOOL ME_IsSelection(ME_TextEditor *editor)
1044 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1047 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1049 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1057 BOOL ME_UpdateSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
1059 ME_Cursor old_anchor = editor->pCursors[1];
1061 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1063 /* any selection was present ? if so, it's no more, repaint ! */
1064 editor->pCursors[1] = editor->pCursors[0];
1065 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1072 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1074 editor->pCursors[1] = *pTempCursor;
1083 void ME_DeleteSelection(ME_TextEditor *editor)
1086 ME_GetSelection(editor, &from, &to);
1087 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1090 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1096 ME_GetSelection(editor, &from, &to);
1097 ME_CursorFromCharOfs(editor, from, &c);
1099 style = c.pRun->member.run.style;
1100 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1103 style = ME_GetInsertStyle(editor, 0);
1107 void ME_SendSelChange(ME_TextEditor *editor)
1111 ME_ClearTempStyle(editor);
1113 if (!(editor->nEventMask & ENM_SELCHANGE))
1116 sc.nmhdr.hwndFrom = editor->hWnd;
1117 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1118 sc.nmhdr.code = EN_SELCHANGE;
1119 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1120 sc.seltyp = SEL_EMPTY;
1121 if (sc.chrg.cpMin != sc.chrg.cpMax)
1122 sc.seltyp |= SEL_TEXT;
1123 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1124 sc.seltyp |= SEL_MULTICHAR;
1125 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1130 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1133 ME_Cursor *p = &editor->pCursors[nCursor];
1134 ME_Cursor tmp_curs = *p;
1135 BOOL success = FALSE;
1137 if (ME_IsSelection(editor) && !extend)
1138 ME_InvalidateSelection(editor);
1140 editor->nUDArrowX = -1;
1143 editor->bCaretAtEnd = 0;
1145 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1147 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1150 editor->bCaretAtEnd = 0;
1152 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1154 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1157 ME_MoveCursorLines(editor, &tmp_curs, -1);
1160 ME_MoveCursorLines(editor, &tmp_curs, +1);
1163 ME_ArrowPageUp(editor, &tmp_curs);
1166 ME_ArrowPageDown(editor, &tmp_curs);
1170 ME_ArrowCtrlHome(editor, &tmp_curs);
1172 ME_ArrowHome(editor, &tmp_curs);
1173 editor->bCaretAtEnd = 0;
1178 ME_ArrowCtrlEnd(editor, &tmp_curs);
1180 ME_ArrowEnd(editor, &tmp_curs);
1185 editor->pCursors[1] = tmp_curs;
1188 if (ME_IsSelection(editor))
1189 ME_InvalidateSelection(editor);
1190 HideCaret(editor->hWnd);
1191 ME_EnsureVisible(editor, tmp_curs.pRun);
1192 ME_ShowCaret(editor);
1193 ME_SendSelChange(editor);