dwrite: Implement CreateFontFaceFromHdc().
[wine.git] / dlls / riched20 / caret.c
blob043a32a0d64089b5f29dfb8bfb65bf777afca2e8
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 int from_ofs = ME_GetCursorOfs( &editor->pCursors[0] );
60 int to_ofs = ME_GetCursorOfs( &editor->pCursors[1] );
61 BOOL swap = (from_ofs > to_ofs);
63 if (from_ofs == to_ofs)
65 /* If cursor[0] is at the beginning of a run and cursor[1] at the end
66 of the prev run then we need to swap. */
67 if (editor->pCursors[0].nOffset < editor->pCursors[1].nOffset)
68 swap = TRUE;
71 if (!swap)
73 *from = &editor->pCursors[0];
74 *to = &editor->pCursors[1];
75 return 0;
76 } else {
77 *from = &editor->pCursors[1];
78 *to = &editor->pCursors[0];
79 return 1;
83 int ME_GetTextLength(ME_TextEditor *editor)
85 ME_Cursor cursor;
86 ME_SetCursorToEnd(editor, &cursor);
87 return ME_GetCursorOfs(&cursor);
91 int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how)
93 int length;
95 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
96 return E_INVALIDARG;
97 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
98 return E_INVALIDARG;
100 length = ME_GetTextLength(editor);
102 if ((editor->styleFlags & ES_MULTILINE)
103 && (how->flags & GTL_USECRLF)
104 && !editor->bEmulateVersion10) /* Ignore GTL_USECRLF flag in 1.0 emulation */
105 length += editor->nParagraphs - 1;
107 if (how->flags & GTL_NUMBYTES ||
108 (how->flags & GTL_PRECISE && /* GTL_PRECISE seems to imply GTL_NUMBYTES */
109 !(how->flags & GTL_NUMCHARS))) /* unless GTL_NUMCHARS is given */
111 CPINFO cpinfo;
113 if (how->codepage == 1200)
114 return length * 2;
115 if (how->flags & GTL_PRECISE)
116 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
117 if (GetCPInfo(how->codepage, &cpinfo))
118 return length * cpinfo.MaxCharSize;
119 ERR("Invalid codepage %u\n", how->codepage);
120 return E_INVALIDARG;
122 return length;
126 int ME_SetSelection(ME_TextEditor *editor, int from, int to)
128 int selectionEnd = 0;
129 const int len = ME_GetTextLength(editor);
131 /* all negative values are effectively the same */
132 if (from < 0)
133 from = -1;
134 if (to < 0)
135 to = -1;
137 /* select all */
138 if (from == 0 && to == -1)
140 ME_SetCursorToStart(editor, &editor->pCursors[1]);
141 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
142 editor->pCursors[0].nOffset = editor->pCursors[0].pRun->member.run.len;
143 ME_InvalidateSelection(editor);
144 return len + 1;
147 /* if both values are equal and also out of bound, that means to */
148 /* put the selection at the end of the text */
149 if ((from == to) && (to < 0 || to > len))
151 selectionEnd = 1;
153 else
155 /* if from is negative and to is positive then selection is */
156 /* deselected and caret moved to end of the current selection */
157 if (from < 0)
159 int start, end;
160 ME_GetSelectionOfs(editor, &start, &end);
161 if (start != end)
163 if (end > len)
165 editor->pCursors[0].nOffset = 0;
166 end --;
168 editor->pCursors[1] = editor->pCursors[0];
169 ME_Repaint(editor);
171 return end;
174 /* adjust to if it's a negative value */
175 if (to < 0)
176 to = len + 1;
178 /* flip from and to if they are reversed */
179 if (from>to)
181 int tmp = from;
182 from = to;
183 to = tmp;
186 /* after fiddling with the values, we find from > len && to > len */
187 if (from > len)
188 selectionEnd = 1;
189 /* special case with to too big */
190 else if (to > len)
191 to = len + 1;
194 if (selectionEnd)
196 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
197 editor->pCursors[1] = editor->pCursors[0];
198 ME_InvalidateSelection(editor);
199 return len;
202 ME_CursorFromCharOfs(editor, from, &editor->pCursors[1]);
203 editor->pCursors[0] = editor->pCursors[1];
204 ME_MoveCursorChars(editor, &editor->pCursors[0], to - from);
205 /* Selection is not allowed in the middle of an end paragraph run. */
206 if (editor->pCursors[1].pRun->member.run.nFlags & MERF_ENDPARA)
207 editor->pCursors[1].nOffset = 0;
208 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA)
210 if (to > len)
211 editor->pCursors[0].nOffset = editor->pCursors[0].pRun->member.run.len;
212 else
213 editor->pCursors[0].nOffset = 0;
215 return to;
219 static void
220 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
221 int *x, int *y, int *height)
223 ME_DisplayItem *row;
224 ME_DisplayItem *run = pCursor->pRun;
225 ME_DisplayItem *para = pCursor->pPara;
226 ME_DisplayItem *pSizeRun = run;
227 ME_Context c;
228 int run_x;
230 assert(height && x && y);
231 assert(~para->member.para.nFlags & MEPF_REWRAP);
232 assert(run && run->type == diRun);
233 assert(para && para->type == diParagraph);
235 row = ME_FindItemBack(run, diStartRowOrParagraph);
236 assert(row && row->type == diStartRow);
238 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
240 if (!pCursor->nOffset)
242 ME_DisplayItem *prev = ME_FindItemBack(run, diRunOrParagraph);
243 assert(prev);
244 if (prev->type == diRun)
245 pSizeRun = prev;
247 if (editor->bCaretAtEnd && !pCursor->nOffset &&
248 run == ME_FindItemFwd(row, diRun))
250 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
251 assert(tmp);
252 if (tmp->type == diRun)
254 row = ME_FindItemBack(tmp, diStartRow);
255 pSizeRun = run = tmp;
256 assert(run);
257 assert(run->type == diRun);
260 run_x = ME_PointFromCharContext( &c, &run->member.run, pCursor->nOffset, TRUE );
262 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
263 *x = c.rcView.left + run->member.run.pt.x + run_x - editor->horz_si.nPos;
264 *y = c.rcView.top + para->member.para.pt.y + row->member.row.nBaseline
265 + run->member.run.pt.y - pSizeRun->member.run.nAscent
266 - editor->vert_si.nPos;
267 ME_DestroyContext(&c);
268 return;
272 void
273 ME_MoveCaret(ME_TextEditor *editor)
275 int x, y, height;
277 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
278 if(editor->bHaveFocus && !ME_IsSelection(editor))
280 x = min(x, editor->rcFormat.right-1);
281 ITextHost_TxCreateCaret(editor->texthost, NULL, 0, height);
282 ITextHost_TxSetCaretPos(editor->texthost, x, y);
287 void ME_ShowCaret(ME_TextEditor *ed)
289 ME_MoveCaret(ed);
290 if(ed->bHaveFocus && !ME_IsSelection(ed))
291 ITextHost_TxShowCaret(ed->texthost, TRUE);
294 void ME_HideCaret(ME_TextEditor *ed)
296 if(!ed->bHaveFocus || ME_IsSelection(ed))
298 ITextHost_TxShowCaret(ed->texthost, FALSE);
299 DestroyCaret();
303 BOOL ME_InternalDeleteText(ME_TextEditor *editor, ME_Cursor *start,
304 int nChars, BOOL bForce)
306 ME_Cursor c = *start;
307 int nOfs = ME_GetCursorOfs(start), text_len = ME_GetTextLength( editor );
308 int shift = 0;
309 int totalChars = nChars;
310 ME_DisplayItem *start_para;
311 BOOL delete_all = FALSE;
313 /* Prevent deletion past last end of paragraph run. */
314 nChars = min(nChars, text_len - nOfs);
315 if (nChars == text_len) delete_all = TRUE;
316 start_para = c.pPara;
318 if (!bForce)
320 ME_ProtectPartialTableDeletion(editor, &c, &nChars);
321 if (nChars == 0)
322 return FALSE;
325 while(nChars > 0)
327 ME_Run *run;
328 ME_CursorFromCharOfs(editor, nOfs+nChars, &c);
329 if (!c.nOffset &&
330 nOfs+nChars == (c.pRun->member.run.nCharOfs
331 + c.pPara->member.para.nCharOfs))
333 /* We aren't deleting anything in this run, so we will go back to the
334 * last run we are deleting text in. */
335 ME_PrevRun(&c.pPara, &c.pRun);
336 c.nOffset = c.pRun->member.run.len;
338 run = &c.pRun->member.run;
339 if (run->nFlags & MERF_ENDPARA) {
340 int eollen = c.pRun->member.run.len;
341 BOOL keepFirstParaFormat;
343 if (!ME_FindItemFwd(c.pRun, diParagraph))
345 return TRUE;
347 keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
348 run->nCharOfs);
349 if (!editor->bEmulateVersion10) /* v4.1 */
351 ME_DisplayItem *next_para = ME_FindItemFwd(c.pRun, diParagraphOrEnd);
352 ME_DisplayItem *this_para = next_para->member.para.prev_para;
354 /* The end of paragraph before a table row is only deleted if there
355 * is nothing else on the line before it. */
356 if (this_para == start_para &&
357 next_para->member.para.nFlags & MEPF_ROWSTART)
359 /* If the paragraph will be empty, then it should be deleted, however
360 * it still might have text right now which would inherit the
361 * MEPF_STARTROW property if we joined it right now.
362 * Instead we will delete it after the preceding text is deleted. */
363 if (nOfs > this_para->member.para.nCharOfs) {
364 /* Skip this end of line. */
365 nChars -= (eollen < nChars) ? eollen : nChars;
366 continue;
368 keepFirstParaFormat = TRUE;
371 ME_JoinParagraphs(editor, c.pPara, keepFirstParaFormat);
372 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
373 ME_CheckCharOffsets(editor);
374 nChars -= (eollen < nChars) ? eollen : nChars;
375 continue;
377 else
379 ME_Cursor cursor;
380 int nCharsToDelete = min(nChars, c.nOffset);
381 int i;
383 c.nOffset -= nCharsToDelete;
385 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
387 cursor = c;
388 /* nChars is the number of characters that should be deleted from the
389 PRECEDING runs (these BEFORE cursor.pRun)
390 nCharsToDelete is a number of chars to delete from THIS run */
391 nChars -= nCharsToDelete;
392 shift -= nCharsToDelete;
393 TRACE("Deleting %d (remaning %d) chars at %d in %s (%d)\n",
394 nCharsToDelete, nChars, c.nOffset,
395 debugstr_run( run ), run->len);
397 /* nOfs is a character offset (from the start of the document
398 to the current (deleted) run */
399 add_undo_insert_run( editor, nOfs + nChars, get_text( run, c.nOffset ), nCharsToDelete, run->nFlags, run->style );
401 ME_StrDeleteV(run->para->text, run->nCharOfs + c.nOffset, nCharsToDelete);
402 run->len -= nCharsToDelete;
403 TRACE("Post deletion string: %s (%d)\n", debugstr_run( run ), run->len);
404 TRACE("Shift value: %d\n", shift);
406 /* update cursors (including c) */
407 for (i=-1; i<editor->nCursors; i++) {
408 ME_Cursor *pThisCur = editor->pCursors + i;
409 if (i == -1) pThisCur = &c;
410 if (pThisCur->pRun == cursor.pRun) {
411 if (pThisCur->nOffset > cursor.nOffset) {
412 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
413 pThisCur->nOffset = cursor.nOffset;
414 else
415 pThisCur->nOffset -= nCharsToDelete;
416 assert(pThisCur->nOffset >= 0);
417 assert(pThisCur->nOffset <= run->len);
419 if (pThisCur->nOffset == run->len)
421 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
422 assert(pThisCur->pRun->type == diRun);
423 pThisCur->nOffset = 0;
428 /* c = updated data now */
430 if (c.pRun == cursor.pRun)
431 ME_SkipAndPropagateCharOffset(c.pRun, shift);
432 else
433 ME_PropagateCharOffset(c.pRun, shift);
435 if (!cursor.pRun->member.run.len)
437 TRACE("Removing empty run\n");
438 ME_Remove(cursor.pRun);
439 ME_DestroyDisplayItem(cursor.pRun);
442 shift = 0;
444 ME_CheckCharOffsets(editor);
446 continue;
449 if (delete_all) ME_SetDefaultParaFormat( start_para->member.para.pFmt );
450 return TRUE;
453 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
455 assert(nCursor>=0 && nCursor<editor->nCursors);
456 /* text operations set modified state */
457 editor->nModifyStep = 1;
458 return ME_InternalDeleteText(editor, &editor->pCursors[nCursor],
459 nChars, FALSE);
462 static ME_DisplayItem *
463 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
464 const WCHAR *str, int len, ME_Style *style,
465 int flags)
467 ME_Cursor *p = &editor->pCursors[nCursor];
469 editor->bCaretAtEnd = FALSE;
471 assert(p->pRun->type == diRun);
473 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
477 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
479 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
480 ME_DisplayItem *di;
481 WCHAR space = ' ';
483 /* FIXME no no no */
484 if (ME_IsSelection(editor))
485 ME_DeleteSelection(editor);
487 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
488 MERF_GRAPHICS);
489 di->member.run.ole_obj = ALLOC_OBJ(*reo);
490 ME_CopyReObject(di->member.run.ole_obj, reo);
491 ME_ReleaseStyle(pStyle);
495 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
497 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
498 WCHAR space = ' ';
500 /* FIXME no no no */
501 if (ME_IsSelection(editor))
502 ME_DeleteSelection(editor);
504 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
505 MERF_ENDROW);
506 ME_ReleaseStyle(pStyle);
510 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
511 const WCHAR *str, int len, ME_Style *style)
513 const WCHAR *pos;
514 ME_Cursor *p = NULL;
515 int oldLen;
517 /* FIXME really HERE ? */
518 if (ME_IsSelection(editor))
519 ME_DeleteSelection(editor);
521 /* FIXME: is this too slow? */
522 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
523 oldLen = ME_GetTextLength(editor);
525 /* text operations set modified state */
526 editor->nModifyStep = 1;
528 assert(style);
530 assert(nCursor>=0 && nCursor<editor->nCursors);
531 if (len == -1)
532 len = lstrlenW(str);
534 /* grow the text limit to fit our text */
535 if(editor->nTextLimit < oldLen +len)
536 editor->nTextLimit = oldLen + len;
538 pos = str;
540 while (len)
542 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
543 while(pos - str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
544 pos++;
546 if (pos != str) { /* handle text */
547 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
548 } else if (*pos == '\t') { /* handle tabs */
549 WCHAR tab = '\t';
550 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
551 pos++;
552 } else { /* handle EOLs */
553 ME_DisplayItem *tp, *end_run, *run, *prev;
554 ME_Style *tmp_style;
555 int eol_len = 0;
557 /* Find number of CR and LF in end of paragraph run */
558 if (*pos =='\r')
560 if (len > 1 && pos[1] == '\n')
561 eol_len = 2;
562 else if (len > 2 && pos[1] == '\r' && pos[2] == '\n')
563 eol_len = 3;
564 else
565 eol_len = 1;
566 } else {
567 assert(*pos == '\n');
568 eol_len = 1;
570 pos += eol_len;
572 if (!editor->bEmulateVersion10 && eol_len == 3)
574 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
575 WCHAR space = ' ';
576 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
577 } else {
578 const WCHAR cr = '\r', *eol_str = str;
580 if (!editor->bEmulateVersion10)
582 eol_str = &cr;
583 eol_len = 1;
586 p = &editor->pCursors[nCursor];
588 if (p->nOffset == p->pRun->member.run.len)
590 run = ME_FindItemFwd( p->pRun, diRun );
591 if (!run) run = p->pRun;
593 else
595 if (p->nOffset) ME_SplitRunSimple(editor, p);
596 run = p->pRun;
599 tmp_style = ME_GetInsertStyle(editor, nCursor);
600 /* ME_SplitParagraph increases style refcount */
601 tp = ME_SplitParagraph(editor, run, run->member.run.style, eol_str, eol_len, 0);
603 end_run = ME_FindItemBack(tp, diRun);
604 ME_ReleaseStyle(end_run->member.run.style);
605 end_run->member.run.style = tmp_style;
607 /* Move any cursors that were at the end of the previous run to the beginning of the new para */
608 prev = ME_FindItemBack( end_run, diRun );
609 if (prev)
611 int i;
612 for (i = 0; i < editor->nCursors; i++)
614 if (editor->pCursors[i].pRun == prev &&
615 editor->pCursors[i].nOffset == prev->member.run.len)
617 editor->pCursors[i].pPara = tp;
618 editor->pCursors[i].pRun = run;
619 editor->pCursors[i].nOffset = 0;
626 len -= pos - str;
627 str = pos;
631 /* Move the cursor nRelOfs characters (either forwards or backwards)
633 * returns the actual number of characters moved.
635 int ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
637 cursor->nOffset += nRelOfs;
638 if (cursor->nOffset < 0)
640 cursor->nOffset += cursor->pRun->member.run.nCharOfs;
641 if (cursor->nOffset >= 0)
643 /* new offset in the same paragraph */
644 do {
645 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
646 } while (cursor->nOffset < cursor->pRun->member.run.nCharOfs);
647 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
648 return nRelOfs;
651 cursor->nOffset += cursor->pPara->member.para.nCharOfs;
652 if (cursor->nOffset <= 0)
654 /* moved to the start of the text */
655 nRelOfs -= cursor->nOffset;
656 ME_SetCursorToStart(editor, cursor);
657 return nRelOfs;
660 /* new offset in a previous paragraph */
661 do {
662 cursor->pPara = cursor->pPara->member.para.prev_para;
663 } while (cursor->nOffset < cursor->pPara->member.para.nCharOfs);
664 cursor->nOffset -= cursor->pPara->member.para.nCharOfs;
666 cursor->pRun = ME_FindItemBack(cursor->pPara->member.para.next_para, diRun);
667 while (cursor->nOffset < cursor->pRun->member.run.nCharOfs) {
668 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
670 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
671 } else if (cursor->nOffset >= cursor->pRun->member.run.len) {
672 ME_DisplayItem *next_para;
673 int new_offset;
675 new_offset = ME_GetCursorOfs(cursor);
676 next_para = cursor->pPara->member.para.next_para;
677 if (new_offset < next_para->member.para.nCharOfs)
679 /* new offset in the same paragraph */
680 do {
681 cursor->nOffset -= cursor->pRun->member.run.len;
682 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
683 } while (cursor->nOffset >= cursor->pRun->member.run.len);
684 return nRelOfs;
687 if (new_offset >= ME_GetTextLength(editor))
689 /* new offset at the end of the text */
690 ME_SetCursorToEnd(editor, cursor);
691 nRelOfs -= new_offset - ME_GetTextLength(editor);
692 return nRelOfs;
695 /* new offset in a following paragraph */
696 do {
697 cursor->pPara = next_para;
698 next_para = next_para->member.para.next_para;
699 } while (new_offset >= next_para->member.para.nCharOfs);
701 cursor->nOffset = new_offset - cursor->pPara->member.para.nCharOfs;
702 cursor->pRun = ME_FindItemFwd(cursor->pPara, diRun);
703 while (cursor->nOffset >= cursor->pRun->member.run.len)
705 cursor->nOffset -= cursor->pRun->member.run.len;
706 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
708 } /* else new offset is in the same run */
709 return nRelOfs;
713 static BOOL
714 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
716 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
717 ME_DisplayItem *pPara = cursor->pPara;
718 int nOffset = cursor->nOffset;
720 if (nRelOfs == -1)
722 /* Backward movement */
723 while (TRUE)
725 nOffset = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
726 pRun->member.run.len, nOffset, WB_MOVEWORDLEFT);
727 if (nOffset)
728 break;
729 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
730 if (pOtherRun->type == diRun)
732 if (ME_CallWordBreakProc(editor, get_text( &pOtherRun->member.run, 0 ),
733 pOtherRun->member.run.len,
734 pOtherRun->member.run.len - 1,
735 WB_ISDELIMITER)
736 && !(pRun->member.run.nFlags & MERF_ENDPARA)
737 && !(cursor->pRun == pRun && cursor->nOffset == 0)
738 && !ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
739 pRun->member.run.len, 0,
740 WB_ISDELIMITER))
741 break;
742 pRun = pOtherRun;
743 nOffset = pOtherRun->member.run.len;
745 else if (pOtherRun->type == diParagraph)
747 if (cursor->pRun == pRun && cursor->nOffset == 0)
749 pPara = pOtherRun;
750 /* Skip empty start of table row paragraph */
751 if (pPara->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART)
752 pPara = pPara->member.para.prev_para;
753 /* Paragraph breaks are treated as separate words */
754 if (pPara->member.para.prev_para->type == diTextStart)
755 return FALSE;
757 pRun = ME_FindItemBack(pPara, diRun);
758 pPara = pPara->member.para.prev_para;
760 break;
764 else
766 /* Forward movement */
767 BOOL last_delim = FALSE;
769 while (TRUE)
771 if (last_delim && !ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
772 pRun->member.run.len, nOffset, WB_ISDELIMITER))
773 break;
774 nOffset = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
775 pRun->member.run.len, nOffset, WB_MOVEWORDRIGHT);
776 if (nOffset < pRun->member.run.len)
777 break;
778 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
779 if (pOtherRun->type == diRun)
781 last_delim = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
782 pRun->member.run.len, nOffset - 1, WB_ISDELIMITER);
783 pRun = pOtherRun;
784 nOffset = 0;
786 else if (pOtherRun->type == diParagraph)
788 if (pOtherRun->member.para.nFlags & MEPF_ROWSTART)
789 pOtherRun = pOtherRun->member.para.next_para;
790 if (cursor->pRun == pRun) {
791 pPara = pOtherRun;
792 pRun = ME_FindItemFwd(pPara, diRun);
794 nOffset = 0;
795 break;
797 else /* diTextEnd */
799 if (cursor->pRun == pRun)
800 return FALSE;
801 nOffset = 0;
802 break;
806 cursor->pPara = pPara;
807 cursor->pRun = pRun;
808 cursor->nOffset = nOffset;
809 return TRUE;
813 static void
814 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
816 /* pCursor[0] is the end of the selection
817 * pCursor[1] is the start of the selection (or the position selection anchor)
818 * pCursor[2] and [3] are the selection anchors that are backed up
819 * so they are kept when the selection changes for drag selection.
822 editor->nSelectionType = selectionType;
823 switch(selectionType)
825 case stPosition:
826 break;
827 case stWord:
828 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
829 editor->pCursors[1] = editor->pCursors[0];
830 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
831 break;
832 case stLine:
833 case stParagraph:
835 ME_DisplayItem *pItem;
836 ME_DIType fwdSearchType, backSearchType;
837 if (selectionType == stParagraph) {
838 backSearchType = diParagraph;
839 fwdSearchType = diParagraphOrEnd;
840 } else {
841 backSearchType = diStartRow;
842 fwdSearchType = diStartRowOrParagraphOrEnd;
844 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
845 assert(pItem);
846 if (pItem->type == diTextEnd)
847 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
848 else
849 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
850 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
851 editor->pCursors[0].nOffset = 0;
853 pItem = ME_FindItemBack(pItem, backSearchType);
854 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
855 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
856 editor->pCursors[1].nOffset = 0;
857 break;
859 case stDocument:
860 /* Select everything with cursor anchored from the start of the text */
861 editor->nSelectionType = stDocument;
862 ME_SetCursorToStart(editor, &editor->pCursors[1]);
863 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
864 break;
865 default: assert(0);
867 /* Store the anchor positions for extending the selection. */
868 editor->pCursors[2] = editor->pCursors[0];
869 editor->pCursors[3] = editor->pCursors[1];
872 int ME_GetCursorOfs(const ME_Cursor *cursor)
874 return cursor->pPara->member.para.nCharOfs
875 + cursor->pRun->member.run.nCharOfs + cursor->nOffset;
878 /* Helper function for ME_FindPixelPos to find paragraph within tables */
879 static ME_DisplayItem* ME_FindPixelPosInTableRow(int x, int y,
880 ME_DisplayItem *para)
882 ME_DisplayItem *cell, *next_cell;
883 assert(para->member.para.nFlags & MEPF_ROWSTART);
884 cell = para->member.para.next_para->member.para.pCell;
885 assert(cell);
887 /* find the cell we are in */
888 while ((next_cell = cell->member.cell.next_cell) != NULL) {
889 if (x < next_cell->member.cell.pt.x)
891 para = ME_FindItemFwd(cell, diParagraph);
892 /* Found the cell, but there might be multiple paragraphs in
893 * the cell, so need to search down the cell for the paragraph. */
894 while (cell == para->member.para.pCell) {
895 if (y < para->member.para.pt.y + para->member.para.nHeight)
897 if (para->member.para.nFlags & MEPF_ROWSTART)
898 return ME_FindPixelPosInTableRow(x, y, para);
899 else
900 return para;
902 para = para->member.para.next_para;
904 /* Past the end of the cell, so go back to the last cell paragraph */
905 return para->member.para.prev_para;
907 cell = next_cell;
909 /* Return table row delimiter */
910 para = ME_FindItemFwd(cell, diParagraph);
911 assert(para->member.para.nFlags & MEPF_ROWEND);
912 assert(para->member.para.pFmt->dwMask & PFM_TABLEROWDELIMITER);
913 assert(para->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER);
914 return para;
917 static BOOL ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
918 int x, ME_Cursor *cursor, BOOL *pbCaretAtEnd)
920 ME_DisplayItem *pNext, *pLastRun;
921 ME_Row *row = &pRow->member.row;
922 BOOL exact = TRUE;
924 if (x < row->pt.x)
926 x = row->pt.x;
927 exact = FALSE;
929 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
930 assert(pNext->type == diRun);
931 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
932 cursor->nOffset = 0;
933 do {
934 int run_x = pNext->member.run.pt.x;
935 int width = pNext->member.run.nWidth;
937 if (x >= run_x && x < run_x+width)
939 cursor->nOffset = ME_CharFromPoint(editor, x-run_x, &pNext->member.run, TRUE, TRUE);
940 cursor->pRun = pNext;
941 cursor->pPara = ME_GetParagraph( cursor->pRun );
942 return exact;
944 pLastRun = pNext;
945 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
946 } while(pNext && pNext->type == diRun);
948 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
950 cursor->pRun = ME_FindItemFwd(pNext, diRun);
951 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
953 else
954 cursor->pRun = pLastRun;
956 cursor->pPara = ME_GetParagraph( cursor->pRun );
957 return FALSE;
960 /* Finds the run and offset from the pixel position.
962 * x & y are pixel positions in virtual coordinates into the rich edit control,
963 * so client coordinates must first be adjusted by the scroll position.
965 * returns TRUE if the result was exactly under the cursor, otherwise returns
966 * FALSE, and result is set to the closest position to the coordinates.
968 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
969 ME_Cursor *result, BOOL *is_eol)
971 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
972 BOOL isExact = TRUE;
974 x -= editor->rcFormat.left;
975 y -= editor->rcFormat.top;
977 if (is_eol)
978 *is_eol = FALSE;
980 /* find paragraph */
981 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
983 assert(p->type == diParagraph);
984 if (y < p->member.para.pt.y + p->member.para.nHeight)
986 if (p->member.para.nFlags & MEPF_ROWSTART)
987 p = ME_FindPixelPosInTableRow(x, y, p);
988 y -= p->member.para.pt.y;
989 p = ME_FindItemFwd(p, diStartRow);
990 break;
991 } else if (p->member.para.nFlags & MEPF_ROWSTART) {
992 p = ME_GetTableRowEnd(p);
995 /* find row */
996 for (; p != editor->pBuffer->pLast; )
998 ME_DisplayItem *pp;
999 assert(p->type == diStartRow);
1000 if (y < p->member.row.pt.y + p->member.row.nHeight) break;
1001 pp = ME_FindItemFwd(p, diStartRow);
1002 if (!pp) break;
1003 p = pp;
1005 if (p == editor->pBuffer->pLast)
1007 /* The position is below the last paragraph, so the last row will be used
1008 * rather than the end of the text, so the x position will be used to
1009 * determine the offset closest to the pixel position. */
1010 isExact = FALSE;
1011 p = ME_FindItemBack(p, diStartRow);
1012 if (!p) p = editor->pBuffer->pLast;
1015 assert( p->type == diStartRow || p == editor->pBuffer->pLast );
1017 if( p->type == diStartRow )
1018 return ME_FindRunInRow( editor, p, x, result, is_eol ) && isExact;
1020 result->pRun = ME_FindItemBack(p, diRun);
1021 result->pPara = ME_GetParagraph(result->pRun);
1022 result->nOffset = 0;
1023 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
1024 return FALSE;
1028 /* Sets the cursor to the position closest to the pixel position
1030 * x & y are pixel positions in client coordinates.
1032 * isExact will be set to TRUE if the run is directly under the pixel
1033 * position, FALSE if it not, unless isExact is set to NULL.
1035 * return FALSE if outside client area and the cursor is not set,
1036 * otherwise TRUE is returned.
1038 BOOL ME_CharFromPos(ME_TextEditor *editor, int x, int y,
1039 ME_Cursor *cursor, BOOL *isExact)
1041 RECT rc;
1042 BOOL bResult;
1044 ITextHost_TxGetClientRect(editor->texthost, &rc);
1045 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
1046 if (isExact) *isExact = FALSE;
1047 return FALSE;
1049 x += editor->horz_si.nPos;
1050 y += editor->vert_si.nPos;
1051 bResult = ME_FindPixelPos(editor, x, y, cursor, NULL);
1052 if (isExact) *isExact = bResult;
1053 return TRUE;
1058 /* Extends the selection with a word, line, or paragraph selection type.
1060 * The selection is anchored by editor->pCursors[2-3] such that the text
1061 * between the anchors will remain selected, and one end will be extended.
1063 * editor->pCursors[0] should have the position to extend the selection to
1064 * before this function is called.
1066 * Nothing will be done if editor->nSelectionType equals stPosition.
1068 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1070 ME_Cursor tmp_cursor;
1071 int curOfs, anchorStartOfs, anchorEndOfs;
1072 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1073 return;
1074 curOfs = ME_GetCursorOfs(&editor->pCursors[0]);
1075 anchorStartOfs = ME_GetCursorOfs(&editor->pCursors[3]);
1076 anchorEndOfs = ME_GetCursorOfs(&editor->pCursors[2]);
1078 tmp_cursor = editor->pCursors[0];
1079 editor->pCursors[0] = editor->pCursors[2];
1080 editor->pCursors[1] = editor->pCursors[3];
1081 if (curOfs < anchorStartOfs)
1083 /* Extend the left side of selection */
1084 editor->pCursors[1] = tmp_cursor;
1085 if (editor->nSelectionType == stWord)
1086 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1087 else
1089 ME_DisplayItem *pItem;
1090 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1091 diStartRowOrParagraph:diParagraph);
1092 pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
1093 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
1094 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
1095 editor->pCursors[1].nOffset = 0;
1098 else if (curOfs >= anchorEndOfs)
1100 /* Extend the right side of selection */
1101 editor->pCursors[0] = tmp_cursor;
1102 if (editor->nSelectionType == stWord)
1103 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
1104 else
1106 ME_DisplayItem *pItem;
1107 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1108 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
1109 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
1110 if (pItem->type == diTextEnd)
1111 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
1112 else
1113 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
1114 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
1115 editor->pCursors[0].nOffset = 0;
1120 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1122 ME_Cursor tmp_cursor;
1123 BOOL is_selection = FALSE, is_shift;
1125 editor->nUDArrowX = -1;
1127 x += editor->horz_si.nPos;
1128 y += editor->vert_si.nPos;
1130 tmp_cursor = editor->pCursors[0];
1131 is_selection = ME_IsSelection(editor);
1132 is_shift = GetKeyState(VK_SHIFT) < 0;
1134 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
1136 if (x >= editor->rcFormat.left || is_shift)
1138 if (clickNum > 1)
1140 editor->pCursors[1] = editor->pCursors[0];
1141 if (is_shift) {
1142 if (x >= editor->rcFormat.left)
1143 ME_SelectByType(editor, stWord);
1144 else
1145 ME_SelectByType(editor, stParagraph);
1146 } else if (clickNum % 2 == 0) {
1147 ME_SelectByType(editor, stWord);
1148 } else {
1149 ME_SelectByType(editor, stParagraph);
1152 else if (!is_shift)
1154 editor->nSelectionType = stPosition;
1155 editor->pCursors[1] = editor->pCursors[0];
1157 else if (!is_selection)
1159 editor->nSelectionType = stPosition;
1160 editor->pCursors[1] = tmp_cursor;
1162 else if (editor->nSelectionType != stPosition)
1164 ME_ExtendAnchorSelection(editor);
1167 else
1169 if (clickNum < 2) {
1170 ME_SelectByType(editor, stLine);
1171 } else if (clickNum % 2 == 0 || is_shift) {
1172 ME_SelectByType(editor, stParagraph);
1173 } else {
1174 ME_SelectByType(editor, stDocument);
1177 ME_InvalidateSelection(editor);
1178 ITextHost_TxShowCaret(editor->texthost, FALSE);
1179 ME_ShowCaret(editor);
1180 ME_SendSelChange(editor);
1183 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1185 ME_Cursor tmp_cursor;
1187 if (editor->nSelectionType == stDocument)
1188 return;
1189 x += editor->horz_si.nPos;
1190 y += editor->vert_si.nPos;
1192 tmp_cursor = editor->pCursors[0];
1193 /* FIXME: do something with the return value of ME_FindPixelPos */
1194 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
1196 ME_InvalidateSelection(editor);
1197 editor->pCursors[0] = tmp_cursor;
1198 ME_ExtendAnchorSelection(editor);
1200 if (editor->nSelectionType != stPosition &&
1201 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1203 /* The scroll the cursor towards the other end, since it was the one
1204 * extended by ME_ExtendAnchorSelection */
1205 ME_EnsureVisible(editor, &editor->pCursors[1]);
1206 } else {
1207 ME_EnsureVisible(editor, &editor->pCursors[0]);
1210 ME_InvalidateSelection(editor);
1211 ITextHost_TxShowCaret(editor->texthost, FALSE);
1212 ME_ShowCaret(editor);
1213 ME_SendSelChange(editor);
1216 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1218 ME_DisplayItem *pRun = pCursor->pRun;
1219 int x;
1221 if (editor->nUDArrowX != -1)
1222 x = editor->nUDArrowX;
1223 else {
1224 if (editor->bCaretAtEnd)
1226 pRun = ME_FindItemBack(pRun, diRun);
1227 assert(pRun);
1228 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1230 else {
1231 x = pRun->member.run.pt.x;
1232 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset, TRUE);
1234 editor->nUDArrowX = x;
1236 return x;
1240 static void
1241 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1243 ME_DisplayItem *pRun = pCursor->pRun;
1244 ME_DisplayItem *pOldPara = pCursor->pPara;
1245 ME_DisplayItem *pItem, *pNewPara;
1246 int x = ME_GetXForArrow(editor, pCursor);
1248 if (editor->bCaretAtEnd && !pCursor->nOffset)
1249 if (!ME_PrevRun(&pOldPara, &pRun))
1250 return;
1252 if (nRelOfs == -1)
1254 /* start of this row */
1255 pItem = ME_FindItemBack(pRun, diStartRow);
1256 assert(pItem);
1257 /* start of the previous row */
1258 pItem = ME_FindItemBack(pItem, diStartRow);
1259 if (!pItem)
1260 return; /* row not found - ignore */
1261 pNewPara = ME_GetParagraph(pItem);
1262 if (pOldPara->member.para.nFlags & MEPF_ROWEND ||
1263 (pOldPara->member.para.pCell &&
1264 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1266 /* Brought out of a cell */
1267 pNewPara = ME_GetTableRowStart(pOldPara)->member.para.prev_para;
1268 if (pNewPara->type == diTextStart)
1269 return; /* At the top, so don't go anywhere. */
1270 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1272 if (pNewPara->member.para.nFlags & MEPF_ROWEND)
1274 /* Brought into a table row */
1275 ME_Cell *cell = &ME_FindItemBack(pNewPara, diCell)->member.cell;
1276 while (x < cell->pt.x && cell->prev_cell)
1277 cell = &cell->prev_cell->member.cell;
1278 if (cell->next_cell) /* else - we are still at the end of the row */
1279 pItem = ME_FindItemBack(cell->next_cell, diStartRow);
1282 else
1284 /* start of the next row */
1285 pItem = ME_FindItemFwd(pRun, diStartRow);
1286 if (!pItem)
1287 return; /* row not found - ignore */
1288 pNewPara = ME_GetParagraph(pItem);
1289 if (pOldPara->member.para.nFlags & MEPF_ROWSTART ||
1290 (pOldPara->member.para.pCell &&
1291 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1293 /* Brought out of a cell */
1294 pNewPara = ME_GetTableRowEnd(pOldPara)->member.para.next_para;
1295 if (pNewPara->type == diTextEnd)
1296 return; /* At the bottom, so don't go anywhere. */
1297 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1299 if (pNewPara->member.para.nFlags & MEPF_ROWSTART)
1301 /* Brought into a table row */
1302 ME_DisplayItem *cell = ME_FindItemFwd(pNewPara, diCell);
1303 while (cell->member.cell.next_cell &&
1304 x >= cell->member.cell.next_cell->member.cell.pt.x)
1305 cell = cell->member.cell.next_cell;
1306 pItem = ME_FindItemFwd(cell, diStartRow);
1309 if (!pItem)
1311 /* row not found - ignore */
1312 return;
1314 ME_FindRunInRow(editor, pItem, x, pCursor, &editor->bCaretAtEnd);
1315 assert(pCursor->pRun);
1316 assert(pCursor->pRun->type == diRun);
1319 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1321 ME_DisplayItem *p = ME_FindItemFwd(editor->pBuffer->pFirst, diStartRow);
1323 if (editor->vert_si.nPos < p->member.row.nHeight)
1325 ME_SetCursorToStart(editor, pCursor);
1326 editor->bCaretAtEnd = FALSE;
1327 /* Native clears seems to clear this x value on page up at the top
1328 * of the text, but not on page down at the end of the text.
1329 * Doesn't make sense, but we try to be bug for bug compatible. */
1330 editor->nUDArrowX = -1;
1331 } else {
1332 ME_DisplayItem *pRun = pCursor->pRun;
1333 ME_DisplayItem *pLast;
1334 int x, y, yd, yp;
1335 int yOldScrollPos = editor->vert_si.nPos;
1337 x = ME_GetXForArrow(editor, pCursor);
1338 if (!pCursor->nOffset && editor->bCaretAtEnd)
1339 pRun = ME_FindItemBack(pRun, diRun);
1341 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1342 assert(p->type == diStartRow);
1343 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1344 y = yp + p->member.row.pt.y;
1346 ME_ScrollUp(editor, editor->sizeWindow.cy);
1347 /* Only move the cursor by the amount scrolled. */
1348 yd = y + editor->vert_si.nPos - yOldScrollPos;
1349 pLast = p;
1351 do {
1352 p = ME_FindItemBack(p, diStartRowOrParagraph);
1353 if (!p)
1354 break;
1355 if (p->type == diParagraph) { /* crossing paragraphs */
1356 if (p->member.para.prev_para == NULL)
1357 break;
1358 yp = p->member.para.prev_para->member.para.pt.y;
1359 continue;
1361 y = yp + p->member.row.pt.y;
1362 if (y < yd)
1363 break;
1364 pLast = p;
1365 } while(1);
1367 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
1369 assert(pCursor->pRun);
1370 assert(pCursor->pRun->type == diRun);
1373 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1375 ME_DisplayItem *pLast;
1376 int x, y;
1378 /* Find y position of the last row */
1379 pLast = editor->pBuffer->pLast;
1380 y = pLast->member.para.prev_para->member.para.pt.y
1381 + ME_FindItemBack(pLast, diStartRow)->member.row.pt.y;
1383 x = ME_GetXForArrow(editor, pCursor);
1385 if (editor->vert_si.nPos >= y - editor->sizeWindow.cy)
1387 ME_SetCursorToEnd(editor, pCursor);
1388 editor->bCaretAtEnd = FALSE;
1389 } else {
1390 ME_DisplayItem *pRun = pCursor->pRun;
1391 ME_DisplayItem *p;
1392 int yd, yp;
1393 int yOldScrollPos = editor->vert_si.nPos;
1395 if (!pCursor->nOffset && editor->bCaretAtEnd)
1396 pRun = ME_FindItemBack(pRun, diRun);
1398 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1399 assert(p->type == diStartRow);
1400 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1401 y = yp + p->member.row.pt.y;
1403 /* For native richedit controls:
1404 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1405 * v4.1 can scroll past this position here. */
1406 ME_ScrollDown(editor, editor->sizeWindow.cy);
1407 /* Only move the cursor by the amount scrolled. */
1408 yd = y + editor->vert_si.nPos - yOldScrollPos;
1409 pLast = p;
1411 do {
1412 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1413 if (!p)
1414 break;
1415 if (p->type == diParagraph) {
1416 yp = p->member.para.pt.y;
1417 continue;
1419 y = yp + p->member.row.pt.y;
1420 if (y >= yd)
1421 break;
1422 pLast = p;
1423 } while(1);
1425 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
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 ME_SetCursorToStart(editor, pCursor);
1454 editor->bCaretAtEnd = FALSE;
1457 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1459 ME_DisplayItem *pRow;
1461 if (editor->bCaretAtEnd && !pCursor->nOffset)
1462 return;
1464 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1465 assert(pRow);
1466 if (pRow->type == diStartRow) {
1467 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1468 assert(pRun);
1469 pCursor->pRun = pRun;
1470 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1471 pCursor->nOffset = 0;
1472 editor->bCaretAtEnd = TRUE;
1473 return;
1475 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1476 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1477 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1478 pCursor->nOffset = 0;
1479 editor->bCaretAtEnd = FALSE;
1482 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1484 ME_SetCursorToEnd(editor, pCursor);
1485 editor->bCaretAtEnd = FALSE;
1488 BOOL ME_IsSelection(ME_TextEditor *editor)
1490 return editor->pCursors[0].pRun != editor->pCursors[1].pRun ||
1491 editor->pCursors[0].nOffset != editor->pCursors[1].nOffset;
1494 void ME_DeleteSelection(ME_TextEditor *editor)
1496 int from, to;
1497 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
1498 int nEndCursor = nStartCursor ^ 1;
1499 ME_DeleteTextAtCursor(editor, nStartCursor, to - from);
1500 editor->pCursors[nEndCursor] = editor->pCursors[nStartCursor];
1503 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1505 return ME_GetInsertStyle(editor, 0);
1508 void ME_SendSelChange(ME_TextEditor *editor)
1510 SELCHANGE sc;
1512 if (!(editor->nEventMask & ENM_SELCHANGE))
1513 return;
1515 sc.nmhdr.hwndFrom = NULL;
1516 sc.nmhdr.idFrom = 0;
1517 sc.nmhdr.code = EN_SELCHANGE;
1518 ME_GetSelectionOfs(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1519 sc.seltyp = SEL_EMPTY;
1520 if (sc.chrg.cpMin != sc.chrg.cpMax)
1521 sc.seltyp |= SEL_TEXT;
1522 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* what were RICHEDIT authors thinking ? */
1523 sc.seltyp |= SEL_MULTICHAR;
1524 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1525 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1526 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1527 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1528 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1530 ME_ClearTempStyle(editor);
1532 editor->notified_cr = sc.chrg;
1533 ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
1537 BOOL
1538 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1540 int nCursor = 0;
1541 ME_Cursor *p = &editor->pCursors[nCursor];
1542 ME_Cursor tmp_curs = *p;
1543 BOOL success = FALSE;
1545 ME_CheckCharOffsets(editor);
1546 switch(nVKey) {
1547 case VK_LEFT:
1548 editor->bCaretAtEnd = FALSE;
1549 if (ctrl)
1550 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1551 else
1552 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1553 break;
1554 case VK_RIGHT:
1555 editor->bCaretAtEnd = FALSE;
1556 if (ctrl)
1557 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1558 else
1559 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1560 break;
1561 case VK_UP:
1562 ME_MoveCursorLines(editor, &tmp_curs, -1);
1563 break;
1564 case VK_DOWN:
1565 ME_MoveCursorLines(editor, &tmp_curs, +1);
1566 break;
1567 case VK_PRIOR:
1568 ME_ArrowPageUp(editor, &tmp_curs);
1569 break;
1570 case VK_NEXT:
1571 ME_ArrowPageDown(editor, &tmp_curs);
1572 break;
1573 case VK_HOME: {
1574 if (ctrl)
1575 ME_ArrowCtrlHome(editor, &tmp_curs);
1576 else
1577 ME_ArrowHome(editor, &tmp_curs);
1578 editor->bCaretAtEnd = FALSE;
1579 break;
1581 case VK_END:
1582 if (ctrl)
1583 ME_ArrowCtrlEnd(editor, &tmp_curs);
1584 else
1585 ME_ArrowEnd(editor, &tmp_curs);
1586 break;
1589 if (!extend)
1590 editor->pCursors[1] = tmp_curs;
1591 *p = tmp_curs;
1593 ME_InvalidateSelection(editor);
1594 ME_Repaint(editor);
1595 ITextHost_TxShowCaret(editor->texthost, FALSE);
1596 ME_EnsureVisible(editor, &tmp_curs);
1597 ME_ShowCaret(editor);
1598 ME_SendSelChange(editor);
1599 return success;