riched20: Pass a cursor ptr to the insert style retrieval function.
[wine.git] / dlls / riched20 / caret.c
blob5d49d11bd3b09ee4248b2c0eaa2722c904699a35
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->pPara = para_get_di( editor_first_para( editor ) );
30 cursor->pRun = run_get_di( para_first_run( &cursor->pPara->member.para ) );
31 cursor->nOffset = 0;
34 static void ME_SetCursorToEnd(ME_TextEditor *editor, ME_Cursor *cursor, BOOL final_eop)
36 cursor->pPara = para_get_di( para_prev( editor_end_para( editor ) ) );
37 cursor->pRun = run_get_di( para_end_run( &cursor->pPara->member.para ) );
38 cursor->nOffset = final_eop ? cursor->pRun->member.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]);
47 if (*from > *to)
49 int 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->styleFlags & ES_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 int 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].pRun->member.run.nFlags & MERF_ENDPARA)
209 editor->pCursors[1].nOffset = 0;
210 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA)
212 if (to > len)
213 editor->pCursors[0].nOffset = editor->pCursors[0].pRun->member.run.len;
214 else
215 editor->pCursors[0].nOffset = 0;
217 return to;
221 void ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
222 int *x, int *y, int *height)
224 ME_DisplayItem *row;
225 ME_Run *run = &pCursor->pRun->member.run;
226 ME_Paragraph *para = &pCursor->pPara->member.para;
227 ME_Run *size_run = run, *prev;
228 ME_Context c;
229 int run_x;
231 assert(height && x && y);
232 assert(~para->nFlags & MEPF_REWRAP);
234 row = ME_FindItemBack( run_get_di( run ), diStartRowOrParagraph );
235 assert(row && row->type == diStartRow);
237 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
239 if (!pCursor->nOffset && (prev = run_prev( run ))) size_run = prev;
241 if (editor->bCaretAtEnd && !pCursor->nOffset &&
242 run == &ME_FindItemFwd( row, diRun )->member.run)
244 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
245 if (tmp->type == diRun)
247 row = ME_FindItemBack(tmp, diStartRow);
248 size_run = run = &tmp->member.run;
251 run_x = ME_PointFromCharContext( &c, run, pCursor->nOffset, TRUE );
253 *height = size_run->nAscent + size_run->nDescent;
254 *x = c.rcView.left + run->pt.x + run_x - editor->horz_si.nPos;
255 *y = c.rcView.top + para->pt.y + row->member.row.nBaseline
256 + run->pt.y - size_run->nAscent - editor->vert_si.nPos;
257 ME_DestroyContext(&c);
258 return;
261 void create_caret(ME_TextEditor *editor)
263 int x, y, height;
265 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
266 ITextHost_TxCreateCaret(editor->texthost, NULL, 0, height);
267 editor->caret_height = height;
268 editor->caret_hidden = TRUE;
271 void show_caret(ME_TextEditor *editor)
273 ITextHost_TxShowCaret(editor->texthost, TRUE);
274 editor->caret_hidden = FALSE;
277 void hide_caret(ME_TextEditor *editor)
279 /* calls to HideCaret are cumulative; do so only once */
280 if (!editor->caret_hidden)
282 ITextHost_TxShowCaret(editor->texthost, FALSE);
283 editor->caret_hidden = TRUE;
287 void update_caret(ME_TextEditor *editor)
289 int x, y, height;
291 if (!editor->bHaveFocus) return;
292 if (!ME_IsSelection(editor))
294 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
295 if (height != editor->caret_height) create_caret(editor);
296 x = min(x, editor->rcFormat.right-1);
297 ITextHost_TxSetCaretPos(editor->texthost, x, y);
298 show_caret(editor);
300 else
301 hide_caret(editor);
304 BOOL ME_InternalDeleteText(ME_TextEditor *editor, ME_Cursor *start,
305 int nChars, BOOL bForce)
307 ME_Cursor c = *start;
308 int nOfs = ME_GetCursorOfs(start), text_len = ME_GetTextLength( editor );
309 int shift = 0;
310 int totalChars = nChars;
311 ME_Paragraph *start_para;
312 BOOL delete_all = FALSE;
314 /* Prevent deletion past last end of paragraph run. */
315 nChars = min(nChars, text_len - nOfs);
316 if (nChars == text_len) delete_all = TRUE;
317 start_para = &c.pPara->member.para;
319 if (!bForce)
321 ME_ProtectPartialTableDeletion(editor, &c, &nChars);
322 if (nChars == 0)
323 return FALSE;
326 while(nChars > 0)
328 ME_Run *run;
329 cursor_from_char_ofs( editor, nOfs + nChars, &c );
330 if (!c.nOffset)
332 /* We aren't deleting anything in this run, so we will go back to the
333 * last run we are deleting text in. */
334 c.pRun = run_get_di( run_prev_all_paras( &c.pRun->member.run ) );
335 c.pPara = para_get_di( c.pRun->member.run.para );
336 c.nOffset = c.pRun->member.run.len;
338 run = &c.pRun->member.run;
339 if (run->nFlags & MERF_ENDPARA)
341 int eollen = c.pRun->member.run.len;
342 BOOL keepFirstParaFormat;
344 if (!para_next( para_next( &c.pPara->member.para ) )) return TRUE;
346 keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
347 run->nCharOfs);
348 if (!editor->bEmulateVersion10) /* v4.1 */
350 ME_Paragraph *this_para = run->para;
351 ME_Paragraph *next_para = para_next( this_para );
353 /* The end of paragraph before a table row is only deleted if there
354 * is nothing else on the line before it. */
355 if (this_para == start_para && next_para->nFlags & MEPF_ROWSTART)
357 /* If the paragraph will be empty, then it should be deleted, however
358 * it still might have text right now which would inherit the
359 * MEPF_STARTROW property if we joined it right now.
360 * Instead we will delete it after the preceding text is deleted. */
361 if (nOfs > this_para->nCharOfs)
363 /* Skip this end of line. */
364 nChars -= (eollen < nChars) ? eollen : nChars;
365 continue;
367 keepFirstParaFormat = TRUE;
370 para_join( editor, &c.pPara->member.para, keepFirstParaFormat );
371 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
372 ME_CheckCharOffsets(editor);
373 nChars -= (eollen < nChars) ? eollen : nChars;
374 continue;
376 else
378 ME_Cursor cursor;
379 int nCharsToDelete = min(nChars, c.nOffset);
380 int i;
382 c.nOffset -= nCharsToDelete;
384 para_mark_rewrap( editor, c.pRun->member.run.para );
386 cursor = c;
387 /* nChars is the number of characters that should be deleted from the
388 PRECEDING runs (these BEFORE cursor.pRun)
389 nCharsToDelete is a number of chars to delete from THIS run */
390 nChars -= nCharsToDelete;
391 shift -= nCharsToDelete;
392 TRACE("Deleting %d (remaining %d) chars at %d in %s (%d)\n",
393 nCharsToDelete, nChars, c.nOffset,
394 debugstr_run( run ), run->len);
396 /* nOfs is a character offset (from the start of the document
397 to the current (deleted) run */
398 add_undo_insert_run( editor, nOfs + nChars, get_text( run, c.nOffset ), nCharsToDelete, run->nFlags, run->style );
400 ME_StrDeleteV(run->para->text, run->nCharOfs + c.nOffset, nCharsToDelete);
401 run->len -= nCharsToDelete;
402 TRACE("Post deletion string: %s (%d)\n", debugstr_run( run ), run->len);
403 TRACE("Shift value: %d\n", shift);
405 /* update cursors (including c) */
406 for (i=-1; i<editor->nCursors; i++) {
407 ME_Cursor *pThisCur = editor->pCursors + i;
408 if (i == -1) pThisCur = &c;
409 if (pThisCur->pRun == cursor.pRun) {
410 if (pThisCur->nOffset > cursor.nOffset) {
411 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
412 pThisCur->nOffset = cursor.nOffset;
413 else
414 pThisCur->nOffset -= nCharsToDelete;
415 assert(pThisCur->nOffset >= 0);
416 assert(pThisCur->nOffset <= run->len);
418 if (pThisCur->nOffset == run->len)
420 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
421 assert(pThisCur->pRun->type == diRun);
422 pThisCur->nOffset = 0;
427 /* c = updated data now */
429 if (c.pRun == cursor.pRun)
430 ME_SkipAndPropagateCharOffset(c.pRun, shift);
431 else
432 ME_PropagateCharOffset(c.pRun, shift);
434 if (!cursor.pRun->member.run.len)
436 TRACE("Removing empty run\n");
437 ME_Remove(cursor.pRun);
438 ME_DestroyDisplayItem(cursor.pRun);
441 shift = 0;
443 ME_CheckCharOffsets(editor);
445 continue;
448 if (delete_all) ME_SetDefaultParaFormat( editor, &start_para->fmt );
449 return TRUE;
452 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
454 assert(nCursor>=0 && nCursor<editor->nCursors);
455 /* text operations set modified state */
456 editor->nModifyStep = 1;
457 return ME_InternalDeleteText(editor, &editor->pCursors[nCursor],
458 nChars, FALSE);
461 static struct re_object* create_re_object(const REOBJECT *reo)
463 struct re_object *reobj = heap_alloc(sizeof(*reobj));
465 if (!reobj)
467 WARN("Fail to allocate re_object.\n");
468 return NULL;
470 ME_CopyReObject(&reobj->obj, reo, REO_GETOBJ_ALL_INTERFACES);
471 return reobj;
474 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
476 ME_Run *run, *prev;
477 const WCHAR space = ' ';
478 struct re_object *reobj_prev = NULL;
479 ME_Cursor *cursor = editor->pCursors + nCursor;
480 ME_Style *style = style_get_insert_style( editor, cursor );
482 /* FIXME no no no */
483 if (ME_IsSelection(editor))
484 ME_DeleteSelection(editor);
486 run = run_insert( editor, cursor, style, &space, 1, MERF_GRAPHICS );
487 editor->bCaretAtEnd = FALSE;
489 run->reobj = create_re_object( reo );
491 prev = run;
492 while ((prev = run_prev_all_paras( prev )))
494 if (prev->reobj)
496 reobj_prev = prev->reobj;
497 break;
500 if (reobj_prev)
501 list_add_after(&reobj_prev->entry, &run->reobj->entry);
502 else
503 list_add_head(&editor->reobj_list, &run->reobj->entry);
505 ME_ReleaseStyle( style );
509 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
511 const WCHAR space = ' ';
512 ME_Cursor *cursor = editor->pCursors + nCursor;
513 ME_Style *style = style_get_insert_style( editor, cursor );
515 /* FIXME no no no */
516 if (ME_IsSelection(editor))
517 ME_DeleteSelection(editor);
519 run_insert( editor, cursor, style, &space, 1, MERF_ENDROW );
520 editor->bCaretAtEnd = FALSE;
522 ME_ReleaseStyle( style );
526 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
527 const WCHAR *str, int len, ME_Style *style)
529 const WCHAR *pos;
530 ME_Cursor *cursor = editor->pCursors + nCursor;
531 int oldLen;
533 /* FIXME really HERE ? */
534 if (ME_IsSelection(editor))
535 ME_DeleteSelection(editor);
537 oldLen = ME_GetTextLength(editor);
539 /* text operations set modified state */
540 editor->nModifyStep = 1;
541 editor->bCaretAtEnd = FALSE;
543 assert(style);
545 if (len == -1) len = lstrlenW( str );
547 /* grow the text limit to fit our text */
548 if (editor->nTextLimit < oldLen + len) editor->nTextLimit = oldLen + len;
550 pos = str;
552 while (len)
554 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
555 while (pos - str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
556 pos++;
558 if (pos != str) /* handle text */
559 run_insert( editor, cursor, style, str, pos - str, 0 );
560 else if (*pos == '\t') /* handle tabs */
562 const WCHAR tab = '\t';
563 run_insert( editor, cursor, style, &tab, 1, MERF_TAB );
564 pos++;
566 else /* handle EOLs */
568 ME_Run *end_run, *run, *prev;
569 ME_Paragraph *new_para;
570 int eol_len = 0;
572 /* Check if new line is allowed for this control */
573 if (!(editor->styleFlags & ES_MULTILINE))
574 break;
576 /* Find number of CR and LF in end of paragraph run */
577 if (*pos =='\r')
579 if (len > 1 && pos[1] == '\n')
580 eol_len = 2;
581 else if (len > 2 && pos[1] == '\r' && pos[2] == '\n')
582 eol_len = 3;
583 else
584 eol_len = 1;
586 else
588 assert(*pos == '\n');
589 eol_len = 1;
591 pos += eol_len;
593 if (!editor->bEmulateVersion10 && eol_len == 3)
595 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
596 const WCHAR space = ' ';
597 run_insert( editor, cursor, style, &space, 1, 0 );
599 else
601 const WCHAR cr = '\r', *eol_str = str;
603 if (!editor->bEmulateVersion10)
605 eol_str = &cr;
606 eol_len = 1;
609 if (cursor->nOffset == cursor->pRun->member.run.len)
611 run = run_next( &cursor->pRun->member.run );
612 if (!run) run = &cursor->pRun->member.run;
614 else
616 if (cursor->nOffset) run_split( editor, cursor );
617 run = &cursor->pRun->member.run;
620 new_para = para_split( editor, run, style, eol_str, eol_len, 0 );
621 end_run = para_end_run( para_prev( new_para ) );
623 /* Move any cursors that were at the end of the previous run to the beginning of the new para */
624 prev = run_prev( end_run );
625 if (prev)
627 int i;
628 for (i = 0; i < editor->nCursors; i++)
630 if (editor->pCursors[i].pRun == run_get_di( prev ) &&
631 editor->pCursors[i].nOffset == prev->len)
633 editor->pCursors[i].pPara = para_get_di( new_para );
634 editor->pCursors[i].pRun = run_get_di( run );
635 editor->pCursors[i].nOffset = 0;
642 len -= pos - str;
643 str = pos;
647 /* Move the cursor nRelOfs characters (either forwards or backwards)
648 * If final_eop is TRUE, allow moving the cursor to the end of the final eop.
650 * returns the actual number of characters moved.
652 int ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs, BOOL final_eop)
654 cursor->nOffset += nRelOfs;
655 if (cursor->nOffset < 0)
657 cursor->nOffset += cursor->pRun->member.run.nCharOfs;
658 if (cursor->nOffset >= 0)
660 /* new offset in the same paragraph */
661 do {
662 cursor->pRun = run_get_di( run_prev( &cursor->pRun->member.run ) );
663 } while (cursor->nOffset < cursor->pRun->member.run.nCharOfs);
664 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
665 return nRelOfs;
668 cursor->nOffset += cursor->pPara->member.para.nCharOfs;
669 if (cursor->nOffset <= 0)
671 /* moved to the start of the text */
672 nRelOfs -= cursor->nOffset;
673 ME_SetCursorToStart(editor, cursor);
674 return nRelOfs;
677 /* new offset in a previous paragraph */
678 do {
679 cursor->pPara = para_get_di( para_prev( &cursor->pPara->member.para ) );
680 } while (cursor->nOffset < cursor->pPara->member.para.nCharOfs);
681 cursor->nOffset -= cursor->pPara->member.para.nCharOfs;
683 cursor->pRun = run_get_di( para_end_run( &cursor->pPara->member.para ) );
684 while (cursor->nOffset < cursor->pRun->member.run.nCharOfs) {
685 cursor->pRun = run_get_di( run_prev( &cursor->pRun->member.run ) );
687 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
689 else if (cursor->nOffset >= cursor->pRun->member.run.len)
691 ME_Paragraph *next_para;
692 int new_offset;
694 new_offset = ME_GetCursorOfs(cursor);
695 next_para = para_next( &cursor->pPara->member.para );
696 if (new_offset < next_para->nCharOfs)
698 /* new offset in the same paragraph */
699 do {
700 cursor->nOffset -= cursor->pRun->member.run.len;
701 cursor->pRun = run_get_di( run_next( &cursor->pRun->member.run ) );
702 } while (cursor->nOffset >= cursor->pRun->member.run.len);
703 return nRelOfs;
706 if (new_offset >= ME_GetTextLength(editor) + (final_eop ? 1 : 0))
708 /* new offset at the end of the text */
709 ME_SetCursorToEnd(editor, cursor, final_eop);
710 nRelOfs -= new_offset - (ME_GetTextLength(editor) + (final_eop ? 1 : 0));
711 return nRelOfs;
714 /* new offset in a following paragraph */
715 do {
716 cursor->pPara = para_get_di( next_para );
717 next_para = para_next( next_para );
718 } while (new_offset >= next_para->nCharOfs);
720 cursor->nOffset = new_offset - cursor->pPara->member.para.nCharOfs;
721 cursor->pRun = run_get_di( para_first_run( &cursor->pPara->member.para ) );
722 while (cursor->nOffset >= cursor->pRun->member.run.len)
724 cursor->nOffset -= cursor->pRun->member.run.len;
725 cursor->pRun = run_get_di( run_next( &cursor->pRun->member.run ) );
727 } /* else new offset is in the same run */
728 return nRelOfs;
732 BOOL
733 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
735 ME_Run *run = &cursor->pRun->member.run, *other_run;
736 ME_Paragraph *para = &cursor->pPara->member.para;
737 int nOffset = cursor->nOffset;
739 if (nRelOfs == -1)
741 /* Backward movement */
742 while (TRUE)
744 nOffset = ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset, WB_MOVEWORDLEFT );
745 if (nOffset) break;
746 other_run = run_prev( run );
747 if (other_run)
749 if (ME_CallWordBreakProc( editor, get_text( other_run, 0 ), other_run->len, other_run->len - 1, WB_ISDELIMITER )
750 && !(run->nFlags & MERF_ENDPARA)
751 && !(&cursor->pRun->member.run == run && cursor->nOffset == 0)
752 && !ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, 0, WB_ISDELIMITER ))
753 break;
754 run = other_run;
755 nOffset = other_run->len;
757 else
759 if (&cursor->pRun->member.run == run && cursor->nOffset == 0)
761 para = run->para;
762 /* Skip empty start of table row paragraph */
763 if (para_prev( para ) && para_prev( para )->nFlags & MEPF_ROWSTART)
764 para = para_prev( para );
765 /* Paragraph breaks are treated as separate words */
766 if (!para_prev( para )) return FALSE;
767 para = para_prev( para );
768 run = para_end_run( para );
770 break;
774 else
776 /* Forward movement */
777 BOOL last_delim = FALSE;
779 while (TRUE)
781 if (last_delim && !ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset, WB_ISDELIMITER ))
782 break;
783 nOffset = ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset, WB_MOVEWORDRIGHT );
784 if (nOffset < run->len) break;
785 other_run = run_next( run );
786 if (other_run)
788 last_delim = ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset - 1, WB_ISDELIMITER );
789 run = other_run;
790 nOffset = 0;
792 else
794 para = para_next( para );
795 if (para_get_di( para )->type == diTextEnd)
797 if (&cursor->pRun->member.run == run) return FALSE;
798 nOffset = 0;
799 break;
801 if (para->nFlags & MEPF_ROWSTART) para = para_next( para );
802 if (&cursor->pRun->member.run == run) run = para_first_run( para );
803 nOffset = 0;
804 break;
808 cursor->pPara = para_get_di( para );
809 cursor->pRun = run_get_di( run );
810 cursor->nOffset = nOffset;
811 return TRUE;
815 static void
816 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
818 /* pCursor[0] is the end of the selection
819 * pCursor[1] is the start of the selection (or the position selection anchor)
820 * pCursor[2] and [3] are the selection anchors that are backed up
821 * so they are kept when the selection changes for drag selection.
824 editor->nSelectionType = selectionType;
825 switch(selectionType)
827 case stPosition:
828 break;
829 case stWord:
830 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
831 editor->pCursors[1] = editor->pCursors[0];
832 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
833 break;
834 case stLine:
835 case stParagraph:
837 ME_DisplayItem *pItem;
838 ME_DIType fwdSearchType, backSearchType;
839 if (selectionType == stParagraph) {
840 backSearchType = diParagraph;
841 fwdSearchType = diParagraphOrEnd;
842 } else {
843 backSearchType = diStartRow;
844 fwdSearchType = diStartRowOrParagraphOrEnd;
846 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
847 assert(pItem);
848 if (pItem->type == diTextEnd)
849 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
850 else
851 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
852 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
853 editor->pCursors[0].nOffset = 0;
855 pItem = ME_FindItemBack(pItem, backSearchType);
856 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
857 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
858 editor->pCursors[1].nOffset = 0;
859 break;
861 case stDocument:
862 /* Select everything with cursor anchored from the start of the text */
863 editor->nSelectionType = stDocument;
864 ME_SetCursorToStart(editor, &editor->pCursors[1]);
865 ME_SetCursorToEnd(editor, &editor->pCursors[0], FALSE);
866 break;
867 default: assert(0);
869 /* Store the anchor positions for extending the selection. */
870 editor->pCursors[2] = editor->pCursors[0];
871 editor->pCursors[3] = editor->pCursors[1];
874 int ME_GetCursorOfs(const ME_Cursor *cursor)
876 return cursor->pPara->member.para.nCharOfs
877 + cursor->pRun->member.run.nCharOfs + cursor->nOffset;
880 /* Helper function for ME_FindPixelPos to find paragraph within tables */
881 static ME_Paragraph *pixel_pos_in_table_row( int x, int y, ME_Paragraph *para )
883 ME_DisplayItem *cell, *next_cell;
885 assert( para->nFlags & MEPF_ROWSTART );
886 cell = para_next( para )->pCell;
887 assert(cell);
889 /* find the cell we are in */
890 while ((next_cell = cell->member.cell.next_cell) != NULL)
892 if (x < next_cell->member.cell.pt.x)
894 para = &ME_FindItemFwd( cell, diParagraph )->member.para;
895 /* Found the cell, but there might be multiple paragraphs in
896 * the cell, so need to search down the cell for the paragraph. */
897 while (cell == para->pCell)
899 if (y < para->pt.y + para->nHeight)
901 if (para->nFlags & MEPF_ROWSTART) return pixel_pos_in_table_row( x, y, para );
902 else return para;
904 para = para_next( para );
906 /* Past the end of the cell, so go back to the last cell paragraph */
907 return para_prev( para );
909 cell = next_cell;
911 /* Return table row delimiter */
912 para = table_row_end( para );
913 assert( para->nFlags & MEPF_ROWEND );
914 assert( para->fmt.dwMask & PFM_TABLEROWDELIMITER );
915 assert( para->fmt.wEffects & PFE_TABLEROWDELIMITER );
916 return para;
919 static BOOL ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
920 int x, ME_Cursor *cursor, BOOL *pbCaretAtEnd)
922 ME_DisplayItem *pNext, *pLastRun;
923 ME_Row *row = &pRow->member.row;
924 BOOL exact = TRUE;
926 if (x < row->pt.x)
928 x = row->pt.x;
929 exact = FALSE;
931 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
932 assert(pNext->type == diRun);
933 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
934 cursor->nOffset = 0;
935 do {
936 int run_x = pNext->member.run.pt.x;
937 int width = pNext->member.run.nWidth;
939 if (x >= run_x && x < run_x+width)
941 cursor->nOffset = ME_CharFromPoint(editor, x-run_x, &pNext->member.run, TRUE, TRUE);
942 cursor->pRun = pNext;
943 cursor->pPara = ME_GetParagraph( cursor->pRun );
944 return exact;
946 pLastRun = pNext;
947 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
948 } while(pNext && pNext->type == diRun);
950 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
952 cursor->pRun = ME_FindItemFwd(pNext, diRun);
953 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
955 else
956 cursor->pRun = pLastRun;
958 cursor->pPara = ME_GetParagraph( cursor->pRun );
959 return FALSE;
962 /* Finds the run and offset from the pixel position.
964 * x & y are pixel positions in virtual coordinates into the rich edit control,
965 * so client coordinates must first be adjusted by the scroll position.
967 * If final_eop is TRUE consider the final end-of-paragraph.
969 * returns TRUE if the result was exactly under the cursor, otherwise returns
970 * FALSE, and result is set to the closest position to the coordinates.
972 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
973 ME_Cursor *result, BOOL *is_eol, BOOL final_eop)
975 ME_Paragraph *para = editor_first_para( editor );
976 ME_DisplayItem *row = NULL;
977 BOOL isExact = TRUE;
979 x -= editor->rcFormat.left;
980 y -= editor->rcFormat.top;
982 if (is_eol)
983 *is_eol = FALSE;
985 /* find paragraph */
986 for (; para_next( para ); para = para_next( para ))
988 if (y < para->pt.y + para->nHeight)
990 if (para->nFlags & MEPF_ROWSTART)
991 para = pixel_pos_in_table_row( x, y, para );
992 y -= para->pt.y;
993 row = ME_FindItemFwd( para_get_di( para ), diStartRow);
994 break;
996 else if (para->nFlags & MEPF_ROWSTART)
998 para = table_row_end( para );
1001 /* find row */
1002 while (row)
1004 ME_DisplayItem *next_row;
1006 if (y < row->member.row.pt.y + row->member.row.nHeight) break;
1007 next_row = ME_FindItemFwd(row, diStartRow);
1008 if (!next_row) break;
1009 row = next_row;
1012 if (!row && !final_eop)
1014 /* The position is below the last paragraph, so the last row will be used
1015 * rather than the end of the text, so the x position will be used to
1016 * determine the offset closest to the pixel position. */
1017 isExact = FALSE;
1018 row = ME_FindItemBack( para_get_di( para ), diStartRow);
1021 if (row) return ME_FindRunInRow( editor, row, x, result, is_eol ) && isExact;
1023 ME_SetCursorToEnd(editor, result, TRUE);
1024 return FALSE;
1028 /* Sets the cursor to the position closest to the pixel position
1030 * x & y are pixel positions in client coordinates.
1032 * isExact will be set to TRUE if the run is directly under the pixel
1033 * position, FALSE if it not, unless isExact is set to NULL.
1035 * return FALSE if outside client area and the cursor is not set,
1036 * otherwise TRUE is returned.
1038 BOOL ME_CharFromPos(ME_TextEditor *editor, int x, int y,
1039 ME_Cursor *cursor, BOOL *isExact)
1041 RECT rc;
1042 BOOL bResult;
1044 ITextHost_TxGetClientRect(editor->texthost, &rc);
1045 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
1046 if (isExact) *isExact = FALSE;
1047 return FALSE;
1049 x += editor->horz_si.nPos;
1050 y += editor->vert_si.nPos;
1051 bResult = ME_FindPixelPos(editor, x, y, cursor, NULL, FALSE);
1052 if (isExact) *isExact = bResult;
1053 return TRUE;
1058 /* Extends the selection with a word, line, or paragraph selection type.
1060 * The selection is anchored by editor->pCursors[2-3] such that the text
1061 * between the anchors will remain selected, and one end will be extended.
1063 * editor->pCursors[0] should have the position to extend the selection to
1064 * before this function is called.
1066 * Nothing will be done if editor->nSelectionType equals stPosition.
1068 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1070 ME_Cursor tmp_cursor;
1071 int curOfs, anchorStartOfs, anchorEndOfs;
1072 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1073 return;
1074 curOfs = ME_GetCursorOfs(&editor->pCursors[0]);
1075 anchorStartOfs = ME_GetCursorOfs(&editor->pCursors[3]);
1076 anchorEndOfs = ME_GetCursorOfs(&editor->pCursors[2]);
1078 tmp_cursor = editor->pCursors[0];
1079 editor->pCursors[0] = editor->pCursors[2];
1080 editor->pCursors[1] = editor->pCursors[3];
1081 if (curOfs < anchorStartOfs)
1083 /* Extend the left side of selection */
1084 editor->pCursors[1] = tmp_cursor;
1085 if (editor->nSelectionType == stWord)
1086 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1087 else
1089 ME_DisplayItem *pItem;
1090 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1091 diStartRowOrParagraph:diParagraph);
1092 pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
1093 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
1094 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
1095 editor->pCursors[1].nOffset = 0;
1098 else if (curOfs >= anchorEndOfs)
1100 /* Extend the right side of selection */
1101 editor->pCursors[0] = tmp_cursor;
1102 if (editor->nSelectionType == stWord)
1103 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
1104 else
1106 ME_DisplayItem *pItem;
1107 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1108 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
1109 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
1110 if (pItem->type == diTextEnd)
1111 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
1112 else
1113 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
1114 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
1115 editor->pCursors[0].nOffset = 0;
1120 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1122 ME_Cursor tmp_cursor;
1123 BOOL is_selection = FALSE, is_shift;
1125 editor->nUDArrowX = -1;
1127 x += editor->horz_si.nPos;
1128 y += editor->vert_si.nPos;
1130 tmp_cursor = editor->pCursors[0];
1131 is_selection = ME_IsSelection(editor);
1132 is_shift = GetKeyState(VK_SHIFT) < 0;
1134 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd, FALSE);
1136 if (x >= editor->rcFormat.left || is_shift)
1138 if (clickNum > 1)
1140 editor->pCursors[1] = editor->pCursors[0];
1141 if (is_shift) {
1142 if (x >= editor->rcFormat.left)
1143 ME_SelectByType(editor, stWord);
1144 else
1145 ME_SelectByType(editor, stParagraph);
1146 } else if (clickNum % 2 == 0) {
1147 ME_SelectByType(editor, stWord);
1148 } else {
1149 ME_SelectByType(editor, stParagraph);
1152 else if (!is_shift)
1154 editor->nSelectionType = stPosition;
1155 editor->pCursors[1] = editor->pCursors[0];
1157 else if (!is_selection)
1159 editor->nSelectionType = stPosition;
1160 editor->pCursors[1] = tmp_cursor;
1162 else if (editor->nSelectionType != stPosition)
1164 ME_ExtendAnchorSelection(editor);
1167 else
1169 if (clickNum < 2) {
1170 ME_SelectByType(editor, stLine);
1171 } else if (clickNum % 2 == 0 || is_shift) {
1172 ME_SelectByType(editor, stParagraph);
1173 } else {
1174 ME_SelectByType(editor, stDocument);
1177 ME_InvalidateSelection(editor);
1178 update_caret(editor);
1179 ME_SendSelChange(editor);
1182 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1184 ME_Cursor tmp_cursor;
1186 if (editor->nSelectionType == stDocument)
1187 return;
1188 x += editor->horz_si.nPos;
1189 y += editor->vert_si.nPos;
1191 tmp_cursor = editor->pCursors[0];
1192 /* FIXME: do something with the return value of ME_FindPixelPos */
1193 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd, TRUE);
1195 ME_InvalidateSelection(editor);
1196 editor->pCursors[0] = tmp_cursor;
1197 ME_ExtendAnchorSelection(editor);
1199 if (editor->nSelectionType != stPosition &&
1200 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1202 /* The scroll the cursor towards the other end, since it was the one
1203 * extended by ME_ExtendAnchorSelection */
1204 ME_EnsureVisible(editor, &editor->pCursors[1]);
1205 } else {
1206 ME_EnsureVisible(editor, &editor->pCursors[0]);
1209 ME_InvalidateSelection(editor);
1210 update_caret(editor);
1211 ME_SendSelChange(editor);
1214 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1216 ME_Run *run = &pCursor->pRun->member.run;
1217 int x;
1219 if (editor->nUDArrowX != -1)
1220 x = editor->nUDArrowX;
1221 else
1223 if (editor->bCaretAtEnd)
1225 run = run_prev_all_paras( run );
1226 assert( run );
1227 x = run->pt.x + run->nWidth;
1229 else
1231 x = run->pt.x;
1232 x += ME_PointFromChar( editor, run, pCursor->nOffset, TRUE );
1234 editor->nUDArrowX = x;
1236 return x;
1240 static void
1241 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs, BOOL extend)
1243 ME_DisplayItem *pRun = pCursor->pRun;
1244 ME_DisplayItem *pOldPara = pCursor->pPara;
1245 ME_DisplayItem *pItem, *pNewPara;
1246 int x = ME_GetXForArrow(editor, pCursor);
1248 if (editor->bCaretAtEnd && !pCursor->nOffset)
1249 if (!ME_PrevRun(&pOldPara, &pRun, TRUE))
1250 return;
1252 if (nRelOfs == -1)
1254 /* start of this row */
1255 pItem = ME_FindItemBack(pRun, diStartRow);
1256 assert(pItem);
1257 /* start of the previous row */
1258 pItem = ME_FindItemBack(pItem, diStartRow);
1259 if (!pItem) /* row not found */
1261 if (extend)
1262 ME_SetCursorToStart(editor, pCursor);
1263 return;
1265 pNewPara = ME_GetParagraph(pItem);
1266 if (pOldPara->member.para.nFlags & MEPF_ROWEND ||
1267 (pOldPara->member.para.pCell &&
1268 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1270 /* Brought out of a cell */
1271 pNewPara = table_row_start( &pOldPara->member.para )->prev_para;
1272 if (pNewPara->type == diTextStart)
1273 return; /* At the top, so don't go anywhere. */
1274 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1276 if (pNewPara->member.para.nFlags & MEPF_ROWEND)
1278 /* Brought into a table row */
1279 ME_Cell *cell = &ME_FindItemBack(pNewPara, diCell)->member.cell;
1280 while (x < cell->pt.x && cell->prev_cell)
1281 cell = &cell->prev_cell->member.cell;
1282 if (cell->next_cell) /* else - we are still at the end of the row */
1283 pItem = ME_FindItemBack(cell->next_cell, diStartRow);
1286 else
1288 /* start of the next row */
1289 pItem = ME_FindItemFwd(pRun, diStartRow);
1290 if (!pItem) /* row not found */
1292 if (extend)
1293 ME_SetCursorToEnd(editor, pCursor, TRUE);
1294 return;
1296 pNewPara = ME_GetParagraph(pItem);
1297 if (pOldPara->member.para.nFlags & MEPF_ROWSTART ||
1298 (pOldPara->member.para.pCell &&
1299 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1301 /* Brought out of a cell */
1302 pNewPara = table_row_end( &pOldPara->member.para )->next_para;
1303 if (pNewPara->type == diTextEnd)
1304 return; /* At the bottom, so don't go anywhere. */
1305 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1307 if (pNewPara->member.para.nFlags & MEPF_ROWSTART)
1309 /* Brought into a table row */
1310 ME_DisplayItem *cell = ME_FindItemFwd(pNewPara, diCell);
1311 while (cell->member.cell.next_cell &&
1312 x >= cell->member.cell.next_cell->member.cell.pt.x)
1313 cell = cell->member.cell.next_cell;
1314 pItem = ME_FindItemFwd(cell, diStartRow);
1317 if (!pItem)
1319 /* row not found - ignore */
1320 return;
1322 ME_FindRunInRow(editor, pItem, x, pCursor, &editor->bCaretAtEnd);
1323 assert(pCursor->pRun);
1324 assert(pCursor->pRun->type == diRun);
1327 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1329 ME_DisplayItem *p = ME_FindItemFwd(editor->pBuffer->pFirst, diStartRow);
1331 if (editor->vert_si.nPos < p->member.row.nHeight)
1333 ME_SetCursorToStart(editor, pCursor);
1334 editor->bCaretAtEnd = FALSE;
1335 /* Native clears seems to clear this x value on page up at the top
1336 * of the text, but not on page down at the end of the text.
1337 * Doesn't make sense, but we try to be bug for bug compatible. */
1338 editor->nUDArrowX = -1;
1339 } else {
1340 ME_DisplayItem *pRun = pCursor->pRun;
1341 ME_DisplayItem *pLast;
1342 int x, y, yd, yp;
1343 int yOldScrollPos = editor->vert_si.nPos;
1345 x = ME_GetXForArrow(editor, pCursor);
1346 if (!pCursor->nOffset && editor->bCaretAtEnd)
1347 pRun = ME_FindItemBack(pRun, diRun);
1349 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1350 assert(p->type == diStartRow);
1351 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1352 y = yp + p->member.row.pt.y;
1354 ME_ScrollUp(editor, editor->sizeWindow.cy);
1355 /* Only move the cursor by the amount scrolled. */
1356 yd = y + editor->vert_si.nPos - yOldScrollPos;
1357 pLast = p;
1359 do {
1360 p = ME_FindItemBack(p, diStartRowOrParagraph);
1361 if (!p)
1362 break;
1363 if (p->type == diParagraph) { /* crossing paragraphs */
1364 if (p->member.para.prev_para == NULL)
1365 break;
1366 yp = p->member.para.prev_para->member.para.pt.y;
1367 continue;
1369 y = yp + p->member.row.pt.y;
1370 if (y < yd)
1371 break;
1372 pLast = p;
1373 } while(1);
1375 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
1377 assert(pCursor->pRun);
1378 assert(pCursor->pRun->type == diRun);
1381 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1383 ME_DisplayItem *pLast;
1384 int x, y;
1386 /* Find y position of the last row */
1387 pLast = editor->pBuffer->pLast;
1388 y = pLast->member.para.prev_para->member.para.pt.y
1389 + ME_FindItemBack(pLast, diStartRow)->member.row.pt.y;
1391 x = ME_GetXForArrow(editor, pCursor);
1393 if (editor->vert_si.nPos >= y - editor->sizeWindow.cy)
1395 ME_SetCursorToEnd(editor, pCursor, FALSE);
1396 editor->bCaretAtEnd = FALSE;
1397 } else {
1398 ME_DisplayItem *pRun = pCursor->pRun;
1399 ME_DisplayItem *p;
1400 int yd, yp;
1401 int yOldScrollPos = editor->vert_si.nPos;
1403 if (!pCursor->nOffset && editor->bCaretAtEnd)
1404 pRun = ME_FindItemBack(pRun, diRun);
1406 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1407 assert(p->type == diStartRow);
1408 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1409 y = yp + p->member.row.pt.y;
1411 /* For native richedit controls:
1412 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1413 * v4.1 can scroll past this position here. */
1414 ME_ScrollDown(editor, editor->sizeWindow.cy);
1415 /* Only move the cursor by the amount scrolled. */
1416 yd = y + editor->vert_si.nPos - yOldScrollPos;
1417 pLast = p;
1419 do {
1420 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1421 if (!p)
1422 break;
1423 if (p->type == diParagraph) {
1424 yp = p->member.para.pt.y;
1425 continue;
1427 y = yp + p->member.row.pt.y;
1428 if (y >= yd)
1429 break;
1430 pLast = p;
1431 } while(1);
1433 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
1435 assert(pCursor->pRun);
1436 assert(pCursor->pRun->type == diRun);
1439 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1441 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1442 if (pRow) {
1443 ME_DisplayItem *pRun;
1444 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1445 pRow = ME_FindItemBack(pRow, diStartRow);
1446 if (!pRow)
1447 return;
1449 pRun = ME_FindItemFwd(pRow, diRun);
1450 if (pRun) {
1451 pCursor->pRun = pRun;
1452 assert(pCursor->pPara == ME_GetParagraph(pRun));
1453 pCursor->nOffset = 0;
1456 editor->bCaretAtEnd = FALSE;
1459 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1461 ME_SetCursorToStart(editor, pCursor);
1462 editor->bCaretAtEnd = FALSE;
1465 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1467 ME_DisplayItem *pRow;
1469 if (editor->bCaretAtEnd && !pCursor->nOffset)
1470 return;
1472 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1473 assert(pRow);
1474 if (pRow->type == diStartRow) {
1475 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1476 assert(pRun);
1477 pCursor->pRun = pRun;
1478 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1479 pCursor->nOffset = 0;
1480 editor->bCaretAtEnd = TRUE;
1481 return;
1483 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1484 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1485 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1486 pCursor->nOffset = 0;
1487 editor->bCaretAtEnd = FALSE;
1490 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1492 ME_SetCursorToEnd(editor, pCursor, FALSE);
1493 editor->bCaretAtEnd = FALSE;
1496 BOOL ME_IsSelection(ME_TextEditor *editor)
1498 return editor->pCursors[0].pRun != editor->pCursors[1].pRun ||
1499 editor->pCursors[0].nOffset != editor->pCursors[1].nOffset;
1502 void ME_DeleteSelection(ME_TextEditor *editor)
1504 int from, to;
1505 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
1506 int nEndCursor = nStartCursor ^ 1;
1507 ME_DeleteTextAtCursor(editor, nStartCursor, to - from);
1508 editor->pCursors[nEndCursor] = editor->pCursors[nStartCursor];
1511 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1513 return style_get_insert_style( editor, editor->pCursors );
1516 void ME_SendSelChange(ME_TextEditor *editor)
1518 SELCHANGE sc;
1520 sc.nmhdr.hwndFrom = NULL;
1521 sc.nmhdr.idFrom = 0;
1522 sc.nmhdr.code = EN_SELCHANGE;
1523 ME_GetSelectionOfs(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1524 sc.seltyp = SEL_EMPTY;
1525 if (sc.chrg.cpMin != sc.chrg.cpMax)
1526 sc.seltyp |= SEL_TEXT;
1527 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* what were RICHEDIT authors thinking ? */
1528 sc.seltyp |= SEL_MULTICHAR;
1530 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1532 ME_ClearTempStyle(editor);
1534 editor->notified_cr = sc.chrg;
1536 if (editor->nEventMask & ENM_SELCHANGE)
1538 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1539 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1540 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1541 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1542 ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
1547 BOOL
1548 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1550 int nCursor = 0;
1551 ME_Cursor *p = &editor->pCursors[nCursor];
1552 ME_Cursor tmp_curs = *p;
1553 BOOL success = FALSE;
1555 ME_CheckCharOffsets(editor);
1556 switch(nVKey) {
1557 case VK_LEFT:
1558 editor->bCaretAtEnd = FALSE;
1559 if (ctrl)
1560 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1561 else
1562 success = ME_MoveCursorChars(editor, &tmp_curs, -1, extend);
1563 break;
1564 case VK_RIGHT:
1565 editor->bCaretAtEnd = FALSE;
1566 if (ctrl)
1567 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1568 else
1569 success = ME_MoveCursorChars(editor, &tmp_curs, +1, extend);
1570 break;
1571 case VK_UP:
1572 ME_MoveCursorLines(editor, &tmp_curs, -1, extend);
1573 break;
1574 case VK_DOWN:
1575 ME_MoveCursorLines(editor, &tmp_curs, +1, extend);
1576 break;
1577 case VK_PRIOR:
1578 ME_ArrowPageUp(editor, &tmp_curs);
1579 break;
1580 case VK_NEXT:
1581 ME_ArrowPageDown(editor, &tmp_curs);
1582 break;
1583 case VK_HOME: {
1584 if (ctrl)
1585 ME_ArrowCtrlHome(editor, &tmp_curs);
1586 else
1587 ME_ArrowHome(editor, &tmp_curs);
1588 editor->bCaretAtEnd = FALSE;
1589 break;
1591 case VK_END:
1592 if (ctrl)
1593 ME_ArrowCtrlEnd(editor, &tmp_curs);
1594 else
1595 ME_ArrowEnd(editor, &tmp_curs);
1596 break;
1599 if (!extend)
1600 editor->pCursors[1] = tmp_curs;
1601 *p = tmp_curs;
1603 ME_InvalidateSelection(editor);
1604 ME_Repaint(editor);
1605 hide_caret(editor);
1606 ME_EnsureVisible(editor, &tmp_curs);
1607 update_caret(editor);
1608 ME_SendSelChange(editor);
1609 return success;