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
)
61 && (how
->flags
& GTL_USECRLF
)
62 && !editor
->bEmulateVersion10
) /* Ignore GTL_USECRLF flag in 1.0 emulation */
63 length
+= editor
->nParagraphs
- 1;
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 int ME_SetSelection(ME_TextEditor
*editor
, int from
, int to
)
85 const int len
= ME_GetTextLength(editor
);
87 /* all negative values are effectively the same */
94 if (from
== 0 && to
== -1)
96 editor
->pCursors
[1].pRun
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
);
97 editor
->pCursors
[1].nOffset
= 0;
98 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
99 editor
->pCursors
[0].nOffset
= 0;
100 ME_InvalidateSelection(editor
);
101 ME_ClearTempStyle(editor
);
105 /* if both values are equal and also out of bound, that means to */
106 /* put the selection at the end of the text */
107 if ((from
== to
) && (to
< 0 || to
> len
))
113 /* if from is negative and to is positive then selection is */
114 /* deselected and caret moved to end of the current selection */
118 ME_GetSelection(editor
, &start
, &end
);
119 editor
->pCursors
[1] = editor
->pCursors
[0];
121 ME_ClearTempStyle(editor
);
125 /* adjust to if it's a negative value */
129 /* flip from and to if they are reversed */
137 /* after fiddling with the values, we find from > len && to > len */
140 /* special case with to too big */
147 editor
->pCursors
[1].pRun
= editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
148 editor
->pCursors
[1].nOffset
= editor
->pCursors
[0].nOffset
= 0;
149 ME_InvalidateSelection(editor
);
150 ME_ClearTempStyle(editor
);
154 ME_RunOfsFromCharOfs(editor
, from
, &editor
->pCursors
[1].pRun
, &editor
->pCursors
[1].nOffset
);
155 ME_RunOfsFromCharOfs(editor
, to
, &editor
->pCursors
[0].pRun
, &editor
->pCursors
[0].nOffset
);
161 ME_GetCursorCoordinates(ME_TextEditor
*editor
, ME_Cursor
*pCursor
,
162 int *x
, int *y
, int *height
)
164 ME_DisplayItem
*pCursorRun
= pCursor
->pRun
;
165 ME_DisplayItem
*pSizeRun
= pCursor
->pRun
;
167 assert(height
&& x
&& y
);
168 assert(!(ME_GetParagraph(pCursorRun
)->member
.para
.nFlags
& MEPF_REWRAP
));
169 assert(pCursor
->pRun
);
170 assert(pCursor
->pRun
->type
== diRun
);
172 if (pCursorRun
->type
== diRun
) {
173 ME_DisplayItem
*row
= ME_FindItemBack(pCursorRun
, diStartRowOrParagraph
);
176 HDC hDC
= GetDC(editor
->hWnd
);
178 ME_DisplayItem
*run
= pCursorRun
;
179 ME_DisplayItem
*para
= NULL
;
182 ME_InitContext(&c
, editor
, hDC
);
184 if (!pCursor
->nOffset
)
186 ME_DisplayItem
*prev
= ME_FindItemBack(pCursorRun
, diRunOrParagraph
);
188 if (prev
->type
== diRun
)
191 assert(row
->type
== diStartRow
); /* paragraph -> run without start row ?*/
192 para
= ME_FindItemBack(row
, diParagraph
);
194 assert(para
->type
== diParagraph
);
195 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
&&
196 run
== ME_FindItemFwd(row
, diRun
))
198 ME_DisplayItem
*tmp
= ME_FindItemBack(row
, diRunOrParagraph
);
200 if (tmp
->type
== diRun
)
202 row
= ME_FindItemBack(tmp
, diStartRow
);
203 pSizeRun
= run
= tmp
;
205 assert(run
->type
== diRun
);
206 sz
= ME_GetRunSize(&c
, ¶
->member
.para
,
207 &run
->member
.run
, ME_StrLen(run
->member
.run
.strText
),
208 row
->member
.row
.nLMargin
);
211 if (pCursor
->nOffset
) {
212 sz
= ME_GetRunSize(&c
, ¶
->member
.para
, &run
->member
.run
, pCursor
->nOffset
,
213 row
->member
.row
.nLMargin
);
216 *height
= pSizeRun
->member
.run
.nAscent
+ pSizeRun
->member
.run
.nDescent
;
217 *x
= run
->member
.run
.pt
.x
+ sz
.cx
;
218 *y
= para
->member
.para
.nYPos
+ row
->member
.row
.nBaseline
+ run
->member
.run
.pt
.y
- pSizeRun
->member
.run
.nAscent
- ME_GetYScrollPos(editor
);
219 ME_DestroyContext(&c
, editor
->hWnd
);
223 *height
= 10; /* FIXME use global font */
230 ME_MoveCaret(ME_TextEditor
*editor
)
234 if (ME_WrapMarkedParagraphs(editor
))
235 ME_UpdateScrollBar(editor
);
236 ME_GetCursorCoordinates(editor
, &editor
->pCursors
[0], &x
, &y
, &height
);
237 if(editor
->bHaveFocus
)
239 CreateCaret(editor
->hWnd
, NULL
, 0, height
);
245 void ME_ShowCaret(ME_TextEditor
*ed
)
252 void ME_HideCaret(ME_TextEditor
*ed
)
261 void ME_InternalDeleteText(ME_TextEditor
*editor
, int nOfs
,
270 ME_CursorFromCharOfs(editor
, nOfs
, &c
);
271 run
= &c
.pRun
->member
.run
;
272 if (run
->nFlags
& MERF_ENDPARA
) {
273 int eollen
= run
->nCR
+ run
->nLF
;
275 if (!ME_FindItemFwd(c
.pRun
, diParagraph
))
279 ME_JoinParagraphs(editor
, ME_GetParagraph(c
.pRun
));
280 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
281 ME_CheckCharOffsets(editor
);
282 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
288 int nIntendedChars
= nChars
;
289 int nCharsToDelete
= nChars
;
293 ME_FindItemBack(c
.pRun
, diParagraph
)->member
.para
.nFlags
|= MEPF_REWRAP
;
296 ME_StrRelPos(run
->strText
, loc
, &nChars
);
297 /* nChars is the number of characters that should be deleted from the
298 FOLLOWING runs (these AFTER cursor.pRun)
299 nCharsToDelete is a number of chars to delete from THIS run */
300 nCharsToDelete
-= nChars
;
301 shift
-= nCharsToDelete
;
302 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
303 nCharsToDelete
, nIntendedChars
, nChars
, c
.nOffset
,
304 debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
306 if (!c
.nOffset
&& ME_StrVLen(run
->strText
) == nCharsToDelete
)
308 /* undo = reinsert whole run */
309 /* nOfs is a character offset (from the start of the document
310 to the current (deleted) run */
311 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
313 pUndo
->di
.member
.run
.nCharOfs
= nOfs
;
317 /* undo = reinsert partial run */
318 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
320 ME_DestroyString(pUndo
->di
.member
.run
.strText
);
321 pUndo
->di
.member
.run
.nCharOfs
= nOfs
;
322 pUndo
->di
.member
.run
.strText
= ME_MakeStringN(run
->strText
->szData
+c
.nOffset
, nCharsToDelete
);
325 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
326 TRACE("Shift value: %d\n", shift
);
327 ME_StrDeleteV(run
->strText
, c
.nOffset
, nCharsToDelete
);
329 /* update cursors (including c) */
330 for (i
=-1; i
<editor
->nCursors
; i
++) {
331 ME_Cursor
*pThisCur
= editor
->pCursors
+ i
;
332 if (i
== -1) pThisCur
= &c
;
333 if (pThisCur
->pRun
== cursor
.pRun
) {
334 if (pThisCur
->nOffset
> cursor
.nOffset
) {
335 if (pThisCur
->nOffset
-cursor
.nOffset
< nCharsToDelete
)
336 pThisCur
->nOffset
= cursor
.nOffset
;
338 pThisCur
->nOffset
-= nCharsToDelete
;
339 assert(pThisCur
->nOffset
>= 0);
340 assert(pThisCur
->nOffset
<= ME_StrVLen(run
->strText
));
342 if (pThisCur
->nOffset
== ME_StrVLen(run
->strText
))
344 pThisCur
->pRun
= ME_FindItemFwd(pThisCur
->pRun
, diRunOrParagraphOrEnd
);
345 assert(pThisCur
->pRun
->type
== diRun
);
346 pThisCur
->nOffset
= 0;
351 /* c = updated data now */
353 if (c
.pRun
== cursor
.pRun
)
354 ME_SkipAndPropagateCharOffset(c
.pRun
, shift
);
356 ME_PropagateCharOffset(c
.pRun
, shift
);
358 if (!ME_StrVLen(cursor
.pRun
->member
.run
.strText
))
360 TRACE("Removing useless run\n");
361 ME_Remove(cursor
.pRun
);
362 ME_DestroyDisplayItem(cursor
.pRun
);
367 ME_CheckCharOffsets(editor);
374 void ME_DeleteTextAtCursor(ME_TextEditor
*editor
, int nCursor
,
377 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
378 /* text operations set modified state */
379 editor
->nModifyStep
= 1;
380 ME_InternalDeleteText(editor
, ME_GetCursorOfs(editor
, nCursor
), nChars
);
383 static ME_DisplayItem
*
384 ME_InternalInsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
385 const WCHAR
*str
, int len
, ME_Style
*style
,
388 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
390 editor
->bCaretAtEnd
= FALSE
;
392 assert(p
->pRun
->type
== diRun
);
394 return ME_InsertRunAtCursor(editor
, p
, style
, str
, len
, flags
);
398 void ME_InsertOLEFromCursor(ME_TextEditor
*editor
, const REOBJECT
* reo
, int nCursor
)
400 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
405 if (ME_IsSelection(editor
))
406 ME_DeleteSelection(editor
);
408 di
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
410 di
->member
.run
.ole_obj
= ALLOC_OBJ(*reo
);
411 ME_CopyReObject(di
->member
.run
.ole_obj
, reo
);
412 ME_SendSelChange(editor
);
416 void ME_InsertEndRowFromCursor(ME_TextEditor
*editor
, int nCursor
)
418 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
423 if (ME_IsSelection(editor
))
424 ME_DeleteSelection(editor
);
426 di
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
428 ME_SendSelChange(editor
);
432 ME_InsertTableCellFromCursor(ME_TextEditor
*editor
, int nCursor
)
435 ME_DisplayItem
*p
, *run
;
436 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
438 p
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, pStyle
,
441 while ((run
= ME_FindItemBack(run
, diRunOrParagraph
))->type
== diRun
)
443 if (run
->member
.run
.nFlags
& MERF_CELL
)
445 assert(run
->member
.run
.pCell
->next
);
446 p
->member
.run
.pCell
= run
->member
.run
.pCell
->next
;
450 assert(run
->type
== diParagraph
);
451 assert(run
->member
.para
.bTable
);
452 assert(run
->member
.para
.pCells
);
453 p
->member
.run
.pCell
= run
->member
.para
.pCells
;
457 void ME_InsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
458 const WCHAR
*str
, int len
, ME_Style
*style
)
464 /* FIXME really HERE ? */
465 if (ME_IsSelection(editor
))
466 ME_DeleteSelection(editor
);
468 /* FIXME: is this too slow? */
469 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
470 oldLen
= ME_GetTextLength(editor
);
472 /* text operations set modified state */
473 editor
->nModifyStep
= 1;
477 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
481 /* grow the text limit to fit our text */
482 if(editor
->nTextLimit
< oldLen
+len
)
483 editor
->nTextLimit
= oldLen
+ len
;
488 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
489 while(pos
-str
< len
&& *pos
!= '\r' && *pos
!= '\n' && *pos
!= '\t')
491 if (pos
-str
< len
&& *pos
== '\t') { /* handle tabs */
495 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
497 ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, style
, MERF_TAB
);
506 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
507 if (!editor
->bEmulateVersion10
&& pos
-str
< len
-2 && pos
[0] == '\r' && pos
[1] == '\r' && pos
[2] == '\n') {
511 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
513 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, style
, 0);
522 if (pos
-str
< len
) { /* handle EOLs */
523 ME_DisplayItem
*tp
, *end_run
;
528 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
529 p
= &editor
->pCursors
[nCursor
];
531 ME_SplitRunSimple(editor
, p
->pRun
, p
->nOffset
);
532 p
= &editor
->pCursors
[nCursor
];
534 tmp_style
= ME_GetInsertStyle(editor
, nCursor
);
535 /* ME_SplitParagraph increases style refcount */
537 /* Encode and fill number of CR and LF according to emulation mode */
538 if (editor
->bEmulateVersion10
) {
541 /* We have to find out how many consecutive \r are there, and if there
542 is a \n terminating the run of \r's. */
543 numCR
= 0; numLF
= 0;
545 while (tpos
-str
< len
&& *tpos
== '\r') {
549 if (tpos
-str
>= len
) {
550 /* Reached end of text without finding anything but '\r' */
554 numCR
= 1; numLF
= 0;
555 } else if (*tpos
== '\n') {
556 /* The entire run of \r's plus the one \n is one single line break */
560 /* Found some other content past the run of \r's */
562 numCR
= 1; numLF
= 0;
565 if(pos
-str
< len
&& *pos
=='\r')
567 if(pos
-str
< len
&& *pos
=='\n')
569 numCR
= 1; numLF
= 0;
571 tp
= ME_SplitParagraph(editor
, p
->pRun
, p
->pRun
->member
.run
.style
, numCR
, numLF
);
572 p
->pRun
= ME_FindItemFwd(tp
, diRun
);
573 end_run
= ME_FindItemBack(tp
, diRun
);
574 ME_ReleaseStyle(end_run
->member
.run
.style
);
575 end_run
->member
.run
.style
= tmp_style
;
584 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, len
, style
, 0);
591 ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
593 ME_DisplayItem
*pRun
= pCursor
->pRun
;
597 if (!pCursor
->nOffset
)
600 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
607 if (pRun
->member
.para
.prev_para
->type
== diTextStart
)
609 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
610 /* every paragraph ought to have at least one run */
611 assert(pRun
&& pRun
->type
== diRun
);
612 assert(pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
615 assert(pRun
->type
!= diRun
&& pRun
->type
!= diParagraph
);
618 } while (RUN_IS_HIDDEN(&pRun
->member
.run
));
619 pCursor
->pRun
= pRun
;
620 if (pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
621 pCursor
->nOffset
= 0;
623 pCursor
->nOffset
= pRun
->member
.run
.strText
->nLen
;
626 if (pCursor
->nOffset
)
627 pCursor
->nOffset
= ME_StrRelPos2(pCursor
->pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
632 if (!(pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
634 int new_ofs
= ME_StrRelPos2(pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
636 if (new_ofs
< pRun
->member
.run
.strText
->nLen
)
638 pCursor
->nOffset
= new_ofs
;
643 pRun
= ME_FindItemFwd(pRun
, diRun
);
644 } while (pRun
&& RUN_IS_HIDDEN(&pRun
->member
.run
));
647 pCursor
->pRun
= pRun
;
648 pCursor
->nOffset
= 0;
657 ME_MoveCursorWords(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
659 ME_DisplayItem
*pRun
= cursor
->pRun
, *pOtherRun
;
660 int nOffset
= cursor
->nOffset
;
664 /* Backward movement */
667 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
668 nOffset
, WB_MOVEWORDLEFT
);
671 pOtherRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
672 if (pOtherRun
->type
== diRun
)
674 if (ME_CallWordBreakProc(editor
, pOtherRun
->member
.run
.strText
,
675 pOtherRun
->member
.run
.strText
->nLen
- 1,
677 && !(pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
678 && !(cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
679 && !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
, 0,
683 nOffset
= pOtherRun
->member
.run
.strText
->nLen
;
685 else if (pOtherRun
->type
== diParagraph
)
687 if (cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
689 /* Paragraph breaks are treated as separate words */
690 if (pOtherRun
->member
.para
.prev_para
->type
== diTextStart
)
692 pRun
= ME_FindItemBack(pOtherRun
, diRunOrParagraph
);
700 /* Forward movement */
701 BOOL last_delim
= FALSE
;
705 if (last_delim
&& !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
706 nOffset
, WB_ISDELIMITER
))
708 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
709 nOffset
, WB_MOVEWORDRIGHT
);
710 if (nOffset
< pRun
->member
.run
.strText
->nLen
)
712 pOtherRun
= ME_FindItemFwd(pRun
, diRunOrParagraphOrEnd
);
713 if (pOtherRun
->type
== diRun
)
715 last_delim
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
716 nOffset
- 1, WB_ISDELIMITER
);
720 else if (pOtherRun
->type
== diParagraph
)
722 if (cursor
->pRun
== pRun
)
723 pRun
= ME_FindItemFwd(pOtherRun
, diRun
);
729 if (cursor
->pRun
== pRun
)
737 cursor
->nOffset
= nOffset
;
743 ME_SelectWord(ME_TextEditor
*editor
)
745 if (!(editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
746 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], -1);
747 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], +1);
748 ME_InvalidateSelection(editor
);
749 ME_SendSelChange(editor
);
753 int ME_GetCursorOfs(ME_TextEditor
*editor
, int nCursor
)
755 ME_Cursor
*pCursor
= &editor
->pCursors
[nCursor
];
756 return ME_GetParagraph(pCursor
->pRun
)->member
.para
.nCharOfs
757 + pCursor
->pRun
->member
.run
.nCharOfs
+ pCursor
->nOffset
;
760 static void ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
, ME_Cursor
*result
, BOOL
*is_eol
)
762 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
763 ME_DisplayItem
*last
= NULL
;
770 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->member
.para
.next_para
)
772 assert(p
->type
== diParagraph
);
773 if (y
< p
->member
.para
.nYPos
+ p
->member
.para
.nHeight
)
775 y
-= p
->member
.para
.nYPos
;
776 p
= ME_FindItemFwd(p
, diStartRow
);
781 for (; p
!= editor
->pBuffer
->pLast
; )
784 assert(p
->type
== diStartRow
);
785 if (y
< p
->member
.row
.nYPos
+ p
->member
.row
.nHeight
)
787 p
= ME_FindItemFwd(p
, diRun
);
790 pp
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
791 if (pp
->type
!= diStartRow
)
793 p
= ME_FindItemFwd(p
, diRun
);
798 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->next
)
803 rx
= x
- p
->member
.run
.pt
.x
;
804 if (rx
< p
->member
.run
.nWidth
)
807 assert(p
->type
== diRun
);
808 if ((p
->member
.run
.nFlags
& MERF_ENDPARA
) || rx
< 0)
811 result
->nOffset
= ME_CharFromPointCursor(editor
, rx
, &p
->member
.run
);
812 if (editor
->pCursors
[0].nOffset
== p
->member
.run
.strText
->nLen
&& rx
)
814 result
->pRun
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, diRun
);
821 p
= ME_FindItemFwd(p
, diRun
);
822 if (is_eol
) *is_eol
= 1;
823 rx
= 0; /* FIXME not sure */
827 rx
= 0; /* FIXME not sure */
834 result
->pRun
= ME_FindItemBack(p
, diRun
);
836 assert(result
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
841 ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
)
846 GetClientRect(editor
->hWnd
, &rc
);
847 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
)
849 y
+= ME_GetYScrollPos(editor
);
850 ME_FindPixelPos(editor
, x
, y
, &cursor
, NULL
);
851 return (ME_GetParagraph(cursor
.pRun
)->member
.para
.nCharOfs
852 + cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
);
856 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
)
858 ME_Cursor tmp_cursor
;
859 int is_selection
= 0;
861 editor
->nUDArrowX
= -1;
863 y
+= ME_GetYScrollPos(editor
);
865 tmp_cursor
= editor
->pCursors
[0];
866 is_selection
= ME_IsSelection(editor
);
868 if (x
>= editor
->selofs
)
870 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
871 if (GetKeyState(VK_SHIFT
)>=0)
873 editor
->pCursors
[1] = editor
->pCursors
[0];
875 else if (!is_selection
) {
876 editor
->pCursors
[1] = tmp_cursor
;
880 ME_InvalidateSelection(editor
);
881 HideCaret(editor
->hWnd
);
882 ME_MoveCaret(editor
);
883 ShowCaret(editor
->hWnd
);
884 ME_ClearTempStyle(editor
);
885 ME_SendSelChange(editor
);
889 ME_DisplayItem
*pRow
;
893 /* Set pCursors[0] to beginning of line */
894 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[1], &editor
->bCaretAtEnd
);
895 /* Set pCursors[1] to end of line */
896 pRow
= ME_FindItemFwd(editor
->pCursors
[1].pRun
, diStartRowOrParagraphOrEnd
);
898 /* pCursor[0] is the position where the cursor will be drawn,
899 * pCursor[1] is the other end of the selection range
900 * pCursor[2] and [3] are backups of [0] and [1] so I
901 * don't have to look them up again
904 if (pRow
->type
== diStartRow
) {
905 /* FIXME WTF was I thinking about here ? */
906 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
908 editor
->pCursors
[0].pRun
= pRun
;
909 editor
->pCursors
[0].nOffset
= 0;
910 editor
->bCaretAtEnd
= 1;
912 editor
->pCursors
[0].pRun
= ME_FindItemBack(pRow
, diRun
);
913 assert(editor
->pCursors
[0].pRun
&& editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
914 editor
->pCursors
[0].nOffset
= 0;
915 editor
->bCaretAtEnd
= 0;
917 editor
->pCursors
[2] = editor
->pCursors
[0];
918 editor
->pCursors
[3] = editor
->pCursors
[1];
919 ME_InvalidateSelection(editor
);
920 HideCaret(editor
->hWnd
);
921 ME_MoveCaret(editor
);
922 ShowCaret(editor
->hWnd
);
923 ME_ClearTempStyle(editor
);
924 ME_SendSelChange(editor
);
928 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
930 ME_Cursor tmp_cursor
;
932 y
+= ME_GetYScrollPos(editor
);
934 tmp_cursor
= editor
->pCursors
[0];
935 /* FIXME: do something with the return value of ME_FindPixelPos */
936 if (!editor
->linesel
)
937 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
938 else ME_FindPixelPos(editor
, (y
> editor
->sely
) * editor
->rcFormat
.right
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
940 if (!memcmp(&tmp_cursor
, editor
->pCursors
, sizeof(tmp_cursor
)))
943 ME_InvalidateSelection(editor
);
944 if (!editor
->linesel
)
945 editor
->pCursors
[0] = tmp_cursor
;
946 else if (!memcmp(&tmp_cursor
, editor
->pCursors
+2, sizeof(tmp_cursor
)) ||
947 !memcmp(&tmp_cursor
, editor
->pCursors
+3, sizeof(tmp_cursor
)))
949 editor
->pCursors
[0] = editor
->pCursors
[2];
950 editor
->pCursors
[1] = editor
->pCursors
[3];
952 else if (y
< editor
->sely
)
954 editor
->pCursors
[0] = tmp_cursor
;
955 editor
->pCursors
[1] = editor
->pCursors
[2];
959 editor
->pCursors
[0] = tmp_cursor
;
960 editor
->pCursors
[1] = editor
->pCursors
[3];
963 HideCaret(editor
->hWnd
);
964 ME_MoveCaret(editor
);
965 ME_InvalidateSelection(editor
);
966 ShowCaret(editor
->hWnd
);
967 ME_SendSelChange(editor
);
968 SendMessageW(editor
->hWnd
, EM_SCROLLCARET
, 0, 0);
971 static ME_DisplayItem
*ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
972 int x
, int *pOffset
, int *pbCaretAtEnd
)
974 ME_DisplayItem
*pNext
, *pLastRun
;
975 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
976 assert(pNext
->type
== diRun
);
978 if (pbCaretAtEnd
) *pbCaretAtEnd
= FALSE
;
979 if (pOffset
) *pOffset
= 0;
981 int run_x
= pNext
->member
.run
.pt
.x
;
982 int width
= pNext
->member
.run
.nWidth
;
987 if (x
>= run_x
&& x
< run_x
+width
)
989 int ch
= ME_CharFromPointCursor(editor
, x
-run_x
, &pNext
->member
.run
);
990 ME_String
*s
= pNext
->member
.run
.strText
;
998 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
999 } while(pNext
&& pNext
->type
== diRun
);
1001 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
1003 pNext
= ME_FindItemFwd(pNext
, diRun
);
1004 if (pbCaretAtEnd
) *pbCaretAtEnd
= TRUE
;
1011 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1013 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1016 if (editor
->nUDArrowX
!= -1)
1017 x
= editor
->nUDArrowX
;
1019 if (editor
->bCaretAtEnd
)
1021 pRun
= ME_FindItemBack(pRun
, diRun
);
1023 x
= pRun
->member
.run
.pt
.x
+ pRun
->member
.run
.nWidth
;
1026 x
= pRun
->member
.run
.pt
.x
;
1027 x
+= ME_PointFromChar(editor
, &pRun
->member
.run
, pCursor
->nOffset
);
1029 editor
->nUDArrowX
= x
;
1036 ME_MoveCursorLines(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
1038 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1039 ME_DisplayItem
*pItem
;
1040 int x
= ME_GetXForArrow(editor
, pCursor
);
1042 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1043 pRun
= ME_FindItemBack(pRun
, diRun
);
1048 /* start of this row */
1049 pItem
= ME_FindItemBack(pRun
, diStartRow
);
1051 /* start of the previous row */
1052 pItem
= ME_FindItemBack(pItem
, diStartRow
);
1056 /* start of the next row */
1057 pItem
= ME_FindItemFwd(pRun
, diStartRow
);
1058 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1063 /* row not found - ignore */
1066 pCursor
->pRun
= ME_FindRunInRow(editor
, pItem
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1067 assert(pCursor
->pRun
);
1068 assert(pCursor
->pRun
->type
== diRun
);
1072 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1074 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1075 ME_DisplayItem
*pLast
, *p
;
1076 int x
, y
, ys
, yd
, yp
, yprev
;
1077 ME_Cursor tmp_curs
= *pCursor
;
1079 x
= ME_GetXForArrow(editor
, pCursor
);
1080 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1081 pRun
= ME_FindItemBack(pRun
, diRun
);
1083 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1084 assert(p
->type
== diStartRow
);
1085 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
1086 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
1087 yd
= y
- editor
->sizeWindow
.cy
;
1091 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
1094 if (p
->type
== diParagraph
) { /* crossing paragraphs */
1095 if (p
->member
.para
.prev_para
== NULL
)
1097 yp
= p
->member
.para
.prev_para
->member
.para
.nYPos
;
1100 y
= yp
+ p
->member
.row
.nYPos
;
1107 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1108 ME_UpdateSelection(editor
, &tmp_curs
);
1109 if (yprev
< editor
->sizeWindow
.cy
)
1111 ME_EnsureVisible(editor
, ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
));
1116 ME_ScrollUp(editor
, ys
-yprev
);
1118 assert(pCursor
->pRun
);
1119 assert(pCursor
->pRun
->type
== diRun
);
1122 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1123 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1124 In such a situation, clicking the scrollbar restores its position back to the
1125 normal range (ie. sets it to (doclength-screenheight)). */
1127 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1129 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1130 ME_DisplayItem
*pLast
, *p
;
1131 int x
, y
, ys
, yd
, yp
, yprev
;
1132 ME_Cursor tmp_curs
= *pCursor
;
1134 x
= ME_GetXForArrow(editor
, pCursor
);
1135 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1136 pRun
= ME_FindItemBack(pRun
, diRun
);
1138 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1139 assert(p
->type
== diStartRow
);
1140 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
1141 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
1142 yd
= y
+ editor
->sizeWindow
.cy
;
1146 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
1149 if (p
->type
== diParagraph
) {
1150 yp
= p
->member
.para
.nYPos
;
1153 y
= yp
+ p
->member
.row
.nYPos
;
1160 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1161 ME_UpdateSelection(editor
, &tmp_curs
);
1162 if (yprev
>= editor
->nTotalLength
-editor
->sizeWindow
.cy
)
1164 ME_EnsureVisible(editor
, ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
));
1169 ME_ScrollUp(editor
,ys
-yprev
);
1171 assert(pCursor
->pRun
);
1172 assert(pCursor
->pRun
->type
== diRun
);
1175 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1177 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
1178 ME_WrapMarkedParagraphs(editor
);
1180 ME_DisplayItem
*pRun
;
1181 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
1182 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1186 pRun
= ME_FindItemFwd(pRow
, diRun
);
1188 pCursor
->pRun
= pRun
;
1189 pCursor
->nOffset
= 0;
1192 editor
->bCaretAtEnd
= FALSE
;
1195 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1197 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diTextStart
);
1199 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1201 pCursor
->pRun
= pRun
;
1202 pCursor
->nOffset
= 0;
1207 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1209 ME_DisplayItem
*pRow
;
1211 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1214 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1216 if (pRow
->type
== diStartRow
) {
1217 /* FIXME WTF was I thinking about here ? */
1218 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1220 pCursor
->pRun
= pRun
;
1221 pCursor
->nOffset
= 0;
1222 editor
->bCaretAtEnd
= 1;
1225 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1226 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1227 pCursor
->nOffset
= 0;
1228 editor
->bCaretAtEnd
= FALSE
;
1231 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1233 ME_DisplayItem
*p
= ME_FindItemFwd(pCursor
->pRun
, diTextEnd
);
1235 p
= ME_FindItemBack(p
, diRun
);
1237 assert(p
->member
.run
.nFlags
& MERF_ENDPARA
);
1239 pCursor
->nOffset
= 0;
1240 editor
->bCaretAtEnd
= FALSE
;
1243 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1245 return memcmp(&editor
->pCursors
[0], &editor
->pCursors
[1], sizeof(ME_Cursor
))!=0;
1248 static int ME_GetSelCursor(ME_TextEditor
*editor
, int dir
)
1250 int cdir
= ME_GetCursorOfs(editor
, 0) - ME_GetCursorOfs(editor
, 1);
1258 BOOL
ME_UpdateSelection(ME_TextEditor
*editor
, const ME_Cursor
*pTempCursor
)
1260 ME_Cursor old_anchor
= editor
->pCursors
[1];
1262 if (GetKeyState(VK_SHIFT
)>=0) /* cancelling selection */
1264 /* any selection was present ? if so, it's no more, repaint ! */
1265 editor
->pCursors
[1] = editor
->pCursors
[0];
1266 if (memcmp(pTempCursor
, &old_anchor
, sizeof(ME_Cursor
))) {
1273 if (!memcmp(pTempCursor
, &editor
->pCursors
[1], sizeof(ME_Cursor
))) /* starting selection */
1275 editor
->pCursors
[1] = *pTempCursor
;
1284 void ME_DeleteSelection(ME_TextEditor
*editor
)
1287 ME_GetSelection(editor
, &from
, &to
);
1288 ME_DeleteTextAtCursor(editor
, ME_GetSelCursor(editor
,-1), to
-from
);
1291 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1293 return ME_GetInsertStyle(editor
, 0);
1296 void ME_SendSelChange(ME_TextEditor
*editor
)
1300 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1303 sc
.nmhdr
.hwndFrom
= editor
->hWnd
;
1304 sc
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
1305 sc
.nmhdr
.code
= EN_SELCHANGE
;
1306 SendMessageW(editor
->hWnd
, EM_EXGETSEL
, 0, (LPARAM
)&sc
.chrg
);
1307 sc
.seltyp
= SEL_EMPTY
;
1308 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1309 sc
.seltyp
|= SEL_TEXT
;
1310 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* wth were RICHEDIT authors thinking ? */
1311 sc
.seltyp
|= SEL_MULTICHAR
;
1312 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1313 sc
.chrg
.cpMin
, sc
.chrg
.cpMax
, sc
.seltyp
,
1314 (sc
.seltyp
& SEL_TEXT
) ? "SEL_TEXT" : "",
1315 (sc
.seltyp
& SEL_MULTICHAR
) ? "SEL_MULTICHAR" : "");
1316 if (sc
.chrg
.cpMin
!= editor
->notified_cr
.cpMin
|| sc
.chrg
.cpMax
!= editor
->notified_cr
.cpMax
)
1318 ME_ClearTempStyle(editor
);
1320 editor
->notified_cr
= sc
.chrg
;
1321 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
, sc
.nmhdr
.idFrom
, (LPARAM
)&sc
);
1326 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1329 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1330 ME_Cursor tmp_curs
= *p
;
1331 BOOL success
= FALSE
;
1333 ME_CheckCharOffsets(editor
);
1334 editor
->nUDArrowX
= -1;
1337 editor
->bCaretAtEnd
= 0;
1339 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1341 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1344 editor
->bCaretAtEnd
= 0;
1346 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1348 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1351 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1354 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1357 ME_ArrowPageUp(editor
, &tmp_curs
);
1360 ME_ArrowPageDown(editor
, &tmp_curs
);
1364 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1366 ME_ArrowHome(editor
, &tmp_curs
);
1367 editor
->bCaretAtEnd
= 0;
1372 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1374 ME_ArrowEnd(editor
, &tmp_curs
);
1379 editor
->pCursors
[1] = tmp_curs
;
1382 ME_InvalidateSelection(editor
);
1384 HideCaret(editor
->hWnd
);
1385 ME_EnsureVisible(editor
, tmp_curs
.pRun
);
1386 ME_ShowCaret(editor
);
1387 ME_SendSelChange(editor
);