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-GetScrollPos(editor->hWnd, SB_VERT));
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 += GetScrollPos(editor->hWnd, SB_VERT);
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 += GetScrollPos(editor->hWnd, SB_VERT);
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_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
702 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
704 ME_DisplayItem *pRun;
705 if (editor->bCaretAtEnd && !pCursor->nOffset) {
706 pRow = ME_FindItemBack(pRow, diStartRow);
710 pRun = ME_FindItemFwd(pRow, diRun);
712 pCursor->pRun = pRun;
713 pCursor->nOffset = 0;
716 editor->bCaretAtEnd = FALSE;
719 void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
721 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
723 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
725 pCursor->pRun = pRun;
726 pCursor->nOffset = 0;
731 void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
733 ME_DisplayItem *pRow;
735 if (editor->bCaretAtEnd && !pCursor->nOffset)
738 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
740 if (pRow->type == diStartRow) {
741 /* FIXME WTF was I thinking about here ? */
742 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
744 pCursor->pRun = pRun;
745 pCursor->nOffset = 0;
746 editor->bCaretAtEnd = 1;
749 pCursor->pRun = ME_FindItemBack(pRow, diRun);
750 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
751 pCursor->nOffset = 0;
752 editor->bCaretAtEnd = FALSE;
755 void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
757 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
759 p = ME_FindItemBack(p, diRun);
761 assert(p->member.run.nFlags & MERF_ENDPARA);
763 pCursor->nOffset = 0;
764 editor->bCaretAtEnd = FALSE;
767 BOOL ME_IsSelection(ME_TextEditor *editor)
769 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
772 int ME_GetSelCursor(ME_TextEditor *editor, int dir)
774 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
782 BOOL ME_CancelSelection(ME_TextEditor *editor, int dir)
786 if (GetKeyState(VK_SHIFT)<0)
788 if (!memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor)))
791 cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
794 editor->pCursors[1] = editor->pCursors[0];
796 editor->pCursors[0] = editor->pCursors[1];
801 void ME_RepaintSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
803 ME_Cursor old_anchor = editor->pCursors[1];
805 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
807 /* any selection was present ? if so, it's no more, repaint ! */
808 editor->pCursors[1] = editor->pCursors[0];
809 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
817 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
819 editor->pCursors[1] = *pTempCursor;
826 void ME_DeleteSelection(ME_TextEditor *editor)
829 ME_GetSelection(editor, &from, &to);
830 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
833 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
839 ME_GetSelection(editor, &from, &to);
840 ME_CursorFromCharOfs(editor, from, &c);
842 style = c.pRun->member.run.style;
843 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
846 style = ME_GetInsertStyle(editor, 0);
850 void ME_SendSelChange(ME_TextEditor *editor)
853 if (!(editor->nEventMask & ENM_SELCHANGE))
855 sc.nmhdr.hwndFrom = editor->hWnd;
856 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
857 sc.nmhdr.code = EN_SELCHANGE;
858 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
859 sc.seltyp = SEL_EMPTY;
860 if (sc.chrg.cpMin != sc.chrg.cpMax)
861 sc.seltyp |= SEL_TEXT;
862 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
863 sc.seltyp |= SEL_MULTICHAR;
864 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
867 BOOL ME_ArrowKey(ME_TextEditor *editor, int nVKey, int nCtrl)
870 ME_Cursor *p = &editor->pCursors[nCursor];
871 ME_Cursor tmp_curs = *p;
875 ME_ArrowUp(editor, p);
876 ME_ClearTempStyle(editor);
877 ME_RepaintSelection(editor, &tmp_curs);
878 ME_SendSelChange(editor);
881 ME_ArrowDown(editor, p);
882 ME_ClearTempStyle(editor);
883 ME_RepaintSelection(editor, &tmp_curs);
884 ME_SendSelChange(editor);
888 editor->nUDArrowX = -1;
890 case VK_BACK: { /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
891 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
893 if (ME_IsSelection(editor))
895 editor->bCaretAtEnd = FALSE; /* FIXME or maybe not */
896 ME_DeleteSelection(editor);
897 ME_UpdateRepaint(editor);
900 if (ME_ArrowLeft(editor, p)) {
901 editor->bCaretAtEnd = FALSE; /* FIXME or maybe not */
902 ME_ClearTempStyle(editor);
903 ME_MoveCaret(editor);
904 ME_DeleteTextAtCursor(editor, nCursor, 1);
905 ME_UpdateRepaint(editor);
910 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
912 /* editor->bCaretAtEnd = 0; FIXME or maybe not */
913 if (ME_IsSelection(editor))
915 ME_DeleteSelection(editor);
916 ME_ClearTempStyle(editor);
917 ME_UpdateRepaint(editor);
920 ME_DeleteTextAtCursor(editor, nCursor, 1);
921 ME_ClearTempStyle(editor);
922 ME_UpdateRepaint(editor);
926 if (GetKeyState(VK_CONTROL)<0)
927 ME_ArrowCtrlHome(editor, p);
929 ME_ArrowHome(editor, p);
930 editor->bCaretAtEnd = 0;
931 ME_ClearTempStyle(editor);
932 ME_RepaintSelection(editor, &tmp_curs);
933 ME_SendSelChange(editor);
937 if (GetKeyState(VK_CONTROL)<0)
938 ME_ArrowCtrlEnd(editor, p);
940 ME_ArrowEnd(editor, p);
941 ME_ClearTempStyle(editor);
942 ME_RepaintSelection(editor, &tmp_curs);
943 ME_SendSelChange(editor);
946 editor->bCaretAtEnd = 0;
947 if (ME_CancelSelection(editor, -1))
949 ME_ArrowLeft(editor, p);
950 ME_RepaintSelection(editor, &tmp_curs);
951 ME_ClearTempStyle(editor);
952 ME_SendSelChange(editor);
955 editor->bCaretAtEnd = 0;
956 if (ME_CancelSelection(editor, +1))
958 ME_ArrowRight(editor, p);
959 ME_RepaintSelection(editor, &tmp_curs);
960 ME_ClearTempStyle(editor);
961 ME_SendSelChange(editor);