2 * RichEdit - Caret and selection functions.
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Phil Krylov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
28 ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
);
30 int ME_GetSelectionOfs(ME_TextEditor
*editor
, int *from
, int *to
)
32 *from
= ME_GetCursorOfs(&editor
->pCursors
[0]);
33 *to
= ME_GetCursorOfs(&editor
->pCursors
[1]);
45 int ME_GetSelection(ME_TextEditor
*editor
, ME_Cursor
**from
, ME_Cursor
**to
)
47 if (ME_GetCursorOfs(&editor
->pCursors
[0]) < ME_GetCursorOfs(&editor
->pCursors
[1]))
49 *from
= &editor
->pCursors
[0];
50 *to
= &editor
->pCursors
[1];
53 *from
= &editor
->pCursors
[1];
54 *to
= &editor
->pCursors
[0];
59 int ME_GetTextLength(ME_TextEditor
*editor
)
61 ME_DisplayItem
*pLast
= editor
->pBuffer
->pLast
;
62 return ME_CharOfsFromRunOfs(editor
, pLast
->member
.para
.prev_para
,
63 ME_FindItemBack(pLast
, diRun
), 0);
67 int ME_GetTextLengthEx(ME_TextEditor
*editor
, const GETTEXTLENGTHEX
*how
)
71 if (how
->flags
& GTL_PRECISE
&& how
->flags
& GTL_CLOSE
)
73 if (how
->flags
& GTL_NUMCHARS
&& how
->flags
& GTL_NUMBYTES
)
76 length
= ME_GetTextLength(editor
);
78 if ((editor
->styleFlags
& ES_MULTILINE
)
79 && (how
->flags
& GTL_USECRLF
)
80 && !editor
->bEmulateVersion10
) /* Ignore GTL_USECRLF flag in 1.0 emulation */
81 length
+= editor
->nParagraphs
- 1;
83 if (how
->flags
& GTL_NUMBYTES
)
87 if (how
->codepage
== 1200)
89 if (how
->flags
& GTL_PRECISE
)
90 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
91 if (GetCPInfo(how
->codepage
, &cpinfo
))
92 return length
* cpinfo
.MaxCharSize
;
93 ERR("Invalid codepage %u\n", how
->codepage
);
100 int ME_SetSelection(ME_TextEditor
*editor
, int from
, int to
)
102 int selectionEnd
= 0;
103 const int len
= ME_GetTextLength(editor
);
105 /* all negative values are effectively the same */
112 if (from
== 0 && to
== -1)
114 editor
->pCursors
[1].pPara
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
115 editor
->pCursors
[1].pRun
= ME_FindItemFwd(editor
->pCursors
[1].pPara
, diRun
);
116 editor
->pCursors
[1].nOffset
= 0;
117 editor
->pCursors
[0].pPara
= editor
->pBuffer
->pLast
->member
.para
.prev_para
;
118 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
119 editor
->pCursors
[0].nOffset
= 0;
120 ME_InvalidateSelection(editor
);
121 ME_ClearTempStyle(editor
);
125 /* if both values are equal and also out of bound, that means to */
126 /* put the selection at the end of the text */
127 if ((from
== to
) && (to
< 0 || to
> len
))
133 /* if from is negative and to is positive then selection is */
134 /* deselected and caret moved to end of the current selection */
138 ME_GetSelectionOfs(editor
, &start
, &end
);
139 editor
->pCursors
[1] = editor
->pCursors
[0];
141 ME_ClearTempStyle(editor
);
145 /* adjust to if it's a negative value */
149 /* flip from and to if they are reversed */
157 /* after fiddling with the values, we find from > len && to > len */
160 /* special case with to too big */
167 editor
->pCursors
[0].pPara
= editor
->pBuffer
->pLast
->member
.para
.prev_para
;
168 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
169 editor
->pCursors
[0].nOffset
= 0;
170 editor
->pCursors
[1] = editor
->pCursors
[0];
171 ME_InvalidateSelection(editor
);
172 ME_ClearTempStyle(editor
);
176 ME_CursorFromCharOfs(editor
, from
, &editor
->pCursors
[1]);
177 ME_CursorFromCharOfs(editor
, to
, &editor
->pCursors
[0]);
178 /* Selection is not allowed in the middle of an end paragraph run. */
179 if (editor
->pCursors
[1].pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
180 editor
->pCursors
[1].nOffset
= 0;
181 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
182 editor
->pCursors
[0].nOffset
= 0;
188 ME_GetCursorCoordinates(ME_TextEditor
*editor
, ME_Cursor
*pCursor
,
189 int *x
, int *y
, int *height
)
192 ME_DisplayItem
*run
= pCursor
->pRun
;
193 ME_DisplayItem
*para
= pCursor
->pPara
;
194 ME_DisplayItem
*pSizeRun
= run
;
198 assert(height
&& x
&& y
);
199 assert(~para
->member
.para
.nFlags
& MEPF_REWRAP
);
200 assert(run
&& run
->type
== diRun
);
201 assert(para
&& para
->type
== diParagraph
);
203 row
= ME_FindItemBack(run
, diStartRowOrParagraph
);
204 assert(row
&& row
->type
== diStartRow
);
206 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
208 if (!pCursor
->nOffset
)
210 ME_DisplayItem
*prev
= ME_FindItemBack(run
, diRunOrParagraph
);
212 if (prev
->type
== diRun
)
215 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
&&
216 run
== ME_FindItemFwd(row
, diRun
))
218 ME_DisplayItem
*tmp
= ME_FindItemBack(row
, diRunOrParagraph
);
220 if (tmp
->type
== diRun
)
222 row
= ME_FindItemBack(tmp
, diStartRow
);
223 pSizeRun
= run
= tmp
;
225 assert(run
->type
== diRun
);
226 sz
= ME_GetRunSize(&c
, ¶
->member
.para
,
227 &run
->member
.run
, run
->member
.run
.strText
->nLen
,
228 row
->member
.row
.nLMargin
);
231 if (pCursor
->nOffset
) {
232 sz
= ME_GetRunSize(&c
, ¶
->member
.para
, &run
->member
.run
,
233 pCursor
->nOffset
, row
->member
.row
.nLMargin
);
236 *height
= pSizeRun
->member
.run
.nAscent
+ pSizeRun
->member
.run
.nDescent
;
237 *x
= c
.rcView
.left
+ run
->member
.run
.pt
.x
+ sz
.cx
- editor
->horz_si
.nPos
;
238 *y
= c
.rcView
.top
+ para
->member
.para
.pt
.y
+ row
->member
.row
.nBaseline
239 + run
->member
.run
.pt
.y
- pSizeRun
->member
.run
.nAscent
240 - editor
->vert_si
.nPos
;
241 ME_DestroyContext(&c
);
247 ME_MoveCaret(ME_TextEditor
*editor
)
251 ME_GetCursorCoordinates(editor
, &editor
->pCursors
[0], &x
, &y
, &height
);
252 if(editor
->bHaveFocus
&& !ME_IsSelection(editor
))
254 x
= min(x
, editor
->rcFormat
.right
-1);
255 ITextHost_TxCreateCaret(editor
->texthost
, NULL
, 0, height
);
256 ITextHost_TxSetCaretPos(editor
->texthost
, x
, y
);
261 void ME_ShowCaret(ME_TextEditor
*ed
)
264 if(ed
->bHaveFocus
&& !ME_IsSelection(ed
))
265 ITextHost_TxShowCaret(ed
->texthost
, TRUE
);
268 void ME_HideCaret(ME_TextEditor
*ed
)
270 if(!ed
->bHaveFocus
|| ME_IsSelection(ed
))
272 ITextHost_TxShowCaret(ed
->texthost
, FALSE
);
277 BOOL
ME_InternalDeleteText(ME_TextEditor
*editor
, int nOfs
, int nChars
,
282 int totalChars
= nChars
;
283 ME_DisplayItem
*start_para
;
285 /* Prevent deletion past last end of paragraph run. */
286 nChars
= min(nChars
, ME_GetTextLength(editor
) - nOfs
);
288 ME_CursorFromCharOfs(editor
, nOfs
, &c
);
289 start_para
= c
.pPara
;
293 ME_ProtectPartialTableDeletion(editor
, nOfs
, &nChars
);
301 ME_CursorFromCharOfs(editor
, nOfs
+nChars
, &c
);
303 nOfs
+nChars
== (c
.pRun
->member
.run
.nCharOfs
304 + c
.pPara
->member
.para
.nCharOfs
))
306 /* We aren't deleting anything in this run, so we will go back to the
307 * last run we are deleting text in. */
308 ME_PrevRun(&c
.pPara
, &c
.pRun
);
309 c
.nOffset
= c
.pRun
->member
.run
.strText
->nLen
;
311 run
= &c
.pRun
->member
.run
;
312 if (run
->nFlags
& MERF_ENDPARA
) {
313 int eollen
= c
.pRun
->member
.run
.strText
->nLen
;
314 BOOL keepFirstParaFormat
;
316 if (!ME_FindItemFwd(c
.pRun
, diParagraph
))
320 keepFirstParaFormat
= (totalChars
== nChars
&& nChars
<= eollen
&&
322 if (!editor
->bEmulateVersion10
) /* v4.1 */
324 ME_DisplayItem
*next_para
= ME_FindItemFwd(c
.pRun
, diParagraphOrEnd
);
325 ME_DisplayItem
*this_para
= next_para
->member
.para
.prev_para
;
327 /* The end of paragraph before a table row is only deleted if there
328 * is nothing else on the line before it. */
329 if (this_para
== start_para
&&
330 next_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
332 /* If the paragraph will be empty, then it should be deleted, however
333 * it still might have text right now which would inherit the
334 * MEPF_STARTROW property if we joined it right now.
335 * Instead we will delete it after the preceding text is deleted. */
336 if (nOfs
> this_para
->member
.para
.nCharOfs
) {
337 /* Skip this end of line. */
338 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
341 keepFirstParaFormat
= TRUE
;
344 ME_JoinParagraphs(editor
, c
.pPara
, keepFirstParaFormat
);
345 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
346 ME_CheckCharOffsets(editor
);
347 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
353 int nCharsToDelete
= min(nChars
, c
.nOffset
);
356 c
.nOffset
-= nCharsToDelete
;
358 ME_FindItemBack(c
.pRun
, diParagraph
)->member
.para
.nFlags
|= MEPF_REWRAP
;
361 /* nChars is the number of characters that should be deleted from the
362 PRECEDING runs (these BEFORE cursor.pRun)
363 nCharsToDelete is a number of chars to delete from THIS run */
364 nChars
-= nCharsToDelete
;
365 shift
-= nCharsToDelete
;
366 TRACE("Deleting %d (remaning %d) chars at %d in '%s' (%d)\n",
367 nCharsToDelete
, nChars
, c
.nOffset
,
368 debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
370 if (!c
.nOffset
&& run
->strText
->nLen
== nCharsToDelete
)
372 /* undo = reinsert whole run */
373 /* nOfs is a character offset (from the start of the document
374 to the current (deleted) run */
375 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
377 pUndo
->di
.member
.run
.nCharOfs
= nOfs
+nChars
;
381 /* undo = reinsert partial run */
382 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
384 ME_DestroyString(pUndo
->di
.member
.run
.strText
);
385 pUndo
->di
.member
.run
.nCharOfs
= nOfs
+nChars
;
386 pUndo
->di
.member
.run
.strText
= ME_MakeStringN(run
->strText
->szData
+c
.nOffset
, nCharsToDelete
);
389 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
390 TRACE("Shift value: %d\n", shift
);
391 ME_StrDeleteV(run
->strText
, c
.nOffset
, nCharsToDelete
);
393 /* update cursors (including c) */
394 for (i
=-1; i
<editor
->nCursors
; i
++) {
395 ME_Cursor
*pThisCur
= editor
->pCursors
+ i
;
396 if (i
== -1) pThisCur
= &c
;
397 if (pThisCur
->pRun
== cursor
.pRun
) {
398 if (pThisCur
->nOffset
> cursor
.nOffset
) {
399 if (pThisCur
->nOffset
-cursor
.nOffset
< nCharsToDelete
)
400 pThisCur
->nOffset
= cursor
.nOffset
;
402 pThisCur
->nOffset
-= nCharsToDelete
;
403 assert(pThisCur
->nOffset
>= 0);
404 assert(pThisCur
->nOffset
<= run
->strText
->nLen
);
406 if (pThisCur
->nOffset
== run
->strText
->nLen
)
408 pThisCur
->pRun
= ME_FindItemFwd(pThisCur
->pRun
, diRunOrParagraphOrEnd
);
409 assert(pThisCur
->pRun
->type
== diRun
);
410 pThisCur
->nOffset
= 0;
415 /* c = updated data now */
417 if (c
.pRun
== cursor
.pRun
)
418 ME_SkipAndPropagateCharOffset(c
.pRun
, shift
);
420 ME_PropagateCharOffset(c
.pRun
, shift
);
422 if (!cursor
.pRun
->member
.run
.strText
->nLen
)
424 TRACE("Removing useless run\n");
425 ME_Remove(cursor
.pRun
);
426 ME_DestroyDisplayItem(cursor
.pRun
);
431 ME_CheckCharOffsets(editor);
439 BOOL
ME_DeleteTextAtCursor(ME_TextEditor
*editor
, int nCursor
, int nChars
)
441 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
442 /* text operations set modified state */
443 editor
->nModifyStep
= 1;
444 return ME_InternalDeleteText(editor
, ME_GetCursorOfs(&editor
->pCursors
[nCursor
]),
448 static ME_DisplayItem
*
449 ME_InternalInsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
450 const WCHAR
*str
, int len
, ME_Style
*style
,
453 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
455 editor
->bCaretAtEnd
= FALSE
;
457 assert(p
->pRun
->type
== diRun
);
459 return ME_InsertRunAtCursor(editor
, p
, style
, str
, len
, flags
);
463 void ME_InsertOLEFromCursor(ME_TextEditor
*editor
, const REOBJECT
* reo
, int nCursor
)
465 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
470 if (ME_IsSelection(editor
))
471 ME_DeleteSelection(editor
);
473 di
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
475 di
->member
.run
.ole_obj
= ALLOC_OBJ(*reo
);
476 ME_CopyReObject(di
->member
.run
.ole_obj
, reo
);
477 ME_ReleaseStyle(pStyle
);
481 void ME_InsertEndRowFromCursor(ME_TextEditor
*editor
, int nCursor
)
483 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
488 if (ME_IsSelection(editor
))
489 ME_DeleteSelection(editor
);
491 di
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
493 ME_ReleaseStyle(pStyle
);
497 void ME_InsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
498 const WCHAR
*str
, int len
, ME_Style
*style
)
504 /* FIXME really HERE ? */
505 if (ME_IsSelection(editor
))
506 ME_DeleteSelection(editor
);
508 /* FIXME: is this too slow? */
509 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
510 oldLen
= ME_GetTextLength(editor
);
512 /* text operations set modified state */
513 editor
->nModifyStep
= 1;
517 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
521 /* grow the text limit to fit our text */
522 if(editor
->nTextLimit
< oldLen
+len
)
523 editor
->nTextLimit
= oldLen
+ len
;
529 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
530 while(pos
- str
< len
&& *pos
!= '\r' && *pos
!= '\n' && *pos
!= '\t')
533 if (pos
!= str
) { /* handle text */
534 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
535 } else if (*pos
== '\t') { /* handle tabs */
537 ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, style
, MERF_TAB
);
539 } else { /* handle EOLs */
540 ME_DisplayItem
*tp
, *end_run
;
544 /* Find number of CR and LF in end of paragraph run */
547 if (len
> 1 && pos
[1] == '\n')
549 else if (len
> 2 && pos
[1] == '\r' && pos
[2] == '\n')
554 assert(*pos
== '\n');
559 if (!editor
->bEmulateVersion10
&& eol_len
== 3)
561 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
563 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, style
, 0);
567 if (!editor
->bEmulateVersion10
) {
569 eol_str
= ME_MakeStringN(&cr
, 1);
571 eol_str
= ME_MakeStringN(str
, eol_len
);
574 p
= &editor
->pCursors
[nCursor
];
576 ME_SplitRunSimple(editor
, p
->pRun
, p
->nOffset
);
577 p
= &editor
->pCursors
[nCursor
];
579 tmp_style
= ME_GetInsertStyle(editor
, nCursor
);
580 /* ME_SplitParagraph increases style refcount */
581 tp
= ME_SplitParagraph(editor
, p
->pRun
, p
->pRun
->member
.run
.style
, eol_str
, 0);
582 p
->pRun
= ME_FindItemFwd(tp
, diRun
);
584 end_run
= ME_FindItemBack(tp
, diRun
);
585 ME_ReleaseStyle(end_run
->member
.run
.style
);
586 end_run
->member
.run
.style
= tmp_style
;
597 ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
599 ME_DisplayItem
*pRun
= pCursor
->pRun
;
603 if (!pCursor
->nOffset
)
605 ME_DisplayItem
*pPara
= pCursor
->pPara
;
607 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
615 if (pPara
->member
.para
.prev_para
->type
== diTextStart
)
617 pRun
= ME_FindItemBack(pPara
, diRunOrParagraph
);
618 pPara
= pPara
->member
.para
.prev_para
;
619 /* every paragraph ought to have at least one run */
620 assert(pRun
&& pRun
->type
== diRun
);
621 assert(pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
624 assert(pRun
->type
!= diRun
&& pRun
->type
!= diParagraph
);
627 } while (RUN_IS_HIDDEN(&pRun
->member
.run
) ||
628 pRun
->member
.run
.nFlags
& MERF_HIDDEN
);
629 pCursor
->pPara
= pPara
;
630 pCursor
->pRun
= pRun
;
631 if (pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
632 pCursor
->nOffset
= 0;
634 pCursor
->nOffset
= pRun
->member
.run
.strText
->nLen
;
637 if (pCursor
->nOffset
)
638 pCursor
->nOffset
= pCursor
->nOffset
+ nRelOfs
;
643 if (!(pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
645 int new_ofs
= pCursor
->nOffset
+ nRelOfs
;
647 if (new_ofs
< pRun
->member
.run
.strText
->nLen
)
649 pCursor
->nOffset
= new_ofs
;
654 pRun
= ME_FindItemFwd(pRun
, diRun
);
655 } while (pRun
&& (RUN_IS_HIDDEN(&pRun
->member
.run
) ||
656 pRun
->member
.run
.nFlags
& MERF_HIDDEN
));
659 pCursor
->pPara
= ME_GetParagraph(pRun
);
660 pCursor
->pRun
= pRun
;
661 pCursor
->nOffset
= 0;
670 ME_MoveCursorWords(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
672 ME_DisplayItem
*pRun
= cursor
->pRun
, *pOtherRun
;
673 ME_DisplayItem
*pPara
= cursor
->pPara
;
674 int nOffset
= cursor
->nOffset
;
678 /* Backward movement */
681 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
682 nOffset
, WB_MOVEWORDLEFT
);
685 pOtherRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
686 if (pOtherRun
->type
== diRun
)
688 if (ME_CallWordBreakProc(editor
, pOtherRun
->member
.run
.strText
,
689 pOtherRun
->member
.run
.strText
->nLen
- 1,
691 && !(pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
692 && !(cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
693 && !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
, 0,
697 nOffset
= pOtherRun
->member
.run
.strText
->nLen
;
699 else if (pOtherRun
->type
== diParagraph
)
701 if (cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
704 /* Skip empty start of table row paragraph */
705 if (pPara
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
706 pPara
= pPara
->member
.para
.prev_para
;
707 /* Paragraph breaks are treated as separate words */
708 if (pPara
->member
.para
.prev_para
->type
== diTextStart
)
711 pRun
= ME_FindItemBack(pPara
, diRun
);
719 /* Forward movement */
720 BOOL last_delim
= FALSE
;
724 if (last_delim
&& !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
725 nOffset
, WB_ISDELIMITER
))
727 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
728 nOffset
, WB_MOVEWORDRIGHT
);
729 if (nOffset
< pRun
->member
.run
.strText
->nLen
)
731 pOtherRun
= ME_FindItemFwd(pRun
, diRunOrParagraphOrEnd
);
732 if (pOtherRun
->type
== diRun
)
734 last_delim
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
735 nOffset
- 1, WB_ISDELIMITER
);
739 else if (pOtherRun
->type
== diParagraph
)
741 if (pOtherRun
->member
.para
.nFlags
& MEPF_ROWSTART
)
742 pOtherRun
= pOtherRun
->member
.para
.next_para
;
743 if (cursor
->pRun
== pRun
) {
745 pRun
= ME_FindItemFwd(pPara
, diRun
);
752 if (cursor
->pRun
== pRun
)
759 cursor
->pPara
= pPara
;
761 cursor
->nOffset
= nOffset
;
767 ME_SelectByType(ME_TextEditor
*editor
, ME_SelectionType selectionType
)
769 /* pCursor[0] is the end of the selection
770 * pCursor[1] is the start of the selection (or the position selection anchor)
771 * pCursor[2] and [3] are the selection anchors that are backed up
772 * so they are kept when the selection changes for drag selection.
775 editor
->nSelectionType
= selectionType
;
776 switch(selectionType
)
781 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
782 editor
->pCursors
[1] = editor
->pCursors
[0];
783 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
788 ME_DisplayItem
*pItem
;
789 ME_DIType fwdSearchType
, backSearchType
;
790 if (selectionType
== stParagraph
) {
791 backSearchType
= diParagraph
;
792 fwdSearchType
= diParagraphOrEnd
;
794 backSearchType
= diStartRow
;
795 fwdSearchType
= diStartRowOrParagraphOrEnd
;
797 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, fwdSearchType
);
799 if (pItem
->type
== diTextEnd
)
800 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
802 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
803 editor
->pCursors
[0].pPara
= ME_GetParagraph(editor
->pCursors
[0].pRun
);
804 editor
->pCursors
[0].nOffset
= 0;
806 pItem
= ME_FindItemBack(pItem
, backSearchType
);
807 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
808 editor
->pCursors
[1].pPara
= ME_GetParagraph(editor
->pCursors
[1].pRun
);
809 editor
->pCursors
[1].nOffset
= 0;
813 /* Select everything with cursor anchored from the start of the text */
814 editor
->nSelectionType
= stDocument
;
815 editor
->pCursors
[1].pPara
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
816 editor
->pCursors
[1].pRun
= ME_FindItemFwd(editor
->pCursors
[1].pPara
, diRun
);
817 editor
->pCursors
[1].nOffset
= 0;
818 editor
->pCursors
[0].pPara
= editor
->pBuffer
->pLast
->member
.para
.prev_para
;
819 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
820 editor
->pCursors
[0].nOffset
= 0;
824 /* Store the anchor positions for extending the selection. */
825 editor
->pCursors
[2] = editor
->pCursors
[0];
826 editor
->pCursors
[3] = editor
->pCursors
[1];
829 int ME_GetCursorOfs(const ME_Cursor
*cursor
)
831 return cursor
->pPara
->member
.para
.nCharOfs
832 + cursor
->pRun
->member
.run
.nCharOfs
+ cursor
->nOffset
;
835 /* Helper function for ME_FindPixelPos to find paragraph within tables */
836 static ME_DisplayItem
* ME_FindPixelPosInTableRow(int x
, int y
,
837 ME_DisplayItem
*para
)
839 ME_DisplayItem
*cell
, *next_cell
;
840 assert(para
->member
.para
.nFlags
& MEPF_ROWSTART
);
841 cell
= para
->member
.para
.next_para
->member
.para
.pCell
;
844 /* find the cell we are in */
845 while ((next_cell
= cell
->member
.cell
.next_cell
) != NULL
) {
846 if (x
< next_cell
->member
.cell
.pt
.x
)
848 para
= ME_FindItemFwd(cell
, diParagraph
);
849 /* Found the cell, but there might be multiple paragraphs in
850 * the cell, so need to search down the cell for the paragraph. */
851 while (cell
== para
->member
.para
.pCell
) {
852 if (y
< para
->member
.para
.pt
.y
+ para
->member
.para
.nHeight
)
854 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
855 return ME_FindPixelPosInTableRow(x
, y
, para
);
859 para
= para
->member
.para
.next_para
;
861 /* Past the end of the cell, so go back to the last cell paragraph */
862 return para
->member
.para
.prev_para
;
866 /* Return table row delimiter */
867 para
= ME_FindItemFwd(cell
, diParagraph
);
868 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
869 assert(para
->member
.para
.pFmt
->dwMask
& PFM_TABLEROWDELIMITER
);
870 assert(para
->member
.para
.pFmt
->wEffects
& PFE_TABLEROWDELIMITER
);
874 static BOOL
ME_ReturnFoundPos(ME_TextEditor
*editor
, ME_DisplayItem
*found
,
875 ME_Cursor
*result
, int rx
, BOOL isExact
)
878 assert(found
->type
== diRun
);
879 if ((found
->member
.run
.nFlags
& MERF_ENDPARA
) || rx
< 0)
881 result
->pRun
= found
;
882 result
->nOffset
= ME_CharFromPointCursor(editor
, rx
, &found
->member
.run
);
883 if (editor
->pCursors
[0].nOffset
== found
->member
.run
.strText
->nLen
&& rx
)
885 result
->pRun
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, diRun
);
888 result
->pPara
= ME_GetParagraph(result
->pRun
);
892 /* Finds the run and offset from the pixel position.
894 * x & y are pixel positions in virtual coordinates into the rich edit control,
895 * so client coordinates must first be adjusted by the scroll position.
897 * returns TRUE if the result was exactly under the cursor, otherwise returns
898 * FALSE, and result is set to the closest position to the coordinates.
900 static BOOL
ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
,
901 ME_Cursor
*result
, BOOL
*is_eol
)
903 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
904 ME_DisplayItem
*last
= NULL
;
908 x
-= editor
->rcFormat
.left
;
909 y
-= editor
->rcFormat
.top
;
915 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->member
.para
.next_para
)
917 assert(p
->type
== diParagraph
);
918 if (y
< p
->member
.para
.pt
.y
+ p
->member
.para
.nHeight
)
920 if (p
->member
.para
.nFlags
& MEPF_ROWSTART
)
921 p
= ME_FindPixelPosInTableRow(x
, y
, p
);
922 y
-= p
->member
.para
.pt
.y
;
923 p
= ME_FindItemFwd(p
, diStartRow
);
925 } else if (p
->member
.para
.nFlags
& MEPF_ROWSTART
) {
926 p
= ME_GetTableRowEnd(p
);
930 for (; p
!= editor
->pBuffer
->pLast
; )
933 assert(p
->type
== diStartRow
);
934 if (y
< p
->member
.row
.pt
.y
+ p
->member
.row
.nHeight
)
936 p
= ME_FindItemFwd(p
, diRun
);
939 pp
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
940 if (pp
->type
!= diStartRow
)
942 p
= ME_FindItemFwd(p
, diRun
);
947 if (p
== editor
->pBuffer
->pLast
)
949 /* The position is below the last paragraph, so the last row will be used
950 * rather than the end of the text, so the x position will be used to
951 * determine the offset closest to the pixel position. */
953 p
= ME_FindItemBack(p
, diStartRow
);
955 p
= ME_FindItemFwd(p
, diRun
);
959 p
= editor
->pBuffer
->pLast
;
962 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->next
)
967 rx
= x
- p
->member
.run
.pt
.x
;
968 if (rx
< p
->member
.run
.nWidth
)
969 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
973 p
= ME_FindItemFwd(p
, diRun
);
974 if (is_eol
) *is_eol
= 1;
975 rx
= 0; /* FIXME not sure */
976 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
981 rx
= 0; /* FIXME not sure */
983 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
988 result
->pRun
= ME_FindItemBack(p
, diRun
);
989 result
->pPara
= ME_GetParagraph(result
->pRun
);
991 assert(result
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
996 /* Returns the character offset closest to the pixel position
998 * x & y are pixel positions in client coordinates.
1000 * isExact will be set to TRUE if the run is directly under the pixel
1001 * position, FALSE if it not, unless isExact is set to NULL.
1003 int ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
, BOOL
*isExact
)
1009 ITextHost_TxGetClientRect(editor
->texthost
, &rc
);
1010 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
) {
1011 if (isExact
) *isExact
= FALSE
;
1014 x
+= editor
->horz_si
.nPos
;
1015 y
+= editor
->vert_si
.nPos
;
1016 bResult
= ME_FindPixelPos(editor
, x
, y
, &cursor
, NULL
);
1017 if (isExact
) *isExact
= bResult
;
1018 return cursor
.pPara
->member
.para
.nCharOfs
1019 + cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
;
1024 /* Extends the selection with a word, line, or paragraph selection type.
1026 * The selection is anchored by editor->pCursors[2-3] such that the text
1027 * between the anchors will remain selected, and one end will be extended.
1029 * editor->pCursors[0] should have the position to extend the selection to
1030 * before this function is called.
1032 * Nothing will be done if editor->nSelectionType equals stPosition.
1034 static void ME_ExtendAnchorSelection(ME_TextEditor
*editor
)
1036 ME_Cursor tmp_cursor
;
1037 int curOfs
, anchorStartOfs
, anchorEndOfs
;
1038 if (editor
->nSelectionType
== stPosition
|| editor
->nSelectionType
== stDocument
)
1040 curOfs
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1041 anchorStartOfs
= ME_GetCursorOfs(&editor
->pCursors
[3]);
1042 anchorEndOfs
= ME_GetCursorOfs(&editor
->pCursors
[2]);
1044 tmp_cursor
= editor
->pCursors
[0];
1045 editor
->pCursors
[0] = editor
->pCursors
[2];
1046 editor
->pCursors
[1] = editor
->pCursors
[3];
1047 if (curOfs
< anchorStartOfs
)
1049 /* Extend the left side of selection */
1050 editor
->pCursors
[1] = tmp_cursor
;
1051 if (editor
->nSelectionType
== stWord
)
1052 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
1055 ME_DisplayItem
*pItem
;
1056 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1057 diStartRowOrParagraph
:diParagraph
);
1058 pItem
= ME_FindItemBack(editor
->pCursors
[1].pRun
, searchType
);
1059 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
1060 editor
->pCursors
[1].pPara
= ME_GetParagraph(editor
->pCursors
[1].pRun
);
1061 editor
->pCursors
[1].nOffset
= 0;
1064 else if (curOfs
>= anchorEndOfs
)
1066 /* Extend the right side of selection */
1067 editor
->pCursors
[0] = tmp_cursor
;
1068 if (editor
->nSelectionType
== stWord
)
1069 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
1072 ME_DisplayItem
*pItem
;
1073 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1074 diStartRowOrParagraphOrEnd
:diParagraphOrEnd
);
1075 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, searchType
);
1076 if (pItem
->type
== diTextEnd
)
1077 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
1079 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
1080 editor
->pCursors
[0].pPara
= ME_GetParagraph(editor
->pCursors
[0].pRun
);
1081 editor
->pCursors
[0].nOffset
= 0;
1086 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
, int clickNum
)
1088 ME_Cursor tmp_cursor
;
1089 int is_selection
= 0;
1092 editor
->nUDArrowX
= -1;
1094 x
+= editor
->horz_si
.nPos
;
1095 y
+= editor
->vert_si
.nPos
;
1097 tmp_cursor
= editor
->pCursors
[0];
1098 is_selection
= ME_IsSelection(editor
);
1099 is_shift
= GetKeyState(VK_SHIFT
) < 0;
1101 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
1103 if (x
>= editor
->rcFormat
.left
|| is_shift
)
1107 editor
->pCursors
[1] = editor
->pCursors
[0];
1109 if (x
>= editor
->rcFormat
.left
)
1110 ME_SelectByType(editor
, stWord
);
1112 ME_SelectByType(editor
, stParagraph
);
1113 } else if (clickNum
% 2 == 0) {
1114 ME_SelectByType(editor
, stWord
);
1116 ME_SelectByType(editor
, stParagraph
);
1121 editor
->nSelectionType
= stPosition
;
1122 editor
->pCursors
[1] = editor
->pCursors
[0];
1124 else if (!is_selection
)
1126 editor
->nSelectionType
= stPosition
;
1127 editor
->pCursors
[1] = tmp_cursor
;
1129 else if (editor
->nSelectionType
!= stPosition
)
1131 ME_ExtendAnchorSelection(editor
);
1137 ME_SelectByType(editor
, stLine
);
1138 } else if (clickNum
% 2 == 0 || is_shift
) {
1139 ME_SelectByType(editor
, stParagraph
);
1141 ME_SelectByType(editor
, stDocument
);
1144 ME_InvalidateSelection(editor
);
1145 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1146 ME_ShowCaret(editor
);
1147 ME_ClearTempStyle(editor
);
1148 ME_SendSelChange(editor
);
1151 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
1153 ME_Cursor tmp_cursor
;
1155 if (editor
->nSelectionType
== stDocument
)
1157 x
+= editor
->horz_si
.nPos
;
1158 y
+= editor
->vert_si
.nPos
;
1160 tmp_cursor
= editor
->pCursors
[0];
1161 /* FIXME: do something with the return value of ME_FindPixelPos */
1162 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
1164 ME_InvalidateSelection(editor
);
1165 editor
->pCursors
[0] = tmp_cursor
;
1166 ME_ExtendAnchorSelection(editor
);
1168 if (editor
->nSelectionType
!= stPosition
&&
1169 memcmp(&editor
->pCursors
[1], &editor
->pCursors
[3], sizeof(ME_Cursor
)))
1171 /* The scroll the cursor towards the other end, since it was the one
1172 * extended by ME_ExtendAnchorSelection */
1173 ME_EnsureVisible(editor
, &editor
->pCursors
[1]);
1175 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
1178 ME_InvalidateSelection(editor
);
1179 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1180 ME_ShowCaret(editor
);
1181 ME_SendSelChange(editor
);
1184 static ME_DisplayItem
*ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
1185 int x
, int *pOffset
, int *pbCaretAtEnd
)
1187 ME_DisplayItem
*pNext
, *pLastRun
;
1188 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
1189 assert(pNext
->type
== diRun
);
1191 if (pbCaretAtEnd
) *pbCaretAtEnd
= FALSE
;
1192 if (pOffset
) *pOffset
= 0;
1194 int run_x
= pNext
->member
.run
.pt
.x
;
1195 int width
= pNext
->member
.run
.nWidth
;
1200 if (x
>= run_x
&& x
< run_x
+width
)
1202 int ch
= ME_CharFromPointCursor(editor
, x
-run_x
, &pNext
->member
.run
);
1203 ME_String
*s
= pNext
->member
.run
.strText
;
1211 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
1212 } while(pNext
&& pNext
->type
== diRun
);
1214 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
1216 pNext
= ME_FindItemFwd(pNext
, diRun
);
1217 if (pbCaretAtEnd
) *pbCaretAtEnd
= TRUE
;
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 pCursor
->pRun
= ME_FindRunInRow(editor
, pItem
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1323 pCursor
->pPara
= ME_GetParagraph(pCursor
->pRun
);
1324 assert(pCursor
->pRun
);
1325 assert(pCursor
->pRun
->type
== diRun
);
1328 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1330 ME_DisplayItem
*p
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diStartRow
);
1332 if (editor
->vert_si
.nPos
< p
->member
.row
.nHeight
)
1334 pCursor
->pPara
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
1335 pCursor
->pRun
= ME_FindItemFwd(pCursor
->pPara
, diRun
);
1336 pCursor
->nOffset
= 0;
1337 editor
->bCaretAtEnd
= FALSE
;
1338 /* Native clears seems to clear this x value on page up at the top
1339 * of the text, but not on page down at the end of the text.
1340 * Doesn't make sense, but we try to be bug for bug compatible. */
1341 editor
->nUDArrowX
= -1;
1343 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1344 ME_DisplayItem
*pLast
;
1345 int x
, y
, ys
, yd
, yp
, yprev
;
1346 int yOldScrollPos
= editor
->vert_si
.nPos
;
1348 x
= ME_GetXForArrow(editor
, pCursor
);
1349 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1350 pRun
= ME_FindItemBack(pRun
, diRun
);
1352 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1353 assert(p
->type
== diStartRow
);
1354 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1355 yprev
= ys
= y
= yp
+ p
->member
.row
.pt
.y
;
1357 ME_ScrollUp(editor
, editor
->sizeWindow
.cy
);
1358 /* Only move the cursor by the amount scrolled. */
1359 yd
= y
+ editor
->vert_si
.nPos
- yOldScrollPos
;
1363 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
1366 if (p
->type
== diParagraph
) { /* crossing paragraphs */
1367 if (p
->member
.para
.prev_para
== NULL
)
1369 yp
= p
->member
.para
.prev_para
->member
.para
.pt
.y
;
1372 y
= yp
+ p
->member
.row
.pt
.y
;
1379 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
,
1380 &editor
->bCaretAtEnd
);
1381 pCursor
->pPara
= ME_GetParagraph(pCursor
->pRun
);
1383 assert(pCursor
->pRun
);
1384 assert(pCursor
->pRun
->type
== diRun
);
1387 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1389 ME_DisplayItem
*pLast
;
1392 /* Find y position of the last row */
1393 pLast
= editor
->pBuffer
->pLast
;
1394 y
= pLast
->member
.para
.prev_para
->member
.para
.pt
.y
1395 + ME_FindItemBack(pLast
, diStartRow
)->member
.row
.pt
.y
;
1397 x
= ME_GetXForArrow(editor
, pCursor
);
1399 if (editor
->vert_si
.nPos
>= y
- editor
->sizeWindow
.cy
)
1401 pCursor
->pPara
= editor
->pBuffer
->pLast
->member
.para
.prev_para
;
1402 pCursor
->pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
1403 pCursor
->nOffset
= 0;
1404 editor
->bCaretAtEnd
= FALSE
;
1406 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1408 int ys
, yd
, yp
, yprev
;
1409 int yOldScrollPos
= editor
->vert_si
.nPos
;
1411 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1412 pRun
= ME_FindItemBack(pRun
, diRun
);
1414 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1415 assert(p
->type
== diStartRow
);
1416 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1417 yprev
= ys
= y
= yp
+ p
->member
.row
.pt
.y
;
1419 /* For native richedit controls:
1420 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1421 * v4.1 can scroll past this position here. */
1422 ME_ScrollDown(editor
, editor
->sizeWindow
.cy
);
1423 /* Only move the cursor by the amount scrolled. */
1424 yd
= y
+ editor
->vert_si
.nPos
- yOldScrollPos
;
1428 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
1431 if (p
->type
== diParagraph
) {
1432 yp
= p
->member
.para
.pt
.y
;
1435 y
= yp
+ p
->member
.row
.pt
.y
;
1442 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
,
1443 &editor
->bCaretAtEnd
);
1444 pCursor
->pPara
= ME_GetParagraph(pCursor
->pRun
);
1446 assert(pCursor
->pRun
);
1447 assert(pCursor
->pRun
->type
== diRun
);
1450 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1452 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
1454 ME_DisplayItem
*pRun
;
1455 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
1456 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1460 pRun
= ME_FindItemFwd(pRow
, diRun
);
1462 pCursor
->pRun
= pRun
;
1463 assert(pCursor
->pPara
== ME_GetParagraph(pRun
));
1464 pCursor
->nOffset
= 0;
1467 editor
->bCaretAtEnd
= FALSE
;
1470 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1472 pCursor
->pPara
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
1473 pCursor
->pRun
= ME_FindItemFwd(pCursor
->pPara
, diRun
);
1474 pCursor
->nOffset
= 0;
1475 editor
->bCaretAtEnd
= FALSE
;
1478 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1480 ME_DisplayItem
*pRow
;
1482 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1485 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1487 if (pRow
->type
== diStartRow
) {
1488 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1490 pCursor
->pRun
= pRun
;
1491 assert(pCursor
->pPara
== ME_GetParagraph(pCursor
->pRun
));
1492 pCursor
->nOffset
= 0;
1493 editor
->bCaretAtEnd
= TRUE
;
1496 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1497 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1498 assert(pCursor
->pPara
== ME_GetParagraph(pCursor
->pRun
));
1499 pCursor
->nOffset
= 0;
1500 editor
->bCaretAtEnd
= FALSE
;
1503 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1505 pCursor
->pPara
= editor
->pBuffer
->pLast
->member
.para
.prev_para
;
1506 pCursor
->pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
1507 assert(pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1508 pCursor
->nOffset
= 0;
1509 editor
->bCaretAtEnd
= FALSE
;
1512 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1514 return editor
->pCursors
[0].pRun
!= editor
->pCursors
[1].pRun
||
1515 editor
->pCursors
[0].nOffset
!= editor
->pCursors
[1].nOffset
;
1518 void ME_DeleteSelection(ME_TextEditor
*editor
)
1521 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
1522 ME_DeleteTextAtCursor(editor
, nStartCursor
, to
- from
);
1525 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1527 return ME_GetInsertStyle(editor
, 0);
1530 void ME_SendSelChange(ME_TextEditor
*editor
)
1534 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1537 sc
.nmhdr
.code
= EN_SELCHANGE
;
1538 ME_GetSelectionOfs(editor
, &sc
.chrg
.cpMin
, &sc
.chrg
.cpMax
);
1539 sc
.seltyp
= SEL_EMPTY
;
1540 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1541 sc
.seltyp
|= SEL_TEXT
;
1542 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* what were RICHEDIT authors thinking ? */
1543 sc
.seltyp
|= SEL_MULTICHAR
;
1544 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1545 sc
.chrg
.cpMin
, sc
.chrg
.cpMax
, sc
.seltyp
,
1546 (sc
.seltyp
& SEL_TEXT
) ? "SEL_TEXT" : "",
1547 (sc
.seltyp
& SEL_MULTICHAR
) ? "SEL_MULTICHAR" : "");
1548 if (sc
.chrg
.cpMin
!= editor
->notified_cr
.cpMin
|| sc
.chrg
.cpMax
!= editor
->notified_cr
.cpMax
)
1550 ME_ClearTempStyle(editor
);
1552 editor
->notified_cr
= sc
.chrg
;
1553 ITextHost_TxNotify(editor
->texthost
, sc
.nmhdr
.code
, &sc
);
1558 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1561 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1562 ME_Cursor tmp_curs
= *p
;
1563 BOOL success
= FALSE
;
1565 ME_CheckCharOffsets(editor
);
1568 editor
->bCaretAtEnd
= 0;
1570 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1572 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1575 editor
->bCaretAtEnd
= 0;
1577 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1579 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1582 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1585 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1588 ME_ArrowPageUp(editor
, &tmp_curs
);
1591 ME_ArrowPageDown(editor
, &tmp_curs
);
1595 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1597 ME_ArrowHome(editor
, &tmp_curs
);
1598 editor
->bCaretAtEnd
= 0;
1603 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1605 ME_ArrowEnd(editor
, &tmp_curs
);
1610 editor
->pCursors
[1] = tmp_curs
;
1613 ME_InvalidateSelection(editor
);
1615 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1616 ME_EnsureVisible(editor
, &tmp_curs
);
1617 ME_ShowCaret(editor
);
1618 ME_SendSelChange(editor
);