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
);
27 void ME_SetCursorToStart(ME_TextEditor
*editor
, ME_Cursor
*cursor
)
29 cursor
->para
= editor_first_para( editor
);
30 cursor
->run
= para_first_run( cursor
->para
);
34 static void ME_SetCursorToEnd(ME_TextEditor
*editor
, ME_Cursor
*cursor
, BOOL final_eop
)
36 cursor
->para
= para_prev( editor_end_para( editor
) );
37 cursor
->run
= para_end_run( cursor
->para
);
38 cursor
->nOffset
= final_eop
? cursor
->run
->len
: 0;
42 int ME_GetSelectionOfs(ME_TextEditor
*editor
, int *from
, int *to
)
44 *from
= ME_GetCursorOfs(&editor
->pCursors
[0]);
45 *to
= ME_GetCursorOfs(&editor
->pCursors
[1]);
57 int ME_GetSelection(ME_TextEditor
*editor
, ME_Cursor
**from
, ME_Cursor
**to
)
59 int from_ofs
= ME_GetCursorOfs( &editor
->pCursors
[0] );
60 int to_ofs
= ME_GetCursorOfs( &editor
->pCursors
[1] );
61 BOOL swap
= (from_ofs
> to_ofs
);
63 if (from_ofs
== to_ofs
)
65 /* If cursor[0] is at the beginning of a run and cursor[1] at the end
66 of the prev run then we need to swap. */
67 if (editor
->pCursors
[0].nOffset
< editor
->pCursors
[1].nOffset
)
73 *from
= &editor
->pCursors
[0];
74 *to
= &editor
->pCursors
[1];
77 *from
= &editor
->pCursors
[1];
78 *to
= &editor
->pCursors
[0];
83 int ME_GetTextLength(ME_TextEditor
*editor
)
86 ME_SetCursorToEnd(editor
, &cursor
, FALSE
);
87 return ME_GetCursorOfs(&cursor
);
91 int ME_GetTextLengthEx(ME_TextEditor
*editor
, const GETTEXTLENGTHEX
*how
)
95 if (how
->flags
& GTL_PRECISE
&& how
->flags
& GTL_CLOSE
)
97 if (how
->flags
& GTL_NUMCHARS
&& how
->flags
& GTL_NUMBYTES
)
100 length
= ME_GetTextLength(editor
);
102 if ((editor
->props
& TXTBIT_MULTILINE
)
103 && (how
->flags
& GTL_USECRLF
)
104 && !editor
->bEmulateVersion10
) /* Ignore GTL_USECRLF flag in 1.0 emulation */
105 length
+= editor
->nParagraphs
- 1;
107 if (how
->flags
& GTL_NUMBYTES
||
108 (how
->flags
& GTL_PRECISE
&& /* GTL_PRECISE seems to imply GTL_NUMBYTES */
109 !(how
->flags
& GTL_NUMCHARS
))) /* unless GTL_NUMCHARS is given */
113 if (how
->codepage
== 1200)
115 if (how
->flags
& GTL_PRECISE
)
116 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
117 if (GetCPInfo(how
->codepage
, &cpinfo
))
118 return length
* cpinfo
.MaxCharSize
;
119 ERR("Invalid codepage %u\n", how
->codepage
);
125 /******************************************************************
126 * set_selection_cursors
128 * Updates the selection cursors.
130 * Note that this does not invalidate either the old or the new selections.
132 int set_selection_cursors(ME_TextEditor
*editor
, int from
, int to
)
134 int selectionEnd
= 0;
135 const int len
= ME_GetTextLength(editor
);
137 /* all negative values are effectively the same */
144 if (from
== 0 && to
== -1)
146 ME_SetCursorToStart(editor
, &editor
->pCursors
[1]);
147 ME_SetCursorToEnd(editor
, &editor
->pCursors
[0], TRUE
);
151 /* if both values are equal and also out of bound, that means to */
152 /* put the selection at the end of the text */
153 if ((from
== to
) && (to
< 0 || to
> len
))
159 /* if from is negative and to is positive then selection is */
160 /* deselected and caret moved to end of the current selection */
164 ME_GetSelectionOfs(editor
, &start
, &end
);
169 editor
->pCursors
[0].nOffset
= 0;
172 editor
->pCursors
[1] = editor
->pCursors
[0];
177 /* adjust to if it's a negative value */
181 /* flip from and to if they are reversed */
189 /* after fiddling with the values, we find from > len && to > len */
192 /* special case with to too big */
199 ME_SetCursorToEnd(editor
, &editor
->pCursors
[0], FALSE
);
200 editor
->pCursors
[1] = editor
->pCursors
[0];
204 cursor_from_char_ofs( editor
, from
, &editor
->pCursors
[1] );
205 editor
->pCursors
[0] = editor
->pCursors
[1];
206 ME_MoveCursorChars(editor
, &editor
->pCursors
[0], to
- from
, FALSE
);
207 /* Selection is not allowed in the middle of an end paragraph run. */
208 if (editor
->pCursors
[1].run
->nFlags
& MERF_ENDPARA
)
209 editor
->pCursors
[1].nOffset
= 0;
210 if (editor
->pCursors
[0].run
->nFlags
& MERF_ENDPARA
)
213 editor
->pCursors
[0].nOffset
= editor
->pCursors
[0].run
->len
;
215 editor
->pCursors
[0].nOffset
= 0;
221 void cursor_coords( ME_TextEditor
*editor
, ME_Cursor
*cursor
,
222 int *x
, int *y
, int *height
)
225 ME_Run
*run
= cursor
->run
;
226 ME_Paragraph
*para
= cursor
->para
;
227 ME_Run
*size_run
= run
, *prev
;
230 HDC hdc
= ITextHost_TxGetDC( editor
->texthost
);
232 assert(~para
->nFlags
& MEPF_REWRAP
);
234 row
= row_from_cursor( cursor
);
236 ME_InitContext( &c
, editor
, hdc
);
238 if (!cursor
->nOffset
&& (prev
= run_prev( run
))) size_run
= prev
;
240 run_x
= ME_PointFromCharContext( &c
, run
, cursor
->nOffset
, TRUE
);
242 *height
= size_run
->nAscent
+ size_run
->nDescent
;
243 *x
= c
.rcView
.left
+ run
->pt
.x
+ run_x
- editor
->horz_si
.nPos
;
244 *y
= c
.rcView
.top
+ para
->pt
.y
+ row
->nBaseline
245 + run
->pt
.y
- size_run
->nAscent
- editor
->vert_si
.nPos
;
246 ME_DestroyContext(&c
);
247 ITextHost_TxReleaseDC( editor
->texthost
, hdc
);
251 void create_caret(ME_TextEditor
*editor
)
255 cursor_coords( editor
, &editor
->pCursors
[0], &x
, &y
, &height
);
256 ITextHost_TxCreateCaret(editor
->texthost
, NULL
, 0, height
);
257 editor
->caret_height
= height
;
258 editor
->caret_hidden
= TRUE
;
261 void show_caret(ME_TextEditor
*editor
)
263 ITextHost_TxShowCaret(editor
->texthost
, TRUE
);
264 editor
->caret_hidden
= FALSE
;
267 void hide_caret(ME_TextEditor
*editor
)
269 /* calls to HideCaret are cumulative; do so only once */
270 if (!editor
->caret_hidden
)
272 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
273 editor
->caret_hidden
= TRUE
;
277 void update_caret(ME_TextEditor
*editor
)
281 if (!editor
->bHaveFocus
) return;
282 if (!ME_IsSelection(editor
))
284 cursor_coords( editor
, &editor
->pCursors
[0], &x
, &y
, &height
);
285 if (height
!= editor
->caret_height
) create_caret(editor
);
286 x
= min(x
, editor
->rcFormat
.right
-1);
287 ITextHost_TxSetCaretPos(editor
->texthost
, x
, y
);
294 BOOL
ME_InternalDeleteText(ME_TextEditor
*editor
, ME_Cursor
*start
,
295 int nChars
, BOOL bForce
)
297 ME_Cursor c
= *start
;
298 int nOfs
= ME_GetCursorOfs(start
), text_len
= ME_GetTextLength( editor
);
300 int totalChars
= nChars
;
301 ME_Paragraph
*start_para
;
302 BOOL delete_all
= FALSE
;
304 /* Prevent deletion past last end of paragraph run. */
305 nChars
= min(nChars
, text_len
- nOfs
);
306 if (nChars
== text_len
) delete_all
= TRUE
;
311 table_protect_partial_deletion( editor
, &c
, &nChars
);
312 if (nChars
== 0) return FALSE
;
318 cursor_from_char_ofs( editor
, nOfs
+ nChars
, &c
);
321 /* We aren't deleting anything in this run, so we will go back to the
322 * last run we are deleting text in. */
323 c
.run
= run_prev_all_paras( c
.run
);
324 c
.para
= c
.run
->para
;
325 c
.nOffset
= c
.run
->len
;
328 if (run
->nFlags
& MERF_ENDPARA
)
330 int eollen
= c
.run
->len
;
331 BOOL keepFirstParaFormat
;
333 if (!para_next( para_next( c
.para
) )) return TRUE
;
335 keepFirstParaFormat
= (totalChars
== nChars
&& nChars
<= eollen
&&
337 if (!editor
->bEmulateVersion10
) /* v4.1 */
339 ME_Paragraph
*this_para
= run
->para
;
340 ME_Paragraph
*next_para
= para_next( this_para
);
342 /* The end of paragraph before a table row is only deleted if there
343 * is nothing else on the line before it. */
344 if (this_para
== start_para
&& next_para
->nFlags
& MEPF_ROWSTART
)
346 /* If the paragraph will be empty, then it should be deleted, however
347 * it still might have text right now which would inherit the
348 * MEPF_STARTROW property if we joined it right now.
349 * Instead we will delete it after the preceding text is deleted. */
350 if (nOfs
> this_para
->nCharOfs
)
352 /* Skip this end of line. */
353 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
356 keepFirstParaFormat
= TRUE
;
359 para_join( editor
, c
.para
, keepFirstParaFormat
);
360 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
361 ME_CheckCharOffsets(editor
);
362 nChars
-= (eollen
< nChars
) ? eollen
: nChars
;
368 int nCharsToDelete
= min(nChars
, c
.nOffset
);
371 c
.nOffset
-= nCharsToDelete
;
373 para_mark_rewrap( editor
, c
.run
->para
);
376 /* nChars is the number of characters that should be deleted from the
377 PRECEDING runs (these BEFORE cursor.pRun)
378 nCharsToDelete is a number of chars to delete from THIS run */
379 nChars
-= nCharsToDelete
;
380 shift
-= nCharsToDelete
;
381 TRACE("Deleting %d (remaining %d) chars at %d in %s (%d)\n",
382 nCharsToDelete
, nChars
, c
.nOffset
,
383 debugstr_run( run
), run
->len
);
385 /* nOfs is a character offset (from the start of the document
386 to the current (deleted) run */
387 add_undo_insert_run( editor
, nOfs
+ nChars
, get_text( run
, c
.nOffset
), nCharsToDelete
, run
->nFlags
, run
->style
);
389 ME_StrDeleteV(run
->para
->text
, run
->nCharOfs
+ c
.nOffset
, nCharsToDelete
);
390 run
->len
-= nCharsToDelete
;
391 TRACE("Post deletion string: %s (%d)\n", debugstr_run( run
), run
->len
);
392 TRACE("Shift value: %d\n", shift
);
394 /* update cursors (including c) */
395 for (i
=-1; i
<editor
->nCursors
; i
++) {
396 ME_Cursor
*pThisCur
= editor
->pCursors
+ i
;
397 if (i
== -1) pThisCur
= &c
;
398 if (pThisCur
->run
== cursor
.run
) {
399 if (pThisCur
->nOffset
> cursor
.nOffset
) {
400 if (pThisCur
->nOffset
-cursor
.nOffset
< nCharsToDelete
)
401 pThisCur
->nOffset
= cursor
.nOffset
;
403 pThisCur
->nOffset
-= nCharsToDelete
;
404 assert(pThisCur
->nOffset
>= 0);
405 assert(pThisCur
->nOffset
<= run
->len
);
407 if (pThisCur
->nOffset
== run
->len
)
409 pThisCur
->run
= run_next( pThisCur
->run
);
410 assert( pThisCur
->run
);
411 pThisCur
->nOffset
= 0;
416 /* c = updated data now */
418 if (c
.run
== cursor
.run
) c
.run
->nCharOfs
-= shift
;
419 editor_propagate_char_ofs( NULL
, c
.run
, shift
);
421 if (!cursor
.run
->len
)
423 TRACE("Removing empty run\n");
424 ME_Remove( run_get_di( cursor
.run
));
425 ME_DestroyDisplayItem( run_get_di( cursor
.run
));
432 if (delete_all
) editor_set_default_para_fmt( editor
, &start_para
->fmt
);
436 BOOL
ME_DeleteTextAtCursor(ME_TextEditor
*editor
, int nCursor
, int nChars
)
438 assert(nCursor
>=0 && nCursor
<editor
->nCursors
);
439 /* text operations set modified state */
440 editor
->nModifyStep
= 1;
441 return ME_InternalDeleteText(editor
, &editor
->pCursors
[nCursor
],
445 static struct re_object
* create_re_object(const REOBJECT
*reo
)
447 struct re_object
*reobj
= heap_alloc(sizeof(*reobj
));
451 WARN("Fail to allocate re_object.\n");
454 ME_CopyReObject(&reobj
->obj
, reo
, REO_GETOBJ_ALL_INTERFACES
);
458 void ME_InsertOLEFromCursor(ME_TextEditor
*editor
, const REOBJECT
* reo
, int nCursor
)
461 const WCHAR space
= ' ';
462 struct re_object
*reobj_prev
= NULL
;
463 ME_Cursor
*cursor
= editor
->pCursors
+ nCursor
;
464 ME_Style
*style
= style_get_insert_style( editor
, cursor
);
467 if (ME_IsSelection(editor
))
468 ME_DeleteSelection(editor
);
470 run
= run_insert( editor
, cursor
, style
, &space
, 1, MERF_GRAPHICS
);
472 run
->reobj
= create_re_object( reo
);
475 while ((prev
= run_prev_all_paras( prev
)))
479 reobj_prev
= prev
->reobj
;
484 list_add_after(&reobj_prev
->entry
, &run
->reobj
->entry
);
486 list_add_head(&editor
->reobj_list
, &run
->reobj
->entry
);
488 ME_ReleaseStyle( style
);
492 void ME_InsertEndRowFromCursor(ME_TextEditor
*editor
, int nCursor
)
494 const WCHAR space
= ' ';
495 ME_Cursor
*cursor
= editor
->pCursors
+ nCursor
;
496 ME_Style
*style
= style_get_insert_style( editor
, cursor
);
499 if (ME_IsSelection(editor
))
500 ME_DeleteSelection(editor
);
502 run_insert( editor
, cursor
, style
, &space
, 1, MERF_ENDROW
);
504 ME_ReleaseStyle( style
);
508 void ME_InsertTextFromCursor(ME_TextEditor
*editor
, int nCursor
,
509 const WCHAR
*str
, int len
, ME_Style
*style
)
512 ME_Cursor
*cursor
= editor
->pCursors
+ nCursor
;
515 /* FIXME really HERE ? */
516 if (ME_IsSelection(editor
))
517 ME_DeleteSelection(editor
);
519 oldLen
= ME_GetTextLength(editor
);
521 /* text operations set modified state */
522 editor
->nModifyStep
= 1;
526 if (len
== -1) len
= lstrlenW( str
);
528 /* grow the text limit to fit our text */
529 if (editor
->nTextLimit
< oldLen
+ len
) editor
->nTextLimit
= oldLen
+ len
;
535 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
536 while (pos
- str
< len
&& *pos
!= '\r' && *pos
!= '\n' && *pos
!= '\t')
539 if (pos
!= str
) /* handle text */
540 run_insert( editor
, cursor
, style
, str
, pos
- str
, 0 );
541 else if (*pos
== '\t') /* handle tabs */
543 const WCHAR tab
= '\t';
544 run_insert( editor
, cursor
, style
, &tab
, 1, MERF_TAB
);
547 else /* handle EOLs */
549 ME_Run
*end_run
, *run
, *prev
;
550 ME_Paragraph
*new_para
;
553 /* Check if new line is allowed for this control */
554 if (!(editor
->props
& TXTBIT_MULTILINE
))
557 /* Find number of CR and LF in end of paragraph run */
560 if (len
> 1 && pos
[1] == '\n')
562 else if (len
> 2 && pos
[1] == '\r' && pos
[2] == '\n')
569 assert(*pos
== '\n');
574 if (!editor
->bEmulateVersion10
&& eol_len
== 3)
576 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
577 const WCHAR space
= ' ';
578 run_insert( editor
, cursor
, style
, &space
, 1, 0 );
582 const WCHAR cr
= '\r', *eol_str
= str
;
584 if (!editor
->bEmulateVersion10
)
590 if (cursor
->nOffset
== cursor
->run
->len
)
592 run
= run_next( cursor
->run
);
593 if (!run
) run
= cursor
->run
;
597 if (cursor
->nOffset
) run_split( editor
, cursor
);
601 new_para
= para_split( editor
, run
, style
, eol_str
, eol_len
, 0 );
602 end_run
= para_end_run( para_prev( new_para
) );
604 /* Move any cursors that were at the end of the previous run to the beginning of the new para */
605 prev
= run_prev( end_run
);
609 for (i
= 0; i
< editor
->nCursors
; i
++)
611 if (editor
->pCursors
[i
].run
== prev
&&
612 editor
->pCursors
[i
].nOffset
== prev
->len
)
614 editor
->pCursors
[i
].para
= new_para
;
615 editor
->pCursors
[i
].run
= run
;
616 editor
->pCursors
[i
].nOffset
= 0;
628 /* Move the cursor nRelOfs characters (either forwards or backwards)
629 * If final_eop is TRUE, allow moving the cursor to the end of the final eop.
631 * returns the actual number of characters moved.
633 int ME_MoveCursorChars(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
, BOOL final_eop
)
635 cursor
->nOffset
+= nRelOfs
;
636 if (cursor
->nOffset
< 0)
638 cursor
->nOffset
+= cursor
->run
->nCharOfs
;
639 if (cursor
->nOffset
>= 0)
641 /* new offset in the same paragraph */
643 cursor
->run
= run_prev( cursor
->run
);
644 } while (cursor
->nOffset
< cursor
->run
->nCharOfs
);
645 cursor
->nOffset
-= cursor
->run
->nCharOfs
;
649 cursor
->nOffset
+= cursor
->para
->nCharOfs
;
650 if (cursor
->nOffset
<= 0)
652 /* moved to the start of the text */
653 nRelOfs
-= cursor
->nOffset
;
654 ME_SetCursorToStart(editor
, cursor
);
658 /* new offset in a previous paragraph */
660 cursor
->para
= para_prev( cursor
->para
);
661 } while (cursor
->nOffset
< cursor
->para
->nCharOfs
);
662 cursor
->nOffset
-= cursor
->para
->nCharOfs
;
664 cursor
->run
= para_end_run( cursor
->para
);
665 while (cursor
->nOffset
< cursor
->run
->nCharOfs
)
666 cursor
->run
= run_prev( cursor
->run
);
667 cursor
->nOffset
-= cursor
->run
->nCharOfs
;
669 else if (cursor
->nOffset
>= cursor
->run
->len
)
671 ME_Paragraph
*next_para
;
674 new_offset
= ME_GetCursorOfs(cursor
);
675 next_para
= para_next( cursor
->para
);
676 if (new_offset
< next_para
->nCharOfs
)
678 /* new offset in the same paragraph */
680 cursor
->nOffset
-= cursor
->run
->len
;
681 cursor
->run
= run_next( cursor
->run
);
682 } while (cursor
->nOffset
>= cursor
->run
->len
);
686 if (new_offset
>= ME_GetTextLength(editor
) + (final_eop
? 1 : 0))
688 /* new offset at the end of the text */
689 ME_SetCursorToEnd(editor
, cursor
, final_eop
);
690 nRelOfs
-= new_offset
- (ME_GetTextLength(editor
) + (final_eop
? 1 : 0));
694 /* new offset in a following paragraph */
696 cursor
->para
= next_para
;
697 next_para
= para_next( next_para
);
698 } while (new_offset
>= next_para
->nCharOfs
);
700 cursor
->nOffset
= new_offset
- cursor
->para
->nCharOfs
;
701 cursor
->run
= para_first_run( cursor
->para
);
702 while (cursor
->nOffset
>= cursor
->run
->len
)
704 cursor
->nOffset
-= cursor
->run
->len
;
705 cursor
->run
= run_next( cursor
->run
);
707 } /* else new offset is in the same run */
713 ME_MoveCursorWords(ME_TextEditor
*editor
, ME_Cursor
*cursor
, int nRelOfs
)
715 ME_Run
*run
= cursor
->run
, *other_run
;
716 ME_Paragraph
*para
= cursor
->para
;
717 int nOffset
= cursor
->nOffset
;
721 /* Backward movement */
724 nOffset
= ME_CallWordBreakProc( editor
, get_text( run
, 0 ), run
->len
, nOffset
, WB_MOVEWORDLEFT
);
726 other_run
= run_prev( run
);
729 if (ME_CallWordBreakProc( editor
, get_text( other_run
, 0 ), other_run
->len
, other_run
->len
- 1, WB_ISDELIMITER
)
730 && !(run
->nFlags
& MERF_ENDPARA
)
731 && !(cursor
->run
== run
&& cursor
->nOffset
== 0)
732 && !ME_CallWordBreakProc( editor
, get_text( run
, 0 ), run
->len
, 0, WB_ISDELIMITER
))
735 nOffset
= other_run
->len
;
739 if (cursor
->run
== run
&& cursor
->nOffset
== 0)
742 /* Skip empty start of table row paragraph */
743 if (para_prev( para
) && para_prev( para
)->nFlags
& MEPF_ROWSTART
)
744 para
= para_prev( para
);
745 /* Paragraph breaks are treated as separate words */
746 if (!para_prev( para
)) return FALSE
;
747 para
= para_prev( para
);
748 run
= para_end_run( para
);
756 /* Forward movement */
757 BOOL last_delim
= FALSE
;
761 if (last_delim
&& !ME_CallWordBreakProc( editor
, get_text( run
, 0 ), run
->len
, nOffset
, WB_ISDELIMITER
))
763 nOffset
= ME_CallWordBreakProc( editor
, get_text( run
, 0 ), run
->len
, nOffset
, WB_MOVEWORDRIGHT
);
764 if (nOffset
< run
->len
) break;
765 other_run
= run_next( run
);
768 last_delim
= ME_CallWordBreakProc( editor
, get_text( run
, 0 ), run
->len
, nOffset
- 1, WB_ISDELIMITER
);
774 para
= para_next( para
);
775 if (!para_next( para
))
777 if (cursor
->run
== run
) return FALSE
;
781 if (para
->nFlags
& MEPF_ROWSTART
) para
= para_next( para
);
782 if (cursor
->run
== run
) run
= para_first_run( para
);
790 cursor
->nOffset
= nOffset
;
796 ME_SelectByType(ME_TextEditor
*editor
, ME_SelectionType selectionType
)
798 /* pCursor[0] is the end of the selection
799 * pCursor[1] is the start of the selection (or the position selection anchor)
800 * pCursor[2] and [3] are the selection anchors that are backed up
801 * so they are kept when the selection changes for drag selection.
804 editor
->nSelectionType
= selectionType
;
805 switch(selectionType
)
810 ME_MoveCursorWords(editor
, &editor
->pCursors
[0], +1);
811 editor
->pCursors
[1] = editor
->pCursors
[0];
812 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
815 editor
->pCursors
[1] = editor
->pCursors
[0];
817 editor
->pCursors
[0].run
= para_end_run( editor
->pCursors
[0].para
);
818 editor
->pCursors
[0].para
= editor
->pCursors
[0].run
->para
;
819 editor
->pCursors
[0].nOffset
= editor
->pCursors
[0].run
->len
;
821 editor
->pCursors
[1].run
= para_first_run( editor
->pCursors
[1].para
);
822 editor
->pCursors
[1].nOffset
= 0;
826 ME_Row
*row
= row_from_cursor( editor
->pCursors
);
828 row_first_cursor( row
, editor
->pCursors
+ 1 );
829 row_end_cursor( row
, editor
->pCursors
, TRUE
);
833 /* Select everything with cursor anchored from the start of the text */
834 ME_SetCursorToStart(editor
, &editor
->pCursors
[1]);
835 ME_SetCursorToEnd(editor
, &editor
->pCursors
[0], TRUE
);
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(const ME_Cursor
*cursor
)
846 return cursor
->para
->nCharOfs
+ cursor
->run
->nCharOfs
+ cursor
->nOffset
;
849 /* Helper function for cursor_from_virtual_coords() to find paragraph within tables */
850 static ME_Paragraph
*pixel_pos_in_table_row( int x
, int y
, ME_Paragraph
*para
)
852 ME_Cell
*cell
, *next_cell
;
854 assert( para
->nFlags
& MEPF_ROWSTART
);
855 cell
= table_row_first_cell( para
);
858 /* find the cell we are in */
859 while ((next_cell
= cell_next( cell
)) != NULL
)
861 if (x
< next_cell
->pt
.x
)
863 para
= cell_first_para( cell
);
864 /* Found the cell, but there might be multiple paragraphs in
865 * the cell, so need to search down the cell for the paragraph. */
866 while (cell
== para_cell( para
))
868 if (y
< para
->pt
.y
+ para
->nHeight
)
870 if (para
->nFlags
& MEPF_ROWSTART
) return pixel_pos_in_table_row( x
, y
, para
);
873 para
= para_next( para
);
875 /* Past the end of the cell, so go back to the last cell paragraph */
876 return para_prev( para
);
880 /* Return table row delimiter */
881 para
= table_row_end( para
);
882 assert( para
->nFlags
& MEPF_ROWEND
);
883 assert( para
->fmt
.dwMask
& PFM_TABLEROWDELIMITER
);
884 assert( para
->fmt
.wEffects
& PFE_TABLEROWDELIMITER
);
888 static BOOL
row_cursor( ME_TextEditor
*editor
, ME_Row
*row
, int x
,
900 run
= row_first_run( row
);
905 if (x
>= run
->pt
.x
&& x
< run
->pt
.x
+ run
->nWidth
)
907 cursor
->nOffset
= ME_CharFromPoint( editor
, x
- run
->pt
.x
, run
, TRUE
, TRUE
);
909 cursor
->para
= run
->para
;
913 run
= row_next_run( row
, run
);
919 cursor
->para
= run
->para
;
923 /* Finds the run and offset from the pixel position.
925 * x & y are pixel positions in virtual coordinates into the rich edit control,
926 * so client coordinates must first be adjusted by the scroll position.
928 * If final_eop is TRUE consider the final end-of-paragraph.
930 * returns TRUE if the result was exactly under the cursor, otherwise returns
931 * FALSE, and result is set to the closest position to the coordinates.
933 static BOOL
cursor_from_virtual_coords( ME_TextEditor
*editor
, int x
, int y
,
934 ME_Cursor
*result
, BOOL final_eop
)
936 ME_Paragraph
*para
= editor_first_para( editor
);
937 ME_Row
*row
= NULL
, *next_row
;
940 x
-= editor
->rcFormat
.left
;
941 y
-= editor
->rcFormat
.top
;
944 for (; para_next( para
); para
= para_next( para
))
946 if (y
< para
->pt
.y
+ para
->nHeight
)
948 if (para
->nFlags
& MEPF_ROWSTART
)
949 para
= pixel_pos_in_table_row( x
, y
, para
);
951 row
= para_first_row( para
);
954 else if (para
->nFlags
& MEPF_ROWSTART
)
956 para
= table_row_end( para
);
962 if (y
< row
->pt
.y
+ row
->nHeight
) break;
963 next_row
= row_next( row
);
964 if (!next_row
) break;
968 if (!row
&& !final_eop
&& para_prev( para
))
970 /* The position is below the last paragraph, so the last row will be used
971 * rather than the end of the text, so the x position will be used to
972 * determine the offset closest to the pixel position. */
974 row
= para_end_row( para_prev( para
) );
977 if (row
) return row_cursor( editor
, row
, x
, result
) && isExact
;
979 ME_SetCursorToEnd(editor
, result
, TRUE
);
984 /* Sets the cursor to the position closest to the pixel position
986 * x & y are pixel positions in client coordinates.
988 * isExact will be set to TRUE if the run is directly under the pixel
989 * position, FALSE if it not, unless isExact is set to NULL.
991 * return FALSE if outside client area and the cursor is not set,
992 * otherwise TRUE is returned.
994 BOOL
ME_CharFromPos(ME_TextEditor
*editor
, int x
, int y
,
995 ME_Cursor
*cursor
, BOOL
*isExact
)
1000 ITextHost_TxGetClientRect(editor
->texthost
, &rc
);
1001 if (x
< 0 || y
< 0 || x
>= rc
.right
|| y
>= rc
.bottom
) {
1002 if (isExact
) *isExact
= FALSE
;
1005 x
+= editor
->horz_si
.nPos
;
1006 y
+= editor
->vert_si
.nPos
;
1007 bResult
= cursor_from_virtual_coords( editor
, x
, y
, cursor
, FALSE
);
1008 if (isExact
) *isExact
= bResult
;
1014 /* Extends the selection with a word, line, or paragraph selection type.
1016 * The selection is anchored by editor->pCursors[2-3] such that the text
1017 * between the anchors will remain selected, and one end will be extended.
1019 * editor->pCursors[0] should have the position to extend the selection to
1020 * before this function is called.
1022 * Nothing will be done if editor->nSelectionType equals stPosition.
1024 static void ME_ExtendAnchorSelection(ME_TextEditor
*editor
)
1026 ME_Cursor tmp_cursor
;
1027 int curOfs
, anchorStartOfs
, anchorEndOfs
;
1028 if (editor
->nSelectionType
== stPosition
|| editor
->nSelectionType
== stDocument
)
1030 curOfs
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1031 anchorStartOfs
= ME_GetCursorOfs(&editor
->pCursors
[3]);
1032 anchorEndOfs
= ME_GetCursorOfs(&editor
->pCursors
[2]);
1034 tmp_cursor
= editor
->pCursors
[0];
1035 editor
->pCursors
[0] = editor
->pCursors
[2];
1036 editor
->pCursors
[1] = editor
->pCursors
[3];
1037 if (curOfs
< anchorStartOfs
)
1039 /* Extend the left side of selection */
1040 editor
->pCursors
[1] = tmp_cursor
;
1041 switch (editor
->nSelectionType
)
1044 ME_MoveCursorWords(editor
, &editor
->pCursors
[1], -1);
1049 ME_Row
*row
= row_from_cursor( editor
->pCursors
+ 1 );
1050 row_first_cursor( row
, editor
->pCursors
+ 1 );
1055 editor
->pCursors
[1].run
= para_first_run( editor
->pCursors
[1].para
);
1056 editor
->pCursors
[1].nOffset
= 0;
1063 else if (curOfs
>= anchorEndOfs
)
1065 /* Extend the right side of selection */
1066 editor
->pCursors
[0] = tmp_cursor
;
1067 switch (editor
->nSelectionType
)
1070 ME_MoveCursorWords( editor
, &editor
->pCursors
[0], +1 );
1075 ME_Row
*row
= row_from_cursor( editor
->pCursors
);
1076 row_end_cursor( row
, editor
->pCursors
, TRUE
);
1081 editor
->pCursors
[0].run
= para_end_run( editor
->pCursors
[0].para
);
1082 editor
->pCursors
[0].para
= editor
->pCursors
[0].run
->para
;
1083 editor
->pCursors
[0].nOffset
= editor
->pCursors
[0].run
->len
;
1092 void ME_LButtonDown(ME_TextEditor
*editor
, int x
, int y
, int clickNum
)
1094 ME_Cursor tmp_cursor
;
1095 BOOL is_selection
= FALSE
, is_shift
;
1097 editor
->nUDArrowX
= -1;
1099 x
+= editor
->horz_si
.nPos
;
1100 y
+= editor
->vert_si
.nPos
;
1102 tmp_cursor
= editor
->pCursors
[0];
1103 is_selection
= ME_IsSelection(editor
);
1104 is_shift
= GetKeyState(VK_SHIFT
) < 0;
1106 cursor_from_virtual_coords( editor
, x
, y
, &editor
->pCursors
[0], FALSE
);
1108 if (x
>= editor
->rcFormat
.left
|| is_shift
)
1112 editor
->pCursors
[1] = editor
->pCursors
[0];
1114 if (x
>= editor
->rcFormat
.left
)
1115 ME_SelectByType(editor
, stWord
);
1117 ME_SelectByType(editor
, stParagraph
);
1118 } else if (clickNum
% 2 == 0) {
1119 ME_SelectByType(editor
, stWord
);
1121 ME_SelectByType(editor
, stParagraph
);
1126 editor
->nSelectionType
= stPosition
;
1127 editor
->pCursors
[1] = editor
->pCursors
[0];
1129 else if (!is_selection
)
1131 editor
->nSelectionType
= stPosition
;
1132 editor
->pCursors
[1] = tmp_cursor
;
1134 else if (editor
->nSelectionType
!= stPosition
)
1136 ME_ExtendAnchorSelection(editor
);
1142 ME_SelectByType(editor
, stLine
);
1143 } else if (clickNum
% 2 == 0 || is_shift
) {
1144 ME_SelectByType(editor
, stParagraph
);
1146 ME_SelectByType(editor
, stDocument
);
1149 ME_InvalidateSelection(editor
);
1150 update_caret(editor
);
1151 ME_SendSelChange(editor
);
1154 void ME_MouseMove(ME_TextEditor
*editor
, int x
, int y
)
1156 ME_Cursor tmp_cursor
;
1158 if (editor
->nSelectionType
== stDocument
)
1160 x
+= editor
->horz_si
.nPos
;
1161 y
+= editor
->vert_si
.nPos
;
1163 tmp_cursor
= editor
->pCursors
[0];
1164 /* FIXME: do something with the return value of cursor_from_virtual_coords */
1165 cursor_from_virtual_coords( editor
, x
, y
, &tmp_cursor
, TRUE
);
1167 ME_InvalidateSelection(editor
);
1168 editor
->pCursors
[0] = tmp_cursor
;
1169 ME_ExtendAnchorSelection(editor
);
1171 if (editor
->nSelectionType
!= stPosition
&&
1172 memcmp(&editor
->pCursors
[1], &editor
->pCursors
[3], sizeof(ME_Cursor
)))
1173 /* The scroll the cursor towards the other end, since it was the one
1174 * extended by ME_ExtendAnchorSelection */
1175 editor_ensure_visible( editor
, &editor
->pCursors
[1] );
1177 editor_ensure_visible( editor
, &editor
->pCursors
[0] );
1179 ME_InvalidateSelection(editor
);
1180 update_caret(editor
);
1181 ME_SendSelChange(editor
);
1184 static int ME_GetXForArrow(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1186 ME_Run
*run
= pCursor
->run
;
1189 if (editor
->nUDArrowX
!= -1)
1190 x
= editor
->nUDArrowX
;
1194 x
+= ME_PointFromChar( editor
, run
, pCursor
->nOffset
, TRUE
);
1195 editor
->nUDArrowX
= x
;
1201 static void cursor_move_line( ME_TextEditor
*editor
, ME_Cursor
*cursor
, BOOL up
, BOOL extend
)
1203 ME_Paragraph
*old_para
= cursor
->para
, *new_para
;
1204 ME_Row
*row
= row_from_cursor( cursor
);
1205 int x
= ME_GetXForArrow( editor
, cursor
);
1209 /* start of the previous row */
1210 row
= row_prev_all_paras( row
);
1213 if (extend
) ME_SetCursorToStart( editor
, cursor
);
1216 new_para
= row_para( row
);
1217 if (old_para
->nFlags
& MEPF_ROWEND
||
1218 (para_cell( old_para
) && para_cell( old_para
) != para_cell( new_para
)))
1220 /* Brought out of a cell */
1221 new_para
= para_prev( table_row_start( old_para
));
1222 if (!new_para
) return; /* At the top, so don't go anywhere. */
1223 row
= para_first_row( new_para
);
1225 if (new_para
->nFlags
& MEPF_ROWEND
)
1227 /* Brought into a table row */
1228 ME_Cell
*cell
= table_row_end_cell( new_para
);
1229 while (x
< cell
->pt
.x
&& cell_prev( cell
))
1230 cell
= cell_prev( cell
);
1231 if (cell_next( cell
)) /* else - we are still at the end of the row */
1232 row
= para_end_row( cell_end_para( cell
) );
1237 /* start of the next row */
1238 row
= row_next_all_paras( row
);
1241 if (extend
) ME_SetCursorToEnd( editor
, cursor
, TRUE
);
1244 new_para
= row_para( row
);
1245 if (old_para
->nFlags
& MEPF_ROWSTART
||
1246 (para_cell( old_para
) && para_cell( old_para
) != para_cell( new_para
)))
1248 /* Brought out of a cell */
1249 new_para
= para_next( table_row_end( old_para
) );
1250 if (!para_next( new_para
)) return; /* At the bottom, so don't go anywhere. */
1251 row
= para_first_row( new_para
);
1253 if (new_para
->nFlags
& MEPF_ROWSTART
)
1255 /* Brought into a table row */
1256 ME_Cell
*cell
= table_row_first_cell( new_para
);
1257 while (cell_next( cell
) && x
>= cell_next( cell
)->pt
.x
)
1258 cell
= cell_next( cell
);
1259 row
= para_first_row( cell_first_para( cell
) );
1264 row_cursor( editor
, row
, x
, cursor
);
1267 static void ME_ArrowPageUp( ME_TextEditor
*editor
, ME_Cursor
*cursor
)
1269 ME_Row
*row
= para_first_row( editor_first_para( editor
) ), *last_row
;
1270 int x
, yd
, old_scroll_pos
= editor
->vert_si
.nPos
;
1272 if (editor
->vert_si
.nPos
< row
->nHeight
)
1274 ME_SetCursorToStart( editor
, cursor
);
1275 /* Native clears seems to clear this x value on page up at the top
1276 * of the text, but not on page down at the end of the text.
1277 * Doesn't make sense, but we try to be bug for bug compatible. */
1278 editor
->nUDArrowX
= -1;
1282 x
= ME_GetXForArrow( editor
, cursor
);
1283 row
= row_from_cursor( cursor
);
1285 ME_ScrollUp( editor
, editor
->sizeWindow
.cy
);
1286 /* Only move the cursor by the amount scrolled. */
1287 yd
= cursor
->para
->pt
.y
+ row
->pt
.y
+ editor
->vert_si
.nPos
- old_scroll_pos
;
1290 while ((row
= row_prev_all_paras( row
)))
1292 if (row_para( row
)->pt
.y
+ row
->pt
.y
< yd
) break;
1296 row_cursor( editor
, last_row
, x
, cursor
);
1300 static void ME_ArrowPageDown( ME_TextEditor
*editor
, ME_Cursor
*cursor
)
1302 ME_Row
*row
= para_end_row( para_prev( editor_end_para( editor
) ) ), *last_row
;
1303 int x
, yd
, old_scroll_pos
= editor
->vert_si
.nPos
;
1305 x
= ME_GetXForArrow( editor
, cursor
);
1307 if (editor
->vert_si
.nPos
>= row_para( row
)->pt
.y
+ row
->pt
.y
- editor
->sizeWindow
.cy
)
1308 ME_SetCursorToEnd( editor
, cursor
, FALSE
);
1311 row
= row_from_cursor( cursor
);
1313 /* For native richedit controls:
1314 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1315 * v4.1 can scroll past this position here. */
1316 ME_ScrollDown( editor
, editor
->sizeWindow
.cy
);
1317 /* Only move the cursor by the amount scrolled. */
1318 yd
= cursor
->para
->pt
.y
+ row
->pt
.y
+ editor
->vert_si
.nPos
- old_scroll_pos
;
1321 while ((row
= row_next_all_paras( row
)))
1323 if (row_para( row
)->pt
.y
+ row
->pt
.y
>= yd
) break;
1327 row_cursor( editor
, last_row
, x
, cursor
);
1331 static void ME_ArrowHome( ME_TextEditor
*editor
, ME_Cursor
*cursor
)
1333 ME_Row
*row
= row_from_cursor( cursor
);
1335 row_first_cursor( row
, cursor
);
1338 static void ME_ArrowCtrlHome(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1340 ME_SetCursorToStart(editor
, pCursor
);
1343 static void ME_ArrowEnd( ME_TextEditor
*editor
, ME_Cursor
*cursor
)
1345 ME_Row
*row
= row_from_cursor( cursor
);
1347 row_end_cursor( row
, cursor
, FALSE
);
1350 static void ME_ArrowCtrlEnd(ME_TextEditor
*editor
, ME_Cursor
*pCursor
)
1352 ME_SetCursorToEnd(editor
, pCursor
, FALSE
);
1355 BOOL
ME_IsSelection(ME_TextEditor
*editor
)
1357 return editor
->pCursors
[0].run
!= editor
->pCursors
[1].run
||
1358 editor
->pCursors
[0].nOffset
!= editor
->pCursors
[1].nOffset
;
1361 void ME_DeleteSelection(ME_TextEditor
*editor
)
1364 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
1365 int nEndCursor
= nStartCursor
^ 1;
1366 ME_DeleteTextAtCursor(editor
, nStartCursor
, to
- from
);
1367 editor
->pCursors
[nEndCursor
] = editor
->pCursors
[nStartCursor
];
1370 ME_Style
*ME_GetSelectionInsertStyle(ME_TextEditor
*editor
)
1372 return style_get_insert_style( editor
, editor
->pCursors
);
1375 void ME_SendSelChange(ME_TextEditor
*editor
)
1379 sc
.nmhdr
.hwndFrom
= NULL
;
1380 sc
.nmhdr
.idFrom
= 0;
1381 sc
.nmhdr
.code
= EN_SELCHANGE
;
1382 ME_GetSelectionOfs(editor
, &sc
.chrg
.cpMin
, &sc
.chrg
.cpMax
);
1383 sc
.seltyp
= SEL_EMPTY
;
1384 if (sc
.chrg
.cpMin
!= sc
.chrg
.cpMax
)
1385 sc
.seltyp
|= SEL_TEXT
;
1386 if (sc
.chrg
.cpMin
< sc
.chrg
.cpMax
+1) /* what were RICHEDIT authors thinking ? */
1387 sc
.seltyp
|= SEL_MULTICHAR
;
1389 if (sc
.chrg
.cpMin
!= editor
->notified_cr
.cpMin
|| sc
.chrg
.cpMax
!= editor
->notified_cr
.cpMax
)
1391 ME_ClearTempStyle(editor
);
1393 editor
->notified_cr
= sc
.chrg
;
1395 if (editor
->nEventMask
& ENM_SELCHANGE
)
1397 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1398 sc
.chrg
.cpMin
, sc
.chrg
.cpMax
, sc
.seltyp
,
1399 (sc
.seltyp
& SEL_TEXT
) ? "SEL_TEXT" : "",
1400 (sc
.seltyp
& SEL_MULTICHAR
) ? "SEL_MULTICHAR" : "");
1401 ITextHost_TxNotify(editor
->texthost
, sc
.nmhdr
.code
, &sc
);
1407 ME_ArrowKey(ME_TextEditor
*editor
, int nVKey
, BOOL extend
, BOOL ctrl
)
1410 ME_Cursor
*p
= &editor
->pCursors
[nCursor
];
1411 ME_Cursor tmp_curs
= *p
;
1412 BOOL success
= FALSE
;
1414 ME_CheckCharOffsets(editor
);
1418 success
= ME_MoveCursorWords(editor
, &tmp_curs
, -1);
1420 success
= ME_MoveCursorChars(editor
, &tmp_curs
, -1, extend
);
1424 success
= ME_MoveCursorWords(editor
, &tmp_curs
, +1);
1426 success
= ME_MoveCursorChars(editor
, &tmp_curs
, +1, extend
);
1429 cursor_move_line( editor
, &tmp_curs
, TRUE
, extend
);
1432 cursor_move_line( editor
, &tmp_curs
, FALSE
, extend
);
1435 ME_ArrowPageUp(editor
, &tmp_curs
);
1438 ME_ArrowPageDown(editor
, &tmp_curs
);
1442 ME_ArrowCtrlHome(editor
, &tmp_curs
);
1444 ME_ArrowHome(editor
, &tmp_curs
);
1449 ME_ArrowCtrlEnd(editor
, &tmp_curs
);
1451 ME_ArrowEnd(editor
, &tmp_curs
);
1456 editor
->pCursors
[1] = tmp_curs
;
1459 ME_InvalidateSelection(editor
);
1462 editor_ensure_visible( editor
, &tmp_curs
);
1463 update_caret(editor
);
1464 ME_SendSelChange(editor
);