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 if (ME_GetCursorOfs(&editor
->pCursors
[0]) < ME_GetCursorOfs(&editor
->pCursors
[1]))
61 *from
= &editor
->pCursors
[0];
62 *to
= &editor
->pCursors
[1];
65 *from
= &editor
->pCursors
[1];
66 *to
= &editor
->pCursors
[0];
71 int ME_GetTextLength(ME_TextEditor
*editor
)
74 ME_SetCursorToEnd(editor
, &cursor
);
75 return ME_GetCursorOfs(&cursor
);
79 int ME_GetTextLengthEx(ME_TextEditor
*editor
, const GETTEXTLENGTHEX
*how
)
83 if (how
->flags
& GTL_PRECISE
&& how
->flags
& GTL_CLOSE
)
85 if (how
->flags
& GTL_NUMCHARS
&& how
->flags
& GTL_NUMBYTES
)
88 length
= ME_GetTextLength(editor
);
90 if ((editor
->styleFlags
& ES_MULTILINE
)
91 && (how
->flags
& GTL_USECRLF
)
92 && !editor
->bEmulateVersion10
) /* Ignore GTL_USECRLF flag in 1.0 emulation */
93 length
+= editor
->nParagraphs
- 1;
95 if (how
->flags
& GTL_NUMBYTES
||
96 (how
->flags
& GTL_PRECISE
&& /* GTL_PRECISE seems to imply GTL_NUMBYTES */
97 !(how
->flags
& GTL_NUMCHARS
))) /* unless GTL_NUMCHARS is given */
101 if (how
->codepage
== 1200)
103 if (how
->flags
& GTL_PRECISE
)
104 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
105 if (GetCPInfo(how
->codepage
, &cpinfo
))
106 return length
* cpinfo
.MaxCharSize
;
107 ERR("Invalid codepage %u\n", how
->codepage
);
114 int ME_SetSelection(ME_TextEditor
*editor
, int from
, int to
)
116 int selectionEnd
= 0;
117 const int len
= ME_GetTextLength(editor
);
119 /* all negative values are effectively the same */
126 if (from
== 0 && to
== -1)
128 ME_SetCursorToStart(editor
, &editor
->pCursors
[1]);
129 ME_SetCursorToEnd(editor
, &editor
->pCursors
[0]);
130 ME_InvalidateSelection(editor
);
131 ME_ClearTempStyle(editor
);
135 /* if both values are equal and also out of bound, that means to */
136 /* put the selection at the end of the text */
137 if ((from
== to
) && (to
< 0 || to
> len
))
143 /* if from is negative and to is positive then selection is */
144 /* deselected and caret moved to end of the current selection */
148 ME_GetSelectionOfs(editor
, &start
, &end
);
151 editor
->pCursors
[1] = editor
->pCursors
[0];
154 ME_ClearTempStyle(editor
);
158 /* adjust to if it's a negative value */
162 /* flip from and to if they are reversed */
170 /* after fiddling with the values, we find from > len && to > len */
173 /* special case with to too big */
180 ME_SetCursorToEnd(editor
, &editor
->pCursors
[0]);
181 editor
->pCursors
[1] = editor
->pCursors
[0];
182 ME_InvalidateSelection(editor
);
183 ME_ClearTempStyle(editor
);
187 ME_CursorFromCharOfs(editor
, from
, &editor
->pCursors
[1]);
188 editor
->pCursors
[0] = editor
->pCursors
[1];
189 ME_MoveCursorChars(editor
, &editor
->pCursors
[0], to
- from
);
190 /* Selection is not allowed in the middle of an end paragraph run. */
191 if (editor
->pCursors
[1].pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
192 editor
->pCursors
[1].nOffset
= 0;
193 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
194 editor
->pCursors
[0].nOffset
= 0;
200 ME_GetCursorCoordinates(ME_TextEditor
*editor
, ME_Cursor
*pCursor
,
201 int *x
, int *y
, int *height
)
204 ME_DisplayItem
*run
= pCursor
->pRun
;
205 ME_DisplayItem
*para
= pCursor
->pPara
;
206 ME_DisplayItem
*pSizeRun
= run
;
210 assert(height
&& x
&& y
);
211 assert(~para
->member
.para
.nFlags
& MEPF_REWRAP
);
212 assert(run
&& run
->type
== diRun
);
213 assert(para
&& para
->type
== diParagraph
);
215 row
= ME_FindItemBack(run
, diStartRowOrParagraph
);
216 assert(row
&& row
->type
== diStartRow
);
218 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
220 if (!pCursor
->nOffset
)
222 ME_DisplayItem
*prev
= ME_FindItemBack(run
, diRunOrParagraph
);
224 if (prev
->type
== diRun
)
227 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
&&
228 run
== ME_FindItemFwd(row
, diRun
))
230 ME_DisplayItem
*tmp
= ME_FindItemBack(row
, diRunOrParagraph
);
232 if (tmp
->type
== diRun
)
234 row
= ME_FindItemBack(tmp
, diStartRow
);
235 pSizeRun
= run
= tmp
;
237 assert(run
->type
== diRun
);
240 run_x
= ME_PointFromCharContext( &c
, &run
->member
.run
, pCursor
->nOffset
);
242 *height
= pSizeRun
->member
.run
.nAscent
+ pSizeRun
->member
.run
.nDescent
;
243 *x
= c
.rcView
.left
+ run
->member
.run
.pt
.x
+ run_x
- editor
->horz_si
.nPos
;
244 *y
= c
.rcView
.top
+ para
->member
.para
.pt
.y
+ row
->member
.row
.nBaseline
245 + run
->member
.run
.pt
.y
- pSizeRun
->member
.run
.nAscent
246 - editor
->vert_si
.nPos
;
247 ME_DestroyContext(&c
);
253 ME_MoveCaret(ME_TextEditor
*editor
)
257 ME_GetCursorCoordinates(editor
, &editor
->pCursors
[0], &x
, &y
, &height
);
258 if(editor
->bHaveFocus
&& !ME_IsSelection(editor
))
260 x
= min(x
, editor
->rcFormat
.right
-1);
261 ITextHost_TxCreateCaret(editor
->texthost
, NULL
, 0, height
);
262 ITextHost_TxSetCaretPos(editor
->texthost
, x
, y
);
267 void ME_ShowCaret(ME_TextEditor
*ed
)
270 if(ed
->bHaveFocus
&& !ME_IsSelection(ed
))
271 ITextHost_TxShowCaret(ed
->texthost
, TRUE
);
274 void ME_HideCaret(ME_TextEditor
*ed
)
276 if(!ed
->bHaveFocus
|| ME_IsSelection(ed
))
278 ITextHost_TxShowCaret(ed
->texthost
, FALSE
);
283 BOOL
ME_InternalDeleteText(ME_TextEditor
*editor
, ME_Cursor
*start
,
284 int nChars
, BOOL bForce
)
286 ME_Cursor c
= *start
;
287 int nOfs
= ME_GetCursorOfs(start
);
289 int totalChars
= nChars
;
290 ME_DisplayItem
*start_para
;
292 /* Prevent deletion past last end of paragraph run. */
293 nChars
= min(nChars
, ME_GetTextLength(editor
) - nOfs
);
294 start_para
= c
.pPara
;
298 ME_ProtectPartialTableDeletion(editor
, &c
, &nChars
);
306 ME_CursorFromCharOfs(editor
, nOfs
+nChars
, &c
);
308 nOfs
+nChars
== (c
.pRun
->member
.run
.nCharOfs
309 + c
.pPara
->member
.para
.nCharOfs
))
311 /* We aren't deleting anything in this run, so we will go back to the
312 * last run we are deleting text in. */
313 ME_PrevRun(&c
.pPara
, &c
.pRun
);
314 c
.nOffset
= c
.pRun
->member
.run
.len
;
316 run
= &c
.pRun
->member
.run
;
317 if (run
->nFlags
& MERF_ENDPARA
) {
318 int eollen
= c
.pRun
->member
.run
.len
;
319 BOOL keepFirstParaFormat
;
321 if (!ME_FindItemFwd(c
.pRun
, diParagraph
))
325 keepFirstParaFormat
= (totalChars
== nChars
&& nChars
<= eollen
&&
327 if (!editor
->bEmulateVersion10
) /* v4.1 */
329 ME_DisplayItem
*next_para
= ME_FindItemFwd(c
.pRun
, diParagraphOrEnd
);
330 ME_DisplayItem
*this_para
= next_para
->member
.para
.prev_para
;
332 /* The end of paragraph before a table row is only deleted if there
333 * is nothing else on the line before it. */
334 if (this_para
== start_para
&&
335 next_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
337 /* If the paragraph will be empty, then it should be deleted, however
338 * it still might have text right now which would inherit the
339 * MEPF_STARTROW property if we joined it right now.
340 * Instead we will delete it after the preceding text is deleted. */
341 if (nOfs
> this_para
->member
.para
.nCharOfs
) {
342 /* Skip this end of line. */
343 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
346 keepFirstParaFormat
= TRUE
;
349 ME_JoinParagraphs(editor
, c
.pPara
, keepFirstParaFormat
);
350 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
351 ME_CheckCharOffsets(editor
);
352 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
358 int nCharsToDelete
= min(nChars
, c
.nOffset
);
361 c
.nOffset
-= nCharsToDelete
;
363 ME_FindItemBack(c
.pRun
, diParagraph
)->member
.para
.nFlags
|= MEPF_REWRAP
;
366 /* nChars is the number of characters that should be deleted from the
367 PRECEDING runs (these BEFORE cursor.pRun)
368 nCharsToDelete is a number of chars to delete from THIS run */
369 nChars
-= nCharsToDelete
;
370 shift
-= nCharsToDelete
;
371 TRACE("Deleting %d (remaning %d) chars at %d in %s (%d)\n",
372 nCharsToDelete
, nChars
, c
.nOffset
,
373 debugstr_run( run
), run
->len
);
375 /* nOfs is a character offset (from the start of the document
376 to the current (deleted) run */
377 add_undo_insert_run( editor
, nOfs
+ nChars
, get_text( run
, c
.nOffset
), nCharsToDelete
, run
->nFlags
, run
->style
);
379 ME_StrDeleteV(run
->para
->text
, run
->nCharOfs
+ c
.nOffset
, nCharsToDelete
);
380 run
->len
-= nCharsToDelete
;
381 TRACE("Post deletion string: %s (%d)\n", debugstr_run( run
), run
->len
);
382 TRACE("Shift value: %d\n", shift
);
384 /* update cursors (including c) */
385 for (i
=-1; i
<editor
->nCursors
; i
++) {
386 ME_Cursor
*pThisCur
= editor
->pCursors
+ i
;
387 if (i
== -1) pThisCur
= &c
;
388 if (pThisCur
->pRun
== cursor
.pRun
) {
389 if (pThisCur
->nOffset
> cursor
.nOffset
) {
390 if (pThisCur
->nOffset
-cursor
.nOffset
< nCharsToDelete
)
391 pThisCur
->nOffset
= cursor
.nOffset
;
393 pThisCur
->nOffset
-= nCharsToDelete
;
394 assert(pThisCur
->nOffset
>= 0);
395 assert(pThisCur
->nOffset
<= run
->len
);
397 if (pThisCur
->nOffset
== run
->len
)
399 pThisCur
->pRun
= ME_FindItemFwd(pThisCur
->pRun
, diRunOrParagraphOrEnd
);
400 assert(pThisCur
->pRun
->type
== diRun
);
401 pThisCur
->nOffset
= 0;
406 /* c = updated data now */
408 if (c
.pRun
== cursor
.pRun
)
409 ME_SkipAndPropagateCharOffset(c
.pRun
, shift
);
411 ME_PropagateCharOffset(c
.pRun
, shift
);
413 if (!cursor
.pRun
->member
.run
.len
)
415 TRACE("Removing empty run\n");
416 ME_Remove(cursor
.pRun
);
417 ME_DestroyDisplayItem(cursor
.pRun
);
422 ME_CheckCharOffsets(editor);
430 BOOL
ME_DeleteTextAtCursor(ME_TextEditor
*editor
, int nCursor
, int nChars
)
432 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
433 /* text operations set modified state */
434 editor
->nModifyStep
= 1;
435 return ME_InternalDeleteText(editor
, &editor
->pCursors
[nCursor
],
439 static ME_DisplayItem
*
440 ME_InternalInsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
441 const WCHAR
*str
, int len
, ME_Style
*style
,
444 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
446 editor
->bCaretAtEnd
= FALSE
;
448 assert(p
->pRun
->type
== diRun
);
450 return ME_InsertRunAtCursor(editor
, p
, style
, str
, len
, flags
);
454 void ME_InsertOLEFromCursor(ME_TextEditor
*editor
, const REOBJECT
* reo
, int nCursor
)
456 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
461 if (ME_IsSelection(editor
))
462 ME_DeleteSelection(editor
);
464 di
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
466 di
->member
.run
.ole_obj
= ALLOC_OBJ(*reo
);
467 ME_CopyReObject(di
->member
.run
.ole_obj
, reo
);
468 ME_ReleaseStyle(pStyle
);
472 void ME_InsertEndRowFromCursor(ME_TextEditor
*editor
, int nCursor
)
474 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
478 if (ME_IsSelection(editor
))
479 ME_DeleteSelection(editor
);
481 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
483 ME_ReleaseStyle(pStyle
);
487 void ME_InsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
488 const WCHAR
*str
, int len
, ME_Style
*style
)
494 /* FIXME really HERE ? */
495 if (ME_IsSelection(editor
))
496 ME_DeleteSelection(editor
);
498 /* FIXME: is this too slow? */
499 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
500 oldLen
= ME_GetTextLength(editor
);
502 /* text operations set modified state */
503 editor
->nModifyStep
= 1;
507 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
511 /* grow the text limit to fit our text */
512 if(editor
->nTextLimit
< oldLen
+len
)
513 editor
->nTextLimit
= oldLen
+ len
;
519 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
520 while(pos
- str
< len
&& *pos
!= '\r' && *pos
!= '\n' && *pos
!= '\t')
523 if (pos
!= str
) { /* handle text */
524 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
525 } else if (*pos
== '\t') { /* handle tabs */
527 ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, style
, MERF_TAB
);
529 } else { /* handle EOLs */
530 ME_DisplayItem
*tp
, *end_run
;
534 /* Find number of CR and LF in end of paragraph run */
537 if (len
> 1 && pos
[1] == '\n')
539 else if (len
> 2 && pos
[1] == '\r' && pos
[2] == '\n')
544 assert(*pos
== '\n');
549 if (!editor
->bEmulateVersion10
&& eol_len
== 3)
551 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
553 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, style
, 0);
555 const WCHAR cr
= '\r', *eol_str
= str
;
557 if (!editor
->bEmulateVersion10
)
563 p
= &editor
->pCursors
[nCursor
];
565 ME_SplitRunSimple(editor
, p
);
566 tmp_style
= ME_GetInsertStyle(editor
, nCursor
);
567 /* ME_SplitParagraph increases style refcount */
568 tp
= ME_SplitParagraph(editor
, p
->pRun
, p
->pRun
->member
.run
.style
, eol_str
, eol_len
, 0);
569 p
->pRun
= ME_FindItemFwd(tp
, diRun
);
571 end_run
= ME_FindItemBack(tp
, diRun
);
572 ME_ReleaseStyle(end_run
->member
.run
.style
);
573 end_run
->member
.run
.style
= tmp_style
;
582 /* Move the cursor nRelOfs characters (either forwards or backwards)
584 * returns the actual number of characters moved.
586 int ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
588 cursor
->nOffset
+= nRelOfs
;
589 if (cursor
->nOffset
< 0)
591 cursor
->nOffset
+= cursor
->pRun
->member
.run
.nCharOfs
;
592 if (cursor
->nOffset
>= 0)
594 /* new offset in the same paragraph */
596 cursor
->pRun
= ME_FindItemBack(cursor
->pRun
, diRun
);
597 } while (cursor
->nOffset
< cursor
->pRun
->member
.run
.nCharOfs
);
598 cursor
->nOffset
-= cursor
->pRun
->member
.run
.nCharOfs
;
602 cursor
->nOffset
+= cursor
->pPara
->member
.para
.nCharOfs
;
603 if (cursor
->nOffset
<= 0)
605 /* moved to the start of the text */
606 nRelOfs
-= cursor
->nOffset
;
607 ME_SetCursorToStart(editor
, cursor
);
611 /* new offset in a previous paragraph */
613 cursor
->pPara
= cursor
->pPara
->member
.para
.prev_para
;
614 } while (cursor
->nOffset
< cursor
->pPara
->member
.para
.nCharOfs
);
615 cursor
->nOffset
-= cursor
->pPara
->member
.para
.nCharOfs
;
617 cursor
->pRun
= ME_FindItemBack(cursor
->pPara
->member
.para
.next_para
, diRun
);
618 while (cursor
->nOffset
< cursor
->pRun
->member
.run
.nCharOfs
) {
619 cursor
->pRun
= ME_FindItemBack(cursor
->pRun
, diRun
);
621 cursor
->nOffset
-= cursor
->pRun
->member
.run
.nCharOfs
;
622 } else if (cursor
->nOffset
>= cursor
->pRun
->member
.run
.len
) {
623 ME_DisplayItem
*next_para
;
626 new_offset
= ME_GetCursorOfs(cursor
);
627 next_para
= cursor
->pPara
->member
.para
.next_para
;
628 if (new_offset
< next_para
->member
.para
.nCharOfs
)
630 /* new offset in the same paragraph */
632 cursor
->nOffset
-= cursor
->pRun
->member
.run
.len
;
633 cursor
->pRun
= ME_FindItemFwd(cursor
->pRun
, diRun
);
634 } while (cursor
->nOffset
>= cursor
->pRun
->member
.run
.len
);
638 if (new_offset
>= ME_GetTextLength(editor
))
640 /* new offset at the end of the text */
641 ME_SetCursorToEnd(editor
, cursor
);
642 nRelOfs
-= new_offset
- ME_GetTextLength(editor
);
646 /* new offset in a following paragraph */
648 cursor
->pPara
= next_para
;
649 next_para
= next_para
->member
.para
.next_para
;
650 } while (new_offset
>= next_para
->member
.para
.nCharOfs
);
652 cursor
->nOffset
= new_offset
- cursor
->pPara
->member
.para
.nCharOfs
;
653 cursor
->pRun
= ME_FindItemFwd(cursor
->pPara
, diRun
);
654 while (cursor
->nOffset
>= cursor
->pRun
->member
.run
.len
)
656 cursor
->nOffset
-= cursor
->pRun
->member
.run
.len
;
657 cursor
->pRun
= ME_FindItemFwd(cursor
->pRun
, diRun
);
659 } /* else new offset is in the same run */
665 ME_MoveCursorWords(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
667 ME_DisplayItem
*pRun
= cursor
->pRun
, *pOtherRun
;
668 ME_DisplayItem
*pPara
= cursor
->pPara
;
669 int nOffset
= cursor
->nOffset
;
673 /* Backward movement */
676 nOffset
= ME_CallWordBreakProc(editor
, get_text( &pRun
->member
.run
, 0 ),
677 pRun
->member
.run
.len
, nOffset
, WB_MOVEWORDLEFT
);
680 pOtherRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
681 if (pOtherRun
->type
== diRun
)
683 if (ME_CallWordBreakProc(editor
, get_text( &pOtherRun
->member
.run
, 0 ),
684 pOtherRun
->member
.run
.len
,
685 pOtherRun
->member
.run
.len
- 1,
687 && !(pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
688 && !(cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
689 && !ME_CallWordBreakProc(editor
, get_text( &pRun
->member
.run
, 0 ),
690 pRun
->member
.run
.len
, 0,
694 nOffset
= pOtherRun
->member
.run
.len
;
696 else if (pOtherRun
->type
== diParagraph
)
698 if (cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
701 /* Skip empty start of table row paragraph */
702 if (pPara
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
703 pPara
= pPara
->member
.para
.prev_para
;
704 /* Paragraph breaks are treated as separate words */
705 if (pPara
->member
.para
.prev_para
->type
== diTextStart
)
708 pRun
= ME_FindItemBack(pPara
, diRun
);
709 pPara
= pPara
->member
.para
.prev_para
;
717 /* Forward movement */
718 BOOL last_delim
= FALSE
;
722 if (last_delim
&& !ME_CallWordBreakProc(editor
, get_text( &pRun
->member
.run
, 0 ),
723 pRun
->member
.run
.len
, nOffset
, WB_ISDELIMITER
))
725 nOffset
= ME_CallWordBreakProc(editor
, get_text( &pRun
->member
.run
, 0 ),
726 pRun
->member
.run
.len
, nOffset
, WB_MOVEWORDRIGHT
);
727 if (nOffset
< pRun
->member
.run
.len
)
729 pOtherRun
= ME_FindItemFwd(pRun
, diRunOrParagraphOrEnd
);
730 if (pOtherRun
->type
== diRun
)
732 last_delim
= ME_CallWordBreakProc(editor
, get_text( &pRun
->member
.run
, 0 ),
733 pRun
->member
.run
.len
, nOffset
- 1, WB_ISDELIMITER
);
737 else if (pOtherRun
->type
== diParagraph
)
739 if (pOtherRun
->member
.para
.nFlags
& MEPF_ROWSTART
)
740 pOtherRun
= pOtherRun
->member
.para
.next_para
;
741 if (cursor
->pRun
== pRun
) {
743 pRun
= ME_FindItemFwd(pPara
, diRun
);
750 if (cursor
->pRun
== pRun
)
757 cursor
->pPara
= pPara
;
759 cursor
->nOffset
= nOffset
;
765 ME_SelectByType(ME_TextEditor
*editor
, ME_SelectionType selectionType
)
767 /* pCursor[0] is the end of the selection
768 * pCursor[1] is the start of the selection (or the position selection anchor)
769 * pCursor[2] and [3] are the selection anchors that are backed up
770 * so they are kept when the selection changes for drag selection.
773 editor
->nSelectionType
= selectionType
;
774 switch(selectionType
)
779 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
780 editor
->pCursors
[1] = editor
->pCursors
[0];
781 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
786 ME_DisplayItem
*pItem
;
787 ME_DIType fwdSearchType
, backSearchType
;
788 if (selectionType
== stParagraph
) {
789 backSearchType
= diParagraph
;
790 fwdSearchType
= diParagraphOrEnd
;
792 backSearchType
= diStartRow
;
793 fwdSearchType
= diStartRowOrParagraphOrEnd
;
795 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, fwdSearchType
);
797 if (pItem
->type
== diTextEnd
)
798 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
800 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
801 editor
->pCursors
[0].pPara
= ME_GetParagraph(editor
->pCursors
[0].pRun
);
802 editor
->pCursors
[0].nOffset
= 0;
804 pItem
= ME_FindItemBack(pItem
, backSearchType
);
805 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
806 editor
->pCursors
[1].pPara
= ME_GetParagraph(editor
->pCursors
[1].pRun
);
807 editor
->pCursors
[1].nOffset
= 0;
811 /* Select everything with cursor anchored from the start of the text */
812 editor
->nSelectionType
= stDocument
;
813 ME_SetCursorToStart(editor
, &editor
->pCursors
[1]);
814 ME_SetCursorToEnd(editor
, &editor
->pCursors
[0]);
818 /* Store the anchor positions for extending the selection. */
819 editor
->pCursors
[2] = editor
->pCursors
[0];
820 editor
->pCursors
[3] = editor
->pCursors
[1];
823 int ME_GetCursorOfs(const ME_Cursor
*cursor
)
825 return cursor
->pPara
->member
.para
.nCharOfs
826 + cursor
->pRun
->member
.run
.nCharOfs
+ cursor
->nOffset
;
829 /* Helper function for ME_FindPixelPos to find paragraph within tables */
830 static ME_DisplayItem
* ME_FindPixelPosInTableRow(int x
, int y
,
831 ME_DisplayItem
*para
)
833 ME_DisplayItem
*cell
, *next_cell
;
834 assert(para
->member
.para
.nFlags
& MEPF_ROWSTART
);
835 cell
= para
->member
.para
.next_para
->member
.para
.pCell
;
838 /* find the cell we are in */
839 while ((next_cell
= cell
->member
.cell
.next_cell
) != NULL
) {
840 if (x
< next_cell
->member
.cell
.pt
.x
)
842 para
= ME_FindItemFwd(cell
, diParagraph
);
843 /* Found the cell, but there might be multiple paragraphs in
844 * the cell, so need to search down the cell for the paragraph. */
845 while (cell
== para
->member
.para
.pCell
) {
846 if (y
< para
->member
.para
.pt
.y
+ para
->member
.para
.nHeight
)
848 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
849 return ME_FindPixelPosInTableRow(x
, y
, para
);
853 para
= para
->member
.para
.next_para
;
855 /* Past the end of the cell, so go back to the last cell paragraph */
856 return para
->member
.para
.prev_para
;
860 /* Return table row delimiter */
861 para
= ME_FindItemFwd(cell
, diParagraph
);
862 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
863 assert(para
->member
.para
.pFmt
->dwMask
& PFM_TABLEROWDELIMITER
);
864 assert(para
->member
.para
.pFmt
->wEffects
& PFE_TABLEROWDELIMITER
);
868 static BOOL
ME_ReturnFoundPos(ME_TextEditor
*editor
, ME_DisplayItem
*found
,
869 ME_Cursor
*result
, int rx
, BOOL isExact
)
872 assert(found
->type
== diRun
);
873 if ((found
->member
.run
.nFlags
& MERF_ENDPARA
) || rx
< 0)
875 result
->pRun
= found
;
876 result
->nOffset
= ME_CharFromPoint(editor
, rx
, &found
->member
.run
, TRUE
);
877 if (result
->nOffset
== found
->member
.run
.len
&& rx
)
879 result
->pRun
= ME_FindItemFwd(result
->pRun
, diRun
);
882 result
->pPara
= ME_GetParagraph(result
->pRun
);
886 /* Finds the run and offset from the pixel position.
888 * x & y are pixel positions in virtual coordinates into the rich edit control,
889 * so client coordinates must first be adjusted by the scroll position.
891 * returns TRUE if the result was exactly under the cursor, otherwise returns
892 * FALSE, and result is set to the closest position to the coordinates.
894 static BOOL
ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
,
895 ME_Cursor
*result
, BOOL
*is_eol
)
897 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
898 ME_DisplayItem
*last
= NULL
;
902 x
-= editor
->rcFormat
.left
;
903 y
-= editor
->rcFormat
.top
;
909 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->member
.para
.next_para
)
911 assert(p
->type
== diParagraph
);
912 if (y
< p
->member
.para
.pt
.y
+ p
->member
.para
.nHeight
)
914 if (p
->member
.para
.nFlags
& MEPF_ROWSTART
)
915 p
= ME_FindPixelPosInTableRow(x
, y
, p
);
916 y
-= p
->member
.para
.pt
.y
;
917 p
= ME_FindItemFwd(p
, diStartRow
);
919 } else if (p
->member
.para
.nFlags
& MEPF_ROWSTART
) {
920 p
= ME_GetTableRowEnd(p
);
924 for (; p
!= editor
->pBuffer
->pLast
; )
927 assert(p
->type
== diStartRow
);
928 if (y
< p
->member
.row
.pt
.y
+ p
->member
.row
.nHeight
)
930 p
= ME_FindItemFwd(p
, diRun
);
933 pp
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
934 if (pp
->type
!= diStartRow
)
936 p
= ME_FindItemFwd(p
, diRun
);
941 if (p
== editor
->pBuffer
->pLast
)
943 /* The position is below the last paragraph, so the last row will be used
944 * rather than the end of the text, so the x position will be used to
945 * determine the offset closest to the pixel position. */
947 p
= ME_FindItemBack(p
, diStartRow
);
949 p
= ME_FindItemFwd(p
, diRun
);
953 p
= editor
->pBuffer
->pLast
;
956 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->next
)
961 rx
= x
- p
->member
.run
.pt
.x
;
962 if (rx
< p
->member
.run
.nWidth
)
963 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
967 p
= ME_FindItemFwd(p
, diRun
);
968 if (is_eol
) *is_eol
= 1;
969 rx
= 0; /* FIXME not sure */
970 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
975 rx
= 0; /* FIXME not sure */
977 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
982 result
->pRun
= ME_FindItemBack(p
, diRun
);
983 result
->pPara
= ME_GetParagraph(result
->pRun
);
985 assert(result
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
990 /* Sets the cursor to the position closest to the pixel position
992 * x & y are pixel positions in client coordinates.
994 * isExact will be set to TRUE if the run is directly under the pixel
995 * position, FALSE if it not, unless isExact is set to NULL.
997 * return FALSE if outside client area and the cursor is not set,
998 * otherwise TRUE is returned.
1000 BOOL
ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
,
1001 ME_Cursor
*cursor
, BOOL
*isExact
)
1006 ITextHost_TxGetClientRect(editor
->texthost
, &rc
);
1007 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
) {
1008 if (isExact
) *isExact
= FALSE
;
1011 x
+= editor
->horz_si
.nPos
;
1012 y
+= editor
->vert_si
.nPos
;
1013 bResult
= ME_FindPixelPos(editor
, x
, y
, cursor
, NULL
);
1014 if (isExact
) *isExact
= bResult
;
1020 /* Extends the selection with a word, line, or paragraph selection type.
1022 * The selection is anchored by editor->pCursors[2-3] such that the text
1023 * between the anchors will remain selected, and one end will be extended.
1025 * editor->pCursors[0] should have the position to extend the selection to
1026 * before this function is called.
1028 * Nothing will be done if editor->nSelectionType equals stPosition.
1030 static void ME_ExtendAnchorSelection(ME_TextEditor
*editor
)
1032 ME_Cursor tmp_cursor
;
1033 int curOfs
, anchorStartOfs
, anchorEndOfs
;
1034 if (editor
->nSelectionType
== stPosition
|| editor
->nSelectionType
== stDocument
)
1036 curOfs
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1037 anchorStartOfs
= ME_GetCursorOfs(&editor
->pCursors
[3]);
1038 anchorEndOfs
= ME_GetCursorOfs(&editor
->pCursors
[2]);
1040 tmp_cursor
= editor
->pCursors
[0];
1041 editor
->pCursors
[0] = editor
->pCursors
[2];
1042 editor
->pCursors
[1] = editor
->pCursors
[3];
1043 if (curOfs
< anchorStartOfs
)
1045 /* Extend the left side of selection */
1046 editor
->pCursors
[1] = tmp_cursor
;
1047 if (editor
->nSelectionType
== stWord
)
1048 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
1051 ME_DisplayItem
*pItem
;
1052 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1053 diStartRowOrParagraph
:diParagraph
);
1054 pItem
= ME_FindItemBack(editor
->pCursors
[1].pRun
, searchType
);
1055 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
1056 editor
->pCursors
[1].pPara
= ME_GetParagraph(editor
->pCursors
[1].pRun
);
1057 editor
->pCursors
[1].nOffset
= 0;
1060 else if (curOfs
>= anchorEndOfs
)
1062 /* Extend the right side of selection */
1063 editor
->pCursors
[0] = tmp_cursor
;
1064 if (editor
->nSelectionType
== stWord
)
1065 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
1068 ME_DisplayItem
*pItem
;
1069 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1070 diStartRowOrParagraphOrEnd
:diParagraphOrEnd
);
1071 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, searchType
);
1072 if (pItem
->type
== diTextEnd
)
1073 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
1075 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
1076 editor
->pCursors
[0].pPara
= ME_GetParagraph(editor
->pCursors
[0].pRun
);
1077 editor
->pCursors
[0].nOffset
= 0;
1082 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
, int clickNum
)
1084 ME_Cursor tmp_cursor
;
1085 int is_selection
= 0;
1088 editor
->nUDArrowX
= -1;
1090 x
+= editor
->horz_si
.nPos
;
1091 y
+= editor
->vert_si
.nPos
;
1093 tmp_cursor
= editor
->pCursors
[0];
1094 is_selection
= ME_IsSelection(editor
);
1095 is_shift
= GetKeyState(VK_SHIFT
) < 0;
1097 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
1099 if (x
>= editor
->rcFormat
.left
|| is_shift
)
1103 editor
->pCursors
[1] = editor
->pCursors
[0];
1105 if (x
>= editor
->rcFormat
.left
)
1106 ME_SelectByType(editor
, stWord
);
1108 ME_SelectByType(editor
, stParagraph
);
1109 } else if (clickNum
% 2 == 0) {
1110 ME_SelectByType(editor
, stWord
);
1112 ME_SelectByType(editor
, stParagraph
);
1117 editor
->nSelectionType
= stPosition
;
1118 editor
->pCursors
[1] = editor
->pCursors
[0];
1120 else if (!is_selection
)
1122 editor
->nSelectionType
= stPosition
;
1123 editor
->pCursors
[1] = tmp_cursor
;
1125 else if (editor
->nSelectionType
!= stPosition
)
1127 ME_ExtendAnchorSelection(editor
);
1133 ME_SelectByType(editor
, stLine
);
1134 } else if (clickNum
% 2 == 0 || is_shift
) {
1135 ME_SelectByType(editor
, stParagraph
);
1137 ME_SelectByType(editor
, stDocument
);
1140 ME_InvalidateSelection(editor
);
1141 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1142 ME_ShowCaret(editor
);
1143 ME_ClearTempStyle(editor
);
1144 ME_SendSelChange(editor
);
1147 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
1149 ME_Cursor tmp_cursor
;
1151 if (editor
->nSelectionType
== stDocument
)
1153 x
+= editor
->horz_si
.nPos
;
1154 y
+= editor
->vert_si
.nPos
;
1156 tmp_cursor
= editor
->pCursors
[0];
1157 /* FIXME: do something with the return value of ME_FindPixelPos */
1158 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
1160 ME_InvalidateSelection(editor
);
1161 editor
->pCursors
[0] = tmp_cursor
;
1162 ME_ExtendAnchorSelection(editor
);
1164 if (editor
->nSelectionType
!= stPosition
&&
1165 memcmp(&editor
->pCursors
[1], &editor
->pCursors
[3], sizeof(ME_Cursor
)))
1167 /* The scroll the cursor towards the other end, since it was the one
1168 * extended by ME_ExtendAnchorSelection */
1169 ME_EnsureVisible(editor
, &editor
->pCursors
[1]);
1171 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
1174 ME_InvalidateSelection(editor
);
1175 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1176 ME_ShowCaret(editor
);
1177 ME_SendSelChange(editor
);
1180 static BOOL
ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
1181 int x
, ME_Cursor
*cursor
, int *pbCaretAtEnd
)
1183 ME_DisplayItem
*pNext
, *pLastRun
;
1184 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
1185 assert(pNext
->type
== diRun
);
1186 if (pbCaretAtEnd
) *pbCaretAtEnd
= FALSE
;
1187 cursor
->nOffset
= 0;
1189 int run_x
= pNext
->member
.run
.pt
.x
;
1190 int width
= pNext
->member
.run
.nWidth
;
1193 cursor
->pRun
= pNext
;
1194 cursor
->pPara
= ME_GetParagraph( cursor
->pRun
);
1197 if (x
>= run_x
&& x
< run_x
+width
)
1199 int ch
= ME_CharFromPoint(editor
, x
-run_x
, &pNext
->member
.run
, TRUE
);
1200 if (ch
< pNext
->member
.run
.len
)
1202 cursor
->nOffset
= ch
;
1203 cursor
->pRun
= pNext
;
1204 cursor
->pPara
= ME_GetParagraph( cursor
->pRun
);
1209 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
1210 } while(pNext
&& pNext
->type
== diRun
);
1212 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
1214 cursor
->pRun
= ME_FindItemFwd(pNext
, diRun
);
1215 if (pbCaretAtEnd
) *pbCaretAtEnd
= TRUE
;
1218 cursor
->pRun
= pLastRun
;
1220 cursor
->pPara
= ME_GetParagraph( cursor
->pRun
);
1224 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1226 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1229 if (editor
->nUDArrowX
!= -1)
1230 x
= editor
->nUDArrowX
;
1232 if (editor
->bCaretAtEnd
)
1234 pRun
= ME_FindItemBack(pRun
, diRun
);
1236 x
= pRun
->member
.run
.pt
.x
+ pRun
->member
.run
.nWidth
;
1239 x
= pRun
->member
.run
.pt
.x
;
1240 x
+= ME_PointFromChar(editor
, &pRun
->member
.run
, pCursor
->nOffset
);
1242 editor
->nUDArrowX
= x
;
1249 ME_MoveCursorLines(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
1251 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1252 ME_DisplayItem
*pOldPara
= pCursor
->pPara
;
1253 ME_DisplayItem
*pItem
, *pNewPara
;
1254 int x
= ME_GetXForArrow(editor
, pCursor
);
1256 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1257 if (!ME_PrevRun(&pOldPara
, &pRun
))
1262 /* start of this row */
1263 pItem
= ME_FindItemBack(pRun
, diStartRow
);
1265 /* start of the previous row */
1266 pItem
= ME_FindItemBack(pItem
, diStartRow
);
1268 return; /* row not found - ignore */
1269 pNewPara
= ME_GetParagraph(pItem
);
1270 if (pOldPara
->member
.para
.nFlags
& MEPF_ROWEND
||
1271 (pOldPara
->member
.para
.pCell
&&
1272 pOldPara
->member
.para
.pCell
!= pNewPara
->member
.para
.pCell
))
1274 /* Brought out of a cell */
1275 pNewPara
= ME_GetTableRowStart(pOldPara
)->member
.para
.prev_para
;
1276 if (pNewPara
->type
== diTextStart
)
1277 return; /* At the top, so don't go anywhere. */
1278 pItem
= ME_FindItemFwd(pNewPara
, diStartRow
);
1280 if (pNewPara
->member
.para
.nFlags
& MEPF_ROWEND
)
1282 /* Brought into a table row */
1283 ME_Cell
*cell
= &ME_FindItemBack(pNewPara
, diCell
)->member
.cell
;
1284 while (x
< cell
->pt
.x
&& cell
->prev_cell
)
1285 cell
= &cell
->prev_cell
->member
.cell
;
1286 if (cell
->next_cell
) /* else - we are still at the end of the row */
1287 pItem
= ME_FindItemBack(cell
->next_cell
, diStartRow
);
1292 /* start of the next row */
1293 pItem
= ME_FindItemFwd(pRun
, diStartRow
);
1295 return; /* row not found - ignore */
1296 pNewPara
= ME_GetParagraph(pItem
);
1297 if (pOldPara
->member
.para
.nFlags
& MEPF_ROWSTART
||
1298 (pOldPara
->member
.para
.pCell
&&
1299 pOldPara
->member
.para
.pCell
!= pNewPara
->member
.para
.pCell
))
1301 /* Brought out of a cell */
1302 pNewPara
= ME_GetTableRowEnd(pOldPara
)->member
.para
.next_para
;
1303 if (pNewPara
->type
== diTextEnd
)
1304 return; /* At the bottom, so don't go anywhere. */
1305 pItem
= ME_FindItemFwd(pNewPara
, diStartRow
);
1307 if (pNewPara
->member
.para
.nFlags
& MEPF_ROWSTART
)
1309 /* Brought into a table row */
1310 ME_DisplayItem
*cell
= ME_FindItemFwd(pNewPara
, diCell
);
1311 while (cell
->member
.cell
.next_cell
&&
1312 x
>= cell
->member
.cell
.next_cell
->member
.cell
.pt
.x
)
1313 cell
= cell
->member
.cell
.next_cell
;
1314 pItem
= ME_FindItemFwd(cell
, diStartRow
);
1319 /* row not found - ignore */
1322 ME_FindRunInRow(editor
, pItem
, x
, pCursor
, &editor
->bCaretAtEnd
);
1323 assert(pCursor
->pRun
);
1324 assert(pCursor
->pRun
->type
== diRun
);
1327 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1329 ME_DisplayItem
*p
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diStartRow
);
1331 if (editor
->vert_si
.nPos
< p
->member
.row
.nHeight
)
1333 ME_SetCursorToStart(editor
, pCursor
);
1334 editor
->bCaretAtEnd
= FALSE
;
1335 /* Native clears seems to clear this x value on page up at the top
1336 * of the text, but not on page down at the end of the text.
1337 * Doesn't make sense, but we try to be bug for bug compatible. */
1338 editor
->nUDArrowX
= -1;
1340 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1341 ME_DisplayItem
*pLast
;
1343 int yOldScrollPos
= editor
->vert_si
.nPos
;
1345 x
= ME_GetXForArrow(editor
, pCursor
);
1346 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1347 pRun
= ME_FindItemBack(pRun
, diRun
);
1349 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1350 assert(p
->type
== diStartRow
);
1351 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1352 y
= yp
+ p
->member
.row
.pt
.y
;
1354 ME_ScrollUp(editor
, editor
->sizeWindow
.cy
);
1355 /* Only move the cursor by the amount scrolled. */
1356 yd
= y
+ editor
->vert_si
.nPos
- yOldScrollPos
;
1360 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
1363 if (p
->type
== diParagraph
) { /* crossing paragraphs */
1364 if (p
->member
.para
.prev_para
== NULL
)
1366 yp
= p
->member
.para
.prev_para
->member
.para
.pt
.y
;
1369 y
= yp
+ p
->member
.row
.pt
.y
;
1375 ME_FindRunInRow(editor
, pLast
, x
, pCursor
, &editor
->bCaretAtEnd
);
1377 assert(pCursor
->pRun
);
1378 assert(pCursor
->pRun
->type
== diRun
);
1381 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1383 ME_DisplayItem
*pLast
;
1386 /* Find y position of the last row */
1387 pLast
= editor
->pBuffer
->pLast
;
1388 y
= pLast
->member
.para
.prev_para
->member
.para
.pt
.y
1389 + ME_FindItemBack(pLast
, diStartRow
)->member
.row
.pt
.y
;
1391 x
= ME_GetXForArrow(editor
, pCursor
);
1393 if (editor
->vert_si
.nPos
>= y
- editor
->sizeWindow
.cy
)
1395 ME_SetCursorToEnd(editor
, pCursor
);
1396 editor
->bCaretAtEnd
= FALSE
;
1398 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1401 int yOldScrollPos
= editor
->vert_si
.nPos
;
1403 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1404 pRun
= ME_FindItemBack(pRun
, diRun
);
1406 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1407 assert(p
->type
== diStartRow
);
1408 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1409 y
= yp
+ p
->member
.row
.pt
.y
;
1411 /* For native richedit controls:
1412 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1413 * v4.1 can scroll past this position here. */
1414 ME_ScrollDown(editor
, editor
->sizeWindow
.cy
);
1415 /* Only move the cursor by the amount scrolled. */
1416 yd
= y
+ editor
->vert_si
.nPos
- yOldScrollPos
;
1420 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
1423 if (p
->type
== diParagraph
) {
1424 yp
= p
->member
.para
.pt
.y
;
1427 y
= yp
+ p
->member
.row
.pt
.y
;
1433 ME_FindRunInRow(editor
, pLast
, x
, pCursor
, &editor
->bCaretAtEnd
);
1435 assert(pCursor
->pRun
);
1436 assert(pCursor
->pRun
->type
== diRun
);
1439 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1441 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
1443 ME_DisplayItem
*pRun
;
1444 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
1445 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1449 pRun
= ME_FindItemFwd(pRow
, diRun
);
1451 pCursor
->pRun
= pRun
;
1452 assert(pCursor
->pPara
== ME_GetParagraph(pRun
));
1453 pCursor
->nOffset
= 0;
1456 editor
->bCaretAtEnd
= FALSE
;
1459 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1461 ME_SetCursorToStart(editor
, pCursor
);
1462 editor
->bCaretAtEnd
= FALSE
;
1465 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1467 ME_DisplayItem
*pRow
;
1469 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1472 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1474 if (pRow
->type
== diStartRow
) {
1475 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1477 pCursor
->pRun
= pRun
;
1478 assert(pCursor
->pPara
== ME_GetParagraph(pCursor
->pRun
));
1479 pCursor
->nOffset
= 0;
1480 editor
->bCaretAtEnd
= TRUE
;
1483 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1484 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1485 assert(pCursor
->pPara
== ME_GetParagraph(pCursor
->pRun
));
1486 pCursor
->nOffset
= 0;
1487 editor
->bCaretAtEnd
= FALSE
;
1490 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1492 ME_SetCursorToEnd(editor
, pCursor
);
1493 editor
->bCaretAtEnd
= FALSE
;
1496 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1498 return editor
->pCursors
[0].pRun
!= editor
->pCursors
[1].pRun
||
1499 editor
->pCursors
[0].nOffset
!= editor
->pCursors
[1].nOffset
;
1502 void ME_DeleteSelection(ME_TextEditor
*editor
)
1505 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
1506 ME_DeleteTextAtCursor(editor
, nStartCursor
, to
- from
);
1509 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1511 return ME_GetInsertStyle(editor
, 0);
1514 void ME_SendSelChange(ME_TextEditor
*editor
)
1518 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1521 sc
.nmhdr
.hwndFrom
= NULL
;
1522 sc
.nmhdr
.idFrom
= 0;
1523 sc
.nmhdr
.code
= EN_SELCHANGE
;
1524 ME_GetSelectionOfs(editor
, &sc
.chrg
.cpMin
, &sc
.chrg
.cpMax
);
1525 sc
.seltyp
= SEL_EMPTY
;
1526 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1527 sc
.seltyp
|= SEL_TEXT
;
1528 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* what were RICHEDIT authors thinking ? */
1529 sc
.seltyp
|= SEL_MULTICHAR
;
1530 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1531 sc
.chrg
.cpMin
, sc
.chrg
.cpMax
, sc
.seltyp
,
1532 (sc
.seltyp
& SEL_TEXT
) ? "SEL_TEXT" : "",
1533 (sc
.seltyp
& SEL_MULTICHAR
) ? "SEL_MULTICHAR" : "");
1534 if (sc
.chrg
.cpMin
!= editor
->notified_cr
.cpMin
|| sc
.chrg
.cpMax
!= editor
->notified_cr
.cpMax
)
1536 ME_ClearTempStyle(editor
);
1538 editor
->notified_cr
= sc
.chrg
;
1539 ITextHost_TxNotify(editor
->texthost
, sc
.nmhdr
.code
, &sc
);
1544 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1547 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1548 ME_Cursor tmp_curs
= *p
;
1549 BOOL success
= FALSE
;
1551 ME_CheckCharOffsets(editor
);
1554 editor
->bCaretAtEnd
= 0;
1556 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1558 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1561 editor
->bCaretAtEnd
= 0;
1563 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1565 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1568 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1571 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1574 ME_ArrowPageUp(editor
, &tmp_curs
);
1577 ME_ArrowPageDown(editor
, &tmp_curs
);
1581 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1583 ME_ArrowHome(editor
, &tmp_curs
);
1584 editor
->bCaretAtEnd
= 0;
1589 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1591 ME_ArrowEnd(editor
, &tmp_curs
);
1596 editor
->pCursors
[1] = tmp_curs
;
1599 ME_InvalidateSelection(editor
);
1601 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1602 ME_EnsureVisible(editor
, &tmp_curs
);
1603 ME_ShowCaret(editor
);
1604 ME_SendSelChange(editor
);