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

~ [ 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.