wined3d: No longer make sub-resources full resources.
[wine.git] / dlls / riched20 / caret.c
blob4b0bfcf4ce478a15c0c80358b471ccf0f00e1275
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 void ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
220 int *x, int *y, int *height)
222 ME_DisplayItem *row;
223 ME_DisplayItem *run = pCursor->pRun;
224 ME_DisplayItem *para = pCursor->pPara;
225 ME_DisplayItem *pSizeRun = run;
226 ME_Context c;
227 int run_x;
229 assert(height && x && y);
230 assert(~para->member.para.nFlags & MEPF_REWRAP);
231 assert(run && run->type == diRun);
232 assert(para && para->type == diParagraph);
234 row = ME_FindItemBack(run, diStartRowOrParagraph);
235 assert(row && row->type == diStartRow);
237 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
239 if (!pCursor->nOffset)
241 ME_DisplayItem *prev = ME_FindItemBack(run, diRunOrParagraph);
242 assert(prev);
243 if (prev->type == diRun)
244 pSizeRun = prev;
246 if (editor->bCaretAtEnd && !pCursor->nOffset &&
247 run == ME_FindItemFwd(row, diRun))
249 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
250 assert(tmp);
251 if (tmp->type == diRun)
253 row = ME_FindItemBack(tmp, diStartRow);
254 pSizeRun = run = tmp;
255 assert(run);
256 assert(run->type == diRun);
259 run_x = ME_PointFromCharContext( &c, &run->member.run, pCursor->nOffset, TRUE );
261 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
262 *x = c.rcView.left + run->member.run.pt.x + run_x - editor->horz_si.nPos;
263 *y = c.rcView.top + para->member.para.pt.y + row->member.row.nBaseline
264 + run->member.run.pt.y - pSizeRun->member.run.nAscent
265 - editor->vert_si.nPos;
266 ME_DestroyContext(&c);
267 return;
271 void
272 ME_MoveCaret(ME_TextEditor *editor)
274 int x, y, height;
276 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
277 if(editor->bHaveFocus && !ME_IsSelection(editor))
279 x = min(x, editor->rcFormat.right-1);
280 ITextHost_TxCreateCaret(editor->texthost, NULL, 0, height);
281 ITextHost_TxSetCaretPos(editor->texthost, x, y);
286 void ME_ShowCaret(ME_TextEditor *ed)
288 ME_MoveCaret(ed);
289 if(ed->bHaveFocus && !ME_IsSelection(ed))
290 ITextHost_TxShowCaret(ed->texthost, TRUE);
293 void ME_HideCaret(ME_TextEditor *ed)
295 if(!ed->bHaveFocus || ME_IsSelection(ed))
297 ITextHost_TxShowCaret(ed->texthost, FALSE);
298 DestroyCaret();
302 BOOL ME_InternalDeleteText(ME_TextEditor *editor, ME_Cursor *start,
303 int nChars, BOOL bForce)
305 ME_Cursor c = *start;
306 int nOfs = ME_GetCursorOfs(start), text_len = ME_GetTextLength( editor );
307 int shift = 0;
308 int totalChars = nChars;
309 ME_DisplayItem *start_para;
310 BOOL delete_all = FALSE;
312 /* Prevent deletion past last end of paragraph run. */
313 nChars = min(nChars, text_len - nOfs);
314 if (nChars == text_len) delete_all = TRUE;
315 start_para = c.pPara;
317 if (!bForce)
319 ME_ProtectPartialTableDeletion(editor, &c, &nChars);
320 if (nChars == 0)
321 return FALSE;
324 while(nChars > 0)
326 ME_Run *run;
327 ME_CursorFromCharOfs(editor, nOfs+nChars, &c);
328 if (!c.nOffset &&
329 nOfs+nChars == (c.pRun->member.run.nCharOfs
330 + c.pPara->member.para.nCharOfs))
332 /* We aren't deleting anything in this run, so we will go back to the
333 * last run we are deleting text in. */
334 ME_PrevRun(&c.pPara, &c.pRun);
335 c.nOffset = c.pRun->member.run.len;
337 run = &c.pRun->member.run;
338 if (run->nFlags & MERF_ENDPARA) {
339 int eollen = c.pRun->member.run.len;
340 BOOL keepFirstParaFormat;
342 if (!ME_FindItemFwd(c.pRun, diParagraph))
344 return TRUE;
346 keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
347 run->nCharOfs);
348 if (!editor->bEmulateVersion10) /* v4.1 */
350 ME_DisplayItem *next_para = ME_FindItemFwd(c.pRun, diParagraphOrEnd);
351 ME_DisplayItem *this_para = next_para->member.para.prev_para;
353 /* The end of paragraph before a table row is only deleted if there
354 * is nothing else on the line before it. */
355 if (this_para == start_para &&
356 next_para->member.para.nFlags & MEPF_ROWSTART)
358 /* If the paragraph will be empty, then it should be deleted, however
359 * it still might have text right now which would inherit the
360 * MEPF_STARTROW property if we joined it right now.
361 * Instead we will delete it after the preceding text is deleted. */
362 if (nOfs > this_para->member.para.nCharOfs) {
363 /* Skip this end of line. */
364 nChars -= (eollen < nChars) ? eollen : nChars;
365 continue;
367 keepFirstParaFormat = TRUE;
370 ME_JoinParagraphs(editor, c.pPara, keepFirstParaFormat);
371 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
372 ME_CheckCharOffsets(editor);
373 nChars -= (eollen < nChars) ? eollen : nChars;
374 continue;
376 else
378 ME_Cursor cursor;
379 int nCharsToDelete = min(nChars, c.nOffset);
380 int i;
382 c.nOffset -= nCharsToDelete;
384 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
386 cursor = c;
387 /* nChars is the number of characters that should be deleted from the
388 PRECEDING runs (these BEFORE cursor.pRun)
389 nCharsToDelete is a number of chars to delete from THIS run */
390 nChars -= nCharsToDelete;
391 shift -= nCharsToDelete;
392 TRACE("Deleting %d (remaning %d) chars at %d in %s (%d)\n",
393 nCharsToDelete, nChars, c.nOffset,
394 debugstr_run( run ), run->len);
396 /* nOfs is a character offset (from the start of the document
397 to the current (deleted) run */
398 add_undo_insert_run( editor, nOfs + nChars, get_text( run, c.nOffset ), nCharsToDelete, run->nFlags, run->style );
400 ME_StrDeleteV(run->para->text, run->nCharOfs + c.nOffset, nCharsToDelete);
401 run->len -= nCharsToDelete;
402 TRACE("Post deletion string: %s (%d)\n", debugstr_run( run ), run->len);
403 TRACE("Shift value: %d\n", shift);
405 /* update cursors (including c) */
406 for (i=-1; i<editor->nCursors; i++) {
407 ME_Cursor *pThisCur = editor->pCursors + i;
408 if (i == -1) pThisCur = &c;
409 if (pThisCur->pRun == cursor.pRun) {
410 if (pThisCur->nOffset > cursor.nOffset) {
411 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
412 pThisCur->nOffset = cursor.nOffset;
413 else
414 pThisCur->nOffset -= nCharsToDelete;
415 assert(pThisCur->nOffset >= 0);
416 assert(pThisCur->nOffset <= run->len);
418 if (pThisCur->nOffset == run->len)
420 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
421 assert(pThisCur->pRun->type == diRun);
422 pThisCur->nOffset = 0;
427 /* c = updated data now */
429 if (c.pRun == cursor.pRun)
430 ME_SkipAndPropagateCharOffset(c.pRun, shift);
431 else
432 ME_PropagateCharOffset(c.pRun, shift);
434 if (!cursor.pRun->member.run.len)
436 TRACE("Removing empty run\n");
437 ME_Remove(cursor.pRun);
438 ME_DestroyDisplayItem(cursor.pRun);
441 shift = 0;
443 ME_CheckCharOffsets(editor);
445 continue;
448 if (delete_all) ME_SetDefaultParaFormat( editor, start_para->member.para.pFmt );
449 return TRUE;
452 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
454 assert(nCursor>=0 && nCursor<editor->nCursors);
455 /* text operations set modified state */
456 editor->nModifyStep = 1;
457 return ME_InternalDeleteText(editor, &editor->pCursors[nCursor],
458 nChars, FALSE);
461 static ME_DisplayItem *
462 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
463 const WCHAR *str, int len, ME_Style *style,
464 int flags)
466 ME_Cursor *p = &editor->pCursors[nCursor];
468 editor->bCaretAtEnd = FALSE;
470 assert(p->pRun->type == diRun);
472 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
476 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
478 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
479 ME_DisplayItem *di;
480 WCHAR space = ' ';
482 /* FIXME no no no */
483 if (ME_IsSelection(editor))
484 ME_DeleteSelection(editor);
486 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
487 MERF_GRAPHICS);
488 di->member.run.ole_obj = ALLOC_OBJ(*reo);
489 ME_CopyReObject(di->member.run.ole_obj, reo);
490 ME_ReleaseStyle(pStyle);
494 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
496 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
497 WCHAR space = ' ';
499 /* FIXME no no no */
500 if (ME_IsSelection(editor))
501 ME_DeleteSelection(editor);
503 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
504 MERF_ENDROW);
505 ME_ReleaseStyle(pStyle);
509 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
510 const WCHAR *str, int len, ME_Style *style)
512 const WCHAR *pos;
513 ME_Cursor *p = NULL;
514 int oldLen;
516 /* FIXME really HERE ? */
517 if (ME_IsSelection(editor))
518 ME_DeleteSelection(editor);
520 /* FIXME: is this too slow? */
521 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
522 oldLen = ME_GetTextLength(editor);
524 /* text operations set modified state */
525 editor->nModifyStep = 1;
527 assert(style);
529 assert(nCursor>=0 && nCursor<editor->nCursors);
530 if (len == -1)
531 len = lstrlenW(str);
533 /* grow the text limit to fit our text */
534 if(editor->nTextLimit < oldLen +len)
535 editor->nTextLimit = oldLen + len;
537 pos = str;
539 while (len)
541 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
542 while(pos - str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
543 pos++;
545 if (pos != str) { /* handle text */
546 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
547 } else if (*pos == '\t') { /* handle tabs */
548 WCHAR tab = '\t';
549 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
550 pos++;
551 } else { /* handle EOLs */
552 ME_DisplayItem *tp, *end_run, *run, *prev;
553 ME_Style *tmp_style;
554 int eol_len = 0;
556 /* Find number of CR and LF in end of paragraph run */
557 if (*pos =='\r')
559 if (len > 1 && pos[1] == '\n')
560 eol_len = 2;
561 else if (len > 2 && pos[1] == '\r' && pos[2] == '\n')
562 eol_len = 3;
563 else
564 eol_len = 1;
565 } else {
566 assert(*pos == '\n');
567 eol_len = 1;
569 pos += eol_len;
571 if (!editor->bEmulateVersion10 && eol_len == 3)
573 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
574 WCHAR space = ' ';
575 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
576 } else {
577 const WCHAR cr = '\r', *eol_str = str;
579 if (!editor->bEmulateVersion10)
581 eol_str = &cr;
582 eol_len = 1;
585 p = &editor->pCursors[nCursor];
587 if (p->nOffset == p->pRun->member.run.len)
589 run = ME_FindItemFwd( p->pRun, diRun );
590 if (!run) run = p->pRun;
592 else
594 if (p->nOffset) ME_SplitRunSimple(editor, p);
595 run = p->pRun;
598 tmp_style = ME_GetInsertStyle(editor, nCursor);
599 /* ME_SplitParagraph increases style refcount */
600 tp = ME_SplitParagraph(editor, run, run->member.run.style, eol_str, eol_len, 0);
602 end_run = ME_FindItemBack(tp, diRun);
603 ME_ReleaseStyle(end_run->member.run.style);
604 end_run->member.run.style = tmp_style;
606 /* Move any cursors that were at the end of the previous run to the beginning of the new para */
607 prev = ME_FindItemBack( end_run, diRun );
608 if (prev)
610 int i;
611 for (i = 0; i < editor->nCursors; i++)
613 if (editor->pCursors[i].pRun == prev &&
614 editor->pCursors[i].nOffset == prev->member.run.len)
616 editor->pCursors[i].pPara = tp;
617 editor->pCursors[i].pRun = run;
618 editor->pCursors[i].nOffset = 0;
625 len -= pos - str;
626 str = pos;
630 /* Move the cursor nRelOfs characters (either forwards or backwards)
632 * returns the actual number of characters moved.
634 int ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
636 cursor->nOffset += nRelOfs;
637 if (cursor->nOffset < 0)
639 cursor->nOffset += cursor->pRun->member.run.nCharOfs;
640 if (cursor->nOffset >= 0)
642 /* new offset in the same paragraph */
643 do {
644 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
645 } while (cursor->nOffset < cursor->pRun->member.run.nCharOfs);
646 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
647 return nRelOfs;
650 cursor->nOffset += cursor->pPara->member.para.nCharOfs;
651 if (cursor->nOffset <= 0)
653 /* moved to the start of the text */
654 nRelOfs -= cursor->nOffset;
655 ME_SetCursorToStart(editor, cursor);
656 return nRelOfs;
659 /* new offset in a previous paragraph */
660 do {
661 cursor->pPara = cursor->pPara->member.para.prev_para;
662 } while (cursor->nOffset < cursor->pPara->member.para.nCharOfs);
663 cursor->nOffset -= cursor->pPara->member.para.nCharOfs;
665 cursor->pRun = ME_FindItemBack(cursor->pPara->member.para.next_para, diRun);
666 while (cursor->nOffset < cursor->pRun->member.run.nCharOfs) {
667 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
669 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
670 } else if (cursor->nOffset >= cursor->pRun->member.run.len) {
671 ME_DisplayItem *next_para;
672 int new_offset;
674 new_offset = ME_GetCursorOfs(cursor);
675 next_para = cursor->pPara->member.para.next_para;
676 if (new_offset < next_para->member.para.nCharOfs)
678 /* new offset in the same paragraph */
679 do {
680 cursor->nOffset -= cursor->pRun->member.run.len;
681 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
682 } while (cursor->nOffset >= cursor->pRun->member.run.len);
683 return nRelOfs;
686 if (new_offset >= ME_GetTextLength(editor))
688 /* new offset at the end of the text */
689 ME_SetCursorToEnd(editor, cursor);
690 nRelOfs -= new_offset - ME_GetTextLength(editor);
691 return nRelOfs;
694 /* new offset in a following paragraph */
695 do {
696 cursor->pPara = next_para;
697 next_para = next_para->member.para.next_para;
698 } while (new_offset >= next_para->member.para.nCharOfs);
700 cursor->nOffset = new_offset - cursor->pPara->member.para.nCharOfs;
701 cursor->pRun = ME_FindItemFwd(cursor->pPara, diRun);
702 while (cursor->nOffset >= cursor->pRun->member.run.len)
704 cursor->nOffset -= cursor->pRun->member.run.len;
705 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
707 } /* else new offset is in the same run */
708 return nRelOfs;
712 BOOL
713 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
715 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
716 ME_DisplayItem *pPara = cursor->pPara;
717 int nOffset = cursor->nOffset;
719 if (nRelOfs == -1)
721 /* Backward movement */
722 while (TRUE)
724 nOffset = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
725 pRun->member.run.len, nOffset, WB_MOVEWORDLEFT);
726 if (nOffset)
727 break;
728 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
729 if (pOtherRun->type == diRun)
731 if (ME_CallWordBreakProc(editor, get_text( &pOtherRun->member.run, 0 ),
732 pOtherRun->member.run.len,
733 pOtherRun->member.run.len - 1,
734 WB_ISDELIMITER)
735 && !(pRun->member.run.nFlags & MERF_ENDPARA)
736 && !(cursor->pRun == pRun && cursor->nOffset == 0)
737 && !ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
738 pRun->member.run.len, 0,
739 WB_ISDELIMITER))
740 break;
741 pRun = pOtherRun;
742 nOffset = pOtherRun->member.run.len;
744 else if (pOtherRun->type == diParagraph)
746 if (cursor->pRun == pRun && cursor->nOffset == 0)
748 pPara = pOtherRun;
749 /* Skip empty start of table row paragraph */
750 if (pPara->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART)
751 pPara = pPara->member.para.prev_para;
752 /* Paragraph breaks are treated as separate words */
753 if (pPara->member.para.prev_para->type == diTextStart)
754 return FALSE;
756 pRun = ME_FindItemBack(pPara, diRun);
757 pPara = pPara->member.para.prev_para;
759 break;
763 else
765 /* Forward movement */
766 BOOL last_delim = FALSE;
768 while (TRUE)
770 if (last_delim && !ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
771 pRun->member.run.len, nOffset, WB_ISDELIMITER))
772 break;
773 nOffset = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
774 pRun->member.run.len, nOffset, WB_MOVEWORDRIGHT);
775 if (nOffset < pRun->member.run.len)
776 break;
777 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
778 if (pOtherRun->type == diRun)
780 last_delim = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
781 pRun->member.run.len, nOffset - 1, WB_ISDELIMITER);
782 pRun = pOtherRun;
783 nOffset = 0;
785 else if (pOtherRun->type == diParagraph)
787 if (pOtherRun->member.para.nFlags & MEPF_ROWSTART)
788 pOtherRun = pOtherRun->member.para.next_para;
789 if (cursor->pRun == pRun) {
790 pPara = pOtherRun;
791 pRun = ME_FindItemFwd(pPara, diRun);
793 nOffset = 0;
794 break;
796 else /* diTextEnd */
798 if (cursor->pRun == pRun)
799 return FALSE;
800 nOffset = 0;
801 break;
805 cursor->pPara = pPara;
806 cursor->pRun = pRun;
807 cursor->nOffset = nOffset;
808 return TRUE;
812 static void
813 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
815 /* pCursor[0] is the end of the selection
816 * pCursor[1] is the start of the selection (or the position selection anchor)
817 * pCursor[2] and [3] are the selection anchors that are backed up
818 * so they are kept when the selection changes for drag selection.
821 editor->nSelectionType = selectionType;
822 switch(selectionType)
824 case stPosition:
825 break;
826 case stWord:
827 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
828 editor->pCursors[1] = editor->pCursors[0];
829 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
830 break;
831 case stLine:
832 case stParagraph:
834 ME_DisplayItem *pItem;
835 ME_DIType fwdSearchType, backSearchType;
836 if (selectionType == stParagraph) {
837 backSearchType = diParagraph;
838 fwdSearchType = diParagraphOrEnd;
839 } else {
840 backSearchType = diStartRow;
841 fwdSearchType = diStartRowOrParagraphOrEnd;
843 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
844 assert(pItem);
845 if (pItem->type == diTextEnd)
846 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
847 else
848 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
849 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
850 editor->pCursors[0].nOffset = 0;
852 pItem = ME_FindItemBack(pItem, backSearchType);
853 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
854 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
855 editor->pCursors[1].nOffset = 0;
856 break;
858 case stDocument:
859 /* Select everything with cursor anchored from the start of the text */
860 editor->nSelectionType = stDocument;
861 ME_SetCursorToStart(editor, &editor->pCursors[1]);
862 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
863 break;
864 default: assert(0);
866 /* Store the anchor positions for extending the selection. */
867 editor->pCursors[2] = editor->pCursors[0];
868 editor->pCursors[3] = editor->pCursors[1];
871 int ME_GetCursorOfs(const ME_Cursor *cursor)
873 return cursor->pPara->member.para.nCharOfs
874 + cursor->pRun->member.run.nCharOfs + cursor->nOffset;
877 /* Helper function for ME_FindPixelPos to find paragraph within tables */
878 static ME_DisplayItem* ME_FindPixelPosInTableRow(int x, int y,
879 ME_DisplayItem *para)
881 ME_DisplayItem *cell, *next_cell;
882 assert(para->member.para.nFlags & MEPF_ROWSTART);
883 cell = para->member.para.next_para->member.para.pCell;
884 assert(cell);
886 /* find the cell we are in */
887 while ((next_cell = cell->member.cell.next_cell) != NULL) {
888 if (x < next_cell->member.cell.pt.x)
890 para = ME_FindItemFwd(cell, diParagraph);
891 /* Found the cell, but there might be multiple paragraphs in
892 * the cell, so need to search down the cell for the paragraph. */
893 while (cell == para->member.para.pCell) {
894 if (y < para->member.para.pt.y + para->member.para.nHeight)
896 if (para->member.para.nFlags & MEPF_ROWSTART)
897 return ME_FindPixelPosInTableRow(x, y, para);
898 else
899 return para;
901 para = para->member.para.next_para;
903 /* Past the end of the cell, so go back to the last cell paragraph */
904 return para->member.para.prev_para;
906 cell = next_cell;
908 /* Return table row delimiter */
909 para = ME_FindItemFwd(cell, diParagraph);
910 assert(para->member.para.nFlags & MEPF_ROWEND);
911 assert(para->member.para.pFmt->dwMask & PFM_TABLEROWDELIMITER);
912 assert(para->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER);
913 return para;
916 static BOOL ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
917 int x, ME_Cursor *cursor, BOOL *pbCaretAtEnd)
919 ME_DisplayItem *pNext, *pLastRun;
920 ME_Row *row = &pRow->member.row;
921 BOOL exact = TRUE;
923 if (x < row->pt.x)
925 x = row->pt.x;
926 exact = FALSE;
928 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
929 assert(pNext->type == diRun);
930 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
931 cursor->nOffset = 0;
932 do {
933 int run_x = pNext->member.run.pt.x;
934 int width = pNext->member.run.nWidth;
936 if (x >= run_x && x < run_x+width)
938 cursor->nOffset = ME_CharFromPoint(editor, x-run_x, &pNext->member.run, TRUE, TRUE);
939 cursor->pRun = pNext;
940 cursor->pPara = ME_GetParagraph( cursor->pRun );
941 return exact;
943 pLastRun = pNext;
944 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
945 } while(pNext && pNext->type == diRun);
947 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
949 cursor->pRun = ME_FindItemFwd(pNext, diRun);
950 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
952 else
953 cursor->pRun = pLastRun;
955 cursor->pPara = ME_GetParagraph( cursor->pRun );
956 return FALSE;
959 /* Finds the run and offset from the pixel position.
961 * x & y are pixel positions in virtual coordinates into the rich edit control,
962 * so client coordinates must first be adjusted by the scroll position.
964 * returns TRUE if the result was exactly under the cursor, otherwise returns
965 * FALSE, and result is set to the closest position to the coordinates.
967 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
968 ME_Cursor *result, BOOL *is_eol)
970 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
971 BOOL isExact = TRUE;
973 x -= editor->rcFormat.left;
974 y -= editor->rcFormat.top;
976 if (is_eol)
977 *is_eol = FALSE;
979 /* find paragraph */
980 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
982 assert(p->type == diParagraph);
983 if (y < p->member.para.pt.y + p->member.para.nHeight)
985 if (p->member.para.nFlags & MEPF_ROWSTART)
986 p = ME_FindPixelPosInTableRow(x, y, p);
987 y -= p->member.para.pt.y;
988 p = ME_FindItemFwd(p, diStartRow);
989 break;
990 } else if (p->member.para.nFlags & MEPF_ROWSTART) {
991 p = ME_GetTableRowEnd(p);
994 /* find row */
995 for (; p != editor->pBuffer->pLast; )
997 ME_DisplayItem *pp;
998 assert(p->type == diStartRow);
999 if (y < p->member.row.pt.y + p->member.row.nHeight) break;
1000 pp = ME_FindItemFwd(p, diStartRow);
1001 if (!pp) break;
1002 p = pp;
1004 if (p == editor->pBuffer->pLast)
1006 /* The position is below the last paragraph, so the last row will be used
1007 * rather than the end of the text, so the x position will be used to
1008 * determine the offset closest to the pixel position. */
1009 isExact = FALSE;
1010 p = ME_FindItemBack(p, diStartRow);
1011 if (!p) p = editor->pBuffer->pLast;
1014 assert( p->type == diStartRow || p == editor->pBuffer->pLast );
1016 if( p->type == diStartRow )
1017 return ME_FindRunInRow( editor, p, x, result, is_eol ) && isExact;
1019 result->pRun = ME_FindItemBack(p, diRun);
1020 result->pPara = ME_GetParagraph(result->pRun);
1021 result->nOffset = 0;
1022 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
1023 return FALSE;
1027 /* Sets the cursor to the position closest to the pixel position
1029 * x & y are pixel positions in client coordinates.
1031 * isExact will be set to TRUE if the run is directly under the pixel
1032 * position, FALSE if it not, unless isExact is set to NULL.
1034 * return FALSE if outside client area and the cursor is not set,
1035 * otherwise TRUE is returned.
1037 BOOL ME_CharFromPos(ME_TextEditor *editor, int x, int y,
1038 ME_Cursor *cursor, BOOL *isExact)
1040 RECT rc;
1041 BOOL bResult;
1043 ITextHost_TxGetClientRect(editor->texthost, &rc);
1044 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
1045 if (isExact) *isExact = FALSE;
1046 return FALSE;
1048 x += editor->horz_si.nPos;
1049 y += editor->vert_si.nPos;
1050 bResult = ME_FindPixelPos(editor, x, y, cursor, NULL);
1051 if (isExact) *isExact = bResult;
1052 return TRUE;
1057 /* Extends the selection with a word, line, or paragraph selection type.
1059 * The selection is anchored by editor->pCursors[2-3] such that the text
1060 * between the anchors will remain selected, and one end will be extended.
1062 * editor->pCursors[0] should have the position to extend the selection to
1063 * before this function is called.
1065 * Nothing will be done if editor->nSelectionType equals stPosition.
1067 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1069 ME_Cursor tmp_cursor;
1070 int curOfs, anchorStartOfs, anchorEndOfs;
1071 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1072 return;
1073 curOfs = ME_GetCursorOfs(&editor->pCursors[0]);
1074 anchorStartOfs = ME_GetCursorOfs(&editor->pCursors[3]);
1075 anchorEndOfs = ME_GetCursorOfs(&editor->pCursors[2]);
1077 tmp_cursor = editor->pCursors[0];
1078 editor->pCursors[0] = editor->pCursors[2];
1079 editor->pCursors[1] = editor->pCursors[3];
1080 if (curOfs < anchorStartOfs)
1082 /* Extend the left side of selection */
1083 editor->pCursors[1] = tmp_cursor;
1084 if (editor->nSelectionType == stWord)
1085 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1086 else
1088 ME_DisplayItem *pItem;
1089 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1090 diStartRowOrParagraph:diParagraph);
1091 pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
1092 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
1093 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
1094 editor->pCursors[1].nOffset = 0;
1097 else if (curOfs >= anchorEndOfs)
1099 /* Extend the right side of selection */
1100 editor->pCursors[0] = tmp_cursor;
1101 if (editor->nSelectionType == stWord)
1102 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
1103 else
1105 ME_DisplayItem *pItem;
1106 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1107 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
1108 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
1109 if (pItem->type == diTextEnd)
1110 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
1111 else
1112 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
1113 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
1114 editor->pCursors[0].nOffset = 0;
1119 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1121 ME_Cursor tmp_cursor;
1122 BOOL is_selection = FALSE, is_shift;
1124 editor->nUDArrowX = -1;
1126 x += editor->horz_si.nPos;
1127 y += editor->vert_si.nPos;
1129 tmp_cursor = editor->pCursors[0];
1130 is_selection = ME_IsSelection(editor);
1131 is_shift = GetKeyState(VK_SHIFT) < 0;
1133 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
1135 if (x >= editor->rcFormat.left || is_shift)
1137 if (clickNum > 1)
1139 editor->pCursors[1] = editor->pCursors[0];
1140 if (is_shift) {
1141 if (x >= editor->rcFormat.left)
1142 ME_SelectByType(editor, stWord);
1143 else
1144 ME_SelectByType(editor, stParagraph);
1145 } else if (clickNum % 2 == 0) {
1146 ME_SelectByType(editor, stWord);
1147 } else {
1148 ME_SelectByType(editor, stParagraph);
1151 else if (!is_shift)
1153 editor->nSelectionType = stPosition;
1154 editor->pCursors[1] = editor->pCursors[0];
1156 else if (!is_selection)
1158 editor->nSelectionType = stPosition;
1159 editor->pCursors[1] = tmp_cursor;
1161 else if (editor->nSelectionType != stPosition)
1163 ME_ExtendAnchorSelection(editor);
1166 else
1168 if (clickNum < 2) {
1169 ME_SelectByType(editor, stLine);
1170 } else if (clickNum % 2 == 0 || is_shift) {
1171 ME_SelectByType(editor, stParagraph);
1172 } else {
1173 ME_SelectByType(editor, stDocument);
1176 ME_InvalidateSelection(editor);
1177 ITextHost_TxShowCaret(editor->texthost, FALSE);
1178 ME_ShowCaret(editor);
1179 ME_SendSelChange(editor);
1182 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1184 ME_Cursor tmp_cursor;
1186 if (editor->nSelectionType == stDocument)
1187 return;
1188 x += editor->horz_si.nPos;
1189 y += editor->vert_si.nPos;
1191 tmp_cursor = editor->pCursors[0];
1192 /* FIXME: do something with the return value of ME_FindPixelPos */
1193 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
1195 ME_InvalidateSelection(editor);
1196 editor->pCursors[0] = tmp_cursor;
1197 ME_ExtendAnchorSelection(editor);
1199 if (editor->nSelectionType != stPosition &&
1200 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1202 /* The scroll the cursor towards the other end, since it was the one
1203 * extended by ME_ExtendAnchorSelection */
1204 ME_EnsureVisible(editor, &editor->pCursors[1]);
1205 } else {
1206 ME_EnsureVisible(editor, &editor->pCursors[0]);
1209 ME_InvalidateSelection(editor);
1210 ITextHost_TxShowCaret(editor->texthost, FALSE);
1211 ME_ShowCaret(editor);
1212 ME_SendSelChange(editor);
1215 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1217 ME_DisplayItem *pRun = pCursor->pRun;
1218 int x;
1220 if (editor->nUDArrowX != -1)
1221 x = editor->nUDArrowX;
1222 else {
1223 if (editor->bCaretAtEnd)
1225 pRun = ME_FindItemBack(pRun, diRun);
1226 assert(pRun);
1227 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1229 else {
1230 x = pRun->member.run.pt.x;
1231 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset, TRUE);
1233 editor->nUDArrowX = x;
1235 return x;
1239 static void
1240 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1242 ME_DisplayItem *pRun = pCursor->pRun;
1243 ME_DisplayItem *pOldPara = pCursor->pPara;
1244 ME_DisplayItem *pItem, *pNewPara;
1245 int x = ME_GetXForArrow(editor, pCursor);
1247 if (editor->bCaretAtEnd && !pCursor->nOffset)
1248 if (!ME_PrevRun(&pOldPara, &pRun))
1249 return;
1251 if (nRelOfs == -1)
1253 /* start of this row */
1254 pItem = ME_FindItemBack(pRun, diStartRow);
1255 assert(pItem);
1256 /* start of the previous row */
1257 pItem = ME_FindItemBack(pItem, diStartRow);
1258 if (!pItem)
1259 return; /* row not found - ignore */
1260 pNewPara = ME_GetParagraph(pItem);
1261 if (pOldPara->member.para.nFlags & MEPF_ROWEND ||
1262 (pOldPara->member.para.pCell &&
1263 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1265 /* Brought out of a cell */
1266 pNewPara = ME_GetTableRowStart(pOldPara)->member.para.prev_para;
1267 if (pNewPara->type == diTextStart)
1268 return; /* At the top, so don't go anywhere. */
1269 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1271 if (pNewPara->member.para.nFlags & MEPF_ROWEND)
1273 /* Brought into a table row */
1274 ME_Cell *cell = &ME_FindItemBack(pNewPara, diCell)->member.cell;
1275 while (x < cell->pt.x && cell->prev_cell)
1276 cell = &cell->prev_cell->member.cell;
1277 if (cell->next_cell) /* else - we are still at the end of the row */
1278 pItem = ME_FindItemBack(cell->next_cell, diStartRow);
1281 else
1283 /* start of the next row */
1284 pItem = ME_FindItemFwd(pRun, diStartRow);
1285 if (!pItem)
1286 return; /* row not found - ignore */
1287 pNewPara = ME_GetParagraph(pItem);
1288 if (pOldPara->member.para.nFlags & MEPF_ROWSTART ||
1289 (pOldPara->member.para.pCell &&
1290 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1292 /* Brought out of a cell */
1293 pNewPara = ME_GetTableRowEnd(pOldPara)->member.para.next_para;
1294 if (pNewPara->type == diTextEnd)
1295 return; /* At the bottom, so don't go anywhere. */
1296 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1298 if (pNewPara->member.para.nFlags & MEPF_ROWSTART)
1300 /* Brought into a table row */
1301 ME_DisplayItem *cell = ME_FindItemFwd(pNewPara, diCell);
1302 while (cell->member.cell.next_cell &&
1303 x >= cell->member.cell.next_cell->member.cell.pt.x)
1304 cell = cell->member.cell.next_cell;
1305 pItem = ME_FindItemFwd(cell, diStartRow);
1308 if (!pItem)
1310 /* row not found - ignore */
1311 return;
1313 ME_FindRunInRow(editor, pItem, x, pCursor, &editor->bCaretAtEnd);
1314 assert(pCursor->pRun);
1315 assert(pCursor->pRun->type == diRun);
1318 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1320 ME_DisplayItem *p = ME_FindItemFwd(editor->pBuffer->pFirst, diStartRow);
1322 if (editor->vert_si.nPos < p->member.row.nHeight)
1324 ME_SetCursorToStart(editor, pCursor);
1325 editor->bCaretAtEnd = FALSE;
1326 /* Native clears seems to clear this x value on page up at the top
1327 * of the text, but not on page down at the end of the text.
1328 * Doesn't make sense, but we try to be bug for bug compatible. */
1329 editor->nUDArrowX = -1;
1330 } else {
1331 ME_DisplayItem *pRun = pCursor->pRun;
1332 ME_DisplayItem *pLast;
1333 int x, y, yd, yp;
1334 int yOldScrollPos = editor->vert_si.nPos;
1336 x = ME_GetXForArrow(editor, pCursor);
1337 if (!pCursor->nOffset && editor->bCaretAtEnd)
1338 pRun = ME_FindItemBack(pRun, diRun);
1340 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1341 assert(p->type == diStartRow);
1342 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1343 y = yp + p->member.row.pt.y;
1345 ME_ScrollUp(editor, editor->sizeWindow.cy);
1346 /* Only move the cursor by the amount scrolled. */
1347 yd = y + editor->vert_si.nPos - yOldScrollPos;
1348 pLast = p;
1350 do {
1351 p = ME_FindItemBack(p, diStartRowOrParagraph);
1352 if (!p)
1353 break;
1354 if (p->type == diParagraph) { /* crossing paragraphs */
1355 if (p->member.para.prev_para == NULL)
1356 break;
1357 yp = p->member.para.prev_para->member.para.pt.y;
1358 continue;
1360 y = yp + p->member.row.pt.y;
1361 if (y < yd)
1362 break;
1363 pLast = p;
1364 } while(1);
1366 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
1368 assert(pCursor->pRun);
1369 assert(pCursor->pRun->type == diRun);
1372 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1374 ME_DisplayItem *pLast;
1375 int x, y;
1377 /* Find y position of the last row */
1378 pLast = editor->pBuffer->pLast;
1379 y = pLast->member.para.prev_para->member.para.pt.y
1380 + ME_FindItemBack(pLast, diStartRow)->member.row.pt.y;
1382 x = ME_GetXForArrow(editor, pCursor);
1384 if (editor->vert_si.nPos >= y - editor->sizeWindow.cy)
1386 ME_SetCursorToEnd(editor, pCursor);
1387 editor->bCaretAtEnd = FALSE;
1388 } else {
1389 ME_DisplayItem *pRun = pCursor->pRun;
1390 ME_DisplayItem *p;
1391 int yd, yp;
1392 int yOldScrollPos = editor->vert_si.nPos;
1394 if (!pCursor->nOffset && editor->bCaretAtEnd)
1395 pRun = ME_FindItemBack(pRun, diRun);
1397 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1398 assert(p->type == diStartRow);
1399 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1400 y = yp + p->member.row.pt.y;
1402 /* For native richedit controls:
1403 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1404 * v4.1 can scroll past this position here. */
1405 ME_ScrollDown(editor, editor->sizeWindow.cy);
1406 /* Only move the cursor by the amount scrolled. */
1407 yd = y + editor->vert_si.nPos - yOldScrollPos;
1408 pLast = p;
1410 do {
1411 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1412 if (!p)
1413 break;
1414 if (p->type == diParagraph) {
1415 yp = p->member.para.pt.y;
1416 continue;
1418 y = yp + p->member.row.pt.y;
1419 if (y >= yd)
1420 break;
1421 pLast = p;
1422 } while(1);
1424 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
1426 assert(pCursor->pRun);
1427 assert(pCursor->pRun->type == diRun);
1430 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1432 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1433 if (pRow) {
1434 ME_DisplayItem *pRun;
1435 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1436 pRow = ME_FindItemBack(pRow, diStartRow);
1437 if (!pRow)
1438 return;
1440 pRun = ME_FindItemFwd(pRow, diRun);
1441 if (pRun) {
1442 pCursor->pRun = pRun;
1443 assert(pCursor->pPara == ME_GetParagraph(pRun));
1444 pCursor->nOffset = 0;
1447 editor->bCaretAtEnd = FALSE;
1450 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1452 ME_SetCursorToStart(editor, pCursor);
1453 editor->bCaretAtEnd = FALSE;
1456 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1458 ME_DisplayItem *pRow;
1460 if (editor->bCaretAtEnd && !pCursor->nOffset)
1461 return;
1463 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1464 assert(pRow);
1465 if (pRow->type == diStartRow) {
1466 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1467 assert(pRun);
1468 pCursor->pRun = pRun;
1469 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1470 pCursor->nOffset = 0;
1471 editor->bCaretAtEnd = TRUE;
1472 return;
1474 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1475 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1476 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1477 pCursor->nOffset = 0;
1478 editor->bCaretAtEnd = FALSE;
1481 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1483 ME_SetCursorToEnd(editor, pCursor);
1484 editor->bCaretAtEnd = FALSE;
1487 BOOL ME_IsSelection(ME_TextEditor *editor)
1489 return editor->pCursors[0].pRun != editor->pCursors[1].pRun ||
1490 editor->pCursors[0].nOffset != editor->pCursors[1].nOffset;
1493 void ME_DeleteSelection(ME_TextEditor *editor)
1495 int from, to;
1496 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
1497 int nEndCursor = nStartCursor ^ 1;
1498 ME_DeleteTextAtCursor(editor, nStartCursor, to - from);
1499 editor->pCursors[nEndCursor] = editor->pCursors[nStartCursor];
1502 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1504 return ME_GetInsertStyle(editor, 0);
1507 void ME_SendSelChange(ME_TextEditor *editor)
1509 SELCHANGE sc;
1511 if (!(editor->nEventMask & ENM_SELCHANGE))
1512 return;
1514 sc.nmhdr.hwndFrom = NULL;
1515 sc.nmhdr.idFrom = 0;
1516 sc.nmhdr.code = EN_SELCHANGE;
1517 ME_GetSelectionOfs(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1518 sc.seltyp = SEL_EMPTY;
1519 if (sc.chrg.cpMin != sc.chrg.cpMax)
1520 sc.seltyp |= SEL_TEXT;
1521 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* what were RICHEDIT authors thinking ? */
1522 sc.seltyp |= SEL_MULTICHAR;
1523 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1524 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1525 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1526 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1527 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1529 ME_ClearTempStyle(editor);
1531 editor->notified_cr = sc.chrg;
1532 ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
1536 BOOL
1537 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1539 int nCursor = 0;
1540 ME_Cursor *p = &editor->pCursors[nCursor];
1541 ME_Cursor tmp_curs = *p;
1542 BOOL success = FALSE;
1544 ME_CheckCharOffsets(editor);
1545 switch(nVKey) {
1546 case VK_LEFT:
1547 editor->bCaretAtEnd = FALSE;
1548 if (ctrl)
1549 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1550 else
1551 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1552 break;
1553 case VK_RIGHT:
1554 editor->bCaretAtEnd = FALSE;
1555 if (ctrl)
1556 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1557 else
1558 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1559 break;
1560 case VK_UP:
1561 ME_MoveCursorLines(editor, &tmp_curs, -1);
1562 break;
1563 case VK_DOWN:
1564 ME_MoveCursorLines(editor, &tmp_curs, +1);
1565 break;
1566 case VK_PRIOR:
1567 ME_ArrowPageUp(editor, &tmp_curs);
1568 break;
1569 case VK_NEXT:
1570 ME_ArrowPageDown(editor, &tmp_curs);
1571 break;
1572 case VK_HOME: {
1573 if (ctrl)
1574 ME_ArrowCtrlHome(editor, &tmp_curs);
1575 else
1576 ME_ArrowHome(editor, &tmp_curs);
1577 editor->bCaretAtEnd = FALSE;
1578 break;
1580 case VK_END:
1581 if (ctrl)
1582 ME_ArrowCtrlEnd(editor, &tmp_curs);
1583 else
1584 ME_ArrowEnd(editor, &tmp_curs);
1585 break;
1588 if (!extend)
1589 editor->pCursors[1] = tmp_curs;
1590 *p = tmp_curs;
1592 ME_InvalidateSelection(editor);
1593 ME_Repaint(editor);
1594 ITextHost_TxShowCaret(editor->texthost, FALSE);
1595 ME_EnsureVisible(editor, &tmp_curs);
1596 ME_ShowCaret(editor);
1597 ME_SendSelChange(editor);
1598 return success;