richedit: Implemented paragraph selection.
[wine.git] / dlls / riched20 / caret.c
blob0be4cf5abdd81df36cf4ccf6a57c4b402ab8d30d
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 = run->member.run.pt.x + sz.cx;
218 *y = para->member.para.nYPos + row->member.row.nBaseline + run->member.run.pt.y - pSizeRun->member.run.nAscent - ME_GetYScrollPos(editor);
219 ME_DestroyContext(&c, editor->hWnd);
220 return;
223 *height = 10; /* FIXME use global font */
224 *x = 0;
225 *y = 0;
229 void
230 ME_MoveCaret(ME_TextEditor *editor)
232 int x, y, height;
234 if (ME_WrapMarkedParagraphs(editor))
235 ME_UpdateScrollBar(editor);
236 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
237 if(editor->bHaveFocus)
239 RECT rect;
241 GetClientRect(editor->hWnd, &rect);
242 x = min(x, rect.right-2);
243 CreateCaret(editor->hWnd, NULL, 0, height);
244 SetCaretPos(x, y);
249 void ME_ShowCaret(ME_TextEditor *ed)
251 ME_MoveCaret(ed);
252 if(ed->bHaveFocus)
253 ShowCaret(ed->hWnd);
256 void ME_HideCaret(ME_TextEditor *ed)
258 if(ed->bHaveFocus)
260 HideCaret(ed->hWnd);
261 DestroyCaret();
265 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
266 int nChars)
268 ME_Cursor c;
269 int shift = 0;
271 while(nChars > 0)
273 ME_Run *run;
274 ME_CursorFromCharOfs(editor, nOfs, &c);
275 run = &c.pRun->member.run;
276 if (run->nFlags & MERF_ENDPARA) {
277 int eollen = run->nCR + run->nLF;
279 if (!ME_FindItemFwd(c.pRun, diParagraph))
281 return;
283 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
284 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
285 ME_CheckCharOffsets(editor);
286 nChars -= (eollen < nChars) ? eollen : nChars;
287 continue;
289 else
291 ME_Cursor cursor;
292 int nIntendedChars = nChars;
293 int nCharsToDelete = nChars;
294 int i;
295 int loc = c.nOffset;
297 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
299 cursor = c;
300 ME_StrRelPos(run->strText, loc, &nChars);
301 /* nChars is the number of characters that should be deleted from the
302 FOLLOWING runs (these AFTER cursor.pRun)
303 nCharsToDelete is a number of chars to delete from THIS run */
304 nCharsToDelete -= nChars;
305 shift -= nCharsToDelete;
306 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
307 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
308 debugstr_w(run->strText->szData), run->strText->nLen);
310 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
312 /* undo = reinsert whole run */
313 /* nOfs is a character offset (from the start of the document
314 to the current (deleted) run */
315 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
316 if (pUndo)
317 pUndo->di.member.run.nCharOfs = nOfs;
319 else
321 /* undo = reinsert partial run */
322 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
323 if (pUndo) {
324 ME_DestroyString(pUndo->di.member.run.strText);
325 pUndo->di.member.run.nCharOfs = nOfs;
326 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
329 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
330 TRACE("Shift value: %d\n", shift);
331 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
333 /* update cursors (including c) */
334 for (i=-1; i<editor->nCursors; i++) {
335 ME_Cursor *pThisCur = editor->pCursors + i;
336 if (i == -1) pThisCur = &c;
337 if (pThisCur->pRun == cursor.pRun) {
338 if (pThisCur->nOffset > cursor.nOffset) {
339 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
340 pThisCur->nOffset = cursor.nOffset;
341 else
342 pThisCur->nOffset -= nCharsToDelete;
343 assert(pThisCur->nOffset >= 0);
344 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
346 if (pThisCur->nOffset == ME_StrVLen(run->strText))
348 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
349 assert(pThisCur->pRun->type == diRun);
350 pThisCur->nOffset = 0;
355 /* c = updated data now */
357 if (c.pRun == cursor.pRun)
358 ME_SkipAndPropagateCharOffset(c.pRun, shift);
359 else
360 ME_PropagateCharOffset(c.pRun, shift);
362 if (!ME_StrVLen(cursor.pRun->member.run.strText))
364 TRACE("Removing useless run\n");
365 ME_Remove(cursor.pRun);
366 ME_DestroyDisplayItem(cursor.pRun);
369 shift = 0;
371 ME_CheckCharOffsets(editor);
373 continue;
378 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
379 int nChars)
381 assert(nCursor>=0 && nCursor<editor->nCursors);
382 /* text operations set modified state */
383 editor->nModifyStep = 1;
384 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
387 static ME_DisplayItem *
388 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
389 const WCHAR *str, int len, ME_Style *style,
390 int flags)
392 ME_Cursor *p = &editor->pCursors[nCursor];
394 editor->bCaretAtEnd = FALSE;
396 assert(p->pRun->type == diRun);
398 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
402 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
404 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
405 ME_DisplayItem *di;
406 WCHAR space = ' ';
408 /* FIXME no no no */
409 if (ME_IsSelection(editor))
410 ME_DeleteSelection(editor);
412 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
413 MERF_GRAPHICS);
414 di->member.run.ole_obj = ALLOC_OBJ(*reo);
415 ME_CopyReObject(di->member.run.ole_obj, reo);
416 ME_SendSelChange(editor);
420 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
422 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
423 ME_DisplayItem *di;
424 WCHAR space = ' ';
426 /* FIXME no no no */
427 if (ME_IsSelection(editor))
428 ME_DeleteSelection(editor);
430 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
431 MERF_ENDROW);
432 ME_SendSelChange(editor);
435 void
436 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
438 WCHAR tab = '\t';
439 ME_DisplayItem *p, *run;
440 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
442 p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
443 MERF_CELL);
444 run = p;
445 while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
447 if (run->member.run.nFlags & MERF_CELL)
449 assert(run->member.run.pCell->next);
450 p->member.run.pCell = run->member.run.pCell->next;
451 return;
454 assert(run->type == diParagraph);
455 assert(run->member.para.bTable);
456 assert(run->member.para.pCells);
457 p->member.run.pCell = run->member.para.pCells;
461 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
462 const WCHAR *str, int len, ME_Style *style)
464 const WCHAR *pos;
465 ME_Cursor *p = NULL;
466 int oldLen;
468 /* FIXME really HERE ? */
469 if (ME_IsSelection(editor))
470 ME_DeleteSelection(editor);
472 /* FIXME: is this too slow? */
473 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
474 oldLen = ME_GetTextLength(editor);
476 /* text operations set modified state */
477 editor->nModifyStep = 1;
479 assert(style);
481 assert(nCursor>=0 && nCursor<editor->nCursors);
482 if (len == -1)
483 len = lstrlenW(str);
485 /* grow the text limit to fit our text */
486 if(editor->nTextLimit < oldLen +len)
487 editor->nTextLimit = oldLen + len;
489 while (len)
491 pos = str;
492 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
493 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
494 pos++;
495 if (pos-str < len && *pos == '\t') { /* handle tabs */
496 WCHAR tab = '\t';
498 if (pos!=str)
499 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
501 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
503 pos++;
504 if(pos-str <= len) {
505 len -= pos - str;
506 str = pos;
507 continue;
510 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
511 if (!editor->bEmulateVersion10 && pos-str < len-2 && pos[0] == '\r' && pos[1] == '\r' && pos[2] == '\n') {
512 WCHAR space = ' ';
514 if (pos!=str)
515 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
517 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
519 pos+=3;
520 if(pos-str <= len) {
521 len -= pos - str;
522 str = pos;
523 continue;
526 if (pos-str < len) { /* handle EOLs */
527 ME_DisplayItem *tp, *end_run;
528 ME_Style *tmp_style;
529 int numCR, numLF;
531 if (pos!=str)
532 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
533 p = &editor->pCursors[nCursor];
534 if (p->nOffset) {
535 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
536 p = &editor->pCursors[nCursor];
538 tmp_style = ME_GetInsertStyle(editor, nCursor);
539 /* ME_SplitParagraph increases style refcount */
541 /* Encode and fill number of CR and LF according to emulation mode */
542 if (editor->bEmulateVersion10) {
543 const WCHAR * tpos;
545 /* We have to find out how many consecutive \r are there, and if there
546 is a \n terminating the run of \r's. */
547 numCR = 0; numLF = 0;
548 tpos = pos;
549 while (tpos-str < len && *tpos == '\r') {
550 tpos++;
551 numCR++;
553 if (tpos-str >= len) {
554 /* Reached end of text without finding anything but '\r' */
555 if (tpos != pos) {
556 pos++;
558 numCR = 1; numLF = 0;
559 } else if (*tpos == '\n') {
560 /* The entire run of \r's plus the one \n is one single line break */
561 pos = tpos + 1;
562 numLF = 1;
563 } else {
564 /* Found some other content past the run of \r's */
565 pos++;
566 numCR = 1; numLF = 0;
568 } else {
569 if(pos-str < len && *pos =='\r')
570 pos++;
571 if(pos-str < len && *pos =='\n')
572 pos++;
573 numCR = 1; numLF = 0;
575 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, numCR, numLF);
576 p->pRun = ME_FindItemFwd(tp, diRun);
577 end_run = ME_FindItemBack(tp, diRun);
578 ME_ReleaseStyle(end_run->member.run.style);
579 end_run->member.run.style = tmp_style;
580 p->nOffset = 0;
582 if(pos-str <= len) {
583 len -= pos - str;
584 str = pos;
585 continue;
588 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
589 len = 0;
594 static BOOL
595 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
597 ME_DisplayItem *pRun = pCursor->pRun;
599 if (nRelOfs == -1)
601 if (!pCursor->nOffset)
603 do {
604 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
605 assert(pRun);
606 switch (pRun->type)
608 case diRun:
609 break;
610 case diParagraph:
611 if (pRun->member.para.prev_para->type == diTextStart)
612 return FALSE;
613 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
614 /* every paragraph ought to have at least one run */
615 assert(pRun && pRun->type == diRun);
616 assert(pRun->member.run.nFlags & MERF_ENDPARA);
617 break;
618 default:
619 assert(pRun->type != diRun && pRun->type != diParagraph);
620 return FALSE;
622 } while (RUN_IS_HIDDEN(&pRun->member.run));
623 pCursor->pRun = pRun;
624 if (pRun->member.run.nFlags & MERF_ENDPARA)
625 pCursor->nOffset = 0;
626 else
627 pCursor->nOffset = pRun->member.run.strText->nLen;
630 if (pCursor->nOffset)
631 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
632 return TRUE;
634 else
636 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
638 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
640 if (new_ofs < pRun->member.run.strText->nLen)
642 pCursor->nOffset = new_ofs;
643 return TRUE;
646 do {
647 pRun = ME_FindItemFwd(pRun, diRun);
648 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
649 if (pRun)
651 pCursor->pRun = pRun;
652 pCursor->nOffset = 0;
653 return TRUE;
656 return FALSE;
660 static BOOL
661 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
663 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
664 int nOffset = cursor->nOffset;
666 if (nRelOfs == -1)
668 /* Backward movement */
669 while (TRUE)
671 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
672 nOffset, WB_MOVEWORDLEFT);
673 if (nOffset)
674 break;
675 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
676 if (pOtherRun->type == diRun)
678 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
679 pOtherRun->member.run.strText->nLen - 1,
680 WB_ISDELIMITER)
681 && !(pRun->member.run.nFlags & MERF_ENDPARA)
682 && !(cursor->pRun == pRun && cursor->nOffset == 0)
683 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
684 WB_ISDELIMITER))
685 break;
686 pRun = pOtherRun;
687 nOffset = pOtherRun->member.run.strText->nLen;
689 else if (pOtherRun->type == diParagraph)
691 if (cursor->pRun == pRun && cursor->nOffset == 0)
693 /* Paragraph breaks are treated as separate words */
694 if (pOtherRun->member.para.prev_para->type == diTextStart)
695 return FALSE;
696 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
698 break;
702 else
704 /* Forward movement */
705 BOOL last_delim = FALSE;
707 while (TRUE)
709 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
710 nOffset, WB_ISDELIMITER))
711 break;
712 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
713 nOffset, WB_MOVEWORDRIGHT);
714 if (nOffset < pRun->member.run.strText->nLen)
715 break;
716 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
717 if (pOtherRun->type == diRun)
719 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
720 nOffset - 1, WB_ISDELIMITER);
721 pRun = pOtherRun;
722 nOffset = 0;
724 else if (pOtherRun->type == diParagraph)
726 if (cursor->pRun == pRun)
727 pRun = ME_FindItemFwd(pOtherRun, diRun);
728 nOffset = 0;
729 break;
731 else /* diTextEnd */
733 if (cursor->pRun == pRun)
734 return FALSE;
735 nOffset = 0;
736 break;
740 cursor->pRun = pRun;
741 cursor->nOffset = nOffset;
742 return TRUE;
746 void
747 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
749 /* pCursor[0] will be the start of the selection
750 * pCursor[1] is the other end of the selection range
751 * pCursor[2] and [3] are the selection anchors that are backed up
752 * so they are kept when the selection changes for drag selection.
755 editor->nSelectionType = selectionType;
756 switch(selectionType)
758 case stPosition:
759 break;
760 case stWord:
761 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
762 editor->pCursors[0] = editor->pCursors[1];
763 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
764 break;
765 case stLine:
766 case stParagraph:
768 ME_DisplayItem *pItem;
769 ME_DIType fwdSearchType, backSearchType;
770 if (selectionType == stParagraph) {
771 backSearchType = diParagraph;
772 fwdSearchType = diParagraphOrEnd;
773 } else {
774 backSearchType = diStartRow;
775 fwdSearchType = diStartRowOrParagraphOrEnd;
777 pItem = ME_FindItemBack(editor->pCursors[0].pRun, backSearchType);
778 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
779 editor->pCursors[0].nOffset = 0;
781 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
782 assert(pItem);
783 if (pItem->type == diTextEnd)
784 editor->pCursors[1].pRun = ME_FindItemBack(pItem, diRun);
785 else
786 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
787 editor->pCursors[1].nOffset = 0;
788 break;
790 default: assert(0);
792 /* Store the anchor positions for extending the selection. */
793 editor->pCursors[2] = editor->pCursors[0];
794 editor->pCursors[3] = editor->pCursors[1];
798 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
800 ME_Cursor *pCursor = &editor->pCursors[nCursor];
801 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
802 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
805 static void ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
807 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
808 ME_DisplayItem *last = NULL;
809 int rx = 0;
811 if (is_eol)
812 *is_eol = 0;
814 /* find paragraph */
815 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
817 assert(p->type == diParagraph);
818 if (y < p->member.para.nYPos + p->member.para.nHeight)
820 y -= p->member.para.nYPos;
821 p = ME_FindItemFwd(p, diStartRow);
822 break;
825 /* find row */
826 for (; p != editor->pBuffer->pLast; )
828 ME_DisplayItem *pp;
829 assert(p->type == diStartRow);
830 if (y < p->member.row.nYPos + p->member.row.nHeight)
832 p = ME_FindItemFwd(p, diRun);
833 break;
835 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
836 if (pp->type != diStartRow)
838 p = ME_FindItemFwd(p, diRun);
839 break;
841 p = pp;
843 if (p == editor->pBuffer->pLast)
845 /* The position is below the last paragraph, so the last row will be used
846 * rather than the end of the text, so the x position will be used to
847 * determine the offset closest to the pixel position. */
848 p = ME_FindItemBack(p, diStartRow);
849 if (p != NULL){
850 p = ME_FindItemFwd(p, diRun);
852 else
854 p = editor->pBuffer->pLast;
857 for (; p != editor->pBuffer->pLast; p = p->next)
859 switch (p->type)
861 case diRun:
862 rx = x - p->member.run.pt.x;
863 if (rx < p->member.run.nWidth)
865 found_here:
866 assert(p->type == diRun);
867 if ((p->member.run.nFlags & MERF_ENDPARA) || rx < 0)
868 rx = 0;
869 result->pRun = p;
870 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
871 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
873 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
874 result->nOffset = 0;
876 return;
878 break;
879 case diStartRow:
880 p = ME_FindItemFwd(p, diRun);
881 if (is_eol) *is_eol = 1;
882 rx = 0; /* FIXME not sure */
883 goto found_here;
884 case diParagraph:
885 case diTextEnd:
886 rx = 0; /* FIXME not sure */
887 p = last;
888 goto found_here;
889 default: assert(0);
891 last = p;
893 result->pRun = ME_FindItemBack(p, diRun);
894 result->nOffset = 0;
895 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
900 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
902 ME_Cursor cursor;
903 RECT rc;
905 GetClientRect(editor->hWnd, &rc);
906 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
907 return -1;
908 y += ME_GetYScrollPos(editor);
909 ME_FindPixelPos(editor, x, y, &cursor, NULL);
910 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
911 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
914 /* Extends the selection with a word, line, or paragraph selection type.
916 * The selection is anchored by editor->pCursors[2-3] such that the text
917 * between the anchors will remain selected, and one end will be extended.
919 * editor->pCursors[0] should have the position to extend the selection to
920 * before this function is called.
922 * Nothing will be done if editor->nSelectionType equals stPosition.
924 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
926 ME_Cursor tmp_cursor;
927 int curOfs, anchorStartOfs, anchorEndOfs;
928 if (editor->nSelectionType == stPosition)
929 return;
930 curOfs = ME_GetCursorOfs(editor, 0);
931 anchorStartOfs = ME_GetCursorOfs(editor, 2);
932 anchorEndOfs = ME_GetCursorOfs(editor, 3);
934 tmp_cursor = editor->pCursors[0];
935 editor->pCursors[0] = editor->pCursors[2];
936 editor->pCursors[1] = editor->pCursors[3];
937 if (curOfs < anchorStartOfs)
939 /* Extend the left side of selection */
940 editor->pCursors[0] = tmp_cursor;
941 if (editor->nSelectionType == stWord)
942 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
943 else
945 ME_DisplayItem *pItem;
946 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
947 diStartRowOrParagraph:diParagraph);
948 pItem = ME_FindItemBack(editor->pCursors[0].pRun, searchType);
949 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
950 editor->pCursors[0].nOffset = 0;
953 else if (curOfs >= anchorEndOfs)
955 /* Extend the right side of selection */
956 editor->pCursors[1] = tmp_cursor;
957 if (editor->nSelectionType == stWord)
958 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
959 else
961 ME_DisplayItem *pItem;
962 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
963 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
964 pItem = ME_FindItemFwd(editor->pCursors[1].pRun, searchType);
965 if (pItem->type == diTextEnd)
966 editor->pCursors[1].pRun = ME_FindItemBack(pItem, diRun);
967 else
968 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
969 editor->pCursors[1].nOffset = 0;
974 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
976 ME_Cursor tmp_cursor;
977 int is_selection = 0;
978 BOOL is_shift;
980 editor->nUDArrowX = -1;
982 y += ME_GetYScrollPos(editor);
984 tmp_cursor = editor->pCursors[0];
985 is_selection = ME_IsSelection(editor);
986 is_shift = GetKeyState(VK_SHIFT) < 0;
988 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
990 if (x >= editor->selofs || is_shift)
992 if (clickNum > 1)
994 editor->pCursors[1] = editor->pCursors[0];
995 if (x >= editor->selofs)
996 ME_SelectByType(editor, stWord);
997 else
998 ME_SelectByType(editor, stParagraph);
1000 else if (!is_shift)
1002 editor->nSelectionType = stPosition;
1003 editor->pCursors[1] = editor->pCursors[0];
1005 else if (!is_selection)
1007 editor->nSelectionType = stPosition;
1008 editor->pCursors[1] = tmp_cursor;
1010 else if (editor->nSelectionType != stPosition)
1012 ME_ExtendAnchorSelection(editor);
1015 else
1017 if (clickNum < 2) {
1018 ME_SelectByType(editor, stLine);
1019 } else {
1020 ME_SelectByType(editor, stParagraph);
1023 ME_InvalidateSelection(editor);
1024 HideCaret(editor->hWnd);
1025 ME_MoveCaret(editor);
1026 ShowCaret(editor->hWnd);
1027 ME_ClearTempStyle(editor);
1028 ME_SendSelChange(editor);
1031 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1033 ME_Cursor tmp_cursor;
1035 y += ME_GetYScrollPos(editor);
1037 tmp_cursor = editor->pCursors[0];
1038 /* FIXME: do something with the return value of ME_FindPixelPos */
1039 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
1041 ME_InvalidateSelection(editor);
1042 editor->pCursors[0] = tmp_cursor;
1043 ME_ExtendAnchorSelection(editor);
1045 if (editor->nSelectionType != stPosition &&
1046 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1048 /* The scroll the cursor towards the other end, since it was the one
1049 * extended by ME_ExtendAnchorSelection
1051 ME_Cursor tmpCursor = editor->pCursors[0];
1052 editor->pCursors[0] = editor->pCursors[1];
1053 editor->pCursors[1] = tmpCursor;
1054 SendMessageW(editor->hWnd, EM_SCROLLCARET, 0, 0);
1055 editor->pCursors[1] = editor->pCursors[0];
1056 editor->pCursors[0] = tmpCursor;
1057 } else {
1058 SendMessageW(editor->hWnd, EM_SCROLLCARET, 0, 0);
1061 HideCaret(editor->hWnd);
1062 ME_MoveCaret(editor);
1063 ME_InvalidateSelection(editor);
1064 ShowCaret(editor->hWnd);
1065 ME_SendSelChange(editor);
1068 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
1069 int x, int *pOffset, int *pbCaretAtEnd)
1071 ME_DisplayItem *pNext, *pLastRun;
1072 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
1073 assert(pNext->type == diRun);
1074 pLastRun = pNext;
1075 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
1076 if (pOffset) *pOffset = 0;
1077 do {
1078 int run_x = pNext->member.run.pt.x;
1079 int width = pNext->member.run.nWidth;
1080 if (x < run_x)
1082 return pNext;
1084 if (x >= run_x && x < run_x+width)
1086 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
1087 ME_String *s = pNext->member.run.strText;
1088 if (ch < s->nLen) {
1089 if (pOffset)
1090 *pOffset = ch;
1091 return pNext;
1094 pLastRun = pNext;
1095 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
1096 } while(pNext && pNext->type == diRun);
1098 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
1100 pNext = ME_FindItemFwd(pNext, diRun);
1101 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
1102 return pNext;
1103 } else {
1104 return pLastRun;
1108 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1110 ME_DisplayItem *pRun = pCursor->pRun;
1111 int x;
1113 if (editor->nUDArrowX != -1)
1114 x = editor->nUDArrowX;
1115 else {
1116 if (editor->bCaretAtEnd)
1118 pRun = ME_FindItemBack(pRun, diRun);
1119 assert(pRun);
1120 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1122 else {
1123 x = pRun->member.run.pt.x;
1124 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
1126 editor->nUDArrowX = x;
1128 return x;
1132 static void
1133 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1135 ME_DisplayItem *pRun = pCursor->pRun;
1136 ME_DisplayItem *pItem;
1137 int x = ME_GetXForArrow(editor, pCursor);
1139 if (editor->bCaretAtEnd && !pCursor->nOffset)
1140 pRun = ME_FindItemBack(pRun, diRun);
1141 if (!pRun)
1142 return;
1143 if (nRelOfs == -1)
1145 /* start of this row */
1146 pItem = ME_FindItemBack(pRun, diStartRow);
1147 assert(pItem);
1148 /* start of the previous row */
1149 pItem = ME_FindItemBack(pItem, diStartRow);
1151 else
1153 /* start of the next row */
1154 pItem = ME_FindItemFwd(pRun, diStartRow);
1155 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1158 if (!pItem)
1160 /* row not found - ignore */
1161 return;
1163 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1164 assert(pCursor->pRun);
1165 assert(pCursor->pRun->type == diRun);
1169 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1171 ME_DisplayItem *pRun = pCursor->pRun;
1172 ME_DisplayItem *pLast, *p;
1173 int x, y, ys, yd, yp, yprev;
1174 ME_Cursor tmp_curs = *pCursor;
1176 x = ME_GetXForArrow(editor, pCursor);
1177 if (!pCursor->nOffset && editor->bCaretAtEnd)
1178 pRun = ME_FindItemBack(pRun, diRun);
1180 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1181 assert(p->type == diStartRow);
1182 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1183 yprev = ys = y = yp + p->member.row.nYPos;
1184 yd = y - editor->sizeWindow.cy;
1185 pLast = p;
1187 do {
1188 p = ME_FindItemBack(p, diStartRowOrParagraph);
1189 if (!p)
1190 break;
1191 if (p->type == diParagraph) { /* crossing paragraphs */
1192 if (p->member.para.prev_para == NULL)
1193 break;
1194 yp = p->member.para.prev_para->member.para.nYPos;
1195 continue;
1197 y = yp + p->member.row.nYPos;
1198 if (y < yd)
1199 break;
1200 pLast = p;
1201 yprev = y;
1202 } while(1);
1204 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1205 ME_UpdateSelection(editor, &tmp_curs);
1206 if (yprev < editor->sizeWindow.cy)
1208 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
1209 ME_Repaint(editor);
1211 else
1213 ME_ScrollUp(editor, ys-yprev);
1215 assert(pCursor->pRun);
1216 assert(pCursor->pRun->type == diRun);
1219 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1220 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1221 In such a situation, clicking the scrollbar restores its position back to the
1222 normal range (ie. sets it to (doclength-screenheight)). */
1224 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1226 ME_DisplayItem *pRun = pCursor->pRun;
1227 ME_DisplayItem *pLast, *p;
1228 int x, y, ys, yd, yp, yprev;
1229 ME_Cursor tmp_curs = *pCursor;
1231 x = ME_GetXForArrow(editor, pCursor);
1232 if (!pCursor->nOffset && editor->bCaretAtEnd)
1233 pRun = ME_FindItemBack(pRun, diRun);
1235 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1236 assert(p->type == diStartRow);
1237 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1238 yprev = ys = y = yp + p->member.row.nYPos;
1239 yd = y + editor->sizeWindow.cy;
1240 pLast = p;
1242 do {
1243 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1244 if (!p)
1245 break;
1246 if (p->type == diParagraph) {
1247 yp = p->member.para.nYPos;
1248 continue;
1250 y = yp + p->member.row.nYPos;
1251 if (y >= yd)
1252 break;
1253 pLast = p;
1254 yprev = y;
1255 } while(1);
1257 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1258 ME_UpdateSelection(editor, &tmp_curs);
1259 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
1261 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1262 ME_Repaint(editor);
1264 else
1266 ME_ScrollUp(editor,ys-yprev);
1268 assert(pCursor->pRun);
1269 assert(pCursor->pRun->type == diRun);
1272 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1274 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1275 ME_WrapMarkedParagraphs(editor);
1276 if (pRow) {
1277 ME_DisplayItem *pRun;
1278 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1279 pRow = ME_FindItemBack(pRow, diStartRow);
1280 if (!pRow)
1281 return;
1283 pRun = ME_FindItemFwd(pRow, diRun);
1284 if (pRun) {
1285 pCursor->pRun = pRun;
1286 pCursor->nOffset = 0;
1289 editor->bCaretAtEnd = FALSE;
1292 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1294 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1295 if (pRow) {
1296 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1297 if (pRun) {
1298 pCursor->pRun = pRun;
1299 pCursor->nOffset = 0;
1304 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1306 ME_DisplayItem *pRow;
1308 if (editor->bCaretAtEnd && !pCursor->nOffset)
1309 return;
1311 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1312 assert(pRow);
1313 if (pRow->type == diStartRow) {
1314 /* FIXME WTF was I thinking about here ? */
1315 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1316 assert(pRun);
1317 pCursor->pRun = pRun;
1318 pCursor->nOffset = 0;
1319 editor->bCaretAtEnd = 1;
1320 return;
1322 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1323 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1324 pCursor->nOffset = 0;
1325 editor->bCaretAtEnd = FALSE;
1328 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1330 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1331 assert(p);
1332 p = ME_FindItemBack(p, diRun);
1333 assert(p);
1334 assert(p->member.run.nFlags & MERF_ENDPARA);
1335 pCursor->pRun = p;
1336 pCursor->nOffset = 0;
1337 editor->bCaretAtEnd = FALSE;
1340 BOOL ME_IsSelection(ME_TextEditor *editor)
1342 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1345 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1347 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1349 if (cdir*dir>0)
1350 return 0;
1351 else
1352 return 1;
1355 BOOL ME_UpdateSelection(ME_TextEditor *editor, const ME_Cursor *pTempCursor)
1357 ME_Cursor old_anchor = editor->pCursors[1];
1359 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1361 /* any selection was present ? if so, it's no more, repaint ! */
1362 editor->pCursors[1] = editor->pCursors[0];
1363 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1364 return TRUE;
1366 return FALSE;
1368 else
1370 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1372 editor->pCursors[1] = *pTempCursor;
1373 return TRUE;
1377 ME_Repaint(editor);
1378 return TRUE;
1381 void ME_DeleteSelection(ME_TextEditor *editor)
1383 int from, to;
1384 ME_GetSelection(editor, &from, &to);
1385 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1388 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1390 return ME_GetInsertStyle(editor, 0);
1393 void ME_SendSelChange(ME_TextEditor *editor)
1395 SELCHANGE sc;
1397 if (!(editor->nEventMask & ENM_SELCHANGE))
1398 return;
1400 sc.nmhdr.hwndFrom = editor->hWnd;
1401 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1402 sc.nmhdr.code = EN_SELCHANGE;
1403 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1404 sc.seltyp = SEL_EMPTY;
1405 if (sc.chrg.cpMin != sc.chrg.cpMax)
1406 sc.seltyp |= SEL_TEXT;
1407 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1408 sc.seltyp |= SEL_MULTICHAR;
1409 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1410 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1411 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1412 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1413 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1415 ME_ClearTempStyle(editor);
1417 editor->notified_cr = sc.chrg;
1418 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1422 BOOL
1423 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1425 int nCursor = 0;
1426 ME_Cursor *p = &editor->pCursors[nCursor];
1427 ME_Cursor tmp_curs = *p;
1428 BOOL success = FALSE;
1430 ME_CheckCharOffsets(editor);
1431 switch(nVKey) {
1432 case VK_LEFT:
1433 editor->bCaretAtEnd = 0;
1434 if (ctrl)
1435 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1436 else
1437 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1438 break;
1439 case VK_RIGHT:
1440 editor->bCaretAtEnd = 0;
1441 if (ctrl)
1442 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1443 else
1444 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1445 break;
1446 case VK_UP:
1447 ME_MoveCursorLines(editor, &tmp_curs, -1);
1448 break;
1449 case VK_DOWN:
1450 ME_MoveCursorLines(editor, &tmp_curs, +1);
1451 break;
1452 case VK_PRIOR:
1453 ME_ArrowPageUp(editor, &tmp_curs);
1454 break;
1455 case VK_NEXT:
1456 ME_ArrowPageDown(editor, &tmp_curs);
1457 break;
1458 case VK_HOME: {
1459 if (ctrl)
1460 ME_ArrowCtrlHome(editor, &tmp_curs);
1461 else
1462 ME_ArrowHome(editor, &tmp_curs);
1463 editor->bCaretAtEnd = 0;
1464 break;
1466 case VK_END:
1467 if (ctrl)
1468 ME_ArrowCtrlEnd(editor, &tmp_curs);
1469 else
1470 ME_ArrowEnd(editor, &tmp_curs);
1471 break;
1474 if (!extend)
1475 editor->pCursors[1] = tmp_curs;
1476 *p = tmp_curs;
1478 ME_InvalidateSelection(editor);
1479 ME_Repaint(editor);
1480 HideCaret(editor->hWnd);
1481 ME_EnsureVisible(editor, tmp_curs.pRun);
1482 ME_ShowCaret(editor);
1483 ME_SendSelChange(editor);
1484 return success;