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