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
);
31 void ME_GetSelection(ME_TextEditor
*editor
, int *from
, int *to
)
33 *from
= ME_GetCursorOfs(editor
, 0);
34 *to
= ME_GetCursorOfs(editor
, 1);
44 int ME_GetTextLength(ME_TextEditor
*editor
)
46 return ME_CharOfsFromRunOfs(editor
, ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
), 0);
50 int ME_GetTextLengthEx(ME_TextEditor
*editor
, GETTEXTLENGTHEX
*how
)
54 if (how
->flags
& GTL_PRECISE
&& how
->flags
& GTL_CLOSE
)
56 if (how
->flags
& GTL_NUMCHARS
&& how
->flags
& GTL_NUMBYTES
)
59 length
= ME_GetTextLength(editor
);
61 if (how
->flags
& GTL_USECRLF
)
62 length
+= editor
->nParagraphs
;
64 if (how
->flags
& GTL_NUMBYTES
)
68 if (how
->codepage
== 1200)
70 if (how
->flags
& GTL_PRECISE
)
71 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
72 if (GetCPInfo(how
->codepage
, &cpinfo
))
73 return length
* cpinfo
.MaxCharSize
;
74 ERR("Invalid codepage %u\n", how
->codepage
);
81 void ME_SetSelection(ME_TextEditor
*editor
, int from
, int to
)
83 if (from
== 0 && to
== -1)
85 editor
->pCursors
[1].pRun
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
);
86 editor
->pCursors
[1].nOffset
= 0;
87 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
88 editor
->pCursors
[0].nOffset
= 0;
89 ME_InvalidateSelection(editor
);
90 ME_ClearTempStyle(editor
);
93 if (from
== -1 && to
== -1) /*-1,-1 means put the selection at the end of the text */
95 editor
->pCursors
[1].pRun
= editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
96 editor
->pCursors
[1].nOffset
= editor
->pCursors
[0].nOffset
= 0;
97 ME_InvalidateSelection(editor
);
98 ME_ClearTempStyle(editor
);
103 editor
->pCursors
[1] = editor
->pCursors
[0];
105 ME_ClearTempStyle(editor
);
114 ME_RunOfsFromCharOfs(editor
, from
, &editor
->pCursors
[1].pRun
, &editor
->pCursors
[1].nOffset
);
115 ME_RunOfsFromCharOfs(editor
, to
, &editor
->pCursors
[0].pRun
, &editor
->pCursors
[0].nOffset
);
120 ME_GetCursorCoordinates(ME_TextEditor
*editor
, ME_Cursor
*pCursor
,
121 int *x
, int *y
, int *height
)
123 ME_DisplayItem
*pCursorRun
= pCursor
->pRun
;
124 ME_DisplayItem
*pSizeRun
= pCursor
->pRun
;
126 assert(!pCursor
->nOffset
|| !editor
->bCaretAtEnd
);
127 assert(height
&& x
&& y
);
128 assert(!(ME_GetParagraph(pCursorRun
)->member
.para
.nFlags
& MEPF_REWRAP
));
129 assert(pCursor
->pRun
);
130 assert(pCursor
->pRun
->type
== diRun
);
132 if (pCursorRun
->type
== diRun
) {
133 ME_DisplayItem
*row
= ME_FindItemBack(pCursorRun
, diStartRowOrParagraph
);
136 HDC hDC
= GetDC(editor
->hWnd
);
138 ME_DisplayItem
*run
= pCursorRun
;
139 ME_DisplayItem
*para
= NULL
;
142 ME_InitContext(&c
, editor
, hDC
);
144 if (!pCursor
->nOffset
&& !editor
->bCaretAtEnd
)
146 ME_DisplayItem
*prev
= ME_FindItemBack(pCursorRun
, diRunOrStartRow
);
148 if (prev
->type
== diRun
)
151 assert(row
->type
== diStartRow
); /* paragraph -> run without start row ?*/
152 para
= ME_FindItemBack(row
, diParagraph
);
154 assert(para
->type
== diParagraph
);
155 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
&&
156 run
== ME_FindItemFwd(row
, diRun
))
158 ME_DisplayItem
*tmp
= ME_FindItemBack(row
, diRunOrParagraph
);
160 if (tmp
->type
== diRun
)
162 row
= ME_FindItemBack(tmp
, diStartRow
);
163 pSizeRun
= run
= tmp
;
165 assert(run
->type
== diRun
);
166 sz
= ME_GetRunSize(&c
, ¶
->member
.para
, &run
->member
.run
, ME_StrLen(run
->member
.run
.strText
));
169 if (pCursor
->nOffset
&& !(run
->member
.run
.nFlags
& MERF_SKIPPED
)) {
170 sz
= ME_GetRunSize(&c
, ¶
->member
.para
, &run
->member
.run
, pCursor
->nOffset
);
173 *height
= pSizeRun
->member
.run
.nAscent
+ pSizeRun
->member
.run
.nDescent
;
174 *x
= run
->member
.run
.pt
.x
+ sz
.cx
;
175 *y
= para
->member
.para
.nYPos
+ row
->member
.row
.nBaseline
+ pSizeRun
->member
.run
.pt
.y
- pSizeRun
->member
.run
.nAscent
- ME_GetYScrollPos(editor
);
177 ME_DestroyContext(&c
);
178 ReleaseDC(editor
->hWnd
, hDC
);
182 *height
= 10; /* FIXME use global font */
189 ME_MoveCaret(ME_TextEditor
*editor
)
193 ME_WrapMarkedParagraphs(editor
);
194 ME_GetCursorCoordinates(editor
, &editor
->pCursors
[0], &x
, &y
, &height
);
195 CreateCaret(editor
->hWnd
, NULL
, 0, height
);
200 void ME_ShowCaret(ME_TextEditor
*ed
)
206 void ME_HideCaret(ME_TextEditor
*ed
)
212 void ME_InternalDeleteText(ME_TextEditor
*editor
, int nOfs
,
221 ME_CursorFromCharOfs(editor
, nOfs
, &c
);
222 run
= &c
.pRun
->member
.run
;
223 if (run
->nFlags
& MERF_ENDPARA
) {
224 if (!ME_FindItemFwd(c
.pRun
, diParagraph
))
228 ME_JoinParagraphs(editor
, ME_GetParagraph(c
.pRun
));
229 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
230 ME_CheckCharOffsets(editor
);
232 if (editor
->bEmulateVersion10
&& nChars
)
239 int nIntendedChars
= nChars
;
240 int nCharsToDelete
= nChars
;
244 ME_FindItemBack(c
.pRun
, diParagraph
)->member
.para
.nFlags
|= MEPF_REWRAP
;
247 ME_StrRelPos(run
->strText
, loc
, &nChars
);
248 /* nChars is the number of characters that should be deleted from the
249 FOLLOWING runs (these AFTER cursor.pRun)
250 nCharsToDelete is a number of chars to delete from THIS run */
251 nCharsToDelete
-= nChars
;
252 shift
-= nCharsToDelete
;
253 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
254 nCharsToDelete
, nIntendedChars
, nChars
, c
.nOffset
,
255 debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
257 if (!c
.nOffset
&& ME_StrVLen(run
->strText
) == nCharsToDelete
)
259 /* undo = reinsert whole run */
260 /* nOfs is a character offset (from the start of the document
261 to the current (deleted) run */
262 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
264 pUndo
->di
.member
.run
.nCharOfs
= nOfs
;
268 /* undo = reinsert partial run */
269 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
271 ME_DestroyString(pUndo
->di
.member
.run
.strText
);
272 pUndo
->di
.member
.run
.nCharOfs
= nOfs
;
273 pUndo
->di
.member
.run
.strText
= ME_MakeStringN(run
->strText
->szData
+c
.nOffset
, nCharsToDelete
);
276 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
277 TRACE("Shift value: %d\n", shift
);
278 ME_StrDeleteV(run
->strText
, c
.nOffset
, nCharsToDelete
);
280 /* update cursors (including c) */
281 for (i
=-1; i
<editor
->nCursors
; i
++) {
282 ME_Cursor
*pThisCur
= editor
->pCursors
+ i
;
283 if (i
== -1) pThisCur
= &c
;
284 if (pThisCur
->pRun
== cursor
.pRun
) {
285 if (pThisCur
->nOffset
> cursor
.nOffset
) {
286 if (pThisCur
->nOffset
-cursor
.nOffset
< nCharsToDelete
)
287 pThisCur
->nOffset
= cursor
.nOffset
;
289 pThisCur
->nOffset
-= nCharsToDelete
;
290 assert(pThisCur
->nOffset
>= 0);
291 assert(pThisCur
->nOffset
<= ME_StrVLen(run
->strText
));
293 if (pThisCur
->nOffset
== ME_StrVLen(run
->strText
))
295 pThisCur
->pRun
= ME_FindItemFwd(pThisCur
->pRun
, diRunOrParagraphOrEnd
);
296 assert(pThisCur
->pRun
->type
== diRun
);
297 pThisCur
->nOffset
= 0;
302 /* c = updated data now */
304 if (c
.pRun
== cursor
.pRun
)
305 ME_SkipAndPropagateCharOffset(c
.pRun
, shift
);
307 ME_PropagateCharOffset(c
.pRun
, shift
);
309 if (!ME_StrVLen(cursor
.pRun
->member
.run
.strText
))
311 TRACE("Removing useless run\n");
312 ME_Remove(cursor
.pRun
);
313 ME_DestroyDisplayItem(cursor
.pRun
);
318 ME_CheckCharOffsets(editor);
325 void ME_DeleteTextAtCursor(ME_TextEditor
*editor
, int nCursor
,
328 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
329 ME_InternalDeleteText(editor
, ME_GetCursorOfs(editor
, nCursor
), nChars
);
332 static ME_DisplayItem
*
333 ME_InternalInsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
334 const WCHAR
*str
, int len
, ME_Style
*style
,
337 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
339 editor
->bCaretAtEnd
= FALSE
;
341 assert(p
->pRun
->type
== diRun
);
343 return ME_InsertRunAtCursor(editor
, p
, style
, str
, len
, flags
);
347 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
348 void ME_InsertGraphicsFromCursor(ME_TextEditor
*editor
, int nCursor
)
350 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
354 if (ME_IsSelection(editor
))
355 ME_DeleteSelection(editor
);
357 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
359 ME_SendSelChange(editor
);
364 ME_InsertTableCellFromCursor(ME_TextEditor
*editor
, int nCursor
)
367 ME_DisplayItem
*p
, *run
;
368 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
370 p
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, pStyle
,
373 while ((run
= ME_FindItemBack(run
, diRunOrParagraph
))->type
== diRun
)
375 if (run
->member
.run
.nFlags
& MERF_CELL
)
377 assert(run
->member
.run
.pCell
->next
);
378 p
->member
.run
.pCell
= run
->member
.run
.pCell
->next
;
382 assert(run
->type
== diParagraph
);
383 assert(run
->member
.para
.bTable
);
384 assert(run
->member
.para
.pCells
);
385 p
->member
.run
.pCell
= run
->member
.para
.pCells
;
389 void ME_InsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
390 const WCHAR
*str
, int len
, ME_Style
*style
)
394 /* FIXME: is this too slow? */
395 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
396 int freeSpace
= editor
->nTextLimit
- ME_GetTextLength(editor
);
400 /* FIXME really HERE ? */
401 if (ME_IsSelection(editor
))
402 ME_DeleteSelection(editor
);
404 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
407 len
= min(len
, freeSpace
);
411 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
412 while(pos
-str
< len
&& *pos
!= '\r' && *pos
!= '\n' && *pos
!= '\t')
414 if (pos
-str
< len
&& *pos
== '\t') { /* handle tabs */
418 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
420 ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, style
, MERF_TAB
);
429 if (pos
-str
< len
) { /* handle EOLs */
430 ME_DisplayItem
*tp
, *end_run
;
433 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
434 p
= &editor
->pCursors
[nCursor
];
436 ME_SplitRunSimple(editor
, p
->pRun
, p
->nOffset
);
437 p
= &editor
->pCursors
[nCursor
];
439 tmp_style
= ME_GetInsertStyle(editor
, nCursor
);
440 /* ME_SplitParagraph increases style refcount */
441 tp
= ME_SplitParagraph(editor
, p
->pRun
, p
->pRun
->member
.run
.style
);
442 p
->pRun
= ME_FindItemFwd(tp
, diRun
);
443 end_run
= ME_FindItemBack(tp
, diRun
);
444 ME_ReleaseStyle(end_run
->member
.run
.style
);
445 end_run
->member
.run
.style
= tmp_style
;
447 if(pos
-str
< len
&& *pos
=='\r')
449 if(pos
-str
< len
&& *pos
=='\n')
457 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, len
, style
, 0);
464 ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
466 ME_DisplayItem
*pRun
= pCursor
->pRun
;
470 if (!pCursor
->nOffset
)
473 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
480 if (pRun
->member
.para
.prev_para
->type
== diTextStart
)
482 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
483 /* every paragraph ought to have at least one run */
484 assert(pRun
&& pRun
->type
== diRun
);
485 assert(pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
488 assert(pRun
->type
!= diRun
&& pRun
->type
!= diParagraph
);
491 } while (RUN_IS_HIDDEN(&pRun
->member
.run
));
492 pCursor
->pRun
= pRun
;
493 if (pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
494 pCursor
->nOffset
= 0;
496 pCursor
->nOffset
= pRun
->member
.run
.strText
->nLen
;
499 if (pCursor
->nOffset
)
500 pCursor
->nOffset
= ME_StrRelPos2(pCursor
->pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
505 if (!(pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
507 int new_ofs
= ME_StrRelPos2(pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
509 if (new_ofs
< pRun
->member
.run
.strText
->nLen
)
511 pCursor
->nOffset
= new_ofs
;
516 pRun
= ME_FindItemFwd(pRun
, diRun
);
517 } while (pRun
&& RUN_IS_HIDDEN(&pRun
->member
.run
));
520 pCursor
->pRun
= pRun
;
521 pCursor
->nOffset
= 0;
530 ME_MoveCursorWords(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
532 ME_DisplayItem
*pRun
= cursor
->pRun
, *pOtherRun
;
533 int nOffset
= cursor
->nOffset
;
537 /* Backward movement */
540 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
541 nOffset
, WB_MOVEWORDLEFT
);
544 pOtherRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
545 if (pOtherRun
->type
== diRun
)
547 if (ME_CallWordBreakProc(editor
, pOtherRun
->member
.run
.strText
,
548 pOtherRun
->member
.run
.strText
->nLen
- 1,
550 && !(pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
551 && !(cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
552 && !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
, 0,
556 nOffset
= pOtherRun
->member
.run
.strText
->nLen
;
558 else if (pOtherRun
->type
== diParagraph
)
560 if (cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
562 /* Paragraph breaks are treated as separate words */
563 if (pOtherRun
->member
.para
.prev_para
->type
== diTextStart
)
565 pRun
= ME_FindItemBack(pOtherRun
, diRunOrParagraph
);
573 /* Forward movement */
574 BOOL last_delim
= FALSE
;
578 if (last_delim
&& !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
579 nOffset
, WB_ISDELIMITER
))
581 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
582 nOffset
, WB_MOVEWORDRIGHT
);
583 if (nOffset
< pRun
->member
.run
.strText
->nLen
)
585 pOtherRun
= ME_FindItemFwd(pRun
, diRunOrParagraphOrEnd
);
586 if (pOtherRun
->type
== diRun
)
588 last_delim
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
589 nOffset
- 1, WB_ISDELIMITER
);
593 else if (pOtherRun
->type
== diParagraph
)
595 if (cursor
->pRun
== pRun
)
596 pRun
= ME_FindItemFwd(pOtherRun
, diRun
);
602 if (cursor
->pRun
== pRun
)
610 cursor
->nOffset
= nOffset
;
616 ME_SelectWord(ME_TextEditor
*editor
)
618 if (!(editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
619 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], -1);
620 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], +1);
621 ME_InvalidateSelection(editor
);
622 ME_SendSelChange(editor
);
626 int ME_GetCursorOfs(ME_TextEditor
*editor
, int nCursor
)
628 ME_Cursor
*pCursor
= &editor
->pCursors
[nCursor
];
630 return ME_GetParagraph(pCursor
->pRun
)->member
.para
.nCharOfs
631 + pCursor
->pRun
->member
.run
.nCharOfs
+ pCursor
->nOffset
;
634 int ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
, ME_Cursor
*result
, BOOL
*is_eol
)
636 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
642 while(p
!= editor
->pBuffer
->pLast
)
644 if (p
->type
== diParagraph
)
646 int ry
= y
- p
->member
.para
.nYPos
;
649 result
->pRun
= ME_FindItemFwd(p
, diRun
);
653 if (ry
>= p
->member
.para
.nHeight
)
655 p
= p
->member
.para
.next_para
;
658 p
= ME_FindItemFwd(p
, diStartRow
);
662 if (p
->type
== diStartRow
)
664 int ry
= y
- p
->member
.row
.nYPos
;
667 if (ry
>= p
->member
.row
.nHeight
)
669 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
670 if (p
->type
!= diStartRow
)
674 p
= ME_FindItemFwd(p
, diRun
);
677 if (p
->type
== diRun
)
680 rx
= x
- p
->member
.run
.pt
.x
;
683 if (rx
>= p
->member
.run
.nWidth
) /* not this run yet... find next item */
688 if (p
->type
== diRun
)
690 rx
= x
- p
->member
.run
.pt
.x
;
691 goto continue_search
;
693 if (p
->type
== diStartRow
)
695 p
= ME_FindItemFwd(p
, diRun
);
698 rx
= 0; /* FIXME not sure */
701 if (p
->type
== diParagraph
|| p
->type
== diTextEnd
)
703 rx
= 0; /* FIXME not sure */
711 if (p
->member
.run
.nFlags
& MERF_ENDPARA
)
714 result
->nOffset
= ME_CharFromPointCursor(editor
, rx
, &p
->member
.run
);
715 if (editor
->pCursors
[0].nOffset
== p
->member
.run
.strText
->nLen
&& rx
)
717 result
->pRun
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, diRun
);
726 result
->pRun
= ME_FindItemBack(p
, diRun
);
728 assert(result
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
734 ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
)
739 GetClientRect(editor
->hWnd
, &rc
);
740 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
)
742 y
+= ME_GetYScrollPos(editor
);
743 ME_FindPixelPos(editor
, x
, y
, &cursor
, NULL
);
744 return (ME_GetParagraph(cursor
.pRun
)->member
.para
.nCharOfs
745 + cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
);
749 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
)
751 ME_Cursor tmp_cursor
;
752 int is_selection
= 0;
754 editor
->nUDArrowX
= -1;
756 y
+= ME_GetYScrollPos(editor
);
758 tmp_cursor
= editor
->pCursors
[0];
759 is_selection
= ME_IsSelection(editor
);
761 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
763 if (GetKeyState(VK_SHIFT
)>=0)
765 editor
->pCursors
[1] = editor
->pCursors
[0];
770 editor
->pCursors
[1] = tmp_cursor
;
774 ME_InvalidateSelection(editor
);
775 HideCaret(editor
->hWnd
);
776 ME_MoveCaret(editor
);
777 ShowCaret(editor
->hWnd
);
778 ME_ClearTempStyle(editor
);
779 ME_SendSelChange(editor
);
782 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
784 ME_Cursor tmp_cursor
;
786 y
+= ME_GetYScrollPos(editor
);
788 tmp_cursor
= editor
->pCursors
[0];
789 /* FIXME: do something with the return value of ME_FindPixelPos */
790 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
792 if (tmp_cursor
.pRun
== editor
->pCursors
[0].pRun
&&
793 tmp_cursor
.nOffset
== editor
->pCursors
[0].nOffset
)
796 ME_InvalidateSelection(editor
);
797 editor
->pCursors
[0] = tmp_cursor
;
798 HideCaret(editor
->hWnd
);
799 ME_MoveCaret(editor
);
800 ME_InvalidateSelection(editor
);
801 ShowCaret(editor
->hWnd
);
802 ME_SendSelChange(editor
);
805 static ME_DisplayItem
*ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
806 int x
, int *pOffset
, int *pbCaretAtEnd
)
808 ME_DisplayItem
*pNext
, *pLastRun
;
809 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
810 assert(pNext
->type
== diRun
);
812 *pbCaretAtEnd
= FALSE
;
814 int run_x
= pNext
->member
.run
.pt
.x
;
815 int width
= pNext
->member
.run
.nWidth
;
818 if (pOffset
) *pOffset
= 0;
821 if (x
>= run_x
&& x
< run_x
+width
)
823 int ch
= ME_CharFromPointCursor(editor
, x
-run_x
, &pNext
->member
.run
);
824 ME_String
*s
= pNext
->member
.run
.strText
;
832 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
833 } while(pNext
&& pNext
->type
== diRun
);
835 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
837 pNext
= ME_FindItemFwd(pNext
, diRun
);
838 if (pbCaretAtEnd
) *pbCaretAtEnd
= 1;
839 if (pOffset
) *pOffset
= 0;
842 if (pbCaretAtEnd
) *pbCaretAtEnd
= 0;
843 if (pOffset
) *pOffset
= 0;
848 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
850 ME_DisplayItem
*pRun
= pCursor
->pRun
;
853 if (editor
->nUDArrowX
!= -1)
854 x
= editor
->nUDArrowX
;
856 if (editor
->bCaretAtEnd
)
858 pRun
= ME_FindItemBack(pRun
, diRun
);
860 x
= pRun
->member
.run
.pt
.x
+ pRun
->member
.run
.nWidth
;
863 x
= pRun
->member
.run
.pt
.x
;
864 x
+= ME_PointFromChar(editor
, &pRun
->member
.run
, pCursor
->nOffset
);
866 editor
->nUDArrowX
= x
;
873 ME_MoveCursorLines(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
875 ME_DisplayItem
*pRun
= pCursor
->pRun
;
876 ME_DisplayItem
*pItem
;
877 int x
= ME_GetXForArrow(editor
, pCursor
);
879 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
880 pRun
= ME_FindItemBack(pRun
, diRun
);
885 /* start of this row */
886 pItem
= ME_FindItemBack(pRun
, diStartRow
);
888 /* start of the previous row */
889 pItem
= ME_FindItemBack(pItem
, diStartRow
);
893 /* start of the next row */
894 pItem
= ME_FindItemFwd(pRun
, diStartRow
);
895 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
900 /* row not found - ignore */
903 pCursor
->pRun
= ME_FindRunInRow(editor
, pItem
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
904 assert(pCursor
->pRun
);
905 assert(pCursor
->pRun
->type
== diRun
);
909 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
911 ME_DisplayItem
*pRun
= pCursor
->pRun
;
912 ME_DisplayItem
*pLast
, *p
;
913 int x
, y
, ys
, yd
, yp
, yprev
;
914 ME_Cursor tmp_curs
= *pCursor
;
916 x
= ME_GetXForArrow(editor
, pCursor
);
917 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
918 pRun
= ME_FindItemBack(pRun
, diRun
);
920 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
921 assert(p
->type
== diStartRow
);
922 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
923 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
924 yd
= y
- editor
->sizeWindow
.cy
;
928 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
931 if (p
->type
== diParagraph
) { /* crossing paragraphs */
932 if (p
->member
.para
.prev_para
== NULL
)
934 yp
= p
->member
.para
.prev_para
->member
.para
.nYPos
;
937 y
= yp
+ p
->member
.row
.nYPos
;
944 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
945 ME_UpdateSelection(editor
, &tmp_curs
);
946 if (yprev
< editor
->sizeWindow
.cy
)
948 ME_EnsureVisible(editor
, ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
));
953 ME_ScrollUp(editor
, ys
-yprev
);
955 assert(pCursor
->pRun
);
956 assert(pCursor
->pRun
->type
== diRun
);
959 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
960 of pixels, even if it makes the scroll bar position exceed its normal maximum.
961 In such a situation, clicking the scrollbar restores its position back to the
962 normal range (ie. sets it to (doclength-screenheight)). */
964 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
966 ME_DisplayItem
*pRun
= pCursor
->pRun
;
967 ME_DisplayItem
*pLast
, *p
;
968 int x
, y
, ys
, yd
, yp
, yprev
;
969 ME_Cursor tmp_curs
= *pCursor
;
971 x
= ME_GetXForArrow(editor
, pCursor
);
972 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
973 pRun
= ME_FindItemBack(pRun
, diRun
);
975 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
976 assert(p
->type
== diStartRow
);
977 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
978 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
979 yd
= y
+ editor
->sizeWindow
.cy
;
983 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
986 if (p
->type
== diParagraph
) {
987 yp
= p
->member
.para
.nYPos
;
990 y
= yp
+ p
->member
.row
.nYPos
;
997 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
998 ME_UpdateSelection(editor
, &tmp_curs
);
999 if (yprev
>= editor
->nTotalLength
-editor
->sizeWindow
.cy
)
1001 ME_EnsureVisible(editor
, ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
));
1006 ME_ScrollUp(editor
,ys
-yprev
);
1008 assert(pCursor
->pRun
);
1009 assert(pCursor
->pRun
->type
== diRun
);
1012 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1014 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
1015 /* bCaretAtEnd doesn't make sense if the cursor isn't set at the
1016 first character of the next row */
1017 assert(!editor
->bCaretAtEnd
|| !pCursor
->nOffset
);
1018 ME_WrapMarkedParagraphs(editor
);
1020 ME_DisplayItem
*pRun
;
1021 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
1022 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1026 pRun
= ME_FindItemFwd(pRow
, diRun
);
1028 pCursor
->pRun
= pRun
;
1029 pCursor
->nOffset
= 0;
1032 editor
->bCaretAtEnd
= FALSE
;
1035 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1037 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diTextStart
);
1039 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1041 pCursor
->pRun
= pRun
;
1042 pCursor
->nOffset
= 0;
1047 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1049 ME_DisplayItem
*pRow
;
1051 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1054 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1056 if (pRow
->type
== diStartRow
) {
1057 /* FIXME WTF was I thinking about here ? */
1058 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1060 pCursor
->pRun
= pRun
;
1061 pCursor
->nOffset
= 0;
1062 editor
->bCaretAtEnd
= 1;
1065 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1066 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1067 pCursor
->nOffset
= 0;
1068 editor
->bCaretAtEnd
= FALSE
;
1071 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1073 ME_DisplayItem
*p
= ME_FindItemFwd(pCursor
->pRun
, diTextEnd
);
1075 p
= ME_FindItemBack(p
, diRun
);
1077 assert(p
->member
.run
.nFlags
& MERF_ENDPARA
);
1079 pCursor
->nOffset
= 0;
1080 editor
->bCaretAtEnd
= FALSE
;
1083 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1085 return memcmp(&editor
->pCursors
[0], &editor
->pCursors
[1], sizeof(ME_Cursor
))!=0;
1088 static int ME_GetSelCursor(ME_TextEditor
*editor
, int dir
)
1090 int cdir
= ME_GetCursorOfs(editor
, 0) - ME_GetCursorOfs(editor
, 1);
1098 BOOL
ME_UpdateSelection(ME_TextEditor
*editor
, ME_Cursor
*pTempCursor
)
1100 ME_Cursor old_anchor
= editor
->pCursors
[1];
1102 if (GetKeyState(VK_SHIFT
)>=0) /* cancelling selection */
1104 /* any selection was present ? if so, it's no more, repaint ! */
1105 editor
->pCursors
[1] = editor
->pCursors
[0];
1106 if (memcmp(pTempCursor
, &old_anchor
, sizeof(ME_Cursor
))) {
1113 if (!memcmp(pTempCursor
, &editor
->pCursors
[1], sizeof(ME_Cursor
))) /* starting selection */
1115 editor
->pCursors
[1] = *pTempCursor
;
1124 void ME_DeleteSelection(ME_TextEditor
*editor
)
1127 ME_GetSelection(editor
, &from
, &to
);
1128 ME_DeleteTextAtCursor(editor
, ME_GetSelCursor(editor
,-1), to
-from
);
1131 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1137 ME_GetSelection(editor
, &from
, &to
);
1138 ME_CursorFromCharOfs(editor
, from
, &c
);
1140 style
= c
.pRun
->member
.run
.style
;
1141 ME_AddRefStyle(style
); /* ME_GetInsertStyle has already done that */
1144 style
= ME_GetInsertStyle(editor
, 0);
1148 void ME_SendSelChange(ME_TextEditor
*editor
)
1152 ME_ClearTempStyle(editor
);
1154 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1157 sc
.nmhdr
.hwndFrom
= editor
->hWnd
;
1158 sc
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
1159 sc
.nmhdr
.code
= EN_SELCHANGE
;
1160 SendMessageW(editor
->hWnd
, EM_EXGETSEL
, 0, (LPARAM
)&sc
.chrg
);
1161 sc
.seltyp
= SEL_EMPTY
;
1162 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1163 sc
.seltyp
|= SEL_TEXT
;
1164 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* wth were RICHEDIT authors thinking ? */
1165 sc
.seltyp
|= SEL_MULTICHAR
;
1166 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
, sc
.nmhdr
.idFrom
, (LPARAM
)&sc
);
1171 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1174 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1175 ME_Cursor tmp_curs
= *p
;
1176 BOOL success
= FALSE
;
1178 ME_CheckCharOffsets(editor
);
1179 editor
->nUDArrowX
= -1;
1182 editor
->bCaretAtEnd
= 0;
1184 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1186 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1189 editor
->bCaretAtEnd
= 0;
1191 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1193 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1196 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1199 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1202 ME_ArrowPageUp(editor
, &tmp_curs
);
1205 ME_ArrowPageDown(editor
, &tmp_curs
);
1209 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1211 ME_ArrowHome(editor
, &tmp_curs
);
1212 editor
->bCaretAtEnd
= 0;
1217 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1219 ME_ArrowEnd(editor
, &tmp_curs
);
1224 editor
->pCursors
[1] = tmp_curs
;
1227 ME_InvalidateSelection(editor
);
1229 HideCaret(editor
->hWnd
);
1230 ME_EnsureVisible(editor
, tmp_curs
.pRun
);
1231 ME_ShowCaret(editor
);
1232 ME_SendSelChange(editor
);