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
) && (how
->flags
& GTL_USECRLF
))
61 length
+= editor
->nParagraphs
- 1;
63 if (how
->flags
& GTL_NUMBYTES
)
67 if (how
->codepage
== 1200)
69 if (how
->flags
& GTL_PRECISE
)
70 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
71 if (GetCPInfo(how
->codepage
, &cpinfo
))
72 return length
* cpinfo
.MaxCharSize
;
73 ERR("Invalid codepage %u\n", how
->codepage
);
80 int ME_SetSelection(ME_TextEditor
*editor
, int from
, int to
)
83 const int len
= ME_GetTextLength(editor
);
85 /* all negative values are effectively the same */
92 if (from
== 0 && to
== -1)
94 editor
->pCursors
[1].pRun
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
);
95 editor
->pCursors
[1].nOffset
= 0;
96 editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
97 editor
->pCursors
[0].nOffset
= 0;
98 ME_InvalidateSelection(editor
);
99 ME_ClearTempStyle(editor
);
103 /* if both values are equal and also out of bound, that means to */
104 /* put the selection at the end of the text */
105 if ((from
== to
) && (to
< 0 || to
> len
))
111 /* if from is negative and to is positive then selection is */
112 /* deselected and caret moved to end of the current selection */
116 ME_GetSelection(editor
, &start
, &end
);
117 editor
->pCursors
[1] = editor
->pCursors
[0];
119 ME_ClearTempStyle(editor
);
123 /* adjust to if it's a negative value */
127 /* flip from and to if they are reversed */
135 /* after fiddling with the values, we find from > len && to > len */
138 /* special case with to too big */
145 editor
->pCursors
[1].pRun
= editor
->pCursors
[0].pRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
146 editor
->pCursors
[1].nOffset
= editor
->pCursors
[0].nOffset
= 0;
147 ME_InvalidateSelection(editor
);
148 ME_ClearTempStyle(editor
);
152 ME_RunOfsFromCharOfs(editor
, from
, &editor
->pCursors
[1].pRun
, &editor
->pCursors
[1].nOffset
);
153 ME_RunOfsFromCharOfs(editor
, to
, &editor
->pCursors
[0].pRun
, &editor
->pCursors
[0].nOffset
);
159 ME_GetCursorCoordinates(ME_TextEditor
*editor
, ME_Cursor
*pCursor
,
160 int *x
, int *y
, int *height
)
162 ME_DisplayItem
*pCursorRun
= pCursor
->pRun
;
163 ME_DisplayItem
*pSizeRun
= pCursor
->pRun
;
165 assert(!pCursor
->nOffset
|| !editor
->bCaretAtEnd
);
166 assert(height
&& x
&& y
);
167 assert(!(ME_GetParagraph(pCursorRun
)->member
.para
.nFlags
& MEPF_REWRAP
));
168 assert(pCursor
->pRun
);
169 assert(pCursor
->pRun
->type
== diRun
);
171 if (pCursorRun
->type
== diRun
) {
172 ME_DisplayItem
*row
= ME_FindItemBack(pCursorRun
, diStartRowOrParagraph
);
175 HDC hDC
= GetDC(editor
->hWnd
);
177 ME_DisplayItem
*run
= pCursorRun
;
178 ME_DisplayItem
*para
= NULL
;
181 ME_InitContext(&c
, editor
, hDC
);
183 if (!pCursor
->nOffset
&& !editor
->bCaretAtEnd
)
185 ME_DisplayItem
*prev
= ME_FindItemBack(pCursorRun
, diRunOrStartRow
);
187 if (prev
->type
== diRun
)
190 assert(row
->type
== diStartRow
); /* paragraph -> run without start row ?*/
191 para
= ME_FindItemBack(row
, diParagraph
);
193 assert(para
->type
== diParagraph
);
194 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
&&
195 run
== ME_FindItemFwd(row
, diRun
))
197 ME_DisplayItem
*tmp
= ME_FindItemBack(row
, diRunOrParagraph
);
199 if (tmp
->type
== diRun
)
201 row
= ME_FindItemBack(tmp
, diStartRow
);
202 pSizeRun
= run
= tmp
;
204 assert(run
->type
== diRun
);
205 sz
= ME_GetRunSize(&c
, ¶
->member
.para
, &run
->member
.run
, ME_StrLen(run
->member
.run
.strText
));
208 if (pCursor
->nOffset
&& !(run
->member
.run
.nFlags
& MERF_SKIPPED
)) {
209 sz
= ME_GetRunSize(&c
, ¶
->member
.para
, &run
->member
.run
, pCursor
->nOffset
);
212 *height
= pSizeRun
->member
.run
.nAscent
+ pSizeRun
->member
.run
.nDescent
;
213 *x
= run
->member
.run
.pt
.x
+ sz
.cx
;
214 *y
= para
->member
.para
.nYPos
+ row
->member
.row
.nBaseline
+ pSizeRun
->member
.run
.pt
.y
- pSizeRun
->member
.run
.nAscent
- ME_GetYScrollPos(editor
);
216 ME_DestroyContext(&c
);
217 ReleaseDC(editor
->hWnd
, hDC
);
221 *height
= 10; /* FIXME use global font */
228 ME_MoveCaret(ME_TextEditor
*editor
)
232 if (ME_WrapMarkedParagraphs(editor
))
233 ME_UpdateScrollBar(editor
);
234 ME_GetCursorCoordinates(editor
, &editor
->pCursors
[0], &x
, &y
, &height
);
235 if(editor
->bHaveFocus
)
237 CreateCaret(editor
->hWnd
, NULL
, 0, height
);
243 void ME_ShowCaret(ME_TextEditor
*ed
)
250 void ME_HideCaret(ME_TextEditor
*ed
)
259 void ME_InternalDeleteText(ME_TextEditor
*editor
, int nOfs
,
268 ME_CursorFromCharOfs(editor
, nOfs
, &c
);
269 run
= &c
.pRun
->member
.run
;
270 if (run
->nFlags
& MERF_ENDPARA
) {
271 if (!ME_FindItemFwd(c
.pRun
, diParagraph
))
275 ME_JoinParagraphs(editor
, ME_GetParagraph(c
.pRun
));
276 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
277 ME_CheckCharOffsets(editor
);
279 if (editor
->bEmulateVersion10
&& nChars
)
286 int nIntendedChars
= nChars
;
287 int nCharsToDelete
= nChars
;
291 ME_FindItemBack(c
.pRun
, diParagraph
)->member
.para
.nFlags
|= MEPF_REWRAP
;
294 ME_StrRelPos(run
->strText
, loc
, &nChars
);
295 /* nChars is the number of characters that should be deleted from the
296 FOLLOWING runs (these AFTER cursor.pRun)
297 nCharsToDelete is a number of chars to delete from THIS run */
298 nCharsToDelete
-= nChars
;
299 shift
-= nCharsToDelete
;
300 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
301 nCharsToDelete
, nIntendedChars
, nChars
, c
.nOffset
,
302 debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
304 if (!c
.nOffset
&& ME_StrVLen(run
->strText
) == nCharsToDelete
)
306 /* undo = reinsert whole run */
307 /* nOfs is a character offset (from the start of the document
308 to the current (deleted) run */
309 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
311 pUndo
->di
.member
.run
.nCharOfs
= nOfs
;
315 /* undo = reinsert partial run */
316 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
318 ME_DestroyString(pUndo
->di
.member
.run
.strText
);
319 pUndo
->di
.member
.run
.nCharOfs
= nOfs
;
320 pUndo
->di
.member
.run
.strText
= ME_MakeStringN(run
->strText
->szData
+c
.nOffset
, nCharsToDelete
);
323 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
324 TRACE("Shift value: %d\n", shift
);
325 ME_StrDeleteV(run
->strText
, c
.nOffset
, nCharsToDelete
);
327 /* update cursors (including c) */
328 for (i
=-1; i
<editor
->nCursors
; i
++) {
329 ME_Cursor
*pThisCur
= editor
->pCursors
+ i
;
330 if (i
== -1) pThisCur
= &c
;
331 if (pThisCur
->pRun
== cursor
.pRun
) {
332 if (pThisCur
->nOffset
> cursor
.nOffset
) {
333 if (pThisCur
->nOffset
-cursor
.nOffset
< nCharsToDelete
)
334 pThisCur
->nOffset
= cursor
.nOffset
;
336 pThisCur
->nOffset
-= nCharsToDelete
;
337 assert(pThisCur
->nOffset
>= 0);
338 assert(pThisCur
->nOffset
<= ME_StrVLen(run
->strText
));
340 if (pThisCur
->nOffset
== ME_StrVLen(run
->strText
))
342 pThisCur
->pRun
= ME_FindItemFwd(pThisCur
->pRun
, diRunOrParagraphOrEnd
);
343 assert(pThisCur
->pRun
->type
== diRun
);
344 pThisCur
->nOffset
= 0;
349 /* c = updated data now */
351 if (c
.pRun
== cursor
.pRun
)
352 ME_SkipAndPropagateCharOffset(c
.pRun
, shift
);
354 ME_PropagateCharOffset(c
.pRun
, shift
);
356 if (!ME_StrVLen(cursor
.pRun
->member
.run
.strText
))
358 TRACE("Removing useless run\n");
359 ME_Remove(cursor
.pRun
);
360 ME_DestroyDisplayItem(cursor
.pRun
);
365 ME_CheckCharOffsets(editor);
372 void ME_DeleteTextAtCursor(ME_TextEditor
*editor
, int nCursor
,
375 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
376 /* text operations set modified state */
377 editor
->nModifyStep
= 1;
378 ME_InternalDeleteText(editor
, ME_GetCursorOfs(editor
, nCursor
), nChars
);
381 static ME_DisplayItem
*
382 ME_InternalInsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
383 const WCHAR
*str
, int len
, ME_Style
*style
,
386 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
388 editor
->bCaretAtEnd
= FALSE
;
390 assert(p
->pRun
->type
== diRun
);
392 return ME_InsertRunAtCursor(editor
, p
, style
, str
, len
, flags
);
396 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
397 void ME_InsertGraphicsFromCursor(ME_TextEditor
*editor
, int nCursor
)
399 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
403 if (ME_IsSelection(editor
))
404 ME_DeleteSelection(editor
);
406 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
408 ME_SendSelChange(editor
);
413 ME_InsertTableCellFromCursor(ME_TextEditor
*editor
, int nCursor
)
416 ME_DisplayItem
*p
, *run
;
417 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
419 p
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, pStyle
,
422 while ((run
= ME_FindItemBack(run
, diRunOrParagraph
))->type
== diRun
)
424 if (run
->member
.run
.nFlags
& MERF_CELL
)
426 assert(run
->member
.run
.pCell
->next
);
427 p
->member
.run
.pCell
= run
->member
.run
.pCell
->next
;
431 assert(run
->type
== diParagraph
);
432 assert(run
->member
.para
.bTable
);
433 assert(run
->member
.para
.pCells
);
434 p
->member
.run
.pCell
= run
->member
.para
.pCells
;
438 void ME_InsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
439 const WCHAR
*str
, int len
, ME_Style
*style
)
445 /* FIXME really HERE ? */
446 if (ME_IsSelection(editor
))
447 ME_DeleteSelection(editor
);
449 /* FIXME: is this too slow? */
450 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
451 oldLen
= ME_GetTextLength(editor
);
453 /* text operations set modified state */
454 editor
->nModifyStep
= 1;
458 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
462 /* grow the text limit to fit our text */
463 if(editor
->nTextLimit
< oldLen
+len
)
464 editor
->nTextLimit
= oldLen
+ len
;
469 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
470 while(pos
-str
< len
&& *pos
!= '\r' && *pos
!= '\n' && *pos
!= '\t')
472 if (pos
-str
< len
&& *pos
== '\t') { /* handle tabs */
476 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
478 ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, style
, MERF_TAB
);
487 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
488 if (!editor
->bEmulateVersion10
&& pos
-str
< len
-2 && pos
[0] == '\r' && pos
[1] == '\r' && pos
[2] == '\n') {
492 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
494 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, style
, 0);
503 if (pos
-str
< len
) { /* handle EOLs */
504 ME_DisplayItem
*tp
, *end_run
;
507 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
508 p
= &editor
->pCursors
[nCursor
];
510 ME_SplitRunSimple(editor
, p
->pRun
, p
->nOffset
);
511 p
= &editor
->pCursors
[nCursor
];
513 tmp_style
= ME_GetInsertStyle(editor
, nCursor
);
514 /* ME_SplitParagraph increases style refcount */
515 tp
= ME_SplitParagraph(editor
, p
->pRun
, p
->pRun
->member
.run
.style
);
516 p
->pRun
= ME_FindItemFwd(tp
, diRun
);
517 end_run
= ME_FindItemBack(tp
, diRun
);
518 ME_ReleaseStyle(end_run
->member
.run
.style
);
519 end_run
->member
.run
.style
= tmp_style
;
521 if(pos
-str
< len
&& *pos
=='\r')
523 if(pos
-str
< len
&& *pos
=='\n')
531 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, len
, style
, 0);
538 ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
540 ME_DisplayItem
*pRun
= pCursor
->pRun
;
544 if (!pCursor
->nOffset
)
547 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
554 if (pRun
->member
.para
.prev_para
->type
== diTextStart
)
556 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
557 /* every paragraph ought to have at least one run */
558 assert(pRun
&& pRun
->type
== diRun
);
559 assert(pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
562 assert(pRun
->type
!= diRun
&& pRun
->type
!= diParagraph
);
565 } while (RUN_IS_HIDDEN(&pRun
->member
.run
));
566 pCursor
->pRun
= pRun
;
567 if (pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
568 pCursor
->nOffset
= 0;
570 pCursor
->nOffset
= pRun
->member
.run
.strText
->nLen
;
573 if (pCursor
->nOffset
)
574 pCursor
->nOffset
= ME_StrRelPos2(pCursor
->pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
579 if (!(pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
581 int new_ofs
= ME_StrRelPos2(pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
583 if (new_ofs
< pRun
->member
.run
.strText
->nLen
)
585 pCursor
->nOffset
= new_ofs
;
590 pRun
= ME_FindItemFwd(pRun
, diRun
);
591 } while (pRun
&& RUN_IS_HIDDEN(&pRun
->member
.run
));
594 pCursor
->pRun
= pRun
;
595 pCursor
->nOffset
= 0;
604 ME_MoveCursorWords(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
606 ME_DisplayItem
*pRun
= cursor
->pRun
, *pOtherRun
;
607 int nOffset
= cursor
->nOffset
;
611 /* Backward movement */
614 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
615 nOffset
, WB_MOVEWORDLEFT
);
618 pOtherRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
619 if (pOtherRun
->type
== diRun
)
621 if (ME_CallWordBreakProc(editor
, pOtherRun
->member
.run
.strText
,
622 pOtherRun
->member
.run
.strText
->nLen
- 1,
624 && !(pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
625 && !(cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
626 && !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
, 0,
630 nOffset
= pOtherRun
->member
.run
.strText
->nLen
;
632 else if (pOtherRun
->type
== diParagraph
)
634 if (cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
636 /* Paragraph breaks are treated as separate words */
637 if (pOtherRun
->member
.para
.prev_para
->type
== diTextStart
)
639 pRun
= ME_FindItemBack(pOtherRun
, diRunOrParagraph
);
647 /* Forward movement */
648 BOOL last_delim
= FALSE
;
652 if (last_delim
&& !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
653 nOffset
, WB_ISDELIMITER
))
655 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
656 nOffset
, WB_MOVEWORDRIGHT
);
657 if (nOffset
< pRun
->member
.run
.strText
->nLen
)
659 pOtherRun
= ME_FindItemFwd(pRun
, diRunOrParagraphOrEnd
);
660 if (pOtherRun
->type
== diRun
)
662 last_delim
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
663 nOffset
- 1, WB_ISDELIMITER
);
667 else if (pOtherRun
->type
== diParagraph
)
669 if (cursor
->pRun
== pRun
)
670 pRun
= ME_FindItemFwd(pOtherRun
, diRun
);
676 if (cursor
->pRun
== pRun
)
684 cursor
->nOffset
= nOffset
;
690 ME_SelectWord(ME_TextEditor
*editor
)
692 if (!(editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
693 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], -1);
694 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], +1);
695 ME_InvalidateSelection(editor
);
696 ME_SendSelChange(editor
);
700 int ME_GetCursorOfs(ME_TextEditor
*editor
, int nCursor
)
702 ME_Cursor
*pCursor
= &editor
->pCursors
[nCursor
];
703 return ME_GetParagraph(pCursor
->pRun
)->member
.para
.nCharOfs
704 + pCursor
->pRun
->member
.run
.nCharOfs
+ pCursor
->nOffset
;
707 static void ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
, ME_Cursor
*result
, BOOL
*is_eol
)
709 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
710 ME_DisplayItem
*last
= NULL
;
717 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->member
.para
.next_para
)
719 assert(p
->type
== diParagraph
);
720 if (y
< p
->member
.para
.nYPos
+ p
->member
.para
.nHeight
)
722 y
-= p
->member
.para
.nYPos
;
723 p
= ME_FindItemFwd(p
, diStartRow
);
728 for (; p
!= editor
->pBuffer
->pLast
; )
731 assert(p
->type
== diStartRow
);
732 if (y
< p
->member
.row
.nYPos
+ p
->member
.row
.nHeight
)
734 p
= ME_FindItemFwd(p
, diRun
);
737 pp
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
738 if (pp
->type
!= diStartRow
)
740 p
= ME_FindItemFwd(p
, diRun
);
745 for (; p
!= editor
->pBuffer
->pLast
; p
= p
->next
)
750 rx
= x
- p
->member
.run
.pt
.x
;
751 if (rx
< p
->member
.run
.nWidth
)
754 assert(p
->type
== diRun
);
755 if ((p
->member
.run
.nFlags
& MERF_ENDPARA
) || rx
< 0)
758 result
->nOffset
= ME_CharFromPointCursor(editor
, rx
, &p
->member
.run
);
759 if (editor
->pCursors
[0].nOffset
== p
->member
.run
.strText
->nLen
&& rx
)
761 result
->pRun
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, diRun
);
768 p
= ME_FindItemFwd(p
, diRun
);
769 if (is_eol
) *is_eol
= 1;
770 rx
= 0; /* FIXME not sure */
774 rx
= 0; /* FIXME not sure */
781 result
->pRun
= ME_FindItemBack(p
, diRun
);
783 assert(result
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
788 ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
)
793 GetClientRect(editor
->hWnd
, &rc
);
794 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
)
796 y
+= ME_GetYScrollPos(editor
);
797 ME_FindPixelPos(editor
, x
, y
, &cursor
, NULL
);
798 return (ME_GetParagraph(cursor
.pRun
)->member
.para
.nCharOfs
799 + cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
);
803 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
)
805 ME_Cursor tmp_cursor
;
806 int is_selection
= 0;
808 editor
->nUDArrowX
= -1;
810 y
+= ME_GetYScrollPos(editor
);
812 tmp_cursor
= editor
->pCursors
[0];
813 is_selection
= ME_IsSelection(editor
);
815 if (x
>= editor
->selofs
)
817 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
818 if (GetKeyState(VK_SHIFT
)>=0)
820 editor
->pCursors
[1] = editor
->pCursors
[0];
822 else if (!is_selection
) {
823 editor
->pCursors
[1] = tmp_cursor
;
827 ME_InvalidateSelection(editor
);
828 HideCaret(editor
->hWnd
);
829 ME_MoveCaret(editor
);
830 ShowCaret(editor
->hWnd
);
831 ME_ClearTempStyle(editor
);
832 ME_SendSelChange(editor
);
836 ME_DisplayItem
*pRow
;
840 /* Set pCursors[0] to beginning of line */
841 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[1], &editor
->bCaretAtEnd
);
842 /* Set pCursors[1] to end of line */
843 pRow
= ME_FindItemFwd(editor
->pCursors
[1].pRun
, diStartRowOrParagraphOrEnd
);
845 /* pCursor[0] is the position where the cursor will be drawn,
846 * pCursor[1] is the other end of the selection range
847 * pCursor[2] and [3] are backups of [0] and [1] so I
848 * don't have to look them up again
851 if (pRow
->type
== diStartRow
) {
852 /* FIXME WTF was I thinking about here ? */
853 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
855 editor
->pCursors
[0].pRun
= pRun
;
856 editor
->pCursors
[0].nOffset
= 0;
857 editor
->bCaretAtEnd
= 1;
859 editor
->pCursors
[0].pRun
= ME_FindItemBack(pRow
, diRun
);
860 assert(editor
->pCursors
[0].pRun
&& editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
861 editor
->pCursors
[0].nOffset
= 0;
862 editor
->bCaretAtEnd
= 0;
864 editor
->pCursors
[2] = editor
->pCursors
[0];
865 editor
->pCursors
[3] = editor
->pCursors
[1];
866 ME_InvalidateSelection(editor
);
867 HideCaret(editor
->hWnd
);
868 ME_MoveCaret(editor
);
869 ShowCaret(editor
->hWnd
);
870 ME_ClearTempStyle(editor
);
871 ME_SendSelChange(editor
);
875 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
877 ME_Cursor tmp_cursor
;
879 y
+= ME_GetYScrollPos(editor
);
881 tmp_cursor
= editor
->pCursors
[0];
882 /* FIXME: do something with the return value of ME_FindPixelPos */
883 if (!editor
->linesel
)
884 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
885 else ME_FindPixelPos(editor
, (y
> editor
->sely
) * editor
->rcFormat
.right
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
887 if (!memcmp(&tmp_cursor
, editor
->pCursors
, sizeof(tmp_cursor
)))
890 ME_InvalidateSelection(editor
);
891 if (!editor
->linesel
)
892 editor
->pCursors
[0] = tmp_cursor
;
893 else if (!memcmp(&tmp_cursor
, editor
->pCursors
+2, sizeof(tmp_cursor
)) ||
894 !memcmp(&tmp_cursor
, editor
->pCursors
+3, sizeof(tmp_cursor
)))
896 editor
->pCursors
[0] = editor
->pCursors
[2];
897 editor
->pCursors
[1] = editor
->pCursors
[3];
899 else if (y
< editor
->sely
)
901 editor
->pCursors
[0] = tmp_cursor
;
902 editor
->pCursors
[1] = editor
->pCursors
[2];
906 editor
->pCursors
[0] = tmp_cursor
;
907 editor
->pCursors
[1] = editor
->pCursors
[3];
910 HideCaret(editor
->hWnd
);
911 ME_MoveCaret(editor
);
912 ME_InvalidateSelection(editor
);
913 ShowCaret(editor
->hWnd
);
914 ME_SendSelChange(editor
);
917 static ME_DisplayItem
*ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
918 int x
, int *pOffset
, int *pbCaretAtEnd
)
920 ME_DisplayItem
*pNext
, *pLastRun
;
921 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
922 assert(pNext
->type
== diRun
);
924 *pbCaretAtEnd
= FALSE
;
926 int run_x
= pNext
->member
.run
.pt
.x
;
927 int width
= pNext
->member
.run
.nWidth
;
930 if (pOffset
) *pOffset
= 0;
933 if (x
>= run_x
&& x
< run_x
+width
)
935 int ch
= ME_CharFromPointCursor(editor
, x
-run_x
, &pNext
->member
.run
);
936 ME_String
*s
= pNext
->member
.run
.strText
;
944 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
945 } while(pNext
&& pNext
->type
== diRun
);
947 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
949 pNext
= ME_FindItemFwd(pNext
, diRun
);
950 if (pbCaretAtEnd
) *pbCaretAtEnd
= 1;
951 if (pOffset
) *pOffset
= 0;
954 if (pbCaretAtEnd
) *pbCaretAtEnd
= 0;
955 if (pOffset
) *pOffset
= 0;
960 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
962 ME_DisplayItem
*pRun
= pCursor
->pRun
;
965 if (editor
->nUDArrowX
!= -1)
966 x
= editor
->nUDArrowX
;
968 if (editor
->bCaretAtEnd
)
970 pRun
= ME_FindItemBack(pRun
, diRun
);
972 x
= pRun
->member
.run
.pt
.x
+ pRun
->member
.run
.nWidth
;
975 x
= pRun
->member
.run
.pt
.x
;
976 x
+= ME_PointFromChar(editor
, &pRun
->member
.run
, pCursor
->nOffset
);
978 editor
->nUDArrowX
= x
;
985 ME_MoveCursorLines(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
987 ME_DisplayItem
*pRun
= pCursor
->pRun
;
988 ME_DisplayItem
*pItem
;
989 int x
= ME_GetXForArrow(editor
, pCursor
);
991 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
992 pRun
= ME_FindItemBack(pRun
, diRun
);
997 /* start of this row */
998 pItem
= ME_FindItemBack(pRun
, diStartRow
);
1000 /* start of the previous row */
1001 pItem
= ME_FindItemBack(pItem
, diStartRow
);
1005 /* start of the next row */
1006 pItem
= ME_FindItemFwd(pRun
, diStartRow
);
1007 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1012 /* row not found - ignore */
1015 pCursor
->pRun
= ME_FindRunInRow(editor
, pItem
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1016 assert(pCursor
->pRun
);
1017 assert(pCursor
->pRun
->type
== diRun
);
1021 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1023 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1024 ME_DisplayItem
*pLast
, *p
;
1025 int x
, y
, ys
, yd
, yp
, yprev
;
1026 ME_Cursor tmp_curs
= *pCursor
;
1028 x
= ME_GetXForArrow(editor
, pCursor
);
1029 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1030 pRun
= ME_FindItemBack(pRun
, diRun
);
1032 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1033 assert(p
->type
== diStartRow
);
1034 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
1035 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
1036 yd
= y
- editor
->sizeWindow
.cy
;
1040 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
1043 if (p
->type
== diParagraph
) { /* crossing paragraphs */
1044 if (p
->member
.para
.prev_para
== NULL
)
1046 yp
= p
->member
.para
.prev_para
->member
.para
.nYPos
;
1049 y
= yp
+ p
->member
.row
.nYPos
;
1056 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1057 ME_UpdateSelection(editor
, &tmp_curs
);
1058 if (yprev
< editor
->sizeWindow
.cy
)
1060 ME_EnsureVisible(editor
, ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
));
1065 ME_ScrollUp(editor
, ys
-yprev
);
1067 assert(pCursor
->pRun
);
1068 assert(pCursor
->pRun
->type
== diRun
);
1071 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1072 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1073 In such a situation, clicking the scrollbar restores its position back to the
1074 normal range (ie. sets it to (doclength-screenheight)). */
1076 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1078 ME_DisplayItem
*pRun
= pCursor
->pRun
;
1079 ME_DisplayItem
*pLast
, *p
;
1080 int x
, y
, ys
, yd
, yp
, yprev
;
1081 ME_Cursor tmp_curs
= *pCursor
;
1083 x
= ME_GetXForArrow(editor
, pCursor
);
1084 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
1085 pRun
= ME_FindItemBack(pRun
, diRun
);
1087 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
1088 assert(p
->type
== diStartRow
);
1089 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
1090 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
1091 yd
= y
+ editor
->sizeWindow
.cy
;
1095 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
1098 if (p
->type
== diParagraph
) {
1099 yp
= p
->member
.para
.nYPos
;
1102 y
= yp
+ p
->member
.row
.nYPos
;
1109 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
1110 ME_UpdateSelection(editor
, &tmp_curs
);
1111 if (yprev
>= editor
->nTotalLength
-editor
->sizeWindow
.cy
)
1113 ME_EnsureVisible(editor
, ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
));
1118 ME_ScrollUp(editor
,ys
-yprev
);
1120 assert(pCursor
->pRun
);
1121 assert(pCursor
->pRun
->type
== diRun
);
1124 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1126 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
1127 /* bCaretAtEnd doesn't make sense if the cursor isn't set at the
1128 first character of the next row */
1129 assert(!editor
->bCaretAtEnd
|| !pCursor
->nOffset
);
1130 ME_WrapMarkedParagraphs(editor
);
1132 ME_DisplayItem
*pRun
;
1133 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
1134 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1138 pRun
= ME_FindItemFwd(pRow
, diRun
);
1140 pCursor
->pRun
= pRun
;
1141 pCursor
->nOffset
= 0;
1144 editor
->bCaretAtEnd
= FALSE
;
1147 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1149 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diTextStart
);
1151 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1153 pCursor
->pRun
= pRun
;
1154 pCursor
->nOffset
= 0;
1159 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1161 ME_DisplayItem
*pRow
;
1163 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1166 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1168 if (pRow
->type
== diStartRow
) {
1169 /* FIXME WTF was I thinking about here ? */
1170 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1172 pCursor
->pRun
= pRun
;
1173 pCursor
->nOffset
= 0;
1174 editor
->bCaretAtEnd
= 1;
1177 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1178 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1179 pCursor
->nOffset
= 0;
1180 editor
->bCaretAtEnd
= FALSE
;
1183 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1185 ME_DisplayItem
*p
= ME_FindItemFwd(pCursor
->pRun
, diTextEnd
);
1187 p
= ME_FindItemBack(p
, diRun
);
1189 assert(p
->member
.run
.nFlags
& MERF_ENDPARA
);
1191 pCursor
->nOffset
= 0;
1192 editor
->bCaretAtEnd
= FALSE
;
1195 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1197 return memcmp(&editor
->pCursors
[0], &editor
->pCursors
[1], sizeof(ME_Cursor
))!=0;
1200 static int ME_GetSelCursor(ME_TextEditor
*editor
, int dir
)
1202 int cdir
= ME_GetCursorOfs(editor
, 0) - ME_GetCursorOfs(editor
, 1);
1210 BOOL
ME_UpdateSelection(ME_TextEditor
*editor
, const ME_Cursor
*pTempCursor
)
1212 ME_Cursor old_anchor
= editor
->pCursors
[1];
1214 if (GetKeyState(VK_SHIFT
)>=0) /* cancelling selection */
1216 /* any selection was present ? if so, it's no more, repaint ! */
1217 editor
->pCursors
[1] = editor
->pCursors
[0];
1218 if (memcmp(pTempCursor
, &old_anchor
, sizeof(ME_Cursor
))) {
1225 if (!memcmp(pTempCursor
, &editor
->pCursors
[1], sizeof(ME_Cursor
))) /* starting selection */
1227 editor
->pCursors
[1] = *pTempCursor
;
1236 void ME_DeleteSelection(ME_TextEditor
*editor
)
1239 ME_GetSelection(editor
, &from
, &to
);
1240 ME_DeleteTextAtCursor(editor
, ME_GetSelCursor(editor
,-1), to
-from
);
1243 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1248 ME_GetSelection(editor
, &from
, &to
);
1251 ME_CursorFromCharOfs(editor
, from
, &c
);
1252 style
= c
.pRun
->member
.run
.style
;
1253 ME_AddRefStyle(style
); /* ME_GetInsertStyle has already done that */
1256 style
= ME_GetInsertStyle(editor
, 0);
1260 void ME_SendSelChange(ME_TextEditor
*editor
)
1264 ME_ClearTempStyle(editor
);
1266 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1269 sc
.nmhdr
.hwndFrom
= editor
->hWnd
;
1270 sc
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
1271 sc
.nmhdr
.code
= EN_SELCHANGE
;
1272 SendMessageW(editor
->hWnd
, EM_EXGETSEL
, 0, (LPARAM
)&sc
.chrg
);
1273 sc
.seltyp
= SEL_EMPTY
;
1274 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1275 sc
.seltyp
|= SEL_TEXT
;
1276 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* wth were RICHEDIT authors thinking ? */
1277 sc
.seltyp
|= SEL_MULTICHAR
;
1278 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
, sc
.nmhdr
.idFrom
, (LPARAM
)&sc
);
1282 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1285 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1286 ME_Cursor tmp_curs
= *p
;
1287 BOOL success
= FALSE
;
1289 ME_CheckCharOffsets(editor
);
1290 editor
->nUDArrowX
= -1;
1293 editor
->bCaretAtEnd
= 0;
1295 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1297 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1300 editor
->bCaretAtEnd
= 0;
1302 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1304 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1307 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1310 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1313 ME_ArrowPageUp(editor
, &tmp_curs
);
1316 ME_ArrowPageDown(editor
, &tmp_curs
);
1320 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1322 ME_ArrowHome(editor
, &tmp_curs
);
1323 editor
->bCaretAtEnd
= 0;
1328 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1330 ME_ArrowEnd(editor
, &tmp_curs
);
1335 editor
->pCursors
[1] = tmp_curs
;
1338 ME_InvalidateSelection(editor
);
1340 HideCaret(editor
->hWnd
);
1341 ME_EnsureVisible(editor
, tmp_curs
.pRun
);
1342 ME_ShowCaret(editor
);
1343 ME_SendSelChange(editor
);