winemac: Activate an app if it sets focus on a window shortly after a hot key is...
[wine.git] / dlls / riched20 / caret.c
blobb39a8c7e10d08602a4cfab717dc962bf66d1aeb4
1 /*
2 * RichEdit - Caret and selection functions.
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Phil Krylov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "editor.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
27 void ME_SetCursorToStart(ME_TextEditor *editor, ME_Cursor *cursor)
29 cursor->pPara = editor->pBuffer->pFirst->member.para.next_para;
30 cursor->pRun = ME_FindItemFwd(cursor->pPara, diRun);
31 cursor->nOffset = 0;
34 static void ME_SetCursorToEnd(ME_TextEditor *editor, ME_Cursor *cursor)
36 cursor->pPara = editor->pBuffer->pLast->member.para.prev_para;
37 cursor->pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
38 cursor->nOffset = 0;
42 int ME_GetSelectionOfs(ME_TextEditor *editor, int *from, int *to)
44 *from = ME_GetCursorOfs(&editor->pCursors[0]);
45 *to = ME_GetCursorOfs(&editor->pCursors[1]);
47 if (*from > *to)
49 int tmp = *from;
50 *from = *to;
51 *to = tmp;
52 return 1;
54 return 0;
57 int ME_GetSelection(ME_TextEditor *editor, ME_Cursor **from, ME_Cursor **to)
59 if (ME_GetCursorOfs(&editor->pCursors[0]) < ME_GetCursorOfs(&editor->pCursors[1]))
61 *from = &editor->pCursors[0];
62 *to = &editor->pCursors[1];
63 return 0;
64 } else {
65 *from = &editor->pCursors[1];
66 *to = &editor->pCursors[0];
67 return 1;
71 int ME_GetTextLength(ME_TextEditor *editor)
73 ME_Cursor cursor;
74 ME_SetCursorToEnd(editor, &cursor);
75 return ME_GetCursorOfs(&cursor);
79 int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how)
81 int length;
83 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
84 return E_INVALIDARG;
85 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
86 return E_INVALIDARG;
88 length = ME_GetTextLength(editor);
90 if ((editor->styleFlags & ES_MULTILINE)
91 && (how->flags & GTL_USECRLF)
92 && !editor->bEmulateVersion10) /* Ignore GTL_USECRLF flag in 1.0 emulation */
93 length += editor->nParagraphs - 1;
95 if (how->flags & GTL_NUMBYTES ||
96 (how->flags & GTL_PRECISE && /* GTL_PRECISE seems to imply GTL_NUMBYTES */
97 !(how->flags & GTL_NUMCHARS))) /* unless GTL_NUMCHARS is given */
99 CPINFO cpinfo;
101 if (how->codepage == 1200)
102 return length * 2;
103 if (how->flags & GTL_PRECISE)
104 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
105 if (GetCPInfo(how->codepage, &cpinfo))
106 return length * cpinfo.MaxCharSize;
107 ERR("Invalid codepage %u\n", how->codepage);
108 return E_INVALIDARG;
110 return length;
114 int ME_SetSelection(ME_TextEditor *editor, int from, int to)
116 int selectionEnd = 0;
117 const int len = ME_GetTextLength(editor);
119 /* all negative values are effectively the same */
120 if (from < 0)
121 from = -1;
122 if (to < 0)
123 to = -1;
125 /* select all */
126 if (from == 0 && to == -1)
128 ME_SetCursorToStart(editor, &editor->pCursors[1]);
129 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
130 ME_InvalidateSelection(editor);
131 ME_ClearTempStyle(editor);
132 return len + 1;
135 /* if both values are equal and also out of bound, that means to */
136 /* put the selection at the end of the text */
137 if ((from == to) && (to < 0 || to > len))
139 selectionEnd = 1;
141 else
143 /* if from is negative and to is positive then selection is */
144 /* deselected and caret moved to end of the current selection */
145 if (from < 0)
147 int start, end;
148 ME_GetSelectionOfs(editor, &start, &end);
149 if (start != end)
151 editor->pCursors[1] = editor->pCursors[0];
152 ME_Repaint(editor);
154 ME_ClearTempStyle(editor);
155 return end;
158 /* adjust to if it's a negative value */
159 if (to < 0)
160 to = len + 1;
162 /* flip from and to if they are reversed */
163 if (from>to)
165 int tmp = from;
166 from = to;
167 to = tmp;
170 /* after fiddling with the values, we find from > len && to > len */
171 if (from > len)
172 selectionEnd = 1;
173 /* special case with to too big */
174 else if (to > len)
175 to = len + 1;
178 if (selectionEnd)
180 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
181 editor->pCursors[1] = editor->pCursors[0];
182 ME_InvalidateSelection(editor);
183 ME_ClearTempStyle(editor);
184 return len;
187 ME_CursorFromCharOfs(editor, from, &editor->pCursors[1]);
188 editor->pCursors[0] = editor->pCursors[1];
189 ME_MoveCursorChars(editor, &editor->pCursors[0], to - from);
190 /* Selection is not allowed in the middle of an end paragraph run. */
191 if (editor->pCursors[1].pRun->member.run.nFlags & MERF_ENDPARA)
192 editor->pCursors[1].nOffset = 0;
193 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA)
194 editor->pCursors[0].nOffset = 0;
195 return to;
199 static void
200 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
201 int *x, int *y, int *height)
203 ME_DisplayItem *row;
204 ME_DisplayItem *run = pCursor->pRun;
205 ME_DisplayItem *para = pCursor->pPara;
206 ME_DisplayItem *pSizeRun = run;
207 ME_Context c;
208 int run_x;
210 assert(height && x && y);
211 assert(~para->member.para.nFlags & MEPF_REWRAP);
212 assert(run && run->type == diRun);
213 assert(para && para->type == diParagraph);
215 row = ME_FindItemBack(run, diStartRowOrParagraph);
216 assert(row && row->type == diStartRow);
218 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
220 if (!pCursor->nOffset)
222 ME_DisplayItem *prev = ME_FindItemBack(run, diRunOrParagraph);
223 assert(prev);
224 if (prev->type == diRun)
225 pSizeRun = prev;
227 if (editor->bCaretAtEnd && !pCursor->nOffset &&
228 run == ME_FindItemFwd(row, diRun))
230 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
231 assert(tmp);
232 if (tmp->type == diRun)
234 row = ME_FindItemBack(tmp, diStartRow);
235 pSizeRun = run = tmp;
236 assert(run);
237 assert(run->type == diRun);
240 run_x = ME_PointFromCharContext( &c, &run->member.run, pCursor->nOffset, TRUE );
242 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
243 *x = c.rcView.left + run->member.run.pt.x + run_x - editor->horz_si.nPos;
244 *y = c.rcView.top + para->member.para.pt.y + row->member.row.nBaseline
245 + run->member.run.pt.y - pSizeRun->member.run.nAscent
246 - editor->vert_si.nPos;
247 ME_DestroyContext(&c);
248 return;
252 void
253 ME_MoveCaret(ME_TextEditor *editor)
255 int x, y, height;
257 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
258 if(editor->bHaveFocus && !ME_IsSelection(editor))
260 x = min(x, editor->rcFormat.right-1);
261 ITextHost_TxCreateCaret(editor->texthost, NULL, 0, height);
262 ITextHost_TxSetCaretPos(editor->texthost, x, y);
267 void ME_ShowCaret(ME_TextEditor *ed)
269 ME_MoveCaret(ed);
270 if(ed->bHaveFocus && !ME_IsSelection(ed))
271 ITextHost_TxShowCaret(ed->texthost, TRUE);
274 void ME_HideCaret(ME_TextEditor *ed)
276 if(!ed->bHaveFocus || ME_IsSelection(ed))
278 ITextHost_TxShowCaret(ed->texthost, FALSE);
279 DestroyCaret();
283 BOOL ME_InternalDeleteText(ME_TextEditor *editor, ME_Cursor *start,
284 int nChars, BOOL bForce)
286 ME_Cursor c = *start;
287 int nOfs = ME_GetCursorOfs(start);
288 int shift = 0;
289 int totalChars = nChars;
290 ME_DisplayItem *start_para;
292 /* Prevent deletion past last end of paragraph run. */
293 nChars = min(nChars, ME_GetTextLength(editor) - nOfs);
294 start_para = c.pPara;
296 if (!bForce)
298 ME_ProtectPartialTableDeletion(editor, &c, &nChars);
299 if (nChars == 0)
300 return FALSE;
303 while(nChars > 0)
305 ME_Run *run;
306 ME_CursorFromCharOfs(editor, nOfs+nChars, &c);
307 if (!c.nOffset &&
308 nOfs+nChars == (c.pRun->member.run.nCharOfs
309 + c.pPara->member.para.nCharOfs))
311 /* We aren't deleting anything in this run, so we will go back to the
312 * last run we are deleting text in. */
313 ME_PrevRun(&c.pPara, &c.pRun);
314 c.nOffset = c.pRun->member.run.len;
316 run = &c.pRun->member.run;
317 if (run->nFlags & MERF_ENDPARA) {
318 int eollen = c.pRun->member.run.len;
319 BOOL keepFirstParaFormat;
321 if (!ME_FindItemFwd(c.pRun, diParagraph))
323 return TRUE;
325 keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
326 run->nCharOfs);
327 if (!editor->bEmulateVersion10) /* v4.1 */
329 ME_DisplayItem *next_para = ME_FindItemFwd(c.pRun, diParagraphOrEnd);
330 ME_DisplayItem *this_para = next_para->member.para.prev_para;
332 /* The end of paragraph before a table row is only deleted if there
333 * is nothing else on the line before it. */
334 if (this_para == start_para &&
335 next_para->member.para.nFlags & MEPF_ROWSTART)
337 /* If the paragraph will be empty, then it should be deleted, however
338 * it still might have text right now which would inherit the
339 * MEPF_STARTROW property if we joined it right now.
340 * Instead we will delete it after the preceding text is deleted. */
341 if (nOfs > this_para->member.para.nCharOfs) {
342 /* Skip this end of line. */
343 nChars -= (eollen < nChars) ? eollen : nChars;
344 continue;
346 keepFirstParaFormat = TRUE;
349 ME_JoinParagraphs(editor, c.pPara, keepFirstParaFormat);
350 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
351 ME_CheckCharOffsets(editor);
352 nChars -= (eollen < nChars) ? eollen : nChars;
353 continue;
355 else
357 ME_Cursor cursor;
358 int nCharsToDelete = min(nChars, c.nOffset);
359 int i;
361 c.nOffset -= nCharsToDelete;
363 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
365 cursor = c;
366 /* nChars is the number of characters that should be deleted from the
367 PRECEDING runs (these BEFORE cursor.pRun)
368 nCharsToDelete is a number of chars to delete from THIS run */
369 nChars -= nCharsToDelete;
370 shift -= nCharsToDelete;
371 TRACE("Deleting %d (remaning %d) chars at %d in %s (%d)\n",
372 nCharsToDelete, nChars, c.nOffset,
373 debugstr_run( run ), run->len);
375 /* nOfs is a character offset (from the start of the document
376 to the current (deleted) run */
377 add_undo_insert_run( editor, nOfs + nChars, get_text( run, c.nOffset ), nCharsToDelete, run->nFlags, run->style );
379 ME_StrDeleteV(run->para->text, run->nCharOfs + c.nOffset, nCharsToDelete);
380 run->len -= nCharsToDelete;
381 TRACE("Post deletion string: %s (%d)\n", debugstr_run( run ), run->len);
382 TRACE("Shift value: %d\n", shift);
384 /* update cursors (including c) */
385 for (i=-1; i<editor->nCursors; i++) {
386 ME_Cursor *pThisCur = editor->pCursors + i;
387 if (i == -1) pThisCur = &c;
388 if (pThisCur->pRun == cursor.pRun) {
389 if (pThisCur->nOffset > cursor.nOffset) {
390 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
391 pThisCur->nOffset = cursor.nOffset;
392 else
393 pThisCur->nOffset -= nCharsToDelete;
394 assert(pThisCur->nOffset >= 0);
395 assert(pThisCur->nOffset <= run->len);
397 if (pThisCur->nOffset == run->len)
399 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
400 assert(pThisCur->pRun->type == diRun);
401 pThisCur->nOffset = 0;
406 /* c = updated data now */
408 if (c.pRun == cursor.pRun)
409 ME_SkipAndPropagateCharOffset(c.pRun, shift);
410 else
411 ME_PropagateCharOffset(c.pRun, shift);
413 if (!cursor.pRun->member.run.len)
415 TRACE("Removing empty run\n");
416 ME_Remove(cursor.pRun);
417 ME_DestroyDisplayItem(cursor.pRun);
420 shift = 0;
422 ME_CheckCharOffsets(editor);
424 continue;
427 return TRUE;
430 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
432 assert(nCursor>=0 && nCursor<editor->nCursors);
433 /* text operations set modified state */
434 editor->nModifyStep = 1;
435 return ME_InternalDeleteText(editor, &editor->pCursors[nCursor],
436 nChars, FALSE);
439 static ME_DisplayItem *
440 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
441 const WCHAR *str, int len, ME_Style *style,
442 int flags)
444 ME_Cursor *p = &editor->pCursors[nCursor];
446 editor->bCaretAtEnd = FALSE;
448 assert(p->pRun->type == diRun);
450 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
454 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
456 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
457 ME_DisplayItem *di;
458 WCHAR space = ' ';
460 /* FIXME no no no */
461 if (ME_IsSelection(editor))
462 ME_DeleteSelection(editor);
464 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
465 MERF_GRAPHICS);
466 di->member.run.ole_obj = ALLOC_OBJ(*reo);
467 ME_CopyReObject(di->member.run.ole_obj, reo);
468 ME_ReleaseStyle(pStyle);
472 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
474 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
475 WCHAR space = ' ';
477 /* FIXME no no no */
478 if (ME_IsSelection(editor))
479 ME_DeleteSelection(editor);
481 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
482 MERF_ENDROW);
483 ME_ReleaseStyle(pStyle);
487 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
488 const WCHAR *str, int len, ME_Style *style)
490 const WCHAR *pos;
491 ME_Cursor *p = NULL;
492 int oldLen;
494 /* FIXME really HERE ? */
495 if (ME_IsSelection(editor))
496 ME_DeleteSelection(editor);
498 /* FIXME: is this too slow? */
499 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
500 oldLen = ME_GetTextLength(editor);
502 /* text operations set modified state */
503 editor->nModifyStep = 1;
505 assert(style);
507 assert(nCursor>=0 && nCursor<editor->nCursors);
508 if (len == -1)
509 len = lstrlenW(str);
511 /* grow the text limit to fit our text */
512 if(editor->nTextLimit < oldLen +len)
513 editor->nTextLimit = oldLen + len;
515 pos = str;
517 while (len)
519 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
520 while(pos - str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
521 pos++;
523 if (pos != str) { /* handle text */
524 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
525 } else if (*pos == '\t') { /* handle tabs */
526 WCHAR tab = '\t';
527 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
528 pos++;
529 } else { /* handle EOLs */
530 ME_DisplayItem *tp, *end_run;
531 ME_Style *tmp_style;
532 int eol_len = 0;
534 /* Find number of CR and LF in end of paragraph run */
535 if (*pos =='\r')
537 if (len > 1 && pos[1] == '\n')
538 eol_len = 2;
539 else if (len > 2 && pos[1] == '\r' && pos[2] == '\n')
540 eol_len = 3;
541 else
542 eol_len = 1;
543 } else {
544 assert(*pos == '\n');
545 eol_len = 1;
547 pos += eol_len;
549 if (!editor->bEmulateVersion10 && eol_len == 3)
551 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
552 WCHAR space = ' ';
553 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
554 } else {
555 const WCHAR cr = '\r', *eol_str = str;
557 if (!editor->bEmulateVersion10)
559 eol_str = &cr;
560 eol_len = 1;
563 p = &editor->pCursors[nCursor];
564 if (p->nOffset)
565 ME_SplitRunSimple(editor, p);
566 tmp_style = ME_GetInsertStyle(editor, nCursor);
567 /* ME_SplitParagraph increases style refcount */
568 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, eol_str, eol_len, 0);
569 p->pRun = ME_FindItemFwd(tp, diRun);
570 p->pPara = tp;
571 end_run = ME_FindItemBack(tp, diRun);
572 ME_ReleaseStyle(end_run->member.run.style);
573 end_run->member.run.style = tmp_style;
574 p->nOffset = 0;
577 len -= pos - str;
578 str = pos;
582 /* Move the cursor nRelOfs characters (either forwards or backwards)
584 * returns the actual number of characters moved.
586 int ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
588 cursor->nOffset += nRelOfs;
589 if (cursor->nOffset < 0)
591 cursor->nOffset += cursor->pRun->member.run.nCharOfs;
592 if (cursor->nOffset >= 0)
594 /* new offset in the same paragraph */
595 do {
596 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
597 } while (cursor->nOffset < cursor->pRun->member.run.nCharOfs);
598 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
599 return nRelOfs;
602 cursor->nOffset += cursor->pPara->member.para.nCharOfs;
603 if (cursor->nOffset <= 0)
605 /* moved to the start of the text */
606 nRelOfs -= cursor->nOffset;
607 ME_SetCursorToStart(editor, cursor);
608 return nRelOfs;
611 /* new offset in a previous paragraph */
612 do {
613 cursor->pPara = cursor->pPara->member.para.prev_para;
614 } while (cursor->nOffset < cursor->pPara->member.para.nCharOfs);
615 cursor->nOffset -= cursor->pPara->member.para.nCharOfs;
617 cursor->pRun = ME_FindItemBack(cursor->pPara->member.para.next_para, diRun);
618 while (cursor->nOffset < cursor->pRun->member.run.nCharOfs) {
619 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
621 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
622 } else if (cursor->nOffset >= cursor->pRun->member.run.len) {
623 ME_DisplayItem *next_para;
624 int new_offset;
626 new_offset = ME_GetCursorOfs(cursor);
627 next_para = cursor->pPara->member.para.next_para;
628 if (new_offset < next_para->member.para.nCharOfs)
630 /* new offset in the same paragraph */
631 do {
632 cursor->nOffset -= cursor->pRun->member.run.len;
633 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
634 } while (cursor->nOffset >= cursor->pRun->member.run.len);
635 return nRelOfs;
638 if (new_offset >= ME_GetTextLength(editor))
640 /* new offset at the end of the text */
641 ME_SetCursorToEnd(editor, cursor);
642 nRelOfs -= new_offset - ME_GetTextLength(editor);
643 return nRelOfs;
646 /* new offset in a following paragraph */
647 do {
648 cursor->pPara = next_para;
649 next_para = next_para->member.para.next_para;
650 } while (new_offset >= next_para->member.para.nCharOfs);
652 cursor->nOffset = new_offset - cursor->pPara->member.para.nCharOfs;
653 cursor->pRun = ME_FindItemFwd(cursor->pPara, diRun);
654 while (cursor->nOffset >= cursor->pRun->member.run.len)
656 cursor->nOffset -= cursor->pRun->member.run.len;
657 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
659 } /* else new offset is in the same run */
660 return nRelOfs;
664 static BOOL
665 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
667 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
668 ME_DisplayItem *pPara = cursor->pPara;
669 int nOffset = cursor->nOffset;
671 if (nRelOfs == -1)
673 /* Backward movement */
674 while (TRUE)
676 nOffset = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
677 pRun->member.run.len, nOffset, WB_MOVEWORDLEFT);
678 if (nOffset)
679 break;
680 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
681 if (pOtherRun->type == diRun)
683 if (ME_CallWordBreakProc(editor, get_text( &pOtherRun->member.run, 0 ),
684 pOtherRun->member.run.len,
685 pOtherRun->member.run.len - 1,
686 WB_ISDELIMITER)
687 && !(pRun->member.run.nFlags & MERF_ENDPARA)
688 && !(cursor->pRun == pRun && cursor->nOffset == 0)
689 && !ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
690 pRun->member.run.len, 0,
691 WB_ISDELIMITER))
692 break;
693 pRun = pOtherRun;
694 nOffset = pOtherRun->member.run.len;
696 else if (pOtherRun->type == diParagraph)
698 if (cursor->pRun == pRun && cursor->nOffset == 0)
700 pPara = pOtherRun;
701 /* Skip empty start of table row paragraph */
702 if (pPara->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART)
703 pPara = pPara->member.para.prev_para;
704 /* Paragraph breaks are treated as separate words */
705 if (pPara->member.para.prev_para->type == diTextStart)
706 return FALSE;
708 pRun = ME_FindItemBack(pPara, diRun);
709 pPara = pPara->member.para.prev_para;
711 break;
715 else
717 /* Forward movement */
718 BOOL last_delim = FALSE;
720 while (TRUE)
722 if (last_delim && !ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
723 pRun->member.run.len, nOffset, WB_ISDELIMITER))
724 break;
725 nOffset = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
726 pRun->member.run.len, nOffset, WB_MOVEWORDRIGHT);
727 if (nOffset < pRun->member.run.len)
728 break;
729 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
730 if (pOtherRun->type == diRun)
732 last_delim = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
733 pRun->member.run.len, nOffset - 1, WB_ISDELIMITER);
734 pRun = pOtherRun;
735 nOffset = 0;
737 else if (pOtherRun->type == diParagraph)
739 if (pOtherRun->member.para.nFlags & MEPF_ROWSTART)
740 pOtherRun = pOtherRun->member.para.next_para;
741 if (cursor->pRun == pRun) {
742 pPara = pOtherRun;
743 pRun = ME_FindItemFwd(pPara, diRun);
745 nOffset = 0;
746 break;
748 else /* diTextEnd */
750 if (cursor->pRun == pRun)
751 return FALSE;
752 nOffset = 0;
753 break;
757 cursor->pPara = pPara;
758 cursor->pRun = pRun;
759 cursor->nOffset = nOffset;
760 return TRUE;
764 static void
765 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
767 /* pCursor[0] is the end of the selection
768 * pCursor[1] is the start of the selection (or the position selection anchor)
769 * pCursor[2] and [3] are the selection anchors that are backed up
770 * so they are kept when the selection changes for drag selection.
773 editor->nSelectionType = selectionType;
774 switch(selectionType)
776 case stPosition:
777 break;
778 case stWord:
779 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
780 editor->pCursors[1] = editor->pCursors[0];
781 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
782 break;
783 case stLine:
784 case stParagraph:
786 ME_DisplayItem *pItem;
787 ME_DIType fwdSearchType, backSearchType;
788 if (selectionType == stParagraph) {
789 backSearchType = diParagraph;
790 fwdSearchType = diParagraphOrEnd;
791 } else {
792 backSearchType = diStartRow;
793 fwdSearchType = diStartRowOrParagraphOrEnd;
795 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
796 assert(pItem);
797 if (pItem->type == diTextEnd)
798 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
799 else
800 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
801 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
802 editor->pCursors[0].nOffset = 0;
804 pItem = ME_FindItemBack(pItem, backSearchType);
805 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
806 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
807 editor->pCursors[1].nOffset = 0;
808 break;
810 case stDocument:
811 /* Select everything with cursor anchored from the start of the text */
812 editor->nSelectionType = stDocument;
813 ME_SetCursorToStart(editor, &editor->pCursors[1]);
814 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
815 break;
816 default: assert(0);
818 /* Store the anchor positions for extending the selection. */
819 editor->pCursors[2] = editor->pCursors[0];
820 editor->pCursors[3] = editor->pCursors[1];
823 int ME_GetCursorOfs(const ME_Cursor *cursor)
825 return cursor->pPara->member.para.nCharOfs
826 + cursor->pRun->member.run.nCharOfs + cursor->nOffset;
829 /* Helper function for ME_FindPixelPos to find paragraph within tables */
830 static ME_DisplayItem* ME_FindPixelPosInTableRow(int x, int y,
831 ME_DisplayItem *para)
833 ME_DisplayItem *cell, *next_cell;
834 assert(para->member.para.nFlags & MEPF_ROWSTART);
835 cell = para->member.para.next_para->member.para.pCell;
836 assert(cell);
838 /* find the cell we are in */
839 while ((next_cell = cell->member.cell.next_cell) != NULL) {
840 if (x < next_cell->member.cell.pt.x)
842 para = ME_FindItemFwd(cell, diParagraph);
843 /* Found the cell, but there might be multiple paragraphs in
844 * the cell, so need to search down the cell for the paragraph. */
845 while (cell == para->member.para.pCell) {
846 if (y < para->member.para.pt.y + para->member.para.nHeight)
848 if (para->member.para.nFlags & MEPF_ROWSTART)
849 return ME_FindPixelPosInTableRow(x, y, para);
850 else
851 return para;
853 para = para->member.para.next_para;
855 /* Past the end of the cell, so go back to the last cell paragraph */
856 return para->member.para.prev_para;
858 cell = next_cell;
860 /* Return table row delimiter */
861 para = ME_FindItemFwd(cell, diParagraph);
862 assert(para->member.para.nFlags & MEPF_ROWEND);
863 assert(para->member.para.pFmt->dwMask & PFM_TABLEROWDELIMITER);
864 assert(para->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER);
865 return para;
868 static BOOL ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
869 int x, ME_Cursor *cursor, int *pbCaretAtEnd)
871 ME_DisplayItem *pNext, *pLastRun;
872 ME_Row *row = &pRow->member.row;
873 BOOL exact = TRUE;
875 if (x < row->pt.x)
877 x = row->pt.x;
878 exact = FALSE;
880 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
881 assert(pNext->type == diRun);
882 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
883 cursor->nOffset = 0;
884 do {
885 int run_x = pNext->member.run.pt.x;
886 int width = pNext->member.run.nWidth;
888 if (x >= run_x && x < run_x+width)
890 cursor->nOffset = ME_CharFromPoint(editor, x-run_x, &pNext->member.run, TRUE, TRUE);
891 cursor->pRun = pNext;
892 cursor->pPara = ME_GetParagraph( cursor->pRun );
893 return exact;
895 pLastRun = pNext;
896 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
897 } while(pNext && pNext->type == diRun);
899 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
901 cursor->pRun = ME_FindItemFwd(pNext, diRun);
902 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
904 else
905 cursor->pRun = pLastRun;
907 cursor->pPara = ME_GetParagraph( cursor->pRun );
908 return FALSE;
911 /* Finds the run and offset from the pixel position.
913 * x & y are pixel positions in virtual coordinates into the rich edit control,
914 * so client coordinates must first be adjusted by the scroll position.
916 * returns TRUE if the result was exactly under the cursor, otherwise returns
917 * FALSE, and result is set to the closest position to the coordinates.
919 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
920 ME_Cursor *result, BOOL *is_eol)
922 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
923 BOOL isExact = TRUE;
925 x -= editor->rcFormat.left;
926 y -= editor->rcFormat.top;
928 if (is_eol)
929 *is_eol = 0;
931 /* find paragraph */
932 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
934 assert(p->type == diParagraph);
935 if (y < p->member.para.pt.y + p->member.para.nHeight)
937 if (p->member.para.nFlags & MEPF_ROWSTART)
938 p = ME_FindPixelPosInTableRow(x, y, p);
939 y -= p->member.para.pt.y;
940 p = ME_FindItemFwd(p, diStartRow);
941 break;
942 } else if (p->member.para.nFlags & MEPF_ROWSTART) {
943 p = ME_GetTableRowEnd(p);
946 /* find row */
947 for (; p != editor->pBuffer->pLast; )
949 ME_DisplayItem *pp;
950 assert(p->type == diStartRow);
951 if (y < p->member.row.pt.y + p->member.row.nHeight) break;
952 pp = ME_FindItemFwd(p, diStartRow);
953 if (!pp) break;
954 p = pp;
956 if (p == editor->pBuffer->pLast)
958 /* The position is below the last paragraph, so the last row will be used
959 * rather than the end of the text, so the x position will be used to
960 * determine the offset closest to the pixel position. */
961 isExact = FALSE;
962 p = ME_FindItemBack(p, diStartRow);
963 if (!p) p = editor->pBuffer->pLast;
966 assert( p->type == diStartRow || p == editor->pBuffer->pLast );
968 if( p->type == diStartRow )
969 return ME_FindRunInRow( editor, p, x, result, is_eol ) && isExact;
971 result->pRun = ME_FindItemBack(p, diRun);
972 result->pPara = ME_GetParagraph(result->pRun);
973 result->nOffset = 0;
974 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
975 return FALSE;
979 /* Sets the cursor to the position closest to the pixel position
981 * x & y are pixel positions in client coordinates.
983 * isExact will be set to TRUE if the run is directly under the pixel
984 * position, FALSE if it not, unless isExact is set to NULL.
986 * return FALSE if outside client area and the cursor is not set,
987 * otherwise TRUE is returned.
989 BOOL ME_CharFromPos(ME_TextEditor *editor, int x, int y,
990 ME_Cursor *cursor, BOOL *isExact)
992 RECT rc;
993 BOOL bResult;
995 ITextHost_TxGetClientRect(editor->texthost, &rc);
996 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
997 if (isExact) *isExact = FALSE;
998 return FALSE;
1000 x += editor->horz_si.nPos;
1001 y += editor->vert_si.nPos;
1002 bResult = ME_FindPixelPos(editor, x, y, cursor, NULL);
1003 if (isExact) *isExact = bResult;
1004 return TRUE;
1009 /* Extends the selection with a word, line, or paragraph selection type.
1011 * The selection is anchored by editor->pCursors[2-3] such that the text
1012 * between the anchors will remain selected, and one end will be extended.
1014 * editor->pCursors[0] should have the position to extend the selection to
1015 * before this function is called.
1017 * Nothing will be done if editor->nSelectionType equals stPosition.
1019 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1021 ME_Cursor tmp_cursor;
1022 int curOfs, anchorStartOfs, anchorEndOfs;
1023 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1024 return;
1025 curOfs = ME_GetCursorOfs(&editor->pCursors[0]);
1026 anchorStartOfs = ME_GetCursorOfs(&editor->pCursors[3]);
1027 anchorEndOfs = ME_GetCursorOfs(&editor->pCursors[2]);
1029 tmp_cursor = editor->pCursors[0];
1030 editor->pCursors[0] = editor->pCursors[2];
1031 editor->pCursors[1] = editor->pCursors[3];
1032 if (curOfs < anchorStartOfs)
1034 /* Extend the left side of selection */
1035 editor->pCursors[1] = tmp_cursor;
1036 if (editor->nSelectionType == stWord)
1037 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1038 else
1040 ME_DisplayItem *pItem;
1041 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1042 diStartRowOrParagraph:diParagraph);
1043 pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
1044 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
1045 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
1046 editor->pCursors[1].nOffset = 0;
1049 else if (curOfs >= anchorEndOfs)
1051 /* Extend the right side of selection */
1052 editor->pCursors[0] = tmp_cursor;
1053 if (editor->nSelectionType == stWord)
1054 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
1055 else
1057 ME_DisplayItem *pItem;
1058 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1059 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
1060 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
1061 if (pItem->type == diTextEnd)
1062 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
1063 else
1064 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
1065 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
1066 editor->pCursors[0].nOffset = 0;
1071 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1073 ME_Cursor tmp_cursor;
1074 int is_selection = 0;
1075 BOOL is_shift;
1077 editor->nUDArrowX = -1;
1079 x += editor->horz_si.nPos;
1080 y += editor->vert_si.nPos;
1082 tmp_cursor = editor->pCursors[0];
1083 is_selection = ME_IsSelection(editor);
1084 is_shift = GetKeyState(VK_SHIFT) < 0;
1086 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
1088 if (x >= editor->rcFormat.left || is_shift)
1090 if (clickNum > 1)
1092 editor->pCursors[1] = editor->pCursors[0];
1093 if (is_shift) {
1094 if (x >= editor->rcFormat.left)
1095 ME_SelectByType(editor, stWord);
1096 else
1097 ME_SelectByType(editor, stParagraph);
1098 } else if (clickNum % 2 == 0) {
1099 ME_SelectByType(editor, stWord);
1100 } else {
1101 ME_SelectByType(editor, stParagraph);
1104 else if (!is_shift)
1106 editor->nSelectionType = stPosition;
1107 editor->pCursors[1] = editor->pCursors[0];
1109 else if (!is_selection)
1111 editor->nSelectionType = stPosition;
1112 editor->pCursors[1] = tmp_cursor;
1114 else if (editor->nSelectionType != stPosition)
1116 ME_ExtendAnchorSelection(editor);
1119 else
1121 if (clickNum < 2) {
1122 ME_SelectByType(editor, stLine);
1123 } else if (clickNum % 2 == 0 || is_shift) {
1124 ME_SelectByType(editor, stParagraph);
1125 } else {
1126 ME_SelectByType(editor, stDocument);
1129 ME_InvalidateSelection(editor);
1130 ITextHost_TxShowCaret(editor->texthost, FALSE);
1131 ME_ShowCaret(editor);
1132 ME_ClearTempStyle(editor);
1133 ME_SendSelChange(editor);
1136 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1138 ME_Cursor tmp_cursor;
1140 if (editor->nSelectionType == stDocument)
1141 return;
1142 x += editor->horz_si.nPos;
1143 y += editor->vert_si.nPos;
1145 tmp_cursor = editor->pCursors[0];
1146 /* FIXME: do something with the return value of ME_FindPixelPos */
1147 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
1149 ME_InvalidateSelection(editor);
1150 editor->pCursors[0] = tmp_cursor;
1151 ME_ExtendAnchorSelection(editor);
1153 if (editor->nSelectionType != stPosition &&
1154 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1156 /* The scroll the cursor towards the other end, since it was the one
1157 * extended by ME_ExtendAnchorSelection */
1158 ME_EnsureVisible(editor, &editor->pCursors[1]);
1159 } else {
1160 ME_EnsureVisible(editor, &editor->pCursors[0]);
1163 ME_InvalidateSelection(editor);
1164 ITextHost_TxShowCaret(editor->texthost, FALSE);
1165 ME_ShowCaret(editor);
1166 ME_SendSelChange(editor);
1169 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1171 ME_DisplayItem *pRun = pCursor->pRun;
1172 int x;
1174 if (editor->nUDArrowX != -1)
1175 x = editor->nUDArrowX;
1176 else {
1177 if (editor->bCaretAtEnd)
1179 pRun = ME_FindItemBack(pRun, diRun);
1180 assert(pRun);
1181 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1183 else {
1184 x = pRun->member.run.pt.x;
1185 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset, TRUE);
1187 editor->nUDArrowX = x;
1189 return x;
1193 static void
1194 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1196 ME_DisplayItem *pRun = pCursor->pRun;
1197 ME_DisplayItem *pOldPara = pCursor->pPara;
1198 ME_DisplayItem *pItem, *pNewPara;
1199 int x = ME_GetXForArrow(editor, pCursor);
1201 if (editor->bCaretAtEnd && !pCursor->nOffset)
1202 if (!ME_PrevRun(&pOldPara, &pRun))
1203 return;
1205 if (nRelOfs == -1)
1207 /* start of this row */
1208 pItem = ME_FindItemBack(pRun, diStartRow);
1209 assert(pItem);
1210 /* start of the previous row */
1211 pItem = ME_FindItemBack(pItem, diStartRow);
1212 if (!pItem)
1213 return; /* row not found - ignore */
1214 pNewPara = ME_GetParagraph(pItem);
1215 if (pOldPara->member.para.nFlags & MEPF_ROWEND ||
1216 (pOldPara->member.para.pCell &&
1217 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1219 /* Brought out of a cell */
1220 pNewPara = ME_GetTableRowStart(pOldPara)->member.para.prev_para;
1221 if (pNewPara->type == diTextStart)
1222 return; /* At the top, so don't go anywhere. */
1223 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1225 if (pNewPara->member.para.nFlags & MEPF_ROWEND)
1227 /* Brought into a table row */
1228 ME_Cell *cell = &ME_FindItemBack(pNewPara, diCell)->member.cell;
1229 while (x < cell->pt.x && cell->prev_cell)
1230 cell = &cell->prev_cell->member.cell;
1231 if (cell->next_cell) /* else - we are still at the end of the row */
1232 pItem = ME_FindItemBack(cell->next_cell, diStartRow);
1235 else
1237 /* start of the next row */
1238 pItem = ME_FindItemFwd(pRun, diStartRow);
1239 if (!pItem)
1240 return; /* row not found - ignore */
1241 pNewPara = ME_GetParagraph(pItem);
1242 if (pOldPara->member.para.nFlags & MEPF_ROWSTART ||
1243 (pOldPara->member.para.pCell &&
1244 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1246 /* Brought out of a cell */
1247 pNewPara = ME_GetTableRowEnd(pOldPara)->member.para.next_para;
1248 if (pNewPara->type == diTextEnd)
1249 return; /* At the bottom, so don't go anywhere. */
1250 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1252 if (pNewPara->member.para.nFlags & MEPF_ROWSTART)
1254 /* Brought into a table row */
1255 ME_DisplayItem *cell = ME_FindItemFwd(pNewPara, diCell);
1256 while (cell->member.cell.next_cell &&
1257 x >= cell->member.cell.next_cell->member.cell.pt.x)
1258 cell = cell->member.cell.next_cell;
1259 pItem = ME_FindItemFwd(cell, diStartRow);
1262 if (!pItem)
1264 /* row not found - ignore */
1265 return;
1267 ME_FindRunInRow(editor, pItem, x, pCursor, &editor->bCaretAtEnd);
1268 assert(pCursor->pRun);
1269 assert(pCursor->pRun->type == diRun);
1272 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1274 ME_DisplayItem *p = ME_FindItemFwd(editor->pBuffer->pFirst, diStartRow);
1276 if (editor->vert_si.nPos < p->member.row.nHeight)
1278 ME_SetCursorToStart(editor, pCursor);
1279 editor->bCaretAtEnd = FALSE;
1280 /* Native clears seems to clear this x value on page up at the top
1281 * of the text, but not on page down at the end of the text.
1282 * Doesn't make sense, but we try to be bug for bug compatible. */
1283 editor->nUDArrowX = -1;
1284 } else {
1285 ME_DisplayItem *pRun = pCursor->pRun;
1286 ME_DisplayItem *pLast;
1287 int x, y, yd, yp;
1288 int yOldScrollPos = editor->vert_si.nPos;
1290 x = ME_GetXForArrow(editor, pCursor);
1291 if (!pCursor->nOffset && editor->bCaretAtEnd)
1292 pRun = ME_FindItemBack(pRun, diRun);
1294 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1295 assert(p->type == diStartRow);
1296 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1297 y = yp + p->member.row.pt.y;
1299 ME_ScrollUp(editor, editor->sizeWindow.cy);
1300 /* Only move the cursor by the amount scrolled. */
1301 yd = y + editor->vert_si.nPos - yOldScrollPos;
1302 pLast = p;
1304 do {
1305 p = ME_FindItemBack(p, diStartRowOrParagraph);
1306 if (!p)
1307 break;
1308 if (p->type == diParagraph) { /* crossing paragraphs */
1309 if (p->member.para.prev_para == NULL)
1310 break;
1311 yp = p->member.para.prev_para->member.para.pt.y;
1312 continue;
1314 y = yp + p->member.row.pt.y;
1315 if (y < yd)
1316 break;
1317 pLast = p;
1318 } while(1);
1320 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
1322 assert(pCursor->pRun);
1323 assert(pCursor->pRun->type == diRun);
1326 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1328 ME_DisplayItem *pLast;
1329 int x, y;
1331 /* Find y position of the last row */
1332 pLast = editor->pBuffer->pLast;
1333 y = pLast->member.para.prev_para->member.para.pt.y
1334 + ME_FindItemBack(pLast, diStartRow)->member.row.pt.y;
1336 x = ME_GetXForArrow(editor, pCursor);
1338 if (editor->vert_si.nPos >= y - editor->sizeWindow.cy)
1340 ME_SetCursorToEnd(editor, pCursor);
1341 editor->bCaretAtEnd = FALSE;
1342 } else {
1343 ME_DisplayItem *pRun = pCursor->pRun;
1344 ME_DisplayItem *p;
1345 int yd, yp;
1346 int yOldScrollPos = editor->vert_si.nPos;
1348 if (!pCursor->nOffset && editor->bCaretAtEnd)
1349 pRun = ME_FindItemBack(pRun, diRun);
1351 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1352 assert(p->type == diStartRow);
1353 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1354 y = yp + p->member.row.pt.y;
1356 /* For native richedit controls:
1357 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1358 * v4.1 can scroll past this position here. */
1359 ME_ScrollDown(editor, editor->sizeWindow.cy);
1360 /* Only move the cursor by the amount scrolled. */
1361 yd = y + editor->vert_si.nPos - yOldScrollPos;
1362 pLast = p;
1364 do {
1365 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1366 if (!p)
1367 break;
1368 if (p->type == diParagraph) {
1369 yp = p->member.para.pt.y;
1370 continue;
1372 y = yp + p->member.row.pt.y;
1373 if (y >= yd)
1374 break;
1375 pLast = p;
1376 } while(1);
1378 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
1380 assert(pCursor->pRun);
1381 assert(pCursor->pRun->type == diRun);
1384 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1386 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1387 if (pRow) {
1388 ME_DisplayItem *pRun;
1389 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1390 pRow = ME_FindItemBack(pRow, diStartRow);
1391 if (!pRow)
1392 return;
1394 pRun = ME_FindItemFwd(pRow, diRun);
1395 if (pRun) {
1396 pCursor->pRun = pRun;
1397 assert(pCursor->pPara == ME_GetParagraph(pRun));
1398 pCursor->nOffset = 0;
1401 editor->bCaretAtEnd = FALSE;
1404 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1406 ME_SetCursorToStart(editor, pCursor);
1407 editor->bCaretAtEnd = FALSE;
1410 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1412 ME_DisplayItem *pRow;
1414 if (editor->bCaretAtEnd && !pCursor->nOffset)
1415 return;
1417 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1418 assert(pRow);
1419 if (pRow->type == diStartRow) {
1420 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1421 assert(pRun);
1422 pCursor->pRun = pRun;
1423 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1424 pCursor->nOffset = 0;
1425 editor->bCaretAtEnd = TRUE;
1426 return;
1428 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1429 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1430 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1431 pCursor->nOffset = 0;
1432 editor->bCaretAtEnd = FALSE;
1435 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1437 ME_SetCursorToEnd(editor, pCursor);
1438 editor->bCaretAtEnd = FALSE;
1441 BOOL ME_IsSelection(ME_TextEditor *editor)
1443 return editor->pCursors[0].pRun != editor->pCursors[1].pRun ||
1444 editor->pCursors[0].nOffset != editor->pCursors[1].nOffset;
1447 void ME_DeleteSelection(ME_TextEditor *editor)
1449 int from, to;
1450 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
1451 ME_DeleteTextAtCursor(editor, nStartCursor, to - from);
1454 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1456 return ME_GetInsertStyle(editor, 0);
1459 void ME_SendSelChange(ME_TextEditor *editor)
1461 SELCHANGE sc;
1463 if (!(editor->nEventMask & ENM_SELCHANGE))
1464 return;
1466 sc.nmhdr.hwndFrom = NULL;
1467 sc.nmhdr.idFrom = 0;
1468 sc.nmhdr.code = EN_SELCHANGE;
1469 ME_GetSelectionOfs(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1470 sc.seltyp = SEL_EMPTY;
1471 if (sc.chrg.cpMin != sc.chrg.cpMax)
1472 sc.seltyp |= SEL_TEXT;
1473 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* what were RICHEDIT authors thinking ? */
1474 sc.seltyp |= SEL_MULTICHAR;
1475 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1476 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1477 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1478 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1479 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1481 ME_ClearTempStyle(editor);
1483 editor->notified_cr = sc.chrg;
1484 ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
1488 BOOL
1489 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1491 int nCursor = 0;
1492 ME_Cursor *p = &editor->pCursors[nCursor];
1493 ME_Cursor tmp_curs = *p;
1494 BOOL success = FALSE;
1496 ME_CheckCharOffsets(editor);
1497 switch(nVKey) {
1498 case VK_LEFT:
1499 editor->bCaretAtEnd = 0;
1500 if (ctrl)
1501 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1502 else
1503 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1504 break;
1505 case VK_RIGHT:
1506 editor->bCaretAtEnd = 0;
1507 if (ctrl)
1508 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1509 else
1510 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1511 break;
1512 case VK_UP:
1513 ME_MoveCursorLines(editor, &tmp_curs, -1);
1514 break;
1515 case VK_DOWN:
1516 ME_MoveCursorLines(editor, &tmp_curs, +1);
1517 break;
1518 case VK_PRIOR:
1519 ME_ArrowPageUp(editor, &tmp_curs);
1520 break;
1521 case VK_NEXT:
1522 ME_ArrowPageDown(editor, &tmp_curs);
1523 break;
1524 case VK_HOME: {
1525 if (ctrl)
1526 ME_ArrowCtrlHome(editor, &tmp_curs);
1527 else
1528 ME_ArrowHome(editor, &tmp_curs);
1529 editor->bCaretAtEnd = 0;
1530 break;
1532 case VK_END:
1533 if (ctrl)
1534 ME_ArrowCtrlEnd(editor, &tmp_curs);
1535 else
1536 ME_ArrowEnd(editor, &tmp_curs);
1537 break;
1540 if (!extend)
1541 editor->pCursors[1] = tmp_curs;
1542 *p = tmp_curs;
1544 ME_InvalidateSelection(editor);
1545 ME_Repaint(editor);
1546 ITextHost_TxShowCaret(editor->texthost, FALSE);
1547 ME_EnsureVisible(editor, &tmp_curs);
1548 ME_ShowCaret(editor);
1549 ME_SendSelChange(editor);
1550 return success;