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 return ME_CharOfsFromRunOfs(editor
, ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
), 0);
49 int ME_GetTextLengthEx(ME_TextEditor
*editor
, const GETTEXTLENGTHEX
*how
)
53 if (how
->flags
& GTL_PRECISE
&& how
->flags
& GTL_CLOSE
)
55 if (how
->flags
& GTL_NUMCHARS
&& how
->flags
& GTL_NUMBYTES
)
58 length
= ME_GetTextLength(editor
);
60 if ((GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_MULTILINE
)
61 && (how
->flags
& GTL_USECRLF
)
62 && !editor
->bEmulateVersion10
) /* Ignore GTL_USECRLF flag in 1.0 emulation */
63 length
+= editor
->nParagraphs
- 1;
65 if (how
->flags
& GTL_NUMBYTES
)
69 if (how
->codepage
== 1200)
71 if (how
->flags
& GTL_PRECISE
)
72 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
73 if (GetCPInfo(how
->codepage
, &cpinfo
))
74 return length
* cpinfo
.MaxCharSize
;
75 ERR("Invalid codepage %u\n", how
->codepage
);
82 int ME_SetSelection(ME_TextEditor
*editor
, int from
, int to
)
85 const int len
= ME_GetTextLength(editor
);
87 /* all negative values are effectively the same */
94 if (from
== 0 && to
== -1)
96 editor
->pCursors
[1].pRun
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
);
97 editor
->pCursors
[1].nOffset
= 0;
98 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
99 editor
->pCursors
[0].nOffset
= 0;
100 ME_InvalidateSelection(editor
);
101 ME_ClearTempStyle(editor
);
105 /* if both values are equal and also out of bound, that means to */
106 /* put the selection at the end of the text */
107 if ((from
== to
) && (to
< 0 || to
> len
))
113 /* if from is negative and to is positive then selection is */
114 /* deselected and caret moved to end of the current selection */
118 ME_GetSelection(editor
, &start
, &end
);
119 editor
->pCursors
[1] = editor
->pCursors
[0];
121 ME_ClearTempStyle(editor
);
125 /* adjust to if it's a negative value */
129 /* flip from and to if they are reversed */
137 /* after fiddling with the values, we find from > len && to > len */
140 /* special case with to too big */
147 editor
->pCursors
[1].pRun
= editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
148 editor
->pCursors
[1].nOffset
= editor
->pCursors
[0].nOffset
= 0;
149 ME_InvalidateSelection(editor
);
150 ME_ClearTempStyle(editor
);
154 ME_RunOfsFromCharOfs(editor
, from
, &editor
->pCursors
[1].pRun
, &editor
->pCursors
[1].nOffset
);
155 ME_RunOfsFromCharOfs(editor
, to
, &editor
->pCursors
[0].pRun
, &editor
->pCursors
[0].nOffset
);
161 ME_GetCursorCoordinates(ME_TextEditor
*editor
, ME_Cursor
*pCursor
,
162 int *x
, int *y
, int *height
)
164 ME_DisplayItem
*pCursorRun
= pCursor
->pRun
;
165 ME_DisplayItem
*pSizeRun
= pCursor
->pRun
;
167 assert(height
&& x
&& y
);
168 assert(!(ME_GetParagraph(pCursorRun
)->member
.para
.nFlags
& MEPF_REWRAP
));
169 assert(pCursor
->pRun
);
170 assert(pCursor
->pRun
->type
== diRun
);
172 if (pCursorRun
->type
== diRun
) {
173 ME_DisplayItem
*row
= ME_FindItemBack(pCursorRun
, diStartRowOrParagraph
);
176 HDC hDC
= GetDC(editor
->hWnd
);
178 ME_DisplayItem
*run
= pCursorRun
;
179 ME_DisplayItem
*para
= NULL
;
182 ME_InitContext(&c
, editor
, hDC
);
184 if (!pCursor
->nOffset
)
186 ME_DisplayItem
*prev
= ME_FindItemBack(pCursorRun
, diRunOrParagraph
);
188 if (prev
->type
== diRun
)
191 assert(row
->type
== diStartRow
); /* paragraph -> run without start row ?*/
192 para
= ME_FindItemBack(row
, diParagraph
);
194 assert(para
->type
== diParagraph
);
195 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
&&
196 run
== ME_FindItemFwd(row
, diRun
))
198 ME_DisplayItem
*tmp
= ME_FindItemBack(row
, diRunOrParagraph
);
200 if (tmp
->type
== diRun
)
202 row
= ME_FindItemBack(tmp
, diStartRow
);
203 pSizeRun
= run
= tmp
;
205 assert(run
->type
== diRun
);
206 sz
= ME_GetRunSize(&c
, ¶
->member
.para
,
207 &run
->member
.run
, ME_StrLen(run
->member
.run
.strText
),
208 row
->member
.row
.nLMargin
);
211 if (pCursor
->nOffset
) {
212 sz
= ME_GetRunSize(&c
, ¶
->member
.para
, &run
->member
.run
, pCursor
->nOffset
,
213 row
->member
.row
.nLMargin
);
216 *height
= pSizeRun
->member
.run
.nAscent
+ pSizeRun
->member
.run
.nDescent
;
217 *x
= c
.rcView
.left
+ run
->member
.run
.pt
.x
+ sz
.cx
;
218 *y
= c
.rcView
.top
+ para
->member
.para
.pt
.y
+ row
->member
.row
.nBaseline
219 + run
->member
.run
.pt
.y
- pSizeRun
->member
.run
.nAscent
- ME_GetYScrollPos(editor
);
220 ME_DestroyContext(&c
, editor
->hWnd
);
224 *height
= 10; /* FIXME use global font */
231 ME_MoveCaret(ME_TextEditor
*editor
)
235 if (ME_WrapMarkedParagraphs(editor
))
236 ME_UpdateScrollBar(editor
);
237 ME_GetCursorCoordinates(editor
, &editor
->pCursors
[0], &x
, &y
, &height
);
238 if(editor
->bHaveFocus
&& !ME_IsSelection(editor
))
240 x
= min(x
, editor
->rcFormat
.right
-1);
241 CreateCaret(editor
->hWnd
, NULL
, 0, height
);
247 void ME_ShowCaret(ME_TextEditor
*ed
)
250 if(ed
->bHaveFocus
&& !ME_IsSelection(ed
))
254 void ME_HideCaret(ME_TextEditor
*ed
)
256 if(!ed
->bHaveFocus
|| ME_IsSelection(ed
))
263 BOOL
ME_InternalDeleteText(ME_TextEditor
*editor
, int nOfs
, int nChars
,
268 int totalChars
= nChars
;
269 ME_DisplayItem
*start_para
;
272 /* Prevent deletion past last end of paragraph run. */
273 ME_DisplayItem
*pTextEnd
= editor
->pBuffer
->pLast
;
274 int nMaxChars
= pTextEnd
->member
.para
.prev_para
->member
.para
.nCharOfs
;
275 nMaxChars
+= ME_FindItemBack(pTextEnd
, diRun
)->member
.run
.nCharOfs
;
277 nChars
= min(nChars
, nMaxChars
);
280 ME_CursorFromCharOfs(editor
, nOfs
, &c
);
281 start_para
= ME_GetParagraph(c
.pRun
);
285 ME_ProtectPartialTableDeletion(editor
, nOfs
, &nChars
);
293 ME_CursorFromCharOfs(editor
, nOfs
+nChars
, &c
);
295 nOfs
+nChars
== (c
.pRun
->member
.run
.nCharOfs
296 + ME_GetParagraph(c
.pRun
)->member
.para
.nCharOfs
))
298 /* We aren't deleting anything in this run, so we will go back to the
299 * last run we are deleting text in. */
300 c
.pRun
= ME_FindItemBack(c
.pRun
, diRun
);
301 if (c
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
302 c
.nOffset
= c
.pRun
->member
.run
.nCR
+ c
.pRun
->member
.run
.nLF
;
304 c
.nOffset
= c
.pRun
->member
.run
.strText
->nLen
;
306 run
= &c
.pRun
->member
.run
;
307 if (run
->nFlags
& MERF_ENDPARA
) {
308 int eollen
= run
->nCR
+ run
->nLF
;
309 BOOL keepFirstParaFormat
;
311 if (!ME_FindItemFwd(c
.pRun
, diParagraph
))
315 keepFirstParaFormat
= (totalChars
== nChars
&& nChars
<= eollen
&&
317 if (!editor
->bEmulateVersion10
) /* v4.1 */
319 ME_DisplayItem
*next_para
= ME_FindItemFwd(c
.pRun
, diParagraphOrEnd
);
320 ME_DisplayItem
*this_para
= next_para
->member
.para
.prev_para
;
322 /* The end of paragraph before a table row is only deleted if there
323 * is nothing else on the line before it. */
324 if (this_para
== start_para
&&
325 next_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
327 /* If the paragraph will be empty, then it should be deleted, however
328 * it still might have text right now which would inherit the
329 * MEPF_STARTROW property if we joined it right now.
330 * Instead we will delete it after the preceding text is deleted. */
331 if (nOfs
> this_para
->member
.para
.nCharOfs
) {
332 /* Skip this end of line. */
333 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
336 keepFirstParaFormat
= TRUE
;
339 ME_JoinParagraphs(editor
, ME_GetParagraph(c
.pRun
), keepFirstParaFormat
);
340 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
341 ME_CheckCharOffsets(editor
);
342 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
348 int nCharsToDelete
= min(nChars
, c
.nOffset
);
351 c
.nOffset
-= nCharsToDelete
;
353 ME_FindItemBack(c
.pRun
, diParagraph
)->member
.para
.nFlags
|= MEPF_REWRAP
;
356 /* nChars is the number of characters that should be deleted from the
357 PRECEDING runs (these BEFORE cursor.pRun)
358 nCharsToDelete is a number of chars to delete from THIS run */
359 nChars
-= nCharsToDelete
;
360 shift
-= nCharsToDelete
;
361 TRACE("Deleting %d (remaning %d) chars at %d in '%s' (%d)\n",
362 nCharsToDelete
, nChars
, c
.nOffset
,
363 debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
365 if (!c
.nOffset
&& ME_StrVLen(run
->strText
) == nCharsToDelete
)
367 /* undo = reinsert whole run */
368 /* nOfs is a character offset (from the start of the document
369 to the current (deleted) run */
370 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
372 pUndo
->di
.member
.run
.nCharOfs
= nOfs
+nChars
;
376 /* undo = reinsert partial run */
377 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
379 ME_DestroyString(pUndo
->di
.member
.run
.strText
);
380 pUndo
->di
.member
.run
.nCharOfs
= nOfs
+nChars
;
381 pUndo
->di
.member
.run
.strText
= ME_MakeStringN(run
->strText
->szData
+c
.nOffset
, nCharsToDelete
);
384 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
385 TRACE("Shift value: %d\n", shift
);
386 ME_StrDeleteV(run
->strText
, c
.nOffset
, nCharsToDelete
);
388 /* update cursors (including c) */
389 for (i
=-1; i
<editor
->nCursors
; i
++) {
390 ME_Cursor
*pThisCur
= editor
->pCursors
+ i
;
391 if (i
== -1) pThisCur
= &c
;
392 if (pThisCur
->pRun
== cursor
.pRun
) {
393 if (pThisCur
->nOffset
> cursor
.nOffset
) {
394 if (pThisCur
->nOffset
-cursor
.nOffset
< nCharsToDelete
)
395 pThisCur
->nOffset
= cursor
.nOffset
;
397 pThisCur
->nOffset
-= nCharsToDelete
;
398 assert(pThisCur
->nOffset
>= 0);
399 assert(pThisCur
->nOffset
<= ME_StrVLen(run
->strText
));
401 if (pThisCur
->nOffset
== ME_StrVLen(run
->strText
))
403 pThisCur
->pRun
= ME_FindItemFwd(pThisCur
->pRun
, diRunOrParagraphOrEnd
);
404 assert(pThisCur
->pRun
->type
== diRun
);
405 pThisCur
->nOffset
= 0;
410 /* c = updated data now */
412 if (c
.pRun
== cursor
.pRun
)
413 ME_SkipAndPropagateCharOffset(c
.pRun
, shift
);
415 ME_PropagateCharOffset(c
.pRun
, shift
);
417 if (!ME_StrVLen(cursor
.pRun
->member
.run
.strText
))
419 TRACE("Removing useless run\n");
420 ME_Remove(cursor
.pRun
);
421 ME_DestroyDisplayItem(cursor
.pRun
);
426 ME_CheckCharOffsets(editor);
434 BOOL
ME_DeleteTextAtCursor(ME_TextEditor
*editor
, int nCursor
, int nChars
)
436 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
437 /* text operations set modified state */
438 editor
->nModifyStep
= 1;
439 return ME_InternalDeleteText(editor
, ME_GetCursorOfs(editor
, nCursor
), nChars
,
443 static ME_DisplayItem
*
444 ME_InternalInsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
445 const WCHAR
*str
, int len
, ME_Style
*style
,
448 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
450 editor
->bCaretAtEnd
= FALSE
;
452 assert(p
->pRun
->type
== diRun
);
454 return ME_InsertRunAtCursor(editor
, p
, style
, str
, len
, flags
);
458 void ME_InsertOLEFromCursor(ME_TextEditor
*editor
, const REOBJECT
* reo
, int nCursor
)
460 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
465 if (ME_IsSelection(editor
))
466 ME_DeleteSelection(editor
);
468 di
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
470 di
->member
.run
.ole_obj
= ALLOC_OBJ(*reo
);
471 ME_CopyReObject(di
->member
.run
.ole_obj
, reo
);
472 ME_SendSelChange(editor
);
476 void ME_InsertEndRowFromCursor(ME_TextEditor
*editor
, int nCursor
)
478 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
483 if (ME_IsSelection(editor
))
484 ME_DeleteSelection(editor
);
486 di
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
488 ME_SendSelChange(editor
);
492 void ME_InsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
493 const WCHAR
*str
, int len
, ME_Style
*style
)
499 /* FIXME really HERE ? */
500 if (ME_IsSelection(editor
))
501 ME_DeleteSelection(editor
);
503 /* FIXME: is this too slow? */
504 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
505 oldLen
= ME_GetTextLength(editor
);
507 /* text operations set modified state */
508 editor
->nModifyStep
= 1;
512 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
516 /* grow the text limit to fit our text */
517 if(editor
->nTextLimit
< oldLen
+len
)
518 editor
->nTextLimit
= oldLen
+ len
;
523 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
524 while(pos
-str
< len
&& *pos
!= '\r' && *pos
!= '\n' && *pos
!= '\t')
526 if (pos
-str
< len
&& *pos
== '\t') { /* handle tabs */
530 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
532 ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, style
, MERF_TAB
);
541 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
542 if (!editor
->bEmulateVersion10
&& pos
-str
< len
-2 && pos
[0] == '\r' && pos
[1] == '\r' && pos
[2] == '\n') {
546 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
548 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, style
, 0);
557 if (pos
-str
< len
) { /* handle EOLs */
558 ME_DisplayItem
*tp
, *end_run
;
563 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
564 p
= &editor
->pCursors
[nCursor
];
566 ME_SplitRunSimple(editor
, p
->pRun
, p
->nOffset
);
567 p
= &editor
->pCursors
[nCursor
];
569 tmp_style
= ME_GetInsertStyle(editor
, nCursor
);
570 /* ME_SplitParagraph increases style refcount */
572 /* Encode and fill number of CR and LF according to emulation mode */
573 if (editor
->bEmulateVersion10
) {
576 /* We have to find out how many consecutive \r are there, and if there
577 is a \n terminating the run of \r's. */
578 numCR
= 0; numLF
= 0;
580 while (tpos
-str
< len
&& *tpos
== '\r') {
584 if (tpos
-str
>= len
) {
585 /* Reached end of text without finding anything but '\r' */
589 numCR
= 1; numLF
= 0;
590 } else if (*tpos
== '\n') {
591 /* The entire run of \r's plus the one \n is one single line break */
595 /* Found some other content past the run of \r's */
597 numCR
= 1; numLF
= 0;
600 if(pos
-str
< len
&& *pos
=='\r')
602 if(pos
-str
< len
&& *pos
=='\n')
604 numCR
= 1; numLF
= 0;
606 tp
= ME_SplitParagraph(editor
, p
->pRun
, p
->pRun
->member
.run
.style
, numCR
, numLF
, 0);
607 p
->pRun
= ME_FindItemFwd(tp
, diRun
);
608 end_run
= ME_FindItemBack(tp
, diRun
);
609 ME_ReleaseStyle(end_run
->member
.run
.style
);
610 end_run
->member
.run
.style
= tmp_style
;
619 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, len
, style
, 0);
626 ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
628 ME_DisplayItem
*pRun
= pCursor
->pRun
;
632 if (!pCursor
->nOffset
)
635 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
642 if (pRun
->member
.para
.prev_para
->type
== diTextStart
)
644 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
645 /* every paragraph ought to have at least one run */
646 assert(pRun
&& pRun
->type
== diRun
);
647 assert(pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
650 assert(pRun
->type
!= diRun
&& pRun
->type
!= diParagraph
);
653 } while (RUN_IS_HIDDEN(&pRun
->member
.run
) ||
654 pRun
->member
.run
.nFlags
& MERF_HIDDEN
);
655 pCursor
->pRun
= pRun
;
656 if (pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
657 pCursor
->nOffset
= 0;
659 pCursor
->nOffset
= pRun
->member
.run
.strText
->nLen
;
662 if (pCursor
->nOffset
)
663 pCursor
->nOffset
= ME_StrRelPos2(pCursor
->pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
668 if (!(pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
670 int new_ofs
= ME_StrRelPos2(pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
672 if (new_ofs
< pRun
->member
.run
.strText
->nLen
)
674 pCursor
->nOffset
= new_ofs
;
679 pRun
= ME_FindItemFwd(pRun
, diRun
);
680 } while (pRun
&& (RUN_IS_HIDDEN(&pRun
->member
.run
) ||
681 pRun
->member
.run
.nFlags
& MERF_HIDDEN
));
684 pCursor
->pRun
= pRun
;
685 pCursor
->nOffset
= 0;
694 ME_MoveCursorWords(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
696 ME_DisplayItem
*pRun
= cursor
->pRun
, *pOtherRun
;
697 int nOffset
= cursor
->nOffset
;
701 /* Backward movement */
704 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
705 nOffset
, WB_MOVEWORDLEFT
);
708 pOtherRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
709 if (pOtherRun
->type
== diRun
)
711 if (ME_CallWordBreakProc(editor
, pOtherRun
->member
.run
.strText
,
712 pOtherRun
->member
.run
.strText
->nLen
- 1,
714 && !(pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
715 && !(cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
716 && !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
, 0,
720 nOffset
= pOtherRun
->member
.run
.strText
->nLen
;
722 else if (pOtherRun
->type
== diParagraph
)
724 if (cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
726 /* Skip empty start of table row paragraph */
727 if (pOtherRun
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
)
728 pOtherRun
= pOtherRun
->member
.para
.prev_para
;
729 /* Paragraph breaks are treated as separate words */
730 if (pOtherRun
->member
.para
.prev_para
->type
== diTextStart
)
733 pRun
= ME_FindItemBack(pOtherRun
, diRunOrParagraph
);
741 /* Forward movement */
742 BOOL last_delim
= FALSE
;
746 if (last_delim
&& !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
747 nOffset
, WB_ISDELIMITER
))
749 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
750 nOffset
, WB_MOVEWORDRIGHT
);
751 if (nOffset
< pRun
->member
.run
.strText
->nLen
)
753 pOtherRun
= ME_FindItemFwd(pRun
, diRunOrParagraphOrEnd
);
754 if (pOtherRun
->type
== diRun
)
756 last_delim
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
757 nOffset
- 1, WB_ISDELIMITER
);
761 else if (pOtherRun
->type
== diParagraph
)
763 if (pOtherRun
->member
.para
.nFlags
& MEPF_ROWSTART
)
764 pOtherRun
= pOtherRun
->member
.para
.next_para
;
765 if (cursor
->pRun
== pRun
)
766 pRun
= ME_FindItemFwd(pOtherRun
, diRun
);
772 if (cursor
->pRun
== pRun
)
780 cursor
->nOffset
= nOffset
;
786 ME_SelectByType(ME_TextEditor
*editor
, ME_SelectionType selectionType
)
788 /* pCursor[0] is the end of the selection
789 * pCursor[1] is the start of the selection (or the position selection anchor)
790 * pCursor[2] and [3] are the selection anchors that are backed up
791 * so they are kept when the selection changes for drag selection.
794 editor
->nSelectionType
= selectionType
;
795 switch(selectionType
)
800 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
801 editor
->pCursors
[1] = editor
->pCursors
[0];
802 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
807 ME_DisplayItem
*pItem
;
808 ME_DIType fwdSearchType
, backSearchType
;
809 if (selectionType
== stParagraph
) {
810 backSearchType
= diParagraph
;
811 fwdSearchType
= diParagraphOrEnd
;
813 backSearchType
= diStartRow
;
814 fwdSearchType
= diStartRowOrParagraphOrEnd
;
816 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, fwdSearchType
);
818 if (pItem
->type
== diTextEnd
)
819 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
821 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
822 editor
->pCursors
[0].nOffset
= 0;
824 pItem
= ME_FindItemBack(pItem
, backSearchType
);
825 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
826 editor
->pCursors
[1].nOffset
= 0;
830 /* Select everything with cursor anchored from the start of the text */
831 editor
->nSelectionType
= stDocument
;
832 editor
->pCursors
[1].pRun
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
);
833 editor
->pCursors
[1].nOffset
= 0;
834 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
835 editor
->pCursors
[0].nOffset
= 0;
839 /* Store the anchor positions for extending the selection. */
840 editor
->pCursors
[2] = editor
->pCursors
[0];
841 editor
->pCursors
[3] = editor
->pCursors
[1];
844 int ME_GetCursorOfs(ME_TextEditor
*editor
, int nCursor
)
846 ME_Cursor
*pCursor
= &editor
->pCursors
[nCursor
];
847 return ME_GetParagraph(pCursor
->pRun
)->member
.para
.nCharOfs
848 + pCursor
->pRun
->member
.run
.nCharOfs
+ pCursor
->nOffset
;
851 /* Helper function for ME_FindPixelPos to find paragraph within tables */
852 static ME_DisplayItem
* ME_FindPixelPosInTableRow(int x
, int y
,
853 ME_DisplayItem
*para
)
855 ME_DisplayItem
*cell
, *next_cell
;
856 assert(para
->member
.para
.nFlags
& MEPF_ROWSTART
);
857 cell
= para
->member
.para
.next_para
->member
.para
.pCell
;
860 /* find the cell we are in */
861 while ((next_cell
= cell
->member
.cell
.next_cell
) != NULL
) {
862 if (x
< next_cell
->member
.cell
.pt
.x
)
864 para
= ME_FindItemFwd(cell
, diParagraph
);
865 /* Found the cell, but there might be multiple paragraphs in
866 * the cell, so need to search down the cell for the paragraph. */
867 while (cell
== para
->member
.para
.pCell
) {
868 if (y
< para
->member
.para
.pt
.y
+ para
->member
.para
.nHeight
)
870 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
871 return ME_FindPixelPosInTableRow(x
, y
, para
);
875 para
= para
->member
.para
.next_para
;
877 /* Past the end of the cell, so go back to the last cell paragraph */
878 return para
->member
.para
.prev_para
;
882 /* Return table row delimiter */
883 para
= ME_FindItemFwd(cell
, diParagraph
);
884 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
885 assert(para
->member
.para
.pFmt
->dwMask
& PFM_TABLEROWDELIMITER
);
886 assert(para
->member
.para
.pFmt
->wEffects
& PFE_TABLEROWDELIMITER
);
890 static BOOL
ME_ReturnFoundPos(ME_TextEditor
*editor
, ME_DisplayItem
*found
,
891 ME_Cursor
*result
, int rx
, BOOL isExact
)
894 assert(found
->type
== diRun
);
895 if ((found
->member
.run
.nFlags
& MERF_ENDPARA
) || rx
< 0)
897 result
->pRun
= found
;
898 result
->nOffset
= ME_CharFromPointCursor(editor
, rx
, &found
->member
.run
);
899 if (editor
->pCursors
[0].nOffset
== found
->member
.run
.strText
->nLen
&& rx
)
901 result
->pRun
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, diRun
);
907 /* Finds the run and offset from the pixel position.
909 * x & y are pixel positions in virtual coordinates into the rich edit control,
910 * so client coordinates must first be adjusted by the scroll position.
912 * returns TRUE if the result was exactly under the cursor, otherwise returns
913 * FALSE, and result is set to the closest position to the coordinates.
915 static BOOL
ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
,
916 ME_Cursor
*result
, BOOL
*is_eol
)
918 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
919 ME_DisplayItem
*last
= NULL
;
923 x
-= editor
->rcFormat
.left
;
924 y
-= editor
->rcFormat
.top
;
930 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->member
.para
.next_para
)
932 assert(p
->type
== diParagraph
);
933 if (y
< p
->member
.para
.pt
.y
+ p
->member
.para
.nHeight
)
935 if (p
->member
.para
.nFlags
& MEPF_ROWSTART
)
936 p
= ME_FindPixelPosInTableRow(x
, y
, p
);
937 y
-= p
->member
.para
.pt
.y
;
938 p
= ME_FindItemFwd(p
, diStartRow
);
940 } else if (p
->member
.para
.nFlags
& MEPF_ROWSTART
) {
941 p
= ME_GetTableRowEnd(p
);
945 for (; p
!= editor
->pBuffer
->pLast
; )
948 assert(p
->type
== diStartRow
);
949 if (y
< p
->member
.row
.pt
.y
+ p
->member
.row
.nHeight
)
951 p
= ME_FindItemFwd(p
, diRun
);
954 pp
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
955 if (pp
->type
!= diStartRow
)
957 p
= ME_FindItemFwd(p
, diRun
);
962 if (p
== editor
->pBuffer
->pLast
)
964 /* The position is below the last paragraph, so the last row will be used
965 * rather than the end of the text, so the x position will be used to
966 * determine the offset closest to the pixel position. */
968 p
= ME_FindItemBack(p
, diStartRow
);
970 p
= ME_FindItemFwd(p
, diRun
);
974 p
= editor
->pBuffer
->pLast
;
977 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->next
)
982 rx
= x
- p
->member
.run
.pt
.x
;
983 if (rx
< p
->member
.run
.nWidth
)
984 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
988 p
= ME_FindItemFwd(p
, diRun
);
989 if (is_eol
) *is_eol
= 1;
990 rx
= 0; /* FIXME not sure */
991 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
996 rx
= 0; /* FIXME not sure */
998 return ME_ReturnFoundPos(editor
, p
, result
, rx
, isExact
);
1003 result
->pRun
= ME_FindItemBack(p
, diRun
);
1004 result
->nOffset
= 0;
1005 assert(result
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1010 /* Returns the character offset closest to the pixel position
1012 * x & y are pixel positions in client coordinates.
1014 * isExact will be set to TRUE if the run is directly under the pixel
1015 * position, FALSE if it not, unless isExact is set to NULL.
1017 int ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
, BOOL
*isExact
)
1023 GetClientRect(editor
->hWnd
, &rc
);
1024 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
) {
1025 if (isExact
) *isExact
= FALSE
;
1028 y
+= ME_GetYScrollPos(editor
);
1029 bResult
= ME_FindPixelPos(editor
, x
, y
, &cursor
, NULL
);
1030 if (isExact
) *isExact
= bResult
;
1031 return (ME_GetParagraph(cursor
.pRun
)->member
.para
.nCharOfs
1032 + cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
);
1037 /* Extends the selection with a word, line, or paragraph selection type.
1039 * The selection is anchored by editor->pCursors[2-3] such that the text
1040 * between the anchors will remain selected, and one end will be extended.
1042 * editor->pCursors[0] should have the position to extend the selection to
1043 * before this function is called.
1045 * Nothing will be done if editor->nSelectionType equals stPosition.
1047 static void ME_ExtendAnchorSelection(ME_TextEditor
*editor
)
1049 ME_Cursor tmp_cursor
;
1050 int curOfs
, anchorStartOfs
, anchorEndOfs
;
1051 if (editor
->nSelectionType
== stPosition
|| editor
->nSelectionType
== stDocument
)
1053 curOfs
= ME_GetCursorOfs(editor
, 0);
1054 anchorStartOfs
= ME_GetCursorOfs(editor
, 3);
1055 anchorEndOfs
= ME_GetCursorOfs(editor
, 2);
1057 tmp_cursor
= editor
->pCursors
[0];
1058 editor
->pCursors
[0] = editor
->pCursors
[2];
1059 editor
->pCursors
[1] = editor
->pCursors
[3];
1060 if (curOfs
< anchorStartOfs
)
1062 /* Extend the left side of selection */
1063 editor
->pCursors
[1] = tmp_cursor
;
1064 if (editor
->nSelectionType
== stWord
)
1065 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
1068 ME_DisplayItem
*pItem
;
1069 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1070 diStartRowOrParagraph
:diParagraph
);
1071 pItem
= ME_FindItemBack(editor
->pCursors
[1].pRun
, searchType
);
1072 editor
->pCursors
[1].pRun
= ME_FindItemFwd(pItem
, diRun
);
1073 editor
->pCursors
[1].nOffset
= 0;
1076 else if (curOfs
>= anchorEndOfs
)
1078 /* Extend the right side of selection */
1079 editor
->pCursors
[0] = tmp_cursor
;
1080 if (editor
->nSelectionType
== stWord
)
1081 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
1084 ME_DisplayItem
*pItem
;
1085 ME_DIType searchType
= ((editor
->nSelectionType
== stLine
) ?
1086 diStartRowOrParagraphOrEnd
:diParagraphOrEnd
);
1087 pItem
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, searchType
);
1088 if (pItem
->type
== diTextEnd
)
1089 editor
->pCursors
[0].pRun
= ME_FindItemBack(pItem
, diRun
);
1091 editor
->pCursors
[0].pRun
= ME_FindItemFwd(pItem
, diRun
);
1092 editor
->pCursors
[0].nOffset
= 0;
1097 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
, int clickNum
)
1099 ME_Cursor tmp_cursor
;
1100 int is_selection
= 0;
1103 editor
->nUDArrowX
= -1;
1105 y
+= ME_GetYScrollPos(editor
);
1107 tmp_cursor
= editor
->pCursors
[0];
1108 is_selection
= ME_IsSelection(editor
);
1109 is_shift
= GetKeyState(VK_SHIFT
) < 0;
1111 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
1113 if (x
>= editor
->rcFormat
.left
|| is_shift
)
1117 editor
->pCursors
[1] = editor
->pCursors
[0];
1119 if (x
>= editor
->rcFormat
.left
)
1120 ME_SelectByType(editor
, stWord
);
1122 ME_SelectByType(editor
, stParagraph
);
1123 } else if (clickNum
% 2 == 0) {
1124 ME_SelectByType(editor
, stWord
);
1126 ME_SelectByType(editor
, stParagraph
);
1131 editor
->nSelectionType
= stPosition
;
1132 editor
->pCursors
[1] = editor
->pCursors
[0];
1134 else if (!is_selection
)
1136 editor
->nSelectionType
= stPosition
;
1137 editor
->pCursors
[1] = tmp_cursor
;
1139 else if (editor
->nSelectionType
!= stPosition
)
1141 ME_ExtendAnchorSelection(editor
);
1147 ME_SelectByType(editor
, stLine
);
1148 } else if (clickNum
% 2 == 0 || is_shift
) {
1149 ME_SelectByType(editor
, stParagraph
);
1151 ME_SelectByType(editor
, stDocument
);
1154 ME_InvalidateSelection(editor
);
1155 HideCaret(editor
->hWnd
);
1156 ME_ShowCaret(editor
);
1157 ME_ClearTempStyle(editor
);
1158 ME_SendSelChange(editor
);
1161 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
1163 ME_Cursor tmp_cursor
;
1165 if (editor
->nSelectionType
== stDocument
)
1167 y
+= ME_GetYScrollPos(editor
);
1169 tmp_cursor
= editor
->pCursors
[0];
1170 /* FIXME: do something with the return value of ME_FindPixelPos */
1171 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
1173 ME_InvalidateSelection(editor
);
1174 editor
->pCursors
[0] = tmp_cursor
;
1175 ME_ExtendAnchorSelection(editor
);
1177 if (editor
->nSelectionType
!= stPosition
&&
1178 memcmp(&editor
->pCursors
[1], &editor
->pCursors
[3], sizeof(ME_Cursor
)))
1180 /* The scroll the cursor towards the other end, since it was the one
1181 * extended by ME_ExtendAnchorSelection */
1182 ME_EnsureVisible(editor
, editor
->pCursors
[1].pRun
);
1184 ME_EnsureVisible(editor
, editor
->pCursors
[0].pRun
);
1187 ME_InvalidateSelection(editor
);
1188 HideCaret(editor
->hWnd
);
1189 ME_ShowCaret(editor
);
1190 ME_SendSelChange(editor
);
1193 static ME_DisplayItem
*ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
1194 int x
, int *pOffset
, int *pbCaretAtEnd
)
1196 ME_DisplayItem
*pNext
, *pLastRun
;
1197 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
1198 assert(pNext
->type
== diRun
);
1200 if (pbCaretAtEnd
) *pbCaretAtEnd
= FALSE
;
1201 if (pOffset
) *pOffset
= 0;
1203 int run_x
= pNext
->member
.run
.pt
.x
;
1204 int width
= pNext
->member
.run
.nWidth
;
1209 if (x
>= run_x
&& x
< run_x
+width
)
1211 int ch
= ME_CharFromPointCursor(editor
, x
-run_x
, &pNext
->member
.run
);
1212 ME_String
*s
= pNext
->member
.run
.strText
;
1220 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
1221 } while(pNext
&& pNext
->type
== diRun
);
1223 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
1225 pNext
= ME_FindItemFwd(pNext
, diRun
);
1226 if (pbCaretAtEnd
) *pbCaretAtEnd
= TRUE
;
1233 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1235 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1238 if (editor
->nUDArrowX
!= -1)
1239 x
= editor
->nUDArrowX
;
1241 if (editor
->bCaretAtEnd
)
1243 pRun
= ME_FindItemBack(pRun
, diRun
);
1245 x
= pRun
->member
.run
.pt
.x
+ pRun
->member
.run
.nWidth
;
1248 x
= pRun
->member
.run
.pt
.x
;
1249 x
+= ME_PointFromChar(editor
, &pRun
->member
.run
, pCursor
->nOffset
);
1251 editor
->nUDArrowX
= x
;
1258 ME_MoveCursorLines(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
1260 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1261 ME_DisplayItem
*pItem
, *pOldPara
, *pNewPara
;
1262 int x
= ME_GetXForArrow(editor
, pCursor
);
1264 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1265 pRun
= ME_FindItemBack(pRun
, diRun
);
1268 pOldPara
= ME_GetParagraph(pRun
);
1271 /* start of this row */
1272 pItem
= ME_FindItemBack(pRun
, diStartRow
);
1274 /* start of the previous row */
1275 pItem
= ME_FindItemBack(pItem
, diStartRow
);
1277 return; /* row not found - ignore */
1278 pNewPara
= ME_GetParagraph(pItem
);
1279 if (pOldPara
->member
.para
.nFlags
& MEPF_ROWEND
||
1280 (pOldPara
->member
.para
.pCell
&&
1281 pOldPara
->member
.para
.pCell
!= pNewPara
->member
.para
.pCell
))
1283 /* Brought out of a cell */
1284 pNewPara
= ME_GetTableRowStart(pOldPara
)->member
.para
.prev_para
;
1285 if (pNewPara
->type
== diTextStart
)
1286 return; /* At the top, so don't go anywhere. */
1287 pItem
= ME_FindItemFwd(pNewPara
, diStartRow
);
1289 if (pNewPara
->member
.para
.nFlags
& MEPF_ROWEND
)
1291 /* Brought into a table row */
1292 ME_Cell
*cell
= &ME_FindItemBack(pNewPara
, diCell
)->member
.cell
;
1293 while (x
< cell
->pt
.x
&& cell
->prev_cell
)
1294 cell
= &cell
->prev_cell
->member
.cell
;
1295 if (cell
->next_cell
) /* else - we are still at the end of the row */
1296 pItem
= ME_FindItemBack(cell
->next_cell
, diStartRow
);
1301 /* start of the next row */
1302 pItem
= ME_FindItemFwd(pRun
, diStartRow
);
1304 return; /* row not found - ignore */
1305 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1307 pNewPara
= ME_GetParagraph(pItem
);
1308 if (pOldPara
->member
.para
.nFlags
& MEPF_ROWSTART
||
1309 (pOldPara
->member
.para
.pCell
&&
1310 pOldPara
->member
.para
.pCell
!= pNewPara
->member
.para
.pCell
))
1312 /* Brought out of a cell */
1313 pNewPara
= ME_GetTableRowEnd(pOldPara
)->member
.para
.next_para
;
1314 if (pNewPara
->type
== diTextEnd
)
1315 return; /* At the bottom, so don't go anywhere. */
1316 pItem
= ME_FindItemFwd(pNewPara
, diStartRow
);
1318 if (pNewPara
->member
.para
.nFlags
& MEPF_ROWSTART
)
1320 /* Brought into a table row */
1321 ME_DisplayItem
*cell
= ME_FindItemFwd(pNewPara
, diCell
);
1322 while (cell
->member
.cell
.next_cell
&&
1323 x
>= cell
->member
.cell
.next_cell
->member
.cell
.pt
.x
)
1324 cell
= cell
->member
.cell
.next_cell
;
1325 pItem
= ME_FindItemFwd(cell
, diStartRow
);
1330 /* row not found - ignore */
1333 pCursor
->pRun
= ME_FindRunInRow(editor
, pItem
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1334 assert(pCursor
->pRun
);
1335 assert(pCursor
->pRun
->type
== diRun
);
1339 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1341 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1342 ME_DisplayItem
*pLast
, *p
;
1343 int x
, y
, ys
, yd
, yp
, yprev
;
1344 ME_Cursor tmp_curs
= *pCursor
;
1346 x
= ME_GetXForArrow(editor
, pCursor
);
1347 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1348 pRun
= ME_FindItemBack(pRun
, diRun
);
1350 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1351 assert(p
->type
== diStartRow
);
1352 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1353 yprev
= ys
= y
= yp
+ p
->member
.row
.pt
.y
;
1354 yd
= y
- editor
->sizeWindow
.cy
;
1358 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
1361 if (p
->type
== diParagraph
) { /* crossing paragraphs */
1362 if (p
->member
.para
.prev_para
== NULL
)
1364 yp
= p
->member
.para
.prev_para
->member
.para
.pt
.y
;
1367 y
= yp
+ p
->member
.row
.pt
.y
;
1374 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1375 ME_UpdateSelection(editor
, &tmp_curs
);
1376 if (yprev
< editor
->sizeWindow
.cy
)
1378 ME_EnsureVisible(editor
, ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
));
1383 ME_ScrollUp(editor
, ys
-yprev
);
1385 assert(pCursor
->pRun
);
1386 assert(pCursor
->pRun
->type
== diRun
);
1389 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1390 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1391 In such a situation, clicking the scrollbar restores its position back to the
1392 normal range (ie. sets it to (doclength-screenheight)). */
1394 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1396 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1397 ME_DisplayItem
*pLast
, *p
;
1398 int x
, y
, ys
, yd
, yp
, yprev
;
1399 ME_Cursor tmp_curs
= *pCursor
;
1401 x
= ME_GetXForArrow(editor
, pCursor
);
1402 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1403 pRun
= ME_FindItemBack(pRun
, diRun
);
1405 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1406 assert(p
->type
== diStartRow
);
1407 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.pt
.y
;
1408 yprev
= ys
= y
= yp
+ p
->member
.row
.pt
.y
;
1409 yd
= y
+ editor
->sizeWindow
.cy
;
1413 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
1416 if (p
->type
== diParagraph
) {
1417 yp
= p
->member
.para
.pt
.y
;
1420 y
= yp
+ p
->member
.row
.pt
.y
;
1427 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1428 ME_UpdateSelection(editor
, &tmp_curs
);
1429 if (yprev
>= editor
->nTotalLength
-editor
->sizeWindow
.cy
)
1431 ME_EnsureVisible(editor
, ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
));
1436 ME_ScrollUp(editor
,ys
-yprev
);
1438 assert(pCursor
->pRun
);
1439 assert(pCursor
->pRun
->type
== diRun
);
1442 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1444 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
1445 ME_WrapMarkedParagraphs(editor
);
1447 ME_DisplayItem
*pRun
;
1448 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
1449 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1453 pRun
= ME_FindItemFwd(pRow
, diRun
);
1455 pCursor
->pRun
= pRun
;
1456 pCursor
->nOffset
= 0;
1459 editor
->bCaretAtEnd
= FALSE
;
1462 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1464 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diTextStart
);
1466 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1468 pCursor
->pRun
= pRun
;
1469 pCursor
->nOffset
= 0;
1474 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1476 ME_DisplayItem
*pRow
;
1478 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1481 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1483 if (pRow
->type
== diStartRow
) {
1484 /* FIXME WTF was I thinking about here ? */
1485 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1487 pCursor
->pRun
= pRun
;
1488 pCursor
->nOffset
= 0;
1489 editor
->bCaretAtEnd
= 1;
1492 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1493 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1494 pCursor
->nOffset
= 0;
1495 editor
->bCaretAtEnd
= FALSE
;
1498 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1500 ME_DisplayItem
*p
= ME_FindItemFwd(pCursor
->pRun
, diTextEnd
);
1502 p
= ME_FindItemBack(p
, diRun
);
1504 assert(p
->member
.run
.nFlags
& MERF_ENDPARA
);
1506 pCursor
->nOffset
= 0;
1507 editor
->bCaretAtEnd
= FALSE
;
1510 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1512 return memcmp(&editor
->pCursors
[0], &editor
->pCursors
[1], sizeof(ME_Cursor
))!=0;
1515 static int ME_GetSelCursor(ME_TextEditor
*editor
, int dir
)
1517 int cdir
= ME_GetCursorOfs(editor
, 0) - ME_GetCursorOfs(editor
, 1);
1525 BOOL
ME_UpdateSelection(ME_TextEditor
*editor
, const ME_Cursor
*pTempCursor
)
1527 ME_Cursor old_anchor
= editor
->pCursors
[1];
1529 if (GetKeyState(VK_SHIFT
)>=0) /* cancelling selection */
1531 /* any selection was present ? if so, it's no more, repaint ! */
1532 editor
->pCursors
[1] = editor
->pCursors
[0];
1533 if (memcmp(pTempCursor
, &old_anchor
, sizeof(ME_Cursor
))) {
1540 if (!memcmp(pTempCursor
, &editor
->pCursors
[1], sizeof(ME_Cursor
))) /* starting selection */
1542 editor
->pCursors
[1] = *pTempCursor
;
1551 void ME_DeleteSelection(ME_TextEditor
*editor
)
1554 ME_GetSelection(editor
, &from
, &to
);
1555 ME_DeleteTextAtCursor(editor
, ME_GetSelCursor(editor
,-1), to
-from
);
1558 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1560 return ME_GetInsertStyle(editor
, 0);
1563 void ME_SendSelChange(ME_TextEditor
*editor
)
1567 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1570 sc
.nmhdr
.code
= EN_SELCHANGE
;
1571 ME_GetSelection(editor
, &sc
.chrg
.cpMin
, &sc
.chrg
.cpMax
);
1572 sc
.seltyp
= SEL_EMPTY
;
1573 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1574 sc
.seltyp
|= SEL_TEXT
;
1575 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* wth were RICHEDIT authors thinking ? */
1576 sc
.seltyp
|= SEL_MULTICHAR
;
1577 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1578 sc
.chrg
.cpMin
, sc
.chrg
.cpMax
, sc
.seltyp
,
1579 (sc
.seltyp
& SEL_TEXT
) ? "SEL_TEXT" : "",
1580 (sc
.seltyp
& SEL_MULTICHAR
) ? "SEL_MULTICHAR" : "");
1581 if (sc
.chrg
.cpMin
!= editor
->notified_cr
.cpMin
|| sc
.chrg
.cpMax
!= editor
->notified_cr
.cpMax
)
1583 ME_ClearTempStyle(editor
);
1585 editor
->notified_cr
= sc
.chrg
;
1586 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
, sc
.nmhdr
.idFrom
, (LPARAM
)&sc
);
1591 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1594 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1595 ME_Cursor tmp_curs
= *p
;
1596 BOOL success
= FALSE
;
1598 ME_CheckCharOffsets(editor
);
1601 editor
->bCaretAtEnd
= 0;
1603 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1605 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1608 editor
->bCaretAtEnd
= 0;
1610 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1612 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1615 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1618 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1621 ME_ArrowPageUp(editor
, &tmp_curs
);
1624 ME_ArrowPageDown(editor
, &tmp_curs
);
1628 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1630 ME_ArrowHome(editor
, &tmp_curs
);
1631 editor
->bCaretAtEnd
= 0;
1636 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1638 ME_ArrowEnd(editor
, &tmp_curs
);
1643 editor
->pCursors
[1] = tmp_curs
;
1646 ME_InvalidateSelection(editor
);
1648 HideCaret(editor
->hWnd
);
1649 ME_EnsureVisible(editor
, tmp_curs
.pRun
);
1650 ME_ShowCaret(editor
);
1651 ME_SendSelChange(editor
);