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 y
+= ME_GetYScrollPos(editor
);
722 ME_FindPixelPos(editor
, x
, y
, &cursor
, NULL
);
723 return (ME_GetParagraph(cursor
.pRun
)->member
.para
.nCharOfs
724 + cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
);
728 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
)
730 ME_Cursor tmp_cursor
;
731 int is_selection
= 0;
733 editor
->nUDArrowX
= -1;
735 y
+= ME_GetYScrollPos(editor
);
737 tmp_cursor
= editor
->pCursors
[0];
738 is_selection
= ME_IsSelection(editor
);
740 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
742 if (GetKeyState(VK_SHIFT
)>=0)
744 editor
->pCursors
[1] = editor
->pCursors
[0];
749 editor
->pCursors
[1] = tmp_cursor
;
753 ME_InvalidateSelection(editor
);
754 HideCaret(editor
->hWnd
);
755 ME_MoveCaret(editor
);
756 ShowCaret(editor
->hWnd
);
757 ME_ClearTempStyle(editor
);
758 ME_SendSelChange(editor
);
761 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
763 ME_Cursor tmp_cursor
;
765 y
+= ME_GetYScrollPos(editor
);
767 tmp_cursor
= editor
->pCursors
[0];
768 /* FIXME: do something with the return value of ME_FindPixelPos */
769 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
771 if (tmp_cursor
.pRun
== editor
->pCursors
[0].pRun
&&
772 tmp_cursor
.nOffset
== editor
->pCursors
[0].nOffset
)
775 ME_InvalidateSelection(editor
);
776 editor
->pCursors
[0] = tmp_cursor
;
777 HideCaret(editor
->hWnd
);
778 ME_MoveCaret(editor
);
779 ME_InvalidateSelection(editor
);
780 ShowCaret(editor
->hWnd
);
781 ME_SendSelChange(editor
);
784 static ME_DisplayItem
*ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
785 int x
, int *pOffset
, int *pbCaretAtEnd
)
787 ME_DisplayItem
*pNext
, *pLastRun
;
788 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
789 assert(pNext
->type
== diRun
);
791 *pbCaretAtEnd
= FALSE
;
793 int run_x
= pNext
->member
.run
.pt
.x
;
794 int width
= pNext
->member
.run
.nWidth
;
797 if (pOffset
) *pOffset
= 0;
800 if (x
>= run_x
&& x
< run_x
+width
)
802 int ch
= ME_CharFromPointCursor(editor
, x
-run_x
, &pNext
->member
.run
);
803 ME_String
*s
= pNext
->member
.run
.strText
;
811 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
812 } while(pNext
&& pNext
->type
== diRun
);
814 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
816 pNext
= ME_FindItemFwd(pNext
, diRun
);
817 if (pbCaretAtEnd
) *pbCaretAtEnd
= 1;
818 if (pOffset
) *pOffset
= 0;
821 if (pbCaretAtEnd
) *pbCaretAtEnd
= 0;
822 if (pOffset
) *pOffset
= 0;
827 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
829 ME_DisplayItem
*pRun
= pCursor
->pRun
;
832 if (editor
->nUDArrowX
!= -1)
833 x
= editor
->nUDArrowX
;
835 if (editor
->bCaretAtEnd
)
837 pRun
= ME_FindItemBack(pRun
, diRun
);
839 x
= pRun
->member
.run
.pt
.x
+ pRun
->member
.run
.nWidth
;
842 x
= pRun
->member
.run
.pt
.x
;
843 x
+= ME_PointFromChar(editor
, &pRun
->member
.run
, pCursor
->nOffset
);
845 editor
->nUDArrowX
= x
;
852 ME_MoveCursorLines(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
854 ME_DisplayItem
*pRun
= pCursor
->pRun
;
855 ME_DisplayItem
*pItem
;
856 int x
= ME_GetXForArrow(editor
, pCursor
);
858 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
859 pRun
= ME_FindItemBack(pRun
, diRun
);
864 /* start of this row */
865 pItem
= ME_FindItemBack(pRun
, diStartRow
);
867 /* start of the previous row */
868 pItem
= ME_FindItemBack(pItem
, diStartRow
);
872 /* start of the next row */
873 pItem
= ME_FindItemFwd(pRun
, diStartRow
);
874 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
879 /* row not found - ignore */
882 pCursor
->pRun
= ME_FindRunInRow(editor
, pItem
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
883 assert(pCursor
->pRun
);
884 assert(pCursor
->pRun
->type
== diRun
);
888 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
890 ME_DisplayItem
*pRun
= pCursor
->pRun
;
891 ME_DisplayItem
*pLast
, *p
;
892 int x
, y
, ys
, yd
, yp
, yprev
;
893 ME_Cursor tmp_curs
= *pCursor
;
895 x
= ME_GetXForArrow(editor
, pCursor
);
896 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
897 pRun
= ME_FindItemBack(pRun
, diRun
);
899 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
900 assert(p
->type
== diStartRow
);
901 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
902 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
903 yd
= y
- editor
->sizeWindow
.cy
;
907 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
910 if (p
->type
== diParagraph
) { /* crossing paragraphs */
911 if (p
->member
.para
.prev_para
== NULL
)
913 yp
= p
->member
.para
.prev_para
->member
.para
.nYPos
;
916 y
= yp
+ p
->member
.row
.nYPos
;
923 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
924 ME_UpdateSelection(editor
, &tmp_curs
);
925 if (yprev
< editor
->sizeWindow
.cy
)
927 ME_EnsureVisible(editor
, ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
));
931 ME_Scroll(editor
, 0, ys
-yprev
);
934 assert(pCursor
->pRun
);
935 assert(pCursor
->pRun
->type
== diRun
);
938 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
939 of pixels, even if it makes the scroll bar position exceed its normal maximum.
940 In such a situation, clicking the scrollbar restores its position back to the
941 normal range (ie. sets it to (doclength-screenheight)). */
943 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
945 ME_DisplayItem
*pRun
= pCursor
->pRun
;
946 ME_DisplayItem
*pLast
, *p
;
947 int x
, y
, ys
, yd
, yp
, yprev
;
948 ME_Cursor tmp_curs
= *pCursor
;
950 x
= ME_GetXForArrow(editor
, pCursor
);
951 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
952 pRun
= ME_FindItemBack(pRun
, diRun
);
954 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
955 assert(p
->type
== diStartRow
);
956 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
957 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
958 yd
= y
+ editor
->sizeWindow
.cy
;
962 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
965 if (p
->type
== diParagraph
) {
966 yp
= p
->member
.para
.nYPos
;
969 y
= yp
+ p
->member
.row
.nYPos
;
976 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
977 ME_UpdateSelection(editor
, &tmp_curs
);
978 if (yprev
>= editor
->nTotalLength
-editor
->sizeWindow
.cy
)
980 ME_EnsureVisible(editor
, ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
));
984 ME_Scroll(editor
, 0, ys
-yprev
);
987 assert(pCursor
->pRun
);
988 assert(pCursor
->pRun
->type
== diRun
);
991 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
993 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
995 ME_DisplayItem
*pRun
;
996 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
997 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1001 pRun
= ME_FindItemFwd(pRow
, diRun
);
1003 pCursor
->pRun
= pRun
;
1004 pCursor
->nOffset
= 0;
1007 editor
->bCaretAtEnd
= FALSE
;
1010 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1012 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diTextStart
);
1014 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1016 pCursor
->pRun
= pRun
;
1017 pCursor
->nOffset
= 0;
1022 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1024 ME_DisplayItem
*pRow
;
1026 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1029 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1031 if (pRow
->type
== diStartRow
) {
1032 /* FIXME WTF was I thinking about here ? */
1033 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1035 pCursor
->pRun
= pRun
;
1036 pCursor
->nOffset
= 0;
1037 editor
->bCaretAtEnd
= 1;
1040 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1041 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1042 pCursor
->nOffset
= 0;
1043 editor
->bCaretAtEnd
= FALSE
;
1046 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1048 ME_DisplayItem
*p
= ME_FindItemFwd(pCursor
->pRun
, diTextEnd
);
1050 p
= ME_FindItemBack(p
, diRun
);
1052 assert(p
->member
.run
.nFlags
& MERF_ENDPARA
);
1054 pCursor
->nOffset
= 0;
1055 editor
->bCaretAtEnd
= FALSE
;
1058 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1060 return memcmp(&editor
->pCursors
[0], &editor
->pCursors
[1], sizeof(ME_Cursor
))!=0;
1063 static int ME_GetSelCursor(ME_TextEditor
*editor
, int dir
)
1065 int cdir
= ME_GetCursorOfs(editor
, 0) - ME_GetCursorOfs(editor
, 1);
1073 BOOL
ME_UpdateSelection(ME_TextEditor
*editor
, ME_Cursor
*pTempCursor
)
1075 ME_Cursor old_anchor
= editor
->pCursors
[1];
1077 if (GetKeyState(VK_SHIFT
)>=0) /* cancelling selection */
1079 /* any selection was present ? if so, it's no more, repaint ! */
1080 editor
->pCursors
[1] = editor
->pCursors
[0];
1081 if (memcmp(pTempCursor
, &old_anchor
, sizeof(ME_Cursor
))) {
1088 if (!memcmp(pTempCursor
, &editor
->pCursors
[1], sizeof(ME_Cursor
))) /* starting selection */
1090 editor
->pCursors
[1] = *pTempCursor
;
1099 void ME_DeleteSelection(ME_TextEditor
*editor
)
1102 ME_GetSelection(editor
, &from
, &to
);
1103 ME_DeleteTextAtCursor(editor
, ME_GetSelCursor(editor
,-1), to
-from
);
1106 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1112 ME_GetSelection(editor
, &from
, &to
);
1113 ME_CursorFromCharOfs(editor
, from
, &c
);
1115 style
= c
.pRun
->member
.run
.style
;
1116 ME_AddRefStyle(style
); /* ME_GetInsertStyle has already done that */
1119 style
= ME_GetInsertStyle(editor
, 0);
1123 void ME_SendSelChange(ME_TextEditor
*editor
)
1127 ME_ClearTempStyle(editor
);
1129 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1132 sc
.nmhdr
.hwndFrom
= editor
->hWnd
;
1133 sc
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
1134 sc
.nmhdr
.code
= EN_SELCHANGE
;
1135 SendMessageW(editor
->hWnd
, EM_EXGETSEL
, 0, (LPARAM
)&sc
.chrg
);
1136 sc
.seltyp
= SEL_EMPTY
;
1137 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1138 sc
.seltyp
|= SEL_TEXT
;
1139 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* wth were RICHEDIT authors thinking ? */
1140 sc
.seltyp
|= SEL_MULTICHAR
;
1141 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
, sc
.nmhdr
.idFrom
, (LPARAM
)&sc
);
1146 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1149 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1150 ME_Cursor tmp_curs
= *p
;
1151 BOOL success
= FALSE
;
1153 if (ME_IsSelection(editor
) && !extend
)
1154 ME_InvalidateSelection(editor
);
1156 editor
->nUDArrowX
= -1;
1159 editor
->bCaretAtEnd
= 0;
1161 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1163 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1166 editor
->bCaretAtEnd
= 0;
1168 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1170 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1173 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1176 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1179 ME_ArrowPageUp(editor
, &tmp_curs
);
1182 ME_ArrowPageDown(editor
, &tmp_curs
);
1186 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1188 ME_ArrowHome(editor
, &tmp_curs
);
1189 editor
->bCaretAtEnd
= 0;
1194 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1196 ME_ArrowEnd(editor
, &tmp_curs
);
1201 editor
->pCursors
[1] = tmp_curs
;
1204 if (ME_IsSelection(editor
))
1205 ME_InvalidateSelection(editor
);
1206 HideCaret(editor
->hWnd
);
1207 ME_EnsureVisible(editor
, tmp_curs
.pRun
);
1208 ME_ShowCaret(editor
);
1209 ME_SendSelChange(editor
);