riched20: Return a BOOL that indicates whether the hit is exact.
[wine.git] / dlls / riched20 / caret.c
blob5556c8e24d8529d64cd98e9f406ae466c5a2e4f2
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 );
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_ReturnFoundPos(ME_TextEditor *editor, ME_DisplayItem *found,
869 ME_Cursor *result, int rx, BOOL isExact)
871 assert(found);
872 assert(found->type == diRun);
873 if ((found->member.run.nFlags & MERF_ENDPARA) || rx < 0)
874 rx = 0;
875 result->pRun = found;
876 result->nOffset = ME_CharFromPoint(editor, rx, &found->member.run, TRUE);
877 if (result->nOffset == found->member.run.len && rx)
879 result->pRun = ME_FindItemFwd(result->pRun, diRun);
880 result->nOffset = 0;
882 result->pPara = ME_GetParagraph(result->pRun);
883 return isExact;
886 /* Finds the run and offset from the pixel position.
888 * x & y are pixel positions in virtual coordinates into the rich edit control,
889 * so client coordinates must first be adjusted by the scroll position.
891 * returns TRUE if the result was exactly under the cursor, otherwise returns
892 * FALSE, and result is set to the closest position to the coordinates.
894 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
895 ME_Cursor *result, BOOL *is_eol)
897 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
898 ME_DisplayItem *last = NULL;
899 int rx = 0;
900 BOOL isExact = TRUE;
902 x -= editor->rcFormat.left;
903 y -= editor->rcFormat.top;
905 if (is_eol)
906 *is_eol = 0;
908 /* find paragraph */
909 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
911 assert(p->type == diParagraph);
912 if (y < p->member.para.pt.y + p->member.para.nHeight)
914 if (p->member.para.nFlags & MEPF_ROWSTART)
915 p = ME_FindPixelPosInTableRow(x, y, p);
916 y -= p->member.para.pt.y;
917 p = ME_FindItemFwd(p, diStartRow);
918 break;
919 } else if (p->member.para.nFlags & MEPF_ROWSTART) {
920 p = ME_GetTableRowEnd(p);
923 /* find row */
924 for (; p != editor->pBuffer->pLast; )
926 ME_DisplayItem *pp;
927 assert(p->type == diStartRow);
928 if (y < p->member.row.pt.y + p->member.row.nHeight)
930 p = ME_FindItemFwd(p, diRun);
931 break;
933 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
934 if (pp->type != diStartRow)
936 p = ME_FindItemFwd(p, diRun);
937 break;
939 p = pp;
941 if (p == editor->pBuffer->pLast)
943 /* The position is below the last paragraph, so the last row will be used
944 * rather than the end of the text, so the x position will be used to
945 * determine the offset closest to the pixel position. */
946 isExact = FALSE;
947 p = ME_FindItemBack(p, diStartRow);
948 if (p != NULL){
949 p = ME_FindItemFwd(p, diRun);
951 else
953 p = editor->pBuffer->pLast;
956 for (; p != editor->pBuffer->pLast; p = p->next)
958 switch (p->type)
960 case diRun:
961 rx = x - p->member.run.pt.x;
962 if (rx < p->member.run.nWidth)
963 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
964 break;
965 case diStartRow:
966 isExact = FALSE;
967 p = ME_FindItemFwd(p, diRun);
968 if (is_eol) *is_eol = 1;
969 rx = 0; /* FIXME not sure */
970 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
971 case diCell:
972 case diParagraph:
973 case diTextEnd:
974 isExact = FALSE;
975 rx = 0; /* FIXME not sure */
976 p = last;
977 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
978 default: assert(0);
980 last = p;
982 result->pRun = ME_FindItemBack(p, diRun);
983 result->pPara = ME_GetParagraph(result->pRun);
984 result->nOffset = 0;
985 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
986 return FALSE;
990 /* Sets the cursor to the position closest to the pixel position
992 * x & y are pixel positions in client coordinates.
994 * isExact will be set to TRUE if the run is directly under the pixel
995 * position, FALSE if it not, unless isExact is set to NULL.
997 * return FALSE if outside client area and the cursor is not set,
998 * otherwise TRUE is returned.
1000 BOOL ME_CharFromPos(ME_TextEditor *editor, int x, int y,
1001 ME_Cursor *cursor, BOOL *isExact)
1003 RECT rc;
1004 BOOL bResult;
1006 ITextHost_TxGetClientRect(editor->texthost, &rc);
1007 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
1008 if (isExact) *isExact = FALSE;
1009 return FALSE;
1011 x += editor->horz_si.nPos;
1012 y += editor->vert_si.nPos;
1013 bResult = ME_FindPixelPos(editor, x, y, cursor, NULL);
1014 if (isExact) *isExact = bResult;
1015 return TRUE;
1020 /* Extends the selection with a word, line, or paragraph selection type.
1022 * The selection is anchored by editor->pCursors[2-3] such that the text
1023 * between the anchors will remain selected, and one end will be extended.
1025 * editor->pCursors[0] should have the position to extend the selection to
1026 * before this function is called.
1028 * Nothing will be done if editor->nSelectionType equals stPosition.
1030 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1032 ME_Cursor tmp_cursor;
1033 int curOfs, anchorStartOfs, anchorEndOfs;
1034 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1035 return;
1036 curOfs = ME_GetCursorOfs(&editor->pCursors[0]);
1037 anchorStartOfs = ME_GetCursorOfs(&editor->pCursors[3]);
1038 anchorEndOfs = ME_GetCursorOfs(&editor->pCursors[2]);
1040 tmp_cursor = editor->pCursors[0];
1041 editor->pCursors[0] = editor->pCursors[2];
1042 editor->pCursors[1] = editor->pCursors[3];
1043 if (curOfs < anchorStartOfs)
1045 /* Extend the left side of selection */
1046 editor->pCursors[1] = tmp_cursor;
1047 if (editor->nSelectionType == stWord)
1048 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1049 else
1051 ME_DisplayItem *pItem;
1052 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1053 diStartRowOrParagraph:diParagraph);
1054 pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
1055 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
1056 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
1057 editor->pCursors[1].nOffset = 0;
1060 else if (curOfs >= anchorEndOfs)
1062 /* Extend the right side of selection */
1063 editor->pCursors[0] = tmp_cursor;
1064 if (editor->nSelectionType == stWord)
1065 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
1066 else
1068 ME_DisplayItem *pItem;
1069 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1070 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
1071 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
1072 if (pItem->type == diTextEnd)
1073 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
1074 else
1075 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
1076 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
1077 editor->pCursors[0].nOffset = 0;
1082 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1084 ME_Cursor tmp_cursor;
1085 int is_selection = 0;
1086 BOOL is_shift;
1088 editor->nUDArrowX = -1;
1090 x += editor->horz_si.nPos;
1091 y += editor->vert_si.nPos;
1093 tmp_cursor = editor->pCursors[0];
1094 is_selection = ME_IsSelection(editor);
1095 is_shift = GetKeyState(VK_SHIFT) < 0;
1097 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
1099 if (x >= editor->rcFormat.left || is_shift)
1101 if (clickNum > 1)
1103 editor->pCursors[1] = editor->pCursors[0];
1104 if (is_shift) {
1105 if (x >= editor->rcFormat.left)
1106 ME_SelectByType(editor, stWord);
1107 else
1108 ME_SelectByType(editor, stParagraph);
1109 } else if (clickNum % 2 == 0) {
1110 ME_SelectByType(editor, stWord);
1111 } else {
1112 ME_SelectByType(editor, stParagraph);
1115 else if (!is_shift)
1117 editor->nSelectionType = stPosition;
1118 editor->pCursors[1] = editor->pCursors[0];
1120 else if (!is_selection)
1122 editor->nSelectionType = stPosition;
1123 editor->pCursors[1] = tmp_cursor;
1125 else if (editor->nSelectionType != stPosition)
1127 ME_ExtendAnchorSelection(editor);
1130 else
1132 if (clickNum < 2) {
1133 ME_SelectByType(editor, stLine);
1134 } else if (clickNum % 2 == 0 || is_shift) {
1135 ME_SelectByType(editor, stParagraph);
1136 } else {
1137 ME_SelectByType(editor, stDocument);
1140 ME_InvalidateSelection(editor);
1141 ITextHost_TxShowCaret(editor->texthost, FALSE);
1142 ME_ShowCaret(editor);
1143 ME_ClearTempStyle(editor);
1144 ME_SendSelChange(editor);
1147 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1149 ME_Cursor tmp_cursor;
1151 if (editor->nSelectionType == stDocument)
1152 return;
1153 x += editor->horz_si.nPos;
1154 y += editor->vert_si.nPos;
1156 tmp_cursor = editor->pCursors[0];
1157 /* FIXME: do something with the return value of ME_FindPixelPos */
1158 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
1160 ME_InvalidateSelection(editor);
1161 editor->pCursors[0] = tmp_cursor;
1162 ME_ExtendAnchorSelection(editor);
1164 if (editor->nSelectionType != stPosition &&
1165 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1167 /* The scroll the cursor towards the other end, since it was the one
1168 * extended by ME_ExtendAnchorSelection */
1169 ME_EnsureVisible(editor, &editor->pCursors[1]);
1170 } else {
1171 ME_EnsureVisible(editor, &editor->pCursors[0]);
1174 ME_InvalidateSelection(editor);
1175 ITextHost_TxShowCaret(editor->texthost, FALSE);
1176 ME_ShowCaret(editor);
1177 ME_SendSelChange(editor);
1180 static BOOL ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
1181 int x, ME_Cursor *cursor, int *pbCaretAtEnd)
1183 ME_DisplayItem *pNext, *pLastRun;
1184 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
1185 assert(pNext->type == diRun);
1186 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
1187 cursor->nOffset = 0;
1188 do {
1189 int run_x = pNext->member.run.pt.x;
1190 int width = pNext->member.run.nWidth;
1191 if (x < run_x)
1193 cursor->pRun = pNext;
1194 cursor->pPara = ME_GetParagraph( cursor->pRun );
1195 return FALSE;
1197 if (x >= run_x && x < run_x+width)
1199 int ch = ME_CharFromPoint(editor, x-run_x, &pNext->member.run, TRUE);
1200 if (ch < pNext->member.run.len)
1202 cursor->nOffset = ch;
1203 cursor->pRun = pNext;
1204 cursor->pPara = ME_GetParagraph( cursor->pRun );
1205 return TRUE;
1208 pLastRun = pNext;
1209 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
1210 } while(pNext && pNext->type == diRun);
1212 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
1214 cursor->pRun = ME_FindItemFwd(pNext, diRun);
1215 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
1217 else
1218 cursor->pRun = pLastRun;
1220 cursor->pPara = ME_GetParagraph( cursor->pRun );
1221 return FALSE;
1224 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1226 ME_DisplayItem *pRun = pCursor->pRun;
1227 int x;
1229 if (editor->nUDArrowX != -1)
1230 x = editor->nUDArrowX;
1231 else {
1232 if (editor->bCaretAtEnd)
1234 pRun = ME_FindItemBack(pRun, diRun);
1235 assert(pRun);
1236 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1238 else {
1239 x = pRun->member.run.pt.x;
1240 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
1242 editor->nUDArrowX = x;
1244 return x;
1248 static void
1249 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1251 ME_DisplayItem *pRun = pCursor->pRun;
1252 ME_DisplayItem *pOldPara = pCursor->pPara;
1253 ME_DisplayItem *pItem, *pNewPara;
1254 int x = ME_GetXForArrow(editor, pCursor);
1256 if (editor->bCaretAtEnd && !pCursor->nOffset)
1257 if (!ME_PrevRun(&pOldPara, &pRun))
1258 return;
1260 if (nRelOfs == -1)
1262 /* start of this row */
1263 pItem = ME_FindItemBack(pRun, diStartRow);
1264 assert(pItem);
1265 /* start of the previous row */
1266 pItem = ME_FindItemBack(pItem, diStartRow);
1267 if (!pItem)
1268 return; /* row not found - ignore */
1269 pNewPara = ME_GetParagraph(pItem);
1270 if (pOldPara->member.para.nFlags & MEPF_ROWEND ||
1271 (pOldPara->member.para.pCell &&
1272 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1274 /* Brought out of a cell */
1275 pNewPara = ME_GetTableRowStart(pOldPara)->member.para.prev_para;
1276 if (pNewPara->type == diTextStart)
1277 return; /* At the top, so don't go anywhere. */
1278 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1280 if (pNewPara->member.para.nFlags & MEPF_ROWEND)
1282 /* Brought into a table row */
1283 ME_Cell *cell = &ME_FindItemBack(pNewPara, diCell)->member.cell;
1284 while (x < cell->pt.x && cell->prev_cell)
1285 cell = &cell->prev_cell->member.cell;
1286 if (cell->next_cell) /* else - we are still at the end of the row */
1287 pItem = ME_FindItemBack(cell->next_cell, diStartRow);
1290 else
1292 /* start of the next row */
1293 pItem = ME_FindItemFwd(pRun, diStartRow);
1294 if (!pItem)
1295 return; /* row not found - ignore */
1296 pNewPara = ME_GetParagraph(pItem);
1297 if (pOldPara->member.para.nFlags & MEPF_ROWSTART ||
1298 (pOldPara->member.para.pCell &&
1299 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1301 /* Brought out of a cell */
1302 pNewPara = ME_GetTableRowEnd(pOldPara)->member.para.next_para;
1303 if (pNewPara->type == diTextEnd)
1304 return; /* At the bottom, so don't go anywhere. */
1305 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1307 if (pNewPara->member.para.nFlags & MEPF_ROWSTART)
1309 /* Brought into a table row */
1310 ME_DisplayItem *cell = ME_FindItemFwd(pNewPara, diCell);
1311 while (cell->member.cell.next_cell &&
1312 x >= cell->member.cell.next_cell->member.cell.pt.x)
1313 cell = cell->member.cell.next_cell;
1314 pItem = ME_FindItemFwd(cell, diStartRow);
1317 if (!pItem)
1319 /* row not found - ignore */
1320 return;
1322 ME_FindRunInRow(editor, pItem, x, pCursor, &editor->bCaretAtEnd);
1323 assert(pCursor->pRun);
1324 assert(pCursor->pRun->type == diRun);
1327 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1329 ME_DisplayItem *p = ME_FindItemFwd(editor->pBuffer->pFirst, diStartRow);
1331 if (editor->vert_si.nPos < p->member.row.nHeight)
1333 ME_SetCursorToStart(editor, pCursor);
1334 editor->bCaretAtEnd = FALSE;
1335 /* Native clears seems to clear this x value on page up at the top
1336 * of the text, but not on page down at the end of the text.
1337 * Doesn't make sense, but we try to be bug for bug compatible. */
1338 editor->nUDArrowX = -1;
1339 } else {
1340 ME_DisplayItem *pRun = pCursor->pRun;
1341 ME_DisplayItem *pLast;
1342 int x, y, yd, yp;
1343 int yOldScrollPos = editor->vert_si.nPos;
1345 x = ME_GetXForArrow(editor, pCursor);
1346 if (!pCursor->nOffset && editor->bCaretAtEnd)
1347 pRun = ME_FindItemBack(pRun, diRun);
1349 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1350 assert(p->type == diStartRow);
1351 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1352 y = yp + p->member.row.pt.y;
1354 ME_ScrollUp(editor, editor->sizeWindow.cy);
1355 /* Only move the cursor by the amount scrolled. */
1356 yd = y + editor->vert_si.nPos - yOldScrollPos;
1357 pLast = p;
1359 do {
1360 p = ME_FindItemBack(p, diStartRowOrParagraph);
1361 if (!p)
1362 break;
1363 if (p->type == diParagraph) { /* crossing paragraphs */
1364 if (p->member.para.prev_para == NULL)
1365 break;
1366 yp = p->member.para.prev_para->member.para.pt.y;
1367 continue;
1369 y = yp + p->member.row.pt.y;
1370 if (y < yd)
1371 break;
1372 pLast = p;
1373 } while(1);
1375 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
1377 assert(pCursor->pRun);
1378 assert(pCursor->pRun->type == diRun);
1381 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1383 ME_DisplayItem *pLast;
1384 int x, y;
1386 /* Find y position of the last row */
1387 pLast = editor->pBuffer->pLast;
1388 y = pLast->member.para.prev_para->member.para.pt.y
1389 + ME_FindItemBack(pLast, diStartRow)->member.row.pt.y;
1391 x = ME_GetXForArrow(editor, pCursor);
1393 if (editor->vert_si.nPos >= y - editor->sizeWindow.cy)
1395 ME_SetCursorToEnd(editor, pCursor);
1396 editor->bCaretAtEnd = FALSE;
1397 } else {
1398 ME_DisplayItem *pRun = pCursor->pRun;
1399 ME_DisplayItem *p;
1400 int yd, yp;
1401 int yOldScrollPos = editor->vert_si.nPos;
1403 if (!pCursor->nOffset && editor->bCaretAtEnd)
1404 pRun = ME_FindItemBack(pRun, diRun);
1406 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1407 assert(p->type == diStartRow);
1408 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1409 y = yp + p->member.row.pt.y;
1411 /* For native richedit controls:
1412 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1413 * v4.1 can scroll past this position here. */
1414 ME_ScrollDown(editor, editor->sizeWindow.cy);
1415 /* Only move the cursor by the amount scrolled. */
1416 yd = y + editor->vert_si.nPos - yOldScrollPos;
1417 pLast = p;
1419 do {
1420 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1421 if (!p)
1422 break;
1423 if (p->type == diParagraph) {
1424 yp = p->member.para.pt.y;
1425 continue;
1427 y = yp + p->member.row.pt.y;
1428 if (y >= yd)
1429 break;
1430 pLast = p;
1431 } while(1);
1433 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
1435 assert(pCursor->pRun);
1436 assert(pCursor->pRun->type == diRun);
1439 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1441 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1442 if (pRow) {
1443 ME_DisplayItem *pRun;
1444 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1445 pRow = ME_FindItemBack(pRow, diStartRow);
1446 if (!pRow)
1447 return;
1449 pRun = ME_FindItemFwd(pRow, diRun);
1450 if (pRun) {
1451 pCursor->pRun = pRun;
1452 assert(pCursor->pPara == ME_GetParagraph(pRun));
1453 pCursor->nOffset = 0;
1456 editor->bCaretAtEnd = FALSE;
1459 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1461 ME_SetCursorToStart(editor, pCursor);
1462 editor->bCaretAtEnd = FALSE;
1465 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1467 ME_DisplayItem *pRow;
1469 if (editor->bCaretAtEnd && !pCursor->nOffset)
1470 return;
1472 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1473 assert(pRow);
1474 if (pRow->type == diStartRow) {
1475 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1476 assert(pRun);
1477 pCursor->pRun = pRun;
1478 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1479 pCursor->nOffset = 0;
1480 editor->bCaretAtEnd = TRUE;
1481 return;
1483 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1484 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1485 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1486 pCursor->nOffset = 0;
1487 editor->bCaretAtEnd = FALSE;
1490 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1492 ME_SetCursorToEnd(editor, pCursor);
1493 editor->bCaretAtEnd = FALSE;
1496 BOOL ME_IsSelection(ME_TextEditor *editor)
1498 return editor->pCursors[0].pRun != editor->pCursors[1].pRun ||
1499 editor->pCursors[0].nOffset != editor->pCursors[1].nOffset;
1502 void ME_DeleteSelection(ME_TextEditor *editor)
1504 int from, to;
1505 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
1506 ME_DeleteTextAtCursor(editor, nStartCursor, to - from);
1509 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1511 return ME_GetInsertStyle(editor, 0);
1514 void ME_SendSelChange(ME_TextEditor *editor)
1516 SELCHANGE sc;
1518 if (!(editor->nEventMask & ENM_SELCHANGE))
1519 return;
1521 sc.nmhdr.hwndFrom = NULL;
1522 sc.nmhdr.idFrom = 0;
1523 sc.nmhdr.code = EN_SELCHANGE;
1524 ME_GetSelectionOfs(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1525 sc.seltyp = SEL_EMPTY;
1526 if (sc.chrg.cpMin != sc.chrg.cpMax)
1527 sc.seltyp |= SEL_TEXT;
1528 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* what were RICHEDIT authors thinking ? */
1529 sc.seltyp |= SEL_MULTICHAR;
1530 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1531 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1532 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1533 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1534 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1536 ME_ClearTempStyle(editor);
1538 editor->notified_cr = sc.chrg;
1539 ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
1543 BOOL
1544 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1546 int nCursor = 0;
1547 ME_Cursor *p = &editor->pCursors[nCursor];
1548 ME_Cursor tmp_curs = *p;
1549 BOOL success = FALSE;
1551 ME_CheckCharOffsets(editor);
1552 switch(nVKey) {
1553 case VK_LEFT:
1554 editor->bCaretAtEnd = 0;
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_RIGHT:
1561 editor->bCaretAtEnd = 0;
1562 if (ctrl)
1563 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1564 else
1565 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1566 break;
1567 case VK_UP:
1568 ME_MoveCursorLines(editor, &tmp_curs, -1);
1569 break;
1570 case VK_DOWN:
1571 ME_MoveCursorLines(editor, &tmp_curs, +1);
1572 break;
1573 case VK_PRIOR:
1574 ME_ArrowPageUp(editor, &tmp_curs);
1575 break;
1576 case VK_NEXT:
1577 ME_ArrowPageDown(editor, &tmp_curs);
1578 break;
1579 case VK_HOME: {
1580 if (ctrl)
1581 ME_ArrowCtrlHome(editor, &tmp_curs);
1582 else
1583 ME_ArrowHome(editor, &tmp_curs);
1584 editor->bCaretAtEnd = 0;
1585 break;
1587 case VK_END:
1588 if (ctrl)
1589 ME_ArrowCtrlEnd(editor, &tmp_curs);
1590 else
1591 ME_ArrowEnd(editor, &tmp_curs);
1592 break;
1595 if (!extend)
1596 editor->pCursors[1] = tmp_curs;
1597 *p = tmp_curs;
1599 ME_InvalidateSelection(editor);
1600 ME_Repaint(editor);
1601 ITextHost_TxShowCaret(editor->texthost, FALSE);
1602 ME_EnsureVisible(editor, &tmp_curs);
1603 ME_ShowCaret(editor);
1604 ME_SendSelChange(editor);
1605 return success;