riched20: Replace an if-statement with empty body with a FIXME comment.
[wine.git] / dlls / riched20 / caret.c
blobda52288b506c910ff7a4047c6a9ffda3254cdcfa
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);
28 static BOOL
29 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs);
32 void ME_GetSelection(ME_TextEditor *editor, int *from, int *to)
34 *from = ME_GetCursorOfs(editor, 0);
35 *to = ME_GetCursorOfs(editor, 1);
37 if (*from > *to)
39 int tmp = *from;
40 *from = *to;
41 *to = tmp;
45 int ME_GetTextLength(ME_TextEditor *editor)
47 return ME_CharOfsFromRunOfs(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun), 0);
51 int ME_GetTextLengthEx(ME_TextEditor *editor, GETTEXTLENGTHEX *how)
53 int length;
55 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
56 return E_INVALIDARG;
57 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
58 return E_INVALIDARG;
60 length = ME_GetTextLength(editor);
62 if (how->flags & GTL_USECRLF)
63 length += editor->nParagraphs;
65 if (how->flags & GTL_NUMBYTES)
67 CPINFO cpinfo;
69 if (how->codepage == 1200)
70 return length * 2;
71 if (how->flags & GTL_PRECISE)
72 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
73 if (GetCPInfo(how->codepage, &cpinfo))
74 return length * cpinfo.MaxCharSize;
75 ERR("Invalid codepage %u\n", how->codepage);
76 return E_INVALIDARG;
78 return length;
82 void ME_SetSelection(ME_TextEditor *editor, int from, int to)
84 if (from == 0 && to == -1)
86 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
87 editor->pCursors[1].nOffset = 0;
88 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
89 editor->pCursors[0].nOffset = 0;
90 ME_Repaint(editor);
91 ME_ClearTempStyle(editor);
92 return;
94 if (from == -1)
96 editor->pCursors[1] = editor->pCursors[0];
97 ME_Repaint(editor);
98 ME_ClearTempStyle(editor);
99 return;
101 if (from>to)
103 int tmp = from;
104 from = to;
105 to = tmp;
107 ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
108 ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);
112 void
113 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
114 int *x, int *y, int *height)
116 ME_DisplayItem *pCursorRun = pCursor->pRun;
117 ME_DisplayItem *pSizeRun = pCursor->pRun;
119 assert(!pCursor->nOffset || !editor->bCaretAtEnd);
120 assert(height && x && y);
122 if (pCursorRun->type == diRun) {
123 ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
125 if (row) {
126 HDC hDC = GetDC(editor->hWnd);
127 ME_Context c;
128 ME_DisplayItem *run = pCursorRun;
129 ME_DisplayItem *para;
130 SIZE sz = {0, 0};
132 ME_InitContext(&c, editor, hDC);
134 if (!pCursor->nOffset && !editor->bCaretAtEnd)
136 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrStartRow);
137 if (prev->type == diRun)
138 pSizeRun = prev;
140 assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
141 para = ME_FindItemBack(row, diParagraph);
142 if (editor->bCaretAtEnd && !pCursor->nOffset &&
143 run == ME_FindItemFwd(row, diRun))
145 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
146 if (tmp->type == diRun)
148 row = ME_FindItemBack(tmp, diStartRow);
149 pSizeRun = run = tmp;
150 sz = ME_GetRunSize(&c, &para->member.para, &run->member.run, ME_StrLen(run->member.run.strText));
153 if (pCursor->nOffset && !(run->member.run.nFlags & MERF_SKIPPED)) {
154 sz = ME_GetRunSize(&c, &para->member.para, &run->member.run, pCursor->nOffset);
157 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
158 *x = run->member.run.pt.x + sz.cx;
159 *y = para->member.para.nYPos + row->member.row.nBaseline + pSizeRun->member.run.pt.y - pSizeRun->member.run.nAscent - ME_GetYScrollPos(editor);
161 ME_DestroyContext(&c);
162 ReleaseDC(editor->hWnd, hDC);
163 return;
166 *height = 10; /* FIXME use global font */
167 *x = 0;
168 *y = 0;
172 void
173 ME_MoveCaret(ME_TextEditor *editor)
175 int x, y, height;
177 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
178 CreateCaret(editor->hWnd, NULL, 0, height);
179 SetCaretPos(x, y);
183 void ME_ShowCaret(ME_TextEditor *ed)
185 ME_MoveCaret(ed);
186 ShowCaret(ed->hWnd);
189 void ME_HideCaret(ME_TextEditor *ed)
191 HideCaret(ed->hWnd);
192 DestroyCaret();
195 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
196 int nChars)
198 ME_Cursor c;
199 int shift = 0;
201 while(nChars > 0)
203 ME_Run *run;
204 ME_CursorFromCharOfs(editor, nOfs, &c);
205 run = &c.pRun->member.run;
206 if (run->nFlags & MERF_ENDPARA) {
207 if (!ME_FindItemFwd(c.pRun, diParagraph))
209 return;
211 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
212 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
213 ME_CheckCharOffsets(editor);
214 nChars--;
215 if (editor->bEmulateVersion10 && nChars)
216 nChars--;
217 continue;
219 else
221 ME_Cursor cursor;
222 int nIntendedChars = nChars;
223 int nCharsToDelete = nChars;
224 int i;
225 int loc = c.nOffset;
227 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
229 cursor = c;
230 ME_StrRelPos(run->strText, loc, &nChars);
231 /* nChars is the number of characters that should be deleted from the
232 FOLLOWING runs (these AFTER cursor.pRun)
233 nCharsToDelete is a number of chars to delete from THIS run */
234 nCharsToDelete -= nChars;
235 shift -= nCharsToDelete;
236 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
237 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
238 debugstr_w(run->strText->szData), run->strText->nLen);
240 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
242 /* undo = reinsert whole run */
243 /* nOfs is a character offset (from the start of the document
244 to the current (deleted) run */
245 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
246 if (pUndo)
247 pUndo->di.member.run.nCharOfs = nOfs;
249 else
251 /* undo = reinsert partial run */
252 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
253 if (pUndo) {
254 ME_DestroyString(pUndo->di.member.run.strText);
255 pUndo->di.member.run.nCharOfs = nOfs;
256 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
259 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
260 TRACE("Shift value: %d\n", shift);
261 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
263 /* update cursors (including c) */
264 for (i=-1; i<editor->nCursors; i++) {
265 ME_Cursor *pThisCur = editor->pCursors + i;
266 if (i == -1) pThisCur = &c;
267 if (pThisCur->pRun == cursor.pRun) {
268 if (pThisCur->nOffset > cursor.nOffset) {
269 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
270 pThisCur->nOffset = cursor.nOffset;
271 else
272 pThisCur->nOffset -= nCharsToDelete;
273 assert(pThisCur->nOffset >= 0);
274 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
276 if (pThisCur->nOffset == ME_StrVLen(run->strText))
278 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
279 assert(pThisCur->pRun->type == diRun);
280 pThisCur->nOffset = 0;
285 /* c = updated data now */
287 if (c.pRun == cursor.pRun)
288 ME_SkipAndPropagateCharOffset(c.pRun, shift);
289 else
290 ME_PropagateCharOffset(c.pRun, shift);
292 if (!ME_StrVLen(cursor.pRun->member.run.strText))
294 TRACE("Removing useless run\n");
295 ME_Remove(cursor.pRun);
296 ME_DestroyDisplayItem(cursor.pRun);
299 shift = 0;
301 ME_CheckCharOffsets(editor);
303 continue;
308 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
309 int nChars)
311 assert(nCursor>=0 && nCursor<editor->nCursors);
312 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
315 static ME_DisplayItem *
316 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
317 const WCHAR *str, int len, ME_Style *style,
318 int flags)
320 ME_Cursor *p = &editor->pCursors[nCursor];
322 editor->bCaretAtEnd = FALSE;
324 assert(p->pRun->type == diRun);
326 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
330 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
331 void ME_InsertGraphicsFromCursor(ME_TextEditor *editor, int nCursor)
333 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
334 WCHAR space = ' ';
336 /* FIXME no no no */
337 if (ME_IsSelection(editor))
338 ME_DeleteSelection(editor);
340 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
341 MERF_GRAPHICS);
342 ME_SendSelChange(editor);
346 void
347 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
349 WCHAR tab = '\t';
350 ME_DisplayItem *p, *run;
351 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
353 p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
354 MERF_CELL);
355 run = p;
356 while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
358 if (run->member.run.nFlags & MERF_CELL)
360 assert(run->member.run.pCell->next);
361 p->member.run.pCell = run->member.run.pCell->next;
362 return;
365 assert(run->type == diParagraph);
366 assert(run->member.para.bTable);
367 assert(run->member.para.pCells);
368 p->member.run.pCell = run->member.para.pCells;
372 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
373 const WCHAR *str, int len, ME_Style *style)
375 const WCHAR *pos;
376 ME_Cursor *p = NULL;
378 assert(style);
380 /* FIXME really HERE ? */
381 if (ME_IsSelection(editor))
382 ME_DeleteSelection(editor);
384 assert(nCursor>=0 && nCursor<editor->nCursors);
385 if (len == -1)
386 len = lstrlenW(str);
387 while (len)
389 pos = str;
390 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
391 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
392 pos++;
393 if (pos-str < len && *pos == '\t') { /* handle tabs */
394 WCHAR tab = '\t';
396 if (pos!=str)
397 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
399 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
401 pos++;
402 if(pos-str <= len) {
403 len -= pos - str;
404 str = pos;
405 continue;
408 if (pos-str < len) { /* handle EOLs */
409 ME_DisplayItem *tp, *end_run;
410 ME_Style *tmp_style;
411 if (pos!=str)
412 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
413 p = &editor->pCursors[nCursor];
414 if (p->nOffset) {
415 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
416 p = &editor->pCursors[nCursor];
418 tmp_style = ME_GetInsertStyle(editor, nCursor);
419 /* ME_SplitParagraph increases style refcount */
420 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
421 p->pRun = ME_FindItemFwd(tp, diRun);
422 end_run = ME_FindItemBack(tp, diRun);
423 ME_ReleaseStyle(end_run->member.run.style);
424 end_run->member.run.style = tmp_style;
425 p->nOffset = 0;
426 if(pos-str < len && *pos =='\r')
427 pos++;
428 if(pos-str < len && *pos =='\n')
429 pos++;
430 if(pos-str <= len) {
431 len -= pos - str;
432 str = pos;
433 continue;
436 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
437 len = 0;
442 static BOOL
443 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
445 ME_DisplayItem *pRun = pCursor->pRun;
447 if (nRelOfs == -1)
449 if (!pCursor->nOffset)
451 do {
452 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
453 assert(pRun);
454 switch (pRun->type)
456 case diRun:
457 break;
458 case diParagraph:
459 if (pRun->member.para.prev_para->type == diTextStart)
460 return FALSE;
461 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
462 /* every paragraph ought to have at least one run */
463 assert(pRun && pRun->type == diRun);
464 assert(pRun->member.run.nFlags & MERF_ENDPARA);
465 break;
466 default:
467 assert(pRun->type != diRun && pRun->type != diParagraph);
468 return FALSE;
470 } while (RUN_IS_HIDDEN(&pRun->member.run));
471 pCursor->pRun = pRun;
472 if (pRun->member.run.nFlags & MERF_ENDPARA)
473 pCursor->nOffset = 0;
474 else
475 pCursor->nOffset = pRun->member.run.strText->nLen;
478 if (pCursor->nOffset)
479 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
480 return TRUE;
482 else
484 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
486 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
488 if (new_ofs < pRun->member.run.strText->nLen)
490 pCursor->nOffset = new_ofs;
491 return TRUE;
494 do {
495 pRun = ME_FindItemFwd(pRun, diRun);
496 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
497 if (pRun)
499 pCursor->pRun = pRun;
500 pCursor->nOffset = 0;
501 return TRUE;
504 return FALSE;
508 static BOOL
509 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
511 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
512 int nOffset = cursor->nOffset;
514 if (nRelOfs == -1)
516 /* Backward movement */
517 while (TRUE)
519 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
520 nOffset, WB_MOVEWORDLEFT);
521 if (nOffset)
522 break;
523 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
524 if (pOtherRun->type == diRun)
526 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
527 pOtherRun->member.run.strText->nLen - 1,
528 WB_ISDELIMITER)
529 && !(pRun->member.run.nFlags & MERF_ENDPARA)
530 && !(cursor->pRun == pRun && cursor->nOffset == 0)
531 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
532 WB_ISDELIMITER))
533 break;
534 pRun = pOtherRun;
535 nOffset = pOtherRun->member.run.strText->nLen;
537 else if (pOtherRun->type == diParagraph)
539 if (cursor->pRun == pRun && cursor->nOffset == 0)
541 /* Paragraph breaks are treated as separate words */
542 if (pOtherRun->member.para.prev_para->type == diTextStart)
543 return FALSE;
544 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
546 break;
550 else
552 /* Forward movement */
553 BOOL last_delim = FALSE;
555 while (TRUE)
557 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
558 nOffset, WB_ISDELIMITER))
559 break;
560 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
561 nOffset, WB_MOVEWORDRIGHT);
562 if (nOffset < pRun->member.run.strText->nLen)
563 break;
564 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
565 if (pOtherRun->type == diRun)
567 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
568 nOffset - 1, WB_ISDELIMITER);
569 pRun = pOtherRun;
570 nOffset = 0;
572 else if (pOtherRun->type == diParagraph)
574 if (cursor->pRun == pRun)
575 pRun = ME_FindItemFwd(pOtherRun, diRun);
576 nOffset = 0;
577 break;
579 else /* diTextEnd */
581 if (cursor->pRun == pRun)
582 return FALSE;
583 nOffset = 0;
584 break;
588 cursor->pRun = pRun;
589 cursor->nOffset = nOffset;
590 return TRUE;
594 void
595 ME_SelectWord(ME_TextEditor *editor)
597 if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
598 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
599 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
600 ME_InvalidateSelection(editor);
601 ME_SendSelChange(editor);
605 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
607 ME_Cursor *pCursor = &editor->pCursors[nCursor];
609 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
610 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
613 int ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
615 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
616 int rx = 0;
618 if (is_eol)
619 *is_eol = 0;
621 while(p != editor->pBuffer->pLast)
623 if (p->type == diParagraph)
625 int ry = y - p->member.para.nYPos;
626 if (ry < 0)
628 result->pRun = ME_FindItemFwd(p, diRun);
629 result->nOffset = 0;
630 return 0;
632 if (ry >= p->member.para.nHeight)
634 p = p->member.para.next_para;
635 continue;
637 p = ME_FindItemFwd(p, diStartRow);
638 y = ry;
639 continue;
641 if (p->type == diStartRow)
643 int ry = y - p->member.row.nYPos;
644 if (ry < 0)
645 return 0;
646 if (ry >= p->member.row.nHeight)
648 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
649 if (p->type != diStartRow)
650 return 0;
651 continue;
653 p = ME_FindItemFwd(p, diRun);
654 continue;
656 if (p->type == diRun)
658 ME_DisplayItem *pp;
659 rx = x - p->member.run.pt.x;
660 if (rx < 0)
661 rx = 0;
662 if (rx >= p->member.run.nWidth) /* not this run yet... find next item */
664 pp = p;
665 do {
666 p = p->next;
667 if (p->type == diRun)
669 rx = x - p->member.run.pt.x;
670 goto continue_search;
672 if (p->type == diStartRow)
674 p = ME_FindItemFwd(p, diRun);
675 if (is_eol)
676 *is_eol = 1;
677 rx = 0; /* FIXME not sure */
678 goto found_here;
680 if (p->type == diParagraph || p->type == diTextEnd)
682 rx = 0; /* FIXME not sure */
683 p = pp;
684 goto found_here;
686 } while(1);
687 continue;
689 found_here:
690 if (p->member.run.nFlags & MERF_ENDPARA)
691 rx = 0;
692 result->pRun = p;
693 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
694 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
696 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
697 result->nOffset = 0;
699 return 1;
701 assert(0);
702 continue_search:
705 result->pRun = ME_FindItemBack(p, diRun);
706 result->nOffset = 0;
707 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
708 return 0;
713 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
715 ME_Cursor cursor;
716 RECT rc;
718 GetClientRect(editor->hWnd, &rc);
719 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
720 return -1;
721 y += ME_GetYScrollPos(editor);
722 ME_FindPixelPos(editor, x, y, &cursor, NULL);
723 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
724 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
728 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
730 ME_Cursor tmp_cursor;
731 int is_selection = 0;
733 editor->nUDArrowX = -1;
735 y += ME_GetYScrollPos(editor);
737 tmp_cursor = editor->pCursors[0];
738 is_selection = ME_IsSelection(editor);
740 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
742 if (GetKeyState(VK_SHIFT)>=0)
744 editor->pCursors[1] = editor->pCursors[0];
746 else
748 if (!is_selection) {
749 editor->pCursors[1] = tmp_cursor;
750 is_selection = 1;
753 ME_InvalidateSelection(editor);
754 HideCaret(editor->hWnd);
755 ME_MoveCaret(editor);
756 ShowCaret(editor->hWnd);
757 ME_ClearTempStyle(editor);
758 ME_SendSelChange(editor);
761 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
763 ME_Cursor tmp_cursor;
765 y += ME_GetYScrollPos(editor);
767 tmp_cursor = editor->pCursors[0];
768 /* FIXME: do something with the return value of ME_FindPixelPos */
769 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
771 if (tmp_cursor.pRun == editor->pCursors[0].pRun &&
772 tmp_cursor.nOffset == editor->pCursors[0].nOffset)
773 return;
775 ME_InvalidateSelection(editor);
776 editor->pCursors[0] = tmp_cursor;
777 HideCaret(editor->hWnd);
778 ME_MoveCaret(editor);
779 ME_InvalidateSelection(editor);
780 ShowCaret(editor->hWnd);
781 ME_SendSelChange(editor);
784 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
785 int x, int *pOffset, int *pbCaretAtEnd)
787 ME_DisplayItem *pNext, *pLastRun;
788 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
789 assert(pNext->type == diRun);
790 pLastRun = pNext;
791 *pbCaretAtEnd = FALSE;
792 do {
793 int run_x = pNext->member.run.pt.x;
794 int width = pNext->member.run.nWidth;
795 if (x < run_x)
797 if (pOffset) *pOffset = 0;
798 return pNext;
800 if (x >= run_x && x < run_x+width)
802 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
803 ME_String *s = pNext->member.run.strText;
804 if (ch < s->nLen) {
805 if (pOffset)
806 *pOffset = ch;
807 return pNext;
810 pLastRun = pNext;
811 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
812 } while(pNext && pNext->type == diRun);
814 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
816 pNext = ME_FindItemFwd(pNext, diRun);
817 if (pbCaretAtEnd) *pbCaretAtEnd = 1;
818 if (pOffset) *pOffset = 0;
819 return pNext;
820 } else {
821 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
822 if (pOffset) *pOffset = 0;
823 return pLastRun;
827 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
829 ME_DisplayItem *pRun = pCursor->pRun;
830 int x;
832 if (editor->nUDArrowX != -1)
833 x = editor->nUDArrowX;
834 else {
835 if (editor->bCaretAtEnd)
837 pRun = ME_FindItemBack(pRun, diRun);
838 assert(pRun);
839 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
841 else {
842 x = pRun->member.run.pt.x;
843 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
845 editor->nUDArrowX = x;
847 return x;
851 static void
852 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
854 ME_DisplayItem *pRun = pCursor->pRun;
855 ME_DisplayItem *pItem;
856 int x = ME_GetXForArrow(editor, pCursor);
858 if (editor->bCaretAtEnd && !pCursor->nOffset)
859 pRun = ME_FindItemBack(pRun, diRun);
860 if (!pRun)
861 return;
862 if (nRelOfs == -1)
864 /* start of this row */
865 pItem = ME_FindItemBack(pRun, diStartRow);
866 assert(pItem);
867 /* start of the previous row */
868 pItem = ME_FindItemBack(pItem, diStartRow);
870 else
872 /* start of the next row */
873 pItem = ME_FindItemFwd(pRun, diStartRow);
874 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
877 if (!pItem)
879 /* row not found - ignore */
880 return;
882 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
883 assert(pCursor->pRun);
884 assert(pCursor->pRun->type == diRun);
888 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
890 ME_DisplayItem *pRun = pCursor->pRun;
891 ME_DisplayItem *pLast, *p;
892 int x, y, ys, yd, yp, yprev;
893 ME_Cursor tmp_curs = *pCursor;
895 x = ME_GetXForArrow(editor, pCursor);
896 if (!pCursor->nOffset && editor->bCaretAtEnd)
897 pRun = ME_FindItemBack(pRun, diRun);
899 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
900 assert(p->type == diStartRow);
901 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
902 yprev = ys = y = yp + p->member.row.nYPos;
903 yd = y - editor->sizeWindow.cy;
904 pLast = p;
906 do {
907 p = ME_FindItemBack(p, diStartRowOrParagraph);
908 if (!p)
909 break;
910 if (p->type == diParagraph) { /* crossing paragraphs */
911 if (p->member.para.prev_para == NULL)
912 break;
913 yp = p->member.para.prev_para->member.para.nYPos;
914 continue;
916 y = yp + p->member.row.nYPos;
917 if (y < yd)
918 break;
919 pLast = p;
920 yprev = y;
921 } while(1);
923 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
924 ME_UpdateSelection(editor, &tmp_curs);
925 if (yprev < editor->sizeWindow.cy)
927 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
928 ME_Repaint(editor);
930 else {
931 ME_Scroll(editor, 0, ys-yprev);
932 ME_Repaint(editor);
934 assert(pCursor->pRun);
935 assert(pCursor->pRun->type == diRun);
938 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
939 of pixels, even if it makes the scroll bar position exceed its normal maximum.
940 In such a situation, clicking the scrollbar restores its position back to the
941 normal range (ie. sets it to (doclength-screenheight)). */
943 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
945 ME_DisplayItem *pRun = pCursor->pRun;
946 ME_DisplayItem *pLast, *p;
947 int x, y, ys, yd, yp, yprev;
948 ME_Cursor tmp_curs = *pCursor;
950 x = ME_GetXForArrow(editor, pCursor);
951 if (!pCursor->nOffset && editor->bCaretAtEnd)
952 pRun = ME_FindItemBack(pRun, diRun);
954 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
955 assert(p->type == diStartRow);
956 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
957 yprev = ys = y = yp + p->member.row.nYPos;
958 yd = y + editor->sizeWindow.cy;
959 pLast = p;
961 do {
962 p = ME_FindItemFwd(p, diStartRowOrParagraph);
963 if (!p)
964 break;
965 if (p->type == diParagraph) {
966 yp = p->member.para.nYPos;
967 continue;
969 y = yp + p->member.row.nYPos;
970 if (y >= yd)
971 break;
972 pLast = p;
973 yprev = y;
974 } while(1);
976 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
977 ME_UpdateSelection(editor, &tmp_curs);
978 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
980 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
981 ME_Repaint(editor);
983 else {
984 ME_Scroll(editor, 0, ys-yprev);
985 ME_Repaint(editor);
987 assert(pCursor->pRun);
988 assert(pCursor->pRun->type == diRun);
991 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
993 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
994 if (pRow) {
995 ME_DisplayItem *pRun;
996 if (editor->bCaretAtEnd && !pCursor->nOffset) {
997 pRow = ME_FindItemBack(pRow, diStartRow);
998 if (!pRow)
999 return;
1001 pRun = ME_FindItemFwd(pRow, diRun);
1002 if (pRun) {
1003 pCursor->pRun = pRun;
1004 pCursor->nOffset = 0;
1007 editor->bCaretAtEnd = FALSE;
1010 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1012 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1013 if (pRow) {
1014 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1015 if (pRun) {
1016 pCursor->pRun = pRun;
1017 pCursor->nOffset = 0;
1022 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1024 ME_DisplayItem *pRow;
1026 if (editor->bCaretAtEnd && !pCursor->nOffset)
1027 return;
1029 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1030 assert(pRow);
1031 if (pRow->type == diStartRow) {
1032 /* FIXME WTF was I thinking about here ? */
1033 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1034 assert(pRun);
1035 pCursor->pRun = pRun;
1036 pCursor->nOffset = 0;
1037 editor->bCaretAtEnd = 1;
1038 return;
1040 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1041 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1042 pCursor->nOffset = 0;
1043 editor->bCaretAtEnd = FALSE;
1046 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1048 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1049 assert(p);
1050 p = ME_FindItemBack(p, diRun);
1051 assert(p);
1052 assert(p->member.run.nFlags & MERF_ENDPARA);
1053 pCursor->pRun = p;
1054 pCursor->nOffset = 0;
1055 editor->bCaretAtEnd = FALSE;
1058 BOOL ME_IsSelection(ME_TextEditor *editor)
1060 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1063 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1065 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1067 if (cdir*dir>0)
1068 return 0;
1069 else
1070 return 1;
1073 BOOL ME_UpdateSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
1075 ME_Cursor old_anchor = editor->pCursors[1];
1077 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1079 /* any selection was present ? if so, it's no more, repaint ! */
1080 editor->pCursors[1] = editor->pCursors[0];
1081 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1082 return TRUE;
1084 return FALSE;
1086 else
1088 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1090 editor->pCursors[1] = *pTempCursor;
1091 return TRUE;
1095 ME_Repaint(editor);
1096 return TRUE;
1099 void ME_DeleteSelection(ME_TextEditor *editor)
1101 int from, to;
1102 ME_GetSelection(editor, &from, &to);
1103 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1106 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1108 ME_Style *style;
1109 int from, to;
1110 ME_Cursor c;
1112 ME_GetSelection(editor, &from, &to);
1113 ME_CursorFromCharOfs(editor, from, &c);
1114 if (from != to) {
1115 style = c.pRun->member.run.style;
1116 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1118 else
1119 style = ME_GetInsertStyle(editor, 0);
1120 return style;
1123 void ME_SendSelChange(ME_TextEditor *editor)
1125 SELCHANGE sc;
1127 ME_ClearTempStyle(editor);
1129 if (!(editor->nEventMask & ENM_SELCHANGE))
1130 return;
1132 sc.nmhdr.hwndFrom = editor->hWnd;
1133 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1134 sc.nmhdr.code = EN_SELCHANGE;
1135 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1136 sc.seltyp = SEL_EMPTY;
1137 if (sc.chrg.cpMin != sc.chrg.cpMax)
1138 sc.seltyp |= SEL_TEXT;
1139 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1140 sc.seltyp |= SEL_MULTICHAR;
1141 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1145 BOOL
1146 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1148 int nCursor = 0;
1149 ME_Cursor *p = &editor->pCursors[nCursor];
1150 ME_Cursor tmp_curs = *p;
1151 BOOL success = FALSE;
1153 if (ME_IsSelection(editor) && !extend)
1154 ME_InvalidateSelection(editor);
1156 editor->nUDArrowX = -1;
1157 switch(nVKey) {
1158 case VK_LEFT:
1159 editor->bCaretAtEnd = 0;
1160 if (ctrl)
1161 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1162 else
1163 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1164 break;
1165 case VK_RIGHT:
1166 editor->bCaretAtEnd = 0;
1167 if (ctrl)
1168 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1169 else
1170 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1171 break;
1172 case VK_UP:
1173 ME_MoveCursorLines(editor, &tmp_curs, -1);
1174 break;
1175 case VK_DOWN:
1176 ME_MoveCursorLines(editor, &tmp_curs, +1);
1177 break;
1178 case VK_PRIOR:
1179 ME_ArrowPageUp(editor, &tmp_curs);
1180 break;
1181 case VK_NEXT:
1182 ME_ArrowPageDown(editor, &tmp_curs);
1183 break;
1184 case VK_HOME: {
1185 if (ctrl)
1186 ME_ArrowCtrlHome(editor, &tmp_curs);
1187 else
1188 ME_ArrowHome(editor, &tmp_curs);
1189 editor->bCaretAtEnd = 0;
1190 break;
1192 case VK_END:
1193 if (ctrl)
1194 ME_ArrowCtrlEnd(editor, &tmp_curs);
1195 else
1196 ME_ArrowEnd(editor, &tmp_curs);
1197 break;
1200 if (!extend)
1201 editor->pCursors[1] = tmp_curs;
1202 *p = tmp_curs;
1204 if (ME_IsSelection(editor))
1205 ME_InvalidateSelection(editor);
1206 HideCaret(editor->hWnd);
1207 ME_EnsureVisible(editor, tmp_curs.pRun);
1208 ME_ShowCaret(editor);
1209 ME_SendSelChange(editor);
1210 return success;