richedit: Caret size must match font size characters to be inserted.
[wine.git] / dlls / riched20 / caret.c
bloba3a3d61a1cafe5ac907cc0e6b211ca74553eff30
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 CreateCaret(editor->hWnd, NULL, 0, height);
240 SetCaretPos(x, y);
245 void ME_ShowCaret(ME_TextEditor *ed)
247 ME_MoveCaret(ed);
248 if(ed->bHaveFocus)
249 ShowCaret(ed->hWnd);
252 void ME_HideCaret(ME_TextEditor *ed)
254 if(ed->bHaveFocus)
256 HideCaret(ed->hWnd);
257 DestroyCaret();
261 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
262 int nChars)
264 ME_Cursor c;
265 int shift = 0;
267 while(nChars > 0)
269 ME_Run *run;
270 ME_CursorFromCharOfs(editor, nOfs, &c);
271 run = &c.pRun->member.run;
272 if (run->nFlags & MERF_ENDPARA) {
273 int eollen = run->nCR + run->nLF;
275 if (!ME_FindItemFwd(c.pRun, diParagraph))
277 return;
279 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
280 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
281 ME_CheckCharOffsets(editor);
282 nChars -= (eollen < nChars) ? eollen : nChars;
283 continue;
285 else
287 ME_Cursor cursor;
288 int nIntendedChars = nChars;
289 int nCharsToDelete = nChars;
290 int i;
291 int loc = c.nOffset;
293 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
295 cursor = c;
296 ME_StrRelPos(run->strText, loc, &nChars);
297 /* nChars is the number of characters that should be deleted from the
298 FOLLOWING runs (these AFTER cursor.pRun)
299 nCharsToDelete is a number of chars to delete from THIS run */
300 nCharsToDelete -= nChars;
301 shift -= nCharsToDelete;
302 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
303 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
304 debugstr_w(run->strText->szData), run->strText->nLen);
306 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
308 /* undo = reinsert whole run */
309 /* nOfs is a character offset (from the start of the document
310 to the current (deleted) run */
311 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
312 if (pUndo)
313 pUndo->di.member.run.nCharOfs = nOfs;
315 else
317 /* undo = reinsert partial run */
318 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
319 if (pUndo) {
320 ME_DestroyString(pUndo->di.member.run.strText);
321 pUndo->di.member.run.nCharOfs = nOfs;
322 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
325 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
326 TRACE("Shift value: %d\n", shift);
327 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
329 /* update cursors (including c) */
330 for (i=-1; i<editor->nCursors; i++) {
331 ME_Cursor *pThisCur = editor->pCursors + i;
332 if (i == -1) pThisCur = &c;
333 if (pThisCur->pRun == cursor.pRun) {
334 if (pThisCur->nOffset > cursor.nOffset) {
335 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
336 pThisCur->nOffset = cursor.nOffset;
337 else
338 pThisCur->nOffset -= nCharsToDelete;
339 assert(pThisCur->nOffset >= 0);
340 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
342 if (pThisCur->nOffset == ME_StrVLen(run->strText))
344 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
345 assert(pThisCur->pRun->type == diRun);
346 pThisCur->nOffset = 0;
351 /* c = updated data now */
353 if (c.pRun == cursor.pRun)
354 ME_SkipAndPropagateCharOffset(c.pRun, shift);
355 else
356 ME_PropagateCharOffset(c.pRun, shift);
358 if (!ME_StrVLen(cursor.pRun->member.run.strText))
360 TRACE("Removing useless run\n");
361 ME_Remove(cursor.pRun);
362 ME_DestroyDisplayItem(cursor.pRun);
365 shift = 0;
367 ME_CheckCharOffsets(editor);
369 continue;
374 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
375 int nChars)
377 assert(nCursor>=0 && nCursor<editor->nCursors);
378 /* text operations set modified state */
379 editor->nModifyStep = 1;
380 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
383 static ME_DisplayItem *
384 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
385 const WCHAR *str, int len, ME_Style *style,
386 int flags)
388 ME_Cursor *p = &editor->pCursors[nCursor];
390 editor->bCaretAtEnd = FALSE;
392 assert(p->pRun->type == diRun);
394 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
398 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
400 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
401 ME_DisplayItem *di;
402 WCHAR space = ' ';
404 /* FIXME no no no */
405 if (ME_IsSelection(editor))
406 ME_DeleteSelection(editor);
408 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
409 MERF_GRAPHICS);
410 di->member.run.ole_obj = ALLOC_OBJ(*reo);
411 ME_CopyReObject(di->member.run.ole_obj, reo);
412 ME_SendSelChange(editor);
416 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
418 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
419 ME_DisplayItem *di;
420 WCHAR space = ' ';
422 /* FIXME no no no */
423 if (ME_IsSelection(editor))
424 ME_DeleteSelection(editor);
426 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
427 MERF_ENDROW);
428 ME_SendSelChange(editor);
431 void
432 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
434 WCHAR tab = '\t';
435 ME_DisplayItem *p, *run;
436 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
438 p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
439 MERF_CELL);
440 run = p;
441 while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
443 if (run->member.run.nFlags & MERF_CELL)
445 assert(run->member.run.pCell->next);
446 p->member.run.pCell = run->member.run.pCell->next;
447 return;
450 assert(run->type == diParagraph);
451 assert(run->member.para.bTable);
452 assert(run->member.para.pCells);
453 p->member.run.pCell = run->member.para.pCells;
457 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
458 const WCHAR *str, int len, ME_Style *style)
460 const WCHAR *pos;
461 ME_Cursor *p = NULL;
462 int oldLen;
464 /* FIXME really HERE ? */
465 if (ME_IsSelection(editor))
466 ME_DeleteSelection(editor);
468 /* FIXME: is this too slow? */
469 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
470 oldLen = ME_GetTextLength(editor);
472 /* text operations set modified state */
473 editor->nModifyStep = 1;
475 assert(style);
477 assert(nCursor>=0 && nCursor<editor->nCursors);
478 if (len == -1)
479 len = lstrlenW(str);
481 /* grow the text limit to fit our text */
482 if(editor->nTextLimit < oldLen +len)
483 editor->nTextLimit = oldLen + len;
485 while (len)
487 pos = str;
488 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
489 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
490 pos++;
491 if (pos-str < len && *pos == '\t') { /* handle tabs */
492 WCHAR tab = '\t';
494 if (pos!=str)
495 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
497 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
499 pos++;
500 if(pos-str <= len) {
501 len -= pos - str;
502 str = pos;
503 continue;
506 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
507 if (!editor->bEmulateVersion10 && pos-str < len-2 && pos[0] == '\r' && pos[1] == '\r' && pos[2] == '\n') {
508 WCHAR space = ' ';
510 if (pos!=str)
511 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
513 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
515 pos+=3;
516 if(pos-str <= len) {
517 len -= pos - str;
518 str = pos;
519 continue;
522 if (pos-str < len) { /* handle EOLs */
523 ME_DisplayItem *tp, *end_run;
524 ME_Style *tmp_style;
525 int numCR, numLF;
527 if (pos!=str)
528 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
529 p = &editor->pCursors[nCursor];
530 if (p->nOffset) {
531 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
532 p = &editor->pCursors[nCursor];
534 tmp_style = ME_GetInsertStyle(editor, nCursor);
535 /* ME_SplitParagraph increases style refcount */
537 /* Encode and fill number of CR and LF according to emulation mode */
538 if (editor->bEmulateVersion10) {
539 const WCHAR * tpos;
541 /* We have to find out how many consecutive \r are there, and if there
542 is a \n terminating the run of \r's. */
543 numCR = 0; numLF = 0;
544 tpos = pos;
545 while (tpos-str < len && *tpos == '\r') {
546 tpos++;
547 numCR++;
549 if (tpos-str >= len) {
550 /* Reached end of text without finding anything but '\r' */
551 if (tpos != pos) {
552 pos++;
554 numCR = 1; numLF = 0;
555 } else if (*tpos == '\n') {
556 /* The entire run of \r's plus the one \n is one single line break */
557 pos = tpos + 1;
558 numLF = 1;
559 } else {
560 /* Found some other content past the run of \r's */
561 pos++;
562 numCR = 1; numLF = 0;
564 } else {
565 if(pos-str < len && *pos =='\r')
566 pos++;
567 if(pos-str < len && *pos =='\n')
568 pos++;
569 numCR = 1; numLF = 0;
571 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, numCR, numLF);
572 p->pRun = ME_FindItemFwd(tp, diRun);
573 end_run = ME_FindItemBack(tp, diRun);
574 ME_ReleaseStyle(end_run->member.run.style);
575 end_run->member.run.style = tmp_style;
576 p->nOffset = 0;
578 if(pos-str <= len) {
579 len -= pos - str;
580 str = pos;
581 continue;
584 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
585 len = 0;
590 static BOOL
591 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
593 ME_DisplayItem *pRun = pCursor->pRun;
595 if (nRelOfs == -1)
597 if (!pCursor->nOffset)
599 do {
600 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
601 assert(pRun);
602 switch (pRun->type)
604 case diRun:
605 break;
606 case diParagraph:
607 if (pRun->member.para.prev_para->type == diTextStart)
608 return FALSE;
609 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
610 /* every paragraph ought to have at least one run */
611 assert(pRun && pRun->type == diRun);
612 assert(pRun->member.run.nFlags & MERF_ENDPARA);
613 break;
614 default:
615 assert(pRun->type != diRun && pRun->type != diParagraph);
616 return FALSE;
618 } while (RUN_IS_HIDDEN(&pRun->member.run));
619 pCursor->pRun = pRun;
620 if (pRun->member.run.nFlags & MERF_ENDPARA)
621 pCursor->nOffset = 0;
622 else
623 pCursor->nOffset = pRun->member.run.strText->nLen;
626 if (pCursor->nOffset)
627 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
628 return TRUE;
630 else
632 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
634 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
636 if (new_ofs < pRun->member.run.strText->nLen)
638 pCursor->nOffset = new_ofs;
639 return TRUE;
642 do {
643 pRun = ME_FindItemFwd(pRun, diRun);
644 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
645 if (pRun)
647 pCursor->pRun = pRun;
648 pCursor->nOffset = 0;
649 return TRUE;
652 return FALSE;
656 static BOOL
657 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
659 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
660 int nOffset = cursor->nOffset;
662 if (nRelOfs == -1)
664 /* Backward movement */
665 while (TRUE)
667 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
668 nOffset, WB_MOVEWORDLEFT);
669 if (nOffset)
670 break;
671 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
672 if (pOtherRun->type == diRun)
674 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
675 pOtherRun->member.run.strText->nLen - 1,
676 WB_ISDELIMITER)
677 && !(pRun->member.run.nFlags & MERF_ENDPARA)
678 && !(cursor->pRun == pRun && cursor->nOffset == 0)
679 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
680 WB_ISDELIMITER))
681 break;
682 pRun = pOtherRun;
683 nOffset = pOtherRun->member.run.strText->nLen;
685 else if (pOtherRun->type == diParagraph)
687 if (cursor->pRun == pRun && cursor->nOffset == 0)
689 /* Paragraph breaks are treated as separate words */
690 if (pOtherRun->member.para.prev_para->type == diTextStart)
691 return FALSE;
692 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
694 break;
698 else
700 /* Forward movement */
701 BOOL last_delim = FALSE;
703 while (TRUE)
705 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
706 nOffset, WB_ISDELIMITER))
707 break;
708 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
709 nOffset, WB_MOVEWORDRIGHT);
710 if (nOffset < pRun->member.run.strText->nLen)
711 break;
712 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
713 if (pOtherRun->type == diRun)
715 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
716 nOffset - 1, WB_ISDELIMITER);
717 pRun = pOtherRun;
718 nOffset = 0;
720 else if (pOtherRun->type == diParagraph)
722 if (cursor->pRun == pRun)
723 pRun = ME_FindItemFwd(pOtherRun, diRun);
724 nOffset = 0;
725 break;
727 else /* diTextEnd */
729 if (cursor->pRun == pRun)
730 return FALSE;
731 nOffset = 0;
732 break;
736 cursor->pRun = pRun;
737 cursor->nOffset = nOffset;
738 return TRUE;
742 void
743 ME_SelectWord(ME_TextEditor *editor)
745 if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
746 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
747 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
748 ME_InvalidateSelection(editor);
749 ME_SendSelChange(editor);
753 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
755 ME_Cursor *pCursor = &editor->pCursors[nCursor];
756 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
757 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
760 static void ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
762 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
763 ME_DisplayItem *last = NULL;
764 int rx = 0;
766 if (is_eol)
767 *is_eol = 0;
769 /* find paragraph */
770 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
772 assert(p->type == diParagraph);
773 if (y < p->member.para.nYPos + p->member.para.nHeight)
775 y -= p->member.para.nYPos;
776 p = ME_FindItemFwd(p, diStartRow);
777 break;
780 /* find row */
781 for (; p != editor->pBuffer->pLast; )
783 ME_DisplayItem *pp;
784 assert(p->type == diStartRow);
785 if (y < p->member.row.nYPos + p->member.row.nHeight)
787 p = ME_FindItemFwd(p, diRun);
788 break;
790 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
791 if (pp->type != diStartRow)
793 p = ME_FindItemFwd(p, diRun);
794 break;
796 p = pp;
798 for (; p != editor->pBuffer->pLast; p = p->next)
800 switch (p->type)
802 case diRun:
803 rx = x - p->member.run.pt.x;
804 if (rx < p->member.run.nWidth)
806 found_here:
807 assert(p->type == diRun);
808 if ((p->member.run.nFlags & MERF_ENDPARA) || rx < 0)
809 rx = 0;
810 result->pRun = p;
811 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
812 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
814 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
815 result->nOffset = 0;
817 return;
819 break;
820 case diStartRow:
821 p = ME_FindItemFwd(p, diRun);
822 if (is_eol) *is_eol = 1;
823 rx = 0; /* FIXME not sure */
824 goto found_here;
825 case diParagraph:
826 case diTextEnd:
827 rx = 0; /* FIXME not sure */
828 p = last;
829 goto found_here;
830 default: assert(0);
832 last = p;
834 result->pRun = ME_FindItemBack(p, diRun);
835 result->nOffset = 0;
836 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
841 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
843 ME_Cursor cursor;
844 RECT rc;
846 GetClientRect(editor->hWnd, &rc);
847 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
848 return -1;
849 y += ME_GetYScrollPos(editor);
850 ME_FindPixelPos(editor, x, y, &cursor, NULL);
851 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
852 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
856 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
858 ME_Cursor tmp_cursor;
859 int is_selection = 0;
861 editor->nUDArrowX = -1;
863 y += ME_GetYScrollPos(editor);
865 tmp_cursor = editor->pCursors[0];
866 is_selection = ME_IsSelection(editor);
868 if (x >= editor->selofs)
870 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
871 if (GetKeyState(VK_SHIFT)>=0)
873 editor->pCursors[1] = editor->pCursors[0];
875 else if (!is_selection) {
876 editor->pCursors[1] = tmp_cursor;
877 is_selection = 1;
880 ME_InvalidateSelection(editor);
881 HideCaret(editor->hWnd);
882 ME_MoveCaret(editor);
883 ShowCaret(editor->hWnd);
884 ME_ClearTempStyle(editor);
885 ME_SendSelChange(editor);
887 else
889 ME_DisplayItem *pRow;
891 editor->linesel = 1;
892 editor->sely = y;
893 /* Set pCursors[0] to beginning of line */
894 ME_FindPixelPos(editor, x, y, &editor->pCursors[1], &editor->bCaretAtEnd);
895 /* Set pCursors[1] to end of line */
896 pRow = ME_FindItemFwd(editor->pCursors[1].pRun, diStartRowOrParagraphOrEnd);
897 assert(pRow);
898 /* pCursor[0] is the position where the cursor will be drawn,
899 * pCursor[1] is the other end of the selection range
900 * pCursor[2] and [3] are backups of [0] and [1] so I
901 * don't have to look them up again
904 if (pRow->type == diStartRow) {
905 /* FIXME WTF was I thinking about here ? */
906 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
907 assert(pRun);
908 editor->pCursors[0].pRun = pRun;
909 editor->pCursors[0].nOffset = 0;
910 editor->bCaretAtEnd = 1;
911 } else {
912 editor->pCursors[0].pRun = ME_FindItemBack(pRow, diRun);
913 assert(editor->pCursors[0].pRun && editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA);
914 editor->pCursors[0].nOffset = 0;
915 editor->bCaretAtEnd = 0;
917 editor->pCursors[2] = editor->pCursors[0];
918 editor->pCursors[3] = editor->pCursors[1];
919 ME_InvalidateSelection(editor);
920 HideCaret(editor->hWnd);
921 ME_MoveCaret(editor);
922 ShowCaret(editor->hWnd);
923 ME_ClearTempStyle(editor);
924 ME_SendSelChange(editor);
928 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
930 ME_Cursor tmp_cursor;
932 y += ME_GetYScrollPos(editor);
934 tmp_cursor = editor->pCursors[0];
935 /* FIXME: do something with the return value of ME_FindPixelPos */
936 if (!editor->linesel)
937 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
938 else ME_FindPixelPos(editor, (y > editor->sely) * editor->rcFormat.right, y, &tmp_cursor, &editor->bCaretAtEnd);
940 if (!memcmp(&tmp_cursor, editor->pCursors, sizeof(tmp_cursor)))
941 return;
943 ME_InvalidateSelection(editor);
944 if (!editor->linesel)
945 editor->pCursors[0] = tmp_cursor;
946 else if (!memcmp(&tmp_cursor, editor->pCursors+2, sizeof(tmp_cursor)) ||
947 !memcmp(&tmp_cursor, editor->pCursors+3, sizeof(tmp_cursor)))
949 editor->pCursors[0] = editor->pCursors[2];
950 editor->pCursors[1] = editor->pCursors[3];
952 else if (y < editor->sely)
954 editor->pCursors[0] = tmp_cursor;
955 editor->pCursors[1] = editor->pCursors[2];
957 else
959 editor->pCursors[0] = tmp_cursor;
960 editor->pCursors[1] = editor->pCursors[3];
963 HideCaret(editor->hWnd);
964 ME_MoveCaret(editor);
965 ME_InvalidateSelection(editor);
966 ShowCaret(editor->hWnd);
967 ME_SendSelChange(editor);
968 SendMessageW(editor->hWnd, EM_SCROLLCARET, 0, 0);
971 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
972 int x, int *pOffset, int *pbCaretAtEnd)
974 ME_DisplayItem *pNext, *pLastRun;
975 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
976 assert(pNext->type == diRun);
977 pLastRun = pNext;
978 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
979 if (pOffset) *pOffset = 0;
980 do {
981 int run_x = pNext->member.run.pt.x;
982 int width = pNext->member.run.nWidth;
983 if (x < run_x)
985 return pNext;
987 if (x >= run_x && x < run_x+width)
989 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
990 ME_String *s = pNext->member.run.strText;
991 if (ch < s->nLen) {
992 if (pOffset)
993 *pOffset = ch;
994 return pNext;
997 pLastRun = pNext;
998 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
999 } while(pNext && pNext->type == diRun);
1001 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
1003 pNext = ME_FindItemFwd(pNext, diRun);
1004 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
1005 return pNext;
1006 } else {
1007 return pLastRun;
1011 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1013 ME_DisplayItem *pRun = pCursor->pRun;
1014 int x;
1016 if (editor->nUDArrowX != -1)
1017 x = editor->nUDArrowX;
1018 else {
1019 if (editor->bCaretAtEnd)
1021 pRun = ME_FindItemBack(pRun, diRun);
1022 assert(pRun);
1023 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1025 else {
1026 x = pRun->member.run.pt.x;
1027 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
1029 editor->nUDArrowX = x;
1031 return x;
1035 static void
1036 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1038 ME_DisplayItem *pRun = pCursor->pRun;
1039 ME_DisplayItem *pItem;
1040 int x = ME_GetXForArrow(editor, pCursor);
1042 if (editor->bCaretAtEnd && !pCursor->nOffset)
1043 pRun = ME_FindItemBack(pRun, diRun);
1044 if (!pRun)
1045 return;
1046 if (nRelOfs == -1)
1048 /* start of this row */
1049 pItem = ME_FindItemBack(pRun, diStartRow);
1050 assert(pItem);
1051 /* start of the previous row */
1052 pItem = ME_FindItemBack(pItem, diStartRow);
1054 else
1056 /* start of the next row */
1057 pItem = ME_FindItemFwd(pRun, diStartRow);
1058 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1061 if (!pItem)
1063 /* row not found - ignore */
1064 return;
1066 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1067 assert(pCursor->pRun);
1068 assert(pCursor->pRun->type == diRun);
1072 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1074 ME_DisplayItem *pRun = pCursor->pRun;
1075 ME_DisplayItem *pLast, *p;
1076 int x, y, ys, yd, yp, yprev;
1077 ME_Cursor tmp_curs = *pCursor;
1079 x = ME_GetXForArrow(editor, pCursor);
1080 if (!pCursor->nOffset && editor->bCaretAtEnd)
1081 pRun = ME_FindItemBack(pRun, diRun);
1083 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1084 assert(p->type == diStartRow);
1085 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1086 yprev = ys = y = yp + p->member.row.nYPos;
1087 yd = y - editor->sizeWindow.cy;
1088 pLast = p;
1090 do {
1091 p = ME_FindItemBack(p, diStartRowOrParagraph);
1092 if (!p)
1093 break;
1094 if (p->type == diParagraph) { /* crossing paragraphs */
1095 if (p->member.para.prev_para == NULL)
1096 break;
1097 yp = p->member.para.prev_para->member.para.nYPos;
1098 continue;
1100 y = yp + p->member.row.nYPos;
1101 if (y < yd)
1102 break;
1103 pLast = p;
1104 yprev = y;
1105 } while(1);
1107 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1108 ME_UpdateSelection(editor, &tmp_curs);
1109 if (yprev < editor->sizeWindow.cy)
1111 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
1112 ME_Repaint(editor);
1114 else
1116 ME_ScrollUp(editor, ys-yprev);
1118 assert(pCursor->pRun);
1119 assert(pCursor->pRun->type == diRun);
1122 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1123 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1124 In such a situation, clicking the scrollbar restores its position back to the
1125 normal range (ie. sets it to (doclength-screenheight)). */
1127 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1129 ME_DisplayItem *pRun = pCursor->pRun;
1130 ME_DisplayItem *pLast, *p;
1131 int x, y, ys, yd, yp, yprev;
1132 ME_Cursor tmp_curs = *pCursor;
1134 x = ME_GetXForArrow(editor, pCursor);
1135 if (!pCursor->nOffset && editor->bCaretAtEnd)
1136 pRun = ME_FindItemBack(pRun, diRun);
1138 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1139 assert(p->type == diStartRow);
1140 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1141 yprev = ys = y = yp + p->member.row.nYPos;
1142 yd = y + editor->sizeWindow.cy;
1143 pLast = p;
1145 do {
1146 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1147 if (!p)
1148 break;
1149 if (p->type == diParagraph) {
1150 yp = p->member.para.nYPos;
1151 continue;
1153 y = yp + p->member.row.nYPos;
1154 if (y >= yd)
1155 break;
1156 pLast = p;
1157 yprev = y;
1158 } while(1);
1160 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1161 ME_UpdateSelection(editor, &tmp_curs);
1162 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
1164 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1165 ME_Repaint(editor);
1167 else
1169 ME_ScrollUp(editor,ys-yprev);
1171 assert(pCursor->pRun);
1172 assert(pCursor->pRun->type == diRun);
1175 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1177 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1178 ME_WrapMarkedParagraphs(editor);
1179 if (pRow) {
1180 ME_DisplayItem *pRun;
1181 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1182 pRow = ME_FindItemBack(pRow, diStartRow);
1183 if (!pRow)
1184 return;
1186 pRun = ME_FindItemFwd(pRow, diRun);
1187 if (pRun) {
1188 pCursor->pRun = pRun;
1189 pCursor->nOffset = 0;
1192 editor->bCaretAtEnd = FALSE;
1195 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1197 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1198 if (pRow) {
1199 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1200 if (pRun) {
1201 pCursor->pRun = pRun;
1202 pCursor->nOffset = 0;
1207 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1209 ME_DisplayItem *pRow;
1211 if (editor->bCaretAtEnd && !pCursor->nOffset)
1212 return;
1214 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1215 assert(pRow);
1216 if (pRow->type == diStartRow) {
1217 /* FIXME WTF was I thinking about here ? */
1218 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1219 assert(pRun);
1220 pCursor->pRun = pRun;
1221 pCursor->nOffset = 0;
1222 editor->bCaretAtEnd = 1;
1223 return;
1225 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1226 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1227 pCursor->nOffset = 0;
1228 editor->bCaretAtEnd = FALSE;
1231 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1233 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1234 assert(p);
1235 p = ME_FindItemBack(p, diRun);
1236 assert(p);
1237 assert(p->member.run.nFlags & MERF_ENDPARA);
1238 pCursor->pRun = p;
1239 pCursor->nOffset = 0;
1240 editor->bCaretAtEnd = FALSE;
1243 BOOL ME_IsSelection(ME_TextEditor *editor)
1245 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1248 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1250 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1252 if (cdir*dir>0)
1253 return 0;
1254 else
1255 return 1;
1258 BOOL ME_UpdateSelection(ME_TextEditor *editor, const ME_Cursor *pTempCursor)
1260 ME_Cursor old_anchor = editor->pCursors[1];
1262 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1264 /* any selection was present ? if so, it's no more, repaint ! */
1265 editor->pCursors[1] = editor->pCursors[0];
1266 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1267 return TRUE;
1269 return FALSE;
1271 else
1273 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1275 editor->pCursors[1] = *pTempCursor;
1276 return TRUE;
1280 ME_Repaint(editor);
1281 return TRUE;
1284 void ME_DeleteSelection(ME_TextEditor *editor)
1286 int from, to;
1287 ME_GetSelection(editor, &from, &to);
1288 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1291 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1293 return ME_GetInsertStyle(editor, 0);
1296 void ME_SendSelChange(ME_TextEditor *editor)
1298 SELCHANGE sc;
1300 if (!(editor->nEventMask & ENM_SELCHANGE))
1301 return;
1303 sc.nmhdr.hwndFrom = editor->hWnd;
1304 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1305 sc.nmhdr.code = EN_SELCHANGE;
1306 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1307 sc.seltyp = SEL_EMPTY;
1308 if (sc.chrg.cpMin != sc.chrg.cpMax)
1309 sc.seltyp |= SEL_TEXT;
1310 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1311 sc.seltyp |= SEL_MULTICHAR;
1312 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1313 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1314 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1315 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1316 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1318 ME_ClearTempStyle(editor);
1320 editor->notified_cr = sc.chrg;
1321 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1325 BOOL
1326 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1328 int nCursor = 0;
1329 ME_Cursor *p = &editor->pCursors[nCursor];
1330 ME_Cursor tmp_curs = *p;
1331 BOOL success = FALSE;
1333 ME_CheckCharOffsets(editor);
1334 editor->nUDArrowX = -1;
1335 switch(nVKey) {
1336 case VK_LEFT:
1337 editor->bCaretAtEnd = 0;
1338 if (ctrl)
1339 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1340 else
1341 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1342 break;
1343 case VK_RIGHT:
1344 editor->bCaretAtEnd = 0;
1345 if (ctrl)
1346 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1347 else
1348 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1349 break;
1350 case VK_UP:
1351 ME_MoveCursorLines(editor, &tmp_curs, -1);
1352 break;
1353 case VK_DOWN:
1354 ME_MoveCursorLines(editor, &tmp_curs, +1);
1355 break;
1356 case VK_PRIOR:
1357 ME_ArrowPageUp(editor, &tmp_curs);
1358 break;
1359 case VK_NEXT:
1360 ME_ArrowPageDown(editor, &tmp_curs);
1361 break;
1362 case VK_HOME: {
1363 if (ctrl)
1364 ME_ArrowCtrlHome(editor, &tmp_curs);
1365 else
1366 ME_ArrowHome(editor, &tmp_curs);
1367 editor->bCaretAtEnd = 0;
1368 break;
1370 case VK_END:
1371 if (ctrl)
1372 ME_ArrowCtrlEnd(editor, &tmp_curs);
1373 else
1374 ME_ArrowEnd(editor, &tmp_curs);
1375 break;
1378 if (!extend)
1379 editor->pCursors[1] = tmp_curs;
1380 *p = tmp_curs;
1382 ME_InvalidateSelection(editor);
1383 ME_Repaint(editor);
1384 HideCaret(editor->hWnd);
1385 ME_EnsureVisible(editor, tmp_curs.pRun);
1386 ME_ShowCaret(editor);
1387 ME_SendSelChange(editor);
1388 return success;