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