winex11: Implement additional XEMBED events.
[wine/wine-gecko.git] / dlls / riched20 / caret.c
blobfd7034d40cd1e383d1937e3303bd5b5271fc9037
1 /*
2 * RichEdit - Caret and selection functions.
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Phil Krylov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "editor.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
27 void ME_SetCursorToStart(ME_TextEditor *editor, ME_Cursor *cursor)
29 cursor->pPara = editor->pBuffer->pFirst->member.para.next_para;
30 cursor->pRun = ME_FindItemFwd(cursor->pPara, diRun);
31 cursor->nOffset = 0;
34 static void ME_SetCursorToEnd(ME_TextEditor *editor, ME_Cursor *cursor)
36 cursor->pPara = editor->pBuffer->pLast->member.para.prev_para;
37 cursor->pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
38 cursor->nOffset = 0;
42 int ME_GetSelectionOfs(ME_TextEditor *editor, int *from, int *to)
44 *from = ME_GetCursorOfs(&editor->pCursors[0]);
45 *to = ME_GetCursorOfs(&editor->pCursors[1]);
47 if (*from > *to)
49 int tmp = *from;
50 *from = *to;
51 *to = tmp;
52 return 1;
54 return 0;
57 int ME_GetSelection(ME_TextEditor *editor, ME_Cursor **from, ME_Cursor **to)
59 if (ME_GetCursorOfs(&editor->pCursors[0]) < ME_GetCursorOfs(&editor->pCursors[1]))
61 *from = &editor->pCursors[0];
62 *to = &editor->pCursors[1];
63 return 0;
64 } else {
65 *from = &editor->pCursors[1];
66 *to = &editor->pCursors[0];
67 return 1;
71 int ME_GetTextLength(ME_TextEditor *editor)
73 ME_Cursor cursor;
74 ME_SetCursorToEnd(editor, &cursor);
75 return ME_GetCursorOfs(&cursor);
79 int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how)
81 int length;
83 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
84 return E_INVALIDARG;
85 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
86 return E_INVALIDARG;
88 length = ME_GetTextLength(editor);
90 if ((editor->styleFlags & ES_MULTILINE)
91 && (how->flags & GTL_USECRLF)
92 && !editor->bEmulateVersion10) /* Ignore GTL_USECRLF flag in 1.0 emulation */
93 length += editor->nParagraphs - 1;
95 if (how->flags & GTL_NUMBYTES ||
96 (how->flags & GTL_PRECISE && /* GTL_PRECISE seems to imply GTL_NUMBYTES */
97 !(how->flags & GTL_NUMCHARS))) /* unless GTL_NUMCHARS is given */
99 CPINFO cpinfo;
101 if (how->codepage == 1200)
102 return length * 2;
103 if (how->flags & GTL_PRECISE)
104 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
105 if (GetCPInfo(how->codepage, &cpinfo))
106 return length * cpinfo.MaxCharSize;
107 ERR("Invalid codepage %u\n", how->codepage);
108 return E_INVALIDARG;
110 return length;
114 int ME_SetSelection(ME_TextEditor *editor, int from, int to)
116 int selectionEnd = 0;
117 const int len = ME_GetTextLength(editor);
119 /* all negative values are effectively the same */
120 if (from < 0)
121 from = -1;
122 if (to < 0)
123 to = -1;
125 /* select all */
126 if (from == 0 && to == -1)
128 ME_SetCursorToStart(editor, &editor->pCursors[1]);
129 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
130 ME_InvalidateSelection(editor);
131 ME_ClearTempStyle(editor);
132 return len + 1;
135 /* if both values are equal and also out of bound, that means to */
136 /* put the selection at the end of the text */
137 if ((from == to) && (to < 0 || to > len))
139 selectionEnd = 1;
141 else
143 /* if from is negative and to is positive then selection is */
144 /* deselected and caret moved to end of the current selection */
145 if (from < 0)
147 int start, end;
148 ME_GetSelectionOfs(editor, &start, &end);
149 if (start != end)
151 editor->pCursors[1] = editor->pCursors[0];
152 ME_Repaint(editor);
154 ME_ClearTempStyle(editor);
155 return end;
158 /* adjust to if it's a negative value */
159 if (to < 0)
160 to = len + 1;
162 /* flip from and to if they are reversed */
163 if (from>to)
165 int tmp = from;
166 from = to;
167 to = tmp;
170 /* after fiddling with the values, we find from > len && to > len */
171 if (from > len)
172 selectionEnd = 1;
173 /* special case with to too big */
174 else if (to > len)
175 to = len + 1;
178 if (selectionEnd)
180 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
181 editor->pCursors[1] = editor->pCursors[0];
182 ME_InvalidateSelection(editor);
183 ME_ClearTempStyle(editor);
184 return len;
187 ME_CursorFromCharOfs(editor, from, &editor->pCursors[1]);
188 editor->pCursors[0] = editor->pCursors[1];
189 ME_MoveCursorChars(editor, &editor->pCursors[0], to - from);
190 /* Selection is not allowed in the middle of an end paragraph run. */
191 if (editor->pCursors[1].pRun->member.run.nFlags & MERF_ENDPARA)
192 editor->pCursors[1].nOffset = 0;
193 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA)
194 editor->pCursors[0].nOffset = 0;
195 return to;
199 static void
200 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
201 int *x, int *y, int *height)
203 ME_DisplayItem *row;
204 ME_DisplayItem *run = pCursor->pRun;
205 ME_DisplayItem *para = pCursor->pPara;
206 ME_DisplayItem *pSizeRun = run;
207 ME_Context c;
208 int run_x;
210 assert(height && x && y);
211 assert(~para->member.para.nFlags & MEPF_REWRAP);
212 assert(run && run->type == diRun);
213 assert(para && para->type == diParagraph);
215 row = ME_FindItemBack(run, diStartRowOrParagraph);
216 assert(row && row->type == diStartRow);
218 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
220 if (!pCursor->nOffset)
222 ME_DisplayItem *prev = ME_FindItemBack(run, diRunOrParagraph);
223 assert(prev);
224 if (prev->type == diRun)
225 pSizeRun = prev;
227 if (editor->bCaretAtEnd && !pCursor->nOffset &&
228 run == ME_FindItemFwd(row, diRun))
230 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
231 assert(tmp);
232 if (tmp->type == diRun)
234 row = ME_FindItemBack(tmp, diStartRow);
235 pSizeRun = run = tmp;
236 assert(run);
237 assert(run->type == diRun);
240 run_x = ME_PointFromCharContext( &c, &run->member.run, pCursor->nOffset, TRUE );
242 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
243 *x = c.rcView.left + run->member.run.pt.x + run_x - editor->horz_si.nPos;
244 *y = c.rcView.top + para->member.para.pt.y + row->member.row.nBaseline
245 + run->member.run.pt.y - pSizeRun->member.run.nAscent
246 - editor->vert_si.nPos;
247 ME_DestroyContext(&c);
248 return;
252 void
253 ME_MoveCaret(ME_TextEditor *editor)
255 int x, y, height;
257 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
258 if(editor->bHaveFocus && !ME_IsSelection(editor))
260 x = min(x, editor->rcFormat.right-1);
261 ITextHost_TxCreateCaret(editor->texthost, NULL, 0, height);
262 ITextHost_TxSetCaretPos(editor->texthost, x, y);
267 void ME_ShowCaret(ME_TextEditor *ed)
269 ME_MoveCaret(ed);
270 if(ed->bHaveFocus && !ME_IsSelection(ed))
271 ITextHost_TxShowCaret(ed->texthost, TRUE);
274 void ME_HideCaret(ME_TextEditor *ed)
276 if(!ed->bHaveFocus || ME_IsSelection(ed))
278 ITextHost_TxShowCaret(ed->texthost, FALSE);
279 DestroyCaret();
283 BOOL ME_InternalDeleteText(ME_TextEditor *editor, ME_Cursor *start,
284 int nChars, BOOL bForce)
286 ME_Cursor c = *start;
287 int nOfs = ME_GetCursorOfs(start), text_len = ME_GetTextLength( editor );
288 int shift = 0;
289 int totalChars = nChars;
290 ME_DisplayItem *start_para;
291 BOOL delete_all = FALSE;
293 /* Prevent deletion past last end of paragraph run. */
294 nChars = min(nChars, text_len - nOfs);
295 if (nChars == text_len) delete_all = TRUE;
296 start_para = c.pPara;
298 if (!bForce)
300 ME_ProtectPartialTableDeletion(editor, &c, &nChars);
301 if (nChars == 0)
302 return FALSE;
305 while(nChars > 0)
307 ME_Run *run;
308 ME_CursorFromCharOfs(editor, nOfs+nChars, &c);
309 if (!c.nOffset &&
310 nOfs+nChars == (c.pRun->member.run.nCharOfs
311 + c.pPara->member.para.nCharOfs))
313 /* We aren't deleting anything in this run, so we will go back to the
314 * last run we are deleting text in. */
315 ME_PrevRun(&c.pPara, &c.pRun);
316 c.nOffset = c.pRun->member.run.len;
318 run = &c.pRun->member.run;
319 if (run->nFlags & MERF_ENDPARA) {
320 int eollen = c.pRun->member.run.len;
321 BOOL keepFirstParaFormat;
323 if (!ME_FindItemFwd(c.pRun, diParagraph))
325 return TRUE;
327 keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
328 run->nCharOfs);
329 if (!editor->bEmulateVersion10) /* v4.1 */
331 ME_DisplayItem *next_para = ME_FindItemFwd(c.pRun, diParagraphOrEnd);
332 ME_DisplayItem *this_para = next_para->member.para.prev_para;
334 /* The end of paragraph before a table row is only deleted if there
335 * is nothing else on the line before it. */
336 if (this_para == start_para &&
337 next_para->member.para.nFlags & MEPF_ROWSTART)
339 /* If the paragraph will be empty, then it should be deleted, however
340 * it still might have text right now which would inherit the
341 * MEPF_STARTROW property if we joined it right now.
342 * Instead we will delete it after the preceding text is deleted. */
343 if (nOfs > this_para->member.para.nCharOfs) {
344 /* Skip this end of line. */
345 nChars -= (eollen < nChars) ? eollen : nChars;
346 continue;
348 keepFirstParaFormat = TRUE;
351 ME_JoinParagraphs(editor, c.pPara, keepFirstParaFormat);
352 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
353 ME_CheckCharOffsets(editor);
354 nChars -= (eollen < nChars) ? eollen : nChars;
355 continue;
357 else
359 ME_Cursor cursor;
360 int nCharsToDelete = min(nChars, c.nOffset);
361 int i;
363 c.nOffset -= nCharsToDelete;
365 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
367 cursor = c;
368 /* nChars is the number of characters that should be deleted from the
369 PRECEDING runs (these BEFORE cursor.pRun)
370 nCharsToDelete is a number of chars to delete from THIS run */
371 nChars -= nCharsToDelete;
372 shift -= nCharsToDelete;
373 TRACE("Deleting %d (remaning %d) chars at %d in %s (%d)\n",
374 nCharsToDelete, nChars, c.nOffset,
375 debugstr_run( run ), run->len);
377 /* nOfs is a character offset (from the start of the document
378 to the current (deleted) run */
379 add_undo_insert_run( editor, nOfs + nChars, get_text( run, c.nOffset ), nCharsToDelete, run->nFlags, run->style );
381 ME_StrDeleteV(run->para->text, run->nCharOfs + c.nOffset, nCharsToDelete);
382 run->len -= nCharsToDelete;
383 TRACE("Post deletion string: %s (%d)\n", debugstr_run( run ), run->len);
384 TRACE("Shift value: %d\n", shift);
386 /* update cursors (including c) */
387 for (i=-1; i<editor->nCursors; i++) {
388 ME_Cursor *pThisCur = editor->pCursors + i;
389 if (i == -1) pThisCur = &c;
390 if (pThisCur->pRun == cursor.pRun) {
391 if (pThisCur->nOffset > cursor.nOffset) {
392 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
393 pThisCur->nOffset = cursor.nOffset;
394 else
395 pThisCur->nOffset -= nCharsToDelete;
396 assert(pThisCur->nOffset >= 0);
397 assert(pThisCur->nOffset <= run->len);
399 if (pThisCur->nOffset == run->len)
401 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
402 assert(pThisCur->pRun->type == diRun);
403 pThisCur->nOffset = 0;
408 /* c = updated data now */
410 if (c.pRun == cursor.pRun)
411 ME_SkipAndPropagateCharOffset(c.pRun, shift);
412 else
413 ME_PropagateCharOffset(c.pRun, shift);
415 if (!cursor.pRun->member.run.len)
417 TRACE("Removing empty run\n");
418 ME_Remove(cursor.pRun);
419 ME_DestroyDisplayItem(cursor.pRun);
422 shift = 0;
424 ME_CheckCharOffsets(editor);
426 continue;
429 if (delete_all) ME_SetDefaultParaFormat( start_para->member.para.pFmt );
430 return TRUE;
433 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
435 assert(nCursor>=0 && nCursor<editor->nCursors);
436 /* text operations set modified state */
437 editor->nModifyStep = 1;
438 return ME_InternalDeleteText(editor, &editor->pCursors[nCursor],
439 nChars, FALSE);
442 static ME_DisplayItem *
443 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
444 const WCHAR *str, int len, ME_Style *style,
445 int flags)
447 ME_Cursor *p = &editor->pCursors[nCursor];
449 editor->bCaretAtEnd = FALSE;
451 assert(p->pRun->type == diRun);
453 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
457 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
459 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
460 ME_DisplayItem *di;
461 WCHAR space = ' ';
463 /* FIXME no no no */
464 if (ME_IsSelection(editor))
465 ME_DeleteSelection(editor);
467 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
468 MERF_GRAPHICS);
469 di->member.run.ole_obj = ALLOC_OBJ(*reo);
470 ME_CopyReObject(di->member.run.ole_obj, reo);
471 ME_ReleaseStyle(pStyle);
475 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
477 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
478 WCHAR space = ' ';
480 /* FIXME no no no */
481 if (ME_IsSelection(editor))
482 ME_DeleteSelection(editor);
484 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
485 MERF_ENDROW);
486 ME_ReleaseStyle(pStyle);
490 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
491 const WCHAR *str, int len, ME_Style *style)
493 const WCHAR *pos;
494 ME_Cursor *p = NULL;
495 int oldLen;
497 /* FIXME really HERE ? */
498 if (ME_IsSelection(editor))
499 ME_DeleteSelection(editor);
501 /* FIXME: is this too slow? */
502 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
503 oldLen = ME_GetTextLength(editor);
505 /* text operations set modified state */
506 editor->nModifyStep = 1;
508 assert(style);
510 assert(nCursor>=0 && nCursor<editor->nCursors);
511 if (len == -1)
512 len = lstrlenW(str);
514 /* grow the text limit to fit our text */
515 if(editor->nTextLimit < oldLen +len)
516 editor->nTextLimit = oldLen + len;
518 pos = str;
520 while (len)
522 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
523 while(pos - str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
524 pos++;
526 if (pos != str) { /* handle text */
527 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
528 } else if (*pos == '\t') { /* handle tabs */
529 WCHAR tab = '\t';
530 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
531 pos++;
532 } else { /* handle EOLs */
533 ME_DisplayItem *tp, *end_run;
534 ME_Style *tmp_style;
535 int eol_len = 0;
537 /* Find number of CR and LF in end of paragraph run */
538 if (*pos =='\r')
540 if (len > 1 && pos[1] == '\n')
541 eol_len = 2;
542 else if (len > 2 && pos[1] == '\r' && pos[2] == '\n')
543 eol_len = 3;
544 else
545 eol_len = 1;
546 } else {
547 assert(*pos == '\n');
548 eol_len = 1;
550 pos += eol_len;
552 if (!editor->bEmulateVersion10 && eol_len == 3)
554 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
555 WCHAR space = ' ';
556 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
557 } else {
558 const WCHAR cr = '\r', *eol_str = str;
560 if (!editor->bEmulateVersion10)
562 eol_str = &cr;
563 eol_len = 1;
566 p = &editor->pCursors[nCursor];
567 if (p->nOffset)
568 ME_SplitRunSimple(editor, p);
569 tmp_style = ME_GetInsertStyle(editor, nCursor);
570 /* ME_SplitParagraph increases style refcount */
571 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, eol_str, eol_len, 0);
572 p->pRun = ME_FindItemFwd(tp, diRun);
573 p->pPara = tp;
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;
585 /* Move the cursor nRelOfs characters (either forwards or backwards)
587 * returns the actual number of characters moved.
589 int ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
591 cursor->nOffset += nRelOfs;
592 if (cursor->nOffset < 0)
594 cursor->nOffset += cursor->pRun->member.run.nCharOfs;
595 if (cursor->nOffset >= 0)
597 /* new offset in the same paragraph */
598 do {
599 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
600 } while (cursor->nOffset < cursor->pRun->member.run.nCharOfs);
601 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
602 return nRelOfs;
605 cursor->nOffset += cursor->pPara->member.para.nCharOfs;
606 if (cursor->nOffset <= 0)
608 /* moved to the start of the text */
609 nRelOfs -= cursor->nOffset;
610 ME_SetCursorToStart(editor, cursor);
611 return nRelOfs;
614 /* new offset in a previous paragraph */
615 do {
616 cursor->pPara = cursor->pPara->member.para.prev_para;
617 } while (cursor->nOffset < cursor->pPara->member.para.nCharOfs);
618 cursor->nOffset -= cursor->pPara->member.para.nCharOfs;
620 cursor->pRun = ME_FindItemBack(cursor->pPara->member.para.next_para, diRun);
621 while (cursor->nOffset < cursor->pRun->member.run.nCharOfs) {
622 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
624 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
625 } else if (cursor->nOffset >= cursor->pRun->member.run.len) {
626 ME_DisplayItem *next_para;
627 int new_offset;
629 new_offset = ME_GetCursorOfs(cursor);
630 next_para = cursor->pPara->member.para.next_para;
631 if (new_offset < next_para->member.para.nCharOfs)
633 /* new offset in the same paragraph */
634 do {
635 cursor->nOffset -= cursor->pRun->member.run.len;
636 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
637 } while (cursor->nOffset >= cursor->pRun->member.run.len);
638 return nRelOfs;
641 if (new_offset >= ME_GetTextLength(editor))
643 /* new offset at the end of the text */
644 ME_SetCursorToEnd(editor, cursor);
645 nRelOfs -= new_offset - ME_GetTextLength(editor);
646 return nRelOfs;
649 /* new offset in a following paragraph */
650 do {
651 cursor->pPara = next_para;
652 next_para = next_para->member.para.next_para;
653 } while (new_offset >= next_para->member.para.nCharOfs);
655 cursor->nOffset = new_offset - cursor->pPara->member.para.nCharOfs;
656 cursor->pRun = ME_FindItemFwd(cursor->pPara, diRun);
657 while (cursor->nOffset >= cursor->pRun->member.run.len)
659 cursor->nOffset -= cursor->pRun->member.run.len;
660 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
662 } /* else new offset is in the same run */
663 return nRelOfs;
667 static BOOL
668 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
670 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
671 ME_DisplayItem *pPara = cursor->pPara;
672 int nOffset = cursor->nOffset;
674 if (nRelOfs == -1)
676 /* Backward movement */
677 while (TRUE)
679 nOffset = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
680 pRun->member.run.len, nOffset, WB_MOVEWORDLEFT);
681 if (nOffset)
682 break;
683 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
684 if (pOtherRun->type == diRun)
686 if (ME_CallWordBreakProc(editor, get_text( &pOtherRun->member.run, 0 ),
687 pOtherRun->member.run.len,
688 pOtherRun->member.run.len - 1,
689 WB_ISDELIMITER)
690 && !(pRun->member.run.nFlags & MERF_ENDPARA)
691 && !(cursor->pRun == pRun && cursor->nOffset == 0)
692 && !ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
693 pRun->member.run.len, 0,
694 WB_ISDELIMITER))
695 break;
696 pRun = pOtherRun;
697 nOffset = pOtherRun->member.run.len;
699 else if (pOtherRun->type == diParagraph)
701 if (cursor->pRun == pRun && cursor->nOffset == 0)
703 pPara = pOtherRun;
704 /* Skip empty start of table row paragraph */
705 if (pPara->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART)
706 pPara = pPara->member.para.prev_para;
707 /* Paragraph breaks are treated as separate words */
708 if (pPara->member.para.prev_para->type == diTextStart)
709 return FALSE;
711 pRun = ME_FindItemBack(pPara, diRun);
712 pPara = pPara->member.para.prev_para;
714 break;
718 else
720 /* Forward movement */
721 BOOL last_delim = FALSE;
723 while (TRUE)
725 if (last_delim && !ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
726 pRun->member.run.len, nOffset, WB_ISDELIMITER))
727 break;
728 nOffset = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
729 pRun->member.run.len, nOffset, WB_MOVEWORDRIGHT);
730 if (nOffset < pRun->member.run.len)
731 break;
732 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
733 if (pOtherRun->type == diRun)
735 last_delim = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
736 pRun->member.run.len, nOffset - 1, WB_ISDELIMITER);
737 pRun = pOtherRun;
738 nOffset = 0;
740 else if (pOtherRun->type == diParagraph)
742 if (pOtherRun->member.para.nFlags & MEPF_ROWSTART)
743 pOtherRun = pOtherRun->member.para.next_para;
744 if (cursor->pRun == pRun) {
745 pPara = pOtherRun;
746 pRun = ME_FindItemFwd(pPara, diRun);
748 nOffset = 0;
749 break;
751 else /* diTextEnd */
753 if (cursor->pRun == pRun)
754 return FALSE;
755 nOffset = 0;
756 break;
760 cursor->pPara = pPara;
761 cursor->pRun = pRun;
762 cursor->nOffset = nOffset;
763 return TRUE;
767 static void
768 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
770 /* pCursor[0] is the end of the selection
771 * pCursor[1] is the start of the selection (or the position selection anchor)
772 * pCursor[2] and [3] are the selection anchors that are backed up
773 * so they are kept when the selection changes for drag selection.
776 editor->nSelectionType = selectionType;
777 switch(selectionType)
779 case stPosition:
780 break;
781 case stWord:
782 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
783 editor->pCursors[1] = editor->pCursors[0];
784 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
785 break;
786 case stLine:
787 case stParagraph:
789 ME_DisplayItem *pItem;
790 ME_DIType fwdSearchType, backSearchType;
791 if (selectionType == stParagraph) {
792 backSearchType = diParagraph;
793 fwdSearchType = diParagraphOrEnd;
794 } else {
795 backSearchType = diStartRow;
796 fwdSearchType = diStartRowOrParagraphOrEnd;
798 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
799 assert(pItem);
800 if (pItem->type == diTextEnd)
801 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
802 else
803 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
804 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
805 editor->pCursors[0].nOffset = 0;
807 pItem = ME_FindItemBack(pItem, backSearchType);
808 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
809 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
810 editor->pCursors[1].nOffset = 0;
811 break;
813 case stDocument:
814 /* Select everything with cursor anchored from the start of the text */
815 editor->nSelectionType = stDocument;
816 ME_SetCursorToStart(editor, &editor->pCursors[1]);
817 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
818 break;
819 default: assert(0);
821 /* Store the anchor positions for extending the selection. */
822 editor->pCursors[2] = editor->pCursors[0];
823 editor->pCursors[3] = editor->pCursors[1];
826 int ME_GetCursorOfs(const ME_Cursor *cursor)
828 return cursor->pPara->member.para.nCharOfs
829 + cursor->pRun->member.run.nCharOfs + cursor->nOffset;
832 /* Helper function for ME_FindPixelPos to find paragraph within tables */
833 static ME_DisplayItem* ME_FindPixelPosInTableRow(int x, int y,
834 ME_DisplayItem *para)
836 ME_DisplayItem *cell, *next_cell;
837 assert(para->member.para.nFlags & MEPF_ROWSTART);
838 cell = para->member.para.next_para->member.para.pCell;
839 assert(cell);
841 /* find the cell we are in */
842 while ((next_cell = cell->member.cell.next_cell) != NULL) {
843 if (x < next_cell->member.cell.pt.x)
845 para = ME_FindItemFwd(cell, diParagraph);
846 /* Found the cell, but there might be multiple paragraphs in
847 * the cell, so need to search down the cell for the paragraph. */
848 while (cell == para->member.para.pCell) {
849 if (y < para->member.para.pt.y + para->member.para.nHeight)
851 if (para->member.para.nFlags & MEPF_ROWSTART)
852 return ME_FindPixelPosInTableRow(x, y, para);
853 else
854 return para;
856 para = para->member.para.next_para;
858 /* Past the end of the cell, so go back to the last cell paragraph */
859 return para->member.para.prev_para;
861 cell = next_cell;
863 /* Return table row delimiter */
864 para = ME_FindItemFwd(cell, diParagraph);
865 assert(para->member.para.nFlags & MEPF_ROWEND);
866 assert(para->member.para.pFmt->dwMask & PFM_TABLEROWDELIMITER);
867 assert(para->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER);
868 return para;
871 static BOOL ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
872 int x, ME_Cursor *cursor, int *pbCaretAtEnd)
874 ME_DisplayItem *pNext, *pLastRun;
875 ME_Row *row = &pRow->member.row;
876 BOOL exact = TRUE;
878 if (x < row->pt.x)
880 x = row->pt.x;
881 exact = FALSE;
883 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
884 assert(pNext->type == diRun);
885 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
886 cursor->nOffset = 0;
887 do {
888 int run_x = pNext->member.run.pt.x;
889 int width = pNext->member.run.nWidth;
891 if (x >= run_x && x < run_x+width)
893 cursor->nOffset = ME_CharFromPoint(editor, x-run_x, &pNext->member.run, TRUE, TRUE);
894 cursor->pRun = pNext;
895 cursor->pPara = ME_GetParagraph( cursor->pRun );
896 return exact;
898 pLastRun = pNext;
899 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
900 } while(pNext && pNext->type == diRun);
902 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
904 cursor->pRun = ME_FindItemFwd(pNext, diRun);
905 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
907 else
908 cursor->pRun = pLastRun;
910 cursor->pPara = ME_GetParagraph( cursor->pRun );
911 return FALSE;
914 /* Finds the run and offset from the pixel position.
916 * x & y are pixel positions in virtual coordinates into the rich edit control,
917 * so client coordinates must first be adjusted by the scroll position.
919 * returns TRUE if the result was exactly under the cursor, otherwise returns
920 * FALSE, and result is set to the closest position to the coordinates.
922 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
923 ME_Cursor *result, BOOL *is_eol)
925 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
926 BOOL isExact = TRUE;
928 x -= editor->rcFormat.left;
929 y -= editor->rcFormat.top;
931 if (is_eol)
932 *is_eol = 0;
934 /* find paragraph */
935 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
937 assert(p->type == diParagraph);
938 if (y < p->member.para.pt.y + p->member.para.nHeight)
940 if (p->member.para.nFlags & MEPF_ROWSTART)
941 p = ME_FindPixelPosInTableRow(x, y, p);
942 y -= p->member.para.pt.y;
943 p = ME_FindItemFwd(p, diStartRow);
944 break;
945 } else if (p->member.para.nFlags & MEPF_ROWSTART) {
946 p = ME_GetTableRowEnd(p);
949 /* find row */
950 for (; p != editor->pBuffer->pLast; )
952 ME_DisplayItem *pp;
953 assert(p->type == diStartRow);
954 if (y < p->member.row.pt.y + p->member.row.nHeight) break;
955 pp = ME_FindItemFwd(p, diStartRow);
956 if (!pp) break;
957 p = pp;
959 if (p == editor->pBuffer->pLast)
961 /* The position is below the last paragraph, so the last row will be used
962 * rather than the end of the text, so the x position will be used to
963 * determine the offset closest to the pixel position. */
964 isExact = FALSE;
965 p = ME_FindItemBack(p, diStartRow);
966 if (!p) p = editor->pBuffer->pLast;
969 assert( p->type == diStartRow || p == editor->pBuffer->pLast );
971 if( p->type == diStartRow )
972 return ME_FindRunInRow( editor, p, x, result, is_eol ) && isExact;
974 result->pRun = ME_FindItemBack(p, diRun);
975 result->pPara = ME_GetParagraph(result->pRun);
976 result->nOffset = 0;
977 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
978 return FALSE;
982 /* Sets the cursor to the position closest to the pixel position
984 * x & y are pixel positions in client coordinates.
986 * isExact will be set to TRUE if the run is directly under the pixel
987 * position, FALSE if it not, unless isExact is set to NULL.
989 * return FALSE if outside client area and the cursor is not set,
990 * otherwise TRUE is returned.
992 BOOL ME_CharFromPos(ME_TextEditor *editor, int x, int y,
993 ME_Cursor *cursor, BOOL *isExact)
995 RECT rc;
996 BOOL bResult;
998 ITextHost_TxGetClientRect(editor->texthost, &rc);
999 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
1000 if (isExact) *isExact = FALSE;
1001 return FALSE;
1003 x += editor->horz_si.nPos;
1004 y += editor->vert_si.nPos;
1005 bResult = ME_FindPixelPos(editor, x, y, cursor, NULL);
1006 if (isExact) *isExact = bResult;
1007 return TRUE;
1012 /* Extends the selection with a word, line, or paragraph selection type.
1014 * The selection is anchored by editor->pCursors[2-3] such that the text
1015 * between the anchors will remain selected, and one end will be extended.
1017 * editor->pCursors[0] should have the position to extend the selection to
1018 * before this function is called.
1020 * Nothing will be done if editor->nSelectionType equals stPosition.
1022 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1024 ME_Cursor tmp_cursor;
1025 int curOfs, anchorStartOfs, anchorEndOfs;
1026 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1027 return;
1028 curOfs = ME_GetCursorOfs(&editor->pCursors[0]);
1029 anchorStartOfs = ME_GetCursorOfs(&editor->pCursors[3]);
1030 anchorEndOfs = ME_GetCursorOfs(&editor->pCursors[2]);
1032 tmp_cursor = editor->pCursors[0];
1033 editor->pCursors[0] = editor->pCursors[2];
1034 editor->pCursors[1] = editor->pCursors[3];
1035 if (curOfs < anchorStartOfs)
1037 /* Extend the left side of selection */
1038 editor->pCursors[1] = tmp_cursor;
1039 if (editor->nSelectionType == stWord)
1040 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1041 else
1043 ME_DisplayItem *pItem;
1044 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1045 diStartRowOrParagraph:diParagraph);
1046 pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
1047 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
1048 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
1049 editor->pCursors[1].nOffset = 0;
1052 else if (curOfs >= anchorEndOfs)
1054 /* Extend the right side of selection */
1055 editor->pCursors[0] = tmp_cursor;
1056 if (editor->nSelectionType == stWord)
1057 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
1058 else
1060 ME_DisplayItem *pItem;
1061 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1062 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
1063 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
1064 if (pItem->type == diTextEnd)
1065 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
1066 else
1067 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
1068 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
1069 editor->pCursors[0].nOffset = 0;
1074 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1076 ME_Cursor tmp_cursor;
1077 int is_selection = 0;
1078 BOOL is_shift;
1080 editor->nUDArrowX = -1;
1082 x += editor->horz_si.nPos;
1083 y += editor->vert_si.nPos;
1085 tmp_cursor = editor->pCursors[0];
1086 is_selection = ME_IsSelection(editor);
1087 is_shift = GetKeyState(VK_SHIFT) < 0;
1089 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
1091 if (x >= editor->rcFormat.left || is_shift)
1093 if (clickNum > 1)
1095 editor->pCursors[1] = editor->pCursors[0];
1096 if (is_shift) {
1097 if (x >= editor->rcFormat.left)
1098 ME_SelectByType(editor, stWord);
1099 else
1100 ME_SelectByType(editor, stParagraph);
1101 } else if (clickNum % 2 == 0) {
1102 ME_SelectByType(editor, stWord);
1103 } else {
1104 ME_SelectByType(editor, stParagraph);
1107 else if (!is_shift)
1109 editor->nSelectionType = stPosition;
1110 editor->pCursors[1] = editor->pCursors[0];
1112 else if (!is_selection)
1114 editor->nSelectionType = stPosition;
1115 editor->pCursors[1] = tmp_cursor;
1117 else if (editor->nSelectionType != stPosition)
1119 ME_ExtendAnchorSelection(editor);
1122 else
1124 if (clickNum < 2) {
1125 ME_SelectByType(editor, stLine);
1126 } else if (clickNum % 2 == 0 || is_shift) {
1127 ME_SelectByType(editor, stParagraph);
1128 } else {
1129 ME_SelectByType(editor, stDocument);
1132 ME_InvalidateSelection(editor);
1133 ITextHost_TxShowCaret(editor->texthost, FALSE);
1134 ME_ShowCaret(editor);
1135 ME_ClearTempStyle(editor);
1136 ME_SendSelChange(editor);
1139 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1141 ME_Cursor tmp_cursor;
1143 if (editor->nSelectionType == stDocument)
1144 return;
1145 x += editor->horz_si.nPos;
1146 y += editor->vert_si.nPos;
1148 tmp_cursor = editor->pCursors[0];
1149 /* FIXME: do something with the return value of ME_FindPixelPos */
1150 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
1152 ME_InvalidateSelection(editor);
1153 editor->pCursors[0] = tmp_cursor;
1154 ME_ExtendAnchorSelection(editor);
1156 if (editor->nSelectionType != stPosition &&
1157 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1159 /* The scroll the cursor towards the other end, since it was the one
1160 * extended by ME_ExtendAnchorSelection */
1161 ME_EnsureVisible(editor, &editor->pCursors[1]);
1162 } else {
1163 ME_EnsureVisible(editor, &editor->pCursors[0]);
1166 ME_InvalidateSelection(editor);
1167 ITextHost_TxShowCaret(editor->texthost, FALSE);
1168 ME_ShowCaret(editor);
1169 ME_SendSelChange(editor);
1172 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1174 ME_DisplayItem *pRun = pCursor->pRun;
1175 int x;
1177 if (editor->nUDArrowX != -1)
1178 x = editor->nUDArrowX;
1179 else {
1180 if (editor->bCaretAtEnd)
1182 pRun = ME_FindItemBack(pRun, diRun);
1183 assert(pRun);
1184 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1186 else {
1187 x = pRun->member.run.pt.x;
1188 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset, TRUE);
1190 editor->nUDArrowX = x;
1192 return x;
1196 static void
1197 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1199 ME_DisplayItem *pRun = pCursor->pRun;
1200 ME_DisplayItem *pOldPara = pCursor->pPara;
1201 ME_DisplayItem *pItem, *pNewPara;
1202 int x = ME_GetXForArrow(editor, pCursor);
1204 if (editor->bCaretAtEnd && !pCursor->nOffset)
1205 if (!ME_PrevRun(&pOldPara, &pRun))
1206 return;
1208 if (nRelOfs == -1)
1210 /* start of this row */
1211 pItem = ME_FindItemBack(pRun, diStartRow);
1212 assert(pItem);
1213 /* start of the previous row */
1214 pItem = ME_FindItemBack(pItem, diStartRow);
1215 if (!pItem)
1216 return; /* row not found - ignore */
1217 pNewPara = ME_GetParagraph(pItem);
1218 if (pOldPara->member.para.nFlags & MEPF_ROWEND ||
1219 (pOldPara->member.para.pCell &&
1220 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1222 /* Brought out of a cell */
1223 pNewPara = ME_GetTableRowStart(pOldPara)->member.para.prev_para;
1224 if (pNewPara->type == diTextStart)
1225 return; /* At the top, so don't go anywhere. */
1226 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1228 if (pNewPara->member.para.nFlags & MEPF_ROWEND)
1230 /* Brought into a table row */
1231 ME_Cell *cell = &ME_FindItemBack(pNewPara, diCell)->member.cell;
1232 while (x < cell->pt.x && cell->prev_cell)
1233 cell = &cell->prev_cell->member.cell;
1234 if (cell->next_cell) /* else - we are still at the end of the row */
1235 pItem = ME_FindItemBack(cell->next_cell, diStartRow);
1238 else
1240 /* start of the next row */
1241 pItem = ME_FindItemFwd(pRun, diStartRow);
1242 if (!pItem)
1243 return; /* row not found - ignore */
1244 pNewPara = ME_GetParagraph(pItem);
1245 if (pOldPara->member.para.nFlags & MEPF_ROWSTART ||
1246 (pOldPara->member.para.pCell &&
1247 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1249 /* Brought out of a cell */
1250 pNewPara = ME_GetTableRowEnd(pOldPara)->member.para.next_para;
1251 if (pNewPara->type == diTextEnd)
1252 return; /* At the bottom, so don't go anywhere. */
1253 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1255 if (pNewPara->member.para.nFlags & MEPF_ROWSTART)
1257 /* Brought into a table row */
1258 ME_DisplayItem *cell = ME_FindItemFwd(pNewPara, diCell);
1259 while (cell->member.cell.next_cell &&
1260 x >= cell->member.cell.next_cell->member.cell.pt.x)
1261 cell = cell->member.cell.next_cell;
1262 pItem = ME_FindItemFwd(cell, diStartRow);
1265 if (!pItem)
1267 /* row not found - ignore */
1268 return;
1270 ME_FindRunInRow(editor, pItem, x, pCursor, &editor->bCaretAtEnd);
1271 assert(pCursor->pRun);
1272 assert(pCursor->pRun->type == diRun);
1275 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1277 ME_DisplayItem *p = ME_FindItemFwd(editor->pBuffer->pFirst, diStartRow);
1279 if (editor->vert_si.nPos < p->member.row.nHeight)
1281 ME_SetCursorToStart(editor, pCursor);
1282 editor->bCaretAtEnd = FALSE;
1283 /* Native clears seems to clear this x value on page up at the top
1284 * of the text, but not on page down at the end of the text.
1285 * Doesn't make sense, but we try to be bug for bug compatible. */
1286 editor->nUDArrowX = -1;
1287 } else {
1288 ME_DisplayItem *pRun = pCursor->pRun;
1289 ME_DisplayItem *pLast;
1290 int x, y, yd, yp;
1291 int yOldScrollPos = editor->vert_si.nPos;
1293 x = ME_GetXForArrow(editor, pCursor);
1294 if (!pCursor->nOffset && editor->bCaretAtEnd)
1295 pRun = ME_FindItemBack(pRun, diRun);
1297 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1298 assert(p->type == diStartRow);
1299 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1300 y = yp + p->member.row.pt.y;
1302 ME_ScrollUp(editor, editor->sizeWindow.cy);
1303 /* Only move the cursor by the amount scrolled. */
1304 yd = y + editor->vert_si.nPos - yOldScrollPos;
1305 pLast = p;
1307 do {
1308 p = ME_FindItemBack(p, diStartRowOrParagraph);
1309 if (!p)
1310 break;
1311 if (p->type == diParagraph) { /* crossing paragraphs */
1312 if (p->member.para.prev_para == NULL)
1313 break;
1314 yp = p->member.para.prev_para->member.para.pt.y;
1315 continue;
1317 y = yp + p->member.row.pt.y;
1318 if (y < yd)
1319 break;
1320 pLast = p;
1321 } while(1);
1323 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
1325 assert(pCursor->pRun);
1326 assert(pCursor->pRun->type == diRun);
1329 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1331 ME_DisplayItem *pLast;
1332 int x, y;
1334 /* Find y position of the last row */
1335 pLast = editor->pBuffer->pLast;
1336 y = pLast->member.para.prev_para->member.para.pt.y
1337 + ME_FindItemBack(pLast, diStartRow)->member.row.pt.y;
1339 x = ME_GetXForArrow(editor, pCursor);
1341 if (editor->vert_si.nPos >= y - editor->sizeWindow.cy)
1343 ME_SetCursorToEnd(editor, pCursor);
1344 editor->bCaretAtEnd = FALSE;
1345 } else {
1346 ME_DisplayItem *pRun = pCursor->pRun;
1347 ME_DisplayItem *p;
1348 int yd, yp;
1349 int yOldScrollPos = editor->vert_si.nPos;
1351 if (!pCursor->nOffset && editor->bCaretAtEnd)
1352 pRun = ME_FindItemBack(pRun, diRun);
1354 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1355 assert(p->type == diStartRow);
1356 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1357 y = yp + p->member.row.pt.y;
1359 /* For native richedit controls:
1360 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1361 * v4.1 can scroll past this position here. */
1362 ME_ScrollDown(editor, editor->sizeWindow.cy);
1363 /* Only move the cursor by the amount scrolled. */
1364 yd = y + editor->vert_si.nPos - yOldScrollPos;
1365 pLast = p;
1367 do {
1368 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1369 if (!p)
1370 break;
1371 if (p->type == diParagraph) {
1372 yp = p->member.para.pt.y;
1373 continue;
1375 y = yp + p->member.row.pt.y;
1376 if (y >= yd)
1377 break;
1378 pLast = p;
1379 } while(1);
1381 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
1383 assert(pCursor->pRun);
1384 assert(pCursor->pRun->type == diRun);
1387 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1389 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1390 if (pRow) {
1391 ME_DisplayItem *pRun;
1392 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1393 pRow = ME_FindItemBack(pRow, diStartRow);
1394 if (!pRow)
1395 return;
1397 pRun = ME_FindItemFwd(pRow, diRun);
1398 if (pRun) {
1399 pCursor->pRun = pRun;
1400 assert(pCursor->pPara == ME_GetParagraph(pRun));
1401 pCursor->nOffset = 0;
1404 editor->bCaretAtEnd = FALSE;
1407 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1409 ME_SetCursorToStart(editor, pCursor);
1410 editor->bCaretAtEnd = FALSE;
1413 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1415 ME_DisplayItem *pRow;
1417 if (editor->bCaretAtEnd && !pCursor->nOffset)
1418 return;
1420 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1421 assert(pRow);
1422 if (pRow->type == diStartRow) {
1423 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1424 assert(pRun);
1425 pCursor->pRun = pRun;
1426 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1427 pCursor->nOffset = 0;
1428 editor->bCaretAtEnd = TRUE;
1429 return;
1431 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1432 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1433 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1434 pCursor->nOffset = 0;
1435 editor->bCaretAtEnd = FALSE;
1438 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1440 ME_SetCursorToEnd(editor, pCursor);
1441 editor->bCaretAtEnd = FALSE;
1444 BOOL ME_IsSelection(ME_TextEditor *editor)
1446 return editor->pCursors[0].pRun != editor->pCursors[1].pRun ||
1447 editor->pCursors[0].nOffset != editor->pCursors[1].nOffset;
1450 void ME_DeleteSelection(ME_TextEditor *editor)
1452 int from, to;
1453 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
1454 ME_DeleteTextAtCursor(editor, nStartCursor, to - from);
1457 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1459 return ME_GetInsertStyle(editor, 0);
1462 void ME_SendSelChange(ME_TextEditor *editor)
1464 SELCHANGE sc;
1466 if (!(editor->nEventMask & ENM_SELCHANGE))
1467 return;
1469 sc.nmhdr.hwndFrom = NULL;
1470 sc.nmhdr.idFrom = 0;
1471 sc.nmhdr.code = EN_SELCHANGE;
1472 ME_GetSelectionOfs(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1473 sc.seltyp = SEL_EMPTY;
1474 if (sc.chrg.cpMin != sc.chrg.cpMax)
1475 sc.seltyp |= SEL_TEXT;
1476 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* what were RICHEDIT authors thinking ? */
1477 sc.seltyp |= SEL_MULTICHAR;
1478 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1479 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1480 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1481 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1482 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1484 ME_ClearTempStyle(editor);
1486 editor->notified_cr = sc.chrg;
1487 ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
1491 BOOL
1492 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1494 int nCursor = 0;
1495 ME_Cursor *p = &editor->pCursors[nCursor];
1496 ME_Cursor tmp_curs = *p;
1497 BOOL success = FALSE;
1499 ME_CheckCharOffsets(editor);
1500 switch(nVKey) {
1501 case VK_LEFT:
1502 editor->bCaretAtEnd = 0;
1503 if (ctrl)
1504 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1505 else
1506 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1507 break;
1508 case VK_RIGHT:
1509 editor->bCaretAtEnd = 0;
1510 if (ctrl)
1511 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1512 else
1513 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1514 break;
1515 case VK_UP:
1516 ME_MoveCursorLines(editor, &tmp_curs, -1);
1517 break;
1518 case VK_DOWN:
1519 ME_MoveCursorLines(editor, &tmp_curs, +1);
1520 break;
1521 case VK_PRIOR:
1522 ME_ArrowPageUp(editor, &tmp_curs);
1523 break;
1524 case VK_NEXT:
1525 ME_ArrowPageDown(editor, &tmp_curs);
1526 break;
1527 case VK_HOME: {
1528 if (ctrl)
1529 ME_ArrowCtrlHome(editor, &tmp_curs);
1530 else
1531 ME_ArrowHome(editor, &tmp_curs);
1532 editor->bCaretAtEnd = 0;
1533 break;
1535 case VK_END:
1536 if (ctrl)
1537 ME_ArrowCtrlEnd(editor, &tmp_curs);
1538 else
1539 ME_ArrowEnd(editor, &tmp_curs);
1540 break;
1543 if (!extend)
1544 editor->pCursors[1] = tmp_curs;
1545 *p = tmp_curs;
1547 ME_InvalidateSelection(editor);
1548 ME_Repaint(editor);
1549 ITextHost_TxShowCaret(editor->texthost, FALSE);
1550 ME_EnsureVisible(editor, &tmp_curs);
1551 ME_ShowCaret(editor);
1552 ME_SendSelChange(editor);
1553 return success;