2 * RichEdit - Caret and selection functions.
4 * Copyright 2004 by Krzysztof Foltman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26 void ME_GetSelection(ME_TextEditor *editor, int *from, int *to)
28 *from = ME_GetCursorOfs(editor, 0);
29 *to = ME_GetCursorOfs(editor, 1);
39 int ME_GetTextLength(ME_TextEditor *editor)
41 return ME_CharOfsFromRunOfs(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun), 0);
45 int ME_GetTextLengthEx(ME_TextEditor *editor, GETTEXTLENGTHEX *how)
49 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
51 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
54 length = ME_GetTextLength(editor);
56 if (how->flags & GTL_USECRLF)
57 length += editor->nParagraphs;
59 if (how->flags & GTL_NUMBYTES)
63 if (how->codepage == 1200)
65 if (how->flags & GTL_PRECISE)
66 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
67 if (GetCPInfo(how->codepage, &cpinfo))
68 return length * cpinfo.MaxCharSize;
69 ERR("Invalid codepage %u\n", how->codepage);
76 void ME_SetSelection(ME_TextEditor *editor, int from, int to)
78 if (from == 0 && to == -1)
80 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
81 editor->pCursors[1].nOffset = 0;
82 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
83 editor->pCursors[0].nOffset = 0;
85 ME_ClearTempStyle(editor);
90 editor->pCursors[1] = editor->pCursors[0];
92 ME_ClearTempStyle(editor);
101 ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
102 ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);
105 void ME_MoveCaret(ME_TextEditor *editor)
107 HDC hDC = GetDC(editor->hWnd);
110 ME_Cursor *pCursor = &editor->pCursors[0];
111 ME_DisplayItem *pCursorRun = pCursor->pRun;
112 ME_DisplayItem *pSizeRun = pCursor->pRun;
114 ME_InitContext(&c, editor, hDC);
115 assert(!pCursor->nOffset || !editor->bCaretAtEnd);
117 if (pCursorRun->type == diRun) {
118 ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
120 ME_DisplayItem *run = pCursorRun;
121 ME_DisplayItem *para;
123 if (!pCursor->nOffset && !editor->bCaretAtEnd)
125 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrStartRow);
126 if (prev->type == diRun)
129 assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
130 para = ME_FindItemBack(row, diParagraph);
131 if (editor->bCaretAtEnd && !pCursor->nOffset &&
132 run == ME_FindItemFwd(row, diRun))
134 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
135 if (tmp->type == diRun)
137 row = ME_FindItemBack(tmp, diStartRow);
138 pSizeRun = run = tmp;
139 sz = ME_GetRunSize(&c, ¶->member.para, &run->member.run, ME_StrLen(run->member.run.strText));
142 if (pCursor->nOffset && !(run->member.run.nFlags & MERF_SKIPPED)) {
143 sz = ME_GetRunSize(&c, ¶->member.para, &run->member.run, pCursor->nOffset);
145 CreateCaret(editor->hWnd, NULL, 0, pSizeRun->member.run.nAscent+pSizeRun->member.run.nDescent);
146 SetCaretPos(run->member.run.pt.x+sz.cx,
147 para->member.para.nYPos+row->member.row.nBaseline+pSizeRun->member.run.pt.y-pSizeRun->member.run.nAscent-ME_GetYScrollPos(editor));
149 assert(0 == "Wrapped paragraph run without a row?");
150 CreateCaret(editor->hWnd, NULL, 0, 10);
155 assert(0 == "Cursor not on a run");
156 CreateCaret(editor->hWnd, NULL, 0, 10); /* FIXME use global font */
159 ME_DestroyContext(&c);
160 ReleaseDC(editor->hWnd, hDC);
163 void ME_ShowCaret(ME_TextEditor *ed)
169 void ME_HideCaret(ME_TextEditor *ed)
175 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
184 ME_CursorFromCharOfs(editor, nOfs, &c);
185 run = &c.pRun->member.run;
186 if (run->nFlags & MERF_ENDPARA) {
187 if (!ME_FindItemFwd(c.pRun, diParagraph))
191 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
192 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
193 ME_CheckCharOffsets(editor);
195 if (editor->bEmulateVersion10 && nChars)
202 int nIntendedChars = nChars;
203 int nCharsToDelete = nChars;
207 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
210 ME_StrRelPos(run->strText, loc, &nChars);
211 /* nChars is the number of characters that should be deleted from the
212 FOLLOWING runs (these AFTER cursor.pRun)
213 nCharsToDelete is a number of chars to delete from THIS run */
214 nCharsToDelete -= nChars;
215 shift -= nCharsToDelete;
216 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
217 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
218 debugstr_w(run->strText->szData), run->strText->nLen);
220 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
222 /* undo = reinsert whole run */
223 /* nOfs is a character offset (from the start of the document
224 to the current (deleted) run */
225 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
227 pUndo->di.member.run.nCharOfs = nOfs;
231 /* undo = reinsert partial run */
232 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
234 ME_DestroyString(pUndo->di.member.run.strText);
235 pUndo->di.member.run.nCharOfs = nOfs;
236 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
239 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
240 TRACE("Shift value: %d\n", shift);
241 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
243 /* update cursors (including c) */
244 for (i=-1; i<editor->nCursors; i++) {
245 ME_Cursor *pThisCur = editor->pCursors + i;
246 if (i == -1) pThisCur = &c;
247 if (pThisCur->pRun == cursor.pRun) {
248 if (pThisCur->nOffset > cursor.nOffset) {
249 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
250 pThisCur->nOffset = cursor.nOffset;
252 pThisCur->nOffset -= nCharsToDelete;
253 assert(pThisCur->nOffset >= 0);
254 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
256 if (pThisCur->nOffset == ME_StrVLen(run->strText))
258 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
259 assert(pThisCur->pRun->type == diRun);
260 pThisCur->nOffset = 0;
265 /* c = updated data now */
267 if (c.pRun == cursor.pRun)
268 ME_SkipAndPropagateCharOffset(c.pRun, shift);
270 ME_PropagateCharOffset(c.pRun, shift);
272 if (!ME_StrVLen(cursor.pRun->member.run.strText))
274 TRACE("Removing useless run\n");
275 ME_Remove(cursor.pRun);
276 ME_DestroyDisplayItem(cursor.pRun);
281 ME_CheckCharOffsets(editor);
288 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
291 assert(nCursor>=0 && nCursor<editor->nCursors);
292 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
295 static WCHAR wszSpace[] = {' ', 0};
297 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
298 void ME_InsertGraphicsFromCursor(ME_TextEditor *editor, int nCursor)
300 ME_Cursor *pCursor = &editor->pCursors[nCursor];
301 ME_DisplayItem *pItem = NULL;
302 ME_DisplayItem *pNewRun = NULL;
303 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
307 if (ME_IsSelection(editor))
308 ME_DeleteSelection(editor);
310 pUndo = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
312 pUndo->nStart = pCursor->nOffset + pCursor->pRun->member.run.nCharOfs + ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs;
315 if (pCursor->nOffset)
317 ME_SplitRunSimple(editor, pCursor->pRun, pCursor->nOffset);
319 pItem = pCursor->pRun;
320 pNewRun = ME_MakeRun(pStyle, ME_MakeStringN(wszSpace, 1), MERF_GRAPHICS);
321 pNewRun->member.run.nCharOfs = pCursor->pRun->member.run.nCharOfs;
322 ME_InsertBefore(pCursor->pRun, pNewRun);
323 ME_PropagateCharOffset(pItem, 1);
324 ME_CheckCharOffsets(editor);
325 ME_SendSelChange(editor);
328 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
329 const WCHAR *str, int len, ME_Style *style)
335 editor->bCaretAtEnd = FALSE;
337 ME_AddRefStyle(style);
339 /* FIXME really HERE ? */
340 if (ME_IsSelection(editor))
341 ME_DeleteSelection(editor);
343 assert(nCursor>=0 && nCursor<editor->nCursors);
347 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
348 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
350 if (pos-str < len && *pos == '\t') { /* handle tabs */
351 ME_DisplayItem *pNewRun = NULL;
355 ME_InsertTextFromCursor(editor, nCursor, str, pos-str, style);
357 p = &editor->pCursors[nCursor];
359 assert(p->pRun->type == diRun);
360 pNewRun = ME_MakeRun(style, ME_MakeStringN(&tab, 1), MERF_TAB); /* addrefs style */
361 ME_InsertRun(editor, ME_CharOfsFromRunOfs(editor, p->pRun, p->nOffset), pNewRun);
362 ME_DestroyDisplayItem(pNewRun);
363 ME_ReleaseStyle(style);
367 ME_InsertTextFromCursor(editor, nCursor, pos, len-(pos-str), style);
371 if (pos-str < len) { /* handle EOLs */
372 ME_DisplayItem *tp, *end_run;
376 ME_InsertTextFromCursor(editor, nCursor, str, pos-str, style);
377 p = &editor->pCursors[nCursor];
378 tp = ME_FindItemBack(p->pRun, diParagraph);
379 para = &tp->member.para;
382 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
383 p = &editor->pCursors[nCursor];
385 tmp_style = ME_GetInsertStyle(editor, nCursor);
386 /* ME_SplitParagraph increases style refcount */
387 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
388 p->pRun = ME_FindItemFwd(tp, diRun);
389 end_run = ME_FindItemBack(tp, diRun);
390 ME_ReleaseStyle(end_run->member.run.style);
391 end_run->member.run.style = tmp_style;
393 if(pos-str < len && *pos =='\r')
395 if(pos-str < len && *pos =='\n')
398 ME_InsertTextFromCursor(editor, nCursor, pos, len-(pos-str), style);
400 ME_ReleaseStyle(style);
403 p = &editor->pCursors[nCursor];
405 ME_DisplayItem *pNewRun = NULL;
407 assert(p->pRun->type == diRun);
408 pNewRun = ME_MakeRun(style, ME_MakeStringN(str, len), 0); /* addrefs style */
409 ME_InsertRun(editor, ME_CharOfsFromRunOfs(editor, p->pRun, p->nOffset), pNewRun);
410 ME_DestroyDisplayItem(pNewRun);
411 ME_ReleaseStyle(style);
418 static BOOL ME_ArrowLeft(ME_TextEditor *editor, ME_Cursor *p)
421 p->nOffset = ME_StrRelPos2(p->pRun->member.run.strText, p->nOffset, -1);
426 ME_DisplayItem *pRun = ME_FindItemBack(p->pRun, diRunOrParagraph);
428 if (pRun->type == diRun) {
430 assert(p->pRun->type == diRun);
431 assert(pRun->member.run.strText->nLen);
432 p->nOffset = pRun->member.run.strText->nLen;
434 p->nOffset = ME_StrRelPos2(pRun->member.run.strText, p->nOffset, -1);
440 if (pRun->type == diParagraph)
442 if (pRun->member.para.prev_para->type == diTextStart)
444 assert(pRun->member.para.prev_para->type == diParagraph);
445 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
446 /* every paragraph ought to have at least one run */
447 assert(pRun && pRun->type == diRun);
448 assert(pRun->member.run.nFlags & MERF_ENDPARA);
458 static BOOL ME_ArrowRight(ME_TextEditor *editor, ME_Cursor *p)
460 ME_DisplayItem *pRun;
462 if (!(p->pRun->member.run.nFlags & MERF_ENDPARA))
464 int new_ofs = ME_StrRelPos2(p->pRun->member.run.strText, p->nOffset, 1);
466 if (new_ofs<p->pRun->member.run.strText->nLen)
468 p->nOffset = new_ofs;
472 pRun = ME_FindItemFwd(p->pRun, diRun);
475 assert(p->pRun->type == diRun);
481 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
483 ME_Cursor *pCursor = &editor->pCursors[nCursor];
485 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
486 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
489 int ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
491 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
497 while(p != editor->pBuffer->pLast)
499 if (p->type == diParagraph)
501 int ry = y - p->member.para.nYPos;
504 result->pRun = ME_FindItemFwd(p, diRun);
508 if (ry >= p->member.para.nHeight)
510 p = p->member.para.next_para;
513 p = ME_FindItemFwd(p, diStartRow);
517 if (p->type == diStartRow)
519 int ry = y - p->member.row.nYPos;
522 if (ry >= p->member.row.nHeight)
524 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
525 if (p->type != diStartRow)
529 p = ME_FindItemFwd(p, diRun);
532 if (p->type == diRun)
535 rx = x - p->member.run.pt.x;
538 if (rx >= p->member.run.nWidth) /* not this run yet... find next item */
543 if (p->type == diRun)
545 rx = x - p->member.run.pt.x;
546 goto continue_search;
548 if (p->type == diStartRow)
550 p = ME_FindItemFwd(p, diRun);
553 rx = 0; /* FIXME not sure */
556 if (p->type == diParagraph || p->type == diTextEnd)
558 rx = 0; /* FIXME not sure */
566 if (p->member.run.nFlags & MERF_ENDPARA)
569 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
570 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
572 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
581 result->pRun = ME_FindItemBack(p, diRun);
583 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
587 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
589 ME_Cursor tmp_cursor;
590 int is_selection = 0;
592 editor->nUDArrowX = -1;
594 y += ME_GetYScrollPos(editor);
596 tmp_cursor = editor->pCursors[0];
597 is_selection = ME_IsSelection(editor);
599 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
601 if (GetKeyState(VK_SHIFT)>=0)
603 editor->pCursors[1] = editor->pCursors[0];
608 editor->pCursors[1] = tmp_cursor;
612 HideCaret(editor->hWnd);
613 ME_MoveCaret(editor);
616 ShowCaret(editor->hWnd);
617 ME_ClearTempStyle(editor);
618 ME_SendSelChange(editor);
621 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
623 ME_Cursor tmp_cursor;
625 y += ME_GetYScrollPos(editor);
627 tmp_cursor = editor->pCursors[0];
628 if (!ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd))
631 if (tmp_cursor.pRun == editor->pCursors[0].pRun &&
632 tmp_cursor.nOffset == editor->pCursors[0].nOffset)
635 HideCaret(editor->hWnd);
636 ME_MoveCaret(editor);
638 ShowCaret(editor->hWnd);
639 ME_SendSelChange(editor);
642 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
643 int x, int *pOffset, int *pbCaretAtEnd)
645 ME_DisplayItem *pNext, *pLastRun;
646 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
647 assert(pNext->type == diRun);
649 *pbCaretAtEnd = FALSE;
651 int run_x = pNext->member.run.pt.x;
652 int width = pNext->member.run.nWidth;
655 if (pOffset) *pOffset = 0;
658 if (x >= run_x && x < run_x+width)
660 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
661 ME_String *s = pNext->member.run.strText;
669 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
670 } while(pNext && pNext->type == diRun);
672 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
674 pNext = ME_FindItemFwd(pNext, diRun);
675 if (pbCaretAtEnd) *pbCaretAtEnd = 1;
676 if (pOffset) *pOffset = 0;
679 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
680 if (pOffset) *pOffset = 0;
685 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
687 ME_DisplayItem *pRun = pCursor->pRun;
690 if (editor->nUDArrowX != -1)
691 x = editor->nUDArrowX;
693 if (editor->bCaretAtEnd)
695 pRun = ME_FindItemBack(pRun, diRun);
697 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
700 x = pRun->member.run.pt.x;
701 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
703 editor->nUDArrowX = x;
708 static void ME_ArrowUp(ME_TextEditor *editor, ME_Cursor *pCursor)
710 ME_DisplayItem *pRun = pCursor->pRun;
711 ME_DisplayItem *pItem, *pItem2;
712 int x = ME_GetXForArrow(editor, pCursor);
714 if (editor->bCaretAtEnd && !pCursor->nOffset)
716 pRun = ME_FindItemBack(pRun, diRun);
721 /* start of this row */
722 pItem = ME_FindItemBack(pRun, diStartRow);
724 /* start of the previous row */
725 pItem2 = ME_FindItemBack(pItem, diStartRow);
726 /* no previous row = the first line of the first paragraph */
727 if (!pItem2) /* can't go up - don't go BOL (as in MS richedit) */
730 ME_WrapTextParagraph(editor, ME_FindItemBack(pItem2, diParagraph));
732 pCursor->pRun = ME_FindRunInRow(editor, pItem2, x, &pCursor->nOffset, &editor->bCaretAtEnd);
735 static void ME_ArrowDown(ME_TextEditor *editor, ME_Cursor *pCursor)
737 ME_DisplayItem *pRun = pCursor->pRun;
738 ME_DisplayItem *pItem;
739 int x = ME_GetXForArrow(editor, pCursor);
740 if (!pCursor->nOffset && editor->bCaretAtEnd)
742 pRun = ME_FindItemBack(pRun, diRun);
743 /* x = pRun->member.run.pt.x + pRun->member.run.nWidth; */
745 /* start of the next row */
746 pItem = ME_FindItemFwd(pRun, diStartRow);
747 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
751 /* next row not found - ignore */
754 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
755 assert(pCursor->pRun);
756 assert(pCursor->pRun->type == diRun);
759 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
761 ME_DisplayItem *pRun = pCursor->pRun;
762 ME_DisplayItem *pLast, *p;
763 int x, y, ys, yd, yp, yprev;
764 ME_Cursor tmp_curs = *pCursor;
766 x = ME_GetXForArrow(editor, pCursor);
767 if (!pCursor->nOffset && editor->bCaretAtEnd)
768 pRun = ME_FindItemBack(pRun, diRun);
770 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
771 assert(p->type == diStartRow);
772 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
773 yprev = ys = y = yp + p->member.row.nYPos;
774 yd = y - editor->sizeWindow.cy;
778 p = ME_FindItemBack(p, diStartRowOrParagraph);
781 if (p->type == diParagraph) { /* crossing paragraphs */
782 if (p->member.para.prev_para == NULL)
784 yp = p->member.para.prev_para->member.para.nYPos;
787 y = yp + p->member.row.nYPos;
794 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
795 ME_UpdateSelection(editor, &tmp_curs);
796 if (yprev < editor->sizeWindow.cy)
798 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
802 ME_Scroll(editor, 0, ys-yprev);
805 assert(pCursor->pRun);
806 assert(pCursor->pRun->type == diRun);
809 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
810 of pixels, even if it makes the scroll bar position exceed its normal maximum.
811 In such a situation, clicking the scrollbar restores its position back to the
812 normal range (ie. sets it to (doclength-screenheight)). */
814 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
816 ME_DisplayItem *pRun = pCursor->pRun;
817 ME_DisplayItem *pLast, *p;
818 int x, y, ys, yd, yp, yprev;
819 ME_Cursor tmp_curs = *pCursor;
821 x = ME_GetXForArrow(editor, pCursor);
822 if (!pCursor->nOffset && editor->bCaretAtEnd)
823 pRun = ME_FindItemBack(pRun, diRun);
825 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
826 assert(p->type == diStartRow);
827 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
828 yprev = ys = y = yp + p->member.row.nYPos;
829 yd = y + editor->sizeWindow.cy;
833 p = ME_FindItemFwd(p, diStartRowOrParagraph);
836 if (p->type == diParagraph) {
837 yp = p->member.para.nYPos;
840 y = yp + p->member.row.nYPos;
847 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
848 ME_UpdateSelection(editor, &tmp_curs);
849 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
851 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
855 ME_Scroll(editor, 0, ys-yprev);
858 assert(pCursor->pRun);
859 assert(pCursor->pRun->type == diRun);
862 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
864 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
866 ME_DisplayItem *pRun;
867 if (editor->bCaretAtEnd && !pCursor->nOffset) {
868 pRow = ME_FindItemBack(pRow, diStartRow);
872 pRun = ME_FindItemFwd(pRow, diRun);
874 pCursor->pRun = pRun;
875 pCursor->nOffset = 0;
878 editor->bCaretAtEnd = FALSE;
881 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
883 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
885 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
887 pCursor->pRun = pRun;
888 pCursor->nOffset = 0;
893 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
895 ME_DisplayItem *pRow;
897 if (editor->bCaretAtEnd && !pCursor->nOffset)
900 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
902 if (pRow->type == diStartRow) {
903 /* FIXME WTF was I thinking about here ? */
904 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
906 pCursor->pRun = pRun;
907 pCursor->nOffset = 0;
908 editor->bCaretAtEnd = 1;
911 pCursor->pRun = ME_FindItemBack(pRow, diRun);
912 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
913 pCursor->nOffset = 0;
914 editor->bCaretAtEnd = FALSE;
917 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
919 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
921 p = ME_FindItemBack(p, diRun);
923 assert(p->member.run.nFlags & MERF_ENDPARA);
925 pCursor->nOffset = 0;
926 editor->bCaretAtEnd = FALSE;
929 BOOL ME_IsSelection(ME_TextEditor *editor)
931 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
934 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
936 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
944 static BOOL ME_CancelSelection(ME_TextEditor *editor, int dir)
948 if (GetKeyState(VK_SHIFT)<0)
950 if (!memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor)))
953 cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
956 editor->pCursors[1] = editor->pCursors[0];
958 editor->pCursors[0] = editor->pCursors[1];
963 BOOL ME_UpdateSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
965 ME_Cursor old_anchor = editor->pCursors[1];
967 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
969 /* any selection was present ? if so, it's no more, repaint ! */
970 editor->pCursors[1] = editor->pCursors[0];
971 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
978 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
980 editor->pCursors[1] = *pTempCursor;
989 static void ME_RepaintSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
991 if (ME_UpdateSelection(editor, pTempCursor)) {
992 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
997 void ME_DeleteSelection(ME_TextEditor *editor)
1000 ME_GetSelection(editor, &from, &to);
1001 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1004 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1010 ME_GetSelection(editor, &from, &to);
1011 ME_CursorFromCharOfs(editor, from, &c);
1013 style = c.pRun->member.run.style;
1014 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1017 style = ME_GetInsertStyle(editor, 0);
1021 void ME_SendSelChange(ME_TextEditor *editor)
1024 if (!(editor->nEventMask & ENM_SELCHANGE))
1026 sc.nmhdr.hwndFrom = editor->hWnd;
1027 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1028 sc.nmhdr.code = EN_SELCHANGE;
1029 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1030 sc.seltyp = SEL_EMPTY;
1031 if (sc.chrg.cpMin != sc.chrg.cpMax)
1032 sc.seltyp |= SEL_TEXT;
1033 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1034 sc.seltyp |= SEL_MULTICHAR;
1035 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1038 BOOL ME_ArrowKey(ME_TextEditor *editor, int nVKey, int nCtrl)
1041 ME_Cursor *p = &editor->pCursors[nCursor];
1042 ME_Cursor tmp_curs = *p;
1046 ME_ArrowUp(editor, p);
1047 ME_ClearTempStyle(editor);
1048 ME_RepaintSelection(editor, &tmp_curs);
1049 ME_SendSelChange(editor);
1052 ME_ArrowDown(editor, p);
1053 ME_ClearTempStyle(editor);
1054 ME_RepaintSelection(editor, &tmp_curs);
1055 ME_SendSelChange(editor);
1058 ME_ArrowPageUp(editor, p);
1059 ME_ClearTempStyle(editor);
1060 ME_SendSelChange(editor);
1063 ME_ArrowPageDown(editor, p);
1064 ME_ClearTempStyle(editor);
1065 ME_SendSelChange(editor);
1069 editor->nUDArrowX = -1;
1071 case VK_BACK: { /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1072 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1074 if (ME_IsSelection(editor))
1076 editor->bCaretAtEnd = FALSE; /* FIXME or maybe not */
1077 ME_DeleteSelection(editor);
1078 ME_UpdateRepaint(editor);
1081 if (ME_ArrowLeft(editor, p)) {
1082 editor->bCaretAtEnd = FALSE; /* FIXME or maybe not */
1083 ME_ClearTempStyle(editor);
1084 ME_MoveCaret(editor);
1085 ME_DeleteTextAtCursor(editor, nCursor, 1);
1086 ME_UpdateRepaint(editor);
1091 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1093 /* editor->bCaretAtEnd = 0; FIXME or maybe not */
1094 if (ME_IsSelection(editor))
1096 ME_DeleteSelection(editor);
1097 ME_ClearTempStyle(editor);
1098 ME_UpdateRepaint(editor);
1101 ME_DeleteTextAtCursor(editor, nCursor, 1);
1102 ME_ClearTempStyle(editor);
1103 ME_UpdateRepaint(editor);
1107 if (GetKeyState(VK_CONTROL)<0)
1108 ME_ArrowCtrlHome(editor, p);
1110 ME_ArrowHome(editor, p);
1111 editor->bCaretAtEnd = 0;
1112 ME_ClearTempStyle(editor);
1113 ME_RepaintSelection(editor, &tmp_curs);
1114 ME_SendSelChange(editor);
1118 if (GetKeyState(VK_CONTROL)<0)
1119 ME_ArrowCtrlEnd(editor, p);
1121 ME_ArrowEnd(editor, p);
1122 ME_ClearTempStyle(editor);
1123 ME_RepaintSelection(editor, &tmp_curs);
1124 ME_SendSelChange(editor);
1127 editor->bCaretAtEnd = 0;
1128 if (ME_CancelSelection(editor, -1))
1130 ME_ArrowLeft(editor, p);
1131 ME_RepaintSelection(editor, &tmp_curs);
1132 ME_ClearTempStyle(editor);
1133 ME_SendSelChange(editor);
1136 editor->bCaretAtEnd = 0;
1137 if (ME_CancelSelection(editor, +1))
1139 ME_ArrowRight(editor, p);
1140 ME_RepaintSelection(editor, &tmp_curs);
1141 ME_ClearTempStyle(editor);
1142 ME_SendSelChange(editor);