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
);
27 void ME_SetCursorToStart(ME_TextEditor
*editor
, ME_Cursor
*cursor
)
29 cursor
->pPara
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
30 cursor
->pRun
= ME_FindItemFwd(cursor
->pPara
, diRun
);
34 static void ME_SetCursorToEnd(ME_TextEditor
*editor
, ME_Cursor
*cursor
)
36 cursor
->pPara
= editor
->pBuffer
->pLast
->member
.para
.prev_para
;
37 cursor
->pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
42 int ME_GetSelectionOfs(ME_TextEditor
*editor
, int *from
, int *to
)
44 *from
= ME_GetCursorOfs(&editor
->pCursors
[0]);
45 *to
= ME_GetCursorOfs(&editor
->pCursors
[1]);
57 int ME_GetSelection(ME_TextEditor
*editor
, ME_Cursor
**from
, ME_Cursor
**to
)
59 int from_ofs
= ME_GetCursorOfs( &editor
->pCursors
[0] );
60 int to_ofs
= ME_GetCursorOfs( &editor
->pCursors
[1] );
61 BOOL swap
= (from_ofs
> to_ofs
);
63 if (from_ofs
== to_ofs
)
65 /* If cursor[0] is at the beginning of a run and cursor[1] at the end
66 of the prev run then we need to swap. */
67 if (editor
->pCursors
[0].nOffset
< editor
->pCursors
[1].nOffset
)
73 *from
= &editor
->pCursors
[0];
74 *to
= &editor
->pCursors
[1];
77 *from
= &editor
->pCursors
[1];
78 *to
= &editor
->pCursors
[0];
83 int ME_GetTextLength(ME_TextEditor
*editor
)
86 ME_SetCursorToEnd(editor
, &cursor
);
87 return ME_GetCursorOfs(&cursor
);
91 int ME_GetTextLengthEx(ME_TextEditor
*editor
, const GETTEXTLENGTHEX
*how
)
95 if (how
->flags
& GTL_PRECISE
&& how
->flags
& GTL_CLOSE
)
97 if (how
->flags
& GTL_NUMCHARS
&& how
->flags
& GTL_NUMBYTES
)
100 length
= ME_GetTextLength(editor
);
102 if ((editor
->styleFlags
& ES_MULTILINE
)
103 && (how
->flags
& GTL_USECRLF
)
104 && !editor
->bEmulateVersion10
) /* Ignore GTL_USECRLF flag in 1.0 emulation */
105 length
+= editor
->nParagraphs
- 1;
107 if (how
->flags
& GTL_NUMBYTES
||
108 (how
->flags
& GTL_PRECISE
&& /* GTL_PRECISE seems to imply GTL_NUMBYTES */
109 !(how
->flags
& GTL_NUMCHARS
))) /* unless GTL_NUMCHARS is given */
113 if (how
->codepage
== 1200)
115 if (how
->flags
& GTL_PRECISE
)
116 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
117 if (GetCPInfo(how
->codepage
, &cpinfo
))
118 return length
* cpinfo
.MaxCharSize
;
119 ERR("Invalid codepage %u\n", how
->codepage
);
126 int ME_SetSelection(ME_TextEditor
*editor
, int from
, int to
)
128 int selectionEnd
= 0;
129 const int len
= ME_GetTextLength(editor
);
131 /* all negative values are effectively the same */
138 if (from
== 0 && to
== -1)
140 ME_SetCursorToStart(editor
, &editor
->pCursors
[1]);
141 ME_SetCursorToEnd(editor
, &editor
->pCursors
[0]);
142 ME_InvalidateSelection(editor
);
146 /* if both values are equal and also out of bound, that means to */
147 /* put the selection at the end of the text */
148 if ((from
== to
) && (to
< 0 || to
> len
))
154 /* if from is negative and to is positive then selection is */
155 /* deselected and caret moved to end of the current selection */
159 ME_GetSelectionOfs(editor
, &start
, &end
);
162 editor
->pCursors
[1] = editor
->pCursors
[0];
168 /* adjust to if it's a negative value */
172 /* flip from and to if they are reversed */
180 /* after fiddling with the values, we find from > len && to > len */
183 /* special case with to too big */
190 ME_SetCursorToEnd(editor
, &editor
->pCursors
[0]);
191 editor
->pCursors
[1] = editor
->pCursors
[0];
192 ME_InvalidateSelection(editor
);
196 ME_CursorFromCharOfs(editor
, from
, &editor
->pCursors
[1]);
197 editor
->pCursors
[0] = editor
->pCursors
[1];
198 ME_MoveCursorChars(editor
, &editor
->pCursors
[0], to
- from
);
199 /* Selection is not allowed in the middle of an end paragraph run. */
200 if (editor
->pCursors
[1].pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
201 editor
->pCursors
[1].nOffset
= 0;
202 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
203 editor
->pCursors
[0].nOffset
= 0;
209 ME_GetCursorCoordinates(ME_TextEditor
*editor
, ME_Cursor
*pCursor
,
210 int *x
, int *y
, int *height
)
213 ME_DisplayItem
*run
= pCursor
->pRun
;
214 ME_DisplayItem
*para
= pCursor
->pPara
;
215 ME_DisplayItem
*pSizeRun
= run
;
219 assert(height
&& x
&& y
);
220 assert(~para
->member
.para
.nFlags
& MEPF_REWRAP
);
221 assert(run
&& run
->type
== diRun
);
222 assert(para
&& para
->type
== diParagraph
);
224 row
= ME_FindItemBack(run
, diStartRowOrParagraph
);
225 assert(row
&& row
->type
== diStartRow
);
227 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
229 if (!pCursor
->nOffset
)
231 ME_DisplayItem
*prev
= ME_FindItemBack(run
, diRunOrParagraph
);
233 if (prev
->type
== diRun
)
236 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
&&
237 run
== ME_FindItemFwd(row
, diRun
))
239 ME_DisplayItem
*tmp
= ME_FindItemBack(row
, diRunOrParagraph
);
241 if (tmp
->type
== diRun
)
243 row
= ME_FindItemBack(tmp
, diStartRow
);
244 pSizeRun
= run
= tmp
;
246 assert(run
->type
== diRun
);
249 run_x
= ME_PointFromCharContext( &c
, &run
->member
.run
, pCursor
->nOffset
, TRUE
);
251 *height
= pSizeRun
->member
.run
.nAscent
+ pSizeRun
->member
.run
.nDescent
;
252 *x
= c
.rcView
.left
+ run
->member
.run
.pt
.x
+ run_x
- editor
->horz_si
.nPos
;
253 *y
= c
.rcView
.top
+ para
->member
.para
.pt
.y
+ row
->member
.row
.nBaseline
254 + run
->member
.run
.pt
.y
- pSizeRun
->member
.run
.nAscent
255 - editor
->vert_si
.nPos
;
256 ME_DestroyContext(&c
);
262 ME_MoveCaret(ME_TextEditor
*editor
)
266 ME_GetCursorCoordinates(editor
, &editor
->pCursors
[0], &x
, &y
, &height
);
267 if(editor
->bHaveFocus
&& !ME_IsSelection(editor
))
269 x
= min(x
, editor
->rcFormat
.right
-1);
270 ITextHost_TxCreateCaret(editor
->texthost
, NULL
, 0, height
);
271 ITextHost_TxSetCaretPos(editor
->texthost
, x
, y
);
276 void ME_ShowCaret(ME_TextEditor
*ed
)
279 if(ed
->bHaveFocus
&& !ME_IsSelection(ed
))
280 ITextHost_TxShowCaret(ed
->texthost
, TRUE
);
283 void ME_HideCaret(ME_TextEditor
*ed
)
285 if(!ed
->bHaveFocus
|| ME_IsSelection(ed
))
287 ITextHost_TxShowCaret(ed
->texthost
, FALSE
);
292 BOOL
ME_InternalDeleteText(ME_TextEditor
*editor
, ME_Cursor
*start
,
293 int nChars
, BOOL bForce
)
295 ME_Cursor c
= *start
;
296 int nOfs
= ME_GetCursorOfs(start
), text_len
= ME_GetTextLength( editor
);
298 int totalChars
= nChars
;
299 ME_DisplayItem
*start_para
;
300 BOOL delete_all
= FALSE
;
302 /* Prevent deletion past last end of paragraph run. */
303 nChars
= min(nChars
, text_len
- nOfs
);
304 if (nChars
== text_len
) delete_all
= TRUE
;
305 start_para
= c
.pPara
;
309 ME_ProtectPartialTableDeletion(editor
, &c
, &nChars
);
317 ME_CursorFromCharOfs(editor
, nOfs
+nChars
, &c
);
319 nOfs
+nChars
== (c
.pRun
->member
.run
.nCharOfs
320 + c
.pPara
->member
.para
.nCharOfs
))
322 /* We aren't deleting anything in this run, so we will go back to the
323 * last run we are deleting text in. */
324 ME_PrevRun(&c
.pPara
, &c
.pRun
);
325 c
.nOffset
= c
.pRun
->member
.run
.len
;
327 run
= &c
.pRun
->member
.run
;
328 if (run
->nFlags
& MERF_ENDPARA
) {
329 int eollen
= c
.pRun
->member
.run
.len
;
330 BOOL keepFirstParaFormat
;
332 if (!ME_FindItemFwd(c
.pRun
, diParagraph
))
336 keepFirstParaFormat
= (totalChars
== nChars
&& nChars
<= eollen
&&
338 if (!editor
->bEmulateVersion10
) /* v4.1 */
340 ME_DisplayItem
*next_para
= ME_FindItemFwd(c
.pRun
, diParagraphOrEnd
);
341 ME_DisplayItem
*this_para
= next_para
->member
.para
.prev_para
;
343 /* The end of paragraph before a table row is only deleted if there
344 * is nothing else on the line before it. */
345 if (this_para
== start_para
&&
346 next_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
348 /* If the paragraph will be empty, then it should be deleted, however
349 * it still might have text right now which would inherit the
350 * MEPF_STARTROW property if we joined it right now.
351 * Instead we will delete it after the preceding text is deleted. */
352 if (nOfs
> this_para
->member
.para
.nCharOfs
) {
353 /* Skip this end of line. */
354 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
357 keepFirstParaFormat
= TRUE
;
360 ME_JoinParagraphs(editor
, c
.pPara
, keepFirstParaFormat
);
361 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
362 ME_CheckCharOffsets(editor
);
363 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
369 int nCharsToDelete
= min(nChars
, c
.nOffset
);
372 c
.nOffset
-= nCharsToDelete
;
374 ME_FindItemBack(c
.pRun
, diParagraph
)->member
.para
.nFlags
|= MEPF_REWRAP
;
377 /* nChars is the number of characters that should be deleted from the
378 PRECEDING runs (these BEFORE cursor.pRun)
379 nCharsToDelete is a number of chars to delete from THIS run */
380 nChars
-= nCharsToDelete
;
381 shift
-= nCharsToDelete
;
382 TRACE("Deleting %d (remaning %d) chars at %d in %s (%d)\n",
383 nCharsToDelete
, nChars
, c
.nOffset
,
384 debugstr_run( run
), run
->len
);
386 /* nOfs is a character offset (from the start of the document
387 to the current (deleted) run */
388 add_undo_insert_run( editor
, nOfs
+ nChars
, get_text( run
, c
.nOffset
), nCharsToDelete
, run
->nFlags
, run
->style
);
390 ME_StrDeleteV(run
->para
->text
, run
->nCharOfs
+ c
.nOffset
, nCharsToDelete
);
391 run
->len
-= nCharsToDelete
;
392 TRACE("Post deletion string: %s (%d)\n", debugstr_run( run
), run
->len
);
393 TRACE("Shift value: %d\n", shift
);
395 /* update cursors (including c) */
396 for (i
=-1; i
<editor
->nCursors
; i
++) {
397 ME_Cursor
*pThisCur
= editor
->pCursors
+ i
;
398 if (i
== -1) pThisCur
= &c
;
399 if (pThisCur
->pRun
== cursor
.pRun
) {
400 if (pThisCur
->nOffset
> cursor
.nOffset
) {
401 if (pThisCur
->nOffset
-cursor
.nOffset
< nCharsToDelete
)
402 pThisCur
->nOffset
= cursor
.nOffset
;
404 pThisCur
->nOffset
-= nCharsToDelete
;
405 assert(pThisCur
->nOffset
>= 0);
406 assert(pThisCur
->nOffset
<= run
->len
);
408 if (pThisCur
->nOffset
== run
->len
)
410 pThisCur
->pRun
= ME_FindItemFwd(pThisCur
->pRun
, diRunOrParagraphOrEnd
);
411 assert(pThisCur
->pRun
->type
== diRun
);
412 pThisCur
->nOffset
= 0;
417 /* c = updated data now */
419 if (c
.pRun
== cursor
.pRun
)
420 ME_SkipAndPropagateCharOffset(c
.pRun
, shift
);
422 ME_PropagateCharOffset(c
.pRun
, shift
);
424 if (!cursor
.pRun
->member
.run
.len
)
426 TRACE("Removing empty run\n");
427 ME_Remove(cursor
.pRun
);
428 ME_DestroyDisplayItem(cursor
.pRun
);
433 ME_CheckCharOffsets(editor);
438 if (delete_all
) ME_SetDefaultParaFormat( start_para
->member
.para
.pFmt
);
442 BOOL
ME_DeleteTextAtCursor(ME_TextEditor
*editor
, int nCursor
, int nChars
)
444 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
445 /* text operations set modified state */
446 editor
->nModifyStep
= 1;
447 return ME_InternalDeleteText(editor
, &editor
->pCursors
[nCursor
],
451 static ME_DisplayItem
*
452 ME_InternalInsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
453 const WCHAR
*str
, int len
, ME_Style
*style
,
456 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
458 editor
->bCaretAtEnd
= FALSE
;
460 assert(p
->pRun
->type
== diRun
);
462 return ME_InsertRunAtCursor(editor
, p
, style
, str
, len
, flags
);
466 void ME_InsertOLEFromCursor(ME_TextEditor
*editor
, const REOBJECT
* reo
, 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 di
->member
.run
.ole_obj
= ALLOC_OBJ(*reo
);
479 ME_CopyReObject(di
->member
.run
.ole_obj
, reo
);
480 ME_ReleaseStyle(pStyle
);
484 void ME_InsertEndRowFromCursor(ME_TextEditor
*editor
, int nCursor
)
486 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
490 if (ME_IsSelection(editor
))
491 ME_DeleteSelection(editor
);
493 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
495 ME_ReleaseStyle(pStyle
);
499 void ME_InsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
500 const WCHAR
*str
, int len
, ME_Style
*style
)
506 /* FIXME really HERE ? */
507 if (ME_IsSelection(editor
))
508 ME_DeleteSelection(editor
);
510 /* FIXME: is this too slow? */
511 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
512 oldLen
= ME_GetTextLength(editor
);
514 /* text operations set modified state */
515 editor
->nModifyStep
= 1;
519 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
523 /* grow the text limit to fit our text */
524 if(editor
->nTextLimit
< oldLen
+len
)
525 editor
->nTextLimit
= oldLen
+ len
;
531 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
532 while(pos
- str
< len
&& *pos
!= '\r' && *pos
!= '\n' && *pos
!= '\t')
535 if (pos
!= str
) { /* handle text */
536 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
537 } else if (*pos
== '\t') { /* handle tabs */
539 ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, style
, MERF_TAB
);
541 } else { /* handle EOLs */
542 ME_DisplayItem
*tp
, *end_run
, *run
, *prev
;
546 /* Find number of CR and LF in end of paragraph run */
549 if (len
> 1 && pos
[1] == '\n')
551 else if (len
> 2 && pos
[1] == '\r' && pos
[2] == '\n')
556 assert(*pos
== '\n');
561 if (!editor
->bEmulateVersion10
&& eol_len
== 3)
563 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
565 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, style
, 0);
567 const WCHAR cr
= '\r', *eol_str
= str
;
569 if (!editor
->bEmulateVersion10
)
575 p
= &editor
->pCursors
[nCursor
];
577 if (p
->nOffset
== p
->pRun
->member
.run
.len
)
579 run
= ME_FindItemFwd( p
->pRun
, diRun
);
580 if (!run
) run
= p
->pRun
;
584 if (p
->nOffset
) ME_SplitRunSimple(editor
, p
);
588 tmp_style
= ME_GetInsertStyle(editor
, nCursor
);
589 /* ME_SplitParagraph increases style refcount */
590 tp
= ME_SplitParagraph(editor
, run
, run
->member
.run
.style
, eol_str
, eol_len
, 0);
592 end_run
= ME_FindItemBack(tp
, diRun
);
593 ME_ReleaseStyle(end_run
->member
.run
.style
);
594 end_run
->member
.run
.style
= tmp_style
;
596 /* Move any cursors that were at the end of the previous run to the beginning of the new para */
597 prev
= ME_FindItemBack( end_run
, diRun
);
601 for (i
= 0; i
< editor
->nCursors
; i
++)
603 if (editor
->pCursors
[i
].pRun
== prev
&&
604 editor
->pCursors
[i
].nOffset
== prev
->member
.run
.len
)
606 editor
->pCursors
[i
].pPara
= tp
;
607 editor
->pCursors
[i
].pRun
= run
;
608 editor
->pCursors
[i
].nOffset
= 0;
620 /* Move the cursor nRelOfs characters (either forwards or backwards)
622 * returns the actual number of characters moved.
624 int ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
626 cursor
->nOffset
+= nRelOfs
;
627 if (cursor
->nOffset
< 0)
629 cursor
->nOffset
+= cursor
->pRun
->member
.run
.nCharOfs
;
630 if (cursor
->nOffset
>= 0)
632 /* new offset in the same paragraph */
634 cursor
->pRun
= ME_FindItemBack(cursor
->pRun
, diRun
);
635 } while (cursor
->nOffset
< cursor
->pRun
->member
.run
.nCharOfs
);
636 cursor
->nOffset
-= cursor
->pRun
->member
.run
.nCharOfs
;
640 cursor
->nOffset
+= cursor
->pPara
->member
.para
.nCharOfs
;
641 if (cursor
->nOffset
<= 0)
643 /* moved to the start of the text */
644 nRelOfs
-= cursor
->nOffset
;
645 ME_SetCursorToStart(editor
, cursor
);
649 /* new offset in a previous paragraph */
651 cursor
->pPara
= cursor
->pPara
->member
.para
.prev_para
;
652 } while (cursor
->nOffset
< cursor
->pPara
->member
.para
.nCharOfs
);
653 cursor
->nOffset
-= cursor
->pPara
->member
.para
.nCharOfs
;
655 cursor
->pRun
= ME_FindItemBack(cursor
->pPara
->member
.para
.next_para
, diRun
);
656 while (cursor
->nOffset
< cursor
->pRun
->member
.run
.nCharOfs
) {
657 cursor
->pRun
= ME_FindItemBack(cursor
->pRun
, diRun
);
659 cursor
->nOffset
-= cursor
->pRun
->member
.run
.nCharOfs
;
660 } else if (cursor
->nOffset
>= cursor
->pRun
->member
.run
.len
) {
661 ME_DisplayItem
*next_para
;
664 new_offset
= ME_GetCursorOfs(cursor
);
665 next_para
= cursor
->pPara
->member
.para
.next_para
;
666 if (new_offset
< next_para
->member
.para
.nCharOfs
)
668 /* new offset in the same paragraph */
670 cursor
->nOffset
-= cursor
->pRun
->member
.run
.len
;
671 cursor
->pRun
= ME_FindItemFwd(cursor
->pRun
, diRun
);
672 } while (cursor
->nOffset
>= cursor
->pRun
->member
.run
.len
);
676 if (new_offset
>= ME_GetTextLength(editor
))
678 /* new offset at the end of the text */
679 ME_SetCursorToEnd(editor
, cursor
);
680 nRelOfs
-= new_offset
- ME_GetTextLength(editor
);
684 /* new offset in a following paragraph */
686 cursor
->pPara
= next_para
;
687 next_para
= next_para
->member
.para
.next_para
;
688 } while (new_offset
>= next_para
->member
.para
.nCharOfs
);
690 cursor
->nOffset
= new_offset
- cursor
->pPara
->member
.para
.nCharOfs
;
691 cursor
->pRun
= ME_FindItemFwd(cursor
->pPara
, diRun
);
692 while (cursor
->nOffset
>= cursor
->pRun
->member
.run
.len
)
694 cursor
->nOffset
-= cursor
->pRun
->member
.run
.len
;
695 cursor
->pRun
= ME_FindItemFwd(cursor
->pRun
, diRun
);
697 } /* else new offset is in the same run */
703 ME_MoveCursorWords(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
705 ME_DisplayItem
*pRun
= cursor
->pRun
, *pOtherRun
;
706 ME_DisplayItem
*pPara
= cursor
->pPara
;
707 int nOffset
= cursor
->nOffset
;
711 /* Backward movement */
714 nOffset
= ME_CallWordBreakProc(editor
, get_text( &pRun
->member
.run
, 0 ),
715 pRun
->member
.run
.len
, nOffset
, WB_MOVEWORDLEFT
);
718 pOtherRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
719 if (pOtherRun
->type
== diRun
)
721 if (ME_CallWordBreakProc(editor
, get_text( &pOtherRun
->member
.run
, 0 ),
722 pOtherRun
->member
.run
.len
,
723 pOtherRun
->member
.run
.len
- 1,
725 && !(pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
726 && !(cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
727 && !ME_CallWordBreakProc(editor
, get_text( &pRun
->member
.run
, 0 ),
728 pRun
->member
.run
.len
, 0,
732 nOffset
= pOtherRun
->member
.run
.len
;
734 else if (pOtherRun
->type
== diParagraph
)
736 if (cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
739 /* Skip empty start of table row paragraph */
740 if (pPara
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
741 pPara
= pPara
->member
.para
.prev_para
;
742 /* Paragraph breaks are treated as separate words */
743 if (pPara
->member
.para
.prev_para
->type
== diTextStart
)
746 pRun
= ME_FindItemBack(pPara
, diRun
);
747 pPara
= pPara
->member
.para
.prev_para
;
755 /* Forward movement */
756 BOOL last_delim
= FALSE
;
760 if (last_delim
&& !ME_CallWordBreakProc(editor
, get_text( &pRun
->member
.run
, 0 ),
761 pRun
->member
.run
.len
, nOffset
, WB_ISDELIMITER
))
763 nOffset
= ME_CallWordBreakProc(editor
, get_text( &pRun
->member
.run
, 0 ),
764 pRun
->member
.run
.len
, nOffset
, WB_MOVEWORDRIGHT
);
765 if (nOffset
< pRun
->member
.run
.len
)
767 pOtherRun
= ME_FindItemFwd(pRun
, diRunOrParagraphOrEnd
);
768 if (pOtherRun
->type
== diRun
)
770 last_delim
= ME_CallWordBreakProc(editor
, get_text( &pRun
->member
.run
, 0 ),
771 pRun
->member
.run
.len
, nOffset
- 1, WB_ISDELIMITER
);
775 else if (pOtherRun
->type
== diParagraph
)
777 if (pOtherRun
->member
.para
.nFlags
& MEPF_ROWSTART
)
778 pOtherRun
= pOtherRun
->member
.para
.next_para
;
779 if (cursor
->pRun
== pRun
) {
781 pRun
= ME_FindItemFwd(pPara
, diRun
);
788 if (cursor
->pRun
== pRun
)
795 cursor
->pPara
= pPara
;
797 cursor
->nOffset
= nOffset
;
803 ME_SelectByType(ME_TextEditor
*editor
, ME_SelectionType selectionType
)
805 /* pCursor[0] is the end of the selection
806 * pCursor[1] is the start of the selection (or the position selection anchor)
807 * pCursor[2] and [3] are the selection anchors that are backed up
808 * so they are kept when the selection changes for drag selection.
811 editor
->nSelectionType
= selectionType
;
812 switch(selectionType
)
817 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
818 editor
->pCursors
[1] = editor
->pCursors
[0];
819 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
824 ME_DisplayItem
*pItem
;
825 ME_DIType fwdSearchType
, backSearchType
;
826 if (selectionType
== stParagraph
) {
827 backSearchType
= diParagraph
;
828 fwdSearchType
= diParagraphOrEnd
;
830 backSearchType
= diStartRow
;
831 fwdSearchType
= diStartRowOrParagraphOrEnd
;
833 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, fwdSearchType
);
835 if (pItem
->type
== diTextEnd
)
836 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
838 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
839 editor
->pCursors
[0].pPara
= ME_GetParagraph(editor
->pCursors
[0].pRun
);
840 editor
->pCursors
[0].nOffset
= 0;
842 pItem
= ME_FindItemBack(pItem
, backSearchType
);
843 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
844 editor
->pCursors
[1].pPara
= ME_GetParagraph(editor
->pCursors
[1].pRun
);
845 editor
->pCursors
[1].nOffset
= 0;
849 /* Select everything with cursor anchored from the start of the text */
850 editor
->nSelectionType
= stDocument
;
851 ME_SetCursorToStart(editor
, &editor
->pCursors
[1]);
852 ME_SetCursorToEnd(editor
, &editor
->pCursors
[0]);
856 /* Store the anchor positions for extending the selection. */
857 editor
->pCursors
[2] = editor
->pCursors
[0];
858 editor
->pCursors
[3] = editor
->pCursors
[1];
861 int ME_GetCursorOfs(const ME_Cursor
*cursor
)
863 return cursor
->pPara
->member
.para
.nCharOfs
864 + cursor
->pRun
->member
.run
.nCharOfs
+ cursor
->nOffset
;
867 /* Helper function for ME_FindPixelPos to find paragraph within tables */
868 static ME_DisplayItem
* ME_FindPixelPosInTableRow(int x
, int y
,
869 ME_DisplayItem
*para
)
871 ME_DisplayItem
*cell
, *next_cell
;
872 assert(para
->member
.para
.nFlags
& MEPF_ROWSTART
);
873 cell
= para
->member
.para
.next_para
->member
.para
.pCell
;
876 /* find the cell we are in */
877 while ((next_cell
= cell
->member
.cell
.next_cell
) != NULL
) {
878 if (x
< next_cell
->member
.cell
.pt
.x
)
880 para
= ME_FindItemFwd(cell
, diParagraph
);
881 /* Found the cell, but there might be multiple paragraphs in
882 * the cell, so need to search down the cell for the paragraph. */
883 while (cell
== para
->member
.para
.pCell
) {
884 if (y
< para
->member
.para
.pt
.y
+ para
->member
.para
.nHeight
)
886 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
887 return ME_FindPixelPosInTableRow(x
, y
, para
);
891 para
= para
->member
.para
.next_para
;
893 /* Past the end of the cell, so go back to the last cell paragraph */
894 return para
->member
.para
.prev_para
;
898 /* Return table row delimiter */
899 para
= ME_FindItemFwd(cell
, diParagraph
);
900 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
901 assert(para
->member
.para
.pFmt
->dwMask
& PFM_TABLEROWDELIMITER
);
902 assert(para
->member
.para
.pFmt
->wEffects
& PFE_TABLEROWDELIMITER
);
906 static BOOL
ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
907 int x
, ME_Cursor
*cursor
, BOOL
*pbCaretAtEnd
)
909 ME_DisplayItem
*pNext
, *pLastRun
;
910 ME_Row
*row
= &pRow
->member
.row
;
918 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
919 assert(pNext
->type
== diRun
);
920 if (pbCaretAtEnd
) *pbCaretAtEnd
= FALSE
;
923 int run_x
= pNext
->member
.run
.pt
.x
;
924 int width
= pNext
->member
.run
.nWidth
;
926 if (x
>= run_x
&& x
< run_x
+width
)
928 cursor
->nOffset
= ME_CharFromPoint(editor
, x
-run_x
, &pNext
->member
.run
, TRUE
, TRUE
);
929 cursor
->pRun
= pNext
;
930 cursor
->pPara
= ME_GetParagraph( cursor
->pRun
);
934 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
935 } while(pNext
&& pNext
->type
== diRun
);
937 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
939 cursor
->pRun
= ME_FindItemFwd(pNext
, diRun
);
940 if (pbCaretAtEnd
) *pbCaretAtEnd
= TRUE
;
943 cursor
->pRun
= pLastRun
;
945 cursor
->pPara
= ME_GetParagraph( cursor
->pRun
);
949 /* Finds the run and offset from the pixel position.
951 * x & y are pixel positions in virtual coordinates into the rich edit control,
952 * so client coordinates must first be adjusted by the scroll position.
954 * returns TRUE if the result was exactly under the cursor, otherwise returns
955 * FALSE, and result is set to the closest position to the coordinates.
957 static BOOL
ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
,
958 ME_Cursor
*result
, BOOL
*is_eol
)
960 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
963 x
-= editor
->rcFormat
.left
;
964 y
-= editor
->rcFormat
.top
;
970 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->member
.para
.next_para
)
972 assert(p
->type
== diParagraph
);
973 if (y
< p
->member
.para
.pt
.y
+ p
->member
.para
.nHeight
)
975 if (p
->member
.para
.nFlags
& MEPF_ROWSTART
)
976 p
= ME_FindPixelPosInTableRow(x
, y
, p
);
977 y
-= p
->member
.para
.pt
.y
;
978 p
= ME_FindItemFwd(p
, diStartRow
);
980 } else if (p
->member
.para
.nFlags
& MEPF_ROWSTART
) {
981 p
= ME_GetTableRowEnd(p
);
985 for (; p
!= editor
->pBuffer
->pLast
; )
988 assert(p
->type
== diStartRow
);
989 if (y
< p
->member
.row
.pt
.y
+ p
->member
.row
.nHeight
) break;
990 pp
= ME_FindItemFwd(p
, diStartRow
);
994 if (p
== editor
->pBuffer
->pLast
)
996 /* The position is below the last paragraph, so the last row will be used
997 * rather than the end of the text, so the x position will be used to
998 * determine the offset closest to the pixel position. */
1000 p
= ME_FindItemBack(p
, diStartRow
);
1001 if (!p
) p
= editor
->pBuffer
->pLast
;
1004 assert( p
->type
== diStartRow
|| p
== editor
->pBuffer
->pLast
);
1006 if( p
->type
== diStartRow
)
1007 return ME_FindRunInRow( editor
, p
, x
, result
, is_eol
) && isExact
;
1009 result
->pRun
= ME_FindItemBack(p
, diRun
);
1010 result
->pPara
= ME_GetParagraph(result
->pRun
);
1011 result
->nOffset
= 0;
1012 assert(result
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1017 /* Sets the cursor to the position closest to the pixel position
1019 * x & y are pixel positions in client coordinates.
1021 * isExact will be set to TRUE if the run is directly under the pixel
1022 * position, FALSE if it not, unless isExact is set to NULL.
1024 * return FALSE if outside client area and the cursor is not set,
1025 * otherwise TRUE is returned.
1027 BOOL
ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
,
1028 ME_Cursor
*cursor
, BOOL
*isExact
)
1033 ITextHost_TxGetClientRect(editor
->texthost
, &rc
);
1034 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
) {
1035 if (isExact
) *isExact
= FALSE
;
1038 x
+= editor
->horz_si
.nPos
;
1039 y
+= editor
->vert_si
.nPos
;
1040 bResult
= ME_FindPixelPos(editor
, x
, y
, cursor
, NULL
);
1041 if (isExact
) *isExact
= bResult
;
1047 /* Extends the selection with a word, line, or paragraph selection type.
1049 * The selection is anchored by editor->pCursors[2-3] such that the text
1050 * between the anchors will remain selected, and one end will be extended.
1052 * editor->pCursors[0] should have the position to extend the selection to
1053 * before this function is called.
1055 * Nothing will be done if editor->nSelectionType equals stPosition.
1057 static void ME_ExtendAnchorSelection(ME_TextEditor
*editor
)
1059 ME_Cursor tmp_cursor
;
1060 int curOfs
, anchorStartOfs
, anchorEndOfs
;
1061 if (editor
->nSelectionType
== stPosition
|| editor
->nSelectionType
== stDocument
)
1063 curOfs
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1064 anchorStartOfs
= ME_GetCursorOfs(&editor
->pCursors
[3]);
1065 anchorEndOfs
= ME_GetCursorOfs(&editor
->pCursors
[2]);
1067 tmp_cursor
= editor
->pCursors
[0];
1068 editor
->pCursors
[0] = editor
->pCursors
[2];
1069 editor
->pCursors
[1] = editor
->pCursors
[3];
1070 if (curOfs
< anchorStartOfs
)
1072 /* Extend the left side of selection */
1073 editor
->pCursors
[1] = tmp_cursor
;
1074 if (editor
->nSelectionType
== stWord
)
1075 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
1078 ME_DisplayItem
*pItem
;
1079 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1080 diStartRowOrParagraph
:diParagraph
);
1081 pItem
= ME_FindItemBack(editor
->pCursors
[1].pRun
, searchType
);
1082 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
1083 editor
->pCursors
[1].pPara
= ME_GetParagraph(editor
->pCursors
[1].pRun
);
1084 editor
->pCursors
[1].nOffset
= 0;
1087 else if (curOfs
>= anchorEndOfs
)
1089 /* Extend the right side of selection */
1090 editor
->pCursors
[0] = tmp_cursor
;
1091 if (editor
->nSelectionType
== stWord
)
1092 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
1095 ME_DisplayItem
*pItem
;
1096 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1097 diStartRowOrParagraphOrEnd
:diParagraphOrEnd
);
1098 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, searchType
);
1099 if (pItem
->type
== diTextEnd
)
1100 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
1102 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
1103 editor
->pCursors
[0].pPara
= ME_GetParagraph(editor
->pCursors
[0].pRun
);
1104 editor
->pCursors
[0].nOffset
= 0;
1109 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
, int clickNum
)
1111 ME_Cursor tmp_cursor
;
1112 BOOL is_selection
= FALSE
, is_shift
;
1114 editor
->nUDArrowX
= -1;
1116 x
+= editor
->horz_si
.nPos
;
1117 y
+= editor
->vert_si
.nPos
;
1119 tmp_cursor
= editor
->pCursors
[0];
1120 is_selection
= ME_IsSelection(editor
);
1121 is_shift
= GetKeyState(VK_SHIFT
) < 0;
1123 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
1125 if (x
>= editor
->rcFormat
.left
|| is_shift
)
1129 editor
->pCursors
[1] = editor
->pCursors
[0];
1131 if (x
>= editor
->rcFormat
.left
)
1132 ME_SelectByType(editor
, stWord
);
1134 ME_SelectByType(editor
, stParagraph
);
1135 } else if (clickNum
% 2 == 0) {
1136 ME_SelectByType(editor
, stWord
);
1138 ME_SelectByType(editor
, stParagraph
);
1143 editor
->nSelectionType
= stPosition
;
1144 editor
->pCursors
[1] = editor
->pCursors
[0];
1146 else if (!is_selection
)
1148 editor
->nSelectionType
= stPosition
;
1149 editor
->pCursors
[1] = tmp_cursor
;
1151 else if (editor
->nSelectionType
!= stPosition
)
1153 ME_ExtendAnchorSelection(editor
);
1159 ME_SelectByType(editor
, stLine
);
1160 } else if (clickNum
% 2 == 0 || is_shift
) {
1161 ME_SelectByType(editor
, stParagraph
);
1163 ME_SelectByType(editor
, stDocument
);
1166 ME_InvalidateSelection(editor
);
1167 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1168 ME_ShowCaret(editor
);
1169 ME_SendSelChange(editor
);
1172 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
1174 ME_Cursor tmp_cursor
;
1176 if (editor
->nSelectionType
== stDocument
)
1178 x
+= editor
->horz_si
.nPos
;
1179 y
+= editor
->vert_si
.nPos
;
1181 tmp_cursor
= editor
->pCursors
[0];
1182 /* FIXME: do something with the return value of ME_FindPixelPos */
1183 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
1185 ME_InvalidateSelection(editor
);
1186 editor
->pCursors
[0] = tmp_cursor
;
1187 ME_ExtendAnchorSelection(editor
);
1189 if (editor
->nSelectionType
!= stPosition
&&
1190 memcmp(&editor
->pCursors
[1], &editor
->pCursors
[3], sizeof(ME_Cursor
)))
1192 /* The scroll the cursor towards the other end, since it was the one
1193 * extended by ME_ExtendAnchorSelection */
1194 ME_EnsureVisible(editor
, &editor
->pCursors
[1]);
1196 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
1199 ME_InvalidateSelection(editor
);
1200 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1201 ME_ShowCaret(editor
);
1202 ME_SendSelChange(editor
);
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
, TRUE
);
1223 editor
->nUDArrowX
= x
;
1230 ME_MoveCursorLines(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
1232 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1233 ME_DisplayItem
*pOldPara
= pCursor
->pPara
;
1234 ME_DisplayItem
*pItem
, *pNewPara
;
1235 int x
= ME_GetXForArrow(editor
, pCursor
);
1237 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1238 if (!ME_PrevRun(&pOldPara
, &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 ME_FindRunInRow(editor
, pItem
, x
, pCursor
, &editor
->bCaretAtEnd
);
1304 assert(pCursor
->pRun
);
1305 assert(pCursor
->pRun
->type
== diRun
);
1308 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1310 ME_DisplayItem
*p
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diStartRow
);
1312 if (editor
->vert_si
.nPos
< p
->member
.row
.nHeight
)
1314 ME_SetCursorToStart(editor
, pCursor
);
1315 editor
->bCaretAtEnd
= FALSE
;
1316 /* Native clears seems to clear this x value on page up at the top
1317 * of the text, but not on page down at the end of the text.
1318 * Doesn't make sense, but we try to be bug for bug compatible. */
1319 editor
->nUDArrowX
= -1;
1321 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1322 ME_DisplayItem
*pLast
;
1324 int yOldScrollPos
= editor
->vert_si
.nPos
;
1326 x
= ME_GetXForArrow(editor
, pCursor
);
1327 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1328 pRun
= ME_FindItemBack(pRun
, diRun
);
1330 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1331 assert(p
->type
== diStartRow
);
1332 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1333 y
= yp
+ p
->member
.row
.pt
.y
;
1335 ME_ScrollUp(editor
, editor
->sizeWindow
.cy
);
1336 /* Only move the cursor by the amount scrolled. */
1337 yd
= y
+ editor
->vert_si
.nPos
- yOldScrollPos
;
1341 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
1344 if (p
->type
== diParagraph
) { /* crossing paragraphs */
1345 if (p
->member
.para
.prev_para
== NULL
)
1347 yp
= p
->member
.para
.prev_para
->member
.para
.pt
.y
;
1350 y
= yp
+ p
->member
.row
.pt
.y
;
1356 ME_FindRunInRow(editor
, pLast
, x
, pCursor
, &editor
->bCaretAtEnd
);
1358 assert(pCursor
->pRun
);
1359 assert(pCursor
->pRun
->type
== diRun
);
1362 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1364 ME_DisplayItem
*pLast
;
1367 /* Find y position of the last row */
1368 pLast
= editor
->pBuffer
->pLast
;
1369 y
= pLast
->member
.para
.prev_para
->member
.para
.pt
.y
1370 + ME_FindItemBack(pLast
, diStartRow
)->member
.row
.pt
.y
;
1372 x
= ME_GetXForArrow(editor
, pCursor
);
1374 if (editor
->vert_si
.nPos
>= y
- editor
->sizeWindow
.cy
)
1376 ME_SetCursorToEnd(editor
, pCursor
);
1377 editor
->bCaretAtEnd
= FALSE
;
1379 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1382 int yOldScrollPos
= editor
->vert_si
.nPos
;
1384 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1385 pRun
= ME_FindItemBack(pRun
, diRun
);
1387 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1388 assert(p
->type
== diStartRow
);
1389 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1390 y
= yp
+ p
->member
.row
.pt
.y
;
1392 /* For native richedit controls:
1393 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1394 * v4.1 can scroll past this position here. */
1395 ME_ScrollDown(editor
, editor
->sizeWindow
.cy
);
1396 /* Only move the cursor by the amount scrolled. */
1397 yd
= y
+ editor
->vert_si
.nPos
- yOldScrollPos
;
1401 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
1404 if (p
->type
== diParagraph
) {
1405 yp
= p
->member
.para
.pt
.y
;
1408 y
= yp
+ p
->member
.row
.pt
.y
;
1414 ME_FindRunInRow(editor
, pLast
, x
, pCursor
, &editor
->bCaretAtEnd
);
1416 assert(pCursor
->pRun
);
1417 assert(pCursor
->pRun
->type
== diRun
);
1420 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1422 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
1424 ME_DisplayItem
*pRun
;
1425 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
1426 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1430 pRun
= ME_FindItemFwd(pRow
, diRun
);
1432 pCursor
->pRun
= pRun
;
1433 assert(pCursor
->pPara
== ME_GetParagraph(pRun
));
1434 pCursor
->nOffset
= 0;
1437 editor
->bCaretAtEnd
= FALSE
;
1440 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1442 ME_SetCursorToStart(editor
, pCursor
);
1443 editor
->bCaretAtEnd
= FALSE
;
1446 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1448 ME_DisplayItem
*pRow
;
1450 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1453 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1455 if (pRow
->type
== diStartRow
) {
1456 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1458 pCursor
->pRun
= pRun
;
1459 assert(pCursor
->pPara
== ME_GetParagraph(pCursor
->pRun
));
1460 pCursor
->nOffset
= 0;
1461 editor
->bCaretAtEnd
= TRUE
;
1464 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1465 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1466 assert(pCursor
->pPara
== ME_GetParagraph(pCursor
->pRun
));
1467 pCursor
->nOffset
= 0;
1468 editor
->bCaretAtEnd
= FALSE
;
1471 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1473 ME_SetCursorToEnd(editor
, pCursor
);
1474 editor
->bCaretAtEnd
= FALSE
;
1477 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1479 return editor
->pCursors
[0].pRun
!= editor
->pCursors
[1].pRun
||
1480 editor
->pCursors
[0].nOffset
!= editor
->pCursors
[1].nOffset
;
1483 void ME_DeleteSelection(ME_TextEditor
*editor
)
1486 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
1487 int nEndCursor
= nStartCursor
^ 1;
1488 ME_DeleteTextAtCursor(editor
, nStartCursor
, to
- from
);
1489 editor
->pCursors
[nEndCursor
] = editor
->pCursors
[nStartCursor
];
1492 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1494 return ME_GetInsertStyle(editor
, 0);
1497 void ME_SendSelChange(ME_TextEditor
*editor
)
1501 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1504 sc
.nmhdr
.hwndFrom
= NULL
;
1505 sc
.nmhdr
.idFrom
= 0;
1506 sc
.nmhdr
.code
= EN_SELCHANGE
;
1507 ME_GetSelectionOfs(editor
, &sc
.chrg
.cpMin
, &sc
.chrg
.cpMax
);
1508 sc
.seltyp
= SEL_EMPTY
;
1509 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1510 sc
.seltyp
|= SEL_TEXT
;
1511 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* what were RICHEDIT authors thinking ? */
1512 sc
.seltyp
|= SEL_MULTICHAR
;
1513 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1514 sc
.chrg
.cpMin
, sc
.chrg
.cpMax
, sc
.seltyp
,
1515 (sc
.seltyp
& SEL_TEXT
) ? "SEL_TEXT" : "",
1516 (sc
.seltyp
& SEL_MULTICHAR
) ? "SEL_MULTICHAR" : "");
1517 if (sc
.chrg
.cpMin
!= editor
->notified_cr
.cpMin
|| sc
.chrg
.cpMax
!= editor
->notified_cr
.cpMax
)
1519 ME_ClearTempStyle(editor
);
1521 editor
->notified_cr
= sc
.chrg
;
1522 ITextHost_TxNotify(editor
->texthost
, sc
.nmhdr
.code
, &sc
);
1527 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1530 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1531 ME_Cursor tmp_curs
= *p
;
1532 BOOL success
= FALSE
;
1534 ME_CheckCharOffsets(editor
);
1537 editor
->bCaretAtEnd
= FALSE
;
1539 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1541 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1544 editor
->bCaretAtEnd
= FALSE
;
1546 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1548 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1551 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1554 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1557 ME_ArrowPageUp(editor
, &tmp_curs
);
1560 ME_ArrowPageDown(editor
, &tmp_curs
);
1564 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1566 ME_ArrowHome(editor
, &tmp_curs
);
1567 editor
->bCaretAtEnd
= FALSE
;
1572 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1574 ME_ArrowEnd(editor
, &tmp_curs
);
1579 editor
->pCursors
[1] = tmp_curs
;
1582 ME_InvalidateSelection(editor
);
1584 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1585 ME_EnsureVisible(editor
, &tmp_curs
);
1586 ME_ShowCaret(editor
);
1587 ME_SendSelChange(editor
);