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