mscoree: Add missing interfaces.
[wine/multimedia.git] / dlls / riched20 / caret.c
bloba6a62178b54ac5dd1e670edc35a7ef44ac101cb3
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) && (how->flags & GTL_USECRLF))
61 length += editor->nParagraphs - 1;
63 if (how->flags & GTL_NUMBYTES)
65 CPINFO cpinfo;
67 if (how->codepage == 1200)
68 return length * 2;
69 if (how->flags & GTL_PRECISE)
70 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
71 if (GetCPInfo(how->codepage, &cpinfo))
72 return length * cpinfo.MaxCharSize;
73 ERR("Invalid codepage %u\n", how->codepage);
74 return E_INVALIDARG;
76 return length;
80 int ME_SetSelection(ME_TextEditor *editor, int from, int to)
82 int selectionEnd = 0;
83 const int len = ME_GetTextLength(editor);
85 /* all negative values are effectively the same */
86 if (from < 0)
87 from = -1;
88 if (to < 0)
89 to = -1;
91 /* select all */
92 if (from == 0 && to == -1)
94 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
95 editor->pCursors[1].nOffset = 0;
96 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
97 editor->pCursors[0].nOffset = 0;
98 ME_InvalidateSelection(editor);
99 ME_ClearTempStyle(editor);
100 return len + 1;
103 /* if both values are equal and also out of bound, that means to */
104 /* put the selection at the end of the text */
105 if ((from == to) && (to < 0 || to > len))
107 selectionEnd = 1;
109 else
111 /* if from is negative and to is positive then selection is */
112 /* deselected and caret moved to end of the current selection */
113 if (from < 0)
115 int start, end;
116 ME_GetSelection(editor, &start, &end);
117 editor->pCursors[1] = editor->pCursors[0];
118 ME_Repaint(editor);
119 ME_ClearTempStyle(editor);
120 return end;
123 /* adjust to if it's a negative value */
124 if (to < 0)
125 to = len + 1;
127 /* flip from and to if they are reversed */
128 if (from>to)
130 int tmp = from;
131 from = to;
132 to = tmp;
135 /* after fiddling with the values, we find from > len && to > len */
136 if (from > len)
137 selectionEnd = 1;
138 /* special case with to too big */
139 else if (to > len)
140 to = len + 1;
143 if (selectionEnd)
145 editor->pCursors[1].pRun = editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
146 editor->pCursors[1].nOffset = editor->pCursors[0].nOffset = 0;
147 ME_InvalidateSelection(editor);
148 ME_ClearTempStyle(editor);
149 return len;
152 ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
153 ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);
154 return to;
158 void
159 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
160 int *x, int *y, int *height)
162 ME_DisplayItem *pCursorRun = pCursor->pRun;
163 ME_DisplayItem *pSizeRun = pCursor->pRun;
165 assert(!pCursor->nOffset || !editor->bCaretAtEnd);
166 assert(height && x && y);
167 assert(!(ME_GetParagraph(pCursorRun)->member.para.nFlags & MEPF_REWRAP));
168 assert(pCursor->pRun);
169 assert(pCursor->pRun->type == diRun);
171 if (pCursorRun->type == diRun) {
172 ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
174 if (row) {
175 HDC hDC = GetDC(editor->hWnd);
176 ME_Context c;
177 ME_DisplayItem *run = pCursorRun;
178 ME_DisplayItem *para = NULL;
179 SIZE sz = {0, 0};
181 ME_InitContext(&c, editor, hDC);
183 if (!pCursor->nOffset && !editor->bCaretAtEnd)
185 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrStartRow);
186 assert(prev);
187 if (prev->type == diRun)
188 pSizeRun = prev;
190 assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
191 para = ME_FindItemBack(row, diParagraph);
192 assert(para);
193 assert(para->type == diParagraph);
194 if (editor->bCaretAtEnd && !pCursor->nOffset &&
195 run == ME_FindItemFwd(row, diRun))
197 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
198 assert(tmp);
199 if (tmp->type == diRun)
201 row = ME_FindItemBack(tmp, diStartRow);
202 pSizeRun = run = tmp;
203 assert(run);
204 assert(run->type == diRun);
205 sz = ME_GetRunSize(&c, &para->member.para,
206 &run->member.run, ME_StrLen(run->member.run.strText),
207 row->member.row.nLMargin);
210 if (pCursor->nOffset && !(run->member.run.nFlags & MERF_SKIPPED)) {
211 sz = ME_GetRunSize(&c, &para->member.para, &run->member.run, pCursor->nOffset,
212 row->member.row.nLMargin);
215 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
216 *x = run->member.run.pt.x + sz.cx;
217 *y = para->member.para.nYPos + row->member.row.nBaseline + pSizeRun->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 if (!ME_FindItemFwd(c.pRun, diParagraph))
275 return;
277 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
278 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
279 ME_CheckCharOffsets(editor);
280 nChars--;
281 if (editor->bEmulateVersion10 && nChars)
282 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 if (pos!=str)
526 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
527 p = &editor->pCursors[nCursor];
528 if (p->nOffset) {
529 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
530 p = &editor->pCursors[nCursor];
532 tmp_style = ME_GetInsertStyle(editor, nCursor);
533 /* ME_SplitParagraph increases style refcount */
534 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
535 p->pRun = ME_FindItemFwd(tp, diRun);
536 end_run = ME_FindItemBack(tp, diRun);
537 ME_ReleaseStyle(end_run->member.run.style);
538 end_run->member.run.style = tmp_style;
539 p->nOffset = 0;
540 if(pos-str < len && *pos =='\r')
541 pos++;
542 if(pos-str < len && *pos =='\n')
543 pos++;
544 if(pos-str <= len) {
545 len -= pos - str;
546 str = pos;
547 continue;
550 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
551 len = 0;
556 static BOOL
557 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
559 ME_DisplayItem *pRun = pCursor->pRun;
561 if (nRelOfs == -1)
563 if (!pCursor->nOffset)
565 do {
566 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
567 assert(pRun);
568 switch (pRun->type)
570 case diRun:
571 break;
572 case diParagraph:
573 if (pRun->member.para.prev_para->type == diTextStart)
574 return FALSE;
575 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
576 /* every paragraph ought to have at least one run */
577 assert(pRun && pRun->type == diRun);
578 assert(pRun->member.run.nFlags & MERF_ENDPARA);
579 break;
580 default:
581 assert(pRun->type != diRun && pRun->type != diParagraph);
582 return FALSE;
584 } while (RUN_IS_HIDDEN(&pRun->member.run));
585 pCursor->pRun = pRun;
586 if (pRun->member.run.nFlags & MERF_ENDPARA)
587 pCursor->nOffset = 0;
588 else
589 pCursor->nOffset = pRun->member.run.strText->nLen;
592 if (pCursor->nOffset)
593 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
594 return TRUE;
596 else
598 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
600 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
602 if (new_ofs < pRun->member.run.strText->nLen)
604 pCursor->nOffset = new_ofs;
605 return TRUE;
608 do {
609 pRun = ME_FindItemFwd(pRun, diRun);
610 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
611 if (pRun)
613 pCursor->pRun = pRun;
614 pCursor->nOffset = 0;
615 return TRUE;
618 return FALSE;
622 static BOOL
623 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
625 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
626 int nOffset = cursor->nOffset;
628 if (nRelOfs == -1)
630 /* Backward movement */
631 while (TRUE)
633 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
634 nOffset, WB_MOVEWORDLEFT);
635 if (nOffset)
636 break;
637 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
638 if (pOtherRun->type == diRun)
640 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
641 pOtherRun->member.run.strText->nLen - 1,
642 WB_ISDELIMITER)
643 && !(pRun->member.run.nFlags & MERF_ENDPARA)
644 && !(cursor->pRun == pRun && cursor->nOffset == 0)
645 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
646 WB_ISDELIMITER))
647 break;
648 pRun = pOtherRun;
649 nOffset = pOtherRun->member.run.strText->nLen;
651 else if (pOtherRun->type == diParagraph)
653 if (cursor->pRun == pRun && cursor->nOffset == 0)
655 /* Paragraph breaks are treated as separate words */
656 if (pOtherRun->member.para.prev_para->type == diTextStart)
657 return FALSE;
658 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
660 break;
664 else
666 /* Forward movement */
667 BOOL last_delim = FALSE;
669 while (TRUE)
671 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
672 nOffset, WB_ISDELIMITER))
673 break;
674 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
675 nOffset, WB_MOVEWORDRIGHT);
676 if (nOffset < pRun->member.run.strText->nLen)
677 break;
678 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
679 if (pOtherRun->type == diRun)
681 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
682 nOffset - 1, WB_ISDELIMITER);
683 pRun = pOtherRun;
684 nOffset = 0;
686 else if (pOtherRun->type == diParagraph)
688 if (cursor->pRun == pRun)
689 pRun = ME_FindItemFwd(pOtherRun, diRun);
690 nOffset = 0;
691 break;
693 else /* diTextEnd */
695 if (cursor->pRun == pRun)
696 return FALSE;
697 nOffset = 0;
698 break;
702 cursor->pRun = pRun;
703 cursor->nOffset = nOffset;
704 return TRUE;
708 void
709 ME_SelectWord(ME_TextEditor *editor)
711 if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
712 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
713 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
714 ME_InvalidateSelection(editor);
715 ME_SendSelChange(editor);
719 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
721 ME_Cursor *pCursor = &editor->pCursors[nCursor];
722 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
723 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
726 static void ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
728 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
729 ME_DisplayItem *last = NULL;
730 int rx = 0;
732 if (is_eol)
733 *is_eol = 0;
735 /* find paragraph */
736 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
738 assert(p->type == diParagraph);
739 if (y < p->member.para.nYPos + p->member.para.nHeight)
741 y -= p->member.para.nYPos;
742 p = ME_FindItemFwd(p, diStartRow);
743 break;
746 /* find row */
747 for (; p != editor->pBuffer->pLast; )
749 ME_DisplayItem *pp;
750 assert(p->type == diStartRow);
751 if (y < p->member.row.nYPos + p->member.row.nHeight)
753 p = ME_FindItemFwd(p, diRun);
754 break;
756 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
757 if (pp->type != diStartRow)
759 p = ME_FindItemFwd(p, diRun);
760 break;
762 p = pp;
764 for (; p != editor->pBuffer->pLast; p = p->next)
766 switch (p->type)
768 case diRun:
769 rx = x - p->member.run.pt.x;
770 if (rx < p->member.run.nWidth)
772 found_here:
773 assert(p->type == diRun);
774 if ((p->member.run.nFlags & MERF_ENDPARA) || rx < 0)
775 rx = 0;
776 result->pRun = p;
777 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
778 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
780 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
781 result->nOffset = 0;
783 return;
785 break;
786 case diStartRow:
787 p = ME_FindItemFwd(p, diRun);
788 if (is_eol) *is_eol = 1;
789 rx = 0; /* FIXME not sure */
790 goto found_here;
791 case diParagraph:
792 case diTextEnd:
793 rx = 0; /* FIXME not sure */
794 p = last;
795 goto found_here;
796 default: assert(0);
798 last = p;
800 result->pRun = ME_FindItemBack(p, diRun);
801 result->nOffset = 0;
802 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
807 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
809 ME_Cursor cursor;
810 RECT rc;
812 GetClientRect(editor->hWnd, &rc);
813 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
814 return -1;
815 y += ME_GetYScrollPos(editor);
816 ME_FindPixelPos(editor, x, y, &cursor, NULL);
817 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
818 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
822 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
824 ME_Cursor tmp_cursor;
825 int is_selection = 0;
827 editor->nUDArrowX = -1;
829 y += ME_GetYScrollPos(editor);
831 tmp_cursor = editor->pCursors[0];
832 is_selection = ME_IsSelection(editor);
834 if (x >= editor->selofs)
836 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
837 if (GetKeyState(VK_SHIFT)>=0)
839 editor->pCursors[1] = editor->pCursors[0];
841 else if (!is_selection) {
842 editor->pCursors[1] = tmp_cursor;
843 is_selection = 1;
846 ME_InvalidateSelection(editor);
847 HideCaret(editor->hWnd);
848 ME_MoveCaret(editor);
849 ShowCaret(editor->hWnd);
850 ME_ClearTempStyle(editor);
851 ME_SendSelChange(editor);
853 else
855 ME_DisplayItem *pRow;
857 editor->linesel = 1;
858 editor->sely = y;
859 /* Set pCursors[0] to beginning of line */
860 ME_FindPixelPos(editor, x, y, &editor->pCursors[1], &editor->bCaretAtEnd);
861 /* Set pCursors[1] to end of line */
862 pRow = ME_FindItemFwd(editor->pCursors[1].pRun, diStartRowOrParagraphOrEnd);
863 assert(pRow);
864 /* pCursor[0] is the position where the cursor will be drawn,
865 * pCursor[1] is the other end of the selection range
866 * pCursor[2] and [3] are backups of [0] and [1] so I
867 * don't have to look them up again
870 if (pRow->type == diStartRow) {
871 /* FIXME WTF was I thinking about here ? */
872 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
873 assert(pRun);
874 editor->pCursors[0].pRun = pRun;
875 editor->pCursors[0].nOffset = 0;
876 editor->bCaretAtEnd = 1;
877 } else {
878 editor->pCursors[0].pRun = ME_FindItemBack(pRow, diRun);
879 assert(editor->pCursors[0].pRun && editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA);
880 editor->pCursors[0].nOffset = 0;
881 editor->bCaretAtEnd = 0;
883 editor->pCursors[2] = editor->pCursors[0];
884 editor->pCursors[3] = editor->pCursors[1];
885 ME_InvalidateSelection(editor);
886 HideCaret(editor->hWnd);
887 ME_MoveCaret(editor);
888 ShowCaret(editor->hWnd);
889 ME_ClearTempStyle(editor);
890 ME_SendSelChange(editor);
894 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
896 ME_Cursor tmp_cursor;
898 y += ME_GetYScrollPos(editor);
900 tmp_cursor = editor->pCursors[0];
901 /* FIXME: do something with the return value of ME_FindPixelPos */
902 if (!editor->linesel)
903 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
904 else ME_FindPixelPos(editor, (y > editor->sely) * editor->rcFormat.right, y, &tmp_cursor, &editor->bCaretAtEnd);
906 if (!memcmp(&tmp_cursor, editor->pCursors, sizeof(tmp_cursor)))
907 return;
909 ME_InvalidateSelection(editor);
910 if (!editor->linesel)
911 editor->pCursors[0] = tmp_cursor;
912 else if (!memcmp(&tmp_cursor, editor->pCursors+2, sizeof(tmp_cursor)) ||
913 !memcmp(&tmp_cursor, editor->pCursors+3, sizeof(tmp_cursor)))
915 editor->pCursors[0] = editor->pCursors[2];
916 editor->pCursors[1] = editor->pCursors[3];
918 else if (y < editor->sely)
920 editor->pCursors[0] = tmp_cursor;
921 editor->pCursors[1] = editor->pCursors[2];
923 else
925 editor->pCursors[0] = tmp_cursor;
926 editor->pCursors[1] = editor->pCursors[3];
929 HideCaret(editor->hWnd);
930 ME_MoveCaret(editor);
931 ME_InvalidateSelection(editor);
932 ShowCaret(editor->hWnd);
933 ME_SendSelChange(editor);
936 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
937 int x, int *pOffset, int *pbCaretAtEnd)
939 ME_DisplayItem *pNext, *pLastRun;
940 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
941 assert(pNext->type == diRun);
942 pLastRun = pNext;
943 *pbCaretAtEnd = FALSE;
944 do {
945 int run_x = pNext->member.run.pt.x;
946 int width = pNext->member.run.nWidth;
947 if (x < run_x)
949 if (pOffset) *pOffset = 0;
950 return pNext;
952 if (x >= run_x && x < run_x+width)
954 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
955 ME_String *s = pNext->member.run.strText;
956 if (ch < s->nLen) {
957 if (pOffset)
958 *pOffset = ch;
959 return pNext;
962 pLastRun = pNext;
963 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
964 } while(pNext && pNext->type == diRun);
966 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
968 pNext = ME_FindItemFwd(pNext, diRun);
969 if (pbCaretAtEnd) *pbCaretAtEnd = 1;
970 if (pOffset) *pOffset = 0;
971 return pNext;
972 } else {
973 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
974 if (pOffset) *pOffset = 0;
975 return pLastRun;
979 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
981 ME_DisplayItem *pRun = pCursor->pRun;
982 int x;
984 if (editor->nUDArrowX != -1)
985 x = editor->nUDArrowX;
986 else {
987 if (editor->bCaretAtEnd)
989 pRun = ME_FindItemBack(pRun, diRun);
990 assert(pRun);
991 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
993 else {
994 x = pRun->member.run.pt.x;
995 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
997 editor->nUDArrowX = x;
999 return x;
1003 static void
1004 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1006 ME_DisplayItem *pRun = pCursor->pRun;
1007 ME_DisplayItem *pItem;
1008 int x = ME_GetXForArrow(editor, pCursor);
1010 if (editor->bCaretAtEnd && !pCursor->nOffset)
1011 pRun = ME_FindItemBack(pRun, diRun);
1012 if (!pRun)
1013 return;
1014 if (nRelOfs == -1)
1016 /* start of this row */
1017 pItem = ME_FindItemBack(pRun, diStartRow);
1018 assert(pItem);
1019 /* start of the previous row */
1020 pItem = ME_FindItemBack(pItem, diStartRow);
1022 else
1024 /* start of the next row */
1025 pItem = ME_FindItemFwd(pRun, diStartRow);
1026 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1029 if (!pItem)
1031 /* row not found - ignore */
1032 return;
1034 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1035 assert(pCursor->pRun);
1036 assert(pCursor->pRun->type == diRun);
1040 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1042 ME_DisplayItem *pRun = pCursor->pRun;
1043 ME_DisplayItem *pLast, *p;
1044 int x, y, ys, yd, yp, yprev;
1045 ME_Cursor tmp_curs = *pCursor;
1047 x = ME_GetXForArrow(editor, pCursor);
1048 if (!pCursor->nOffset && editor->bCaretAtEnd)
1049 pRun = ME_FindItemBack(pRun, diRun);
1051 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1052 assert(p->type == diStartRow);
1053 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1054 yprev = ys = y = yp + p->member.row.nYPos;
1055 yd = y - editor->sizeWindow.cy;
1056 pLast = p;
1058 do {
1059 p = ME_FindItemBack(p, diStartRowOrParagraph);
1060 if (!p)
1061 break;
1062 if (p->type == diParagraph) { /* crossing paragraphs */
1063 if (p->member.para.prev_para == NULL)
1064 break;
1065 yp = p->member.para.prev_para->member.para.nYPos;
1066 continue;
1068 y = yp + p->member.row.nYPos;
1069 if (y < yd)
1070 break;
1071 pLast = p;
1072 yprev = y;
1073 } while(1);
1075 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1076 ME_UpdateSelection(editor, &tmp_curs);
1077 if (yprev < editor->sizeWindow.cy)
1079 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
1080 ME_Repaint(editor);
1082 else
1084 ME_ScrollUp(editor, ys-yprev);
1086 assert(pCursor->pRun);
1087 assert(pCursor->pRun->type == diRun);
1090 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1091 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1092 In such a situation, clicking the scrollbar restores its position back to the
1093 normal range (ie. sets it to (doclength-screenheight)). */
1095 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1097 ME_DisplayItem *pRun = pCursor->pRun;
1098 ME_DisplayItem *pLast, *p;
1099 int x, y, ys, yd, yp, yprev;
1100 ME_Cursor tmp_curs = *pCursor;
1102 x = ME_GetXForArrow(editor, pCursor);
1103 if (!pCursor->nOffset && editor->bCaretAtEnd)
1104 pRun = ME_FindItemBack(pRun, diRun);
1106 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1107 assert(p->type == diStartRow);
1108 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1109 yprev = ys = y = yp + p->member.row.nYPos;
1110 yd = y + editor->sizeWindow.cy;
1111 pLast = p;
1113 do {
1114 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1115 if (!p)
1116 break;
1117 if (p->type == diParagraph) {
1118 yp = p->member.para.nYPos;
1119 continue;
1121 y = yp + p->member.row.nYPos;
1122 if (y >= yd)
1123 break;
1124 pLast = p;
1125 yprev = y;
1126 } while(1);
1128 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1129 ME_UpdateSelection(editor, &tmp_curs);
1130 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
1132 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1133 ME_Repaint(editor);
1135 else
1137 ME_ScrollUp(editor,ys-yprev);
1139 assert(pCursor->pRun);
1140 assert(pCursor->pRun->type == diRun);
1143 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1145 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1146 /* bCaretAtEnd doesn't make sense if the cursor isn't set at the
1147 first character of the next row */
1148 assert(!editor->bCaretAtEnd || !pCursor->nOffset);
1149 ME_WrapMarkedParagraphs(editor);
1150 if (pRow) {
1151 ME_DisplayItem *pRun;
1152 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1153 pRow = ME_FindItemBack(pRow, diStartRow);
1154 if (!pRow)
1155 return;
1157 pRun = ME_FindItemFwd(pRow, diRun);
1158 if (pRun) {
1159 pCursor->pRun = pRun;
1160 pCursor->nOffset = 0;
1163 editor->bCaretAtEnd = FALSE;
1166 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1168 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1169 if (pRow) {
1170 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1171 if (pRun) {
1172 pCursor->pRun = pRun;
1173 pCursor->nOffset = 0;
1178 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1180 ME_DisplayItem *pRow;
1182 if (editor->bCaretAtEnd && !pCursor->nOffset)
1183 return;
1185 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1186 assert(pRow);
1187 if (pRow->type == diStartRow) {
1188 /* FIXME WTF was I thinking about here ? */
1189 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1190 assert(pRun);
1191 pCursor->pRun = pRun;
1192 pCursor->nOffset = 0;
1193 editor->bCaretAtEnd = 1;
1194 return;
1196 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1197 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1198 pCursor->nOffset = 0;
1199 editor->bCaretAtEnd = FALSE;
1202 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1204 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1205 assert(p);
1206 p = ME_FindItemBack(p, diRun);
1207 assert(p);
1208 assert(p->member.run.nFlags & MERF_ENDPARA);
1209 pCursor->pRun = p;
1210 pCursor->nOffset = 0;
1211 editor->bCaretAtEnd = FALSE;
1214 BOOL ME_IsSelection(ME_TextEditor *editor)
1216 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1219 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1221 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1223 if (cdir*dir>0)
1224 return 0;
1225 else
1226 return 1;
1229 BOOL ME_UpdateSelection(ME_TextEditor *editor, const ME_Cursor *pTempCursor)
1231 ME_Cursor old_anchor = editor->pCursors[1];
1233 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1235 /* any selection was present ? if so, it's no more, repaint ! */
1236 editor->pCursors[1] = editor->pCursors[0];
1237 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1238 return TRUE;
1240 return FALSE;
1242 else
1244 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1246 editor->pCursors[1] = *pTempCursor;
1247 return TRUE;
1251 ME_Repaint(editor);
1252 return TRUE;
1255 void ME_DeleteSelection(ME_TextEditor *editor)
1257 int from, to;
1258 ME_GetSelection(editor, &from, &to);
1259 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1262 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1264 ME_Style *style;
1265 int from, to;
1267 ME_GetSelection(editor, &from, &to);
1268 if (from != to) {
1269 ME_Cursor c;
1270 ME_CursorFromCharOfs(editor, from, &c);
1271 style = c.pRun->member.run.style;
1272 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1274 else
1275 style = ME_GetInsertStyle(editor, 0);
1276 return style;
1279 void ME_SendSelChange(ME_TextEditor *editor)
1281 SELCHANGE sc;
1283 ME_ClearTempStyle(editor);
1285 if (!(editor->nEventMask & ENM_SELCHANGE))
1286 return;
1288 sc.nmhdr.hwndFrom = editor->hWnd;
1289 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1290 sc.nmhdr.code = EN_SELCHANGE;
1291 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1292 sc.seltyp = SEL_EMPTY;
1293 if (sc.chrg.cpMin != sc.chrg.cpMax)
1294 sc.seltyp |= SEL_TEXT;
1295 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1296 sc.seltyp |= SEL_MULTICHAR;
1297 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1300 BOOL
1301 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1303 int nCursor = 0;
1304 ME_Cursor *p = &editor->pCursors[nCursor];
1305 ME_Cursor tmp_curs = *p;
1306 BOOL success = FALSE;
1308 ME_CheckCharOffsets(editor);
1309 editor->nUDArrowX = -1;
1310 switch(nVKey) {
1311 case VK_LEFT:
1312 editor->bCaretAtEnd = 0;
1313 if (ctrl)
1314 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1315 else
1316 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1317 break;
1318 case VK_RIGHT:
1319 editor->bCaretAtEnd = 0;
1320 if (ctrl)
1321 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1322 else
1323 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1324 break;
1325 case VK_UP:
1326 ME_MoveCursorLines(editor, &tmp_curs, -1);
1327 break;
1328 case VK_DOWN:
1329 ME_MoveCursorLines(editor, &tmp_curs, +1);
1330 break;
1331 case VK_PRIOR:
1332 ME_ArrowPageUp(editor, &tmp_curs);
1333 break;
1334 case VK_NEXT:
1335 ME_ArrowPageDown(editor, &tmp_curs);
1336 break;
1337 case VK_HOME: {
1338 if (ctrl)
1339 ME_ArrowCtrlHome(editor, &tmp_curs);
1340 else
1341 ME_ArrowHome(editor, &tmp_curs);
1342 editor->bCaretAtEnd = 0;
1343 break;
1345 case VK_END:
1346 if (ctrl)
1347 ME_ArrowCtrlEnd(editor, &tmp_curs);
1348 else
1349 ME_ArrowEnd(editor, &tmp_curs);
1350 break;
1353 if (!extend)
1354 editor->pCursors[1] = tmp_curs;
1355 *p = tmp_curs;
1357 ME_InvalidateSelection(editor);
1358 ME_Repaint(editor);
1359 HideCaret(editor->hWnd);
1360 ME_EnsureVisible(editor, tmp_curs.pRun);
1361 ME_ShowCaret(editor);
1362 ME_SendSelChange(editor);
1363 return success;