Add support for more than one sound card.
[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 void ME_SetSelection(ME_TextEditor *editor, int from, int to)
45 {
46   if (from == 0 && to == -1)
47   {
48     editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun); 
49     editor->pCursors[1].nOffset = 0; 
50     editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun); 
51     editor->pCursors[0].nOffset = 0; 
52     ME_Repaint(editor);
53     ME_ClearTempStyle(editor);
54     return;
55   }
56   if (from == -1)
57   {
58     editor->pCursors[1] = editor->pCursors[0]; 
59     ME_Repaint(editor);
60     ME_ClearTempStyle(editor);
61     return;
62   }
63   if (from>to)
64   {
65     int tmp = from;
66     from = to;
67     to = tmp;
68   }
69   ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
70   ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);  
71 }
72
73 void ME_MoveCaret(ME_TextEditor *editor)
74 {
75   HDC hDC = GetDC(editor->hWnd);
76   ME_Context c;
77
78   ME_Cursor *pCursor = &editor->pCursors[0];
79   ME_DisplayItem *pCursorRun = pCursor->pRun;
80   ME_DisplayItem *pSizeRun = pCursor->pRun;
81   
82   ME_InitContext(&c, editor, hDC);
83   assert(!pCursor->nOffset || !editor->bCaretAtEnd);
84   
85   if (pCursorRun->type == diRun) {
86     ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
87     if (row) {
88       ME_DisplayItem *run = pCursorRun;
89       ME_DisplayItem *para;
90       SIZE sz = {0, 0};
91       if (!pCursor->nOffset && !editor->bCaretAtEnd)
92       {
93         ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrStartRow);
94         if (prev->type == diRun)
95           pSizeRun = prev;
96       }
97       assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
98       para = ME_FindItemBack(row, diParagraph);
99       if (editor->bCaretAtEnd && !pCursor->nOffset && 
100           run == ME_FindItemFwd(row, diRun))
101       {
102         ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
103         if (tmp->type == diRun)
104         {
105           row = ME_FindItemBack(tmp, diStartRow);
106           pSizeRun = run = tmp;
107           sz = ME_GetRunSize(&c, &run->member.run, ME_StrLen(run->member.run.strText));
108         }
109       }
110       if (pCursor->nOffset && !(run->member.run.nFlags & MERF_SKIPPED)) {
111         sz = ME_GetRunSize(&c, &run->member.run, pCursor->nOffset);
112       }
113       CreateCaret(editor->hWnd, NULL, 0, pSizeRun->member.run.nAscent+pSizeRun->member.run.nDescent);
114       SetCaretPos(run->member.run.pt.x+sz.cx,
115         para->member.para.nYPos+row->member.row.nBaseline+pSizeRun->member.run.pt.y-pSizeRun->member.run.nAscent-GetScrollPos(editor->hWnd, SB_VERT));
116     } else {
117       assert(0 == "Wrapped paragraph run without a row?");
118       CreateCaret(editor->hWnd, NULL, 0, 10);
119       SetCaretPos(0,0);
120     }
121   }  
122   else {
123     assert(0 == "Cursor not on a run");
124     CreateCaret(editor->hWnd, NULL, 0, 10); /* FIXME use global font */
125     SetCaretPos(0,0);
126   }
127   ME_DestroyContext(&c);
128   ReleaseDC(editor->hWnd, hDC);
129 }
130
131 void ME_ShowCaret(ME_TextEditor *ed)
132 {
133   ME_MoveCaret(ed);
134   ShowCaret(ed->hWnd);
135 }
136
137 void ME_HideCaret(ME_TextEditor *ed)
138 {
139   HideCaret(ed->hWnd);
140   DestroyCaret();
141 }
142
143 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs, 
144   int nChars)
145 {
146   ME_Cursor c;
147   int shift = 0;
148   
149   while(nChars > 0)
150   {
151     ME_Run *run;
152     ME_CursorFromCharOfs(editor, nOfs, &c);
153     run = &c.pRun->member.run;
154     if (run->nFlags & MERF_ENDPARA) {
155       if (!ME_FindItemFwd(c.pRun, diParagraph))
156       {
157         return;
158       }
159       ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
160       /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
161       ME_CheckCharOffsets(editor);
162       nChars--;
163       continue;
164     }
165     else
166     {
167       ME_Cursor cursor;
168       int nIntendedChars = nChars;
169       int nCharsToDelete = nChars;
170       int i;
171       int loc = c.nOffset;
172       
173       ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
174       
175       cursor = c;
176       ME_StrRelPos(run->strText, loc, &nChars);
177       /* nChars is the number of characters that should be deleted from the
178          FOLLOWING runs (these AFTER cursor.pRun)
179          nCharsToDelete is a number of chars to delete from THIS run */
180       nCharsToDelete -= nChars;
181       shift -= nCharsToDelete;
182       TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n", 
183         nCharsToDelete, nIntendedChars, nChars, c.nOffset, 
184         debugstr_w(run->strText->szData), run->strText->nLen);
185
186       if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
187       {
188         /* undo = reinsert whole run */
189         /* nOfs is a character offset (from the start of the document
190            to the current (deleted) run */
191         ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
192         if (pUndo)
193           pUndo->di.member.run.nCharOfs = nOfs;
194       }
195       else
196       {
197         /* undo = reinsert partial run */
198         ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
199         if (pUndo) {
200           ME_DestroyString(pUndo->di.member.run.strText);
201           pUndo->di.member.run.nCharOfs = nOfs;
202           pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
203         }
204       }
205       TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
206       TRACE("Shift value: %d\n", shift);
207       ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
208       
209       /* update cursors (including c) */
210       for (i=-1; i<editor->nCursors; i++) {
211         ME_Cursor *pThisCur = editor->pCursors + i; 
212         if (i == -1) pThisCur = &c;
213         if (pThisCur->pRun == cursor.pRun) {
214           if (pThisCur->nOffset > cursor.nOffset) {
215             if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
216               pThisCur->nOffset = cursor.nOffset;
217             else
218               pThisCur->nOffset -= nCharsToDelete;
219             assert(pThisCur->nOffset >= 0);
220             assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
221           }
222           if (pThisCur->nOffset == ME_StrVLen(run->strText))
223           {
224             pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
225             assert(pThisCur->pRun->type == diRun);
226             pThisCur->nOffset = 0;
227           }
228         }
229       }
230       
231       /* c = updated data now */
232       
233       if (c.pRun == cursor.pRun)
234         ME_SkipAndPropagateCharOffset(c.pRun, shift);
235       else
236         ME_PropagateCharOffset(c.pRun, shift);
237
238       if (!ME_StrVLen(cursor.pRun->member.run.strText))
239       {
240         TRACE("Removing useless run\n");
241         ME_Remove(cursor.pRun);
242         ME_DestroyDisplayItem(cursor.pRun);
243       }
244       
245       shift = 0;
246       /*
247       ME_CheckCharOffsets(editor);
248       */
249       continue;
250     }
251   }
252 }
253
254 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, 
255   int nChars)
256 {  
257   assert(nCursor>=0 && nCursor<editor->nCursors);
258   ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
259 }
260
261 static WCHAR wszSpace[] = {' ', 0};
262
263 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
264 void ME_InsertGraphicsFromCursor(ME_TextEditor *editor, int nCursor)
265 {
266   ME_Cursor *pCursor = &editor->pCursors[nCursor];
267   ME_DisplayItem *pItem = NULL;
268   ME_DisplayItem *pNewRun = NULL;
269   ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
270   ME_UndoItem *pUndo;
271   
272   /* FIXME no no no */
273   if (ME_IsSelection(editor))
274     ME_DeleteSelection(editor);
275
276   pUndo = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
277   if (pUndo) {
278     pUndo->nStart = pCursor->nOffset + pCursor->pRun->member.run.nCharOfs + ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs;
279     pUndo->nLen = 1;
280   }
281   if (pCursor->nOffset)
282   {
283     ME_SplitRunSimple(editor, pCursor->pRun, pCursor->nOffset);
284   }
285   pItem = pCursor->pRun;
286   pNewRun = ME_MakeRun(pStyle, ME_MakeStringN(wszSpace, 1), MERF_GRAPHICS);
287   pNewRun->member.run.nCharOfs = pCursor->pRun->member.run.nCharOfs;
288   ME_InsertBefore(pCursor->pRun, pNewRun);
289   ME_PropagateCharOffset(pItem, 1);
290   ME_CheckCharOffsets(editor);
291   ME_SendSelChange(editor);
292 }
293
294 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor, 
295   const WCHAR *str, int len, ME_Style *style)
296 {
297   const WCHAR *pos;
298   ME_Cursor *p = NULL;
299
300   assert(style);
301   editor->bCaretAtEnd = FALSE;
302
303   ME_AddRefStyle(style);
304   
305   /* FIXME really HERE ? */
306   if (ME_IsSelection(editor))
307     ME_DeleteSelection(editor);
308
309   assert(nCursor>=0 && nCursor<editor->nCursors);
310   if (len == -1)
311     len = lstrlenW(str);
312   pos = str;
313   /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
314   while(pos-str < len && *pos != '\r' && *pos != '\n')
315     pos++;
316   /* handle EOLs */
317   if (pos-str < len) {   
318     ME_DisplayItem *tp, *end_run;
319     ME_Paragraph *para;
320     ME_Style *tmp_style;
321     if (pos!=str)
322       ME_InsertTextFromCursor(editor, nCursor, str, pos-str, style);
323     p = &editor->pCursors[nCursor];
324     tp = ME_FindItemBack(p->pRun, diParagraph);
325     para = &tp->member.para;
326     assert(tp);
327     if (p->nOffset) {
328       ME_SplitRunSimple(editor, p->pRun, p->nOffset);
329       p = &editor->pCursors[nCursor];
330     }
331     tmp_style = ME_GetInsertStyle(editor, nCursor);
332     /* ME_SplitParagraph increases style refcount */
333     tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
334     p->pRun = ME_FindItemFwd(tp, diRun);
335     end_run = ME_FindItemBack(tp, diRun);
336     ME_ReleaseStyle(end_run->member.run.style);
337     end_run->member.run.style = tmp_style;
338     p->nOffset = 0;
339     if(pos-str < len && *pos =='\r')
340       pos++;
341     if(pos-str < len && *pos =='\n')
342       pos++;
343     if(pos-str < len) {
344       ME_InsertTextFromCursor(editor, nCursor, pos, len-(pos-str), style);
345     }
346     ME_ReleaseStyle(style);
347     return;
348   }
349   p = &editor->pCursors[nCursor];
350   if (style) {
351     ME_DisplayItem *pNewRun = NULL;
352
353     assert(p->pRun->type == diRun);
354     pNewRun = ME_MakeRun(style, ME_MakeStringN(str, len), 0); /* addrefs style */
355     ME_InsertRun(editor, ME_CharOfsFromRunOfs(editor, p->pRun, p->nOffset), pNewRun);
356     ME_DestroyDisplayItem(pNewRun);
357     ME_ReleaseStyle(style);
358     return;
359   } else {
360     assert(0);
361   }
362 }
363
364 BOOL ME_ArrowLeft(ME_TextEditor *editor, ME_Cursor *p)
365 {
366   if (p->nOffset) {
367     p->nOffset = ME_StrRelPos2(p->pRun->member.run.strText, p->nOffset, -1);
368     return TRUE;
369   }
370   else
371   {
372     ME_DisplayItem *pRun = ME_FindItemBack(p->pRun, diRunOrParagraph);
373     assert(pRun);
374     if (pRun->type == diRun) {
375       p->pRun = pRun;
376       assert(p->pRun->type == diRun);
377       assert(pRun->member.run.strText->nLen);
378       p->nOffset = pRun->member.run.strText->nLen;
379       if (p->nOffset) {
380         p->nOffset = ME_StrRelPos2(pRun->member.run.strText, p->nOffset, -1);
381         return TRUE;
382       }
383       else
384         assert(0);
385     }
386     if (pRun->type == diParagraph)
387     {
388       if (pRun->member.para.prev_para->type == diTextStart)
389         return FALSE;
390       assert(pRun->member.para.prev_para->type == diParagraph);
391       pRun = ME_FindItemBack(pRun, diRunOrParagraph);
392       /* every paragraph ought to have at least one run */
393       assert(pRun && pRun->type == diRun);
394       assert(pRun->member.run.nFlags & MERF_ENDPARA);
395       p->pRun = pRun;
396       p->nOffset = 0;
397       return TRUE;
398     }
399     assert(0);
400   }
401   return FALSE;
402 }
403
404 BOOL ME_ArrowRight(ME_TextEditor *editor, ME_Cursor *p)
405 {
406   int new_ofs = ME_StrRelPos2(p->pRun->member.run.strText, p->nOffset, 1);
407   if (new_ofs<p->pRun->member.run.strText->nLen) {
408     p->nOffset = new_ofs;
409   }
410   else
411   {
412     ME_DisplayItem *pRun = ME_FindItemFwd(p->pRun, diRun);
413     if (pRun) {
414       p->pRun = pRun;
415       assert(p->pRun->type == diRun);
416       p->nOffset = 0;
417     }
418   }
419   return TRUE;
420 }
421
422 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
423 {
424   ME_Cursor *pCursor = &editor->pCursors[nCursor];
425   
426   return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
427     + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
428 }
429
430 int ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
431 {
432   ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
433   int rx = 0;
434   
435   if (is_eol)
436     *is_eol = 0;
437
438   while(p != editor->pBuffer->pLast)
439   {
440     if (p->type == diParagraph)
441     {
442       int ry = y - p->member.para.nYPos;
443       if (ry < 0)
444       {
445         result->pRun = ME_FindItemFwd(p, diRun);
446         result->nOffset = 0;
447         return 0;
448       }
449       if (ry >= p->member.para.nHeight)
450       {
451         p = p->member.para.next_para;
452         continue;
453       }
454       p = ME_FindItemFwd(p, diStartRow);
455       y = ry;
456       continue;
457     }
458     if (p->type == diStartRow)
459     {
460       int ry = y - p->member.row.nYPos;
461       if (ry < 0)
462         return 0;
463       if (ry >= p->member.row.nHeight)
464       {
465         p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
466         if (p->type != diStartRow)
467           return 0;
468         continue;
469       }
470       p = ME_FindItemFwd(p, diRun);
471       continue;
472     }
473     if (p->type == diRun)
474     {
475       ME_DisplayItem *pp;
476       rx = x - p->member.run.pt.x;
477       if (rx < 0)
478         rx = 0;
479       if (rx >= p->member.run.nWidth) /* not this run yet... find next item */
480       {
481         pp = p;
482         do {
483           p = p->next;
484           if (p->type == diRun)
485           {
486             rx = x - p->member.run.pt.x;
487             goto continue_search;
488           }
489           if (p->type == diStartRow)
490           {
491             p = ME_FindItemFwd(p, diRun);
492             if (is_eol)
493               *is_eol = 1;
494             rx = 0; /* FIXME not sure */
495             goto found_here;
496           }
497           if (p->type == diParagraph || p->type == diTextEnd)
498           {
499             rx = 0; /* FIXME not sure */
500             p = pp;
501             goto found_here;
502           }
503         } while(1);
504         continue;
505       }
506     found_here:
507       if (p->member.run.nFlags & MERF_ENDPARA)
508         rx = 0;
509       result->pRun = p;
510       result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
511       if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
512       {
513         result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
514         result->nOffset = 0;
515       }
516       return 1;
517     }
518     assert(0);
519   continue_search:
520     ;
521   }
522   result->pRun = ME_FindItemBack(p, diRun);
523   result->nOffset = 0;
524   assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
525   return 0;
526 }
527
528 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
529 {
530   ME_Cursor tmp_cursor;
531   int is_selection = 0;
532   
533   editor->nUDArrowX = -1;
534   
535   y += GetScrollPos(editor->hWnd, SB_VERT);  
536
537   tmp_cursor = editor->pCursors[0];
538   is_selection = ME_IsSelection(editor);
539
540   ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
541   
542   if (GetKeyState(VK_SHIFT)>=0)
543   {
544     editor->pCursors[1] = editor->pCursors[0];
545   }
546   else
547   {
548     if (!is_selection) {
549       editor->pCursors[1] = tmp_cursor;
550       is_selection = 1;
551     }
552   }
553   HideCaret(editor->hWnd);
554   ME_MoveCaret(editor);
555   if (is_selection)
556     ME_Repaint(editor);
557   ShowCaret(editor->hWnd);
558   ME_ClearTempStyle(editor);
559   ME_SendSelChange(editor);
560 }
561
562 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
563 {
564   ME_Cursor tmp_cursor;
565   
566   y += GetScrollPos(editor->hWnd, SB_VERT);  
567
568   tmp_cursor = editor->pCursors[0];
569   if (!ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd))
570     /* return */;
571   
572   if (tmp_cursor.pRun == editor->pCursors[0].pRun && 
573       tmp_cursor.nOffset == editor->pCursors[0].nOffset)
574     return;
575   
576   HideCaret(editor->hWnd);
577   ME_MoveCaret(editor);
578   ME_Repaint(editor);
579   ShowCaret(editor->hWnd);
580   ME_SendSelChange(editor);
581 }
582
583 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow, 
584                                 int x, int *pOffset, int *pbCaretAtEnd)
585 {
586   ME_DisplayItem *pNext, *pLastRun;
587   pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
588   assert(pNext->type == diRun);
589   pLastRun = pNext;
590   *pbCaretAtEnd = FALSE;
591   do {
592     int run_x = pNext->member.run.pt.x;
593     int width = pNext->member.run.nWidth;
594     if (x < run_x)
595     {
596       if (pOffset) *pOffset = 0;
597       return pNext;
598     }
599     if (x >= run_x && x < run_x+width)
600     {
601       int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
602       ME_String *s = pNext->member.run.strText;
603       if (ch < s->nLen) {
604         if (pOffset)
605           *pOffset = ch;
606         return pNext;          
607       }
608     }
609     pLastRun = pNext;
610     pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
611   } while(pNext && pNext->type == diRun);
612   
613   if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
614   {
615     pNext = ME_FindItemFwd(pNext, diRun);
616     if (pbCaretAtEnd) *pbCaretAtEnd = 1;
617     if (pOffset) *pOffset = 0;
618     return pNext;
619   } else {
620     if (pbCaretAtEnd) *pbCaretAtEnd = 0;
621     if (pOffset) *pOffset = 0;
622     return pLastRun;
623   }
624 }
625
626 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
627 {
628   ME_DisplayItem *pRun = pCursor->pRun;
629   int x;
630
631   if (editor->nUDArrowX != -1)
632     x = editor->nUDArrowX;
633   else {
634     if (editor->bCaretAtEnd)
635     {
636       pRun = ME_FindItemBack(pRun, diRun);
637       assert(pRun);
638       x = pRun->member.run.pt.x + pRun->member.run.nWidth;
639     }
640     else {
641       x = pRun->member.run.pt.x;
642       x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
643     }
644     editor->nUDArrowX = x;
645   }
646   return x;
647 }
648
649 void ME_ArrowUp(ME_TextEditor *editor, ME_Cursor *pCursor)
650 {
651   ME_DisplayItem *pRun = pCursor->pRun;
652   ME_DisplayItem *pItem, *pItem2;
653   int x = ME_GetXForArrow(editor, pCursor);
654   
655   if (editor->bCaretAtEnd && !pCursor->nOffset)
656   {
657     pRun = ME_FindItemBack(pRun, diRun);
658     if (!pRun)
659       return;
660   }
661   
662   /* start of this row */
663   pItem = ME_FindItemBack(pRun, diStartRow);
664   assert(pItem);
665   /* start of the previous row */
666   pItem2 = ME_FindItemBack(pItem, diStartRow);
667   /* no previous row = the first line of the first paragraph */
668   if (!pItem2) /* can't go up - don't go BOL (as in MS richedit) */
669     return;
670   /* FIXME
671   ME_WrapTextParagraph(editor, ME_FindItemBack(pItem2, diParagraph));
672   */
673   pCursor->pRun = ME_FindRunInRow(editor, pItem2, x, &pCursor->nOffset, &editor->bCaretAtEnd);
674 }
675
676 void ME_ArrowDown(ME_TextEditor *editor, ME_Cursor *pCursor)
677 {
678   ME_DisplayItem *pRun = pCursor->pRun;
679   ME_DisplayItem *pItem;
680   int x = ME_GetXForArrow(editor, pCursor);
681   if (!pCursor->nOffset && editor->bCaretAtEnd)
682   {
683     pRun = ME_FindItemBack(pRun, diRun);
684 /*    x = pRun->member.run.pt.x + pRun->member.run.nWidth; */
685   }
686   /* start of the next row */
687   pItem = ME_FindItemFwd(pRun, diStartRow);
688   /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
689   */
690   if (!pItem)
691   {
692     /* next row not found - ignore */
693     return;
694   }
695   pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
696   assert(pCursor->pRun);
697   assert(pCursor->pRun->type == diRun);
698 }
699
700 void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
701 {
702   ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
703   if (pRow) {
704     ME_DisplayItem *pRun;
705     if (editor->bCaretAtEnd && !pCursor->nOffset) {
706       pRow = ME_FindItemBack(pRow, diStartRow);
707       if (!pRow)
708         return;
709     }
710     pRun = ME_FindItemFwd(pRow, diRun);
711     if (pRun) {
712       pCursor->pRun = pRun;
713       pCursor->nOffset = 0;
714     }
715   }
716   editor->bCaretAtEnd = FALSE;
717 }
718
719 void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
720 {
721   ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
722   if (pRow) {
723     ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
724     if (pRun) {
725       pCursor->pRun = pRun;
726       pCursor->nOffset = 0;
727     }
728   }
729 }
730
731 void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
732 {
733   ME_DisplayItem *pRow;
734   
735   if (editor->bCaretAtEnd && !pCursor->nOffset)
736     return;
737   
738   pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
739   assert(pRow);
740   if (pRow->type == diStartRow) {
741     /* FIXME WTF was I thinking about here ? */
742     ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
743     assert(pRun);
744     pCursor->pRun = pRun;
745     pCursor->nOffset = 0;
746     editor->bCaretAtEnd = 1;
747     return;
748   }
749   pCursor->pRun = ME_FindItemBack(pRow, diRun);
750   assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
751   pCursor->nOffset = 0;
752   editor->bCaretAtEnd = FALSE;
753 }
754       
755 void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
756 {
757   ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
758   assert(p);
759   p = ME_FindItemBack(p, diRun);
760   assert(p);
761   assert(p->member.run.nFlags & MERF_ENDPARA);
762   pCursor->pRun = p;
763   pCursor->nOffset = 0;
764   editor->bCaretAtEnd = FALSE;
765 }
766
767 BOOL ME_IsSelection(ME_TextEditor *editor)
768 {
769   return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
770 }
771
772 int ME_GetSelCursor(ME_TextEditor *editor, int dir)
773 {
774   int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
775   
776   if (cdir*dir>0)
777     return 0;
778   else
779     return 1;
780 }
781       
782 BOOL ME_CancelSelection(ME_TextEditor *editor, int dir)
783 {
784   int cdir;
785   
786   if (GetKeyState(VK_SHIFT)<0)
787     return FALSE;
788   if (!memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor)))
789     return FALSE;
790   
791   cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
792   
793   if (cdir*dir>0)
794     editor->pCursors[1] = editor->pCursors[0];
795   else
796     editor->pCursors[0] = editor->pCursors[1];
797   ME_Repaint(editor);
798   return TRUE;
799 }
800
801 void ME_RepaintSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
802 {
803   ME_Cursor old_anchor = editor->pCursors[1];
804   
805   if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
806   {
807     /* any selection was present ? if so, it's no more, repaint ! */
808     editor->pCursors[1] = editor->pCursors[0];
809     if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
810       ME_Repaint(editor);
811       return;
812     }
813     return;
814   }
815   else
816   {
817     if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
818     {
819       editor->pCursors[1] = *pTempCursor;
820     }
821   }
822
823   ME_Repaint(editor);
824 }
825
826 void ME_DeleteSelection(ME_TextEditor *editor)
827 {
828   int from, to;
829   ME_GetSelection(editor, &from, &to);
830   ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
831 }
832
833 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
834 {
835   ME_Style *style;
836   int from, to;
837   ME_Cursor c;
838   
839   ME_GetSelection(editor, &from, &to);
840   ME_CursorFromCharOfs(editor, from, &c);
841   if (from != to) {
842     style = c.pRun->member.run.style;
843     ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
844   }
845   else
846     style = ME_GetInsertStyle(editor, 0);
847   return style;
848 }
849
850 void ME_SendSelChange(ME_TextEditor *editor)
851 {
852   SELCHANGE sc;
853   if (!(editor->nEventMask & ENM_SELCHANGE))
854     return;
855   sc.nmhdr.hwndFrom = editor->hWnd;
856   sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
857   sc.nmhdr.code = EN_SELCHANGE;
858   SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
859   sc.seltyp = SEL_EMPTY;
860   if (sc.chrg.cpMin != sc.chrg.cpMax)
861     sc.seltyp |= SEL_TEXT;
862   if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
863     sc.seltyp |= SEL_MULTICHAR;
864   SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
865 }
866
867 BOOL ME_ArrowKey(ME_TextEditor *editor, int nVKey, int nCtrl)
868 {
869   int nCursor = 0;
870   ME_Cursor *p = &editor->pCursors[nCursor];
871   ME_Cursor tmp_curs = *p;
872   
873   switch(nVKey) {
874     case VK_UP:
875       ME_ArrowUp(editor, p);
876       ME_ClearTempStyle(editor);
877       ME_RepaintSelection(editor, &tmp_curs);
878       ME_SendSelChange(editor);
879       return TRUE;
880     case VK_DOWN:
881       ME_ArrowDown(editor, p);
882       ME_ClearTempStyle(editor);
883       ME_RepaintSelection(editor, &tmp_curs);
884       ME_SendSelChange(editor);
885       return TRUE;
886   }
887   
888   editor->nUDArrowX = -1;
889   switch(nVKey) {
890     case VK_BACK: { /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
891       if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
892         return FALSE;
893       if (ME_IsSelection(editor))
894       {
895         editor->bCaretAtEnd = FALSE; /* FIXME or maybe not */
896         ME_DeleteSelection(editor);
897         ME_UpdateRepaint(editor);
898         return TRUE;
899       }
900       if (ME_ArrowLeft(editor, p)) {
901         editor->bCaretAtEnd = FALSE; /* FIXME or maybe not */
902         ME_ClearTempStyle(editor);
903         ME_MoveCaret(editor);
904         ME_DeleteTextAtCursor(editor, nCursor, 1);
905         ME_UpdateRepaint(editor);
906       }
907       return TRUE;
908     }
909     case VK_DELETE: {
910       if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
911         return FALSE;
912       /* editor->bCaretAtEnd = 0; FIXME or maybe not */
913       if (ME_IsSelection(editor))
914       {
915         ME_DeleteSelection(editor);
916         ME_ClearTempStyle(editor);
917         ME_UpdateRepaint(editor);
918         return TRUE;
919       }
920       ME_DeleteTextAtCursor(editor, nCursor, 1);
921       ME_ClearTempStyle(editor);
922       ME_UpdateRepaint(editor);
923       return TRUE;
924     }
925     case VK_HOME: {
926       if (GetKeyState(VK_CONTROL)<0)
927         ME_ArrowCtrlHome(editor, p);
928       else
929         ME_ArrowHome(editor, p);
930       editor->bCaretAtEnd = 0;
931       ME_ClearTempStyle(editor);
932       ME_RepaintSelection(editor, &tmp_curs);
933       ME_SendSelChange(editor);
934       return TRUE;
935     }
936     case VK_END: 
937       if (GetKeyState(VK_CONTROL)<0)
938         ME_ArrowCtrlEnd(editor, p);
939       else
940         ME_ArrowEnd(editor, p);
941       ME_ClearTempStyle(editor);
942       ME_RepaintSelection(editor, &tmp_curs);
943       ME_SendSelChange(editor);
944       return TRUE;
945     case VK_LEFT:
946       editor->bCaretAtEnd = 0;
947       if (ME_CancelSelection(editor, -1))
948         return TRUE;
949       ME_ArrowLeft(editor, p);
950       ME_RepaintSelection(editor, &tmp_curs);
951       ME_ClearTempStyle(editor);
952       ME_SendSelChange(editor);
953       return TRUE;
954     case VK_RIGHT:
955       editor->bCaretAtEnd = 0;
956       if (ME_CancelSelection(editor, +1))
957         return TRUE;
958       ME_ArrowRight(editor, p);
959       ME_RepaintSelection(editor, &tmp_curs);
960       ME_ClearTempStyle(editor);
961       ME_SendSelChange(editor);
962       return TRUE;
963   }
964   return FALSE;
965 }