user32: Fix a typo in the StretchDIBits stub name.
[wine/multimedia.git] / dlls / riched20 / caret.c
blob2337107172bf6f64d0268a11bcffe9728383d793
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, &run->member.run, ME_StrLen(run->member.run.strText));
208 if (pCursor->nOffset && !(run->member.run.nFlags & MERF_SKIPPED)) {
209 sz = ME_GetRunSize(&c, &para->member.para, &run->member.run, pCursor->nOffset);
212 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
213 *x = run->member.run.pt.x + sz.cx;
214 *y = para->member.para.nYPos + row->member.row.nBaseline + pSizeRun->member.run.pt.y - pSizeRun->member.run.nAscent - ME_GetYScrollPos(editor);
216 ME_DestroyContext(&c);
217 ReleaseDC(editor->hWnd, hDC);
218 return;
221 *height = 10; /* FIXME use global font */
222 *x = 0;
223 *y = 0;
227 void
228 ME_MoveCaret(ME_TextEditor *editor)
230 int x, y, height;
232 if (ME_WrapMarkedParagraphs(editor))
233 ME_UpdateScrollBar(editor);
234 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
235 if(editor->bHaveFocus)
237 CreateCaret(editor->hWnd, NULL, 0, height);
238 SetCaretPos(x, y);
243 void ME_ShowCaret(ME_TextEditor *ed)
245 ME_MoveCaret(ed);
246 if(ed->bHaveFocus)
247 ShowCaret(ed->hWnd);
250 void ME_HideCaret(ME_TextEditor *ed)
252 if(ed->bHaveFocus)
254 HideCaret(ed->hWnd);
255 DestroyCaret();
259 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
260 int nChars)
262 ME_Cursor c;
263 int shift = 0;
265 while(nChars > 0)
267 ME_Run *run;
268 ME_CursorFromCharOfs(editor, nOfs, &c);
269 run = &c.pRun->member.run;
270 if (run->nFlags & MERF_ENDPARA) {
271 if (!ME_FindItemFwd(c.pRun, diParagraph))
273 return;
275 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
276 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
277 ME_CheckCharOffsets(editor);
278 nChars--;
279 if (editor->bEmulateVersion10 && nChars)
280 nChars--;
281 continue;
283 else
285 ME_Cursor cursor;
286 int nIntendedChars = nChars;
287 int nCharsToDelete = nChars;
288 int i;
289 int loc = c.nOffset;
291 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
293 cursor = c;
294 ME_StrRelPos(run->strText, loc, &nChars);
295 /* nChars is the number of characters that should be deleted from the
296 FOLLOWING runs (these AFTER cursor.pRun)
297 nCharsToDelete is a number of chars to delete from THIS run */
298 nCharsToDelete -= nChars;
299 shift -= nCharsToDelete;
300 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
301 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
302 debugstr_w(run->strText->szData), run->strText->nLen);
304 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
306 /* undo = reinsert whole run */
307 /* nOfs is a character offset (from the start of the document
308 to the current (deleted) run */
309 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
310 if (pUndo)
311 pUndo->di.member.run.nCharOfs = nOfs;
313 else
315 /* undo = reinsert partial run */
316 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
317 if (pUndo) {
318 ME_DestroyString(pUndo->di.member.run.strText);
319 pUndo->di.member.run.nCharOfs = nOfs;
320 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
323 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
324 TRACE("Shift value: %d\n", shift);
325 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
327 /* update cursors (including c) */
328 for (i=-1; i<editor->nCursors; i++) {
329 ME_Cursor *pThisCur = editor->pCursors + i;
330 if (i == -1) pThisCur = &c;
331 if (pThisCur->pRun == cursor.pRun) {
332 if (pThisCur->nOffset > cursor.nOffset) {
333 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
334 pThisCur->nOffset = cursor.nOffset;
335 else
336 pThisCur->nOffset -= nCharsToDelete;
337 assert(pThisCur->nOffset >= 0);
338 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
340 if (pThisCur->nOffset == ME_StrVLen(run->strText))
342 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
343 assert(pThisCur->pRun->type == diRun);
344 pThisCur->nOffset = 0;
349 /* c = updated data now */
351 if (c.pRun == cursor.pRun)
352 ME_SkipAndPropagateCharOffset(c.pRun, shift);
353 else
354 ME_PropagateCharOffset(c.pRun, shift);
356 if (!ME_StrVLen(cursor.pRun->member.run.strText))
358 TRACE("Removing useless run\n");
359 ME_Remove(cursor.pRun);
360 ME_DestroyDisplayItem(cursor.pRun);
363 shift = 0;
365 ME_CheckCharOffsets(editor);
367 continue;
372 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
373 int nChars)
375 assert(nCursor>=0 && nCursor<editor->nCursors);
376 /* text operations set modified state */
377 editor->nModifyStep = 1;
378 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
381 static ME_DisplayItem *
382 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
383 const WCHAR *str, int len, ME_Style *style,
384 int flags)
386 ME_Cursor *p = &editor->pCursors[nCursor];
388 editor->bCaretAtEnd = FALSE;
390 assert(p->pRun->type == diRun);
392 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
396 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
397 void ME_InsertGraphicsFromCursor(ME_TextEditor *editor, int nCursor)
399 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
400 WCHAR space = ' ';
402 /* FIXME no no no */
403 if (ME_IsSelection(editor))
404 ME_DeleteSelection(editor);
406 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
407 MERF_GRAPHICS);
408 ME_SendSelChange(editor);
412 void
413 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
415 WCHAR tab = '\t';
416 ME_DisplayItem *p, *run;
417 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
419 p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
420 MERF_CELL);
421 run = p;
422 while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
424 if (run->member.run.nFlags & MERF_CELL)
426 assert(run->member.run.pCell->next);
427 p->member.run.pCell = run->member.run.pCell->next;
428 return;
431 assert(run->type == diParagraph);
432 assert(run->member.para.bTable);
433 assert(run->member.para.pCells);
434 p->member.run.pCell = run->member.para.pCells;
438 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
439 const WCHAR *str, int len, ME_Style *style)
441 const WCHAR *pos;
442 ME_Cursor *p = NULL;
443 int oldLen;
445 /* FIXME really HERE ? */
446 if (ME_IsSelection(editor))
447 ME_DeleteSelection(editor);
449 /* FIXME: is this too slow? */
450 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
451 oldLen = ME_GetTextLength(editor);
453 /* text operations set modified state */
454 editor->nModifyStep = 1;
456 assert(style);
458 assert(nCursor>=0 && nCursor<editor->nCursors);
459 if (len == -1)
460 len = lstrlenW(str);
462 /* grow the text limit to fit our text */
463 if(editor->nTextLimit < oldLen +len)
464 editor->nTextLimit = oldLen + len;
466 while (len)
468 pos = str;
469 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
470 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
471 pos++;
472 if (pos-str < len && *pos == '\t') { /* handle tabs */
473 WCHAR tab = '\t';
475 if (pos!=str)
476 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
478 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
480 pos++;
481 if(pos-str <= len) {
482 len -= pos - str;
483 str = pos;
484 continue;
487 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
488 if (!editor->bEmulateVersion10 && pos-str < len-2 && pos[0] == '\r' && pos[1] == '\r' && pos[2] == '\n') {
489 WCHAR space = ' ';
491 if (pos!=str)
492 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
494 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
496 pos+=3;
497 if(pos-str <= len) {
498 len -= pos - str;
499 str = pos;
500 continue;
503 if (pos-str < len) { /* handle EOLs */
504 ME_DisplayItem *tp, *end_run;
505 ME_Style *tmp_style;
506 if (pos!=str)
507 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
508 p = &editor->pCursors[nCursor];
509 if (p->nOffset) {
510 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
511 p = &editor->pCursors[nCursor];
513 tmp_style = ME_GetInsertStyle(editor, nCursor);
514 /* ME_SplitParagraph increases style refcount */
515 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
516 p->pRun = ME_FindItemFwd(tp, diRun);
517 end_run = ME_FindItemBack(tp, diRun);
518 ME_ReleaseStyle(end_run->member.run.style);
519 end_run->member.run.style = tmp_style;
520 p->nOffset = 0;
521 if(pos-str < len && *pos =='\r')
522 pos++;
523 if(pos-str < len && *pos =='\n')
524 pos++;
525 if(pos-str <= len) {
526 len -= pos - str;
527 str = pos;
528 continue;
531 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
532 len = 0;
537 static BOOL
538 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
540 ME_DisplayItem *pRun = pCursor->pRun;
542 if (nRelOfs == -1)
544 if (!pCursor->nOffset)
546 do {
547 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
548 assert(pRun);
549 switch (pRun->type)
551 case diRun:
552 break;
553 case diParagraph:
554 if (pRun->member.para.prev_para->type == diTextStart)
555 return FALSE;
556 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
557 /* every paragraph ought to have at least one run */
558 assert(pRun && pRun->type == diRun);
559 assert(pRun->member.run.nFlags & MERF_ENDPARA);
560 break;
561 default:
562 assert(pRun->type != diRun && pRun->type != diParagraph);
563 return FALSE;
565 } while (RUN_IS_HIDDEN(&pRun->member.run));
566 pCursor->pRun = pRun;
567 if (pRun->member.run.nFlags & MERF_ENDPARA)
568 pCursor->nOffset = 0;
569 else
570 pCursor->nOffset = pRun->member.run.strText->nLen;
573 if (pCursor->nOffset)
574 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
575 return TRUE;
577 else
579 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
581 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
583 if (new_ofs < pRun->member.run.strText->nLen)
585 pCursor->nOffset = new_ofs;
586 return TRUE;
589 do {
590 pRun = ME_FindItemFwd(pRun, diRun);
591 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
592 if (pRun)
594 pCursor->pRun = pRun;
595 pCursor->nOffset = 0;
596 return TRUE;
599 return FALSE;
603 static BOOL
604 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
606 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
607 int nOffset = cursor->nOffset;
609 if (nRelOfs == -1)
611 /* Backward movement */
612 while (TRUE)
614 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
615 nOffset, WB_MOVEWORDLEFT);
616 if (nOffset)
617 break;
618 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
619 if (pOtherRun->type == diRun)
621 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
622 pOtherRun->member.run.strText->nLen - 1,
623 WB_ISDELIMITER)
624 && !(pRun->member.run.nFlags & MERF_ENDPARA)
625 && !(cursor->pRun == pRun && cursor->nOffset == 0)
626 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
627 WB_ISDELIMITER))
628 break;
629 pRun = pOtherRun;
630 nOffset = pOtherRun->member.run.strText->nLen;
632 else if (pOtherRun->type == diParagraph)
634 if (cursor->pRun == pRun && cursor->nOffset == 0)
636 /* Paragraph breaks are treated as separate words */
637 if (pOtherRun->member.para.prev_para->type == diTextStart)
638 return FALSE;
639 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
641 break;
645 else
647 /* Forward movement */
648 BOOL last_delim = FALSE;
650 while (TRUE)
652 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
653 nOffset, WB_ISDELIMITER))
654 break;
655 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
656 nOffset, WB_MOVEWORDRIGHT);
657 if (nOffset < pRun->member.run.strText->nLen)
658 break;
659 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
660 if (pOtherRun->type == diRun)
662 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
663 nOffset - 1, WB_ISDELIMITER);
664 pRun = pOtherRun;
665 nOffset = 0;
667 else if (pOtherRun->type == diParagraph)
669 if (cursor->pRun == pRun)
670 pRun = ME_FindItemFwd(pOtherRun, diRun);
671 nOffset = 0;
672 break;
674 else /* diTextEnd */
676 if (cursor->pRun == pRun)
677 return FALSE;
678 nOffset = 0;
679 break;
683 cursor->pRun = pRun;
684 cursor->nOffset = nOffset;
685 return TRUE;
689 void
690 ME_SelectWord(ME_TextEditor *editor)
692 if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
693 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
694 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
695 ME_InvalidateSelection(editor);
696 ME_SendSelChange(editor);
700 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
702 ME_Cursor *pCursor = &editor->pCursors[nCursor];
703 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
704 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
707 static void ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
709 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
710 ME_DisplayItem *last = NULL;
711 int rx = 0;
713 if (is_eol)
714 *is_eol = 0;
716 /* find paragraph */
717 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
719 assert(p->type == diParagraph);
720 if (y < p->member.para.nYPos + p->member.para.nHeight)
722 y -= p->member.para.nYPos;
723 p = ME_FindItemFwd(p, diStartRow);
724 break;
727 /* find row */
728 for (; p != editor->pBuffer->pLast; )
730 ME_DisplayItem *pp;
731 assert(p->type == diStartRow);
732 if (y < p->member.row.nYPos + p->member.row.nHeight)
734 p = ME_FindItemFwd(p, diRun);
735 break;
737 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
738 if (pp->type != diStartRow)
740 p = ME_FindItemFwd(p, diRun);
741 break;
743 p = pp;
745 for (; p != editor->pBuffer->pLast; p = p->next)
747 switch (p->type)
749 case diRun:
750 rx = x - p->member.run.pt.x;
751 if (rx < p->member.run.nWidth)
753 found_here:
754 assert(p->type == diRun);
755 if ((p->member.run.nFlags & MERF_ENDPARA) || rx < 0)
756 rx = 0;
757 result->pRun = p;
758 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
759 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
761 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
762 result->nOffset = 0;
764 return;
766 break;
767 case diStartRow:
768 p = ME_FindItemFwd(p, diRun);
769 if (is_eol) *is_eol = 1;
770 rx = 0; /* FIXME not sure */
771 goto found_here;
772 case diParagraph:
773 case diTextEnd:
774 rx = 0; /* FIXME not sure */
775 p = last;
776 goto found_here;
777 default: assert(0);
779 last = p;
781 result->pRun = ME_FindItemBack(p, diRun);
782 result->nOffset = 0;
783 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
788 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
790 ME_Cursor cursor;
791 RECT rc;
793 GetClientRect(editor->hWnd, &rc);
794 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
795 return -1;
796 y += ME_GetYScrollPos(editor);
797 ME_FindPixelPos(editor, x, y, &cursor, NULL);
798 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
799 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
803 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
805 ME_Cursor tmp_cursor;
806 int is_selection = 0;
808 editor->nUDArrowX = -1;
810 y += ME_GetYScrollPos(editor);
812 tmp_cursor = editor->pCursors[0];
813 is_selection = ME_IsSelection(editor);
815 if (x >= editor->selofs)
817 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
818 if (GetKeyState(VK_SHIFT)>=0)
820 editor->pCursors[1] = editor->pCursors[0];
822 else if (!is_selection) {
823 editor->pCursors[1] = tmp_cursor;
824 is_selection = 1;
827 ME_InvalidateSelection(editor);
828 HideCaret(editor->hWnd);
829 ME_MoveCaret(editor);
830 ShowCaret(editor->hWnd);
831 ME_ClearTempStyle(editor);
832 ME_SendSelChange(editor);
834 else
836 ME_DisplayItem *pRow;
838 editor->linesel = 1;
839 editor->sely = y;
840 /* Set pCursors[0] to beginning of line */
841 ME_FindPixelPos(editor, x, y, &editor->pCursors[1], &editor->bCaretAtEnd);
842 /* Set pCursors[1] to end of line */
843 pRow = ME_FindItemFwd(editor->pCursors[1].pRun, diStartRowOrParagraphOrEnd);
844 assert(pRow);
845 /* pCursor[0] is the position where the cursor will be drawn,
846 * pCursor[1] is the other end of the selection range
847 * pCursor[2] and [3] are backups of [0] and [1] so I
848 * don't have to look them up again
851 if (pRow->type == diStartRow) {
852 /* FIXME WTF was I thinking about here ? */
853 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
854 assert(pRun);
855 editor->pCursors[0].pRun = pRun;
856 editor->pCursors[0].nOffset = 0;
857 editor->bCaretAtEnd = 1;
858 } else {
859 editor->pCursors[0].pRun = ME_FindItemBack(pRow, diRun);
860 assert(editor->pCursors[0].pRun && editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA);
861 editor->pCursors[0].nOffset = 0;
862 editor->bCaretAtEnd = 0;
864 editor->pCursors[2] = editor->pCursors[0];
865 editor->pCursors[3] = editor->pCursors[1];
866 ME_InvalidateSelection(editor);
867 HideCaret(editor->hWnd);
868 ME_MoveCaret(editor);
869 ShowCaret(editor->hWnd);
870 ME_ClearTempStyle(editor);
871 ME_SendSelChange(editor);
875 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
877 ME_Cursor tmp_cursor;
879 y += ME_GetYScrollPos(editor);
881 tmp_cursor = editor->pCursors[0];
882 /* FIXME: do something with the return value of ME_FindPixelPos */
883 if (!editor->linesel)
884 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
885 else ME_FindPixelPos(editor, (y > editor->sely) * editor->rcFormat.right, y, &tmp_cursor, &editor->bCaretAtEnd);
887 if (!memcmp(&tmp_cursor, editor->pCursors, sizeof(tmp_cursor)))
888 return;
890 ME_InvalidateSelection(editor);
891 if (!editor->linesel)
892 editor->pCursors[0] = tmp_cursor;
893 else if (!memcmp(&tmp_cursor, editor->pCursors+2, sizeof(tmp_cursor)) ||
894 !memcmp(&tmp_cursor, editor->pCursors+3, sizeof(tmp_cursor)))
896 editor->pCursors[0] = editor->pCursors[2];
897 editor->pCursors[1] = editor->pCursors[3];
899 else if (y < editor->sely)
901 editor->pCursors[0] = tmp_cursor;
902 editor->pCursors[1] = editor->pCursors[2];
904 else
906 editor->pCursors[0] = tmp_cursor;
907 editor->pCursors[1] = editor->pCursors[3];
910 HideCaret(editor->hWnd);
911 ME_MoveCaret(editor);
912 ME_InvalidateSelection(editor);
913 ShowCaret(editor->hWnd);
914 ME_SendSelChange(editor);
917 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
918 int x, int *pOffset, int *pbCaretAtEnd)
920 ME_DisplayItem *pNext, *pLastRun;
921 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
922 assert(pNext->type == diRun);
923 pLastRun = pNext;
924 *pbCaretAtEnd = FALSE;
925 do {
926 int run_x = pNext->member.run.pt.x;
927 int width = pNext->member.run.nWidth;
928 if (x < run_x)
930 if (pOffset) *pOffset = 0;
931 return pNext;
933 if (x >= run_x && x < run_x+width)
935 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
936 ME_String *s = pNext->member.run.strText;
937 if (ch < s->nLen) {
938 if (pOffset)
939 *pOffset = ch;
940 return pNext;
943 pLastRun = pNext;
944 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
945 } while(pNext && pNext->type == diRun);
947 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
949 pNext = ME_FindItemFwd(pNext, diRun);
950 if (pbCaretAtEnd) *pbCaretAtEnd = 1;
951 if (pOffset) *pOffset = 0;
952 return pNext;
953 } else {
954 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
955 if (pOffset) *pOffset = 0;
956 return pLastRun;
960 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
962 ME_DisplayItem *pRun = pCursor->pRun;
963 int x;
965 if (editor->nUDArrowX != -1)
966 x = editor->nUDArrowX;
967 else {
968 if (editor->bCaretAtEnd)
970 pRun = ME_FindItemBack(pRun, diRun);
971 assert(pRun);
972 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
974 else {
975 x = pRun->member.run.pt.x;
976 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
978 editor->nUDArrowX = x;
980 return x;
984 static void
985 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
987 ME_DisplayItem *pRun = pCursor->pRun;
988 ME_DisplayItem *pItem;
989 int x = ME_GetXForArrow(editor, pCursor);
991 if (editor->bCaretAtEnd && !pCursor->nOffset)
992 pRun = ME_FindItemBack(pRun, diRun);
993 if (!pRun)
994 return;
995 if (nRelOfs == -1)
997 /* start of this row */
998 pItem = ME_FindItemBack(pRun, diStartRow);
999 assert(pItem);
1000 /* start of the previous row */
1001 pItem = ME_FindItemBack(pItem, diStartRow);
1003 else
1005 /* start of the next row */
1006 pItem = ME_FindItemFwd(pRun, diStartRow);
1007 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1010 if (!pItem)
1012 /* row not found - ignore */
1013 return;
1015 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1016 assert(pCursor->pRun);
1017 assert(pCursor->pRun->type == diRun);
1021 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1023 ME_DisplayItem *pRun = pCursor->pRun;
1024 ME_DisplayItem *pLast, *p;
1025 int x, y, ys, yd, yp, yprev;
1026 ME_Cursor tmp_curs = *pCursor;
1028 x = ME_GetXForArrow(editor, pCursor);
1029 if (!pCursor->nOffset && editor->bCaretAtEnd)
1030 pRun = ME_FindItemBack(pRun, diRun);
1032 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1033 assert(p->type == diStartRow);
1034 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1035 yprev = ys = y = yp + p->member.row.nYPos;
1036 yd = y - editor->sizeWindow.cy;
1037 pLast = p;
1039 do {
1040 p = ME_FindItemBack(p, diStartRowOrParagraph);
1041 if (!p)
1042 break;
1043 if (p->type == diParagraph) { /* crossing paragraphs */
1044 if (p->member.para.prev_para == NULL)
1045 break;
1046 yp = p->member.para.prev_para->member.para.nYPos;
1047 continue;
1049 y = yp + p->member.row.nYPos;
1050 if (y < yd)
1051 break;
1052 pLast = p;
1053 yprev = y;
1054 } while(1);
1056 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1057 ME_UpdateSelection(editor, &tmp_curs);
1058 if (yprev < editor->sizeWindow.cy)
1060 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
1061 ME_Repaint(editor);
1063 else
1065 ME_ScrollUp(editor, ys-yprev);
1067 assert(pCursor->pRun);
1068 assert(pCursor->pRun->type == diRun);
1071 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1072 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1073 In such a situation, clicking the scrollbar restores its position back to the
1074 normal range (ie. sets it to (doclength-screenheight)). */
1076 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1078 ME_DisplayItem *pRun = pCursor->pRun;
1079 ME_DisplayItem *pLast, *p;
1080 int x, y, ys, yd, yp, yprev;
1081 ME_Cursor tmp_curs = *pCursor;
1083 x = ME_GetXForArrow(editor, pCursor);
1084 if (!pCursor->nOffset && editor->bCaretAtEnd)
1085 pRun = ME_FindItemBack(pRun, diRun);
1087 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1088 assert(p->type == diStartRow);
1089 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1090 yprev = ys = y = yp + p->member.row.nYPos;
1091 yd = y + editor->sizeWindow.cy;
1092 pLast = p;
1094 do {
1095 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1096 if (!p)
1097 break;
1098 if (p->type == diParagraph) {
1099 yp = p->member.para.nYPos;
1100 continue;
1102 y = yp + p->member.row.nYPos;
1103 if (y >= yd)
1104 break;
1105 pLast = p;
1106 yprev = y;
1107 } while(1);
1109 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1110 ME_UpdateSelection(editor, &tmp_curs);
1111 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
1113 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1114 ME_Repaint(editor);
1116 else
1118 ME_ScrollUp(editor,ys-yprev);
1120 assert(pCursor->pRun);
1121 assert(pCursor->pRun->type == diRun);
1124 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1126 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1127 /* bCaretAtEnd doesn't make sense if the cursor isn't set at the
1128 first character of the next row */
1129 assert(!editor->bCaretAtEnd || !pCursor->nOffset);
1130 ME_WrapMarkedParagraphs(editor);
1131 if (pRow) {
1132 ME_DisplayItem *pRun;
1133 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1134 pRow = ME_FindItemBack(pRow, diStartRow);
1135 if (!pRow)
1136 return;
1138 pRun = ME_FindItemFwd(pRow, diRun);
1139 if (pRun) {
1140 pCursor->pRun = pRun;
1141 pCursor->nOffset = 0;
1144 editor->bCaretAtEnd = FALSE;
1147 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1149 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1150 if (pRow) {
1151 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1152 if (pRun) {
1153 pCursor->pRun = pRun;
1154 pCursor->nOffset = 0;
1159 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1161 ME_DisplayItem *pRow;
1163 if (editor->bCaretAtEnd && !pCursor->nOffset)
1164 return;
1166 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1167 assert(pRow);
1168 if (pRow->type == diStartRow) {
1169 /* FIXME WTF was I thinking about here ? */
1170 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1171 assert(pRun);
1172 pCursor->pRun = pRun;
1173 pCursor->nOffset = 0;
1174 editor->bCaretAtEnd = 1;
1175 return;
1177 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1178 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1179 pCursor->nOffset = 0;
1180 editor->bCaretAtEnd = FALSE;
1183 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1185 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1186 assert(p);
1187 p = ME_FindItemBack(p, diRun);
1188 assert(p);
1189 assert(p->member.run.nFlags & MERF_ENDPARA);
1190 pCursor->pRun = p;
1191 pCursor->nOffset = 0;
1192 editor->bCaretAtEnd = FALSE;
1195 BOOL ME_IsSelection(ME_TextEditor *editor)
1197 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1200 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1202 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1204 if (cdir*dir>0)
1205 return 0;
1206 else
1207 return 1;
1210 BOOL ME_UpdateSelection(ME_TextEditor *editor, const ME_Cursor *pTempCursor)
1212 ME_Cursor old_anchor = editor->pCursors[1];
1214 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1216 /* any selection was present ? if so, it's no more, repaint ! */
1217 editor->pCursors[1] = editor->pCursors[0];
1218 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1219 return TRUE;
1221 return FALSE;
1223 else
1225 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1227 editor->pCursors[1] = *pTempCursor;
1228 return TRUE;
1232 ME_Repaint(editor);
1233 return TRUE;
1236 void ME_DeleteSelection(ME_TextEditor *editor)
1238 int from, to;
1239 ME_GetSelection(editor, &from, &to);
1240 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1243 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1245 ME_Style *style;
1246 int from, to;
1248 ME_GetSelection(editor, &from, &to);
1249 if (from != to) {
1250 ME_Cursor c;
1251 ME_CursorFromCharOfs(editor, from, &c);
1252 style = c.pRun->member.run.style;
1253 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1255 else
1256 style = ME_GetInsertStyle(editor, 0);
1257 return style;
1260 void ME_SendSelChange(ME_TextEditor *editor)
1262 SELCHANGE sc;
1264 ME_ClearTempStyle(editor);
1266 if (!(editor->nEventMask & ENM_SELCHANGE))
1267 return;
1269 sc.nmhdr.hwndFrom = editor->hWnd;
1270 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1271 sc.nmhdr.code = EN_SELCHANGE;
1272 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1273 sc.seltyp = SEL_EMPTY;
1274 if (sc.chrg.cpMin != sc.chrg.cpMax)
1275 sc.seltyp |= SEL_TEXT;
1276 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1277 sc.seltyp |= SEL_MULTICHAR;
1278 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1281 BOOL
1282 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1284 int nCursor = 0;
1285 ME_Cursor *p = &editor->pCursors[nCursor];
1286 ME_Cursor tmp_curs = *p;
1287 BOOL success = FALSE;
1289 ME_CheckCharOffsets(editor);
1290 editor->nUDArrowX = -1;
1291 switch(nVKey) {
1292 case VK_LEFT:
1293 editor->bCaretAtEnd = 0;
1294 if (ctrl)
1295 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1296 else
1297 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1298 break;
1299 case VK_RIGHT:
1300 editor->bCaretAtEnd = 0;
1301 if (ctrl)
1302 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1303 else
1304 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1305 break;
1306 case VK_UP:
1307 ME_MoveCursorLines(editor, &tmp_curs, -1);
1308 break;
1309 case VK_DOWN:
1310 ME_MoveCursorLines(editor, &tmp_curs, +1);
1311 break;
1312 case VK_PRIOR:
1313 ME_ArrowPageUp(editor, &tmp_curs);
1314 break;
1315 case VK_NEXT:
1316 ME_ArrowPageDown(editor, &tmp_curs);
1317 break;
1318 case VK_HOME: {
1319 if (ctrl)
1320 ME_ArrowCtrlHome(editor, &tmp_curs);
1321 else
1322 ME_ArrowHome(editor, &tmp_curs);
1323 editor->bCaretAtEnd = 0;
1324 break;
1326 case VK_END:
1327 if (ctrl)
1328 ME_ArrowCtrlEnd(editor, &tmp_curs);
1329 else
1330 ME_ArrowEnd(editor, &tmp_curs);
1331 break;
1334 if (!extend)
1335 editor->pCursors[1] = tmp_curs;
1336 *p = tmp_curs;
1338 ME_InvalidateSelection(editor);
1339 ME_Repaint(editor);
1340 HideCaret(editor->hWnd);
1341 ME_EnsureVisible(editor, tmp_curs.pRun);
1342 ME_ShowCaret(editor);
1343 ME_SendSelChange(editor);
1344 return success;