richedit: Protect deletion of cell boundaries when not deleting row.
[wine.git] / dlls / riched20 / caret.c
blob754c75fae2de81db140123987c9942effcd11390
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 && !ME_IsSelection(editor))
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 && !ME_IsSelection(ed))
253 ShowCaret(ed->hWnd);
256 void ME_HideCaret(ME_TextEditor *ed)
258 if(!ed->bHaveFocus || ME_IsSelection(ed))
260 HideCaret(ed->hWnd);
261 DestroyCaret();
265 BOOL ME_InternalDeleteText(ME_TextEditor *editor, int nOfs, int nChars,
266 BOOL bForce)
268 ME_Cursor c;
269 int shift = 0;
270 int totalChars = nChars;
272 if (!bForce)
274 ME_ProtectPartialTableDeletion(editor, nOfs, &nChars);
275 if (nChars == 0)
276 return FALSE;
279 while(nChars > 0)
281 ME_Run *run;
282 ME_CursorFromCharOfs(editor, nOfs, &c);
283 run = &c.pRun->member.run;
284 if (run->nFlags & MERF_ENDPARA) {
285 int eollen = run->nCR + run->nLF;
286 BOOL keepFirstParaFormat;
288 if (!ME_FindItemFwd(c.pRun, diParagraph))
290 return TRUE;
292 keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
293 run->nCharOfs);
294 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun), keepFirstParaFormat);
295 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
296 ME_CheckCharOffsets(editor);
297 nChars -= (eollen < nChars) ? eollen : nChars;
298 continue;
300 else
302 ME_Cursor cursor;
303 int nIntendedChars = nChars;
304 int nCharsToDelete = nChars;
305 int i;
306 int loc = c.nOffset;
308 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
310 cursor = c;
311 ME_StrRelPos(run->strText, loc, &nChars);
312 /* nChars is the number of characters that should be deleted from the
313 FOLLOWING runs (these AFTER cursor.pRun)
314 nCharsToDelete is a number of chars to delete from THIS run */
315 nCharsToDelete -= nChars;
316 shift -= nCharsToDelete;
317 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
318 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
319 debugstr_w(run->strText->szData), run->strText->nLen);
321 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
323 /* undo = reinsert whole run */
324 /* nOfs is a character offset (from the start of the document
325 to the current (deleted) run */
326 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
327 if (pUndo)
328 pUndo->di.member.run.nCharOfs = nOfs;
330 else
332 /* undo = reinsert partial run */
333 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
334 if (pUndo) {
335 ME_DestroyString(pUndo->di.member.run.strText);
336 pUndo->di.member.run.nCharOfs = nOfs;
337 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
340 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
341 TRACE("Shift value: %d\n", shift);
342 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
344 /* update cursors (including c) */
345 for (i=-1; i<editor->nCursors; i++) {
346 ME_Cursor *pThisCur = editor->pCursors + i;
347 if (i == -1) pThisCur = &c;
348 if (pThisCur->pRun == cursor.pRun) {
349 if (pThisCur->nOffset > cursor.nOffset) {
350 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
351 pThisCur->nOffset = cursor.nOffset;
352 else
353 pThisCur->nOffset -= nCharsToDelete;
354 assert(pThisCur->nOffset >= 0);
355 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
357 if (pThisCur->nOffset == ME_StrVLen(run->strText))
359 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
360 assert(pThisCur->pRun->type == diRun);
361 pThisCur->nOffset = 0;
366 /* c = updated data now */
368 if (c.pRun == cursor.pRun)
369 ME_SkipAndPropagateCharOffset(c.pRun, shift);
370 else
371 ME_PropagateCharOffset(c.pRun, shift);
373 if (!ME_StrVLen(cursor.pRun->member.run.strText))
375 TRACE("Removing useless run\n");
376 ME_Remove(cursor.pRun);
377 ME_DestroyDisplayItem(cursor.pRun);
380 shift = 0;
382 ME_CheckCharOffsets(editor);
384 continue;
387 return TRUE;
390 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
392 assert(nCursor>=0 && nCursor<editor->nCursors);
393 /* text operations set modified state */
394 editor->nModifyStep = 1;
395 return ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars,
396 FALSE);
399 static ME_DisplayItem *
400 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
401 const WCHAR *str, int len, ME_Style *style,
402 int flags)
404 ME_Cursor *p = &editor->pCursors[nCursor];
406 editor->bCaretAtEnd = FALSE;
408 assert(p->pRun->type == diRun);
410 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
414 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
416 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
417 ME_DisplayItem *di;
418 WCHAR space = ' ';
420 /* FIXME no no no */
421 if (ME_IsSelection(editor))
422 ME_DeleteSelection(editor);
424 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
425 MERF_GRAPHICS);
426 di->member.run.ole_obj = ALLOC_OBJ(*reo);
427 ME_CopyReObject(di->member.run.ole_obj, reo);
428 ME_SendSelChange(editor);
432 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
434 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
435 ME_DisplayItem *di;
436 WCHAR space = ' ';
438 /* FIXME no no no */
439 if (ME_IsSelection(editor))
440 ME_DeleteSelection(editor);
442 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
443 MERF_ENDROW);
444 ME_SendSelChange(editor);
448 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
449 const WCHAR *str, int len, ME_Style *style)
451 const WCHAR *pos;
452 ME_Cursor *p = NULL;
453 int oldLen;
455 /* FIXME really HERE ? */
456 if (ME_IsSelection(editor))
457 ME_DeleteSelection(editor);
459 /* FIXME: is this too slow? */
460 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
461 oldLen = ME_GetTextLength(editor);
463 /* text operations set modified state */
464 editor->nModifyStep = 1;
466 assert(style);
468 assert(nCursor>=0 && nCursor<editor->nCursors);
469 if (len == -1)
470 len = lstrlenW(str);
472 /* grow the text limit to fit our text */
473 if(editor->nTextLimit < oldLen +len)
474 editor->nTextLimit = oldLen + len;
476 while (len)
478 pos = str;
479 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
480 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
481 pos++;
482 if (pos-str < len && *pos == '\t') { /* handle tabs */
483 WCHAR tab = '\t';
485 if (pos!=str)
486 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
488 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
490 pos++;
491 if(pos-str <= len) {
492 len -= pos - str;
493 str = pos;
494 continue;
497 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
498 if (!editor->bEmulateVersion10 && pos-str < len-2 && pos[0] == '\r' && pos[1] == '\r' && pos[2] == '\n') {
499 WCHAR space = ' ';
501 if (pos!=str)
502 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
504 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
506 pos+=3;
507 if(pos-str <= len) {
508 len -= pos - str;
509 str = pos;
510 continue;
513 if (pos-str < len) { /* handle EOLs */
514 ME_DisplayItem *tp, *end_run;
515 ME_Style *tmp_style;
516 int numCR, numLF;
518 if (pos!=str)
519 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
520 p = &editor->pCursors[nCursor];
521 if (p->nOffset) {
522 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
523 p = &editor->pCursors[nCursor];
525 tmp_style = ME_GetInsertStyle(editor, nCursor);
526 /* ME_SplitParagraph increases style refcount */
528 /* Encode and fill number of CR and LF according to emulation mode */
529 if (editor->bEmulateVersion10) {
530 const WCHAR * tpos;
532 /* We have to find out how many consecutive \r are there, and if there
533 is a \n terminating the run of \r's. */
534 numCR = 0; numLF = 0;
535 tpos = pos;
536 while (tpos-str < len && *tpos == '\r') {
537 tpos++;
538 numCR++;
540 if (tpos-str >= len) {
541 /* Reached end of text without finding anything but '\r' */
542 if (tpos != pos) {
543 pos++;
545 numCR = 1; numLF = 0;
546 } else if (*tpos == '\n') {
547 /* The entire run of \r's plus the one \n is one single line break */
548 pos = tpos + 1;
549 numLF = 1;
550 } else {
551 /* Found some other content past the run of \r's */
552 pos++;
553 numCR = 1; numLF = 0;
555 } else {
556 if(pos-str < len && *pos =='\r')
557 pos++;
558 if(pos-str < len && *pos =='\n')
559 pos++;
560 numCR = 1; numLF = 0;
562 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, numCR, numLF);
563 p->pRun = ME_FindItemFwd(tp, diRun);
564 end_run = ME_FindItemBack(tp, diRun);
565 ME_ReleaseStyle(end_run->member.run.style);
566 end_run->member.run.style = tmp_style;
567 p->nOffset = 0;
569 if(pos-str <= len) {
570 len -= pos - str;
571 str = pos;
572 continue;
575 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
576 len = 0;
581 static BOOL
582 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
584 ME_DisplayItem *pRun = pCursor->pRun;
586 if (nRelOfs == -1)
588 if (!pCursor->nOffset)
590 do {
591 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
592 assert(pRun);
593 switch (pRun->type)
595 case diRun:
596 break;
597 case diParagraph:
598 if (pRun->member.para.prev_para->type == diTextStart)
599 return FALSE;
600 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
601 /* every paragraph ought to have at least one run */
602 assert(pRun && pRun->type == diRun);
603 assert(pRun->member.run.nFlags & MERF_ENDPARA);
604 break;
605 default:
606 assert(pRun->type != diRun && pRun->type != diParagraph);
607 return FALSE;
609 } while (RUN_IS_HIDDEN(&pRun->member.run));
610 pCursor->pRun = pRun;
611 if (pRun->member.run.nFlags & MERF_ENDPARA)
612 pCursor->nOffset = 0;
613 else
614 pCursor->nOffset = pRun->member.run.strText->nLen;
617 if (pCursor->nOffset)
618 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
619 return TRUE;
621 else
623 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
625 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
627 if (new_ofs < pRun->member.run.strText->nLen)
629 pCursor->nOffset = new_ofs;
630 return TRUE;
633 do {
634 pRun = ME_FindItemFwd(pRun, diRun);
635 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
636 if (pRun)
638 pCursor->pRun = pRun;
639 pCursor->nOffset = 0;
640 return TRUE;
643 return FALSE;
647 static BOOL
648 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
650 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
651 int nOffset = cursor->nOffset;
653 if (nRelOfs == -1)
655 /* Backward movement */
656 while (TRUE)
658 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
659 nOffset, WB_MOVEWORDLEFT);
660 if (nOffset)
661 break;
662 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
663 if (pOtherRun->type == diRun)
665 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
666 pOtherRun->member.run.strText->nLen - 1,
667 WB_ISDELIMITER)
668 && !(pRun->member.run.nFlags & MERF_ENDPARA)
669 && !(cursor->pRun == pRun && cursor->nOffset == 0)
670 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
671 WB_ISDELIMITER))
672 break;
673 pRun = pOtherRun;
674 nOffset = pOtherRun->member.run.strText->nLen;
676 else if (pOtherRun->type == diParagraph)
678 if (cursor->pRun == pRun && cursor->nOffset == 0)
680 /* Paragraph breaks are treated as separate words */
681 if (pOtherRun->member.para.prev_para->type == diTextStart)
682 return FALSE;
683 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
685 break;
689 else
691 /* Forward movement */
692 BOOL last_delim = FALSE;
694 while (TRUE)
696 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
697 nOffset, WB_ISDELIMITER))
698 break;
699 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
700 nOffset, WB_MOVEWORDRIGHT);
701 if (nOffset < pRun->member.run.strText->nLen)
702 break;
703 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
704 if (pOtherRun->type == diRun)
706 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
707 nOffset - 1, WB_ISDELIMITER);
708 pRun = pOtherRun;
709 nOffset = 0;
711 else if (pOtherRun->type == diParagraph)
713 if (cursor->pRun == pRun)
714 pRun = ME_FindItemFwd(pOtherRun, diRun);
715 nOffset = 0;
716 break;
718 else /* diTextEnd */
720 if (cursor->pRun == pRun)
721 return FALSE;
722 nOffset = 0;
723 break;
727 cursor->pRun = pRun;
728 cursor->nOffset = nOffset;
729 return TRUE;
733 void
734 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
736 /* pCursor[0] is the end of the selection
737 * pCursor[1] is the start of the selection (or the position selection anchor)
738 * pCursor[2] and [3] are the selection anchors that are backed up
739 * so they are kept when the selection changes for drag selection.
742 editor->nSelectionType = selectionType;
743 switch(selectionType)
745 case stPosition:
746 break;
747 case stWord:
748 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
749 editor->pCursors[1] = editor->pCursors[0];
750 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
751 break;
752 case stLine:
753 case stParagraph:
755 ME_DisplayItem *pItem;
756 ME_DIType fwdSearchType, backSearchType;
757 if (selectionType == stParagraph) {
758 backSearchType = diParagraph;
759 fwdSearchType = diParagraphOrEnd;
760 } else {
761 backSearchType = diStartRow;
762 fwdSearchType = diStartRowOrParagraphOrEnd;
764 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
765 assert(pItem);
766 if (pItem->type == diTextEnd)
767 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
768 else
769 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
770 editor->pCursors[0].nOffset = 0;
772 pItem = ME_FindItemBack(pItem, backSearchType);
773 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
774 editor->pCursors[1].nOffset = 0;
775 break;
777 case stDocument:
778 /* Select everything with cursor anchored from the start of the text */
779 editor->nSelectionType = stDocument;
780 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
781 editor->pCursors[1].nOffset = 0;
782 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
783 editor->pCursors[0].nOffset = 0;
784 break;
785 default: assert(0);
787 /* Store the anchor positions for extending the selection. */
788 editor->pCursors[2] = editor->pCursors[0];
789 editor->pCursors[3] = editor->pCursors[1];
792 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
794 ME_Cursor *pCursor = &editor->pCursors[nCursor];
795 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
796 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
799 /* Finds the run and offset from the pixel position.
801 * x & y are pixel positions in virtual coordinates into the rich edit control,
802 * so client coordinates must first be adjusted by the scroll position.
804 * returns TRUE if the result was exactly under the cursor, otherwise returns
805 * FALSE, and result is set to the closest position to the coordinates.
807 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
808 ME_Cursor *result, BOOL *is_eol)
810 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
811 ME_DisplayItem *last = NULL;
812 int rx = 0;
813 BOOL isExact = TRUE;
815 if (is_eol)
816 *is_eol = 0;
818 /* find paragraph */
819 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
821 assert(p->type == diParagraph);
822 if (y < p->member.para.nYPos + p->member.para.nHeight)
824 y -= p->member.para.nYPos;
825 p = ME_FindItemFwd(p, diStartRow);
826 break;
829 /* find row */
830 for (; p != editor->pBuffer->pLast; )
832 ME_DisplayItem *pp;
833 assert(p->type == diStartRow);
834 if (y < p->member.row.nYPos + p->member.row.nHeight)
836 p = ME_FindItemFwd(p, diRun);
837 break;
839 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
840 if (pp->type != diStartRow)
842 p = ME_FindItemFwd(p, diRun);
843 break;
845 p = pp;
847 if (p == editor->pBuffer->pLast)
849 /* The position is below the last paragraph, so the last row will be used
850 * rather than the end of the text, so the x position will be used to
851 * determine the offset closest to the pixel position. */
852 isExact = FALSE;
853 p = ME_FindItemBack(p, diStartRow);
854 if (p != NULL){
855 p = ME_FindItemFwd(p, diRun);
857 else
859 p = editor->pBuffer->pLast;
862 for (; p != editor->pBuffer->pLast; p = p->next)
864 switch (p->type)
866 case diRun:
867 rx = x - p->member.run.pt.x;
868 if (rx < p->member.run.nWidth)
870 found_here:
871 assert(p->type == diRun);
872 if ((p->member.run.nFlags & MERF_ENDPARA) || rx < 0)
873 rx = 0;
874 result->pRun = p;
875 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
876 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
878 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
879 result->nOffset = 0;
881 return isExact;
883 break;
884 case diStartRow:
885 isExact = FALSE;
886 p = ME_FindItemFwd(p, diRun);
887 if (is_eol) *is_eol = 1;
888 rx = 0; /* FIXME not sure */
889 goto found_here;
890 case diParagraph:
891 case diTextEnd:
892 isExact = FALSE;
893 rx = 0; /* FIXME not sure */
894 p = last;
895 goto found_here;
896 default: assert(0);
898 last = p;
900 result->pRun = ME_FindItemBack(p, diRun);
901 result->nOffset = 0;
902 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
903 return FALSE;
907 /* Returns the character offset closest to the pixel position
909 * x & y are pixel positions in client coordinates.
911 * isExact will be set to TRUE if the run is directly under the pixel
912 * position, FALSE if it not, unless isExact is set to NULL.
914 int ME_CharFromPos(ME_TextEditor *editor, int x, int y, BOOL *isExact)
916 ME_Cursor cursor;
917 RECT rc;
918 BOOL bResult;
920 GetClientRect(editor->hWnd, &rc);
921 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
922 if (isExact) *isExact = FALSE;
923 return -1;
925 y += ME_GetYScrollPos(editor);
926 bResult = ME_FindPixelPos(editor, x, y, &cursor, NULL);
927 if (isExact) *isExact = bResult;
928 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
929 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
934 /* Extends the selection with a word, line, or paragraph selection type.
936 * The selection is anchored by editor->pCursors[2-3] such that the text
937 * between the anchors will remain selected, and one end will be extended.
939 * editor->pCursors[0] should have the position to extend the selection to
940 * before this function is called.
942 * Nothing will be done if editor->nSelectionType equals stPosition.
944 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
946 ME_Cursor tmp_cursor;
947 int curOfs, anchorStartOfs, anchorEndOfs;
948 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
949 return;
950 curOfs = ME_GetCursorOfs(editor, 0);
951 anchorStartOfs = ME_GetCursorOfs(editor, 3);
952 anchorEndOfs = ME_GetCursorOfs(editor, 2);
954 tmp_cursor = editor->pCursors[0];
955 editor->pCursors[0] = editor->pCursors[2];
956 editor->pCursors[1] = editor->pCursors[3];
957 if (curOfs < anchorStartOfs)
959 /* Extend the left side of selection */
960 editor->pCursors[1] = tmp_cursor;
961 if (editor->nSelectionType == stWord)
962 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
963 else
965 ME_DisplayItem *pItem;
966 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
967 diStartRowOrParagraph:diParagraph);
968 pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
969 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
970 editor->pCursors[1].nOffset = 0;
973 else if (curOfs >= anchorEndOfs)
975 /* Extend the right side of selection */
976 editor->pCursors[0] = tmp_cursor;
977 if (editor->nSelectionType == stWord)
978 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
979 else
981 ME_DisplayItem *pItem;
982 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
983 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
984 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
985 if (pItem->type == diTextEnd)
986 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
987 else
988 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
989 editor->pCursors[0].nOffset = 0;
994 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
996 ME_Cursor tmp_cursor;
997 int is_selection = 0;
998 BOOL is_shift;
1000 editor->nUDArrowX = -1;
1002 y += ME_GetYScrollPos(editor);
1004 tmp_cursor = editor->pCursors[0];
1005 is_selection = ME_IsSelection(editor);
1006 is_shift = GetKeyState(VK_SHIFT) < 0;
1008 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
1010 if (x >= editor->selofs || is_shift)
1012 if (clickNum > 1)
1014 editor->pCursors[1] = editor->pCursors[0];
1015 if (is_shift) {
1016 if (x >= editor->selofs)
1017 ME_SelectByType(editor, stWord);
1018 else
1019 ME_SelectByType(editor, stParagraph);
1020 } else if (clickNum % 2 == 0) {
1021 ME_SelectByType(editor, stWord);
1022 } else {
1023 ME_SelectByType(editor, stParagraph);
1026 else if (!is_shift)
1028 editor->nSelectionType = stPosition;
1029 editor->pCursors[1] = editor->pCursors[0];
1031 else if (!is_selection)
1033 editor->nSelectionType = stPosition;
1034 editor->pCursors[1] = tmp_cursor;
1036 else if (editor->nSelectionType != stPosition)
1038 ME_ExtendAnchorSelection(editor);
1041 else
1043 if (clickNum < 2) {
1044 ME_SelectByType(editor, stLine);
1045 } else if (clickNum % 2 == 0 || is_shift) {
1046 ME_SelectByType(editor, stParagraph);
1047 } else {
1048 ME_SelectByType(editor, stDocument);
1051 ME_InvalidateSelection(editor);
1052 HideCaret(editor->hWnd);
1053 ME_ShowCaret(editor);
1054 ME_ClearTempStyle(editor);
1055 ME_SendSelChange(editor);
1058 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1060 ME_Cursor tmp_cursor;
1062 if (editor->nSelectionType == stDocument)
1063 return;
1064 y += ME_GetYScrollPos(editor);
1066 tmp_cursor = editor->pCursors[0];
1067 /* FIXME: do something with the return value of ME_FindPixelPos */
1068 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
1070 ME_InvalidateSelection(editor);
1071 editor->pCursors[0] = tmp_cursor;
1072 ME_ExtendAnchorSelection(editor);
1074 if (editor->nSelectionType != stPosition &&
1075 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1077 /* The scroll the cursor towards the other end, since it was the one
1078 * extended by ME_ExtendAnchorSelection
1080 ME_Cursor tmpCursor = editor->pCursors[0];
1081 editor->pCursors[0] = editor->pCursors[1];
1082 editor->pCursors[1] = tmpCursor;
1083 SendMessageW(editor->hWnd, EM_SCROLLCARET, 0, 0);
1084 editor->pCursors[1] = editor->pCursors[0];
1085 editor->pCursors[0] = tmpCursor;
1086 } else {
1087 SendMessageW(editor->hWnd, EM_SCROLLCARET, 0, 0);
1090 ME_InvalidateSelection(editor);
1091 HideCaret(editor->hWnd);
1092 ME_ShowCaret(editor);
1093 ME_SendSelChange(editor);
1096 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
1097 int x, int *pOffset, int *pbCaretAtEnd)
1099 ME_DisplayItem *pNext, *pLastRun;
1100 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
1101 assert(pNext->type == diRun);
1102 pLastRun = pNext;
1103 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
1104 if (pOffset) *pOffset = 0;
1105 do {
1106 int run_x = pNext->member.run.pt.x;
1107 int width = pNext->member.run.nWidth;
1108 if (x < run_x)
1110 return pNext;
1112 if (x >= run_x && x < run_x+width)
1114 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
1115 ME_String *s = pNext->member.run.strText;
1116 if (ch < s->nLen) {
1117 if (pOffset)
1118 *pOffset = ch;
1119 return pNext;
1122 pLastRun = pNext;
1123 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
1124 } while(pNext && pNext->type == diRun);
1126 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
1128 pNext = ME_FindItemFwd(pNext, diRun);
1129 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
1130 return pNext;
1131 } else {
1132 return pLastRun;
1136 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1138 ME_DisplayItem *pRun = pCursor->pRun;
1139 int x;
1141 if (editor->nUDArrowX != -1)
1142 x = editor->nUDArrowX;
1143 else {
1144 if (editor->bCaretAtEnd)
1146 pRun = ME_FindItemBack(pRun, diRun);
1147 assert(pRun);
1148 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1150 else {
1151 x = pRun->member.run.pt.x;
1152 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
1154 editor->nUDArrowX = x;
1156 return x;
1160 static void
1161 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1163 ME_DisplayItem *pRun = pCursor->pRun;
1164 ME_DisplayItem *pItem;
1165 int x = ME_GetXForArrow(editor, pCursor);
1167 if (editor->bCaretAtEnd && !pCursor->nOffset)
1168 pRun = ME_FindItemBack(pRun, diRun);
1169 if (!pRun)
1170 return;
1171 if (nRelOfs == -1)
1173 /* start of this row */
1174 pItem = ME_FindItemBack(pRun, diStartRow);
1175 assert(pItem);
1176 /* start of the previous row */
1177 pItem = ME_FindItemBack(pItem, diStartRow);
1179 else
1181 /* start of the next row */
1182 pItem = ME_FindItemFwd(pRun, diStartRow);
1183 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1186 if (!pItem)
1188 /* row not found - ignore */
1189 return;
1191 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1192 assert(pCursor->pRun);
1193 assert(pCursor->pRun->type == diRun);
1197 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1199 ME_DisplayItem *pRun = pCursor->pRun;
1200 ME_DisplayItem *pLast, *p;
1201 int x, y, ys, yd, yp, yprev;
1202 ME_Cursor tmp_curs = *pCursor;
1204 x = ME_GetXForArrow(editor, pCursor);
1205 if (!pCursor->nOffset && editor->bCaretAtEnd)
1206 pRun = ME_FindItemBack(pRun, diRun);
1208 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1209 assert(p->type == diStartRow);
1210 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1211 yprev = ys = y = yp + p->member.row.nYPos;
1212 yd = y - editor->sizeWindow.cy;
1213 pLast = p;
1215 do {
1216 p = ME_FindItemBack(p, diStartRowOrParagraph);
1217 if (!p)
1218 break;
1219 if (p->type == diParagraph) { /* crossing paragraphs */
1220 if (p->member.para.prev_para == NULL)
1221 break;
1222 yp = p->member.para.prev_para->member.para.nYPos;
1223 continue;
1225 y = yp + p->member.row.nYPos;
1226 if (y < yd)
1227 break;
1228 pLast = p;
1229 yprev = y;
1230 } while(1);
1232 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1233 ME_UpdateSelection(editor, &tmp_curs);
1234 if (yprev < editor->sizeWindow.cy)
1236 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
1237 ME_Repaint(editor);
1239 else
1241 ME_ScrollUp(editor, ys-yprev);
1243 assert(pCursor->pRun);
1244 assert(pCursor->pRun->type == diRun);
1247 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1248 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1249 In such a situation, clicking the scrollbar restores its position back to the
1250 normal range (ie. sets it to (doclength-screenheight)). */
1252 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1254 ME_DisplayItem *pRun = pCursor->pRun;
1255 ME_DisplayItem *pLast, *p;
1256 int x, y, ys, yd, yp, yprev;
1257 ME_Cursor tmp_curs = *pCursor;
1259 x = ME_GetXForArrow(editor, pCursor);
1260 if (!pCursor->nOffset && editor->bCaretAtEnd)
1261 pRun = ME_FindItemBack(pRun, diRun);
1263 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1264 assert(p->type == diStartRow);
1265 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1266 yprev = ys = y = yp + p->member.row.nYPos;
1267 yd = y + editor->sizeWindow.cy;
1268 pLast = p;
1270 do {
1271 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1272 if (!p)
1273 break;
1274 if (p->type == diParagraph) {
1275 yp = p->member.para.nYPos;
1276 continue;
1278 y = yp + p->member.row.nYPos;
1279 if (y >= yd)
1280 break;
1281 pLast = p;
1282 yprev = y;
1283 } while(1);
1285 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1286 ME_UpdateSelection(editor, &tmp_curs);
1287 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
1289 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1290 ME_Repaint(editor);
1292 else
1294 ME_ScrollUp(editor,ys-yprev);
1296 assert(pCursor->pRun);
1297 assert(pCursor->pRun->type == diRun);
1300 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1302 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1303 ME_WrapMarkedParagraphs(editor);
1304 if (pRow) {
1305 ME_DisplayItem *pRun;
1306 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1307 pRow = ME_FindItemBack(pRow, diStartRow);
1308 if (!pRow)
1309 return;
1311 pRun = ME_FindItemFwd(pRow, diRun);
1312 if (pRun) {
1313 pCursor->pRun = pRun;
1314 pCursor->nOffset = 0;
1317 editor->bCaretAtEnd = FALSE;
1320 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1322 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1323 if (pRow) {
1324 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1325 if (pRun) {
1326 pCursor->pRun = pRun;
1327 pCursor->nOffset = 0;
1332 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1334 ME_DisplayItem *pRow;
1336 if (editor->bCaretAtEnd && !pCursor->nOffset)
1337 return;
1339 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1340 assert(pRow);
1341 if (pRow->type == diStartRow) {
1342 /* FIXME WTF was I thinking about here ? */
1343 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1344 assert(pRun);
1345 pCursor->pRun = pRun;
1346 pCursor->nOffset = 0;
1347 editor->bCaretAtEnd = 1;
1348 return;
1350 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1351 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1352 pCursor->nOffset = 0;
1353 editor->bCaretAtEnd = FALSE;
1356 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1358 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1359 assert(p);
1360 p = ME_FindItemBack(p, diRun);
1361 assert(p);
1362 assert(p->member.run.nFlags & MERF_ENDPARA);
1363 pCursor->pRun = p;
1364 pCursor->nOffset = 0;
1365 editor->bCaretAtEnd = FALSE;
1368 BOOL ME_IsSelection(ME_TextEditor *editor)
1370 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1373 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1375 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1377 if (cdir*dir>0)
1378 return 0;
1379 else
1380 return 1;
1383 BOOL ME_UpdateSelection(ME_TextEditor *editor, const ME_Cursor *pTempCursor)
1385 ME_Cursor old_anchor = editor->pCursors[1];
1387 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1389 /* any selection was present ? if so, it's no more, repaint ! */
1390 editor->pCursors[1] = editor->pCursors[0];
1391 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1392 return TRUE;
1394 return FALSE;
1396 else
1398 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1400 editor->pCursors[1] = *pTempCursor;
1401 return TRUE;
1405 ME_Repaint(editor);
1406 return TRUE;
1409 void ME_DeleteSelection(ME_TextEditor *editor)
1411 int from, to;
1412 ME_GetSelection(editor, &from, &to);
1413 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1416 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1418 return ME_GetInsertStyle(editor, 0);
1421 void ME_SendSelChange(ME_TextEditor *editor)
1423 SELCHANGE sc;
1425 if (!(editor->nEventMask & ENM_SELCHANGE))
1426 return;
1428 sc.nmhdr.hwndFrom = editor->hWnd;
1429 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1430 sc.nmhdr.code = EN_SELCHANGE;
1431 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1432 sc.seltyp = SEL_EMPTY;
1433 if (sc.chrg.cpMin != sc.chrg.cpMax)
1434 sc.seltyp |= SEL_TEXT;
1435 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1436 sc.seltyp |= SEL_MULTICHAR;
1437 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1438 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1439 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1440 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1441 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1443 ME_ClearTempStyle(editor);
1445 editor->notified_cr = sc.chrg;
1446 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1450 BOOL
1451 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1453 int nCursor = 0;
1454 ME_Cursor *p = &editor->pCursors[nCursor];
1455 ME_Cursor tmp_curs = *p;
1456 BOOL success = FALSE;
1458 ME_CheckCharOffsets(editor);
1459 switch(nVKey) {
1460 case VK_LEFT:
1461 editor->bCaretAtEnd = 0;
1462 if (ctrl)
1463 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1464 else
1465 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1466 break;
1467 case VK_RIGHT:
1468 editor->bCaretAtEnd = 0;
1469 if (ctrl)
1470 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1471 else
1472 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1473 break;
1474 case VK_UP:
1475 ME_MoveCursorLines(editor, &tmp_curs, -1);
1476 break;
1477 case VK_DOWN:
1478 ME_MoveCursorLines(editor, &tmp_curs, +1);
1479 break;
1480 case VK_PRIOR:
1481 ME_ArrowPageUp(editor, &tmp_curs);
1482 break;
1483 case VK_NEXT:
1484 ME_ArrowPageDown(editor, &tmp_curs);
1485 break;
1486 case VK_HOME: {
1487 if (ctrl)
1488 ME_ArrowCtrlHome(editor, &tmp_curs);
1489 else
1490 ME_ArrowHome(editor, &tmp_curs);
1491 editor->bCaretAtEnd = 0;
1492 break;
1494 case VK_END:
1495 if (ctrl)
1496 ME_ArrowCtrlEnd(editor, &tmp_curs);
1497 else
1498 ME_ArrowEnd(editor, &tmp_curs);
1499 break;
1502 if (!extend)
1503 editor->pCursors[1] = tmp_curs;
1504 *p = tmp_curs;
1506 ME_InvalidateSelection(editor);
1507 ME_Repaint(editor);
1508 HideCaret(editor->hWnd);
1509 ME_EnsureVisible(editor, tmp_curs.pRun);
1510 ME_ShowCaret(editor);
1511 ME_SendSelChange(editor);
1512 return success;