Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / riched20 / caret.c
blob3f9547e7c25d62b0f23391b5e470d67df6f990ac
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, 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 (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 ME_WrapMarkedParagraphs(editor);
234 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
235 CreateCaret(editor->hWnd, NULL, 0, height);
236 SetCaretPos(x, y);
240 void ME_ShowCaret(ME_TextEditor *ed)
242 ME_MoveCaret(ed);
243 ShowCaret(ed->hWnd);
246 void ME_HideCaret(ME_TextEditor *ed)
248 HideCaret(ed->hWnd);
249 DestroyCaret();
252 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
253 int nChars)
255 ME_Cursor c;
256 int shift = 0;
258 while(nChars > 0)
260 ME_Run *run;
261 ME_CursorFromCharOfs(editor, nOfs, &c);
262 run = &c.pRun->member.run;
263 if (run->nFlags & MERF_ENDPARA) {
264 if (!ME_FindItemFwd(c.pRun, diParagraph))
266 return;
268 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
269 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
270 ME_CheckCharOffsets(editor);
271 nChars--;
272 if (editor->bEmulateVersion10 && nChars)
273 nChars--;
274 continue;
276 else
278 ME_Cursor cursor;
279 int nIntendedChars = nChars;
280 int nCharsToDelete = nChars;
281 int i;
282 int loc = c.nOffset;
284 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
286 cursor = c;
287 ME_StrRelPos(run->strText, loc, &nChars);
288 /* nChars is the number of characters that should be deleted from the
289 FOLLOWING runs (these AFTER cursor.pRun)
290 nCharsToDelete is a number of chars to delete from THIS run */
291 nCharsToDelete -= nChars;
292 shift -= nCharsToDelete;
293 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
294 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
295 debugstr_w(run->strText->szData), run->strText->nLen);
297 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
299 /* undo = reinsert whole run */
300 /* nOfs is a character offset (from the start of the document
301 to the current (deleted) run */
302 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
303 if (pUndo)
304 pUndo->di.member.run.nCharOfs = nOfs;
306 else
308 /* undo = reinsert partial run */
309 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
310 if (pUndo) {
311 ME_DestroyString(pUndo->di.member.run.strText);
312 pUndo->di.member.run.nCharOfs = nOfs;
313 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
316 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
317 TRACE("Shift value: %d\n", shift);
318 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
320 /* update cursors (including c) */
321 for (i=-1; i<editor->nCursors; i++) {
322 ME_Cursor *pThisCur = editor->pCursors + i;
323 if (i == -1) pThisCur = &c;
324 if (pThisCur->pRun == cursor.pRun) {
325 if (pThisCur->nOffset > cursor.nOffset) {
326 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
327 pThisCur->nOffset = cursor.nOffset;
328 else
329 pThisCur->nOffset -= nCharsToDelete;
330 assert(pThisCur->nOffset >= 0);
331 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
333 if (pThisCur->nOffset == ME_StrVLen(run->strText))
335 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
336 assert(pThisCur->pRun->type == diRun);
337 pThisCur->nOffset = 0;
342 /* c = updated data now */
344 if (c.pRun == cursor.pRun)
345 ME_SkipAndPropagateCharOffset(c.pRun, shift);
346 else
347 ME_PropagateCharOffset(c.pRun, shift);
349 if (!ME_StrVLen(cursor.pRun->member.run.strText))
351 TRACE("Removing useless run\n");
352 ME_Remove(cursor.pRun);
353 ME_DestroyDisplayItem(cursor.pRun);
356 shift = 0;
358 ME_CheckCharOffsets(editor);
360 continue;
365 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
366 int nChars)
368 assert(nCursor>=0 && nCursor<editor->nCursors);
369 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
372 static ME_DisplayItem *
373 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
374 const WCHAR *str, int len, ME_Style *style,
375 int flags)
377 ME_Cursor *p = &editor->pCursors[nCursor];
379 editor->bCaretAtEnd = FALSE;
381 assert(p->pRun->type == diRun);
383 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
387 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
388 void ME_InsertGraphicsFromCursor(ME_TextEditor *editor, int nCursor)
390 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
391 WCHAR space = ' ';
393 /* FIXME no no no */
394 if (ME_IsSelection(editor))
395 ME_DeleteSelection(editor);
397 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
398 MERF_GRAPHICS);
399 ME_SendSelChange(editor);
403 void
404 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
406 WCHAR tab = '\t';
407 ME_DisplayItem *p, *run;
408 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
410 p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
411 MERF_CELL);
412 run = p;
413 while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
415 if (run->member.run.nFlags & MERF_CELL)
417 assert(run->member.run.pCell->next);
418 p->member.run.pCell = run->member.run.pCell->next;
419 return;
422 assert(run->type == diParagraph);
423 assert(run->member.para.bTable);
424 assert(run->member.para.pCells);
425 p->member.run.pCell = run->member.para.pCells;
429 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
430 const WCHAR *str, int len, ME_Style *style)
432 const WCHAR *pos;
433 ME_Cursor *p = NULL;
434 int freeSpace;
436 /* FIXME really HERE ? */
437 if (ME_IsSelection(editor))
438 ME_DeleteSelection(editor);
440 /* FIXME: is this too slow? */
441 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
442 freeSpace = editor->nTextLimit - ME_GetTextLength(editor);
444 /* text operations set modified state */
445 editor->nModifyStep = 1;
447 assert(style);
449 assert(nCursor>=0 && nCursor<editor->nCursors);
450 if (len == -1)
451 len = lstrlenW(str);
452 len = min(len, freeSpace);
453 while (len)
455 pos = str;
456 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
457 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
458 pos++;
459 if (pos-str < len && *pos == '\t') { /* handle tabs */
460 WCHAR tab = '\t';
462 if (pos!=str)
463 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
465 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
467 pos++;
468 if(pos-str <= len) {
469 len -= pos - str;
470 str = pos;
471 continue;
474 if (pos-str < len) { /* handle EOLs */
475 ME_DisplayItem *tp, *end_run;
476 ME_Style *tmp_style;
477 if (pos!=str)
478 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
479 p = &editor->pCursors[nCursor];
480 if (p->nOffset) {
481 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
482 p = &editor->pCursors[nCursor];
484 tmp_style = ME_GetInsertStyle(editor, nCursor);
485 /* ME_SplitParagraph increases style refcount */
486 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
487 p->pRun = ME_FindItemFwd(tp, diRun);
488 end_run = ME_FindItemBack(tp, diRun);
489 ME_ReleaseStyle(end_run->member.run.style);
490 end_run->member.run.style = tmp_style;
491 p->nOffset = 0;
492 if(pos-str < len && *pos =='\r')
493 pos++;
494 if(pos-str < len && *pos =='\n')
495 pos++;
496 if(pos-str <= len) {
497 len -= pos - str;
498 str = pos;
499 continue;
502 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
503 len = 0;
508 static BOOL
509 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
511 ME_DisplayItem *pRun = pCursor->pRun;
513 if (nRelOfs == -1)
515 if (!pCursor->nOffset)
517 do {
518 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
519 assert(pRun);
520 switch (pRun->type)
522 case diRun:
523 break;
524 case diParagraph:
525 if (pRun->member.para.prev_para->type == diTextStart)
526 return FALSE;
527 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
528 /* every paragraph ought to have at least one run */
529 assert(pRun && pRun->type == diRun);
530 assert(pRun->member.run.nFlags & MERF_ENDPARA);
531 break;
532 default:
533 assert(pRun->type != diRun && pRun->type != diParagraph);
534 return FALSE;
536 } while (RUN_IS_HIDDEN(&pRun->member.run));
537 pCursor->pRun = pRun;
538 if (pRun->member.run.nFlags & MERF_ENDPARA)
539 pCursor->nOffset = 0;
540 else
541 pCursor->nOffset = pRun->member.run.strText->nLen;
544 if (pCursor->nOffset)
545 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
546 return TRUE;
548 else
550 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
552 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
554 if (new_ofs < pRun->member.run.strText->nLen)
556 pCursor->nOffset = new_ofs;
557 return TRUE;
560 do {
561 pRun = ME_FindItemFwd(pRun, diRun);
562 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
563 if (pRun)
565 pCursor->pRun = pRun;
566 pCursor->nOffset = 0;
567 return TRUE;
570 return FALSE;
574 static BOOL
575 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
577 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
578 int nOffset = cursor->nOffset;
580 if (nRelOfs == -1)
582 /* Backward movement */
583 while (TRUE)
585 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
586 nOffset, WB_MOVEWORDLEFT);
587 if (nOffset)
588 break;
589 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
590 if (pOtherRun->type == diRun)
592 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
593 pOtherRun->member.run.strText->nLen - 1,
594 WB_ISDELIMITER)
595 && !(pRun->member.run.nFlags & MERF_ENDPARA)
596 && !(cursor->pRun == pRun && cursor->nOffset == 0)
597 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
598 WB_ISDELIMITER))
599 break;
600 pRun = pOtherRun;
601 nOffset = pOtherRun->member.run.strText->nLen;
603 else if (pOtherRun->type == diParagraph)
605 if (cursor->pRun == pRun && cursor->nOffset == 0)
607 /* Paragraph breaks are treated as separate words */
608 if (pOtherRun->member.para.prev_para->type == diTextStart)
609 return FALSE;
610 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
612 break;
616 else
618 /* Forward movement */
619 BOOL last_delim = FALSE;
621 while (TRUE)
623 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
624 nOffset, WB_ISDELIMITER))
625 break;
626 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
627 nOffset, WB_MOVEWORDRIGHT);
628 if (nOffset < pRun->member.run.strText->nLen)
629 break;
630 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
631 if (pOtherRun->type == diRun)
633 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
634 nOffset - 1, WB_ISDELIMITER);
635 pRun = pOtherRun;
636 nOffset = 0;
638 else if (pOtherRun->type == diParagraph)
640 if (cursor->pRun == pRun)
641 pRun = ME_FindItemFwd(pOtherRun, diRun);
642 nOffset = 0;
643 break;
645 else /* diTextEnd */
647 if (cursor->pRun == pRun)
648 return FALSE;
649 nOffset = 0;
650 break;
654 cursor->pRun = pRun;
655 cursor->nOffset = nOffset;
656 return TRUE;
660 void
661 ME_SelectWord(ME_TextEditor *editor)
663 if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
664 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
665 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
666 ME_InvalidateSelection(editor);
667 ME_SendSelChange(editor);
671 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
673 ME_Cursor *pCursor = &editor->pCursors[nCursor];
675 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
676 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
679 int ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
681 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
682 int rx = 0;
684 if (is_eol)
685 *is_eol = 0;
687 while(p != editor->pBuffer->pLast)
689 if (p->type == diParagraph)
691 int ry = y - p->member.para.nYPos;
692 if (ry < 0)
694 result->pRun = ME_FindItemFwd(p, diRun);
695 result->nOffset = 0;
696 return 0;
698 if (ry >= p->member.para.nHeight)
700 p = p->member.para.next_para;
701 continue;
703 p = ME_FindItemFwd(p, diStartRow);
704 y = ry;
705 continue;
707 if (p->type == diStartRow)
709 int ry = y - p->member.row.nYPos;
710 if (ry < 0)
711 return 0;
712 if (ry >= p->member.row.nHeight)
714 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
715 if (p->type != diStartRow)
716 return 0;
717 continue;
719 p = ME_FindItemFwd(p, diRun);
720 continue;
722 if (p->type == diRun)
724 ME_DisplayItem *pp;
725 rx = x - p->member.run.pt.x;
726 if (rx < 0)
727 rx = 0;
728 if (rx >= p->member.run.nWidth) /* not this run yet... find next item */
730 pp = p;
731 do {
732 p = p->next;
733 if (p->type == diRun)
735 rx = x - p->member.run.pt.x;
736 goto continue_search;
738 if (p->type == diStartRow)
740 p = ME_FindItemFwd(p, diRun);
741 if (is_eol)
742 *is_eol = 1;
743 rx = 0; /* FIXME not sure */
744 goto found_here;
746 if (p->type == diParagraph || p->type == diTextEnd)
748 rx = 0; /* FIXME not sure */
749 p = pp;
750 goto found_here;
752 } while(1);
753 continue;
755 found_here:
756 if (p->member.run.nFlags & MERF_ENDPARA)
757 rx = 0;
758 result->pRun = p;
759 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
760 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
762 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
763 result->nOffset = 0;
765 return 1;
767 assert(0);
768 continue_search:
771 result->pRun = ME_FindItemBack(p, diRun);
772 result->nOffset = 0;
773 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
774 return 0;
779 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
781 ME_Cursor cursor;
782 RECT rc;
784 GetClientRect(editor->hWnd, &rc);
785 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
786 return -1;
787 y += ME_GetYScrollPos(editor);
788 ME_FindPixelPos(editor, x, y, &cursor, NULL);
789 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
790 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
794 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
796 ME_Cursor tmp_cursor;
797 int is_selection = 0;
799 editor->nUDArrowX = -1;
801 y += ME_GetYScrollPos(editor);
803 tmp_cursor = editor->pCursors[0];
804 is_selection = ME_IsSelection(editor);
806 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
808 if (GetKeyState(VK_SHIFT)>=0)
810 editor->pCursors[1] = editor->pCursors[0];
812 else
814 if (!is_selection) {
815 editor->pCursors[1] = tmp_cursor;
816 is_selection = 1;
819 ME_InvalidateSelection(editor);
820 HideCaret(editor->hWnd);
821 ME_MoveCaret(editor);
822 ShowCaret(editor->hWnd);
823 ME_ClearTempStyle(editor);
824 ME_SendSelChange(editor);
827 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
829 ME_Cursor tmp_cursor;
831 y += ME_GetYScrollPos(editor);
833 tmp_cursor = editor->pCursors[0];
834 /* FIXME: do something with the return value of ME_FindPixelPos */
835 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
837 if (tmp_cursor.pRun == editor->pCursors[0].pRun &&
838 tmp_cursor.nOffset == editor->pCursors[0].nOffset)
839 return;
841 ME_InvalidateSelection(editor);
842 editor->pCursors[0] = tmp_cursor;
843 HideCaret(editor->hWnd);
844 ME_MoveCaret(editor);
845 ME_InvalidateSelection(editor);
846 ShowCaret(editor->hWnd);
847 ME_SendSelChange(editor);
850 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
851 int x, int *pOffset, int *pbCaretAtEnd)
853 ME_DisplayItem *pNext, *pLastRun;
854 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
855 assert(pNext->type == diRun);
856 pLastRun = pNext;
857 *pbCaretAtEnd = FALSE;
858 do {
859 int run_x = pNext->member.run.pt.x;
860 int width = pNext->member.run.nWidth;
861 if (x < run_x)
863 if (pOffset) *pOffset = 0;
864 return pNext;
866 if (x >= run_x && x < run_x+width)
868 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
869 ME_String *s = pNext->member.run.strText;
870 if (ch < s->nLen) {
871 if (pOffset)
872 *pOffset = ch;
873 return pNext;
876 pLastRun = pNext;
877 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
878 } while(pNext && pNext->type == diRun);
880 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
882 pNext = ME_FindItemFwd(pNext, diRun);
883 if (pbCaretAtEnd) *pbCaretAtEnd = 1;
884 if (pOffset) *pOffset = 0;
885 return pNext;
886 } else {
887 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
888 if (pOffset) *pOffset = 0;
889 return pLastRun;
893 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
895 ME_DisplayItem *pRun = pCursor->pRun;
896 int x;
898 if (editor->nUDArrowX != -1)
899 x = editor->nUDArrowX;
900 else {
901 if (editor->bCaretAtEnd)
903 pRun = ME_FindItemBack(pRun, diRun);
904 assert(pRun);
905 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
907 else {
908 x = pRun->member.run.pt.x;
909 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
911 editor->nUDArrowX = x;
913 return x;
917 static void
918 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
920 ME_DisplayItem *pRun = pCursor->pRun;
921 ME_DisplayItem *pItem;
922 int x = ME_GetXForArrow(editor, pCursor);
924 if (editor->bCaretAtEnd && !pCursor->nOffset)
925 pRun = ME_FindItemBack(pRun, diRun);
926 if (!pRun)
927 return;
928 if (nRelOfs == -1)
930 /* start of this row */
931 pItem = ME_FindItemBack(pRun, diStartRow);
932 assert(pItem);
933 /* start of the previous row */
934 pItem = ME_FindItemBack(pItem, diStartRow);
936 else
938 /* start of the next row */
939 pItem = ME_FindItemFwd(pRun, diStartRow);
940 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
943 if (!pItem)
945 /* row not found - ignore */
946 return;
948 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
949 assert(pCursor->pRun);
950 assert(pCursor->pRun->type == diRun);
954 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
956 ME_DisplayItem *pRun = pCursor->pRun;
957 ME_DisplayItem *pLast, *p;
958 int x, y, ys, yd, yp, yprev;
959 ME_Cursor tmp_curs = *pCursor;
961 x = ME_GetXForArrow(editor, pCursor);
962 if (!pCursor->nOffset && editor->bCaretAtEnd)
963 pRun = ME_FindItemBack(pRun, diRun);
965 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
966 assert(p->type == diStartRow);
967 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
968 yprev = ys = y = yp + p->member.row.nYPos;
969 yd = y - editor->sizeWindow.cy;
970 pLast = p;
972 do {
973 p = ME_FindItemBack(p, diStartRowOrParagraph);
974 if (!p)
975 break;
976 if (p->type == diParagraph) { /* crossing paragraphs */
977 if (p->member.para.prev_para == NULL)
978 break;
979 yp = p->member.para.prev_para->member.para.nYPos;
980 continue;
982 y = yp + p->member.row.nYPos;
983 if (y < yd)
984 break;
985 pLast = p;
986 yprev = y;
987 } while(1);
989 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
990 ME_UpdateSelection(editor, &tmp_curs);
991 if (yprev < editor->sizeWindow.cy)
993 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
994 ME_Repaint(editor);
996 else
998 ME_ScrollUp(editor, ys-yprev);
1000 assert(pCursor->pRun);
1001 assert(pCursor->pRun->type == diRun);
1004 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1005 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1006 In such a situation, clicking the scrollbar restores its position back to the
1007 normal range (ie. sets it to (doclength-screenheight)). */
1009 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1011 ME_DisplayItem *pRun = pCursor->pRun;
1012 ME_DisplayItem *pLast, *p;
1013 int x, y, ys, yd, yp, yprev;
1014 ME_Cursor tmp_curs = *pCursor;
1016 x = ME_GetXForArrow(editor, pCursor);
1017 if (!pCursor->nOffset && editor->bCaretAtEnd)
1018 pRun = ME_FindItemBack(pRun, diRun);
1020 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1021 assert(p->type == diStartRow);
1022 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1023 yprev = ys = y = yp + p->member.row.nYPos;
1024 yd = y + editor->sizeWindow.cy;
1025 pLast = p;
1027 do {
1028 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1029 if (!p)
1030 break;
1031 if (p->type == diParagraph) {
1032 yp = p->member.para.nYPos;
1033 continue;
1035 y = yp + p->member.row.nYPos;
1036 if (y >= yd)
1037 break;
1038 pLast = p;
1039 yprev = y;
1040 } while(1);
1042 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1043 ME_UpdateSelection(editor, &tmp_curs);
1044 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
1046 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1047 ME_Repaint(editor);
1049 else
1051 ME_ScrollUp(editor,ys-yprev);
1053 assert(pCursor->pRun);
1054 assert(pCursor->pRun->type == diRun);
1057 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1059 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1060 /* bCaretAtEnd doesn't make sense if the cursor isn't set at the
1061 first character of the next row */
1062 assert(!editor->bCaretAtEnd || !pCursor->nOffset);
1063 ME_WrapMarkedParagraphs(editor);
1064 if (pRow) {
1065 ME_DisplayItem *pRun;
1066 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1067 pRow = ME_FindItemBack(pRow, diStartRow);
1068 if (!pRow)
1069 return;
1071 pRun = ME_FindItemFwd(pRow, diRun);
1072 if (pRun) {
1073 pCursor->pRun = pRun;
1074 pCursor->nOffset = 0;
1077 editor->bCaretAtEnd = FALSE;
1080 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1082 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1083 if (pRow) {
1084 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1085 if (pRun) {
1086 pCursor->pRun = pRun;
1087 pCursor->nOffset = 0;
1092 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1094 ME_DisplayItem *pRow;
1096 if (editor->bCaretAtEnd && !pCursor->nOffset)
1097 return;
1099 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1100 assert(pRow);
1101 if (pRow->type == diStartRow) {
1102 /* FIXME WTF was I thinking about here ? */
1103 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1104 assert(pRun);
1105 pCursor->pRun = pRun;
1106 pCursor->nOffset = 0;
1107 editor->bCaretAtEnd = 1;
1108 return;
1110 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1111 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1112 pCursor->nOffset = 0;
1113 editor->bCaretAtEnd = FALSE;
1116 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1118 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1119 assert(p);
1120 p = ME_FindItemBack(p, diRun);
1121 assert(p);
1122 assert(p->member.run.nFlags & MERF_ENDPARA);
1123 pCursor->pRun = p;
1124 pCursor->nOffset = 0;
1125 editor->bCaretAtEnd = FALSE;
1128 BOOL ME_IsSelection(ME_TextEditor *editor)
1130 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1133 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1135 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1137 if (cdir*dir>0)
1138 return 0;
1139 else
1140 return 1;
1143 BOOL ME_UpdateSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
1145 ME_Cursor old_anchor = editor->pCursors[1];
1147 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1149 /* any selection was present ? if so, it's no more, repaint ! */
1150 editor->pCursors[1] = editor->pCursors[0];
1151 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1152 return TRUE;
1154 return FALSE;
1156 else
1158 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1160 editor->pCursors[1] = *pTempCursor;
1161 return TRUE;
1165 ME_Repaint(editor);
1166 return TRUE;
1169 void ME_DeleteSelection(ME_TextEditor *editor)
1171 int from, to;
1172 ME_GetSelection(editor, &from, &to);
1173 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1176 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1178 ME_Style *style;
1179 int from, to;
1180 ME_Cursor c;
1182 ME_GetSelection(editor, &from, &to);
1183 ME_CursorFromCharOfs(editor, from, &c);
1184 if (from != to) {
1185 style = c.pRun->member.run.style;
1186 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1188 else
1189 style = ME_GetInsertStyle(editor, 0);
1190 return style;
1193 void ME_SendSelChange(ME_TextEditor *editor)
1195 SELCHANGE sc;
1197 ME_ClearTempStyle(editor);
1199 if (!(editor->nEventMask & ENM_SELCHANGE))
1200 return;
1202 sc.nmhdr.hwndFrom = editor->hWnd;
1203 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1204 sc.nmhdr.code = EN_SELCHANGE;
1205 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1206 sc.seltyp = SEL_EMPTY;
1207 if (sc.chrg.cpMin != sc.chrg.cpMax)
1208 sc.seltyp |= SEL_TEXT;
1209 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1210 sc.seltyp |= SEL_MULTICHAR;
1211 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1215 BOOL
1216 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1218 int nCursor = 0;
1219 ME_Cursor *p = &editor->pCursors[nCursor];
1220 ME_Cursor tmp_curs = *p;
1221 BOOL success = FALSE;
1223 ME_CheckCharOffsets(editor);
1224 editor->nUDArrowX = -1;
1225 switch(nVKey) {
1226 case VK_LEFT:
1227 editor->bCaretAtEnd = 0;
1228 if (ctrl)
1229 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1230 else
1231 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1232 break;
1233 case VK_RIGHT:
1234 editor->bCaretAtEnd = 0;
1235 if (ctrl)
1236 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1237 else
1238 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1239 break;
1240 case VK_UP:
1241 ME_MoveCursorLines(editor, &tmp_curs, -1);
1242 break;
1243 case VK_DOWN:
1244 ME_MoveCursorLines(editor, &tmp_curs, +1);
1245 break;
1246 case VK_PRIOR:
1247 ME_ArrowPageUp(editor, &tmp_curs);
1248 break;
1249 case VK_NEXT:
1250 ME_ArrowPageDown(editor, &tmp_curs);
1251 break;
1252 case VK_HOME: {
1253 if (ctrl)
1254 ME_ArrowCtrlHome(editor, &tmp_curs);
1255 else
1256 ME_ArrowHome(editor, &tmp_curs);
1257 editor->bCaretAtEnd = 0;
1258 break;
1260 case VK_END:
1261 if (ctrl)
1262 ME_ArrowCtrlEnd(editor, &tmp_curs);
1263 else
1264 ME_ArrowEnd(editor, &tmp_curs);
1265 break;
1268 if (!extend)
1269 editor->pCursors[1] = tmp_curs;
1270 *p = tmp_curs;
1272 ME_InvalidateSelection(editor);
1273 ME_Repaint(editor);
1274 HideCaret(editor->hWnd);
1275 ME_EnsureVisible(editor, tmp_curs.pRun);
1276 ME_ShowCaret(editor);
1277 ME_SendSelChange(editor);
1278 return success;