riched20: Use row and para helpers in the extend selection function.
[wine/zf.git] / dlls / riched20 / caret.c
blobd92b93ac2f0642aed3d12f1a486620940d6705be
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 cursor_coords( ME_TextEditor *editor, ME_Cursor *cursor,
222 int *x, int *y, int *height )
224 ME_Row *row;
225 ME_Run *run = &cursor->pRun->member.run;
226 ME_Paragraph *para = &cursor->pPara->member.para;
227 ME_Run *size_run = run, *prev;
228 ME_Context c;
229 int run_x;
231 assert(~para->nFlags & MEPF_REWRAP);
233 row = row_from_cursor( cursor );
235 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
237 if (!cursor->nOffset && (prev = run_prev( run ))) size_run = prev;
239 run_x = ME_PointFromCharContext( &c, run, cursor->nOffset, TRUE );
241 *height = size_run->nAscent + size_run->nDescent;
242 *x = c.rcView.left + run->pt.x + run_x - editor->horz_si.nPos;
243 *y = c.rcView.top + para->pt.y + row->nBaseline
244 + run->pt.y - size_run->nAscent - editor->vert_si.nPos;
245 ME_DestroyContext(&c);
246 return;
249 void create_caret(ME_TextEditor *editor)
251 int x, y, height;
253 cursor_coords( editor, &editor->pCursors[0], &x, &y, &height );
254 ITextHost_TxCreateCaret(editor->texthost, NULL, 0, height);
255 editor->caret_height = height;
256 editor->caret_hidden = TRUE;
259 void show_caret(ME_TextEditor *editor)
261 ITextHost_TxShowCaret(editor->texthost, TRUE);
262 editor->caret_hidden = FALSE;
265 void hide_caret(ME_TextEditor *editor)
267 /* calls to HideCaret are cumulative; do so only once */
268 if (!editor->caret_hidden)
270 ITextHost_TxShowCaret(editor->texthost, FALSE);
271 editor->caret_hidden = TRUE;
275 void update_caret(ME_TextEditor *editor)
277 int x, y, height;
279 if (!editor->bHaveFocus) return;
280 if (!ME_IsSelection(editor))
282 cursor_coords( editor, &editor->pCursors[0], &x, &y, &height );
283 if (height != editor->caret_height) create_caret(editor);
284 x = min(x, editor->rcFormat.right-1);
285 ITextHost_TxSetCaretPos(editor->texthost, x, y);
286 show_caret(editor);
288 else
289 hide_caret(editor);
292 BOOL ME_InternalDeleteText(ME_TextEditor *editor, ME_Cursor *start,
293 int nChars, BOOL bForce)
295 ME_Cursor c = *start;
296 int nOfs = ME_GetCursorOfs(start), text_len = ME_GetTextLength( editor );
297 int shift = 0;
298 int totalChars = nChars;
299 ME_Paragraph *start_para;
300 BOOL delete_all = FALSE;
302 /* Prevent deletion past last end of paragraph run. */
303 nChars = min(nChars, text_len - nOfs);
304 if (nChars == text_len) delete_all = TRUE;
305 start_para = &c.pPara->member.para;
307 if (!bForce)
309 ME_ProtectPartialTableDeletion(editor, &c, &nChars);
310 if (nChars == 0)
311 return FALSE;
314 while(nChars > 0)
316 ME_Run *run;
317 cursor_from_char_ofs( editor, nOfs + nChars, &c );
318 if (!c.nOffset)
320 /* We aren't deleting anything in this run, so we will go back to the
321 * last run we are deleting text in. */
322 c.pRun = run_get_di( run_prev_all_paras( &c.pRun->member.run ) );
323 c.pPara = para_get_di( c.pRun->member.run.para );
324 c.nOffset = c.pRun->member.run.len;
326 run = &c.pRun->member.run;
327 if (run->nFlags & MERF_ENDPARA)
329 int eollen = c.pRun->member.run.len;
330 BOOL keepFirstParaFormat;
332 if (!para_next( para_next( &c.pPara->member.para ) )) return TRUE;
334 keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
335 run->nCharOfs);
336 if (!editor->bEmulateVersion10) /* v4.1 */
338 ME_Paragraph *this_para = run->para;
339 ME_Paragraph *next_para = para_next( this_para );
341 /* The end of paragraph before a table row is only deleted if there
342 * is nothing else on the line before it. */
343 if (this_para == start_para && next_para->nFlags & MEPF_ROWSTART)
345 /* If the paragraph will be empty, then it should be deleted, however
346 * it still might have text right now which would inherit the
347 * MEPF_STARTROW property if we joined it right now.
348 * Instead we will delete it after the preceding text is deleted. */
349 if (nOfs > this_para->nCharOfs)
351 /* Skip this end of line. */
352 nChars -= (eollen < nChars) ? eollen : nChars;
353 continue;
355 keepFirstParaFormat = TRUE;
358 para_join( editor, &c.pPara->member.para, keepFirstParaFormat );
359 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
360 ME_CheckCharOffsets(editor);
361 nChars -= (eollen < nChars) ? eollen : nChars;
362 continue;
364 else
366 ME_Cursor cursor;
367 int nCharsToDelete = min(nChars, c.nOffset);
368 int i;
370 c.nOffset -= nCharsToDelete;
372 para_mark_rewrap( editor, c.pRun->member.run.para );
374 cursor = c;
375 /* nChars is the number of characters that should be deleted from the
376 PRECEDING runs (these BEFORE cursor.pRun)
377 nCharsToDelete is a number of chars to delete from THIS run */
378 nChars -= nCharsToDelete;
379 shift -= nCharsToDelete;
380 TRACE("Deleting %d (remaining %d) chars at %d in %s (%d)\n",
381 nCharsToDelete, nChars, c.nOffset,
382 debugstr_run( run ), run->len);
384 /* nOfs is a character offset (from the start of the document
385 to the current (deleted) run */
386 add_undo_insert_run( editor, nOfs + nChars, get_text( run, c.nOffset ), nCharsToDelete, run->nFlags, run->style );
388 ME_StrDeleteV(run->para->text, run->nCharOfs + c.nOffset, nCharsToDelete);
389 run->len -= nCharsToDelete;
390 TRACE("Post deletion string: %s (%d)\n", debugstr_run( run ), run->len);
391 TRACE("Shift value: %d\n", shift);
393 /* update cursors (including c) */
394 for (i=-1; i<editor->nCursors; i++) {
395 ME_Cursor *pThisCur = editor->pCursors + i;
396 if (i == -1) pThisCur = &c;
397 if (pThisCur->pRun == cursor.pRun) {
398 if (pThisCur->nOffset > cursor.nOffset) {
399 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
400 pThisCur->nOffset = cursor.nOffset;
401 else
402 pThisCur->nOffset -= nCharsToDelete;
403 assert(pThisCur->nOffset >= 0);
404 assert(pThisCur->nOffset <= run->len);
406 if (pThisCur->nOffset == run->len)
408 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
409 assert(pThisCur->pRun->type == diRun);
410 pThisCur->nOffset = 0;
415 /* c = updated data now */
417 if (c.pRun == cursor.pRun)
418 ME_SkipAndPropagateCharOffset(c.pRun, shift);
419 else
420 ME_PropagateCharOffset(c.pRun, shift);
422 if (!cursor.pRun->member.run.len)
424 TRACE("Removing empty run\n");
425 ME_Remove(cursor.pRun);
426 ME_DestroyDisplayItem(cursor.pRun);
429 shift = 0;
431 ME_CheckCharOffsets(editor);
433 continue;
436 if (delete_all) ME_SetDefaultParaFormat( editor, &start_para->fmt );
437 return TRUE;
440 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
442 assert(nCursor>=0 && nCursor<editor->nCursors);
443 /* text operations set modified state */
444 editor->nModifyStep = 1;
445 return ME_InternalDeleteText(editor, &editor->pCursors[nCursor],
446 nChars, FALSE);
449 static struct re_object* create_re_object(const REOBJECT *reo)
451 struct re_object *reobj = heap_alloc(sizeof(*reobj));
453 if (!reobj)
455 WARN("Fail to allocate re_object.\n");
456 return NULL;
458 ME_CopyReObject(&reobj->obj, reo, REO_GETOBJ_ALL_INTERFACES);
459 return reobj;
462 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
464 ME_Run *run, *prev;
465 const WCHAR space = ' ';
466 struct re_object *reobj_prev = NULL;
467 ME_Cursor *cursor = editor->pCursors + nCursor;
468 ME_Style *style = style_get_insert_style( editor, cursor );
470 /* FIXME no no no */
471 if (ME_IsSelection(editor))
472 ME_DeleteSelection(editor);
474 run = run_insert( editor, cursor, style, &space, 1, MERF_GRAPHICS );
476 run->reobj = create_re_object( reo );
478 prev = run;
479 while ((prev = run_prev_all_paras( prev )))
481 if (prev->reobj)
483 reobj_prev = prev->reobj;
484 break;
487 if (reobj_prev)
488 list_add_after(&reobj_prev->entry, &run->reobj->entry);
489 else
490 list_add_head(&editor->reobj_list, &run->reobj->entry);
492 ME_ReleaseStyle( style );
496 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
498 const WCHAR space = ' ';
499 ME_Cursor *cursor = editor->pCursors + nCursor;
500 ME_Style *style = style_get_insert_style( editor, cursor );
502 /* FIXME no no no */
503 if (ME_IsSelection(editor))
504 ME_DeleteSelection(editor);
506 run_insert( editor, cursor, style, &space, 1, MERF_ENDROW );
508 ME_ReleaseStyle( style );
512 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
513 const WCHAR *str, int len, ME_Style *style)
515 const WCHAR *pos;
516 ME_Cursor *cursor = editor->pCursors + nCursor;
517 int oldLen;
519 /* FIXME really HERE ? */
520 if (ME_IsSelection(editor))
521 ME_DeleteSelection(editor);
523 oldLen = ME_GetTextLength(editor);
525 /* text operations set modified state */
526 editor->nModifyStep = 1;
528 assert(style);
530 if (len == -1) len = lstrlenW( str );
532 /* grow the text limit to fit our text */
533 if (editor->nTextLimit < oldLen + len) editor->nTextLimit = oldLen + len;
535 pos = str;
537 while (len)
539 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
540 while (pos - str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
541 pos++;
543 if (pos != str) /* handle text */
544 run_insert( editor, cursor, style, str, pos - str, 0 );
545 else if (*pos == '\t') /* handle tabs */
547 const WCHAR tab = '\t';
548 run_insert( editor, cursor, style, &tab, 1, MERF_TAB );
549 pos++;
551 else /* handle EOLs */
553 ME_Run *end_run, *run, *prev;
554 ME_Paragraph *new_para;
555 int eol_len = 0;
557 /* Check if new line is allowed for this control */
558 if (!(editor->styleFlags & ES_MULTILINE))
559 break;
561 /* Find number of CR and LF in end of paragraph run */
562 if (*pos =='\r')
564 if (len > 1 && pos[1] == '\n')
565 eol_len = 2;
566 else if (len > 2 && pos[1] == '\r' && pos[2] == '\n')
567 eol_len = 3;
568 else
569 eol_len = 1;
571 else
573 assert(*pos == '\n');
574 eol_len = 1;
576 pos += eol_len;
578 if (!editor->bEmulateVersion10 && eol_len == 3)
580 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
581 const WCHAR space = ' ';
582 run_insert( editor, cursor, style, &space, 1, 0 );
584 else
586 const WCHAR cr = '\r', *eol_str = str;
588 if (!editor->bEmulateVersion10)
590 eol_str = &cr;
591 eol_len = 1;
594 if (cursor->nOffset == cursor->pRun->member.run.len)
596 run = run_next( &cursor->pRun->member.run );
597 if (!run) run = &cursor->pRun->member.run;
599 else
601 if (cursor->nOffset) run_split( editor, cursor );
602 run = &cursor->pRun->member.run;
605 new_para = para_split( editor, run, style, eol_str, eol_len, 0 );
606 end_run = para_end_run( para_prev( new_para ) );
608 /* Move any cursors that were at the end of the previous run to the beginning of the new para */
609 prev = run_prev( end_run );
610 if (prev)
612 int i;
613 for (i = 0; i < editor->nCursors; i++)
615 if (editor->pCursors[i].pRun == run_get_di( prev ) &&
616 editor->pCursors[i].nOffset == prev->len)
618 editor->pCursors[i].pPara = para_get_di( new_para );
619 editor->pCursors[i].pRun = run_get_di( run );
620 editor->pCursors[i].nOffset = 0;
627 len -= pos - str;
628 str = pos;
632 /* Move the cursor nRelOfs characters (either forwards or backwards)
633 * If final_eop is TRUE, allow moving the cursor to the end of the final eop.
635 * returns the actual number of characters moved.
637 int ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs, BOOL final_eop)
639 cursor->nOffset += nRelOfs;
640 if (cursor->nOffset < 0)
642 cursor->nOffset += cursor->pRun->member.run.nCharOfs;
643 if (cursor->nOffset >= 0)
645 /* new offset in the same paragraph */
646 do {
647 cursor->pRun = run_get_di( run_prev( &cursor->pRun->member.run ) );
648 } while (cursor->nOffset < cursor->pRun->member.run.nCharOfs);
649 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
650 return nRelOfs;
653 cursor->nOffset += cursor->pPara->member.para.nCharOfs;
654 if (cursor->nOffset <= 0)
656 /* moved to the start of the text */
657 nRelOfs -= cursor->nOffset;
658 ME_SetCursorToStart(editor, cursor);
659 return nRelOfs;
662 /* new offset in a previous paragraph */
663 do {
664 cursor->pPara = para_get_di( para_prev( &cursor->pPara->member.para ) );
665 } while (cursor->nOffset < cursor->pPara->member.para.nCharOfs);
666 cursor->nOffset -= cursor->pPara->member.para.nCharOfs;
668 cursor->pRun = run_get_di( para_end_run( &cursor->pPara->member.para ) );
669 while (cursor->nOffset < cursor->pRun->member.run.nCharOfs) {
670 cursor->pRun = run_get_di( run_prev( &cursor->pRun->member.run ) );
672 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
674 else if (cursor->nOffset >= cursor->pRun->member.run.len)
676 ME_Paragraph *next_para;
677 int new_offset;
679 new_offset = ME_GetCursorOfs(cursor);
680 next_para = para_next( &cursor->pPara->member.para );
681 if (new_offset < next_para->nCharOfs)
683 /* new offset in the same paragraph */
684 do {
685 cursor->nOffset -= cursor->pRun->member.run.len;
686 cursor->pRun = run_get_di( run_next( &cursor->pRun->member.run ) );
687 } while (cursor->nOffset >= cursor->pRun->member.run.len);
688 return nRelOfs;
691 if (new_offset >= ME_GetTextLength(editor) + (final_eop ? 1 : 0))
693 /* new offset at the end of the text */
694 ME_SetCursorToEnd(editor, cursor, final_eop);
695 nRelOfs -= new_offset - (ME_GetTextLength(editor) + (final_eop ? 1 : 0));
696 return nRelOfs;
699 /* new offset in a following paragraph */
700 do {
701 cursor->pPara = para_get_di( next_para );
702 next_para = para_next( next_para );
703 } while (new_offset >= next_para->nCharOfs);
705 cursor->nOffset = new_offset - cursor->pPara->member.para.nCharOfs;
706 cursor->pRun = run_get_di( para_first_run( &cursor->pPara->member.para ) );
707 while (cursor->nOffset >= cursor->pRun->member.run.len)
709 cursor->nOffset -= cursor->pRun->member.run.len;
710 cursor->pRun = run_get_di( run_next( &cursor->pRun->member.run ) );
712 } /* else new offset is in the same run */
713 return nRelOfs;
717 BOOL
718 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
720 ME_Run *run = &cursor->pRun->member.run, *other_run;
721 ME_Paragraph *para = &cursor->pPara->member.para;
722 int nOffset = cursor->nOffset;
724 if (nRelOfs == -1)
726 /* Backward movement */
727 while (TRUE)
729 nOffset = ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset, WB_MOVEWORDLEFT );
730 if (nOffset) break;
731 other_run = run_prev( run );
732 if (other_run)
734 if (ME_CallWordBreakProc( editor, get_text( other_run, 0 ), other_run->len, other_run->len - 1, WB_ISDELIMITER )
735 && !(run->nFlags & MERF_ENDPARA)
736 && !(&cursor->pRun->member.run == run && cursor->nOffset == 0)
737 && !ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, 0, WB_ISDELIMITER ))
738 break;
739 run = other_run;
740 nOffset = other_run->len;
742 else
744 if (&cursor->pRun->member.run == run && cursor->nOffset == 0)
746 para = run->para;
747 /* Skip empty start of table row paragraph */
748 if (para_prev( para ) && para_prev( para )->nFlags & MEPF_ROWSTART)
749 para = para_prev( para );
750 /* Paragraph breaks are treated as separate words */
751 if (!para_prev( para )) return FALSE;
752 para = para_prev( para );
753 run = para_end_run( para );
755 break;
759 else
761 /* Forward movement */
762 BOOL last_delim = FALSE;
764 while (TRUE)
766 if (last_delim && !ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset, WB_ISDELIMITER ))
767 break;
768 nOffset = ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset, WB_MOVEWORDRIGHT );
769 if (nOffset < run->len) break;
770 other_run = run_next( run );
771 if (other_run)
773 last_delim = ME_CallWordBreakProc( editor, get_text( run, 0 ), run->len, nOffset - 1, WB_ISDELIMITER );
774 run = other_run;
775 nOffset = 0;
777 else
779 para = para_next( para );
780 if (para_get_di( para )->type == diTextEnd)
782 if (&cursor->pRun->member.run == run) return FALSE;
783 nOffset = 0;
784 break;
786 if (para->nFlags & MEPF_ROWSTART) para = para_next( para );
787 if (&cursor->pRun->member.run == run) run = para_first_run( para );
788 nOffset = 0;
789 break;
793 cursor->pPara = para_get_di( para );
794 cursor->pRun = run_get_di( run );
795 cursor->nOffset = nOffset;
796 return TRUE;
800 static void
801 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
803 /* pCursor[0] is the end of the selection
804 * pCursor[1] is the start of the selection (or the position selection anchor)
805 * pCursor[2] and [3] are the selection anchors that are backed up
806 * so they are kept when the selection changes for drag selection.
809 editor->nSelectionType = selectionType;
810 switch(selectionType)
812 case stPosition:
813 break;
814 case stWord:
815 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
816 editor->pCursors[1] = editor->pCursors[0];
817 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
818 break;
819 case stParagraph:
820 editor->pCursors[1] = editor->pCursors[0];
822 editor->pCursors[0].pRun = run_get_di( para_end_run( &editor->pCursors[0].pPara->member.para ) );
823 editor->pCursors[0].pPara = para_get_di( editor->pCursors[0].pRun->member.run.para );
824 editor->pCursors[0].nOffset = editor->pCursors[0].pRun->member.run.len;
826 editor->pCursors[1].pRun = run_get_di( para_first_run( &editor->pCursors[1].pPara->member.para ) );
827 editor->pCursors[1].nOffset = 0;
828 break;
829 case stLine:
831 ME_Row *row = row_from_cursor( editor->pCursors );
833 row_first_cursor( row, editor->pCursors + 1 );
834 row_end_cursor( row, editor->pCursors, TRUE );
835 break;
837 case stDocument:
838 /* Select everything with cursor anchored from the start of the text */
839 ME_SetCursorToStart(editor, &editor->pCursors[1]);
840 ME_SetCursorToEnd(editor, &editor->pCursors[0], TRUE);
841 break;
842 default: assert(0);
844 /* Store the anchor positions for extending the selection. */
845 editor->pCursors[2] = editor->pCursors[0];
846 editor->pCursors[3] = editor->pCursors[1];
849 int ME_GetCursorOfs(const ME_Cursor *cursor)
851 return cursor->pPara->member.para.nCharOfs
852 + cursor->pRun->member.run.nCharOfs + cursor->nOffset;
855 /* Helper function for cursor_from_virtual_coords() to find paragraph within tables */
856 static ME_Paragraph *pixel_pos_in_table_row( int x, int y, ME_Paragraph *para )
858 ME_Cell *cell, *next_cell;
860 assert( para->nFlags & MEPF_ROWSTART );
861 cell = table_row_first_cell( para );
862 assert( cell );
864 /* find the cell we are in */
865 while ((next_cell = cell_next( cell )) != NULL)
867 if (x < next_cell->pt.x)
869 para = cell_first_para( cell );
870 /* Found the cell, but there might be multiple paragraphs in
871 * the cell, so need to search down the cell for the paragraph. */
872 while (cell == para_cell( para ))
874 if (y < para->pt.y + para->nHeight)
876 if (para->nFlags & MEPF_ROWSTART) return pixel_pos_in_table_row( x, y, para );
877 else return para;
879 para = para_next( para );
881 /* Past the end of the cell, so go back to the last cell paragraph */
882 return para_prev( para );
884 cell = next_cell;
886 /* Return table row delimiter */
887 para = table_row_end( para );
888 assert( para->nFlags & MEPF_ROWEND );
889 assert( para->fmt.dwMask & PFM_TABLEROWDELIMITER );
890 assert( para->fmt.wEffects & PFE_TABLEROWDELIMITER );
891 return para;
894 static BOOL row_cursor( ME_TextEditor *editor, ME_Row *row, int x,
895 ME_Cursor *cursor )
897 ME_Run *run, *last;
898 BOOL exact = TRUE;
900 if (x < row->pt.x)
902 x = row->pt.x;
903 exact = FALSE;
906 run = row_first_run( row );
907 assert( run );
908 cursor->nOffset = 0;
911 if (x >= run->pt.x && x < run->pt.x + run->nWidth)
913 cursor->nOffset = ME_CharFromPoint( editor, x - run->pt.x, run, TRUE, TRUE );
914 cursor->pRun = run_get_di( run );
915 cursor->pPara = para_get_di( run->para );
916 return exact;
918 last = run;
919 run = row_next_run( row, run );
920 } while (run);
922 run = last;
924 cursor->pRun = run_get_di( run );
925 cursor->pPara = para_get_di( run->para );
926 return FALSE;
929 /* Finds the run and offset from the pixel position.
931 * x & y are pixel positions in virtual coordinates into the rich edit control,
932 * so client coordinates must first be adjusted by the scroll position.
934 * If final_eop is TRUE consider the final end-of-paragraph.
936 * returns TRUE if the result was exactly under the cursor, otherwise returns
937 * FALSE, and result is set to the closest position to the coordinates.
939 static BOOL cursor_from_virtual_coords( ME_TextEditor *editor, int x, int y,
940 ME_Cursor *result, BOOL final_eop )
942 ME_Paragraph *para = editor_first_para( editor );
943 ME_Row *row = NULL, *next_row;
944 BOOL isExact = TRUE;
946 x -= editor->rcFormat.left;
947 y -= editor->rcFormat.top;
949 /* find paragraph */
950 for (; para_next( para ); para = para_next( para ))
952 if (y < para->pt.y + para->nHeight)
954 if (para->nFlags & MEPF_ROWSTART)
955 para = pixel_pos_in_table_row( x, y, para );
956 y -= para->pt.y;
957 row = para_first_row( para );
958 break;
960 else if (para->nFlags & MEPF_ROWSTART)
962 para = table_row_end( para );
965 /* find row */
966 while (row)
968 if (y < row->pt.y + row->nHeight) break;
969 next_row = row_next( row );
970 if (!next_row) break;
971 row = next_row;
974 if (!row && !final_eop && para_prev( para ))
976 /* The position is below the last paragraph, so the last row will be used
977 * rather than the end of the text, so the x position will be used to
978 * determine the offset closest to the pixel position. */
979 isExact = FALSE;
980 row = para_end_row( para_prev( para ) );
983 if (row) return row_cursor( editor, row, x, result ) && isExact;
985 ME_SetCursorToEnd(editor, result, TRUE);
986 return FALSE;
990 /* Sets the cursor to the position closest to the pixel position
992 * x & y are pixel positions in client coordinates.
994 * isExact will be set to TRUE if the run is directly under the pixel
995 * position, FALSE if it not, unless isExact is set to NULL.
997 * return FALSE if outside client area and the cursor is not set,
998 * otherwise TRUE is returned.
1000 BOOL ME_CharFromPos(ME_TextEditor *editor, int x, int y,
1001 ME_Cursor *cursor, BOOL *isExact)
1003 RECT rc;
1004 BOOL bResult;
1006 ITextHost_TxGetClientRect(editor->texthost, &rc);
1007 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
1008 if (isExact) *isExact = FALSE;
1009 return FALSE;
1011 x += editor->horz_si.nPos;
1012 y += editor->vert_si.nPos;
1013 bResult = cursor_from_virtual_coords( editor, x, y, cursor, FALSE );
1014 if (isExact) *isExact = bResult;
1015 return TRUE;
1020 /* Extends the selection with a word, line, or paragraph selection type.
1022 * The selection is anchored by editor->pCursors[2-3] such that the text
1023 * between the anchors will remain selected, and one end will be extended.
1025 * editor->pCursors[0] should have the position to extend the selection to
1026 * before this function is called.
1028 * Nothing will be done if editor->nSelectionType equals stPosition.
1030 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1032 ME_Cursor tmp_cursor;
1033 int curOfs, anchorStartOfs, anchorEndOfs;
1034 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1035 return;
1036 curOfs = ME_GetCursorOfs(&editor->pCursors[0]);
1037 anchorStartOfs = ME_GetCursorOfs(&editor->pCursors[3]);
1038 anchorEndOfs = ME_GetCursorOfs(&editor->pCursors[2]);
1040 tmp_cursor = editor->pCursors[0];
1041 editor->pCursors[0] = editor->pCursors[2];
1042 editor->pCursors[1] = editor->pCursors[3];
1043 if (curOfs < anchorStartOfs)
1045 /* Extend the left side of selection */
1046 editor->pCursors[1] = tmp_cursor;
1047 switch (editor->nSelectionType)
1049 case stWord:
1050 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1051 break;
1053 case stLine:
1055 ME_Row *row = row_from_cursor( editor->pCursors + 1 );
1056 row_first_cursor( row, editor->pCursors + 1 );
1057 break;
1060 case stParagraph:
1061 editor->pCursors[1].pRun = run_get_di( para_first_run( &editor->pCursors[1].pPara->member.para ) );
1062 editor->pCursors[1].nOffset = 0;
1063 break;
1065 default:
1066 break;
1069 else if (curOfs >= anchorEndOfs)
1071 /* Extend the right side of selection */
1072 editor->pCursors[0] = tmp_cursor;
1073 switch (editor->nSelectionType)
1075 case stWord:
1076 ME_MoveCursorWords( editor, &editor->pCursors[0], +1 );
1077 break;
1079 case stLine:
1081 ME_Row *row = row_from_cursor( editor->pCursors );
1082 row_end_cursor( row, editor->pCursors, TRUE );
1083 break;
1086 case stParagraph:
1087 editor->pCursors[0].pRun = run_get_di( para_end_run( &editor->pCursors[0].pPara->member.para ) );
1088 editor->pCursors[0].pPara = para_get_di( editor->pCursors[0].pRun->member.run.para );
1089 editor->pCursors[0].nOffset = editor->pCursors[0].pRun->member.run.len;
1090 break;
1092 default:
1093 break;
1098 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1100 ME_Cursor tmp_cursor;
1101 BOOL is_selection = FALSE, is_shift;
1103 editor->nUDArrowX = -1;
1105 x += editor->horz_si.nPos;
1106 y += editor->vert_si.nPos;
1108 tmp_cursor = editor->pCursors[0];
1109 is_selection = ME_IsSelection(editor);
1110 is_shift = GetKeyState(VK_SHIFT) < 0;
1112 cursor_from_virtual_coords( editor, x, y, &editor->pCursors[0], FALSE );
1114 if (x >= editor->rcFormat.left || is_shift)
1116 if (clickNum > 1)
1118 editor->pCursors[1] = editor->pCursors[0];
1119 if (is_shift) {
1120 if (x >= editor->rcFormat.left)
1121 ME_SelectByType(editor, stWord);
1122 else
1123 ME_SelectByType(editor, stParagraph);
1124 } else if (clickNum % 2 == 0) {
1125 ME_SelectByType(editor, stWord);
1126 } else {
1127 ME_SelectByType(editor, stParagraph);
1130 else if (!is_shift)
1132 editor->nSelectionType = stPosition;
1133 editor->pCursors[1] = editor->pCursors[0];
1135 else if (!is_selection)
1137 editor->nSelectionType = stPosition;
1138 editor->pCursors[1] = tmp_cursor;
1140 else if (editor->nSelectionType != stPosition)
1142 ME_ExtendAnchorSelection(editor);
1145 else
1147 if (clickNum < 2) {
1148 ME_SelectByType(editor, stLine);
1149 } else if (clickNum % 2 == 0 || is_shift) {
1150 ME_SelectByType(editor, stParagraph);
1151 } else {
1152 ME_SelectByType(editor, stDocument);
1155 ME_InvalidateSelection(editor);
1156 update_caret(editor);
1157 ME_SendSelChange(editor);
1160 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1162 ME_Cursor tmp_cursor;
1164 if (editor->nSelectionType == stDocument)
1165 return;
1166 x += editor->horz_si.nPos;
1167 y += editor->vert_si.nPos;
1169 tmp_cursor = editor->pCursors[0];
1170 /* FIXME: do something with the return value of cursor_from_virtual_coords */
1171 cursor_from_virtual_coords( editor, x, y, &tmp_cursor, TRUE );
1173 ME_InvalidateSelection(editor);
1174 editor->pCursors[0] = tmp_cursor;
1175 ME_ExtendAnchorSelection(editor);
1177 if (editor->nSelectionType != stPosition &&
1178 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1180 /* The scroll the cursor towards the other end, since it was the one
1181 * extended by ME_ExtendAnchorSelection */
1182 ME_EnsureVisible(editor, &editor->pCursors[1]);
1183 } else {
1184 ME_EnsureVisible(editor, &editor->pCursors[0]);
1187 ME_InvalidateSelection(editor);
1188 update_caret(editor);
1189 ME_SendSelChange(editor);
1192 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1194 ME_Run *run = &pCursor->pRun->member.run;
1195 int x;
1197 if (editor->nUDArrowX != -1)
1198 x = editor->nUDArrowX;
1199 else
1201 x = run->pt.x;
1202 x += ME_PointFromChar( editor, run, pCursor->nOffset, TRUE );
1203 editor->nUDArrowX = x;
1205 return x;
1209 static void
1210 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs, BOOL extend)
1212 ME_DisplayItem *pRun = pCursor->pRun;
1213 ME_Paragraph *old_para = &pCursor->pPara->member.para, *new_para;
1214 ME_DisplayItem *pItem;
1215 int x = ME_GetXForArrow(editor, pCursor);
1217 if (nRelOfs == -1)
1219 /* start of this row */
1220 pItem = ME_FindItemBack(pRun, diStartRow);
1221 assert(pItem);
1222 /* start of the previous row */
1223 pItem = ME_FindItemBack(pItem, diStartRow);
1224 if (!pItem) /* row not found */
1226 if (extend)
1227 ME_SetCursorToStart(editor, pCursor);
1228 return;
1230 new_para = &ME_GetParagraph(pItem)->member.para;
1231 if (old_para->nFlags & MEPF_ROWEND ||
1232 (para_cell( old_para ) && para_cell( old_para ) != para_cell( new_para )))
1234 /* Brought out of a cell */
1235 new_para = para_prev( table_row_start( old_para ));
1236 if (!new_para) return; /* At the top, so don't go anywhere. */
1237 pItem = ME_FindItemFwd( para_get_di( new_para ), diStartRow);
1239 if (new_para->nFlags & MEPF_ROWEND)
1241 /* Brought into a table row */
1242 ME_Cell *cell = table_row_end_cell( new_para );
1243 while (x < cell->pt.x && cell_prev( cell ))
1244 cell = cell_prev( cell );
1245 if (cell_next( cell )) /* else - we are still at the end of the row */
1246 pItem = ME_FindItemBack( cell_get_di( cell_next( cell ) ), diStartRow );
1249 else
1251 /* start of the next row */
1252 pItem = ME_FindItemFwd(pRun, diStartRow);
1253 if (!pItem) /* row not found */
1255 if (extend)
1256 ME_SetCursorToEnd(editor, pCursor, TRUE);
1257 return;
1259 new_para = &ME_GetParagraph(pItem)->member.para;
1260 if (old_para->nFlags & MEPF_ROWSTART ||
1261 (para_cell( old_para ) && para_cell( old_para ) != para_cell( new_para )))
1263 /* Brought out of a cell */
1264 new_para = para_next( table_row_end( old_para ) );
1265 if (!para_next( new_para )) return; /* At the bottom, so don't go anywhere. */
1266 pItem = ME_FindItemFwd( para_get_di( new_para ), diStartRow );
1268 if (new_para->nFlags & MEPF_ROWSTART)
1270 /* Brought into a table row */
1271 ME_Cell *cell = table_row_first_cell( new_para );
1272 while (cell_next( cell ) && x >= cell_next( cell )->pt.x)
1273 cell = cell_next( cell );
1274 pItem = ME_FindItemFwd( cell_get_di( cell ), diStartRow );
1277 if (!pItem)
1279 /* row not found - ignore */
1280 return;
1282 row_cursor( editor, &pItem->member.row, x, pCursor );
1283 assert(pCursor->pRun);
1284 assert(pCursor->pRun->type == diRun);
1287 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1289 ME_DisplayItem *p = ME_FindItemFwd(editor->pBuffer->pFirst, diStartRow);
1291 if (editor->vert_si.nPos < p->member.row.nHeight)
1293 ME_SetCursorToStart(editor, pCursor);
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;
1298 } else {
1299 ME_DisplayItem *pRun = pCursor->pRun;
1300 ME_DisplayItem *pLast;
1301 int x, y, yd, yp;
1302 int yOldScrollPos = editor->vert_si.nPos;
1304 x = ME_GetXForArrow(editor, pCursor);
1306 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1307 assert(p->type == diStartRow);
1308 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1309 y = yp + p->member.row.pt.y;
1311 ME_ScrollUp(editor, editor->sizeWindow.cy);
1312 /* Only move the cursor by the amount scrolled. */
1313 yd = y + editor->vert_si.nPos - yOldScrollPos;
1314 pLast = p;
1316 do {
1317 p = ME_FindItemBack(p, diStartRowOrParagraph);
1318 if (!p)
1319 break;
1320 if (p->type == diParagraph) { /* crossing paragraphs */
1321 if (p->member.para.prev_para == NULL)
1322 break;
1323 yp = p->member.para.prev_para->member.para.pt.y;
1324 continue;
1326 y = yp + p->member.row.pt.y;
1327 if (y < yd)
1328 break;
1329 pLast = p;
1330 } while(1);
1332 row_cursor( editor, &pLast->member.row, x, pCursor );
1334 assert(pCursor->pRun);
1335 assert(pCursor->pRun->type == diRun);
1338 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1340 ME_DisplayItem *pLast;
1341 int x, y;
1343 /* Find y position of the last row */
1344 pLast = editor->pBuffer->pLast;
1345 y = pLast->member.para.prev_para->member.para.pt.y
1346 + ME_FindItemBack(pLast, diStartRow)->member.row.pt.y;
1348 x = ME_GetXForArrow(editor, pCursor);
1350 if (editor->vert_si.nPos >= y - editor->sizeWindow.cy)
1352 ME_SetCursorToEnd(editor, pCursor, FALSE);
1353 } else {
1354 ME_DisplayItem *pRun = pCursor->pRun;
1355 ME_DisplayItem *p;
1356 int yd, yp;
1357 int yOldScrollPos = editor->vert_si.nPos;
1359 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1360 assert(p->type == diStartRow);
1361 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1362 y = yp + p->member.row.pt.y;
1364 /* For native richedit controls:
1365 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1366 * v4.1 can scroll past this position here. */
1367 ME_ScrollDown(editor, editor->sizeWindow.cy);
1368 /* Only move the cursor by the amount scrolled. */
1369 yd = y + editor->vert_si.nPos - yOldScrollPos;
1370 pLast = p;
1372 do {
1373 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1374 if (!p)
1375 break;
1376 if (p->type == diParagraph) {
1377 yp = p->member.para.pt.y;
1378 continue;
1380 y = yp + p->member.row.pt.y;
1381 if (y >= yd)
1382 break;
1383 pLast = p;
1384 } while(1);
1386 row_cursor( editor, &pLast->member.row, x, pCursor );
1388 assert(pCursor->pRun);
1389 assert(pCursor->pRun->type == diRun);
1392 static void ME_ArrowHome( ME_TextEditor *editor, ME_Cursor *cursor )
1394 ME_Row *row = row_from_cursor( cursor );
1396 row_first_cursor( row, cursor );
1399 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1401 ME_SetCursorToStart(editor, pCursor);
1404 static void ME_ArrowEnd( ME_TextEditor *editor, ME_Cursor *cursor )
1406 ME_Row *row = row_from_cursor( cursor );
1408 row_end_cursor( row, cursor, FALSE );
1411 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1413 ME_SetCursorToEnd(editor, pCursor, FALSE);
1416 BOOL ME_IsSelection(ME_TextEditor *editor)
1418 return editor->pCursors[0].pRun != editor->pCursors[1].pRun ||
1419 editor->pCursors[0].nOffset != editor->pCursors[1].nOffset;
1422 void ME_DeleteSelection(ME_TextEditor *editor)
1424 int from, to;
1425 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
1426 int nEndCursor = nStartCursor ^ 1;
1427 ME_DeleteTextAtCursor(editor, nStartCursor, to - from);
1428 editor->pCursors[nEndCursor] = editor->pCursors[nStartCursor];
1431 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1433 return style_get_insert_style( editor, editor->pCursors );
1436 void ME_SendSelChange(ME_TextEditor *editor)
1438 SELCHANGE sc;
1440 sc.nmhdr.hwndFrom = NULL;
1441 sc.nmhdr.idFrom = 0;
1442 sc.nmhdr.code = EN_SELCHANGE;
1443 ME_GetSelectionOfs(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1444 sc.seltyp = SEL_EMPTY;
1445 if (sc.chrg.cpMin != sc.chrg.cpMax)
1446 sc.seltyp |= SEL_TEXT;
1447 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* what were RICHEDIT authors thinking ? */
1448 sc.seltyp |= SEL_MULTICHAR;
1450 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1452 ME_ClearTempStyle(editor);
1454 editor->notified_cr = sc.chrg;
1456 if (editor->nEventMask & ENM_SELCHANGE)
1458 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1459 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1460 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1461 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1462 ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
1467 BOOL
1468 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1470 int nCursor = 0;
1471 ME_Cursor *p = &editor->pCursors[nCursor];
1472 ME_Cursor tmp_curs = *p;
1473 BOOL success = FALSE;
1475 ME_CheckCharOffsets(editor);
1476 switch(nVKey) {
1477 case VK_LEFT:
1478 if (ctrl)
1479 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1480 else
1481 success = ME_MoveCursorChars(editor, &tmp_curs, -1, extend);
1482 break;
1483 case VK_RIGHT:
1484 if (ctrl)
1485 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1486 else
1487 success = ME_MoveCursorChars(editor, &tmp_curs, +1, extend);
1488 break;
1489 case VK_UP:
1490 ME_MoveCursorLines(editor, &tmp_curs, -1, extend);
1491 break;
1492 case VK_DOWN:
1493 ME_MoveCursorLines(editor, &tmp_curs, +1, extend);
1494 break;
1495 case VK_PRIOR:
1496 ME_ArrowPageUp(editor, &tmp_curs);
1497 break;
1498 case VK_NEXT:
1499 ME_ArrowPageDown(editor, &tmp_curs);
1500 break;
1501 case VK_HOME: {
1502 if (ctrl)
1503 ME_ArrowCtrlHome(editor, &tmp_curs);
1504 else
1505 ME_ArrowHome(editor, &tmp_curs);
1506 break;
1508 case VK_END:
1509 if (ctrl)
1510 ME_ArrowCtrlEnd(editor, &tmp_curs);
1511 else
1512 ME_ArrowEnd(editor, &tmp_curs);
1513 break;
1516 if (!extend)
1517 editor->pCursors[1] = tmp_curs;
1518 *p = tmp_curs;
1520 ME_InvalidateSelection(editor);
1521 ME_Repaint(editor);
1522 hide_caret(editor);
1523 ME_EnsureVisible(editor, &tmp_curs);
1524 update_caret(editor);
1525 ME_SendSelChange(editor);
1526 return success;