dsound: Call waveInUnPrepareHeader and waveInPrepareHeader when submitting new buffer.
[wine/multimedia.git] / dlls / riched20 / caret.c
blob47e816cc4ccd16c51899710f2a90e00d9fb88e75
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);
31 void ME_GetSelection(ME_TextEditor *editor, int *from, int *to)
33 *from = ME_GetCursorOfs(editor, 0);
34 *to = ME_GetCursorOfs(editor, 1);
36 if (*from > *to)
38 int tmp = *from;
39 *from = *to;
40 *to = tmp;
44 int ME_GetTextLength(ME_TextEditor *editor)
46 return ME_CharOfsFromRunOfs(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun), 0);
50 int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how)
52 int length;
54 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
55 return E_INVALIDARG;
56 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
57 return E_INVALIDARG;
59 length = ME_GetTextLength(editor);
61 if ((GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_MULTILINE) && (how->flags & GTL_USECRLF))
62 length += editor->nParagraphs;
64 if (how->flags & GTL_NUMBYTES)
66 CPINFO cpinfo;
68 if (how->codepage == 1200)
69 return length * 2;
70 if (how->flags & GTL_PRECISE)
71 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
72 if (GetCPInfo(how->codepage, &cpinfo))
73 return length * cpinfo.MaxCharSize;
74 ERR("Invalid codepage %u\n", how->codepage);
75 return E_INVALIDARG;
77 return length;
81 int ME_SetSelection(ME_TextEditor *editor, int from, int to)
83 int selectionEnd = 0;
84 const int len = ME_GetTextLength(editor);
86 /* all negative values are effectively the same */
87 if (from < 0)
88 from = -1;
89 if (to < 0)
90 to = -1;
92 /* select all */
93 if (from == 0 && to == -1)
95 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
96 editor->pCursors[1].nOffset = 0;
97 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
98 editor->pCursors[0].nOffset = 0;
99 ME_InvalidateSelection(editor);
100 ME_ClearTempStyle(editor);
101 return len + 1;
104 /* if both values are equal and also out of bound, that means to */
105 /* put the selection at the end of the text */
106 if ((from == to) && (to < 0 || to > len))
108 selectionEnd = 1;
110 else
112 /* if from is negative and to is positive then selection is */
113 /* deselected and caret moved to end of the current selection */
114 if (from < 0)
116 int start, end;
117 ME_GetSelection(editor, &start, &end);
118 editor->pCursors[1] = editor->pCursors[0];
119 ME_Repaint(editor);
120 ME_ClearTempStyle(editor);
121 return end;
124 /* adjust to if it's a negative value */
125 if (to < 0)
126 to = len + 1;
128 /* flip from and to if they are reversed */
129 if (from>to)
131 int tmp = from;
132 from = to;
133 to = tmp;
136 /* after fiddling with the values, we find from > len && to > len */
137 if (from > len)
138 selectionEnd = 1;
139 /* special case with to too big */
140 else if (to > len)
141 to = len + 1;
144 if (selectionEnd)
146 editor->pCursors[1].pRun = editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
147 editor->pCursors[1].nOffset = editor->pCursors[0].nOffset = 0;
148 ME_InvalidateSelection(editor);
149 ME_ClearTempStyle(editor);
150 return len;
153 ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
154 ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);
155 return to;
159 void
160 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
161 int *x, int *y, int *height)
163 ME_DisplayItem *pCursorRun = pCursor->pRun;
164 ME_DisplayItem *pSizeRun = pCursor->pRun;
166 assert(!pCursor->nOffset || !editor->bCaretAtEnd);
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 && !editor->bCaretAtEnd)
186 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrStartRow);
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, &run->member.run, ME_StrLen(run->member.run.strText));
209 if (pCursor->nOffset && !(run->member.run.nFlags & MERF_SKIPPED)) {
210 sz = ME_GetRunSize(&c, &para->member.para, &run->member.run, pCursor->nOffset);
213 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
214 *x = run->member.run.pt.x + sz.cx;
215 *y = para->member.para.nYPos + row->member.row.nBaseline + pSizeRun->member.run.pt.y - pSizeRun->member.run.nAscent - ME_GetYScrollPos(editor);
217 ME_DestroyContext(&c);
218 ReleaseDC(editor->hWnd, hDC);
219 return;
222 *height = 10; /* FIXME use global font */
223 *x = 0;
224 *y = 0;
228 void
229 ME_MoveCaret(ME_TextEditor *editor)
231 int x, y, height;
233 if (ME_WrapMarkedParagraphs(editor))
234 ME_UpdateScrollBar(editor);
235 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
236 if(editor->bHaveFocus)
238 CreateCaret(editor->hWnd, NULL, 0, height);
239 SetCaretPos(x, y);
244 void ME_ShowCaret(ME_TextEditor *ed)
246 ME_MoveCaret(ed);
247 if(ed->bHaveFocus)
248 ShowCaret(ed->hWnd);
251 void ME_HideCaret(ME_TextEditor *ed)
253 if(ed->bHaveFocus)
255 HideCaret(ed->hWnd);
256 DestroyCaret();
260 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
261 int nChars)
263 ME_Cursor c;
264 int shift = 0;
266 while(nChars > 0)
268 ME_Run *run;
269 ME_CursorFromCharOfs(editor, nOfs, &c);
270 run = &c.pRun->member.run;
271 if (run->nFlags & MERF_ENDPARA) {
272 if (!ME_FindItemFwd(c.pRun, diParagraph))
274 return;
276 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
277 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
278 ME_CheckCharOffsets(editor);
279 nChars--;
280 if (editor->bEmulateVersion10 && nChars)
281 nChars--;
282 continue;
284 else
286 ME_Cursor cursor;
287 int nIntendedChars = nChars;
288 int nCharsToDelete = nChars;
289 int i;
290 int loc = c.nOffset;
292 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
294 cursor = c;
295 ME_StrRelPos(run->strText, loc, &nChars);
296 /* nChars is the number of characters that should be deleted from the
297 FOLLOWING runs (these AFTER cursor.pRun)
298 nCharsToDelete is a number of chars to delete from THIS run */
299 nCharsToDelete -= nChars;
300 shift -= nCharsToDelete;
301 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
302 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
303 debugstr_w(run->strText->szData), run->strText->nLen);
305 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
307 /* undo = reinsert whole run */
308 /* nOfs is a character offset (from the start of the document
309 to the current (deleted) run */
310 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
311 if (pUndo)
312 pUndo->di.member.run.nCharOfs = nOfs;
314 else
316 /* undo = reinsert partial run */
317 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
318 if (pUndo) {
319 ME_DestroyString(pUndo->di.member.run.strText);
320 pUndo->di.member.run.nCharOfs = nOfs;
321 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
324 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
325 TRACE("Shift value: %d\n", shift);
326 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
328 /* update cursors (including c) */
329 for (i=-1; i<editor->nCursors; i++) {
330 ME_Cursor *pThisCur = editor->pCursors + i;
331 if (i == -1) pThisCur = &c;
332 if (pThisCur->pRun == cursor.pRun) {
333 if (pThisCur->nOffset > cursor.nOffset) {
334 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
335 pThisCur->nOffset = cursor.nOffset;
336 else
337 pThisCur->nOffset -= nCharsToDelete;
338 assert(pThisCur->nOffset >= 0);
339 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
341 if (pThisCur->nOffset == ME_StrVLen(run->strText))
343 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
344 assert(pThisCur->pRun->type == diRun);
345 pThisCur->nOffset = 0;
350 /* c = updated data now */
352 if (c.pRun == cursor.pRun)
353 ME_SkipAndPropagateCharOffset(c.pRun, shift);
354 else
355 ME_PropagateCharOffset(c.pRun, shift);
357 if (!ME_StrVLen(cursor.pRun->member.run.strText))
359 TRACE("Removing useless run\n");
360 ME_Remove(cursor.pRun);
361 ME_DestroyDisplayItem(cursor.pRun);
364 shift = 0;
366 ME_CheckCharOffsets(editor);
368 continue;
373 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
374 int nChars)
376 assert(nCursor>=0 && nCursor<editor->nCursors);
377 /* text operations set modified state */
378 editor->nModifyStep = 1;
379 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
382 static ME_DisplayItem *
383 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
384 const WCHAR *str, int len, ME_Style *style,
385 int flags)
387 ME_Cursor *p = &editor->pCursors[nCursor];
389 editor->bCaretAtEnd = FALSE;
391 assert(p->pRun->type == diRun);
393 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
397 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
398 void ME_InsertGraphicsFromCursor(ME_TextEditor *editor, int nCursor)
400 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
401 WCHAR space = ' ';
403 /* FIXME no no no */
404 if (ME_IsSelection(editor))
405 ME_DeleteSelection(editor);
407 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
408 MERF_GRAPHICS);
409 ME_SendSelChange(editor);
413 void
414 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
416 WCHAR tab = '\t';
417 ME_DisplayItem *p, *run;
418 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
420 p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
421 MERF_CELL);
422 run = p;
423 while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
425 if (run->member.run.nFlags & MERF_CELL)
427 assert(run->member.run.pCell->next);
428 p->member.run.pCell = run->member.run.pCell->next;
429 return;
432 assert(run->type == diParagraph);
433 assert(run->member.para.bTable);
434 assert(run->member.para.pCells);
435 p->member.run.pCell = run->member.para.pCells;
439 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
440 const WCHAR *str, int len, ME_Style *style)
442 const WCHAR *pos;
443 ME_Cursor *p = NULL;
444 int oldLen;
446 /* FIXME really HERE ? */
447 if (ME_IsSelection(editor))
448 ME_DeleteSelection(editor);
450 /* FIXME: is this too slow? */
451 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
452 oldLen = ME_GetTextLength(editor);
454 /* text operations set modified state */
455 editor->nModifyStep = 1;
457 assert(style);
459 assert(nCursor>=0 && nCursor<editor->nCursors);
460 if (len == -1)
461 len = lstrlenW(str);
463 /* grow the text limit to fit our text */
464 if(editor->nTextLimit < oldLen +len)
465 editor->nTextLimit = oldLen + len;
467 while (len)
469 pos = str;
470 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
471 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
472 pos++;
473 if (pos-str < len && *pos == '\t') { /* handle tabs */
474 WCHAR tab = '\t';
476 if (pos!=str)
477 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
479 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
481 pos++;
482 if(pos-str <= len) {
483 len -= pos - str;
484 str = pos;
485 continue;
488 if (pos-str < len) { /* handle EOLs */
489 ME_DisplayItem *tp, *end_run;
490 ME_Style *tmp_style;
491 if (pos!=str)
492 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
493 p = &editor->pCursors[nCursor];
494 if (p->nOffset) {
495 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
496 p = &editor->pCursors[nCursor];
498 tmp_style = ME_GetInsertStyle(editor, nCursor);
499 /* ME_SplitParagraph increases style refcount */
500 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
501 p->pRun = ME_FindItemFwd(tp, diRun);
502 end_run = ME_FindItemBack(tp, diRun);
503 ME_ReleaseStyle(end_run->member.run.style);
504 end_run->member.run.style = tmp_style;
505 p->nOffset = 0;
506 if(pos-str < len && *pos =='\r')
507 pos++;
508 if(pos-str < len && *pos =='\n')
509 pos++;
510 if(pos-str <= len) {
511 len -= pos - str;
512 str = pos;
513 continue;
516 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
517 len = 0;
522 static BOOL
523 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
525 ME_DisplayItem *pRun = pCursor->pRun;
527 if (nRelOfs == -1)
529 if (!pCursor->nOffset)
531 do {
532 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
533 assert(pRun);
534 switch (pRun->type)
536 case diRun:
537 break;
538 case diParagraph:
539 if (pRun->member.para.prev_para->type == diTextStart)
540 return FALSE;
541 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
542 /* every paragraph ought to have at least one run */
543 assert(pRun && pRun->type == diRun);
544 assert(pRun->member.run.nFlags & MERF_ENDPARA);
545 break;
546 default:
547 assert(pRun->type != diRun && pRun->type != diParagraph);
548 return FALSE;
550 } while (RUN_IS_HIDDEN(&pRun->member.run));
551 pCursor->pRun = pRun;
552 if (pRun->member.run.nFlags & MERF_ENDPARA)
553 pCursor->nOffset = 0;
554 else
555 pCursor->nOffset = pRun->member.run.strText->nLen;
558 if (pCursor->nOffset)
559 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
560 return TRUE;
562 else
564 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
566 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
568 if (new_ofs < pRun->member.run.strText->nLen)
570 pCursor->nOffset = new_ofs;
571 return TRUE;
574 do {
575 pRun = ME_FindItemFwd(pRun, diRun);
576 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
577 if (pRun)
579 pCursor->pRun = pRun;
580 pCursor->nOffset = 0;
581 return TRUE;
584 return FALSE;
588 static BOOL
589 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
591 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
592 int nOffset = cursor->nOffset;
594 if (nRelOfs == -1)
596 /* Backward movement */
597 while (TRUE)
599 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
600 nOffset, WB_MOVEWORDLEFT);
601 if (nOffset)
602 break;
603 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
604 if (pOtherRun->type == diRun)
606 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
607 pOtherRun->member.run.strText->nLen - 1,
608 WB_ISDELIMITER)
609 && !(pRun->member.run.nFlags & MERF_ENDPARA)
610 && !(cursor->pRun == pRun && cursor->nOffset == 0)
611 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
612 WB_ISDELIMITER))
613 break;
614 pRun = pOtherRun;
615 nOffset = pOtherRun->member.run.strText->nLen;
617 else if (pOtherRun->type == diParagraph)
619 if (cursor->pRun == pRun && cursor->nOffset == 0)
621 /* Paragraph breaks are treated as separate words */
622 if (pOtherRun->member.para.prev_para->type == diTextStart)
623 return FALSE;
624 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
626 break;
630 else
632 /* Forward movement */
633 BOOL last_delim = FALSE;
635 while (TRUE)
637 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
638 nOffset, WB_ISDELIMITER))
639 break;
640 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
641 nOffset, WB_MOVEWORDRIGHT);
642 if (nOffset < pRun->member.run.strText->nLen)
643 break;
644 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
645 if (pOtherRun->type == diRun)
647 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
648 nOffset - 1, WB_ISDELIMITER);
649 pRun = pOtherRun;
650 nOffset = 0;
652 else if (pOtherRun->type == diParagraph)
654 if (cursor->pRun == pRun)
655 pRun = ME_FindItemFwd(pOtherRun, diRun);
656 nOffset = 0;
657 break;
659 else /* diTextEnd */
661 if (cursor->pRun == pRun)
662 return FALSE;
663 nOffset = 0;
664 break;
668 cursor->pRun = pRun;
669 cursor->nOffset = nOffset;
670 return TRUE;
674 void
675 ME_SelectWord(ME_TextEditor *editor)
677 if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
678 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
679 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
680 ME_InvalidateSelection(editor);
681 ME_SendSelChange(editor);
685 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
687 ME_Cursor *pCursor = &editor->pCursors[nCursor];
689 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
690 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
693 int ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
695 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
696 int rx = 0;
698 if (is_eol)
699 *is_eol = 0;
701 while(p != editor->pBuffer->pLast)
703 if (p->type == diParagraph)
705 int ry = y - p->member.para.nYPos;
706 if (ry < 0)
708 result->pRun = ME_FindItemFwd(p, diRun);
709 result->nOffset = 0;
710 return 0;
712 if (ry >= p->member.para.nHeight)
714 p = p->member.para.next_para;
715 continue;
717 p = ME_FindItemFwd(p, diStartRow);
718 y = ry;
719 continue;
721 if (p->type == diStartRow)
723 int ry = y - p->member.row.nYPos;
724 if (ry < 0)
725 return 0;
726 if (ry >= p->member.row.nHeight)
728 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
729 if (p->type != diStartRow)
730 return 0;
731 continue;
733 p = ME_FindItemFwd(p, diRun);
734 continue;
736 if (p->type == diRun)
738 ME_DisplayItem *pp;
739 rx = x - p->member.run.pt.x;
740 if (rx < 0)
741 rx = 0;
742 if (rx >= p->member.run.nWidth) /* not this run yet... find next item */
744 pp = p;
745 do {
746 p = p->next;
747 if (p->type == diRun)
749 rx = x - p->member.run.pt.x;
750 goto continue_search;
752 if (p->type == diStartRow)
754 p = ME_FindItemFwd(p, diRun);
755 if (is_eol)
756 *is_eol = 1;
757 rx = 0; /* FIXME not sure */
758 goto found_here;
760 if (p->type == diParagraph || p->type == diTextEnd)
762 rx = 0; /* FIXME not sure */
763 p = pp;
764 goto found_here;
766 } while(1);
767 continue;
769 found_here:
770 if (p->member.run.nFlags & MERF_ENDPARA)
771 rx = 0;
772 result->pRun = p;
773 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
774 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
776 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
777 result->nOffset = 0;
779 return 1;
781 assert(0);
782 continue_search:
785 result->pRun = ME_FindItemBack(p, diRun);
786 result->nOffset = 0;
787 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
788 return 0;
793 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
795 ME_Cursor cursor;
796 RECT rc;
798 GetClientRect(editor->hWnd, &rc);
799 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
800 return -1;
801 y += ME_GetYScrollPos(editor);
802 ME_FindPixelPos(editor, x, y, &cursor, NULL);
803 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
804 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
808 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
810 ME_Cursor tmp_cursor;
811 int is_selection = 0;
813 editor->nUDArrowX = -1;
815 y += ME_GetYScrollPos(editor);
817 tmp_cursor = editor->pCursors[0];
818 is_selection = ME_IsSelection(editor);
820 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
822 if (GetKeyState(VK_SHIFT)>=0)
824 editor->pCursors[1] = editor->pCursors[0];
826 else
828 if (!is_selection) {
829 editor->pCursors[1] = tmp_cursor;
830 is_selection = 1;
833 ME_InvalidateSelection(editor);
834 HideCaret(editor->hWnd);
835 ME_MoveCaret(editor);
836 ShowCaret(editor->hWnd);
837 ME_ClearTempStyle(editor);
838 ME_SendSelChange(editor);
841 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
843 ME_Cursor tmp_cursor;
845 y += ME_GetYScrollPos(editor);
847 tmp_cursor = editor->pCursors[0];
848 /* FIXME: do something with the return value of ME_FindPixelPos */
849 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
851 if (tmp_cursor.pRun == editor->pCursors[0].pRun &&
852 tmp_cursor.nOffset == editor->pCursors[0].nOffset)
853 return;
855 ME_InvalidateSelection(editor);
856 editor->pCursors[0] = tmp_cursor;
857 HideCaret(editor->hWnd);
858 ME_MoveCaret(editor);
859 ME_InvalidateSelection(editor);
860 ShowCaret(editor->hWnd);
861 ME_SendSelChange(editor);
864 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
865 int x, int *pOffset, int *pbCaretAtEnd)
867 ME_DisplayItem *pNext, *pLastRun;
868 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
869 assert(pNext->type == diRun);
870 pLastRun = pNext;
871 *pbCaretAtEnd = FALSE;
872 do {
873 int run_x = pNext->member.run.pt.x;
874 int width = pNext->member.run.nWidth;
875 if (x < run_x)
877 if (pOffset) *pOffset = 0;
878 return pNext;
880 if (x >= run_x && x < run_x+width)
882 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
883 ME_String *s = pNext->member.run.strText;
884 if (ch < s->nLen) {
885 if (pOffset)
886 *pOffset = ch;
887 return pNext;
890 pLastRun = pNext;
891 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
892 } while(pNext && pNext->type == diRun);
894 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
896 pNext = ME_FindItemFwd(pNext, diRun);
897 if (pbCaretAtEnd) *pbCaretAtEnd = 1;
898 if (pOffset) *pOffset = 0;
899 return pNext;
900 } else {
901 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
902 if (pOffset) *pOffset = 0;
903 return pLastRun;
907 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
909 ME_DisplayItem *pRun = pCursor->pRun;
910 int x;
912 if (editor->nUDArrowX != -1)
913 x = editor->nUDArrowX;
914 else {
915 if (editor->bCaretAtEnd)
917 pRun = ME_FindItemBack(pRun, diRun);
918 assert(pRun);
919 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
921 else {
922 x = pRun->member.run.pt.x;
923 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
925 editor->nUDArrowX = x;
927 return x;
931 static void
932 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
934 ME_DisplayItem *pRun = pCursor->pRun;
935 ME_DisplayItem *pItem;
936 int x = ME_GetXForArrow(editor, pCursor);
938 if (editor->bCaretAtEnd && !pCursor->nOffset)
939 pRun = ME_FindItemBack(pRun, diRun);
940 if (!pRun)
941 return;
942 if (nRelOfs == -1)
944 /* start of this row */
945 pItem = ME_FindItemBack(pRun, diStartRow);
946 assert(pItem);
947 /* start of the previous row */
948 pItem = ME_FindItemBack(pItem, diStartRow);
950 else
952 /* start of the next row */
953 pItem = ME_FindItemFwd(pRun, diStartRow);
954 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
957 if (!pItem)
959 /* row not found - ignore */
960 return;
962 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
963 assert(pCursor->pRun);
964 assert(pCursor->pRun->type == diRun);
968 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
970 ME_DisplayItem *pRun = pCursor->pRun;
971 ME_DisplayItem *pLast, *p;
972 int x, y, ys, yd, yp, yprev;
973 ME_Cursor tmp_curs = *pCursor;
975 x = ME_GetXForArrow(editor, pCursor);
976 if (!pCursor->nOffset && editor->bCaretAtEnd)
977 pRun = ME_FindItemBack(pRun, diRun);
979 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
980 assert(p->type == diStartRow);
981 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
982 yprev = ys = y = yp + p->member.row.nYPos;
983 yd = y - editor->sizeWindow.cy;
984 pLast = p;
986 do {
987 p = ME_FindItemBack(p, diStartRowOrParagraph);
988 if (!p)
989 break;
990 if (p->type == diParagraph) { /* crossing paragraphs */
991 if (p->member.para.prev_para == NULL)
992 break;
993 yp = p->member.para.prev_para->member.para.nYPos;
994 continue;
996 y = yp + p->member.row.nYPos;
997 if (y < yd)
998 break;
999 pLast = p;
1000 yprev = y;
1001 } while(1);
1003 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1004 ME_UpdateSelection(editor, &tmp_curs);
1005 if (yprev < editor->sizeWindow.cy)
1007 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
1008 ME_Repaint(editor);
1010 else
1012 ME_ScrollUp(editor, ys-yprev);
1014 assert(pCursor->pRun);
1015 assert(pCursor->pRun->type == diRun);
1018 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1019 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1020 In such a situation, clicking the scrollbar restores its position back to the
1021 normal range (ie. sets it to (doclength-screenheight)). */
1023 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1025 ME_DisplayItem *pRun = pCursor->pRun;
1026 ME_DisplayItem *pLast, *p;
1027 int x, y, ys, yd, yp, yprev;
1028 ME_Cursor tmp_curs = *pCursor;
1030 x = ME_GetXForArrow(editor, pCursor);
1031 if (!pCursor->nOffset && editor->bCaretAtEnd)
1032 pRun = ME_FindItemBack(pRun, diRun);
1034 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1035 assert(p->type == diStartRow);
1036 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1037 yprev = ys = y = yp + p->member.row.nYPos;
1038 yd = y + editor->sizeWindow.cy;
1039 pLast = p;
1041 do {
1042 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1043 if (!p)
1044 break;
1045 if (p->type == diParagraph) {
1046 yp = p->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->nTotalLength-editor->sizeWindow.cy)
1060 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1061 ME_Repaint(editor);
1063 else
1065 ME_ScrollUp(editor,ys-yprev);
1067 assert(pCursor->pRun);
1068 assert(pCursor->pRun->type == diRun);
1071 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1073 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1074 /* bCaretAtEnd doesn't make sense if the cursor isn't set at the
1075 first character of the next row */
1076 assert(!editor->bCaretAtEnd || !pCursor->nOffset);
1077 ME_WrapMarkedParagraphs(editor);
1078 if (pRow) {
1079 ME_DisplayItem *pRun;
1080 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1081 pRow = ME_FindItemBack(pRow, diStartRow);
1082 if (!pRow)
1083 return;
1085 pRun = ME_FindItemFwd(pRow, diRun);
1086 if (pRun) {
1087 pCursor->pRun = pRun;
1088 pCursor->nOffset = 0;
1091 editor->bCaretAtEnd = FALSE;
1094 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1096 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1097 if (pRow) {
1098 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1099 if (pRun) {
1100 pCursor->pRun = pRun;
1101 pCursor->nOffset = 0;
1106 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1108 ME_DisplayItem *pRow;
1110 if (editor->bCaretAtEnd && !pCursor->nOffset)
1111 return;
1113 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1114 assert(pRow);
1115 if (pRow->type == diStartRow) {
1116 /* FIXME WTF was I thinking about here ? */
1117 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1118 assert(pRun);
1119 pCursor->pRun = pRun;
1120 pCursor->nOffset = 0;
1121 editor->bCaretAtEnd = 1;
1122 return;
1124 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1125 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1126 pCursor->nOffset = 0;
1127 editor->bCaretAtEnd = FALSE;
1130 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1132 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1133 assert(p);
1134 p = ME_FindItemBack(p, diRun);
1135 assert(p);
1136 assert(p->member.run.nFlags & MERF_ENDPARA);
1137 pCursor->pRun = p;
1138 pCursor->nOffset = 0;
1139 editor->bCaretAtEnd = FALSE;
1142 BOOL ME_IsSelection(ME_TextEditor *editor)
1144 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1147 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1149 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1151 if (cdir*dir>0)
1152 return 0;
1153 else
1154 return 1;
1157 BOOL ME_UpdateSelection(ME_TextEditor *editor, const ME_Cursor *pTempCursor)
1159 ME_Cursor old_anchor = editor->pCursors[1];
1161 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1163 /* any selection was present ? if so, it's no more, repaint ! */
1164 editor->pCursors[1] = editor->pCursors[0];
1165 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1166 return TRUE;
1168 return FALSE;
1170 else
1172 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1174 editor->pCursors[1] = *pTempCursor;
1175 return TRUE;
1179 ME_Repaint(editor);
1180 return TRUE;
1183 void ME_DeleteSelection(ME_TextEditor *editor)
1185 int from, to;
1186 ME_GetSelection(editor, &from, &to);
1187 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1190 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1192 ME_Style *style;
1193 int from, to;
1194 ME_Cursor c;
1196 ME_GetSelection(editor, &from, &to);
1197 ME_CursorFromCharOfs(editor, from, &c);
1198 if (from != to) {
1199 style = c.pRun->member.run.style;
1200 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1202 else
1203 style = ME_GetInsertStyle(editor, 0);
1204 return style;
1207 void ME_SendSelChange(ME_TextEditor *editor)
1209 SELCHANGE sc;
1211 ME_ClearTempStyle(editor);
1213 if (!(editor->nEventMask & ENM_SELCHANGE))
1214 return;
1216 sc.nmhdr.hwndFrom = editor->hWnd;
1217 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1218 sc.nmhdr.code = EN_SELCHANGE;
1219 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1220 sc.seltyp = SEL_EMPTY;
1221 if (sc.chrg.cpMin != sc.chrg.cpMax)
1222 sc.seltyp |= SEL_TEXT;
1223 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1224 sc.seltyp |= SEL_MULTICHAR;
1225 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1229 BOOL
1230 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1232 int nCursor = 0;
1233 ME_Cursor *p = &editor->pCursors[nCursor];
1234 ME_Cursor tmp_curs = *p;
1235 BOOL success = FALSE;
1237 ME_CheckCharOffsets(editor);
1238 editor->nUDArrowX = -1;
1239 switch(nVKey) {
1240 case VK_LEFT:
1241 editor->bCaretAtEnd = 0;
1242 if (ctrl)
1243 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1244 else
1245 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1246 break;
1247 case VK_RIGHT:
1248 editor->bCaretAtEnd = 0;
1249 if (ctrl)
1250 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1251 else
1252 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1253 break;
1254 case VK_UP:
1255 ME_MoveCursorLines(editor, &tmp_curs, -1);
1256 break;
1257 case VK_DOWN:
1258 ME_MoveCursorLines(editor, &tmp_curs, +1);
1259 break;
1260 case VK_PRIOR:
1261 ME_ArrowPageUp(editor, &tmp_curs);
1262 break;
1263 case VK_NEXT:
1264 ME_ArrowPageDown(editor, &tmp_curs);
1265 break;
1266 case VK_HOME: {
1267 if (ctrl)
1268 ME_ArrowCtrlHome(editor, &tmp_curs);
1269 else
1270 ME_ArrowHome(editor, &tmp_curs);
1271 editor->bCaretAtEnd = 0;
1272 break;
1274 case VK_END:
1275 if (ctrl)
1276 ME_ArrowCtrlEnd(editor, &tmp_curs);
1277 else
1278 ME_ArrowEnd(editor, &tmp_curs);
1279 break;
1282 if (!extend)
1283 editor->pCursors[1] = tmp_curs;
1284 *p = tmp_curs;
1286 ME_InvalidateSelection(editor);
1287 ME_Repaint(editor);
1288 HideCaret(editor->hWnd);
1289 ME_EnsureVisible(editor, tmp_curs.pRun);
1290 ME_ShowCaret(editor);
1291 ME_SendSelChange(editor);
1292 return success;