win32u: Respect per-monitor thread dpi awareness when getting window from point.
[wine.git] / dlls / riched20 / caret.c
blob9b94bcb22498286b435aeb1db63c5fd5495e3d39
1 /*
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
23 #include "editor.h"
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 );
31 cursor->nOffset = 0;
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, LONG *from, LONG *to)
44 *from = ME_GetCursorOfs(&editor->pCursors[0]);
45 *to = ME_GetCursorOfs(&editor->pCursors[1]);
47 if (*from > *to)
49 LONG tmp = *from;
50 *from = *to;
51 *to = tmp;
52 return 1;
54 return 0;
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)
68 swap = TRUE;
71 if (!swap)
73 *from = &editor->pCursors[0];
74 *to = &editor->pCursors[1];
75 return 0;
76 } else {
77 *from = &editor->pCursors[1];
78 *to = &editor->pCursors[0];
79 return 1;
83 int ME_GetTextLength(ME_TextEditor *editor)
85 ME_Cursor cursor;
86 ME_SetCursorToEnd(editor, &cursor, FALSE);
87 return ME_GetCursorOfs(&cursor);
91 int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how)
93 int length;
95 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
96 return E_INVALIDARG;
97 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
98 return E_INVALIDARG;
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 */
111 CPINFO cpinfo;
113 if (how->codepage == 1200)
114 return length * 2;
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);
120 return E_INVALIDARG;
122 return length;
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 */
138 if (from < 0)
139 from = -1;
140 if (to < 0)
141 to = -1;
143 /* select all */
144 if (from == 0 && to == -1)
146 ME_SetCursorToStart(editor, &editor->pCursors[1]);
147 ME_SetCursorToEnd(editor, &editor->pCursors[0], TRUE);
148 return len + 1;
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))
155 selectionEnd = 1;
157 else
159 /* if from is negative and to is positive then selection is */
160 /* deselected and caret moved to end of the current selection */
161 if (from < 0)
163 LONG start, end;
164 ME_GetSelectionOfs(editor, &start, &end);
165 if (start != end)
167 if (end > len)
169 editor->pCursors[0].nOffset = 0;
170 end --;
172 editor->pCursors[1] = editor->pCursors[0];
174 return end;
177 /* adjust to if it's a negative value */
178 if (to < 0)
179 to = len + 1;
181 /* flip from and to if they are reversed */
182 if (from>to)
184 int tmp = from;
185 from = to;
186 to = tmp;
189 /* after fiddling with the values, we find from > len && to > len */
190 if (from > len)
191 selectionEnd = 1;
192 /* special case with to too big */
193 else if (to > len)
194 to = len + 1;
197 if (selectionEnd)
199 ME_SetCursorToEnd(editor, &editor->pCursors[0], FALSE);
200 editor->pCursors[1] = editor->pCursors[0];
201 return len;
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)
212 if (to > len)
213 editor->pCursors[0].nOffset = editor->pCursors[0].run->len;
214 else
215 editor->pCursors[0].nOffset = 0;
217 return to;
221 void cursor_coords( ME_TextEditor *editor, ME_Cursor *cursor,
222 int *x, int *y, int *height )
224 ME_Row *row;
225 ME_Run *run = cursor->run;
226 ME_Paragraph *para = cursor->para;
227 ME_Run *size_run = run, *prev;
228 ME_Context c;
229 int run_x;
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 );
248 return;
251 void create_caret(ME_TextEditor *editor)
253 int x, y, height;
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)
279 int x, y, height;
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);
288 show_caret(editor);
290 else
291 hide_caret(editor);
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 );
299 int shift = 0;
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;
307 start_para = c.para;
309 if (!bForce)
311 table_protect_partial_deletion( editor, &c, &nChars );
312 if (nChars == 0) return FALSE;
315 while (nChars > 0)
317 ME_Run *run;
318 cursor_from_char_ofs( editor, nOfs + nChars, &c );
319 if (!c.nOffset)
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;
327 run = c.run;
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 &&
336 run->nCharOfs);
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;
354 continue;
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;
363 continue;
365 else
367 ME_Cursor cursor;
368 int nCharsToDelete = min(nChars, c.nOffset);
369 int i;
371 c.nOffset -= nCharsToDelete;
373 para_mark_rewrap( editor, c.run->para );
375 cursor = c;
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;
402 else
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( editor, 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 ));
428 shift = 0;
429 continue;
432 if (delete_all) editor_set_default_para_fmt( editor, &start_para->fmt );
433 return TRUE;
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],
442 nChars, FALSE);
445 static struct re_object* create_re_object(const REOBJECT *reo, ME_Run *run)
447 struct re_object *reobj = malloc(sizeof(*reobj));
449 if (!reobj)
451 WARN("Fail to allocate re_object.\n");
452 return NULL;
454 ME_CopyReObject(&reobj->obj, reo, REO_GETOBJ_ALL_INTERFACES);
455 reobj->run = run;
456 return reobj;
459 HRESULT editor_insert_oleobj(ME_TextEditor *editor, const REOBJECT *reo)
461 ME_Run *run, *prev;
462 const WCHAR space = ' ';
463 struct re_object *reobj_prev = NULL;
464 ME_Cursor *cursor, cursor_from_ofs;
465 ME_Style *style;
466 HRESULT hr;
467 SIZEL extent;
469 if (editor->lpOleCallback)
471 hr = IRichEditOleCallback_QueryInsertObject(editor->lpOleCallback, (LPCLSID)&reo->clsid, reo->pstg, REO_CP_SELECTION);
472 if (hr != S_OK)
473 return hr;
476 extent = reo->sizel;
477 if (!extent.cx && !extent.cy && reo->poleobj)
479 hr = IOleObject_GetExtent( reo->poleobj, DVASPECT_CONTENT, &extent );
480 if (FAILED(hr))
482 extent.cx = 0;
483 extent.cy = 0;
487 if (reo->cp == REO_CP_SELECTION)
488 cursor = editor->pCursors;
489 else
491 cursor_from_char_ofs( editor, reo->cp, &cursor_from_ofs );
492 cursor = &cursor_from_ofs;
494 style = style_get_insert_style( editor, cursor );
496 if (ME_IsSelection(editor))
497 ME_DeleteSelection(editor);
499 run = run_insert( editor, cursor, style, &space, 1, MERF_GRAPHICS );
501 run->reobj = create_re_object( reo, run );
502 run->reobj->obj.sizel = extent;
504 prev = run;
505 while ((prev = run_prev_all_paras( prev )))
507 if (prev->reobj)
509 reobj_prev = prev->reobj;
510 break;
513 if (reobj_prev)
514 list_add_after(&reobj_prev->entry, &run->reobj->entry);
515 else
516 list_add_head(&editor->reobj_list, &run->reobj->entry);
518 ME_ReleaseStyle( style );
519 return S_OK;
523 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
525 const WCHAR space = ' ';
526 ME_Cursor *cursor = editor->pCursors + nCursor;
527 ME_Style *style = style_get_insert_style( editor, cursor );
529 /* FIXME no no no */
530 if (ME_IsSelection(editor))
531 ME_DeleteSelection(editor);
533 run_insert( editor, cursor, style, &space, 1, MERF_ENDROW );
535 ME_ReleaseStyle( style );
539 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
540 const WCHAR *str, int len, ME_Style *style)
542 const WCHAR *pos;
543 ME_Cursor *cursor = editor->pCursors + nCursor;
544 int oldLen;
546 /* FIXME really HERE ? */
547 if (ME_IsSelection(editor))
548 ME_DeleteSelection(editor);
550 oldLen = ME_GetTextLength(editor);
552 /* text operations set modified state */
553 editor->nModifyStep = 1;
555 assert(style);
557 if (len == -1) len = lstrlenW( str );
559 /* grow the text limit to fit our text */
560 if (editor->nTextLimit < oldLen + len) editor->nTextLimit = oldLen + len;
562 pos = str;
564 while (len)
566 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
567 while (pos - str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
568 pos++;
570 if (pos != str) /* handle text */
571 run_insert( editor, cursor, style, str, pos - str, 0 );
572 else if (*pos == '\t') /* handle tabs */
574 const WCHAR tab = '\t';
575 run_insert( editor, cursor, style, &tab, 1, MERF_TAB );
576 pos++;
578 else /* handle EOLs */
580 ME_Run *end_run, *run, *prev;
581 ME_Paragraph *new_para;
582 int eol_len = 0;
584 /* Check if new line is allowed for this control */
585 if (!(editor->props & TXTBIT_MULTILINE))
586 break;
588 /* Find number of CR and LF in end of paragraph run */
589 if (*pos =='\r')
591 if (len > 1 && pos[1] == '\n')
592 eol_len = 2;
593 else if (len > 2 && pos[1] == '\r' && pos[2] == '\n')
594 eol_len = 3;
595 else
596 eol_len = 1;
598 else
600 assert(*pos == '\n');
601 eol_len = 1;
603 pos += eol_len;
605 if (!editor->bEmulateVersion10 && eol_len == 3)
607 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
608 const WCHAR space = ' ';
609 run_insert( editor, cursor, style, &space, 1, 0 );
611 else
613 const WCHAR cr = '\r', *eol_str = str;
615 if (!editor->bEmulateVersion10)
617 eol_str = &cr;
618 eol_len = 1;
621 if (cursor->nOffset == cursor->run->len)
623 run = run_next( cursor->run );
624 if (!run) run = cursor->run;
626 else
628 if (cursor->nOffset) run_split( editor, cursor );
629 run = cursor->run;
632 new_para = para_split( editor, run, style, eol_str, eol_len, 0 );
633 end_run = para_end_run( para_prev( new_para ) );
635 /* Move any cursors that were at the end of the previous run to the beginning of the new para */
636 prev = run_prev( end_run );
637 if (prev)
639 int i;
640 for (i = 0; i < editor->nCursors; i++)
642 if (editor->pCursors[i].run == prev &&
643 editor->pCursors[i].nOffset == prev->len)
645 editor->pCursors[i].para = new_para;
646 editor->pCursors[i].run = run;
647 editor->pCursors[i].nOffset = 0;
654 len -= pos - str;
655 str = pos;
659 /* Move the cursor nRelOfs characters (either forwards or backwards)
660 * If final_eop is TRUE, allow moving the cursor to the end of the final eop.
662 * returns the actual number of characters moved.
664 int ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs, BOOL final_eop)
666 cursor->nOffset += nRelOfs;
667 if (cursor->nOffset < 0)
669 cursor->nOffset += cursor->run->nCharOfs;
670 if (cursor->nOffset >= 0)
672 /* new offset in the same paragraph */
673 do {
674 cursor->run = run_prev( cursor->run );
675 } while (cursor->nOffset < cursor->run->nCharOfs);
676 cursor->nOffset -= cursor->run->nCharOfs;
677 return nRelOfs;
680 cursor->nOffset += cursor->para->nCharOfs;
681 if (cursor->nOffset <= 0)
683 /* moved to the start of the text */
684 nRelOfs -= cursor->nOffset;
685 ME_SetCursorToStart(editor, cursor);
686 return nRelOfs;
689 /* new offset in a previous paragraph */
690 do {
691 cursor->para = para_prev( cursor->para );
692 } while (cursor->nOffset < cursor->para->nCharOfs);
693 cursor->nOffset -= cursor->para->nCharOfs;
695 cursor->run = para_end_run( cursor->para );
696 while (cursor->nOffset < cursor->run->nCharOfs)
697 cursor->run = run_prev( cursor->run );
698 cursor->nOffset -= cursor->run->nCharOfs;
700 else if (cursor->nOffset >= cursor->run->len)
702 ME_Paragraph *next_para;
703 int new_offset;
705 new_offset = ME_GetCursorOfs(cursor);
706 next_para = para_next( cursor->para );
707 if (new_offset < next_para->nCharOfs)
709 /* new offset in the same paragraph */
710 do {
711 cursor->nOffset -= cursor->run->len;
712 cursor->run = run_next( cursor->run );
713 } while (cursor->nOffset >= cursor->run->len);
714 return nRelOfs;
717 if (new_offset >= ME_GetTextLength(editor) + (final_eop ? 1 : 0))
719 /* new offset at the end of the text */
720 ME_SetCursorToEnd(editor, cursor, final_eop);
721 nRelOfs -= new_offset - (ME_GetTextLength(editor) + (final_eop ? 1 : 0));
722 return nRelOfs;
725 /* new offset in a following paragraph */
726 do {
727 cursor->para = next_para;
728 next_para = para_next( next_para );
729 } while (new_offset >= next_para->nCharOfs);
731 cursor->nOffset = new_offset - cursor->para->nCharOfs;
732 cursor->run = para_first_run( cursor->para );
733 while (cursor->nOffset >= cursor->run->len)
735 cursor->nOffset -= cursor->run->len;
736 cursor->run = run_next( cursor->run );
738 } /* else new offset is in the same run */
739 return nRelOfs;
743 BOOL
744 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
746 ME_Run *run = cursor->run, *other_run;
747 ME_Paragraph *para = cursor->para;
748 int nOffset = cursor->nOffset;
750 if (nRelOfs == -1)
752 /* Backward movement */
753 while (TRUE)
755 nOffset = ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset, WB_MOVEWORDLEFT );
756 if (nOffset) break;
757 other_run = run_prev( run );
758 if (other_run)
760 if (ME_CallWordBreakProc( editor, get_text( other_run, 0 ), other_run->len, other_run->len - 1, WB_ISDELIMITER )
761 && !(run->nFlags & MERF_ENDPARA)
762 && !(cursor->run == run && cursor->nOffset == 0)
763 && !ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, 0, WB_ISDELIMITER ))
764 break;
765 run = other_run;
766 nOffset = other_run->len;
768 else
770 if (cursor->run == run && cursor->nOffset == 0)
772 para = run->para;
773 /* Skip empty start of table row paragraph */
774 if (para_prev( para ) && para_prev( para )->nFlags & MEPF_ROWSTART)
775 para = para_prev( para );
776 /* Paragraph breaks are treated as separate words */
777 if (!para_prev( para )) return FALSE;
778 para = para_prev( para );
779 run = para_end_run( para );
781 break;
785 else
787 /* Forward movement */
788 BOOL last_delim = FALSE;
790 while (TRUE)
792 if (last_delim && !ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset, WB_ISDELIMITER ))
793 break;
794 nOffset = ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset, WB_MOVEWORDRIGHT );
795 if (nOffset < run->len) break;
796 other_run = run_next( run );
797 if (other_run)
799 last_delim = ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset - 1, WB_ISDELIMITER );
800 run = other_run;
801 nOffset = 0;
803 else
805 ME_Paragraph *other_para = para_next( para );
806 if (!para_next( other_para ))
808 if (cursor->run == run) return FALSE;
809 nOffset = 0;
810 break;
812 if (other_para->nFlags & MEPF_ROWSTART) other_para = para_next( other_para );
813 if (cursor->run == run) {
814 para = other_para;
815 run = para_first_run( para );
817 nOffset = 0;
818 break;
822 cursor->para = para;
823 cursor->run = run;
824 cursor->nOffset = nOffset;
825 return TRUE;
829 static void
830 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
832 /* pCursor[0] is the end of the selection
833 * pCursor[1] is the start of the selection (or the position selection anchor)
834 * pCursor[2] and [3] are the selection anchors that are backed up
835 * so they are kept when the selection changes for drag selection.
838 editor->nSelectionType = selectionType;
839 switch(selectionType)
841 case stPosition:
842 break;
843 case stWord:
844 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
845 editor->pCursors[1] = editor->pCursors[0];
846 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
847 break;
848 case stParagraph:
849 editor->pCursors[1] = editor->pCursors[0];
851 editor->pCursors[0].run = para_end_run( editor->pCursors[0].para );
852 editor->pCursors[0].para = editor->pCursors[0].run->para;
853 editor->pCursors[0].nOffset = editor->pCursors[0].run->len;
855 editor->pCursors[1].run = para_first_run( editor->pCursors[1].para );
856 editor->pCursors[1].nOffset = 0;
857 break;
858 case stLine:
860 ME_Row *row = row_from_cursor( editor->pCursors );
862 row_first_cursor( row, editor->pCursors + 1 );
863 row_end_cursor( row, editor->pCursors, TRUE );
864 break;
866 case stDocument:
867 /* Select everything with cursor anchored from the start of the text */
868 ME_SetCursorToStart(editor, &editor->pCursors[1]);
869 ME_SetCursorToEnd(editor, &editor->pCursors[0], TRUE);
870 break;
871 default: assert(0);
873 /* Store the anchor positions for extending the selection. */
874 editor->pCursors[2] = editor->pCursors[0];
875 editor->pCursors[3] = editor->pCursors[1];
878 int ME_GetCursorOfs(const ME_Cursor *cursor)
880 return cursor->para->nCharOfs + cursor->run->nCharOfs + cursor->nOffset;
883 /* Helper function for cursor_from_virtual_coords() to find paragraph within tables */
884 static ME_Paragraph *pixel_pos_in_table_row( int x, int y, ME_Paragraph *para )
886 ME_Cell *cell, *next_cell;
888 assert( para->nFlags & MEPF_ROWSTART );
889 cell = table_row_first_cell( para );
890 assert( cell );
892 /* find the cell we are in */
893 while ((next_cell = cell_next( cell )) != NULL)
895 if (x < next_cell->pt.x)
897 para = cell_first_para( cell );
898 /* Found the cell, but there might be multiple paragraphs in
899 * the cell, so need to search down the cell for the paragraph. */
900 while (cell == para_cell( para ))
902 if (y < para->pt.y + para->nHeight)
904 if (para->nFlags & MEPF_ROWSTART) return pixel_pos_in_table_row( x, y, para );
905 else return para;
907 para = para_next( para );
909 /* Past the end of the cell, so go back to the last cell paragraph */
910 return para_prev( para );
912 cell = next_cell;
914 /* Return table row delimiter */
915 para = table_row_end( para );
916 assert( para->nFlags & MEPF_ROWEND );
917 assert( para->fmt.dwMask & PFM_TABLEROWDELIMITER );
918 assert( para->fmt.wEffects & PFE_TABLEROWDELIMITER );
919 return para;
922 static BOOL row_cursor( ME_TextEditor *editor, ME_Row *row, int x,
923 ME_Cursor *cursor )
925 ME_Run *run, *last;
926 BOOL exact = TRUE;
928 if (x < row->pt.x)
930 x = row->pt.x;
931 exact = FALSE;
934 run = row_first_run( row );
935 assert( run );
936 cursor->nOffset = 0;
939 if (x >= run->pt.x && x < run->pt.x + run->nWidth)
941 cursor->nOffset = ME_CharFromPoint( editor, x - run->pt.x, run, TRUE, TRUE );
942 cursor->run = run;
943 cursor->para = run->para;
944 return exact;
946 last = run;
947 run = row_next_run( row, run );
948 } while (run);
950 run = last;
952 cursor->run = run;
953 cursor->para = run->para;
954 return FALSE;
957 /* Finds the run and offset from the pixel position.
959 * x & y are pixel positions in virtual coordinates into the rich edit control,
960 * so client coordinates must first be adjusted by the scroll position.
962 * If final_eop is TRUE consider the final end-of-paragraph.
964 * returns TRUE if the result was exactly under the cursor, otherwise returns
965 * FALSE, and result is set to the closest position to the coordinates.
967 static BOOL cursor_from_virtual_coords( ME_TextEditor *editor, int x, int y,
968 ME_Cursor *result, BOOL final_eop )
970 ME_Paragraph *para = editor_first_para( editor );
971 ME_Row *row = NULL, *next_row;
972 BOOL isExact = TRUE;
974 x -= editor->rcFormat.left;
975 y -= editor->rcFormat.top;
977 /* find paragraph */
978 for (; para_next( para ); para = para_next( para ))
980 if (y < para->pt.y + para->nHeight)
982 if (para->nFlags & MEPF_ROWSTART)
983 para = pixel_pos_in_table_row( x, y, para );
984 y -= para->pt.y;
985 row = para_first_row( para );
986 break;
988 else if (para->nFlags & MEPF_ROWSTART)
990 para = table_row_end( para );
993 /* find row */
994 while (row)
996 if (y < row->pt.y + row->nHeight) break;
997 next_row = row_next( row );
998 if (!next_row) break;
999 row = next_row;
1002 if (!row && !final_eop && para_prev( para ))
1004 /* The position is below the last paragraph, so the last row will be used
1005 * rather than the end of the text, so the x position will be used to
1006 * determine the offset closest to the pixel position. */
1007 isExact = FALSE;
1008 row = para_end_row( para_prev( para ) );
1011 if (row) return row_cursor( editor, row, x, result ) && isExact;
1013 ME_SetCursorToEnd(editor, result, TRUE);
1014 return FALSE;
1018 /* Sets the cursor to the position closest to the pixel position
1020 * x & y are pixel positions in client coordinates.
1022 * return TRUE if the run is directly under the pixel
1023 * position, FALSE if it not.
1025 BOOL cursor_from_coords( ME_TextEditor *editor, int x, int y, ME_Cursor *cursor )
1027 x += editor->horz_si.nPos;
1028 y += editor->vert_si.nPos;
1029 return cursor_from_virtual_coords( editor, x, y, cursor, FALSE );
1033 /* Extends the selection with a word, line, or paragraph selection type.
1035 * The selection is anchored by editor->pCursors[2-3] such that the text
1036 * between the anchors will remain selected, and one end will be extended.
1038 * editor->pCursors[0] should have the position to extend the selection to
1039 * before this function is called.
1041 * Nothing will be done if editor->nSelectionType equals stPosition.
1043 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1045 ME_Cursor tmp_cursor;
1046 int curOfs, anchorStartOfs, anchorEndOfs;
1047 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1048 return;
1049 curOfs = ME_GetCursorOfs(&editor->pCursors[0]);
1050 anchorStartOfs = ME_GetCursorOfs(&editor->pCursors[3]);
1051 anchorEndOfs = ME_GetCursorOfs(&editor->pCursors[2]);
1053 tmp_cursor = editor->pCursors[0];
1054 editor->pCursors[0] = editor->pCursors[2];
1055 editor->pCursors[1] = editor->pCursors[3];
1056 if (curOfs < anchorStartOfs)
1058 /* Extend the left side of selection */
1059 editor->pCursors[1] = tmp_cursor;
1060 switch (editor->nSelectionType)
1062 case stWord:
1063 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1064 break;
1066 case stLine:
1068 ME_Row *row = row_from_cursor( editor->pCursors + 1 );
1069 row_first_cursor( row, editor->pCursors + 1 );
1070 break;
1073 case stParagraph:
1074 editor->pCursors[1].run = para_first_run( editor->pCursors[1].para );
1075 editor->pCursors[1].nOffset = 0;
1076 break;
1078 default:
1079 break;
1082 else if (curOfs >= anchorEndOfs)
1084 /* Extend the right side of selection */
1085 editor->pCursors[0] = tmp_cursor;
1086 switch (editor->nSelectionType)
1088 case stWord:
1089 ME_MoveCursorWords( editor, &editor->pCursors[0], +1 );
1090 break;
1092 case stLine:
1094 ME_Row *row = row_from_cursor( editor->pCursors );
1095 row_end_cursor( row, editor->pCursors, TRUE );
1096 break;
1099 case stParagraph:
1100 editor->pCursors[0].run = para_end_run( editor->pCursors[0].para );
1101 editor->pCursors[0].para = editor->pCursors[0].run->para;
1102 editor->pCursors[0].nOffset = editor->pCursors[0].run->len;
1103 break;
1105 default:
1106 break;
1111 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1113 ME_Cursor tmp_cursor;
1114 BOOL is_selection = FALSE, is_shift;
1116 editor->nUDArrowX = -1;
1118 x += editor->horz_si.nPos;
1119 y += editor->vert_si.nPos;
1121 tmp_cursor = editor->pCursors[0];
1122 is_selection = ME_IsSelection(editor);
1123 is_shift = GetKeyState(VK_SHIFT) < 0;
1125 cursor_from_virtual_coords( editor, x, y, &editor->pCursors[0], FALSE );
1127 if (x >= editor->rcFormat.left || is_shift)
1129 if (clickNum > 1)
1131 editor->pCursors[1] = editor->pCursors[0];
1132 if (is_shift) {
1133 if (x >= editor->rcFormat.left)
1134 ME_SelectByType(editor, stWord);
1135 else
1136 ME_SelectByType(editor, stParagraph);
1137 } else if (clickNum % 2 == 0) {
1138 ME_SelectByType(editor, stWord);
1139 } else {
1140 ME_SelectByType(editor, stParagraph);
1143 else if (!is_shift)
1145 editor->nSelectionType = stPosition;
1146 editor->pCursors[1] = editor->pCursors[0];
1148 else if (!is_selection)
1150 editor->nSelectionType = stPosition;
1151 editor->pCursors[1] = tmp_cursor;
1153 else if (editor->nSelectionType != stPosition)
1155 ME_ExtendAnchorSelection(editor);
1158 else
1160 if (clickNum < 2) {
1161 ME_SelectByType(editor, stLine);
1162 } else if (clickNum % 2 == 0 || is_shift) {
1163 ME_SelectByType(editor, stParagraph);
1164 } else {
1165 ME_SelectByType(editor, stDocument);
1168 ME_InvalidateSelection(editor);
1169 update_caret(editor);
1170 ME_SendSelChange(editor);
1173 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1175 ME_Cursor tmp_cursor;
1177 if (editor->nSelectionType == stDocument)
1178 return;
1179 x += editor->horz_si.nPos;
1180 y += editor->vert_si.nPos;
1182 tmp_cursor = editor->pCursors[0];
1183 /* FIXME: do something with the return value of cursor_from_virtual_coords */
1184 cursor_from_virtual_coords( editor, x, y, &tmp_cursor, TRUE );
1186 ME_InvalidateSelection(editor);
1187 editor->pCursors[0] = tmp_cursor;
1188 ME_ExtendAnchorSelection(editor);
1190 if (editor->nSelectionType != stPosition &&
1191 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1192 /* The scroll the cursor towards the other end, since it was the one
1193 * extended by ME_ExtendAnchorSelection */
1194 editor_ensure_visible( editor, &editor->pCursors[1] );
1195 else
1196 editor_ensure_visible( editor, &editor->pCursors[0] );
1198 ME_InvalidateSelection(editor);
1199 update_caret(editor);
1200 ME_SendSelChange(editor);
1203 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1205 ME_Run *run = pCursor->run;
1206 int x;
1208 if (editor->nUDArrowX != -1)
1209 x = editor->nUDArrowX;
1210 else
1212 x = run->pt.x;
1213 x += ME_PointFromChar( editor, run, pCursor->nOffset, TRUE );
1214 editor->nUDArrowX = x;
1216 return x;
1220 static void cursor_move_line( ME_TextEditor *editor, ME_Cursor *cursor, BOOL up, BOOL extend )
1222 ME_Paragraph *old_para = cursor->para, *new_para;
1223 ME_Row *row = row_from_cursor( cursor );
1224 int x = ME_GetXForArrow( editor, cursor );
1226 if (up)
1228 /* start of the previous row */
1229 row = row_prev_all_paras( row );
1230 if (!row)
1232 if (extend) ME_SetCursorToStart( editor, cursor );
1233 return;
1235 new_para = row_para( row );
1236 if (old_para->nFlags & MEPF_ROWEND ||
1237 (para_cell( old_para ) && para_cell( old_para ) != para_cell( new_para )))
1239 /* Brought out of a cell */
1240 new_para = para_prev( table_row_start( old_para ));
1241 if (!new_para) return; /* At the top, so don't go anywhere. */
1242 row = para_first_row( new_para );
1244 if (new_para->nFlags & MEPF_ROWEND)
1246 /* Brought into a table row */
1247 ME_Cell *cell = table_row_end_cell( new_para );
1248 while (x < cell->pt.x && cell_prev( cell ))
1249 cell = cell_prev( cell );
1250 if (cell_next( cell )) /* else - we are still at the end of the row */
1251 row = para_end_row( cell_end_para( cell ) );
1254 else
1256 /* start of the next row */
1257 row = row_next_all_paras( row );
1258 if (!row)
1260 if (extend) ME_SetCursorToEnd( editor, cursor, TRUE );
1261 return;
1263 new_para = row_para( row );
1264 if (old_para->nFlags & MEPF_ROWSTART ||
1265 (para_cell( old_para ) && para_cell( old_para ) != para_cell( new_para )))
1267 /* Brought out of a cell */
1268 new_para = para_next( table_row_end( old_para ) );
1269 if (!para_next( new_para )) return; /* At the bottom, so don't go anywhere. */
1270 row = para_first_row( new_para );
1272 if (new_para->nFlags & MEPF_ROWSTART)
1274 /* Brought into a table row */
1275 ME_Cell *cell = table_row_first_cell( new_para );
1276 while (cell_next( cell ) && x >= cell_next( cell )->pt.x)
1277 cell = cell_next( cell );
1278 row = para_first_row( cell_first_para( cell ) );
1281 if (!row) return;
1283 row_cursor( editor, row, x, cursor );
1286 static void ME_ArrowPageUp( ME_TextEditor *editor, ME_Cursor *cursor )
1288 ME_Row *row = para_first_row( editor_first_para( editor ) ), *last_row;
1289 int x, yd, old_scroll_pos = editor->vert_si.nPos;
1291 if (editor->vert_si.nPos < row->nHeight)
1293 ME_SetCursorToStart( editor, cursor );
1294 /* Native clears seems to clear this x value on page up at the top
1295 * of the text, but not on page down at the end of the text.
1296 * Doesn't make sense, but we try to be bug for bug compatible. */
1297 editor->nUDArrowX = -1;
1299 else
1301 x = ME_GetXForArrow( editor, cursor );
1302 row = row_from_cursor( cursor );
1304 ME_ScrollUp( editor, editor->sizeWindow.cy );
1305 /* Only move the cursor by the amount scrolled. */
1306 yd = cursor->para->pt.y + row->pt.y + editor->vert_si.nPos - old_scroll_pos;
1307 last_row = row;
1309 while ((row = row_prev_all_paras( row )))
1311 if (row_para( row )->pt.y + row->pt.y < yd) break;
1312 last_row = row;
1315 row_cursor( editor, last_row, x, cursor );
1319 static void ME_ArrowPageDown( ME_TextEditor *editor, ME_Cursor *cursor )
1321 ME_Row *row = para_end_row( para_prev( editor_end_para( editor ) ) ), *last_row;
1322 int x, yd, old_scroll_pos = editor->vert_si.nPos;
1324 x = ME_GetXForArrow( editor, cursor );
1326 if (editor->vert_si.nPos >= row_para( row )->pt.y + row->pt.y - editor->sizeWindow.cy)
1327 ME_SetCursorToEnd( editor, cursor, FALSE );
1328 else
1330 row = row_from_cursor( cursor );
1332 /* For native richedit controls:
1333 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1334 * v4.1 can scroll past this position here. */
1335 ME_ScrollDown( editor, editor->sizeWindow.cy );
1336 /* Only move the cursor by the amount scrolled. */
1337 yd = cursor->para->pt.y + row->pt.y + editor->vert_si.nPos - old_scroll_pos;
1338 last_row = row;
1340 while ((row = row_next_all_paras( row )))
1342 if (row_para( row )->pt.y + row->pt.y >= yd) break;
1343 last_row = row;
1346 row_cursor( editor, last_row, x, cursor );
1350 static void ME_ArrowHome( ME_TextEditor *editor, ME_Cursor *cursor )
1352 ME_Row *row = row_from_cursor( cursor );
1354 row_first_cursor( row, cursor );
1357 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1359 ME_SetCursorToStart(editor, pCursor);
1362 static void ME_ArrowEnd( ME_TextEditor *editor, ME_Cursor *cursor )
1364 ME_Row *row = row_from_cursor( cursor );
1366 row_end_cursor( row, cursor, FALSE );
1369 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1371 ME_SetCursorToEnd(editor, pCursor, FALSE);
1374 BOOL ME_IsSelection(ME_TextEditor *editor)
1376 return editor->pCursors[0].run != editor->pCursors[1].run ||
1377 editor->pCursors[0].nOffset != editor->pCursors[1].nOffset;
1380 void ME_DeleteSelection(ME_TextEditor *editor)
1382 LONG from, to;
1383 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
1384 int nEndCursor = nStartCursor ^ 1;
1385 ME_DeleteTextAtCursor(editor, nStartCursor, to - from);
1386 editor->pCursors[nEndCursor] = editor->pCursors[nStartCursor];
1389 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1391 return style_get_insert_style( editor, editor->pCursors );
1394 void ME_SendSelChange(ME_TextEditor *editor)
1396 SELCHANGE sc;
1398 sc.nmhdr.hwndFrom = NULL;
1399 sc.nmhdr.idFrom = 0;
1400 sc.nmhdr.code = EN_SELCHANGE;
1401 ME_GetSelectionOfs(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1402 sc.seltyp = SEL_EMPTY;
1403 if (sc.chrg.cpMin != sc.chrg.cpMax)
1404 sc.seltyp |= SEL_TEXT;
1405 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* what were RICHEDIT authors thinking ? */
1406 sc.seltyp |= SEL_MULTICHAR;
1408 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1410 ME_ClearTempStyle(editor);
1412 editor->notified_cr = sc.chrg;
1414 if (editor->nEventMask & ENM_SELCHANGE)
1416 TRACE("cpMin=%ld cpMax=%ld seltyp=%d (%s %s)\n",
1417 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1418 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1419 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1420 ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
1425 BOOL
1426 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1428 int nCursor = 0;
1429 ME_Cursor *p = &editor->pCursors[nCursor];
1430 ME_Cursor tmp_curs = *p;
1431 BOOL success = FALSE;
1433 ME_CheckCharOffsets(editor);
1434 switch(nVKey) {
1435 case VK_LEFT:
1436 if (ctrl)
1437 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1438 else
1439 success = ME_MoveCursorChars(editor, &tmp_curs, -1, extend);
1440 break;
1441 case VK_RIGHT:
1442 if (ctrl)
1443 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1444 else
1445 success = ME_MoveCursorChars(editor, &tmp_curs, +1, extend);
1446 break;
1447 case VK_UP:
1448 cursor_move_line( editor, &tmp_curs, TRUE, extend );
1449 break;
1450 case VK_DOWN:
1451 cursor_move_line( editor, &tmp_curs, FALSE, extend );
1452 break;
1453 case VK_PRIOR:
1454 ME_ArrowPageUp(editor, &tmp_curs);
1455 break;
1456 case VK_NEXT:
1457 ME_ArrowPageDown(editor, &tmp_curs);
1458 break;
1459 case VK_HOME: {
1460 if (ctrl)
1461 ME_ArrowCtrlHome(editor, &tmp_curs);
1462 else
1463 ME_ArrowHome(editor, &tmp_curs);
1464 break;
1466 case VK_END:
1467 if (ctrl)
1468 ME_ArrowCtrlEnd(editor, &tmp_curs);
1469 else
1470 ME_ArrowEnd(editor, &tmp_curs);
1471 break;
1474 if (!extend)
1475 editor->pCursors[1] = tmp_curs;
1476 *p = tmp_curs;
1478 ME_InvalidateSelection(editor);
1479 ME_Repaint(editor);
1480 hide_caret(editor);
1481 editor_ensure_visible( editor, &tmp_curs );
1482 update_caret(editor);
1483 ME_SendSelChange(editor);
1484 return success;