2 * RichEdit - Caret and selection functions.
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Phil Krylov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
28 ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
);
30 void ME_GetSelection(ME_TextEditor
*editor
, int *from
, int *to
)
32 *from
= ME_GetCursorOfs(editor
, 0);
33 *to
= ME_GetCursorOfs(editor
, 1);
43 int ME_GetTextLength(ME_TextEditor
*editor
)
45 ME_DisplayItem
*pLast
= editor
->pBuffer
->pLast
;
46 return ME_CharOfsFromRunOfs(editor
, pLast
->member
.para
.prev_para
,
47 ME_FindItemBack(pLast
, diRun
), 0);
51 int ME_GetTextLengthEx(ME_TextEditor
*editor
, const GETTEXTLENGTHEX
*how
)
55 if (how
->flags
& GTL_PRECISE
&& how
->flags
& GTL_CLOSE
)
57 if (how
->flags
& GTL_NUMCHARS
&& how
->flags
& GTL_NUMBYTES
)
60 length
= ME_GetTextLength(editor
);
62 if ((editor
->styleFlags
& ES_MULTILINE
)
63 && (how
->flags
& GTL_USECRLF
)
64 && !editor
->bEmulateVersion10
) /* Ignore GTL_USECRLF flag in 1.0 emulation */
65 length
+= editor
->nParagraphs
- 1;
67 if (how
->flags
& GTL_NUMBYTES
)
71 if (how
->codepage
== 1200)
73 if (how
->flags
& GTL_PRECISE
)
74 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
75 if (GetCPInfo(how
->codepage
, &cpinfo
))
76 return length
* cpinfo
.MaxCharSize
;
77 ERR("Invalid codepage %u\n", how
->codepage
);
84 int ME_SetSelection(ME_TextEditor
*editor
, int from
, int to
)
87 const int len
= ME_GetTextLength(editor
);
89 /* all negative values are effectively the same */
96 if (from
== 0 && to
== -1)
98 editor
->pCursors
[1].pRun
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
);
99 editor
->pCursors
[1].nOffset
= 0;
100 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
101 editor
->pCursors
[0].nOffset
= 0;
102 ME_InvalidateSelection(editor
);
103 ME_ClearTempStyle(editor
);
107 /* if both values are equal and also out of bound, that means to */
108 /* put the selection at the end of the text */
109 if ((from
== to
) && (to
< 0 || to
> len
))
115 /* if from is negative and to is positive then selection is */
116 /* deselected and caret moved to end of the current selection */
120 ME_GetSelection(editor
, &start
, &end
);
121 editor
->pCursors
[1] = editor
->pCursors
[0];
123 ME_ClearTempStyle(editor
);
127 /* adjust to if it's a negative value */
131 /* flip from and to if they are reversed */
139 /* after fiddling with the values, we find from > len && to > len */
142 /* special case with to too big */
149 editor
->pCursors
[1].pRun
= editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
150 editor
->pCursors
[1].nOffset
= editor
->pCursors
[0].nOffset
= 0;
151 ME_InvalidateSelection(editor
);
152 ME_ClearTempStyle(editor
);
156 ME_CursorFromCharOfs(editor
, from
, &editor
->pCursors
[1]);
157 ME_CursorFromCharOfs(editor
, to
, &editor
->pCursors
[0]);
158 /* Selection is not allowed in the middle of an end paragraph run. */
159 if (editor
->pCursors
[1].pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
160 editor
->pCursors
[1].nOffset
= 0;
161 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
162 editor
->pCursors
[0].nOffset
= 0;
168 ME_GetCursorCoordinates(ME_TextEditor
*editor
, ME_Cursor
*pCursor
,
169 int *x
, int *y
, int *height
)
171 ME_DisplayItem
*pCursorRun
= pCursor
->pRun
;
172 ME_DisplayItem
*pSizeRun
= pCursor
->pRun
;
174 assert(height
&& x
&& y
);
175 assert(!(ME_GetParagraph(pCursorRun
)->member
.para
.nFlags
& MEPF_REWRAP
));
176 assert(pCursor
->pRun
);
177 assert(pCursor
->pRun
->type
== diRun
);
179 if (pCursorRun
->type
== diRun
) {
180 ME_DisplayItem
*row
= ME_FindItemBack(pCursorRun
, diStartRowOrParagraph
);
183 HDC hDC
= ITextHost_TxGetDC(editor
->texthost
);
185 ME_DisplayItem
*run
= pCursorRun
;
186 ME_DisplayItem
*para
= NULL
;
189 ME_InitContext(&c
, editor
, hDC
);
191 if (!pCursor
->nOffset
)
193 ME_DisplayItem
*prev
= ME_FindItemBack(pCursorRun
, diRunOrParagraph
);
195 if (prev
->type
== diRun
)
198 assert(row
->type
== diStartRow
); /* paragraph -> run without start row ?*/
199 para
= ME_FindItemBack(row
, diParagraph
);
201 assert(para
->type
== diParagraph
);
202 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
&&
203 run
== ME_FindItemFwd(row
, diRun
))
205 ME_DisplayItem
*tmp
= ME_FindItemBack(row
, diRunOrParagraph
);
207 if (tmp
->type
== diRun
)
209 row
= ME_FindItemBack(tmp
, diStartRow
);
210 pSizeRun
= run
= tmp
;
212 assert(run
->type
== diRun
);
213 sz
= ME_GetRunSize(&c
, ¶
->member
.para
,
214 &run
->member
.run
, ME_StrLen(run
->member
.run
.strText
),
215 row
->member
.row
.nLMargin
);
218 if (pCursor
->nOffset
) {
219 sz
= ME_GetRunSize(&c
, ¶
->member
.para
, &run
->member
.run
, pCursor
->nOffset
,
220 row
->member
.row
.nLMargin
);
223 *height
= pSizeRun
->member
.run
.nAscent
+ pSizeRun
->member
.run
.nDescent
;
224 *x
= c
.rcView
.left
+ run
->member
.run
.pt
.x
+ sz
.cx
- editor
->horz_si
.nPos
;
225 *y
= c
.rcView
.top
+ para
->member
.para
.pt
.y
+ row
->member
.row
.nBaseline
226 + run
->member
.run
.pt
.y
- pSizeRun
->member
.run
.nAscent
- editor
->vert_si
.nPos
;
227 ME_DestroyContext(&c
);
231 *height
= 10; /* FIXME use global font */
238 ME_MoveCaret(ME_TextEditor
*editor
)
242 ME_GetCursorCoordinates(editor
, &editor
->pCursors
[0], &x
, &y
, &height
);
243 if(editor
->bHaveFocus
&& !ME_IsSelection(editor
))
245 x
= min(x
, editor
->rcFormat
.right
-1);
246 ITextHost_TxCreateCaret(editor
->texthost
, NULL
, 0, height
);
247 ITextHost_TxSetCaretPos(editor
->texthost
, x
, y
);
252 void ME_ShowCaret(ME_TextEditor
*ed
)
255 if(ed
->bHaveFocus
&& !ME_IsSelection(ed
))
256 ITextHost_TxShowCaret(ed
->texthost
, TRUE
);
259 void ME_HideCaret(ME_TextEditor
*ed
)
261 if(!ed
->bHaveFocus
|| ME_IsSelection(ed
))
263 ITextHost_TxShowCaret(ed
->texthost
, FALSE
);
268 BOOL
ME_InternalDeleteText(ME_TextEditor
*editor
, int nOfs
, int nChars
,
273 int totalChars
= nChars
;
274 ME_DisplayItem
*start_para
;
276 /* Prevent deletion past last end of paragraph run. */
277 nChars
= min(nChars
, ME_GetTextLength(editor
) - nOfs
);
279 ME_CursorFromCharOfs(editor
, nOfs
, &c
);
280 start_para
= ME_GetParagraph(c
.pRun
);
284 ME_ProtectPartialTableDeletion(editor
, nOfs
, &nChars
);
292 ME_CursorFromCharOfs(editor
, nOfs
+nChars
, &c
);
294 nOfs
+nChars
== (c
.pRun
->member
.run
.nCharOfs
295 + ME_GetParagraph(c
.pRun
)->member
.para
.nCharOfs
))
297 /* We aren't deleting anything in this run, so we will go back to the
298 * last run we are deleting text in. */
299 c
.pRun
= ME_FindItemBack(c
.pRun
, diRun
);
300 c
.nOffset
= c
.pRun
->member
.run
.strText
->nLen
;
302 run
= &c
.pRun
->member
.run
;
303 if (run
->nFlags
& MERF_ENDPARA
) {
304 int eollen
= c
.pRun
->member
.run
.strText
->nLen
;
305 BOOL keepFirstParaFormat
;
307 if (!ME_FindItemFwd(c
.pRun
, diParagraph
))
311 keepFirstParaFormat
= (totalChars
== nChars
&& nChars
<= eollen
&&
313 if (!editor
->bEmulateVersion10
) /* v4.1 */
315 ME_DisplayItem
*next_para
= ME_FindItemFwd(c
.pRun
, diParagraphOrEnd
);
316 ME_DisplayItem
*this_para
= next_para
->member
.para
.prev_para
;
318 /* The end of paragraph before a table row is only deleted if there
319 * is nothing else on the line before it. */
320 if (this_para
== start_para
&&
321 next_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
323 /* If the paragraph will be empty, then it should be deleted, however
324 * it still might have text right now which would inherit the
325 * MEPF_STARTROW property if we joined it right now.
326 * Instead we will delete it after the preceding text is deleted. */
327 if (nOfs
> this_para
->member
.para
.nCharOfs
) {
328 /* Skip this end of line. */
329 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
332 keepFirstParaFormat
= TRUE
;
335 ME_JoinParagraphs(editor
, ME_GetParagraph(c
.pRun
), keepFirstParaFormat
);
336 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
337 ME_CheckCharOffsets(editor
);
338 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
344 int nCharsToDelete
= min(nChars
, c
.nOffset
);
347 c
.nOffset
-= nCharsToDelete
;
349 ME_FindItemBack(c
.pRun
, diParagraph
)->member
.para
.nFlags
|= MEPF_REWRAP
;
352 /* nChars is the number of characters that should be deleted from the
353 PRECEDING runs (these BEFORE cursor.pRun)
354 nCharsToDelete is a number of chars to delete from THIS run */
355 nChars
-= nCharsToDelete
;
356 shift
-= nCharsToDelete
;
357 TRACE("Deleting %d (remaning %d) chars at %d in '%s' (%d)\n",
358 nCharsToDelete
, nChars
, c
.nOffset
,
359 debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
361 if (!c
.nOffset
&& ME_StrVLen(run
->strText
) == nCharsToDelete
)
363 /* undo = reinsert whole run */
364 /* nOfs is a character offset (from the start of the document
365 to the current (deleted) run */
366 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
368 pUndo
->di
.member
.run
.nCharOfs
= nOfs
+nChars
;
372 /* undo = reinsert partial run */
373 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
375 ME_DestroyString(pUndo
->di
.member
.run
.strText
);
376 pUndo
->di
.member
.run
.nCharOfs
= nOfs
+nChars
;
377 pUndo
->di
.member
.run
.strText
= ME_MakeStringN(run
->strText
->szData
+c
.nOffset
, nCharsToDelete
);
380 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
381 TRACE("Shift value: %d\n", shift
);
382 ME_StrDeleteV(run
->strText
, c
.nOffset
, nCharsToDelete
);
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
<= ME_StrVLen(run
->strText
));
397 if (pThisCur
->nOffset
== ME_StrVLen(run
->strText
))
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 (!ME_StrVLen(cursor
.pRun
->member
.run
.strText
))
415 TRACE("Removing useless 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
, ME_GetCursorOfs(editor
, nCursor
), nChars
,
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_SendSelChange(editor
);
472 void ME_InsertEndRowFromCursor(ME_TextEditor
*editor
, int nCursor
)
474 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
479 if (ME_IsSelection(editor
))
480 ME_DeleteSelection(editor
);
482 di
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
484 ME_SendSelChange(editor
);
488 void ME_InsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
489 const WCHAR
*str
, int len
, ME_Style
*style
)
495 /* FIXME really HERE ? */
496 if (ME_IsSelection(editor
))
497 ME_DeleteSelection(editor
);
499 /* FIXME: is this too slow? */
500 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
501 oldLen
= ME_GetTextLength(editor
);
503 /* text operations set modified state */
504 editor
->nModifyStep
= 1;
508 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
512 /* grow the text limit to fit our text */
513 if(editor
->nTextLimit
< oldLen
+len
)
514 editor
->nTextLimit
= oldLen
+ len
;
520 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
521 while(pos
- str
< len
&& *pos
!= '\r' && *pos
!= '\n' && *pos
!= '\t')
524 if (pos
!= str
) { /* handle text */
525 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
526 } else if (*pos
== '\t') { /* handle tabs */
528 ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, style
, MERF_TAB
);
530 } else { /* handle EOLs */
531 ME_DisplayItem
*tp
, *end_run
;
535 /* Find number of CR and LF in end of paragraph run */
538 if (len
> 1 && pos
[1] == '\n')
540 else if (len
> 2 && pos
[1] == '\r' && pos
[2] == '\n')
545 assert(*pos
== '\n');
550 if (!editor
->bEmulateVersion10
&& eol_len
== 3)
552 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
554 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, style
, 0);
558 if (!editor
->bEmulateVersion10
) {
560 eol_str
= ME_MakeStringN(&cr
, 1);
562 eol_str
= ME_MakeStringN(str
, eol_len
);
565 p
= &editor
->pCursors
[nCursor
];
567 ME_SplitRunSimple(editor
, p
->pRun
, p
->nOffset
);
568 p
= &editor
->pCursors
[nCursor
];
570 tmp_style
= ME_GetInsertStyle(editor
, nCursor
);
571 /* ME_SplitParagraph increases style refcount */
572 tp
= ME_SplitParagraph(editor
, p
->pRun
, p
->pRun
->member
.run
.style
, eol_str
, 0);
573 p
->pRun
= ME_FindItemFwd(tp
, diRun
);
574 end_run
= ME_FindItemBack(tp
, diRun
);
575 ME_ReleaseStyle(end_run
->member
.run
.style
);
576 end_run
->member
.run
.style
= tmp_style
;
587 ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
589 ME_DisplayItem
*pRun
= pCursor
->pRun
;
593 if (!pCursor
->nOffset
)
596 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
603 if (pRun
->member
.para
.prev_para
->type
== diTextStart
)
605 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
606 /* every paragraph ought to have at least one run */
607 assert(pRun
&& pRun
->type
== diRun
);
608 assert(pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
611 assert(pRun
->type
!= diRun
&& pRun
->type
!= diParagraph
);
614 } while (RUN_IS_HIDDEN(&pRun
->member
.run
) ||
615 pRun
->member
.run
.nFlags
& MERF_HIDDEN
);
616 pCursor
->pRun
= pRun
;
617 if (pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
618 pCursor
->nOffset
= 0;
620 pCursor
->nOffset
= pRun
->member
.run
.strText
->nLen
;
623 if (pCursor
->nOffset
)
624 pCursor
->nOffset
= ME_StrRelPos2(pCursor
->pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
629 if (!(pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
631 int new_ofs
= ME_StrRelPos2(pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
633 if (new_ofs
< pRun
->member
.run
.strText
->nLen
)
635 pCursor
->nOffset
= new_ofs
;
640 pRun
= ME_FindItemFwd(pRun
, diRun
);
641 } while (pRun
&& (RUN_IS_HIDDEN(&pRun
->member
.run
) ||
642 pRun
->member
.run
.nFlags
& MERF_HIDDEN
));
645 pCursor
->pRun
= pRun
;
646 pCursor
->nOffset
= 0;
655 ME_MoveCursorWords(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
657 ME_DisplayItem
*pRun
= cursor
->pRun
, *pOtherRun
;
658 int nOffset
= cursor
->nOffset
;
662 /* Backward movement */
665 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
666 nOffset
, WB_MOVEWORDLEFT
);
669 pOtherRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
670 if (pOtherRun
->type
== diRun
)
672 if (ME_CallWordBreakProc(editor
, pOtherRun
->member
.run
.strText
,
673 pOtherRun
->member
.run
.strText
->nLen
- 1,
675 && !(pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
676 && !(cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
677 && !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
, 0,
681 nOffset
= pOtherRun
->member
.run
.strText
->nLen
;
683 else if (pOtherRun
->type
== diParagraph
)
685 if (cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
687 /* Skip empty start of table row paragraph */
688 if (pOtherRun
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
689 pOtherRun
= pOtherRun
->member
.para
.prev_para
;
690 /* Paragraph breaks are treated as separate words */
691 if (pOtherRun
->member
.para
.prev_para
->type
== diTextStart
)
694 pRun
= ME_FindItemBack(pOtherRun
, diRunOrParagraph
);
702 /* Forward movement */
703 BOOL last_delim
= FALSE
;
707 if (last_delim
&& !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
708 nOffset
, WB_ISDELIMITER
))
710 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
711 nOffset
, WB_MOVEWORDRIGHT
);
712 if (nOffset
< pRun
->member
.run
.strText
->nLen
)
714 pOtherRun
= ME_FindItemFwd(pRun
, diRunOrParagraphOrEnd
);
715 if (pOtherRun
->type
== diRun
)
717 last_delim
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
718 nOffset
- 1, WB_ISDELIMITER
);
722 else if (pOtherRun
->type
== diParagraph
)
724 if (pOtherRun
->member
.para
.nFlags
& MEPF_ROWSTART
)
725 pOtherRun
= pOtherRun
->member
.para
.next_para
;
726 if (cursor
->pRun
== pRun
)
727 pRun
= ME_FindItemFwd(pOtherRun
, diRun
);
733 if (cursor
->pRun
== pRun
)
741 cursor
->nOffset
= nOffset
;
747 ME_SelectByType(ME_TextEditor
*editor
, ME_SelectionType selectionType
)
749 /* pCursor[0] is the end of the selection
750 * pCursor[1] is the start of the selection (or the position selection anchor)
751 * pCursor[2] and [3] are the selection anchors that are backed up
752 * so they are kept when the selection changes for drag selection.
755 editor
->nSelectionType
= selectionType
;
756 switch(selectionType
)
761 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
762 editor
->pCursors
[1] = editor
->pCursors
[0];
763 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
768 ME_DisplayItem
*pItem
;
769 ME_DIType fwdSearchType
, backSearchType
;
770 if (selectionType
== stParagraph
) {
771 backSearchType
= diParagraph
;
772 fwdSearchType
= diParagraphOrEnd
;
774 backSearchType
= diStartRow
;
775 fwdSearchType
= diStartRowOrParagraphOrEnd
;
777 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, fwdSearchType
);
779 if (pItem
->type
== diTextEnd
)
780 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
782 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
783 editor
->pCursors
[0].nOffset
= 0;
785 pItem
= ME_FindItemBack(pItem
, backSearchType
);
786 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
787 editor
->pCursors
[1].nOffset
= 0;
791 /* Select everything with cursor anchored from the start of the text */
792 editor
->nSelectionType
= stDocument
;
793 editor
->pCursors
[1].pRun
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
);
794 editor
->pCursors
[1].nOffset
= 0;
795 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
796 editor
->pCursors
[0].nOffset
= 0;
800 /* Store the anchor positions for extending the selection. */
801 editor
->pCursors
[2] = editor
->pCursors
[0];
802 editor
->pCursors
[3] = editor
->pCursors
[1];
805 int ME_GetCursorOfs(ME_TextEditor
*editor
, int nCursor
)
807 ME_Cursor
*pCursor
= &editor
->pCursors
[nCursor
];
808 return ME_GetParagraph(pCursor
->pRun
)->member
.para
.nCharOfs
809 + pCursor
->pRun
->member
.run
.nCharOfs
+ pCursor
->nOffset
;
812 /* Helper function for ME_FindPixelPos to find paragraph within tables */
813 static ME_DisplayItem
* ME_FindPixelPosInTableRow(int x
, int y
,
814 ME_DisplayItem
*para
)
816 ME_DisplayItem
*cell
, *next_cell
;
817 assert(para
->member
.para
.nFlags
& MEPF_ROWSTART
);
818 cell
= para
->member
.para
.next_para
->member
.para
.pCell
;
821 /* find the cell we are in */
822 while ((next_cell
= cell
->member
.cell
.next_cell
) != NULL
) {
823 if (x
< next_cell
->member
.cell
.pt
.x
)
825 para
= ME_FindItemFwd(cell
, diParagraph
);
826 /* Found the cell, but there might be multiple paragraphs in
827 * the cell, so need to search down the cell for the paragraph. */
828 while (cell
== para
->member
.para
.pCell
) {
829 if (y
< para
->member
.para
.pt
.y
+ para
->member
.para
.nHeight
)
831 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
832 return ME_FindPixelPosInTableRow(x
, y
, para
);
836 para
= para
->member
.para
.next_para
;
838 /* Past the end of the cell, so go back to the last cell paragraph */
839 return para
->member
.para
.prev_para
;
843 /* Return table row delimiter */
844 para
= ME_FindItemFwd(cell
, diParagraph
);
845 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
846 assert(para
->member
.para
.pFmt
->dwMask
& PFM_TABLEROWDELIMITER
);
847 assert(para
->member
.para
.pFmt
->wEffects
& PFE_TABLEROWDELIMITER
);
851 static BOOL
ME_ReturnFoundPos(ME_TextEditor
*editor
, ME_DisplayItem
*found
,
852 ME_Cursor
*result
, int rx
, BOOL isExact
)
855 assert(found
->type
== diRun
);
856 if ((found
->member
.run
.nFlags
& MERF_ENDPARA
) || rx
< 0)
858 result
->pRun
= found
;
859 result
->nOffset
= ME_CharFromPointCursor(editor
, rx
, &found
->member
.run
);
860 if (editor
->pCursors
[0].nOffset
== found
->member
.run
.strText
->nLen
&& rx
)
862 result
->pRun
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, diRun
);
868 /* Finds the run and offset from the pixel position.
870 * x & y are pixel positions in virtual coordinates into the rich edit control,
871 * so client coordinates must first be adjusted by the scroll position.
873 * returns TRUE if the result was exactly under the cursor, otherwise returns
874 * FALSE, and result is set to the closest position to the coordinates.
876 static BOOL
ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
,
877 ME_Cursor
*result
, BOOL
*is_eol
)
879 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
880 ME_DisplayItem
*last
= NULL
;
884 x
-= editor
->rcFormat
.left
;
885 y
-= editor
->rcFormat
.top
;
891 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->member
.para
.next_para
)
893 assert(p
->type
== diParagraph
);
894 if (y
< p
->member
.para
.pt
.y
+ p
->member
.para
.nHeight
)
896 if (p
->member
.para
.nFlags
& MEPF_ROWSTART
)
897 p
= ME_FindPixelPosInTableRow(x
, y
, p
);
898 y
-= p
->member
.para
.pt
.y
;
899 p
= ME_FindItemFwd(p
, diStartRow
);
901 } else if (p
->member
.para
.nFlags
& MEPF_ROWSTART
) {
902 p
= ME_GetTableRowEnd(p
);
906 for (; p
!= editor
->pBuffer
->pLast
; )
909 assert(p
->type
== diStartRow
);
910 if (y
< p
->member
.row
.pt
.y
+ p
->member
.row
.nHeight
)
912 p
= ME_FindItemFwd(p
, diRun
);
915 pp
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
916 if (pp
->type
!= diStartRow
)
918 p
= ME_FindItemFwd(p
, diRun
);
923 if (p
== editor
->pBuffer
->pLast
)
925 /* The position is below the last paragraph, so the last row will be used
926 * rather than the end of the text, so the x position will be used to
927 * determine the offset closest to the pixel position. */
929 p
= ME_FindItemBack(p
, diStartRow
);
931 p
= ME_FindItemFwd(p
, diRun
);
935 p
= editor
->pBuffer
->pLast
;
938 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->next
)
943 rx
= x
- p
->member
.run
.pt
.x
;
944 if (rx
< p
->member
.run
.nWidth
)
945 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
949 p
= ME_FindItemFwd(p
, diRun
);
950 if (is_eol
) *is_eol
= 1;
951 rx
= 0; /* FIXME not sure */
952 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
957 rx
= 0; /* FIXME not sure */
959 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
964 result
->pRun
= ME_FindItemBack(p
, diRun
);
966 assert(result
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
971 /* Returns the character offset closest to the pixel position
973 * x & y are pixel positions in client coordinates.
975 * isExact will be set to TRUE if the run is directly under the pixel
976 * position, FALSE if it not, unless isExact is set to NULL.
978 int ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
, BOOL
*isExact
)
984 ITextHost_TxGetClientRect(editor
->texthost
, &rc
);
985 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
) {
986 if (isExact
) *isExact
= FALSE
;
989 x
+= editor
->horz_si
.nPos
;
990 y
+= editor
->vert_si
.nPos
;
991 bResult
= ME_FindPixelPos(editor
, x
, y
, &cursor
, NULL
);
992 if (isExact
) *isExact
= bResult
;
993 return (ME_GetParagraph(cursor
.pRun
)->member
.para
.nCharOfs
994 + cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
);
999 /* Extends the selection with a word, line, or paragraph selection type.
1001 * The selection is anchored by editor->pCursors[2-3] such that the text
1002 * between the anchors will remain selected, and one end will be extended.
1004 * editor->pCursors[0] should have the position to extend the selection to
1005 * before this function is called.
1007 * Nothing will be done if editor->nSelectionType equals stPosition.
1009 static void ME_ExtendAnchorSelection(ME_TextEditor
*editor
)
1011 ME_Cursor tmp_cursor
;
1012 int curOfs
, anchorStartOfs
, anchorEndOfs
;
1013 if (editor
->nSelectionType
== stPosition
|| editor
->nSelectionType
== stDocument
)
1015 curOfs
= ME_GetCursorOfs(editor
, 0);
1016 anchorStartOfs
= ME_GetCursorOfs(editor
, 3);
1017 anchorEndOfs
= ME_GetCursorOfs(editor
, 2);
1019 tmp_cursor
= editor
->pCursors
[0];
1020 editor
->pCursors
[0] = editor
->pCursors
[2];
1021 editor
->pCursors
[1] = editor
->pCursors
[3];
1022 if (curOfs
< anchorStartOfs
)
1024 /* Extend the left side of selection */
1025 editor
->pCursors
[1] = tmp_cursor
;
1026 if (editor
->nSelectionType
== stWord
)
1027 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
1030 ME_DisplayItem
*pItem
;
1031 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1032 diStartRowOrParagraph
:diParagraph
);
1033 pItem
= ME_FindItemBack(editor
->pCursors
[1].pRun
, searchType
);
1034 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
1035 editor
->pCursors
[1].nOffset
= 0;
1038 else if (curOfs
>= anchorEndOfs
)
1040 /* Extend the right side of selection */
1041 editor
->pCursors
[0] = tmp_cursor
;
1042 if (editor
->nSelectionType
== stWord
)
1043 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
1046 ME_DisplayItem
*pItem
;
1047 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1048 diStartRowOrParagraphOrEnd
:diParagraphOrEnd
);
1049 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, searchType
);
1050 if (pItem
->type
== diTextEnd
)
1051 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
1053 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
1054 editor
->pCursors
[0].nOffset
= 0;
1059 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
, int clickNum
)
1061 ME_Cursor tmp_cursor
;
1062 int is_selection
= 0;
1065 editor
->nUDArrowX
= -1;
1067 x
+= editor
->horz_si
.nPos
;
1068 y
+= editor
->vert_si
.nPos
;
1070 tmp_cursor
= editor
->pCursors
[0];
1071 is_selection
= ME_IsSelection(editor
);
1072 is_shift
= GetKeyState(VK_SHIFT
) < 0;
1074 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
1076 if (x
>= editor
->rcFormat
.left
|| is_shift
)
1080 editor
->pCursors
[1] = editor
->pCursors
[0];
1082 if (x
>= editor
->rcFormat
.left
)
1083 ME_SelectByType(editor
, stWord
);
1085 ME_SelectByType(editor
, stParagraph
);
1086 } else if (clickNum
% 2 == 0) {
1087 ME_SelectByType(editor
, stWord
);
1089 ME_SelectByType(editor
, stParagraph
);
1094 editor
->nSelectionType
= stPosition
;
1095 editor
->pCursors
[1] = editor
->pCursors
[0];
1097 else if (!is_selection
)
1099 editor
->nSelectionType
= stPosition
;
1100 editor
->pCursors
[1] = tmp_cursor
;
1102 else if (editor
->nSelectionType
!= stPosition
)
1104 ME_ExtendAnchorSelection(editor
);
1110 ME_SelectByType(editor
, stLine
);
1111 } else if (clickNum
% 2 == 0 || is_shift
) {
1112 ME_SelectByType(editor
, stParagraph
);
1114 ME_SelectByType(editor
, stDocument
);
1117 ME_InvalidateSelection(editor
);
1118 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1119 ME_ShowCaret(editor
);
1120 ME_ClearTempStyle(editor
);
1121 ME_SendSelChange(editor
);
1124 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
1126 ME_Cursor tmp_cursor
;
1128 if (editor
->nSelectionType
== stDocument
)
1130 x
+= editor
->horz_si
.nPos
;
1131 y
+= editor
->vert_si
.nPos
;
1133 tmp_cursor
= editor
->pCursors
[0];
1134 /* FIXME: do something with the return value of ME_FindPixelPos */
1135 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
1137 ME_InvalidateSelection(editor
);
1138 editor
->pCursors
[0] = tmp_cursor
;
1139 ME_ExtendAnchorSelection(editor
);
1141 if (editor
->nSelectionType
!= stPosition
&&
1142 memcmp(&editor
->pCursors
[1], &editor
->pCursors
[3], sizeof(ME_Cursor
)))
1144 /* The scroll the cursor towards the other end, since it was the one
1145 * extended by ME_ExtendAnchorSelection */
1146 ME_EnsureVisible(editor
, &editor
->pCursors
[1]);
1148 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
1151 ME_InvalidateSelection(editor
);
1152 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1153 ME_ShowCaret(editor
);
1154 ME_SendSelChange(editor
);
1157 static ME_DisplayItem
*ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
1158 int x
, int *pOffset
, int *pbCaretAtEnd
)
1160 ME_DisplayItem
*pNext
, *pLastRun
;
1161 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
1162 assert(pNext
->type
== diRun
);
1164 if (pbCaretAtEnd
) *pbCaretAtEnd
= FALSE
;
1165 if (pOffset
) *pOffset
= 0;
1167 int run_x
= pNext
->member
.run
.pt
.x
;
1168 int width
= pNext
->member
.run
.nWidth
;
1173 if (x
>= run_x
&& x
< run_x
+width
)
1175 int ch
= ME_CharFromPointCursor(editor
, x
-run_x
, &pNext
->member
.run
);
1176 ME_String
*s
= pNext
->member
.run
.strText
;
1184 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
1185 } while(pNext
&& pNext
->type
== diRun
);
1187 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
1189 pNext
= ME_FindItemFwd(pNext
, diRun
);
1190 if (pbCaretAtEnd
) *pbCaretAtEnd
= TRUE
;
1197 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1199 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1202 if (editor
->nUDArrowX
!= -1)
1203 x
= editor
->nUDArrowX
;
1205 if (editor
->bCaretAtEnd
)
1207 pRun
= ME_FindItemBack(pRun
, diRun
);
1209 x
= pRun
->member
.run
.pt
.x
+ pRun
->member
.run
.nWidth
;
1212 x
= pRun
->member
.run
.pt
.x
;
1213 x
+= ME_PointFromChar(editor
, &pRun
->member
.run
, pCursor
->nOffset
);
1215 editor
->nUDArrowX
= x
;
1222 ME_MoveCursorLines(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
1224 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1225 ME_DisplayItem
*pItem
, *pOldPara
, *pNewPara
;
1226 int x
= ME_GetXForArrow(editor
, pCursor
);
1228 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1229 pRun
= ME_FindItemBack(pRun
, diRun
);
1232 pOldPara
= ME_GetParagraph(pRun
);
1235 /* start of this row */
1236 pItem
= ME_FindItemBack(pRun
, diStartRow
);
1238 /* start of the previous row */
1239 pItem
= ME_FindItemBack(pItem
, diStartRow
);
1241 return; /* row not found - ignore */
1242 pNewPara
= ME_GetParagraph(pItem
);
1243 if (pOldPara
->member
.para
.nFlags
& MEPF_ROWEND
||
1244 (pOldPara
->member
.para
.pCell
&&
1245 pOldPara
->member
.para
.pCell
!= pNewPara
->member
.para
.pCell
))
1247 /* Brought out of a cell */
1248 pNewPara
= ME_GetTableRowStart(pOldPara
)->member
.para
.prev_para
;
1249 if (pNewPara
->type
== diTextStart
)
1250 return; /* At the top, so don't go anywhere. */
1251 pItem
= ME_FindItemFwd(pNewPara
, diStartRow
);
1253 if (pNewPara
->member
.para
.nFlags
& MEPF_ROWEND
)
1255 /* Brought into a table row */
1256 ME_Cell
*cell
= &ME_FindItemBack(pNewPara
, diCell
)->member
.cell
;
1257 while (x
< cell
->pt
.x
&& cell
->prev_cell
)
1258 cell
= &cell
->prev_cell
->member
.cell
;
1259 if (cell
->next_cell
) /* else - we are still at the end of the row */
1260 pItem
= ME_FindItemBack(cell
->next_cell
, diStartRow
);
1265 /* start of the next row */
1266 pItem
= ME_FindItemFwd(pRun
, diStartRow
);
1268 return; /* row not found - ignore */
1269 pNewPara
= ME_GetParagraph(pItem
);
1270 if (pOldPara
->member
.para
.nFlags
& MEPF_ROWSTART
||
1271 (pOldPara
->member
.para
.pCell
&&
1272 pOldPara
->member
.para
.pCell
!= pNewPara
->member
.para
.pCell
))
1274 /* Brought out of a cell */
1275 pNewPara
= ME_GetTableRowEnd(pOldPara
)->member
.para
.next_para
;
1276 if (pNewPara
->type
== diTextEnd
)
1277 return; /* At the bottom, so don't go anywhere. */
1278 pItem
= ME_FindItemFwd(pNewPara
, diStartRow
);
1280 if (pNewPara
->member
.para
.nFlags
& MEPF_ROWSTART
)
1282 /* Brought into a table row */
1283 ME_DisplayItem
*cell
= ME_FindItemFwd(pNewPara
, diCell
);
1284 while (cell
->member
.cell
.next_cell
&&
1285 x
>= cell
->member
.cell
.next_cell
->member
.cell
.pt
.x
)
1286 cell
= cell
->member
.cell
.next_cell
;
1287 pItem
= ME_FindItemFwd(cell
, diStartRow
);
1292 /* row not found - ignore */
1295 pCursor
->pRun
= ME_FindRunInRow(editor
, pItem
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1296 assert(pCursor
->pRun
);
1297 assert(pCursor
->pRun
->type
== diRun
);
1300 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1302 ME_DisplayItem
*p
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diStartRow
);
1304 if (editor
->vert_si
.nPos
< p
->member
.row
.nHeight
)
1306 pCursor
->pRun
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
);
1307 pCursor
->nOffset
= 0;
1308 editor
->bCaretAtEnd
= FALSE
;
1309 /* Native clears seems to clear this x value on page up at the top
1310 * of the text, but not on page down at the end of the text.
1311 * Doesn't make sense, but we try to be bug for bug compatible. */
1312 editor
->nUDArrowX
= -1;
1314 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1315 ME_DisplayItem
*pLast
;
1316 int x
, y
, ys
, yd
, yp
, yprev
;
1317 int yOldScrollPos
= editor
->vert_si
.nPos
;
1319 x
= ME_GetXForArrow(editor
, pCursor
);
1320 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1321 pRun
= ME_FindItemBack(pRun
, diRun
);
1323 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1324 assert(p
->type
== diStartRow
);
1325 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1326 yprev
= ys
= y
= yp
+ p
->member
.row
.pt
.y
;
1328 ME_ScrollUp(editor
, editor
->sizeWindow
.cy
);
1329 /* Only move the cursor by the amount scrolled. */
1330 yd
= y
+ editor
->vert_si
.nPos
- yOldScrollPos
;
1334 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
1337 if (p
->type
== diParagraph
) { /* crossing paragraphs */
1338 if (p
->member
.para
.prev_para
== NULL
)
1340 yp
= p
->member
.para
.prev_para
->member
.para
.pt
.y
;
1343 y
= yp
+ p
->member
.row
.pt
.y
;
1350 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
,
1351 &editor
->bCaretAtEnd
);
1353 assert(pCursor
->pRun
);
1354 assert(pCursor
->pRun
->type
== diRun
);
1357 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1359 ME_DisplayItem
*pLast
;
1362 /* Find y position of the last row */
1363 pLast
= editor
->pBuffer
->pLast
;
1364 y
= pLast
->member
.para
.prev_para
->member
.para
.pt
.y
1365 + ME_FindItemBack(pLast
, diStartRow
)->member
.row
.pt
.y
;
1367 x
= ME_GetXForArrow(editor
, pCursor
);
1369 if (editor
->vert_si
.nPos
>= y
- editor
->sizeWindow
.cy
)
1371 pCursor
->pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
1372 pCursor
->nOffset
= 0;
1373 editor
->bCaretAtEnd
= FALSE
;
1375 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1377 int ys
, yd
, yp
, yprev
;
1378 int yOldScrollPos
= editor
->vert_si
.nPos
;
1380 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1381 pRun
= ME_FindItemBack(pRun
, diRun
);
1383 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1384 assert(p
->type
== diStartRow
);
1385 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1386 yprev
= ys
= y
= yp
+ p
->member
.row
.pt
.y
;
1388 /* For native richedit controls:
1389 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1390 * v4.1 can scroll past this position here. */
1391 ME_ScrollDown(editor
, editor
->sizeWindow
.cy
);
1392 /* Only move the cursor by the amount scrolled. */
1393 yd
= y
+ editor
->vert_si
.nPos
- yOldScrollPos
;
1397 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
1400 if (p
->type
== diParagraph
) {
1401 yp
= p
->member
.para
.pt
.y
;
1404 y
= yp
+ p
->member
.row
.pt
.y
;
1411 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
,
1412 &editor
->bCaretAtEnd
);
1414 assert(pCursor
->pRun
);
1415 assert(pCursor
->pRun
->type
== diRun
);
1418 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1420 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
1422 ME_DisplayItem
*pRun
;
1423 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
1424 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1428 pRun
= ME_FindItemFwd(pRow
, diRun
);
1430 pCursor
->pRun
= pRun
;
1431 pCursor
->nOffset
= 0;
1434 editor
->bCaretAtEnd
= FALSE
;
1437 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1439 pCursor
->pRun
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
);
1440 pCursor
->nOffset
= 0;
1441 editor
->bCaretAtEnd
= FALSE
;
1444 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1446 ME_DisplayItem
*pRow
;
1448 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1451 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1453 if (pRow
->type
== diStartRow
) {
1454 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1456 pCursor
->pRun
= pRun
;
1457 pCursor
->nOffset
= 0;
1458 editor
->bCaretAtEnd
= TRUE
;
1461 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1462 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1463 pCursor
->nOffset
= 0;
1464 editor
->bCaretAtEnd
= FALSE
;
1467 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1469 pCursor
->pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
1470 assert(pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1471 pCursor
->nOffset
= 0;
1472 editor
->bCaretAtEnd
= FALSE
;
1475 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1477 return memcmp(&editor
->pCursors
[0], &editor
->pCursors
[1], sizeof(ME_Cursor
))!=0;
1480 static int ME_GetSelCursor(ME_TextEditor
*editor
, int dir
)
1482 int cdir
= ME_GetCursorOfs(editor
, 0) - ME_GetCursorOfs(editor
, 1);
1490 void ME_DeleteSelection(ME_TextEditor
*editor
)
1493 ME_GetSelection(editor
, &from
, &to
);
1494 ME_DeleteTextAtCursor(editor
, ME_GetSelCursor(editor
,-1), to
-from
);
1497 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1499 return ME_GetInsertStyle(editor
, 0);
1502 void ME_SendSelChange(ME_TextEditor
*editor
)
1506 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1509 sc
.nmhdr
.code
= EN_SELCHANGE
;
1510 ME_GetSelection(editor
, &sc
.chrg
.cpMin
, &sc
.chrg
.cpMax
);
1511 sc
.seltyp
= SEL_EMPTY
;
1512 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1513 sc
.seltyp
|= SEL_TEXT
;
1514 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* wth were RICHEDIT authors thinking ? */
1515 sc
.seltyp
|= SEL_MULTICHAR
;
1516 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1517 sc
.chrg
.cpMin
, sc
.chrg
.cpMax
, sc
.seltyp
,
1518 (sc
.seltyp
& SEL_TEXT
) ? "SEL_TEXT" : "",
1519 (sc
.seltyp
& SEL_MULTICHAR
) ? "SEL_MULTICHAR" : "");
1520 if (sc
.chrg
.cpMin
!= editor
->notified_cr
.cpMin
|| sc
.chrg
.cpMax
!= editor
->notified_cr
.cpMax
)
1522 ME_ClearTempStyle(editor
);
1524 editor
->notified_cr
= sc
.chrg
;
1525 ITextHost_TxNotify(editor
->texthost
, sc
.nmhdr
.code
, &sc
);
1530 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1533 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1534 ME_Cursor tmp_curs
= *p
;
1535 BOOL success
= FALSE
;
1537 ME_CheckCharOffsets(editor
);
1540 editor
->bCaretAtEnd
= 0;
1542 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1544 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1547 editor
->bCaretAtEnd
= 0;
1549 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1551 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1554 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1557 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1560 ME_ArrowPageUp(editor
, &tmp_curs
);
1563 ME_ArrowPageDown(editor
, &tmp_curs
);
1567 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1569 ME_ArrowHome(editor
, &tmp_curs
);
1570 editor
->bCaretAtEnd
= 0;
1575 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1577 ME_ArrowEnd(editor
, &tmp_curs
);
1582 editor
->pCursors
[1] = tmp_curs
;
1585 ME_InvalidateSelection(editor
);
1587 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1588 ME_EnsureVisible(editor
, &tmp_curs
);
1589 ME_ShowCaret(editor
);
1590 ME_SendSelChange(editor
);