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
);
95 editor
->pCursors
[1] = editor
->pCursors
[0];
97 ME_ClearTempStyle(editor
);
106 ME_RunOfsFromCharOfs(editor
, from
, &editor
->pCursors
[1].pRun
, &editor
->pCursors
[1].nOffset
);
107 ME_RunOfsFromCharOfs(editor
, to
, &editor
->pCursors
[0].pRun
, &editor
->pCursors
[0].nOffset
);
112 ME_GetCursorCoordinates(ME_TextEditor
*editor
, ME_Cursor
*pCursor
,
113 int *x
, int *y
, int *height
)
115 ME_DisplayItem
*pCursorRun
= pCursor
->pRun
;
116 ME_DisplayItem
*pSizeRun
= pCursor
->pRun
;
118 assert(!pCursor
->nOffset
|| !editor
->bCaretAtEnd
);
119 assert(height
&& x
&& y
);
120 assert(!(ME_GetParagraph(pCursorRun
)->member
.para
.nFlags
& MEPF_REWRAP
));
121 assert(pCursor
->pRun
);
122 assert(pCursor
->pRun
->type
== diRun
);
124 if (pCursorRun
->type
== diRun
) {
125 ME_DisplayItem
*row
= ME_FindItemBack(pCursorRun
, diStartRowOrParagraph
);
128 HDC hDC
= GetDC(editor
->hWnd
);
130 ME_DisplayItem
*run
= pCursorRun
;
131 ME_DisplayItem
*para
= NULL
;
134 ME_InitContext(&c
, editor
, hDC
);
136 if (!pCursor
->nOffset
&& !editor
->bCaretAtEnd
)
138 ME_DisplayItem
*prev
= ME_FindItemBack(pCursorRun
, diRunOrStartRow
);
140 if (prev
->type
== diRun
)
143 assert(row
->type
== diStartRow
); /* paragraph -> run without start row ?*/
144 para
= ME_FindItemBack(row
, diParagraph
);
146 assert(para
->type
== diParagraph
);
147 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
&&
148 run
== ME_FindItemFwd(row
, diRun
))
150 ME_DisplayItem
*tmp
= ME_FindItemBack(row
, diRunOrParagraph
);
152 if (tmp
->type
== diRun
)
154 row
= ME_FindItemBack(tmp
, diStartRow
);
155 pSizeRun
= run
= tmp
;
157 assert(run
->type
== diRun
);
158 sz
= ME_GetRunSize(&c
, ¶
->member
.para
, &run
->member
.run
, ME_StrLen(run
->member
.run
.strText
));
161 if (pCursor
->nOffset
&& !(run
->member
.run
.nFlags
& MERF_SKIPPED
)) {
162 sz
= ME_GetRunSize(&c
, ¶
->member
.para
, &run
->member
.run
, pCursor
->nOffset
);
165 *height
= pSizeRun
->member
.run
.nAscent
+ pSizeRun
->member
.run
.nDescent
;
166 *x
= run
->member
.run
.pt
.x
+ sz
.cx
;
167 *y
= para
->member
.para
.nYPos
+ row
->member
.row
.nBaseline
+ pSizeRun
->member
.run
.pt
.y
- pSizeRun
->member
.run
.nAscent
- ME_GetYScrollPos(editor
);
169 ME_DestroyContext(&c
);
170 ReleaseDC(editor
->hWnd
, hDC
);
174 *height
= 10; /* FIXME use global font */
181 ME_MoveCaret(ME_TextEditor
*editor
)
185 ME_WrapMarkedParagraphs(editor
);
186 ME_GetCursorCoordinates(editor
, &editor
->pCursors
[0], &x
, &y
, &height
);
187 CreateCaret(editor
->hWnd
, NULL
, 0, height
);
192 void ME_ShowCaret(ME_TextEditor
*ed
)
198 void ME_HideCaret(ME_TextEditor
*ed
)
204 void ME_InternalDeleteText(ME_TextEditor
*editor
, int nOfs
,
213 ME_CursorFromCharOfs(editor
, nOfs
, &c
);
214 run
= &c
.pRun
->member
.run
;
215 if (run
->nFlags
& MERF_ENDPARA
) {
216 if (!ME_FindItemFwd(c
.pRun
, diParagraph
))
220 ME_JoinParagraphs(editor
, ME_GetParagraph(c
.pRun
));
221 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
222 ME_CheckCharOffsets(editor
);
224 if (editor
->bEmulateVersion10
&& nChars
)
231 int nIntendedChars
= nChars
;
232 int nCharsToDelete
= nChars
;
236 ME_FindItemBack(c
.pRun
, diParagraph
)->member
.para
.nFlags
|= MEPF_REWRAP
;
239 ME_StrRelPos(run
->strText
, loc
, &nChars
);
240 /* nChars is the number of characters that should be deleted from the
241 FOLLOWING runs (these AFTER cursor.pRun)
242 nCharsToDelete is a number of chars to delete from THIS run */
243 nCharsToDelete
-= nChars
;
244 shift
-= nCharsToDelete
;
245 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
246 nCharsToDelete
, nIntendedChars
, nChars
, c
.nOffset
,
247 debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
249 if (!c
.nOffset
&& ME_StrVLen(run
->strText
) == nCharsToDelete
)
251 /* undo = reinsert whole run */
252 /* nOfs is a character offset (from the start of the document
253 to the current (deleted) run */
254 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
256 pUndo
->di
.member
.run
.nCharOfs
= nOfs
;
260 /* undo = reinsert partial run */
261 ME_UndoItem
*pUndo
= ME_AddUndoItem(editor
, diUndoInsertRun
, c
.pRun
);
263 ME_DestroyString(pUndo
->di
.member
.run
.strText
);
264 pUndo
->di
.member
.run
.nCharOfs
= nOfs
;
265 pUndo
->di
.member
.run
.strText
= ME_MakeStringN(run
->strText
->szData
+c
.nOffset
, nCharsToDelete
);
268 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run
->strText
->szData
), run
->strText
->nLen
);
269 TRACE("Shift value: %d\n", shift
);
270 ME_StrDeleteV(run
->strText
, c
.nOffset
, nCharsToDelete
);
272 /* update cursors (including c) */
273 for (i
=-1; i
<editor
->nCursors
; i
++) {
274 ME_Cursor
*pThisCur
= editor
->pCursors
+ i
;
275 if (i
== -1) pThisCur
= &c
;
276 if (pThisCur
->pRun
== cursor
.pRun
) {
277 if (pThisCur
->nOffset
> cursor
.nOffset
) {
278 if (pThisCur
->nOffset
-cursor
.nOffset
< nCharsToDelete
)
279 pThisCur
->nOffset
= cursor
.nOffset
;
281 pThisCur
->nOffset
-= nCharsToDelete
;
282 assert(pThisCur
->nOffset
>= 0);
283 assert(pThisCur
->nOffset
<= ME_StrVLen(run
->strText
));
285 if (pThisCur
->nOffset
== ME_StrVLen(run
->strText
))
287 pThisCur
->pRun
= ME_FindItemFwd(pThisCur
->pRun
, diRunOrParagraphOrEnd
);
288 assert(pThisCur
->pRun
->type
== diRun
);
289 pThisCur
->nOffset
= 0;
294 /* c = updated data now */
296 if (c
.pRun
== cursor
.pRun
)
297 ME_SkipAndPropagateCharOffset(c
.pRun
, shift
);
299 ME_PropagateCharOffset(c
.pRun
, shift
);
301 if (!ME_StrVLen(cursor
.pRun
->member
.run
.strText
))
303 TRACE("Removing useless run\n");
304 ME_Remove(cursor
.pRun
);
305 ME_DestroyDisplayItem(cursor
.pRun
);
310 ME_CheckCharOffsets(editor);
317 void ME_DeleteTextAtCursor(ME_TextEditor
*editor
, int nCursor
,
320 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
321 ME_InternalDeleteText(editor
, ME_GetCursorOfs(editor
, nCursor
), nChars
);
324 static ME_DisplayItem
*
325 ME_InternalInsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
326 const WCHAR
*str
, int len
, ME_Style
*style
,
329 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
331 editor
->bCaretAtEnd
= FALSE
;
333 assert(p
->pRun
->type
== diRun
);
335 return ME_InsertRunAtCursor(editor
, p
, style
, str
, len
, flags
);
339 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
340 void ME_InsertGraphicsFromCursor(ME_TextEditor
*editor
, int nCursor
)
342 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
346 if (ME_IsSelection(editor
))
347 ME_DeleteSelection(editor
);
349 ME_InternalInsertTextFromCursor(editor
, nCursor
, &space
, 1, pStyle
,
351 ME_SendSelChange(editor
);
356 ME_InsertTableCellFromCursor(ME_TextEditor
*editor
, int nCursor
)
359 ME_DisplayItem
*p
, *run
;
360 ME_Style
*pStyle
= ME_GetInsertStyle(editor
, nCursor
);
362 p
= ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, pStyle
,
365 while ((run
= ME_FindItemBack(run
, diRunOrParagraph
))->type
== diRun
)
367 if (run
->member
.run
.nFlags
& MERF_CELL
)
369 assert(run
->member
.run
.pCell
->next
);
370 p
->member
.run
.pCell
= run
->member
.run
.pCell
->next
;
374 assert(run
->type
== diParagraph
);
375 assert(run
->member
.para
.bTable
);
376 assert(run
->member
.para
.pCells
);
377 p
->member
.run
.pCell
= run
->member
.para
.pCells
;
381 void ME_InsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
382 const WCHAR
*str
, int len
, ME_Style
*style
)
386 /* FIXME: is this too slow? */
387 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
388 int freeSpace
= editor
->nTextLimit
- ME_GetTextLength(editor
);
392 /* FIXME really HERE ? */
393 if (ME_IsSelection(editor
))
394 ME_DeleteSelection(editor
);
396 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
399 len
= min(len
, freeSpace
);
403 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
404 while(pos
-str
< len
&& *pos
!= '\r' && *pos
!= '\n' && *pos
!= '\t')
406 if (pos
-str
< len
&& *pos
== '\t') { /* handle tabs */
410 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
412 ME_InternalInsertTextFromCursor(editor
, nCursor
, &tab
, 1, style
, MERF_TAB
);
421 if (pos
-str
< len
) { /* handle EOLs */
422 ME_DisplayItem
*tp
, *end_run
;
425 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, pos
-str
, style
, 0);
426 p
= &editor
->pCursors
[nCursor
];
428 ME_SplitRunSimple(editor
, p
->pRun
, p
->nOffset
);
429 p
= &editor
->pCursors
[nCursor
];
431 tmp_style
= ME_GetInsertStyle(editor
, nCursor
);
432 /* ME_SplitParagraph increases style refcount */
433 tp
= ME_SplitParagraph(editor
, p
->pRun
, p
->pRun
->member
.run
.style
);
434 p
->pRun
= ME_FindItemFwd(tp
, diRun
);
435 end_run
= ME_FindItemBack(tp
, diRun
);
436 ME_ReleaseStyle(end_run
->member
.run
.style
);
437 end_run
->member
.run
.style
= tmp_style
;
439 if(pos
-str
< len
&& *pos
=='\r')
441 if(pos
-str
< len
&& *pos
=='\n')
449 ME_InternalInsertTextFromCursor(editor
, nCursor
, str
, len
, style
, 0);
456 ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
458 ME_DisplayItem
*pRun
= pCursor
->pRun
;
462 if (!pCursor
->nOffset
)
465 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
472 if (pRun
->member
.para
.prev_para
->type
== diTextStart
)
474 pRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
475 /* every paragraph ought to have at least one run */
476 assert(pRun
&& pRun
->type
== diRun
);
477 assert(pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
480 assert(pRun
->type
!= diRun
&& pRun
->type
!= diParagraph
);
483 } while (RUN_IS_HIDDEN(&pRun
->member
.run
));
484 pCursor
->pRun
= pRun
;
485 if (pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
486 pCursor
->nOffset
= 0;
488 pCursor
->nOffset
= pRun
->member
.run
.strText
->nLen
;
491 if (pCursor
->nOffset
)
492 pCursor
->nOffset
= ME_StrRelPos2(pCursor
->pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
497 if (!(pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
499 int new_ofs
= ME_StrRelPos2(pRun
->member
.run
.strText
, pCursor
->nOffset
, nRelOfs
);
501 if (new_ofs
< pRun
->member
.run
.strText
->nLen
)
503 pCursor
->nOffset
= new_ofs
;
508 pRun
= ME_FindItemFwd(pRun
, diRun
);
509 } while (pRun
&& RUN_IS_HIDDEN(&pRun
->member
.run
));
512 pCursor
->pRun
= pRun
;
513 pCursor
->nOffset
= 0;
522 ME_MoveCursorWords(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
524 ME_DisplayItem
*pRun
= cursor
->pRun
, *pOtherRun
;
525 int nOffset
= cursor
->nOffset
;
529 /* Backward movement */
532 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
533 nOffset
, WB_MOVEWORDLEFT
);
536 pOtherRun
= ME_FindItemBack(pRun
, diRunOrParagraph
);
537 if (pOtherRun
->type
== diRun
)
539 if (ME_CallWordBreakProc(editor
, pOtherRun
->member
.run
.strText
,
540 pOtherRun
->member
.run
.strText
->nLen
- 1,
542 && !(pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
543 && !(cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
544 && !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
, 0,
548 nOffset
= pOtherRun
->member
.run
.strText
->nLen
;
550 else if (pOtherRun
->type
== diParagraph
)
552 if (cursor
->pRun
== pRun
&& cursor
->nOffset
== 0)
554 /* Paragraph breaks are treated as separate words */
555 if (pOtherRun
->member
.para
.prev_para
->type
== diTextStart
)
557 pRun
= ME_FindItemBack(pOtherRun
, diRunOrParagraph
);
565 /* Forward movement */
566 BOOL last_delim
= FALSE
;
570 if (last_delim
&& !ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
571 nOffset
, WB_ISDELIMITER
))
573 nOffset
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
574 nOffset
, WB_MOVEWORDRIGHT
);
575 if (nOffset
< pRun
->member
.run
.strText
->nLen
)
577 pOtherRun
= ME_FindItemFwd(pRun
, diRunOrParagraphOrEnd
);
578 if (pOtherRun
->type
== diRun
)
580 last_delim
= ME_CallWordBreakProc(editor
, pRun
->member
.run
.strText
,
581 nOffset
- 1, WB_ISDELIMITER
);
585 else if (pOtherRun
->type
== diParagraph
)
587 if (cursor
->pRun
== pRun
)
588 pRun
= ME_FindItemFwd(pOtherRun
, diRun
);
594 if (cursor
->pRun
== pRun
)
602 cursor
->nOffset
= nOffset
;
608 ME_SelectWord(ME_TextEditor
*editor
)
610 if (!(editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
))
611 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], -1);
612 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], +1);
613 ME_InvalidateSelection(editor
);
614 ME_SendSelChange(editor
);
618 int ME_GetCursorOfs(ME_TextEditor
*editor
, int nCursor
)
620 ME_Cursor
*pCursor
= &editor
->pCursors
[nCursor
];
622 return ME_GetParagraph(pCursor
->pRun
)->member
.para
.nCharOfs
623 + pCursor
->pRun
->member
.run
.nCharOfs
+ pCursor
->nOffset
;
626 int ME_FindPixelPos(ME_TextEditor
*editor
, int x
, int y
, ME_Cursor
*result
, BOOL
*is_eol
)
628 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
634 while(p
!= editor
->pBuffer
->pLast
)
636 if (p
->type
== diParagraph
)
638 int ry
= y
- p
->member
.para
.nYPos
;
641 result
->pRun
= ME_FindItemFwd(p
, diRun
);
645 if (ry
>= p
->member
.para
.nHeight
)
647 p
= p
->member
.para
.next_para
;
650 p
= ME_FindItemFwd(p
, diStartRow
);
654 if (p
->type
== diStartRow
)
656 int ry
= y
- p
->member
.row
.nYPos
;
659 if (ry
>= p
->member
.row
.nHeight
)
661 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
662 if (p
->type
!= diStartRow
)
666 p
= ME_FindItemFwd(p
, diRun
);
669 if (p
->type
== diRun
)
672 rx
= x
- p
->member
.run
.pt
.x
;
675 if (rx
>= p
->member
.run
.nWidth
) /* not this run yet... find next item */
680 if (p
->type
== diRun
)
682 rx
= x
- p
->member
.run
.pt
.x
;
683 goto continue_search
;
685 if (p
->type
== diStartRow
)
687 p
= ME_FindItemFwd(p
, diRun
);
690 rx
= 0; /* FIXME not sure */
693 if (p
->type
== diParagraph
|| p
->type
== diTextEnd
)
695 rx
= 0; /* FIXME not sure */
703 if (p
->member
.run
.nFlags
& MERF_ENDPARA
)
706 result
->nOffset
= ME_CharFromPointCursor(editor
, rx
, &p
->member
.run
);
707 if (editor
->pCursors
[0].nOffset
== p
->member
.run
.strText
->nLen
&& rx
)
709 result
->pRun
= ME_FindItemFwd(editor
->pCursors
[0].pRun
, diRun
);
718 result
->pRun
= ME_FindItemBack(p
, diRun
);
720 assert(result
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
726 ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
)
731 GetClientRect(editor
->hWnd
, &rc
);
732 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
)
734 y
+= ME_GetYScrollPos(editor
);
735 ME_FindPixelPos(editor
, x
, y
, &cursor
, NULL
);
736 return (ME_GetParagraph(cursor
.pRun
)->member
.para
.nCharOfs
737 + cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
);
741 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
)
743 ME_Cursor tmp_cursor
;
744 int is_selection
= 0;
746 editor
->nUDArrowX
= -1;
748 y
+= ME_GetYScrollPos(editor
);
750 tmp_cursor
= editor
->pCursors
[0];
751 is_selection
= ME_IsSelection(editor
);
753 ME_FindPixelPos(editor
, x
, y
, &editor
->pCursors
[0], &editor
->bCaretAtEnd
);
755 if (GetKeyState(VK_SHIFT
)>=0)
757 editor
->pCursors
[1] = editor
->pCursors
[0];
762 editor
->pCursors
[1] = tmp_cursor
;
766 ME_InvalidateSelection(editor
);
767 HideCaret(editor
->hWnd
);
768 ME_MoveCaret(editor
);
769 ShowCaret(editor
->hWnd
);
770 ME_ClearTempStyle(editor
);
771 ME_SendSelChange(editor
);
774 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
776 ME_Cursor tmp_cursor
;
778 y
+= ME_GetYScrollPos(editor
);
780 tmp_cursor
= editor
->pCursors
[0];
781 /* FIXME: do something with the return value of ME_FindPixelPos */
782 ME_FindPixelPos(editor
, x
, y
, &tmp_cursor
, &editor
->bCaretAtEnd
);
784 if (tmp_cursor
.pRun
== editor
->pCursors
[0].pRun
&&
785 tmp_cursor
.nOffset
== editor
->pCursors
[0].nOffset
)
788 ME_InvalidateSelection(editor
);
789 editor
->pCursors
[0] = tmp_cursor
;
790 HideCaret(editor
->hWnd
);
791 ME_MoveCaret(editor
);
792 ME_InvalidateSelection(editor
);
793 ShowCaret(editor
->hWnd
);
794 ME_SendSelChange(editor
);
797 static ME_DisplayItem
*ME_FindRunInRow(ME_TextEditor
*editor
, ME_DisplayItem
*pRow
,
798 int x
, int *pOffset
, int *pbCaretAtEnd
)
800 ME_DisplayItem
*pNext
, *pLastRun
;
801 pNext
= ME_FindItemFwd(pRow
, diRunOrStartRow
);
802 assert(pNext
->type
== diRun
);
804 *pbCaretAtEnd
= FALSE
;
806 int run_x
= pNext
->member
.run
.pt
.x
;
807 int width
= pNext
->member
.run
.nWidth
;
810 if (pOffset
) *pOffset
= 0;
813 if (x
>= run_x
&& x
< run_x
+width
)
815 int ch
= ME_CharFromPointCursor(editor
, x
-run_x
, &pNext
->member
.run
);
816 ME_String
*s
= pNext
->member
.run
.strText
;
824 pNext
= ME_FindItemFwd(pNext
, diRunOrStartRow
);
825 } while(pNext
&& pNext
->type
== diRun
);
827 if ((pLastRun
->member
.run
.nFlags
& MERF_ENDPARA
) == 0)
829 pNext
= ME_FindItemFwd(pNext
, diRun
);
830 if (pbCaretAtEnd
) *pbCaretAtEnd
= 1;
831 if (pOffset
) *pOffset
= 0;
834 if (pbCaretAtEnd
) *pbCaretAtEnd
= 0;
835 if (pOffset
) *pOffset
= 0;
840 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
842 ME_DisplayItem
*pRun
= pCursor
->pRun
;
845 if (editor
->nUDArrowX
!= -1)
846 x
= editor
->nUDArrowX
;
848 if (editor
->bCaretAtEnd
)
850 pRun
= ME_FindItemBack(pRun
, diRun
);
852 x
= pRun
->member
.run
.pt
.x
+ pRun
->member
.run
.nWidth
;
855 x
= pRun
->member
.run
.pt
.x
;
856 x
+= ME_PointFromChar(editor
, &pRun
->member
.run
, pCursor
->nOffset
);
858 editor
->nUDArrowX
= x
;
865 ME_MoveCursorLines(ME_TextEditor
*editor
, ME_Cursor
*pCursor
, int nRelOfs
)
867 ME_DisplayItem
*pRun
= pCursor
->pRun
;
868 ME_DisplayItem
*pItem
;
869 int x
= ME_GetXForArrow(editor
, pCursor
);
871 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
872 pRun
= ME_FindItemBack(pRun
, diRun
);
877 /* start of this row */
878 pItem
= ME_FindItemBack(pRun
, diStartRow
);
880 /* start of the previous row */
881 pItem
= ME_FindItemBack(pItem
, diStartRow
);
885 /* start of the next row */
886 pItem
= ME_FindItemFwd(pRun
, diStartRow
);
887 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
892 /* row not found - ignore */
895 pCursor
->pRun
= ME_FindRunInRow(editor
, pItem
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
896 assert(pCursor
->pRun
);
897 assert(pCursor
->pRun
->type
== diRun
);
901 static void ME_ArrowPageUp(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
903 ME_DisplayItem
*pRun
= pCursor
->pRun
;
904 ME_DisplayItem
*pLast
, *p
;
905 int x
, y
, ys
, yd
, yp
, yprev
;
906 ME_Cursor tmp_curs
= *pCursor
;
908 x
= ME_GetXForArrow(editor
, pCursor
);
909 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
910 pRun
= ME_FindItemBack(pRun
, diRun
);
912 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
913 assert(p
->type
== diStartRow
);
914 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
915 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
916 yd
= y
- editor
->sizeWindow
.cy
;
920 p
= ME_FindItemBack(p
, diStartRowOrParagraph
);
923 if (p
->type
== diParagraph
) { /* crossing paragraphs */
924 if (p
->member
.para
.prev_para
== NULL
)
926 yp
= p
->member
.para
.prev_para
->member
.para
.nYPos
;
929 y
= yp
+ p
->member
.row
.nYPos
;
936 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
937 ME_UpdateSelection(editor
, &tmp_curs
);
938 if (yprev
< editor
->sizeWindow
.cy
)
940 ME_EnsureVisible(editor
, ME_FindItemFwd(editor
->pBuffer
->pFirst
, diRun
));
944 ME_Scroll(editor
, 0, ys
-yprev
);
947 assert(pCursor
->pRun
);
948 assert(pCursor
->pRun
->type
== diRun
);
951 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
952 of pixels, even if it makes the scroll bar position exceed its normal maximum.
953 In such a situation, clicking the scrollbar restores its position back to the
954 normal range (ie. sets it to (doclength-screenheight)). */
956 static void ME_ArrowPageDown(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
958 ME_DisplayItem
*pRun
= pCursor
->pRun
;
959 ME_DisplayItem
*pLast
, *p
;
960 int x
, y
, ys
, yd
, yp
, yprev
;
961 ME_Cursor tmp_curs
= *pCursor
;
963 x
= ME_GetXForArrow(editor
, pCursor
);
964 if (!pCursor
->nOffset
&& editor
->bCaretAtEnd
)
965 pRun
= ME_FindItemBack(pRun
, diRun
);
967 p
= ME_FindItemBack(pRun
, diStartRowOrParagraph
);
968 assert(p
->type
== diStartRow
);
969 yp
= ME_FindItemBack(p
, diParagraph
)->member
.para
.nYPos
;
970 yprev
= ys
= y
= yp
+ p
->member
.row
.nYPos
;
971 yd
= y
+ editor
->sizeWindow
.cy
;
975 p
= ME_FindItemFwd(p
, diStartRowOrParagraph
);
978 if (p
->type
== diParagraph
) {
979 yp
= p
->member
.para
.nYPos
;
982 y
= yp
+ p
->member
.row
.nYPos
;
989 pCursor
->pRun
= ME_FindRunInRow(editor
, pLast
, x
, &pCursor
->nOffset
, &editor
->bCaretAtEnd
);
990 ME_UpdateSelection(editor
, &tmp_curs
);
991 if (yprev
>= editor
->nTotalLength
-editor
->sizeWindow
.cy
)
993 ME_EnsureVisible(editor
, ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
));
997 ME_Scroll(editor
, 0, ys
-yprev
);
1000 assert(pCursor
->pRun
);
1001 assert(pCursor
->pRun
->type
== diRun
);
1004 static void ME_ArrowHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1006 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diStartRow
);
1007 /* bCaretAtEnd doesn't make sense if the cursor isn't set at the
1008 first character of the next row */
1009 assert(!editor
->bCaretAtEnd
|| !pCursor
->nOffset
);
1010 ME_WrapMarkedParagraphs(editor
);
1012 ME_DisplayItem
*pRun
;
1013 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
) {
1014 pRow
= ME_FindItemBack(pRow
, diStartRow
);
1018 pRun
= ME_FindItemFwd(pRow
, diRun
);
1020 pCursor
->pRun
= pRun
;
1021 pCursor
->nOffset
= 0;
1024 editor
->bCaretAtEnd
= FALSE
;
1027 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1029 ME_DisplayItem
*pRow
= ME_FindItemBack(pCursor
->pRun
, diTextStart
);
1031 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1033 pCursor
->pRun
= pRun
;
1034 pCursor
->nOffset
= 0;
1039 static void ME_ArrowEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1041 ME_DisplayItem
*pRow
;
1043 if (editor
->bCaretAtEnd
&& !pCursor
->nOffset
)
1046 pRow
= ME_FindItemFwd(pCursor
->pRun
, diStartRowOrParagraphOrEnd
);
1048 if (pRow
->type
== diStartRow
) {
1049 /* FIXME WTF was I thinking about here ? */
1050 ME_DisplayItem
*pRun
= ME_FindItemFwd(pRow
, diRun
);
1052 pCursor
->pRun
= pRun
;
1053 pCursor
->nOffset
= 0;
1054 editor
->bCaretAtEnd
= 1;
1057 pCursor
->pRun
= ME_FindItemBack(pRow
, diRun
);
1058 assert(pCursor
->pRun
&& pCursor
->pRun
->member
.run
.nFlags
& MERF_ENDPARA
);
1059 pCursor
->nOffset
= 0;
1060 editor
->bCaretAtEnd
= FALSE
;
1063 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1065 ME_DisplayItem
*p
= ME_FindItemFwd(pCursor
->pRun
, diTextEnd
);
1067 p
= ME_FindItemBack(p
, diRun
);
1069 assert(p
->member
.run
.nFlags
& MERF_ENDPARA
);
1071 pCursor
->nOffset
= 0;
1072 editor
->bCaretAtEnd
= FALSE
;
1075 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1077 return memcmp(&editor
->pCursors
[0], &editor
->pCursors
[1], sizeof(ME_Cursor
))!=0;
1080 static int ME_GetSelCursor(ME_TextEditor
*editor
, int dir
)
1082 int cdir
= ME_GetCursorOfs(editor
, 0) - ME_GetCursorOfs(editor
, 1);
1090 BOOL
ME_UpdateSelection(ME_TextEditor
*editor
, ME_Cursor
*pTempCursor
)
1092 ME_Cursor old_anchor
= editor
->pCursors
[1];
1094 if (GetKeyState(VK_SHIFT
)>=0) /* cancelling selection */
1096 /* any selection was present ? if so, it's no more, repaint ! */
1097 editor
->pCursors
[1] = editor
->pCursors
[0];
1098 if (memcmp(pTempCursor
, &old_anchor
, sizeof(ME_Cursor
))) {
1105 if (!memcmp(pTempCursor
, &editor
->pCursors
[1], sizeof(ME_Cursor
))) /* starting selection */
1107 editor
->pCursors
[1] = *pTempCursor
;
1116 void ME_DeleteSelection(ME_TextEditor
*editor
)
1119 ME_GetSelection(editor
, &from
, &to
);
1120 ME_DeleteTextAtCursor(editor
, ME_GetSelCursor(editor
,-1), to
-from
);
1123 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1129 ME_GetSelection(editor
, &from
, &to
);
1130 ME_CursorFromCharOfs(editor
, from
, &c
);
1132 style
= c
.pRun
->member
.run
.style
;
1133 ME_AddRefStyle(style
); /* ME_GetInsertStyle has already done that */
1136 style
= ME_GetInsertStyle(editor
, 0);
1140 void ME_SendSelChange(ME_TextEditor
*editor
)
1144 ME_ClearTempStyle(editor
);
1146 if (!(editor
->nEventMask
& ENM_SELCHANGE
))
1149 sc
.nmhdr
.hwndFrom
= editor
->hWnd
;
1150 sc
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
1151 sc
.nmhdr
.code
= EN_SELCHANGE
;
1152 SendMessageW(editor
->hWnd
, EM_EXGETSEL
, 0, (LPARAM
)&sc
.chrg
);
1153 sc
.seltyp
= SEL_EMPTY
;
1154 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1155 sc
.seltyp
|= SEL_TEXT
;
1156 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* wth were RICHEDIT authors thinking ? */
1157 sc
.seltyp
|= SEL_MULTICHAR
;
1158 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
, sc
.nmhdr
.idFrom
, (LPARAM
)&sc
);
1163 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1166 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1167 ME_Cursor tmp_curs
= *p
;
1168 BOOL success
= FALSE
;
1170 ME_CheckCharOffsets(editor
);
1171 editor
->nUDArrowX
= -1;
1174 editor
->bCaretAtEnd
= 0;
1176 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1178 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1);
1181 editor
->bCaretAtEnd
= 0;
1183 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1185 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1);
1188 ME_MoveCursorLines(editor
, &tmp_curs
, -1);
1191 ME_MoveCursorLines(editor
, &tmp_curs
, +1);
1194 ME_ArrowPageUp(editor
, &tmp_curs
);
1197 ME_ArrowPageDown(editor
, &tmp_curs
);
1201 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1203 ME_ArrowHome(editor
, &tmp_curs
);
1204 editor
->bCaretAtEnd
= 0;
1209 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1211 ME_ArrowEnd(editor
, &tmp_curs
);
1216 editor
->pCursors
[1] = tmp_curs
;
1219 ME_InvalidateSelection(editor
);
1221 HideCaret(editor
->hWnd
);
1222 ME_EnsureVisible(editor
, tmp_curs
.pRun
);
1223 ME_ShowCaret(editor
);
1224 ME_SendSelChange(editor
);