richedit: Make the ME_GetCursorOfs function more flexible.
[wine/wine-gecko.git] / dlls / riched20 / caret.c
blob2f93f0d86f92c93e49ed4b8a9f2508fd01457418
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->pCursors[0]);
33 *to = ME_GetCursorOfs(&editor->pCursors[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].pPara = editor->pBuffer->pFirst->member.para.next_para;
99 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pCursors[1].pPara, diRun);
100 editor->pCursors[1].nOffset = 0;
101 editor->pCursors[0].pPara = editor->pBuffer->pLast->member.para.prev_para;
102 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
103 editor->pCursors[0].nOffset = 0;
104 ME_InvalidateSelection(editor);
105 ME_ClearTempStyle(editor);
106 return len + 1;
109 /* if both values are equal and also out of bound, that means to */
110 /* put the selection at the end of the text */
111 if ((from == to) && (to < 0 || to > len))
113 selectionEnd = 1;
115 else
117 /* if from is negative and to is positive then selection is */
118 /* deselected and caret moved to end of the current selection */
119 if (from < 0)
121 int start, end;
122 ME_GetSelection(editor, &start, &end);
123 editor->pCursors[1] = editor->pCursors[0];
124 ME_Repaint(editor);
125 ME_ClearTempStyle(editor);
126 return end;
129 /* adjust to if it's a negative value */
130 if (to < 0)
131 to = len + 1;
133 /* flip from and to if they are reversed */
134 if (from>to)
136 int tmp = from;
137 from = to;
138 to = tmp;
141 /* after fiddling with the values, we find from > len && to > len */
142 if (from > len)
143 selectionEnd = 1;
144 /* special case with to too big */
145 else if (to > len)
146 to = len + 1;
149 if (selectionEnd)
151 editor->pCursors[0].pPara = editor->pBuffer->pLast->member.para.prev_para;
152 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
153 editor->pCursors[0].nOffset = 0;
154 editor->pCursors[1] = editor->pCursors[0];
155 ME_InvalidateSelection(editor);
156 ME_ClearTempStyle(editor);
157 return len;
160 ME_CursorFromCharOfs(editor, from, &editor->pCursors[1]);
161 ME_CursorFromCharOfs(editor, to, &editor->pCursors[0]);
162 /* Selection is not allowed in the middle of an end paragraph run. */
163 if (editor->pCursors[1].pRun->member.run.nFlags & MERF_ENDPARA)
164 editor->pCursors[1].nOffset = 0;
165 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA)
166 editor->pCursors[0].nOffset = 0;
167 return to;
171 static void
172 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
173 int *x, int *y, int *height)
175 ME_DisplayItem *row;
176 ME_DisplayItem *run = pCursor->pRun;
177 ME_DisplayItem *para = pCursor->pPara;
178 ME_DisplayItem *pSizeRun = run;
179 ME_Context c;
180 SIZE sz = {0, 0};
182 assert(height && x && y);
183 assert(~para->member.para.nFlags & MEPF_REWRAP);
184 assert(run && run->type == diRun);
185 assert(para && para->type == diParagraph);
187 row = ME_FindItemBack(run, diStartRowOrParagraph);
188 assert(row && row->type == diStartRow);
190 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
192 if (!pCursor->nOffset)
194 ME_DisplayItem *prev = ME_FindItemBack(run, diRunOrParagraph);
195 assert(prev);
196 if (prev->type == diRun)
197 pSizeRun = prev;
199 if (editor->bCaretAtEnd && !pCursor->nOffset &&
200 run == ME_FindItemFwd(row, diRun))
202 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
203 assert(tmp);
204 if (tmp->type == diRun)
206 row = ME_FindItemBack(tmp, diStartRow);
207 pSizeRun = run = tmp;
208 assert(run);
209 assert(run->type == diRun);
210 sz = ME_GetRunSize(&c, &para->member.para,
211 &run->member.run, run->member.run.strText->nLen,
212 row->member.row.nLMargin);
215 if (pCursor->nOffset) {
216 sz = ME_GetRunSize(&c, &para->member.para, &run->member.run,
217 pCursor->nOffset, row->member.row.nLMargin);
220 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
221 *x = c.rcView.left + run->member.run.pt.x + sz.cx - editor->horz_si.nPos;
222 *y = c.rcView.top + para->member.para.pt.y + row->member.row.nBaseline
223 + run->member.run.pt.y - pSizeRun->member.run.nAscent
224 - editor->vert_si.nPos;
225 ME_DestroyContext(&c);
226 return;
230 void
231 ME_MoveCaret(ME_TextEditor *editor)
233 int x, y, height;
235 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
236 if(editor->bHaveFocus && !ME_IsSelection(editor))
238 x = min(x, editor->rcFormat.right-1);
239 ITextHost_TxCreateCaret(editor->texthost, NULL, 0, height);
240 ITextHost_TxSetCaretPos(editor->texthost, x, y);
245 void ME_ShowCaret(ME_TextEditor *ed)
247 ME_MoveCaret(ed);
248 if(ed->bHaveFocus && !ME_IsSelection(ed))
249 ITextHost_TxShowCaret(ed->texthost, TRUE);
252 void ME_HideCaret(ME_TextEditor *ed)
254 if(!ed->bHaveFocus || ME_IsSelection(ed))
256 ITextHost_TxShowCaret(ed->texthost, FALSE);
257 DestroyCaret();
261 BOOL ME_InternalDeleteText(ME_TextEditor *editor, int nOfs, int nChars,
262 BOOL bForce)
264 ME_Cursor c;
265 int shift = 0;
266 int totalChars = nChars;
267 ME_DisplayItem *start_para;
269 /* Prevent deletion past last end of paragraph run. */
270 nChars = min(nChars, ME_GetTextLength(editor) - nOfs);
272 ME_CursorFromCharOfs(editor, nOfs, &c);
273 start_para = c.pPara;
275 if (!bForce)
277 ME_ProtectPartialTableDeletion(editor, nOfs, &nChars);
278 if (nChars == 0)
279 return FALSE;
282 while(nChars > 0)
284 ME_Run *run;
285 ME_CursorFromCharOfs(editor, nOfs+nChars, &c);
286 if (!c.nOffset &&
287 nOfs+nChars == (c.pRun->member.run.nCharOfs
288 + c.pPara->member.para.nCharOfs))
290 /* We aren't deleting anything in this run, so we will go back to the
291 * last run we are deleting text in. */
292 c.pRun = ME_FindItemBack(c.pRun, diRun);
293 c.pPara = ME_GetParagraph(c.pRun);
294 c.nOffset = c.pRun->member.run.strText->nLen;
296 run = &c.pRun->member.run;
297 if (run->nFlags & MERF_ENDPARA) {
298 int eollen = c.pRun->member.run.strText->nLen;
299 BOOL keepFirstParaFormat;
301 if (!ME_FindItemFwd(c.pRun, diParagraph))
303 return TRUE;
305 keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
306 run->nCharOfs);
307 if (!editor->bEmulateVersion10) /* v4.1 */
309 ME_DisplayItem *next_para = ME_FindItemFwd(c.pRun, diParagraphOrEnd);
310 ME_DisplayItem *this_para = next_para->member.para.prev_para;
312 /* The end of paragraph before a table row is only deleted if there
313 * is nothing else on the line before it. */
314 if (this_para == start_para &&
315 next_para->member.para.nFlags & MEPF_ROWSTART)
317 /* If the paragraph will be empty, then it should be deleted, however
318 * it still might have text right now which would inherit the
319 * MEPF_STARTROW property if we joined it right now.
320 * Instead we will delete it after the preceding text is deleted. */
321 if (nOfs > this_para->member.para.nCharOfs) {
322 /* Skip this end of line. */
323 nChars -= (eollen < nChars) ? eollen : nChars;
324 continue;
326 keepFirstParaFormat = TRUE;
329 ME_JoinParagraphs(editor, c.pPara, keepFirstParaFormat);
330 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
331 ME_CheckCharOffsets(editor);
332 nChars -= (eollen < nChars) ? eollen : nChars;
333 continue;
335 else
337 ME_Cursor cursor;
338 int nCharsToDelete = min(nChars, c.nOffset);
339 int i;
341 c.nOffset -= nCharsToDelete;
343 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
345 cursor = c;
346 /* nChars is the number of characters that should be deleted from the
347 PRECEDING runs (these BEFORE cursor.pRun)
348 nCharsToDelete is a number of chars to delete from THIS run */
349 nChars -= nCharsToDelete;
350 shift -= nCharsToDelete;
351 TRACE("Deleting %d (remaning %d) chars at %d in '%s' (%d)\n",
352 nCharsToDelete, nChars, c.nOffset,
353 debugstr_w(run->strText->szData), run->strText->nLen);
355 if (!c.nOffset && run->strText->nLen == nCharsToDelete)
357 /* undo = reinsert whole run */
358 /* nOfs is a character offset (from the start of the document
359 to the current (deleted) run */
360 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
361 if (pUndo)
362 pUndo->di.member.run.nCharOfs = nOfs+nChars;
364 else
366 /* undo = reinsert partial run */
367 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
368 if (pUndo) {
369 ME_DestroyString(pUndo->di.member.run.strText);
370 pUndo->di.member.run.nCharOfs = nOfs+nChars;
371 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
374 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
375 TRACE("Shift value: %d\n", shift);
376 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
378 /* update cursors (including c) */
379 for (i=-1; i<editor->nCursors; i++) {
380 ME_Cursor *pThisCur = editor->pCursors + i;
381 if (i == -1) pThisCur = &c;
382 if (pThisCur->pRun == cursor.pRun) {
383 if (pThisCur->nOffset > cursor.nOffset) {
384 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
385 pThisCur->nOffset = cursor.nOffset;
386 else
387 pThisCur->nOffset -= nCharsToDelete;
388 assert(pThisCur->nOffset >= 0);
389 assert(pThisCur->nOffset <= run->strText->nLen);
391 if (pThisCur->nOffset == run->strText->nLen)
393 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
394 assert(pThisCur->pRun->type == diRun);
395 pThisCur->nOffset = 0;
400 /* c = updated data now */
402 if (c.pRun == cursor.pRun)
403 ME_SkipAndPropagateCharOffset(c.pRun, shift);
404 else
405 ME_PropagateCharOffset(c.pRun, shift);
407 if (!cursor.pRun->member.run.strText->nLen)
409 TRACE("Removing useless run\n");
410 ME_Remove(cursor.pRun);
411 ME_DestroyDisplayItem(cursor.pRun);
414 shift = 0;
416 ME_CheckCharOffsets(editor);
418 continue;
421 return TRUE;
424 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
426 assert(nCursor>=0 && nCursor<editor->nCursors);
427 /* text operations set modified state */
428 editor->nModifyStep = 1;
429 return ME_InternalDeleteText(editor, ME_GetCursorOfs(&editor->pCursors[nCursor]),
430 nChars, FALSE);
433 static ME_DisplayItem *
434 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
435 const WCHAR *str, int len, ME_Style *style,
436 int flags)
438 ME_Cursor *p = &editor->pCursors[nCursor];
440 editor->bCaretAtEnd = FALSE;
442 assert(p->pRun->type == diRun);
444 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
448 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
450 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
451 ME_DisplayItem *di;
452 WCHAR space = ' ';
454 /* FIXME no no no */
455 if (ME_IsSelection(editor))
456 ME_DeleteSelection(editor);
458 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
459 MERF_GRAPHICS);
460 di->member.run.ole_obj = ALLOC_OBJ(*reo);
461 ME_CopyReObject(di->member.run.ole_obj, reo);
462 ME_ReleaseStyle(pStyle);
466 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
468 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
469 ME_DisplayItem *di;
470 WCHAR space = ' ';
472 /* FIXME no no no */
473 if (ME_IsSelection(editor))
474 ME_DeleteSelection(editor);
476 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
477 MERF_ENDROW);
478 ME_ReleaseStyle(pStyle);
482 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
483 const WCHAR *str, int len, ME_Style *style)
485 const WCHAR *pos;
486 ME_Cursor *p = NULL;
487 int oldLen;
489 /* FIXME really HERE ? */
490 if (ME_IsSelection(editor))
491 ME_DeleteSelection(editor);
493 /* FIXME: is this too slow? */
494 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
495 oldLen = ME_GetTextLength(editor);
497 /* text operations set modified state */
498 editor->nModifyStep = 1;
500 assert(style);
502 assert(nCursor>=0 && nCursor<editor->nCursors);
503 if (len == -1)
504 len = lstrlenW(str);
506 /* grow the text limit to fit our text */
507 if(editor->nTextLimit < oldLen +len)
508 editor->nTextLimit = oldLen + len;
510 pos = str;
512 while (len)
514 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
515 while(pos - str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
516 pos++;
518 if (pos != str) { /* handle text */
519 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
520 } else if (*pos == '\t') { /* handle tabs */
521 WCHAR tab = '\t';
522 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
523 pos++;
524 } else { /* handle EOLs */
525 ME_DisplayItem *tp, *end_run;
526 ME_Style *tmp_style;
527 int eol_len = 0;
529 /* Find number of CR and LF in end of paragraph run */
530 if (*pos =='\r')
532 if (len > 1 && pos[1] == '\n')
533 eol_len = 2;
534 else if (len > 2 && pos[1] == '\r' && pos[2] == '\n')
535 eol_len = 3;
536 else
537 eol_len = 1;
538 } else {
539 assert(*pos == '\n');
540 eol_len = 1;
542 pos += eol_len;
544 if (!editor->bEmulateVersion10 && eol_len == 3)
546 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
547 WCHAR space = ' ';
548 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
549 } else {
550 ME_String *eol_str;
552 if (!editor->bEmulateVersion10) {
553 WCHAR cr = '\r';
554 eol_str = ME_MakeStringN(&cr, 1);
555 } else {
556 eol_str = ME_MakeStringN(str, eol_len);
559 p = &editor->pCursors[nCursor];
560 if (p->nOffset) {
561 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
562 p = &editor->pCursors[nCursor];
564 tmp_style = ME_GetInsertStyle(editor, nCursor);
565 /* ME_SplitParagraph increases style refcount */
566 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, eol_str, 0);
567 p->pRun = ME_FindItemFwd(tp, diRun);
568 p->pPara = ME_GetParagraph(p->pRun);
569 end_run = ME_FindItemBack(tp, diRun);
570 ME_ReleaseStyle(end_run->member.run.style);
571 end_run->member.run.style = tmp_style;
572 p->nOffset = 0;
575 len -= pos - str;
576 str = pos;
581 static BOOL
582 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
584 ME_DisplayItem *pRun = pCursor->pRun;
586 if (nRelOfs == -1)
588 if (!pCursor->nOffset)
590 ME_DisplayItem *pPara = pCursor->pPara;
591 do {
592 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
593 assert(pRun);
594 switch (pRun->type)
596 case diRun:
597 break;
598 case diParagraph:
599 pPara = pRun;
600 if (pPara->member.para.prev_para->type == diTextStart)
601 return FALSE;
602 pRun = ME_FindItemBack(pPara, diRunOrParagraph);
603 pPara = pPara->member.para.prev_para;
604 /* every paragraph ought to have at least one run */
605 assert(pRun && pRun->type == diRun);
606 assert(pRun->member.run.nFlags & MERF_ENDPARA);
607 break;
608 default:
609 assert(pRun->type != diRun && pRun->type != diParagraph);
610 return FALSE;
612 } while (RUN_IS_HIDDEN(&pRun->member.run) ||
613 pRun->member.run.nFlags & MERF_HIDDEN);
614 pCursor->pPara = pPara;
615 pCursor->pRun = pRun;
616 if (pRun->member.run.nFlags & MERF_ENDPARA)
617 pCursor->nOffset = 0;
618 else
619 pCursor->nOffset = pRun->member.run.strText->nLen;
622 if (pCursor->nOffset)
623 pCursor->nOffset = pCursor->nOffset + nRelOfs;
624 return TRUE;
626 else
628 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
630 int new_ofs = pCursor->nOffset + nRelOfs;
632 if (new_ofs < pRun->member.run.strText->nLen)
634 pCursor->nOffset = new_ofs;
635 return TRUE;
638 do {
639 pRun = ME_FindItemFwd(pRun, diRun);
640 } while (pRun && (RUN_IS_HIDDEN(&pRun->member.run) ||
641 pRun->member.run.nFlags & MERF_HIDDEN));
642 if (pRun)
644 pCursor->pPara = ME_GetParagraph(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, diRun);
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->pPara = ME_GetParagraph(pRun);
741 cursor->pRun = pRun;
742 cursor->nOffset = nOffset;
743 return TRUE;
747 static void
748 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
750 /* pCursor[0] is the end of the selection
751 * pCursor[1] is the start of the selection (or the position selection anchor)
752 * pCursor[2] and [3] are the selection anchors that are backed up
753 * so they are kept when the selection changes for drag selection.
756 editor->nSelectionType = selectionType;
757 switch(selectionType)
759 case stPosition:
760 break;
761 case stWord:
762 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
763 editor->pCursors[1] = editor->pCursors[0];
764 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
765 break;
766 case stLine:
767 case stParagraph:
769 ME_DisplayItem *pItem;
770 ME_DIType fwdSearchType, backSearchType;
771 if (selectionType == stParagraph) {
772 backSearchType = diParagraph;
773 fwdSearchType = diParagraphOrEnd;
774 } else {
775 backSearchType = diStartRow;
776 fwdSearchType = diStartRowOrParagraphOrEnd;
778 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
779 assert(pItem);
780 if (pItem->type == diTextEnd)
781 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
782 else
783 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
784 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
785 editor->pCursors[0].nOffset = 0;
787 pItem = ME_FindItemBack(pItem, backSearchType);
788 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
789 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
790 editor->pCursors[1].nOffset = 0;
791 break;
793 case stDocument:
794 /* Select everything with cursor anchored from the start of the text */
795 editor->nSelectionType = stDocument;
796 editor->pCursors[1].pPara = editor->pBuffer->pFirst->member.para.next_para;
797 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pCursors[1].pPara, diRun);
798 editor->pCursors[1].nOffset = 0;
799 editor->pCursors[0].pPara = editor->pBuffer->pLast->member.para.prev_para;
800 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
801 editor->pCursors[0].nOffset = 0;
802 break;
803 default: assert(0);
805 /* Store the anchor positions for extending the selection. */
806 editor->pCursors[2] = editor->pCursors[0];
807 editor->pCursors[3] = editor->pCursors[1];
810 int ME_GetCursorOfs(const ME_Cursor *cursor)
812 return cursor->pPara->member.para.nCharOfs
813 + cursor->pRun->member.run.nCharOfs + cursor->nOffset;
816 /* Helper function for ME_FindPixelPos to find paragraph within tables */
817 static ME_DisplayItem* ME_FindPixelPosInTableRow(int x, int y,
818 ME_DisplayItem *para)
820 ME_DisplayItem *cell, *next_cell;
821 assert(para->member.para.nFlags & MEPF_ROWSTART);
822 cell = para->member.para.next_para->member.para.pCell;
823 assert(cell);
825 /* find the cell we are in */
826 while ((next_cell = cell->member.cell.next_cell) != NULL) {
827 if (x < next_cell->member.cell.pt.x)
829 para = ME_FindItemFwd(cell, diParagraph);
830 /* Found the cell, but there might be multiple paragraphs in
831 * the cell, so need to search down the cell for the paragraph. */
832 while (cell == para->member.para.pCell) {
833 if (y < para->member.para.pt.y + para->member.para.nHeight)
835 if (para->member.para.nFlags & MEPF_ROWSTART)
836 return ME_FindPixelPosInTableRow(x, y, para);
837 else
838 return para;
840 para = para->member.para.next_para;
842 /* Past the end of the cell, so go back to the last cell paragraph */
843 return para->member.para.prev_para;
845 cell = next_cell;
847 /* Return table row delimiter */
848 para = ME_FindItemFwd(cell, diParagraph);
849 assert(para->member.para.nFlags & MEPF_ROWEND);
850 assert(para->member.para.pFmt->dwMask & PFM_TABLEROWDELIMITER);
851 assert(para->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER);
852 return para;
855 static BOOL ME_ReturnFoundPos(ME_TextEditor *editor, ME_DisplayItem *found,
856 ME_Cursor *result, int rx, BOOL isExact)
858 assert(found);
859 assert(found->type == diRun);
860 if ((found->member.run.nFlags & MERF_ENDPARA) || rx < 0)
861 rx = 0;
862 result->pRun = found;
863 result->nOffset = ME_CharFromPointCursor(editor, rx, &found->member.run);
864 if (editor->pCursors[0].nOffset == found->member.run.strText->nLen && rx)
866 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
867 result->nOffset = 0;
869 result->pPara = ME_GetParagraph(result->pRun);
870 return isExact;
873 /* Finds the run and offset from the pixel position.
875 * x & y are pixel positions in virtual coordinates into the rich edit control,
876 * so client coordinates must first be adjusted by the scroll position.
878 * returns TRUE if the result was exactly under the cursor, otherwise returns
879 * FALSE, and result is set to the closest position to the coordinates.
881 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
882 ME_Cursor *result, BOOL *is_eol)
884 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
885 ME_DisplayItem *last = NULL;
886 int rx = 0;
887 BOOL isExact = TRUE;
889 x -= editor->rcFormat.left;
890 y -= editor->rcFormat.top;
892 if (is_eol)
893 *is_eol = 0;
895 /* find paragraph */
896 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
898 assert(p->type == diParagraph);
899 if (y < p->member.para.pt.y + p->member.para.nHeight)
901 if (p->member.para.nFlags & MEPF_ROWSTART)
902 p = ME_FindPixelPosInTableRow(x, y, p);
903 y -= p->member.para.pt.y;
904 p = ME_FindItemFwd(p, diStartRow);
905 break;
906 } else if (p->member.para.nFlags & MEPF_ROWSTART) {
907 p = ME_GetTableRowEnd(p);
910 /* find row */
911 for (; p != editor->pBuffer->pLast; )
913 ME_DisplayItem *pp;
914 assert(p->type == diStartRow);
915 if (y < p->member.row.pt.y + p->member.row.nHeight)
917 p = ME_FindItemFwd(p, diRun);
918 break;
920 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
921 if (pp->type != diStartRow)
923 p = ME_FindItemFwd(p, diRun);
924 break;
926 p = pp;
928 if (p == editor->pBuffer->pLast)
930 /* The position is below the last paragraph, so the last row will be used
931 * rather than the end of the text, so the x position will be used to
932 * determine the offset closest to the pixel position. */
933 isExact = FALSE;
934 p = ME_FindItemBack(p, diStartRow);
935 if (p != NULL){
936 p = ME_FindItemFwd(p, diRun);
938 else
940 p = editor->pBuffer->pLast;
943 for (; p != editor->pBuffer->pLast; p = p->next)
945 switch (p->type)
947 case diRun:
948 rx = x - p->member.run.pt.x;
949 if (rx < p->member.run.nWidth)
950 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
951 break;
952 case diStartRow:
953 isExact = FALSE;
954 p = ME_FindItemFwd(p, diRun);
955 if (is_eol) *is_eol = 1;
956 rx = 0; /* FIXME not sure */
957 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
958 case diCell:
959 case diParagraph:
960 case diTextEnd:
961 isExact = FALSE;
962 rx = 0; /* FIXME not sure */
963 p = last;
964 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
965 default: assert(0);
967 last = p;
969 result->pRun = ME_FindItemBack(p, diRun);
970 result->pPara = ME_GetParagraph(result->pRun);
971 result->nOffset = 0;
972 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
973 return FALSE;
977 /* Returns the character offset closest to the pixel position
979 * x & y are pixel positions in client coordinates.
981 * isExact will be set to TRUE if the run is directly under the pixel
982 * position, FALSE if it not, unless isExact is set to NULL.
984 int ME_CharFromPos(ME_TextEditor *editor, int x, int y, BOOL *isExact)
986 ME_Cursor cursor;
987 RECT rc;
988 BOOL bResult;
990 ITextHost_TxGetClientRect(editor->texthost, &rc);
991 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
992 if (isExact) *isExact = FALSE;
993 return -1;
995 x += editor->horz_si.nPos;
996 y += editor->vert_si.nPos;
997 bResult = ME_FindPixelPos(editor, x, y, &cursor, NULL);
998 if (isExact) *isExact = bResult;
999 return cursor.pPara->member.para.nCharOfs
1000 + cursor.pRun->member.run.nCharOfs + cursor.nOffset;
1005 /* Extends the selection with a word, line, or paragraph selection type.
1007 * The selection is anchored by editor->pCursors[2-3] such that the text
1008 * between the anchors will remain selected, and one end will be extended.
1010 * editor->pCursors[0] should have the position to extend the selection to
1011 * before this function is called.
1013 * Nothing will be done if editor->nSelectionType equals stPosition.
1015 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1017 ME_Cursor tmp_cursor;
1018 int curOfs, anchorStartOfs, anchorEndOfs;
1019 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1020 return;
1021 curOfs = ME_GetCursorOfs(&editor->pCursors[0]);
1022 anchorStartOfs = ME_GetCursorOfs(&editor->pCursors[3]);
1023 anchorEndOfs = ME_GetCursorOfs(&editor->pCursors[2]);
1025 tmp_cursor = editor->pCursors[0];
1026 editor->pCursors[0] = editor->pCursors[2];
1027 editor->pCursors[1] = editor->pCursors[3];
1028 if (curOfs < anchorStartOfs)
1030 /* Extend the left side of selection */
1031 editor->pCursors[1] = tmp_cursor;
1032 if (editor->nSelectionType == stWord)
1033 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1034 else
1036 ME_DisplayItem *pItem;
1037 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1038 diStartRowOrParagraph:diParagraph);
1039 pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
1040 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
1041 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
1042 editor->pCursors[1].nOffset = 0;
1045 else if (curOfs >= anchorEndOfs)
1047 /* Extend the right side of selection */
1048 editor->pCursors[0] = tmp_cursor;
1049 if (editor->nSelectionType == stWord)
1050 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
1051 else
1053 ME_DisplayItem *pItem;
1054 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1055 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
1056 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
1057 if (pItem->type == diTextEnd)
1058 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
1059 else
1060 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
1061 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
1062 editor->pCursors[0].nOffset = 0;
1067 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1069 ME_Cursor tmp_cursor;
1070 int is_selection = 0;
1071 BOOL is_shift;
1073 editor->nUDArrowX = -1;
1075 x += editor->horz_si.nPos;
1076 y += editor->vert_si.nPos;
1078 tmp_cursor = editor->pCursors[0];
1079 is_selection = ME_IsSelection(editor);
1080 is_shift = GetKeyState(VK_SHIFT) < 0;
1082 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
1084 if (x >= editor->rcFormat.left || is_shift)
1086 if (clickNum > 1)
1088 editor->pCursors[1] = editor->pCursors[0];
1089 if (is_shift) {
1090 if (x >= editor->rcFormat.left)
1091 ME_SelectByType(editor, stWord);
1092 else
1093 ME_SelectByType(editor, stParagraph);
1094 } else if (clickNum % 2 == 0) {
1095 ME_SelectByType(editor, stWord);
1096 } else {
1097 ME_SelectByType(editor, stParagraph);
1100 else if (!is_shift)
1102 editor->nSelectionType = stPosition;
1103 editor->pCursors[1] = editor->pCursors[0];
1105 else if (!is_selection)
1107 editor->nSelectionType = stPosition;
1108 editor->pCursors[1] = tmp_cursor;
1110 else if (editor->nSelectionType != stPosition)
1112 ME_ExtendAnchorSelection(editor);
1115 else
1117 if (clickNum < 2) {
1118 ME_SelectByType(editor, stLine);
1119 } else if (clickNum % 2 == 0 || is_shift) {
1120 ME_SelectByType(editor, stParagraph);
1121 } else {
1122 ME_SelectByType(editor, stDocument);
1125 ME_InvalidateSelection(editor);
1126 ITextHost_TxShowCaret(editor->texthost, FALSE);
1127 ME_ShowCaret(editor);
1128 ME_ClearTempStyle(editor);
1129 ME_SendSelChange(editor);
1132 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1134 ME_Cursor tmp_cursor;
1136 if (editor->nSelectionType == stDocument)
1137 return;
1138 x += editor->horz_si.nPos;
1139 y += editor->vert_si.nPos;
1141 tmp_cursor = editor->pCursors[0];
1142 /* FIXME: do something with the return value of ME_FindPixelPos */
1143 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
1145 ME_InvalidateSelection(editor);
1146 editor->pCursors[0] = tmp_cursor;
1147 ME_ExtendAnchorSelection(editor);
1149 if (editor->nSelectionType != stPosition &&
1150 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1152 /* The scroll the cursor towards the other end, since it was the one
1153 * extended by ME_ExtendAnchorSelection */
1154 ME_EnsureVisible(editor, &editor->pCursors[1]);
1155 } else {
1156 ME_EnsureVisible(editor, &editor->pCursors[0]);
1159 ME_InvalidateSelection(editor);
1160 ITextHost_TxShowCaret(editor->texthost, FALSE);
1161 ME_ShowCaret(editor);
1162 ME_SendSelChange(editor);
1165 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
1166 int x, int *pOffset, int *pbCaretAtEnd)
1168 ME_DisplayItem *pNext, *pLastRun;
1169 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
1170 assert(pNext->type == diRun);
1171 pLastRun = pNext;
1172 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
1173 if (pOffset) *pOffset = 0;
1174 do {
1175 int run_x = pNext->member.run.pt.x;
1176 int width = pNext->member.run.nWidth;
1177 if (x < run_x)
1179 return pNext;
1181 if (x >= run_x && x < run_x+width)
1183 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
1184 ME_String *s = pNext->member.run.strText;
1185 if (ch < s->nLen) {
1186 if (pOffset)
1187 *pOffset = ch;
1188 return pNext;
1191 pLastRun = pNext;
1192 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
1193 } while(pNext && pNext->type == diRun);
1195 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
1197 pNext = ME_FindItemFwd(pNext, diRun);
1198 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
1199 return pNext;
1200 } else {
1201 return pLastRun;
1205 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1207 ME_DisplayItem *pRun = pCursor->pRun;
1208 int x;
1210 if (editor->nUDArrowX != -1)
1211 x = editor->nUDArrowX;
1212 else {
1213 if (editor->bCaretAtEnd)
1215 pRun = ME_FindItemBack(pRun, diRun);
1216 assert(pRun);
1217 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1219 else {
1220 x = pRun->member.run.pt.x;
1221 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
1223 editor->nUDArrowX = x;
1225 return x;
1229 static void
1230 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1232 ME_DisplayItem *pRun = pCursor->pRun;
1233 ME_DisplayItem *pItem, *pOldPara, *pNewPara;
1234 int x = ME_GetXForArrow(editor, pCursor);
1236 if (editor->bCaretAtEnd && !pCursor->nOffset)
1237 pRun = ME_FindItemBack(pRun, diRun);
1238 if (!pRun)
1239 return;
1240 pOldPara = ME_GetParagraph(pRun);
1241 if (nRelOfs == -1)
1243 /* start of this row */
1244 pItem = ME_FindItemBack(pRun, diStartRow);
1245 assert(pItem);
1246 /* start of the previous row */
1247 pItem = ME_FindItemBack(pItem, diStartRow);
1248 if (!pItem)
1249 return; /* row not found - ignore */
1250 pNewPara = ME_GetParagraph(pItem);
1251 if (pOldPara->member.para.nFlags & MEPF_ROWEND ||
1252 (pOldPara->member.para.pCell &&
1253 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1255 /* Brought out of a cell */
1256 pNewPara = ME_GetTableRowStart(pOldPara)->member.para.prev_para;
1257 if (pNewPara->type == diTextStart)
1258 return; /* At the top, so don't go anywhere. */
1259 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1261 if (pNewPara->member.para.nFlags & MEPF_ROWEND)
1263 /* Brought into a table row */
1264 ME_Cell *cell = &ME_FindItemBack(pNewPara, diCell)->member.cell;
1265 while (x < cell->pt.x && cell->prev_cell)
1266 cell = &cell->prev_cell->member.cell;
1267 if (cell->next_cell) /* else - we are still at the end of the row */
1268 pItem = ME_FindItemBack(cell->next_cell, diStartRow);
1271 else
1273 /* start of the next row */
1274 pItem = ME_FindItemFwd(pRun, diStartRow);
1275 if (!pItem)
1276 return; /* row not found - ignore */
1277 pNewPara = ME_GetParagraph(pItem);
1278 if (pOldPara->member.para.nFlags & MEPF_ROWSTART ||
1279 (pOldPara->member.para.pCell &&
1280 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1282 /* Brought out of a cell */
1283 pNewPara = ME_GetTableRowEnd(pOldPara)->member.para.next_para;
1284 if (pNewPara->type == diTextEnd)
1285 return; /* At the bottom, so don't go anywhere. */
1286 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1288 if (pNewPara->member.para.nFlags & MEPF_ROWSTART)
1290 /* Brought into a table row */
1291 ME_DisplayItem *cell = ME_FindItemFwd(pNewPara, diCell);
1292 while (cell->member.cell.next_cell &&
1293 x >= cell->member.cell.next_cell->member.cell.pt.x)
1294 cell = cell->member.cell.next_cell;
1295 pItem = ME_FindItemFwd(cell, diStartRow);
1298 if (!pItem)
1300 /* row not found - ignore */
1301 return;
1303 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1304 pCursor->pPara = ME_GetParagraph(pCursor->pRun);
1305 assert(pCursor->pRun);
1306 assert(pCursor->pRun->type == diRun);
1309 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1311 ME_DisplayItem *p = ME_FindItemFwd(editor->pBuffer->pFirst, diStartRow);
1313 if (editor->vert_si.nPos < p->member.row.nHeight)
1315 pCursor->pPara = editor->pBuffer->pFirst->member.para.next_para;
1316 pCursor->pRun = ME_FindItemFwd(pCursor->pPara, diRun);
1317 pCursor->nOffset = 0;
1318 editor->bCaretAtEnd = FALSE;
1319 /* Native clears seems to clear this x value on page up at the top
1320 * of the text, but not on page down at the end of the text.
1321 * Doesn't make sense, but we try to be bug for bug compatible. */
1322 editor->nUDArrowX = -1;
1323 } else {
1324 ME_DisplayItem *pRun = pCursor->pRun;
1325 ME_DisplayItem *pLast;
1326 int x, y, ys, yd, yp, yprev;
1327 int yOldScrollPos = editor->vert_si.nPos;
1329 x = ME_GetXForArrow(editor, pCursor);
1330 if (!pCursor->nOffset && editor->bCaretAtEnd)
1331 pRun = ME_FindItemBack(pRun, diRun);
1333 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1334 assert(p->type == diStartRow);
1335 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1336 yprev = ys = y = yp + p->member.row.pt.y;
1338 ME_ScrollUp(editor, editor->sizeWindow.cy);
1339 /* Only move the cursor by the amount scrolled. */
1340 yd = y + editor->vert_si.nPos - yOldScrollPos;
1341 pLast = p;
1343 do {
1344 p = ME_FindItemBack(p, diStartRowOrParagraph);
1345 if (!p)
1346 break;
1347 if (p->type == diParagraph) { /* crossing paragraphs */
1348 if (p->member.para.prev_para == NULL)
1349 break;
1350 yp = p->member.para.prev_para->member.para.pt.y;
1351 continue;
1353 y = yp + p->member.row.pt.y;
1354 if (y < yd)
1355 break;
1356 pLast = p;
1357 yprev = y;
1358 } while(1);
1360 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset,
1361 &editor->bCaretAtEnd);
1362 pCursor->pPara = ME_GetParagraph(pCursor->pRun);
1364 assert(pCursor->pRun);
1365 assert(pCursor->pRun->type == diRun);
1368 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1370 ME_DisplayItem *pLast;
1371 int x, y;
1373 /* Find y position of the last row */
1374 pLast = editor->pBuffer->pLast;
1375 y = pLast->member.para.prev_para->member.para.pt.y
1376 + ME_FindItemBack(pLast, diStartRow)->member.row.pt.y;
1378 x = ME_GetXForArrow(editor, pCursor);
1380 if (editor->vert_si.nPos >= y - editor->sizeWindow.cy)
1382 pCursor->pPara = editor->pBuffer->pLast->member.para.prev_para;
1383 pCursor->pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
1384 pCursor->nOffset = 0;
1385 editor->bCaretAtEnd = FALSE;
1386 } else {
1387 ME_DisplayItem *pRun = pCursor->pRun;
1388 ME_DisplayItem *p;
1389 int ys, yd, yp, yprev;
1390 int yOldScrollPos = editor->vert_si.nPos;
1392 if (!pCursor->nOffset && editor->bCaretAtEnd)
1393 pRun = ME_FindItemBack(pRun, diRun);
1395 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1396 assert(p->type == diStartRow);
1397 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1398 yprev = ys = y = yp + p->member.row.pt.y;
1400 /* For native richedit controls:
1401 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1402 * v4.1 can scroll past this position here. */
1403 ME_ScrollDown(editor, editor->sizeWindow.cy);
1404 /* Only move the cursor by the amount scrolled. */
1405 yd = y + editor->vert_si.nPos - yOldScrollPos;
1406 pLast = p;
1408 do {
1409 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1410 if (!p)
1411 break;
1412 if (p->type == diParagraph) {
1413 yp = p->member.para.pt.y;
1414 continue;
1416 y = yp + p->member.row.pt.y;
1417 if (y >= yd)
1418 break;
1419 pLast = p;
1420 yprev = y;
1421 } while(1);
1423 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset,
1424 &editor->bCaretAtEnd);
1425 pCursor->pPara = ME_GetParagraph(pCursor->pRun);
1427 assert(pCursor->pRun);
1428 assert(pCursor->pRun->type == diRun);
1431 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1433 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1434 if (pRow) {
1435 ME_DisplayItem *pRun;
1436 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1437 pRow = ME_FindItemBack(pRow, diStartRow);
1438 if (!pRow)
1439 return;
1441 pRun = ME_FindItemFwd(pRow, diRun);
1442 if (pRun) {
1443 pCursor->pRun = pRun;
1444 assert(pCursor->pPara == ME_GetParagraph(pRun));
1445 pCursor->nOffset = 0;
1448 editor->bCaretAtEnd = FALSE;
1451 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1453 pCursor->pPara = editor->pBuffer->pFirst->member.para.next_para;
1454 pCursor->pRun = ME_FindItemFwd(pCursor->pPara, diRun);
1455 pCursor->nOffset = 0;
1456 editor->bCaretAtEnd = FALSE;
1459 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1461 ME_DisplayItem *pRow;
1463 if (editor->bCaretAtEnd && !pCursor->nOffset)
1464 return;
1466 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1467 assert(pRow);
1468 if (pRow->type == diStartRow) {
1469 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1470 assert(pRun);
1471 pCursor->pRun = pRun;
1472 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1473 pCursor->nOffset = 0;
1474 editor->bCaretAtEnd = TRUE;
1475 return;
1477 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1478 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1479 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1480 pCursor->nOffset = 0;
1481 editor->bCaretAtEnd = FALSE;
1484 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1486 pCursor->pPara = editor->pBuffer->pLast->member.para.prev_para;
1487 pCursor->pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
1488 assert(pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1489 pCursor->nOffset = 0;
1490 editor->bCaretAtEnd = FALSE;
1493 BOOL ME_IsSelection(ME_TextEditor *editor)
1495 return editor->pCursors[0].pRun != editor->pCursors[1].pRun ||
1496 editor->pCursors[0].nOffset != editor->pCursors[1].nOffset;
1499 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1501 int cdir = ME_GetCursorOfs(&editor->pCursors[0])
1502 - ME_GetCursorOfs(&editor->pCursors[1]);
1504 if (cdir*dir>0)
1505 return 0;
1506 else
1507 return 1;
1510 void ME_DeleteSelection(ME_TextEditor *editor)
1512 int from, to;
1513 ME_GetSelection(editor, &from, &to);
1514 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1517 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1519 return ME_GetInsertStyle(editor, 0);
1522 void ME_SendSelChange(ME_TextEditor *editor)
1524 SELCHANGE sc;
1526 if (!(editor->nEventMask & ENM_SELCHANGE))
1527 return;
1529 sc.nmhdr.code = EN_SELCHANGE;
1530 ME_GetSelection(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1531 sc.seltyp = SEL_EMPTY;
1532 if (sc.chrg.cpMin != sc.chrg.cpMax)
1533 sc.seltyp |= SEL_TEXT;
1534 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1535 sc.seltyp |= SEL_MULTICHAR;
1536 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1537 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1538 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1539 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1540 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1542 ME_ClearTempStyle(editor);
1544 editor->notified_cr = sc.chrg;
1545 ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
1549 BOOL
1550 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1552 int nCursor = 0;
1553 ME_Cursor *p = &editor->pCursors[nCursor];
1554 ME_Cursor tmp_curs = *p;
1555 BOOL success = FALSE;
1557 ME_CheckCharOffsets(editor);
1558 switch(nVKey) {
1559 case VK_LEFT:
1560 editor->bCaretAtEnd = 0;
1561 if (ctrl)
1562 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1563 else
1564 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1565 break;
1566 case VK_RIGHT:
1567 editor->bCaretAtEnd = 0;
1568 if (ctrl)
1569 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1570 else
1571 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1572 break;
1573 case VK_UP:
1574 ME_MoveCursorLines(editor, &tmp_curs, -1);
1575 break;
1576 case VK_DOWN:
1577 ME_MoveCursorLines(editor, &tmp_curs, +1);
1578 break;
1579 case VK_PRIOR:
1580 ME_ArrowPageUp(editor, &tmp_curs);
1581 break;
1582 case VK_NEXT:
1583 ME_ArrowPageDown(editor, &tmp_curs);
1584 break;
1585 case VK_HOME: {
1586 if (ctrl)
1587 ME_ArrowCtrlHome(editor, &tmp_curs);
1588 else
1589 ME_ArrowHome(editor, &tmp_curs);
1590 editor->bCaretAtEnd = 0;
1591 break;
1593 case VK_END:
1594 if (ctrl)
1595 ME_ArrowCtrlEnd(editor, &tmp_curs);
1596 else
1597 ME_ArrowEnd(editor, &tmp_curs);
1598 break;
1601 if (!extend)
1602 editor->pCursors[1] = tmp_curs;
1603 *p = tmp_curs;
1605 ME_InvalidateSelection(editor);
1606 ME_Repaint(editor);
1607 ITextHost_TxShowCaret(editor->texthost, FALSE);
1608 ME_EnsureVisible(editor, &tmp_curs);
1609 ME_ShowCaret(editor);
1610 ME_SendSelChange(editor);
1611 return success;