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