[RICHED20] Sync with Wine Staging 1.9.16. CORE-11866
[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 void ME_SetCursorToStart(ME_TextEditor *editor, ME_Cursor *cursor)
28 {
29 cursor->pPara = editor->pBuffer->pFirst->member.para.next_para;
30 cursor->pRun = ME_FindItemFwd(cursor->pPara, diRun);
31 cursor->nOffset = 0;
32 }
33
34 static void ME_SetCursorToEnd(ME_TextEditor *editor, ME_Cursor *cursor)
35 {
36 cursor->pPara = editor->pBuffer->pLast->member.para.prev_para;
37 cursor->pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
38 cursor->nOffset = 0;
39 }
40
41
42 int ME_GetSelectionOfs(ME_TextEditor *editor, int *from, int *to)
43 {
44 *from = ME_GetCursorOfs(&editor->pCursors[0]);
45 *to = ME_GetCursorOfs(&editor->pCursors[1]);
46
47 if (*from > *to)
48 {
49 int tmp = *from;
50 *from = *to;
51 *to = tmp;
52 return 1;
53 }
54 return 0;
55 }
56
57 int ME_GetSelection(ME_TextEditor *editor, ME_Cursor **from, ME_Cursor **to)
58 {
59 int from_ofs = ME_GetCursorOfs( &editor->pCursors[0] );
60 int to_ofs = ME_GetCursorOfs( &editor->pCursors[1] );
61 BOOL swap = (from_ofs > to_ofs);
62
63 if (from_ofs == to_ofs)
64 {
65 /* If cursor[0] is at the beginning of a run and cursor[1] at the end
66 of the prev run then we need to swap. */
67 if (editor->pCursors[0].nOffset < editor->pCursors[1].nOffset)
68 swap = TRUE;
69 }
70
71 if (!swap)
72 {
73 *from = &editor->pCursors[0];
74 *to = &editor->pCursors[1];
75 return 0;
76 } else {
77 *from = &editor->pCursors[1];
78 *to = &editor->pCursors[0];
79 return 1;
80 }
81 }
82
83 int ME_GetTextLength(ME_TextEditor *editor)
84 {
85 ME_Cursor cursor;
86 ME_SetCursorToEnd(editor, &cursor);
87 return ME_GetCursorOfs(&cursor);
88 }
89
90
91 int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how)
92 {
93 int length;
94
95 if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
96 return E_INVALIDARG;
97 if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
98 return E_INVALIDARG;
99
100 length = ME_GetTextLength(editor);
101
102 if ((editor->styleFlags & ES_MULTILINE)
103 && (how->flags & GTL_USECRLF)
104 && !editor->bEmulateVersion10) /* Ignore GTL_USECRLF flag in 1.0 emulation */
105 length += editor->nParagraphs - 1;
106
107 if (how->flags & GTL_NUMBYTES ||
108 (how->flags & GTL_PRECISE && /* GTL_PRECISE seems to imply GTL_NUMBYTES */
109 !(how->flags & GTL_NUMCHARS))) /* unless GTL_NUMCHARS is given */
110 {
111 CPINFO cpinfo;
112
113 if (how->codepage == 1200)
114 return length * 2;
115 if (how->flags & GTL_PRECISE)
116 FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
117 if (GetCPInfo(how->codepage, &cpinfo))
118 return length * cpinfo.MaxCharSize;
119 ERR("Invalid codepage %u\n", how->codepage);
120 return E_INVALIDARG;
121 }
122 return length;
123 }
124
125
126 int ME_SetSelection(ME_TextEditor *editor, int from, int to)
127 {
128 int selectionEnd = 0;
129 const int len = ME_GetTextLength(editor);
130
131 /* all negative values are effectively the same */
132 if (from < 0)
133 from = -1;
134 if (to < 0)
135 to = -1;
136
137 /* select all */
138 if (from == 0 && to == -1)
139 {
140 ME_SetCursorToStart(editor, &editor->pCursors[1]);
141 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
142 editor->pCursors[0].nOffset = editor->pCursors[0].pRun->member.run.len;
143 ME_InvalidateSelection(editor);
144 return len + 1;
145 }
146
147 /* if both values are equal and also out of bound, that means to */
148 /* put the selection at the end of the text */
149 if ((from == to) && (to < 0 || to > len))
150 {
151 selectionEnd = 1;
152 }
153 else
154 {
155 /* if from is negative and to is positive then selection is */
156 /* deselected and caret moved to end of the current selection */
157 if (from < 0)
158 {
159 int start, end;
160 ME_GetSelectionOfs(editor, &start, &end);
161 if (start != end)
162 {
163 if (end > len)
164 {
165 editor->pCursors[0].nOffset = 0;
166 end --;
167 }
168 editor->pCursors[1] = editor->pCursors[0];
169 ME_Repaint(editor);
170 }
171 return end;
172 }
173
174 /* adjust to if it's a negative value */
175 if (to < 0)
176 to = len + 1;
177
178 /* flip from and to if they are reversed */
179 if (from>to)
180 {
181 int tmp = from;
182 from = to;
183 to = tmp;
184 }
185
186 /* after fiddling with the values, we find from > len && to > len */
187 if (from > len)
188 selectionEnd = 1;
189 /* special case with to too big */
190 else if (to > len)
191 to = len + 1;
192 }
193
194 if (selectionEnd)
195 {
196 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
197 editor->pCursors[1] = editor->pCursors[0];
198 ME_InvalidateSelection(editor);
199 return len;
200 }
201
202 ME_CursorFromCharOfs(editor, from, &editor->pCursors[1]);
203 editor->pCursors[0] = editor->pCursors[1];
204 ME_MoveCursorChars(editor, &editor->pCursors[0], to - from);
205 /* Selection is not allowed in the middle of an end paragraph run. */
206 if (editor->pCursors[1].pRun->member.run.nFlags & MERF_ENDPARA)
207 editor->pCursors[1].nOffset = 0;
208 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA)
209 {
210 if (to > len)
211 editor->pCursors[0].nOffset = editor->pCursors[0].pRun->member.run.len;
212 else
213 editor->pCursors[0].nOffset = 0;
214 }
215 return to;
216 }
217
218
219 void ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
220 int *x, int *y, int *height)
221 {
222 ME_DisplayItem *row;
223 ME_DisplayItem *run = pCursor->pRun;
224 ME_DisplayItem *para = pCursor->pPara;
225 ME_DisplayItem *pSizeRun = run;
226 ME_Context c;
227 int run_x;
228
229 assert(height && x && y);
230 assert(~para->member.para.nFlags & MEPF_REWRAP);
231 assert(run && run->type == diRun);
232 assert(para && para->type == diParagraph);
233
234 row = ME_FindItemBack(run, diStartRowOrParagraph);
235 assert(row && row->type == diStartRow);
236
237 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
238
239 if (!pCursor->nOffset)
240 {
241 ME_DisplayItem *prev = ME_FindItemBack(run, diRunOrParagraph);
242 assert(prev);
243 if (prev->type == diRun)
244 pSizeRun = prev;
245 }
246 if (editor->bCaretAtEnd && !pCursor->nOffset &&
247 run == ME_FindItemFwd(row, diRun))
248 {
249 ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
250 assert(tmp);
251 if (tmp->type == diRun)
252 {
253 row = ME_FindItemBack(tmp, diStartRow);
254 pSizeRun = run = tmp;
255 assert(run);
256 assert(run->type == diRun);
257 }
258 }
259 run_x = ME_PointFromCharContext( &c, &run->member.run, pCursor->nOffset, TRUE );
260
261 *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
262 *x = c.rcView.left + run->member.run.pt.x + run_x - editor->horz_si.nPos;
263 *y = c.rcView.top + para->member.para.pt.y + row->member.row.nBaseline
264 + run->member.run.pt.y - pSizeRun->member.run.nAscent
265 - editor->vert_si.nPos;
266 ME_DestroyContext(&c);
267 return;
268 }
269
270
271 void
272 ME_MoveCaret(ME_TextEditor *editor)
273 {
274 int x, y, height;
275
276 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
277 if(editor->bHaveFocus && !ME_IsSelection(editor))
278 {
279 x = min(x, editor->rcFormat.right-1);
280 ITextHost_TxCreateCaret(editor->texthost, NULL, 0, height);
281 ITextHost_TxSetCaretPos(editor->texthost, x, y);
282 }
283 }
284
285
286 void ME_ShowCaret(ME_TextEditor *ed)
287 {
288 ME_MoveCaret(ed);
289 if(ed->bHaveFocus && !ME_IsSelection(ed))
290 ITextHost_TxShowCaret(ed->texthost, TRUE);
291 }
292
293 void ME_HideCaret(ME_TextEditor *ed)
294 {
295 if(!ed->bHaveFocus || ME_IsSelection(ed))
296 {
297 ITextHost_TxShowCaret(ed->texthost, FALSE);
298 DestroyCaret();
299 }
300 }
301
302 BOOL ME_InternalDeleteText(ME_TextEditor *editor, ME_Cursor *start,
303 int nChars, BOOL bForce)
304 {
305 ME_Cursor c = *start;
306 int nOfs = ME_GetCursorOfs(start), text_len = ME_GetTextLength( editor );
307 int shift = 0;
308 int totalChars = nChars;
309 ME_DisplayItem *start_para;
310 BOOL delete_all = FALSE;
311
312 /* Prevent deletion past last end of paragraph run. */
313 nChars = min(nChars, text_len - nOfs);
314 if (nChars == text_len) delete_all = TRUE;
315 start_para = c.pPara;
316
317 if (!bForce)
318 {
319 ME_ProtectPartialTableDeletion(editor, &c, &nChars);
320 if (nChars == 0)
321 return FALSE;
322 }
323
324 while(nChars > 0)
325 {
326 ME_Run *run;
327 ME_CursorFromCharOfs(editor, nOfs+nChars, &c);
328 if (!c.nOffset &&
329 nOfs+nChars == (c.pRun->member.run.nCharOfs
330 + c.pPara->member.para.nCharOfs))
331 {
332 /* We aren't deleting anything in this run, so we will go back to the
333 * last run we are deleting text in. */
334 ME_PrevRun(&c.pPara, &c.pRun, TRUE);
335 c.nOffset = c.pRun->member.run.len;
336 }
337 run = &c.pRun->member.run;
338 if (run->nFlags & MERF_ENDPARA) {
339 int eollen = c.pRun->member.run.len;
340 BOOL keepFirstParaFormat;
341
342 if (!ME_FindItemFwd(c.pRun, diParagraph))
343 {
344 return TRUE;
345 }
346 keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
347 run->nCharOfs);
348 if (!editor->bEmulateVersion10) /* v4.1 */
349 {
350 ME_DisplayItem *next_para = ME_FindItemFwd(c.pRun, diParagraphOrEnd);
351 ME_DisplayItem *this_para = next_para->member.para.prev_para;
352
353 /* The end of paragraph before a table row is only deleted if there
354 * is nothing else on the line before it. */
355 if (this_para == start_para &&
356 next_para->member.para.nFlags & MEPF_ROWSTART)
357 {
358 /* If the paragraph will be empty, then it should be deleted, however
359 * it still might have text right now which would inherit the
360 * MEPF_STARTROW property if we joined it right now.
361 * Instead we will delete it after the preceding text is deleted. */
362 if (nOfs > this_para->member.para.nCharOfs) {
363 /* Skip this end of line. */
364 nChars -= (eollen < nChars) ? eollen : nChars;
365 continue;
366 }
367 keepFirstParaFormat = TRUE;
368 }
369 }
370 ME_JoinParagraphs(editor, c.pPara, keepFirstParaFormat);
371 /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
372 ME_CheckCharOffsets(editor);
373 nChars -= (eollen < nChars) ? eollen : nChars;
374 continue;
375 }
376 else
377 {
378 ME_Cursor cursor;
379 int nCharsToDelete = min(nChars, c.nOffset);
380 int i;
381
382 c.nOffset -= nCharsToDelete;
383
384 ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
385
386 cursor = c;
387 /* nChars is the number of characters that should be deleted from the
388 PRECEDING runs (these BEFORE cursor.pRun)
389 nCharsToDelete is a number of chars to delete from THIS run */
390 nChars -= nCharsToDelete;
391 shift -= nCharsToDelete;
392 TRACE("Deleting %d (remaining %d) chars at %d in %s (%d)\n",
393 nCharsToDelete, nChars, c.nOffset,
394 debugstr_run( run ), run->len);
395
396 /* nOfs is a character offset (from the start of the document
397 to the current (deleted) run */
398 add_undo_insert_run( editor, nOfs + nChars, get_text( run, c.nOffset ), nCharsToDelete, run->nFlags, run->style );
399
400 ME_StrDeleteV(run->para->text, run->nCharOfs + c.nOffset, nCharsToDelete);
401 run->len -= nCharsToDelete;
402 TRACE("Post deletion string: %s (%d)\n", debugstr_run( run ), run->len);
403 TRACE("Shift value: %d\n", shift);
404
405 /* update cursors (including c) */
406 for (i=-1; i<editor->nCursors; i++) {
407 ME_Cursor *pThisCur = editor->pCursors + i;
408 if (i == -1) pThisCur = &c;
409 if (pThisCur->pRun == cursor.pRun) {
410 if (pThisCur->nOffset > cursor.nOffset) {
411 if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
412 pThisCur->nOffset = cursor.nOffset;
413 else
414 pThisCur->nOffset -= nCharsToDelete;
415 assert(pThisCur->nOffset >= 0);
416 assert(pThisCur->nOffset <= run->len);
417 }
418 if (pThisCur->nOffset == run->len)
419 {
420 pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
421 assert(pThisCur->pRun->type == diRun);
422 pThisCur->nOffset = 0;
423 }
424 }
425 }
426
427 /* c = updated data now */
428
429 if (c.pRun == cursor.pRun)
430 ME_SkipAndPropagateCharOffset(c.pRun, shift);
431 else
432 ME_PropagateCharOffset(c.pRun, shift);
433
434 if (!cursor.pRun->member.run.len)
435 {
436 TRACE("Removing empty run\n");
437 ME_Remove(cursor.pRun);
438 ME_DestroyDisplayItem(cursor.pRun);
439 }
440
441 shift = 0;
442 /*
443 ME_CheckCharOffsets(editor);
444 */
445 continue;
446 }
447 }
448 if (delete_all) ME_SetDefaultParaFormat( editor, start_para->member.para.pFmt );
449 return TRUE;
450 }
451
452 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
453 {
454 assert(nCursor>=0 && nCursor<editor->nCursors);
455 /* text operations set modified state */
456 editor->nModifyStep = 1;
457 return ME_InternalDeleteText(editor, &editor->pCursors[nCursor],
458 nChars, FALSE);
459 }
460
461 static ME_DisplayItem *
462 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
463 const WCHAR *str, int len, ME_Style *style,
464 int flags)
465 {
466 ME_Cursor *p = &editor->pCursors[nCursor];
467
468 editor->bCaretAtEnd = FALSE;
469
470 assert(p->pRun->type == diRun);
471
472 return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
473 }
474
475
476 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
477 {
478 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
479 ME_DisplayItem *di;
480 WCHAR space = ' ';
481
482 /* FIXME no no no */
483 if (ME_IsSelection(editor))
484 ME_DeleteSelection(editor);
485
486 di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
487 MERF_GRAPHICS);
488 di->member.run.ole_obj = ALLOC_OBJ(*reo);
489 ME_CopyReObject(di->member.run.ole_obj, reo);
490 ME_ReleaseStyle(pStyle);
491 }
492
493
494 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
495 {
496 ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
497 WCHAR space = ' ';
498
499 /* FIXME no no no */
500 if (ME_IsSelection(editor))
501 ME_DeleteSelection(editor);
502
503 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
504 MERF_ENDROW);
505 ME_ReleaseStyle(pStyle);
506 }
507
508
509 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor,
510 const WCHAR *str, int len, ME_Style *style)
511 {
512 const WCHAR *pos;
513 ME_Cursor *p = NULL;
514 int oldLen;
515
516 /* FIXME really HERE ? */
517 if (ME_IsSelection(editor))
518 ME_DeleteSelection(editor);
519
520 /* FIXME: is this too slow? */
521 /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
522 oldLen = ME_GetTextLength(editor);
523
524 /* text operations set modified state */
525 editor->nModifyStep = 1;
526
527 assert(style);
528
529 assert(nCursor>=0 && nCursor<editor->nCursors);
530 if (len == -1)
531 len = lstrlenW(str);
532
533 /* grow the text limit to fit our text */
534 if(editor->nTextLimit < oldLen +len)
535 editor->nTextLimit = oldLen + len;
536
537 pos = str;
538
539 while (len)
540 {
541 /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
542 while(pos - str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
543 pos++;
544
545 if (pos != str) { /* handle text */
546 ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
547 } else if (*pos == '\t') { /* handle tabs */
548 WCHAR tab = '\t';
549 ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
550 pos++;
551 } else { /* handle EOLs */
552 ME_DisplayItem *tp, *end_run, *run, *prev;
553 ME_Style *tmp_style;
554 int eol_len = 0;
555
556 /* Find number of CR and LF in end of paragraph run */
557 if (*pos =='\r')
558 {
559 if (len > 1 && pos[1] == '\n')
560 eol_len = 2;
561 else if (len > 2 && pos[1] == '\r' && pos[2] == '\n')
562 eol_len = 3;
563 else
564 eol_len = 1;
565 } else {
566 assert(*pos == '\n');
567 eol_len = 1;
568 }
569 pos += eol_len;
570
571 if (!editor->bEmulateVersion10 && eol_len == 3)
572 {
573 /* handle special \r\r\n sequence (richedit 2.x and higher only) */
574 WCHAR space = ' ';
575 ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
576 } else {
577 const WCHAR cr = '\r', *eol_str = str;
578
579 if (!editor->bEmulateVersion10)
580 {
581 eol_str = &cr;
582 eol_len = 1;
583 }
584
585 p = &editor->pCursors[nCursor];
586
587 if (p->nOffset == p->pRun->member.run.len)
588 {
589 run = ME_FindItemFwd( p->pRun, diRun );
590 if (!run) run = p->pRun;
591 }
592 else
593 {
594 if (p->nOffset) ME_SplitRunSimple(editor, p);
595 run = p->pRun;
596 }
597
598 tmp_style = ME_GetInsertStyle(editor, nCursor);
599 /* ME_SplitParagraph increases style refcount */
600 tp = ME_SplitParagraph(editor, run, run->member.run.style, eol_str, eol_len, 0);
601
602 end_run = ME_FindItemBack(tp, diRun);
603 ME_ReleaseStyle(end_run->member.run.style);
604 end_run->member.run.style = tmp_style;
605
606 /* Move any cursors that were at the end of the previous run to the beginning of the new para */
607 prev = ME_FindItemBack( end_run, diRun );
608 if (prev)
609 {
610 int i;
611 for (i = 0; i < editor->nCursors; i++)
612 {
613 if (editor->pCursors[i].pRun == prev &&
614 editor->pCursors[i].nOffset == prev->member.run.len)
615 {
616 editor->pCursors[i].pPara = tp;
617 editor->pCursors[i].pRun = run;
618 editor->pCursors[i].nOffset = 0;
619 }
620 }
621 }
622
623 }
624 }
625 len -= pos - str;
626 str = pos;
627 }
628 }
629
630 /* Move the cursor nRelOfs characters (either forwards or backwards)
631 *
632 * returns the actual number of characters moved.
633 **/
634 int ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
635 {
636 cursor->nOffset += nRelOfs;
637 if (cursor->nOffset < 0)
638 {
639 cursor->nOffset += cursor->pRun->member.run.nCharOfs;
640 if (cursor->nOffset >= 0)
641 {
642 /* new offset in the same paragraph */
643 do {
644 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
645 } while (cursor->nOffset < cursor->pRun->member.run.nCharOfs);
646 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
647 return nRelOfs;
648 }
649
650 cursor->nOffset += cursor->pPara->member.para.nCharOfs;
651 if (cursor->nOffset <= 0)
652 {
653 /* moved to the start of the text */
654 nRelOfs -= cursor->nOffset;
655 ME_SetCursorToStart(editor, cursor);
656 return nRelOfs;
657 }
658
659 /* new offset in a previous paragraph */
660 do {
661 cursor->pPara = cursor->pPara->member.para.prev_para;
662 } while (cursor->nOffset < cursor->pPara->member.para.nCharOfs);
663 cursor->nOffset -= cursor->pPara->member.para.nCharOfs;
664
665 cursor->pRun = ME_FindItemBack(cursor->pPara->member.para.next_para, diRun);
666 while (cursor->nOffset < cursor->pRun->member.run.nCharOfs) {
667 cursor->pRun = ME_FindItemBack(cursor->pRun, diRun);
668 }
669 cursor->nOffset -= cursor->pRun->member.run.nCharOfs;
670 } else if (cursor->nOffset >= cursor->pRun->member.run.len) {
671 ME_DisplayItem *next_para;
672 int new_offset;
673
674 new_offset = ME_GetCursorOfs(cursor);
675 next_para = cursor->pPara->member.para.next_para;
676 if (new_offset < next_para->member.para.nCharOfs)
677 {
678 /* new offset in the same paragraph */
679 do {
680 cursor->nOffset -= cursor->pRun->member.run.len;
681 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
682 } while (cursor->nOffset >= cursor->pRun->member.run.len);
683 return nRelOfs;
684 }
685
686 if (new_offset >= ME_GetTextLength(editor))
687 {
688 /* new offset at the end of the text */
689 ME_SetCursorToEnd(editor, cursor);
690 nRelOfs -= new_offset - ME_GetTextLength(editor);
691 return nRelOfs;
692 }
693
694 /* new offset in a following paragraph */
695 do {
696 cursor->pPara = next_para;
697 next_para = next_para->member.para.next_para;
698 } while (new_offset >= next_para->member.para.nCharOfs);
699
700 cursor->nOffset = new_offset - cursor->pPara->member.para.nCharOfs;
701 cursor->pRun = ME_FindItemFwd(cursor->pPara, diRun);
702 while (cursor->nOffset >= cursor->pRun->member.run.len)
703 {
704 cursor->nOffset -= cursor->pRun->member.run.len;
705 cursor->pRun = ME_FindItemFwd(cursor->pRun, diRun);
706 }
707 } /* else new offset is in the same run */
708 return nRelOfs;
709 }
710
711
712 BOOL
713 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
714 {
715 ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
716 ME_DisplayItem *pPara = cursor->pPara;
717 int nOffset = cursor->nOffset;
718
719 if (nRelOfs == -1)
720 {
721 /* Backward movement */
722 while (TRUE)
723 {
724 nOffset = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
725 pRun->member.run.len, nOffset, WB_MOVEWORDLEFT);
726 if (nOffset)
727 break;
728 pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
729 if (pOtherRun->type == diRun)
730 {
731 if (ME_CallWordBreakProc(editor, get_text( &pOtherRun->member.run, 0 ),
732 pOtherRun->member.run.len,
733 pOtherRun->member.run.len - 1,
734 WB_ISDELIMITER)
735 && !(pRun->member.run.nFlags & MERF_ENDPARA)
736 && !(cursor->pRun == pRun && cursor->nOffset == 0)
737 && !ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
738 pRun->member.run.len, 0,
739 WB_ISDELIMITER))
740 break;
741 pRun = pOtherRun;
742 nOffset = pOtherRun->member.run.len;
743 }
744 else if (pOtherRun->type == diParagraph)
745 {
746 if (cursor->pRun == pRun && cursor->nOffset == 0)
747 {
748 pPara = pOtherRun;
749 /* Skip empty start of table row paragraph */
750 if (pPara->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART)
751 pPara = pPara->member.para.prev_para;
752 /* Paragraph breaks are treated as separate words */
753 if (pPara->member.para.prev_para->type == diTextStart)
754 return FALSE;
755
756 pRun = ME_FindItemBack(pPara, diRun);
757 pPara = pPara->member.para.prev_para;
758 }
759 break;
760 }
761 }
762 }
763 else
764 {
765 /* Forward movement */
766 BOOL last_delim = FALSE;
767
768 while (TRUE)
769 {
770 if (last_delim && !ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
771 pRun->member.run.len, nOffset, WB_ISDELIMITER))
772 break;
773 nOffset = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
774 pRun->member.run.len, nOffset, WB_MOVEWORDRIGHT);
775 if (nOffset < pRun->member.run.len)
776 break;
777 pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
778 if (pOtherRun->type == diRun)
779 {
780 last_delim = ME_CallWordBreakProc(editor, get_text( &pRun->member.run, 0 ),
781 pRun->member.run.len, nOffset - 1, WB_ISDELIMITER);
782 pRun = pOtherRun;
783 nOffset = 0;
784 }
785 else if (pOtherRun->type == diParagraph)
786 {
787 if (pOtherRun->member.para.nFlags & MEPF_ROWSTART)
788 pOtherRun = pOtherRun->member.para.next_para;
789 if (cursor->pRun == pRun) {
790 pPara = pOtherRun;
791 pRun = ME_FindItemFwd(pPara, diRun);
792 }
793 nOffset = 0;
794 break;
795 }
796 else /* diTextEnd */
797 {
798 if (cursor->pRun == pRun)
799 return FALSE;
800 nOffset = 0;
801 break;
802 }
803 }
804 }
805 cursor->pPara = pPara;
806 cursor->pRun = pRun;
807 cursor->nOffset = nOffset;
808 return TRUE;
809 }
810
811
812 static void
813 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
814 {
815 /* pCursor[0] is the end of the selection
816 * pCursor[1] is the start of the selection (or the position selection anchor)
817 * pCursor[2] and [3] are the selection anchors that are backed up
818 * so they are kept when the selection changes for drag selection.
819 */
820
821 editor->nSelectionType = selectionType;
822 switch(selectionType)
823 {
824 case stPosition:
825 break;
826 case stWord:
827 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
828 editor->pCursors[1] = editor->pCursors[0];
829 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
830 break;
831 case stLine:
832 case stParagraph:
833 {
834 ME_DisplayItem *pItem;
835 ME_DIType fwdSearchType, backSearchType;
836 if (selectionType == stParagraph) {
837 backSearchType = diParagraph;
838 fwdSearchType = diParagraphOrEnd;
839 } else {
840 backSearchType = diStartRow;
841 fwdSearchType = diStartRowOrParagraphOrEnd;
842 }
843 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
844 assert(pItem);
845 if (pItem->type == diTextEnd)
846 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
847 else
848 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
849 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
850 editor->pCursors[0].nOffset = 0;
851
852 pItem = ME_FindItemBack(pItem, backSearchType);
853 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
854 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
855 editor->pCursors[1].nOffset = 0;
856 break;
857 }
858 case stDocument:
859 /* Select everything with cursor anchored from the start of the text */
860 editor->nSelectionType = stDocument;
861 ME_SetCursorToStart(editor, &editor->pCursors[1]);
862 ME_SetCursorToEnd(editor, &editor->pCursors[0]);
863 break;
864 default: assert(0);
865 }
866 /* Store the anchor positions for extending the selection. */
867 editor->pCursors[2] = editor->pCursors[0];
868 editor->pCursors[3] = editor->pCursors[1];
869 }
870
871 int ME_GetCursorOfs(const ME_Cursor *cursor)
872 {
873 return cursor->pPara->member.para.nCharOfs
874 + cursor->pRun->member.run.nCharOfs + cursor->nOffset;
875 }
876
877 /* Helper function for ME_FindPixelPos to find paragraph within tables */
878 static ME_DisplayItem* ME_FindPixelPosInTableRow(int x, int y,
879 ME_DisplayItem *para)
880 {
881 ME_DisplayItem *cell, *next_cell;
882 assert(para->member.para.nFlags & MEPF_ROWSTART);
883 cell = para->member.para.next_para->member.para.pCell;
884 assert(cell);
885
886 /* find the cell we are in */
887 while ((next_cell = cell->member.cell.next_cell) != NULL) {
888 if (x < next_cell->member.cell.pt.x)
889 {
890 para = ME_FindItemFwd(cell, diParagraph);
891 /* Found the cell, but there might be multiple paragraphs in
892 * the cell, so need to search down the cell for the paragraph. */
893 while (cell == para->member.para.pCell) {
894 if (y < para->member.para.pt.y + para->member.para.nHeight)
895 {
896 if (para->member.para.nFlags & MEPF_ROWSTART)
897 return ME_FindPixelPosInTableRow(x, y, para);
898 else
899 return para;
900 }
901 para = para->member.para.next_para;
902 }
903 /* Past the end of the cell, so go back to the last cell paragraph */
904 return para->member.para.prev_para;
905 }
906 cell = next_cell;
907 }
908 /* Return table row delimiter */
909 para = ME_FindItemFwd(cell, diParagraph);
910 assert(para->member.para.nFlags & MEPF_ROWEND);
911 assert(para->member.para.pFmt->dwMask & PFM_TABLEROWDELIMITER);
912 assert(para->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER);
913 return para;
914 }
915
916 static BOOL ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow,
917 int x, ME_Cursor *cursor, BOOL *pbCaretAtEnd)
918 {
919 ME_DisplayItem *pNext, *pLastRun;
920 ME_Row *row = &pRow->member.row;
921 BOOL exact = TRUE;
922
923 if (x < row->pt.x)
924 {
925 x = row->pt.x;
926 exact = FALSE;
927 }
928 pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
929 assert(pNext->type == diRun);
930 if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
931 cursor->nOffset = 0;
932 do {
933 int run_x = pNext->member.run.pt.x;
934 int width = pNext->member.run.nWidth;
935
936 if (x >= run_x && x < run_x+width)
937 {
938 cursor->nOffset = ME_CharFromPoint(editor, x-run_x, &pNext->member.run, TRUE, TRUE);
939 cursor->pRun = pNext;
940 cursor->pPara = ME_GetParagraph( cursor->pRun );
941 return exact;
942 }
943 pLastRun = pNext;
944 pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
945 } while(pNext && pNext->type == diRun);
946
947 if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
948 {
949 cursor->pRun = ME_FindItemFwd(pNext, diRun);
950 if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
951 }
952 else
953 cursor->pRun = pLastRun;
954
955 cursor->pPara = ME_GetParagraph( cursor->pRun );
956 return FALSE;
957 }
958
959 /* Finds the run and offset from the pixel position.
960 *
961 * x & y are pixel positions in virtual coordinates into the rich edit control,
962 * so client coordinates must first be adjusted by the scroll position.
963 *
964 * returns TRUE if the result was exactly under the cursor, otherwise returns
965 * FALSE, and result is set to the closest position to the coordinates.
966 */
967 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
968 ME_Cursor *result, BOOL *is_eol)
969 {
970 ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
971 BOOL isExact = TRUE;
972
973 x -= editor->rcFormat.left;
974 y -= editor->rcFormat.top;
975
976 if (is_eol)
977 *is_eol = FALSE;
978
979 /* find paragraph */
980 for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
981 {
982 assert(p->type == diParagraph);
983 if (y < p->member.para.pt.y + p->member.para.nHeight)
984 {
985 if (p->member.para.nFlags & MEPF_ROWSTART)
986 p = ME_FindPixelPosInTableRow(x, y, p);
987 y -= p->member.para.pt.y;
988 p = ME_FindItemFwd(p, diStartRow);
989 break;
990 } else if (p->member.para.nFlags & MEPF_ROWSTART) {
991 p = ME_GetTableRowEnd(p);
992 }
993 }
994 /* find row */
995 for (; p != editor->pBuffer->pLast; )
996 {
997 ME_DisplayItem *pp;
998 assert(p->type == diStartRow);
999 if (y < p->member.row.pt.y + p->member.row.nHeight) break;
1000 pp = ME_FindItemFwd(p, diStartRow);
1001 if (!pp) break;
1002 p = pp;
1003 }
1004 if (p == editor->pBuffer->pLast)
1005 {
1006 /* The position is below the last paragraph, so the last row will be used
1007 * rather than the end of the text, so the x position will be used to
1008 * determine the offset closest to the pixel position. */
1009 isExact = FALSE;
1010 p = ME_FindItemBack(p, diStartRow);
1011 if (!p) p = editor->pBuffer->pLast;
1012 }
1013
1014 assert( p->type == diStartRow || p == editor->pBuffer->pLast );
1015
1016 if( p->type == diStartRow )
1017 return ME_FindRunInRow( editor, p, x, result, is_eol ) && isExact;
1018
1019 result->pRun = ME_FindItemBack(p, diRun);
1020 result->pPara = ME_GetParagraph(result->pRun);
1021 result->nOffset = 0;
1022 assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
1023 return FALSE;
1024 }
1025
1026
1027 /* Sets the cursor to the position closest to the pixel position
1028 *
1029 * x & y are pixel positions in client coordinates.
1030 *
1031 * isExact will be set to TRUE if the run is directly under the pixel
1032 * position, FALSE if it not, unless isExact is set to NULL.
1033 *
1034 * return FALSE if outside client area and the cursor is not set,
1035 * otherwise TRUE is returned.
1036 */
1037 BOOL ME_CharFromPos(ME_TextEditor *editor, int x, int y,
1038 ME_Cursor *cursor, BOOL *isExact)
1039 {
1040 RECT rc;
1041 BOOL bResult;
1042
1043 ITextHost_TxGetClientRect(editor->texthost, &rc);
1044 if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
1045 if (isExact) *isExact = FALSE;
1046 return FALSE;
1047 }
1048 x += editor->horz_si.nPos;
1049 y += editor->vert_si.nPos;
1050 bResult = ME_FindPixelPos(editor, x, y, cursor, NULL);
1051 if (isExact) *isExact = bResult;
1052 return TRUE;
1053 }
1054
1055
1056
1057 /* Extends the selection with a word, line, or paragraph selection type.
1058 *
1059 * The selection is anchored by editor->pCursors[2-3] such that the text
1060 * between the anchors will remain selected, and one end will be extended.
1061 *
1062 * editor->pCursors[0] should have the position to extend the selection to
1063 * before this function is called.
1064 *
1065 * Nothing will be done if editor->nSelectionType equals stPosition.
1066 */
1067 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1068 {
1069 ME_Cursor tmp_cursor;
1070 int curOfs, anchorStartOfs, anchorEndOfs;
1071 if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1072 return;
1073 curOfs = ME_GetCursorOfs(&editor->pCursors[0]);
1074 anchorStartOfs = ME_GetCursorOfs(&editor->pCursors[3]);
1075 anchorEndOfs = ME_GetCursorOfs(&editor->pCursors[2]);
1076
1077 tmp_cursor = editor->pCursors[0];
1078 editor->pCursors[0] = editor->pCursors[2];
1079 editor->pCursors[1] = editor->pCursors[3];
1080 if (curOfs < anchorStartOfs)
1081 {
1082 /* Extend the left side of selection */
1083 editor->pCursors[1] = tmp_cursor;
1084 if (editor->nSelectionType == stWord)
1085 ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1086 else
1087 {
1088 ME_DisplayItem *pItem;
1089 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1090 diStartRowOrParagraph:diParagraph);
1091 pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
1092 editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
1093 editor->pCursors[1].pPara = ME_GetParagraph(editor->pCursors[1].pRun);
1094 editor->pCursors[1].nOffset = 0;
1095 }
1096 }
1097 else if (curOfs >= anchorEndOfs)
1098 {
1099 /* Extend the right side of selection */
1100 editor->pCursors[0] = tmp_cursor;
1101 if (editor->nSelectionType == stWord)
1102 ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
1103 else
1104 {
1105 ME_DisplayItem *pItem;
1106 ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1107 diStartRowOrParagraphOrEnd:diParagraphOrEnd);
1108 pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
1109 if (pItem->type == diTextEnd)
1110 editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
1111 else
1112 editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
1113 editor->pCursors[0].pPara = ME_GetParagraph(editor->pCursors[0].pRun);
1114 editor->pCursors[0].nOffset = 0;
1115 }
1116 }
1117 }
1118
1119 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1120 {
1121 ME_Cursor tmp_cursor;
1122 BOOL is_selection = FALSE, is_shift;
1123
1124 editor->nUDArrowX = -1;
1125
1126 x += editor->horz_si.nPos;
1127 y += editor->vert_si.nPos;
1128
1129 tmp_cursor = editor->pCursors[0];
1130 is_selection = ME_IsSelection(editor);
1131 is_shift = GetKeyState(VK_SHIFT) < 0;
1132
1133 ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
1134
1135 if (x >= editor->rcFormat.left || is_shift)
1136 {
1137 if (clickNum > 1)
1138 {
1139 editor->pCursors[1] = editor->pCursors[0];
1140 if (is_shift) {
1141 if (x >= editor->rcFormat.left)
1142 ME_SelectByType(editor, stWord);
1143 else
1144 ME_SelectByType(editor, stParagraph);
1145 } else if (clickNum % 2 == 0) {
1146 ME_SelectByType(editor, stWord);
1147 } else {
1148 ME_SelectByType(editor, stParagraph);
1149 }
1150 }
1151 else if (!is_shift)
1152 {
1153 editor->nSelectionType = stPosition;
1154 editor->pCursors[1] = editor->pCursors[0];
1155 }
1156 else if (!is_selection)
1157 {
1158 editor->nSelectionType = stPosition;
1159 editor->pCursors[1] = tmp_cursor;
1160 }
1161 else if (editor->nSelectionType != stPosition)
1162 {
1163 ME_ExtendAnchorSelection(editor);
1164 }
1165 }
1166 else
1167 {
1168 if (clickNum < 2) {
1169 ME_SelectByType(editor, stLine);
1170 } else if (clickNum % 2 == 0 || is_shift) {
1171 ME_SelectByType(editor, stParagraph);
1172 } else {
1173 ME_SelectByType(editor, stDocument);
1174 }
1175 }
1176 ME_InvalidateSelection(editor);
1177 ITextHost_TxShowCaret(editor->texthost, FALSE);
1178 ME_ShowCaret(editor);
1179 ME_SendSelChange(editor);
1180 }
1181
1182 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1183 {
1184 ME_Cursor tmp_cursor;
1185
1186 if (editor->nSelectionType == stDocument)
1187 return;
1188 x += editor->horz_si.nPos;
1189 y += editor->vert_si.nPos;
1190
1191 tmp_cursor = editor->pCursors[0];
1192 /* FIXME: do something with the return value of ME_FindPixelPos */
1193 ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
1194
1195 ME_InvalidateSelection(editor);
1196 editor->pCursors[0] = tmp_cursor;
1197 ME_ExtendAnchorSelection(editor);
1198
1199 if (editor->nSelectionType != stPosition &&
1200 memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1201 {
1202 /* The scroll the cursor towards the other end, since it was the one
1203 * extended by ME_ExtendAnchorSelection */
1204 ME_EnsureVisible(editor, &editor->pCursors[1]);
1205 } else {
1206 ME_EnsureVisible(editor, &editor->pCursors[0]);
1207 }
1208
1209 ME_InvalidateSelection(editor);
1210 ITextHost_TxShowCaret(editor->texthost, FALSE);
1211 ME_ShowCaret(editor);
1212 ME_SendSelChange(editor);
1213 }
1214
1215 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1216 {
1217 ME_DisplayItem *pRun = pCursor->pRun;
1218 int x;
1219
1220 if (editor->nUDArrowX != -1)
1221 x = editor->nUDArrowX;
1222 else {
1223 if (editor->bCaretAtEnd)
1224 {
1225 pRun = ME_FindItemBack(pRun, diRun);
1226 assert(pRun);
1227 x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1228 }
1229 else {
1230 x = pRun->member.run.pt.x;
1231 x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset, TRUE);
1232 }
1233 editor->nUDArrowX = x;
1234 }
1235 return x;
1236 }
1237
1238
1239 static void
1240 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1241 {
1242 ME_DisplayItem *pRun = pCursor->pRun;
1243 ME_DisplayItem *pOldPara = pCursor->pPara;
1244 ME_DisplayItem *pItem, *pNewPara;
1245 int x = ME_GetXForArrow(editor, pCursor);
1246
1247 if (editor->bCaretAtEnd && !pCursor->nOffset)
1248 if (!ME_PrevRun(&pOldPara, &pRun, TRUE))
1249 return;
1250
1251 if (nRelOfs == -1)
1252 {
1253 /* start of this row */
1254 pItem = ME_FindItemBack(pRun, diStartRow);
1255 assert(pItem);
1256 /* start of the previous row */
1257 pItem = ME_FindItemBack(pItem, diStartRow);
1258 if (!pItem)
1259 return; /* row not found - ignore */
1260 pNewPara = ME_GetParagraph(pItem);
1261 if (pOldPara->member.para.nFlags & MEPF_ROWEND ||
1262 (pOldPara->member.para.pCell &&
1263 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1264 {
1265 /* Brought out of a cell */
1266 pNewPara = ME_GetTableRowStart(pOldPara)->member.para.prev_para;
1267 if (pNewPara->type == diTextStart)
1268 return; /* At the top, so don't go anywhere. */
1269 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1270 }
1271 if (pNewPara->member.para.nFlags & MEPF_ROWEND)
1272 {
1273 /* Brought into a table row */
1274 ME_Cell *cell = &ME_FindItemBack(pNewPara, diCell)->member.cell;
1275 while (x < cell->pt.x && cell->prev_cell)
1276 cell = &cell->prev_cell->member.cell;
1277 if (cell->next_cell) /* else - we are still at the end of the row */
1278 pItem = ME_FindItemBack(cell->next_cell, diStartRow);
1279 }
1280 }
1281 else
1282 {
1283 /* start of the next row */
1284 pItem = ME_FindItemFwd(pRun, diStartRow);
1285 if (!pItem)
1286 return; /* row not found - ignore */
1287 pNewPara = ME_GetParagraph(pItem);
1288 if (pOldPara->member.para.nFlags & MEPF_ROWSTART ||
1289 (pOldPara->member.para.pCell &&
1290 pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1291 {
1292 /* Brought out of a cell */
1293 pNewPara = ME_GetTableRowEnd(pOldPara)->member.para.next_para;
1294 if (pNewPara->type == diTextEnd)
1295 return; /* At the bottom, so don't go anywhere. */
1296 pItem = ME_FindItemFwd(pNewPara, diStartRow);
1297 }
1298 if (pNewPara->member.para.nFlags & MEPF_ROWSTART)
1299 {
1300 /* Brought into a table row */
1301 ME_DisplayItem *cell = ME_FindItemFwd(pNewPara, diCell);
1302 while (cell->member.cell.next_cell &&
1303 x >= cell->member.cell.next_cell->member.cell.pt.x)
1304 cell = cell->member.cell.next_cell;
1305 pItem = ME_FindItemFwd(cell, diStartRow);
1306 }
1307 }
1308 if (!pItem)
1309 {
1310 /* row not found - ignore */
1311 return;
1312 }
1313 ME_FindRunInRow(editor, pItem, x, pCursor, &editor->bCaretAtEnd);
1314 assert(pCursor->pRun);
1315 assert(pCursor->pRun->type == diRun);
1316 }
1317
1318 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1319 {
1320 ME_DisplayItem *p = ME_FindItemFwd(editor->pBuffer->pFirst, diStartRow);
1321
1322 if (editor->vert_si.nPos < p->member.row.nHeight)
1323 {
1324 ME_SetCursorToStart(editor, pCursor);
1325 editor->bCaretAtEnd = FALSE;
1326 /* Native clears seems to clear this x value on page up at the top
1327 * of the text, but not on page down at the end of the text.
1328 * Doesn't make sense, but we try to be bug for bug compatible. */
1329 editor->nUDArrowX = -1;
1330 } else {
1331 ME_DisplayItem *pRun = pCursor->pRun;
1332 ME_DisplayItem *pLast;
1333 int x, y, yd, yp;
1334 int yOldScrollPos = editor->vert_si.nPos;
1335
1336 x = ME_GetXForArrow(editor, pCursor);
1337 if (!pCursor->nOffset && editor->bCaretAtEnd)
1338 pRun = ME_FindItemBack(pRun, diRun);
1339
1340 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1341 assert(p->type == diStartRow);
1342 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1343 y = yp + p->member.row.pt.y;
1344
1345 ME_ScrollUp(editor, editor->sizeWindow.cy);
1346 /* Only move the cursor by the amount scrolled. */
1347 yd = y + editor->vert_si.nPos - yOldScrollPos;
1348 pLast = p;
1349
1350 do {
1351 p = ME_FindItemBack(p, diStartRowOrParagraph);
1352 if (!p)
1353 break;
1354 if (p->type == diParagraph) { /* crossing paragraphs */
1355 if (p->member.para.prev_para == NULL)
1356 break;
1357 yp = p->member.para.prev_para->member.para.pt.y;
1358 continue;
1359 }
1360 y = yp + p->member.row.pt.y;
1361 if (y < yd)
1362 break;
1363 pLast = p;
1364 } while(1);
1365
1366 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
1367 }
1368 assert(pCursor->pRun);
1369 assert(pCursor->pRun->type == diRun);
1370 }
1371
1372 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1373 {
1374 ME_DisplayItem *pLast;
1375 int x, y;
1376
1377 /* Find y position of the last row */
1378 pLast = editor->pBuffer->pLast;
1379 y = pLast->member.para.prev_para->member.para.pt.y
1380 + ME_FindItemBack(pLast, diStartRow)->member.row.pt.y;
1381
1382 x = ME_GetXForArrow(editor, pCursor);
1383
1384 if (editor->vert_si.nPos >= y - editor->sizeWindow.cy)
1385 {
1386 ME_SetCursorToEnd(editor, pCursor);
1387 editor->bCaretAtEnd = FALSE;
1388 } else {
1389 ME_DisplayItem *pRun = pCursor->pRun;
1390 ME_DisplayItem *p;
1391 int yd, yp;
1392 int yOldScrollPos = editor->vert_si.nPos;
1393
1394 if (!pCursor->nOffset && editor->bCaretAtEnd)
1395 pRun = ME_FindItemBack(pRun, diRun);
1396
1397 p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1398 assert(p->type == diStartRow);
1399 yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1400 y = yp + p->member.row.pt.y;
1401
1402 /* For native richedit controls:
1403 * v1.0 - v3.1 can only scroll down as far as the scrollbar lets us
1404 * v4.1 can scroll past this position here. */
1405 ME_ScrollDown(editor, editor->sizeWindow.cy);
1406 /* Only move the cursor by the amount scrolled. */
1407 yd = y + editor->vert_si.nPos - yOldScrollPos;
1408 pLast = p;
1409
1410 do {
1411 p = ME_FindItemFwd(p, diStartRowOrParagraph);
1412 if (!p)
1413 break;
1414 if (p->type == diParagraph) {
1415 yp = p->member.para.pt.y;
1416 continue;
1417 }
1418 y = yp + p->member.row.pt.y;
1419 if (y >= yd)
1420 break;
1421 pLast = p;
1422 } while(1);
1423
1424 ME_FindRunInRow(editor, pLast, x, pCursor, &editor->bCaretAtEnd);
1425 }
1426 assert(pCursor->pRun);
1427 assert(pCursor->pRun->type == diRun);
1428 }
1429
1430 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1431 {
1432 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1433 if (pRow) {
1434 ME_DisplayItem *pRun;
1435 if (editor->bCaretAtEnd && !pCursor->nOffset) {
1436 pRow = ME_FindItemBack(pRow, diStartRow);
1437 if (!pRow)
1438 return;
1439 }
1440 pRun = ME_FindItemFwd(pRow, diRun);
1441 if (pRun) {
1442 pCursor->pRun = pRun;
1443 assert(pCursor->pPara == ME_GetParagraph(pRun));
1444 pCursor->nOffset = 0;
1445 }
1446 }
1447 editor->bCaretAtEnd = FALSE;
1448 }
1449
1450 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1451 {
1452 ME_SetCursorToStart(editor, pCursor);
1453 editor->bCaretAtEnd = FALSE;
1454 }
1455
1456 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1457 {
1458 ME_DisplayItem *pRow;
1459
1460 if (editor->bCaretAtEnd && !pCursor->nOffset)
1461 return;
1462
1463 pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1464 assert(pRow);
1465 if (pRow->type == diStartRow) {
1466 ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1467 assert(pRun);
1468 pCursor->pRun = pRun;
1469 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1470 pCursor->nOffset = 0;
1471 editor->bCaretAtEnd = TRUE;
1472 return;
1473 }
1474 pCursor->pRun = ME_FindItemBack(pRow, diRun);
1475 assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1476 assert(pCursor->pPara == ME_GetParagraph(pCursor->pRun));
1477 pCursor->nOffset = 0;
1478 editor->bCaretAtEnd = FALSE;
1479 }
1480
1481 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1482 {
1483 ME_SetCursorToEnd(editor, pCursor);
1484 editor->bCaretAtEnd = FALSE;
1485 }
1486
1487 BOOL ME_IsSelection(ME_TextEditor *editor)
1488 {
1489 return editor->pCursors[0].pRun != editor->pCursors[1].pRun ||
1490 editor->pCursors[0].nOffset != editor->pCursors[1].nOffset;
1491 }
1492
1493 void ME_DeleteSelection(ME_TextEditor *editor)
1494 {
1495 int from, to;
1496 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
1497 int nEndCursor = nStartCursor ^ 1;
1498 ME_DeleteTextAtCursor(editor, nStartCursor, to - from);
1499 editor->pCursors[nEndCursor] = editor->pCursors[nStartCursor];
1500 }
1501
1502 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1503 {
1504 return ME_GetInsertStyle(editor, 0);
1505 }
1506
1507 void ME_SendSelChange(ME_TextEditor *editor)
1508 {
1509 SELCHANGE sc;
1510
1511 if (!(editor->nEventMask & ENM_SELCHANGE))
1512 return;
1513
1514 sc.nmhdr.hwndFrom = NULL;
1515 sc.nmhdr.idFrom = 0;
1516 sc.nmhdr.code = EN_SELCHANGE;
1517 ME_GetSelectionOfs(editor, &sc.chrg.cpMin, &sc.chrg.cpMax);
1518 sc.seltyp = SEL_EMPTY;
1519 if (sc.chrg.cpMin != sc.chrg.cpMax)
1520 sc.seltyp |= SEL_TEXT;
1521 if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* what were RICHEDIT authors thinking ? */
1522 sc.seltyp |= SEL_MULTICHAR;
1523 TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1524 sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1525 (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1526 (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1527 if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1528 {
1529 ME_ClearTempStyle(editor);
1530
1531 editor->notified_cr = sc.chrg;
1532 ITextHost_TxNotify(editor->texthost, sc.nmhdr.code, &sc);
1533 }
1534 }
1535
1536 BOOL
1537 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1538 {
1539 int nCursor = 0;
1540 ME_Cursor *p = &editor->pCursors[nCursor];
1541 ME_Cursor tmp_curs = *p;
1542 BOOL success = FALSE;
1543
1544 ME_CheckCharOffsets(editor);
1545 switch(nVKey) {
1546 case VK_LEFT:
1547 editor->bCaretAtEnd = FALSE;
1548 if (ctrl)
1549 success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1550 else
1551 success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1552 break;
1553 case VK_RIGHT:
1554 editor->bCaretAtEnd = FALSE;
1555 if (ctrl)
1556 success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1557 else
1558 success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1559 break;
1560 case VK_UP:
1561 ME_MoveCursorLines(editor, &tmp_curs, -1);
1562 break;
1563 case VK_DOWN:
1564 ME_MoveCursorLines(editor, &tmp_curs, +1);
1565 break;
1566 case VK_PRIOR:
1567 ME_ArrowPageUp(editor, &tmp_curs);
1568 break;
1569 case VK_NEXT:
1570 ME_ArrowPageDown(editor, &tmp_curs);
1571 break;
1572 case VK_HOME: {
1573 if (ctrl)
1574 ME_ArrowCtrlHome(editor, &tmp_curs);
1575 else
1576 ME_ArrowHome(editor, &tmp_curs);
1577 editor->bCaretAtEnd = FALSE;
1578 break;
1579 }
1580 case VK_END:
1581 if (ctrl)
1582 ME_ArrowCtrlEnd(editor, &tmp_curs);
1583 else
1584 ME_ArrowEnd(editor, &tmp_curs);
1585 break;
1586 }
1587
1588 if (!extend)
1589 editor->pCursors[1] = tmp_curs;
1590 *p = tmp_curs;
1591
1592 ME_InvalidateSelection(editor);
1593 ME_Repaint(editor);
1594 ITextHost_TxShowCaret(editor->texthost, FALSE);
1595 ME_EnsureVisible(editor, &tmp_curs);
1596 ME_ShowCaret(editor);
1597 ME_SendSelChange(editor);
1598 return success;
1599 }