widl: Use type_t for typedefs, not var_t. Simplify representation.
[wine/gsoc_dplay.git] / dlls / riched20 / caret.c
blobbdab1791871e7082de0b29ed397b4878778a9704
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 void ME_SetSelection(ME_TextEditor *editor, int from, int to)
83 if (from == 0 && to == -1)
85 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
86 editor->pCursors[1].nOffset = 0;
87 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
88 editor->pCursors[0].nOffset = 0;
89 ME_InvalidateSelection(editor);
90 ME_ClearTempStyle(editor);
91 return;
93 if (from == -1)
95 editor->pCursors[1] = editor->pCursors[0];
96 ME_Repaint(editor);
97 ME_ClearTempStyle(editor);
98 return;
100 if (from>to)
102 int tmp = from;
103 from = to;
104 to = tmp;
106 ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
107 ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);
111 void
112 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
113 int *x, int *y, int *height)
115 ME_DisplayItem *pCursorRun = pCursor->pRun;
116 ME_DisplayItem *pSizeRun = pCursor->pRun;
118 assert(!pCursor->nOffset || !editor->bCaretAtEnd);
119 assert(height && x && y);
120 assert(!(ME_GetParagraph(pCursorRun)->member.para.nFlags & MEPF_REWRAP));
121 assert(pCursor->pRun);
122 assert(pCursor->pRun->type == diRun);
124 if (pCursorRun->type == diRun) {
125 ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
127 if (row) {
128 HDC hDC = GetDC(editor->hWnd);
129 ME_Context c;
130 ME_DisplayItem *run = pCursorRun;
131 ME_DisplayItem *para = NULL;
132 SIZE sz = {0, 0};
134 ME_InitContext(&c, editor, hDC);
136 if (!pCursor->nOffset && !editor->bCaretAtEnd)
138 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrStartRow);
139 assert(prev);
140 if (prev->type == diRun)
141 pSizeRun = prev;
143 assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
144 para = ME_FindItemBack(row, diParagraph);
145 assert(para);
146 assert(para->type == diParagraph);
147 if (editor->bCaretAtEnd && !pCursor->nOffset &&
148 run == ME_FindItemFwd(row, diRun))
150 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
151 assert(tmp);
152 if (tmp->type == diRun)
154 row = ME_FindItemBack(tmp, diStartRow);
155 pSizeRun = run = tmp;
156 assert(run);
157 assert(run->type == diRun);
158 sz = ME_GetRunSize(&c, &para->member.para, &run->member.run, ME_StrLen(run->member.run.strText));
161 if (pCursor->nOffset && !(run->member.run.nFlags & MERF_SKIPPED)) {
162 sz = ME_GetRunSize(&c, &para->member.para, &run->member.run, pCursor->nOffset);
165 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
166 *x = run->member.run.pt.x + sz.cx;
167 *y = para->member.para.nYPos + row->member.row.nBaseline + pSizeRun->member.run.pt.y - pSizeRun->member.run.nAscent - ME_GetYScrollPos(editor);
169 ME_DestroyContext(&c);
170 ReleaseDC(editor->hWnd, hDC);
171 return;
174 *height = 10; /* FIXME use global font */
175 *x = 0;
176 *y = 0;
180 void
181 ME_MoveCaret(ME_TextEditor *editor)
183 int x, y, height;
185 ME_WrapMarkedParagraphs(editor);
186 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
187 CreateCaret(editor->hWnd, NULL, 0, height);
188 SetCaretPos(x, y);
192 void ME_ShowCaret(ME_TextEditor *ed)
194 ME_MoveCaret(ed);
195 ShowCaret(ed->hWnd);
198 void ME_HideCaret(ME_TextEditor *ed)
200 HideCaret(ed->hWnd);
201 DestroyCaret();
204 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
205 int nChars)
207 ME_Cursor c;
208 int shift = 0;
210 while(nChars > 0)
212 ME_Run *run;
213 ME_CursorFromCharOfs(editor, nOfs, &c);
214 run = &c.pRun->member.run;
215 if (run->nFlags & MERF_ENDPARA) {
216 if (!ME_FindItemFwd(c.pRun, diParagraph))
218 return;
220 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
221 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
222 ME_CheckCharOffsets(editor);
223 nChars--;
224 if (editor->bEmulateVersion10 && nChars)
225 nChars--;
226 continue;
228 else
230 ME_Cursor cursor;
231 int nIntendedChars = nChars;
232 int nCharsToDelete = nChars;
233 int i;
234 int loc = c.nOffset;
236 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
238 cursor = c;
239 ME_StrRelPos(run->strText, loc, &nChars);
240 /* nChars is the number of characters that should be deleted from the
241 FOLLOWING runs (these AFTER cursor.pRun)
242 nCharsToDelete is a number of chars to delete from THIS run */
243 nCharsToDelete -= nChars;
244 shift -= nCharsToDelete;
245 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
246 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
247 debugstr_w(run->strText->szData), run->strText->nLen);
249 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
251 /* undo = reinsert whole run */
252 /* nOfs is a character offset (from the start of the document
253 to the current (deleted) run */
254 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
255 if (pUndo)
256 pUndo->di.member.run.nCharOfs = nOfs;
258 else
260 /* undo = reinsert partial run */
261 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
262 if (pUndo) {
263 ME_DestroyString(pUndo->di.member.run.strText);
264 pUndo->di.member.run.nCharOfs = nOfs;
265 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
268 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
269 TRACE("Shift value: %d\n", shift);
270 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
272 /* update cursors (including c) */
273 for (i=-1; i<editor->nCursors; i++) {
274 ME_Cursor *pThisCur = editor->pCursors + i;
275 if (i == -1) pThisCur = &c;
276 if (pThisCur->pRun == cursor.pRun) {
277 if (pThisCur->nOffset > cursor.nOffset) {
278 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
279 pThisCur->nOffset = cursor.nOffset;
280 else
281 pThisCur->nOffset -= nCharsToDelete;
282 assert(pThisCur->nOffset >= 0);
283 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
285 if (pThisCur->nOffset == ME_StrVLen(run->strText))
287 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
288 assert(pThisCur->pRun->type == diRun);
289 pThisCur->nOffset = 0;
294 /* c = updated data now */
296 if (c.pRun == cursor.pRun)
297 ME_SkipAndPropagateCharOffset(c.pRun, shift);
298 else
299 ME_PropagateCharOffset(c.pRun, shift);
301 if (!ME_StrVLen(cursor.pRun->member.run.strText))
303 TRACE("Removing useless run\n");
304 ME_Remove(cursor.pRun);
305 ME_DestroyDisplayItem(cursor.pRun);
308 shift = 0;
310 ME_CheckCharOffsets(editor);
312 continue;
317 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
318 int nChars)
320 assert(nCursor>=0 && nCursor<editor->nCursors);
321 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
324 static ME_DisplayItem *
325 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
326 const WCHAR *str, int len, ME_Style *style,
327 int flags)
329 ME_Cursor *p = &editor->pCursors[nCursor];
331 editor->bCaretAtEnd = FALSE;
333 assert(p->pRun->type == diRun);
335 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
339 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
340 void ME_InsertGraphicsFromCursor(ME_TextEditor *editor, int nCursor)
342 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
343 WCHAR space = ' ';
345 /* FIXME no no no */
346 if (ME_IsSelection(editor))
347 ME_DeleteSelection(editor);
349 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
350 MERF_GRAPHICS);
351 ME_SendSelChange(editor);
355 void
356 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
358 WCHAR tab = '\t';
359 ME_DisplayItem *p, *run;
360 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
362 p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
363 MERF_CELL);
364 run = p;
365 while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
367 if (run->member.run.nFlags & MERF_CELL)
369 assert(run->member.run.pCell->next);
370 p->member.run.pCell = run->member.run.pCell->next;
371 return;
374 assert(run->type == diParagraph);
375 assert(run->member.para.bTable);
376 assert(run->member.para.pCells);
377 p->member.run.pCell = run->member.para.pCells;
381 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
382 const WCHAR *str, int len, ME_Style *style)
384 const WCHAR *pos;
385 ME_Cursor *p = NULL;
386 /* FIXME: is this too slow? */
387 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
388 int freeSpace = editor->nTextLimit - ME_GetTextLength(editor);
390 assert(style);
392 /* FIXME really HERE ? */
393 if (ME_IsSelection(editor))
394 ME_DeleteSelection(editor);
396 assert(nCursor>=0 && nCursor<editor->nCursors);
397 if (len == -1)
398 len = lstrlenW(str);
399 len = min(len, freeSpace);
400 while (len)
402 pos = str;
403 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
404 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
405 pos++;
406 if (pos-str < len && *pos == '\t') { /* handle tabs */
407 WCHAR tab = '\t';
409 if (pos!=str)
410 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
412 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
414 pos++;
415 if(pos-str <= len) {
416 len -= pos - str;
417 str = pos;
418 continue;
421 if (pos-str < len) { /* handle EOLs */
422 ME_DisplayItem *tp, *end_run;
423 ME_Style *tmp_style;
424 if (pos!=str)
425 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
426 p = &editor->pCursors[nCursor];
427 if (p->nOffset) {
428 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
429 p = &editor->pCursors[nCursor];
431 tmp_style = ME_GetInsertStyle(editor, nCursor);
432 /* ME_SplitParagraph increases style refcount */
433 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
434 p->pRun = ME_FindItemFwd(tp, diRun);
435 end_run = ME_FindItemBack(tp, diRun);
436 ME_ReleaseStyle(end_run->member.run.style);
437 end_run->member.run.style = tmp_style;
438 p->nOffset = 0;
439 if(pos-str < len && *pos =='\r')
440 pos++;
441 if(pos-str < len && *pos =='\n')
442 pos++;
443 if(pos-str <= len) {
444 len -= pos - str;
445 str = pos;
446 continue;
449 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
450 len = 0;
455 static BOOL
456 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
458 ME_DisplayItem *pRun = pCursor->pRun;
460 if (nRelOfs == -1)
462 if (!pCursor->nOffset)
464 do {
465 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
466 assert(pRun);
467 switch (pRun->type)
469 case diRun:
470 break;
471 case diParagraph:
472 if (pRun->member.para.prev_para->type == diTextStart)
473 return FALSE;
474 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
475 /* every paragraph ought to have at least one run */
476 assert(pRun && pRun->type == diRun);
477 assert(pRun->member.run.nFlags & MERF_ENDPARA);
478 break;
479 default:
480 assert(pRun->type != diRun && pRun->type != diParagraph);
481 return FALSE;
483 } while (RUN_IS_HIDDEN(&pRun->member.run));
484 pCursor->pRun = pRun;
485 if (pRun->member.run.nFlags & MERF_ENDPARA)
486 pCursor->nOffset = 0;
487 else
488 pCursor->nOffset = pRun->member.run.strText->nLen;
491 if (pCursor->nOffset)
492 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
493 return TRUE;
495 else
497 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
499 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
501 if (new_ofs < pRun->member.run.strText->nLen)
503 pCursor->nOffset = new_ofs;
504 return TRUE;
507 do {
508 pRun = ME_FindItemFwd(pRun, diRun);
509 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
510 if (pRun)
512 pCursor->pRun = pRun;
513 pCursor->nOffset = 0;
514 return TRUE;
517 return FALSE;
521 static BOOL
522 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
524 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
525 int nOffset = cursor->nOffset;
527 if (nRelOfs == -1)
529 /* Backward movement */
530 while (TRUE)
532 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
533 nOffset, WB_MOVEWORDLEFT);
534 if (nOffset)
535 break;
536 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
537 if (pOtherRun->type == diRun)
539 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
540 pOtherRun->member.run.strText->nLen - 1,
541 WB_ISDELIMITER)
542 && !(pRun->member.run.nFlags & MERF_ENDPARA)
543 && !(cursor->pRun == pRun && cursor->nOffset == 0)
544 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
545 WB_ISDELIMITER))
546 break;
547 pRun = pOtherRun;
548 nOffset = pOtherRun->member.run.strText->nLen;
550 else if (pOtherRun->type == diParagraph)
552 if (cursor->pRun == pRun && cursor->nOffset == 0)
554 /* Paragraph breaks are treated as separate words */
555 if (pOtherRun->member.para.prev_para->type == diTextStart)
556 return FALSE;
557 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
559 break;
563 else
565 /* Forward movement */
566 BOOL last_delim = FALSE;
568 while (TRUE)
570 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
571 nOffset, WB_ISDELIMITER))
572 break;
573 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
574 nOffset, WB_MOVEWORDRIGHT);
575 if (nOffset < pRun->member.run.strText->nLen)
576 break;
577 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
578 if (pOtherRun->type == diRun)
580 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
581 nOffset - 1, WB_ISDELIMITER);
582 pRun = pOtherRun;
583 nOffset = 0;
585 else if (pOtherRun->type == diParagraph)
587 if (cursor->pRun == pRun)
588 pRun = ME_FindItemFwd(pOtherRun, diRun);
589 nOffset = 0;
590 break;
592 else /* diTextEnd */
594 if (cursor->pRun == pRun)
595 return FALSE;
596 nOffset = 0;
597 break;
601 cursor->pRun = pRun;
602 cursor->nOffset = nOffset;
603 return TRUE;
607 void
608 ME_SelectWord(ME_TextEditor *editor)
610 if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
611 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
612 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
613 ME_InvalidateSelection(editor);
614 ME_SendSelChange(editor);
618 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
620 ME_Cursor *pCursor = &editor->pCursors[nCursor];
622 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
623 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
626 int ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
628 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
629 int rx = 0;
631 if (is_eol)
632 *is_eol = 0;
634 while(p != editor->pBuffer->pLast)
636 if (p->type == diParagraph)
638 int ry = y - p->member.para.nYPos;
639 if (ry < 0)
641 result->pRun = ME_FindItemFwd(p, diRun);
642 result->nOffset = 0;
643 return 0;
645 if (ry >= p->member.para.nHeight)
647 p = p->member.para.next_para;
648 continue;
650 p = ME_FindItemFwd(p, diStartRow);
651 y = ry;
652 continue;
654 if (p->type == diStartRow)
656 int ry = y - p->member.row.nYPos;
657 if (ry < 0)
658 return 0;
659 if (ry >= p->member.row.nHeight)
661 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
662 if (p->type != diStartRow)
663 return 0;
664 continue;
666 p = ME_FindItemFwd(p, diRun);
667 continue;
669 if (p->type == diRun)
671 ME_DisplayItem *pp;
672 rx = x - p->member.run.pt.x;
673 if (rx < 0)
674 rx = 0;
675 if (rx >= p->member.run.nWidth) /* not this run yet... find next item */
677 pp = p;
678 do {
679 p = p->next;
680 if (p->type == diRun)
682 rx = x - p->member.run.pt.x;
683 goto continue_search;
685 if (p->type == diStartRow)
687 p = ME_FindItemFwd(p, diRun);
688 if (is_eol)
689 *is_eol = 1;
690 rx = 0; /* FIXME not sure */
691 goto found_here;
693 if (p->type == diParagraph || p->type == diTextEnd)
695 rx = 0; /* FIXME not sure */
696 p = pp;
697 goto found_here;
699 } while(1);
700 continue;
702 found_here:
703 if (p->member.run.nFlags & MERF_ENDPARA)
704 rx = 0;
705 result->pRun = p;
706 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
707 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
709 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
710 result->nOffset = 0;
712 return 1;
714 assert(0);
715 continue_search:
718 result->pRun = ME_FindItemBack(p, diRun);
719 result->nOffset = 0;
720 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
721 return 0;
726 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
728 ME_Cursor cursor;
729 RECT rc;
731 GetClientRect(editor->hWnd, &rc);
732 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
733 return -1;
734 y += ME_GetYScrollPos(editor);
735 ME_FindPixelPos(editor, x, y, &cursor, NULL);
736 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
737 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
741 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
743 ME_Cursor tmp_cursor;
744 int is_selection = 0;
746 editor->nUDArrowX = -1;
748 y += ME_GetYScrollPos(editor);
750 tmp_cursor = editor->pCursors[0];
751 is_selection = ME_IsSelection(editor);
753 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
755 if (GetKeyState(VK_SHIFT)>=0)
757 editor->pCursors[1] = editor->pCursors[0];
759 else
761 if (!is_selection) {
762 editor->pCursors[1] = tmp_cursor;
763 is_selection = 1;
766 ME_InvalidateSelection(editor);
767 HideCaret(editor->hWnd);
768 ME_MoveCaret(editor);
769 ShowCaret(editor->hWnd);
770 ME_ClearTempStyle(editor);
771 ME_SendSelChange(editor);
774 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
776 ME_Cursor tmp_cursor;
778 y += ME_GetYScrollPos(editor);
780 tmp_cursor = editor->pCursors[0];
781 /* FIXME: do something with the return value of ME_FindPixelPos */
782 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
784 if (tmp_cursor.pRun == editor->pCursors[0].pRun &&
785 tmp_cursor.nOffset == editor->pCursors[0].nOffset)
786 return;
788 ME_InvalidateSelection(editor);
789 editor->pCursors[0] = tmp_cursor;
790 HideCaret(editor->hWnd);
791 ME_MoveCaret(editor);
792 ME_InvalidateSelection(editor);
793 ShowCaret(editor->hWnd);
794 ME_SendSelChange(editor);
797 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
798 int x, int *pOffset, int *pbCaretAtEnd)
800 ME_DisplayItem *pNext, *pLastRun;
801 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
802 assert(pNext->type == diRun);
803 pLastRun = pNext;
804 *pbCaretAtEnd = FALSE;
805 do {
806 int run_x = pNext->member.run.pt.x;
807 int width = pNext->member.run.nWidth;
808 if (x < run_x)
810 if (pOffset) *pOffset = 0;
811 return pNext;
813 if (x >= run_x && x < run_x+width)
815 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
816 ME_String *s = pNext->member.run.strText;
817 if (ch < s->nLen) {
818 if (pOffset)
819 *pOffset = ch;
820 return pNext;
823 pLastRun = pNext;
824 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
825 } while(pNext && pNext->type == diRun);
827 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
829 pNext = ME_FindItemFwd(pNext, diRun);
830 if (pbCaretAtEnd) *pbCaretAtEnd = 1;
831 if (pOffset) *pOffset = 0;
832 return pNext;
833 } else {
834 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
835 if (pOffset) *pOffset = 0;
836 return pLastRun;
840 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
842 ME_DisplayItem *pRun = pCursor->pRun;
843 int x;
845 if (editor->nUDArrowX != -1)
846 x = editor->nUDArrowX;
847 else {
848 if (editor->bCaretAtEnd)
850 pRun = ME_FindItemBack(pRun, diRun);
851 assert(pRun);
852 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
854 else {
855 x = pRun->member.run.pt.x;
856 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
858 editor->nUDArrowX = x;
860 return x;
864 static void
865 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
867 ME_DisplayItem *pRun = pCursor->pRun;
868 ME_DisplayItem *pItem;
869 int x = ME_GetXForArrow(editor, pCursor);
871 if (editor->bCaretAtEnd && !pCursor->nOffset)
872 pRun = ME_FindItemBack(pRun, diRun);
873 if (!pRun)
874 return;
875 if (nRelOfs == -1)
877 /* start of this row */
878 pItem = ME_FindItemBack(pRun, diStartRow);
879 assert(pItem);
880 /* start of the previous row */
881 pItem = ME_FindItemBack(pItem, diStartRow);
883 else
885 /* start of the next row */
886 pItem = ME_FindItemFwd(pRun, diStartRow);
887 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
890 if (!pItem)
892 /* row not found - ignore */
893 return;
895 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
896 assert(pCursor->pRun);
897 assert(pCursor->pRun->type == diRun);
901 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
903 ME_DisplayItem *pRun = pCursor->pRun;
904 ME_DisplayItem *pLast, *p;
905 int x, y, ys, yd, yp, yprev;
906 ME_Cursor tmp_curs = *pCursor;
908 x = ME_GetXForArrow(editor, pCursor);
909 if (!pCursor->nOffset && editor->bCaretAtEnd)
910 pRun = ME_FindItemBack(pRun, diRun);
912 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
913 assert(p->type == diStartRow);
914 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
915 yprev = ys = y = yp + p->member.row.nYPos;
916 yd = y - editor->sizeWindow.cy;
917 pLast = p;
919 do {
920 p = ME_FindItemBack(p, diStartRowOrParagraph);
921 if (!p)
922 break;
923 if (p->type == diParagraph) { /* crossing paragraphs */
924 if (p->member.para.prev_para == NULL)
925 break;
926 yp = p->member.para.prev_para->member.para.nYPos;
927 continue;
929 y = yp + p->member.row.nYPos;
930 if (y < yd)
931 break;
932 pLast = p;
933 yprev = y;
934 } while(1);
936 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
937 ME_UpdateSelection(editor, &tmp_curs);
938 if (yprev < editor->sizeWindow.cy)
940 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
941 ME_Repaint(editor);
943 else {
944 ME_Scroll(editor, 0, ys-yprev);
945 ME_Repaint(editor);
947 assert(pCursor->pRun);
948 assert(pCursor->pRun->type == diRun);
951 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
952 of pixels, even if it makes the scroll bar position exceed its normal maximum.
953 In such a situation, clicking the scrollbar restores its position back to the
954 normal range (ie. sets it to (doclength-screenheight)). */
956 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
958 ME_DisplayItem *pRun = pCursor->pRun;
959 ME_DisplayItem *pLast, *p;
960 int x, y, ys, yd, yp, yprev;
961 ME_Cursor tmp_curs = *pCursor;
963 x = ME_GetXForArrow(editor, pCursor);
964 if (!pCursor->nOffset && editor->bCaretAtEnd)
965 pRun = ME_FindItemBack(pRun, diRun);
967 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
968 assert(p->type == diStartRow);
969 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
970 yprev = ys = y = yp + p->member.row.nYPos;
971 yd = y + editor->sizeWindow.cy;
972 pLast = p;
974 do {
975 p = ME_FindItemFwd(p, diStartRowOrParagraph);
976 if (!p)
977 break;
978 if (p->type == diParagraph) {
979 yp = p->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->nTotalLength-editor->sizeWindow.cy)
993 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
994 ME_Repaint(editor);
996 else {
997 ME_Scroll(editor, 0, ys-yprev);
998 ME_Repaint(editor);
1000 assert(pCursor->pRun);
1001 assert(pCursor->pRun->type == diRun);
1004 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1006 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1007 /* bCaretAtEnd doesn't make sense if the cursor isn't set at the
1008 first character of the next row */
1009 assert(!editor->bCaretAtEnd || !pCursor->nOffset);
1010 ME_WrapMarkedParagraphs(editor);
1011 if (pRow) {
1012 ME_DisplayItem *pRun;
1013 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1014 pRow = ME_FindItemBack(pRow, diStartRow);
1015 if (!pRow)
1016 return;
1018 pRun = ME_FindItemFwd(pRow, diRun);
1019 if (pRun) {
1020 pCursor->pRun = pRun;
1021 pCursor->nOffset = 0;
1024 editor->bCaretAtEnd = FALSE;
1027 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1029 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1030 if (pRow) {
1031 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1032 if (pRun) {
1033 pCursor->pRun = pRun;
1034 pCursor->nOffset = 0;
1039 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1041 ME_DisplayItem *pRow;
1043 if (editor->bCaretAtEnd && !pCursor->nOffset)
1044 return;
1046 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1047 assert(pRow);
1048 if (pRow->type == diStartRow) {
1049 /* FIXME WTF was I thinking about here ? */
1050 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1051 assert(pRun);
1052 pCursor->pRun = pRun;
1053 pCursor->nOffset = 0;
1054 editor->bCaretAtEnd = 1;
1055 return;
1057 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1058 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1059 pCursor->nOffset = 0;
1060 editor->bCaretAtEnd = FALSE;
1063 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1065 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1066 assert(p);
1067 p = ME_FindItemBack(p, diRun);
1068 assert(p);
1069 assert(p->member.run.nFlags & MERF_ENDPARA);
1070 pCursor->pRun = p;
1071 pCursor->nOffset = 0;
1072 editor->bCaretAtEnd = FALSE;
1075 BOOL ME_IsSelection(ME_TextEditor *editor)
1077 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1080 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1082 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1084 if (cdir*dir>0)
1085 return 0;
1086 else
1087 return 1;
1090 BOOL ME_UpdateSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
1092 ME_Cursor old_anchor = editor->pCursors[1];
1094 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1096 /* any selection was present ? if so, it's no more, repaint ! */
1097 editor->pCursors[1] = editor->pCursors[0];
1098 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1099 return TRUE;
1101 return FALSE;
1103 else
1105 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1107 editor->pCursors[1] = *pTempCursor;
1108 return TRUE;
1112 ME_Repaint(editor);
1113 return TRUE;
1116 void ME_DeleteSelection(ME_TextEditor *editor)
1118 int from, to;
1119 ME_GetSelection(editor, &from, &to);
1120 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1123 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1125 ME_Style *style;
1126 int from, to;
1127 ME_Cursor c;
1129 ME_GetSelection(editor, &from, &to);
1130 ME_CursorFromCharOfs(editor, from, &c);
1131 if (from != to) {
1132 style = c.pRun->member.run.style;
1133 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1135 else
1136 style = ME_GetInsertStyle(editor, 0);
1137 return style;
1140 void ME_SendSelChange(ME_TextEditor *editor)
1142 SELCHANGE sc;
1144 ME_ClearTempStyle(editor);
1146 if (!(editor->nEventMask & ENM_SELCHANGE))
1147 return;
1149 sc.nmhdr.hwndFrom = editor->hWnd;
1150 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1151 sc.nmhdr.code = EN_SELCHANGE;
1152 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1153 sc.seltyp = SEL_EMPTY;
1154 if (sc.chrg.cpMin != sc.chrg.cpMax)
1155 sc.seltyp |= SEL_TEXT;
1156 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1157 sc.seltyp |= SEL_MULTICHAR;
1158 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1162 BOOL
1163 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1165 int nCursor = 0;
1166 ME_Cursor *p = &editor->pCursors[nCursor];
1167 ME_Cursor tmp_curs = *p;
1168 BOOL success = FALSE;
1170 ME_CheckCharOffsets(editor);
1171 editor->nUDArrowX = -1;
1172 switch(nVKey) {
1173 case VK_LEFT:
1174 editor->bCaretAtEnd = 0;
1175 if (ctrl)
1176 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1177 else
1178 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1179 break;
1180 case VK_RIGHT:
1181 editor->bCaretAtEnd = 0;
1182 if (ctrl)
1183 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1184 else
1185 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1186 break;
1187 case VK_UP:
1188 ME_MoveCursorLines(editor, &tmp_curs, -1);
1189 break;
1190 case VK_DOWN:
1191 ME_MoveCursorLines(editor, &tmp_curs, +1);
1192 break;
1193 case VK_PRIOR:
1194 ME_ArrowPageUp(editor, &tmp_curs);
1195 break;
1196 case VK_NEXT:
1197 ME_ArrowPageDown(editor, &tmp_curs);
1198 break;
1199 case VK_HOME: {
1200 if (ctrl)
1201 ME_ArrowCtrlHome(editor, &tmp_curs);
1202 else
1203 ME_ArrowHome(editor, &tmp_curs);
1204 editor->bCaretAtEnd = 0;
1205 break;
1207 case VK_END:
1208 if (ctrl)
1209 ME_ArrowCtrlEnd(editor, &tmp_curs);
1210 else
1211 ME_ArrowEnd(editor, &tmp_curs);
1212 break;
1215 if (!extend)
1216 editor->pCursors[1] = tmp_curs;
1217 *p = tmp_curs;
1219 ME_InvalidateSelection(editor);
1220 ME_Repaint(editor);
1221 HideCaret(editor->hWnd);
1222 ME_EnsureVisible(editor, tmp_curs.pRun);
1223 ME_ShowCaret(editor);
1224 ME_SendSelChange(editor);
1225 return success;