sxs: Remove unused Wine debug channel.
[wine/wine64.git] / dlls / riched20 / caret.c
blobab44dac12ff3f63060dae610255acb2c2d51324b
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 static BOOL
28 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs);
30 void ME_GetSelection(ME_TextEditor *editor, int *from, int *to)
32 *from = ME_GetCursorOfs(editor, 0);
33 *to = ME_GetCursorOfs(editor, 1);
35 if (*from > *to)
37 int tmp = *from;
38 *from = *to;
39 *to = tmp;
43 int ME_GetTextLength(ME_TextEditor *editor)
45 return ME_CharOfsFromRunOfs(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun), 0);
49 int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how)
51 int length;
53 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
54 return E_INVALIDARG;
55 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
56 return E_INVALIDARG;
58 length = ME_GetTextLength(editor);
60 if ((GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_MULTILINE)
61 && (how->flags & GTL_USECRLF)
62 && !editor->bEmulateVersion10) /* Ignore GTL_USECRLF flag in 1.0 emulation */
63 length += editor->nParagraphs - 1;
65 if (how->flags & GTL_NUMBYTES)
67 CPINFO cpinfo;
69 if (how->codepage == 1200)
70 return length * 2;
71 if (how->flags & GTL_PRECISE)
72 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
73 if (GetCPInfo(how->codepage, &cpinfo))
74 return length * cpinfo.MaxCharSize;
75 ERR("Invalid codepage %u\n", how->codepage);
76 return E_INVALIDARG;
78 return length;
82 int ME_SetSelection(ME_TextEditor *editor, int from, int to)
84 int selectionEnd = 0;
85 const int len = ME_GetTextLength(editor);
87 /* all negative values are effectively the same */
88 if (from < 0)
89 from = -1;
90 if (to < 0)
91 to = -1;
93 /* select all */
94 if (from == 0 && to == -1)
96 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
97 editor->pCursors[1].nOffset = 0;
98 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
99 editor->pCursors[0].nOffset = 0;
100 ME_InvalidateSelection(editor);
101 ME_ClearTempStyle(editor);
102 return len + 1;
105 /* if both values are equal and also out of bound, that means to */
106 /* put the selection at the end of the text */
107 if ((from == to) && (to < 0 || to > len))
109 selectionEnd = 1;
111 else
113 /* if from is negative and to is positive then selection is */
114 /* deselected and caret moved to end of the current selection */
115 if (from < 0)
117 int start, end;
118 ME_GetSelection(editor, &start, &end);
119 editor->pCursors[1] = editor->pCursors[0];
120 ME_Repaint(editor);
121 ME_ClearTempStyle(editor);
122 return end;
125 /* adjust to if it's a negative value */
126 if (to < 0)
127 to = len + 1;
129 /* flip from and to if they are reversed */
130 if (from>to)
132 int tmp = from;
133 from = to;
134 to = tmp;
137 /* after fiddling with the values, we find from > len && to > len */
138 if (from > len)
139 selectionEnd = 1;
140 /* special case with to too big */
141 else if (to > len)
142 to = len + 1;
145 if (selectionEnd)
147 editor->pCursors[1].pRun = editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
148 editor->pCursors[1].nOffset = editor->pCursors[0].nOffset = 0;
149 ME_InvalidateSelection(editor);
150 ME_ClearTempStyle(editor);
151 return len;
154 ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
155 ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);
156 return to;
160 void
161 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
162 int *x, int *y, int *height)
164 ME_DisplayItem *pCursorRun = pCursor->pRun;
165 ME_DisplayItem *pSizeRun = pCursor->pRun;
167 assert(height && x && y);
168 assert(!(ME_GetParagraph(pCursorRun)->member.para.nFlags & MEPF_REWRAP));
169 assert(pCursor->pRun);
170 assert(pCursor->pRun->type == diRun);
172 if (pCursorRun->type == diRun) {
173 ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
175 if (row) {
176 HDC hDC = GetDC(editor->hWnd);
177 ME_Context c;
178 ME_DisplayItem *run = pCursorRun;
179 ME_DisplayItem *para = NULL;
180 SIZE sz = {0, 0};
182 ME_InitContext(&c, editor, hDC);
184 if (!pCursor->nOffset)
186 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrParagraph);
187 assert(prev);
188 if (prev->type == diRun)
189 pSizeRun = prev;
191 assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
192 para = ME_FindItemBack(row, diParagraph);
193 assert(para);
194 assert(para->type == diParagraph);
195 if (editor->bCaretAtEnd && !pCursor->nOffset &&
196 run == ME_FindItemFwd(row, diRun))
198 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
199 assert(tmp);
200 if (tmp->type == diRun)
202 row = ME_FindItemBack(tmp, diStartRow);
203 pSizeRun = run = tmp;
204 assert(run);
205 assert(run->type == diRun);
206 sz = ME_GetRunSize(&c, &para->member.para,
207 &run->member.run, ME_StrLen(run->member.run.strText),
208 row->member.row.nLMargin);
211 if (pCursor->nOffset) {
212 sz = ME_GetRunSize(&c, &para->member.para, &run->member.run, pCursor->nOffset,
213 row->member.row.nLMargin);
216 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
217 *x = c.rcView.left + run->member.run.pt.x + sz.cx;
218 *y = c.rcView.top + para->member.para.pt.y + row->member.row.nBaseline
219 + run->member.run.pt.y - pSizeRun->member.run.nAscent - ME_GetYScrollPos(editor);
220 ME_DestroyContext(&c, editor->hWnd);
221 return;
224 *height = 10; /* FIXME use global font */
225 *x = 0;
226 *y = 0;
230 void
231 ME_MoveCaret(ME_TextEditor *editor)
233 int x, y, height;
235 if (ME_WrapMarkedParagraphs(editor))
236 ME_UpdateScrollBar(editor);
237 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
238 if(editor->bHaveFocus && !ME_IsSelection(editor))
240 x = min(x, editor->rcFormat.right-1);
241 CreateCaret(editor->hWnd, NULL, 0, height);
242 SetCaretPos(x, y);
247 void ME_ShowCaret(ME_TextEditor *ed)
249 ME_MoveCaret(ed);
250 if(ed->bHaveFocus && !ME_IsSelection(ed))
251 ShowCaret(ed->hWnd);
254 void ME_HideCaret(ME_TextEditor *ed)
256 if(!ed->bHaveFocus || ME_IsSelection(ed))
258 HideCaret(ed->hWnd);
259 DestroyCaret();
263 BOOL ME_InternalDeleteText(ME_TextEditor *editor, int nOfs, int nChars,
264 BOOL bForce)
266 ME_Cursor c;
267 int shift = 0;
268 int totalChars = nChars;
269 ME_DisplayItem *start_para;
272 /* Prevent deletion past last end of paragraph run. */
273 ME_DisplayItem *pTextEnd = editor->pBuffer->pLast;
274 int nMaxChars = pTextEnd->member.para.prev_para->member.para.nCharOfs;
275 nMaxChars += ME_FindItemBack(pTextEnd, diRun)->member.run.nCharOfs;
276 nMaxChars -= nOfs;
277 nChars = min(nChars, nMaxChars);
280 ME_CursorFromCharOfs(editor, nOfs, &c);
281 start_para = ME_GetParagraph(c.pRun);
283 if (!bForce)
285 ME_ProtectPartialTableDeletion(editor, nOfs, &nChars);
286 if (nChars == 0)
287 return FALSE;
290 while(nChars > 0)
292 ME_Run *run;
293 ME_CursorFromCharOfs(editor, nOfs+nChars, &c);
294 if (!c.nOffset &&
295 nOfs+nChars == (c.pRun->member.run.nCharOfs
296 + ME_GetParagraph(c.pRun)->member.para.nCharOfs))
298 /* We aren't deleting anything in this run, so we will go back to the
299 * last run we are deleting text in. */
300 c.pRun = ME_FindItemBack(c.pRun, diRun);
301 if (c.pRun->member.run.nFlags & MERF_ENDPARA)
302 c.nOffset = c.pRun->member.run.nCR + c.pRun->member.run.nLF;
303 else
304 c.nOffset = c.pRun->member.run.strText->nLen;
306 run = &c.pRun->member.run;
307 if (run->nFlags & MERF_ENDPARA) {
308 int eollen = run->nCR + run->nLF;
309 BOOL keepFirstParaFormat;
311 if (!ME_FindItemFwd(c.pRun, diParagraph))
313 return TRUE;
315 keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
316 run->nCharOfs);
317 if (!editor->bEmulateVersion10) /* v4.1 */
319 ME_DisplayItem *next_para = ME_FindItemFwd(c.pRun, diParagraphOrEnd);
320 ME_DisplayItem *this_para = next_para->member.para.prev_para;
322 /* The end of paragraph before a table row is only deleted if there
323 * is nothing else on the line before it. */
324 if (this_para == start_para &&
325 next_para->member.para.nFlags & MEPF_ROWSTART)
327 /* If the paragraph will be empty, then it should be deleted, however
328 * it still might have text right now which would inherit the
329 * MEPF_STARTROW property if we joined it right now.
330 * Instead we will delete it after the preceding text is deleted. */
331 if (nOfs > this_para->member.para.nCharOfs) {
332 /* Skip this end of line. */
333 nChars -= (eollen < nChars) ? eollen : nChars;
334 continue;
336 keepFirstParaFormat = TRUE;
339 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun), keepFirstParaFormat);
340 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
341 ME_CheckCharOffsets(editor);
342 nChars -= (eollen < nChars) ? eollen : nChars;
343 continue;
345 else
347 ME_Cursor cursor;
348 int nCharsToDelete = min(nChars, c.nOffset);
349 int i;
351 c.nOffset -= nCharsToDelete;
353 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
355 cursor = c;
356 /* nChars is the number of characters that should be deleted from the
357 PRECEDING runs (these BEFORE cursor.pRun)
358 nCharsToDelete is a number of chars to delete from THIS run */
359 nChars -= nCharsToDelete;
360 shift -= nCharsToDelete;
361 TRACE("Deleting %d (remaning %d) chars at %d in '%s' (%d)\n",
362 nCharsToDelete, nChars, c.nOffset,
363 debugstr_w(run->strText->szData), run->strText->nLen);
365 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
367 /* undo = reinsert whole run */
368 /* nOfs is a character offset (from the start of the document
369 to the current (deleted) run */
370 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
371 if (pUndo)
372 pUndo->di.member.run.nCharOfs = nOfs+nChars;
374 else
376 /* undo = reinsert partial run */
377 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
378 if (pUndo) {
379 ME_DestroyString(pUndo->di.member.run.strText);
380 pUndo->di.member.run.nCharOfs = nOfs+nChars;
381 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
384 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
385 TRACE("Shift value: %d\n", shift);
386 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
388 /* update cursors (including c) */
389 for (i=-1; i<editor->nCursors; i++) {
390 ME_Cursor *pThisCur = editor->pCursors + i;
391 if (i == -1) pThisCur = &c;
392 if (pThisCur->pRun == cursor.pRun) {
393 if (pThisCur->nOffset > cursor.nOffset) {
394 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
395 pThisCur->nOffset = cursor.nOffset;
396 else
397 pThisCur->nOffset -= nCharsToDelete;
398 assert(pThisCur->nOffset >= 0);
399 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
401 if (pThisCur->nOffset == ME_StrVLen(run->strText))
403 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
404 assert(pThisCur->pRun->type == diRun);
405 pThisCur->nOffset = 0;
410 /* c = updated data now */
412 if (c.pRun == cursor.pRun)
413 ME_SkipAndPropagateCharOffset(c.pRun, shift);
414 else
415 ME_PropagateCharOffset(c.pRun, shift);
417 if (!ME_StrVLen(cursor.pRun->member.run.strText))
419 TRACE("Removing useless run\n");
420 ME_Remove(cursor.pRun);
421 ME_DestroyDisplayItem(cursor.pRun);
424 shift = 0;
426 ME_CheckCharOffsets(editor);
428 continue;
431 return TRUE;
434 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
436 assert(nCursor>=0 && nCursor<editor->nCursors);
437 /* text operations set modified state */
438 editor->nModifyStep = 1;
439 return ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars,
440 FALSE);
443 static ME_DisplayItem *
444 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
445 const WCHAR *str, int len, ME_Style *style,
446 int flags)
448 ME_Cursor *p = &editor->pCursors[nCursor];
450 editor->bCaretAtEnd = FALSE;
452 assert(p->pRun->type == diRun);
454 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
458 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
460 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
461 ME_DisplayItem *di;
462 WCHAR space = ' ';
464 /* FIXME no no no */
465 if (ME_IsSelection(editor))
466 ME_DeleteSelection(editor);
468 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
469 MERF_GRAPHICS);
470 di->member.run.ole_obj = ALLOC_OBJ(*reo);
471 ME_CopyReObject(di->member.run.ole_obj, reo);
472 ME_SendSelChange(editor);
476 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, 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_ENDROW);
488 ME_SendSelChange(editor);
492 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
493 const WCHAR *str, int len, ME_Style *style)
495 const WCHAR *pos;
496 ME_Cursor *p = NULL;
497 int oldLen;
499 /* FIXME really HERE ? */
500 if (ME_IsSelection(editor))
501 ME_DeleteSelection(editor);
503 /* FIXME: is this too slow? */
504 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
505 oldLen = ME_GetTextLength(editor);
507 /* text operations set modified state */
508 editor->nModifyStep = 1;
510 assert(style);
512 assert(nCursor>=0 && nCursor<editor->nCursors);
513 if (len == -1)
514 len = lstrlenW(str);
516 /* grow the text limit to fit our text */
517 if(editor->nTextLimit < oldLen +len)
518 editor->nTextLimit = oldLen + len;
520 while (len)
522 pos = str;
523 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
524 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
525 pos++;
526 if (pos-str < len && *pos == '\t') { /* handle tabs */
527 WCHAR tab = '\t';
529 if (pos!=str)
530 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
532 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
534 pos++;
535 if(pos-str <= len) {
536 len -= pos - str;
537 str = pos;
538 continue;
541 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
542 if (!editor->bEmulateVersion10 && pos-str < len-2 && pos[0] == '\r' && pos[1] == '\r' && pos[2] == '\n') {
543 WCHAR space = ' ';
545 if (pos!=str)
546 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
548 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
550 pos+=3;
551 if(pos-str <= len) {
552 len -= pos - str;
553 str = pos;
554 continue;
557 if (pos-str < len) { /* handle EOLs */
558 ME_DisplayItem *tp, *end_run;
559 ME_Style *tmp_style;
560 int numCR, numLF;
562 if (pos!=str)
563 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
564 p = &editor->pCursors[nCursor];
565 if (p->nOffset) {
566 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
567 p = &editor->pCursors[nCursor];
569 tmp_style = ME_GetInsertStyle(editor, nCursor);
570 /* ME_SplitParagraph increases style refcount */
572 /* Encode and fill number of CR and LF according to emulation mode */
573 if (editor->bEmulateVersion10) {
574 const WCHAR * tpos;
576 /* We have to find out how many consecutive \r are there, and if there
577 is a \n terminating the run of \r's. */
578 numCR = 0; numLF = 0;
579 tpos = pos;
580 while (tpos-str < len && *tpos == '\r') {
581 tpos++;
582 numCR++;
584 if (tpos-str >= len) {
585 /* Reached end of text without finding anything but '\r' */
586 if (tpos != pos) {
587 pos++;
589 numCR = 1; numLF = 0;
590 } else if (*tpos == '\n') {
591 /* The entire run of \r's plus the one \n is one single line break */
592 pos = tpos + 1;
593 numLF = 1;
594 } else {
595 /* Found some other content past the run of \r's */
596 pos++;
597 numCR = 1; numLF = 0;
599 } else {
600 if(pos-str < len && *pos =='\r')
601 pos++;
602 if(pos-str < len && *pos =='\n')
603 pos++;
604 numCR = 1; numLF = 0;
606 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, numCR, numLF, 0);
607 p->pRun = ME_FindItemFwd(tp, diRun);
608 end_run = ME_FindItemBack(tp, diRun);
609 ME_ReleaseStyle(end_run->member.run.style);
610 end_run->member.run.style = tmp_style;
611 p->nOffset = 0;
613 if(pos-str <= len) {
614 len -= pos - str;
615 str = pos;
616 continue;
619 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
620 len = 0;
625 static BOOL
626 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
628 ME_DisplayItem *pRun = pCursor->pRun;
630 if (nRelOfs == -1)
632 if (!pCursor->nOffset)
634 do {
635 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
636 assert(pRun);
637 switch (pRun->type)
639 case diRun:
640 break;
641 case diParagraph:
642 if (pRun->member.para.prev_para->type == diTextStart)
643 return FALSE;
644 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
645 /* every paragraph ought to have at least one run */
646 assert(pRun && pRun->type == diRun);
647 assert(pRun->member.run.nFlags & MERF_ENDPARA);
648 break;
649 default:
650 assert(pRun->type != diRun && pRun->type != diParagraph);
651 return FALSE;
653 } while (RUN_IS_HIDDEN(&pRun->member.run) ||
654 pRun->member.run.nFlags & MERF_HIDDEN);
655 pCursor->pRun = pRun;
656 if (pRun->member.run.nFlags & MERF_ENDPARA)
657 pCursor->nOffset = 0;
658 else
659 pCursor->nOffset = pRun->member.run.strText->nLen;
662 if (pCursor->nOffset)
663 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
664 return TRUE;
666 else
668 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
670 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
672 if (new_ofs < pRun->member.run.strText->nLen)
674 pCursor->nOffset = new_ofs;
675 return TRUE;
678 do {
679 pRun = ME_FindItemFwd(pRun, diRun);
680 } while (pRun && (RUN_IS_HIDDEN(&pRun->member.run) ||
681 pRun->member.run.nFlags & MERF_HIDDEN));
682 if (pRun)
684 pCursor->pRun = pRun;
685 pCursor->nOffset = 0;
686 return TRUE;
689 return FALSE;
693 static BOOL
694 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
696 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
697 int nOffset = cursor->nOffset;
699 if (nRelOfs == -1)
701 /* Backward movement */
702 while (TRUE)
704 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
705 nOffset, WB_MOVEWORDLEFT);
706 if (nOffset)
707 break;
708 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
709 if (pOtherRun->type == diRun)
711 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
712 pOtherRun->member.run.strText->nLen - 1,
713 WB_ISDELIMITER)
714 && !(pRun->member.run.nFlags & MERF_ENDPARA)
715 && !(cursor->pRun == pRun && cursor->nOffset == 0)
716 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
717 WB_ISDELIMITER))
718 break;
719 pRun = pOtherRun;
720 nOffset = pOtherRun->member.run.strText->nLen;
722 else if (pOtherRun->type == diParagraph)
724 if (cursor->pRun == pRun && cursor->nOffset == 0)
726 /* Skip empty start of table row paragraph */
727 if (pOtherRun->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART)
728 pOtherRun = pOtherRun->member.para.prev_para;
729 /* Paragraph breaks are treated as separate words */
730 if (pOtherRun->member.para.prev_para->type == diTextStart)
731 return FALSE;
733 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
735 break;
739 else
741 /* Forward movement */
742 BOOL last_delim = FALSE;
744 while (TRUE)
746 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
747 nOffset, WB_ISDELIMITER))
748 break;
749 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
750 nOffset, WB_MOVEWORDRIGHT);
751 if (nOffset < pRun->member.run.strText->nLen)
752 break;
753 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
754 if (pOtherRun->type == diRun)
756 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
757 nOffset - 1, WB_ISDELIMITER);
758 pRun = pOtherRun;
759 nOffset = 0;
761 else if (pOtherRun->type == diParagraph)
763 if (pOtherRun->member.para.nFlags & MEPF_ROWSTART)
764 pOtherRun = pOtherRun->member.para.next_para;
765 if (cursor->pRun == pRun)
766 pRun = ME_FindItemFwd(pOtherRun, diRun);
767 nOffset = 0;
768 break;
770 else /* diTextEnd */
772 if (cursor->pRun == pRun)
773 return FALSE;
774 nOffset = 0;
775 break;
779 cursor->pRun = pRun;
780 cursor->nOffset = nOffset;
781 return TRUE;
785 void
786 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
788 /* pCursor[0] is the end of the selection
789 * pCursor[1] is the start of the selection (or the position selection anchor)
790 * pCursor[2] and [3] are the selection anchors that are backed up
791 * so they are kept when the selection changes for drag selection.
794 editor->nSelectionType = selectionType;
795 switch(selectionType)
797 case stPosition:
798 break;
799 case stWord:
800 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
801 editor->pCursors[1] = editor->pCursors[0];
802 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
803 break;
804 case stLine:
805 case stParagraph:
807 ME_DisplayItem *pItem;
808 ME_DIType fwdSearchType, backSearchType;
809 if (selectionType == stParagraph) {
810 backSearchType = diParagraph;
811 fwdSearchType = diParagraphOrEnd;
812 } else {
813 backSearchType = diStartRow;
814 fwdSearchType = diStartRowOrParagraphOrEnd;
816 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
817 assert(pItem);
818 if (pItem->type == diTextEnd)
819 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
820 else
821 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
822 editor->pCursors[0].nOffset = 0;
824 pItem = ME_FindItemBack(pItem, backSearchType);
825 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
826 editor->pCursors[1].nOffset = 0;
827 break;
829 case stDocument:
830 /* Select everything with cursor anchored from the start of the text */
831 editor->nSelectionType = stDocument;
832 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
833 editor->pCursors[1].nOffset = 0;
834 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
835 editor->pCursors[0].nOffset = 0;
836 break;
837 default: assert(0);
839 /* Store the anchor positions for extending the selection. */
840 editor->pCursors[2] = editor->pCursors[0];
841 editor->pCursors[3] = editor->pCursors[1];
844 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
846 ME_Cursor *pCursor = &editor->pCursors[nCursor];
847 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
848 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
851 /* Helper function for ME_FindPixelPos to find paragraph within tables */
852 static ME_DisplayItem* ME_FindPixelPosInTableRow(int x, int y,
853 ME_DisplayItem *para)
855 ME_DisplayItem *cell, *next_cell;
856 assert(para->member.para.nFlags & MEPF_ROWSTART);
857 cell = para->member.para.next_para->member.para.pCell;
858 assert(cell);
860 /* find the cell we are in */
861 while ((next_cell = cell->member.cell.next_cell) != NULL) {
862 if (x < next_cell->member.cell.pt.x)
864 para = ME_FindItemFwd(cell, diParagraph);
865 /* Found the cell, but there might be multiple paragraphs in
866 * the cell, so need to search down the cell for the paragraph. */
867 while (cell == para->member.para.pCell) {
868 if (y < para->member.para.pt.y + para->member.para.nHeight)
870 if (para->member.para.nFlags & MEPF_ROWSTART)
871 return ME_FindPixelPosInTableRow(x, y, para);
872 else
873 return para;
875 para = para->member.para.next_para;
877 /* Past the end of the cell, so go back to the last cell paragraph */
878 return para->member.para.prev_para;
880 cell = next_cell;
882 /* Return table row delimiter */
883 para = ME_FindItemFwd(cell, diParagraph);
884 assert(para->member.para.nFlags & MEPF_ROWEND);
885 assert(para->member.para.pFmt->dwMask & PFM_TABLEROWDELIMITER);
886 assert(para->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER);
887 return para;
890 static BOOL ME_ReturnFoundPos(ME_TextEditor *editor, ME_DisplayItem *found,
891 ME_Cursor *result, int rx, BOOL isExact)
893 assert(found);
894 assert(found->type == diRun);
895 if ((found->member.run.nFlags & MERF_ENDPARA) || rx < 0)
896 rx = 0;
897 result->pRun = found;
898 result->nOffset = ME_CharFromPointCursor(editor, rx, &found->member.run);
899 if (editor->pCursors[0].nOffset == found->member.run.strText->nLen && rx)
901 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
902 result->nOffset = 0;
904 return isExact;
907 /* Finds the run and offset from the pixel position.
909 * x & y are pixel positions in virtual coordinates into the rich edit control,
910 * so client coordinates must first be adjusted by the scroll position.
912 * returns TRUE if the result was exactly under the cursor, otherwise returns
913 * FALSE, and result is set to the closest position to the coordinates.
915 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
916 ME_Cursor *result, BOOL *is_eol)
918 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
919 ME_DisplayItem *last = NULL;
920 int rx = 0;
921 BOOL isExact = TRUE;
923 x -= editor->rcFormat.left;
924 y -= editor->rcFormat.top;
926 if (is_eol)
927 *is_eol = 0;
929 /* find paragraph */
930 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
932 assert(p->type == diParagraph);
933 if (y < p->member.para.pt.y + p->member.para.nHeight)
935 if (p->member.para.nFlags & MEPF_ROWSTART)
936 p = ME_FindPixelPosInTableRow(x, y, p);
937 y -= p->member.para.pt.y;
938 p = ME_FindItemFwd(p, diStartRow);
939 break;
940 } else if (p->member.para.nFlags & MEPF_ROWSTART) {
941 p = ME_GetTableRowEnd(p);
944 /* find row */
945 for (; p != editor->pBuffer->pLast; )
947 ME_DisplayItem *pp;
948 assert(p->type == diStartRow);
949 if (y < p->member.row.pt.y + p->member.row.nHeight)
951 p = ME_FindItemFwd(p, diRun);
952 break;
954 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
955 if (pp->type != diStartRow)
957 p = ME_FindItemFwd(p, diRun);
958 break;
960 p = pp;
962 if (p == editor->pBuffer->pLast)
964 /* The position is below the last paragraph, so the last row will be used
965 * rather than the end of the text, so the x position will be used to
966 * determine the offset closest to the pixel position. */
967 isExact = FALSE;
968 p = ME_FindItemBack(p, diStartRow);
969 if (p != NULL){
970 p = ME_FindItemFwd(p, diRun);
972 else
974 p = editor->pBuffer->pLast;
977 for (; p != editor->pBuffer->pLast; p = p->next)
979 switch (p->type)
981 case diRun:
982 rx = x - p->member.run.pt.x;
983 if (rx < p->member.run.nWidth)
984 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
985 break;
986 case diStartRow:
987 isExact = FALSE;
988 p = ME_FindItemFwd(p, diRun);
989 if (is_eol) *is_eol = 1;
990 rx = 0; /* FIXME not sure */
991 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
992 case diCell:
993 case diParagraph:
994 case diTextEnd:
995 isExact = FALSE;
996 rx = 0; /* FIXME not sure */
997 p = last;
998 return ME_ReturnFoundPos(editor, p, result, rx, isExact);
999 default: assert(0);
1001 last = p;
1003 result->pRun = ME_FindItemBack(p, diRun);
1004 result->nOffset = 0;
1005 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
1006 return FALSE;
1010 /* Returns the character offset closest to the pixel position
1012 * x & y are pixel positions in client coordinates.
1014 * isExact will be set to TRUE if the run is directly under the pixel
1015 * position, FALSE if it not, unless isExact is set to NULL.
1017 int ME_CharFromPos(ME_TextEditor *editor, int x, int y, BOOL *isExact)
1019 ME_Cursor cursor;
1020 RECT rc;
1021 BOOL bResult;
1023 GetClientRect(editor->hWnd, &rc);
1024 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
1025 if (isExact) *isExact = FALSE;
1026 return -1;
1028 y += ME_GetYScrollPos(editor);
1029 bResult = ME_FindPixelPos(editor, x, y, &cursor, NULL);
1030 if (isExact) *isExact = bResult;
1031 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
1032 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
1037 /* Extends the selection with a word, line, or paragraph selection type.
1039 * The selection is anchored by editor->pCursors[2-3] such that the text
1040 * between the anchors will remain selected, and one end will be extended.
1042 * editor->pCursors[0] should have the position to extend the selection to
1043 * before this function is called.
1045 * Nothing will be done if editor->nSelectionType equals stPosition.
1047 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1049 ME_Cursor tmp_cursor;
1050 int curOfs, anchorStartOfs, anchorEndOfs;
1051 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1052 return;
1053 curOfs = ME_GetCursorOfs(editor, 0);
1054 anchorStartOfs = ME_GetCursorOfs(editor, 3);
1055 anchorEndOfs = ME_GetCursorOfs(editor, 2);
1057 tmp_cursor = editor->pCursors[0];
1058 editor->pCursors[0] = editor->pCursors[2];
1059 editor->pCursors[1] = editor->pCursors[3];
1060 if (curOfs < anchorStartOfs)
1062 /* Extend the left side of selection */
1063 editor->pCursors[1] = tmp_cursor;
1064 if (editor->nSelectionType == stWord)
1065 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1066 else
1068 ME_DisplayItem *pItem;
1069 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1070 diStartRowOrParagraph:diParagraph);
1071 pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
1072 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
1073 editor->pCursors[1].nOffset = 0;
1076 else if (curOfs >= anchorEndOfs)
1078 /* Extend the right side of selection */
1079 editor->pCursors[0] = tmp_cursor;
1080 if (editor->nSelectionType == stWord)
1081 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
1082 else
1084 ME_DisplayItem *pItem;
1085 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1086 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
1087 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
1088 if (pItem->type == diTextEnd)
1089 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
1090 else
1091 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
1092 editor->pCursors[0].nOffset = 0;
1097 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1099 ME_Cursor tmp_cursor;
1100 int is_selection = 0;
1101 BOOL is_shift;
1103 editor->nUDArrowX = -1;
1105 y += ME_GetYScrollPos(editor);
1107 tmp_cursor = editor->pCursors[0];
1108 is_selection = ME_IsSelection(editor);
1109 is_shift = GetKeyState(VK_SHIFT) < 0;
1111 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
1113 if (x >= editor->rcFormat.left || is_shift)
1115 if (clickNum > 1)
1117 editor->pCursors[1] = editor->pCursors[0];
1118 if (is_shift) {
1119 if (x >= editor->rcFormat.left)
1120 ME_SelectByType(editor, stWord);
1121 else
1122 ME_SelectByType(editor, stParagraph);
1123 } else if (clickNum % 2 == 0) {
1124 ME_SelectByType(editor, stWord);
1125 } else {
1126 ME_SelectByType(editor, stParagraph);
1129 else if (!is_shift)
1131 editor->nSelectionType = stPosition;
1132 editor->pCursors[1] = editor->pCursors[0];
1134 else if (!is_selection)
1136 editor->nSelectionType = stPosition;
1137 editor->pCursors[1] = tmp_cursor;
1139 else if (editor->nSelectionType != stPosition)
1141 ME_ExtendAnchorSelection(editor);
1144 else
1146 if (clickNum < 2) {
1147 ME_SelectByType(editor, stLine);
1148 } else if (clickNum % 2 == 0 || is_shift) {
1149 ME_SelectByType(editor, stParagraph);
1150 } else {
1151 ME_SelectByType(editor, stDocument);
1154 ME_InvalidateSelection(editor);
1155 HideCaret(editor->hWnd);
1156 ME_ShowCaret(editor);
1157 ME_ClearTempStyle(editor);
1158 ME_SendSelChange(editor);
1161 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1163 ME_Cursor tmp_cursor;
1165 if (editor->nSelectionType == stDocument)
1166 return;
1167 y += ME_GetYScrollPos(editor);
1169 tmp_cursor = editor->pCursors[0];
1170 /* FIXME: do something with the return value of ME_FindPixelPos */
1171 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
1173 ME_InvalidateSelection(editor);
1174 editor->pCursors[0] = tmp_cursor;
1175 ME_ExtendAnchorSelection(editor);
1177 if (editor->nSelectionType != stPosition &&
1178 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1180 /* The scroll the cursor towards the other end, since it was the one
1181 * extended by ME_ExtendAnchorSelection */
1182 ME_EnsureVisible(editor, editor->pCursors[1].pRun);
1183 } else {
1184 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
1187 ME_InvalidateSelection(editor);
1188 HideCaret(editor->hWnd);
1189 ME_ShowCaret(editor);
1190 ME_SendSelChange(editor);
1193 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
1194 int x, int *pOffset, int *pbCaretAtEnd)
1196 ME_DisplayItem *pNext, *pLastRun;
1197 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
1198 assert(pNext->type == diRun);
1199 pLastRun = pNext;
1200 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
1201 if (pOffset) *pOffset = 0;
1202 do {
1203 int run_x = pNext->member.run.pt.x;
1204 int width = pNext->member.run.nWidth;
1205 if (x < run_x)
1207 return pNext;
1209 if (x >= run_x && x < run_x+width)
1211 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
1212 ME_String *s = pNext->member.run.strText;
1213 if (ch < s->nLen) {
1214 if (pOffset)
1215 *pOffset = ch;
1216 return pNext;
1219 pLastRun = pNext;
1220 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
1221 } while(pNext && pNext->type == diRun);
1223 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
1225 pNext = ME_FindItemFwd(pNext, diRun);
1226 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
1227 return pNext;
1228 } else {
1229 return pLastRun;
1233 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1235 ME_DisplayItem *pRun = pCursor->pRun;
1236 int x;
1238 if (editor->nUDArrowX != -1)
1239 x = editor->nUDArrowX;
1240 else {
1241 if (editor->bCaretAtEnd)
1243 pRun = ME_FindItemBack(pRun, diRun);
1244 assert(pRun);
1245 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1247 else {
1248 x = pRun->member.run.pt.x;
1249 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
1251 editor->nUDArrowX = x;
1253 return x;
1257 static void
1258 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1260 ME_DisplayItem *pRun = pCursor->pRun;
1261 ME_DisplayItem *pItem, *pOldPara, *pNewPara;
1262 int x = ME_GetXForArrow(editor, pCursor);
1264 if (editor->bCaretAtEnd && !pCursor->nOffset)
1265 pRun = ME_FindItemBack(pRun, diRun);
1266 if (!pRun)
1267 return;
1268 pOldPara = ME_GetParagraph(pRun);
1269 if (nRelOfs == -1)
1271 /* start of this row */
1272 pItem = ME_FindItemBack(pRun, diStartRow);
1273 assert(pItem);
1274 /* start of the previous row */
1275 pItem = ME_FindItemBack(pItem, diStartRow);
1276 if (!pItem)
1277 return; /* row not found - ignore */
1278 pNewPara = ME_GetParagraph(pItem);
1279 if (pOldPara->member.para.nFlags & MEPF_ROWEND ||
1280 (pOldPara->member.para.pCell &&
1281 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1283 /* Brought out of a cell */
1284 pNewPara = ME_GetTableRowStart(pOldPara)->member.para.prev_para;
1285 if (pNewPara->type == diTextStart)
1286 return; /* At the top, so don't go anywhere. */
1287 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1289 if (pNewPara->member.para.nFlags & MEPF_ROWEND)
1291 /* Brought into a table row */
1292 ME_Cell *cell = &ME_FindItemBack(pNewPara, diCell)->member.cell;
1293 while (x < cell->pt.x && cell->prev_cell)
1294 cell = &cell->prev_cell->member.cell;
1295 if (cell->next_cell) /* else - we are still at the end of the row */
1296 pItem = ME_FindItemBack(cell->next_cell, diStartRow);
1299 else
1301 /* start of the next row */
1302 pItem = ME_FindItemFwd(pRun, diStartRow);
1303 if (!pItem)
1304 return; /* row not found - ignore */
1305 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1307 pNewPara = ME_GetParagraph(pItem);
1308 if (pOldPara->member.para.nFlags & MEPF_ROWSTART ||
1309 (pOldPara->member.para.pCell &&
1310 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1312 /* Brought out of a cell */
1313 pNewPara = ME_GetTableRowEnd(pOldPara)->member.para.next_para;
1314 if (pNewPara->type == diTextEnd)
1315 return; /* At the bottom, so don't go anywhere. */
1316 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1318 if (pNewPara->member.para.nFlags & MEPF_ROWSTART)
1320 /* Brought into a table row */
1321 ME_DisplayItem *cell = ME_FindItemFwd(pNewPara, diCell);
1322 while (cell->member.cell.next_cell &&
1323 x >= cell->member.cell.next_cell->member.cell.pt.x)
1324 cell = cell->member.cell.next_cell;
1325 pItem = ME_FindItemFwd(cell, diStartRow);
1328 if (!pItem)
1330 /* row not found - ignore */
1331 return;
1333 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1334 assert(pCursor->pRun);
1335 assert(pCursor->pRun->type == diRun);
1339 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1341 ME_DisplayItem *pRun = pCursor->pRun;
1342 ME_DisplayItem *pLast, *p;
1343 int x, y, ys, yd, yp, yprev;
1344 ME_Cursor tmp_curs = *pCursor;
1346 x = ME_GetXForArrow(editor, pCursor);
1347 if (!pCursor->nOffset && editor->bCaretAtEnd)
1348 pRun = ME_FindItemBack(pRun, diRun);
1350 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1351 assert(p->type == diStartRow);
1352 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1353 yprev = ys = y = yp + p->member.row.pt.y;
1354 yd = y - editor->sizeWindow.cy;
1355 pLast = p;
1357 do {
1358 p = ME_FindItemBack(p, diStartRowOrParagraph);
1359 if (!p)
1360 break;
1361 if (p->type == diParagraph) { /* crossing paragraphs */
1362 if (p->member.para.prev_para == NULL)
1363 break;
1364 yp = p->member.para.prev_para->member.para.pt.y;
1365 continue;
1367 y = yp + p->member.row.pt.y;
1368 if (y < yd)
1369 break;
1370 pLast = p;
1371 yprev = y;
1372 } while(1);
1374 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1375 ME_UpdateSelection(editor, &tmp_curs);
1376 if (yprev < editor->sizeWindow.cy)
1378 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
1379 ME_Repaint(editor);
1381 else
1383 ME_ScrollUp(editor, ys-yprev);
1385 assert(pCursor->pRun);
1386 assert(pCursor->pRun->type == diRun);
1389 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1390 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1391 In such a situation, clicking the scrollbar restores its position back to the
1392 normal range (ie. sets it to (doclength-screenheight)). */
1394 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1396 ME_DisplayItem *pRun = pCursor->pRun;
1397 ME_DisplayItem *pLast, *p;
1398 int x, y, ys, yd, yp, yprev;
1399 ME_Cursor tmp_curs = *pCursor;
1401 x = ME_GetXForArrow(editor, pCursor);
1402 if (!pCursor->nOffset && editor->bCaretAtEnd)
1403 pRun = ME_FindItemBack(pRun, diRun);
1405 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1406 assert(p->type == diStartRow);
1407 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1408 yprev = ys = y = yp + p->member.row.pt.y;
1409 yd = y + editor->sizeWindow.cy;
1410 pLast = p;
1412 do {
1413 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1414 if (!p)
1415 break;
1416 if (p->type == diParagraph) {
1417 yp = p->member.para.pt.y;
1418 continue;
1420 y = yp + p->member.row.pt.y;
1421 if (y >= yd)
1422 break;
1423 pLast = p;
1424 yprev = y;
1425 } while(1);
1427 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1428 ME_UpdateSelection(editor, &tmp_curs);
1429 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
1431 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1432 ME_Repaint(editor);
1434 else
1436 ME_ScrollUp(editor,ys-yprev);
1438 assert(pCursor->pRun);
1439 assert(pCursor->pRun->type == diRun);
1442 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1444 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1445 ME_WrapMarkedParagraphs(editor);
1446 if (pRow) {
1447 ME_DisplayItem *pRun;
1448 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1449 pRow = ME_FindItemBack(pRow, diStartRow);
1450 if (!pRow)
1451 return;
1453 pRun = ME_FindItemFwd(pRow, diRun);
1454 if (pRun) {
1455 pCursor->pRun = pRun;
1456 pCursor->nOffset = 0;
1459 editor->bCaretAtEnd = FALSE;
1462 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1464 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1465 if (pRow) {
1466 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1467 if (pRun) {
1468 pCursor->pRun = pRun;
1469 pCursor->nOffset = 0;
1474 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1476 ME_DisplayItem *pRow;
1478 if (editor->bCaretAtEnd && !pCursor->nOffset)
1479 return;
1481 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1482 assert(pRow);
1483 if (pRow->type == diStartRow) {
1484 /* FIXME WTF was I thinking about here ? */
1485 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1486 assert(pRun);
1487 pCursor->pRun = pRun;
1488 pCursor->nOffset = 0;
1489 editor->bCaretAtEnd = 1;
1490 return;
1492 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1493 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1494 pCursor->nOffset = 0;
1495 editor->bCaretAtEnd = FALSE;
1498 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1500 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1501 assert(p);
1502 p = ME_FindItemBack(p, diRun);
1503 assert(p);
1504 assert(p->member.run.nFlags & MERF_ENDPARA);
1505 pCursor->pRun = p;
1506 pCursor->nOffset = 0;
1507 editor->bCaretAtEnd = FALSE;
1510 BOOL ME_IsSelection(ME_TextEditor *editor)
1512 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1515 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1517 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1519 if (cdir*dir>0)
1520 return 0;
1521 else
1522 return 1;
1525 BOOL ME_UpdateSelection(ME_TextEditor *editor, const ME_Cursor *pTempCursor)
1527 ME_Cursor old_anchor = editor->pCursors[1];
1529 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1531 /* any selection was present ? if so, it's no more, repaint ! */
1532 editor->pCursors[1] = editor->pCursors[0];
1533 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1534 return TRUE;
1536 return FALSE;
1538 else
1540 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1542 editor->pCursors[1] = *pTempCursor;
1543 return TRUE;
1547 ME_Repaint(editor);
1548 return TRUE;
1551 void ME_DeleteSelection(ME_TextEditor *editor)
1553 int from, to;
1554 ME_GetSelection(editor, &from, &to);
1555 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1558 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1560 return ME_GetInsertStyle(editor, 0);
1563 void ME_SendSelChange(ME_TextEditor *editor)
1565 SELCHANGE sc;
1567 if (!(editor->nEventMask & ENM_SELCHANGE))
1568 return;
1570 sc.nmhdr.code = EN_SELCHANGE;
1571 ME_GetSelection(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1572 sc.seltyp = SEL_EMPTY;
1573 if (sc.chrg.cpMin != sc.chrg.cpMax)
1574 sc.seltyp |= SEL_TEXT;
1575 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1576 sc.seltyp |= SEL_MULTICHAR;
1577 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1578 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1579 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1580 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1581 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1583 ME_ClearTempStyle(editor);
1585 editor->notified_cr = sc.chrg;
1586 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1590 BOOL
1591 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1593 int nCursor = 0;
1594 ME_Cursor *p = &editor->pCursors[nCursor];
1595 ME_Cursor tmp_curs = *p;
1596 BOOL success = FALSE;
1598 ME_CheckCharOffsets(editor);
1599 switch(nVKey) {
1600 case VK_LEFT:
1601 editor->bCaretAtEnd = 0;
1602 if (ctrl)
1603 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1604 else
1605 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1606 break;
1607 case VK_RIGHT:
1608 editor->bCaretAtEnd = 0;
1609 if (ctrl)
1610 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1611 else
1612 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1613 break;
1614 case VK_UP:
1615 ME_MoveCursorLines(editor, &tmp_curs, -1);
1616 break;
1617 case VK_DOWN:
1618 ME_MoveCursorLines(editor, &tmp_curs, +1);
1619 break;
1620 case VK_PRIOR:
1621 ME_ArrowPageUp(editor, &tmp_curs);
1622 break;
1623 case VK_NEXT:
1624 ME_ArrowPageDown(editor, &tmp_curs);
1625 break;
1626 case VK_HOME: {
1627 if (ctrl)
1628 ME_ArrowCtrlHome(editor, &tmp_curs);
1629 else
1630 ME_ArrowHome(editor, &tmp_curs);
1631 editor->bCaretAtEnd = 0;
1632 break;
1634 case VK_END:
1635 if (ctrl)
1636 ME_ArrowCtrlEnd(editor, &tmp_curs);
1637 else
1638 ME_ArrowEnd(editor, &tmp_curs);
1639 break;
1642 if (!extend)
1643 editor->pCursors[1] = tmp_curs;
1644 *p = tmp_curs;
1646 ME_InvalidateSelection(editor);
1647 ME_Repaint(editor);
1648 HideCaret(editor->hWnd);
1649 ME_EnsureVisible(editor, tmp_curs.pRun);
1650 ME_ShowCaret(editor);
1651 ME_SendSelChange(editor);
1652 return success;