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
);
31 void ME_GetSelection(ME_TextEditor
*editor
, int *from
, int *to
)
33 *from
= ME_GetCursorOfs(editor
, 0);
34 *to
= ME_GetCursorOfs(editor
, 1);
44 int ME_GetTextLength(ME_TextEditor
*editor
)
46 return ME_CharOfsFromRunOfs(editor
, ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
), 0);
50 int ME_GetTextLengthEx(ME_TextEditor
*editor
, GETTEXTLENGTHEX
*how
)
54 if (how
->flags
& GTL_PRECISE
&& how
->flags
& GTL_CLOSE
)
56 if (how
->flags
& GTL_NUMCHARS
&& how
->flags
& GTL_NUMBYTES
)
59 length
= ME_GetTextLength(editor
);
61 if (how
->flags
& GTL_USECRLF
)
62 length
+= editor
->nParagraphs
;
64 if (how
->flags
& GTL_NUMBYTES
)
68 if (how
->codepage
== 1200)
70 if (how
->flags
& GTL_PRECISE
)
71 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
72 if (GetCPInfo(how
->codepage
, &cpinfo
))
73 return length
* cpinfo
.MaxCharSize
;
74 ERR("Invalid codepage %u\n", how
->codepage
);
81 int ME_SetSelection(ME_TextEditor
*editor
, int from
, int to
)
84 const int len
= ME_GetTextLength(editor
);
86 /* all negative values are effectively the same */
93 if (from
== 0 && to
== -1)
95 editor
->pCursors
[1].pRun
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
);
96 editor
->pCursors
[1].nOffset
= 0;
97 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
98 editor
->pCursors
[0].nOffset
= 0;
99 ME_InvalidateSelection(editor
);
100 ME_ClearTempStyle(editor
);
104 /* if both values are equal and also out of bound, that means to */
105 /* put the selection at the end of the text */
106 if ((from
== to
) && (to
< 0 || to
> len
))
112 /* if from is negative and to is positive then selection is */
113 /* deselected and caret moved to end of the current selection */
117 ME_GetSelection(editor
, &start
, &end
);
118 editor
->pCursors
[1] = editor
->pCursors
[0];
120 ME_ClearTempStyle(editor
);
124 /* adjust to if it's a negative value */
128 /* flip from and to if they are reversed */
136 /* after fiddling with the values, we find from > len && to > len */
139 /* special case with to too big */
146 editor
->pCursors
[1].pRun
= editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
147 editor
->pCursors
[1].nOffset
= editor
->pCursors
[0].nOffset
= 0;
148 ME_InvalidateSelection(editor
);
149 ME_ClearTempStyle(editor
);
153 ME_RunOfsFromCharOfs(editor
, from
, &editor
->pCursors
[1].pRun
, &editor
->pCursors
[1].nOffset
);
154 ME_RunOfsFromCharOfs(editor
, to
, &editor
->pCursors
[0].pRun
, &editor
->pCursors
[0].nOffset
);
160 ME_GetCursorCoordinates(ME_TextEditor
*editor
, ME_Cursor
*pCursor
,
161 int *x
, int *y
, int *height
)
163 ME_DisplayItem
*pCursorRun
= pCursor
->pRun
;
164 ME_DisplayItem
*pSizeRun
= pCursor
->pRun
;
166 assert(!pCursor
->nOffset
|| !editor
->bCaretAtEnd
);
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
&& !editor
->bCaretAtEnd
)
186 ME_DisplayItem
*prev
= ME_FindItemBack(pCursorRun
, diRunOrStartRow
);
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
, &run
->member
.run
, ME_StrLen(run
->member
.run
.strText
));
209 if (pCursor
->nOffset
&& !(run
->member
.run
.nFlags
& MERF_SKIPPED
)) {
210 sz
= ME_GetRunSize(&c
, ¶
->member
.para
, &run
->member
.run
, pCursor
->nOffset
);
213 *height
= pSizeRun
->member
.run
.nAscent
+ pSizeRun
->member
.run
.nDescent
;
214 *x
= run
->member
.run
.pt
.x
+ sz
.cx
;
215 *y
= para
->member
.para
.nYPos
+ row
->member
.row
.nBaseline
+ pSizeRun
->member
.run
.pt
.y
- pSizeRun
->member
.run
.nAscent
- ME_GetYScrollPos(editor
);
217 ME_DestroyContext(&c
);
218 ReleaseDC(editor
->hWnd
, hDC
);
222 *height
= 10; /* FIXME use global font */
229 ME_MoveCaret(ME_TextEditor
*editor
)
233 ME_WrapMarkedParagraphs(editor
);
234 ME_GetCursorCoordinates(editor
, &editor
->pCursors
[0], &x
, &y
, &height
);
235 CreateCaret(editor
->hWnd
, NULL
, 0, height
);
240 void ME_ShowCaret(ME_TextEditor
*ed
)
246 void ME_HideCaret(ME_TextEditor
*ed
)
252 void ME_InternalDeleteText(ME_TextEditor
*editor
, int nOfs
,
261 ME_CursorFromCharOfs(editor
, nOfs
, &c
);
262 run
= &c
.pRun
->member
.run
;
263 if (run
->nFlags
& MERF_ENDPARA
) {
264 if (!ME_FindItemFwd(c
.pRun
, diParagraph
))
268 ME_JoinParagraphs(editor
, ME_GetParagraph(c
.pRun
));
269 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
270 ME_CheckCharOffsets(editor
);
272 if (editor
->bEmulateVersion10
&& nChars
)
279 int nIntendedChars
= nChars
;
280 int nCharsToDelete
= nChars
;
284 ME_FindItemBack(c
.pRun
, diParagraph
)->member
.para
.nFlags
|= MEPF_REWRAP
;
287 ME_StrRelPos(run
->strText
, loc
, &nChars
);
288 /* nChars is the number of characters that should be deleted from the
289 FOLLOWING runs (these AFTER cursor.pRun)
290 nCharsToDelete is a number of chars to delete from THIS run */
291 nCharsToDelete
-= nChars
;
292 shift
-= nCharsToDelete
;
293 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
294 nCharsToDelete
, nIntendedChars
, nChars
, c
.nOffset
,
295 debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
297 if (!c
.nOffset
&& ME_StrVLen(run
->strText
) == nCharsToDelete
)
299 /* undo = reinsert whole run */
300 /* nOfs is a character offset (from the start of the document
301 to the current (deleted) run */
302 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
304 pUndo
->di
.member
.run
.nCharOfs
= nOfs
;
308 /* undo = reinsert partial run */
309 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
311 ME_DestroyString(pUndo
->di
.member
.run
.strText
);
312 pUndo
->di
.member
.run
.nCharOfs
= nOfs
;
313 pUndo
->di
.member
.run
.strText
= ME_MakeStringN(run
->strText
->szData
+c
.nOffset
, nCharsToDelete
);
316 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
317 TRACE("Shift value: %d\n", shift
);
318 ME_StrDeleteV(run
->strText
, c
.nOffset
, nCharsToDelete
);
320 /* update cursors (including c) */
321 for (i
=-1; i
<editor
->nCursors
; i
++) {
322 ME_Cursor
*pThisCur
= editor
->pCursors
+ i
;
323 if (i
== -1) pThisCur
= &c
;
324 if (pThisCur
->pRun
== cursor
.pRun
) {
325 if (pThisCur
->nOffset
> cursor
.nOffset
) {
326 if (pThisCur
->nOffset
-cursor
.nOffset
< nCharsToDelete
)
327 pThisCur
->nOffset
= cursor
.nOffset
;
329 pThisCur
->nOffset
-= nCharsToDelete
;
330 assert(pThisCur
->nOffset
>= 0);
331 assert(pThisCur
->nOffset
<= ME_StrVLen(run
->strText
));
333 if (pThisCur
->nOffset
== ME_StrVLen(run
->strText
))
335 pThisCur
->pRun
= ME_FindItemFwd(pThisCur
->pRun
, diRunOrParagraphOrEnd
);
336 assert(pThisCur
->pRun
->type
== diRun
);
337 pThisCur
->nOffset
= 0;
342 /* c = updated data now */
344 if (c
.pRun
== cursor
.pRun
)
345 ME_SkipAndPropagateCharOffset(c
.pRun
, shift
);
347 ME_PropagateCharOffset(c
.pRun
, shift
);
349 if (!ME_StrVLen(cursor
.pRun
->member
.run
.strText
))
351 TRACE("Removing useless run\n");
352 ME_Remove(cursor
.pRun
);
353 ME_DestroyDisplayItem(cursor
.pRun
);
358 ME_CheckCharOffsets(editor);
365 void ME_DeleteTextAtCursor(ME_TextEditor
*editor
, int nCursor
,
368 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
369 ME_InternalDeleteText(editor
, ME_GetCursorOfs(editor
, nCursor
), nChars
);
372 static ME_DisplayItem
*
373 ME_InternalInsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
374 const WCHAR
*str
, int len
, ME_Style
*style
,
377 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
379 editor
->bCaretAtEnd
= FALSE
;
381 assert(p
->pRun
->type
== diRun
);
383 return ME_InsertRunAtCursor(editor
, p
, style
, str
, len
, flags
);
387 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
388 void ME_InsertGraphicsFromCursor(ME_TextEditor
*editor
, int nCursor
)
390 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
394 if (ME_IsSelection(editor
))
395 ME_DeleteSelection(editor
);
397 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
399 ME_SendSelChange(editor
);
404 ME_InsertTableCellFromCursor(ME_TextEditor
*editor
, int nCursor
)
407 ME_DisplayItem
*p
, *run
;
408 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
410 p
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, pStyle
,
413 while ((run
= ME_FindItemBack(run
, diRunOrParagraph
))->type
== diRun
)
415 if (run
->member
.run
.nFlags
& MERF_CELL
)
417 assert(run
->member
.run
.pCell
->next
);
418 p
->member
.run
.pCell
= run
->member
.run
.pCell
->next
;
422 assert(run
->type
== diParagraph
);
423 assert(run
->member
.para
.bTable
);
424 assert(run
->member
.para
.pCells
);
425 p
->member
.run
.pCell
= run
->member
.para
.pCells
;
429 void ME_InsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
430 const WCHAR
*str
, int len
, ME_Style
*style
)
434 /* FIXME: is this too slow? */
435 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
436 int freeSpace
= editor
->nTextLimit
- ME_GetTextLength(editor
);
438 /* text operations set modified state */
439 editor
->nModifyStep
= 1;
443 /* FIXME really HERE ? */
444 if (ME_IsSelection(editor
))
445 ME_DeleteSelection(editor
);
447 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
450 len
= min(len
, freeSpace
);
454 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
455 while(pos
-str
< len
&& *pos
!= '\r' && *pos
!= '\n' && *pos
!= '\t')
457 if (pos
-str
< len
&& *pos
== '\t') { /* handle tabs */
461 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
463 ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, style
, MERF_TAB
);
472 if (pos
-str
< len
) { /* handle EOLs */
473 ME_DisplayItem
*tp
, *end_run
;
476 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
477 p
= &editor
->pCursors
[nCursor
];
479 ME_SplitRunSimple(editor
, p
->pRun
, p
->nOffset
);
480 p
= &editor
->pCursors
[nCursor
];
482 tmp_style
= ME_GetInsertStyle(editor
, nCursor
);
483 /* ME_SplitParagraph increases style refcount */
484 tp
= ME_SplitParagraph(editor
, p
->pRun
, p
->pRun
->member
.run
.style
);
485 p
->pRun
= ME_FindItemFwd(tp
, diRun
);
486 end_run
= ME_FindItemBack(tp
, diRun
);
487 ME_ReleaseStyle(end_run
->member
.run
.style
);
488 end_run
->member
.run
.style
= tmp_style
;
490 if(pos
-str
< len
&& *pos
=='\r')
492 if(pos
-str
< len
&& *pos
=='\n')
500 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, len
, style
, 0);
507 ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
509 ME_DisplayItem
*pRun
= pCursor
->pRun
;
513 if (!pCursor
->nOffset
)
516 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
523 if (pRun
->member
.para
.prev_para
->type
== diTextStart
)
525 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
526 /* every paragraph ought to have at least one run */
527 assert(pRun
&& pRun
->type
== diRun
);
528 assert(pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
531 assert(pRun
->type
!= diRun
&& pRun
->type
!= diParagraph
);
534 } while (RUN_IS_HIDDEN(&pRun
->member
.run
));
535 pCursor
->pRun
= pRun
;
536 if (pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
537 pCursor
->nOffset
= 0;
539 pCursor
->nOffset
= pRun
->member
.run
.strText
->nLen
;
542 if (pCursor
->nOffset
)
543 pCursor
->nOffset
= ME_StrRelPos2(pCursor
->pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
548 if (!(pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
550 int new_ofs
= ME_StrRelPos2(pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
552 if (new_ofs
< pRun
->member
.run
.strText
->nLen
)
554 pCursor
->nOffset
= new_ofs
;
559 pRun
= ME_FindItemFwd(pRun
, diRun
);
560 } while (pRun
&& RUN_IS_HIDDEN(&pRun
->member
.run
));
563 pCursor
->pRun
= pRun
;
564 pCursor
->nOffset
= 0;
573 ME_MoveCursorWords(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
575 ME_DisplayItem
*pRun
= cursor
->pRun
, *pOtherRun
;
576 int nOffset
= cursor
->nOffset
;
580 /* Backward movement */
583 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
584 nOffset
, WB_MOVEWORDLEFT
);
587 pOtherRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
588 if (pOtherRun
->type
== diRun
)
590 if (ME_CallWordBreakProc(editor
, pOtherRun
->member
.run
.strText
,
591 pOtherRun
->member
.run
.strText
->nLen
- 1,
593 && !(pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
594 && !(cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
595 && !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
, 0,
599 nOffset
= pOtherRun
->member
.run
.strText
->nLen
;
601 else if (pOtherRun
->type
== diParagraph
)
603 if (cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
605 /* Paragraph breaks are treated as separate words */
606 if (pOtherRun
->member
.para
.prev_para
->type
== diTextStart
)
608 pRun
= ME_FindItemBack(pOtherRun
, diRunOrParagraph
);
616 /* Forward movement */
617 BOOL last_delim
= FALSE
;
621 if (last_delim
&& !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
622 nOffset
, WB_ISDELIMITER
))
624 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
625 nOffset
, WB_MOVEWORDRIGHT
);
626 if (nOffset
< pRun
->member
.run
.strText
->nLen
)
628 pOtherRun
= ME_FindItemFwd(pRun
, diRunOrParagraphOrEnd
);
629 if (pOtherRun
->type
== diRun
)
631 last_delim
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
632 nOffset
- 1, WB_ISDELIMITER
);
636 else if (pOtherRun
->type
== diParagraph
)
638 if (cursor
->pRun
== pRun
)
639 pRun
= ME_FindItemFwd(pOtherRun
, diRun
);
645 if (cursor
->pRun
== pRun
)
653 cursor
->nOffset
= nOffset
;
659 ME_SelectWord(ME_TextEditor
*editor
)
661 if (!(editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
662 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], -1);
663 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], +1);
664 ME_InvalidateSelection(editor
);
665 ME_SendSelChange(editor
);
669 int ME_GetCursorOfs(ME_TextEditor
*editor
, int nCursor
)
671 ME_Cursor
*pCursor
= &editor
->pCursors
[nCursor
];
673 return ME_GetParagraph(pCursor
->pRun
)->member
.para
.nCharOfs
674 + pCursor
->pRun
->member
.run
.nCharOfs
+ pCursor
->nOffset
;
677 int ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
, ME_Cursor
*result
, BOOL
*is_eol
)
679 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
685 while(p
!= editor
->pBuffer
->pLast
)
687 if (p
->type
== diParagraph
)
689 int ry
= y
- p
->member
.para
.nYPos
;
692 result
->pRun
= ME_FindItemFwd(p
, diRun
);
696 if (ry
>= p
->member
.para
.nHeight
)
698 p
= p
->member
.para
.next_para
;
701 p
= ME_FindItemFwd(p
, diStartRow
);
705 if (p
->type
== diStartRow
)
707 int ry
= y
- p
->member
.row
.nYPos
;
710 if (ry
>= p
->member
.row
.nHeight
)
712 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
713 if (p
->type
!= diStartRow
)
717 p
= ME_FindItemFwd(p
, diRun
);
720 if (p
->type
== diRun
)
723 rx
= x
- p
->member
.run
.pt
.x
;
726 if (rx
>= p
->member
.run
.nWidth
) /* not this run yet... find next item */
731 if (p
->type
== diRun
)
733 rx
= x
- p
->member
.run
.pt
.x
;
734 goto continue_search
;
736 if (p
->type
== diStartRow
)
738 p
= ME_FindItemFwd(p
, diRun
);
741 rx
= 0; /* FIXME not sure */
744 if (p
->type
== diParagraph
|| p
->type
== diTextEnd
)
746 rx
= 0; /* FIXME not sure */
754 if (p
->member
.run
.nFlags
& MERF_ENDPARA
)
757 result
->nOffset
= ME_CharFromPointCursor(editor
, rx
, &p
->member
.run
);
758 if (editor
->pCursors
[0].nOffset
== p
->member
.run
.strText
->nLen
&& rx
)
760 result
->pRun
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, diRun
);
769 result
->pRun
= ME_FindItemBack(p
, diRun
);
771 assert(result
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
777 ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
)
782 GetClientRect(editor
->hWnd
, &rc
);
783 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
)
785 y
+= ME_GetYScrollPos(editor
);
786 ME_FindPixelPos(editor
, x
, y
, &cursor
, NULL
);
787 return (ME_GetParagraph(cursor
.pRun
)->member
.para
.nCharOfs
788 + cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
);
792 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
)
794 ME_Cursor tmp_cursor
;
795 int is_selection
= 0;
797 editor
->nUDArrowX
= -1;
799 y
+= ME_GetYScrollPos(editor
);
801 tmp_cursor
= editor
->pCursors
[0];
802 is_selection
= ME_IsSelection(editor
);
804 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
806 if (GetKeyState(VK_SHIFT
)>=0)
808 editor
->pCursors
[1] = editor
->pCursors
[0];
813 editor
->pCursors
[1] = tmp_cursor
;
817 ME_InvalidateSelection(editor
);
818 HideCaret(editor
->hWnd
);
819 ME_MoveCaret(editor
);
820 ShowCaret(editor
->hWnd
);
821 ME_ClearTempStyle(editor
);
822 ME_SendSelChange(editor
);
825 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
827 ME_Cursor tmp_cursor
;
829 y
+= ME_GetYScrollPos(editor
);
831 tmp_cursor
= editor
->pCursors
[0];
832 /* FIXME: do something with the return value of ME_FindPixelPos */
833 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
835 if (tmp_cursor
.pRun
== editor
->pCursors
[0].pRun
&&
836 tmp_cursor
.nOffset
== editor
->pCursors
[0].nOffset
)
839 ME_InvalidateSelection(editor
);
840 editor
->pCursors
[0] = tmp_cursor
;
841 HideCaret(editor
->hWnd
);
842 ME_MoveCaret(editor
);
843 ME_InvalidateSelection(editor
);
844 ShowCaret(editor
->hWnd
);
845 ME_SendSelChange(editor
);
848 static ME_DisplayItem
*ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
849 int x
, int *pOffset
, int *pbCaretAtEnd
)
851 ME_DisplayItem
*pNext
, *pLastRun
;
852 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
853 assert(pNext
->type
== diRun
);
855 *pbCaretAtEnd
= FALSE
;
857 int run_x
= pNext
->member
.run
.pt
.x
;
858 int width
= pNext
->member
.run
.nWidth
;
861 if (pOffset
) *pOffset
= 0;
864 if (x
>= run_x
&& x
< run_x
+width
)
866 int ch
= ME_CharFromPointCursor(editor
, x
-run_x
, &pNext
->member
.run
);
867 ME_String
*s
= pNext
->member
.run
.strText
;
875 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
876 } while(pNext
&& pNext
->type
== diRun
);
878 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
880 pNext
= ME_FindItemFwd(pNext
, diRun
);
881 if (pbCaretAtEnd
) *pbCaretAtEnd
= 1;
882 if (pOffset
) *pOffset
= 0;
885 if (pbCaretAtEnd
) *pbCaretAtEnd
= 0;
886 if (pOffset
) *pOffset
= 0;
891 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
893 ME_DisplayItem
*pRun
= pCursor
->pRun
;
896 if (editor
->nUDArrowX
!= -1)
897 x
= editor
->nUDArrowX
;
899 if (editor
->bCaretAtEnd
)
901 pRun
= ME_FindItemBack(pRun
, diRun
);
903 x
= pRun
->member
.run
.pt
.x
+ pRun
->member
.run
.nWidth
;
906 x
= pRun
->member
.run
.pt
.x
;
907 x
+= ME_PointFromChar(editor
, &pRun
->member
.run
, pCursor
->nOffset
);
909 editor
->nUDArrowX
= x
;
916 ME_MoveCursorLines(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
918 ME_DisplayItem
*pRun
= pCursor
->pRun
;
919 ME_DisplayItem
*pItem
;
920 int x
= ME_GetXForArrow(editor
, pCursor
);
922 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
923 pRun
= ME_FindItemBack(pRun
, diRun
);
928 /* start of this row */
929 pItem
= ME_FindItemBack(pRun
, diStartRow
);
931 /* start of the previous row */
932 pItem
= ME_FindItemBack(pItem
, diStartRow
);
936 /* start of the next row */
937 pItem
= ME_FindItemFwd(pRun
, diStartRow
);
938 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
943 /* row not found - ignore */
946 pCursor
->pRun
= ME_FindRunInRow(editor
, pItem
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
947 assert(pCursor
->pRun
);
948 assert(pCursor
->pRun
->type
== diRun
);
952 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
954 ME_DisplayItem
*pRun
= pCursor
->pRun
;
955 ME_DisplayItem
*pLast
, *p
;
956 int x
, y
, ys
, yd
, yp
, yprev
;
957 ME_Cursor tmp_curs
= *pCursor
;
959 x
= ME_GetXForArrow(editor
, pCursor
);
960 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
961 pRun
= ME_FindItemBack(pRun
, diRun
);
963 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
964 assert(p
->type
== diStartRow
);
965 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
966 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
967 yd
= y
- editor
->sizeWindow
.cy
;
971 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
974 if (p
->type
== diParagraph
) { /* crossing paragraphs */
975 if (p
->member
.para
.prev_para
== NULL
)
977 yp
= p
->member
.para
.prev_para
->member
.para
.nYPos
;
980 y
= yp
+ p
->member
.row
.nYPos
;
987 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
988 ME_UpdateSelection(editor
, &tmp_curs
);
989 if (yprev
< editor
->sizeWindow
.cy
)
991 ME_EnsureVisible(editor
, ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
));
996 ME_ScrollUp(editor
, ys
-yprev
);
998 assert(pCursor
->pRun
);
999 assert(pCursor
->pRun
->type
== diRun
);
1002 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1003 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1004 In such a situation, clicking the scrollbar restores its position back to the
1005 normal range (ie. sets it to (doclength-screenheight)). */
1007 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1009 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1010 ME_DisplayItem
*pLast
, *p
;
1011 int x
, y
, ys
, yd
, yp
, yprev
;
1012 ME_Cursor tmp_curs
= *pCursor
;
1014 x
= ME_GetXForArrow(editor
, pCursor
);
1015 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1016 pRun
= ME_FindItemBack(pRun
, diRun
);
1018 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1019 assert(p
->type
== diStartRow
);
1020 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
1021 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
1022 yd
= y
+ editor
->sizeWindow
.cy
;
1026 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
1029 if (p
->type
== diParagraph
) {
1030 yp
= p
->member
.para
.nYPos
;
1033 y
= yp
+ p
->member
.row
.nYPos
;
1040 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1041 ME_UpdateSelection(editor
, &tmp_curs
);
1042 if (yprev
>= editor
->nTotalLength
-editor
->sizeWindow
.cy
)
1044 ME_EnsureVisible(editor
, ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
));
1049 ME_ScrollUp(editor
,ys
-yprev
);
1051 assert(pCursor
->pRun
);
1052 assert(pCursor
->pRun
->type
== diRun
);
1055 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1057 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
1058 /* bCaretAtEnd doesn't make sense if the cursor isn't set at the
1059 first character of the next row */
1060 assert(!editor
->bCaretAtEnd
|| !pCursor
->nOffset
);
1061 ME_WrapMarkedParagraphs(editor
);
1063 ME_DisplayItem
*pRun
;
1064 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
1065 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1069 pRun
= ME_FindItemFwd(pRow
, diRun
);
1071 pCursor
->pRun
= pRun
;
1072 pCursor
->nOffset
= 0;
1075 editor
->bCaretAtEnd
= FALSE
;
1078 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1080 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diTextStart
);
1082 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1084 pCursor
->pRun
= pRun
;
1085 pCursor
->nOffset
= 0;
1090 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1092 ME_DisplayItem
*pRow
;
1094 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1097 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1099 if (pRow
->type
== diStartRow
) {
1100 /* FIXME WTF was I thinking about here ? */
1101 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1103 pCursor
->pRun
= pRun
;
1104 pCursor
->nOffset
= 0;
1105 editor
->bCaretAtEnd
= 1;
1108 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1109 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1110 pCursor
->nOffset
= 0;
1111 editor
->bCaretAtEnd
= FALSE
;
1114 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1116 ME_DisplayItem
*p
= ME_FindItemFwd(pCursor
->pRun
, diTextEnd
);
1118 p
= ME_FindItemBack(p
, diRun
);
1120 assert(p
->member
.run
.nFlags
& MERF_ENDPARA
);
1122 pCursor
->nOffset
= 0;
1123 editor
->bCaretAtEnd
= FALSE
;
1126 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1128 return memcmp(&editor
->pCursors
[0], &editor
->pCursors
[1], sizeof(ME_Cursor
))!=0;
1131 static int ME_GetSelCursor(ME_TextEditor
*editor
, int dir
)
1133 int cdir
= ME_GetCursorOfs(editor
, 0) - ME_GetCursorOfs(editor
, 1);
1141 BOOL
ME_UpdateSelection(ME_TextEditor
*editor
, ME_Cursor
*pTempCursor
)
1143 ME_Cursor old_anchor
= editor
->pCursors
[1];
1145 if (GetKeyState(VK_SHIFT
)>=0) /* cancelling selection */
1147 /* any selection was present ? if so, it's no more, repaint ! */
1148 editor
->pCursors
[1] = editor
->pCursors
[0];
1149 if (memcmp(pTempCursor
, &old_anchor
, sizeof(ME_Cursor
))) {
1156 if (!memcmp(pTempCursor
, &editor
->pCursors
[1], sizeof(ME_Cursor
))) /* starting selection */
1158 editor
->pCursors
[1] = *pTempCursor
;
1167 void ME_DeleteSelection(ME_TextEditor
*editor
)
1170 ME_GetSelection(editor
, &from
, &to
);
1171 ME_DeleteTextAtCursor(editor
, ME_GetSelCursor(editor
,-1), to
-from
);
1174 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1180 ME_GetSelection(editor
, &from
, &to
);
1181 ME_CursorFromCharOfs(editor
, from
, &c
);
1183 style
= c
.pRun
->member
.run
.style
;
1184 ME_AddRefStyle(style
); /* ME_GetInsertStyle has already done that */
1187 style
= ME_GetInsertStyle(editor
, 0);
1191 void ME_SendSelChange(ME_TextEditor
*editor
)
1195 ME_ClearTempStyle(editor
);
1197 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1200 sc
.nmhdr
.hwndFrom
= editor
->hWnd
;
1201 sc
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
1202 sc
.nmhdr
.code
= EN_SELCHANGE
;
1203 SendMessageW(editor
->hWnd
, EM_EXGETSEL
, 0, (LPARAM
)&sc
.chrg
);
1204 sc
.seltyp
= SEL_EMPTY
;
1205 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1206 sc
.seltyp
|= SEL_TEXT
;
1207 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* wth were RICHEDIT authors thinking ? */
1208 sc
.seltyp
|= SEL_MULTICHAR
;
1209 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
, sc
.nmhdr
.idFrom
, (LPARAM
)&sc
);
1214 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1217 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1218 ME_Cursor tmp_curs
= *p
;
1219 BOOL success
= FALSE
;
1221 ME_CheckCharOffsets(editor
);
1222 editor
->nUDArrowX
= -1;
1225 editor
->bCaretAtEnd
= 0;
1227 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1229 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1232 editor
->bCaretAtEnd
= 0;
1234 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1236 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1239 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1242 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1245 ME_ArrowPageUp(editor
, &tmp_curs
);
1248 ME_ArrowPageDown(editor
, &tmp_curs
);
1252 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1254 ME_ArrowHome(editor
, &tmp_curs
);
1255 editor
->bCaretAtEnd
= 0;
1260 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1262 ME_ArrowEnd(editor
, &tmp_curs
);
1267 editor
->pCursors
[1] = tmp_curs
;
1270 ME_InvalidateSelection(editor
);
1272 HideCaret(editor
->hWnd
);
1273 ME_EnsureVisible(editor
, tmp_curs
.pRun
);
1274 ME_ShowCaret(editor
);
1275 ME_SendSelChange(editor
);