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 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
, nCursor
), nChars
,
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_SendSelChange(editor
);
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_SendSelChange(editor
);
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(ME_TextEditor
*editor
, int nCursor
)
812 ME_Cursor
*pCursor
= &editor
->pCursors
[nCursor
];
813 return pCursor
->pPara
->member
.para
.nCharOfs
814 + pCursor
->pRun
->member
.run
.nCharOfs
+ pCursor
->nOffset
;
817 /* Helper function for ME_FindPixelPos to find paragraph within tables */
818 static ME_DisplayItem
* ME_FindPixelPosInTableRow(int x
, int y
,
819 ME_DisplayItem
*para
)
821 ME_DisplayItem
*cell
, *next_cell
;
822 assert(para
->member
.para
.nFlags
& MEPF_ROWSTART
);
823 cell
= para
->member
.para
.next_para
->member
.para
.pCell
;
826 /* find the cell we are in */
827 while ((next_cell
= cell
->member
.cell
.next_cell
) != NULL
) {
828 if (x
< next_cell
->member
.cell
.pt
.x
)
830 para
= ME_FindItemFwd(cell
, diParagraph
);
831 /* Found the cell, but there might be multiple paragraphs in
832 * the cell, so need to search down the cell for the paragraph. */
833 while (cell
== para
->member
.para
.pCell
) {
834 if (y
< para
->member
.para
.pt
.y
+ para
->member
.para
.nHeight
)
836 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
837 return ME_FindPixelPosInTableRow(x
, y
, para
);
841 para
= para
->member
.para
.next_para
;
843 /* Past the end of the cell, so go back to the last cell paragraph */
844 return para
->member
.para
.prev_para
;
848 /* Return table row delimiter */
849 para
= ME_FindItemFwd(cell
, diParagraph
);
850 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
851 assert(para
->member
.para
.pFmt
->dwMask
& PFM_TABLEROWDELIMITER
);
852 assert(para
->member
.para
.pFmt
->wEffects
& PFE_TABLEROWDELIMITER
);
856 static BOOL
ME_ReturnFoundPos(ME_TextEditor
*editor
, ME_DisplayItem
*found
,
857 ME_Cursor
*result
, int rx
, BOOL isExact
)
860 assert(found
->type
== diRun
);
861 if ((found
->member
.run
.nFlags
& MERF_ENDPARA
) || rx
< 0)
863 result
->pRun
= found
;
864 result
->nOffset
= ME_CharFromPointCursor(editor
, rx
, &found
->member
.run
);
865 if (editor
->pCursors
[0].nOffset
== found
->member
.run
.strText
->nLen
&& rx
)
867 result
->pRun
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, diRun
);
870 result
->pPara
= ME_GetParagraph(result
->pRun
);
874 /* Finds the run and offset from the pixel position.
876 * x & y are pixel positions in virtual coordinates into the rich edit control,
877 * so client coordinates must first be adjusted by the scroll position.
879 * returns TRUE if the result was exactly under the cursor, otherwise returns
880 * FALSE, and result is set to the closest position to the coordinates.
882 static BOOL
ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
,
883 ME_Cursor
*result
, BOOL
*is_eol
)
885 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
886 ME_DisplayItem
*last
= NULL
;
890 x
-= editor
->rcFormat
.left
;
891 y
-= editor
->rcFormat
.top
;
897 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->member
.para
.next_para
)
899 assert(p
->type
== diParagraph
);
900 if (y
< p
->member
.para
.pt
.y
+ p
->member
.para
.nHeight
)
902 if (p
->member
.para
.nFlags
& MEPF_ROWSTART
)
903 p
= ME_FindPixelPosInTableRow(x
, y
, p
);
904 y
-= p
->member
.para
.pt
.y
;
905 p
= ME_FindItemFwd(p
, diStartRow
);
907 } else if (p
->member
.para
.nFlags
& MEPF_ROWSTART
) {
908 p
= ME_GetTableRowEnd(p
);
912 for (; p
!= editor
->pBuffer
->pLast
; )
915 assert(p
->type
== diStartRow
);
916 if (y
< p
->member
.row
.pt
.y
+ p
->member
.row
.nHeight
)
918 p
= ME_FindItemFwd(p
, diRun
);
921 pp
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
922 if (pp
->type
!= diStartRow
)
924 p
= ME_FindItemFwd(p
, diRun
);
929 if (p
== editor
->pBuffer
->pLast
)
931 /* The position is below the last paragraph, so the last row will be used
932 * rather than the end of the text, so the x position will be used to
933 * determine the offset closest to the pixel position. */
935 p
= ME_FindItemBack(p
, diStartRow
);
937 p
= ME_FindItemFwd(p
, diRun
);
941 p
= editor
->pBuffer
->pLast
;
944 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->next
)
949 rx
= x
- p
->member
.run
.pt
.x
;
950 if (rx
< p
->member
.run
.nWidth
)
951 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
955 p
= ME_FindItemFwd(p
, diRun
);
956 if (is_eol
) *is_eol
= 1;
957 rx
= 0; /* FIXME not sure */
958 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
963 rx
= 0; /* FIXME not sure */
965 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
970 result
->pRun
= ME_FindItemBack(p
, diRun
);
971 result
->pPara
= ME_GetParagraph(result
->pRun
);
973 assert(result
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
978 /* Returns the character offset closest to the pixel position
980 * x & y are pixel positions in client coordinates.
982 * isExact will be set to TRUE if the run is directly under the pixel
983 * position, FALSE if it not, unless isExact is set to NULL.
985 int ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
, BOOL
*isExact
)
991 ITextHost_TxGetClientRect(editor
->texthost
, &rc
);
992 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
) {
993 if (isExact
) *isExact
= FALSE
;
996 x
+= editor
->horz_si
.nPos
;
997 y
+= editor
->vert_si
.nPos
;
998 bResult
= ME_FindPixelPos(editor
, x
, y
, &cursor
, NULL
);
999 if (isExact
) *isExact
= bResult
;
1000 return cursor
.pPara
->member
.para
.nCharOfs
1001 + cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
;
1006 /* Extends the selection with a word, line, or paragraph selection type.
1008 * The selection is anchored by editor->pCursors[2-3] such that the text
1009 * between the anchors will remain selected, and one end will be extended.
1011 * editor->pCursors[0] should have the position to extend the selection to
1012 * before this function is called.
1014 * Nothing will be done if editor->nSelectionType equals stPosition.
1016 static void ME_ExtendAnchorSelection(ME_TextEditor
*editor
)
1018 ME_Cursor tmp_cursor
;
1019 int curOfs
, anchorStartOfs
, anchorEndOfs
;
1020 if (editor
->nSelectionType
== stPosition
|| editor
->nSelectionType
== stDocument
)
1022 curOfs
= ME_GetCursorOfs(editor
, 0);
1023 anchorStartOfs
= ME_GetCursorOfs(editor
, 3);
1024 anchorEndOfs
= ME_GetCursorOfs(editor
, 2);
1026 tmp_cursor
= editor
->pCursors
[0];
1027 editor
->pCursors
[0] = editor
->pCursors
[2];
1028 editor
->pCursors
[1] = editor
->pCursors
[3];
1029 if (curOfs
< anchorStartOfs
)
1031 /* Extend the left side of selection */
1032 editor
->pCursors
[1] = tmp_cursor
;
1033 if (editor
->nSelectionType
== stWord
)
1034 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
1037 ME_DisplayItem
*pItem
;
1038 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1039 diStartRowOrParagraph
:diParagraph
);
1040 pItem
= ME_FindItemBack(editor
->pCursors
[1].pRun
, searchType
);
1041 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
1042 editor
->pCursors
[1].pPara
= ME_GetParagraph(editor
->pCursors
[1].pRun
);
1043 editor
->pCursors
[1].nOffset
= 0;
1046 else if (curOfs
>= anchorEndOfs
)
1048 /* Extend the right side of selection */
1049 editor
->pCursors
[0] = tmp_cursor
;
1050 if (editor
->nSelectionType
== stWord
)
1051 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
1054 ME_DisplayItem
*pItem
;
1055 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1056 diStartRowOrParagraphOrEnd
:diParagraphOrEnd
);
1057 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, searchType
);
1058 if (pItem
->type
== diTextEnd
)
1059 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
1061 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
1062 editor
->pCursors
[0].pPara
= ME_GetParagraph(editor
->pCursors
[0].pRun
);
1063 editor
->pCursors
[0].nOffset
= 0;
1068 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
, int clickNum
)
1070 ME_Cursor tmp_cursor
;
1071 int is_selection
= 0;
1074 editor
->nUDArrowX
= -1;
1076 x
+= editor
->horz_si
.nPos
;
1077 y
+= editor
->vert_si
.nPos
;
1079 tmp_cursor
= editor
->pCursors
[0];
1080 is_selection
= ME_IsSelection(editor
);
1081 is_shift
= GetKeyState(VK_SHIFT
) < 0;
1083 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
1085 if (x
>= editor
->rcFormat
.left
|| is_shift
)
1089 editor
->pCursors
[1] = editor
->pCursors
[0];
1091 if (x
>= editor
->rcFormat
.left
)
1092 ME_SelectByType(editor
, stWord
);
1094 ME_SelectByType(editor
, stParagraph
);
1095 } else if (clickNum
% 2 == 0) {
1096 ME_SelectByType(editor
, stWord
);
1098 ME_SelectByType(editor
, stParagraph
);
1103 editor
->nSelectionType
= stPosition
;
1104 editor
->pCursors
[1] = editor
->pCursors
[0];
1106 else if (!is_selection
)
1108 editor
->nSelectionType
= stPosition
;
1109 editor
->pCursors
[1] = tmp_cursor
;
1111 else if (editor
->nSelectionType
!= stPosition
)
1113 ME_ExtendAnchorSelection(editor
);
1119 ME_SelectByType(editor
, stLine
);
1120 } else if (clickNum
% 2 == 0 || is_shift
) {
1121 ME_SelectByType(editor
, stParagraph
);
1123 ME_SelectByType(editor
, stDocument
);
1126 ME_InvalidateSelection(editor
);
1127 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1128 ME_ShowCaret(editor
);
1129 ME_ClearTempStyle(editor
);
1130 ME_SendSelChange(editor
);
1133 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
1135 ME_Cursor tmp_cursor
;
1137 if (editor
->nSelectionType
== stDocument
)
1139 x
+= editor
->horz_si
.nPos
;
1140 y
+= editor
->vert_si
.nPos
;
1142 tmp_cursor
= editor
->pCursors
[0];
1143 /* FIXME: do something with the return value of ME_FindPixelPos */
1144 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
1146 ME_InvalidateSelection(editor
);
1147 editor
->pCursors
[0] = tmp_cursor
;
1148 ME_ExtendAnchorSelection(editor
);
1150 if (editor
->nSelectionType
!= stPosition
&&
1151 memcmp(&editor
->pCursors
[1], &editor
->pCursors
[3], sizeof(ME_Cursor
)))
1153 /* The scroll the cursor towards the other end, since it was the one
1154 * extended by ME_ExtendAnchorSelection */
1155 ME_EnsureVisible(editor
, &editor
->pCursors
[1]);
1157 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
1160 ME_InvalidateSelection(editor
);
1161 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1162 ME_ShowCaret(editor
);
1163 ME_SendSelChange(editor
);
1166 static ME_DisplayItem
*ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
1167 int x
, int *pOffset
, int *pbCaretAtEnd
)
1169 ME_DisplayItem
*pNext
, *pLastRun
;
1170 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
1171 assert(pNext
->type
== diRun
);
1173 if (pbCaretAtEnd
) *pbCaretAtEnd
= FALSE
;
1174 if (pOffset
) *pOffset
= 0;
1176 int run_x
= pNext
->member
.run
.pt
.x
;
1177 int width
= pNext
->member
.run
.nWidth
;
1182 if (x
>= run_x
&& x
< run_x
+width
)
1184 int ch
= ME_CharFromPointCursor(editor
, x
-run_x
, &pNext
->member
.run
);
1185 ME_String
*s
= pNext
->member
.run
.strText
;
1193 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
1194 } while(pNext
&& pNext
->type
== diRun
);
1196 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
1198 pNext
= ME_FindItemFwd(pNext
, diRun
);
1199 if (pbCaretAtEnd
) *pbCaretAtEnd
= TRUE
;
1206 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1208 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1211 if (editor
->nUDArrowX
!= -1)
1212 x
= editor
->nUDArrowX
;
1214 if (editor
->bCaretAtEnd
)
1216 pRun
= ME_FindItemBack(pRun
, diRun
);
1218 x
= pRun
->member
.run
.pt
.x
+ pRun
->member
.run
.nWidth
;
1221 x
= pRun
->member
.run
.pt
.x
;
1222 x
+= ME_PointFromChar(editor
, &pRun
->member
.run
, pCursor
->nOffset
);
1224 editor
->nUDArrowX
= x
;
1231 ME_MoveCursorLines(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
1233 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1234 ME_DisplayItem
*pItem
, *pOldPara
, *pNewPara
;
1235 int x
= ME_GetXForArrow(editor
, pCursor
);
1237 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1238 pRun
= ME_FindItemBack(pRun
, diRun
);
1241 pOldPara
= ME_GetParagraph(pRun
);
1244 /* start of this row */
1245 pItem
= ME_FindItemBack(pRun
, diStartRow
);
1247 /* start of the previous row */
1248 pItem
= ME_FindItemBack(pItem
, diStartRow
);
1250 return; /* row not found - ignore */
1251 pNewPara
= ME_GetParagraph(pItem
);
1252 if (pOldPara
->member
.para
.nFlags
& MEPF_ROWEND
||
1253 (pOldPara
->member
.para
.pCell
&&
1254 pOldPara
->member
.para
.pCell
!= pNewPara
->member
.para
.pCell
))
1256 /* Brought out of a cell */
1257 pNewPara
= ME_GetTableRowStart(pOldPara
)->member
.para
.prev_para
;
1258 if (pNewPara
->type
== diTextStart
)
1259 return; /* At the top, so don't go anywhere. */
1260 pItem
= ME_FindItemFwd(pNewPara
, diStartRow
);
1262 if (pNewPara
->member
.para
.nFlags
& MEPF_ROWEND
)
1264 /* Brought into a table row */
1265 ME_Cell
*cell
= &ME_FindItemBack(pNewPara
, diCell
)->member
.cell
;
1266 while (x
< cell
->pt
.x
&& cell
->prev_cell
)
1267 cell
= &cell
->prev_cell
->member
.cell
;
1268 if (cell
->next_cell
) /* else - we are still at the end of the row */
1269 pItem
= ME_FindItemBack(cell
->next_cell
, diStartRow
);
1274 /* start of the next row */
1275 pItem
= ME_FindItemFwd(pRun
, diStartRow
);
1277 return; /* row not found - ignore */
1278 pNewPara
= ME_GetParagraph(pItem
);
1279 if (pOldPara
->member
.para
.nFlags
& MEPF_ROWSTART
||
1280 (pOldPara
->member
.para
.pCell
&&
1281 pOldPara
->member
.para
.pCell
!= pNewPara
->member
.para
.pCell
))
1283 /* Brought out of a cell */
1284 pNewPara
= ME_GetTableRowEnd(pOldPara
)->member
.para
.next_para
;
1285 if (pNewPara
->type
== diTextEnd
)
1286 return; /* At the bottom, so don't go anywhere. */
1287 pItem
= ME_FindItemFwd(pNewPara
, diStartRow
);
1289 if (pNewPara
->member
.para
.nFlags
& MEPF_ROWSTART
)
1291 /* Brought into a table row */
1292 ME_DisplayItem
*cell
= ME_FindItemFwd(pNewPara
, diCell
);
1293 while (cell
->member
.cell
.next_cell
&&
1294 x
>= cell
->member
.cell
.next_cell
->member
.cell
.pt
.x
)
1295 cell
= cell
->member
.cell
.next_cell
;
1296 pItem
= ME_FindItemFwd(cell
, diStartRow
);
1301 /* row not found - ignore */
1304 pCursor
->pRun
= ME_FindRunInRow(editor
, pItem
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1305 pCursor
->pPara
= ME_GetParagraph(pCursor
->pRun
);
1306 assert(pCursor
->pRun
);
1307 assert(pCursor
->pRun
->type
== diRun
);
1310 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1312 ME_DisplayItem
*p
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diStartRow
);
1314 if (editor
->vert_si
.nPos
< p
->member
.row
.nHeight
)
1316 pCursor
->pPara
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
1317 pCursor
->pRun
= ME_FindItemFwd(pCursor
->pPara
, diRun
);
1318 pCursor
->nOffset
= 0;
1319 editor
->bCaretAtEnd
= FALSE
;
1320 /* Native clears seems to clear this x value on page up at the top
1321 * of the text, but not on page down at the end of the text.
1322 * Doesn't make sense, but we try to be bug for bug compatible. */
1323 editor
->nUDArrowX
= -1;
1325 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1326 ME_DisplayItem
*pLast
;
1327 int x
, y
, ys
, yd
, yp
, yprev
;
1328 int yOldScrollPos
= editor
->vert_si
.nPos
;
1330 x
= ME_GetXForArrow(editor
, pCursor
);
1331 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1332 pRun
= ME_FindItemBack(pRun
, diRun
);
1334 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1335 assert(p
->type
== diStartRow
);
1336 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1337 yprev
= ys
= y
= yp
+ p
->member
.row
.pt
.y
;
1339 ME_ScrollUp(editor
, editor
->sizeWindow
.cy
);
1340 /* Only move the cursor by the amount scrolled. */
1341 yd
= y
+ editor
->vert_si
.nPos
- yOldScrollPos
;
1345 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
1348 if (p
->type
== diParagraph
) { /* crossing paragraphs */
1349 if (p
->member
.para
.prev_para
== NULL
)
1351 yp
= p
->member
.para
.prev_para
->member
.para
.pt
.y
;
1354 y
= yp
+ p
->member
.row
.pt
.y
;
1361 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
,
1362 &editor
->bCaretAtEnd
);
1363 pCursor
->pPara
= ME_GetParagraph(pCursor
->pRun
);
1365 assert(pCursor
->pRun
);
1366 assert(pCursor
->pRun
->type
== diRun
);
1369 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1371 ME_DisplayItem
*pLast
;
1374 /* Find y position of the last row */
1375 pLast
= editor
->pBuffer
->pLast
;
1376 y
= pLast
->member
.para
.prev_para
->member
.para
.pt
.y
1377 + ME_FindItemBack(pLast
, diStartRow
)->member
.row
.pt
.y
;
1379 x
= ME_GetXForArrow(editor
, pCursor
);
1381 if (editor
->vert_si
.nPos
>= y
- editor
->sizeWindow
.cy
)
1383 pCursor
->pPara
= editor
->pBuffer
->pLast
->member
.para
.prev_para
;
1384 pCursor
->pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
1385 pCursor
->nOffset
= 0;
1386 editor
->bCaretAtEnd
= FALSE
;
1388 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1390 int ys
, yd
, yp
, yprev
;
1391 int yOldScrollPos
= editor
->vert_si
.nPos
;
1393 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1394 pRun
= ME_FindItemBack(pRun
, diRun
);
1396 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1397 assert(p
->type
== diStartRow
);
1398 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1399 yprev
= ys
= y
= yp
+ p
->member
.row
.pt
.y
;
1401 /* For native richedit controls:
1402 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1403 * v4.1 can scroll past this position here. */
1404 ME_ScrollDown(editor
, editor
->sizeWindow
.cy
);
1405 /* Only move the cursor by the amount scrolled. */
1406 yd
= y
+ editor
->vert_si
.nPos
- yOldScrollPos
;
1410 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
1413 if (p
->type
== diParagraph
) {
1414 yp
= p
->member
.para
.pt
.y
;
1417 y
= yp
+ p
->member
.row
.pt
.y
;
1424 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
,
1425 &editor
->bCaretAtEnd
);
1426 pCursor
->pPara
= ME_GetParagraph(pCursor
->pRun
);
1428 assert(pCursor
->pRun
);
1429 assert(pCursor
->pRun
->type
== diRun
);
1432 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1434 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
1436 ME_DisplayItem
*pRun
;
1437 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
1438 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1442 pRun
= ME_FindItemFwd(pRow
, diRun
);
1444 pCursor
->pRun
= pRun
;
1445 assert(pCursor
->pPara
== ME_GetParagraph(pRun
));
1446 pCursor
->nOffset
= 0;
1449 editor
->bCaretAtEnd
= FALSE
;
1452 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1454 pCursor
->pPara
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
1455 pCursor
->pRun
= ME_FindItemFwd(pCursor
->pPara
, diRun
);
1456 pCursor
->nOffset
= 0;
1457 editor
->bCaretAtEnd
= FALSE
;
1460 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1462 ME_DisplayItem
*pRow
;
1464 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1467 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1469 if (pRow
->type
== diStartRow
) {
1470 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1472 pCursor
->pRun
= pRun
;
1473 assert(pCursor
->pPara
== ME_GetParagraph(pCursor
->pRun
));
1474 pCursor
->nOffset
= 0;
1475 editor
->bCaretAtEnd
= TRUE
;
1478 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1479 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1480 assert(pCursor
->pPara
== ME_GetParagraph(pCursor
->pRun
));
1481 pCursor
->nOffset
= 0;
1482 editor
->bCaretAtEnd
= FALSE
;
1485 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1487 pCursor
->pPara
= editor
->pBuffer
->pLast
->member
.para
.prev_para
;
1488 pCursor
->pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
1489 assert(pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1490 pCursor
->nOffset
= 0;
1491 editor
->bCaretAtEnd
= FALSE
;
1494 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1496 return memcmp(&editor
->pCursors
[0], &editor
->pCursors
[1], sizeof(ME_Cursor
))!=0;
1499 static int ME_GetSelCursor(ME_TextEditor
*editor
, int dir
)
1501 int cdir
= ME_GetCursorOfs(editor
, 0) - ME_GetCursorOfs(editor
, 1);
1509 void ME_DeleteSelection(ME_TextEditor
*editor
)
1512 ME_GetSelection(editor
, &from
, &to
);
1513 ME_DeleteTextAtCursor(editor
, ME_GetSelCursor(editor
,-1), to
-from
);
1516 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1518 return ME_GetInsertStyle(editor
, 0);
1521 void ME_SendSelChange(ME_TextEditor
*editor
)
1525 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1528 sc
.nmhdr
.code
= EN_SELCHANGE
;
1529 ME_GetSelection(editor
, &sc
.chrg
.cpMin
, &sc
.chrg
.cpMax
);
1530 sc
.seltyp
= SEL_EMPTY
;
1531 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1532 sc
.seltyp
|= SEL_TEXT
;
1533 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* wth were RICHEDIT authors thinking ? */
1534 sc
.seltyp
|= SEL_MULTICHAR
;
1535 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1536 sc
.chrg
.cpMin
, sc
.chrg
.cpMax
, sc
.seltyp
,
1537 (sc
.seltyp
& SEL_TEXT
) ? "SEL_TEXT" : "",
1538 (sc
.seltyp
& SEL_MULTICHAR
) ? "SEL_MULTICHAR" : "");
1539 if (sc
.chrg
.cpMin
!= editor
->notified_cr
.cpMin
|| sc
.chrg
.cpMax
!= editor
->notified_cr
.cpMax
)
1541 ME_ClearTempStyle(editor
);
1543 editor
->notified_cr
= sc
.chrg
;
1544 ITextHost_TxNotify(editor
->texthost
, sc
.nmhdr
.code
, &sc
);
1549 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1552 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1553 ME_Cursor tmp_curs
= *p
;
1554 BOOL success
= FALSE
;
1556 ME_CheckCharOffsets(editor
);
1559 editor
->bCaretAtEnd
= 0;
1561 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1563 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1566 editor
->bCaretAtEnd
= 0;
1568 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1570 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1573 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1576 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1579 ME_ArrowPageUp(editor
, &tmp_curs
);
1582 ME_ArrowPageDown(editor
, &tmp_curs
);
1586 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1588 ME_ArrowHome(editor
, &tmp_curs
);
1589 editor
->bCaretAtEnd
= 0;
1594 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1596 ME_ArrowEnd(editor
, &tmp_curs
);
1601 editor
->pCursors
[1] = tmp_curs
;
1604 ME_InvalidateSelection(editor
);
1606 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1607 ME_EnsureVisible(editor
, &tmp_curs
);
1608 ME_ShowCaret(editor
);
1609 ME_SendSelChange(editor
);