richedit: Directly get start and end of text on Ctrl-Home or Ctrl-End.
[wine/multimedia.git] / dlls / riched20 / caret.c
blobedbdf4673f971248903bb9fb8a5be04765a82d00
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 static BOOL
28 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs);
30 void ME_GetSelection(ME_TextEditor *editor, int *from, int *to)
32 *from = ME_GetCursorOfs(editor, 0);
33 *to = ME_GetCursorOfs(editor, 1);
35 if (*from > *to)
37 int tmp = *from;
38 *from = *to;
39 *to = tmp;
43 int ME_GetTextLength(ME_TextEditor *editor)
45 ME_DisplayItem *pLast = editor->pBuffer->pLast;
46 return ME_CharOfsFromRunOfs(editor, pLast->member.para.prev_para,
47 ME_FindItemBack(pLast, diRun), 0);
51 int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how)
53 int length;
55 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
56 return E_INVALIDARG;
57 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
58 return E_INVALIDARG;
60 length = ME_GetTextLength(editor);
62 if ((editor->styleFlags & ES_MULTILINE)
63 && (how->flags & GTL_USECRLF)
64 && !editor->bEmulateVersion10) /* Ignore GTL_USECRLF flag in 1.0 emulation */
65 length += editor->nParagraphs - 1;
67 if (how->flags & GTL_NUMBYTES)
69 CPINFO cpinfo;
71 if (how->codepage == 1200)
72 return length * 2;
73 if (how->flags & GTL_PRECISE)
74 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
75 if (GetCPInfo(how->codepage, &cpinfo))
76 return length * cpinfo.MaxCharSize;
77 ERR("Invalid codepage %u\n", how->codepage);
78 return E_INVALIDARG;
80 return length;
84 int ME_SetSelection(ME_TextEditor *editor, int from, int to)
86 int selectionEnd = 0;
87 const int len = ME_GetTextLength(editor);
89 /* all negative values are effectively the same */
90 if (from < 0)
91 from = -1;
92 if (to < 0)
93 to = -1;
95 /* select all */
96 if (from == 0 && to == -1)
98 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
99 editor->pCursors[1].nOffset = 0;
100 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
101 editor->pCursors[0].nOffset = 0;
102 ME_InvalidateSelection(editor);
103 ME_ClearTempStyle(editor);
104 return len + 1;
107 /* if both values are equal and also out of bound, that means to */
108 /* put the selection at the end of the text */
109 if ((from == to) && (to < 0 || to > len))
111 selectionEnd = 1;
113 else
115 /* if from is negative and to is positive then selection is */
116 /* deselected and caret moved to end of the current selection */
117 if (from < 0)
119 int start, end;
120 ME_GetSelection(editor, &start, &end);
121 editor->pCursors[1] = editor->pCursors[0];
122 ME_Repaint(editor);
123 ME_ClearTempStyle(editor);
124 return end;
127 /* adjust to if it's a negative value */
128 if (to < 0)
129 to = len + 1;
131 /* flip from and to if they are reversed */
132 if (from>to)
134 int tmp = from;
135 from = to;
136 to = tmp;
139 /* after fiddling with the values, we find from > len && to > len */
140 if (from > len)
141 selectionEnd = 1;
142 /* special case with to too big */
143 else if (to > len)
144 to = len + 1;
147 if (selectionEnd)
149 editor->pCursors[1].pRun = editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
150 editor->pCursors[1].nOffset = editor->pCursors[0].nOffset = 0;
151 ME_InvalidateSelection(editor);
152 ME_ClearTempStyle(editor);
153 return len;
156 ME_CursorFromCharOfs(editor, from, &editor->pCursors[1]);
157 ME_CursorFromCharOfs(editor, to, &editor->pCursors[0]);
158 /* Selection is not allowed in the middle of an end paragraph run. */
159 if (editor->pCursors[1].pRun->member.run.nFlags & MERF_ENDPARA)
160 editor->pCursors[1].nOffset = 0;
161 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA)
162 editor->pCursors[0].nOffset = 0;
163 return to;
167 void
168 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
169 int *x, int *y, int *height)
171 ME_DisplayItem *pCursorRun = pCursor->pRun;
172 ME_DisplayItem *pSizeRun = pCursor->pRun;
174 assert(height && x && y);
175 assert(!(ME_GetParagraph(pCursorRun)->member.para.nFlags & MEPF_REWRAP));
176 assert(pCursor->pRun);
177 assert(pCursor->pRun->type == diRun);
179 if (pCursorRun->type == diRun) {
180 ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
182 if (row) {
183 HDC hDC = ITextHost_TxGetDC(editor->texthost);
184 ME_Context c;
185 ME_DisplayItem *run = pCursorRun;
186 ME_DisplayItem *para = NULL;
187 SIZE sz = {0, 0};
189 ME_InitContext(&c, editor, hDC);
191 if (!pCursor->nOffset)
193 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrParagraph);
194 assert(prev);
195 if (prev->type == diRun)
196 pSizeRun = prev;
198 assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
199 para = ME_FindItemBack(row, diParagraph);
200 assert(para);
201 assert(para->type == diParagraph);
202 if (editor->bCaretAtEnd && !pCursor->nOffset &&
203 run == ME_FindItemFwd(row, diRun))
205 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
206 assert(tmp);
207 if (tmp->type == diRun)
209 row = ME_FindItemBack(tmp, diStartRow);
210 pSizeRun = run = tmp;
211 assert(run);
212 assert(run->type == diRun);
213 sz = ME_GetRunSize(&c, &para->member.para,
214 &run->member.run, ME_StrLen(run->member.run.strText),
215 row->member.row.nLMargin);
218 if (pCursor->nOffset) {
219 sz = ME_GetRunSize(&c, &para->member.para, &run->member.run, pCursor->nOffset,
220 row->member.row.nLMargin);
223 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
224 *x = c.rcView.left + run->member.run.pt.x + sz.cx - editor->horz_si.nPos;
225 *y = c.rcView.top + para->member.para.pt.y + row->member.row.nBaseline
226 + run->member.run.pt.y - pSizeRun->member.run.nAscent - editor->vert_si.nPos;
227 ME_DestroyContext(&c);
228 return;
231 *height = 10; /* FIXME use global font */
232 *x = 0;
233 *y = 0;
237 void
238 ME_MoveCaret(ME_TextEditor *editor)
240 int x, y, height;
242 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
243 if(editor->bHaveFocus && !ME_IsSelection(editor))
245 x = min(x, editor->rcFormat.right-1);
246 ITextHost_TxCreateCaret(editor->texthost, NULL, 0, height);
247 ITextHost_TxSetCaretPos(editor->texthost, x, y);
252 void ME_ShowCaret(ME_TextEditor *ed)
254 ME_MoveCaret(ed);
255 if(ed->bHaveFocus && !ME_IsSelection(ed))
256 ITextHost_TxShowCaret(ed->texthost, TRUE);
259 void ME_HideCaret(ME_TextEditor *ed)
261 if(!ed->bHaveFocus || ME_IsSelection(ed))
263 ITextHost_TxShowCaret(ed->texthost, FALSE);
264 DestroyCaret();
268 BOOL ME_InternalDeleteText(ME_TextEditor *editor, int nOfs, int nChars,
269 BOOL bForce)
271 ME_Cursor c;
272 int shift = 0;
273 int totalChars = nChars;
274 ME_DisplayItem *start_para;
276 /* Prevent deletion past last end of paragraph run. */
277 nChars = min(nChars, ME_GetTextLength(editor) - nOfs);
279 ME_CursorFromCharOfs(editor, nOfs, &c);
280 start_para = ME_GetParagraph(c.pRun);
282 if (!bForce)
284 ME_ProtectPartialTableDeletion(editor, nOfs, &nChars);
285 if (nChars == 0)
286 return FALSE;
289 while(nChars > 0)
291 ME_Run *run;
292 ME_CursorFromCharOfs(editor, nOfs+nChars, &c);
293 if (!c.nOffset &&
294 nOfs+nChars == (c.pRun->member.run.nCharOfs
295 + ME_GetParagraph(c.pRun)->member.para.nCharOfs))
297 /* We aren't deleting anything in this run, so we will go back to the
298 * last run we are deleting text in. */
299 c.pRun = ME_FindItemBack(c.pRun, diRun);
300 c.nOffset = c.pRun->member.run.strText->nLen;
302 run = &c.pRun->member.run;
303 if (run->nFlags & MERF_ENDPARA) {
304 int eollen = c.pRun->member.run.strText->nLen;
305 BOOL keepFirstParaFormat;
307 if (!ME_FindItemFwd(c.pRun, diParagraph))
309 return TRUE;
311 keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
312 run->nCharOfs);
313 if (!editor->bEmulateVersion10) /* v4.1 */
315 ME_DisplayItem *next_para = ME_FindItemFwd(c.pRun, diParagraphOrEnd);
316 ME_DisplayItem *this_para = next_para->member.para.prev_para;
318 /* The end of paragraph before a table row is only deleted if there
319 * is nothing else on the line before it. */
320 if (this_para == start_para &&
321 next_para->member.para.nFlags & MEPF_ROWSTART)
323 /* If the paragraph will be empty, then it should be deleted, however
324 * it still might have text right now which would inherit the
325 * MEPF_STARTROW property if we joined it right now.
326 * Instead we will delete it after the preceding text is deleted. */
327 if (nOfs > this_para->member.para.nCharOfs) {
328 /* Skip this end of line. */
329 nChars -= (eollen < nChars) ? eollen : nChars;
330 continue;
332 keepFirstParaFormat = TRUE;
335 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun), keepFirstParaFormat);
336 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
337 ME_CheckCharOffsets(editor);
338 nChars -= (eollen < nChars) ? eollen : nChars;
339 continue;
341 else
343 ME_Cursor cursor;
344 int nCharsToDelete = min(nChars, c.nOffset);
345 int i;
347 c.nOffset -= nCharsToDelete;
349 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
351 cursor = c;
352 /* nChars is the number of characters that should be deleted from the
353 PRECEDING runs (these BEFORE cursor.pRun)
354 nCharsToDelete is a number of chars to delete from THIS run */
355 nChars -= nCharsToDelete;
356 shift -= nCharsToDelete;
357 TRACE("Deleting %d (remaning %d) chars at %d in '%s' (%d)\n",
358 nCharsToDelete, nChars, c.nOffset,
359 debugstr_w(run->strText->szData), run->strText->nLen);
361 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
363 /* undo = reinsert whole run */
364 /* nOfs is a character offset (from the start of the document
365 to the current (deleted) run */
366 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
367 if (pUndo)
368 pUndo->di.member.run.nCharOfs = nOfs+nChars;
370 else
372 /* undo = reinsert partial run */
373 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
374 if (pUndo) {
375 ME_DestroyString(pUndo->di.member.run.strText);
376 pUndo->di.member.run.nCharOfs = nOfs+nChars;
377 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
380 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
381 TRACE("Shift value: %d\n", shift);
382 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
384 /* update cursors (including c) */
385 for (i=-1; i<editor->nCursors; i++) {
386 ME_Cursor *pThisCur = editor->pCursors + i;
387 if (i == -1) pThisCur = &c;
388 if (pThisCur->pRun == cursor.pRun) {
389 if (pThisCur->nOffset > cursor.nOffset) {
390 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
391 pThisCur->nOffset = cursor.nOffset;
392 else
393 pThisCur->nOffset -= nCharsToDelete;
394 assert(pThisCur->nOffset >= 0);
395 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
397 if (pThisCur->nOffset == ME_StrVLen(run->strText))
399 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
400 assert(pThisCur->pRun->type == diRun);
401 pThisCur->nOffset = 0;
406 /* c = updated data now */
408 if (c.pRun == cursor.pRun)
409 ME_SkipAndPropagateCharOffset(c.pRun, shift);
410 else
411 ME_PropagateCharOffset(c.pRun, shift);
413 if (!ME_StrVLen(cursor.pRun->member.run.strText))
415 TRACE("Removing useless run\n");
416 ME_Remove(cursor.pRun);
417 ME_DestroyDisplayItem(cursor.pRun);
420 shift = 0;
422 ME_CheckCharOffsets(editor);
424 continue;
427 return TRUE;
430 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
432 assert(nCursor>=0 && nCursor<editor->nCursors);
433 /* text operations set modified state */
434 editor->nModifyStep = 1;
435 return ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars,
436 FALSE);
439 static ME_DisplayItem *
440 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
441 const WCHAR *str, int len, ME_Style *style,
442 int flags)
444 ME_Cursor *p = &editor->pCursors[nCursor];
446 editor->bCaretAtEnd = FALSE;
448 assert(p->pRun->type == diRun);
450 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
454 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
456 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
457 ME_DisplayItem *di;
458 WCHAR space = ' ';
460 /* FIXME no no no */
461 if (ME_IsSelection(editor))
462 ME_DeleteSelection(editor);
464 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
465 MERF_GRAPHICS);
466 di->member.run.ole_obj = ALLOC_OBJ(*reo);
467 ME_CopyReObject(di->member.run.ole_obj, reo);
468 ME_SendSelChange(editor);
472 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
474 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
475 ME_DisplayItem *di;
476 WCHAR space = ' ';
478 /* FIXME no no no */
479 if (ME_IsSelection(editor))
480 ME_DeleteSelection(editor);
482 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
483 MERF_ENDROW);
484 ME_SendSelChange(editor);
488 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
489 const WCHAR *str, int len, ME_Style *style)
491 const WCHAR *pos;
492 ME_Cursor *p = NULL;
493 int oldLen;
495 /* FIXME really HERE ? */
496 if (ME_IsSelection(editor))
497 ME_DeleteSelection(editor);
499 /* FIXME: is this too slow? */
500 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
501 oldLen = ME_GetTextLength(editor);
503 /* text operations set modified state */
504 editor->nModifyStep = 1;
506 assert(style);
508 assert(nCursor>=0 && nCursor<editor->nCursors);
509 if (len == -1)
510 len = lstrlenW(str);
512 /* grow the text limit to fit our text */
513 if(editor->nTextLimit < oldLen +len)
514 editor->nTextLimit = oldLen + len;
516 pos = str;
518 while (len)
520 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
521 while(pos - str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
522 pos++;
524 if (pos != str) { /* handle text */
525 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
526 } else if (*pos == '\t') { /* handle tabs */
527 WCHAR tab = '\t';
528 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
529 pos++;
530 } else { /* handle EOLs */
531 ME_DisplayItem *tp, *end_run;
532 ME_Style *tmp_style;
533 int eol_len = 0;
535 /* Find number of CR and LF in end of paragraph run */
536 if (*pos =='\r')
538 if (len > 1 && pos[1] == '\n')
539 eol_len = 2;
540 else if (len > 2 && pos[1] == '\r' && pos[2] == '\n')
541 eol_len = 3;
542 else
543 eol_len = 1;
544 } else {
545 assert(*pos == '\n');
546 eol_len = 1;
548 pos += eol_len;
550 if (!editor->bEmulateVersion10 && eol_len == 3)
552 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
553 WCHAR space = ' ';
554 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
555 } else {
556 ME_String *eol_str;
558 if (!editor->bEmulateVersion10) {
559 WCHAR cr = '\r';
560 eol_str = ME_MakeStringN(&cr, 1);
561 } else {
562 eol_str = ME_MakeStringN(str, eol_len);
565 p = &editor->pCursors[nCursor];
566 if (p->nOffset) {
567 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
568 p = &editor->pCursors[nCursor];
570 tmp_style = ME_GetInsertStyle(editor, nCursor);
571 /* ME_SplitParagraph increases style refcount */
572 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, eol_str, 0);
573 p->pRun = ME_FindItemFwd(tp, diRun);
574 end_run = ME_FindItemBack(tp, diRun);
575 ME_ReleaseStyle(end_run->member.run.style);
576 end_run->member.run.style = tmp_style;
577 p->nOffset = 0;
580 len -= pos - str;
581 str = pos;
586 static BOOL
587 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
589 ME_DisplayItem *pRun = pCursor->pRun;
591 if (nRelOfs == -1)
593 if (!pCursor->nOffset)
595 do {
596 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
597 assert(pRun);
598 switch (pRun->type)
600 case diRun:
601 break;
602 case diParagraph:
603 if (pRun->member.para.prev_para->type == diTextStart)
604 return FALSE;
605 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
606 /* every paragraph ought to have at least one run */
607 assert(pRun && pRun->type == diRun);
608 assert(pRun->member.run.nFlags & MERF_ENDPARA);
609 break;
610 default:
611 assert(pRun->type != diRun && pRun->type != diParagraph);
612 return FALSE;
614 } while (RUN_IS_HIDDEN(&pRun->member.run) ||
615 pRun->member.run.nFlags & MERF_HIDDEN);
616 pCursor->pRun = pRun;
617 if (pRun->member.run.nFlags & MERF_ENDPARA)
618 pCursor->nOffset = 0;
619 else
620 pCursor->nOffset = pRun->member.run.strText->nLen;
623 if (pCursor->nOffset)
624 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
625 return TRUE;
627 else
629 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
631 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
633 if (new_ofs < pRun->member.run.strText->nLen)
635 pCursor->nOffset = new_ofs;
636 return TRUE;
639 do {
640 pRun = ME_FindItemFwd(pRun, diRun);
641 } while (pRun && (RUN_IS_HIDDEN(&pRun->member.run) ||
642 pRun->member.run.nFlags & MERF_HIDDEN));
643 if (pRun)
645 pCursor->pRun = pRun;
646 pCursor->nOffset = 0;
647 return TRUE;
650 return FALSE;
654 static BOOL
655 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
657 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
658 int nOffset = cursor->nOffset;
660 if (nRelOfs == -1)
662 /* Backward movement */
663 while (TRUE)
665 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
666 nOffset, WB_MOVEWORDLEFT);
667 if (nOffset)
668 break;
669 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
670 if (pOtherRun->type == diRun)
672 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
673 pOtherRun->member.run.strText->nLen - 1,
674 WB_ISDELIMITER)
675 && !(pRun->member.run.nFlags & MERF_ENDPARA)
676 && !(cursor->pRun == pRun && cursor->nOffset == 0)
677 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
678 WB_ISDELIMITER))
679 break;
680 pRun = pOtherRun;
681 nOffset = pOtherRun->member.run.strText->nLen;
683 else if (pOtherRun->type == diParagraph)
685 if (cursor->pRun == pRun && cursor->nOffset == 0)
687 /* Skip empty start of table row paragraph */
688 if (pOtherRun->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART)
689 pOtherRun = pOtherRun->member.para.prev_para;
690 /* Paragraph breaks are treated as separate words */
691 if (pOtherRun->member.para.prev_para->type == diTextStart)
692 return FALSE;
694 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
696 break;
700 else
702 /* Forward movement */
703 BOOL last_delim = FALSE;
705 while (TRUE)
707 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
708 nOffset, WB_ISDELIMITER))
709 break;
710 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
711 nOffset, WB_MOVEWORDRIGHT);
712 if (nOffset < pRun->member.run.strText->nLen)
713 break;
714 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
715 if (pOtherRun->type == diRun)
717 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
718 nOffset - 1, WB_ISDELIMITER);
719 pRun = pOtherRun;
720 nOffset = 0;
722 else if (pOtherRun->type == diParagraph)
724 if (pOtherRun->member.para.nFlags & MEPF_ROWSTART)
725 pOtherRun = pOtherRun->member.para.next_para;
726 if (cursor->pRun == pRun)
727 pRun = ME_FindItemFwd(pOtherRun, diRun);
728 nOffset = 0;
729 break;
731 else /* diTextEnd */
733 if (cursor->pRun == pRun)
734 return FALSE;
735 nOffset = 0;
736 break;
740 cursor->pRun = pRun;
741 cursor->nOffset = nOffset;
742 return TRUE;
746 static void
747 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
749 /* pCursor[0] is the end of the selection
750 * pCursor[1] is the start of the selection (or the position selection anchor)
751 * pCursor[2] and [3] are the selection anchors that are backed up
752 * so they are kept when the selection changes for drag selection.
755 editor->nSelectionType = selectionType;
756 switch(selectionType)
758 case stPosition:
759 break;
760 case stWord:
761 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
762 editor->pCursors[1] = editor->pCursors[0];
763 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
764 break;
765 case stLine:
766 case stParagraph:
768 ME_DisplayItem *pItem;
769 ME_DIType fwdSearchType, backSearchType;
770 if (selectionType == stParagraph) {
771 backSearchType = diParagraph;
772 fwdSearchType = diParagraphOrEnd;
773 } else {
774 backSearchType = diStartRow;
775 fwdSearchType = diStartRowOrParagraphOrEnd;
777 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
778 assert(pItem);
779 if (pItem->type == diTextEnd)
780 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
781 else
782 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
783 editor->pCursors[0].nOffset = 0;
785 pItem = ME_FindItemBack(pItem, backSearchType);
786 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
787 editor->pCursors[1].nOffset = 0;
788 break;
790 case stDocument:
791 /* Select everything with cursor anchored from the start of the text */
792 editor->nSelectionType = stDocument;
793 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
794 editor->pCursors[1].nOffset = 0;
795 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
796 editor->pCursors[0].nOffset = 0;
797 break;
798 default: assert(0);
800 /* Store the anchor positions for extending the selection. */
801 editor->pCursors[2] = editor->pCursors[0];
802 editor->pCursors[3] = editor->pCursors[1];
805 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
807 ME_Cursor *pCursor = &editor->pCursors[nCursor];
808 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
809 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
812 /* Helper function for ME_FindPixelPos to find paragraph within tables */
813 static ME_DisplayItem* ME_FindPixelPosInTableRow(int x, int y,
814 ME_DisplayItem *para)
816 ME_DisplayItem *cell, *next_cell;
817 assert(para->member.para.nFlags & MEPF_ROWSTART);
818 cell = para->member.para.next_para->member.para.pCell;
819 assert(cell);
821 /* find the cell we are in */
822 while ((next_cell = cell->member.cell.next_cell) != NULL) {
823 if (x < next_cell->member.cell.pt.x)
825 para = ME_FindItemFwd(cell, diParagraph);
826 /* Found the cell, but there might be multiple paragraphs in
827 * the cell, so need to search down the cell for the paragraph. */
828 while (cell == para->member.para.pCell) {
829 if (y < para->member.para.pt.y + para->member.para.nHeight)
831 if (para->member.para.nFlags & MEPF_ROWSTART)
832 return ME_FindPixelPosInTableRow(x, y, para);
833 else
834 return para;
836 para = para->member.para.next_para;
838 /* Past the end of the cell, so go back to the last cell paragraph */
839 return para->member.para.prev_para;
841 cell = next_cell;
843 /* Return table row delimiter */
844 para = ME_FindItemFwd(cell, diParagraph);
845 assert(para->member.para.nFlags & MEPF_ROWEND);
846 assert(para->member.para.pFmt->dwMask & PFM_TABLEROWDELIMITER);
847 assert(para->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER);
848 return para;
851 static BOOL ME_ReturnFoundPos(ME_TextEditor *editor, ME_DisplayItem *found,
852 ME_Cursor *result, int rx, BOOL isExact)
854 assert(found);
855 assert(found->type == diRun);
856 if ((found->member.run.nFlags & MERF_ENDPARA) || rx < 0)
857 rx = 0;
858 result->pRun = found;
859 result->nOffset = ME_CharFromPointCursor(editor, rx, &found->member.run);
860 if (editor->pCursors[0].nOffset == found->member.run.strText->nLen && rx)
862 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
863 result->nOffset = 0;
865 return isExact;
868 /* Finds the run and offset from the pixel position.
870 * x & y are pixel positions in virtual coordinates into the rich edit control,
871 * so client coordinates must first be adjusted by the scroll position.
873 * returns TRUE if the result was exactly under the cursor, otherwise returns
874 * FALSE, and result is set to the closest position to the coordinates.
876 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
877 ME_Cursor *result, BOOL *is_eol)
879 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
880 ME_DisplayItem *last = NULL;
881 int rx = 0;
882 BOOL isExact = TRUE;
884 x -= editor->rcFormat.left;
885 y -= editor->rcFormat.top;
887 if (is_eol)
888 *is_eol = 0;
890 /* find paragraph */
891 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
893 assert(p->type == diParagraph);
894 if (y < p->member.para.pt.y + p->member.para.nHeight)
896 if (p->member.para.nFlags & MEPF_ROWSTART)
897 p = ME_FindPixelPosInTableRow(x, y, p);
898 y -= p->member.para.pt.y;
899 p = ME_FindItemFwd(p, diStartRow);
900 break;
901 } else if (p->member.para.nFlags & MEPF_ROWSTART) {
902 p = ME_GetTableRowEnd(p);
905 /* find row */
906 for (; p != editor->pBuffer->pLast; )
908 ME_DisplayItem *pp;
909 assert(p->type == diStartRow);
910 if (y < p->member.row.pt.y + p->member.row.nHeight)
912 p = ME_FindItemFwd(p, diRun);
913 break;
915 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
916 if (pp->type != diStartRow)
918 p = ME_FindItemFwd(p, diRun);
919 break;
921 p = pp;
923 if (p == editor->pBuffer->pLast)
925 /* The position is below the last paragraph, so the last row will be used
926 * rather than the end of the text, so the x position will be used to
927 * determine the offset closest to the pixel position. */
928 isExact = FALSE;
929 p = ME_FindItemBack(p, diStartRow);
930 if (p != NULL){
931 p = ME_FindItemFwd(p, diRun);
933 else
935 p = editor->pBuffer->pLast;
938 for (; p != editor->pBuffer->pLast; p = p->next)
940 switch (p->type)
942 case diRun:
943 rx = x - p->member.run.pt.x;
944 if (rx < p->member.run.nWidth)
945 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
946 break;
947 case diStartRow:
948 isExact = FALSE;
949 p = ME_FindItemFwd(p, diRun);
950 if (is_eol) *is_eol = 1;
951 rx = 0; /* FIXME not sure */
952 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
953 case diCell:
954 case diParagraph:
955 case diTextEnd:
956 isExact = FALSE;
957 rx = 0; /* FIXME not sure */
958 p = last;
959 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
960 default: assert(0);
962 last = p;
964 result->pRun = ME_FindItemBack(p, diRun);
965 result->nOffset = 0;
966 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
967 return FALSE;
971 /* Returns the character offset closest to the pixel position
973 * x & y are pixel positions in client coordinates.
975 * isExact will be set to TRUE if the run is directly under the pixel
976 * position, FALSE if it not, unless isExact is set to NULL.
978 int ME_CharFromPos(ME_TextEditor *editor, int x, int y, BOOL *isExact)
980 ME_Cursor cursor;
981 RECT rc;
982 BOOL bResult;
984 ITextHost_TxGetClientRect(editor->texthost, &rc);
985 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
986 if (isExact) *isExact = FALSE;
987 return -1;
989 x += editor->horz_si.nPos;
990 y += editor->vert_si.nPos;
991 bResult = ME_FindPixelPos(editor, x, y, &cursor, NULL);
992 if (isExact) *isExact = bResult;
993 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
994 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
999 /* Extends the selection with a word, line, or paragraph selection type.
1001 * The selection is anchored by editor->pCursors[2-3] such that the text
1002 * between the anchors will remain selected, and one end will be extended.
1004 * editor->pCursors[0] should have the position to extend the selection to
1005 * before this function is called.
1007 * Nothing will be done if editor->nSelectionType equals stPosition.
1009 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1011 ME_Cursor tmp_cursor;
1012 int curOfs, anchorStartOfs, anchorEndOfs;
1013 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1014 return;
1015 curOfs = ME_GetCursorOfs(editor, 0);
1016 anchorStartOfs = ME_GetCursorOfs(editor, 3);
1017 anchorEndOfs = ME_GetCursorOfs(editor, 2);
1019 tmp_cursor = editor->pCursors[0];
1020 editor->pCursors[0] = editor->pCursors[2];
1021 editor->pCursors[1] = editor->pCursors[3];
1022 if (curOfs < anchorStartOfs)
1024 /* Extend the left side of selection */
1025 editor->pCursors[1] = tmp_cursor;
1026 if (editor->nSelectionType == stWord)
1027 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1028 else
1030 ME_DisplayItem *pItem;
1031 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1032 diStartRowOrParagraph:diParagraph);
1033 pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
1034 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
1035 editor->pCursors[1].nOffset = 0;
1038 else if (curOfs >= anchorEndOfs)
1040 /* Extend the right side of selection */
1041 editor->pCursors[0] = tmp_cursor;
1042 if (editor->nSelectionType == stWord)
1043 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
1044 else
1046 ME_DisplayItem *pItem;
1047 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1048 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
1049 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
1050 if (pItem->type == diTextEnd)
1051 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
1052 else
1053 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
1054 editor->pCursors[0].nOffset = 0;
1059 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1061 ME_Cursor tmp_cursor;
1062 int is_selection = 0;
1063 BOOL is_shift;
1065 editor->nUDArrowX = -1;
1067 x += editor->horz_si.nPos;
1068 y += editor->vert_si.nPos;
1070 tmp_cursor = editor->pCursors[0];
1071 is_selection = ME_IsSelection(editor);
1072 is_shift = GetKeyState(VK_SHIFT) < 0;
1074 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
1076 if (x >= editor->rcFormat.left || is_shift)
1078 if (clickNum > 1)
1080 editor->pCursors[1] = editor->pCursors[0];
1081 if (is_shift) {
1082 if (x >= editor->rcFormat.left)
1083 ME_SelectByType(editor, stWord);
1084 else
1085 ME_SelectByType(editor, stParagraph);
1086 } else if (clickNum % 2 == 0) {
1087 ME_SelectByType(editor, stWord);
1088 } else {
1089 ME_SelectByType(editor, stParagraph);
1092 else if (!is_shift)
1094 editor->nSelectionType = stPosition;
1095 editor->pCursors[1] = editor->pCursors[0];
1097 else if (!is_selection)
1099 editor->nSelectionType = stPosition;
1100 editor->pCursors[1] = tmp_cursor;
1102 else if (editor->nSelectionType != stPosition)
1104 ME_ExtendAnchorSelection(editor);
1107 else
1109 if (clickNum < 2) {
1110 ME_SelectByType(editor, stLine);
1111 } else if (clickNum % 2 == 0 || is_shift) {
1112 ME_SelectByType(editor, stParagraph);
1113 } else {
1114 ME_SelectByType(editor, stDocument);
1117 ME_InvalidateSelection(editor);
1118 ITextHost_TxShowCaret(editor->texthost, FALSE);
1119 ME_ShowCaret(editor);
1120 ME_ClearTempStyle(editor);
1121 ME_SendSelChange(editor);
1124 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1126 ME_Cursor tmp_cursor;
1128 if (editor->nSelectionType == stDocument)
1129 return;
1130 x += editor->horz_si.nPos;
1131 y += editor->vert_si.nPos;
1133 tmp_cursor = editor->pCursors[0];
1134 /* FIXME: do something with the return value of ME_FindPixelPos */
1135 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
1137 ME_InvalidateSelection(editor);
1138 editor->pCursors[0] = tmp_cursor;
1139 ME_ExtendAnchorSelection(editor);
1141 if (editor->nSelectionType != stPosition &&
1142 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1144 /* The scroll the cursor towards the other end, since it was the one
1145 * extended by ME_ExtendAnchorSelection */
1146 ME_EnsureVisible(editor, &editor->pCursors[1]);
1147 } else {
1148 ME_EnsureVisible(editor, &editor->pCursors[0]);
1151 ME_InvalidateSelection(editor);
1152 ITextHost_TxShowCaret(editor->texthost, FALSE);
1153 ME_ShowCaret(editor);
1154 ME_SendSelChange(editor);
1157 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
1158 int x, int *pOffset, int *pbCaretAtEnd)
1160 ME_DisplayItem *pNext, *pLastRun;
1161 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
1162 assert(pNext->type == diRun);
1163 pLastRun = pNext;
1164 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
1165 if (pOffset) *pOffset = 0;
1166 do {
1167 int run_x = pNext->member.run.pt.x;
1168 int width = pNext->member.run.nWidth;
1169 if (x < run_x)
1171 return pNext;
1173 if (x >= run_x && x < run_x+width)
1175 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
1176 ME_String *s = pNext->member.run.strText;
1177 if (ch < s->nLen) {
1178 if (pOffset)
1179 *pOffset = ch;
1180 return pNext;
1183 pLastRun = pNext;
1184 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
1185 } while(pNext && pNext->type == diRun);
1187 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
1189 pNext = ME_FindItemFwd(pNext, diRun);
1190 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
1191 return pNext;
1192 } else {
1193 return pLastRun;
1197 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1199 ME_DisplayItem *pRun = pCursor->pRun;
1200 int x;
1202 if (editor->nUDArrowX != -1)
1203 x = editor->nUDArrowX;
1204 else {
1205 if (editor->bCaretAtEnd)
1207 pRun = ME_FindItemBack(pRun, diRun);
1208 assert(pRun);
1209 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1211 else {
1212 x = pRun->member.run.pt.x;
1213 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
1215 editor->nUDArrowX = x;
1217 return x;
1221 static void
1222 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1224 ME_DisplayItem *pRun = pCursor->pRun;
1225 ME_DisplayItem *pItem, *pOldPara, *pNewPara;
1226 int x = ME_GetXForArrow(editor, pCursor);
1228 if (editor->bCaretAtEnd && !pCursor->nOffset)
1229 pRun = ME_FindItemBack(pRun, diRun);
1230 if (!pRun)
1231 return;
1232 pOldPara = ME_GetParagraph(pRun);
1233 if (nRelOfs == -1)
1235 /* start of this row */
1236 pItem = ME_FindItemBack(pRun, diStartRow);
1237 assert(pItem);
1238 /* start of the previous row */
1239 pItem = ME_FindItemBack(pItem, diStartRow);
1240 if (!pItem)
1241 return; /* row not found - ignore */
1242 pNewPara = ME_GetParagraph(pItem);
1243 if (pOldPara->member.para.nFlags & MEPF_ROWEND ||
1244 (pOldPara->member.para.pCell &&
1245 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1247 /* Brought out of a cell */
1248 pNewPara = ME_GetTableRowStart(pOldPara)->member.para.prev_para;
1249 if (pNewPara->type == diTextStart)
1250 return; /* At the top, so don't go anywhere. */
1251 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1253 if (pNewPara->member.para.nFlags & MEPF_ROWEND)
1255 /* Brought into a table row */
1256 ME_Cell *cell = &ME_FindItemBack(pNewPara, diCell)->member.cell;
1257 while (x < cell->pt.x && cell->prev_cell)
1258 cell = &cell->prev_cell->member.cell;
1259 if (cell->next_cell) /* else - we are still at the end of the row */
1260 pItem = ME_FindItemBack(cell->next_cell, diStartRow);
1263 else
1265 /* start of the next row */
1266 pItem = ME_FindItemFwd(pRun, diStartRow);
1267 if (!pItem)
1268 return; /* row not found - ignore */
1269 pNewPara = ME_GetParagraph(pItem);
1270 if (pOldPara->member.para.nFlags & MEPF_ROWSTART ||
1271 (pOldPara->member.para.pCell &&
1272 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1274 /* Brought out of a cell */
1275 pNewPara = ME_GetTableRowEnd(pOldPara)->member.para.next_para;
1276 if (pNewPara->type == diTextEnd)
1277 return; /* At the bottom, so don't go anywhere. */
1278 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1280 if (pNewPara->member.para.nFlags & MEPF_ROWSTART)
1282 /* Brought into a table row */
1283 ME_DisplayItem *cell = ME_FindItemFwd(pNewPara, diCell);
1284 while (cell->member.cell.next_cell &&
1285 x >= cell->member.cell.next_cell->member.cell.pt.x)
1286 cell = cell->member.cell.next_cell;
1287 pItem = ME_FindItemFwd(cell, diStartRow);
1290 if (!pItem)
1292 /* row not found - ignore */
1293 return;
1295 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1296 assert(pCursor->pRun);
1297 assert(pCursor->pRun->type == diRun);
1300 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1302 ME_DisplayItem *p = ME_FindItemFwd(editor->pBuffer->pFirst, diStartRow);
1304 if (editor->vert_si.nPos < p->member.row.nHeight)
1306 pCursor->pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
1307 pCursor->nOffset = 0;
1308 editor->bCaretAtEnd = FALSE;
1309 /* Native clears seems to clear this x value on page up at the top
1310 * of the text, but not on page down at the end of the text.
1311 * Doesn't make sense, but we try to be bug for bug compatible. */
1312 editor->nUDArrowX = -1;
1313 } else {
1314 ME_DisplayItem *pRun = pCursor->pRun;
1315 ME_DisplayItem *pLast;
1316 int x, y, ys, yd, yp, yprev;
1317 int yOldScrollPos = editor->vert_si.nPos;
1319 x = ME_GetXForArrow(editor, pCursor);
1320 if (!pCursor->nOffset && editor->bCaretAtEnd)
1321 pRun = ME_FindItemBack(pRun, diRun);
1323 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1324 assert(p->type == diStartRow);
1325 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1326 yprev = ys = y = yp + p->member.row.pt.y;
1328 ME_ScrollUp(editor, editor->sizeWindow.cy);
1329 /* Only move the cursor by the amount scrolled. */
1330 yd = y + editor->vert_si.nPos - yOldScrollPos;
1331 pLast = p;
1333 do {
1334 p = ME_FindItemBack(p, diStartRowOrParagraph);
1335 if (!p)
1336 break;
1337 if (p->type == diParagraph) { /* crossing paragraphs */
1338 if (p->member.para.prev_para == NULL)
1339 break;
1340 yp = p->member.para.prev_para->member.para.pt.y;
1341 continue;
1343 y = yp + p->member.row.pt.y;
1344 if (y < yd)
1345 break;
1346 pLast = p;
1347 yprev = y;
1348 } while(1);
1350 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset,
1351 &editor->bCaretAtEnd);
1353 assert(pCursor->pRun);
1354 assert(pCursor->pRun->type == diRun);
1357 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1359 ME_DisplayItem *pLast;
1360 int x, y;
1362 /* Find y position of the last row */
1363 pLast = editor->pBuffer->pLast;
1364 y = pLast->member.para.prev_para->member.para.pt.y
1365 + ME_FindItemBack(pLast, diStartRow)->member.row.pt.y;
1367 x = ME_GetXForArrow(editor, pCursor);
1369 if (editor->vert_si.nPos >= y - editor->sizeWindow.cy)
1371 pCursor->pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
1372 pCursor->nOffset = 0;
1373 editor->bCaretAtEnd = FALSE;
1374 } else {
1375 ME_DisplayItem *pRun = pCursor->pRun;
1376 ME_DisplayItem *p;
1377 int ys, yd, yp, yprev;
1378 int yOldScrollPos = editor->vert_si.nPos;
1380 if (!pCursor->nOffset && editor->bCaretAtEnd)
1381 pRun = ME_FindItemBack(pRun, diRun);
1383 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1384 assert(p->type == diStartRow);
1385 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1386 yprev = ys = y = yp + p->member.row.pt.y;
1388 /* For native richedit controls:
1389 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1390 * v4.1 can scroll past this position here. */
1391 ME_ScrollDown(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_FindItemFwd(p, diStartRowOrParagraph);
1398 if (!p)
1399 break;
1400 if (p->type == diParagraph) {
1401 yp = p->member.para.pt.y;
1402 continue;
1404 y = yp + p->member.row.pt.y;
1405 if (y >= yd)
1406 break;
1407 pLast = p;
1408 yprev = y;
1409 } while(1);
1411 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset,
1412 &editor->bCaretAtEnd);
1414 assert(pCursor->pRun);
1415 assert(pCursor->pRun->type == diRun);
1418 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1420 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1421 if (pRow) {
1422 ME_DisplayItem *pRun;
1423 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1424 pRow = ME_FindItemBack(pRow, diStartRow);
1425 if (!pRow)
1426 return;
1428 pRun = ME_FindItemFwd(pRow, diRun);
1429 if (pRun) {
1430 pCursor->pRun = pRun;
1431 pCursor->nOffset = 0;
1434 editor->bCaretAtEnd = FALSE;
1437 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1439 pCursor->pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
1440 pCursor->nOffset = 0;
1441 editor->bCaretAtEnd = FALSE;
1444 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1446 ME_DisplayItem *pRow;
1448 if (editor->bCaretAtEnd && !pCursor->nOffset)
1449 return;
1451 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1452 assert(pRow);
1453 if (pRow->type == diStartRow) {
1454 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1455 assert(pRun);
1456 pCursor->pRun = pRun;
1457 pCursor->nOffset = 0;
1458 editor->bCaretAtEnd = TRUE;
1459 return;
1461 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1462 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1463 pCursor->nOffset = 0;
1464 editor->bCaretAtEnd = FALSE;
1467 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1469 pCursor->pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
1470 assert(pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1471 pCursor->nOffset = 0;
1472 editor->bCaretAtEnd = FALSE;
1475 BOOL ME_IsSelection(ME_TextEditor *editor)
1477 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1480 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1482 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1484 if (cdir*dir>0)
1485 return 0;
1486 else
1487 return 1;
1490 void ME_DeleteSelection(ME_TextEditor *editor)
1492 int from, to;
1493 ME_GetSelection(editor, &from, &to);
1494 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1497 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1499 return ME_GetInsertStyle(editor, 0);
1502 void ME_SendSelChange(ME_TextEditor *editor)
1504 SELCHANGE sc;
1506 if (!(editor->nEventMask & ENM_SELCHANGE))
1507 return;
1509 sc.nmhdr.code = EN_SELCHANGE;
1510 ME_GetSelection(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1511 sc.seltyp = SEL_EMPTY;
1512 if (sc.chrg.cpMin != sc.chrg.cpMax)
1513 sc.seltyp |= SEL_TEXT;
1514 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1515 sc.seltyp |= SEL_MULTICHAR;
1516 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1517 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1518 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1519 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1520 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1522 ME_ClearTempStyle(editor);
1524 editor->notified_cr = sc.chrg;
1525 ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
1529 BOOL
1530 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1532 int nCursor = 0;
1533 ME_Cursor *p = &editor->pCursors[nCursor];
1534 ME_Cursor tmp_curs = *p;
1535 BOOL success = FALSE;
1537 ME_CheckCharOffsets(editor);
1538 switch(nVKey) {
1539 case VK_LEFT:
1540 editor->bCaretAtEnd = 0;
1541 if (ctrl)
1542 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1543 else
1544 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1545 break;
1546 case VK_RIGHT:
1547 editor->bCaretAtEnd = 0;
1548 if (ctrl)
1549 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1550 else
1551 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1552 break;
1553 case VK_UP:
1554 ME_MoveCursorLines(editor, &tmp_curs, -1);
1555 break;
1556 case VK_DOWN:
1557 ME_MoveCursorLines(editor, &tmp_curs, +1);
1558 break;
1559 case VK_PRIOR:
1560 ME_ArrowPageUp(editor, &tmp_curs);
1561 break;
1562 case VK_NEXT:
1563 ME_ArrowPageDown(editor, &tmp_curs);
1564 break;
1565 case VK_HOME: {
1566 if (ctrl)
1567 ME_ArrowCtrlHome(editor, &tmp_curs);
1568 else
1569 ME_ArrowHome(editor, &tmp_curs);
1570 editor->bCaretAtEnd = 0;
1571 break;
1573 case VK_END:
1574 if (ctrl)
1575 ME_ArrowCtrlEnd(editor, &tmp_curs);
1576 else
1577 ME_ArrowEnd(editor, &tmp_curs);
1578 break;
1581 if (!extend)
1582 editor->pCursors[1] = tmp_curs;
1583 *p = tmp_curs;
1585 ME_InvalidateSelection(editor);
1586 ME_Repaint(editor);
1587 ITextHost_TxShowCaret(editor->texthost, FALSE);
1588 ME_EnsureVisible(editor, &tmp_curs);
1589 ME_ShowCaret(editor);
1590 ME_SendSelChange(editor);
1591 return success;