- Use OBJ_CASE_INSENSITIVE in object lookup.
[wine/wine-kai.git] / dlls / riched20 / caret.c
blob9c30e5c9a103f8e7d8b46536293cb167b4577f1f
1 /*
2 * RichEdit - Caret and selection functions.
4 * Copyright 2004 by Krzysztof Foltman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "editor.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26 void ME_GetSelection(ME_TextEditor *editor, int *from, int *to)
28 *from = ME_GetCursorOfs(editor, 0);
29 *to = ME_GetCursorOfs(editor, 1);
31 if (*from > *to)
33 int tmp = *from;
34 *from = *to;
35 *to = tmp;
39 int ME_GetTextLength(ME_TextEditor *editor)
41 return ME_CharOfsFromRunOfs(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun), 0);
45 int ME_GetTextLengthEx(ME_TextEditor *editor, GETTEXTLENGTHEX *how)
47 int length;
49 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
50 return E_INVALIDARG;
51 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
52 return E_INVALIDARG;
54 length = ME_GetTextLength(editor);
56 if (how->flags & GTL_USECRLF)
57 length += editor->nParagraphs;
59 if (how->flags & GTL_NUMBYTES)
61 CPINFO cpinfo;
63 if (how->codepage == 1200)
64 return length * 2;
65 if (how->flags & GTL_PRECISE)
66 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
67 if (GetCPInfo(how->codepage, &cpinfo))
68 return length * cpinfo.MaxCharSize;
69 ERR("Invalid codepage %u\n", how->codepage);
70 return E_INVALIDARG;
72 return length;
76 void ME_SetSelection(ME_TextEditor *editor, int from, int to)
78 if (from == 0 && to == -1)
80 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
81 editor->pCursors[1].nOffset = 0;
82 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
83 editor->pCursors[0].nOffset = 0;
84 ME_Repaint(editor);
85 ME_ClearTempStyle(editor);
86 return;
88 if (from == -1)
90 editor->pCursors[1] = editor->pCursors[0];
91 ME_Repaint(editor);
92 ME_ClearTempStyle(editor);
93 return;
95 if (from>to)
97 int tmp = from;
98 from = to;
99 to = tmp;
101 ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
102 ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);
105 void ME_MoveCaret(ME_TextEditor *editor)
107 HDC hDC = GetDC(editor->hWnd);
108 ME_Context c;
110 ME_Cursor *pCursor = &editor->pCursors[0];
111 ME_DisplayItem *pCursorRun = pCursor->pRun;
112 ME_DisplayItem *pSizeRun = pCursor->pRun;
114 ME_InitContext(&c, editor, hDC);
115 assert(!pCursor->nOffset || !editor->bCaretAtEnd);
117 if (pCursorRun->type == diRun) {
118 ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
119 if (row) {
120 ME_DisplayItem *run = pCursorRun;
121 ME_DisplayItem *para;
122 SIZE sz = {0, 0};
123 if (!pCursor->nOffset && !editor->bCaretAtEnd)
125 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrStartRow);
126 if (prev->type == diRun)
127 pSizeRun = prev;
129 assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
130 para = ME_FindItemBack(row, diParagraph);
131 if (editor->bCaretAtEnd && !pCursor->nOffset &&
132 run == ME_FindItemFwd(row, diRun))
134 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
135 if (tmp->type == diRun)
137 row = ME_FindItemBack(tmp, diStartRow);
138 pSizeRun = run = tmp;
139 sz = ME_GetRunSize(&c, &para->member.para, &run->member.run, ME_StrLen(run->member.run.strText));
142 if (pCursor->nOffset && !(run->member.run.nFlags & MERF_SKIPPED)) {
143 sz = ME_GetRunSize(&c, &para->member.para, &run->member.run, pCursor->nOffset);
145 CreateCaret(editor->hWnd, NULL, 0, pSizeRun->member.run.nAscent+pSizeRun->member.run.nDescent);
146 SetCaretPos(run->member.run.pt.x+sz.cx,
147 para->member.para.nYPos+row->member.row.nBaseline+pSizeRun->member.run.pt.y-pSizeRun->member.run.nAscent-ME_GetYScrollPos(editor));
148 } else {
149 assert(0 == "Wrapped paragraph run without a row?");
150 CreateCaret(editor->hWnd, NULL, 0, 10);
151 SetCaretPos(0,0);
154 else {
155 assert(0 == "Cursor not on a run");
156 CreateCaret(editor->hWnd, NULL, 0, 10); /* FIXME use global font */
157 SetCaretPos(0,0);
159 ME_DestroyContext(&c);
160 ReleaseDC(editor->hWnd, hDC);
163 void ME_ShowCaret(ME_TextEditor *ed)
165 ME_MoveCaret(ed);
166 ShowCaret(ed->hWnd);
169 void ME_HideCaret(ME_TextEditor *ed)
171 HideCaret(ed->hWnd);
172 DestroyCaret();
175 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
176 int nChars)
178 ME_Cursor c;
179 int shift = 0;
181 while(nChars > 0)
183 ME_Run *run;
184 ME_CursorFromCharOfs(editor, nOfs, &c);
185 run = &c.pRun->member.run;
186 if (run->nFlags & MERF_ENDPARA) {
187 if (!ME_FindItemFwd(c.pRun, diParagraph))
189 return;
191 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
192 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
193 ME_CheckCharOffsets(editor);
194 nChars--;
195 if (editor->bEmulateVersion10 && nChars)
196 nChars--;
197 continue;
199 else
201 ME_Cursor cursor;
202 int nIntendedChars = nChars;
203 int nCharsToDelete = nChars;
204 int i;
205 int loc = c.nOffset;
207 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
209 cursor = c;
210 ME_StrRelPos(run->strText, loc, &nChars);
211 /* nChars is the number of characters that should be deleted from the
212 FOLLOWING runs (these AFTER cursor.pRun)
213 nCharsToDelete is a number of chars to delete from THIS run */
214 nCharsToDelete -= nChars;
215 shift -= nCharsToDelete;
216 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
217 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
218 debugstr_w(run->strText->szData), run->strText->nLen);
220 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
222 /* undo = reinsert whole run */
223 /* nOfs is a character offset (from the start of the document
224 to the current (deleted) run */
225 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
226 if (pUndo)
227 pUndo->di.member.run.nCharOfs = nOfs;
229 else
231 /* undo = reinsert partial run */
232 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
233 if (pUndo) {
234 ME_DestroyString(pUndo->di.member.run.strText);
235 pUndo->di.member.run.nCharOfs = nOfs;
236 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
239 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
240 TRACE("Shift value: %d\n", shift);
241 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
243 /* update cursors (including c) */
244 for (i=-1; i<editor->nCursors; i++) {
245 ME_Cursor *pThisCur = editor->pCursors + i;
246 if (i == -1) pThisCur = &c;
247 if (pThisCur->pRun == cursor.pRun) {
248 if (pThisCur->nOffset > cursor.nOffset) {
249 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
250 pThisCur->nOffset = cursor.nOffset;
251 else
252 pThisCur->nOffset -= nCharsToDelete;
253 assert(pThisCur->nOffset >= 0);
254 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
256 if (pThisCur->nOffset == ME_StrVLen(run->strText))
258 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
259 assert(pThisCur->pRun->type == diRun);
260 pThisCur->nOffset = 0;
265 /* c = updated data now */
267 if (c.pRun == cursor.pRun)
268 ME_SkipAndPropagateCharOffset(c.pRun, shift);
269 else
270 ME_PropagateCharOffset(c.pRun, shift);
272 if (!ME_StrVLen(cursor.pRun->member.run.strText))
274 TRACE("Removing useless run\n");
275 ME_Remove(cursor.pRun);
276 ME_DestroyDisplayItem(cursor.pRun);
279 shift = 0;
281 ME_CheckCharOffsets(editor);
283 continue;
288 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
289 int nChars)
291 assert(nCursor>=0 && nCursor<editor->nCursors);
292 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
295 static WCHAR wszSpace[] = {' ', 0};
297 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
298 void ME_InsertGraphicsFromCursor(ME_TextEditor *editor, int nCursor)
300 ME_Cursor *pCursor = &editor->pCursors[nCursor];
301 ME_DisplayItem *pItem = NULL;
302 ME_DisplayItem *pNewRun = NULL;
303 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
304 ME_UndoItem *pUndo;
306 /* FIXME no no no */
307 if (ME_IsSelection(editor))
308 ME_DeleteSelection(editor);
310 pUndo = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
311 if (pUndo) {
312 pUndo->nStart = pCursor->nOffset + pCursor->pRun->member.run.nCharOfs + ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs;
313 pUndo->nLen = 1;
315 if (pCursor->nOffset)
317 ME_SplitRunSimple(editor, pCursor->pRun, pCursor->nOffset);
319 pItem = pCursor->pRun;
320 pNewRun = ME_MakeRun(pStyle, ME_MakeStringN(wszSpace, 1), MERF_GRAPHICS);
321 pNewRun->member.run.nCharOfs = pCursor->pRun->member.run.nCharOfs;
322 ME_InsertBefore(pCursor->pRun, pNewRun);
323 ME_PropagateCharOffset(pItem, 1);
324 ME_CheckCharOffsets(editor);
325 ME_SendSelChange(editor);
329 static void
330 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
331 const WCHAR *str, int len, ME_Style *style,
332 int flags)
334 ME_DisplayItem *pNewRun = NULL;
335 ME_Cursor *p = &editor->pCursors[nCursor];
337 editor->bCaretAtEnd = FALSE;
339 assert(p->pRun->type == diRun);
341 ME_AddRefStyle(style);
343 pNewRun = ME_MakeRun(style, ME_MakeStringN(str, len), flags); /* addrefs style */
344 ME_InsertRun(editor, ME_CharOfsFromRunOfs(editor, p->pRun, p->nOffset), pNewRun);
345 ME_DestroyDisplayItem(pNewRun);
346 ME_ReleaseStyle(style);
350 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
351 const WCHAR *str, int len, ME_Style *style)
353 const WCHAR *pos;
354 ME_Cursor *p = NULL;
356 assert(style);
358 /* FIXME really HERE ? */
359 if (ME_IsSelection(editor))
360 ME_DeleteSelection(editor);
362 assert(nCursor>=0 && nCursor<editor->nCursors);
363 if (len == -1)
364 len = lstrlenW(str);
365 while (len)
367 pos = str;
368 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
369 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
370 pos++;
371 if (pos-str < len && *pos == '\t') { /* handle tabs */
372 WCHAR tab = '\t';
374 if (pos!=str)
375 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
377 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
379 pos++;
380 if(pos-str <= len) {
381 len -= pos - str;
382 str = pos;
383 continue;
386 if (pos-str < len) { /* handle EOLs */
387 ME_DisplayItem *tp, *end_run;
388 ME_Paragraph *para;
389 ME_Style *tmp_style;
390 if (pos!=str)
391 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
392 p = &editor->pCursors[nCursor];
393 tp = ME_FindItemBack(p->pRun, diParagraph);
394 para = &tp->member.para;
395 assert(tp);
396 if (p->nOffset) {
397 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
398 p = &editor->pCursors[nCursor];
400 tmp_style = ME_GetInsertStyle(editor, nCursor);
401 /* ME_SplitParagraph increases style refcount */
402 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
403 p->pRun = ME_FindItemFwd(tp, diRun);
404 end_run = ME_FindItemBack(tp, diRun);
405 ME_ReleaseStyle(end_run->member.run.style);
406 end_run->member.run.style = tmp_style;
407 p->nOffset = 0;
408 if(pos-str < len && *pos =='\r')
409 pos++;
410 if(pos-str < len && *pos =='\n')
411 pos++;
412 if(pos-str <= len) {
413 len -= pos - str;
414 str = pos;
415 continue;
418 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
419 len = 0;
423 static BOOL ME_ArrowLeft(ME_TextEditor *editor, ME_Cursor *p)
425 if (p->nOffset) {
426 p->nOffset = ME_StrRelPos2(p->pRun->member.run.strText, p->nOffset, -1);
427 return TRUE;
429 else
431 ME_DisplayItem *pRun = ME_FindItemBack(p->pRun, diRunOrParagraph);
432 assert(pRun);
433 if (pRun->type == diRun) {
434 p->pRun = pRun;
435 assert(p->pRun->type == diRun);
436 assert(pRun->member.run.strText->nLen);
437 p->nOffset = pRun->member.run.strText->nLen;
438 if (p->nOffset) {
439 p->nOffset = ME_StrRelPos2(pRun->member.run.strText, p->nOffset, -1);
440 return TRUE;
442 else
443 assert(0);
445 if (pRun->type == diParagraph)
447 if (pRun->member.para.prev_para->type == diTextStart)
448 return FALSE;
449 assert(pRun->member.para.prev_para->type == diParagraph);
450 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
451 /* every paragraph ought to have at least one run */
452 assert(pRun && pRun->type == diRun);
453 assert(pRun->member.run.nFlags & MERF_ENDPARA);
454 p->pRun = pRun;
455 p->nOffset = 0;
456 return TRUE;
458 assert(0);
460 return FALSE;
463 static BOOL ME_ArrowRight(ME_TextEditor *editor, ME_Cursor *p)
465 ME_DisplayItem *pRun;
467 if (!(p->pRun->member.run.nFlags & MERF_ENDPARA))
469 int new_ofs = ME_StrRelPos2(p->pRun->member.run.strText, p->nOffset, 1);
471 if (new_ofs<p->pRun->member.run.strText->nLen)
473 p->nOffset = new_ofs;
474 return TRUE;
477 pRun = ME_FindItemFwd(p->pRun, diRun);
478 if (pRun) {
479 p->pRun = pRun;
480 assert(p->pRun->type == diRun);
481 p->nOffset = 0;
483 return TRUE;
486 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
488 ME_Cursor *pCursor = &editor->pCursors[nCursor];
490 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
491 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
494 int ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
496 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
497 int rx = 0;
499 if (is_eol)
500 *is_eol = 0;
502 while(p != editor->pBuffer->pLast)
504 if (p->type == diParagraph)
506 int ry = y - p->member.para.nYPos;
507 if (ry < 0)
509 result->pRun = ME_FindItemFwd(p, diRun);
510 result->nOffset = 0;
511 return 0;
513 if (ry >= p->member.para.nHeight)
515 p = p->member.para.next_para;
516 continue;
518 p = ME_FindItemFwd(p, diStartRow);
519 y = ry;
520 continue;
522 if (p->type == diStartRow)
524 int ry = y - p->member.row.nYPos;
525 if (ry < 0)
526 return 0;
527 if (ry >= p->member.row.nHeight)
529 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
530 if (p->type != diStartRow)
531 return 0;
532 continue;
534 p = ME_FindItemFwd(p, diRun);
535 continue;
537 if (p->type == diRun)
539 ME_DisplayItem *pp;
540 rx = x - p->member.run.pt.x;
541 if (rx < 0)
542 rx = 0;
543 if (rx >= p->member.run.nWidth) /* not this run yet... find next item */
545 pp = p;
546 do {
547 p = p->next;
548 if (p->type == diRun)
550 rx = x - p->member.run.pt.x;
551 goto continue_search;
553 if (p->type == diStartRow)
555 p = ME_FindItemFwd(p, diRun);
556 if (is_eol)
557 *is_eol = 1;
558 rx = 0; /* FIXME not sure */
559 goto found_here;
561 if (p->type == diParagraph || p->type == diTextEnd)
563 rx = 0; /* FIXME not sure */
564 p = pp;
565 goto found_here;
567 } while(1);
568 continue;
570 found_here:
571 if (p->member.run.nFlags & MERF_ENDPARA)
572 rx = 0;
573 result->pRun = p;
574 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
575 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
577 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
578 result->nOffset = 0;
580 return 1;
582 assert(0);
583 continue_search:
586 result->pRun = ME_FindItemBack(p, diRun);
587 result->nOffset = 0;
588 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
589 return 0;
594 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
596 ME_Cursor cursor;
597 RECT rc;
599 GetClientRect(editor->hWnd, &rc);
600 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
601 return -1;
602 ME_FindPixelPos(editor, x, y, &cursor, NULL);
603 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
604 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
608 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
610 ME_Cursor tmp_cursor;
611 int is_selection = 0;
613 editor->nUDArrowX = -1;
615 y += ME_GetYScrollPos(editor);
617 tmp_cursor = editor->pCursors[0];
618 is_selection = ME_IsSelection(editor);
620 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
622 if (GetKeyState(VK_SHIFT)>=0)
624 editor->pCursors[1] = editor->pCursors[0];
626 else
628 if (!is_selection) {
629 editor->pCursors[1] = tmp_cursor;
630 is_selection = 1;
633 HideCaret(editor->hWnd);
634 ME_MoveCaret(editor);
635 if (is_selection)
636 ME_Repaint(editor);
637 ShowCaret(editor->hWnd);
638 ME_ClearTempStyle(editor);
639 ME_SendSelChange(editor);
642 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
644 ME_Cursor tmp_cursor;
646 y += ME_GetYScrollPos(editor);
648 tmp_cursor = editor->pCursors[0];
649 if (!ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd))
650 /* return */;
652 if (tmp_cursor.pRun == editor->pCursors[0].pRun &&
653 tmp_cursor.nOffset == editor->pCursors[0].nOffset)
654 return;
656 HideCaret(editor->hWnd);
657 ME_MoveCaret(editor);
658 ME_Repaint(editor);
659 ShowCaret(editor->hWnd);
660 ME_SendSelChange(editor);
663 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
664 int x, int *pOffset, int *pbCaretAtEnd)
666 ME_DisplayItem *pNext, *pLastRun;
667 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
668 assert(pNext->type == diRun);
669 pLastRun = pNext;
670 *pbCaretAtEnd = FALSE;
671 do {
672 int run_x = pNext->member.run.pt.x;
673 int width = pNext->member.run.nWidth;
674 if (x < run_x)
676 if (pOffset) *pOffset = 0;
677 return pNext;
679 if (x >= run_x && x < run_x+width)
681 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
682 ME_String *s = pNext->member.run.strText;
683 if (ch < s->nLen) {
684 if (pOffset)
685 *pOffset = ch;
686 return pNext;
689 pLastRun = pNext;
690 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
691 } while(pNext && pNext->type == diRun);
693 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
695 pNext = ME_FindItemFwd(pNext, diRun);
696 if (pbCaretAtEnd) *pbCaretAtEnd = 1;
697 if (pOffset) *pOffset = 0;
698 return pNext;
699 } else {
700 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
701 if (pOffset) *pOffset = 0;
702 return pLastRun;
706 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
708 ME_DisplayItem *pRun = pCursor->pRun;
709 int x;
711 if (editor->nUDArrowX != -1)
712 x = editor->nUDArrowX;
713 else {
714 if (editor->bCaretAtEnd)
716 pRun = ME_FindItemBack(pRun, diRun);
717 assert(pRun);
718 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
720 else {
721 x = pRun->member.run.pt.x;
722 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
724 editor->nUDArrowX = x;
726 return x;
729 static void ME_ArrowUp(ME_TextEditor *editor, ME_Cursor *pCursor)
731 ME_DisplayItem *pRun = pCursor->pRun;
732 ME_DisplayItem *pItem, *pItem2;
733 int x = ME_GetXForArrow(editor, pCursor);
735 if (editor->bCaretAtEnd && !pCursor->nOffset)
737 pRun = ME_FindItemBack(pRun, diRun);
738 if (!pRun)
739 return;
742 /* start of this row */
743 pItem = ME_FindItemBack(pRun, diStartRow);
744 assert(pItem);
745 /* start of the previous row */
746 pItem2 = ME_FindItemBack(pItem, diStartRow);
747 /* no previous row = the first line of the first paragraph */
748 if (!pItem2) /* can't go up - don't go BOL (as in MS richedit) */
749 return;
750 /* FIXME
751 ME_WrapTextParagraph(editor, ME_FindItemBack(pItem2, diParagraph));
753 pCursor->pRun = ME_FindRunInRow(editor, pItem2, x, &pCursor->nOffset, &editor->bCaretAtEnd);
756 static void ME_ArrowDown(ME_TextEditor *editor, ME_Cursor *pCursor)
758 ME_DisplayItem *pRun = pCursor->pRun;
759 ME_DisplayItem *pItem;
760 int x = ME_GetXForArrow(editor, pCursor);
761 if (!pCursor->nOffset && editor->bCaretAtEnd)
763 pRun = ME_FindItemBack(pRun, diRun);
764 /* x = pRun->member.run.pt.x + pRun->member.run.nWidth; */
766 /* start of the next row */
767 pItem = ME_FindItemFwd(pRun, diStartRow);
768 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
770 if (!pItem)
772 /* next row not found - ignore */
773 return;
775 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
776 assert(pCursor->pRun);
777 assert(pCursor->pRun->type == diRun);
780 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
782 ME_DisplayItem *pRun = pCursor->pRun;
783 ME_DisplayItem *pLast, *p;
784 int x, y, ys, yd, yp, yprev;
785 ME_Cursor tmp_curs = *pCursor;
787 x = ME_GetXForArrow(editor, pCursor);
788 if (!pCursor->nOffset && editor->bCaretAtEnd)
789 pRun = ME_FindItemBack(pRun, diRun);
791 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
792 assert(p->type == diStartRow);
793 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
794 yprev = ys = y = yp + p->member.row.nYPos;
795 yd = y - editor->sizeWindow.cy;
796 pLast = p;
798 do {
799 p = ME_FindItemBack(p, diStartRowOrParagraph);
800 if (!p)
801 break;
802 if (p->type == diParagraph) { /* crossing paragraphs */
803 if (p->member.para.prev_para == NULL)
804 break;
805 yp = p->member.para.prev_para->member.para.nYPos;
806 continue;
808 y = yp + p->member.row.nYPos;
809 if (y < yd)
810 break;
811 pLast = p;
812 yprev = y;
813 } while(1);
815 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
816 ME_UpdateSelection(editor, &tmp_curs);
817 if (yprev < editor->sizeWindow.cy)
819 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
820 ME_Repaint(editor);
822 else {
823 ME_Scroll(editor, 0, ys-yprev);
824 ME_Repaint(editor);
826 assert(pCursor->pRun);
827 assert(pCursor->pRun->type == diRun);
830 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
831 of pixels, even if it makes the scroll bar position exceed its normal maximum.
832 In such a situation, clicking the scrollbar restores its position back to the
833 normal range (ie. sets it to (doclength-screenheight)). */
835 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
837 ME_DisplayItem *pRun = pCursor->pRun;
838 ME_DisplayItem *pLast, *p;
839 int x, y, ys, yd, yp, yprev;
840 ME_Cursor tmp_curs = *pCursor;
842 x = ME_GetXForArrow(editor, pCursor);
843 if (!pCursor->nOffset && editor->bCaretAtEnd)
844 pRun = ME_FindItemBack(pRun, diRun);
846 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
847 assert(p->type == diStartRow);
848 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
849 yprev = ys = y = yp + p->member.row.nYPos;
850 yd = y + editor->sizeWindow.cy;
851 pLast = p;
853 do {
854 p = ME_FindItemFwd(p, diStartRowOrParagraph);
855 if (!p)
856 break;
857 if (p->type == diParagraph) {
858 yp = p->member.para.nYPos;
859 continue;
861 y = yp + p->member.row.nYPos;
862 if (y >= yd)
863 break;
864 pLast = p;
865 yprev = y;
866 } while(1);
868 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
869 ME_UpdateSelection(editor, &tmp_curs);
870 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
872 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
873 ME_Repaint(editor);
875 else {
876 ME_Scroll(editor, 0, ys-yprev);
877 ME_Repaint(editor);
879 assert(pCursor->pRun);
880 assert(pCursor->pRun->type == diRun);
883 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
885 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
886 if (pRow) {
887 ME_DisplayItem *pRun;
888 if (editor->bCaretAtEnd && !pCursor->nOffset) {
889 pRow = ME_FindItemBack(pRow, diStartRow);
890 if (!pRow)
891 return;
893 pRun = ME_FindItemFwd(pRow, diRun);
894 if (pRun) {
895 pCursor->pRun = pRun;
896 pCursor->nOffset = 0;
899 editor->bCaretAtEnd = FALSE;
902 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
904 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
905 if (pRow) {
906 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
907 if (pRun) {
908 pCursor->pRun = pRun;
909 pCursor->nOffset = 0;
914 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
916 ME_DisplayItem *pRow;
918 if (editor->bCaretAtEnd && !pCursor->nOffset)
919 return;
921 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
922 assert(pRow);
923 if (pRow->type == diStartRow) {
924 /* FIXME WTF was I thinking about here ? */
925 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
926 assert(pRun);
927 pCursor->pRun = pRun;
928 pCursor->nOffset = 0;
929 editor->bCaretAtEnd = 1;
930 return;
932 pCursor->pRun = ME_FindItemBack(pRow, diRun);
933 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
934 pCursor->nOffset = 0;
935 editor->bCaretAtEnd = FALSE;
938 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
940 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
941 assert(p);
942 p = ME_FindItemBack(p, diRun);
943 assert(p);
944 assert(p->member.run.nFlags & MERF_ENDPARA);
945 pCursor->pRun = p;
946 pCursor->nOffset = 0;
947 editor->bCaretAtEnd = FALSE;
950 BOOL ME_IsSelection(ME_TextEditor *editor)
952 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
955 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
957 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
959 if (cdir*dir>0)
960 return 0;
961 else
962 return 1;
965 static BOOL ME_CancelSelection(ME_TextEditor *editor, int dir)
967 int cdir;
969 if (GetKeyState(VK_SHIFT)<0)
970 return FALSE;
971 if (!memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor)))
972 return FALSE;
974 cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
976 if (cdir*dir>0)
977 editor->pCursors[1] = editor->pCursors[0];
978 else
979 editor->pCursors[0] = editor->pCursors[1];
980 ME_Repaint(editor);
981 return TRUE;
984 BOOL ME_UpdateSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
986 ME_Cursor old_anchor = editor->pCursors[1];
988 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
990 /* any selection was present ? if so, it's no more, repaint ! */
991 editor->pCursors[1] = editor->pCursors[0];
992 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
993 return TRUE;
995 return FALSE;
997 else
999 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1001 editor->pCursors[1] = *pTempCursor;
1002 return TRUE;
1006 ME_Repaint(editor);
1007 return TRUE;
1010 static void ME_RepaintSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
1012 if (ME_UpdateSelection(editor, pTempCursor)) {
1013 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
1014 ME_Repaint(editor);
1018 void ME_DeleteSelection(ME_TextEditor *editor)
1020 int from, to;
1021 ME_GetSelection(editor, &from, &to);
1022 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1025 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1027 ME_Style *style;
1028 int from, to;
1029 ME_Cursor c;
1031 ME_GetSelection(editor, &from, &to);
1032 ME_CursorFromCharOfs(editor, from, &c);
1033 if (from != to) {
1034 style = c.pRun->member.run.style;
1035 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1037 else
1038 style = ME_GetInsertStyle(editor, 0);
1039 return style;
1042 void ME_SendSelChange(ME_TextEditor *editor)
1044 SELCHANGE sc;
1045 if (!(editor->nEventMask & ENM_SELCHANGE))
1046 return;
1047 sc.nmhdr.hwndFrom = editor->hWnd;
1048 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1049 sc.nmhdr.code = EN_SELCHANGE;
1050 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1051 sc.seltyp = SEL_EMPTY;
1052 if (sc.chrg.cpMin != sc.chrg.cpMax)
1053 sc.seltyp |= SEL_TEXT;
1054 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1055 sc.seltyp |= SEL_MULTICHAR;
1056 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1059 BOOL ME_ArrowKey(ME_TextEditor *editor, int nVKey, int nCtrl)
1061 int nCursor = 0;
1062 ME_Cursor *p = &editor->pCursors[nCursor];
1063 ME_Cursor tmp_curs = *p;
1065 switch(nVKey) {
1066 case VK_UP:
1067 ME_ArrowUp(editor, p);
1068 ME_ClearTempStyle(editor);
1069 ME_RepaintSelection(editor, &tmp_curs);
1070 ME_SendSelChange(editor);
1071 return TRUE;
1072 case VK_DOWN:
1073 ME_ArrowDown(editor, p);
1074 ME_ClearTempStyle(editor);
1075 ME_RepaintSelection(editor, &tmp_curs);
1076 ME_SendSelChange(editor);
1077 return TRUE;
1078 case VK_PRIOR:
1079 ME_ArrowPageUp(editor, p);
1080 ME_ClearTempStyle(editor);
1081 ME_SendSelChange(editor);
1082 return TRUE;
1083 case VK_NEXT:
1084 ME_ArrowPageDown(editor, p);
1085 ME_ClearTempStyle(editor);
1086 ME_SendSelChange(editor);
1087 return TRUE;
1090 editor->nUDArrowX = -1;
1091 switch(nVKey) {
1092 case VK_BACK: { /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1093 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1094 return FALSE;
1095 if (ME_IsSelection(editor))
1097 editor->bCaretAtEnd = FALSE; /* FIXME or maybe not */
1098 ME_DeleteSelection(editor);
1099 ME_UpdateRepaint(editor);
1100 return TRUE;
1102 if (ME_ArrowLeft(editor, p)) {
1103 editor->bCaretAtEnd = FALSE; /* FIXME or maybe not */
1104 ME_ClearTempStyle(editor);
1105 ME_MoveCaret(editor);
1106 ME_DeleteTextAtCursor(editor, nCursor, 1);
1107 ME_UpdateRepaint(editor);
1109 return TRUE;
1111 case VK_DELETE: {
1112 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1113 return FALSE;
1114 /* editor->bCaretAtEnd = 0; FIXME or maybe not */
1115 if (ME_IsSelection(editor))
1117 ME_DeleteSelection(editor);
1118 ME_ClearTempStyle(editor);
1119 ME_UpdateRepaint(editor);
1120 return TRUE;
1122 ME_DeleteTextAtCursor(editor, nCursor, 1);
1123 ME_ClearTempStyle(editor);
1124 ME_UpdateRepaint(editor);
1125 return TRUE;
1127 case VK_HOME: {
1128 if (GetKeyState(VK_CONTROL)<0)
1129 ME_ArrowCtrlHome(editor, p);
1130 else
1131 ME_ArrowHome(editor, p);
1132 editor->bCaretAtEnd = 0;
1133 ME_ClearTempStyle(editor);
1134 ME_RepaintSelection(editor, &tmp_curs);
1135 ME_SendSelChange(editor);
1136 return TRUE;
1138 case VK_END:
1139 if (GetKeyState(VK_CONTROL)<0)
1140 ME_ArrowCtrlEnd(editor, p);
1141 else
1142 ME_ArrowEnd(editor, p);
1143 ME_ClearTempStyle(editor);
1144 ME_RepaintSelection(editor, &tmp_curs);
1145 ME_SendSelChange(editor);
1146 return TRUE;
1147 case VK_LEFT:
1148 editor->bCaretAtEnd = 0;
1149 if (ME_CancelSelection(editor, -1))
1150 return TRUE;
1151 ME_ArrowLeft(editor, p);
1152 ME_RepaintSelection(editor, &tmp_curs);
1153 ME_ClearTempStyle(editor);
1154 ME_SendSelChange(editor);
1155 return TRUE;
1156 case VK_RIGHT:
1157 editor->bCaretAtEnd = 0;
1158 if (ME_CancelSelection(editor, +1))
1159 return TRUE;
1160 ME_ArrowRight(editor, p);
1161 ME_RepaintSelection(editor, &tmp_curs);
1162 ME_ClearTempStyle(editor);
1163 ME_SendSelChange(editor);
1164 return TRUE;
1166 return FALSE;