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
->pCursors
[0]);
33 *to
= ME_GetCursorOfs(&editor
->pCursors
[1]);
43 int ME_GetTextLength(ME_TextEditor
*editor
)
45 ME_DisplayItem
*pLast
= editor
->pBuffer
->pLast
;
46 return ME_CharOfsFromRunOfs(editor
, pLast
->member
.para
.prev_para
,
47 ME_FindItemBack(pLast
, diRun
), 0);
51 int ME_GetTextLengthEx(ME_TextEditor
*editor
, const GETTEXTLENGTHEX
*how
)
55 if (how
->flags
& GTL_PRECISE
&& how
->flags
& GTL_CLOSE
)
57 if (how
->flags
& GTL_NUMCHARS
&& how
->flags
& GTL_NUMBYTES
)
60 length
= ME_GetTextLength(editor
);
62 if ((editor
->styleFlags
& ES_MULTILINE
)
63 && (how
->flags
& GTL_USECRLF
)
64 && !editor
->bEmulateVersion10
) /* Ignore GTL_USECRLF flag in 1.0 emulation */
65 length
+= editor
->nParagraphs
- 1;
67 if (how
->flags
& GTL_NUMBYTES
)
71 if (how
->codepage
== 1200)
73 if (how
->flags
& GTL_PRECISE
)
74 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
75 if (GetCPInfo(how
->codepage
, &cpinfo
))
76 return length
* cpinfo
.MaxCharSize
;
77 ERR("Invalid codepage %u\n", how
->codepage
);
84 int ME_SetSelection(ME_TextEditor
*editor
, int from
, int to
)
87 const int len
= ME_GetTextLength(editor
);
89 /* all negative values are effectively the same */
96 if (from
== 0 && to
== -1)
98 editor
->pCursors
[1].pPara
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
99 editor
->pCursors
[1].pRun
= ME_FindItemFwd(editor
->pCursors
[1].pPara
, diRun
);
100 editor
->pCursors
[1].nOffset
= 0;
101 editor
->pCursors
[0].pPara
= editor
->pBuffer
->pLast
->member
.para
.prev_para
;
102 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
103 editor
->pCursors
[0].nOffset
= 0;
104 ME_InvalidateSelection(editor
);
105 ME_ClearTempStyle(editor
);
109 /* if both values are equal and also out of bound, that means to */
110 /* put the selection at the end of the text */
111 if ((from
== to
) && (to
< 0 || to
> len
))
117 /* if from is negative and to is positive then selection is */
118 /* deselected and caret moved to end of the current selection */
122 ME_GetSelection(editor
, &start
, &end
);
123 editor
->pCursors
[1] = editor
->pCursors
[0];
125 ME_ClearTempStyle(editor
);
129 /* adjust to if it's a negative value */
133 /* flip from and to if they are reversed */
141 /* after fiddling with the values, we find from > len && to > len */
144 /* special case with to too big */
151 editor
->pCursors
[0].pPara
= editor
->pBuffer
->pLast
->member
.para
.prev_para
;
152 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
153 editor
->pCursors
[0].nOffset
= 0;
154 editor
->pCursors
[1] = editor
->pCursors
[0];
155 ME_InvalidateSelection(editor
);
156 ME_ClearTempStyle(editor
);
160 ME_CursorFromCharOfs(editor
, from
, &editor
->pCursors
[1]);
161 ME_CursorFromCharOfs(editor
, to
, &editor
->pCursors
[0]);
162 /* Selection is not allowed in the middle of an end paragraph run. */
163 if (editor
->pCursors
[1].pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
164 editor
->pCursors
[1].nOffset
= 0;
165 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
166 editor
->pCursors
[0].nOffset
= 0;
172 ME_GetCursorCoordinates(ME_TextEditor
*editor
, ME_Cursor
*pCursor
,
173 int *x
, int *y
, int *height
)
176 ME_DisplayItem
*run
= pCursor
->pRun
;
177 ME_DisplayItem
*para
= pCursor
->pPara
;
178 ME_DisplayItem
*pSizeRun
= run
;
182 assert(height
&& x
&& y
);
183 assert(~para
->member
.para
.nFlags
& MEPF_REWRAP
);
184 assert(run
&& run
->type
== diRun
);
185 assert(para
&& para
->type
== diParagraph
);
187 row
= ME_FindItemBack(run
, diStartRowOrParagraph
);
188 assert(row
&& row
->type
== diStartRow
);
190 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
192 if (!pCursor
->nOffset
)
194 ME_DisplayItem
*prev
= ME_FindItemBack(run
, diRunOrParagraph
);
196 if (prev
->type
== diRun
)
199 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
&&
200 run
== ME_FindItemFwd(row
, diRun
))
202 ME_DisplayItem
*tmp
= ME_FindItemBack(row
, diRunOrParagraph
);
204 if (tmp
->type
== diRun
)
206 row
= ME_FindItemBack(tmp
, diStartRow
);
207 pSizeRun
= run
= tmp
;
209 assert(run
->type
== diRun
);
210 sz
= ME_GetRunSize(&c
, ¶
->member
.para
,
211 &run
->member
.run
, run
->member
.run
.strText
->nLen
,
212 row
->member
.row
.nLMargin
);
215 if (pCursor
->nOffset
) {
216 sz
= ME_GetRunSize(&c
, ¶
->member
.para
, &run
->member
.run
,
217 pCursor
->nOffset
, row
->member
.row
.nLMargin
);
220 *height
= pSizeRun
->member
.run
.nAscent
+ pSizeRun
->member
.run
.nDescent
;
221 *x
= c
.rcView
.left
+ run
->member
.run
.pt
.x
+ sz
.cx
- editor
->horz_si
.nPos
;
222 *y
= c
.rcView
.top
+ para
->member
.para
.pt
.y
+ row
->member
.row
.nBaseline
223 + run
->member
.run
.pt
.y
- pSizeRun
->member
.run
.nAscent
224 - editor
->vert_si
.nPos
;
225 ME_DestroyContext(&c
);
231 ME_MoveCaret(ME_TextEditor
*editor
)
235 ME_GetCursorCoordinates(editor
, &editor
->pCursors
[0], &x
, &y
, &height
);
236 if(editor
->bHaveFocus
&& !ME_IsSelection(editor
))
238 x
= min(x
, editor
->rcFormat
.right
-1);
239 ITextHost_TxCreateCaret(editor
->texthost
, NULL
, 0, height
);
240 ITextHost_TxSetCaretPos(editor
->texthost
, x
, y
);
245 void ME_ShowCaret(ME_TextEditor
*ed
)
248 if(ed
->bHaveFocus
&& !ME_IsSelection(ed
))
249 ITextHost_TxShowCaret(ed
->texthost
, TRUE
);
252 void ME_HideCaret(ME_TextEditor
*ed
)
254 if(!ed
->bHaveFocus
|| ME_IsSelection(ed
))
256 ITextHost_TxShowCaret(ed
->texthost
, FALSE
);
261 BOOL
ME_InternalDeleteText(ME_TextEditor
*editor
, int nOfs
, int nChars
,
266 int totalChars
= nChars
;
267 ME_DisplayItem
*start_para
;
269 /* Prevent deletion past last end of paragraph run. */
270 nChars
= min(nChars
, ME_GetTextLength(editor
) - nOfs
);
272 ME_CursorFromCharOfs(editor
, nOfs
, &c
);
273 start_para
= c
.pPara
;
277 ME_ProtectPartialTableDeletion(editor
, nOfs
, &nChars
);
285 ME_CursorFromCharOfs(editor
, nOfs
+nChars
, &c
);
287 nOfs
+nChars
== (c
.pRun
->member
.run
.nCharOfs
288 + c
.pPara
->member
.para
.nCharOfs
))
290 /* We aren't deleting anything in this run, so we will go back to the
291 * last run we are deleting text in. */
292 c
.pRun
= ME_FindItemBack(c
.pRun
, diRun
);
293 c
.pPara
= ME_GetParagraph(c
.pRun
);
294 c
.nOffset
= c
.pRun
->member
.run
.strText
->nLen
;
296 run
= &c
.pRun
->member
.run
;
297 if (run
->nFlags
& MERF_ENDPARA
) {
298 int eollen
= c
.pRun
->member
.run
.strText
->nLen
;
299 BOOL keepFirstParaFormat
;
301 if (!ME_FindItemFwd(c
.pRun
, diParagraph
))
305 keepFirstParaFormat
= (totalChars
== nChars
&& nChars
<= eollen
&&
307 if (!editor
->bEmulateVersion10
) /* v4.1 */
309 ME_DisplayItem
*next_para
= ME_FindItemFwd(c
.pRun
, diParagraphOrEnd
);
310 ME_DisplayItem
*this_para
= next_para
->member
.para
.prev_para
;
312 /* The end of paragraph before a table row is only deleted if there
313 * is nothing else on the line before it. */
314 if (this_para
== start_para
&&
315 next_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
317 /* If the paragraph will be empty, then it should be deleted, however
318 * it still might have text right now which would inherit the
319 * MEPF_STARTROW property if we joined it right now.
320 * Instead we will delete it after the preceding text is deleted. */
321 if (nOfs
> this_para
->member
.para
.nCharOfs
) {
322 /* Skip this end of line. */
323 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
326 keepFirstParaFormat
= TRUE
;
329 ME_JoinParagraphs(editor
, c
.pPara
, keepFirstParaFormat
);
330 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
331 ME_CheckCharOffsets(editor
);
332 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
338 int nCharsToDelete
= min(nChars
, c
.nOffset
);
341 c
.nOffset
-= nCharsToDelete
;
343 ME_FindItemBack(c
.pRun
, diParagraph
)->member
.para
.nFlags
|= MEPF_REWRAP
;
346 /* nChars is the number of characters that should be deleted from the
347 PRECEDING runs (these BEFORE cursor.pRun)
348 nCharsToDelete is a number of chars to delete from THIS run */
349 nChars
-= nCharsToDelete
;
350 shift
-= nCharsToDelete
;
351 TRACE("Deleting %d (remaning %d) chars at %d in '%s' (%d)\n",
352 nCharsToDelete
, nChars
, c
.nOffset
,
353 debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
355 if (!c
.nOffset
&& run
->strText
->nLen
== nCharsToDelete
)
357 /* undo = reinsert whole run */
358 /* nOfs is a character offset (from the start of the document
359 to the current (deleted) run */
360 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
362 pUndo
->di
.member
.run
.nCharOfs
= nOfs
+nChars
;
366 /* undo = reinsert partial run */
367 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
369 ME_DestroyString(pUndo
->di
.member
.run
.strText
);
370 pUndo
->di
.member
.run
.nCharOfs
= nOfs
+nChars
;
371 pUndo
->di
.member
.run
.strText
= ME_MakeStringN(run
->strText
->szData
+c
.nOffset
, nCharsToDelete
);
374 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
375 TRACE("Shift value: %d\n", shift
);
376 ME_StrDeleteV(run
->strText
, c
.nOffset
, nCharsToDelete
);
378 /* update cursors (including c) */
379 for (i
=-1; i
<editor
->nCursors
; i
++) {
380 ME_Cursor
*pThisCur
= editor
->pCursors
+ i
;
381 if (i
== -1) pThisCur
= &c
;
382 if (pThisCur
->pRun
== cursor
.pRun
) {
383 if (pThisCur
->nOffset
> cursor
.nOffset
) {
384 if (pThisCur
->nOffset
-cursor
.nOffset
< nCharsToDelete
)
385 pThisCur
->nOffset
= cursor
.nOffset
;
387 pThisCur
->nOffset
-= nCharsToDelete
;
388 assert(pThisCur
->nOffset
>= 0);
389 assert(pThisCur
->nOffset
<= run
->strText
->nLen
);
391 if (pThisCur
->nOffset
== run
->strText
->nLen
)
393 pThisCur
->pRun
= ME_FindItemFwd(pThisCur
->pRun
, diRunOrParagraphOrEnd
);
394 assert(pThisCur
->pRun
->type
== diRun
);
395 pThisCur
->nOffset
= 0;
400 /* c = updated data now */
402 if (c
.pRun
== cursor
.pRun
)
403 ME_SkipAndPropagateCharOffset(c
.pRun
, shift
);
405 ME_PropagateCharOffset(c
.pRun
, shift
);
407 if (!cursor
.pRun
->member
.run
.strText
->nLen
)
409 TRACE("Removing useless run\n");
410 ME_Remove(cursor
.pRun
);
411 ME_DestroyDisplayItem(cursor
.pRun
);
416 ME_CheckCharOffsets(editor);
424 BOOL
ME_DeleteTextAtCursor(ME_TextEditor
*editor
, int nCursor
, int nChars
)
426 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
427 /* text operations set modified state */
428 editor
->nModifyStep
= 1;
429 return ME_InternalDeleteText(editor
, ME_GetCursorOfs(&editor
->pCursors
[nCursor
]),
433 static ME_DisplayItem
*
434 ME_InternalInsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
435 const WCHAR
*str
, int len
, ME_Style
*style
,
438 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
440 editor
->bCaretAtEnd
= FALSE
;
442 assert(p
->pRun
->type
== diRun
);
444 return ME_InsertRunAtCursor(editor
, p
, style
, str
, len
, flags
);
448 void ME_InsertOLEFromCursor(ME_TextEditor
*editor
, const REOBJECT
* reo
, int nCursor
)
450 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
455 if (ME_IsSelection(editor
))
456 ME_DeleteSelection(editor
);
458 di
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
460 di
->member
.run
.ole_obj
= ALLOC_OBJ(*reo
);
461 ME_CopyReObject(di
->member
.run
.ole_obj
, reo
);
462 ME_ReleaseStyle(pStyle
);
466 void ME_InsertEndRowFromCursor(ME_TextEditor
*editor
, int nCursor
)
468 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
473 if (ME_IsSelection(editor
))
474 ME_DeleteSelection(editor
);
476 di
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
478 ME_ReleaseStyle(pStyle
);
482 void ME_InsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
483 const WCHAR
*str
, int len
, ME_Style
*style
)
489 /* FIXME really HERE ? */
490 if (ME_IsSelection(editor
))
491 ME_DeleteSelection(editor
);
493 /* FIXME: is this too slow? */
494 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
495 oldLen
= ME_GetTextLength(editor
);
497 /* text operations set modified state */
498 editor
->nModifyStep
= 1;
502 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
506 /* grow the text limit to fit our text */
507 if(editor
->nTextLimit
< oldLen
+len
)
508 editor
->nTextLimit
= oldLen
+ len
;
514 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
515 while(pos
- str
< len
&& *pos
!= '\r' && *pos
!= '\n' && *pos
!= '\t')
518 if (pos
!= str
) { /* handle text */
519 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
520 } else if (*pos
== '\t') { /* handle tabs */
522 ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, style
, MERF_TAB
);
524 } else { /* handle EOLs */
525 ME_DisplayItem
*tp
, *end_run
;
529 /* Find number of CR and LF in end of paragraph run */
532 if (len
> 1 && pos
[1] == '\n')
534 else if (len
> 2 && pos
[1] == '\r' && pos
[2] == '\n')
539 assert(*pos
== '\n');
544 if (!editor
->bEmulateVersion10
&& eol_len
== 3)
546 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
548 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, style
, 0);
552 if (!editor
->bEmulateVersion10
) {
554 eol_str
= ME_MakeStringN(&cr
, 1);
556 eol_str
= ME_MakeStringN(str
, eol_len
);
559 p
= &editor
->pCursors
[nCursor
];
561 ME_SplitRunSimple(editor
, p
->pRun
, p
->nOffset
);
562 p
= &editor
->pCursors
[nCursor
];
564 tmp_style
= ME_GetInsertStyle(editor
, nCursor
);
565 /* ME_SplitParagraph increases style refcount */
566 tp
= ME_SplitParagraph(editor
, p
->pRun
, p
->pRun
->member
.run
.style
, eol_str
, 0);
567 p
->pRun
= ME_FindItemFwd(tp
, diRun
);
568 p
->pPara
= ME_GetParagraph(p
->pRun
);
569 end_run
= ME_FindItemBack(tp
, diRun
);
570 ME_ReleaseStyle(end_run
->member
.run
.style
);
571 end_run
->member
.run
.style
= tmp_style
;
582 ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
584 ME_DisplayItem
*pRun
= pCursor
->pRun
;
588 if (!pCursor
->nOffset
)
590 ME_DisplayItem
*pPara
= pCursor
->pPara
;
592 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
600 if (pPara
->member
.para
.prev_para
->type
== diTextStart
)
602 pRun
= ME_FindItemBack(pPara
, diRunOrParagraph
);
603 pPara
= pPara
->member
.para
.prev_para
;
604 /* every paragraph ought to have at least one run */
605 assert(pRun
&& pRun
->type
== diRun
);
606 assert(pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
609 assert(pRun
->type
!= diRun
&& pRun
->type
!= diParagraph
);
612 } while (RUN_IS_HIDDEN(&pRun
->member
.run
) ||
613 pRun
->member
.run
.nFlags
& MERF_HIDDEN
);
614 pCursor
->pPara
= pPara
;
615 pCursor
->pRun
= pRun
;
616 if (pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
617 pCursor
->nOffset
= 0;
619 pCursor
->nOffset
= pRun
->member
.run
.strText
->nLen
;
622 if (pCursor
->nOffset
)
623 pCursor
->nOffset
= pCursor
->nOffset
+ nRelOfs
;
628 if (!(pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
630 int new_ofs
= pCursor
->nOffset
+ nRelOfs
;
632 if (new_ofs
< pRun
->member
.run
.strText
->nLen
)
634 pCursor
->nOffset
= new_ofs
;
639 pRun
= ME_FindItemFwd(pRun
, diRun
);
640 } while (pRun
&& (RUN_IS_HIDDEN(&pRun
->member
.run
) ||
641 pRun
->member
.run
.nFlags
& MERF_HIDDEN
));
644 pCursor
->pPara
= ME_GetParagraph(pRun
);
645 pCursor
->pRun
= pRun
;
646 pCursor
->nOffset
= 0;
655 ME_MoveCursorWords(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
657 ME_DisplayItem
*pRun
= cursor
->pRun
, *pOtherRun
;
658 int nOffset
= cursor
->nOffset
;
662 /* Backward movement */
665 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
666 nOffset
, WB_MOVEWORDLEFT
);
669 pOtherRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
670 if (pOtherRun
->type
== diRun
)
672 if (ME_CallWordBreakProc(editor
, pOtherRun
->member
.run
.strText
,
673 pOtherRun
->member
.run
.strText
->nLen
- 1,
675 && !(pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
676 && !(cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
677 && !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
, 0,
681 nOffset
= pOtherRun
->member
.run
.strText
->nLen
;
683 else if (pOtherRun
->type
== diParagraph
)
685 if (cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
687 /* Skip empty start of table row paragraph */
688 if (pOtherRun
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
689 pOtherRun
= pOtherRun
->member
.para
.prev_para
;
690 /* Paragraph breaks are treated as separate words */
691 if (pOtherRun
->member
.para
.prev_para
->type
== diTextStart
)
694 pRun
= ME_FindItemBack(pOtherRun
, diRun
);
702 /* Forward movement */
703 BOOL last_delim
= FALSE
;
707 if (last_delim
&& !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
708 nOffset
, WB_ISDELIMITER
))
710 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
711 nOffset
, WB_MOVEWORDRIGHT
);
712 if (nOffset
< pRun
->member
.run
.strText
->nLen
)
714 pOtherRun
= ME_FindItemFwd(pRun
, diRunOrParagraphOrEnd
);
715 if (pOtherRun
->type
== diRun
)
717 last_delim
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
718 nOffset
- 1, WB_ISDELIMITER
);
722 else if (pOtherRun
->type
== diParagraph
)
724 if (pOtherRun
->member
.para
.nFlags
& MEPF_ROWSTART
)
725 pOtherRun
= pOtherRun
->member
.para
.next_para
;
726 if (cursor
->pRun
== pRun
)
727 pRun
= ME_FindItemFwd(pOtherRun
, diRun
);
733 if (cursor
->pRun
== pRun
)
740 cursor
->pPara
= ME_GetParagraph(pRun
);
742 cursor
->nOffset
= nOffset
;
748 ME_SelectByType(ME_TextEditor
*editor
, ME_SelectionType selectionType
)
750 /* pCursor[0] is the end of the selection
751 * pCursor[1] is the start of the selection (or the position selection anchor)
752 * pCursor[2] and [3] are the selection anchors that are backed up
753 * so they are kept when the selection changes for drag selection.
756 editor
->nSelectionType
= selectionType
;
757 switch(selectionType
)
762 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
763 editor
->pCursors
[1] = editor
->pCursors
[0];
764 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
769 ME_DisplayItem
*pItem
;
770 ME_DIType fwdSearchType
, backSearchType
;
771 if (selectionType
== stParagraph
) {
772 backSearchType
= diParagraph
;
773 fwdSearchType
= diParagraphOrEnd
;
775 backSearchType
= diStartRow
;
776 fwdSearchType
= diStartRowOrParagraphOrEnd
;
778 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, fwdSearchType
);
780 if (pItem
->type
== diTextEnd
)
781 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
783 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
784 editor
->pCursors
[0].pPara
= ME_GetParagraph(editor
->pCursors
[0].pRun
);
785 editor
->pCursors
[0].nOffset
= 0;
787 pItem
= ME_FindItemBack(pItem
, backSearchType
);
788 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
789 editor
->pCursors
[1].pPara
= ME_GetParagraph(editor
->pCursors
[1].pRun
);
790 editor
->pCursors
[1].nOffset
= 0;
794 /* Select everything with cursor anchored from the start of the text */
795 editor
->nSelectionType
= stDocument
;
796 editor
->pCursors
[1].pPara
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
797 editor
->pCursors
[1].pRun
= ME_FindItemFwd(editor
->pCursors
[1].pPara
, diRun
);
798 editor
->pCursors
[1].nOffset
= 0;
799 editor
->pCursors
[0].pPara
= editor
->pBuffer
->pLast
->member
.para
.prev_para
;
800 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
801 editor
->pCursors
[0].nOffset
= 0;
805 /* Store the anchor positions for extending the selection. */
806 editor
->pCursors
[2] = editor
->pCursors
[0];
807 editor
->pCursors
[3] = editor
->pCursors
[1];
810 int ME_GetCursorOfs(const ME_Cursor
*cursor
)
812 return cursor
->pPara
->member
.para
.nCharOfs
813 + cursor
->pRun
->member
.run
.nCharOfs
+ cursor
->nOffset
;
816 /* Helper function for ME_FindPixelPos to find paragraph within tables */
817 static ME_DisplayItem
* ME_FindPixelPosInTableRow(int x
, int y
,
818 ME_DisplayItem
*para
)
820 ME_DisplayItem
*cell
, *next_cell
;
821 assert(para
->member
.para
.nFlags
& MEPF_ROWSTART
);
822 cell
= para
->member
.para
.next_para
->member
.para
.pCell
;
825 /* find the cell we are in */
826 while ((next_cell
= cell
->member
.cell
.next_cell
) != NULL
) {
827 if (x
< next_cell
->member
.cell
.pt
.x
)
829 para
= ME_FindItemFwd(cell
, diParagraph
);
830 /* Found the cell, but there might be multiple paragraphs in
831 * the cell, so need to search down the cell for the paragraph. */
832 while (cell
== para
->member
.para
.pCell
) {
833 if (y
< para
->member
.para
.pt
.y
+ para
->member
.para
.nHeight
)
835 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
836 return ME_FindPixelPosInTableRow(x
, y
, para
);
840 para
= para
->member
.para
.next_para
;
842 /* Past the end of the cell, so go back to the last cell paragraph */
843 return para
->member
.para
.prev_para
;
847 /* Return table row delimiter */
848 para
= ME_FindItemFwd(cell
, diParagraph
);
849 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
850 assert(para
->member
.para
.pFmt
->dwMask
& PFM_TABLEROWDELIMITER
);
851 assert(para
->member
.para
.pFmt
->wEffects
& PFE_TABLEROWDELIMITER
);
855 static BOOL
ME_ReturnFoundPos(ME_TextEditor
*editor
, ME_DisplayItem
*found
,
856 ME_Cursor
*result
, int rx
, BOOL isExact
)
859 assert(found
->type
== diRun
);
860 if ((found
->member
.run
.nFlags
& MERF_ENDPARA
) || rx
< 0)
862 result
->pRun
= found
;
863 result
->nOffset
= ME_CharFromPointCursor(editor
, rx
, &found
->member
.run
);
864 if (editor
->pCursors
[0].nOffset
== found
->member
.run
.strText
->nLen
&& rx
)
866 result
->pRun
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, diRun
);
869 result
->pPara
= ME_GetParagraph(result
->pRun
);
873 /* Finds the run and offset from the pixel position.
875 * x & y are pixel positions in virtual coordinates into the rich edit control,
876 * so client coordinates must first be adjusted by the scroll position.
878 * returns TRUE if the result was exactly under the cursor, otherwise returns
879 * FALSE, and result is set to the closest position to the coordinates.
881 static BOOL
ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
,
882 ME_Cursor
*result
, BOOL
*is_eol
)
884 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
885 ME_DisplayItem
*last
= NULL
;
889 x
-= editor
->rcFormat
.left
;
890 y
-= editor
->rcFormat
.top
;
896 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->member
.para
.next_para
)
898 assert(p
->type
== diParagraph
);
899 if (y
< p
->member
.para
.pt
.y
+ p
->member
.para
.nHeight
)
901 if (p
->member
.para
.nFlags
& MEPF_ROWSTART
)
902 p
= ME_FindPixelPosInTableRow(x
, y
, p
);
903 y
-= p
->member
.para
.pt
.y
;
904 p
= ME_FindItemFwd(p
, diStartRow
);
906 } else if (p
->member
.para
.nFlags
& MEPF_ROWSTART
) {
907 p
= ME_GetTableRowEnd(p
);
911 for (; p
!= editor
->pBuffer
->pLast
; )
914 assert(p
->type
== diStartRow
);
915 if (y
< p
->member
.row
.pt
.y
+ p
->member
.row
.nHeight
)
917 p
= ME_FindItemFwd(p
, diRun
);
920 pp
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
921 if (pp
->type
!= diStartRow
)
923 p
= ME_FindItemFwd(p
, diRun
);
928 if (p
== editor
->pBuffer
->pLast
)
930 /* The position is below the last paragraph, so the last row will be used
931 * rather than the end of the text, so the x position will be used to
932 * determine the offset closest to the pixel position. */
934 p
= ME_FindItemBack(p
, diStartRow
);
936 p
= ME_FindItemFwd(p
, diRun
);
940 p
= editor
->pBuffer
->pLast
;
943 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->next
)
948 rx
= x
- p
->member
.run
.pt
.x
;
949 if (rx
< p
->member
.run
.nWidth
)
950 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
954 p
= ME_FindItemFwd(p
, diRun
);
955 if (is_eol
) *is_eol
= 1;
956 rx
= 0; /* FIXME not sure */
957 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
962 rx
= 0; /* FIXME not sure */
964 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
969 result
->pRun
= ME_FindItemBack(p
, diRun
);
970 result
->pPara
= ME_GetParagraph(result
->pRun
);
972 assert(result
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
977 /* Returns the character offset closest to the pixel position
979 * x & y are pixel positions in client coordinates.
981 * isExact will be set to TRUE if the run is directly under the pixel
982 * position, FALSE if it not, unless isExact is set to NULL.
984 int ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
, BOOL
*isExact
)
990 ITextHost_TxGetClientRect(editor
->texthost
, &rc
);
991 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
) {
992 if (isExact
) *isExact
= FALSE
;
995 x
+= editor
->horz_si
.nPos
;
996 y
+= editor
->vert_si
.nPos
;
997 bResult
= ME_FindPixelPos(editor
, x
, y
, &cursor
, NULL
);
998 if (isExact
) *isExact
= bResult
;
999 return cursor
.pPara
->member
.para
.nCharOfs
1000 + cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
;
1005 /* Extends the selection with a word, line, or paragraph selection type.
1007 * The selection is anchored by editor->pCursors[2-3] such that the text
1008 * between the anchors will remain selected, and one end will be extended.
1010 * editor->pCursors[0] should have the position to extend the selection to
1011 * before this function is called.
1013 * Nothing will be done if editor->nSelectionType equals stPosition.
1015 static void ME_ExtendAnchorSelection(ME_TextEditor
*editor
)
1017 ME_Cursor tmp_cursor
;
1018 int curOfs
, anchorStartOfs
, anchorEndOfs
;
1019 if (editor
->nSelectionType
== stPosition
|| editor
->nSelectionType
== stDocument
)
1021 curOfs
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1022 anchorStartOfs
= ME_GetCursorOfs(&editor
->pCursors
[3]);
1023 anchorEndOfs
= ME_GetCursorOfs(&editor
->pCursors
[2]);
1025 tmp_cursor
= editor
->pCursors
[0];
1026 editor
->pCursors
[0] = editor
->pCursors
[2];
1027 editor
->pCursors
[1] = editor
->pCursors
[3];
1028 if (curOfs
< anchorStartOfs
)
1030 /* Extend the left side of selection */
1031 editor
->pCursors
[1] = tmp_cursor
;
1032 if (editor
->nSelectionType
== stWord
)
1033 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
1036 ME_DisplayItem
*pItem
;
1037 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1038 diStartRowOrParagraph
:diParagraph
);
1039 pItem
= ME_FindItemBack(editor
->pCursors
[1].pRun
, searchType
);
1040 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
1041 editor
->pCursors
[1].pPara
= ME_GetParagraph(editor
->pCursors
[1].pRun
);
1042 editor
->pCursors
[1].nOffset
= 0;
1045 else if (curOfs
>= anchorEndOfs
)
1047 /* Extend the right side of selection */
1048 editor
->pCursors
[0] = tmp_cursor
;
1049 if (editor
->nSelectionType
== stWord
)
1050 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
1053 ME_DisplayItem
*pItem
;
1054 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1055 diStartRowOrParagraphOrEnd
:diParagraphOrEnd
);
1056 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, searchType
);
1057 if (pItem
->type
== diTextEnd
)
1058 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
1060 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
1061 editor
->pCursors
[0].pPara
= ME_GetParagraph(editor
->pCursors
[0].pRun
);
1062 editor
->pCursors
[0].nOffset
= 0;
1067 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
, int clickNum
)
1069 ME_Cursor tmp_cursor
;
1070 int is_selection
= 0;
1073 editor
->nUDArrowX
= -1;
1075 x
+= editor
->horz_si
.nPos
;
1076 y
+= editor
->vert_si
.nPos
;
1078 tmp_cursor
= editor
->pCursors
[0];
1079 is_selection
= ME_IsSelection(editor
);
1080 is_shift
= GetKeyState(VK_SHIFT
) < 0;
1082 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
1084 if (x
>= editor
->rcFormat
.left
|| is_shift
)
1088 editor
->pCursors
[1] = editor
->pCursors
[0];
1090 if (x
>= editor
->rcFormat
.left
)
1091 ME_SelectByType(editor
, stWord
);
1093 ME_SelectByType(editor
, stParagraph
);
1094 } else if (clickNum
% 2 == 0) {
1095 ME_SelectByType(editor
, stWord
);
1097 ME_SelectByType(editor
, stParagraph
);
1102 editor
->nSelectionType
= stPosition
;
1103 editor
->pCursors
[1] = editor
->pCursors
[0];
1105 else if (!is_selection
)
1107 editor
->nSelectionType
= stPosition
;
1108 editor
->pCursors
[1] = tmp_cursor
;
1110 else if (editor
->nSelectionType
!= stPosition
)
1112 ME_ExtendAnchorSelection(editor
);
1118 ME_SelectByType(editor
, stLine
);
1119 } else if (clickNum
% 2 == 0 || is_shift
) {
1120 ME_SelectByType(editor
, stParagraph
);
1122 ME_SelectByType(editor
, stDocument
);
1125 ME_InvalidateSelection(editor
);
1126 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1127 ME_ShowCaret(editor
);
1128 ME_ClearTempStyle(editor
);
1129 ME_SendSelChange(editor
);
1132 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
1134 ME_Cursor tmp_cursor
;
1136 if (editor
->nSelectionType
== stDocument
)
1138 x
+= editor
->horz_si
.nPos
;
1139 y
+= editor
->vert_si
.nPos
;
1141 tmp_cursor
= editor
->pCursors
[0];
1142 /* FIXME: do something with the return value of ME_FindPixelPos */
1143 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
1145 ME_InvalidateSelection(editor
);
1146 editor
->pCursors
[0] = tmp_cursor
;
1147 ME_ExtendAnchorSelection(editor
);
1149 if (editor
->nSelectionType
!= stPosition
&&
1150 memcmp(&editor
->pCursors
[1], &editor
->pCursors
[3], sizeof(ME_Cursor
)))
1152 /* The scroll the cursor towards the other end, since it was the one
1153 * extended by ME_ExtendAnchorSelection */
1154 ME_EnsureVisible(editor
, &editor
->pCursors
[1]);
1156 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
1159 ME_InvalidateSelection(editor
);
1160 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1161 ME_ShowCaret(editor
);
1162 ME_SendSelChange(editor
);
1165 static ME_DisplayItem
*ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
1166 int x
, int *pOffset
, int *pbCaretAtEnd
)
1168 ME_DisplayItem
*pNext
, *pLastRun
;
1169 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
1170 assert(pNext
->type
== diRun
);
1172 if (pbCaretAtEnd
) *pbCaretAtEnd
= FALSE
;
1173 if (pOffset
) *pOffset
= 0;
1175 int run_x
= pNext
->member
.run
.pt
.x
;
1176 int width
= pNext
->member
.run
.nWidth
;
1181 if (x
>= run_x
&& x
< run_x
+width
)
1183 int ch
= ME_CharFromPointCursor(editor
, x
-run_x
, &pNext
->member
.run
);
1184 ME_String
*s
= pNext
->member
.run
.strText
;
1192 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
1193 } while(pNext
&& pNext
->type
== diRun
);
1195 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
1197 pNext
= ME_FindItemFwd(pNext
, diRun
);
1198 if (pbCaretAtEnd
) *pbCaretAtEnd
= TRUE
;
1205 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1207 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1210 if (editor
->nUDArrowX
!= -1)
1211 x
= editor
->nUDArrowX
;
1213 if (editor
->bCaretAtEnd
)
1215 pRun
= ME_FindItemBack(pRun
, diRun
);
1217 x
= pRun
->member
.run
.pt
.x
+ pRun
->member
.run
.nWidth
;
1220 x
= pRun
->member
.run
.pt
.x
;
1221 x
+= ME_PointFromChar(editor
, &pRun
->member
.run
, pCursor
->nOffset
);
1223 editor
->nUDArrowX
= x
;
1230 ME_MoveCursorLines(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
1232 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1233 ME_DisplayItem
*pItem
, *pOldPara
, *pNewPara
;
1234 int x
= ME_GetXForArrow(editor
, pCursor
);
1236 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1237 pRun
= ME_FindItemBack(pRun
, diRun
);
1240 pOldPara
= ME_GetParagraph(pRun
);
1243 /* start of this row */
1244 pItem
= ME_FindItemBack(pRun
, diStartRow
);
1246 /* start of the previous row */
1247 pItem
= ME_FindItemBack(pItem
, diStartRow
);
1249 return; /* row not found - ignore */
1250 pNewPara
= ME_GetParagraph(pItem
);
1251 if (pOldPara
->member
.para
.nFlags
& MEPF_ROWEND
||
1252 (pOldPara
->member
.para
.pCell
&&
1253 pOldPara
->member
.para
.pCell
!= pNewPara
->member
.para
.pCell
))
1255 /* Brought out of a cell */
1256 pNewPara
= ME_GetTableRowStart(pOldPara
)->member
.para
.prev_para
;
1257 if (pNewPara
->type
== diTextStart
)
1258 return; /* At the top, so don't go anywhere. */
1259 pItem
= ME_FindItemFwd(pNewPara
, diStartRow
);
1261 if (pNewPara
->member
.para
.nFlags
& MEPF_ROWEND
)
1263 /* Brought into a table row */
1264 ME_Cell
*cell
= &ME_FindItemBack(pNewPara
, diCell
)->member
.cell
;
1265 while (x
< cell
->pt
.x
&& cell
->prev_cell
)
1266 cell
= &cell
->prev_cell
->member
.cell
;
1267 if (cell
->next_cell
) /* else - we are still at the end of the row */
1268 pItem
= ME_FindItemBack(cell
->next_cell
, diStartRow
);
1273 /* start of the next row */
1274 pItem
= ME_FindItemFwd(pRun
, diStartRow
);
1276 return; /* row not found - ignore */
1277 pNewPara
= ME_GetParagraph(pItem
);
1278 if (pOldPara
->member
.para
.nFlags
& MEPF_ROWSTART
||
1279 (pOldPara
->member
.para
.pCell
&&
1280 pOldPara
->member
.para
.pCell
!= pNewPara
->member
.para
.pCell
))
1282 /* Brought out of a cell */
1283 pNewPara
= ME_GetTableRowEnd(pOldPara
)->member
.para
.next_para
;
1284 if (pNewPara
->type
== diTextEnd
)
1285 return; /* At the bottom, so don't go anywhere. */
1286 pItem
= ME_FindItemFwd(pNewPara
, diStartRow
);
1288 if (pNewPara
->member
.para
.nFlags
& MEPF_ROWSTART
)
1290 /* Brought into a table row */
1291 ME_DisplayItem
*cell
= ME_FindItemFwd(pNewPara
, diCell
);
1292 while (cell
->member
.cell
.next_cell
&&
1293 x
>= cell
->member
.cell
.next_cell
->member
.cell
.pt
.x
)
1294 cell
= cell
->member
.cell
.next_cell
;
1295 pItem
= ME_FindItemFwd(cell
, diStartRow
);
1300 /* row not found - ignore */
1303 pCursor
->pRun
= ME_FindRunInRow(editor
, pItem
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1304 pCursor
->pPara
= ME_GetParagraph(pCursor
->pRun
);
1305 assert(pCursor
->pRun
);
1306 assert(pCursor
->pRun
->type
== diRun
);
1309 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1311 ME_DisplayItem
*p
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diStartRow
);
1313 if (editor
->vert_si
.nPos
< p
->member
.row
.nHeight
)
1315 pCursor
->pPara
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
1316 pCursor
->pRun
= ME_FindItemFwd(pCursor
->pPara
, diRun
);
1317 pCursor
->nOffset
= 0;
1318 editor
->bCaretAtEnd
= FALSE
;
1319 /* Native clears seems to clear this x value on page up at the top
1320 * of the text, but not on page down at the end of the text.
1321 * Doesn't make sense, but we try to be bug for bug compatible. */
1322 editor
->nUDArrowX
= -1;
1324 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1325 ME_DisplayItem
*pLast
;
1326 int x
, y
, ys
, yd
, yp
, yprev
;
1327 int yOldScrollPos
= editor
->vert_si
.nPos
;
1329 x
= ME_GetXForArrow(editor
, pCursor
);
1330 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1331 pRun
= ME_FindItemBack(pRun
, diRun
);
1333 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1334 assert(p
->type
== diStartRow
);
1335 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1336 yprev
= ys
= y
= yp
+ p
->member
.row
.pt
.y
;
1338 ME_ScrollUp(editor
, editor
->sizeWindow
.cy
);
1339 /* Only move the cursor by the amount scrolled. */
1340 yd
= y
+ editor
->vert_si
.nPos
- yOldScrollPos
;
1344 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
1347 if (p
->type
== diParagraph
) { /* crossing paragraphs */
1348 if (p
->member
.para
.prev_para
== NULL
)
1350 yp
= p
->member
.para
.prev_para
->member
.para
.pt
.y
;
1353 y
= yp
+ p
->member
.row
.pt
.y
;
1360 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
,
1361 &editor
->bCaretAtEnd
);
1362 pCursor
->pPara
= ME_GetParagraph(pCursor
->pRun
);
1364 assert(pCursor
->pRun
);
1365 assert(pCursor
->pRun
->type
== diRun
);
1368 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1370 ME_DisplayItem
*pLast
;
1373 /* Find y position of the last row */
1374 pLast
= editor
->pBuffer
->pLast
;
1375 y
= pLast
->member
.para
.prev_para
->member
.para
.pt
.y
1376 + ME_FindItemBack(pLast
, diStartRow
)->member
.row
.pt
.y
;
1378 x
= ME_GetXForArrow(editor
, pCursor
);
1380 if (editor
->vert_si
.nPos
>= y
- editor
->sizeWindow
.cy
)
1382 pCursor
->pPara
= editor
->pBuffer
->pLast
->member
.para
.prev_para
;
1383 pCursor
->pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
1384 pCursor
->nOffset
= 0;
1385 editor
->bCaretAtEnd
= FALSE
;
1387 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1389 int ys
, yd
, yp
, yprev
;
1390 int yOldScrollPos
= editor
->vert_si
.nPos
;
1392 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1393 pRun
= ME_FindItemBack(pRun
, diRun
);
1395 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1396 assert(p
->type
== diStartRow
);
1397 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1398 yprev
= ys
= y
= yp
+ p
->member
.row
.pt
.y
;
1400 /* For native richedit controls:
1401 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1402 * v4.1 can scroll past this position here. */
1403 ME_ScrollDown(editor
, editor
->sizeWindow
.cy
);
1404 /* Only move the cursor by the amount scrolled. */
1405 yd
= y
+ editor
->vert_si
.nPos
- yOldScrollPos
;
1409 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
1412 if (p
->type
== diParagraph
) {
1413 yp
= p
->member
.para
.pt
.y
;
1416 y
= yp
+ p
->member
.row
.pt
.y
;
1423 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
,
1424 &editor
->bCaretAtEnd
);
1425 pCursor
->pPara
= ME_GetParagraph(pCursor
->pRun
);
1427 assert(pCursor
->pRun
);
1428 assert(pCursor
->pRun
->type
== diRun
);
1431 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1433 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
1435 ME_DisplayItem
*pRun
;
1436 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
1437 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1441 pRun
= ME_FindItemFwd(pRow
, diRun
);
1443 pCursor
->pRun
= pRun
;
1444 assert(pCursor
->pPara
== ME_GetParagraph(pRun
));
1445 pCursor
->nOffset
= 0;
1448 editor
->bCaretAtEnd
= FALSE
;
1451 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1453 pCursor
->pPara
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
1454 pCursor
->pRun
= ME_FindItemFwd(pCursor
->pPara
, diRun
);
1455 pCursor
->nOffset
= 0;
1456 editor
->bCaretAtEnd
= FALSE
;
1459 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1461 ME_DisplayItem
*pRow
;
1463 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1466 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1468 if (pRow
->type
== diStartRow
) {
1469 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1471 pCursor
->pRun
= pRun
;
1472 assert(pCursor
->pPara
== ME_GetParagraph(pCursor
->pRun
));
1473 pCursor
->nOffset
= 0;
1474 editor
->bCaretAtEnd
= TRUE
;
1477 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1478 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1479 assert(pCursor
->pPara
== ME_GetParagraph(pCursor
->pRun
));
1480 pCursor
->nOffset
= 0;
1481 editor
->bCaretAtEnd
= FALSE
;
1484 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1486 pCursor
->pPara
= editor
->pBuffer
->pLast
->member
.para
.prev_para
;
1487 pCursor
->pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
1488 assert(pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1489 pCursor
->nOffset
= 0;
1490 editor
->bCaretAtEnd
= FALSE
;
1493 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1495 return editor
->pCursors
[0].pRun
!= editor
->pCursors
[1].pRun
||
1496 editor
->pCursors
[0].nOffset
!= editor
->pCursors
[1].nOffset
;
1499 static int ME_GetSelCursor(ME_TextEditor
*editor
, int dir
)
1501 int cdir
= ME_GetCursorOfs(&editor
->pCursors
[0])
1502 - ME_GetCursorOfs(&editor
->pCursors
[1]);
1510 void ME_DeleteSelection(ME_TextEditor
*editor
)
1513 ME_GetSelection(editor
, &from
, &to
);
1514 ME_DeleteTextAtCursor(editor
, ME_GetSelCursor(editor
,-1), to
-from
);
1517 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1519 return ME_GetInsertStyle(editor
, 0);
1522 void ME_SendSelChange(ME_TextEditor
*editor
)
1526 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1529 sc
.nmhdr
.code
= EN_SELCHANGE
;
1530 ME_GetSelection(editor
, &sc
.chrg
.cpMin
, &sc
.chrg
.cpMax
);
1531 sc
.seltyp
= SEL_EMPTY
;
1532 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1533 sc
.seltyp
|= SEL_TEXT
;
1534 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* wth were RICHEDIT authors thinking ? */
1535 sc
.seltyp
|= SEL_MULTICHAR
;
1536 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1537 sc
.chrg
.cpMin
, sc
.chrg
.cpMax
, sc
.seltyp
,
1538 (sc
.seltyp
& SEL_TEXT
) ? "SEL_TEXT" : "",
1539 (sc
.seltyp
& SEL_MULTICHAR
) ? "SEL_MULTICHAR" : "");
1540 if (sc
.chrg
.cpMin
!= editor
->notified_cr
.cpMin
|| sc
.chrg
.cpMax
!= editor
->notified_cr
.cpMax
)
1542 ME_ClearTempStyle(editor
);
1544 editor
->notified_cr
= sc
.chrg
;
1545 ITextHost_TxNotify(editor
->texthost
, sc
.nmhdr
.code
, &sc
);
1550 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1553 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1554 ME_Cursor tmp_curs
= *p
;
1555 BOOL success
= FALSE
;
1557 ME_CheckCharOffsets(editor
);
1560 editor
->bCaretAtEnd
= 0;
1562 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1564 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1567 editor
->bCaretAtEnd
= 0;
1569 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1571 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1574 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1577 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1580 ME_ArrowPageUp(editor
, &tmp_curs
);
1583 ME_ArrowPageDown(editor
, &tmp_curs
);
1587 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1589 ME_ArrowHome(editor
, &tmp_curs
);
1590 editor
->bCaretAtEnd
= 0;
1595 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1597 ME_ArrowEnd(editor
, &tmp_curs
);
1602 editor
->pCursors
[1] = tmp_curs
;
1605 ME_InvalidateSelection(editor
);
1607 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1608 ME_EnsureVisible(editor
, &tmp_curs
);
1609 ME_ShowCaret(editor
);
1610 ME_SendSelChange(editor
);