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);
31 void ME_GetSelection(ME_TextEditor *editor, int *from, int *to)
33 *from = ME_GetCursorOfs(editor, 0);
34 *to = ME_GetCursorOfs(editor, 1);
44 int ME_GetTextLength(ME_TextEditor *editor)
46 return ME_CharOfsFromRunOfs(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun), 0);
50 int ME_GetTextLengthEx(ME_TextEditor *editor, GETTEXTLENGTHEX *how)
54 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
56 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
59 length = ME_GetTextLength(editor);
61 if (how->flags & GTL_USECRLF)
62 length += editor->nParagraphs;
64 if (how->flags & GTL_NUMBYTES)
68 if (how->codepage == 1200)
70 if (how->flags & GTL_PRECISE)
71 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
72 if (GetCPInfo(how->codepage, &cpinfo))
73 return length * cpinfo.MaxCharSize;
74 ERR("Invalid codepage %u\n", how->codepage);
81 void ME_SetSelection(ME_TextEditor *editor, int from, int to)
83 if (from == 0 && to == -1)
85 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
86 editor->pCursors[1].nOffset = 0;
87 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
88 editor->pCursors[0].nOffset = 0;
89 ME_InvalidateSelection(editor);
90 ME_ClearTempStyle(editor);
95 editor->pCursors[1] = editor->pCursors[0];
97 ME_ClearTempStyle(editor);
106 ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
107 ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);
112 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
113 int *x, int *y, int *height)
115 ME_DisplayItem *pCursorRun = pCursor->pRun;
116 ME_DisplayItem *pSizeRun = pCursor->pRun;
118 assert(!pCursor->nOffset || !editor->bCaretAtEnd);
119 assert(height && x && y);
120 assert(!(ME_GetParagraph(pCursorRun)->member.para.nFlags & MEPF_REWRAP));
121 assert(pCursor->pRun);
122 assert(pCursor->pRun->type == diRun);
124 if (pCursorRun->type == diRun) {
125 ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
128 HDC hDC = GetDC(editor->hWnd);
130 ME_DisplayItem *run = pCursorRun;
131 ME_DisplayItem *para = NULL;
134 ME_InitContext(&c, editor, hDC);
136 if (!pCursor->nOffset && !editor->bCaretAtEnd)
138 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrStartRow);
140 if (prev->type == diRun)
143 assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
144 para = ME_FindItemBack(row, diParagraph);
146 assert(para->type == diParagraph);
147 if (editor->bCaretAtEnd && !pCursor->nOffset &&
148 run == ME_FindItemFwd(row, diRun))
150 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
152 if (tmp->type == diRun)
154 row = ME_FindItemBack(tmp, diStartRow);
155 pSizeRun = run = tmp;
157 assert(run->type == diRun);
158 sz = ME_GetRunSize(&c, ¶->member.para, &run->member.run, ME_StrLen(run->member.run.strText));
161 if (pCursor->nOffset && !(run->member.run.nFlags & MERF_SKIPPED)) {
162 sz = ME_GetRunSize(&c, ¶->member.para, &run->member.run, pCursor->nOffset);
165 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
166 *x = run->member.run.pt.x + sz.cx;
167 *y = para->member.para.nYPos + row->member.row.nBaseline + pSizeRun->member.run.pt.y - pSizeRun->member.run.nAscent - ME_GetYScrollPos(editor);
169 ME_DestroyContext(&c);
170 ReleaseDC(editor->hWnd, hDC);
174 *height = 10; /* FIXME use global font */
181 ME_MoveCaret(ME_TextEditor *editor)
185 ME_WrapMarkedParagraphs(editor);
186 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
187 CreateCaret(editor->hWnd, NULL, 0, height);
192 void ME_ShowCaret(ME_TextEditor *ed)
198 void ME_HideCaret(ME_TextEditor *ed)
204 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
213 ME_CursorFromCharOfs(editor, nOfs, &c);
214 run = &c.pRun->member.run;
215 if (run->nFlags & MERF_ENDPARA) {
216 if (!ME_FindItemFwd(c.pRun, diParagraph))
220 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
221 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
222 ME_CheckCharOffsets(editor);
224 if (editor->bEmulateVersion10 && nChars)
231 int nIntendedChars = nChars;
232 int nCharsToDelete = nChars;
236 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
239 ME_StrRelPos(run->strText, loc, &nChars);
240 /* nChars is the number of characters that should be deleted from the
241 FOLLOWING runs (these AFTER cursor.pRun)
242 nCharsToDelete is a number of chars to delete from THIS run */
243 nCharsToDelete -= nChars;
244 shift -= nCharsToDelete;
245 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
246 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
247 debugstr_w(run->strText->szData), run->strText->nLen);
249 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
251 /* undo = reinsert whole run */
252 /* nOfs is a character offset (from the start of the document
253 to the current (deleted) run */
254 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
256 pUndo->di.member.run.nCharOfs = nOfs;
260 /* undo = reinsert partial run */
261 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
263 ME_DestroyString(pUndo->di.member.run.strText);
264 pUndo->di.member.run.nCharOfs = nOfs;
265 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
268 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
269 TRACE("Shift value: %d\n", shift);
270 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
272 /* update cursors (including c) */
273 for (i=-1; i<editor->nCursors; i++) {
274 ME_Cursor *pThisCur = editor->pCursors + i;
275 if (i == -1) pThisCur = &c;
276 if (pThisCur->pRun == cursor.pRun) {
277 if (pThisCur->nOffset > cursor.nOffset) {
278 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
279 pThisCur->nOffset = cursor.nOffset;
281 pThisCur->nOffset -= nCharsToDelete;
282 assert(pThisCur->nOffset >= 0);
283 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
285 if (pThisCur->nOffset == ME_StrVLen(run->strText))
287 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
288 assert(pThisCur->pRun->type == diRun);
289 pThisCur->nOffset = 0;
294 /* c = updated data now */
296 if (c.pRun == cursor.pRun)
297 ME_SkipAndPropagateCharOffset(c.pRun, shift);
299 ME_PropagateCharOffset(c.pRun, shift);
301 if (!ME_StrVLen(cursor.pRun->member.run.strText))
303 TRACE("Removing useless run\n");
304 ME_Remove(cursor.pRun);
305 ME_DestroyDisplayItem(cursor.pRun);
310 ME_CheckCharOffsets(editor);
317 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
320 assert(nCursor>=0 && nCursor<editor->nCursors);
321 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
324 static ME_DisplayItem *
325 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
326 const WCHAR *str, int len, ME_Style *style,
329 ME_Cursor *p = &editor->pCursors[nCursor];
331 editor->bCaretAtEnd = FALSE;
333 assert(p->pRun->type == diRun);
335 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
339 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
340 void ME_InsertGraphicsFromCursor(ME_TextEditor *editor, int nCursor)
342 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
346 if (ME_IsSelection(editor))
347 ME_DeleteSelection(editor);
349 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
351 ME_SendSelChange(editor);
356 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
359 ME_DisplayItem *p, *run;
360 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
362 p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
365 while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
367 if (run->member.run.nFlags & MERF_CELL)
369 assert(run->member.run.pCell->next);
370 p->member.run.pCell = run->member.run.pCell->next;
374 assert(run->type == diParagraph);
375 assert(run->member.para.bTable);
376 assert(run->member.para.pCells);
377 p->member.run.pCell = run->member.para.pCells;
381 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
382 const WCHAR *str, int len, ME_Style *style)
389 /* FIXME really HERE ? */
390 if (ME_IsSelection(editor))
391 ME_DeleteSelection(editor);
393 assert(nCursor>=0 && nCursor<editor->nCursors);
399 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
400 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
402 if (pos-str < len && *pos == '\t') { /* handle tabs */
406 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
408 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
417 if (pos-str < len) { /* handle EOLs */
418 ME_DisplayItem *tp, *end_run;
421 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
422 p = &editor->pCursors[nCursor];
424 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
425 p = &editor->pCursors[nCursor];
427 tmp_style = ME_GetInsertStyle(editor, nCursor);
428 /* ME_SplitParagraph increases style refcount */
429 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
430 p->pRun = ME_FindItemFwd(tp, diRun);
431 end_run = ME_FindItemBack(tp, diRun);
432 ME_ReleaseStyle(end_run->member.run.style);
433 end_run->member.run.style = tmp_style;
435 if(pos-str < len && *pos =='\r')
437 if(pos-str < len && *pos =='\n')
445 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
452 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
454 ME_DisplayItem *pRun = pCursor->pRun;
458 if (!pCursor->nOffset)
461 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
468 if (pRun->member.para.prev_para->type == diTextStart)
470 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
471 /* every paragraph ought to have at least one run */
472 assert(pRun && pRun->type == diRun);
473 assert(pRun->member.run.nFlags & MERF_ENDPARA);
476 assert(pRun->type != diRun && pRun->type != diParagraph);
479 } while (RUN_IS_HIDDEN(&pRun->member.run));
480 pCursor->pRun = pRun;
481 if (pRun->member.run.nFlags & MERF_ENDPARA)
482 pCursor->nOffset = 0;
484 pCursor->nOffset = pRun->member.run.strText->nLen;
487 if (pCursor->nOffset)
488 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
493 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
495 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
497 if (new_ofs < pRun->member.run.strText->nLen)
499 pCursor->nOffset = new_ofs;
504 pRun = ME_FindItemFwd(pRun, diRun);
505 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
508 pCursor->pRun = pRun;
509 pCursor->nOffset = 0;
518 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
520 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
521 int nOffset = cursor->nOffset;
525 /* Backward movement */
528 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
529 nOffset, WB_MOVEWORDLEFT);
532 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
533 if (pOtherRun->type == diRun)
535 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
536 pOtherRun->member.run.strText->nLen - 1,
538 && !(pRun->member.run.nFlags & MERF_ENDPARA)
539 && !(cursor->pRun == pRun && cursor->nOffset == 0)
540 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
544 nOffset = pOtherRun->member.run.strText->nLen;
546 else if (pOtherRun->type == diParagraph)
548 if (cursor->pRun == pRun && cursor->nOffset == 0)
550 /* Paragraph breaks are treated as separate words */
551 if (pOtherRun->member.para.prev_para->type == diTextStart)
553 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
561 /* Forward movement */
562 BOOL last_delim = FALSE;
566 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
567 nOffset, WB_ISDELIMITER))
569 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
570 nOffset, WB_MOVEWORDRIGHT);
571 if (nOffset < pRun->member.run.strText->nLen)
573 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
574 if (pOtherRun->type == diRun)
576 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
577 nOffset - 1, WB_ISDELIMITER);
581 else if (pOtherRun->type == diParagraph)
583 if (cursor->pRun == pRun)
584 pRun = ME_FindItemFwd(pOtherRun, diRun);
590 if (cursor->pRun == pRun)
598 cursor->nOffset = nOffset;
604 ME_SelectWord(ME_TextEditor *editor)
606 if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
607 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
608 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
609 ME_InvalidateSelection(editor);
610 ME_SendSelChange(editor);
614 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
616 ME_Cursor *pCursor = &editor->pCursors[nCursor];
618 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
619 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
622 int ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
624 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
630 while(p != editor->pBuffer->pLast)
632 if (p->type == diParagraph)
634 int ry = y - p->member.para.nYPos;
637 result->pRun = ME_FindItemFwd(p, diRun);
641 if (ry >= p->member.para.nHeight)
643 p = p->member.para.next_para;
646 p = ME_FindItemFwd(p, diStartRow);
650 if (p->type == diStartRow)
652 int ry = y - p->member.row.nYPos;
655 if (ry >= p->member.row.nHeight)
657 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
658 if (p->type != diStartRow)
662 p = ME_FindItemFwd(p, diRun);
665 if (p->type == diRun)
668 rx = x - p->member.run.pt.x;
671 if (rx >= p->member.run.nWidth) /* not this run yet... find next item */
676 if (p->type == diRun)
678 rx = x - p->member.run.pt.x;
679 goto continue_search;
681 if (p->type == diStartRow)
683 p = ME_FindItemFwd(p, diRun);
686 rx = 0; /* FIXME not sure */
689 if (p->type == diParagraph || p->type == diTextEnd)
691 rx = 0; /* FIXME not sure */
699 if (p->member.run.nFlags & MERF_ENDPARA)
702 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
703 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
705 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
714 result->pRun = ME_FindItemBack(p, diRun);
716 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
722 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
727 GetClientRect(editor->hWnd, &rc);
728 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
730 y += ME_GetYScrollPos(editor);
731 ME_FindPixelPos(editor, x, y, &cursor, NULL);
732 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
733 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
737 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
739 ME_Cursor tmp_cursor;
740 int is_selection = 0;
742 editor->nUDArrowX = -1;
744 y += ME_GetYScrollPos(editor);
746 tmp_cursor = editor->pCursors[0];
747 is_selection = ME_IsSelection(editor);
749 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
751 if (GetKeyState(VK_SHIFT)>=0)
753 editor->pCursors[1] = editor->pCursors[0];
758 editor->pCursors[1] = tmp_cursor;
762 ME_InvalidateSelection(editor);
763 HideCaret(editor->hWnd);
764 ME_MoveCaret(editor);
765 ShowCaret(editor->hWnd);
766 ME_ClearTempStyle(editor);
767 ME_SendSelChange(editor);
770 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
772 ME_Cursor tmp_cursor;
774 y += ME_GetYScrollPos(editor);
776 tmp_cursor = editor->pCursors[0];
777 /* FIXME: do something with the return value of ME_FindPixelPos */
778 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
780 if (tmp_cursor.pRun == editor->pCursors[0].pRun &&
781 tmp_cursor.nOffset == editor->pCursors[0].nOffset)
784 ME_InvalidateSelection(editor);
785 editor->pCursors[0] = tmp_cursor;
786 HideCaret(editor->hWnd);
787 ME_MoveCaret(editor);
788 ME_InvalidateSelection(editor);
789 ShowCaret(editor->hWnd);
790 ME_SendSelChange(editor);
793 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
794 int x, int *pOffset, int *pbCaretAtEnd)
796 ME_DisplayItem *pNext, *pLastRun;
797 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
798 assert(pNext->type == diRun);
800 *pbCaretAtEnd = FALSE;
802 int run_x = pNext->member.run.pt.x;
803 int width = pNext->member.run.nWidth;
806 if (pOffset) *pOffset = 0;
809 if (x >= run_x && x < run_x+width)
811 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
812 ME_String *s = pNext->member.run.strText;
820 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
821 } while(pNext && pNext->type == diRun);
823 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
825 pNext = ME_FindItemFwd(pNext, diRun);
826 if (pbCaretAtEnd) *pbCaretAtEnd = 1;
827 if (pOffset) *pOffset = 0;
830 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
831 if (pOffset) *pOffset = 0;
836 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
838 ME_DisplayItem *pRun = pCursor->pRun;
841 if (editor->nUDArrowX != -1)
842 x = editor->nUDArrowX;
844 if (editor->bCaretAtEnd)
846 pRun = ME_FindItemBack(pRun, diRun);
848 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
851 x = pRun->member.run.pt.x;
852 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
854 editor->nUDArrowX = x;
861 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
863 ME_DisplayItem *pRun = pCursor->pRun;
864 ME_DisplayItem *pItem;
865 int x = ME_GetXForArrow(editor, pCursor);
867 if (editor->bCaretAtEnd && !pCursor->nOffset)
868 pRun = ME_FindItemBack(pRun, diRun);
873 /* start of this row */
874 pItem = ME_FindItemBack(pRun, diStartRow);
876 /* start of the previous row */
877 pItem = ME_FindItemBack(pItem, diStartRow);
881 /* start of the next row */
882 pItem = ME_FindItemFwd(pRun, diStartRow);
883 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
888 /* row not found - ignore */
891 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
892 assert(pCursor->pRun);
893 assert(pCursor->pRun->type == diRun);
897 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
899 ME_DisplayItem *pRun = pCursor->pRun;
900 ME_DisplayItem *pLast, *p;
901 int x, y, ys, yd, yp, yprev;
902 ME_Cursor tmp_curs = *pCursor;
904 x = ME_GetXForArrow(editor, pCursor);
905 if (!pCursor->nOffset && editor->bCaretAtEnd)
906 pRun = ME_FindItemBack(pRun, diRun);
908 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
909 assert(p->type == diStartRow);
910 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
911 yprev = ys = y = yp + p->member.row.nYPos;
912 yd = y - editor->sizeWindow.cy;
916 p = ME_FindItemBack(p, diStartRowOrParagraph);
919 if (p->type == diParagraph) { /* crossing paragraphs */
920 if (p->member.para.prev_para == NULL)
922 yp = p->member.para.prev_para->member.para.nYPos;
925 y = yp + p->member.row.nYPos;
932 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
933 ME_UpdateSelection(editor, &tmp_curs);
934 if (yprev < editor->sizeWindow.cy)
936 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
940 ME_Scroll(editor, 0, ys-yprev);
943 assert(pCursor->pRun);
944 assert(pCursor->pRun->type == diRun);
947 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
948 of pixels, even if it makes the scroll bar position exceed its normal maximum.
949 In such a situation, clicking the scrollbar restores its position back to the
950 normal range (ie. sets it to (doclength-screenheight)). */
952 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
954 ME_DisplayItem *pRun = pCursor->pRun;
955 ME_DisplayItem *pLast, *p;
956 int x, y, ys, yd, yp, yprev;
957 ME_Cursor tmp_curs = *pCursor;
959 x = ME_GetXForArrow(editor, pCursor);
960 if (!pCursor->nOffset && editor->bCaretAtEnd)
961 pRun = ME_FindItemBack(pRun, diRun);
963 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
964 assert(p->type == diStartRow);
965 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
966 yprev = ys = y = yp + p->member.row.nYPos;
967 yd = y + editor->sizeWindow.cy;
971 p = ME_FindItemFwd(p, diStartRowOrParagraph);
974 if (p->type == diParagraph) {
975 yp = p->member.para.nYPos;
978 y = yp + p->member.row.nYPos;
985 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
986 ME_UpdateSelection(editor, &tmp_curs);
987 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
989 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
993 ME_Scroll(editor, 0, ys-yprev);
996 assert(pCursor->pRun);
997 assert(pCursor->pRun->type == diRun);
1000 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1002 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1003 /* bCaretAtEnd doesn't make sense if the cursor isn't set at the
1004 first character of the next row */
1005 assert(!editor->bCaretAtEnd || !pCursor->nOffset);
1006 ME_WrapMarkedParagraphs(editor);
1008 ME_DisplayItem *pRun;
1009 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1010 pRow = ME_FindItemBack(pRow, diStartRow);
1014 pRun = ME_FindItemFwd(pRow, diRun);
1016 pCursor->pRun = pRun;
1017 pCursor->nOffset = 0;
1020 editor->bCaretAtEnd = FALSE;
1023 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1025 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1027 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1029 pCursor->pRun = pRun;
1030 pCursor->nOffset = 0;
1035 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1037 ME_DisplayItem *pRow;
1039 if (editor->bCaretAtEnd && !pCursor->nOffset)
1042 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1044 if (pRow->type == diStartRow) {
1045 /* FIXME WTF was I thinking about here ? */
1046 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1048 pCursor->pRun = pRun;
1049 pCursor->nOffset = 0;
1050 editor->bCaretAtEnd = 1;
1053 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1054 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1055 pCursor->nOffset = 0;
1056 editor->bCaretAtEnd = FALSE;
1059 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1061 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1063 p = ME_FindItemBack(p, diRun);
1065 assert(p->member.run.nFlags & MERF_ENDPARA);
1067 pCursor->nOffset = 0;
1068 editor->bCaretAtEnd = FALSE;
1071 BOOL ME_IsSelection(ME_TextEditor *editor)
1073 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1076 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1078 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1086 BOOL ME_UpdateSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
1088 ME_Cursor old_anchor = editor->pCursors[1];
1090 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1092 /* any selection was present ? if so, it's no more, repaint ! */
1093 editor->pCursors[1] = editor->pCursors[0];
1094 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1101 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1103 editor->pCursors[1] = *pTempCursor;
1112 void ME_DeleteSelection(ME_TextEditor *editor)
1115 ME_GetSelection(editor, &from, &to);
1116 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1119 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1125 ME_GetSelection(editor, &from, &to);
1126 ME_CursorFromCharOfs(editor, from, &c);
1128 style = c.pRun->member.run.style;
1129 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1132 style = ME_GetInsertStyle(editor, 0);
1136 void ME_SendSelChange(ME_TextEditor *editor)
1140 ME_ClearTempStyle(editor);
1142 if (!(editor->nEventMask & ENM_SELCHANGE))
1145 sc.nmhdr.hwndFrom = editor->hWnd;
1146 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1147 sc.nmhdr.code = EN_SELCHANGE;
1148 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1149 sc.seltyp = SEL_EMPTY;
1150 if (sc.chrg.cpMin != sc.chrg.cpMax)
1151 sc.seltyp |= SEL_TEXT;
1152 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1153 sc.seltyp |= SEL_MULTICHAR;
1154 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1159 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1162 ME_Cursor *p = &editor->pCursors[nCursor];
1163 ME_Cursor tmp_curs = *p;
1164 BOOL success = FALSE;
1166 ME_CheckCharOffsets(editor);
1167 editor->nUDArrowX = -1;
1170 editor->bCaretAtEnd = 0;
1172 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1174 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1177 editor->bCaretAtEnd = 0;
1179 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1181 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1184 ME_MoveCursorLines(editor, &tmp_curs, -1);
1187 ME_MoveCursorLines(editor, &tmp_curs, +1);
1190 ME_ArrowPageUp(editor, &tmp_curs);
1193 ME_ArrowPageDown(editor, &tmp_curs);
1197 ME_ArrowCtrlHome(editor, &tmp_curs);
1199 ME_ArrowHome(editor, &tmp_curs);
1200 editor->bCaretAtEnd = 0;
1205 ME_ArrowCtrlEnd(editor, &tmp_curs);
1207 ME_ArrowEnd(editor, &tmp_curs);
1212 editor->pCursors[1] = tmp_curs;
1215 ME_InvalidateSelection(editor);
1217 HideCaret(editor->hWnd);
1218 ME_EnsureVisible(editor, tmp_curs.pRun);
1219 ME_ShowCaret(editor);
1220 ME_SendSelChange(editor);