Fixes for -Wmissing-declaration and -Wwrite-string warnings.
[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, &para->member.para, &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, &para->member.para, &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-ME_GetYScrollPos(editor));
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' && *pos != '\t')
315     pos++;
316   if (pos-str < len && *pos == '\t') { /* handle tabs */
317     ME_DisplayItem *pNewRun = NULL;
318     WCHAR tab = '\t';
319
320     if (pos!=str)
321       ME_InsertTextFromCursor(editor, nCursor, str, pos-str, style);
322     
323     p = &editor->pCursors[nCursor];
324     assert(style);
325     assert(p->pRun->type == diRun);
326     pNewRun = ME_MakeRun(style, ME_MakeStringN(&tab, 1), MERF_TAB); /* addrefs style */
327     ME_InsertRun(editor, ME_CharOfsFromRunOfs(editor, p->pRun, p->nOffset), pNewRun);
328     ME_DestroyDisplayItem(pNewRun);
329     ME_ReleaseStyle(style);
330
331     pos++;
332     if(pos-str < len) {
333       ME_InsertTextFromCursor(editor, nCursor, pos, len-(pos-str), style);
334     }
335     return;
336   }
337   if (pos-str < len) {   /* handle EOLs */
338     ME_DisplayItem *tp, *end_run;
339     ME_Paragraph *para;
340     ME_Style *tmp_style;
341     if (pos!=str)
342       ME_InsertTextFromCursor(editor, nCursor, str, pos-str, style);
343     p = &editor->pCursors[nCursor];
344     tp = ME_FindItemBack(p->pRun, diParagraph);
345     para = &tp->member.para;
346     assert(tp);
347     if (p->nOffset) {
348       ME_SplitRunSimple(editor, p->pRun, p->nOffset);
349       p = &editor->pCursors[nCursor];
350     }
351     tmp_style = ME_GetInsertStyle(editor, nCursor);
352     /* ME_SplitParagraph increases style refcount */
353     tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
354     p->pRun = ME_FindItemFwd(tp, diRun);
355     end_run = ME_FindItemBack(tp, diRun);
356     ME_ReleaseStyle(end_run->member.run.style);
357     end_run->member.run.style = tmp_style;
358     p->nOffset = 0;
359     if(pos-str < len && *pos =='\r')
360       pos++;
361     if(pos-str < len && *pos =='\n')
362       pos++;
363     if(pos-str < len) {
364       ME_InsertTextFromCursor(editor, nCursor, pos, len-(pos-str), style);
365     }
366     ME_ReleaseStyle(style);
367     return;
368   }
369   p = &editor->pCursors[nCursor];
370   if (style) {
371     ME_DisplayItem *pNewRun = NULL;
372
373     assert(p->pRun->type == diRun);
374     pNewRun = ME_MakeRun(style, ME_MakeStringN(str, len), 0); /* addrefs style */
375     ME_InsertRun(editor, ME_CharOfsFromRunOfs(editor, p->pRun, p->nOffset), pNewRun);
376     ME_DestroyDisplayItem(pNewRun);
377     ME_ReleaseStyle(style);
378     return;
379   } else {
380     assert(0);
381   }
382 }
383
384 BOOL ME_ArrowLeft(ME_TextEditor *editor, ME_Cursor *p)
385 {
386   if (p->nOffset) {
387     p->nOffset = ME_StrRelPos2(p->pRun->member.run.strText, p->nOffset, -1);
388     return TRUE;
389   }
390   else
391   {
392     ME_DisplayItem *pRun = ME_FindItemBack(p->pRun, diRunOrParagraph);
393     assert(pRun);
394     if (pRun->type == diRun) {
395       p->pRun = pRun;
396       assert(p->pRun->type == diRun);
397       assert(pRun->member.run.strText->nLen);
398       p->nOffset = pRun->member.run.strText->nLen;
399       if (p->nOffset) {
400         p->nOffset = ME_StrRelPos2(pRun->member.run.strText, p->nOffset, -1);
401         return TRUE;
402       }
403       else
404         assert(0);
405     }
406     if (pRun->type == diParagraph)
407     {
408       if (pRun->member.para.prev_para->type == diTextStart)
409         return FALSE;
410       assert(pRun->member.para.prev_para->type == diParagraph);
411       pRun = ME_FindItemBack(pRun, diRunOrParagraph);
412       /* every paragraph ought to have at least one run */
413       assert(pRun && pRun->type == diRun);
414       assert(pRun->member.run.nFlags & MERF_ENDPARA);
415       p->pRun = pRun;
416       p->nOffset = 0;
417       return TRUE;
418     }
419     assert(0);
420   }
421   return FALSE;
422 }
423
424 BOOL ME_ArrowRight(ME_TextEditor *editor, ME_Cursor *p)
425 {
426   int new_ofs = ME_StrRelPos2(p->pRun->member.run.strText, p->nOffset, 1);
427   if (new_ofs<p->pRun->member.run.strText->nLen) {
428     p->nOffset = new_ofs;
429   }
430   else
431   {
432     ME_DisplayItem *pRun = ME_FindItemFwd(p->pRun, diRun);
433     if (pRun) {
434       p->pRun = pRun;
435       assert(p->pRun->type == diRun);
436       p->nOffset = 0;
437     }
438   }
439   return TRUE;
440 }
441
442 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
443 {
444   ME_Cursor *pCursor = &editor->pCursors[nCursor];
445   
446   return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
447     + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
448 }
449
450 int ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
451 {
452   ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
453   int rx = 0;
454   
455   if (is_eol)
456     *is_eol = 0;
457
458   while(p != editor->pBuffer->pLast)
459   {
460     if (p->type == diParagraph)
461     {
462       int ry = y - p->member.para.nYPos;
463       if (ry < 0)
464       {
465         result->pRun = ME_FindItemFwd(p, diRun);
466         result->nOffset = 0;
467         return 0;
468       }
469       if (ry >= p->member.para.nHeight)
470       {
471         p = p->member.para.next_para;
472         continue;
473       }
474       p = ME_FindItemFwd(p, diStartRow);
475       y = ry;
476       continue;
477     }
478     if (p->type == diStartRow)
479     {
480       int ry = y - p->member.row.nYPos;
481       if (ry < 0)
482         return 0;
483       if (ry >= p->member.row.nHeight)
484       {
485         p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
486         if (p->type != diStartRow)
487           return 0;
488         continue;
489       }
490       p = ME_FindItemFwd(p, diRun);
491       continue;
492     }
493     if (p->type == diRun)
494     {
495       ME_DisplayItem *pp;
496       rx = x - p->member.run.pt.x;
497       if (rx < 0)
498         rx = 0;
499       if (rx >= p->member.run.nWidth) /* not this run yet... find next item */
500       {
501         pp = p;
502         do {
503           p = p->next;
504           if (p->type == diRun)
505           {
506             rx = x - p->member.run.pt.x;
507             goto continue_search;
508           }
509           if (p->type == diStartRow)
510           {
511             p = ME_FindItemFwd(p, diRun);
512             if (is_eol)
513               *is_eol = 1;
514             rx = 0; /* FIXME not sure */
515             goto found_here;
516           }
517           if (p->type == diParagraph || p->type == diTextEnd)
518           {
519             rx = 0; /* FIXME not sure */
520             p = pp;
521             goto found_here;
522           }
523         } while(1);
524         continue;
525       }
526     found_here:
527       if (p->member.run.nFlags & MERF_ENDPARA)
528         rx = 0;
529       result->pRun = p;
530       result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
531       if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
532       {
533         result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
534         result->nOffset = 0;
535       }
536       return 1;
537     }
538     assert(0);
539   continue_search:
540     ;
541   }
542   result->pRun = ME_FindItemBack(p, diRun);
543   result->nOffset = 0;
544   assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
545   return 0;
546 }
547
548 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
549 {
550   ME_Cursor tmp_cursor;
551   int is_selection = 0;
552   
553   editor->nUDArrowX = -1;
554   
555   y += ME_GetYScrollPos(editor);
556
557   tmp_cursor = editor->pCursors[0];
558   is_selection = ME_IsSelection(editor);
559
560   ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
561   
562   if (GetKeyState(VK_SHIFT)>=0)
563   {
564     editor->pCursors[1] = editor->pCursors[0];
565   }
566   else
567   {
568     if (!is_selection) {
569       editor->pCursors[1] = tmp_cursor;
570       is_selection = 1;
571     }
572   }
573   HideCaret(editor->hWnd);
574   ME_MoveCaret(editor);
575   if (is_selection)
576     ME_Repaint(editor);
577   ShowCaret(editor->hWnd);
578   ME_ClearTempStyle(editor);
579   ME_SendSelChange(editor);
580 }
581
582 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
583 {
584   ME_Cursor tmp_cursor;
585   
586   y += ME_GetYScrollPos(editor);
587
588   tmp_cursor = editor->pCursors[0];
589   if (!ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd))
590     /* return */;
591   
592   if (tmp_cursor.pRun == editor->pCursors[0].pRun && 
593       tmp_cursor.nOffset == editor->pCursors[0].nOffset)
594     return;
595   
596   HideCaret(editor->hWnd);
597   ME_MoveCaret(editor);
598   ME_Repaint(editor);
599   ShowCaret(editor->hWnd);
600   ME_SendSelChange(editor);
601 }
602
603 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow, 
604                                 int x, int *pOffset, int *pbCaretAtEnd)
605 {
606   ME_DisplayItem *pNext, *pLastRun;
607   pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
608   assert(pNext->type == diRun);
609   pLastRun = pNext;
610   *pbCaretAtEnd = FALSE;
611   do {
612     int run_x = pNext->member.run.pt.x;
613     int width = pNext->member.run.nWidth;
614     if (x < run_x)
615     {
616       if (pOffset) *pOffset = 0;
617       return pNext;
618     }
619     if (x >= run_x && x < run_x+width)
620     {
621       int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
622       ME_String *s = pNext->member.run.strText;
623       if (ch < s->nLen) {
624         if (pOffset)
625           *pOffset = ch;
626         return pNext;          
627       }
628     }
629     pLastRun = pNext;
630     pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
631   } while(pNext && pNext->type == diRun);
632   
633   if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
634   {
635     pNext = ME_FindItemFwd(pNext, diRun);
636     if (pbCaretAtEnd) *pbCaretAtEnd = 1;
637     if (pOffset) *pOffset = 0;
638     return pNext;
639   } else {
640     if (pbCaretAtEnd) *pbCaretAtEnd = 0;
641     if (pOffset) *pOffset = 0;
642     return pLastRun;
643   }
644 }
645
646 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
647 {
648   ME_DisplayItem *pRun = pCursor->pRun;
649   int x;
650
651   if (editor->nUDArrowX != -1)
652     x = editor->nUDArrowX;
653   else {
654     if (editor->bCaretAtEnd)
655     {
656       pRun = ME_FindItemBack(pRun, diRun);
657       assert(pRun);
658       x = pRun->member.run.pt.x + pRun->member.run.nWidth;
659     }
660     else {
661       x = pRun->member.run.pt.x;
662       x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
663     }
664     editor->nUDArrowX = x;
665   }
666   return x;
667 }
668
669 void ME_ArrowUp(ME_TextEditor *editor, ME_Cursor *pCursor)
670 {
671   ME_DisplayItem *pRun = pCursor->pRun;
672   ME_DisplayItem *pItem, *pItem2;
673   int x = ME_GetXForArrow(editor, pCursor);
674   
675   if (editor->bCaretAtEnd && !pCursor->nOffset)
676   {
677     pRun = ME_FindItemBack(pRun, diRun);
678     if (!pRun)
679       return;
680   }
681   
682   /* start of this row */
683   pItem = ME_FindItemBack(pRun, diStartRow);
684   assert(pItem);
685   /* start of the previous row */
686   pItem2 = ME_FindItemBack(pItem, diStartRow);
687   /* no previous row = the first line of the first paragraph */
688   if (!pItem2) /* can't go up - don't go BOL (as in MS richedit) */
689     return;
690   /* FIXME
691   ME_WrapTextParagraph(editor, ME_FindItemBack(pItem2, diParagraph));
692   */
693   pCursor->pRun = ME_FindRunInRow(editor, pItem2, x, &pCursor->nOffset, &editor->bCaretAtEnd);
694 }
695
696 void ME_ArrowDown(ME_TextEditor *editor, ME_Cursor *pCursor)
697 {
698   ME_DisplayItem *pRun = pCursor->pRun;
699   ME_DisplayItem *pItem;
700   int x = ME_GetXForArrow(editor, pCursor);
701   if (!pCursor->nOffset && editor->bCaretAtEnd)
702   {
703     pRun = ME_FindItemBack(pRun, diRun);
704 /*    x = pRun->member.run.pt.x + pRun->member.run.nWidth; */
705   }
706   /* start of the next row */
707   pItem = ME_FindItemFwd(pRun, diStartRow);
708   /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
709   */
710   if (!pItem)
711   {
712     /* next row not found - ignore */
713     return;
714   }
715   pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
716   assert(pCursor->pRun);
717   assert(pCursor->pRun->type == diRun);
718 }
719
720 void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
721 {
722   ME_DisplayItem *pRun = pCursor->pRun;
723   ME_DisplayItem *pLast, *p;
724   int x, y, ys, yd, yp, yprev;
725   ME_Cursor tmp_curs = *pCursor;
726   
727   x = ME_GetXForArrow(editor, pCursor);
728   if (!pCursor->nOffset && editor->bCaretAtEnd)
729     pRun = ME_FindItemBack(pRun, diRun);
730   
731   p = ME_FindItemBack(pRun, diStartRowOrParagraph);
732   assert(p->type == diStartRow);
733   yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
734   yprev = ys = y = yp + p->member.row.nYPos;
735   yd = y - editor->sizeWindow.cy;
736   pLast = p;
737   
738   do {
739     p = ME_FindItemBack(p, diStartRowOrParagraph);
740     if (!p)
741       break;
742     if (p->type == diParagraph) { /* crossing paragraphs */
743       if (p->member.para.prev_para == NULL)
744         break;
745       yp = p->member.para.prev_para->member.para.nYPos;
746       continue;
747     }
748     y = yp + p->member.row.nYPos;
749     if (y < yd)
750       break;
751     pLast = p;
752     yprev = y;
753   } while(1);
754   
755   pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
756   ME_UpdateSelection(editor, &tmp_curs);
757   if (yprev < editor->sizeWindow.cy)
758   {
759     ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
760     ME_Repaint(editor);
761   }
762   else {
763     ME_Scroll(editor, 0, ys-yprev);
764     ME_Repaint(editor);
765   }
766   assert(pCursor->pRun);
767   assert(pCursor->pRun->type == diRun);
768 }
769
770 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount 
771    of pixels, even if it makes the scroll bar position exceed its normal maximum.
772    In such a situation, clicking the scrollbar restores its position back to the
773    normal range (ie. sets it to (doclength-screenheight)). */
774
775 void ME_ArrowPageDown(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_FindItemFwd(p, diStartRowOrParagraph);
795     if (!p)
796       break;
797     if (p->type == diParagraph) {
798       yp = p->member.para.nYPos;
799       continue;
800     }
801     y = yp + p->member.row.nYPos;
802     if (y >= yd)
803       break;
804     pLast = p;
805     yprev = y;
806   } while(1);
807   
808   pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
809   ME_UpdateSelection(editor, &tmp_curs);
810   if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
811   {
812     ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
813     ME_Repaint(editor);
814   }
815   else {
816     ME_Scroll(editor, 0, ys-yprev);
817     ME_Repaint(editor);
818   }
819   assert(pCursor->pRun);
820   assert(pCursor->pRun->type == diRun);
821 }
822
823 void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
824 {
825   ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
826   if (pRow) {
827     ME_DisplayItem *pRun;
828     if (editor->bCaretAtEnd && !pCursor->nOffset) {
829       pRow = ME_FindItemBack(pRow, diStartRow);
830       if (!pRow)
831         return;
832     }
833     pRun = ME_FindItemFwd(pRow, diRun);
834     if (pRun) {
835       pCursor->pRun = pRun;
836       pCursor->nOffset = 0;
837     }
838   }
839   editor->bCaretAtEnd = FALSE;
840 }
841
842 void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
843 {
844   ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
845   if (pRow) {
846     ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
847     if (pRun) {
848       pCursor->pRun = pRun;
849       pCursor->nOffset = 0;
850     }
851   }
852 }
853
854 void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
855 {
856   ME_DisplayItem *pRow;
857   
858   if (editor->bCaretAtEnd && !pCursor->nOffset)
859     return;
860   
861   pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
862   assert(pRow);
863   if (pRow->type == diStartRow) {
864     /* FIXME WTF was I thinking about here ? */
865     ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
866     assert(pRun);
867     pCursor->pRun = pRun;
868     pCursor->nOffset = 0;
869     editor->bCaretAtEnd = 1;
870     return;
871   }
872   pCursor->pRun = ME_FindItemBack(pRow, diRun);
873   assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
874   pCursor->nOffset = 0;
875   editor->bCaretAtEnd = FALSE;
876 }
877       
878 void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
879 {
880   ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
881   assert(p);
882   p = ME_FindItemBack(p, diRun);
883   assert(p);
884   assert(p->member.run.nFlags & MERF_ENDPARA);
885   pCursor->pRun = p;
886   pCursor->nOffset = 0;
887   editor->bCaretAtEnd = FALSE;
888 }
889
890 BOOL ME_IsSelection(ME_TextEditor *editor)
891 {
892   return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
893 }
894
895 int ME_GetSelCursor(ME_TextEditor *editor, int dir)
896 {
897   int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
898   
899   if (cdir*dir>0)
900     return 0;
901   else
902     return 1;
903 }
904       
905 BOOL ME_CancelSelection(ME_TextEditor *editor, int dir)
906 {
907   int cdir;
908   
909   if (GetKeyState(VK_SHIFT)<0)
910     return FALSE;
911   if (!memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor)))
912     return FALSE;
913   
914   cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
915   
916   if (cdir*dir>0)
917     editor->pCursors[1] = editor->pCursors[0];
918   else
919     editor->pCursors[0] = editor->pCursors[1];
920   ME_Repaint(editor);
921   return TRUE;
922 }
923
924 BOOL ME_UpdateSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
925 {
926   ME_Cursor old_anchor = editor->pCursors[1];
927   
928   if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
929   {
930     /* any selection was present ? if so, it's no more, repaint ! */
931     editor->pCursors[1] = editor->pCursors[0];
932     if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
933       return TRUE;
934     }
935     return FALSE;
936   }
937   else
938   {
939     if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
940     {
941       editor->pCursors[1] = *pTempCursor;
942       return TRUE;
943     }
944   }
945
946   ME_Repaint(editor);
947   return TRUE;
948 }
949
950 void ME_RepaintSelection(ME_TextEditor *editor, ME_Cursor *pTempCursor)
951 {
952   if (ME_UpdateSelection(editor, pTempCursor)) {
953     ME_EnsureVisible(editor, editor->pCursors[0].pRun); 
954     ME_Repaint(editor);
955   }
956 }
957
958 void ME_DeleteSelection(ME_TextEditor *editor)
959 {
960   int from, to;
961   ME_GetSelection(editor, &from, &to);
962   ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
963 }
964
965 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
966 {
967   ME_Style *style;
968   int from, to;
969   ME_Cursor c;
970   
971   ME_GetSelection(editor, &from, &to);
972   ME_CursorFromCharOfs(editor, from, &c);
973   if (from != to) {
974     style = c.pRun->member.run.style;
975     ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
976   }
977   else
978     style = ME_GetInsertStyle(editor, 0);
979   return style;
980 }
981
982 void ME_SendSelChange(ME_TextEditor *editor)
983 {
984   SELCHANGE sc;
985   if (!(editor->nEventMask & ENM_SELCHANGE))
986     return;
987   sc.nmhdr.hwndFrom = editor->hWnd;
988   sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
989   sc.nmhdr.code = EN_SELCHANGE;
990   SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
991   sc.seltyp = SEL_EMPTY;
992   if (sc.chrg.cpMin != sc.chrg.cpMax)
993     sc.seltyp |= SEL_TEXT;
994   if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
995     sc.seltyp |= SEL_MULTICHAR;
996   SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
997 }
998
999 BOOL ME_ArrowKey(ME_TextEditor *editor, int nVKey, int nCtrl)
1000 {
1001   int nCursor = 0;
1002   ME_Cursor *p = &editor->pCursors[nCursor];
1003   ME_Cursor tmp_curs = *p;
1004   
1005   switch(nVKey) {
1006     case VK_UP:
1007       ME_ArrowUp(editor, p);
1008       ME_ClearTempStyle(editor);
1009       ME_RepaintSelection(editor, &tmp_curs);
1010       ME_SendSelChange(editor);
1011       return TRUE;
1012     case VK_DOWN:
1013       ME_ArrowDown(editor, p);
1014       ME_ClearTempStyle(editor);
1015       ME_RepaintSelection(editor, &tmp_curs);
1016       ME_SendSelChange(editor);
1017       return TRUE;
1018     case VK_PRIOR:
1019       ME_ArrowPageUp(editor, p);
1020       ME_ClearTempStyle(editor);
1021       ME_SendSelChange(editor);
1022       return TRUE;
1023     case VK_NEXT:
1024       ME_ArrowPageDown(editor, p);
1025       ME_ClearTempStyle(editor);
1026       ME_SendSelChange(editor);
1027       return TRUE;
1028   }
1029   
1030   editor->nUDArrowX = -1;
1031   switch(nVKey) {
1032     case VK_BACK: { /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1033       if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1034         return FALSE;
1035       if (ME_IsSelection(editor))
1036       {
1037         editor->bCaretAtEnd = FALSE; /* FIXME or maybe not */
1038         ME_DeleteSelection(editor);
1039         ME_UpdateRepaint(editor);
1040         return TRUE;
1041       }
1042       if (ME_ArrowLeft(editor, p)) {
1043         editor->bCaretAtEnd = FALSE; /* FIXME or maybe not */
1044         ME_ClearTempStyle(editor);
1045         ME_MoveCaret(editor);
1046         ME_DeleteTextAtCursor(editor, nCursor, 1);
1047         ME_UpdateRepaint(editor);
1048       }
1049       return TRUE;
1050     }
1051     case VK_DELETE: {
1052       if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1053         return FALSE;
1054       /* editor->bCaretAtEnd = 0; FIXME or maybe not */
1055       if (ME_IsSelection(editor))
1056       {
1057         ME_DeleteSelection(editor);
1058         ME_ClearTempStyle(editor);
1059         ME_UpdateRepaint(editor);
1060         return TRUE;
1061       }
1062       ME_DeleteTextAtCursor(editor, nCursor, 1);
1063       ME_ClearTempStyle(editor);
1064       ME_UpdateRepaint(editor);
1065       return TRUE;
1066     }
1067     case VK_HOME: {
1068       if (GetKeyState(VK_CONTROL)<0)
1069         ME_ArrowCtrlHome(editor, p);
1070       else
1071         ME_ArrowHome(editor, p);
1072       editor->bCaretAtEnd = 0;
1073       ME_ClearTempStyle(editor);
1074       ME_RepaintSelection(editor, &tmp_curs);
1075       ME_SendSelChange(editor);
1076       return TRUE;
1077     }
1078     case VK_END: 
1079       if (GetKeyState(VK_CONTROL)<0)
1080         ME_ArrowCtrlEnd(editor, p);
1081       else
1082         ME_ArrowEnd(editor, p);
1083       ME_ClearTempStyle(editor);
1084       ME_RepaintSelection(editor, &tmp_curs);
1085       ME_SendSelChange(editor);
1086       return TRUE;
1087     case VK_LEFT:
1088       editor->bCaretAtEnd = 0;
1089       if (ME_CancelSelection(editor, -1))
1090         return TRUE;
1091       ME_ArrowLeft(editor, p);
1092       ME_RepaintSelection(editor, &tmp_curs);
1093       ME_ClearTempStyle(editor);
1094       ME_SendSelChange(editor);
1095       return TRUE;
1096     case VK_RIGHT:
1097       editor->bCaretAtEnd = 0;
1098       if (ME_CancelSelection(editor, +1))
1099         return TRUE;
1100       ME_ArrowRight(editor, p);
1101       ME_RepaintSelection(editor, &tmp_curs);
1102       ME_ClearTempStyle(editor);
1103       ME_SendSelChange(editor);
1104       return TRUE;
1105   }
1106   return FALSE;
1107 }