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
.pt
.y
+ 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
&& !ME_IsSelection(editor
))
241 GetClientRect(editor
->hWnd
, &rect
);
242 x
= min(x
, rect
.right
-2);
243 CreateCaret(editor
->hWnd
, NULL
, 0, height
);
249 void ME_ShowCaret(ME_TextEditor
*ed
)
252 if(ed
->bHaveFocus
&& !ME_IsSelection(ed
))
256 void ME_HideCaret(ME_TextEditor
*ed
)
258 if(!ed
->bHaveFocus
|| ME_IsSelection(ed
))
265 BOOL
ME_InternalDeleteText(ME_TextEditor
*editor
, int nOfs
, int nChars
,
270 int totalChars
= nChars
;
271 ME_DisplayItem
*start_para
;
274 /* Prevent deletion past last end of paragraph run. */
275 ME_DisplayItem
*pTextEnd
= editor
->pBuffer
->pLast
;
276 int nMaxChars
= pTextEnd
->member
.para
.prev_para
->member
.para
.nCharOfs
;
277 nMaxChars
+= ME_FindItemBack(pTextEnd
, diRun
)->member
.run
.nCharOfs
;
279 nChars
= min(nChars
, nMaxChars
);
282 ME_CursorFromCharOfs(editor
, nOfs
, &c
);
283 start_para
= ME_GetParagraph(c
.pRun
);
287 ME_ProtectPartialTableDeletion(editor
, nOfs
, &nChars
);
295 ME_CursorFromCharOfs(editor
, nOfs
+nChars
, &c
);
297 nOfs
+nChars
== (c
.pRun
->member
.run
.nCharOfs
298 + ME_GetParagraph(c
.pRun
)->member
.para
.nCharOfs
))
300 /* We aren't deleting anything in this run, so we will go back to the
301 * last run we are deleting text in. */
302 c
.pRun
= ME_FindItemBack(c
.pRun
, diRun
);
303 if (c
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
304 c
.nOffset
= c
.pRun
->member
.run
.nCR
+ c
.pRun
->member
.run
.nLF
;
306 c
.nOffset
= c
.pRun
->member
.run
.strText
->nLen
;
308 run
= &c
.pRun
->member
.run
;
309 if (run
->nFlags
& MERF_ENDPARA
) {
310 int eollen
= run
->nCR
+ run
->nLF
;
311 BOOL keepFirstParaFormat
;
313 if (!ME_FindItemFwd(c
.pRun
, diParagraph
))
317 keepFirstParaFormat
= (totalChars
== nChars
&& nChars
<= eollen
&&
319 if (!editor
->bEmulateVersion10
) /* v4.1 */
321 ME_DisplayItem
*next_para
= ME_FindItemFwd(c
.pRun
, diParagraphOrEnd
);
322 ME_DisplayItem
*this_para
= next_para
->member
.para
.prev_para
;
324 /* The end of paragraph before a table row is only deleted if there
325 * is nothing else on the line before it. */
326 if (this_para
== start_para
&&
327 next_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
329 /* If the paragraph will be empty, then it should be deleted, however
330 * it still might have text right now which would inherit the
331 * MEPF_STARTROW property if we joined it right now.
332 * Instead we will delete it after the preceding text is deleted. */
333 if (nOfs
> this_para
->member
.para
.nCharOfs
) {
334 /* Skip this end of line. */
335 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
338 keepFirstParaFormat
= TRUE
;
341 ME_JoinParagraphs(editor
, ME_GetParagraph(c
.pRun
), keepFirstParaFormat
);
342 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
343 ME_CheckCharOffsets(editor
);
344 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
350 int nCharsToDelete
= min(nChars
, c
.nOffset
);
353 c
.nOffset
-= nCharsToDelete
;
355 ME_FindItemBack(c
.pRun
, diParagraph
)->member
.para
.nFlags
|= MEPF_REWRAP
;
358 /* nChars is the number of characters that should be deleted from the
359 PRECEDING runs (these BEFORE cursor.pRun)
360 nCharsToDelete is a number of chars to delete from THIS run */
361 nChars
-= nCharsToDelete
;
362 shift
-= nCharsToDelete
;
363 TRACE("Deleting %d (remaning %d) chars at %d in '%s' (%d)\n",
364 nCharsToDelete
, nChars
, c
.nOffset
,
365 debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
367 if (!c
.nOffset
&& ME_StrVLen(run
->strText
) == nCharsToDelete
)
369 /* undo = reinsert whole run */
370 /* nOfs is a character offset (from the start of the document
371 to the current (deleted) run */
372 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
374 pUndo
->di
.member
.run
.nCharOfs
= nOfs
+nChars
;
378 /* undo = reinsert partial run */
379 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
381 ME_DestroyString(pUndo
->di
.member
.run
.strText
);
382 pUndo
->di
.member
.run
.nCharOfs
= nOfs
+nChars
;
383 pUndo
->di
.member
.run
.strText
= ME_MakeStringN(run
->strText
->szData
+c
.nOffset
, nCharsToDelete
);
386 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
387 TRACE("Shift value: %d\n", shift
);
388 ME_StrDeleteV(run
->strText
, c
.nOffset
, nCharsToDelete
);
390 /* update cursors (including c) */
391 for (i
=-1; i
<editor
->nCursors
; i
++) {
392 ME_Cursor
*pThisCur
= editor
->pCursors
+ i
;
393 if (i
== -1) pThisCur
= &c
;
394 if (pThisCur
->pRun
== cursor
.pRun
) {
395 if (pThisCur
->nOffset
> cursor
.nOffset
) {
396 if (pThisCur
->nOffset
-cursor
.nOffset
< nCharsToDelete
)
397 pThisCur
->nOffset
= cursor
.nOffset
;
399 pThisCur
->nOffset
-= nCharsToDelete
;
400 assert(pThisCur
->nOffset
>= 0);
401 assert(pThisCur
->nOffset
<= ME_StrVLen(run
->strText
));
403 if (pThisCur
->nOffset
== ME_StrVLen(run
->strText
))
405 pThisCur
->pRun
= ME_FindItemFwd(pThisCur
->pRun
, diRunOrParagraphOrEnd
);
406 assert(pThisCur
->pRun
->type
== diRun
);
407 pThisCur
->nOffset
= 0;
412 /* c = updated data now */
414 if (c
.pRun
== cursor
.pRun
)
415 ME_SkipAndPropagateCharOffset(c
.pRun
, shift
);
417 ME_PropagateCharOffset(c
.pRun
, shift
);
419 if (!ME_StrVLen(cursor
.pRun
->member
.run
.strText
))
421 TRACE("Removing useless run\n");
422 ME_Remove(cursor
.pRun
);
423 ME_DestroyDisplayItem(cursor
.pRun
);
428 ME_CheckCharOffsets(editor);
436 BOOL
ME_DeleteTextAtCursor(ME_TextEditor
*editor
, int nCursor
, int nChars
)
438 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
439 /* text operations set modified state */
440 editor
->nModifyStep
= 1;
441 return ME_InternalDeleteText(editor
, ME_GetCursorOfs(editor
, nCursor
), nChars
,
445 static ME_DisplayItem
*
446 ME_InternalInsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
447 const WCHAR
*str
, int len
, ME_Style
*style
,
450 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
452 editor
->bCaretAtEnd
= FALSE
;
454 assert(p
->pRun
->type
== diRun
);
456 return ME_InsertRunAtCursor(editor
, p
, style
, str
, len
, flags
);
460 void ME_InsertOLEFromCursor(ME_TextEditor
*editor
, const REOBJECT
* reo
, int nCursor
)
462 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
467 if (ME_IsSelection(editor
))
468 ME_DeleteSelection(editor
);
470 di
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
472 di
->member
.run
.ole_obj
= ALLOC_OBJ(*reo
);
473 ME_CopyReObject(di
->member
.run
.ole_obj
, reo
);
474 ME_SendSelChange(editor
);
478 void ME_InsertEndRowFromCursor(ME_TextEditor
*editor
, int nCursor
)
480 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
485 if (ME_IsSelection(editor
))
486 ME_DeleteSelection(editor
);
488 di
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
490 ME_SendSelChange(editor
);
494 void ME_InsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
495 const WCHAR
*str
, int len
, ME_Style
*style
)
501 /* FIXME really HERE ? */
502 if (ME_IsSelection(editor
))
503 ME_DeleteSelection(editor
);
505 /* FIXME: is this too slow? */
506 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
507 oldLen
= ME_GetTextLength(editor
);
509 /* text operations set modified state */
510 editor
->nModifyStep
= 1;
514 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
518 /* grow the text limit to fit our text */
519 if(editor
->nTextLimit
< oldLen
+len
)
520 editor
->nTextLimit
= oldLen
+ len
;
525 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
526 while(pos
-str
< len
&& *pos
!= '\r' && *pos
!= '\n' && *pos
!= '\t')
528 if (pos
-str
< len
&& *pos
== '\t') { /* handle tabs */
532 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
534 ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, style
, MERF_TAB
);
543 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
544 if (!editor
->bEmulateVersion10
&& pos
-str
< len
-2 && pos
[0] == '\r' && pos
[1] == '\r' && pos
[2] == '\n') {
548 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
550 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, style
, 0);
559 if (pos
-str
< len
) { /* handle EOLs */
560 ME_DisplayItem
*tp
, *end_run
;
565 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
566 p
= &editor
->pCursors
[nCursor
];
568 ME_SplitRunSimple(editor
, p
->pRun
, p
->nOffset
);
569 p
= &editor
->pCursors
[nCursor
];
571 tmp_style
= ME_GetInsertStyle(editor
, nCursor
);
572 /* ME_SplitParagraph increases style refcount */
574 /* Encode and fill number of CR and LF according to emulation mode */
575 if (editor
->bEmulateVersion10
) {
578 /* We have to find out how many consecutive \r are there, and if there
579 is a \n terminating the run of \r's. */
580 numCR
= 0; numLF
= 0;
582 while (tpos
-str
< len
&& *tpos
== '\r') {
586 if (tpos
-str
>= len
) {
587 /* Reached end of text without finding anything but '\r' */
591 numCR
= 1; numLF
= 0;
592 } else if (*tpos
== '\n') {
593 /* The entire run of \r's plus the one \n is one single line break */
597 /* Found some other content past the run of \r's */
599 numCR
= 1; numLF
= 0;
602 if(pos
-str
< len
&& *pos
=='\r')
604 if(pos
-str
< len
&& *pos
=='\n')
606 numCR
= 1; numLF
= 0;
608 tp
= ME_SplitParagraph(editor
, p
->pRun
, p
->pRun
->member
.run
.style
, numCR
, numLF
, 0);
609 p
->pRun
= ME_FindItemFwd(tp
, diRun
);
610 end_run
= ME_FindItemBack(tp
, diRun
);
611 ME_ReleaseStyle(end_run
->member
.run
.style
);
612 end_run
->member
.run
.style
= tmp_style
;
621 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, len
, style
, 0);
628 ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
630 ME_DisplayItem
*pRun
= pCursor
->pRun
;
634 if (!pCursor
->nOffset
)
637 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
644 if (pRun
->member
.para
.prev_para
->type
== diTextStart
)
646 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
647 /* every paragraph ought to have at least one run */
648 assert(pRun
&& pRun
->type
== diRun
);
649 assert(pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
652 assert(pRun
->type
!= diRun
&& pRun
->type
!= diParagraph
);
655 } while (RUN_IS_HIDDEN(&pRun
->member
.run
) ||
656 pRun
->member
.run
.nFlags
& MERF_HIDDEN
);
657 pCursor
->pRun
= pRun
;
658 if (pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
659 pCursor
->nOffset
= 0;
661 pCursor
->nOffset
= pRun
->member
.run
.strText
->nLen
;
664 if (pCursor
->nOffset
)
665 pCursor
->nOffset
= ME_StrRelPos2(pCursor
->pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
670 if (!(pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
672 int new_ofs
= ME_StrRelPos2(pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
674 if (new_ofs
< pRun
->member
.run
.strText
->nLen
)
676 pCursor
->nOffset
= new_ofs
;
681 pRun
= ME_FindItemFwd(pRun
, diRun
);
682 } while (pRun
&& (RUN_IS_HIDDEN(&pRun
->member
.run
) ||
683 pRun
->member
.run
.nFlags
& MERF_HIDDEN
));
686 pCursor
->pRun
= pRun
;
687 pCursor
->nOffset
= 0;
696 ME_MoveCursorWords(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
698 ME_DisplayItem
*pRun
= cursor
->pRun
, *pOtherRun
;
699 int nOffset
= cursor
->nOffset
;
703 /* Backward movement */
706 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
707 nOffset
, WB_MOVEWORDLEFT
);
710 pOtherRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
711 if (pOtherRun
->type
== diRun
)
713 if (ME_CallWordBreakProc(editor
, pOtherRun
->member
.run
.strText
,
714 pOtherRun
->member
.run
.strText
->nLen
- 1,
716 && !(pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
717 && !(cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
718 && !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
, 0,
722 nOffset
= pOtherRun
->member
.run
.strText
->nLen
;
724 else if (pOtherRun
->type
== diParagraph
)
726 if (cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
728 /* Skip empty start of table row paragraph */
729 if (pOtherRun
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
730 pOtherRun
= pOtherRun
->member
.para
.prev_para
;
731 /* Paragraph breaks are treated as separate words */
732 if (pOtherRun
->member
.para
.prev_para
->type
== diTextStart
)
735 pRun
= ME_FindItemBack(pOtherRun
, diRunOrParagraph
);
743 /* Forward movement */
744 BOOL last_delim
= FALSE
;
748 if (last_delim
&& !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
749 nOffset
, WB_ISDELIMITER
))
751 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
752 nOffset
, WB_MOVEWORDRIGHT
);
753 if (nOffset
< pRun
->member
.run
.strText
->nLen
)
755 pOtherRun
= ME_FindItemFwd(pRun
, diRunOrParagraphOrEnd
);
756 if (pOtherRun
->type
== diRun
)
758 last_delim
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
759 nOffset
- 1, WB_ISDELIMITER
);
763 else if (pOtherRun
->type
== diParagraph
)
765 if (pOtherRun
->member
.para
.nFlags
& MEPF_ROWSTART
)
766 pOtherRun
= pOtherRun
->member
.para
.next_para
;
767 if (cursor
->pRun
== pRun
)
768 pRun
= ME_FindItemFwd(pOtherRun
, diRun
);
774 if (cursor
->pRun
== pRun
)
782 cursor
->nOffset
= nOffset
;
788 ME_SelectByType(ME_TextEditor
*editor
, ME_SelectionType selectionType
)
790 /* pCursor[0] is the end of the selection
791 * pCursor[1] is the start of the selection (or the position selection anchor)
792 * pCursor[2] and [3] are the selection anchors that are backed up
793 * so they are kept when the selection changes for drag selection.
796 editor
->nSelectionType
= selectionType
;
797 switch(selectionType
)
802 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
803 editor
->pCursors
[1] = editor
->pCursors
[0];
804 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
809 ME_DisplayItem
*pItem
;
810 ME_DIType fwdSearchType
, backSearchType
;
811 if (selectionType
== stParagraph
) {
812 backSearchType
= diParagraph
;
813 fwdSearchType
= diParagraphOrEnd
;
815 backSearchType
= diStartRow
;
816 fwdSearchType
= diStartRowOrParagraphOrEnd
;
818 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, fwdSearchType
);
820 if (pItem
->type
== diTextEnd
)
821 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
823 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
824 editor
->pCursors
[0].nOffset
= 0;
826 pItem
= ME_FindItemBack(pItem
, backSearchType
);
827 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
828 editor
->pCursors
[1].nOffset
= 0;
832 /* Select everything with cursor anchored from the start of the text */
833 editor
->nSelectionType
= stDocument
;
834 editor
->pCursors
[1].pRun
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
);
835 editor
->pCursors
[1].nOffset
= 0;
836 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
837 editor
->pCursors
[0].nOffset
= 0;
841 /* Store the anchor positions for extending the selection. */
842 editor
->pCursors
[2] = editor
->pCursors
[0];
843 editor
->pCursors
[3] = editor
->pCursors
[1];
846 int ME_GetCursorOfs(ME_TextEditor
*editor
, int nCursor
)
848 ME_Cursor
*pCursor
= &editor
->pCursors
[nCursor
];
849 return ME_GetParagraph(pCursor
->pRun
)->member
.para
.nCharOfs
850 + pCursor
->pRun
->member
.run
.nCharOfs
+ pCursor
->nOffset
;
853 /* Helper function for ME_FindPixelPos to find paragraph within tables */
854 static ME_DisplayItem
* ME_FindPixelPosInTableRow(int x
, int y
,
855 ME_DisplayItem
*para
)
857 ME_DisplayItem
*cell
, *next_cell
;
858 assert(para
->member
.para
.nFlags
& MEPF_ROWSTART
);
859 cell
= para
->member
.para
.next_para
->member
.para
.pCell
;
862 /* find the cell we are in */
863 while ((next_cell
= cell
->member
.cell
.next_cell
) != NULL
) {
864 if (x
< next_cell
->member
.cell
.pt
.x
)
866 para
= ME_FindItemFwd(cell
, diParagraph
);
867 /* Found the cell, but there might be multiple paragraphs in
868 * the cell, so need to search down the cell for the paragraph. */
869 while (cell
== para
->member
.para
.pCell
) {
870 if (y
< para
->member
.para
.pt
.y
+ para
->member
.para
.nHeight
)
872 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
873 return ME_FindPixelPosInTableRow(x
, y
, para
);
877 para
= para
->member
.para
.next_para
;
879 /* Past the end of the cell, so go back to the last cell paragraph */
880 return para
->member
.para
.prev_para
;
884 /* Return table row delimiter */
885 para
= ME_FindItemFwd(cell
, diParagraph
);
886 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
887 assert(para
->member
.para
.pFmt
->dwMask
& PFM_TABLEROWDELIMITER
);
888 assert(para
->member
.para
.pFmt
->wEffects
& PFE_TABLEROWDELIMITER
);
892 static BOOL
ME_ReturnFoundPos(ME_TextEditor
*editor
, ME_DisplayItem
*found
,
893 ME_Cursor
*result
, int rx
, BOOL isExact
)
896 assert(found
->type
== diRun
);
897 if ((found
->member
.run
.nFlags
& MERF_ENDPARA
) || rx
< 0)
899 result
->pRun
= found
;
900 result
->nOffset
= ME_CharFromPointCursor(editor
, rx
, &found
->member
.run
);
901 if (editor
->pCursors
[0].nOffset
== found
->member
.run
.strText
->nLen
&& rx
)
903 result
->pRun
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, diRun
);
909 /* Finds the run and offset from the pixel position.
911 * x & y are pixel positions in virtual coordinates into the rich edit control,
912 * so client coordinates must first be adjusted by the scroll position.
914 * returns TRUE if the result was exactly under the cursor, otherwise returns
915 * FALSE, and result is set to the closest position to the coordinates.
917 static BOOL
ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
,
918 ME_Cursor
*result
, BOOL
*is_eol
)
920 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
921 ME_DisplayItem
*last
= NULL
;
929 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->member
.para
.next_para
)
931 assert(p
->type
== diParagraph
);
932 if (y
< p
->member
.para
.pt
.y
+ p
->member
.para
.nHeight
)
934 if (p
->member
.para
.nFlags
& MEPF_ROWSTART
)
935 p
= ME_FindPixelPosInTableRow(x
, y
, p
);
936 y
-= p
->member
.para
.pt
.y
;
937 p
= ME_FindItemFwd(p
, diStartRow
);
939 } else if (p
->member
.para
.nFlags
& MEPF_ROWSTART
) {
940 p
= ME_GetTableRowEnd(p
);
944 for (; p
!= editor
->pBuffer
->pLast
; )
947 assert(p
->type
== diStartRow
);
948 if (y
< p
->member
.row
.pt
.y
+ p
->member
.row
.nHeight
)
950 p
= ME_FindItemFwd(p
, diRun
);
953 pp
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
954 if (pp
->type
!= diStartRow
)
956 p
= ME_FindItemFwd(p
, diRun
);
961 if (p
== editor
->pBuffer
->pLast
)
963 /* The position is below the last paragraph, so the last row will be used
964 * rather than the end of the text, so the x position will be used to
965 * determine the offset closest to the pixel position. */
967 p
= ME_FindItemBack(p
, diStartRow
);
969 p
= ME_FindItemFwd(p
, diRun
);
973 p
= editor
->pBuffer
->pLast
;
976 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->next
)
981 rx
= x
- p
->member
.run
.pt
.x
;
982 if (rx
< p
->member
.run
.nWidth
)
983 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
987 p
= ME_FindItemFwd(p
, diRun
);
988 if (is_eol
) *is_eol
= 1;
989 rx
= 0; /* FIXME not sure */
990 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
995 rx
= 0; /* FIXME not sure */
997 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
1002 result
->pRun
= ME_FindItemBack(p
, diRun
);
1003 result
->nOffset
= 0;
1004 assert(result
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1009 /* Returns the character offset closest to the pixel position
1011 * x & y are pixel positions in client coordinates.
1013 * isExact will be set to TRUE if the run is directly under the pixel
1014 * position, FALSE if it not, unless isExact is set to NULL.
1016 int ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
, BOOL
*isExact
)
1022 GetClientRect(editor
->hWnd
, &rc
);
1023 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
) {
1024 if (isExact
) *isExact
= FALSE
;
1027 y
+= ME_GetYScrollPos(editor
);
1028 bResult
= ME_FindPixelPos(editor
, x
, y
, &cursor
, NULL
);
1029 if (isExact
) *isExact
= bResult
;
1030 return (ME_GetParagraph(cursor
.pRun
)->member
.para
.nCharOfs
1031 + cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
);
1036 /* Extends the selection with a word, line, or paragraph selection type.
1038 * The selection is anchored by editor->pCursors[2-3] such that the text
1039 * between the anchors will remain selected, and one end will be extended.
1041 * editor->pCursors[0] should have the position to extend the selection to
1042 * before this function is called.
1044 * Nothing will be done if editor->nSelectionType equals stPosition.
1046 static void ME_ExtendAnchorSelection(ME_TextEditor
*editor
)
1048 ME_Cursor tmp_cursor
;
1049 int curOfs
, anchorStartOfs
, anchorEndOfs
;
1050 if (editor
->nSelectionType
== stPosition
|| editor
->nSelectionType
== stDocument
)
1052 curOfs
= ME_GetCursorOfs(editor
, 0);
1053 anchorStartOfs
= ME_GetCursorOfs(editor
, 3);
1054 anchorEndOfs
= ME_GetCursorOfs(editor
, 2);
1056 tmp_cursor
= editor
->pCursors
[0];
1057 editor
->pCursors
[0] = editor
->pCursors
[2];
1058 editor
->pCursors
[1] = editor
->pCursors
[3];
1059 if (curOfs
< anchorStartOfs
)
1061 /* Extend the left side of selection */
1062 editor
->pCursors
[1] = tmp_cursor
;
1063 if (editor
->nSelectionType
== stWord
)
1064 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
1067 ME_DisplayItem
*pItem
;
1068 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1069 diStartRowOrParagraph
:diParagraph
);
1070 pItem
= ME_FindItemBack(editor
->pCursors
[1].pRun
, searchType
);
1071 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
1072 editor
->pCursors
[1].nOffset
= 0;
1075 else if (curOfs
>= anchorEndOfs
)
1077 /* Extend the right side of selection */
1078 editor
->pCursors
[0] = tmp_cursor
;
1079 if (editor
->nSelectionType
== stWord
)
1080 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
1083 ME_DisplayItem
*pItem
;
1084 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1085 diStartRowOrParagraphOrEnd
:diParagraphOrEnd
);
1086 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, searchType
);
1087 if (pItem
->type
== diTextEnd
)
1088 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
1090 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
1091 editor
->pCursors
[0].nOffset
= 0;
1096 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
, int clickNum
)
1098 ME_Cursor tmp_cursor
;
1099 int is_selection
= 0;
1102 editor
->nUDArrowX
= -1;
1104 y
+= ME_GetYScrollPos(editor
);
1106 tmp_cursor
= editor
->pCursors
[0];
1107 is_selection
= ME_IsSelection(editor
);
1108 is_shift
= GetKeyState(VK_SHIFT
) < 0;
1110 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
1112 if (x
>= editor
->selofs
|| is_shift
)
1116 editor
->pCursors
[1] = editor
->pCursors
[0];
1118 if (x
>= editor
->selofs
)
1119 ME_SelectByType(editor
, stWord
);
1121 ME_SelectByType(editor
, stParagraph
);
1122 } else if (clickNum
% 2 == 0) {
1123 ME_SelectByType(editor
, stWord
);
1125 ME_SelectByType(editor
, stParagraph
);
1130 editor
->nSelectionType
= stPosition
;
1131 editor
->pCursors
[1] = editor
->pCursors
[0];
1133 else if (!is_selection
)
1135 editor
->nSelectionType
= stPosition
;
1136 editor
->pCursors
[1] = tmp_cursor
;
1138 else if (editor
->nSelectionType
!= stPosition
)
1140 ME_ExtendAnchorSelection(editor
);
1146 ME_SelectByType(editor
, stLine
);
1147 } else if (clickNum
% 2 == 0 || is_shift
) {
1148 ME_SelectByType(editor
, stParagraph
);
1150 ME_SelectByType(editor
, stDocument
);
1153 ME_InvalidateSelection(editor
);
1154 HideCaret(editor
->hWnd
);
1155 ME_ShowCaret(editor
);
1156 ME_ClearTempStyle(editor
);
1157 ME_SendSelChange(editor
);
1160 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
1162 ME_Cursor tmp_cursor
;
1164 if (editor
->nSelectionType
== stDocument
)
1166 y
+= ME_GetYScrollPos(editor
);
1168 tmp_cursor
= editor
->pCursors
[0];
1169 /* FIXME: do something with the return value of ME_FindPixelPos */
1170 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
1172 ME_InvalidateSelection(editor
);
1173 editor
->pCursors
[0] = tmp_cursor
;
1174 ME_ExtendAnchorSelection(editor
);
1176 if (editor
->nSelectionType
!= stPosition
&&
1177 memcmp(&editor
->pCursors
[1], &editor
->pCursors
[3], sizeof(ME_Cursor
)))
1179 /* The scroll the cursor towards the other end, since it was the one
1180 * extended by ME_ExtendAnchorSelection
1182 ME_Cursor tmpCursor
= editor
->pCursors
[0];
1183 editor
->pCursors
[0] = editor
->pCursors
[1];
1184 editor
->pCursors
[1] = tmpCursor
;
1185 SendMessageW(editor
->hWnd
, EM_SCROLLCARET
, 0, 0);
1186 editor
->pCursors
[1] = editor
->pCursors
[0];
1187 editor
->pCursors
[0] = tmpCursor
;
1189 SendMessageW(editor
->hWnd
, EM_SCROLLCARET
, 0, 0);
1192 ME_InvalidateSelection(editor
);
1193 HideCaret(editor
->hWnd
);
1194 ME_ShowCaret(editor
);
1195 ME_SendSelChange(editor
);
1198 static ME_DisplayItem
*ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
1199 int x
, int *pOffset
, int *pbCaretAtEnd
)
1201 ME_DisplayItem
*pNext
, *pLastRun
;
1202 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
1203 assert(pNext
->type
== diRun
);
1205 if (pbCaretAtEnd
) *pbCaretAtEnd
= FALSE
;
1206 if (pOffset
) *pOffset
= 0;
1208 int run_x
= pNext
->member
.run
.pt
.x
;
1209 int width
= pNext
->member
.run
.nWidth
;
1214 if (x
>= run_x
&& x
< run_x
+width
)
1216 int ch
= ME_CharFromPointCursor(editor
, x
-run_x
, &pNext
->member
.run
);
1217 ME_String
*s
= pNext
->member
.run
.strText
;
1225 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
1226 } while(pNext
&& pNext
->type
== diRun
);
1228 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
1230 pNext
= ME_FindItemFwd(pNext
, diRun
);
1231 if (pbCaretAtEnd
) *pbCaretAtEnd
= TRUE
;
1238 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1240 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1243 if (editor
->nUDArrowX
!= -1)
1244 x
= editor
->nUDArrowX
;
1246 if (editor
->bCaretAtEnd
)
1248 pRun
= ME_FindItemBack(pRun
, diRun
);
1250 x
= pRun
->member
.run
.pt
.x
+ pRun
->member
.run
.nWidth
;
1253 x
= pRun
->member
.run
.pt
.x
;
1254 x
+= ME_PointFromChar(editor
, &pRun
->member
.run
, pCursor
->nOffset
);
1256 editor
->nUDArrowX
= x
;
1263 ME_MoveCursorLines(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
1265 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1266 ME_DisplayItem
*pItem
, *pOldPara
, *pNewPara
;
1267 int x
= ME_GetXForArrow(editor
, pCursor
);
1269 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1270 pRun
= ME_FindItemBack(pRun
, diRun
);
1273 pOldPara
= ME_GetParagraph(pRun
);
1276 /* start of this row */
1277 pItem
= ME_FindItemBack(pRun
, diStartRow
);
1279 /* start of the previous row */
1280 pItem
= ME_FindItemBack(pItem
, diStartRow
);
1282 return; /* row not found - ignore */
1283 pNewPara
= ME_GetParagraph(pItem
);
1284 if (pOldPara
->member
.para
.nFlags
& MEPF_ROWEND
||
1285 (pOldPara
->member
.para
.pCell
&&
1286 pOldPara
->member
.para
.pCell
!= pNewPara
->member
.para
.pCell
))
1288 /* Brought out of a cell */
1289 pNewPara
= ME_GetTableRowStart(pOldPara
)->member
.para
.prev_para
;
1290 if (pNewPara
->type
== diTextStart
)
1291 return; /* At the top, so don't go anywhere. */
1292 pItem
= ME_FindItemFwd(pNewPara
, diStartRow
);
1294 if (pNewPara
->member
.para
.nFlags
& MEPF_ROWEND
)
1296 /* Brought into a table row */
1297 ME_Cell
*cell
= &ME_FindItemBack(pNewPara
, diCell
)->member
.cell
;
1298 while (x
< cell
->pt
.x
&& cell
->prev_cell
)
1299 cell
= &cell
->prev_cell
->member
.cell
;
1300 if (cell
->next_cell
) /* else - we are still at the end of the row */
1301 pItem
= ME_FindItemBack(cell
->next_cell
, diStartRow
);
1306 /* start of the next row */
1307 pItem
= ME_FindItemFwd(pRun
, diStartRow
);
1309 return; /* row not found - ignore */
1310 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1312 pNewPara
= ME_GetParagraph(pItem
);
1313 if (pOldPara
->member
.para
.nFlags
& MEPF_ROWSTART
||
1314 (pOldPara
->member
.para
.pCell
&&
1315 pOldPara
->member
.para
.pCell
!= pNewPara
->member
.para
.pCell
))
1317 /* Brought out of a cell */
1318 pNewPara
= ME_GetTableRowEnd(pOldPara
)->member
.para
.next_para
;
1319 if (pNewPara
->type
== diTextEnd
)
1320 return; /* At the bottom, so don't go anywhere. */
1321 pItem
= ME_FindItemFwd(pNewPara
, diStartRow
);
1323 if (pNewPara
->member
.para
.nFlags
& MEPF_ROWSTART
)
1325 /* Brought into a table row */
1326 ME_DisplayItem
*cell
= ME_FindItemFwd(pNewPara
, diCell
);
1327 while (cell
->member
.cell
.next_cell
&&
1328 x
>= cell
->member
.cell
.next_cell
->member
.cell
.pt
.x
)
1329 cell
= cell
->member
.cell
.next_cell
;
1330 pItem
= ME_FindItemFwd(cell
, diStartRow
);
1335 /* row not found - ignore */
1338 pCursor
->pRun
= ME_FindRunInRow(editor
, pItem
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1339 assert(pCursor
->pRun
);
1340 assert(pCursor
->pRun
->type
== diRun
);
1344 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1346 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1347 ME_DisplayItem
*pLast
, *p
;
1348 int x
, y
, ys
, yd
, yp
, yprev
;
1349 ME_Cursor tmp_curs
= *pCursor
;
1351 x
= ME_GetXForArrow(editor
, pCursor
);
1352 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1353 pRun
= ME_FindItemBack(pRun
, diRun
);
1355 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1356 assert(p
->type
== diStartRow
);
1357 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1358 yprev
= ys
= y
= yp
+ p
->member
.row
.pt
.y
;
1359 yd
= y
- editor
->sizeWindow
.cy
;
1363 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
1366 if (p
->type
== diParagraph
) { /* crossing paragraphs */
1367 if (p
->member
.para
.prev_para
== NULL
)
1369 yp
= p
->member
.para
.prev_para
->member
.para
.pt
.y
;
1372 y
= yp
+ p
->member
.row
.pt
.y
;
1379 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1380 ME_UpdateSelection(editor
, &tmp_curs
);
1381 if (yprev
< editor
->sizeWindow
.cy
)
1383 ME_EnsureVisible(editor
, ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
));
1388 ME_ScrollUp(editor
, ys
-yprev
);
1390 assert(pCursor
->pRun
);
1391 assert(pCursor
->pRun
->type
== diRun
);
1394 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1395 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1396 In such a situation, clicking the scrollbar restores its position back to the
1397 normal range (ie. sets it to (doclength-screenheight)). */
1399 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1401 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1402 ME_DisplayItem
*pLast
, *p
;
1403 int x
, y
, ys
, yd
, yp
, yprev
;
1404 ME_Cursor tmp_curs
= *pCursor
;
1406 x
= ME_GetXForArrow(editor
, pCursor
);
1407 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1408 pRun
= ME_FindItemBack(pRun
, diRun
);
1410 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1411 assert(p
->type
== diStartRow
);
1412 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1413 yprev
= ys
= y
= yp
+ p
->member
.row
.pt
.y
;
1414 yd
= y
+ editor
->sizeWindow
.cy
;
1418 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
1421 if (p
->type
== diParagraph
) {
1422 yp
= p
->member
.para
.pt
.y
;
1425 y
= yp
+ p
->member
.row
.pt
.y
;
1432 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1433 ME_UpdateSelection(editor
, &tmp_curs
);
1434 if (yprev
>= editor
->nTotalLength
-editor
->sizeWindow
.cy
)
1436 ME_EnsureVisible(editor
, ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
));
1441 ME_ScrollUp(editor
,ys
-yprev
);
1443 assert(pCursor
->pRun
);
1444 assert(pCursor
->pRun
->type
== diRun
);
1447 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1449 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
1450 ME_WrapMarkedParagraphs(editor
);
1452 ME_DisplayItem
*pRun
;
1453 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
1454 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1458 pRun
= ME_FindItemFwd(pRow
, diRun
);
1460 pCursor
->pRun
= pRun
;
1461 pCursor
->nOffset
= 0;
1464 editor
->bCaretAtEnd
= FALSE
;
1467 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1469 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diTextStart
);
1471 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1473 pCursor
->pRun
= pRun
;
1474 pCursor
->nOffset
= 0;
1479 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1481 ME_DisplayItem
*pRow
;
1483 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1486 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1488 if (pRow
->type
== diStartRow
) {
1489 /* FIXME WTF was I thinking about here ? */
1490 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1492 pCursor
->pRun
= pRun
;
1493 pCursor
->nOffset
= 0;
1494 editor
->bCaretAtEnd
= 1;
1497 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1498 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1499 pCursor
->nOffset
= 0;
1500 editor
->bCaretAtEnd
= FALSE
;
1503 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1505 ME_DisplayItem
*p
= ME_FindItemFwd(pCursor
->pRun
, diTextEnd
);
1507 p
= ME_FindItemBack(p
, diRun
);
1509 assert(p
->member
.run
.nFlags
& MERF_ENDPARA
);
1511 pCursor
->nOffset
= 0;
1512 editor
->bCaretAtEnd
= FALSE
;
1515 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1517 return memcmp(&editor
->pCursors
[0], &editor
->pCursors
[1], sizeof(ME_Cursor
))!=0;
1520 static int ME_GetSelCursor(ME_TextEditor
*editor
, int dir
)
1522 int cdir
= ME_GetCursorOfs(editor
, 0) - ME_GetCursorOfs(editor
, 1);
1530 BOOL
ME_UpdateSelection(ME_TextEditor
*editor
, const ME_Cursor
*pTempCursor
)
1532 ME_Cursor old_anchor
= editor
->pCursors
[1];
1534 if (GetKeyState(VK_SHIFT
)>=0) /* cancelling selection */
1536 /* any selection was present ? if so, it's no more, repaint ! */
1537 editor
->pCursors
[1] = editor
->pCursors
[0];
1538 if (memcmp(pTempCursor
, &old_anchor
, sizeof(ME_Cursor
))) {
1545 if (!memcmp(pTempCursor
, &editor
->pCursors
[1], sizeof(ME_Cursor
))) /* starting selection */
1547 editor
->pCursors
[1] = *pTempCursor
;
1556 void ME_DeleteSelection(ME_TextEditor
*editor
)
1559 ME_GetSelection(editor
, &from
, &to
);
1560 ME_DeleteTextAtCursor(editor
, ME_GetSelCursor(editor
,-1), to
-from
);
1563 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1565 return ME_GetInsertStyle(editor
, 0);
1568 void ME_SendSelChange(ME_TextEditor
*editor
)
1572 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1575 sc
.nmhdr
.hwndFrom
= editor
->hWnd
;
1576 sc
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
1577 sc
.nmhdr
.code
= EN_SELCHANGE
;
1578 SendMessageW(editor
->hWnd
, EM_EXGETSEL
, 0, (LPARAM
)&sc
.chrg
);
1579 sc
.seltyp
= SEL_EMPTY
;
1580 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1581 sc
.seltyp
|= SEL_TEXT
;
1582 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* wth were RICHEDIT authors thinking ? */
1583 sc
.seltyp
|= SEL_MULTICHAR
;
1584 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1585 sc
.chrg
.cpMin
, sc
.chrg
.cpMax
, sc
.seltyp
,
1586 (sc
.seltyp
& SEL_TEXT
) ? "SEL_TEXT" : "",
1587 (sc
.seltyp
& SEL_MULTICHAR
) ? "SEL_MULTICHAR" : "");
1588 if (sc
.chrg
.cpMin
!= editor
->notified_cr
.cpMin
|| sc
.chrg
.cpMax
!= editor
->notified_cr
.cpMax
)
1590 ME_ClearTempStyle(editor
);
1592 editor
->notified_cr
= sc
.chrg
;
1593 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
, sc
.nmhdr
.idFrom
, (LPARAM
)&sc
);
1598 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1601 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1602 ME_Cursor tmp_curs
= *p
;
1603 BOOL success
= FALSE
;
1605 ME_CheckCharOffsets(editor
);
1608 editor
->bCaretAtEnd
= 0;
1610 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1612 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1615 editor
->bCaretAtEnd
= 0;
1617 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1619 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1622 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1625 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1628 ME_ArrowPageUp(editor
, &tmp_curs
);
1631 ME_ArrowPageDown(editor
, &tmp_curs
);
1635 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1637 ME_ArrowHome(editor
, &tmp_curs
);
1638 editor
->bCaretAtEnd
= 0;
1643 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1645 ME_ArrowEnd(editor
, &tmp_curs
);
1650 editor
->pCursors
[1] = tmp_curs
;
1653 ME_InvalidateSelection(editor
);
1655 HideCaret(editor
->hWnd
);
1656 ME_EnsureVisible(editor
, tmp_curs
.pRun
);
1657 ME_ShowCaret(editor
);
1658 ME_SendSelChange(editor
);