Sync riched20 to Wine 1.0-rc1
[reactos.git] / reactos / dll / win32 / riched20 / caret.c
1 /*
2 * RichEdit - Caret and selection functions.
3 *
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Phil Krylov
6 *
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.
11 *
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.
16 *
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
20 */
21
22
23 #include "editor.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26
27 static BOOL
28 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs);
29
30 void ME_GetSelection(ME_TextEditor *editor, int *from, int *to)
31 {
32 *from = ME_GetCursorOfs(editor, 0);
33 *to = ME_GetCursorOfs(editor, 1);
34
35 if (*from > *to)
36 {
37 int tmp = *from;
38 *from = *to;
39 *to = tmp;
40 }
41 }
42
43 int ME_GetTextLength(ME_TextEditor *editor)
44 {
45 return ME_CharOfsFromRunOfs(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun), 0);
46 }
47
48
49 int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how)
50 {
51 int length;
52
53 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
54 return E_INVALIDARG;
55 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
56 return E_INVALIDARG;
57
58 length = ME_GetTextLength(editor);
59
60 if ((GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_MULTILINE)
61 && (how->flags & GTL_USECRLF)
62 && !editor->bEmulateVersion10) /* Ignore GTL_USECRLF flag in 1.0 emulation */
63 length += editor->nParagraphs - 1;
64
65 if (how->flags & GTL_NUMBYTES)
66 {
67 CPINFO cpinfo;
68
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;
77 }
78 return length;
79 }
80
81
82 int ME_SetSelection(ME_TextEditor *editor, int from, int to)
83 {
84 int selectionEnd = 0;
85 const int len = ME_GetTextLength(editor);
86
87 /* all negative values are effectively the same */
88 if (from < 0)
89 from = -1;
90 if (to < 0)
91 to = -1;
92
93 /* select all */
94 if (from == 0 && to == -1)
95 {
96 editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
97 editor->pCursors[1].nOffset = 0;
98 editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
99 editor->pCursors[0].nOffset = 0;
100 ME_InvalidateSelection(editor);
101 ME_ClearTempStyle(editor);
102 return len + 1;
103 }
104
105 /* if both values are equal and also out of bound, that means to */
106 /* put the selection at the end of the text */
107 if ((from == to) && (to < 0 || to > len))
108 {
109 selectionEnd = 1;
110 }
111 else
112 {
113 /* if from is negative and to is positive then selection is */
114 /* deselected and caret moved to end of the current selection */
115 if (from < 0)
116 {
117 int start, end;
118 ME_GetSelection(editor, &start, &end);
119 editor->pCursors[1] = editor->pCursors[0];
120 ME_Repaint(editor);
121 ME_ClearTempStyle(editor);
122 return end;
123 }
124
125 /* adjust to if it's a negative value */
126 if (to < 0)
127 to = len + 1;
128
129 /* flip from and to if they are reversed */
130 if (from>to)
131 {
132 int tmp = from;
133 from = to;
134 to = tmp;
135 }
136
137 /* after fiddling with the values, we find from > len && to > len */
138 if (from > len)
139 selectionEnd = 1;
140 /* special case with to too big */
141 else if (to > len)
142 to = len + 1;
143 }
144
145 if (selectionEnd)
146 {
147 editor->pCursors[1].pRun = editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
148 editor->pCursors[1].nOffset = editor->pCursors[0].nOffset = 0;
149 ME_InvalidateSelection(editor);
150 ME_ClearTempStyle(editor);
151 return len;
152 }
153
154 ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
155 ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);
156 return to;
157 }
158
159
160 void
161 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
162 int *x, int *y, int *height)
163 {
164 ME_DisplayItem *pCursorRun = pCursor->pRun;
165 ME_DisplayItem *pSizeRun = pCursor->pRun;
166
167 assert(!pCursor->nOffset || !editor->bCaretAtEnd);
168 assert(height && x && y);
169 assert(!(ME_GetParagraph(pCursorRun)->member.para.nFlags & MEPF_REWRAP));
170 assert(pCursor->pRun);
171 assert(pCursor->pRun->type == diRun);
172
173 if (pCursorRun->type == diRun) {
174 ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
175
176 if (row) {
177 HDC hDC = GetDC(editor->hWnd);
178 ME_Context c;
179 ME_DisplayItem *run = pCursorRun;
180 ME_DisplayItem *para = NULL;
181 SIZE sz = {0, 0};
182
183 ME_InitContext(&c, editor, hDC);
184
185 if (!pCursor->nOffset && !editor->bCaretAtEnd)
186 {
187 ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrStartRow);
188 assert(prev);
189 if (prev->type == diRun)
190 pSizeRun = prev;
191 }
192 assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
193 para = ME_FindItemBack(row, diParagraph);
194 assert(para);
195 assert(para->type == diParagraph);
196 if (editor->bCaretAtEnd && !pCursor->nOffset &&
197 run == ME_FindItemFwd(row, diRun))
198 {
199 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
200 assert(tmp);
201 if (tmp->type == diRun)
202 {
203 row = ME_FindItemBack(tmp, diStartRow);
204 pSizeRun = run = tmp;
205 assert(run);
206 assert(run->type == diRun);
207 sz = ME_GetRunSize(&c, &para->member.para,
208 &run->member.run, ME_StrLen(run->member.run.strText),
209 row->member.row.nLMargin);
210 }
211 }
212 if (pCursor->nOffset && !(run->member.run.nFlags & MERF_SKIPPED)) {
213 sz = ME_GetRunSize(&c, &para->member.para, &run->member.run, pCursor->nOffset,
214 row->member.row.nLMargin);
215 }
216
217 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
218 *x = run->member.run.pt.x + sz.cx;
219 *y = para->member.para.nYPos + row->member.row.nBaseline + pSizeRun->member.run.pt.y - pSizeRun->member.run.nAscent - ME_GetYScrollPos(editor);
220
221 ME_DestroyContext(&c, editor->hWnd);
222 return;
223 }
224 }
225 *height = 10; /* FIXME use global font */
226 *x = 0;
227 *y = 0;
228 }
229
230
231 void
232 ME_MoveCaret(ME_TextEditor *editor)
233 {
234 int x, y, height;
235
236 if (ME_WrapMarkedParagraphs(editor))
237 ME_UpdateScrollBar(editor);
238 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
239 if(editor->bHaveFocus)
240 {
241 CreateCaret(editor->hWnd, NULL, 0, height);
242 SetCaretPos(x, y);
243 }
244 }
245
246
247 void ME_ShowCaret(ME_TextEditor *ed)
248 {
249 ME_MoveCaret(ed);
250 if(ed->bHaveFocus)
251 ShowCaret(ed->hWnd);
252 }
253
254 void ME_HideCaret(ME_TextEditor *ed)
255 {
256 if(ed->bHaveFocus)
257 {
258 HideCaret(ed->hWnd);
259 DestroyCaret();
260 }
261 }
262
263 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs,
264 int nChars)
265 {
266 ME_Cursor c;
267 int shift = 0;
268
269 while(nChars > 0)
270 {
271 ME_Run *run;
272 ME_CursorFromCharOfs(editor, nOfs, &c);
273 run = &c.pRun->member.run;
274 if (run->nFlags & MERF_ENDPARA) {
275 int eollen = run->nCR + run->nLF;
276
277 if (!ME_FindItemFwd(c.pRun, diParagraph))
278 {
279 return;
280 }
281 ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
282 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
283 ME_CheckCharOffsets(editor);
284 nChars -= (eollen < nChars) ? eollen : nChars;
285 continue;
286 }
287 else
288 {
289 ME_Cursor cursor;
290 int nIntendedChars = nChars;
291 int nCharsToDelete = nChars;
292 int i;
293 int loc = c.nOffset;
294
295 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
296
297 cursor = c;
298 ME_StrRelPos(run->strText, loc, &nChars);
299 /* nChars is the number of characters that should be deleted from the
300 FOLLOWING runs (these AFTER cursor.pRun)
301 nCharsToDelete is a number of chars to delete from THIS run */
302 nCharsToDelete -= nChars;
303 shift -= nCharsToDelete;
304 TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n",
305 nCharsToDelete, nIntendedChars, nChars, c.nOffset,
306 debugstr_w(run->strText->szData), run->strText->nLen);
307
308 if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
309 {
310 /* undo = reinsert whole run */
311 /* nOfs is a character offset (from the start of the document
312 to the current (deleted) run */
313 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
314 if (pUndo)
315 pUndo->di.member.run.nCharOfs = nOfs;
316 }
317 else
318 {
319 /* undo = reinsert partial run */
320 ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
321 if (pUndo) {
322 ME_DestroyString(pUndo->di.member.run.strText);
323 pUndo->di.member.run.nCharOfs = nOfs;
324 pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
325 }
326 }
327 TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
328 TRACE("Shift value: %d\n", shift);
329 ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
330
331 /* update cursors (including c) */
332 for (i=-1; i<editor->nCursors; i++) {
333 ME_Cursor *pThisCur = editor->pCursors + i;
334 if (i == -1) pThisCur = &c;
335 if (pThisCur->pRun == cursor.pRun) {
336 if (pThisCur->nOffset > cursor.nOffset) {
337 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
338 pThisCur->nOffset = cursor.nOffset;
339 else
340 pThisCur->nOffset -= nCharsToDelete;
341 assert(pThisCur->nOffset >= 0);
342 assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
343 }
344 if (pThisCur->nOffset == ME_StrVLen(run->strText))
345 {
346 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
347 assert(pThisCur->pRun->type == diRun);
348 pThisCur->nOffset = 0;
349 }
350 }
351 }
352
353 /* c = updated data now */
354
355 if (c.pRun == cursor.pRun)
356 ME_SkipAndPropagateCharOffset(c.pRun, shift);
357 else
358 ME_PropagateCharOffset(c.pRun, shift);
359
360 if (!ME_StrVLen(cursor.pRun->member.run.strText))
361 {
362 TRACE("Removing useless run\n");
363 ME_Remove(cursor.pRun);
364 ME_DestroyDisplayItem(cursor.pRun);
365 }
366
367 shift = 0;
368 /*
369 ME_CheckCharOffsets(editor);
370 */
371 continue;
372 }
373 }
374 }
375
376 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor,
377 int nChars)
378 {
379 assert(nCursor>=0 && nCursor<editor->nCursors);
380 /* text operations set modified state */
381 editor->nModifyStep = 1;
382 ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
383 }
384
385 static ME_DisplayItem *
386 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
387 const WCHAR *str, int len, ME_Style *style,
388 int flags)
389 {
390 ME_Cursor *p = &editor->pCursors[nCursor];
391
392 editor->bCaretAtEnd = FALSE;
393
394 assert(p->pRun->type == diRun);
395
396 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
397 }
398
399
400 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
401 {
402 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
403 ME_DisplayItem *di;
404 WCHAR space = ' ';
405
406 /* FIXME no no no */
407 if (ME_IsSelection(editor))
408 ME_DeleteSelection(editor);
409
410 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
411 MERF_GRAPHICS);
412 di->member.run.ole_obj = ALLOC_OBJ(*reo);
413 ME_CopyReObject(di->member.run.ole_obj, reo);
414 ME_SendSelChange(editor);
415 }
416
417
418 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
419 {
420 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
421 ME_DisplayItem *di;
422 WCHAR space = ' ';
423
424 /* FIXME no no no */
425 if (ME_IsSelection(editor))
426 ME_DeleteSelection(editor);
427
428 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
429 MERF_ENDROW);
430 ME_SendSelChange(editor);
431 }
432
433 void
434 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
435 {
436 WCHAR tab = '\t';
437 ME_DisplayItem *p, *run;
438 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
439
440 p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
441 MERF_CELL);
442 run = p;
443 while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
444 {
445 if (run->member.run.nFlags & MERF_CELL)
446 {
447 assert(run->member.run.pCell->next);
448 p->member.run.pCell = run->member.run.pCell->next;
449 return;
450 }
451 }
452 assert(run->type == diParagraph);
453 assert(run->member.para.bTable);
454 assert(run->member.para.pCells);
455 p->member.run.pCell = run->member.para.pCells;
456 }
457
458
459 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
460 const WCHAR *str, int len, ME_Style *style)
461 {
462 const WCHAR *pos;
463 ME_Cursor *p = NULL;
464 int oldLen;
465
466 /* FIXME really HERE ? */
467 if (ME_IsSelection(editor))
468 ME_DeleteSelection(editor);
469
470 /* FIXME: is this too slow? */
471 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
472 oldLen = ME_GetTextLength(editor);
473
474 /* text operations set modified state */
475 editor->nModifyStep = 1;
476
477 assert(style);
478
479 assert(nCursor>=0 && nCursor<editor->nCursors);
480 if (len == -1)
481 len = lstrlenW(str);
482
483 /* grow the text limit to fit our text */
484 if(editor->nTextLimit < oldLen +len)
485 editor->nTextLimit = oldLen + len;
486
487 while (len)
488 {
489 pos = str;
490 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
491 while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
492 pos++;
493 if (pos-str < len && *pos == '\t') { /* handle tabs */
494 WCHAR tab = '\t';
495
496 if (pos!=str)
497 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
498
499 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
500
501 pos++;
502 if(pos-str <= len) {
503 len -= pos - str;
504 str = pos;
505 continue;
506 }
507 }
508 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
509 if (!editor->bEmulateVersion10 && pos-str < len-2 && pos[0] == '\r' && pos[1] == '\r' && pos[2] == '\n') {
510 WCHAR space = ' ';
511
512 if (pos!=str)
513 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
514
515 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
516
517 pos+=3;
518 if(pos-str <= len) {
519 len -= pos - str;
520 str = pos;
521 continue;
522 }
523 }
524 if (pos-str < len) { /* handle EOLs */
525 ME_DisplayItem *tp, *end_run;
526 ME_Style *tmp_style;
527 int numCR, numLF;
528
529 if (pos!=str)
530 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
531 p = &editor->pCursors[nCursor];
532 if (p->nOffset) {
533 ME_SplitRunSimple(editor, p->pRun, p->nOffset);
534 p = &editor->pCursors[nCursor];
535 }
536 tmp_style = ME_GetInsertStyle(editor, nCursor);
537 /* ME_SplitParagraph increases style refcount */
538
539 /* Encode and fill number of CR and LF according to emulation mode */
540 if (editor->bEmulateVersion10) {
541 const WCHAR * tpos;
542
543 /* We have to find out how many consecutive \r are there, and if there
544 is a \n terminating the run of \r's. */
545 numCR = 0; numLF = 0;
546 tpos = pos;
547 while (tpos-str < len && *tpos == '\r') {
548 tpos++;
549 numCR++;
550 }
551 if (tpos-str >= len) {
552 /* Reached end of text without finding anything but '\r' */
553 if (tpos != pos) {
554 pos++;
555 }
556 numCR = 1; numLF = 0;
557 } else if (*tpos == '\n') {
558 /* The entire run of \r's plus the one \n is one single line break */
559 pos = tpos + 1;
560 numLF = 1;
561 } else {
562 /* Found some other content past the run of \r's */
563 pos++;
564 numCR = 1; numLF = 0;
565 }
566 } else {
567 if(pos-str < len && *pos =='\r')
568 pos++;
569 if(pos-str < len && *pos =='\n')
570 pos++;
571 numCR = 1; numLF = 0;
572 }
573 tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, numCR, numLF);
574 p->pRun = ME_FindItemFwd(tp, diRun);
575 end_run = ME_FindItemBack(tp, diRun);
576 ME_ReleaseStyle(end_run->member.run.style);
577 end_run->member.run.style = tmp_style;
578 p->nOffset = 0;
579
580 if(pos-str <= len) {
581 len -= pos - str;
582 str = pos;
583 continue;
584 }
585 }
586 ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
587 len = 0;
588 }
589 }
590
591
592 static BOOL
593 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
594 {
595 ME_DisplayItem *pRun = pCursor->pRun;
596
597 if (nRelOfs == -1)
598 {
599 if (!pCursor->nOffset)
600 {
601 do {
602 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
603 assert(pRun);
604 switch (pRun->type)
605 {
606 case diRun:
607 break;
608 case diParagraph:
609 if (pRun->member.para.prev_para->type == diTextStart)
610 return FALSE;
611 pRun = ME_FindItemBack(pRun, diRunOrParagraph);
612 /* every paragraph ought to have at least one run */
613 assert(pRun && pRun->type == diRun);
614 assert(pRun->member.run.nFlags & MERF_ENDPARA);
615 break;
616 default:
617 assert(pRun->type != diRun && pRun->type != diParagraph);
618 return FALSE;
619 }
620 } while (RUN_IS_HIDDEN(&pRun->member.run));
621 pCursor->pRun = pRun;
622 if (pRun->member.run.nFlags & MERF_ENDPARA)
623 pCursor->nOffset = 0;
624 else
625 pCursor->nOffset = pRun->member.run.strText->nLen;
626 }
627
628 if (pCursor->nOffset)
629 pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
630 return TRUE;
631 }
632 else
633 {
634 if (!(pRun->member.run.nFlags & MERF_ENDPARA))
635 {
636 int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
637
638 if (new_ofs < pRun->member.run.strText->nLen)
639 {
640 pCursor->nOffset = new_ofs;
641 return TRUE;
642 }
643 }
644 do {
645 pRun = ME_FindItemFwd(pRun, diRun);
646 } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
647 if (pRun)
648 {
649 pCursor->pRun = pRun;
650 pCursor->nOffset = 0;
651 return TRUE;
652 }
653 }
654 return FALSE;
655 }
656
657
658 static BOOL
659 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
660 {
661 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
662 int nOffset = cursor->nOffset;
663
664 if (nRelOfs == -1)
665 {
666 /* Backward movement */
667 while (TRUE)
668 {
669 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
670 nOffset, WB_MOVEWORDLEFT);
671 if (nOffset)
672 break;
673 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
674 if (pOtherRun->type == diRun)
675 {
676 if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
677 pOtherRun->member.run.strText->nLen - 1,
678 WB_ISDELIMITER)
679 && !(pRun->member.run.nFlags & MERF_ENDPARA)
680 && !(cursor->pRun == pRun && cursor->nOffset == 0)
681 && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
682 WB_ISDELIMITER))
683 break;
684 pRun = pOtherRun;
685 nOffset = pOtherRun->member.run.strText->nLen;
686 }
687 else if (pOtherRun->type == diParagraph)
688 {
689 if (cursor->pRun == pRun && cursor->nOffset == 0)
690 {
691 /* Paragraph breaks are treated as separate words */
692 if (pOtherRun->member.para.prev_para->type == diTextStart)
693 return FALSE;
694 pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
695 }
696 break;
697 }
698 }
699 }
700 else
701 {
702 /* Forward movement */
703 BOOL last_delim = FALSE;
704
705 while (TRUE)
706 {
707 if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
708 nOffset, WB_ISDELIMITER))
709 break;
710 nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
711 nOffset, WB_MOVEWORDRIGHT);
712 if (nOffset < pRun->member.run.strText->nLen)
713 break;
714 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
715 if (pOtherRun->type == diRun)
716 {
717 last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
718 nOffset - 1, WB_ISDELIMITER);
719 pRun = pOtherRun;
720 nOffset = 0;
721 }
722 else if (pOtherRun->type == diParagraph)
723 {
724 if (cursor->pRun == pRun)
725 pRun = ME_FindItemFwd(pOtherRun, diRun);
726 nOffset = 0;
727 break;
728 }
729 else /* diTextEnd */
730 {
731 if (cursor->pRun == pRun)
732 return FALSE;
733 nOffset = 0;
734 break;
735 }
736 }
737 }
738 cursor->pRun = pRun;
739 cursor->nOffset = nOffset;
740 return TRUE;
741 }
742
743
744 void
745 ME_SelectWord(ME_TextEditor *editor)
746 {
747 if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
748 ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
749 ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
750 ME_InvalidateSelection(editor);
751 ME_SendSelChange(editor);
752 }
753
754
755 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
756 {
757 ME_Cursor *pCursor = &editor->pCursors[nCursor];
758 return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
759 + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
760 }
761
762 static void ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
763 {
764 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
765 ME_DisplayItem *last = NULL;
766 int rx = 0;
767
768 if (is_eol)
769 *is_eol = 0;
770
771 /* find paragraph */
772 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
773 {
774 assert(p->type == diParagraph);
775 if (y < p->member.para.nYPos + p->member.para.nHeight)
776 {
777 y -= p->member.para.nYPos;
778 p = ME_FindItemFwd(p, diStartRow);
779 break;
780 }
781 }
782 /* find row */
783 for (; p != editor->pBuffer->pLast; )
784 {
785 ME_DisplayItem *pp;
786 assert(p->type == diStartRow);
787 if (y < p->member.row.nYPos + p->member.row.nHeight)
788 {
789 p = ME_FindItemFwd(p, diRun);
790 break;
791 }
792 pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
793 if (pp->type != diStartRow)
794 {
795 p = ME_FindItemFwd(p, diRun);
796 break;
797 }
798 p = pp;
799 }
800 for (; p != editor->pBuffer->pLast; p = p->next)
801 {
802 switch (p->type)
803 {
804 case diRun:
805 rx = x - p->member.run.pt.x;
806 if (rx < p->member.run.nWidth)
807 {
808 found_here:
809 assert(p->type == diRun);
810 if ((p->member.run.nFlags & MERF_ENDPARA) || rx < 0)
811 rx = 0;
812 result->pRun = p;
813 result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
814 if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
815 {
816 result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
817 result->nOffset = 0;
818 }
819 return;
820 }
821 break;
822 case diStartRow:
823 p = ME_FindItemFwd(p, diRun);
824 if (is_eol) *is_eol = 1;
825 rx = 0; /* FIXME not sure */
826 goto found_here;
827 case diParagraph:
828 case diTextEnd:
829 rx = 0; /* FIXME not sure */
830 p = last;
831 goto found_here;
832 default: assert(0);
833 }
834 last = p;
835 }
836 result->pRun = ME_FindItemBack(p, diRun);
837 result->nOffset = 0;
838 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
839 }
840
841
842 int
843 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
844 {
845 ME_Cursor cursor;
846 RECT rc;
847
848 GetClientRect(editor->hWnd, &rc);
849 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
850 return -1;
851 y += ME_GetYScrollPos(editor);
852 ME_FindPixelPos(editor, x, y, &cursor, NULL);
853 return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
854 + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
855 }
856
857
858 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
859 {
860 ME_Cursor tmp_cursor;
861 int is_selection = 0;
862
863 editor->nUDArrowX = -1;
864
865 y += ME_GetYScrollPos(editor);
866
867 tmp_cursor = editor->pCursors[0];
868 is_selection = ME_IsSelection(editor);
869
870 if (x >= editor->selofs)
871 {
872 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
873 if (GetKeyState(VK_SHIFT)>=0)
874 {
875 editor->pCursors[1] = editor->pCursors[0];
876 }
877 else if (!is_selection) {
878 editor->pCursors[1] = tmp_cursor;
879 is_selection = 1;
880 }
881
882 ME_InvalidateSelection(editor);
883 HideCaret(editor->hWnd);
884 ME_MoveCaret(editor);
885 ShowCaret(editor->hWnd);
886 ME_ClearTempStyle(editor);
887 ME_SendSelChange(editor);
888 }
889 else
890 {
891 ME_DisplayItem *pRow;
892
893 editor->linesel = 1;
894 editor->sely = y;
895 /* Set pCursors[0] to beginning of line */
896 ME_FindPixelPos(editor, x, y, &editor->pCursors[1], &editor->bCaretAtEnd);
897 /* Set pCursors[1] to end of line */
898 pRow = ME_FindItemFwd(editor->pCursors[1].pRun, diStartRowOrParagraphOrEnd);
899 assert(pRow);
900 /* pCursor[0] is the position where the cursor will be drawn,
901 * pCursor[1] is the other end of the selection range
902 * pCursor[2] and [3] are backups of [0] and [1] so I
903 * don't have to look them up again
904 */
905
906 if (pRow->type == diStartRow) {
907 /* FIXME WTF was I thinking about here ? */
908 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
909 assert(pRun);
910 editor->pCursors[0].pRun = pRun;
911 editor->pCursors[0].nOffset = 0;
912 editor->bCaretAtEnd = 1;
913 } else {
914 editor->pCursors[0].pRun = ME_FindItemBack(pRow, diRun);
915 assert(editor->pCursors[0].pRun && editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA);
916 editor->pCursors[0].nOffset = 0;
917 editor->bCaretAtEnd = 0;
918 }
919 editor->pCursors[2] = editor->pCursors[0];
920 editor->pCursors[3] = editor->pCursors[1];
921 ME_InvalidateSelection(editor);
922 HideCaret(editor->hWnd);
923 ME_MoveCaret(editor);
924 ShowCaret(editor->hWnd);
925 ME_ClearTempStyle(editor);
926 ME_SendSelChange(editor);
927 }
928 }
929
930 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
931 {
932 ME_Cursor tmp_cursor;
933
934 y += ME_GetYScrollPos(editor);
935
936 tmp_cursor = editor->pCursors[0];
937 /* FIXME: do something with the return value of ME_FindPixelPos */
938 if (!editor->linesel)
939 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
940 else ME_FindPixelPos(editor, (y > editor->sely) * editor->rcFormat.right, y, &tmp_cursor, &editor->bCaretAtEnd);
941
942 if (!memcmp(&tmp_cursor, editor->pCursors, sizeof(tmp_cursor)))
943 return;
944
945 ME_InvalidateSelection(editor);
946 if (!editor->linesel)
947 editor->pCursors[0] = tmp_cursor;
948 else if (!memcmp(&tmp_cursor, editor->pCursors+2, sizeof(tmp_cursor)) ||
949 !memcmp(&tmp_cursor, editor->pCursors+3, sizeof(tmp_cursor)))
950 {
951 editor->pCursors[0] = editor->pCursors[2];
952 editor->pCursors[1] = editor->pCursors[3];
953 }
954 else if (y < editor->sely)
955 {
956 editor->pCursors[0] = tmp_cursor;
957 editor->pCursors[1] = editor->pCursors[2];
958 }
959 else
960 {
961 editor->pCursors[0] = tmp_cursor;
962 editor->pCursors[1] = editor->pCursors[3];
963 }
964
965 HideCaret(editor->hWnd);
966 ME_MoveCaret(editor);
967 ME_InvalidateSelection(editor);
968 ShowCaret(editor->hWnd);
969 ME_SendSelChange(editor);
970 }
971
972 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
973 int x, int *pOffset, int *pbCaretAtEnd)
974 {
975 ME_DisplayItem *pNext, *pLastRun;
976 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
977 assert(pNext->type == diRun);
978 pLastRun = pNext;
979 *pbCaretAtEnd = FALSE;
980 do {
981 int run_x = pNext->member.run.pt.x;
982 int width = pNext->member.run.nWidth;
983 if (x < run_x)
984 {
985 if (pOffset) *pOffset = 0;
986 return pNext;
987 }
988 if (x >= run_x && x < run_x+width)
989 {
990 int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
991 ME_String *s = pNext->member.run.strText;
992 if (ch < s->nLen) {
993 if (pOffset)
994 *pOffset = ch;
995 return pNext;
996 }
997 }
998 pLastRun = pNext;
999 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
1000 } while(pNext && pNext->type == diRun);
1001
1002 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
1003 {
1004 pNext = ME_FindItemFwd(pNext, diRun);
1005 if (pbCaretAtEnd) *pbCaretAtEnd = 1;
1006 if (pOffset) *pOffset = 0;
1007 return pNext;
1008 } else {
1009 if (pbCaretAtEnd) *pbCaretAtEnd = 0;
1010 if (pOffset) *pOffset = 0;
1011 return pLastRun;
1012 }
1013 }
1014
1015 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1016 {
1017 ME_DisplayItem *pRun = pCursor->pRun;
1018 int x;
1019
1020 if (editor->nUDArrowX != -1)
1021 x = editor->nUDArrowX;
1022 else {
1023 if (editor->bCaretAtEnd)
1024 {
1025 pRun = ME_FindItemBack(pRun, diRun);
1026 assert(pRun);
1027 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1028 }
1029 else {
1030 x = pRun->member.run.pt.x;
1031 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
1032 }
1033 editor->nUDArrowX = x;
1034 }
1035 return x;
1036 }
1037
1038
1039 static void
1040 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1041 {
1042 ME_DisplayItem *pRun = pCursor->pRun;
1043 ME_DisplayItem *pItem;
1044 int x = ME_GetXForArrow(editor, pCursor);
1045
1046 if (editor->bCaretAtEnd && !pCursor->nOffset)
1047 pRun = ME_FindItemBack(pRun, diRun);
1048 if (!pRun)
1049 return;
1050 if (nRelOfs == -1)
1051 {
1052 /* start of this row */
1053 pItem = ME_FindItemBack(pRun, diStartRow);
1054 assert(pItem);
1055 /* start of the previous row */
1056 pItem = ME_FindItemBack(pItem, diStartRow);
1057 }
1058 else
1059 {
1060 /* start of the next row */
1061 pItem = ME_FindItemFwd(pRun, diStartRow);
1062 /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1063 */
1064 }
1065 if (!pItem)
1066 {
1067 /* row not found - ignore */
1068 return;
1069 }
1070 pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1071 assert(pCursor->pRun);
1072 assert(pCursor->pRun->type == diRun);
1073 }
1074
1075
1076 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1077 {
1078 ME_DisplayItem *pRun = pCursor->pRun;
1079 ME_DisplayItem *pLast, *p;
1080 int x, y, ys, yd, yp, yprev;
1081 ME_Cursor tmp_curs = *pCursor;
1082
1083 x = ME_GetXForArrow(editor, pCursor);
1084 if (!pCursor->nOffset && editor->bCaretAtEnd)
1085 pRun = ME_FindItemBack(pRun, diRun);
1086
1087 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1088 assert(p->type == diStartRow);
1089 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1090 yprev = ys = y = yp + p->member.row.nYPos;
1091 yd = y - editor->sizeWindow.cy;
1092 pLast = p;
1093
1094 do {
1095 p = ME_FindItemBack(p, diStartRowOrParagraph);
1096 if (!p)
1097 break;
1098 if (p->type == diParagraph) { /* crossing paragraphs */
1099 if (p->member.para.prev_para == NULL)
1100 break;
1101 yp = p->member.para.prev_para->member.para.nYPos;
1102 continue;
1103 }
1104 y = yp + p->member.row.nYPos;
1105 if (y < yd)
1106 break;
1107 pLast = p;
1108 yprev = y;
1109 } while(1);
1110
1111 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1112 ME_UpdateSelection(editor, &tmp_curs);
1113 if (yprev < editor->sizeWindow.cy)
1114 {
1115 ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
1116 ME_Repaint(editor);
1117 }
1118 else
1119 {
1120 ME_ScrollUp(editor, ys-yprev);
1121 }
1122 assert(pCursor->pRun);
1123 assert(pCursor->pRun->type == diRun);
1124 }
1125
1126 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount
1127 of pixels, even if it makes the scroll bar position exceed its normal maximum.
1128 In such a situation, clicking the scrollbar restores its position back to the
1129 normal range (ie. sets it to (doclength-screenheight)). */
1130
1131 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1132 {
1133 ME_DisplayItem *pRun = pCursor->pRun;
1134 ME_DisplayItem *pLast, *p;
1135 int x, y, ys, yd, yp, yprev;
1136 ME_Cursor tmp_curs = *pCursor;
1137
1138 x = ME_GetXForArrow(editor, pCursor);
1139 if (!pCursor->nOffset && editor->bCaretAtEnd)
1140 pRun = ME_FindItemBack(pRun, diRun);
1141
1142 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1143 assert(p->type == diStartRow);
1144 yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1145 yprev = ys = y = yp + p->member.row.nYPos;
1146 yd = y + editor->sizeWindow.cy;
1147 pLast = p;
1148
1149 do {
1150 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1151 if (!p)
1152 break;
1153 if (p->type == diParagraph) {
1154 yp = p->member.para.nYPos;
1155 continue;
1156 }
1157 y = yp + p->member.row.nYPos;
1158 if (y >= yd)
1159 break;
1160 pLast = p;
1161 yprev = y;
1162 } while(1);
1163
1164 pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1165 ME_UpdateSelection(editor, &tmp_curs);
1166 if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
1167 {
1168 ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1169 ME_Repaint(editor);
1170 }
1171 else
1172 {
1173 ME_ScrollUp(editor,ys-yprev);
1174 }
1175 assert(pCursor->pRun);
1176 assert(pCursor->pRun->type == diRun);
1177 }
1178
1179 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1180 {
1181 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1182 /* bCaretAtEnd doesn't make sense if the cursor isn't set at the
1183 first character of the next row */
1184 assert(!editor->bCaretAtEnd || !pCursor->nOffset);
1185 ME_WrapMarkedParagraphs(editor);
1186 if (pRow) {
1187 ME_DisplayItem *pRun;
1188 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1189 pRow = ME_FindItemBack(pRow, diStartRow);
1190 if (!pRow)
1191 return;
1192 }
1193 pRun = ME_FindItemFwd(pRow, diRun);
1194 if (pRun) {
1195 pCursor->pRun = pRun;
1196 pCursor->nOffset = 0;
1197 }
1198 }
1199 editor->bCaretAtEnd = FALSE;
1200 }
1201
1202 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1203 {
1204 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1205 if (pRow) {
1206 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1207 if (pRun) {
1208 pCursor->pRun = pRun;
1209 pCursor->nOffset = 0;
1210 }
1211 }
1212 }
1213
1214 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1215 {
1216 ME_DisplayItem *pRow;
1217
1218 if (editor->bCaretAtEnd && !pCursor->nOffset)
1219 return;
1220
1221 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1222 assert(pRow);
1223 if (pRow->type == diStartRow) {
1224 /* FIXME WTF was I thinking about here ? */
1225 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1226 assert(pRun);
1227 pCursor->pRun = pRun;
1228 pCursor->nOffset = 0;
1229 editor->bCaretAtEnd = 1;
1230 return;
1231 }
1232 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1233 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1234 pCursor->nOffset = 0;
1235 editor->bCaretAtEnd = FALSE;
1236 }
1237
1238 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1239 {
1240 ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1241 assert(p);
1242 p = ME_FindItemBack(p, diRun);
1243 assert(p);
1244 assert(p->member.run.nFlags & MERF_ENDPARA);
1245 pCursor->pRun = p;
1246 pCursor->nOffset = 0;
1247 editor->bCaretAtEnd = FALSE;
1248 }
1249
1250 BOOL ME_IsSelection(ME_TextEditor *editor)
1251 {
1252 return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1253 }
1254
1255 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1256 {
1257 int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1258
1259 if (cdir*dir>0)
1260 return 0;
1261 else
1262 return 1;
1263 }
1264
1265 BOOL ME_UpdateSelection(ME_TextEditor *editor, const ME_Cursor *pTempCursor)
1266 {
1267 ME_Cursor old_anchor = editor->pCursors[1];
1268
1269 if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1270 {
1271 /* any selection was present ? if so, it's no more, repaint ! */
1272 editor->pCursors[1] = editor->pCursors[0];
1273 if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1274 return TRUE;
1275 }
1276 return FALSE;
1277 }
1278 else
1279 {
1280 if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1281 {
1282 editor->pCursors[1] = *pTempCursor;
1283 return TRUE;
1284 }
1285 }
1286
1287 ME_Repaint(editor);
1288 return TRUE;
1289 }
1290
1291 void ME_DeleteSelection(ME_TextEditor *editor)
1292 {
1293 int from, to;
1294 ME_GetSelection(editor, &from, &to);
1295 ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1296 }
1297
1298 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1299 {
1300 ME_Style *style;
1301 int from, to;
1302
1303 ME_GetSelection(editor, &from, &to);
1304 if (from != to) {
1305 ME_Cursor c;
1306 ME_CursorFromCharOfs(editor, from, &c);
1307 style = c.pRun->member.run.style;
1308 ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1309 }
1310 else
1311 style = ME_GetInsertStyle(editor, 0);
1312 return style;
1313 }
1314
1315 void ME_SendSelChange(ME_TextEditor *editor)
1316 {
1317 SELCHANGE sc;
1318
1319 ME_ClearTempStyle(editor);
1320
1321 if (!(editor->nEventMask & ENM_SELCHANGE))
1322 return;
1323
1324 sc.nmhdr.hwndFrom = editor->hWnd;
1325 sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1326 sc.nmhdr.code = EN_SELCHANGE;
1327 SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1328 sc.seltyp = SEL_EMPTY;
1329 if (sc.chrg.cpMin != sc.chrg.cpMax)
1330 sc.seltyp |= SEL_TEXT;
1331 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1332 sc.seltyp |= SEL_MULTICHAR;
1333 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1334 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1335 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1336 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1337 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1338 {
1339 editor->notified_cr = sc.chrg;
1340 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1341 }
1342 }
1343
1344 BOOL
1345 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1346 {
1347 int nCursor = 0;
1348 ME_Cursor *p = &editor->pCursors[nCursor];
1349 ME_Cursor tmp_curs = *p;
1350 BOOL success = FALSE;
1351
1352 ME_CheckCharOffsets(editor);
1353 editor->nUDArrowX = -1;
1354 switch(nVKey) {
1355 case VK_LEFT:
1356 editor->bCaretAtEnd = 0;
1357 if (ctrl)
1358 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1359 else
1360 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1361 break;
1362 case VK_RIGHT:
1363 editor->bCaretAtEnd = 0;
1364 if (ctrl)
1365 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1366 else
1367 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1368 break;
1369 case VK_UP:
1370 ME_MoveCursorLines(editor, &tmp_curs, -1);
1371 break;
1372 case VK_DOWN:
1373 ME_MoveCursorLines(editor, &tmp_curs, +1);
1374 break;
1375 case VK_PRIOR:
1376 ME_ArrowPageUp(editor, &tmp_curs);
1377 break;
1378 case VK_NEXT:
1379 ME_ArrowPageDown(editor, &tmp_curs);
1380 break;
1381 case VK_HOME: {
1382 if (ctrl)
1383 ME_ArrowCtrlHome(editor, &tmp_curs);
1384 else
1385 ME_ArrowHome(editor, &tmp_curs);
1386 editor->bCaretAtEnd = 0;
1387 break;
1388 }
1389 case VK_END:
1390 if (ctrl)
1391 ME_ArrowCtrlEnd(editor, &tmp_curs);
1392 else
1393 ME_ArrowEnd(editor, &tmp_curs);
1394 break;
1395 }
1396
1397 if (!extend)
1398 editor->pCursors[1] = tmp_curs;
1399 *p = tmp_curs;
1400
1401 ME_InvalidateSelection(editor);
1402 ME_Repaint(editor);
1403 HideCaret(editor->hWnd);
1404 ME_EnsureVisible(editor, tmp_curs.pRun);
1405 ME_ShowCaret(editor);
1406 ME_SendSelChange(editor);
1407 return success;
1408 }