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