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