systray: Add support for NIS_HIDDEN flag.
[wine.git] / dlls / riched20 / caret.c
blob3f0604eb7e7730f2881471e0f8d1b2a5c3550e78
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 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
398 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
399 ME_DisplayItem *di;
400 WCHAR space = ' ';
402 /* FIXME no no no */
403 if (ME_IsSelection(editor))
404 ME_DeleteSelection(editor);
406 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
407 MERF_GRAPHICS);
408 di->member.run.ole_obj = ALLOC_OBJ(*reo);
409 ME_CopyReObject(di->member.run.ole_obj, reo);
410 ME_SendSelChange(editor);
414 void
415 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
417 WCHAR tab = '\t';
418 ME_DisplayItem *p, *run;
419 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
421 p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
422 MERF_CELL);
423 run = p;
424 while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
426 if (run->member.run.nFlags & MERF_CELL)
428 assert(run->member.run.pCell->next);
429 p->member.run.pCell = run->member.run.pCell->next;
430 return;
433 assert(run->type == diParagraph);
434 assert(run->member.para.bTable);
435 assert(run->member.para.pCells);
436 p->member.run.pCell = run->member.para.pCells;
440 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
441 const WCHAR *str, int len, ME_Style *style)
443 const WCHAR *pos;
444 ME_Cursor *p = NULL;
445 int oldLen;
447 /* FIXME really HERE ? */
448 if (ME_IsSelection(editor))
449 ME_DeleteSelection(editor);
451 /* FIXME: is this too slow? */
452 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
453 oldLen = ME_GetTextLength(editor);
455 /* text operations set modified state */
456 editor->nModifyStep = 1;
458 assert(style);
460 assert(nCursor>=0 && nCursor<editor->nCursors);
461 if (len == -1)
462 len = lstrlenW(str);
464 /* grow the text limit to fit our text */
465 if(editor->nTextLimit < oldLen +len)
466 editor->nTextLimit = oldLen + len;
468 while (len)
470 pos = str;
471 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
472 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
473 pos++;
474 if (pos-str < len && *pos == '\t') { /* handle tabs */
475 WCHAR tab = '\t';
477 if (pos!=str)
478 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
480 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
482 pos++;
483 if(pos-str <= len) {
484 len -= pos - str;
485 str = pos;
486 continue;
489 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
490 if (!editor->bEmulateVersion10 && pos-str < len-2 && pos[0] == '\r' && pos[1] == '\r' && pos[2] == '\n') {
491 WCHAR space = ' ';
493 if (pos!=str)
494 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
496 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
498 pos+=3;
499 if(pos-str <= len) {
500 len -= pos - str;
501 str = pos;
502 continue;
505 if (pos-str < len) { /* handle EOLs */
506 ME_DisplayItem *tp, *end_run;
507 ME_Style *tmp_style;
508 if (pos!=str)
509 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
510 p = &editor->pCursors[nCursor];
511 if (p->nOffset) {
512 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
513 p = &editor->pCursors[nCursor];
515 tmp_style = ME_GetInsertStyle(editor, nCursor);
516 /* ME_SplitParagraph increases style refcount */
517 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
518 p->pRun = ME_FindItemFwd(tp, diRun);
519 end_run = ME_FindItemBack(tp, diRun);
520 ME_ReleaseStyle(end_run->member.run.style);
521 end_run->member.run.style = tmp_style;
522 p->nOffset = 0;
523 if(pos-str < len && *pos =='\r')
524 pos++;
525 if(pos-str < len && *pos =='\n')
526 pos++;
527 if(pos-str <= len) {
528 len -= pos - str;
529 str = pos;
530 continue;
533 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
534 len = 0;
539 static BOOL
540 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
542 ME_DisplayItem *pRun = pCursor->pRun;
544 if (nRelOfs == -1)
546 if (!pCursor->nOffset)
548 do {
549 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
550 assert(pRun);
551 switch (pRun->type)
553 case diRun:
554 break;
555 case diParagraph:
556 if (pRun->member.para.prev_para->type == diTextStart)
557 return FALSE;
558 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
559 /* every paragraph ought to have at least one run */
560 assert(pRun && pRun->type == diRun);
561 assert(pRun->member.run.nFlags & MERF_ENDPARA);
562 break;
563 default:
564 assert(pRun->type != diRun && pRun->type != diParagraph);
565 return FALSE;
567 } while (RUN_IS_HIDDEN(&pRun->member.run));
568 pCursor->pRun = pRun;
569 if (pRun->member.run.nFlags & MERF_ENDPARA)
570 pCursor->nOffset = 0;
571 else
572 pCursor->nOffset = pRun->member.run.strText->nLen;
575 if (pCursor->nOffset)
576 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
577 return TRUE;
579 else
581 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
583 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
585 if (new_ofs < pRun->member.run.strText->nLen)
587 pCursor->nOffset = new_ofs;
588 return TRUE;
591 do {
592 pRun = ME_FindItemFwd(pRun, diRun);
593 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
594 if (pRun)
596 pCursor->pRun = pRun;
597 pCursor->nOffset = 0;
598 return TRUE;
601 return FALSE;
605 static BOOL
606 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
608 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
609 int nOffset = cursor->nOffset;
611 if (nRelOfs == -1)
613 /* Backward movement */
614 while (TRUE)
616 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
617 nOffset, WB_MOVEWORDLEFT);
618 if (nOffset)
619 break;
620 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
621 if (pOtherRun->type == diRun)
623 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
624 pOtherRun->member.run.strText->nLen - 1,
625 WB_ISDELIMITER)
626 && !(pRun->member.run.nFlags & MERF_ENDPARA)
627 && !(cursor->pRun == pRun && cursor->nOffset == 0)
628 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
629 WB_ISDELIMITER))
630 break;
631 pRun = pOtherRun;
632 nOffset = pOtherRun->member.run.strText->nLen;
634 else if (pOtherRun->type == diParagraph)
636 if (cursor->pRun == pRun && cursor->nOffset == 0)
638 /* Paragraph breaks are treated as separate words */
639 if (pOtherRun->member.para.prev_para->type == diTextStart)
640 return FALSE;
641 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
643 break;
647 else
649 /* Forward movement */
650 BOOL last_delim = FALSE;
652 while (TRUE)
654 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
655 nOffset, WB_ISDELIMITER))
656 break;
657 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
658 nOffset, WB_MOVEWORDRIGHT);
659 if (nOffset < pRun->member.run.strText->nLen)
660 break;
661 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
662 if (pOtherRun->type == diRun)
664 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
665 nOffset - 1, WB_ISDELIMITER);
666 pRun = pOtherRun;
667 nOffset = 0;
669 else if (pOtherRun->type == diParagraph)
671 if (cursor->pRun == pRun)
672 pRun = ME_FindItemFwd(pOtherRun, diRun);
673 nOffset = 0;
674 break;
676 else /* diTextEnd */
678 if (cursor->pRun == pRun)
679 return FALSE;
680 nOffset = 0;
681 break;
685 cursor->pRun = pRun;
686 cursor->nOffset = nOffset;
687 return TRUE;
691 void
692 ME_SelectWord(ME_TextEditor *editor)
694 if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
695 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
696 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
697 ME_InvalidateSelection(editor);
698 ME_SendSelChange(editor);
702 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
704 ME_Cursor *pCursor = &editor->pCursors[nCursor];
705 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
706 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
709 static void ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
711 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
712 ME_DisplayItem *last = NULL;
713 int rx = 0;
715 if (is_eol)
716 *is_eol = 0;
718 /* find paragraph */
719 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
721 assert(p->type == diParagraph);
722 if (y < p->member.para.nYPos + p->member.para.nHeight)
724 y -= p->member.para.nYPos;
725 p = ME_FindItemFwd(p, diStartRow);
726 break;
729 /* find row */
730 for (; p != editor->pBuffer->pLast; )
732 ME_DisplayItem *pp;
733 assert(p->type == diStartRow);
734 if (y < p->member.row.nYPos + p->member.row.nHeight)
736 p = ME_FindItemFwd(p, diRun);
737 break;
739 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
740 if (pp->type != diStartRow)
742 p = ME_FindItemFwd(p, diRun);
743 break;
745 p = pp;
747 for (; p != editor->pBuffer->pLast; p = p->next)
749 switch (p->type)
751 case diRun:
752 rx = x - p->member.run.pt.x;
753 if (rx < p->member.run.nWidth)
755 found_here:
756 assert(p->type == diRun);
757 if ((p->member.run.nFlags & MERF_ENDPARA) || rx < 0)
758 rx = 0;
759 result->pRun = p;
760 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
761 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
763 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
764 result->nOffset = 0;
766 return;
768 break;
769 case diStartRow:
770 p = ME_FindItemFwd(p, diRun);
771 if (is_eol) *is_eol = 1;
772 rx = 0; /* FIXME not sure */
773 goto found_here;
774 case diParagraph:
775 case diTextEnd:
776 rx = 0; /* FIXME not sure */
777 p = last;
778 goto found_here;
779 default: assert(0);
781 last = p;
783 result->pRun = ME_FindItemBack(p, diRun);
784 result->nOffset = 0;
785 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
790 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
792 ME_Cursor cursor;
793 RECT rc;
795 GetClientRect(editor->hWnd, &rc);
796 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
797 return -1;
798 y += ME_GetYScrollPos(editor);
799 ME_FindPixelPos(editor, x, y, &cursor, NULL);
800 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
801 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
805 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
807 ME_Cursor tmp_cursor;
808 int is_selection = 0;
810 editor->nUDArrowX = -1;
812 y += ME_GetYScrollPos(editor);
814 tmp_cursor = editor->pCursors[0];
815 is_selection = ME_IsSelection(editor);
817 if (x >= editor->selofs)
819 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
820 if (GetKeyState(VK_SHIFT)>=0)
822 editor->pCursors[1] = editor->pCursors[0];
824 else if (!is_selection) {
825 editor->pCursors[1] = tmp_cursor;
826 is_selection = 1;
829 ME_InvalidateSelection(editor);
830 HideCaret(editor->hWnd);
831 ME_MoveCaret(editor);
832 ShowCaret(editor->hWnd);
833 ME_ClearTempStyle(editor);
834 ME_SendSelChange(editor);
836 else
838 ME_DisplayItem *pRow;
840 editor->linesel = 1;
841 editor->sely = y;
842 /* Set pCursors[0] to beginning of line */
843 ME_FindPixelPos(editor, x, y, &editor->pCursors[1], &editor->bCaretAtEnd);
844 /* Set pCursors[1] to end of line */
845 pRow = ME_FindItemFwd(editor->pCursors[1].pRun, diStartRowOrParagraphOrEnd);
846 assert(pRow);
847 /* pCursor[0] is the position where the cursor will be drawn,
848 * pCursor[1] is the other end of the selection range
849 * pCursor[2] and [3] are backups of [0] and [1] so I
850 * don't have to look them up again
853 if (pRow->type == diStartRow) {
854 /* FIXME WTF was I thinking about here ? */
855 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
856 assert(pRun);
857 editor->pCursors[0].pRun = pRun;
858 editor->pCursors[0].nOffset = 0;
859 editor->bCaretAtEnd = 1;
860 } else {
861 editor->pCursors[0].pRun = ME_FindItemBack(pRow, diRun);
862 assert(editor->pCursors[0].pRun && editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA);
863 editor->pCursors[0].nOffset = 0;
864 editor->bCaretAtEnd = 0;
866 editor->pCursors[2] = editor->pCursors[0];
867 editor->pCursors[3] = editor->pCursors[1];
868 ME_InvalidateSelection(editor);
869 HideCaret(editor->hWnd);
870 ME_MoveCaret(editor);
871 ShowCaret(editor->hWnd);
872 ME_ClearTempStyle(editor);
873 ME_SendSelChange(editor);
877 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
879 ME_Cursor tmp_cursor;
881 y += ME_GetYScrollPos(editor);
883 tmp_cursor = editor->pCursors[0];
884 /* FIXME: do something with the return value of ME_FindPixelPos */
885 if (!editor->linesel)
886 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
887 else ME_FindPixelPos(editor, (y > editor->sely) * editor->rcFormat.right, y, &tmp_cursor, &editor->bCaretAtEnd);
889 if (!memcmp(&tmp_cursor, editor->pCursors, sizeof(tmp_cursor)))
890 return;
892 ME_InvalidateSelection(editor);
893 if (!editor->linesel)
894 editor->pCursors[0] = tmp_cursor;
895 else if (!memcmp(&tmp_cursor, editor->pCursors+2, sizeof(tmp_cursor)) ||
896 !memcmp(&tmp_cursor, editor->pCursors+3, sizeof(tmp_cursor)))
898 editor->pCursors[0] = editor->pCursors[2];
899 editor->pCursors[1] = editor->pCursors[3];
901 else if (y < editor->sely)
903 editor->pCursors[0] = tmp_cursor;
904 editor->pCursors[1] = editor->pCursors[2];
906 else
908 editor->pCursors[0] = tmp_cursor;
909 editor->pCursors[1] = editor->pCursors[3];
912 HideCaret(editor->hWnd);
913 ME_MoveCaret(editor);
914 ME_InvalidateSelection(editor);
915 ShowCaret(editor->hWnd);
916 ME_SendSelChange(editor);
919 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
920 int x, int *pOffset, int *pbCaretAtEnd)
922 ME_DisplayItem *pNext, *pLastRun;
923 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
924 assert(pNext->type == diRun);
925 pLastRun = pNext;
926 *pbCaretAtEnd = FALSE;
927 do {
928 int run_x = pNext->member.run.pt.x;
929 int width = pNext->member.run.nWidth;
930 if (x < run_x)
932 if (pOffset) *pOffset = 0;
933 return pNext;
935 if (x >= run_x && x < run_x+width)
937 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
938 ME_String *s = pNext->member.run.strText;
939 if (ch < s->nLen) {
940 if (pOffset)
941 *pOffset = ch;
942 return pNext;
945 pLastRun = pNext;
946 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
947 } while(pNext && pNext->type == diRun);
949 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
951 pNext = ME_FindItemFwd(pNext, diRun);
952 if (pbCaretAtEnd) *pbCaretAtEnd = 1;
953 if (pOffset) *pOffset = 0;
954 return pNext;
955 } else {
956 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
957 if (pOffset) *pOffset = 0;
958 return pLastRun;
962 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
964 ME_DisplayItem *pRun = pCursor->pRun;
965 int x;
967 if (editor->nUDArrowX != -1)
968 x = editor->nUDArrowX;
969 else {
970 if (editor->bCaretAtEnd)
972 pRun = ME_FindItemBack(pRun, diRun);
973 assert(pRun);
974 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
976 else {
977 x = pRun->member.run.pt.x;
978 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
980 editor->nUDArrowX = x;
982 return x;
986 static void
987 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
989 ME_DisplayItem *pRun = pCursor->pRun;
990 ME_DisplayItem *pItem;
991 int x = ME_GetXForArrow(editor, pCursor);
993 if (editor->bCaretAtEnd && !pCursor->nOffset)
994 pRun = ME_FindItemBack(pRun, diRun);
995 if (!pRun)
996 return;
997 if (nRelOfs == -1)
999 /* start of this row */
1000 pItem = ME_FindItemBack(pRun, diStartRow);
1001 assert(pItem);
1002 /* start of the previous row */
1003 pItem = ME_FindItemBack(pItem, diStartRow);
1005 else
1007 /* start of the next row */
1008 pItem = ME_FindItemFwd(pRun, diStartRow);
1009 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1012 if (!pItem)
1014 /* row not found - ignore */
1015 return;
1017 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1018 assert(pCursor->pRun);
1019 assert(pCursor->pRun->type == diRun);
1023 static void ME_ArrowPageUp(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_FindItemBack(p, diStartRowOrParagraph);
1043 if (!p)
1044 break;
1045 if (p->type == diParagraph) { /* crossing paragraphs */
1046 if (p->member.para.prev_para == NULL)
1047 break;
1048 yp = p->member.para.prev_para->member.para.nYPos;
1049 continue;
1051 y = yp + p->member.row.nYPos;
1052 if (y < yd)
1053 break;
1054 pLast = p;
1055 yprev = y;
1056 } while(1);
1058 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1059 ME_UpdateSelection(editor, &tmp_curs);
1060 if (yprev < editor->sizeWindow.cy)
1062 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
1063 ME_Repaint(editor);
1065 else
1067 ME_ScrollUp(editor, ys-yprev);
1069 assert(pCursor->pRun);
1070 assert(pCursor->pRun->type == diRun);
1073 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1074 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1075 In such a situation, clicking the scrollbar restores its position back to the
1076 normal range (ie. sets it to (doclength-screenheight)). */
1078 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1080 ME_DisplayItem *pRun = pCursor->pRun;
1081 ME_DisplayItem *pLast, *p;
1082 int x, y, ys, yd, yp, yprev;
1083 ME_Cursor tmp_curs = *pCursor;
1085 x = ME_GetXForArrow(editor, pCursor);
1086 if (!pCursor->nOffset && editor->bCaretAtEnd)
1087 pRun = ME_FindItemBack(pRun, diRun);
1089 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1090 assert(p->type == diStartRow);
1091 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1092 yprev = ys = y = yp + p->member.row.nYPos;
1093 yd = y + editor->sizeWindow.cy;
1094 pLast = p;
1096 do {
1097 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1098 if (!p)
1099 break;
1100 if (p->type == diParagraph) {
1101 yp = p->member.para.nYPos;
1102 continue;
1104 y = yp + p->member.row.nYPos;
1105 if (y >= yd)
1106 break;
1107 pLast = p;
1108 yprev = y;
1109 } while(1);
1111 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1112 ME_UpdateSelection(editor, &tmp_curs);
1113 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
1115 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1116 ME_Repaint(editor);
1118 else
1120 ME_ScrollUp(editor,ys-yprev);
1122 assert(pCursor->pRun);
1123 assert(pCursor->pRun->type == diRun);
1126 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1128 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1129 /* bCaretAtEnd doesn't make sense if the cursor isn't set at the
1130 first character of the next row */
1131 assert(!editor->bCaretAtEnd || !pCursor->nOffset);
1132 ME_WrapMarkedParagraphs(editor);
1133 if (pRow) {
1134 ME_DisplayItem *pRun;
1135 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1136 pRow = ME_FindItemBack(pRow, diStartRow);
1137 if (!pRow)
1138 return;
1140 pRun = ME_FindItemFwd(pRow, diRun);
1141 if (pRun) {
1142 pCursor->pRun = pRun;
1143 pCursor->nOffset = 0;
1146 editor->bCaretAtEnd = FALSE;
1149 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1151 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1152 if (pRow) {
1153 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1154 if (pRun) {
1155 pCursor->pRun = pRun;
1156 pCursor->nOffset = 0;
1161 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1163 ME_DisplayItem *pRow;
1165 if (editor->bCaretAtEnd && !pCursor->nOffset)
1166 return;
1168 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1169 assert(pRow);
1170 if (pRow->type == diStartRow) {
1171 /* FIXME WTF was I thinking about here ? */
1172 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1173 assert(pRun);
1174 pCursor->pRun = pRun;
1175 pCursor->nOffset = 0;
1176 editor->bCaretAtEnd = 1;
1177 return;
1179 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1180 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1181 pCursor->nOffset = 0;
1182 editor->bCaretAtEnd = FALSE;
1185 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1187 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1188 assert(p);
1189 p = ME_FindItemBack(p, diRun);
1190 assert(p);
1191 assert(p->member.run.nFlags & MERF_ENDPARA);
1192 pCursor->pRun = p;
1193 pCursor->nOffset = 0;
1194 editor->bCaretAtEnd = FALSE;
1197 BOOL ME_IsSelection(ME_TextEditor *editor)
1199 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1202 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1204 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1206 if (cdir*dir>0)
1207 return 0;
1208 else
1209 return 1;
1212 BOOL ME_UpdateSelection(ME_TextEditor *editor, const ME_Cursor *pTempCursor)
1214 ME_Cursor old_anchor = editor->pCursors[1];
1216 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1218 /* any selection was present ? if so, it's no more, repaint ! */
1219 editor->pCursors[1] = editor->pCursors[0];
1220 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1221 return TRUE;
1223 return FALSE;
1225 else
1227 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1229 editor->pCursors[1] = *pTempCursor;
1230 return TRUE;
1234 ME_Repaint(editor);
1235 return TRUE;
1238 void ME_DeleteSelection(ME_TextEditor *editor)
1240 int from, to;
1241 ME_GetSelection(editor, &from, &to);
1242 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1245 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1247 ME_Style *style;
1248 int from, to;
1250 ME_GetSelection(editor, &from, &to);
1251 if (from != to) {
1252 ME_Cursor c;
1253 ME_CursorFromCharOfs(editor, from, &c);
1254 style = c.pRun->member.run.style;
1255 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1257 else
1258 style = ME_GetInsertStyle(editor, 0);
1259 return style;
1262 void ME_SendSelChange(ME_TextEditor *editor)
1264 SELCHANGE sc;
1266 ME_ClearTempStyle(editor);
1268 if (!(editor->nEventMask & ENM_SELCHANGE))
1269 return;
1271 sc.nmhdr.hwndFrom = editor->hWnd;
1272 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1273 sc.nmhdr.code = EN_SELCHANGE;
1274 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1275 sc.seltyp = SEL_EMPTY;
1276 if (sc.chrg.cpMin != sc.chrg.cpMax)
1277 sc.seltyp |= SEL_TEXT;
1278 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1279 sc.seltyp |= SEL_MULTICHAR;
1280 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1283 BOOL
1284 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1286 int nCursor = 0;
1287 ME_Cursor *p = &editor->pCursors[nCursor];
1288 ME_Cursor tmp_curs = *p;
1289 BOOL success = FALSE;
1291 ME_CheckCharOffsets(editor);
1292 editor->nUDArrowX = -1;
1293 switch(nVKey) {
1294 case VK_LEFT:
1295 editor->bCaretAtEnd = 0;
1296 if (ctrl)
1297 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1298 else
1299 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1300 break;
1301 case VK_RIGHT:
1302 editor->bCaretAtEnd = 0;
1303 if (ctrl)
1304 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1305 else
1306 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1307 break;
1308 case VK_UP:
1309 ME_MoveCursorLines(editor, &tmp_curs, -1);
1310 break;
1311 case VK_DOWN:
1312 ME_MoveCursorLines(editor, &tmp_curs, +1);
1313 break;
1314 case VK_PRIOR:
1315 ME_ArrowPageUp(editor, &tmp_curs);
1316 break;
1317 case VK_NEXT:
1318 ME_ArrowPageDown(editor, &tmp_curs);
1319 break;
1320 case VK_HOME: {
1321 if (ctrl)
1322 ME_ArrowCtrlHome(editor, &tmp_curs);
1323 else
1324 ME_ArrowHome(editor, &tmp_curs);
1325 editor->bCaretAtEnd = 0;
1326 break;
1328 case VK_END:
1329 if (ctrl)
1330 ME_ArrowCtrlEnd(editor, &tmp_curs);
1331 else
1332 ME_ArrowEnd(editor, &tmp_curs);
1333 break;
1336 if (!extend)
1337 editor->pCursors[1] = tmp_curs;
1338 *p = tmp_curs;
1340 ME_InvalidateSelection(editor);
1341 ME_Repaint(editor);
1342 HideCaret(editor->hWnd);
1343 ME_EnsureVisible(editor, tmp_curs.pRun);
1344 ME_ShowCaret(editor);
1345 ME_SendSelChange(editor);
1346 return success;