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
, ¶
->member
.para
, &run
->member
.run
, ME_StrLen(run
->member
.run
.strText
));
110 if (pCursor
->nOffset
&& !(run
->member
.run
.nFlags
& MERF_SKIPPED
)) {
111 sz
= ME_GetRunSize(&c
, ¶
->member
.para
, &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' && *pos
!= '\t')
316 if (pos
-str
< len
&& *pos
== '\t') { /* handle tabs */
317 ME_DisplayItem
*pNewRun
= NULL
;
321 ME_InsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
);
323 p
= &editor
->pCursors
[nCursor
];
325 assert(p
->pRun
->type
== diRun
);
326 pNewRun
= ME_MakeRun(style
, ME_MakeStringN(&tab
, 1), MERF_TAB
); /* addrefs style */
327 ME_InsertRun(editor
, ME_CharOfsFromRunOfs(editor
, p
->pRun
, p
->nOffset
), pNewRun
);
328 ME_DestroyDisplayItem(pNewRun
);
329 ME_ReleaseStyle(style
);
333 ME_InsertTextFromCursor(editor
, nCursor
, pos
, len
-(pos
-str
), style
);
337 if (pos
-str
< len
) { /* handle EOLs */
338 ME_DisplayItem
*tp
, *end_run
;
342 ME_InsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
);
343 p
= &editor
->pCursors
[nCursor
];
344 tp
= ME_FindItemBack(p
->pRun
, diParagraph
);
345 para
= &tp
->member
.para
;
348 ME_SplitRunSimple(editor
, p
->pRun
, p
->nOffset
);
349 p
= &editor
->pCursors
[nCursor
];
351 tmp_style
= ME_GetInsertStyle(editor
, nCursor
);
352 /* ME_SplitParagraph increases style refcount */
353 tp
= ME_SplitParagraph(editor
, p
->pRun
, p
->pRun
->member
.run
.style
);
354 p
->pRun
= ME_FindItemFwd(tp
, diRun
);
355 end_run
= ME_FindItemBack(tp
, diRun
);
356 ME_ReleaseStyle(end_run
->member
.run
.style
);
357 end_run
->member
.run
.style
= tmp_style
;
359 if(pos
-str
< len
&& *pos
=='\r')
361 if(pos
-str
< len
&& *pos
=='\n')
364 ME_InsertTextFromCursor(editor
, nCursor
, pos
, len
-(pos
-str
), style
);
366 ME_ReleaseStyle(style
);
369 p
= &editor
->pCursors
[nCursor
];
371 ME_DisplayItem
*pNewRun
= NULL
;
373 assert(p
->pRun
->type
== diRun
);
374 pNewRun
= ME_MakeRun(style
, ME_MakeStringN(str
, len
), 0); /* addrefs style */
375 ME_InsertRun(editor
, ME_CharOfsFromRunOfs(editor
, p
->pRun
, p
->nOffset
), pNewRun
);
376 ME_DestroyDisplayItem(pNewRun
);
377 ME_ReleaseStyle(style
);
384 BOOL
ME_ArrowLeft(ME_TextEditor
*editor
, ME_Cursor
*p
)
387 p
->nOffset
= ME_StrRelPos2(p
->pRun
->member
.run
.strText
, p
->nOffset
, -1);
392 ME_DisplayItem
*pRun
= ME_FindItemBack(p
->pRun
, diRunOrParagraph
);
394 if (pRun
->type
== diRun
) {
396 assert(p
->pRun
->type
== diRun
);
397 assert(pRun
->member
.run
.strText
->nLen
);
398 p
->nOffset
= pRun
->member
.run
.strText
->nLen
;
400 p
->nOffset
= ME_StrRelPos2(pRun
->member
.run
.strText
, p
->nOffset
, -1);
406 if (pRun
->type
== diParagraph
)
408 if (pRun
->member
.para
.prev_para
->type
== diTextStart
)
410 assert(pRun
->member
.para
.prev_para
->type
== diParagraph
);
411 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
412 /* every paragraph ought to have at least one run */
413 assert(pRun
&& pRun
->type
== diRun
);
414 assert(pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
424 BOOL
ME_ArrowRight(ME_TextEditor
*editor
, ME_Cursor
*p
)
426 int new_ofs
= ME_StrRelPos2(p
->pRun
->member
.run
.strText
, p
->nOffset
, 1);
427 if (new_ofs
<p
->pRun
->member
.run
.strText
->nLen
) {
428 p
->nOffset
= new_ofs
;
432 ME_DisplayItem
*pRun
= ME_FindItemFwd(p
->pRun
, diRun
);
435 assert(p
->pRun
->type
== diRun
);
442 int ME_GetCursorOfs(ME_TextEditor
*editor
, int nCursor
)
444 ME_Cursor
*pCursor
= &editor
->pCursors
[nCursor
];
446 return ME_GetParagraph(pCursor
->pRun
)->member
.para
.nCharOfs
447 + pCursor
->pRun
->member
.run
.nCharOfs
+ pCursor
->nOffset
;
450 int ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
, ME_Cursor
*result
, BOOL
*is_eol
)
452 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
458 while(p
!= editor
->pBuffer
->pLast
)
460 if (p
->type
== diParagraph
)
462 int ry
= y
- p
->member
.para
.nYPos
;
465 result
->pRun
= ME_FindItemFwd(p
, diRun
);
469 if (ry
>= p
->member
.para
.nHeight
)
471 p
= p
->member
.para
.next_para
;
474 p
= ME_FindItemFwd(p
, diStartRow
);
478 if (p
->type
== diStartRow
)
480 int ry
= y
- p
->member
.row
.nYPos
;
483 if (ry
>= p
->member
.row
.nHeight
)
485 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
486 if (p
->type
!= diStartRow
)
490 p
= ME_FindItemFwd(p
, diRun
);
493 if (p
->type
== diRun
)
496 rx
= x
- p
->member
.run
.pt
.x
;
499 if (rx
>= p
->member
.run
.nWidth
) /* not this run yet... find next item */
504 if (p
->type
== diRun
)
506 rx
= x
- p
->member
.run
.pt
.x
;
507 goto continue_search
;
509 if (p
->type
== diStartRow
)
511 p
= ME_FindItemFwd(p
, diRun
);
514 rx
= 0; /* FIXME not sure */
517 if (p
->type
== diParagraph
|| p
->type
== diTextEnd
)
519 rx
= 0; /* FIXME not sure */
527 if (p
->member
.run
.nFlags
& MERF_ENDPARA
)
530 result
->nOffset
= ME_CharFromPointCursor(editor
, rx
, &p
->member
.run
);
531 if (editor
->pCursors
[0].nOffset
== p
->member
.run
.strText
->nLen
&& rx
)
533 result
->pRun
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, diRun
);
542 result
->pRun
= ME_FindItemBack(p
, diRun
);
544 assert(result
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
548 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
)
550 ME_Cursor tmp_cursor
;
551 int is_selection
= 0;
553 editor
->nUDArrowX
= -1;
555 y
+= ME_GetYScrollPos(editor
);
557 tmp_cursor
= editor
->pCursors
[0];
558 is_selection
= ME_IsSelection(editor
);
560 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
562 if (GetKeyState(VK_SHIFT
)>=0)
564 editor
->pCursors
[1] = editor
->pCursors
[0];
569 editor
->pCursors
[1] = tmp_cursor
;
573 HideCaret(editor
->hWnd
);
574 ME_MoveCaret(editor
);
577 ShowCaret(editor
->hWnd
);
578 ME_ClearTempStyle(editor
);
579 ME_SendSelChange(editor
);
582 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
584 ME_Cursor tmp_cursor
;
586 y
+= ME_GetYScrollPos(editor
);
588 tmp_cursor
= editor
->pCursors
[0];
589 if (!ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
))
592 if (tmp_cursor
.pRun
== editor
->pCursors
[0].pRun
&&
593 tmp_cursor
.nOffset
== editor
->pCursors
[0].nOffset
)
596 HideCaret(editor
->hWnd
);
597 ME_MoveCaret(editor
);
599 ShowCaret(editor
->hWnd
);
600 ME_SendSelChange(editor
);
603 static ME_DisplayItem
*ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
604 int x
, int *pOffset
, int *pbCaretAtEnd
)
606 ME_DisplayItem
*pNext
, *pLastRun
;
607 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
608 assert(pNext
->type
== diRun
);
610 *pbCaretAtEnd
= FALSE
;
612 int run_x
= pNext
->member
.run
.pt
.x
;
613 int width
= pNext
->member
.run
.nWidth
;
616 if (pOffset
) *pOffset
= 0;
619 if (x
>= run_x
&& x
< run_x
+width
)
621 int ch
= ME_CharFromPointCursor(editor
, x
-run_x
, &pNext
->member
.run
);
622 ME_String
*s
= pNext
->member
.run
.strText
;
630 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
631 } while(pNext
&& pNext
->type
== diRun
);
633 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
635 pNext
= ME_FindItemFwd(pNext
, diRun
);
636 if (pbCaretAtEnd
) *pbCaretAtEnd
= 1;
637 if (pOffset
) *pOffset
= 0;
640 if (pbCaretAtEnd
) *pbCaretAtEnd
= 0;
641 if (pOffset
) *pOffset
= 0;
646 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
648 ME_DisplayItem
*pRun
= pCursor
->pRun
;
651 if (editor
->nUDArrowX
!= -1)
652 x
= editor
->nUDArrowX
;
654 if (editor
->bCaretAtEnd
)
656 pRun
= ME_FindItemBack(pRun
, diRun
);
658 x
= pRun
->member
.run
.pt
.x
+ pRun
->member
.run
.nWidth
;
661 x
= pRun
->member
.run
.pt
.x
;
662 x
+= ME_PointFromChar(editor
, &pRun
->member
.run
, pCursor
->nOffset
);
664 editor
->nUDArrowX
= x
;
669 void ME_ArrowUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
671 ME_DisplayItem
*pRun
= pCursor
->pRun
;
672 ME_DisplayItem
*pItem
, *pItem2
;
673 int x
= ME_GetXForArrow(editor
, pCursor
);
675 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
677 pRun
= ME_FindItemBack(pRun
, diRun
);
682 /* start of this row */
683 pItem
= ME_FindItemBack(pRun
, diStartRow
);
685 /* start of the previous row */
686 pItem2
= ME_FindItemBack(pItem
, diStartRow
);
687 /* no previous row = the first line of the first paragraph */
688 if (!pItem2
) /* can't go up - don't go BOL (as in MS richedit) */
691 ME_WrapTextParagraph(editor, ME_FindItemBack(pItem2, diParagraph));
693 pCursor
->pRun
= ME_FindRunInRow(editor
, pItem2
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
696 void ME_ArrowDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
698 ME_DisplayItem
*pRun
= pCursor
->pRun
;
699 ME_DisplayItem
*pItem
;
700 int x
= ME_GetXForArrow(editor
, pCursor
);
701 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
703 pRun
= ME_FindItemBack(pRun
, diRun
);
704 /* x = pRun->member.run.pt.x + pRun->member.run.nWidth; */
706 /* start of the next row */
707 pItem
= ME_FindItemFwd(pRun
, diStartRow
);
708 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
712 /* next row not found - ignore */
715 pCursor
->pRun
= ME_FindRunInRow(editor
, pItem
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
716 assert(pCursor
->pRun
);
717 assert(pCursor
->pRun
->type
== diRun
);
720 void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
722 ME_DisplayItem
*pRun
= pCursor
->pRun
;
723 ME_DisplayItem
*pLast
, *p
;
724 int x
, y
, ys
, yd
, yp
, yprev
;
725 ME_Cursor tmp_curs
= *pCursor
;
727 x
= ME_GetXForArrow(editor
, pCursor
);
728 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
729 pRun
= ME_FindItemBack(pRun
, diRun
);
731 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
732 assert(p
->type
== diStartRow
);
733 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
734 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
735 yd
= y
- editor
->sizeWindow
.cy
;
739 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
742 if (p
->type
== diParagraph
) { /* crossing paragraphs */
743 if (p
->member
.para
.prev_para
== NULL
)
745 yp
= p
->member
.para
.prev_para
->member
.para
.nYPos
;
748 y
= yp
+ p
->member
.row
.nYPos
;
755 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
756 ME_UpdateSelection(editor
, &tmp_curs
);
757 if (yprev
< editor
->sizeWindow
.cy
)
759 ME_EnsureVisible(editor
, ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
));
763 ME_Scroll(editor
, 0, ys
-yprev
);
766 assert(pCursor
->pRun
);
767 assert(pCursor
->pRun
->type
== diRun
);
770 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
771 of pixels, even if it makes the scroll bar position exceed its normal maximum.
772 In such a situation, clicking the scrollbar restores its position back to the
773 normal range (ie. sets it to (doclength-screenheight)). */
775 void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
777 ME_DisplayItem
*pRun
= pCursor
->pRun
;
778 ME_DisplayItem
*pLast
, *p
;
779 int x
, y
, ys
, yd
, yp
, yprev
;
780 ME_Cursor tmp_curs
= *pCursor
;
782 x
= ME_GetXForArrow(editor
, pCursor
);
783 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
784 pRun
= ME_FindItemBack(pRun
, diRun
);
786 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
787 assert(p
->type
== diStartRow
);
788 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
789 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
790 yd
= y
+ editor
->sizeWindow
.cy
;
794 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
797 if (p
->type
== diParagraph
) {
798 yp
= p
->member
.para
.nYPos
;
801 y
= yp
+ p
->member
.row
.nYPos
;
808 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
809 ME_UpdateSelection(editor
, &tmp_curs
);
810 if (yprev
>= editor
->nTotalLength
-editor
->sizeWindow
.cy
)
812 ME_EnsureVisible(editor
, ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
));
816 ME_Scroll(editor
, 0, ys
-yprev
);
819 assert(pCursor
->pRun
);
820 assert(pCursor
->pRun
->type
== diRun
);
823 void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
825 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
827 ME_DisplayItem
*pRun
;
828 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
829 pRow
= ME_FindItemBack(pRow
, diStartRow
);
833 pRun
= ME_FindItemFwd(pRow
, diRun
);
835 pCursor
->pRun
= pRun
;
836 pCursor
->nOffset
= 0;
839 editor
->bCaretAtEnd
= FALSE
;
842 void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
844 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diTextStart
);
846 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
848 pCursor
->pRun
= pRun
;
849 pCursor
->nOffset
= 0;
854 void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
856 ME_DisplayItem
*pRow
;
858 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
861 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
863 if (pRow
->type
== diStartRow
) {
864 /* FIXME WTF was I thinking about here ? */
865 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
867 pCursor
->pRun
= pRun
;
868 pCursor
->nOffset
= 0;
869 editor
->bCaretAtEnd
= 1;
872 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
873 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
874 pCursor
->nOffset
= 0;
875 editor
->bCaretAtEnd
= FALSE
;
878 void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
880 ME_DisplayItem
*p
= ME_FindItemFwd(pCursor
->pRun
, diTextEnd
);
882 p
= ME_FindItemBack(p
, diRun
);
884 assert(p
->member
.run
.nFlags
& MERF_ENDPARA
);
886 pCursor
->nOffset
= 0;
887 editor
->bCaretAtEnd
= FALSE
;
890 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
892 return memcmp(&editor
->pCursors
[0], &editor
->pCursors
[1], sizeof(ME_Cursor
))!=0;
895 int ME_GetSelCursor(ME_TextEditor
*editor
, int dir
)
897 int cdir
= ME_GetCursorOfs(editor
, 0) - ME_GetCursorOfs(editor
, 1);
905 BOOL
ME_CancelSelection(ME_TextEditor
*editor
, int dir
)
909 if (GetKeyState(VK_SHIFT
)<0)
911 if (!memcmp(&editor
->pCursors
[0], &editor
->pCursors
[1], sizeof(ME_Cursor
)))
914 cdir
= ME_GetCursorOfs(editor
, 0) - ME_GetCursorOfs(editor
, 1);
917 editor
->pCursors
[1] = editor
->pCursors
[0];
919 editor
->pCursors
[0] = editor
->pCursors
[1];
924 BOOL
ME_UpdateSelection(ME_TextEditor
*editor
, ME_Cursor
*pTempCursor
)
926 ME_Cursor old_anchor
= editor
->pCursors
[1];
928 if (GetKeyState(VK_SHIFT
)>=0) /* cancelling selection */
930 /* any selection was present ? if so, it's no more, repaint ! */
931 editor
->pCursors
[1] = editor
->pCursors
[0];
932 if (memcmp(pTempCursor
, &old_anchor
, sizeof(ME_Cursor
))) {
939 if (!memcmp(pTempCursor
, &editor
->pCursors
[1], sizeof(ME_Cursor
))) /* starting selection */
941 editor
->pCursors
[1] = *pTempCursor
;
950 void ME_RepaintSelection(ME_TextEditor
*editor
, ME_Cursor
*pTempCursor
)
952 if (ME_UpdateSelection(editor
, pTempCursor
)) {
953 ME_EnsureVisible(editor
, editor
->pCursors
[0].pRun
);
958 void ME_DeleteSelection(ME_TextEditor
*editor
)
961 ME_GetSelection(editor
, &from
, &to
);
962 ME_DeleteTextAtCursor(editor
, ME_GetSelCursor(editor
,-1), to
-from
);
965 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
971 ME_GetSelection(editor
, &from
, &to
);
972 ME_CursorFromCharOfs(editor
, from
, &c
);
974 style
= c
.pRun
->member
.run
.style
;
975 ME_AddRefStyle(style
); /* ME_GetInsertStyle has already done that */
978 style
= ME_GetInsertStyle(editor
, 0);
982 void ME_SendSelChange(ME_TextEditor
*editor
)
985 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
987 sc
.nmhdr
.hwndFrom
= editor
->hWnd
;
988 sc
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
989 sc
.nmhdr
.code
= EN_SELCHANGE
;
990 SendMessageW(editor
->hWnd
, EM_EXGETSEL
, 0, (LPARAM
)&sc
.chrg
);
991 sc
.seltyp
= SEL_EMPTY
;
992 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
993 sc
.seltyp
|= SEL_TEXT
;
994 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* wth were RICHEDIT authors thinking ? */
995 sc
.seltyp
|= SEL_MULTICHAR
;
996 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
, sc
.nmhdr
.idFrom
, (LPARAM
)&sc
);
999 BOOL
ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, int nCtrl
)
1002 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1003 ME_Cursor tmp_curs
= *p
;
1007 ME_ArrowUp(editor
, p
);
1008 ME_ClearTempStyle(editor
);
1009 ME_RepaintSelection(editor
, &tmp_curs
);
1010 ME_SendSelChange(editor
);
1013 ME_ArrowDown(editor
, p
);
1014 ME_ClearTempStyle(editor
);
1015 ME_RepaintSelection(editor
, &tmp_curs
);
1016 ME_SendSelChange(editor
);
1019 ME_ArrowPageUp(editor
, p
);
1020 ME_ClearTempStyle(editor
);
1021 ME_SendSelChange(editor
);
1024 ME_ArrowPageDown(editor
, p
);
1025 ME_ClearTempStyle(editor
);
1026 ME_SendSelChange(editor
);
1030 editor
->nUDArrowX
= -1;
1032 case VK_BACK
: { /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1033 if (GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_READONLY
)
1035 if (ME_IsSelection(editor
))
1037 editor
->bCaretAtEnd
= FALSE
; /* FIXME or maybe not */
1038 ME_DeleteSelection(editor
);
1039 ME_UpdateRepaint(editor
);
1042 if (ME_ArrowLeft(editor
, p
)) {
1043 editor
->bCaretAtEnd
= FALSE
; /* FIXME or maybe not */
1044 ME_ClearTempStyle(editor
);
1045 ME_MoveCaret(editor
);
1046 ME_DeleteTextAtCursor(editor
, nCursor
, 1);
1047 ME_UpdateRepaint(editor
);
1052 if (GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_READONLY
)
1054 /* editor->bCaretAtEnd = 0; FIXME or maybe not */
1055 if (ME_IsSelection(editor
))
1057 ME_DeleteSelection(editor
);
1058 ME_ClearTempStyle(editor
);
1059 ME_UpdateRepaint(editor
);
1062 ME_DeleteTextAtCursor(editor
, nCursor
, 1);
1063 ME_ClearTempStyle(editor
);
1064 ME_UpdateRepaint(editor
);
1068 if (GetKeyState(VK_CONTROL
)<0)
1069 ME_ArrowCtrlHome(editor
, p
);
1071 ME_ArrowHome(editor
, p
);
1072 editor
->bCaretAtEnd
= 0;
1073 ME_ClearTempStyle(editor
);
1074 ME_RepaintSelection(editor
, &tmp_curs
);
1075 ME_SendSelChange(editor
);
1079 if (GetKeyState(VK_CONTROL
)<0)
1080 ME_ArrowCtrlEnd(editor
, p
);
1082 ME_ArrowEnd(editor
, p
);
1083 ME_ClearTempStyle(editor
);
1084 ME_RepaintSelection(editor
, &tmp_curs
);
1085 ME_SendSelChange(editor
);
1088 editor
->bCaretAtEnd
= 0;
1089 if (ME_CancelSelection(editor
, -1))
1091 ME_ArrowLeft(editor
, p
);
1092 ME_RepaintSelection(editor
, &tmp_curs
);
1093 ME_ClearTempStyle(editor
);
1094 ME_SendSelChange(editor
);
1097 editor
->bCaretAtEnd
= 0;
1098 if (ME_CancelSelection(editor
, +1))
1100 ME_ArrowRight(editor
, p
);
1101 ME_RepaintSelection(editor
, &tmp_curs
);
1102 ME_ClearTempStyle(editor
);
1103 ME_SendSelChange(editor
);