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);
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 ME_DisplayItem *
316 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
317 const WCHAR *str, int len, ME_Style *style,
320 ME_Cursor *p = &editor->pCursors[nCursor];
322 editor->bCaretAtEnd = FALSE;
324 assert(p->pRun->type == diRun);
326 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
330 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
331 void ME_InsertGraphicsFromCursor(ME_TextEditor *editor, int nCursor)
333 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
337 if (ME_IsSelection(editor))
338 ME_DeleteSelection(editor);
340 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
342 ME_SendSelChange(editor);
347 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
350 ME_DisplayItem *p, *run;
351 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
353 p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
356 while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
358 if (run->member.run.nFlags & MERF_CELL)
360 assert(run->member.run.pCell->next);
361 p->member.run.pCell = run->member.run.pCell->next;
365 assert(run->type == diParagraph);
366 assert(run->member.para.bTable);
367 assert(run->member.para.pCells);
368 p->member.run.pCell = run->member.para.pCells;
372 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
373 const WCHAR *str, int len, ME_Style *style)
380 /* FIXME really HERE ? */
381 if (ME_IsSelection(editor))
382 ME_DeleteSelection(editor);
384 assert(nCursor>=0 && nCursor<editor->nCursors);
390 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
391 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
393 if (pos-str < len && *pos == '\t') { /* handle tabs */
397 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
399 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
408 if (pos-str < len) { /* handle EOLs */
409 ME_DisplayItem *tp, *end_run;
412 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
413 p = &editor->pCursors[nCursor];
415 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
416 p = &editor->pCursors[nCursor];
418 tmp_style = ME_GetInsertStyle(editor, nCursor);
419 /* ME_SplitParagraph increases style refcount */
420 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
421 p->pRun = ME_FindItemFwd(tp, diRun);
422 end_run = ME_FindItemBack(tp, diRun);
423 ME_ReleaseStyle(end_run->member.run.style);
424 end_run->member.run.style = tmp_style;
426 if(pos-str < len && *pos =='\r')
428 if(pos-str < len && *pos =='\n')
436 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
443 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
445 ME_DisplayItem *pRun = pCursor->pRun;
449 if (!pCursor->nOffset)
452 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
459 if (pRun->member.para.prev_para->type == diTextStart)
461 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
462 /* every paragraph ought to have at least one run */
463 assert(pRun && pRun->type == diRun);
464 assert(pRun->member.run.nFlags & MERF_ENDPARA);
467 assert(pRun->type != diRun && pRun->type != diParagraph);
470 } while (RUN_IS_HIDDEN(&pRun->member.run));
471 pCursor->pRun = pRun;
472 if (pRun->member.run.nFlags & MERF_ENDPARA)
473 pCursor->nOffset = 0;
475 pCursor->nOffset = pRun->member.run.strText->nLen;
478 if (pCursor->nOffset)
479 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
484 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
486 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
488 if (new_ofs < pRun->member.run.strText->nLen)
490 pCursor->nOffset = new_ofs;
495 pRun = ME_FindItemFwd(pRun, diRun);
496 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
499 pCursor->pRun = pRun;
500 pCursor->nOffset = 0;
509 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
511 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
512 int nOffset = cursor->nOffset;
516 /* Backward movement */
519 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
520 nOffset, WB_MOVEWORDLEFT);
523 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
524 if (pOtherRun->type == diRun)
526 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
527 pOtherRun->member.run.strText->nLen - 1,
529 && !(pRun->member.run.nFlags & MERF_ENDPARA)
530 && !(cursor->pRun == pRun && cursor->nOffset == 0)
531 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
535 nOffset = pOtherRun->member.run.strText->nLen;
537 else if (pOtherRun->type == diParagraph)
539 if (cursor->pRun == pRun && cursor->nOffset == 0)
541 /* Paragraph breaks are treated as separate words */
542 if (pOtherRun->member.para.prev_para->type == diTextStart)
544 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
552 /* Forward movement */
553 BOOL last_delim = FALSE;
557 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
558 nOffset, WB_ISDELIMITER))
560 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
561 nOffset, WB_MOVEWORDRIGHT);
562 if (nOffset < pRun->member.run.strText->nLen)
564 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
565 if (pOtherRun->type == diRun)
567 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
568 nOffset - 1, WB_ISDELIMITER);
572 else if (pOtherRun->type == diParagraph)
574 if (cursor->pRun == pRun)
575 pRun = ME_FindItemFwd(pOtherRun, diRun);
581 if (cursor->pRun == pRun)
589 cursor->nOffset = nOffset;
595 ME_SelectWord(ME_TextEditor *editor)
597 if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
598 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
599 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
600 ME_InvalidateSelection(editor);
601 ME_SendSelChange(editor);
605 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
607 ME_Cursor *pCursor = &editor->pCursors[nCursor];
609 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
610 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
613 int ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
615 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
621 while(p != editor->pBuffer->pLast)
623 if (p->type == diParagraph)
625 int ry = y - p->member.para.nYPos;
628 result->pRun = ME_FindItemFwd(p, diRun);
632 if (ry >= p->member.para.nHeight)
634 p = p->member.para.next_para;
637 p = ME_FindItemFwd(p, diStartRow);
641 if (p->type == diStartRow)
643 int ry = y - p->member.row.nYPos;
646 if (ry >= p->member.row.nHeight)
648 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
649 if (p->type != diStartRow)
653 p = ME_FindItemFwd(p, diRun);
656 if (p->type == diRun)
659 rx = x - p->member.run.pt.x;
662 if (rx >= p->member.run.nWidth) /* not this run yet... find next item */
667 if (p->type == diRun)
669 rx = x - p->member.run.pt.x;
670 goto continue_search;
672 if (p->type == diStartRow)
674 p = ME_FindItemFwd(p, diRun);
677 rx = 0; /* FIXME not sure */
680 if (p->type == diParagraph || p->type == diTextEnd)
682 rx = 0; /* FIXME not sure */
690 if (p->member.run.nFlags & MERF_ENDPARA)
693 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
694 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
696 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
705 result->pRun = ME_FindItemBack(p, diRun);
707 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
713 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
718 GetClientRect(editor->hWnd, &rc);
719 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
721 ME_FindPixelPos(editor, x, y, &cursor, NULL);
722 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
723 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
727 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
729 ME_Cursor tmp_cursor;
730 int is_selection = 0;
732 editor->nUDArrowX = -1;
734 y += ME_GetYScrollPos(editor);
736 tmp_cursor = editor->pCursors[0];
737 is_selection = ME_IsSelection(editor);
739 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
741 if (GetKeyState(VK_SHIFT)>=0)
743 editor->pCursors[1] = editor->pCursors[0];
748 editor->pCursors[1] = tmp_cursor;
752 ME_InvalidateSelection(editor);
753 HideCaret(editor->hWnd);
754 ME_MoveCaret(editor);
755 ShowCaret(editor->hWnd);
756 ME_ClearTempStyle(editor);
757 ME_SendSelChange(editor);
760 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
762 ME_Cursor tmp_cursor;
764 y += ME_GetYScrollPos(editor);
766 tmp_cursor = editor->pCursors[0];
767 if (!ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd))
770 if (tmp_cursor.pRun == editor->pCursors[0].pRun &&
771 tmp_cursor.nOffset == editor->pCursors[0].nOffset)
774 ME_InvalidateSelection(editor);
775 editor->pCursors[0] = tmp_cursor;
776 HideCaret(editor->hWnd);
777 ME_MoveCaret(editor);
778 ME_InvalidateSelection(editor);
779 ShowCaret(editor->hWnd);
780 ME_SendSelChange(editor);
783 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
784 int x, int *pOffset, int *pbCaretAtEnd)
786 ME_DisplayItem *pNext, *pLastRun;
787 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
788 assert(pNext->type == diRun);
790 *pbCaretAtEnd = FALSE;
792 int run_x = pNext->member.run.pt.x;
793 int width = pNext->member.run.nWidth;
796 if (pOffset) *pOffset = 0;
799 if (x >= run_x && x < run_x+width)
801 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
802 ME_String *s = pNext->member.run.strText;
810 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
811 } while(pNext && pNext->type == diRun);
813 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
815 pNext = ME_FindItemFwd(pNext, diRun);
816 if (pbCaretAtEnd) *pbCaretAtEnd = 1;
817 if (pOffset) *pOffset = 0;
820 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
821 if (pOffset) *pOffset = 0;
826 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
828 ME_DisplayItem *pRun = pCursor->pRun;
831 if (editor->nUDArrowX != -1)
832 x = editor->nUDArrowX;
834 if (editor->bCaretAtEnd)
836 pRun = ME_FindItemBack(pRun, diRun);
838 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
841 x = pRun->member.run.pt.x;
842 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
844 editor->nUDArrowX = x;
851 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
853 ME_DisplayItem *pRun = pCursor->pRun;
854 ME_DisplayItem *pItem;
855 int x = ME_GetXForArrow(editor, pCursor);
857 if (editor->bCaretAtEnd && !pCursor->nOffset)
858 pRun = ME_FindItemBack(pRun, diRun);
863 /* start of this row */
864 pItem = ME_FindItemBack(pRun, diStartRow);
866 /* start of the previous row */
867 pItem = ME_FindItemBack(pItem, diStartRow);
871 /* start of the next row */
872 pItem = ME_FindItemFwd(pRun, diStartRow);
873 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
878 /* row not found - ignore */
881 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
882 assert(pCursor->pRun);
883 assert(pCursor->pRun->type == diRun);
887 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
889 ME_DisplayItem *pRun = pCursor->pRun;
890 ME_DisplayItem *pLast, *p;
891 int x, y, ys, yd, yp, yprev;
892 ME_Cursor tmp_curs = *pCursor;
894 x = ME_GetXForArrow(editor, pCursor);
895 if (!pCursor->nOffset && editor->bCaretAtEnd)
896 pRun = ME_FindItemBack(pRun, diRun);
898 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
899 assert(p->type == diStartRow);
900 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
901 yprev = ys = y = yp + p->member.row.nYPos;
902 yd = y - editor->sizeWindow.cy;
906 p = ME_FindItemBack(p, diStartRowOrParagraph);
909 if (p->type == diParagraph) { /* crossing paragraphs */
910 if (p->member.para.prev_para == NULL)
912 yp = p->member.para.prev_para->member.para.nYPos;
915 y = yp + p->member.row.nYPos;
922 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
923 ME_UpdateSelection(editor, &tmp_curs);
924 if (yprev < editor->sizeWindow.cy)
926 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
930 ME_Scroll(editor, 0, ys-yprev);
933 assert(pCursor->pRun);
934 assert(pCursor->pRun->type == diRun);
937 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
938 of pixels, even if it makes the scroll bar position exceed its normal maximum.
939 In such a situation, clicking the scrollbar restores its position back to the
940 normal range (ie. sets it to (doclength-screenheight)). */
942 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
944 ME_DisplayItem *pRun = pCursor->pRun;
945 ME_DisplayItem *pLast, *p;
946 int x, y, ys, yd, yp, yprev;
947 ME_Cursor tmp_curs = *pCursor;
949 x = ME_GetXForArrow(editor, pCursor);
950 if (!pCursor->nOffset && editor->bCaretAtEnd)
951 pRun = ME_FindItemBack(pRun, diRun);
953 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
954 assert(p->type == diStartRow);
955 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
956 yprev = ys = y = yp + p->member.row.nYPos;
957 yd = y + editor->sizeWindow.cy;
961 p = ME_FindItemFwd(p, diStartRowOrParagraph);
964 if (p->type == diParagraph) {
965 yp = p->member.para.nYPos;
968 y = yp + p->member.row.nYPos;
975 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
976 ME_UpdateSelection(editor, &tmp_curs);
977 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
979 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
983 ME_Scroll(editor, 0, ys-yprev);
986 assert(pCursor->pRun);
987 assert(pCursor->pRun->type == diRun);
990 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
992 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
994 ME_DisplayItem *pRun;
995 if (editor->bCaretAtEnd && !pCursor->nOffset) {
996 pRow = ME_FindItemBack(pRow, diStartRow);
1000 pRun = ME_FindItemFwd(pRow, diRun);
1002 pCursor->pRun = pRun;
1003 pCursor->nOffset = 0;
1006 editor->bCaretAtEnd = FALSE;
1009 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1011 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1013 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1015 pCursor->pRun = pRun;
1016 pCursor->nOffset = 0;
1021 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1023 ME_DisplayItem *pRow;
1025 if (editor->bCaretAtEnd && !pCursor->nOffset)
1028 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1030 if (pRow->type == diStartRow) {
1031 /* FIXME WTF was I thinking about here ? */
1032 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1034 pCursor->pRun = pRun;
1035 pCursor->nOffset = 0;
1036 editor->bCaretAtEnd = 1;
1039 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1040 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1041 pCursor->nOffset = 0;
1042 editor->bCaretAtEnd = FALSE;
1045 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1047 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1049 p = ME_FindItemBack(p, diRun);
1051 assert(p->member.run.nFlags & MERF_ENDPARA);
1053 pCursor->nOffset = 0;
1054 editor->bCaretAtEnd = FALSE;
1057 BOOL ME_IsSelection(ME_TextEditor *editor)
1059 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1062 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1064 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1072 BOOL ME_UpdateSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
1074 ME_Cursor old_anchor = editor->pCursors[1];
1076 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1078 /* any selection was present ? if so, it's no more, repaint ! */
1079 editor->pCursors[1] = editor->pCursors[0];
1080 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1087 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1089 editor->pCursors[1] = *pTempCursor;
1098 void ME_DeleteSelection(ME_TextEditor *editor)
1101 ME_GetSelection(editor, &from, &to);
1102 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1105 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1111 ME_GetSelection(editor, &from, &to);
1112 ME_CursorFromCharOfs(editor, from, &c);
1114 style = c.pRun->member.run.style;
1115 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1118 style = ME_GetInsertStyle(editor, 0);
1122 void ME_SendSelChange(ME_TextEditor *editor)
1126 ME_ClearTempStyle(editor);
1128 if (!(editor->nEventMask & ENM_SELCHANGE))
1131 sc.nmhdr.hwndFrom = editor->hWnd;
1132 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1133 sc.nmhdr.code = EN_SELCHANGE;
1134 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1135 sc.seltyp = SEL_EMPTY;
1136 if (sc.chrg.cpMin != sc.chrg.cpMax)
1137 sc.seltyp |= SEL_TEXT;
1138 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1139 sc.seltyp |= SEL_MULTICHAR;
1140 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1145 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1148 ME_Cursor *p = &editor->pCursors[nCursor];
1149 ME_Cursor tmp_curs = *p;
1150 BOOL success = FALSE;
1152 if (ME_IsSelection(editor) && !extend)
1153 ME_InvalidateSelection(editor);
1155 editor->nUDArrowX = -1;
1158 editor->bCaretAtEnd = 0;
1160 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1162 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1165 editor->bCaretAtEnd = 0;
1167 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1169 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1172 ME_MoveCursorLines(editor, &tmp_curs, -1);
1175 ME_MoveCursorLines(editor, &tmp_curs, +1);
1178 ME_ArrowPageUp(editor, &tmp_curs);
1181 ME_ArrowPageDown(editor, &tmp_curs);
1185 ME_ArrowCtrlHome(editor, &tmp_curs);
1187 ME_ArrowHome(editor, &tmp_curs);
1188 editor->bCaretAtEnd = 0;
1193 ME_ArrowCtrlEnd(editor, &tmp_curs);
1195 ME_ArrowEnd(editor, &tmp_curs);
1200 editor->pCursors[1] = tmp_curs;
1203 if (ME_IsSelection(editor))
1204 ME_InvalidateSelection(editor);
1205 HideCaret(editor->hWnd);
1206 ME_EnsureVisible(editor, tmp_curs.pRun);
1207 ME_ShowCaret(editor);
1208 ME_SendSelChange(editor);