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);
44 void ME_SetSelection(ME_TextEditor *editor, int from, int to)
46 if (from == 0 && to == -1)
48 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
49 editor->pCursors[1].nOffset = 0;
50 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
51 editor->pCursors[0].nOffset = 0;
53 ME_ClearTempStyle(editor);
58 editor->pCursors[1] = editor->pCursors[0];
60 ME_ClearTempStyle(editor);
69 ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
70 ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);
73 void ME_MoveCaret(ME_TextEditor *editor)
75 HDC hDC = GetDC(editor->hWnd);
78 ME_Cursor *pCursor = &editor->pCursors[0];
79 ME_DisplayItem *pCursorRun = pCursor->pRun;
80 ME_DisplayItem *pSizeRun = pCursor->pRun;
82 ME_InitContext(&c, editor, hDC);
83 assert(!pCursor->nOffset || !editor->bCaretAtEnd);
85 if (pCursorRun->type == diRun) {
86 ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
88 ME_DisplayItem *run = pCursorRun;
91 if (!pCursor->nOffset && !editor->bCaretAtEnd)
93 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrStartRow);
94 if (prev->type == diRun)
97 assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
98 para = ME_FindItemBack(row, diParagraph);
99 if (editor->bCaretAtEnd && !pCursor->nOffset &&
100 run == ME_FindItemFwd(row, diRun))
102 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
103 if (tmp->type == diRun)
105 row = ME_FindItemBack(tmp, diStartRow);
106 pSizeRun = run = tmp;
107 sz = ME_GetRunSize(&c, &run->member.run, ME_StrLen(run->member.run.strText));
110 if (pCursor->nOffset && !(run->member.run.nFlags & MERF_SKIPPED)) {
111 sz = ME_GetRunSize(&c, &run->member.run, pCursor->nOffset);
113 CreateCaret(editor->hWnd, NULL, 0, pSizeRun->member.run.nAscent+pSizeRun->member.run.nDescent);
114 SetCaretPos(run->member.run.pt.x+sz.cx,
115 para->member.para.nYPos+row->member.row.nBaseline+pSizeRun->member.run.pt.y-pSizeRun->member.run.nAscent-ME_GetYScrollPos(editor));
117 assert(0 == "Wrapped paragraph run without a row?");
118 CreateCaret(editor->hWnd, NULL, 0, 10);
123 assert(0 == "Cursor not on a run");
124 CreateCaret(editor->hWnd, NULL, 0, 10); /* FIXME use global font */
127 ME_DestroyContext(&c);
128 ReleaseDC(editor->hWnd, hDC);
131 void ME_ShowCaret(ME_TextEditor *ed)
137 void ME_HideCaret(ME_TextEditor *ed)
143 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
152 ME_CursorFromCharOfs(editor, nOfs, &c);
153 run = &c.pRun->member.run;
154 if (run->nFlags & MERF_ENDPARA) {
155 if (!ME_FindItemFwd(c.pRun, diParagraph))
159 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
160 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
161 ME_CheckCharOffsets(editor);
168 int nIntendedChars = nChars;
169 int nCharsToDelete = nChars;
173 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
176 ME_StrRelPos(run->strText, loc, &nChars);
177 /* nChars is the number of characters that should be deleted from the
178 FOLLOWING runs (these AFTER cursor.pRun)
179 nCharsToDelete is a number of chars to delete from THIS run */
180 nCharsToDelete -= nChars;
181 shift -= nCharsToDelete;
182 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
183 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
184 debugstr_w(run->strText->szData), run->strText->nLen);
186 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
188 /* undo = reinsert whole run */
189 /* nOfs is a character offset (from the start of the document
190 to the current (deleted) run */
191 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
193 pUndo->di.member.run.nCharOfs = nOfs;
197 /* undo = reinsert partial run */
198 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
200 ME_DestroyString(pUndo->di.member.run.strText);
201 pUndo->di.member.run.nCharOfs = nOfs;
202 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
205 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
206 TRACE("Shift value: %d\n", shift);
207 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
209 /* update cursors (including c) */
210 for (i=-1; i<editor->nCursors; i++) {
211 ME_Cursor *pThisCur = editor->pCursors + i;
212 if (i == -1) pThisCur = &c;
213 if (pThisCur->pRun == cursor.pRun) {
214 if (pThisCur->nOffset > cursor.nOffset) {
215 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
216 pThisCur->nOffset = cursor.nOffset;
218 pThisCur->nOffset -= nCharsToDelete;
219 assert(pThisCur->nOffset >= 0);
220 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
222 if (pThisCur->nOffset == ME_StrVLen(run->strText))
224 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
225 assert(pThisCur->pRun->type == diRun);
226 pThisCur->nOffset = 0;
231 /* c = updated data now */
233 if (c.pRun == cursor.pRun)
234 ME_SkipAndPropagateCharOffset(c.pRun, shift);
236 ME_PropagateCharOffset(c.pRun, shift);
238 if (!ME_StrVLen(cursor.pRun->member.run.strText))
240 TRACE("Removing useless run\n");
241 ME_Remove(cursor.pRun);
242 ME_DestroyDisplayItem(cursor.pRun);
247 ME_CheckCharOffsets(editor);
254 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
257 assert(nCursor>=0 && nCursor<editor->nCursors);
258 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
261 static WCHAR wszSpace[] = {' ', 0};
263 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
264 void ME_InsertGraphicsFromCursor(ME_TextEditor *editor, int nCursor)
266 ME_Cursor *pCursor = &editor->pCursors[nCursor];
267 ME_DisplayItem *pItem = NULL;
268 ME_DisplayItem *pNewRun = NULL;
269 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
273 if (ME_IsSelection(editor))
274 ME_DeleteSelection(editor);
276 pUndo = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
278 pUndo->nStart = pCursor->nOffset + pCursor->pRun->member.run.nCharOfs + ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs;
281 if (pCursor->nOffset)
283 ME_SplitRunSimple(editor, pCursor->pRun, pCursor->nOffset);
285 pItem = pCursor->pRun;
286 pNewRun = ME_MakeRun(pStyle, ME_MakeStringN(wszSpace, 1), MERF_GRAPHICS);
287 pNewRun->member.run.nCharOfs = pCursor->pRun->member.run.nCharOfs;
288 ME_InsertBefore(pCursor->pRun, pNewRun);
289 ME_PropagateCharOffset(pItem, 1);
290 ME_CheckCharOffsets(editor);
291 ME_SendSelChange(editor);
294 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
295 const WCHAR *str, int len, ME_Style *style)
301 editor->bCaretAtEnd = FALSE;
303 ME_AddRefStyle(style);
305 /* FIXME really HERE ? */
306 if (ME_IsSelection(editor))
307 ME_DeleteSelection(editor);
309 assert(nCursor>=0 && nCursor<editor->nCursors);
313 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
314 while(pos-str < len && *pos != '\r' && *pos != '\n')
318 ME_DisplayItem *tp, *end_run;
322 ME_InsertTextFromCursor(editor, nCursor, str, pos-str, style);
323 p = &editor->pCursors[nCursor];
324 tp = ME_FindItemBack(p->pRun, diParagraph);
325 para = &tp->member.para;
328 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
329 p = &editor->pCursors[nCursor];
331 tmp_style = ME_GetInsertStyle(editor, nCursor);
332 /* ME_SplitParagraph increases style refcount */
333 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
334 p->pRun = ME_FindItemFwd(tp, diRun);
335 end_run = ME_FindItemBack(tp, diRun);
336 ME_ReleaseStyle(end_run->member.run.style);
337 end_run->member.run.style = tmp_style;
339 if(pos-str < len && *pos =='\r')
341 if(pos-str < len && *pos =='\n')
344 ME_InsertTextFromCursor(editor, nCursor, pos, len-(pos-str), style);
346 ME_ReleaseStyle(style);
349 p = &editor->pCursors[nCursor];
351 ME_DisplayItem *pNewRun = NULL;
353 assert(p->pRun->type == diRun);
354 pNewRun = ME_MakeRun(style, ME_MakeStringN(str, len), 0); /* addrefs style */
355 ME_InsertRun(editor, ME_CharOfsFromRunOfs(editor, p->pRun, p->nOffset), pNewRun);
356 ME_DestroyDisplayItem(pNewRun);
357 ME_ReleaseStyle(style);
364 BOOL ME_ArrowLeft(ME_TextEditor *editor, ME_Cursor *p)
367 p->nOffset = ME_StrRelPos2(p->pRun->member.run.strText, p->nOffset, -1);
372 ME_DisplayItem *pRun = ME_FindItemBack(p->pRun, diRunOrParagraph);
374 if (pRun->type == diRun) {
376 assert(p->pRun->type == diRun);
377 assert(pRun->member.run.strText->nLen);
378 p->nOffset = pRun->member.run.strText->nLen;
380 p->nOffset = ME_StrRelPos2(pRun->member.run.strText, p->nOffset, -1);
386 if (pRun->type == diParagraph)
388 if (pRun->member.para.prev_para->type == diTextStart)
390 assert(pRun->member.para.prev_para->type == diParagraph);
391 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
392 /* every paragraph ought to have at least one run */
393 assert(pRun && pRun->type == diRun);
394 assert(pRun->member.run.nFlags & MERF_ENDPARA);
404 BOOL ME_ArrowRight(ME_TextEditor *editor, ME_Cursor *p)
406 int new_ofs = ME_StrRelPos2(p->pRun->member.run.strText, p->nOffset, 1);
407 if (new_ofs<p->pRun->member.run.strText->nLen) {
408 p->nOffset = new_ofs;
412 ME_DisplayItem *pRun = ME_FindItemFwd(p->pRun, diRun);
415 assert(p->pRun->type == diRun);
422 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
424 ME_Cursor *pCursor = &editor->pCursors[nCursor];
426 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
427 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
430 int ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
432 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
438 while(p != editor->pBuffer->pLast)
440 if (p->type == diParagraph)
442 int ry = y - p->member.para.nYPos;
445 result->pRun = ME_FindItemFwd(p, diRun);
449 if (ry >= p->member.para.nHeight)
451 p = p->member.para.next_para;
454 p = ME_FindItemFwd(p, diStartRow);
458 if (p->type == diStartRow)
460 int ry = y - p->member.row.nYPos;
463 if (ry >= p->member.row.nHeight)
465 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
466 if (p->type != diStartRow)
470 p = ME_FindItemFwd(p, diRun);
473 if (p->type == diRun)
476 rx = x - p->member.run.pt.x;
479 if (rx >= p->member.run.nWidth) /* not this run yet... find next item */
484 if (p->type == diRun)
486 rx = x - p->member.run.pt.x;
487 goto continue_search;
489 if (p->type == diStartRow)
491 p = ME_FindItemFwd(p, diRun);
494 rx = 0; /* FIXME not sure */
497 if (p->type == diParagraph || p->type == diTextEnd)
499 rx = 0; /* FIXME not sure */
507 if (p->member.run.nFlags & MERF_ENDPARA)
510 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
511 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
513 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
522 result->pRun = ME_FindItemBack(p, diRun);
524 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
528 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
530 ME_Cursor tmp_cursor;
531 int is_selection = 0;
533 editor->nUDArrowX = -1;
535 y += ME_GetYScrollPos(editor);
537 tmp_cursor = editor->pCursors[0];
538 is_selection = ME_IsSelection(editor);
540 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
542 if (GetKeyState(VK_SHIFT)>=0)
544 editor->pCursors[1] = editor->pCursors[0];
549 editor->pCursors[1] = tmp_cursor;
553 HideCaret(editor->hWnd);
554 ME_MoveCaret(editor);
557 ShowCaret(editor->hWnd);
558 ME_ClearTempStyle(editor);
559 ME_SendSelChange(editor);
562 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
564 ME_Cursor tmp_cursor;
566 y += ME_GetYScrollPos(editor);
568 tmp_cursor = editor->pCursors[0];
569 if (!ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd))
572 if (tmp_cursor.pRun == editor->pCursors[0].pRun &&
573 tmp_cursor.nOffset == editor->pCursors[0].nOffset)
576 HideCaret(editor->hWnd);
577 ME_MoveCaret(editor);
579 ShowCaret(editor->hWnd);
580 ME_SendSelChange(editor);
583 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
584 int x, int *pOffset, int *pbCaretAtEnd)
586 ME_DisplayItem *pNext, *pLastRun;
587 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
588 assert(pNext->type == diRun);
590 *pbCaretAtEnd = FALSE;
592 int run_x = pNext->member.run.pt.x;
593 int width = pNext->member.run.nWidth;
596 if (pOffset) *pOffset = 0;
599 if (x >= run_x && x < run_x+width)
601 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
602 ME_String *s = pNext->member.run.strText;
610 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
611 } while(pNext && pNext->type == diRun);
613 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
615 pNext = ME_FindItemFwd(pNext, diRun);
616 if (pbCaretAtEnd) *pbCaretAtEnd = 1;
617 if (pOffset) *pOffset = 0;
620 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
621 if (pOffset) *pOffset = 0;
626 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
628 ME_DisplayItem *pRun = pCursor->pRun;
631 if (editor->nUDArrowX != -1)
632 x = editor->nUDArrowX;
634 if (editor->bCaretAtEnd)
636 pRun = ME_FindItemBack(pRun, diRun);
638 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
641 x = pRun->member.run.pt.x;
642 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
644 editor->nUDArrowX = x;
649 void ME_ArrowUp(ME_TextEditor *editor, ME_Cursor *pCursor)
651 ME_DisplayItem *pRun = pCursor->pRun;
652 ME_DisplayItem *pItem, *pItem2;
653 int x = ME_GetXForArrow(editor, pCursor);
655 if (editor->bCaretAtEnd && !pCursor->nOffset)
657 pRun = ME_FindItemBack(pRun, diRun);
662 /* start of this row */
663 pItem = ME_FindItemBack(pRun, diStartRow);
665 /* start of the previous row */
666 pItem2 = ME_FindItemBack(pItem, diStartRow);
667 /* no previous row = the first line of the first paragraph */
668 if (!pItem2) /* can't go up - don't go BOL (as in MS richedit) */
671 ME_WrapTextParagraph(editor, ME_FindItemBack(pItem2, diParagraph));
673 pCursor->pRun = ME_FindRunInRow(editor, pItem2, x, &pCursor->nOffset, &editor->bCaretAtEnd);
676 void ME_ArrowDown(ME_TextEditor *editor, ME_Cursor *pCursor)
678 ME_DisplayItem *pRun = pCursor->pRun;
679 ME_DisplayItem *pItem;
680 int x = ME_GetXForArrow(editor, pCursor);
681 if (!pCursor->nOffset && editor->bCaretAtEnd)
683 pRun = ME_FindItemBack(pRun, diRun);
684 /* x = pRun->member.run.pt.x + pRun->member.run.nWidth; */
686 /* start of the next row */
687 pItem = ME_FindItemFwd(pRun, diStartRow);
688 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
692 /* next row not found - ignore */
695 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
696 assert(pCursor->pRun);
697 assert(pCursor->pRun->type == diRun);
700 void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
702 ME_DisplayItem *pRun = pCursor->pRun;
703 ME_DisplayItem *pLast, *p;
704 int x, y, ys, yd, yp, yprev;
705 ME_Cursor tmp_curs = *pCursor;
707 x = ME_GetXForArrow(editor, pCursor);
708 if (!pCursor->nOffset && editor->bCaretAtEnd)
709 pRun = ME_FindItemBack(pRun, diRun);
711 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
712 assert(p->type == diStartRow);
713 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
714 yprev = ys = y = yp + p->member.row.nYPos;
715 yd = y - editor->sizeWindow.cy;
719 p = ME_FindItemBack(p, diStartRowOrParagraph);
722 if (p->type == diParagraph) { /* crossing paragraphs */
723 if (p->member.para.prev_para == NULL)
725 yp = p->member.para.prev_para->member.para.nYPos;
728 y = yp + p->member.row.nYPos;
735 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
736 ME_UpdateSelection(editor, &tmp_curs);
737 if (yprev < editor->sizeWindow.cy)
739 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
743 ME_Scroll(editor, 0, ys-yprev);
746 assert(pCursor->pRun);
747 assert(pCursor->pRun->type == diRun);
750 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
751 of pixels, even if it makes the scroll bar position exceed its normal maximum.
752 In such a situation, clicking the scrollbar restores its position back to the
753 normal range (ie. sets it to (doclength-screenheight)). */
755 void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
757 ME_DisplayItem *pRun = pCursor->pRun;
758 ME_DisplayItem *pLast, *p;
759 int x, y, ys, yd, yp, yprev;
760 ME_Cursor tmp_curs = *pCursor;
762 x = ME_GetXForArrow(editor, pCursor);
763 if (!pCursor->nOffset && editor->bCaretAtEnd)
764 pRun = ME_FindItemBack(pRun, diRun);
766 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
767 assert(p->type == diStartRow);
768 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
769 yprev = ys = y = yp + p->member.row.nYPos;
770 yd = y + editor->sizeWindow.cy;
774 p = ME_FindItemFwd(p, diStartRowOrParagraph);
777 if (p->type == diParagraph) {
778 yp = p->member.para.nYPos;
781 y = yp + p->member.row.nYPos;
788 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
789 ME_UpdateSelection(editor, &tmp_curs);
790 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
792 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
796 ME_Scroll(editor, 0, ys-yprev);
799 assert(pCursor->pRun);
800 assert(pCursor->pRun->type == diRun);
803 void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
805 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
807 ME_DisplayItem *pRun;
808 if (editor->bCaretAtEnd && !pCursor->nOffset) {
809 pRow = ME_FindItemBack(pRow, diStartRow);
813 pRun = ME_FindItemFwd(pRow, diRun);
815 pCursor->pRun = pRun;
816 pCursor->nOffset = 0;
819 editor->bCaretAtEnd = FALSE;
822 void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
824 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
826 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
828 pCursor->pRun = pRun;
829 pCursor->nOffset = 0;
834 void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
836 ME_DisplayItem *pRow;
838 if (editor->bCaretAtEnd && !pCursor->nOffset)
841 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
843 if (pRow->type == diStartRow) {
844 /* FIXME WTF was I thinking about here ? */
845 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
847 pCursor->pRun = pRun;
848 pCursor->nOffset = 0;
849 editor->bCaretAtEnd = 1;
852 pCursor->pRun = ME_FindItemBack(pRow, diRun);
853 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
854 pCursor->nOffset = 0;
855 editor->bCaretAtEnd = FALSE;
858 void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
860 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
862 p = ME_FindItemBack(p, diRun);
864 assert(p->member.run.nFlags & MERF_ENDPARA);
866 pCursor->nOffset = 0;
867 editor->bCaretAtEnd = FALSE;
870 BOOL ME_IsSelection(ME_TextEditor *editor)
872 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
875 int ME_GetSelCursor(ME_TextEditor *editor, int dir)
877 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
885 BOOL ME_CancelSelection(ME_TextEditor *editor, int dir)
889 if (GetKeyState(VK_SHIFT)<0)
891 if (!memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor)))
894 cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
897 editor->pCursors[1] = editor->pCursors[0];
899 editor->pCursors[0] = editor->pCursors[1];
904 BOOL ME_UpdateSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
906 ME_Cursor old_anchor = editor->pCursors[1];
908 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
910 /* any selection was present ? if so, it's no more, repaint ! */
911 editor->pCursors[1] = editor->pCursors[0];
912 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
919 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
921 editor->pCursors[1] = *pTempCursor;
930 void ME_RepaintSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
932 if (ME_UpdateSelection(editor, pTempCursor)) {
933 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
938 void ME_DeleteSelection(ME_TextEditor *editor)
941 ME_GetSelection(editor, &from, &to);
942 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
945 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
951 ME_GetSelection(editor, &from, &to);
952 ME_CursorFromCharOfs(editor, from, &c);
954 style = c.pRun->member.run.style;
955 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
958 style = ME_GetInsertStyle(editor, 0);
962 void ME_SendSelChange(ME_TextEditor *editor)
965 if (!(editor->nEventMask & ENM_SELCHANGE))
967 sc.nmhdr.hwndFrom = editor->hWnd;
968 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
969 sc.nmhdr.code = EN_SELCHANGE;
970 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
971 sc.seltyp = SEL_EMPTY;
972 if (sc.chrg.cpMin != sc.chrg.cpMax)
973 sc.seltyp |= SEL_TEXT;
974 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
975 sc.seltyp |= SEL_MULTICHAR;
976 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
979 BOOL ME_ArrowKey(ME_TextEditor *editor, int nVKey, int nCtrl)
982 ME_Cursor *p = &editor->pCursors[nCursor];
983 ME_Cursor tmp_curs = *p;
987 ME_ArrowUp(editor, p);
988 ME_ClearTempStyle(editor);
989 ME_RepaintSelection(editor, &tmp_curs);
990 ME_SendSelChange(editor);
993 ME_ArrowDown(editor, p);
994 ME_ClearTempStyle(editor);
995 ME_RepaintSelection(editor, &tmp_curs);
996 ME_SendSelChange(editor);
999 ME_ArrowPageUp(editor, p);
1000 ME_ClearTempStyle(editor);
1001 ME_SendSelChange(editor);
1004 ME_ArrowPageDown(editor, p);
1005 ME_ClearTempStyle(editor);
1006 ME_SendSelChange(editor);
1010 editor->nUDArrowX = -1;
1012 case VK_BACK: { /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1013 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1015 if (ME_IsSelection(editor))
1017 editor->bCaretAtEnd = FALSE; /* FIXME or maybe not */
1018 ME_DeleteSelection(editor);
1019 ME_UpdateRepaint(editor);
1022 if (ME_ArrowLeft(editor, p)) {
1023 editor->bCaretAtEnd = FALSE; /* FIXME or maybe not */
1024 ME_ClearTempStyle(editor);
1025 ME_MoveCaret(editor);
1026 ME_DeleteTextAtCursor(editor, nCursor, 1);
1027 ME_UpdateRepaint(editor);
1032 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1034 /* editor->bCaretAtEnd = 0; FIXME or maybe not */
1035 if (ME_IsSelection(editor))
1037 ME_DeleteSelection(editor);
1038 ME_ClearTempStyle(editor);
1039 ME_UpdateRepaint(editor);
1042 ME_DeleteTextAtCursor(editor, nCursor, 1);
1043 ME_ClearTempStyle(editor);
1044 ME_UpdateRepaint(editor);
1048 if (GetKeyState(VK_CONTROL)<0)
1049 ME_ArrowCtrlHome(editor, p);
1051 ME_ArrowHome(editor, p);
1052 editor->bCaretAtEnd = 0;
1053 ME_ClearTempStyle(editor);
1054 ME_RepaintSelection(editor, &tmp_curs);
1055 ME_SendSelChange(editor);
1059 if (GetKeyState(VK_CONTROL)<0)
1060 ME_ArrowCtrlEnd(editor, p);
1062 ME_ArrowEnd(editor, p);
1063 ME_ClearTempStyle(editor);
1064 ME_RepaintSelection(editor, &tmp_curs);
1065 ME_SendSelChange(editor);
1068 editor->bCaretAtEnd = 0;
1069 if (ME_CancelSelection(editor, -1))
1071 ME_ArrowLeft(editor, p);
1072 ME_RepaintSelection(editor, &tmp_curs);
1073 ME_ClearTempStyle(editor);
1074 ME_SendSelChange(editor);
1077 editor->bCaretAtEnd = 0;
1078 if (ME_CancelSelection(editor, +1))
1080 ME_ArrowRight(editor, p);
1081 ME_RepaintSelection(editor, &tmp_curs);
1082 ME_ClearTempStyle(editor);
1083 ME_SendSelChange(editor);