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
);
28 ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
);
30 void ME_GetSelection(ME_TextEditor
*editor
, int *from
, int *to
)
32 *from
= ME_GetCursorOfs(editor
, 0);
33 *to
= ME_GetCursorOfs(editor
, 1);
43 int ME_GetTextLength(ME_TextEditor
*editor
)
45 return ME_CharOfsFromRunOfs(editor
, ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
), 0);
49 int ME_GetTextLengthEx(ME_TextEditor
*editor
, const GETTEXTLENGTHEX
*how
)
53 if (how
->flags
& GTL_PRECISE
&& how
->flags
& GTL_CLOSE
)
55 if (how
->flags
& GTL_NUMCHARS
&& how
->flags
& GTL_NUMBYTES
)
58 length
= ME_GetTextLength(editor
);
60 if ((GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_MULTILINE
) && (how
->flags
& GTL_USECRLF
))
61 length
+= editor
->nParagraphs
- 1;
63 if (how
->flags
& GTL_NUMBYTES
)
67 if (how
->codepage
== 1200)
69 if (how
->flags
& GTL_PRECISE
)
70 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
71 if (GetCPInfo(how
->codepage
, &cpinfo
))
72 return length
* cpinfo
.MaxCharSize
;
73 ERR("Invalid codepage %u\n", how
->codepage
);
80 int ME_SetSelection(ME_TextEditor
*editor
, int from
, int to
)
83 const int len
= ME_GetTextLength(editor
);
85 /* all negative values are effectively the same */
92 if (from
== 0 && to
== -1)
94 editor
->pCursors
[1].pRun
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
);
95 editor
->pCursors
[1].nOffset
= 0;
96 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
97 editor
->pCursors
[0].nOffset
= 0;
98 ME_InvalidateSelection(editor
);
99 ME_ClearTempStyle(editor
);
103 /* if both values are equal and also out of bound, that means to */
104 /* put the selection at the end of the text */
105 if ((from
== to
) && (to
< 0 || to
> len
))
111 /* if from is negative and to is positive then selection is */
112 /* deselected and caret moved to end of the current selection */
116 ME_GetSelection(editor
, &start
, &end
);
117 editor
->pCursors
[1] = editor
->pCursors
[0];
119 ME_ClearTempStyle(editor
);
123 /* adjust to if it's a negative value */
127 /* flip from and to if they are reversed */
135 /* after fiddling with the values, we find from > len && to > len */
138 /* special case with to too big */
145 editor
->pCursors
[1].pRun
= editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
146 editor
->pCursors
[1].nOffset
= editor
->pCursors
[0].nOffset
= 0;
147 ME_InvalidateSelection(editor
);
148 ME_ClearTempStyle(editor
);
152 ME_RunOfsFromCharOfs(editor
, from
, &editor
->pCursors
[1].pRun
, &editor
->pCursors
[1].nOffset
);
153 ME_RunOfsFromCharOfs(editor
, to
, &editor
->pCursors
[0].pRun
, &editor
->pCursors
[0].nOffset
);
159 ME_GetCursorCoordinates(ME_TextEditor
*editor
, ME_Cursor
*pCursor
,
160 int *x
, int *y
, int *height
)
162 ME_DisplayItem
*pCursorRun
= pCursor
->pRun
;
163 ME_DisplayItem
*pSizeRun
= pCursor
->pRun
;
165 assert(!pCursor
->nOffset
|| !editor
->bCaretAtEnd
);
166 assert(height
&& x
&& y
);
167 assert(!(ME_GetParagraph(pCursorRun
)->member
.para
.nFlags
& MEPF_REWRAP
));
168 assert(pCursor
->pRun
);
169 assert(pCursor
->pRun
->type
== diRun
);
171 if (pCursorRun
->type
== diRun
) {
172 ME_DisplayItem
*row
= ME_FindItemBack(pCursorRun
, diStartRowOrParagraph
);
175 HDC hDC
= GetDC(editor
->hWnd
);
177 ME_DisplayItem
*run
= pCursorRun
;
178 ME_DisplayItem
*para
= NULL
;
181 ME_InitContext(&c
, editor
, hDC
);
183 if (!pCursor
->nOffset
&& !editor
->bCaretAtEnd
)
185 ME_DisplayItem
*prev
= ME_FindItemBack(pCursorRun
, diRunOrStartRow
);
187 if (prev
->type
== diRun
)
190 assert(row
->type
== diStartRow
); /* paragraph -> run without start row ?*/
191 para
= ME_FindItemBack(row
, diParagraph
);
193 assert(para
->type
== diParagraph
);
194 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
&&
195 run
== ME_FindItemFwd(row
, diRun
))
197 ME_DisplayItem
*tmp
= ME_FindItemBack(row
, diRunOrParagraph
);
199 if (tmp
->type
== diRun
)
201 row
= ME_FindItemBack(tmp
, diStartRow
);
202 pSizeRun
= run
= tmp
;
204 assert(run
->type
== diRun
);
205 sz
= ME_GetRunSize(&c
, ¶
->member
.para
, &run
->member
.run
, ME_StrLen(run
->member
.run
.strText
));
208 if (pCursor
->nOffset
&& !(run
->member
.run
.nFlags
& MERF_SKIPPED
)) {
209 sz
= ME_GetRunSize(&c
, ¶
->member
.para
, &run
->member
.run
, pCursor
->nOffset
);
212 *height
= pSizeRun
->member
.run
.nAscent
+ pSizeRun
->member
.run
.nDescent
;
213 *x
= run
->member
.run
.pt
.x
+ sz
.cx
;
214 *y
= para
->member
.para
.nYPos
+ row
->member
.row
.nBaseline
+ pSizeRun
->member
.run
.pt
.y
- pSizeRun
->member
.run
.nAscent
- ME_GetYScrollPos(editor
);
216 ME_DestroyContext(&c
);
217 ReleaseDC(editor
->hWnd
, hDC
);
221 *height
= 10; /* FIXME use global font */
228 ME_MoveCaret(ME_TextEditor
*editor
)
232 if (ME_WrapMarkedParagraphs(editor
))
233 ME_UpdateScrollBar(editor
);
234 ME_GetCursorCoordinates(editor
, &editor
->pCursors
[0], &x
, &y
, &height
);
235 if(editor
->bHaveFocus
)
237 CreateCaret(editor
->hWnd
, NULL
, 0, height
);
243 void ME_ShowCaret(ME_TextEditor
*ed
)
250 void ME_HideCaret(ME_TextEditor
*ed
)
259 void ME_InternalDeleteText(ME_TextEditor
*editor
, int nOfs
,
268 ME_CursorFromCharOfs(editor
, nOfs
, &c
);
269 run
= &c
.pRun
->member
.run
;
270 if (run
->nFlags
& MERF_ENDPARA
) {
271 if (!ME_FindItemFwd(c
.pRun
, diParagraph
))
275 ME_JoinParagraphs(editor
, ME_GetParagraph(c
.pRun
));
276 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
277 ME_CheckCharOffsets(editor
);
279 if (editor
->bEmulateVersion10
&& nChars
)
286 int nIntendedChars
= nChars
;
287 int nCharsToDelete
= nChars
;
291 ME_FindItemBack(c
.pRun
, diParagraph
)->member
.para
.nFlags
|= MEPF_REWRAP
;
294 ME_StrRelPos(run
->strText
, loc
, &nChars
);
295 /* nChars is the number of characters that should be deleted from the
296 FOLLOWING runs (these AFTER cursor.pRun)
297 nCharsToDelete is a number of chars to delete from THIS run */
298 nCharsToDelete
-= nChars
;
299 shift
-= nCharsToDelete
;
300 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
301 nCharsToDelete
, nIntendedChars
, nChars
, c
.nOffset
,
302 debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
304 if (!c
.nOffset
&& ME_StrVLen(run
->strText
) == nCharsToDelete
)
306 /* undo = reinsert whole run */
307 /* nOfs is a character offset (from the start of the document
308 to the current (deleted) run */
309 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
311 pUndo
->di
.member
.run
.nCharOfs
= nOfs
;
315 /* undo = reinsert partial run */
316 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
318 ME_DestroyString(pUndo
->di
.member
.run
.strText
);
319 pUndo
->di
.member
.run
.nCharOfs
= nOfs
;
320 pUndo
->di
.member
.run
.strText
= ME_MakeStringN(run
->strText
->szData
+c
.nOffset
, nCharsToDelete
);
323 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
324 TRACE("Shift value: %d\n", shift
);
325 ME_StrDeleteV(run
->strText
, c
.nOffset
, nCharsToDelete
);
327 /* update cursors (including c) */
328 for (i
=-1; i
<editor
->nCursors
; i
++) {
329 ME_Cursor
*pThisCur
= editor
->pCursors
+ i
;
330 if (i
== -1) pThisCur
= &c
;
331 if (pThisCur
->pRun
== cursor
.pRun
) {
332 if (pThisCur
->nOffset
> cursor
.nOffset
) {
333 if (pThisCur
->nOffset
-cursor
.nOffset
< nCharsToDelete
)
334 pThisCur
->nOffset
= cursor
.nOffset
;
336 pThisCur
->nOffset
-= nCharsToDelete
;
337 assert(pThisCur
->nOffset
>= 0);
338 assert(pThisCur
->nOffset
<= ME_StrVLen(run
->strText
));
340 if (pThisCur
->nOffset
== ME_StrVLen(run
->strText
))
342 pThisCur
->pRun
= ME_FindItemFwd(pThisCur
->pRun
, diRunOrParagraphOrEnd
);
343 assert(pThisCur
->pRun
->type
== diRun
);
344 pThisCur
->nOffset
= 0;
349 /* c = updated data now */
351 if (c
.pRun
== cursor
.pRun
)
352 ME_SkipAndPropagateCharOffset(c
.pRun
, shift
);
354 ME_PropagateCharOffset(c
.pRun
, shift
);
356 if (!ME_StrVLen(cursor
.pRun
->member
.run
.strText
))
358 TRACE("Removing useless run\n");
359 ME_Remove(cursor
.pRun
);
360 ME_DestroyDisplayItem(cursor
.pRun
);
365 ME_CheckCharOffsets(editor);
372 void ME_DeleteTextAtCursor(ME_TextEditor
*editor
, int nCursor
,
375 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
376 /* text operations set modified state */
377 editor
->nModifyStep
= 1;
378 ME_InternalDeleteText(editor
, ME_GetCursorOfs(editor
, nCursor
), nChars
);
381 static ME_DisplayItem
*
382 ME_InternalInsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
383 const WCHAR
*str
, int len
, ME_Style
*style
,
386 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
388 editor
->bCaretAtEnd
= FALSE
;
390 assert(p
->pRun
->type
== diRun
);
392 return ME_InsertRunAtCursor(editor
, p
, style
, str
, len
, flags
);
396 void ME_InsertOLEFromCursor(ME_TextEditor
*editor
, const REOBJECT
* reo
, int nCursor
)
398 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
403 if (ME_IsSelection(editor
))
404 ME_DeleteSelection(editor
);
406 di
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
408 di
->member
.run
.ole_obj
= ALLOC_OBJ(*reo
);
409 ME_CopyReObject(di
->member
.run
.ole_obj
, reo
);
410 ME_SendSelChange(editor
);
415 ME_InsertTableCellFromCursor(ME_TextEditor
*editor
, int nCursor
)
418 ME_DisplayItem
*p
, *run
;
419 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
421 p
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, pStyle
,
424 while ((run
= ME_FindItemBack(run
, diRunOrParagraph
))->type
== diRun
)
426 if (run
->member
.run
.nFlags
& MERF_CELL
)
428 assert(run
->member
.run
.pCell
->next
);
429 p
->member
.run
.pCell
= run
->member
.run
.pCell
->next
;
433 assert(run
->type
== diParagraph
);
434 assert(run
->member
.para
.bTable
);
435 assert(run
->member
.para
.pCells
);
436 p
->member
.run
.pCell
= run
->member
.para
.pCells
;
440 void ME_InsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
441 const WCHAR
*str
, int len
, ME_Style
*style
)
447 /* FIXME really HERE ? */
448 if (ME_IsSelection(editor
))
449 ME_DeleteSelection(editor
);
451 /* FIXME: is this too slow? */
452 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
453 oldLen
= ME_GetTextLength(editor
);
455 /* text operations set modified state */
456 editor
->nModifyStep
= 1;
460 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
464 /* grow the text limit to fit our text */
465 if(editor
->nTextLimit
< oldLen
+len
)
466 editor
->nTextLimit
= oldLen
+ len
;
471 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
472 while(pos
-str
< len
&& *pos
!= '\r' && *pos
!= '\n' && *pos
!= '\t')
474 if (pos
-str
< len
&& *pos
== '\t') { /* handle tabs */
478 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
480 ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, style
, MERF_TAB
);
489 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
490 if (!editor
->bEmulateVersion10
&& pos
-str
< len
-2 && pos
[0] == '\r' && pos
[1] == '\r' && pos
[2] == '\n') {
494 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
496 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, style
, 0);
505 if (pos
-str
< len
) { /* handle EOLs */
506 ME_DisplayItem
*tp
, *end_run
;
509 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
510 p
= &editor
->pCursors
[nCursor
];
512 ME_SplitRunSimple(editor
, p
->pRun
, p
->nOffset
);
513 p
= &editor
->pCursors
[nCursor
];
515 tmp_style
= ME_GetInsertStyle(editor
, nCursor
);
516 /* ME_SplitParagraph increases style refcount */
517 tp
= ME_SplitParagraph(editor
, p
->pRun
, p
->pRun
->member
.run
.style
);
518 p
->pRun
= ME_FindItemFwd(tp
, diRun
);
519 end_run
= ME_FindItemBack(tp
, diRun
);
520 ME_ReleaseStyle(end_run
->member
.run
.style
);
521 end_run
->member
.run
.style
= tmp_style
;
523 if(pos
-str
< len
&& *pos
=='\r')
525 if(pos
-str
< len
&& *pos
=='\n')
533 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, len
, style
, 0);
540 ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
542 ME_DisplayItem
*pRun
= pCursor
->pRun
;
546 if (!pCursor
->nOffset
)
549 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
556 if (pRun
->member
.para
.prev_para
->type
== diTextStart
)
558 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
559 /* every paragraph ought to have at least one run */
560 assert(pRun
&& pRun
->type
== diRun
);
561 assert(pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
564 assert(pRun
->type
!= diRun
&& pRun
->type
!= diParagraph
);
567 } while (RUN_IS_HIDDEN(&pRun
->member
.run
));
568 pCursor
->pRun
= pRun
;
569 if (pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
570 pCursor
->nOffset
= 0;
572 pCursor
->nOffset
= pRun
->member
.run
.strText
->nLen
;
575 if (pCursor
->nOffset
)
576 pCursor
->nOffset
= ME_StrRelPos2(pCursor
->pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
581 if (!(pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
583 int new_ofs
= ME_StrRelPos2(pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
585 if (new_ofs
< pRun
->member
.run
.strText
->nLen
)
587 pCursor
->nOffset
= new_ofs
;
592 pRun
= ME_FindItemFwd(pRun
, diRun
);
593 } while (pRun
&& RUN_IS_HIDDEN(&pRun
->member
.run
));
596 pCursor
->pRun
= pRun
;
597 pCursor
->nOffset
= 0;
606 ME_MoveCursorWords(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
608 ME_DisplayItem
*pRun
= cursor
->pRun
, *pOtherRun
;
609 int nOffset
= cursor
->nOffset
;
613 /* Backward movement */
616 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
617 nOffset
, WB_MOVEWORDLEFT
);
620 pOtherRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
621 if (pOtherRun
->type
== diRun
)
623 if (ME_CallWordBreakProc(editor
, pOtherRun
->member
.run
.strText
,
624 pOtherRun
->member
.run
.strText
->nLen
- 1,
626 && !(pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
627 && !(cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
628 && !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
, 0,
632 nOffset
= pOtherRun
->member
.run
.strText
->nLen
;
634 else if (pOtherRun
->type
== diParagraph
)
636 if (cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
638 /* Paragraph breaks are treated as separate words */
639 if (pOtherRun
->member
.para
.prev_para
->type
== diTextStart
)
641 pRun
= ME_FindItemBack(pOtherRun
, diRunOrParagraph
);
649 /* Forward movement */
650 BOOL last_delim
= FALSE
;
654 if (last_delim
&& !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
655 nOffset
, WB_ISDELIMITER
))
657 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
658 nOffset
, WB_MOVEWORDRIGHT
);
659 if (nOffset
< pRun
->member
.run
.strText
->nLen
)
661 pOtherRun
= ME_FindItemFwd(pRun
, diRunOrParagraphOrEnd
);
662 if (pOtherRun
->type
== diRun
)
664 last_delim
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
665 nOffset
- 1, WB_ISDELIMITER
);
669 else if (pOtherRun
->type
== diParagraph
)
671 if (cursor
->pRun
== pRun
)
672 pRun
= ME_FindItemFwd(pOtherRun
, diRun
);
678 if (cursor
->pRun
== pRun
)
686 cursor
->nOffset
= nOffset
;
692 ME_SelectWord(ME_TextEditor
*editor
)
694 if (!(editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
695 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], -1);
696 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], +1);
697 ME_InvalidateSelection(editor
);
698 ME_SendSelChange(editor
);
702 int ME_GetCursorOfs(ME_TextEditor
*editor
, int nCursor
)
704 ME_Cursor
*pCursor
= &editor
->pCursors
[nCursor
];
705 return ME_GetParagraph(pCursor
->pRun
)->member
.para
.nCharOfs
706 + pCursor
->pRun
->member
.run
.nCharOfs
+ pCursor
->nOffset
;
709 static void ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
, ME_Cursor
*result
, BOOL
*is_eol
)
711 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
712 ME_DisplayItem
*last
= NULL
;
719 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->member
.para
.next_para
)
721 assert(p
->type
== diParagraph
);
722 if (y
< p
->member
.para
.nYPos
+ p
->member
.para
.nHeight
)
724 y
-= p
->member
.para
.nYPos
;
725 p
= ME_FindItemFwd(p
, diStartRow
);
730 for (; p
!= editor
->pBuffer
->pLast
; )
733 assert(p
->type
== diStartRow
);
734 if (y
< p
->member
.row
.nYPos
+ p
->member
.row
.nHeight
)
736 p
= ME_FindItemFwd(p
, diRun
);
739 pp
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
740 if (pp
->type
!= diStartRow
)
742 p
= ME_FindItemFwd(p
, diRun
);
747 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->next
)
752 rx
= x
- p
->member
.run
.pt
.x
;
753 if (rx
< p
->member
.run
.nWidth
)
756 assert(p
->type
== diRun
);
757 if ((p
->member
.run
.nFlags
& MERF_ENDPARA
) || rx
< 0)
760 result
->nOffset
= ME_CharFromPointCursor(editor
, rx
, &p
->member
.run
);
761 if (editor
->pCursors
[0].nOffset
== p
->member
.run
.strText
->nLen
&& rx
)
763 result
->pRun
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, diRun
);
770 p
= ME_FindItemFwd(p
, diRun
);
771 if (is_eol
) *is_eol
= 1;
772 rx
= 0; /* FIXME not sure */
776 rx
= 0; /* FIXME not sure */
783 result
->pRun
= ME_FindItemBack(p
, diRun
);
785 assert(result
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
790 ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
)
795 GetClientRect(editor
->hWnd
, &rc
);
796 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
)
798 y
+= ME_GetYScrollPos(editor
);
799 ME_FindPixelPos(editor
, x
, y
, &cursor
, NULL
);
800 return (ME_GetParagraph(cursor
.pRun
)->member
.para
.nCharOfs
801 + cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
);
805 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
)
807 ME_Cursor tmp_cursor
;
808 int is_selection
= 0;
810 editor
->nUDArrowX
= -1;
812 y
+= ME_GetYScrollPos(editor
);
814 tmp_cursor
= editor
->pCursors
[0];
815 is_selection
= ME_IsSelection(editor
);
817 if (x
>= editor
->selofs
)
819 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
820 if (GetKeyState(VK_SHIFT
)>=0)
822 editor
->pCursors
[1] = editor
->pCursors
[0];
824 else if (!is_selection
) {
825 editor
->pCursors
[1] = tmp_cursor
;
829 ME_InvalidateSelection(editor
);
830 HideCaret(editor
->hWnd
);
831 ME_MoveCaret(editor
);
832 ShowCaret(editor
->hWnd
);
833 ME_ClearTempStyle(editor
);
834 ME_SendSelChange(editor
);
838 ME_DisplayItem
*pRow
;
842 /* Set pCursors[0] to beginning of line */
843 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[1], &editor
->bCaretAtEnd
);
844 /* Set pCursors[1] to end of line */
845 pRow
= ME_FindItemFwd(editor
->pCursors
[1].pRun
, diStartRowOrParagraphOrEnd
);
847 /* pCursor[0] is the position where the cursor will be drawn,
848 * pCursor[1] is the other end of the selection range
849 * pCursor[2] and [3] are backups of [0] and [1] so I
850 * don't have to look them up again
853 if (pRow
->type
== diStartRow
) {
854 /* FIXME WTF was I thinking about here ? */
855 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
857 editor
->pCursors
[0].pRun
= pRun
;
858 editor
->pCursors
[0].nOffset
= 0;
859 editor
->bCaretAtEnd
= 1;
861 editor
->pCursors
[0].pRun
= ME_FindItemBack(pRow
, diRun
);
862 assert(editor
->pCursors
[0].pRun
&& editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
863 editor
->pCursors
[0].nOffset
= 0;
864 editor
->bCaretAtEnd
= 0;
866 editor
->pCursors
[2] = editor
->pCursors
[0];
867 editor
->pCursors
[3] = editor
->pCursors
[1];
868 ME_InvalidateSelection(editor
);
869 HideCaret(editor
->hWnd
);
870 ME_MoveCaret(editor
);
871 ShowCaret(editor
->hWnd
);
872 ME_ClearTempStyle(editor
);
873 ME_SendSelChange(editor
);
877 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
879 ME_Cursor tmp_cursor
;
881 y
+= ME_GetYScrollPos(editor
);
883 tmp_cursor
= editor
->pCursors
[0];
884 /* FIXME: do something with the return value of ME_FindPixelPos */
885 if (!editor
->linesel
)
886 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
887 else ME_FindPixelPos(editor
, (y
> editor
->sely
) * editor
->rcFormat
.right
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
889 if (!memcmp(&tmp_cursor
, editor
->pCursors
, sizeof(tmp_cursor
)))
892 ME_InvalidateSelection(editor
);
893 if (!editor
->linesel
)
894 editor
->pCursors
[0] = tmp_cursor
;
895 else if (!memcmp(&tmp_cursor
, editor
->pCursors
+2, sizeof(tmp_cursor
)) ||
896 !memcmp(&tmp_cursor
, editor
->pCursors
+3, sizeof(tmp_cursor
)))
898 editor
->pCursors
[0] = editor
->pCursors
[2];
899 editor
->pCursors
[1] = editor
->pCursors
[3];
901 else if (y
< editor
->sely
)
903 editor
->pCursors
[0] = tmp_cursor
;
904 editor
->pCursors
[1] = editor
->pCursors
[2];
908 editor
->pCursors
[0] = tmp_cursor
;
909 editor
->pCursors
[1] = editor
->pCursors
[3];
912 HideCaret(editor
->hWnd
);
913 ME_MoveCaret(editor
);
914 ME_InvalidateSelection(editor
);
915 ShowCaret(editor
->hWnd
);
916 ME_SendSelChange(editor
);
919 static ME_DisplayItem
*ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
920 int x
, int *pOffset
, int *pbCaretAtEnd
)
922 ME_DisplayItem
*pNext
, *pLastRun
;
923 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
924 assert(pNext
->type
== diRun
);
926 *pbCaretAtEnd
= FALSE
;
928 int run_x
= pNext
->member
.run
.pt
.x
;
929 int width
= pNext
->member
.run
.nWidth
;
932 if (pOffset
) *pOffset
= 0;
935 if (x
>= run_x
&& x
< run_x
+width
)
937 int ch
= ME_CharFromPointCursor(editor
, x
-run_x
, &pNext
->member
.run
);
938 ME_String
*s
= pNext
->member
.run
.strText
;
946 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
947 } while(pNext
&& pNext
->type
== diRun
);
949 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
951 pNext
= ME_FindItemFwd(pNext
, diRun
);
952 if (pbCaretAtEnd
) *pbCaretAtEnd
= 1;
953 if (pOffset
) *pOffset
= 0;
956 if (pbCaretAtEnd
) *pbCaretAtEnd
= 0;
957 if (pOffset
) *pOffset
= 0;
962 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
964 ME_DisplayItem
*pRun
= pCursor
->pRun
;
967 if (editor
->nUDArrowX
!= -1)
968 x
= editor
->nUDArrowX
;
970 if (editor
->bCaretAtEnd
)
972 pRun
= ME_FindItemBack(pRun
, diRun
);
974 x
= pRun
->member
.run
.pt
.x
+ pRun
->member
.run
.nWidth
;
977 x
= pRun
->member
.run
.pt
.x
;
978 x
+= ME_PointFromChar(editor
, &pRun
->member
.run
, pCursor
->nOffset
);
980 editor
->nUDArrowX
= x
;
987 ME_MoveCursorLines(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
989 ME_DisplayItem
*pRun
= pCursor
->pRun
;
990 ME_DisplayItem
*pItem
;
991 int x
= ME_GetXForArrow(editor
, pCursor
);
993 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
994 pRun
= ME_FindItemBack(pRun
, diRun
);
999 /* start of this row */
1000 pItem
= ME_FindItemBack(pRun
, diStartRow
);
1002 /* start of the previous row */
1003 pItem
= ME_FindItemBack(pItem
, diStartRow
);
1007 /* start of the next row */
1008 pItem
= ME_FindItemFwd(pRun
, diStartRow
);
1009 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1014 /* row not found - ignore */
1017 pCursor
->pRun
= ME_FindRunInRow(editor
, pItem
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1018 assert(pCursor
->pRun
);
1019 assert(pCursor
->pRun
->type
== diRun
);
1023 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1025 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1026 ME_DisplayItem
*pLast
, *p
;
1027 int x
, y
, ys
, yd
, yp
, yprev
;
1028 ME_Cursor tmp_curs
= *pCursor
;
1030 x
= ME_GetXForArrow(editor
, pCursor
);
1031 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1032 pRun
= ME_FindItemBack(pRun
, diRun
);
1034 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1035 assert(p
->type
== diStartRow
);
1036 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
1037 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
1038 yd
= y
- editor
->sizeWindow
.cy
;
1042 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
1045 if (p
->type
== diParagraph
) { /* crossing paragraphs */
1046 if (p
->member
.para
.prev_para
== NULL
)
1048 yp
= p
->member
.para
.prev_para
->member
.para
.nYPos
;
1051 y
= yp
+ p
->member
.row
.nYPos
;
1058 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1059 ME_UpdateSelection(editor
, &tmp_curs
);
1060 if (yprev
< editor
->sizeWindow
.cy
)
1062 ME_EnsureVisible(editor
, ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
));
1067 ME_ScrollUp(editor
, ys
-yprev
);
1069 assert(pCursor
->pRun
);
1070 assert(pCursor
->pRun
->type
== diRun
);
1073 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1074 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1075 In such a situation, clicking the scrollbar restores its position back to the
1076 normal range (ie. sets it to (doclength-screenheight)). */
1078 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1080 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1081 ME_DisplayItem
*pLast
, *p
;
1082 int x
, y
, ys
, yd
, yp
, yprev
;
1083 ME_Cursor tmp_curs
= *pCursor
;
1085 x
= ME_GetXForArrow(editor
, pCursor
);
1086 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1087 pRun
= ME_FindItemBack(pRun
, diRun
);
1089 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1090 assert(p
->type
== diStartRow
);
1091 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
1092 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
1093 yd
= y
+ editor
->sizeWindow
.cy
;
1097 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
1100 if (p
->type
== diParagraph
) {
1101 yp
= p
->member
.para
.nYPos
;
1104 y
= yp
+ p
->member
.row
.nYPos
;
1111 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1112 ME_UpdateSelection(editor
, &tmp_curs
);
1113 if (yprev
>= editor
->nTotalLength
-editor
->sizeWindow
.cy
)
1115 ME_EnsureVisible(editor
, ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
));
1120 ME_ScrollUp(editor
,ys
-yprev
);
1122 assert(pCursor
->pRun
);
1123 assert(pCursor
->pRun
->type
== diRun
);
1126 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1128 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
1129 /* bCaretAtEnd doesn't make sense if the cursor isn't set at the
1130 first character of the next row */
1131 assert(!editor
->bCaretAtEnd
|| !pCursor
->nOffset
);
1132 ME_WrapMarkedParagraphs(editor
);
1134 ME_DisplayItem
*pRun
;
1135 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
1136 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1140 pRun
= ME_FindItemFwd(pRow
, diRun
);
1142 pCursor
->pRun
= pRun
;
1143 pCursor
->nOffset
= 0;
1146 editor
->bCaretAtEnd
= FALSE
;
1149 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1151 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diTextStart
);
1153 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1155 pCursor
->pRun
= pRun
;
1156 pCursor
->nOffset
= 0;
1161 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1163 ME_DisplayItem
*pRow
;
1165 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1168 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1170 if (pRow
->type
== diStartRow
) {
1171 /* FIXME WTF was I thinking about here ? */
1172 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1174 pCursor
->pRun
= pRun
;
1175 pCursor
->nOffset
= 0;
1176 editor
->bCaretAtEnd
= 1;
1179 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1180 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1181 pCursor
->nOffset
= 0;
1182 editor
->bCaretAtEnd
= FALSE
;
1185 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1187 ME_DisplayItem
*p
= ME_FindItemFwd(pCursor
->pRun
, diTextEnd
);
1189 p
= ME_FindItemBack(p
, diRun
);
1191 assert(p
->member
.run
.nFlags
& MERF_ENDPARA
);
1193 pCursor
->nOffset
= 0;
1194 editor
->bCaretAtEnd
= FALSE
;
1197 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1199 return memcmp(&editor
->pCursors
[0], &editor
->pCursors
[1], sizeof(ME_Cursor
))!=0;
1202 static int ME_GetSelCursor(ME_TextEditor
*editor
, int dir
)
1204 int cdir
= ME_GetCursorOfs(editor
, 0) - ME_GetCursorOfs(editor
, 1);
1212 BOOL
ME_UpdateSelection(ME_TextEditor
*editor
, const ME_Cursor
*pTempCursor
)
1214 ME_Cursor old_anchor
= editor
->pCursors
[1];
1216 if (GetKeyState(VK_SHIFT
)>=0) /* cancelling selection */
1218 /* any selection was present ? if so, it's no more, repaint ! */
1219 editor
->pCursors
[1] = editor
->pCursors
[0];
1220 if (memcmp(pTempCursor
, &old_anchor
, sizeof(ME_Cursor
))) {
1227 if (!memcmp(pTempCursor
, &editor
->pCursors
[1], sizeof(ME_Cursor
))) /* starting selection */
1229 editor
->pCursors
[1] = *pTempCursor
;
1238 void ME_DeleteSelection(ME_TextEditor
*editor
)
1241 ME_GetSelection(editor
, &from
, &to
);
1242 ME_DeleteTextAtCursor(editor
, ME_GetSelCursor(editor
,-1), to
-from
);
1245 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1250 ME_GetSelection(editor
, &from
, &to
);
1253 ME_CursorFromCharOfs(editor
, from
, &c
);
1254 style
= c
.pRun
->member
.run
.style
;
1255 ME_AddRefStyle(style
); /* ME_GetInsertStyle has already done that */
1258 style
= ME_GetInsertStyle(editor
, 0);
1262 void ME_SendSelChange(ME_TextEditor
*editor
)
1266 ME_ClearTempStyle(editor
);
1268 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1271 sc
.nmhdr
.hwndFrom
= editor
->hWnd
;
1272 sc
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
1273 sc
.nmhdr
.code
= EN_SELCHANGE
;
1274 SendMessageW(editor
->hWnd
, EM_EXGETSEL
, 0, (LPARAM
)&sc
.chrg
);
1275 sc
.seltyp
= SEL_EMPTY
;
1276 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1277 sc
.seltyp
|= SEL_TEXT
;
1278 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* wth were RICHEDIT authors thinking ? */
1279 sc
.seltyp
|= SEL_MULTICHAR
;
1280 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
, sc
.nmhdr
.idFrom
, (LPARAM
)&sc
);
1284 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1287 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1288 ME_Cursor tmp_curs
= *p
;
1289 BOOL success
= FALSE
;
1291 ME_CheckCharOffsets(editor
);
1292 editor
->nUDArrowX
= -1;
1295 editor
->bCaretAtEnd
= 0;
1297 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1299 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1302 editor
->bCaretAtEnd
= 0;
1304 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1306 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1309 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1312 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1315 ME_ArrowPageUp(editor
, &tmp_curs
);
1318 ME_ArrowPageDown(editor
, &tmp_curs
);
1322 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1324 ME_ArrowHome(editor
, &tmp_curs
);
1325 editor
->bCaretAtEnd
= 0;
1330 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1332 ME_ArrowEnd(editor
, &tmp_curs
);
1337 editor
->pCursors
[1] = tmp_curs
;
1340 ME_InvalidateSelection(editor
);
1342 HideCaret(editor
->hWnd
);
1343 ME_EnsureVisible(editor
, tmp_curs
.pRun
);
1344 ME_ShowCaret(editor
);
1345 ME_SendSelChange(editor
);