~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/riched20/caret.c

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  1 /*
  2  * RichEdit - Caret and selection functions.
  3  *
  4  * Copyright 2004 by Krzysztof Foltman
  5  * Copyright 2005 by Phil Krylov
  6  *
  7  * This library is free software; you can redistribute it and/or
  8  * modify it under the terms of the GNU Lesser General Public
  9  * License as published by the Free Software Foundation; either
 10  * version 2.1 of the License, or (at your option) any later version.
 11  *
 12  * This library is distributed in the hope that it will be useful,
 13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15  * Lesser General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Lesser General Public
 18  * License along with this library; if not, write to the Free Software
 19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 20  */
 21 
 22 
 23 #include "editor.h"
 24 
 25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
 26 
 27 static BOOL
 28 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs);
 29 
 30 void ME_GetSelection(ME_TextEditor *editor, int *from, int *to)
 31 {
 32   *from = ME_GetCursorOfs(editor, 0);
 33   *to =   ME_GetCursorOfs(editor, 1);
 34   
 35   if (*from > *to)
 36   {
 37     int tmp = *from;
 38     *from = *to;
 39     *to = tmp;    
 40   }
 41 }
 42 
 43 int ME_GetTextLength(ME_TextEditor *editor)
 44 {
 45   return ME_CharOfsFromRunOfs(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun), 0);   
 46 }
 47 
 48 
 49 int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how)
 50 {
 51   int length;
 52 
 53   if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
 54     return E_INVALIDARG;
 55   if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
 56     return E_INVALIDARG;
 57   
 58   length = ME_GetTextLength(editor);
 59 
 60   if ((GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_MULTILINE)
 61         && (how->flags & GTL_USECRLF)
 62         && !editor->bEmulateVersion10) /* Ignore GTL_USECRLF flag in 1.0 emulation */
 63     length += editor->nParagraphs - 1;
 64   
 65   if (how->flags & GTL_NUMBYTES)
 66   {
 67     CPINFO cpinfo;
 68     
 69     if (how->codepage == 1200)
 70       return length * 2;
 71     if (how->flags & GTL_PRECISE)
 72       FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
 73     if (GetCPInfo(how->codepage, &cpinfo))
 74       return length * cpinfo.MaxCharSize;
 75     ERR("Invalid codepage %u\n", how->codepage);
 76     return E_INVALIDARG;
 77   }
 78   return length; 
 79 }
 80 
 81 
 82 int ME_SetSelection(ME_TextEditor *editor, int from, int to)
 83 {
 84   int selectionEnd = 0;
 85   const int len = ME_GetTextLength(editor);
 86 
 87   /* all negative values are effectively the same */
 88   if (from < 0)
 89     from = -1;
 90   if (to < 0)
 91     to = -1;
 92 
 93   /* select all */
 94   if (from == 0 && to == -1)
 95   {
 96     editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
 97     editor->pCursors[1].nOffset = 0; 
 98     editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun); 
 99     editor->pCursors[0].nOffset = 0;
100     ME_InvalidateSelection(editor);
101     ME_ClearTempStyle(editor);
102     return len + 1;
103   }
104 
105   /* if both values are equal and also out of bound, that means to */
106   /* put the selection at the end of the text */
107   if ((from == to) && (to < 0 || to > len))
108   {
109     selectionEnd = 1;
110   }
111   else
112   {
113     /* if from is negative and to is positive then selection is */
114     /* deselected and caret moved to end of the current selection */
115     if (from < 0)
116     {
117       int start, end;
118       ME_GetSelection(editor, &start, &end);
119       editor->pCursors[1] = editor->pCursors[0];
120       ME_Repaint(editor);
121       ME_ClearTempStyle(editor);
122       return end;
123     }
124 
125     /* adjust to if it's a negative value */
126     if (to < 0)
127       to = len + 1;
128 
129     /* flip from and to if they are reversed */
130     if (from>to)
131     {
132       int tmp = from;
133       from = to;
134       to = tmp;
135     }
136 
137     /* after fiddling with the values, we find from > len && to > len */
138     if (from > len)
139       selectionEnd = 1;
140     /* special case with to too big */
141     else if (to > len)
142       to = len + 1;
143   }
144 
145   if (selectionEnd)
146   {
147     editor->pCursors[1].pRun = editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
148     editor->pCursors[1].nOffset = editor->pCursors[0].nOffset = 0;
149     ME_InvalidateSelection(editor);
150     ME_ClearTempStyle(editor);
151     return len;
152   }
153 
154   ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
155   ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);
156   return to;
157 }
158 
159 
160 void
161 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
162                         int *x, int *y, int *height)
163 {
164   ME_DisplayItem *pCursorRun = pCursor->pRun;
165   ME_DisplayItem *pSizeRun = pCursor->pRun;
166 
167   assert(height && x && y);
168   assert(!(ME_GetParagraph(pCursorRun)->member.para.nFlags & MEPF_REWRAP));
169   assert(pCursor->pRun);
170   assert(pCursor->pRun->type == diRun);
171   
172   if (pCursorRun->type == diRun) {
173     ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
174 
175     if (row) {
176       HDC hDC = GetDC(editor->hWnd);
177       ME_Context c;
178       ME_DisplayItem *run = pCursorRun;
179       ME_DisplayItem *para = NULL;
180       SIZE sz = {0, 0};
181     
182       ME_InitContext(&c, editor, hDC);
183       
184       if (!pCursor->nOffset)
185       {
186         ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrParagraph);
187         assert(prev);
188         if (prev->type == diRun)
189           pSizeRun = prev;
190       }
191       assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
192       para = ME_FindItemBack(row, diParagraph);
193       assert(para);
194       assert(para->type == diParagraph);
195       if (editor->bCaretAtEnd && !pCursor->nOffset && 
196           run == ME_FindItemFwd(row, diRun))
197       {
198         ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
199         assert(tmp);
200         if (tmp->type == diRun)
201         {
202           row = ME_FindItemBack(tmp, diStartRow);
203           pSizeRun = run = tmp;
204           assert(run);
205           assert(run->type == diRun);
206           sz = ME_GetRunSize(&c, &para->member.para,
207                              &run->member.run, ME_StrLen(run->member.run.strText),
208                              row->member.row.nLMargin);
209         }
210       }
211       if (pCursor->nOffset) {
212         sz = ME_GetRunSize(&c, &para->member.para, &run->member.run, pCursor->nOffset,
213                            row->member.row.nLMargin);
214       }
215 
216       *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
217       *x = run->member.run.pt.x + sz.cx;
218       *y = para->member.para.pt.y + row->member.row.nBaseline + run->member.run.pt.y - pSizeRun->member.run.nAscent - ME_GetYScrollPos(editor);
219       ME_DestroyContext(&c, editor->hWnd);
220       return;
221     }
222   }
223   *height = 10; /* FIXME use global font */
224   *x = 0;
225   *y = 0;
226 }
227 
228 
229 void
230 ME_MoveCaret(ME_TextEditor *editor)
231 {
232   int x, y, height;
233 
234   if (ME_WrapMarkedParagraphs(editor))
235     ME_UpdateScrollBar(editor);
236   ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
237   if(editor->bHaveFocus && !ME_IsSelection(editor))
238   {
239     RECT rect;
240 
241     GetClientRect(editor->hWnd, &rect);
242     x = min(x, rect.right-2);
243     CreateCaret(editor->hWnd, NULL, 0, height);
244     SetCaretPos(x, y);
245   }
246 }
247 
248 
249 void ME_ShowCaret(ME_TextEditor *ed)
250 {
251   ME_MoveCaret(ed);
252   if(ed->bHaveFocus && !ME_IsSelection(ed))
253     ShowCaret(ed->hWnd);
254 }
255 
256 void ME_HideCaret(ME_TextEditor *ed)
257 {
258   if(!ed->bHaveFocus || ME_IsSelection(ed))
259   {
260     HideCaret(ed->hWnd);
261     DestroyCaret();
262   }
263 }
264 
265 BOOL ME_InternalDeleteText(ME_TextEditor *editor, int nOfs, int nChars,
266                            BOOL bForce)
267 {
268   ME_Cursor c;
269   int shift = 0;
270   int totalChars = nChars;
271   ME_DisplayItem *start_para;
272 
273   {
274     /* Prevent deletion past last end of paragraph run. */
275     ME_DisplayItem *pTextEnd = editor->pBuffer->pLast;
276     int nMaxChars = pTextEnd->member.para.prev_para->member.para.nCharOfs;
277     nMaxChars += ME_FindItemBack(pTextEnd, diRun)->member.run.nCharOfs;
278     nMaxChars -= nOfs;
279     nChars = min(nChars, nMaxChars);
280   }
281 
282   ME_CursorFromCharOfs(editor, nOfs, &c);
283   start_para = ME_GetParagraph(c.pRun);
284 
285   if (!bForce)
286   {
287     ME_ProtectPartialTableDeletion(editor, nOfs, &nChars);
288     if (nChars == 0)
289       return FALSE;
290   }
291 
292   while(nChars > 0)
293   {
294     ME_Run *run;
295     ME_CursorFromCharOfs(editor, nOfs+nChars, &c);
296     if (!c.nOffset &&
297         nOfs+nChars == (c.pRun->member.run.nCharOfs
298                         + ME_GetParagraph(c.pRun)->member.para.nCharOfs))
299     {
300       /* We aren't deleting anything in this run, so we will go back to the
301        * last run we are deleting text in. */
302       c.pRun = ME_FindItemBack(c.pRun, diRun);
303       if (c.pRun->member.run.nFlags & MERF_ENDPARA)
304         c.nOffset = c.pRun->member.run.nCR + c.pRun->member.run.nLF;
305       else
306         c.nOffset = c.pRun->member.run.strText->nLen;
307     }
308     run = &c.pRun->member.run;
309     if (run->nFlags & MERF_ENDPARA) {
310       int eollen = run->nCR + run->nLF;
311       BOOL keepFirstParaFormat;
312 
313       if (!ME_FindItemFwd(c.pRun, diParagraph))
314       {
315         return TRUE;
316       }
317       keepFirstParaFormat = (totalChars == nChars && nChars <= eollen &&
318                              run->nCharOfs);
319       if (!editor->bEmulateVersion10) /* v4.1 */
320       {
321         ME_DisplayItem *next_para = ME_FindItemFwd(c.pRun, diParagraphOrEnd);
322         ME_DisplayItem *this_para = next_para->member.para.prev_para;
323 
324         /* The end of paragraph before a table row is only deleted if there
325          * is nothing else on the line before it. */
326         if (this_para == start_para &&
327             next_para->member.para.nFlags & MEPF_ROWSTART)
328         {
329           /* If the paragraph will be empty, then it should be deleted, however
330            * it still might have text right now which would inherit the
331            * MEPF_STARTROW property if we joined it right now.
332            * Instead we will delete it after the preceding text is deleted. */
333           if (nOfs > this_para->member.para.nCharOfs) {
334             /* Skip this end of line. */
335             nChars -= (eollen < nChars) ? eollen : nChars;
336             continue;
337           }
338           keepFirstParaFormat = TRUE;
339         }
340       }
341       ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun), keepFirstParaFormat);
342       /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
343       ME_CheckCharOffsets(editor);
344       nChars -= (eollen < nChars) ? eollen : nChars;
345       continue;
346     }
347     else
348     {
349       ME_Cursor cursor;
350       int nCharsToDelete = min(nChars, c.nOffset);
351       int i;
352 
353       c.nOffset -= nCharsToDelete;
354 
355       ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
356 
357       cursor = c;
358       /* nChars is the number of characters that should be deleted from the
359          PRECEDING runs (these BEFORE cursor.pRun)
360          nCharsToDelete is a number of chars to delete from THIS run */
361       nChars -= nCharsToDelete;
362       shift -= nCharsToDelete;
363       TRACE("Deleting %d (remaning %d) chars at %d in '%s' (%d)\n",
364         nCharsToDelete, nChars, c.nOffset,
365         debugstr_w(run->strText->szData), run->strText->nLen);
366 
367       if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
368       {
369         /* undo = reinsert whole run */
370         /* nOfs is a character offset (from the start of the document
371            to the current (deleted) run */
372         ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
373         if (pUndo)
374           pUndo->di.member.run.nCharOfs = nOfs+nChars;
375       }
376       else
377       {
378         /* undo = reinsert partial run */
379         ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
380         if (pUndo) {
381           ME_DestroyString(pUndo->di.member.run.strText);
382           pUndo->di.member.run.nCharOfs = nOfs+nChars;
383           pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
384         }
385       }
386       TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
387       TRACE("Shift value: %d\n", shift);
388       ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
389       
390       /* update cursors (including c) */
391       for (i=-1; i<editor->nCursors; i++) {
392         ME_Cursor *pThisCur = editor->pCursors + i; 
393         if (i == -1) pThisCur = &c;
394         if (pThisCur->pRun == cursor.pRun) {
395           if (pThisCur->nOffset > cursor.nOffset) {
396             if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
397               pThisCur->nOffset = cursor.nOffset;
398             else
399               pThisCur->nOffset -= nCharsToDelete;
400             assert(pThisCur->nOffset >= 0);
401             assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
402           }
403           if (pThisCur->nOffset == ME_StrVLen(run->strText))
404           {
405             pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
406             assert(pThisCur->pRun->type == diRun);
407             pThisCur->nOffset = 0;
408           }
409         }
410       }
411       
412       /* c = updated data now */
413       
414       if (c.pRun == cursor.pRun)
415         ME_SkipAndPropagateCharOffset(c.pRun, shift);
416       else
417         ME_PropagateCharOffset(c.pRun, shift);
418 
419       if (!ME_StrVLen(cursor.pRun->member.run.strText))
420       {
421         TRACE("Removing useless run\n");
422         ME_Remove(cursor.pRun);
423         ME_DestroyDisplayItem(cursor.pRun);
424       }
425       
426       shift = 0;
427       /*
428       ME_CheckCharOffsets(editor);
429       */
430       continue;
431     }
432   }
433   return TRUE;
434 }
435 
436 BOOL ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, int nChars)
437 {  
438   assert(nCursor>=0 && nCursor<editor->nCursors);
439   /* text operations set modified state */
440   editor->nModifyStep = 1;
441   return ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars,
442                                FALSE);
443 }
444 
445 static ME_DisplayItem *
446 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
447                                 const WCHAR *str, int len, ME_Style *style,
448                                 int flags)
449 {
450   ME_Cursor *p = &editor->pCursors[nCursor];
451 
452   editor->bCaretAtEnd = FALSE;
453   
454   assert(p->pRun->type == diRun);
455   
456   return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
457 }
458 
459 
460 void ME_InsertOLEFromCursor(ME_TextEditor *editor, const REOBJECT* reo, int nCursor)
461 {
462   ME_Style              *pStyle = ME_GetInsertStyle(editor, nCursor);
463   ME_DisplayItem        *di;
464   WCHAR                 space = ' ';
465   
466   /* FIXME no no no */
467   if (ME_IsSelection(editor))
468     ME_DeleteSelection(editor);
469 
470   di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
471                                        MERF_GRAPHICS);
472   di->member.run.ole_obj = ALLOC_OBJ(*reo);
473   ME_CopyReObject(di->member.run.ole_obj, reo);
474   ME_SendSelChange(editor);
475 }
476 
477 
478 void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor)
479 {
480   ME_Style              *pStyle = ME_GetInsertStyle(editor, nCursor);
481   ME_DisplayItem        *di;
482   WCHAR                 space = ' ';
483 
484   /* FIXME no no no */
485   if (ME_IsSelection(editor))
486     ME_DeleteSelection(editor);
487 
488   di = ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
489                                        MERF_ENDROW);
490   ME_SendSelChange(editor);
491 }
492 
493 
494 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor, 
495   const WCHAR *str, int len, ME_Style *style)
496 {
497   const WCHAR *pos;
498   ME_Cursor *p = NULL;
499   int oldLen;
500 
501   /* FIXME really HERE ? */
502   if (ME_IsSelection(editor))
503     ME_DeleteSelection(editor);
504 
505   /* FIXME: is this too slow? */
506   /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
507   oldLen = ME_GetTextLength(editor);
508 
509   /* text operations set modified state */
510   editor->nModifyStep = 1;
511 
512   assert(style);
513 
514   assert(nCursor>=0 && nCursor<editor->nCursors);
515   if (len == -1)
516     len = lstrlenW(str);
517 
518   /* grow the text limit to fit our text */
519   if(editor->nTextLimit < oldLen +len)
520     editor->nTextLimit = oldLen + len;
521 
522   while (len)
523   {
524     pos = str;
525     /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
526     while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
527       pos++;
528     if (pos-str < len && *pos == '\t') { /* handle tabs */
529       WCHAR tab = '\t';
530 
531       if (pos!=str)
532         ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
533     
534       ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
535  
536       pos++;
537       if(pos-str <= len) {
538         len -= pos - str;
539         str = pos;
540         continue;
541       }
542     }
543     /* handle special \r\r\n sequence (richedit 2.x and higher only) */
544     if (!editor->bEmulateVersion10 && pos-str < len-2 && pos[0] == '\r' && pos[1] == '\r' && pos[2] == '\n') {
545       WCHAR space = ' ';
546 
547       if (pos!=str)
548         ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
549 
550       ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
551 
552       pos+=3;
553       if(pos-str <= len) {
554         len -= pos - str;
555         str = pos;
556         continue;
557       }
558     }
559     if (pos-str < len) {   /* handle EOLs */
560       ME_DisplayItem *tp, *end_run;
561       ME_Style *tmp_style;
562       int numCR, numLF;
563 
564       if (pos!=str)
565         ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
566       p = &editor->pCursors[nCursor];
567       if (p->nOffset) {
568         ME_SplitRunSimple(editor, p->pRun, p->nOffset);
569         p = &editor->pCursors[nCursor];
570       }
571       tmp_style = ME_GetInsertStyle(editor, nCursor);
572       /* ME_SplitParagraph increases style refcount */
573 
574       /* Encode and fill number of CR and LF according to emulation mode */
575       if (editor->bEmulateVersion10) {
576         const WCHAR * tpos;
577 
578         /* We have to find out how many consecutive \r are there, and if there
579            is a \n terminating the run of \r's. */
580         numCR = 0; numLF = 0;
581         tpos = pos;
582         while (tpos-str < len && *tpos == '\r') {
583           tpos++;
584           numCR++;
585         }
586         if (tpos-str >= len) {
587           /* Reached end of text without finding anything but '\r' */
588           if (tpos != pos) {
589             pos++;
590           }
591           numCR = 1; numLF = 0;
592         } else if (*tpos == '\n') {
593           /* The entire run of \r's plus the one \n is one single line break */
594           pos = tpos + 1;
595           numLF = 1;
596         } else {
597           /* Found some other content past the run of \r's */
598           pos++;
599           numCR = 1; numLF = 0;
600         }
601       } else {
602         if(pos-str < len && *pos =='\r')
603           pos++;
604         if(pos-str < len && *pos =='\n')
605           pos++;
606         numCR = 1; numLF = 0;
607       }
608       tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style, numCR, numLF, 0);
609       p->pRun = ME_FindItemFwd(tp, diRun);
610       end_run = ME_FindItemBack(tp, diRun);
611       ME_ReleaseStyle(end_run->member.run.style);
612       end_run->member.run.style = tmp_style;
613       p->nOffset = 0;
614 
615       if(pos-str <= len) {
616         len -= pos - str;
617         str = pos;
618         continue;
619       }
620     }
621     ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
622     len = 0;
623   }
624 }
625 
626 
627 static BOOL
628 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
629 {
630   ME_DisplayItem *pRun = pCursor->pRun;
631   
632   if (nRelOfs == -1)
633   {
634     if (!pCursor->nOffset)
635     {
636       do {
637         pRun = ME_FindItemBack(pRun, diRunOrParagraph);
638         assert(pRun);
639         switch (pRun->type)
640         {
641           case diRun:
642             break;
643           case diParagraph:
644             if (pRun->member.para.prev_para->type == diTextStart)
645               return FALSE;
646             pRun = ME_FindItemBack(pRun, diRunOrParagraph);
647             /* every paragraph ought to have at least one run */
648             assert(pRun && pRun->type == diRun);
649             assert(pRun->member.run.nFlags & MERF_ENDPARA);
650             break;
651           default:
652             assert(pRun->type != diRun && pRun->type != diParagraph);
653             return FALSE;
654         }
655       } while (RUN_IS_HIDDEN(&pRun->member.run) ||
656                pRun->member.run.nFlags & MERF_HIDDEN);
657       pCursor->pRun = pRun;
658       if (pRun->member.run.nFlags & MERF_ENDPARA)
659         pCursor->nOffset = 0;
660       else
661         pCursor->nOffset = pRun->member.run.strText->nLen;
662     }
663     
664     if (pCursor->nOffset)
665       pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
666     return TRUE;
667   }
668   else
669   {
670     if (!(pRun->member.run.nFlags & MERF_ENDPARA))
671     {
672       int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
673     
674       if (new_ofs < pRun->member.run.strText->nLen)
675       {
676         pCursor->nOffset = new_ofs;
677         return TRUE;
678       }
679     }
680     do {
681       pRun = ME_FindItemFwd(pRun, diRun);
682     } while (pRun && (RUN_IS_HIDDEN(&pRun->member.run) ||
683                       pRun->member.run.nFlags & MERF_HIDDEN));
684     if (pRun)
685     {
686       pCursor->pRun = pRun;
687       pCursor->nOffset = 0;
688       return TRUE;
689     }
690   }
691   return FALSE;
692 }
693 
694 
695 static BOOL
696 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
697 {
698   ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
699   int nOffset = cursor->nOffset;
700   
701   if (nRelOfs == -1)
702   {
703     /* Backward movement */
704     while (TRUE)
705     {
706       nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
707                                      nOffset, WB_MOVEWORDLEFT);
708        if (nOffset)
709         break;
710       pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
711       if (pOtherRun->type == diRun)
712       {
713         if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
714                                  pOtherRun->member.run.strText->nLen - 1,
715                                  WB_ISDELIMITER)
716             && !(pRun->member.run.nFlags & MERF_ENDPARA)
717             && !(cursor->pRun == pRun && cursor->nOffset == 0)
718             && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
719                                      WB_ISDELIMITER))
720           break;
721         pRun = pOtherRun;
722         nOffset = pOtherRun->member.run.strText->nLen;
723       }
724       else if (pOtherRun->type == diParagraph)
725       {
726         if (cursor->pRun == pRun && cursor->nOffset == 0)
727         {
728           /* Skip empty start of table row paragraph */
729           if (pOtherRun->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART)
730             pOtherRun = pOtherRun->member.para.prev_para;
731           /* Paragraph breaks are treated as separate words */
732           if (pOtherRun->member.para.prev_para->type == diTextStart)
733             return FALSE;
734 
735           pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
736         }
737         break;
738       }
739     }
740   }
741   else
742   {
743     /* Forward movement */
744     BOOL last_delim = FALSE;
745     
746     while (TRUE)
747     {
748       if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
749                                               nOffset, WB_ISDELIMITER))
750         break;
751       nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
752                                      nOffset, WB_MOVEWORDRIGHT);
753       if (nOffset < pRun->member.run.strText->nLen)
754         break;
755       pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
756       if (pOtherRun->type == diRun)
757       {
758         last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
759                                           nOffset - 1, WB_ISDELIMITER);
760         pRun = pOtherRun;
761         nOffset = 0;
762       }
763       else if (pOtherRun->type == diParagraph)
764       {
765         if (pOtherRun->member.para.nFlags & MEPF_ROWSTART)
766             pOtherRun = pOtherRun->member.para.next_para;
767         if (cursor->pRun == pRun)
768           pRun = ME_FindItemFwd(pOtherRun, diRun);
769         nOffset = 0;
770         break;
771       }
772       else /* diTextEnd */
773       {
774         if (cursor->pRun == pRun)
775           return FALSE;
776         nOffset = 0;
777         break;
778       }
779     }
780   }
781   cursor->pRun = pRun;
782   cursor->nOffset = nOffset;
783   return TRUE;
784 }
785 
786 
787 void
788 ME_SelectByType(ME_TextEditor *editor, ME_SelectionType selectionType)
789 {
790   /* pCursor[0] is the end of the selection
791    * pCursor[1] is the start of the selection (or the position selection anchor)
792    * pCursor[2] and [3] are the selection anchors that are backed up
793    * so they are kept when the selection changes for drag selection.
794    */
795 
796   editor->nSelectionType = selectionType;
797   switch(selectionType)
798   {
799     case stPosition:
800       break;
801     case stWord:
802       ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
803       editor->pCursors[1] = editor->pCursors[0];
804       ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
805       break;
806     case stLine:
807     case stParagraph:
808     {
809       ME_DisplayItem *pItem;
810       ME_DIType fwdSearchType, backSearchType;
811       if (selectionType == stParagraph) {
812           backSearchType = diParagraph;
813           fwdSearchType = diParagraphOrEnd;
814       } else {
815           backSearchType = diStartRow;
816           fwdSearchType = diStartRowOrParagraphOrEnd;
817       }
818       pItem = ME_FindItemFwd(editor->pCursors[0].pRun, fwdSearchType);
819       assert(pItem);
820       if (pItem->type == diTextEnd)
821           editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
822       else
823           editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
824       editor->pCursors[0].nOffset = 0;
825 
826       pItem = ME_FindItemBack(pItem, backSearchType);
827       editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
828       editor->pCursors[1].nOffset = 0;
829       break;
830     }
831     case stDocument:
832       /* Select everything with cursor anchored from the start of the text */
833       editor->nSelectionType = stDocument;
834       editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
835       editor->pCursors[1].nOffset = 0;
836       editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
837       editor->pCursors[0].nOffset = 0;
838       break;
839     default: assert(0);
840   }
841   /* Store the anchor positions for extending the selection. */
842   editor->pCursors[2] = editor->pCursors[0];
843   editor->pCursors[3] = editor->pCursors[1];
844 }
845 
846 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
847 {
848   ME_Cursor *pCursor = &editor->pCursors[nCursor];
849   return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
850     + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
851 }
852 
853 /* Helper function for ME_FindPixelPos to find paragraph within tables */
854 static ME_DisplayItem* ME_FindPixelPosInTableRow(int x, int y,
855                                                  ME_DisplayItem *para)
856 {
857   ME_DisplayItem *cell, *next_cell;
858   assert(para->member.para.nFlags & MEPF_ROWSTART);
859   cell = para->member.para.next_para->member.para.pCell;
860   assert(cell);
861 
862   /* find the cell we are in */
863   while ((next_cell = cell->member.cell.next_cell) != NULL) {
864     if (x < next_cell->member.cell.pt.x)
865     {
866       para = ME_FindItemFwd(cell, diParagraph);
867       /* Found the cell, but there might be multiple paragraphs in
868        * the cell, so need to search down the cell for the paragraph. */
869       while (cell == para->member.para.pCell) {
870         if (y < para->member.para.pt.y + para->member.para.nHeight)
871         {
872           if (para->member.para.nFlags & MEPF_ROWSTART)
873             return ME_FindPixelPosInTableRow(x, y, para);
874           else
875             return para;
876         }
877         para = para->member.para.next_para;
878       }
879       /* Past the end of the cell, so go back to the last cell paragraph */
880       return para->member.para.prev_para;
881     }
882     cell = next_cell;
883   }
884   /* Return table row delimiter */
885   para = ME_FindItemFwd(cell, diParagraph);
886   assert(para->member.para.nFlags & MEPF_ROWEND);
887   assert(para->member.para.pFmt->dwMask & PFM_TABLEROWDELIMITER);
888   assert(para->member.para.pFmt->wEffects & PFE_TABLEROWDELIMITER);
889   return para;
890 }
891 
892 /* Finds the run and offset from the pixel position.
893  *
894  * x & y are pixel positions in virtual coordinates into the rich edit control,
895  * so client coordinates must first be adjusted by the scroll position.
896  *
897  * returns TRUE if the result was exactly under the cursor, otherwise returns
898  * FALSE, and result is set to the closest position to the coordinates.
899  */
900 static BOOL ME_FindPixelPos(ME_TextEditor *editor, int x, int y,
901                             ME_Cursor *result, BOOL *is_eol)
902 {
903   ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
904   ME_DisplayItem *last = NULL;
905   int rx = 0;
906   BOOL isExact = TRUE;
907 
908   if (is_eol)
909     *is_eol = 0;
910 
911   /* find paragraph */
912   for (; p != editor->pBuffer->pLast; p = p->member.para.next_para)
913   {
914     assert(p->type == diParagraph);
915     if (y < p->member.para.pt.y + p->member.para.nHeight)
916     {
917       if (p->member.para.nFlags & MEPF_ROWSTART)
918         p = ME_FindPixelPosInTableRow(x, y, p);
919       y -= p->member.para.pt.y;
920       p = ME_FindItemFwd(p, diStartRow);
921       break;
922     } else if (p->member.para.nFlags & MEPF_ROWSTART) {
923       p = ME_GetTableRowEnd(p);
924     }
925   }
926   /* find row */
927   for (; p != editor->pBuffer->pLast; )
928   {
929     ME_DisplayItem *pp;
930     assert(p->type == diStartRow);
931     if (y < p->member.row.pt.y + p->member.row.nHeight)
932     {
933         p = ME_FindItemFwd(p, diRun);
934         break;
935     }
936     pp = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
937     if (pp->type != diStartRow)
938     {
939         p = ME_FindItemFwd(p, diRun);
940         break;
941     }
942     p = pp;
943   }
944   if (p == editor->pBuffer->pLast)
945   {
946     /* The position is below the last paragraph, so the last row will be used
947      * rather than the end of the text, so the x position will be used to
948      * determine the offset closest to the pixel position. */
949     isExact = FALSE;
950     p = ME_FindItemBack(p, diStartRow);
951     if (p != NULL){
952       p = ME_FindItemFwd(p, diRun);
953     }
954     else
955     {
956       p = editor->pBuffer->pLast;
957     }
958   }
959   for (; p != editor->pBuffer->pLast; p = p->next)
960   {
961     switch (p->type)
962     {
963     case diRun:
964       rx = x - p->member.run.pt.x;
965       if (rx < p->member.run.nWidth)
966       {
967       found_here:
968         assert(p->type == diRun);
969         if ((p->member.run.nFlags & MERF_ENDPARA) || rx < 0)
970           rx = 0;
971         result->pRun = p;
972         result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
973         if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
974         {
975           result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
976           result->nOffset = 0;
977         }
978         return isExact;
979       }
980       break;
981     case diStartRow:
982       isExact = FALSE;
983       p = ME_FindItemFwd(p, diRun);
984       if (is_eol) *is_eol = 1;
985       rx = 0; /* FIXME not sure */
986       goto found_here;
987     case diCell:
988     case diParagraph:
989     case diTextEnd:
990       isExact = FALSE;
991       rx = 0; /* FIXME not sure */
992       p = last;
993       goto found_here;
994     default: assert(0);
995     }
996     last = p;
997   }
998   result->pRun = ME_FindItemBack(p, diRun);
999   result->nOffset = 0;
1000   assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
1001   return FALSE;
1002 }
1003 
1004 
1005 /* Returns the character offset closest to the pixel position
1006  *
1007  * x & y are pixel positions in client coordinates.
1008  *
1009  * isExact will be set to TRUE if the run is directly under the pixel
1010  * position, FALSE if it not, unless isExact is set to NULL.
1011  */
1012 int ME_CharFromPos(ME_TextEditor *editor, int x, int y, BOOL *isExact)
1013 {
1014   ME_Cursor cursor;
1015   RECT rc;
1016   BOOL bResult;
1017 
1018   GetClientRect(editor->hWnd, &rc);
1019   if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom) {
1020     if (isExact) *isExact = FALSE;
1021     return -1;
1022   }
1023   y += ME_GetYScrollPos(editor);
1024   bResult = ME_FindPixelPos(editor, x, y, &cursor, NULL);
1025   if (isExact) *isExact = bResult;
1026   return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
1027           + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
1028 }
1029 
1030 
1031 
1032 /* Extends the selection with a word, line, or paragraph selection type.
1033  *
1034  * The selection is anchored by editor->pCursors[2-3] such that the text
1035  * between the anchors will remain selected, and one end will be extended.
1036  *
1037  * editor->pCursors[0] should have the position to extend the selection to
1038  * before this function is called.
1039  *
1040  * Nothing will be done if editor->nSelectionType equals stPosition.
1041  */
1042 static void ME_ExtendAnchorSelection(ME_TextEditor *editor)
1043 {
1044   ME_Cursor tmp_cursor;
1045   int curOfs, anchorStartOfs, anchorEndOfs;
1046   if (editor->nSelectionType == stPosition || editor->nSelectionType == stDocument)
1047       return;
1048   curOfs = ME_GetCursorOfs(editor, 0);
1049   anchorStartOfs = ME_GetCursorOfs(editor, 3);
1050   anchorEndOfs = ME_GetCursorOfs(editor, 2);
1051 
1052   tmp_cursor = editor->pCursors[0];
1053   editor->pCursors[0] = editor->pCursors[2];
1054   editor->pCursors[1] = editor->pCursors[3];
1055   if (curOfs < anchorStartOfs)
1056   {
1057       /* Extend the left side of selection */
1058       editor->pCursors[1] = tmp_cursor;
1059       if (editor->nSelectionType == stWord)
1060           ME_MoveCursorWords(editor, &editor->pCursors[1], -1);
1061       else
1062       {
1063           ME_DisplayItem *pItem;
1064           ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1065                                   diStartRowOrParagraph:diParagraph);
1066           pItem = ME_FindItemBack(editor->pCursors[1].pRun, searchType);
1067           editor->pCursors[1].pRun = ME_FindItemFwd(pItem, diRun);
1068           editor->pCursors[1].nOffset = 0;
1069       }
1070   }
1071   else if (curOfs >= anchorEndOfs)
1072   {
1073       /* Extend the right side of selection */
1074       editor->pCursors[0] = tmp_cursor;
1075       if (editor->nSelectionType == stWord)
1076           ME_MoveCursorWords(editor, &editor->pCursors[0], +1);
1077       else
1078       {
1079           ME_DisplayItem *pItem;
1080           ME_DIType searchType = ((editor->nSelectionType == stLine) ?
1081                                   diStartRowOrParagraphOrEnd:diParagraphOrEnd);
1082           pItem = ME_FindItemFwd(editor->pCursors[0].pRun, searchType);
1083           if (pItem->type == diTextEnd)
1084               editor->pCursors[0].pRun = ME_FindItemBack(pItem, diRun);
1085           else
1086               editor->pCursors[0].pRun = ME_FindItemFwd(pItem, diRun);
1087           editor->pCursors[0].nOffset = 0;
1088       }
1089   }
1090 }
1091 
1092 void ME_LButtonDown(ME_TextEditor *editor, int x, int y, int clickNum)
1093 {
1094   ME_Cursor tmp_cursor;
1095   int is_selection = 0;
1096   BOOL is_shift;
1097   
1098   editor->nUDArrowX = -1;
1099   
1100   y += ME_GetYScrollPos(editor);
1101 
1102   tmp_cursor = editor->pCursors[0];
1103   is_selection = ME_IsSelection(editor);
1104   is_shift = GetKeyState(VK_SHIFT) < 0;
1105 
1106   ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
1107 
1108   if (x >= editor->selofs || is_shift)
1109   {
1110     if (clickNum > 1)
1111     {
1112       editor->pCursors[1] = editor->pCursors[0];
1113       if (is_shift) {
1114           if (x >= editor->selofs)
1115               ME_SelectByType(editor, stWord);
1116           else
1117               ME_SelectByType(editor, stParagraph);
1118       } else if (clickNum % 2 == 0) {
1119           ME_SelectByType(editor, stWord);
1120       } else {
1121           ME_SelectByType(editor, stParagraph);
1122       }
1123     }
1124     else if (!is_shift)
1125     {
1126       editor->nSelectionType = stPosition;
1127       editor->pCursors[1] = editor->pCursors[0];
1128     }
1129     else if (!is_selection)
1130     {
1131       editor->nSelectionType = stPosition;
1132       editor->pCursors[1] = tmp_cursor;
1133     }
1134     else if (editor->nSelectionType != stPosition)
1135     {
1136       ME_ExtendAnchorSelection(editor);
1137     }
1138   }
1139   else
1140   {
1141     if (clickNum < 2) {
1142         ME_SelectByType(editor, stLine);
1143     } else if (clickNum % 2 == 0 || is_shift) {
1144         ME_SelectByType(editor, stParagraph);
1145     } else {
1146         ME_SelectByType(editor, stDocument);
1147     }
1148   }
1149   ME_InvalidateSelection(editor);
1150   HideCaret(editor->hWnd);
1151   ME_ShowCaret(editor);
1152   ME_ClearTempStyle(editor);
1153   ME_SendSelChange(editor);
1154 }
1155 
1156 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
1157 {
1158   ME_Cursor tmp_cursor;
1159   
1160   if (editor->nSelectionType == stDocument)
1161       return;
1162   y += ME_GetYScrollPos(editor);
1163 
1164   tmp_cursor = editor->pCursors[0];
1165   /* FIXME: do something with the return value of ME_FindPixelPos */
1166   ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
1167 
1168   ME_InvalidateSelection(editor);
1169   editor->pCursors[0] = tmp_cursor;
1170   ME_ExtendAnchorSelection(editor);
1171 
1172   if (editor->nSelectionType != stPosition &&
1173       memcmp(&editor->pCursors[1], &editor->pCursors[3], sizeof(ME_Cursor)))
1174   {
1175       /* The scroll the cursor towards the other end, since it was the one
1176        * extended by ME_ExtendAnchorSelection
1177        */
1178       ME_Cursor tmpCursor = editor->pCursors[0];
1179       editor->pCursors[0] = editor->pCursors[1];
1180       editor->pCursors[1] = tmpCursor;
1181       SendMessageW(editor->hWnd, EM_SCROLLCARET, 0, 0);
1182       editor->pCursors[1] = editor->pCursors[0];
1183       editor->pCursors[0] = tmpCursor;
1184   } else {
1185       SendMessageW(editor->hWnd, EM_SCROLLCARET, 0, 0);
1186   }
1187 
1188   ME_InvalidateSelection(editor);
1189   HideCaret(editor->hWnd);
1190   ME_ShowCaret(editor);
1191   ME_SendSelChange(editor);
1192 }
1193 
1194 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow, 
1195                                 int x, int *pOffset, int *pbCaretAtEnd)
1196 {
1197   ME_DisplayItem *pNext, *pLastRun;
1198   pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
1199   assert(pNext->type == diRun);
1200   pLastRun = pNext;
1201   if (pbCaretAtEnd) *pbCaretAtEnd = FALSE;
1202   if (pOffset) *pOffset = 0;
1203   do {
1204     int run_x = pNext->member.run.pt.x;
1205     int width = pNext->member.run.nWidth;
1206     if (x < run_x)
1207     {
1208       return pNext;
1209     }
1210     if (x >= run_x && x < run_x+width)
1211     {
1212       int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
1213       ME_String *s = pNext->member.run.strText;
1214       if (ch < s->nLen) {
1215         if (pOffset)
1216           *pOffset = ch;
1217         return pNext;          
1218       }
1219     }
1220     pLastRun = pNext;
1221     pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
1222   } while(pNext && pNext->type == diRun);
1223   
1224   if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
1225   {
1226     pNext = ME_FindItemFwd(pNext, diRun);
1227     if (pbCaretAtEnd) *pbCaretAtEnd = TRUE;
1228     return pNext;
1229   } else {
1230     return pLastRun;
1231   }
1232 }
1233 
1234 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
1235 {
1236   ME_DisplayItem *pRun = pCursor->pRun;
1237   int x;
1238 
1239   if (editor->nUDArrowX != -1)
1240     x = editor->nUDArrowX;
1241   else {
1242     if (editor->bCaretAtEnd)
1243     {
1244       pRun = ME_FindItemBack(pRun, diRun);
1245       assert(pRun);
1246       x = pRun->member.run.pt.x + pRun->member.run.nWidth;
1247     }
1248     else {
1249       x = pRun->member.run.pt.x;
1250       x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
1251     }
1252     editor->nUDArrowX = x;
1253   }
1254   return x;
1255 }
1256 
1257 
1258 static void
1259 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1260 {
1261   ME_DisplayItem *pRun = pCursor->pRun;
1262   ME_DisplayItem *pItem, *pOldPara, *pNewPara;
1263   int x = ME_GetXForArrow(editor, pCursor);
1264 
1265   if (editor->bCaretAtEnd && !pCursor->nOffset)
1266     pRun = ME_FindItemBack(pRun, diRun);
1267   if (!pRun)
1268     return;
1269   pOldPara = ME_GetParagraph(pRun);
1270   if (nRelOfs == -1)
1271   {
1272     /* start of this row */
1273     pItem = ME_FindItemBack(pRun, diStartRow);
1274     assert(pItem);
1275     /* start of the previous row */
1276     pItem = ME_FindItemBack(pItem, diStartRow);
1277     if (!pItem)
1278       return; /* row not found - ignore */
1279     pNewPara = ME_GetParagraph(pItem);
1280     if (pOldPara->member.para.nFlags & MEPF_ROWEND ||
1281         (pOldPara->member.para.pCell &&
1282          pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1283     {
1284       /* Brought out of a cell */
1285       pNewPara = ME_GetTableRowStart(pOldPara)->member.para.prev_para;
1286       if (pNewPara->type == diTextStart)
1287         return; /* At the top, so don't go anywhere. */
1288       pItem = ME_FindItemFwd(pNewPara, diStartRow);
1289     }
1290     if (pNewPara->member.para.nFlags & MEPF_ROWEND)
1291     {
1292       /* Brought into a table row */
1293       ME_Cell *cell = &ME_FindItemBack(pNewPara, diCell)->member.cell;
1294       while (x < cell->pt.x && cell->prev_cell)
1295         cell = &cell->prev_cell->member.cell;
1296       if (cell->next_cell) /* else - we are still at the end of the row */
1297         pItem = ME_FindItemBack(cell->next_cell, diStartRow);
1298     }
1299   }
1300   else
1301   {
1302     /* start of the next row */
1303     pItem = ME_FindItemFwd(pRun, diStartRow);
1304     if (!pItem)
1305       return; /* row not found - ignore */
1306     /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1307     */
1308     pNewPara = ME_GetParagraph(pItem);
1309     if (pOldPara->member.para.nFlags & MEPF_ROWSTART ||
1310         (pOldPara->member.para.pCell &&
1311          pOldPara->member.para.pCell != pNewPara->member.para.pCell))
1312     {
1313       /* Brought out of a cell */
1314       pNewPara = ME_GetTableRowEnd(pOldPara)->member.para.next_para;
1315       if (pNewPara->type == diTextEnd)
1316         return; /* At the bottom, so don't go anywhere. */
1317       pItem = ME_FindItemFwd(pNewPara, diStartRow);
1318     }
1319     if (pNewPara->member.para.nFlags & MEPF_ROWSTART)
1320     {
1321       /* Brought into a table row */
1322       ME_DisplayItem *cell = ME_FindItemFwd(pNewPara, diCell);
1323       while (cell->member.cell.next_cell &&
1324              x >= cell->member.cell.next_cell->member.cell.pt.x)
1325         cell = cell->member.cell.next_cell;
1326       pItem = ME_FindItemFwd(cell, diStartRow);
1327     }
1328   }
1329   if (!pItem)
1330   {
1331     /* row not found - ignore */
1332     return;
1333   }
1334   pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1335   assert(pCursor->pRun);
1336   assert(pCursor->pRun->type == diRun);
1337 }
1338 
1339 
1340 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1341 {
1342   ME_DisplayItem *pRun = pCursor->pRun;
1343   ME_DisplayItem *pLast, *p;
1344   int x, y, ys, yd, yp, yprev;
1345   ME_Cursor tmp_curs = *pCursor;
1346   
1347   x = ME_GetXForArrow(editor, pCursor);
1348   if (!pCursor->nOffset && editor->bCaretAtEnd)
1349     pRun = ME_FindItemBack(pRun, diRun);
1350   
1351   p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1352   assert(p->type == diStartRow);
1353   yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1354   yprev = ys = y = yp + p->member.row.pt.y;
1355   yd = y - editor->sizeWindow.cy;
1356   pLast = p;
1357   
1358   do {
1359     p = ME_FindItemBack(p, diStartRowOrParagraph);
1360     if (!p)
1361       break;
1362     if (p->type == diParagraph) { /* crossing paragraphs */
1363       if (p->member.para.prev_para == NULL)
1364         break;
1365       yp = p->member.para.prev_para->member.para.pt.y;
1366       continue;
1367     }
1368     y = yp + p->member.row.pt.y;
1369     if (y < yd)
1370       break;
1371     pLast = p;
1372     yprev = y;
1373   } while(1);
1374   
1375   pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1376   ME_UpdateSelection(editor, &tmp_curs);
1377   if (yprev < editor->sizeWindow.cy)
1378   {
1379     ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
1380     ME_Repaint(editor);
1381   }
1382   else 
1383   {
1384     ME_ScrollUp(editor, ys-yprev);
1385   }
1386   assert(pCursor->pRun);
1387   assert(pCursor->pRun->type == diRun);
1388 }
1389 
1390 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount 
1391    of pixels, even if it makes the scroll bar position exceed its normal maximum.
1392    In such a situation, clicking the scrollbar restores its position back to the
1393    normal range (ie. sets it to (doclength-screenheight)). */
1394 
1395 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1396 {
1397   ME_DisplayItem *pRun = pCursor->pRun;
1398   ME_DisplayItem *pLast, *p;
1399   int x, y, ys, yd, yp, yprev;
1400   ME_Cursor tmp_curs = *pCursor;
1401   
1402   x = ME_GetXForArrow(editor, pCursor);
1403   if (!pCursor->nOffset && editor->bCaretAtEnd)
1404     pRun = ME_FindItemBack(pRun, diRun);
1405   
1406   p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1407   assert(p->type == diStartRow);
1408   yp = ME_FindItemBack(p, diParagraph)->member.para.pt.y;
1409   yprev = ys = y = yp + p->member.row.pt.y;
1410   yd = y + editor->sizeWindow.cy;
1411   pLast = p;
1412   
1413   do {
1414     p = ME_FindItemFwd(p, diStartRowOrParagraph);
1415     if (!p)
1416       break;
1417     if (p->type == diParagraph) {
1418       yp = p->member.para.pt.y;
1419       continue;
1420     }
1421     y = yp + p->member.row.pt.y;
1422     if (y >= yd)
1423       break;
1424     pLast = p;
1425     yprev = y;
1426   } while(1);
1427   
1428   pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1429   ME_UpdateSelection(editor, &tmp_curs);
1430   if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
1431   {
1432     ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1433     ME_Repaint(editor);
1434   }
1435   else 
1436   {
1437     ME_ScrollUp(editor,ys-yprev);
1438   }
1439   assert(pCursor->pRun);
1440   assert(pCursor->pRun->type == diRun);
1441 }
1442 
1443 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1444 {
1445   ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1446   ME_WrapMarkedParagraphs(editor);
1447   if (pRow) {
1448     ME_DisplayItem *pRun;
1449     if (editor->bCaretAtEnd && !pCursor->nOffset) {
1450       pRow = ME_FindItemBack(pRow, diStartRow);
1451       if (!pRow)
1452         return;
1453     }
1454     pRun = ME_FindItemFwd(pRow, diRun);
1455     if (pRun) {
1456       pCursor->pRun = pRun;
1457       pCursor->nOffset = 0;
1458     }
1459   }
1460   editor->bCaretAtEnd = FALSE;
1461 }
1462 
1463 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1464 {
1465   ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1466   if (pRow) {
1467     ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1468     if (pRun) {
1469       pCursor->pRun = pRun;
1470       pCursor->nOffset = 0;
1471     }
1472   }
1473 }
1474 
1475 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1476 {
1477   ME_DisplayItem *pRow;
1478   
1479   if (editor->bCaretAtEnd && !pCursor->nOffset)
1480     return;
1481   
1482   pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1483   assert(pRow);
1484   if (pRow->type == diStartRow) {
1485     /* FIXME WTF was I thinking about here ? */
1486     ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1487     assert(pRun);
1488     pCursor->pRun = pRun;
1489     pCursor->nOffset = 0;
1490     editor->bCaretAtEnd = 1;
1491     return;
1492   }
1493   pCursor->pRun = ME_FindItemBack(pRow, diRun);
1494   assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1495   pCursor->nOffset = 0;
1496   editor->bCaretAtEnd = FALSE;
1497 }
1498       
1499 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1500 {
1501   ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1502   assert(p);
1503   p = ME_FindItemBack(p, diRun);
1504   assert(p);
1505   assert(p->member.run.nFlags & MERF_ENDPARA);
1506   pCursor->pRun = p;
1507   pCursor->nOffset = 0;
1508   editor->bCaretAtEnd = FALSE;
1509 }
1510 
1511 BOOL ME_IsSelection(ME_TextEditor *editor)
1512 {
1513   return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1514 }
1515 
1516 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1517 {
1518   int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1519   
1520   if (cdir*dir>0)
1521     return 0;
1522   else
1523     return 1;
1524 }
1525 
1526 BOOL ME_UpdateSelection(ME_TextEditor *editor, const ME_Cursor *pTempCursor)
1527 {
1528   ME_Cursor old_anchor = editor->pCursors[1];
1529   
1530   if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1531   {
1532     /* any selection was present ? if so, it's no more, repaint ! */
1533     editor->pCursors[1] = editor->pCursors[0];
1534     if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1535       return TRUE;
1536     }
1537     return FALSE;
1538   }
1539   else
1540   {
1541     if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1542     {
1543       editor->pCursors[1] = *pTempCursor;
1544       return TRUE;
1545     }
1546   }
1547 
1548   ME_Repaint(editor);
1549   return TRUE;
1550 }
1551 
1552 void ME_DeleteSelection(ME_TextEditor *editor)
1553 {
1554   int from, to;
1555   ME_GetSelection(editor, &from, &to);
1556   ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1557 }
1558 
1559 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1560 {
1561   return ME_GetInsertStyle(editor, 0);
1562 }
1563 
1564 void ME_SendSelChange(ME_TextEditor *editor)
1565 {
1566   SELCHANGE sc;
1567 
1568   if (!(editor->nEventMask & ENM_SELCHANGE))
1569     return;
1570   
1571   sc.nmhdr.hwndFrom = editor->hWnd;
1572   sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1573   sc.nmhdr.code = EN_SELCHANGE;
1574   SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1575   sc.seltyp = SEL_EMPTY;
1576   if (sc.chrg.cpMin != sc.chrg.cpMax)
1577     sc.seltyp |= SEL_TEXT;
1578   if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1579     sc.seltyp |= SEL_MULTICHAR;
1580   TRACE("cpMin=%d cpMax=%d seltyp=%d (%s %s)\n",
1581     sc.chrg.cpMin, sc.chrg.cpMax, sc.seltyp,
1582     (sc.seltyp & SEL_TEXT) ? "SEL_TEXT" : "",
1583     (sc.seltyp & SEL_MULTICHAR) ? "SEL_MULTICHAR" : "");
1584   if (sc.chrg.cpMin != editor->notified_cr.cpMin || sc.chrg.cpMax != editor->notified_cr.cpMax)
1585   {
1586     ME_ClearTempStyle(editor);
1587 
1588     editor->notified_cr = sc.chrg;
1589     SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1590   }
1591 }
1592 
1593 BOOL
1594 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1595 {
1596   int nCursor = 0;
1597   ME_Cursor *p = &editor->pCursors[nCursor];
1598   ME_Cursor tmp_curs = *p;
1599   BOOL success = FALSE;
1600   
1601   ME_CheckCharOffsets(editor);
1602   switch(nVKey) {
1603     case VK_LEFT:
1604       editor->bCaretAtEnd = 0;
1605       if (ctrl)
1606         success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1607       else
1608         success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1609       break;
1610     case VK_RIGHT:
1611       editor->bCaretAtEnd = 0;
1612       if (ctrl)
1613         success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1614       else
1615         success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1616       break;
1617     case VK_UP:
1618       ME_MoveCursorLines(editor, &tmp_curs, -1);
1619       break;
1620     case VK_DOWN:
1621       ME_MoveCursorLines(editor, &tmp_curs, +1);
1622       break;
1623     case VK_PRIOR:
1624       ME_ArrowPageUp(editor, &tmp_curs);
1625       break;
1626     case VK_NEXT:
1627       ME_ArrowPageDown(editor, &tmp_curs);
1628       break;
1629     case VK_HOME: {
1630       if (ctrl)
1631         ME_ArrowCtrlHome(editor, &tmp_curs);
1632       else
1633         ME_ArrowHome(editor, &tmp_curs);
1634       editor->bCaretAtEnd = 0;
1635       break;
1636     }
1637     case VK_END: 
1638       if (ctrl)
1639         ME_ArrowCtrlEnd(editor, &tmp_curs);
1640       else
1641         ME_ArrowEnd(editor, &tmp_curs);
1642       break;
1643   }
1644   
1645   if (!extend)
1646     editor->pCursors[1] = tmp_curs;
1647   *p = tmp_curs;
1648   
1649   ME_InvalidateSelection(editor);
1650   ME_Repaint(editor);
1651   HideCaret(editor->hWnd);
1652   ME_EnsureVisible(editor, tmp_curs.pRun); 
1653   ME_ShowCaret(editor);
1654   ME_SendSelChange(editor);
1655   return success;
1656 }
1657 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.