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
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
26 void ME_SetCursorToStart(ME_TextEditor
*editor
, ME_Cursor
*cursor
)
28 cursor
->pPara
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
29 cursor
->pRun
= ME_FindItemFwd(cursor
->pPara
, diRun
);
33 static void ME_SetCursorToEnd(ME_TextEditor
*editor
, ME_Cursor
*cursor
)
35 cursor
->pPara
= editor
->pBuffer
->pLast
->member
.para
.prev_para
;
36 cursor
->pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
41 int ME_GetSelectionOfs(ME_TextEditor
*editor
, int *from
, int *to
)
43 *from
= ME_GetCursorOfs(&editor
->pCursors
[0]);
44 *to
= ME_GetCursorOfs(&editor
->pCursors
[1]);
56 int ME_GetSelection(ME_TextEditor
*editor
, ME_Cursor
**from
, ME_Cursor
**to
)
58 if (ME_GetCursorOfs(&editor
->pCursors
[0]) < ME_GetCursorOfs(&editor
->pCursors
[1]))
60 *from
= &editor
->pCursors
[0];
61 *to
= &editor
->pCursors
[1];
64 *from
= &editor
->pCursors
[1];
65 *to
= &editor
->pCursors
[0];
70 int ME_GetTextLength(ME_TextEditor
*editor
)
73 ME_SetCursorToEnd(editor
, &cursor
);
74 return ME_GetCursorOfs(&cursor
);
78 int ME_GetTextLengthEx(ME_TextEditor
*editor
, const GETTEXTLENGTHEX
*how
)
82 if (how
->flags
& GTL_PRECISE
&& how
->flags
& GTL_CLOSE
)
84 if (how
->flags
& GTL_NUMCHARS
&& how
->flags
& GTL_NUMBYTES
)
87 length
= ME_GetTextLength(editor
);
89 if ((editor
->styleFlags
& ES_MULTILINE
)
90 && (how
->flags
& GTL_USECRLF
)
91 && !editor
->bEmulateVersion10
) /* Ignore GTL_USECRLF flag in 1.0 emulation */
92 length
+= editor
->nParagraphs
- 1;
94 if (how
->flags
& GTL_NUMBYTES
||
95 (how
->flags
& GTL_PRECISE
&& /* GTL_PRECISE seems to imply GTL_NUMBYTES */
96 !(how
->flags
& GTL_NUMCHARS
))) /* unless GTL_NUMCHARS is given */
100 if (how
->codepage
== 1200)
102 if (how
->flags
& GTL_PRECISE
)
103 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
104 if (GetCPInfo(how
->codepage
, &cpinfo
))
105 return length
* cpinfo
.MaxCharSize
;
106 ERR("Invalid codepage %u\n", how
->codepage
);
113 int ME_SetSelection(ME_TextEditor
*editor
, int from
, int to
)
115 int selectionEnd
= 0;
116 const int len
= ME_GetTextLength(editor
);
118 /* all negative values are effectively the same */
125 if (from
== 0 && to
== -1)
127 ME_SetCursorToStart(editor
, &editor
->pCursors
[1]);
128 ME_SetCursorToEnd(editor
, &editor
->pCursors
[0]);
129 ME_InvalidateSelection(editor
);
130 ME_ClearTempStyle(editor
);
134 /* if both values are equal and also out of bound, that means to */
135 /* put the selection at the end of the text */
136 if ((from
== to
) && (to
< 0 || to
> len
))
142 /* if from is negative and to is positive then selection is */
143 /* deselected and caret moved to end of the current selection */
147 ME_GetSelectionOfs(editor
, &start
, &end
);
150 editor
->pCursors
[1] = editor
->pCursors
[0];
153 ME_ClearTempStyle(editor
);
157 /* adjust to if it's a negative value */
161 /* flip from and to if they are reversed */
169 /* after fiddling with the values, we find from > len && to > len */
172 /* special case with to too big */
179 ME_SetCursorToEnd(editor
, &editor
->pCursors
[0]);
180 editor
->pCursors
[1] = editor
->pCursors
[0];
181 ME_InvalidateSelection(editor
);
182 ME_ClearTempStyle(editor
);
186 ME_CursorFromCharOfs(editor
, from
, &editor
->pCursors
[1]);
187 editor
->pCursors
[0] = editor
->pCursors
[1];
188 ME_MoveCursorChars(editor
, &editor
->pCursors
[0], to
- from
);
189 /* Selection is not allowed in the middle of an end paragraph run. */
190 if (editor
->pCursors
[1].pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
191 editor
->pCursors
[1].nOffset
= 0;
192 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
193 editor
->pCursors
[0].nOffset
= 0;
199 ME_GetCursorCoordinates(ME_TextEditor
*editor
, ME_Cursor
*pCursor
,
200 int *x
, int *y
, int *height
)
203 ME_DisplayItem
*run
= pCursor
->pRun
;
204 ME_DisplayItem
*para
= pCursor
->pPara
;
205 ME_DisplayItem
*pSizeRun
= run
;
209 assert(height
&& x
&& y
);
210 assert(~para
->member
.para
.nFlags
& MEPF_REWRAP
);
211 assert(run
&& run
->type
== diRun
);
212 assert(para
&& para
->type
== diParagraph
);
214 row
= ME_FindItemBack(run
, diStartRowOrParagraph
);
215 assert(row
&& row
->type
== diStartRow
);
217 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
219 if (!pCursor
->nOffset
)
221 ME_DisplayItem
*prev
= ME_FindItemBack(run
, diRunOrParagraph
);
223 if (prev
->type
== diRun
)
226 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
&&
227 run
== ME_FindItemFwd(row
, diRun
))
229 ME_DisplayItem
*tmp
= ME_FindItemBack(row
, diRunOrParagraph
);
231 if (tmp
->type
== diRun
)
233 row
= ME_FindItemBack(tmp
, diStartRow
);
234 pSizeRun
= run
= tmp
;
236 assert(run
->type
== diRun
);
239 run_x
= ME_PointFromCharContext( &c
, &run
->member
.run
, pCursor
->nOffset
, TRUE
);
241 *height
= pSizeRun
->member
.run
.nAscent
+ pSizeRun
->member
.run
.nDescent
;
242 *x
= c
.rcView
.left
+ run
->member
.run
.pt
.x
+ run_x
- editor
->horz_si
.nPos
;
243 *y
= c
.rcView
.top
+ para
->member
.para
.pt
.y
+ row
->member
.row
.nBaseline
244 + run
->member
.run
.pt
.y
- pSizeRun
->member
.run
.nAscent
245 - editor
->vert_si
.nPos
;
246 ME_DestroyContext(&c
);
252 ME_MoveCaret(ME_TextEditor
*editor
)
256 ME_GetCursorCoordinates(editor
, &editor
->pCursors
[0], &x
, &y
, &height
);
257 if(editor
->bHaveFocus
&& !ME_IsSelection(editor
))
259 x
= min(x
, editor
->rcFormat
.right
-1);
260 ITextHost_TxCreateCaret(editor
->texthost
, NULL
, 0, height
);
261 ITextHost_TxSetCaretPos(editor
->texthost
, x
, y
);
266 void ME_ShowCaret(ME_TextEditor
*ed
)
269 if(ed
->bHaveFocus
&& !ME_IsSelection(ed
))
270 ITextHost_TxShowCaret(ed
->texthost
, TRUE
);
273 void ME_HideCaret(ME_TextEditor
*ed
)
275 if(!ed
->bHaveFocus
|| ME_IsSelection(ed
))
277 ITextHost_TxShowCaret(ed
->texthost
, FALSE
);
282 BOOL
ME_InternalDeleteText(ME_TextEditor
*editor
, ME_Cursor
*start
,
283 int nChars
, BOOL bForce
)
285 ME_Cursor c
= *start
;
286 int nOfs
= ME_GetCursorOfs(start
);
288 int totalChars
= nChars
;
289 ME_DisplayItem
*start_para
;
291 /* Prevent deletion past last end of paragraph run. */
292 nChars
= min(nChars
, ME_GetTextLength(editor
) - nOfs
);
293 start_para
= c
.pPara
;
297 ME_ProtectPartialTableDeletion(editor
, &c
, &nChars
);
305 ME_CursorFromCharOfs(editor
, nOfs
+nChars
, &c
);
307 nOfs
+nChars
== (c
.pRun
->member
.run
.nCharOfs
308 + c
.pPara
->member
.para
.nCharOfs
))
310 /* We aren't deleting anything in this run, so we will go back to the
311 * last run we are deleting text in. */
312 ME_PrevRun(&c
.pPara
, &c
.pRun
);
313 c
.nOffset
= c
.pRun
->member
.run
.len
;
315 run
= &c
.pRun
->member
.run
;
316 if (run
->nFlags
& MERF_ENDPARA
) {
317 int eollen
= c
.pRun
->member
.run
.len
;
318 BOOL keepFirstParaFormat
;
320 if (!ME_FindItemFwd(c
.pRun
, diParagraph
))
324 keepFirstParaFormat
= (totalChars
== nChars
&& nChars
<= eollen
&&
326 if (!editor
->bEmulateVersion10
) /* v4.1 */
328 ME_DisplayItem
*next_para
= ME_FindItemFwd(c
.pRun
, diParagraphOrEnd
);
329 ME_DisplayItem
*this_para
= next_para
->member
.para
.prev_para
;
331 /* The end of paragraph before a table row is only deleted if there
332 * is nothing else on the line before it. */
333 if (this_para
== start_para
&&
334 next_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
336 /* If the paragraph will be empty, then it should be deleted, however
337 * it still might have text right now which would inherit the
338 * MEPF_STARTROW property if we joined it right now.
339 * Instead we will delete it after the preceding text is deleted. */
340 if (nOfs
> this_para
->member
.para
.nCharOfs
) {
341 /* Skip this end of line. */
342 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
345 keepFirstParaFormat
= TRUE
;
348 ME_JoinParagraphs(editor
, c
.pPara
, keepFirstParaFormat
);
349 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
350 ME_CheckCharOffsets(editor
);
351 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
357 int nCharsToDelete
= min(nChars
, c
.nOffset
);
360 c
.nOffset
-= nCharsToDelete
;
362 ME_FindItemBack(c
.pRun
, diParagraph
)->member
.para
.nFlags
|= MEPF_REWRAP
;
365 /* nChars is the number of characters that should be deleted from the
366 PRECEDING runs (these BEFORE cursor.pRun)
367 nCharsToDelete is a number of chars to delete from THIS run */
368 nChars
-= nCharsToDelete
;
369 shift
-= nCharsToDelete
;
370 TRACE("Deleting %d (remaning %d) chars at %d in %s (%d)\n",
371 nCharsToDelete
, nChars
, c
.nOffset
,
372 debugstr_run( run
), run
->len
);
374 /* nOfs is a character offset (from the start of the document
375 to the current (deleted) run */
376 add_undo_insert_run( editor
, nOfs
+ nChars
, get_text( run
, c
.nOffset
), nCharsToDelete
, run
->nFlags
, run
->style
);
378 ME_StrDeleteV(run
->para
->text
, run
->nCharOfs
+ c
.nOffset
, nCharsToDelete
);
379 run
->len
-= nCharsToDelete
;
380 TRACE("Post deletion string: %s (%d)\n", debugstr_run( run
), run
->len
);
381 TRACE("Shift value: %d\n", shift
);
383 /* update cursors (including c) */
384 for (i
=-1; i
<editor
->nCursors
; i
++) {
385 ME_Cursor
*pThisCur
= editor
->pCursors
+ i
;
386 if (i
== -1) pThisCur
= &c
;
387 if (pThisCur
->pRun
== cursor
.pRun
) {
388 if (pThisCur
->nOffset
> cursor
.nOffset
) {
389 if (pThisCur
->nOffset
-cursor
.nOffset
< nCharsToDelete
)
390 pThisCur
->nOffset
= cursor
.nOffset
;
392 pThisCur
->nOffset
-= nCharsToDelete
;
393 assert(pThisCur
->nOffset
>= 0);
394 assert(pThisCur
->nOffset
<= run
->len
);
396 if (pThisCur
->nOffset
== run
->len
)
398 pThisCur
->pRun
= ME_FindItemFwd(pThisCur
->pRun
, diRunOrParagraphOrEnd
);
399 assert(pThisCur
->pRun
->type
== diRun
);
400 pThisCur
->nOffset
= 0;
405 /* c = updated data now */
407 if (c
.pRun
== cursor
.pRun
)
408 ME_SkipAndPropagateCharOffset(c
.pRun
, shift
);
410 ME_PropagateCharOffset(c
.pRun
, shift
);
412 if (!cursor
.pRun
->member
.run
.len
)
414 TRACE("Removing empty run\n");
415 ME_Remove(cursor
.pRun
);
416 ME_DestroyDisplayItem(cursor
.pRun
);
421 ME_CheckCharOffsets(editor);
429 BOOL
ME_DeleteTextAtCursor(ME_TextEditor
*editor
, int nCursor
, int nChars
)
431 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
432 /* text operations set modified state */
433 editor
->nModifyStep
= 1;
434 return ME_InternalDeleteText(editor
, &editor
->pCursors
[nCursor
],
438 static ME_DisplayItem
*
439 ME_InternalInsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
440 const WCHAR
*str
, int len
, ME_Style
*style
,
443 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
445 editor
->bCaretAtEnd
= FALSE
;
447 assert(p
->pRun
->type
== diRun
);
449 return ME_InsertRunAtCursor(editor
, p
, style
, str
, len
, flags
);
453 void ME_InsertOLEFromCursor(ME_TextEditor
*editor
, const REOBJECT
* reo
, int nCursor
)
455 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
460 if (ME_IsSelection(editor
))
461 ME_DeleteSelection(editor
);
463 di
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
465 di
->member
.run
.ole_obj
= ALLOC_OBJ(*reo
);
466 ME_CopyReObject(di
->member
.run
.ole_obj
, reo
);
467 ME_ReleaseStyle(pStyle
);
471 void ME_InsertEndRowFromCursor(ME_TextEditor
*editor
, int nCursor
)
473 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
477 if (ME_IsSelection(editor
))
478 ME_DeleteSelection(editor
);
480 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
482 ME_ReleaseStyle(pStyle
);
486 void ME_InsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
487 const WCHAR
*str
, int len
, ME_Style
*style
)
493 /* FIXME really HERE ? */
494 if (ME_IsSelection(editor
))
495 ME_DeleteSelection(editor
);
497 /* FIXME: is this too slow? */
498 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
499 oldLen
= ME_GetTextLength(editor
);
501 /* text operations set modified state */
502 editor
->nModifyStep
= 1;
506 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
510 /* grow the text limit to fit our text */
511 if(editor
->nTextLimit
< oldLen
+len
)
512 editor
->nTextLimit
= oldLen
+ len
;
518 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
519 while(pos
- str
< len
&& *pos
!= '\r' && *pos
!= '\n' && *pos
!= '\t')
522 if (pos
!= str
) { /* handle text */
523 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
524 } else if (*pos
== '\t') { /* handle tabs */
526 ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, style
, MERF_TAB
);
528 } else { /* handle EOLs */
529 ME_DisplayItem
*tp
, *end_run
;
533 /* Find number of CR and LF in end of paragraph run */
536 if (len
> 1 && pos
[1] == '\n')
538 else if (len
> 2 && pos
[1] == '\r' && pos
[2] == '\n')
543 assert(*pos
== '\n');
548 if (!editor
->bEmulateVersion10
&& eol_len
== 3)
550 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
552 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, style
, 0);
554 const WCHAR cr
= '\r', *eol_str
= str
;
556 if (!editor
->bEmulateVersion10
)
562 p
= &editor
->pCursors
[nCursor
];
564 ME_SplitRunSimple(editor
, p
);
565 tmp_style
= ME_GetInsertStyle(editor
, nCursor
);
566 /* ME_SplitParagraph increases style refcount */
567 tp
= ME_SplitParagraph(editor
, p
->pRun
, p
->pRun
->member
.run
.style
, eol_str
, eol_len
, 0);
568 p
->pRun
= ME_FindItemFwd(tp
, diRun
);
570 end_run
= ME_FindItemBack(tp
, diRun
);
571 ME_ReleaseStyle(end_run
->member
.run
.style
);
572 end_run
->member
.run
.style
= tmp_style
;
581 /* Move the cursor nRelOfs characters (either forwards or backwards)
583 * returns the actual number of characters moved.
585 int ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
587 cursor
->nOffset
+= nRelOfs
;
588 if (cursor
->nOffset
< 0)
590 cursor
->nOffset
+= cursor
->pRun
->member
.run
.nCharOfs
;
591 if (cursor
->nOffset
>= 0)
593 /* new offset in the same paragraph */
595 cursor
->pRun
= ME_FindItemBack(cursor
->pRun
, diRun
);
596 } while (cursor
->nOffset
< cursor
->pRun
->member
.run
.nCharOfs
);
597 cursor
->nOffset
-= cursor
->pRun
->member
.run
.nCharOfs
;
601 cursor
->nOffset
+= cursor
->pPara
->member
.para
.nCharOfs
;
602 if (cursor
->nOffset
<= 0)
604 /* moved to the start of the text */
605 nRelOfs
-= cursor
->nOffset
;
606 ME_SetCursorToStart(editor
, cursor
);
610 /* new offset in a previous paragraph */
612 cursor
->pPara
= cursor
->pPara
->member
.para
.prev_para
;
613 } while (cursor
->nOffset
< cursor
->pPara
->member
.para
.nCharOfs
);
614 cursor
->nOffset
-= cursor
->pPara
->member
.para
.nCharOfs
;
616 cursor
->pRun
= ME_FindItemBack(cursor
->pPara
->member
.para
.next_para
, diRun
);
617 while (cursor
->nOffset
< cursor
->pRun
->member
.run
.nCharOfs
) {
618 cursor
->pRun
= ME_FindItemBack(cursor
->pRun
, diRun
);
620 cursor
->nOffset
-= cursor
->pRun
->member
.run
.nCharOfs
;
621 } else if (cursor
->nOffset
>= cursor
->pRun
->member
.run
.len
) {
622 ME_DisplayItem
*next_para
;
625 new_offset
= ME_GetCursorOfs(cursor
);
626 next_para
= cursor
->pPara
->member
.para
.next_para
;
627 if (new_offset
< next_para
->member
.para
.nCharOfs
)
629 /* new offset in the same paragraph */
631 cursor
->nOffset
-= cursor
->pRun
->member
.run
.len
;
632 cursor
->pRun
= ME_FindItemFwd(cursor
->pRun
, diRun
);
633 } while (cursor
->nOffset
>= cursor
->pRun
->member
.run
.len
);
637 if (new_offset
>= ME_GetTextLength(editor
))
639 /* new offset at the end of the text */
640 ME_SetCursorToEnd(editor
, cursor
);
641 nRelOfs
-= new_offset
- ME_GetTextLength(editor
);
645 /* new offset in a following paragraph */
647 cursor
->pPara
= next_para
;
648 next_para
= next_para
->member
.para
.next_para
;
649 } while (new_offset
>= next_para
->member
.para
.nCharOfs
);
651 cursor
->nOffset
= new_offset
- cursor
->pPara
->member
.para
.nCharOfs
;
652 cursor
->pRun
= ME_FindItemFwd(cursor
->pPara
, diRun
);
653 while (cursor
->nOffset
>= cursor
->pRun
->member
.run
.len
)
655 cursor
->nOffset
-= cursor
->pRun
->member
.run
.len
;
656 cursor
->pRun
= ME_FindItemFwd(cursor
->pRun
, diRun
);
658 } /* else new offset is in the same run */
664 ME_MoveCursorWords(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
666 ME_DisplayItem
*pRun
= cursor
->pRun
, *pOtherRun
;
667 ME_DisplayItem
*pPara
= cursor
->pPara
;
668 int nOffset
= cursor
->nOffset
;
672 /* Backward movement */
675 nOffset
= ME_CallWordBreakProc(editor
, get_text( &pRun
->member
.run
, 0 ),
676 pRun
->member
.run
.len
, nOffset
, WB_MOVEWORDLEFT
);
679 pOtherRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
680 if (pOtherRun
->type
== diRun
)
682 if (ME_CallWordBreakProc(editor
, get_text( &pOtherRun
->member
.run
, 0 ),
683 pOtherRun
->member
.run
.len
,
684 pOtherRun
->member
.run
.len
- 1,
686 && !(pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
687 && !(cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
688 && !ME_CallWordBreakProc(editor
, get_text( &pRun
->member
.run
, 0 ),
689 pRun
->member
.run
.len
, 0,
693 nOffset
= pOtherRun
->member
.run
.len
;
695 else if (pOtherRun
->type
== diParagraph
)
697 if (cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
700 /* Skip empty start of table row paragraph */
701 if (pPara
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
702 pPara
= pPara
->member
.para
.prev_para
;
703 /* Paragraph breaks are treated as separate words */
704 if (pPara
->member
.para
.prev_para
->type
== diTextStart
)
707 pRun
= ME_FindItemBack(pPara
, diRun
);
708 pPara
= pPara
->member
.para
.prev_para
;
716 /* Forward movement */
717 BOOL last_delim
= FALSE
;
721 if (last_delim
&& !ME_CallWordBreakProc(editor
, get_text( &pRun
->member
.run
, 0 ),
722 pRun
->member
.run
.len
, nOffset
, WB_ISDELIMITER
))
724 nOffset
= ME_CallWordBreakProc(editor
, get_text( &pRun
->member
.run
, 0 ),
725 pRun
->member
.run
.len
, nOffset
, WB_MOVEWORDRIGHT
);
726 if (nOffset
< pRun
->member
.run
.len
)
728 pOtherRun
= ME_FindItemFwd(pRun
, diRunOrParagraphOrEnd
);
729 if (pOtherRun
->type
== diRun
)
731 last_delim
= ME_CallWordBreakProc(editor
, get_text( &pRun
->member
.run
, 0 ),
732 pRun
->member
.run
.len
, nOffset
- 1, WB_ISDELIMITER
);
736 else if (pOtherRun
->type
== diParagraph
)
738 if (pOtherRun
->member
.para
.nFlags
& MEPF_ROWSTART
)
739 pOtherRun
= pOtherRun
->member
.para
.next_para
;
740 if (cursor
->pRun
== pRun
) {
742 pRun
= ME_FindItemFwd(pPara
, diRun
);
749 if (cursor
->pRun
== pRun
)
756 cursor
->pPara
= pPara
;
758 cursor
->nOffset
= nOffset
;
764 ME_SelectByType(ME_TextEditor
*editor
, ME_SelectionType selectionType
)
766 /* pCursor[0] is the end of the selection
767 * pCursor[1] is the start of the selection (or the position selection anchor)
768 * pCursor[2] and [3] are the selection anchors that are backed up
769 * so they are kept when the selection changes for drag selection.
772 editor
->nSelectionType
= selectionType
;
773 switch(selectionType
)
778 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
779 editor
->pCursors
[1] = editor
->pCursors
[0];
780 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
785 ME_DisplayItem
*pItem
;
786 ME_DIType fwdSearchType
, backSearchType
;
787 if (selectionType
== stParagraph
) {
788 backSearchType
= diParagraph
;
789 fwdSearchType
= diParagraphOrEnd
;
791 backSearchType
= diStartRow
;
792 fwdSearchType
= diStartRowOrParagraphOrEnd
;
794 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, fwdSearchType
);
796 if (pItem
->type
== diTextEnd
)
797 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
799 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
800 editor
->pCursors
[0].pPara
= ME_GetParagraph(editor
->pCursors
[0].pRun
);
801 editor
->pCursors
[0].nOffset
= 0;
803 pItem
= ME_FindItemBack(pItem
, backSearchType
);
804 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
805 editor
->pCursors
[1].pPara
= ME_GetParagraph(editor
->pCursors
[1].pRun
);
806 editor
->pCursors
[1].nOffset
= 0;
810 /* Select everything with cursor anchored from the start of the text */
811 editor
->nSelectionType
= stDocument
;
812 ME_SetCursorToStart(editor
, &editor
->pCursors
[1]);
813 ME_SetCursorToEnd(editor
, &editor
->pCursors
[0]);
817 /* Store the anchor positions for extending the selection. */
818 editor
->pCursors
[2] = editor
->pCursors
[0];
819 editor
->pCursors
[3] = editor
->pCursors
[1];
822 int ME_GetCursorOfs(const ME_Cursor
*cursor
)
824 return cursor
->pPara
->member
.para
.nCharOfs
825 + cursor
->pRun
->member
.run
.nCharOfs
+ cursor
->nOffset
;
828 /* Helper function for ME_FindPixelPos to find paragraph within tables */
829 static ME_DisplayItem
* ME_FindPixelPosInTableRow(int x
, int y
,
830 ME_DisplayItem
*para
)
832 ME_DisplayItem
*cell
, *next_cell
;
833 assert(para
->member
.para
.nFlags
& MEPF_ROWSTART
);
834 cell
= para
->member
.para
.next_para
->member
.para
.pCell
;
837 /* find the cell we are in */
838 while ((next_cell
= cell
->member
.cell
.next_cell
) != NULL
) {
839 if (x
< next_cell
->member
.cell
.pt
.x
)
841 para
= ME_FindItemFwd(cell
, diParagraph
);
842 /* Found the cell, but there might be multiple paragraphs in
843 * the cell, so need to search down the cell for the paragraph. */
844 while (cell
== para
->member
.para
.pCell
) {
845 if (y
< para
->member
.para
.pt
.y
+ para
->member
.para
.nHeight
)
847 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
848 return ME_FindPixelPosInTableRow(x
, y
, para
);
852 para
= para
->member
.para
.next_para
;
854 /* Past the end of the cell, so go back to the last cell paragraph */
855 return para
->member
.para
.prev_para
;
859 /* Return table row delimiter */
860 para
= ME_FindItemFwd(cell
, diParagraph
);
861 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
862 assert(para
->member
.para
.pFmt
->dwMask
& PFM_TABLEROWDELIMITER
);
863 assert(para
->member
.para
.pFmt
->wEffects
& PFE_TABLEROWDELIMITER
);
867 static BOOL
ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
868 int x
, ME_Cursor
*cursor
, int *pbCaretAtEnd
)
870 ME_DisplayItem
*pNext
, *pLastRun
;
871 ME_Row
*row
= &pRow
->member
.row
;
879 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
880 assert(pNext
->type
== diRun
);
881 if (pbCaretAtEnd
) *pbCaretAtEnd
= FALSE
;
884 int run_x
= pNext
->member
.run
.pt
.x
;
885 int width
= pNext
->member
.run
.nWidth
;
887 if (x
>= run_x
&& x
< run_x
+width
)
889 cursor
->nOffset
= ME_CharFromPoint(editor
, x
-run_x
, &pNext
->member
.run
, TRUE
, TRUE
);
890 cursor
->pRun
= pNext
;
891 cursor
->pPara
= ME_GetParagraph( cursor
->pRun
);
895 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
896 } while(pNext
&& pNext
->type
== diRun
);
898 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
900 cursor
->pRun
= ME_FindItemFwd(pNext
, diRun
);
901 if (pbCaretAtEnd
) *pbCaretAtEnd
= TRUE
;
904 cursor
->pRun
= pLastRun
;
906 cursor
->pPara
= ME_GetParagraph( cursor
->pRun
);
910 /* Finds the run and offset from the pixel position.
912 * x & y are pixel positions in virtual coordinates into the rich edit control,
913 * so client coordinates must first be adjusted by the scroll position.
915 * returns TRUE if the result was exactly under the cursor, otherwise returns
916 * FALSE, and result is set to the closest position to the coordinates.
918 static BOOL
ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
,
919 ME_Cursor
*result
, BOOL
*is_eol
)
921 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
924 x
-= editor
->rcFormat
.left
;
925 y
-= editor
->rcFormat
.top
;
931 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->member
.para
.next_para
)
933 assert(p
->type
== diParagraph
);
934 if (y
< p
->member
.para
.pt
.y
+ p
->member
.para
.nHeight
)
936 if (p
->member
.para
.nFlags
& MEPF_ROWSTART
)
937 p
= ME_FindPixelPosInTableRow(x
, y
, p
);
938 y
-= p
->member
.para
.pt
.y
;
939 p
= ME_FindItemFwd(p
, diStartRow
);
941 } else if (p
->member
.para
.nFlags
& MEPF_ROWSTART
) {
942 p
= ME_GetTableRowEnd(p
);
946 for (; p
!= editor
->pBuffer
->pLast
; )
949 assert(p
->type
== diStartRow
);
950 if (y
< p
->member
.row
.pt
.y
+ p
->member
.row
.nHeight
) break;
951 pp
= ME_FindItemFwd(p
, diStartRow
);
955 if (p
== editor
->pBuffer
->pLast
)
957 /* The position is below the last paragraph, so the last row will be used
958 * rather than the end of the text, so the x position will be used to
959 * determine the offset closest to the pixel position. */
961 p
= ME_FindItemBack(p
, diStartRow
);
962 if (!p
) p
= editor
->pBuffer
->pLast
;
965 assert( p
->type
== diStartRow
|| p
== editor
->pBuffer
->pLast
);
967 if( p
->type
== diStartRow
)
968 return ME_FindRunInRow( editor
, p
, x
, result
, is_eol
) && 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 /* Sets the cursor to the position 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 * return FALSE if outside client area and the cursor is not set,
986 * otherwise TRUE is returned.
988 BOOL
ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
,
989 ME_Cursor
*cursor
, BOOL
*isExact
)
994 ITextHost_TxGetClientRect(editor
->texthost
, &rc
);
995 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
) {
996 if (isExact
) *isExact
= FALSE
;
999 x
+= editor
->horz_si
.nPos
;
1000 y
+= editor
->vert_si
.nPos
;
1001 bResult
= ME_FindPixelPos(editor
, x
, y
, cursor
, NULL
);
1002 if (isExact
) *isExact
= bResult
;
1008 /* Extends the selection with a word, line, or paragraph selection type.
1010 * The selection is anchored by editor->pCursors[2-3] such that the text
1011 * between the anchors will remain selected, and one end will be extended.
1013 * editor->pCursors[0] should have the position to extend the selection to
1014 * before this function is called.
1016 * Nothing will be done if editor->nSelectionType equals stPosition.
1018 static void ME_ExtendAnchorSelection(ME_TextEditor
*editor
)
1020 ME_Cursor tmp_cursor
;
1021 int curOfs
, anchorStartOfs
, anchorEndOfs
;
1022 if (editor
->nSelectionType
== stPosition
|| editor
->nSelectionType
== stDocument
)
1024 curOfs
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1025 anchorStartOfs
= ME_GetCursorOfs(&editor
->pCursors
[3]);
1026 anchorEndOfs
= ME_GetCursorOfs(&editor
->pCursors
[2]);
1028 tmp_cursor
= editor
->pCursors
[0];
1029 editor
->pCursors
[0] = editor
->pCursors
[2];
1030 editor
->pCursors
[1] = editor
->pCursors
[3];
1031 if (curOfs
< anchorStartOfs
)
1033 /* Extend the left side of selection */
1034 editor
->pCursors
[1] = tmp_cursor
;
1035 if (editor
->nSelectionType
== stWord
)
1036 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
1039 ME_DisplayItem
*pItem
;
1040 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1041 diStartRowOrParagraph
:diParagraph
);
1042 pItem
= ME_FindItemBack(editor
->pCursors
[1].pRun
, searchType
);
1043 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
1044 editor
->pCursors
[1].pPara
= ME_GetParagraph(editor
->pCursors
[1].pRun
);
1045 editor
->pCursors
[1].nOffset
= 0;
1048 else if (curOfs
>= anchorEndOfs
)
1050 /* Extend the right side of selection */
1051 editor
->pCursors
[0] = tmp_cursor
;
1052 if (editor
->nSelectionType
== stWord
)
1053 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
1056 ME_DisplayItem
*pItem
;
1057 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1058 diStartRowOrParagraphOrEnd
:diParagraphOrEnd
);
1059 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, searchType
);
1060 if (pItem
->type
== diTextEnd
)
1061 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
1063 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
1064 editor
->pCursors
[0].pPara
= ME_GetParagraph(editor
->pCursors
[0].pRun
);
1065 editor
->pCursors
[0].nOffset
= 0;
1070 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
, int clickNum
)
1072 ME_Cursor tmp_cursor
;
1073 int is_selection
= 0;
1076 editor
->nUDArrowX
= -1;
1078 x
+= editor
->horz_si
.nPos
;
1079 y
+= editor
->vert_si
.nPos
;
1081 tmp_cursor
= editor
->pCursors
[0];
1082 is_selection
= ME_IsSelection(editor
);
1083 is_shift
= GetKeyState(VK_SHIFT
) < 0;
1085 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
1087 if (x
>= editor
->rcFormat
.left
|| is_shift
)
1091 editor
->pCursors
[1] = editor
->pCursors
[0];
1093 if (x
>= editor
->rcFormat
.left
)
1094 ME_SelectByType(editor
, stWord
);
1096 ME_SelectByType(editor
, stParagraph
);
1097 } else if (clickNum
% 2 == 0) {
1098 ME_SelectByType(editor
, stWord
);
1100 ME_SelectByType(editor
, stParagraph
);
1105 editor
->nSelectionType
= stPosition
;
1106 editor
->pCursors
[1] = editor
->pCursors
[0];
1108 else if (!is_selection
)
1110 editor
->nSelectionType
= stPosition
;
1111 editor
->pCursors
[1] = tmp_cursor
;
1113 else if (editor
->nSelectionType
!= stPosition
)
1115 ME_ExtendAnchorSelection(editor
);
1121 ME_SelectByType(editor
, stLine
);
1122 } else if (clickNum
% 2 == 0 || is_shift
) {
1123 ME_SelectByType(editor
, stParagraph
);
1125 ME_SelectByType(editor
, stDocument
);
1128 ME_InvalidateSelection(editor
);
1129 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1130 ME_ShowCaret(editor
);
1131 ME_ClearTempStyle(editor
);
1132 ME_SendSelChange(editor
);
1135 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
1137 ME_Cursor tmp_cursor
;
1139 if (editor
->nSelectionType
== stDocument
)
1141 x
+= editor
->horz_si
.nPos
;
1142 y
+= editor
->vert_si
.nPos
;
1144 tmp_cursor
= editor
->pCursors
[0];
1145 /* FIXME: do something with the return value of ME_FindPixelPos */
1146 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
1148 ME_InvalidateSelection(editor
);
1149 editor
->pCursors
[0] = tmp_cursor
;
1150 ME_ExtendAnchorSelection(editor
);
1152 if (editor
->nSelectionType
!= stPosition
&&
1153 memcmp(&editor
->pCursors
[1], &editor
->pCursors
[3], sizeof(ME_Cursor
)))
1155 /* The scroll the cursor towards the other end, since it was the one
1156 * extended by ME_ExtendAnchorSelection */
1157 ME_EnsureVisible(editor
, &editor
->pCursors
[1]);
1159 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
1162 ME_InvalidateSelection(editor
);
1163 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1164 ME_ShowCaret(editor
);
1165 ME_SendSelChange(editor
);
1168 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1170 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1173 if (editor
->nUDArrowX
!= -1)
1174 x
= editor
->nUDArrowX
;
1176 if (editor
->bCaretAtEnd
)
1178 pRun
= ME_FindItemBack(pRun
, diRun
);
1180 x
= pRun
->member
.run
.pt
.x
+ pRun
->member
.run
.nWidth
;
1183 x
= pRun
->member
.run
.pt
.x
;
1184 x
+= ME_PointFromChar(editor
, &pRun
->member
.run
, pCursor
->nOffset
, TRUE
);
1186 editor
->nUDArrowX
= x
;
1193 ME_MoveCursorLines(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
1195 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1196 ME_DisplayItem
*pOldPara
= pCursor
->pPara
;
1197 ME_DisplayItem
*pItem
, *pNewPara
;
1198 int x
= ME_GetXForArrow(editor
, pCursor
);
1200 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1201 if (!ME_PrevRun(&pOldPara
, &pRun
))
1206 /* start of this row */
1207 pItem
= ME_FindItemBack(pRun
, diStartRow
);
1209 /* start of the previous row */
1210 pItem
= ME_FindItemBack(pItem
, diStartRow
);
1212 return; /* row not found - ignore */
1213 pNewPara
= ME_GetParagraph(pItem
);
1214 if (pOldPara
->member
.para
.nFlags
& MEPF_ROWEND
||
1215 (pOldPara
->member
.para
.pCell
&&
1216 pOldPara
->member
.para
.pCell
!= pNewPara
->member
.para
.pCell
))
1218 /* Brought out of a cell */
1219 pNewPara
= ME_GetTableRowStart(pOldPara
)->member
.para
.prev_para
;
1220 if (pNewPara
->type
== diTextStart
)
1221 return; /* At the top, so don't go anywhere. */
1222 pItem
= ME_FindItemFwd(pNewPara
, diStartRow
);
1224 if (pNewPara
->member
.para
.nFlags
& MEPF_ROWEND
)
1226 /* Brought into a table row */
1227 ME_Cell
*cell
= &ME_FindItemBack(pNewPara
, diCell
)->member
.cell
;
1228 while (x
< cell
->pt
.x
&& cell
->prev_cell
)
1229 cell
= &cell
->prev_cell
->member
.cell
;
1230 if (cell
->next_cell
) /* else - we are still at the end of the row */
1231 pItem
= ME_FindItemBack(cell
->next_cell
, diStartRow
);
1236 /* start of the next row */
1237 pItem
= ME_FindItemFwd(pRun
, diStartRow
);
1239 return; /* row not found - ignore */
1240 pNewPara
= ME_GetParagraph(pItem
);
1241 if (pOldPara
->member
.para
.nFlags
& MEPF_ROWSTART
||
1242 (pOldPara
->member
.para
.pCell
&&
1243 pOldPara
->member
.para
.pCell
!= pNewPara
->member
.para
.pCell
))
1245 /* Brought out of a cell */
1246 pNewPara
= ME_GetTableRowEnd(pOldPara
)->member
.para
.next_para
;
1247 if (pNewPara
->type
== diTextEnd
)
1248 return; /* At the bottom, so don't go anywhere. */
1249 pItem
= ME_FindItemFwd(pNewPara
, diStartRow
);
1251 if (pNewPara
->member
.para
.nFlags
& MEPF_ROWSTART
)
1253 /* Brought into a table row */
1254 ME_DisplayItem
*cell
= ME_FindItemFwd(pNewPara
, diCell
);
1255 while (cell
->member
.cell
.next_cell
&&
1256 x
>= cell
->member
.cell
.next_cell
->member
.cell
.pt
.x
)
1257 cell
= cell
->member
.cell
.next_cell
;
1258 pItem
= ME_FindItemFwd(cell
, diStartRow
);
1263 /* row not found - ignore */
1266 ME_FindRunInRow(editor
, pItem
, x
, pCursor
, &editor
->bCaretAtEnd
);
1267 assert(pCursor
->pRun
);
1268 assert(pCursor
->pRun
->type
== diRun
);
1271 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1273 ME_DisplayItem
*p
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diStartRow
);
1275 if (editor
->vert_si
.nPos
< p
->member
.row
.nHeight
)
1277 ME_SetCursorToStart(editor
, pCursor
);
1278 editor
->bCaretAtEnd
= FALSE
;
1279 /* Native clears seems to clear this x value on page up at the top
1280 * of the text, but not on page down at the end of the text.
1281 * Doesn't make sense, but we try to be bug for bug compatible. */
1282 editor
->nUDArrowX
= -1;
1284 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1285 ME_DisplayItem
*pLast
;
1287 int yOldScrollPos
= editor
->vert_si
.nPos
;
1289 x
= ME_GetXForArrow(editor
, pCursor
);
1290 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1291 pRun
= ME_FindItemBack(pRun
, diRun
);
1293 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1294 assert(p
->type
== diStartRow
);
1295 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1296 y
= yp
+ p
->member
.row
.pt
.y
;
1298 ME_ScrollUp(editor
, editor
->sizeWindow
.cy
);
1299 /* Only move the cursor by the amount scrolled. */
1300 yd
= y
+ editor
->vert_si
.nPos
- yOldScrollPos
;
1304 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
1307 if (p
->type
== diParagraph
) { /* crossing paragraphs */
1308 if (p
->member
.para
.prev_para
== NULL
)
1310 yp
= p
->member
.para
.prev_para
->member
.para
.pt
.y
;
1313 y
= yp
+ p
->member
.row
.pt
.y
;
1319 ME_FindRunInRow(editor
, pLast
, x
, pCursor
, &editor
->bCaretAtEnd
);
1321 assert(pCursor
->pRun
);
1322 assert(pCursor
->pRun
->type
== diRun
);
1325 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1327 ME_DisplayItem
*pLast
;
1330 /* Find y position of the last row */
1331 pLast
= editor
->pBuffer
->pLast
;
1332 y
= pLast
->member
.para
.prev_para
->member
.para
.pt
.y
1333 + ME_FindItemBack(pLast
, diStartRow
)->member
.row
.pt
.y
;
1335 x
= ME_GetXForArrow(editor
, pCursor
);
1337 if (editor
->vert_si
.nPos
>= y
- editor
->sizeWindow
.cy
)
1339 ME_SetCursorToEnd(editor
, pCursor
);
1340 editor
->bCaretAtEnd
= FALSE
;
1342 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1345 int yOldScrollPos
= editor
->vert_si
.nPos
;
1347 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1348 pRun
= ME_FindItemBack(pRun
, diRun
);
1350 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1351 assert(p
->type
== diStartRow
);
1352 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1353 y
= yp
+ p
->member
.row
.pt
.y
;
1355 /* For native richedit controls:
1356 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1357 * v4.1 can scroll past this position here. */
1358 ME_ScrollDown(editor
, editor
->sizeWindow
.cy
);
1359 /* Only move the cursor by the amount scrolled. */
1360 yd
= y
+ editor
->vert_si
.nPos
- yOldScrollPos
;
1364 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
1367 if (p
->type
== diParagraph
) {
1368 yp
= p
->member
.para
.pt
.y
;
1371 y
= yp
+ p
->member
.row
.pt
.y
;
1377 ME_FindRunInRow(editor
, pLast
, x
, pCursor
, &editor
->bCaretAtEnd
);
1379 assert(pCursor
->pRun
);
1380 assert(pCursor
->pRun
->type
== diRun
);
1383 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1385 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
1387 ME_DisplayItem
*pRun
;
1388 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
1389 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1393 pRun
= ME_FindItemFwd(pRow
, diRun
);
1395 pCursor
->pRun
= pRun
;
1396 assert(pCursor
->pPara
== ME_GetParagraph(pRun
));
1397 pCursor
->nOffset
= 0;
1400 editor
->bCaretAtEnd
= FALSE
;
1403 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1405 ME_SetCursorToStart(editor
, pCursor
);
1406 editor
->bCaretAtEnd
= FALSE
;
1409 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1411 ME_DisplayItem
*pRow
;
1413 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1416 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1418 if (pRow
->type
== diStartRow
) {
1419 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1421 pCursor
->pRun
= pRun
;
1422 assert(pCursor
->pPara
== ME_GetParagraph(pCursor
->pRun
));
1423 pCursor
->nOffset
= 0;
1424 editor
->bCaretAtEnd
= TRUE
;
1427 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1428 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1429 assert(pCursor
->pPara
== ME_GetParagraph(pCursor
->pRun
));
1430 pCursor
->nOffset
= 0;
1431 editor
->bCaretAtEnd
= FALSE
;
1434 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1436 ME_SetCursorToEnd(editor
, pCursor
);
1437 editor
->bCaretAtEnd
= FALSE
;
1440 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1442 return editor
->pCursors
[0].pRun
!= editor
->pCursors
[1].pRun
||
1443 editor
->pCursors
[0].nOffset
!= editor
->pCursors
[1].nOffset
;
1446 void ME_DeleteSelection(ME_TextEditor
*editor
)
1449 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
1450 ME_DeleteTextAtCursor(editor
, nStartCursor
, to
- from
);
1453 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1455 return ME_GetInsertStyle(editor
, 0);
1458 void ME_SendSelChange(ME_TextEditor
*editor
)
1462 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1465 sc
.nmhdr
.hwndFrom
= NULL
;
1466 sc
.nmhdr
.idFrom
= 0;
1467 sc
.nmhdr
.code
= EN_SELCHANGE
;
1468 ME_GetSelectionOfs(editor
, &sc
.chrg
.cpMin
, &sc
.chrg
.cpMax
);
1469 sc
.seltyp
= SEL_EMPTY
;
1470 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1471 sc
.seltyp
|= SEL_TEXT
;
1472 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* what were RICHEDIT authors thinking ? */
1473 sc
.seltyp
|= SEL_MULTICHAR
;
1474 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1475 sc
.chrg
.cpMin
, sc
.chrg
.cpMax
, sc
.seltyp
,
1476 (sc
.seltyp
& SEL_TEXT
) ? "SEL_TEXT" : "",
1477 (sc
.seltyp
& SEL_MULTICHAR
) ? "SEL_MULTICHAR" : "");
1478 if (sc
.chrg
.cpMin
!= editor
->notified_cr
.cpMin
|| sc
.chrg
.cpMax
!= editor
->notified_cr
.cpMax
)
1480 ME_ClearTempStyle(editor
);
1482 editor
->notified_cr
= sc
.chrg
;
1483 ITextHost_TxNotify(editor
->texthost
, sc
.nmhdr
.code
, &sc
);
1488 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1491 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1492 ME_Cursor tmp_curs
= *p
;
1493 BOOL success
= FALSE
;
1495 ME_CheckCharOffsets(editor
);
1498 editor
->bCaretAtEnd
= 0;
1500 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1502 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1505 editor
->bCaretAtEnd
= 0;
1507 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1509 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1512 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1515 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1518 ME_ArrowPageUp(editor
, &tmp_curs
);
1521 ME_ArrowPageDown(editor
, &tmp_curs
);
1525 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1527 ME_ArrowHome(editor
, &tmp_curs
);
1528 editor
->bCaretAtEnd
= 0;
1533 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1535 ME_ArrowEnd(editor
, &tmp_curs
);
1540 editor
->pCursors
[1] = tmp_curs
;
1543 ME_InvalidateSelection(editor
);
1545 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1546 ME_EnsureVisible(editor
, &tmp_curs
);
1547 ME_ShowCaret(editor
);
1548 ME_SendSelChange(editor
);