include: Windows SDK C headers have snprintf.
[wine.git] / dlls / riched20 / caret.c
blob212c9e9f992fc83dcba6b271277b6feb37925603
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 = editor->pBuffer->pFirst->member.para.next_para;
30 cursor->pRun = ME_FindItemFwd(cursor->pPara, diRun);
31 cursor->nOffset = 0;
34 static void ME_SetCursorToEnd(ME_TextEditor *editor, ME_Cursor *cursor, BOOL final_eop)
36 cursor->pPara = editor->pBuffer->pLast->member.para.prev_para;
37 cursor->pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
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 ME_CursorFromCharOfs(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_DisplayItem *run = pCursor->pRun;
226 ME_DisplayItem *para = pCursor->pPara;
227 ME_DisplayItem *pSizeRun = run;
228 ME_Context c;
229 int run_x;
231 assert(height && x && y);
232 assert(~para->member.para.nFlags & MEPF_REWRAP);
233 assert(run && run->type == diRun);
234 assert(para && para->type == diParagraph);
236 row = ME_FindItemBack(run, diStartRowOrParagraph);
237 assert(row && row->type == diStartRow);
239 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
241 if (!pCursor->nOffset)
243 ME_DisplayItem *prev = ME_FindItemBack(run, diRunOrParagraph);
244 assert(prev);
245 if (prev->type == diRun)
246 pSizeRun = prev;
248 if (editor->bCaretAtEnd && !pCursor->nOffset &&
249 run == ME_FindItemFwd(row, diRun))
251 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
252 assert(tmp);
253 if (tmp->type == diRun)
255 row = ME_FindItemBack(tmp, diStartRow);
256 pSizeRun = run = tmp;
257 assert(run);
258 assert(run->type == diRun);
261 run_x = ME_PointFromCharContext( &c, &run->member.run, pCursor->nOffset, TRUE );
263 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
264 *x = c.rcView.left + run->member.run.pt.x + run_x - editor->horz_si.nPos;
265 *y = c.rcView.top + para->member.para.pt.y + row->member.row.nBaseline
266 + run->member.run.pt.y - pSizeRun->member.run.nAscent
267 - editor->vert_si.nPos;
268 ME_DestroyContext(&c);
269 return;
272 void create_caret(ME_TextEditor *editor)
274 int x, y, height;
276 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
277 ITextHost_TxCreateCaret(editor->texthost, NULL, 0, height);
278 editor->caret_height = height;
279 editor->caret_hidden = TRUE;
282 void show_caret(ME_TextEditor *editor)
284 ITextHost_TxShowCaret(editor->texthost, TRUE);
285 editor->caret_hidden = FALSE;
288 void hide_caret(ME_TextEditor *editor)
290 /* calls to HideCaret are cumulative; do so only once */
291 if (!editor->caret_hidden)
293 ITextHost_TxShowCaret(editor->texthost, FALSE);
294 editor->caret_hidden = TRUE;
298 void update_caret(ME_TextEditor *editor)
300 int x, y, height;
302 if (!editor->bHaveFocus) return;
303 if (!ME_IsSelection(editor))
305 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
306 if (height != editor->caret_height) create_caret(editor);
307 x = min(x, editor->rcFormat.right-1);
308 ITextHost_TxSetCaretPos(editor->texthost, x, y);
309 show_caret(editor);
311 else
312 hide_caret(editor);
315 BOOL ME_InternalDeleteText(ME_TextEditor *editor, ME_Cursor *start,
316 int nChars, BOOL bForce)
318 ME_Cursor c = *start;
319 int nOfs = ME_GetCursorOfs(start), text_len = ME_GetTextLength( editor );
320 int shift = 0;
321 int totalChars = nChars;
322 ME_DisplayItem *start_para;
323 BOOL delete_all = FALSE;
325 /* Prevent deletion past last end of paragraph run. */
326 nChars = min(nChars, text_len - nOfs);
327 if (nChars == text_len) delete_all = TRUE;
328 start_para = c.pPara;
330 if (!bForce)
332 ME_ProtectPartialTableDeletion(editor, &c, &nChars);
333 if (nChars == 0)
334 return FALSE;
337 while(nChars > 0)
339 ME_Run *run;
340 ME_CursorFromCharOfs(editor, nOfs+nChars, &c);
341 if (!c.nOffset &&
342 nOfs+nChars == (c.pRun->member.run.nCharOfs
343 + c.pPara->member.para.nCharOfs))
345 /* We aren't deleting anything in this run, so we will go back to the
346 * last run we are deleting text in. */
347 ME_PrevRun(&c.pPara, &c.pRun, TRUE);
348 c.nOffset = c.pRun->member.run.len;
350 run = &c.pRun->member.run;
351 if (run->nFlags & MERF_ENDPARA) {
352 int eollen = c.pRun->member.run.len;
353 BOOL keepFirstParaFormat;
355 if (!ME_FindItemFwd(c.pRun, diParagraph))
357 return TRUE;
359 keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
360 run->nCharOfs);
361 if (!editor->bEmulateVersion10) /* v4.1 */
363 ME_DisplayItem *next_para = ME_FindItemFwd(c.pRun, diParagraphOrEnd);
364 ME_DisplayItem *this_para = next_para->member.para.prev_para;
366 /* The end of paragraph before a table row is only deleted if there
367 * is nothing else on the line before it. */
368 if (this_para == start_para &&
369 next_para->member.para.nFlags & MEPF_ROWSTART)
371 /* If the paragraph will be empty, then it should be deleted, however
372 * it still might have text right now which would inherit the
373 * MEPF_STARTROW property if we joined it right now.
374 * Instead we will delete it after the preceding text is deleted. */
375 if (nOfs > this_para->member.para.nCharOfs) {
376 /* Skip this end of line. */
377 nChars -= (eollen < nChars) ? eollen : nChars;
378 continue;
380 keepFirstParaFormat = TRUE;
383 ME_JoinParagraphs(editor, c.pPara, keepFirstParaFormat);
384 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
385 ME_CheckCharOffsets(editor);
386 nChars -= (eollen < nChars) ? eollen : nChars;
387 continue;
389 else
391 ME_Cursor cursor;
392 int nCharsToDelete = min(nChars, c.nOffset);
393 int i;
395 c.nOffset -= nCharsToDelete;
397 mark_para_rewrap(editor, ME_FindItemBack(c.pRun, diParagraph));
399 cursor = c;
400 /* nChars is the number of characters that should be deleted from the
401 PRECEDING runs (these BEFORE cursor.pRun)
402 nCharsToDelete is a number of chars to delete from THIS run */
403 nChars -= nCharsToDelete;
404 shift -= nCharsToDelete;
405 TRACE("Deleting %d (remaining %d) chars at %d in %s (%d)\n",
406 nCharsToDelete, nChars, c.nOffset,
407 debugstr_run( run ), run->len);
409 /* nOfs is a character offset (from the start of the document
410 to the current (deleted) run */
411 add_undo_insert_run( editor, nOfs + nChars, get_text( run, c.nOffset ), nCharsToDelete, run->nFlags, run->style );
413 ME_StrDeleteV(run->para->text, run->nCharOfs + c.nOffset, nCharsToDelete);
414 run->len -= nCharsToDelete;
415 TRACE("Post deletion string: %s (%d)\n", debugstr_run( run ), run->len);
416 TRACE("Shift value: %d\n", shift);
418 /* update cursors (including c) */
419 for (i=-1; i<editor->nCursors; i++) {
420 ME_Cursor *pThisCur = editor->pCursors + i;
421 if (i == -1) pThisCur = &c;
422 if (pThisCur->pRun == cursor.pRun) {
423 if (pThisCur->nOffset > cursor.nOffset) {
424 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
425 pThisCur->nOffset = cursor.nOffset;
426 else
427 pThisCur->nOffset -= nCharsToDelete;
428 assert(pThisCur->nOffset >= 0);
429 assert(pThisCur->nOffset <= run->len);
431 if (pThisCur->nOffset == run->len)
433 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
434 assert(pThisCur->pRun->type == diRun);
435 pThisCur->nOffset = 0;
440 /* c = updated data now */
442 if (c.pRun == cursor.pRun)
443 ME_SkipAndPropagateCharOffset(c.pRun, shift);
444 else
445 ME_PropagateCharOffset(c.pRun, shift);
447 if (!cursor.pRun->member.run.len)
449 TRACE("Removing empty run\n");
450 ME_Remove(cursor.pRun);
451 ME_DestroyDisplayItem(cursor.pRun);
454 shift = 0;
456 ME_CheckCharOffsets(editor);
458 continue;
461 if (delete_all) ME_SetDefaultParaFormat( editor, &start_para->member.para.fmt );
462 return TRUE;
465 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
467 assert(nCursor>=0 && nCursor<editor->nCursors);
468 /* text operations set modified state */
469 editor->nModifyStep = 1;
470 return ME_InternalDeleteText(editor, &editor->pCursors[nCursor],
471 nChars, FALSE);
474 static ME_DisplayItem *
475 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
476 const WCHAR *str, int len, ME_Style *style,
477 int flags)
479 ME_Cursor *p = &editor->pCursors[nCursor];
481 editor->bCaretAtEnd = FALSE;
483 assert(p->pRun->type == diRun);
485 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
488 static struct re_object* create_re_object(const REOBJECT *reo)
490 struct re_object *reobj = heap_alloc(sizeof(*reobj));
492 if (!reobj)
494 WARN("Fail to allocate re_object.\n");
495 return NULL;
497 ME_CopyReObject(&reobj->obj, reo, REO_GETOBJ_ALL_INTERFACES);
498 return reobj;
501 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
503 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
504 ME_DisplayItem *di;
505 WCHAR space = ' ';
506 ME_DisplayItem *di_prev = NULL;
507 struct re_object *reobj_prev = NULL;
509 /* FIXME no no no */
510 if (ME_IsSelection(editor))
511 ME_DeleteSelection(editor);
513 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
514 MERF_GRAPHICS);
515 di->member.run.reobj = create_re_object(reo);
517 di_prev = di;
518 while (ME_PrevRun(NULL, &di_prev, TRUE))
520 if (di_prev->member.run.reobj)
522 reobj_prev = di_prev->member.run.reobj;
523 break;
526 if (reobj_prev)
527 list_add_after(&reobj_prev->entry, &di->member.run.reobj->entry);
528 else
529 list_add_head(&editor->reobj_list, &di->member.run.reobj->entry);
531 ME_ReleaseStyle(pStyle);
535 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
537 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
538 WCHAR space = ' ';
540 /* FIXME no no no */
541 if (ME_IsSelection(editor))
542 ME_DeleteSelection(editor);
544 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
545 MERF_ENDROW);
546 ME_ReleaseStyle(pStyle);
550 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
551 const WCHAR *str, int len, ME_Style *style)
553 const WCHAR *pos;
554 ME_Cursor *p = NULL;
555 int oldLen;
557 /* FIXME really HERE ? */
558 if (ME_IsSelection(editor))
559 ME_DeleteSelection(editor);
561 /* FIXME: is this too slow? */
562 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
563 oldLen = ME_GetTextLength(editor);
565 /* text operations set modified state */
566 editor->nModifyStep = 1;
568 assert(style);
570 assert(nCursor>=0 && nCursor<editor->nCursors);
571 if (len == -1)
572 len = lstrlenW(str);
574 /* grow the text limit to fit our text */
575 if(editor->nTextLimit < oldLen +len)
576 editor->nTextLimit = oldLen + len;
578 pos = str;
580 while (len)
582 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
583 while(pos - str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
584 pos++;
586 if (pos != str) { /* handle text */
587 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
588 } else if (*pos == '\t') { /* handle tabs */
589 WCHAR tab = '\t';
590 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
591 pos++;
592 } else { /* handle EOLs */
593 ME_DisplayItem *tp, *end_run, *run, *prev;
594 int eol_len = 0;
596 /* Check if new line is allowed for this control */
597 if (!(editor->styleFlags & ES_MULTILINE))
598 break;
600 /* Find number of CR and LF in end of paragraph run */
601 if (*pos =='\r')
603 if (len > 1 && pos[1] == '\n')
604 eol_len = 2;
605 else if (len > 2 && pos[1] == '\r' && pos[2] == '\n')
606 eol_len = 3;
607 else
608 eol_len = 1;
609 } else {
610 assert(*pos == '\n');
611 eol_len = 1;
613 pos += eol_len;
615 if (!editor->bEmulateVersion10 && eol_len == 3)
617 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
618 WCHAR space = ' ';
619 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
620 } else {
621 const WCHAR cr = '\r', *eol_str = str;
623 if (!editor->bEmulateVersion10)
625 eol_str = &cr;
626 eol_len = 1;
629 p = &editor->pCursors[nCursor];
631 if (p->nOffset == p->pRun->member.run.len)
633 run = ME_FindItemFwd( p->pRun, diRun );
634 if (!run) run = p->pRun;
636 else
638 if (p->nOffset) ME_SplitRunSimple(editor, p);
639 run = p->pRun;
642 tp = ME_SplitParagraph(editor, run, style, eol_str, eol_len, 0);
644 end_run = ME_FindItemBack(tp, diRun);
646 /* Move any cursors that were at the end of the previous run to the beginning of the new para */
647 prev = ME_FindItemBack( end_run, diRun );
648 if (prev)
650 int i;
651 for (i = 0; i < editor->nCursors; i++)
653 if (editor->pCursors[i].pRun == prev &&
654 editor->pCursors[i].nOffset == prev->member.run.len)
656 editor->pCursors[i].pPara = tp;
657 editor->pCursors[i].pRun = run;
658 editor->pCursors[i].nOffset = 0;
665 len -= pos - str;
666 str = pos;
670 /* Move the cursor nRelOfs characters (either forwards or backwards)
671 * If final_eop is TRUE, allow moving the cursor to the end of the final eop.
673 * returns the actual number of characters moved.
675 int ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs, BOOL final_eop)
677 cursor->nOffset += nRelOfs;
678 if (cursor->nOffset < 0)
680 cursor->nOffset += cursor->pRun->member.run.nCharOfs;
681 if (cursor->nOffset >= 0)
683 /* new offset in the same paragraph */
684 do {
685 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
686 } while (cursor->nOffset < cursor->pRun->member.run.nCharOfs);
687 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
688 return nRelOfs;
691 cursor->nOffset += cursor->pPara->member.para.nCharOfs;
692 if (cursor->nOffset <= 0)
694 /* moved to the start of the text */
695 nRelOfs -= cursor->nOffset;
696 ME_SetCursorToStart(editor, cursor);
697 return nRelOfs;
700 /* new offset in a previous paragraph */
701 do {
702 cursor->pPara = cursor->pPara->member.para.prev_para;
703 } while (cursor->nOffset < cursor->pPara->member.para.nCharOfs);
704 cursor->nOffset -= cursor->pPara->member.para.nCharOfs;
706 cursor->pRun = ME_FindItemBack(cursor->pPara->member.para.next_para, diRun);
707 while (cursor->nOffset < cursor->pRun->member.run.nCharOfs) {
708 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
710 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
711 } else if (cursor->nOffset >= cursor->pRun->member.run.len) {
712 ME_DisplayItem *next_para;
713 int new_offset;
715 new_offset = ME_GetCursorOfs(cursor);
716 next_para = cursor->pPara->member.para.next_para;
717 if (new_offset < next_para->member.para.nCharOfs)
719 /* new offset in the same paragraph */
720 do {
721 cursor->nOffset -= cursor->pRun->member.run.len;
722 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
723 } while (cursor->nOffset >= cursor->pRun->member.run.len);
724 return nRelOfs;
727 if (new_offset >= ME_GetTextLength(editor) + (final_eop ? 1 : 0))
729 /* new offset at the end of the text */
730 ME_SetCursorToEnd(editor, cursor, final_eop);
731 nRelOfs -= new_offset - (ME_GetTextLength(editor) + (final_eop ? 1 : 0));
732 return nRelOfs;
735 /* new offset in a following paragraph */
736 do {
737 cursor->pPara = next_para;
738 next_para = next_para->member.para.next_para;
739 } while (new_offset >= next_para->member.para.nCharOfs);
741 cursor->nOffset = new_offset - cursor->pPara->member.para.nCharOfs;
742 cursor->pRun = ME_FindItemFwd(cursor->pPara, diRun);
743 while (cursor->nOffset >= cursor->pRun->member.run.len)
745 cursor->nOffset -= cursor->pRun->member.run.len;
746 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
748 } /* else new offset is in the same run */
749 return nRelOfs;
753 BOOL
754 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
756 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
757 ME_DisplayItem *pPara = cursor->pPara;
758 int nOffset = cursor->nOffset;
760 if (nRelOfs == -1)
762 /* Backward movement */
763 while (TRUE)
765 nOffset = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
766 pRun->member.run.len, nOffset, WB_MOVEWORDLEFT);
767 if (nOffset)
768 break;
769 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
770 if (pOtherRun->type == diRun)
772 if (ME_CallWordBreakProc(editor, get_text( &pOtherRun->member.run, 0 ),
773 pOtherRun->member.run.len,
774 pOtherRun->member.run.len - 1,
775 WB_ISDELIMITER)
776 && !(pRun->member.run.nFlags & MERF_ENDPARA)
777 && !(cursor->pRun == pRun && cursor->nOffset == 0)
778 && !ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
779 pRun->member.run.len, 0,
780 WB_ISDELIMITER))
781 break;
782 pRun = pOtherRun;
783 nOffset = pOtherRun->member.run.len;
785 else if (pOtherRun->type == diParagraph)
787 if (cursor->pRun == pRun && cursor->nOffset == 0)
789 pPara = pOtherRun;
790 /* Skip empty start of table row paragraph */
791 if (pPara->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART)
792 pPara = pPara->member.para.prev_para;
793 /* Paragraph breaks are treated as separate words */
794 if (pPara->member.para.prev_para->type == diTextStart)
795 return FALSE;
797 pRun = ME_FindItemBack(pPara, diRun);
798 pPara = pPara->member.para.prev_para;
800 break;
804 else
806 /* Forward movement */
807 BOOL last_delim = FALSE;
809 while (TRUE)
811 if (last_delim && !ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
812 pRun->member.run.len, nOffset, WB_ISDELIMITER))
813 break;
814 nOffset = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
815 pRun->member.run.len, nOffset, WB_MOVEWORDRIGHT);
816 if (nOffset < pRun->member.run.len)
817 break;
818 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
819 if (pOtherRun->type == diRun)
821 last_delim = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
822 pRun->member.run.len, nOffset - 1, WB_ISDELIMITER);
823 pRun = pOtherRun;
824 nOffset = 0;
826 else if (pOtherRun->type == diParagraph)
828 if (pOtherRun->member.para.nFlags & MEPF_ROWSTART)
829 pOtherRun = pOtherRun->member.para.next_para;
830 if (cursor->pRun == pRun) {
831 pPara = pOtherRun;
832 pRun = ME_FindItemFwd(pPara, diRun);
834 nOffset = 0;
835 break;
837 else /* diTextEnd */
839 if (cursor->pRun == pRun)
840 return FALSE;
841 nOffset = 0;
842 break;
846 cursor->pPara = pPara;
847 cursor->pRun = pRun;
848 cursor->nOffset = nOffset;
849 return TRUE;
853 static void
854 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
856 /* pCursor[0] is the end of the selection
857 * pCursor[1] is the start of the selection (or the position selection anchor)
858 * pCursor[2] and [3] are the selection anchors that are backed up
859 * so they are kept when the selection changes for drag selection.
862 editor->nSelectionType = selectionType;
863 switch(selectionType)
865 case stPosition:
866 break;
867 case stWord:
868 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
869 editor->pCursors[1] = editor->pCursors[0];
870 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
871 break;
872 case stLine:
873 case stParagraph:
875 ME_DisplayItem *pItem;
876 ME_DIType fwdSearchType, backSearchType;
877 if (selectionType == stParagraph) {
878 backSearchType = diParagraph;
879 fwdSearchType = diParagraphOrEnd;
880 } else {
881 backSearchType = diStartRow;
882 fwdSearchType = diStartRowOrParagraphOrEnd;
884 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
885 assert(pItem);
886 if (pItem->type == diTextEnd)
887 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
888 else
889 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
890 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
891 editor->pCursors[0].nOffset = 0;
893 pItem = ME_FindItemBack(pItem, backSearchType);
894 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
895 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
896 editor->pCursors[1].nOffset = 0;
897 break;
899 case stDocument:
900 /* Select everything with cursor anchored from the start of the text */
901 editor->nSelectionType = stDocument;
902 ME_SetCursorToStart(editor, &editor->pCursors[1]);
903 ME_SetCursorToEnd(editor, &editor->pCursors[0], FALSE);
904 break;
905 default: assert(0);
907 /* Store the anchor positions for extending the selection. */
908 editor->pCursors[2] = editor->pCursors[0];
909 editor->pCursors[3] = editor->pCursors[1];
912 int ME_GetCursorOfs(const ME_Cursor *cursor)
914 return cursor->pPara->member.para.nCharOfs
915 + cursor->pRun->member.run.nCharOfs + cursor->nOffset;
918 /* Helper function for ME_FindPixelPos to find paragraph within tables */
919 static ME_DisplayItem* ME_FindPixelPosInTableRow(int x, int y,
920 ME_DisplayItem *para)
922 ME_DisplayItem *cell, *next_cell;
923 assert(para->member.para.nFlags & MEPF_ROWSTART);
924 cell = para->member.para.next_para->member.para.pCell;
925 assert(cell);
927 /* find the cell we are in */
928 while ((next_cell = cell->member.cell.next_cell) != NULL) {
929 if (x < next_cell->member.cell.pt.x)
931 para = ME_FindItemFwd(cell, diParagraph);
932 /* Found the cell, but there might be multiple paragraphs in
933 * the cell, so need to search down the cell for the paragraph. */
934 while (cell == para->member.para.pCell) {
935 if (y < para->member.para.pt.y + para->member.para.nHeight)
937 if (para->member.para.nFlags & MEPF_ROWSTART)
938 return ME_FindPixelPosInTableRow(x, y, para);
939 else
940 return para;
942 para = para->member.para.next_para;
944 /* Past the end of the cell, so go back to the last cell paragraph */
945 return para->member.para.prev_para;
947 cell = next_cell;
949 /* Return table row delimiter */
950 para = ME_FindItemFwd(cell, diParagraph);
951 assert(para->member.para.nFlags & MEPF_ROWEND);
952 assert(para->member.para.fmt.dwMask & PFM_TABLEROWDELIMITER);
953 assert(para->member.para.fmt.wEffects & PFE_TABLEROWDELIMITER);
954 return para;
957 static BOOL ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
958 int x, ME_Cursor *cursor, BOOL *pbCaretAtEnd)
960 ME_DisplayItem *pNext, *pLastRun;
961 ME_Row *row = &pRow->member.row;
962 BOOL exact = TRUE;
964 if (x < row->pt.x)
966 x = row->pt.x;
967 exact = FALSE;
969 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
970 assert(pNext->type == diRun);
971 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
972 cursor->nOffset = 0;
973 do {
974 int run_x = pNext->member.run.pt.x;
975 int width = pNext->member.run.nWidth;
977 if (x >= run_x && x < run_x+width)
979 cursor->nOffset = ME_CharFromPoint(editor, x-run_x, &pNext->member.run, TRUE, TRUE);
980 cursor->pRun = pNext;
981 cursor->pPara = ME_GetParagraph( cursor->pRun );
982 return exact;
984 pLastRun = pNext;
985 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
986 } while(pNext && pNext->type == diRun);
988 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
990 cursor->pRun = ME_FindItemFwd(pNext, diRun);
991 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
993 else
994 cursor->pRun = pLastRun;
996 cursor->pPara = ME_GetParagraph( cursor->pRun );
997 return FALSE;
1000 /* Finds the run and offset from the pixel position.
1002 * x & y are pixel positions in virtual coordinates into the rich edit control,
1003 * so client coordinates must first be adjusted by the scroll position.
1005 * If final_eop is TRUE consider the final end-of-paragraph.
1007 * returns TRUE if the result was exactly under the cursor, otherwise returns
1008 * FALSE, and result is set to the closest position to the coordinates.
1010 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
1011 ME_Cursor *result, BOOL *is_eol, BOOL final_eop)
1013 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
1014 BOOL isExact = TRUE;
1016 x -= editor->rcFormat.left;
1017 y -= editor->rcFormat.top;
1019 if (is_eol)
1020 *is_eol = FALSE;
1022 /* find paragraph */
1023 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
1025 assert(p->type == diParagraph);
1026 if (y < p->member.para.pt.y + p->member.para.nHeight)
1028 if (p->member.para.nFlags & MEPF_ROWSTART)
1029 p = ME_FindPixelPosInTableRow(x, y, p);
1030 y -= p->member.para.pt.y;
1031 p = ME_FindItemFwd(p, diStartRow);
1032 break;
1033 } else if (p->member.para.nFlags & MEPF_ROWSTART) {
1034 p = ME_GetTableRowEnd(p);
1037 /* find row */
1038 for (; p != editor->pBuffer->pLast; )
1040 ME_DisplayItem *pp;
1041 assert(p->type == diStartRow);
1042 if (y < p->member.row.pt.y + p->member.row.nHeight) break;
1043 pp = ME_FindItemFwd(p, diStartRow);
1044 if (!pp) break;
1045 p = pp;
1047 if (p == editor->pBuffer->pLast && !final_eop)
1049 /* The position is below the last paragraph, so the last row will be used
1050 * rather than the end of the text, so the x position will be used to
1051 * determine the offset closest to the pixel position. */
1052 isExact = FALSE;
1053 p = ME_FindItemBack(p, diStartRow);
1054 if (!p) p = editor->pBuffer->pLast;
1057 assert( p->type == diStartRow || p == editor->pBuffer->pLast );
1059 if( p->type == diStartRow )
1060 return ME_FindRunInRow( editor, p, x, result, is_eol ) && isExact;
1062 ME_SetCursorToEnd(editor, result, TRUE);
1063 return FALSE;
1067 /* Sets the cursor to the position closest to the pixel position
1069 * x & y are pixel positions in client coordinates.
1071 * isExact will be set to TRUE if the run is directly under the pixel
1072 * position, FALSE if it not, unless isExact is set to NULL.
1074 * return FALSE if outside client area and the cursor is not set,
1075 * otherwise TRUE is returned.
1077 BOOL ME_CharFromPos(ME_TextEditor *editor, int x, int y,
1078 ME_Cursor *cursor, BOOL *isExact)
1080 RECT rc;
1081 BOOL bResult;
1083 ITextHost_TxGetClientRect(editor->texthost, &rc);
1084 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
1085 if (isExact) *isExact = FALSE;
1086 return FALSE;
1088 x += editor->horz_si.nPos;
1089 y += editor->vert_si.nPos;
1090 bResult = ME_FindPixelPos(editor, x, y, cursor, NULL, FALSE);
1091 if (isExact) *isExact = bResult;
1092 return TRUE;
1097 /* Extends the selection with a word, line, or paragraph selection type.
1099 * The selection is anchored by editor->pCursors[2-3] such that the text
1100 * between the anchors will remain selected, and one end will be extended.
1102 * editor->pCursors[0] should have the position to extend the selection to
1103 * before this function is called.
1105 * Nothing will be done if editor->nSelectionType equals stPosition.
1107 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1109 ME_Cursor tmp_cursor;
1110 int curOfs, anchorStartOfs, anchorEndOfs;
1111 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1112 return;
1113 curOfs = ME_GetCursorOfs(&editor->pCursors[0]);
1114 anchorStartOfs = ME_GetCursorOfs(&editor->pCursors[3]);
1115 anchorEndOfs = ME_GetCursorOfs(&editor->pCursors[2]);
1117 tmp_cursor = editor->pCursors[0];
1118 editor->pCursors[0] = editor->pCursors[2];
1119 editor->pCursors[1] = editor->pCursors[3];
1120 if (curOfs < anchorStartOfs)
1122 /* Extend the left side of selection */
1123 editor->pCursors[1] = tmp_cursor;
1124 if (editor->nSelectionType == stWord)
1125 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1126 else
1128 ME_DisplayItem *pItem;
1129 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1130 diStartRowOrParagraph:diParagraph);
1131 pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
1132 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
1133 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
1134 editor->pCursors[1].nOffset = 0;
1137 else if (curOfs >= anchorEndOfs)
1139 /* Extend the right side of selection */
1140 editor->pCursors[0] = tmp_cursor;
1141 if (editor->nSelectionType == stWord)
1142 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
1143 else
1145 ME_DisplayItem *pItem;
1146 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1147 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
1148 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
1149 if (pItem->type == diTextEnd)
1150 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
1151 else
1152 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
1153 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
1154 editor->pCursors[0].nOffset = 0;
1159 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1161 ME_Cursor tmp_cursor;
1162 BOOL is_selection = FALSE, is_shift;
1164 editor->nUDArrowX = -1;
1166 x += editor->horz_si.nPos;
1167 y += editor->vert_si.nPos;
1169 tmp_cursor = editor->pCursors[0];
1170 is_selection = ME_IsSelection(editor);
1171 is_shift = GetKeyState(VK_SHIFT) < 0;
1173 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd, FALSE);
1175 if (x >= editor->rcFormat.left || is_shift)
1177 if (clickNum > 1)
1179 editor->pCursors[1] = editor->pCursors[0];
1180 if (is_shift) {
1181 if (x >= editor->rcFormat.left)
1182 ME_SelectByType(editor, stWord);
1183 else
1184 ME_SelectByType(editor, stParagraph);
1185 } else if (clickNum % 2 == 0) {
1186 ME_SelectByType(editor, stWord);
1187 } else {
1188 ME_SelectByType(editor, stParagraph);
1191 else if (!is_shift)
1193 editor->nSelectionType = stPosition;
1194 editor->pCursors[1] = editor->pCursors[0];
1196 else if (!is_selection)
1198 editor->nSelectionType = stPosition;
1199 editor->pCursors[1] = tmp_cursor;
1201 else if (editor->nSelectionType != stPosition)
1203 ME_ExtendAnchorSelection(editor);
1206 else
1208 if (clickNum < 2) {
1209 ME_SelectByType(editor, stLine);
1210 } else if (clickNum % 2 == 0 || is_shift) {
1211 ME_SelectByType(editor, stParagraph);
1212 } else {
1213 ME_SelectByType(editor, stDocument);
1216 ME_InvalidateSelection(editor);
1217 update_caret(editor);
1218 ME_SendSelChange(editor);
1221 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1223 ME_Cursor tmp_cursor;
1225 if (editor->nSelectionType == stDocument)
1226 return;
1227 x += editor->horz_si.nPos;
1228 y += editor->vert_si.nPos;
1230 tmp_cursor = editor->pCursors[0];
1231 /* FIXME: do something with the return value of ME_FindPixelPos */
1232 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd, TRUE);
1234 ME_InvalidateSelection(editor);
1235 editor->pCursors[0] = tmp_cursor;
1236 ME_ExtendAnchorSelection(editor);
1238 if (editor->nSelectionType != stPosition &&
1239 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1241 /* The scroll the cursor towards the other end, since it was the one
1242 * extended by ME_ExtendAnchorSelection */
1243 ME_EnsureVisible(editor, &editor->pCursors[1]);
1244 } else {
1245 ME_EnsureVisible(editor, &editor->pCursors[0]);
1248 ME_InvalidateSelection(editor);
1249 update_caret(editor);
1250 ME_SendSelChange(editor);
1253 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1255 ME_DisplayItem *pRun = pCursor->pRun;
1256 int x;
1258 if (editor->nUDArrowX != -1)
1259 x = editor->nUDArrowX;
1260 else {
1261 if (editor->bCaretAtEnd)
1263 pRun = ME_FindItemBack(pRun, diRun);
1264 assert(pRun);
1265 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1267 else {
1268 x = pRun->member.run.pt.x;
1269 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset, TRUE);
1271 editor->nUDArrowX = x;
1273 return x;
1277 static void
1278 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs, BOOL extend)
1280 ME_DisplayItem *pRun = pCursor->pRun;
1281 ME_DisplayItem *pOldPara = pCursor->pPara;
1282 ME_DisplayItem *pItem, *pNewPara;
1283 int x = ME_GetXForArrow(editor, pCursor);
1285 if (editor->bCaretAtEnd && !pCursor->nOffset)
1286 if (!ME_PrevRun(&pOldPara, &pRun, TRUE))
1287 return;
1289 if (nRelOfs == -1)
1291 /* start of this row */
1292 pItem = ME_FindItemBack(pRun, diStartRow);
1293 assert(pItem);
1294 /* start of the previous row */
1295 pItem = ME_FindItemBack(pItem, diStartRow);
1296 if (!pItem) /* row not found */
1298 if (extend)
1299 ME_SetCursorToStart(editor, pCursor);
1300 return;
1302 pNewPara = ME_GetParagraph(pItem);
1303 if (pOldPara->member.para.nFlags & MEPF_ROWEND ||
1304 (pOldPara->member.para.pCell &&
1305 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1307 /* Brought out of a cell */
1308 pNewPara = ME_GetTableRowStart(pOldPara)->member.para.prev_para;
1309 if (pNewPara->type == diTextStart)
1310 return; /* At the top, so don't go anywhere. */
1311 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1313 if (pNewPara->member.para.nFlags & MEPF_ROWEND)
1315 /* Brought into a table row */
1316 ME_Cell *cell = &ME_FindItemBack(pNewPara, diCell)->member.cell;
1317 while (x < cell->pt.x && cell->prev_cell)
1318 cell = &cell->prev_cell->member.cell;
1319 if (cell->next_cell) /* else - we are still at the end of the row */
1320 pItem = ME_FindItemBack(cell->next_cell, diStartRow);
1323 else
1325 /* start of the next row */
1326 pItem = ME_FindItemFwd(pRun, diStartRow);
1327 if (!pItem) /* row not found */
1329 if (extend)
1330 ME_SetCursorToEnd(editor, pCursor, TRUE);
1331 return;
1333 pNewPara = ME_GetParagraph(pItem);
1334 if (pOldPara->member.para.nFlags & MEPF_ROWSTART ||
1335 (pOldPara->member.para.pCell &&
1336 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1338 /* Brought out of a cell */
1339 pNewPara = ME_GetTableRowEnd(pOldPara)->member.para.next_para;
1340 if (pNewPara->type == diTextEnd)
1341 return; /* At the bottom, so don't go anywhere. */
1342 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1344 if (pNewPara->member.para.nFlags & MEPF_ROWSTART)
1346 /* Brought into a table row */
1347 ME_DisplayItem *cell = ME_FindItemFwd(pNewPara, diCell);
1348 while (cell->member.cell.next_cell &&
1349 x >= cell->member.cell.next_cell->member.cell.pt.x)
1350 cell = cell->member.cell.next_cell;
1351 pItem = ME_FindItemFwd(cell, diStartRow);
1354 if (!pItem)
1356 /* row not found - ignore */
1357 return;
1359 ME_FindRunInRow(editor, pItem, x, pCursor, &editor->bCaretAtEnd);
1360 assert(pCursor->pRun);
1361 assert(pCursor->pRun->type == diRun);
1364 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1366 ME_DisplayItem *p = ME_FindItemFwd(editor->pBuffer->pFirst, diStartRow);
1368 if (editor->vert_si.nPos < p->member.row.nHeight)
1370 ME_SetCursorToStart(editor, pCursor);
1371 editor->bCaretAtEnd = FALSE;
1372 /* Native clears seems to clear this x value on page up at the top
1373 * of the text, but not on page down at the end of the text.
1374 * Doesn't make sense, but we try to be bug for bug compatible. */
1375 editor->nUDArrowX = -1;
1376 } else {
1377 ME_DisplayItem *pRun = pCursor->pRun;
1378 ME_DisplayItem *pLast;
1379 int x, y, yd, yp;
1380 int yOldScrollPos = editor->vert_si.nPos;
1382 x = ME_GetXForArrow(editor, pCursor);
1383 if (!pCursor->nOffset && editor->bCaretAtEnd)
1384 pRun = ME_FindItemBack(pRun, diRun);
1386 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1387 assert(p->type == diStartRow);
1388 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1389 y = yp + p->member.row.pt.y;
1391 ME_ScrollUp(editor, editor->sizeWindow.cy);
1392 /* Only move the cursor by the amount scrolled. */
1393 yd = y + editor->vert_si.nPos - yOldScrollPos;
1394 pLast = p;
1396 do {
1397 p = ME_FindItemBack(p, diStartRowOrParagraph);
1398 if (!p)
1399 break;
1400 if (p->type == diParagraph) { /* crossing paragraphs */
1401 if (p->member.para.prev_para == NULL)
1402 break;
1403 yp = p->member.para.prev_para->member.para.pt.y;
1404 continue;
1406 y = yp + p->member.row.pt.y;
1407 if (y < yd)
1408 break;
1409 pLast = p;
1410 } while(1);
1412 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
1414 assert(pCursor->pRun);
1415 assert(pCursor->pRun->type == diRun);
1418 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1420 ME_DisplayItem *pLast;
1421 int x, y;
1423 /* Find y position of the last row */
1424 pLast = editor->pBuffer->pLast;
1425 y = pLast->member.para.prev_para->member.para.pt.y
1426 + ME_FindItemBack(pLast, diStartRow)->member.row.pt.y;
1428 x = ME_GetXForArrow(editor, pCursor);
1430 if (editor->vert_si.nPos >= y - editor->sizeWindow.cy)
1432 ME_SetCursorToEnd(editor, pCursor, FALSE);
1433 editor->bCaretAtEnd = FALSE;
1434 } else {
1435 ME_DisplayItem *pRun = pCursor->pRun;
1436 ME_DisplayItem *p;
1437 int yd, yp;
1438 int yOldScrollPos = editor->vert_si.nPos;
1440 if (!pCursor->nOffset && editor->bCaretAtEnd)
1441 pRun = ME_FindItemBack(pRun, diRun);
1443 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1444 assert(p->type == diStartRow);
1445 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1446 y = yp + p->member.row.pt.y;
1448 /* For native richedit controls:
1449 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1450 * v4.1 can scroll past this position here. */
1451 ME_ScrollDown(editor, editor->sizeWindow.cy);
1452 /* Only move the cursor by the amount scrolled. */
1453 yd = y + editor->vert_si.nPos - yOldScrollPos;
1454 pLast = p;
1456 do {
1457 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1458 if (!p)
1459 break;
1460 if (p->type == diParagraph) {
1461 yp = p->member.para.pt.y;
1462 continue;
1464 y = yp + p->member.row.pt.y;
1465 if (y >= yd)
1466 break;
1467 pLast = p;
1468 } while(1);
1470 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
1472 assert(pCursor->pRun);
1473 assert(pCursor->pRun->type == diRun);
1476 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1478 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1479 if (pRow) {
1480 ME_DisplayItem *pRun;
1481 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1482 pRow = ME_FindItemBack(pRow, diStartRow);
1483 if (!pRow)
1484 return;
1486 pRun = ME_FindItemFwd(pRow, diRun);
1487 if (pRun) {
1488 pCursor->pRun = pRun;
1489 assert(pCursor->pPara == ME_GetParagraph(pRun));
1490 pCursor->nOffset = 0;
1493 editor->bCaretAtEnd = FALSE;
1496 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1498 ME_SetCursorToStart(editor, pCursor);
1499 editor->bCaretAtEnd = FALSE;
1502 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1504 ME_DisplayItem *pRow;
1506 if (editor->bCaretAtEnd && !pCursor->nOffset)
1507 return;
1509 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1510 assert(pRow);
1511 if (pRow->type == diStartRow) {
1512 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1513 assert(pRun);
1514 pCursor->pRun = pRun;
1515 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1516 pCursor->nOffset = 0;
1517 editor->bCaretAtEnd = TRUE;
1518 return;
1520 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1521 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1522 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1523 pCursor->nOffset = 0;
1524 editor->bCaretAtEnd = FALSE;
1527 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1529 ME_SetCursorToEnd(editor, pCursor, FALSE);
1530 editor->bCaretAtEnd = FALSE;
1533 BOOL ME_IsSelection(ME_TextEditor *editor)
1535 return editor->pCursors[0].pRun != editor->pCursors[1].pRun ||
1536 editor->pCursors[0].nOffset != editor->pCursors[1].nOffset;
1539 void ME_DeleteSelection(ME_TextEditor *editor)
1541 int from, to;
1542 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
1543 int nEndCursor = nStartCursor ^ 1;
1544 ME_DeleteTextAtCursor(editor, nStartCursor, to - from);
1545 editor->pCursors[nEndCursor] = editor->pCursors[nStartCursor];
1548 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1550 return ME_GetInsertStyle(editor, 0);
1553 void ME_SendSelChange(ME_TextEditor *editor)
1555 SELCHANGE sc;
1557 sc.nmhdr.hwndFrom = NULL;
1558 sc.nmhdr.idFrom = 0;
1559 sc.nmhdr.code = EN_SELCHANGE;
1560 ME_GetSelectionOfs(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1561 sc.seltyp = SEL_EMPTY;
1562 if (sc.chrg.cpMin != sc.chrg.cpMax)
1563 sc.seltyp |= SEL_TEXT;
1564 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* what were RICHEDIT authors thinking ? */
1565 sc.seltyp |= SEL_MULTICHAR;
1567 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1569 ME_ClearTempStyle(editor);
1571 editor->notified_cr = sc.chrg;
1573 if (editor->nEventMask & ENM_SELCHANGE)
1575 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1576 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1577 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1578 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1579 ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
1584 BOOL
1585 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1587 int nCursor = 0;
1588 ME_Cursor *p = &editor->pCursors[nCursor];
1589 ME_Cursor tmp_curs = *p;
1590 BOOL success = FALSE;
1592 ME_CheckCharOffsets(editor);
1593 switch(nVKey) {
1594 case VK_LEFT:
1595 editor->bCaretAtEnd = FALSE;
1596 if (ctrl)
1597 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1598 else
1599 success = ME_MoveCursorChars(editor, &tmp_curs, -1, extend);
1600 break;
1601 case VK_RIGHT:
1602 editor->bCaretAtEnd = FALSE;
1603 if (ctrl)
1604 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1605 else
1606 success = ME_MoveCursorChars(editor, &tmp_curs, +1, extend);
1607 break;
1608 case VK_UP:
1609 ME_MoveCursorLines(editor, &tmp_curs, -1, extend);
1610 break;
1611 case VK_DOWN:
1612 ME_MoveCursorLines(editor, &tmp_curs, +1, extend);
1613 break;
1614 case VK_PRIOR:
1615 ME_ArrowPageUp(editor, &tmp_curs);
1616 break;
1617 case VK_NEXT:
1618 ME_ArrowPageDown(editor, &tmp_curs);
1619 break;
1620 case VK_HOME: {
1621 if (ctrl)
1622 ME_ArrowCtrlHome(editor, &tmp_curs);
1623 else
1624 ME_ArrowHome(editor, &tmp_curs);
1625 editor->bCaretAtEnd = FALSE;
1626 break;
1628 case VK_END:
1629 if (ctrl)
1630 ME_ArrowCtrlEnd(editor, &tmp_curs);
1631 else
1632 ME_ArrowEnd(editor, &tmp_curs);
1633 break;
1636 if (!extend)
1637 editor->pCursors[1] = tmp_curs;
1638 *p = tmp_curs;
1640 ME_InvalidateSelection(editor);
1641 ME_Repaint(editor);
1642 hide_caret(editor);
1643 ME_EnsureVisible(editor, &tmp_curs);
1644 update_caret(editor);
1645 ME_SendSelChange(editor);
1646 return success;