Prevent crash when no URL is specified.
[wine] / dlls / riched20 / editor.c
1 /*
2  * RichEdit - functions dealing with editor object
3  *
4  * Copyright 2004 by Krzysztof Foltman
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /* 
22   API implementation status:
23   
24   Messages (ANSI versions not done yet)
25   - EM_AUTOURLDETECT 2.0
26   - EM_CANPASTE
27   + EM_CANREDO 2.0
28   + EM_CANUNDO
29   - EM_CHARFROMPOS
30   - EM_DISPLAYBAND
31   + EM_EMPTYUNDOBUFFER
32   + EM_EXGETSEL
33   - EM_EXLIMITTEXT
34   - EM_EXLINEFROMCHAR
35   + EM_EXSETSEL
36   - EM_FINDTEXT
37   - EM_FINDTEXTEX
38   - EM_FINDWORDBREAK
39   - EM_FMTLINES
40   - EM_FORMATRANGE
41   - EM_GETCHARFORMAT (partly done)
42   + EM_GETEVENTMASK
43   - EM_GETFIRSTVISIBLELINE
44   - EM_GETIMECOLOR 1.0asian
45   - EM_GETIMECOMPMODE 2.0
46   - EM_GETIMEOPTIONS 1.0asian
47   - EM_GETIMESTATUS
48   - EM_GETLANGOPTIONS 2.0
49   - EM_GETLIMITTEXT
50   - EM_GETLINE        
51   - EM_GETLINECOUNT   returns number of rows, not of paragraphs
52   + EM_GETMODIFY
53   - EM_GETOLEINTERFACE
54   - EM_GETOPTIONS
55   + EM_GETPARAFORMAT
56   - EM_GETPUNCTUATION 1.0asian
57   - EM_GETRECT
58   - EM_GETREDONAME 2.0
59   + EM_GETSEL
60   + EM_GETSELTEXT (ANSI&Unicode)
61 ! - EM_GETTHUMB
62   - EM_GETTEXTMODE 2.0
63 ? + EM_GETTEXTRANGE (ANSI&Unicode)
64   - EM_GETUNDONAME
65   - EM_GETWORDBREAKPROC
66   - EM_GETWORDBREAKPROCEX
67   - EM_GETWORDWRAPMODE 1.0asian
68   - EM_HIDESELECTION
69   - EM_LIMITTEXT
70   - EM_LINEFROMCHAR
71   - EM_LINEINDEX
72   - EM_LINELENGTH
73   - EM_LINESCROLL
74   - EM_PASTESPECIAL
75   - EM_POSFROMCHARS
76   - EM_REDO 2.0
77   - EM_REQUESTRESIZE
78   + EM_REPLACESEL (proper style?) ANSI&Unicode
79   - EM_SCROLL
80   - EM_SCROLLCARET
81   - EM_SELECTIONTYPE
82   + EM_SETBKGNDCOLOR
83   - EM_SETCHARFORMAT (partly done, no ANSI)
84   + EM_SETEVENTMASK (few notifications supported)
85   - EM_SETIMECOLOR 1.0asian
86   - EM_SETIMEOPTIONS 1.0asian
87   - EM_SETLANGOPTIONS 2.0
88   - EM_SETLIMITTEXT
89   + EM_SETMODIFY (not sure if implementation is correct)
90   - EM_SETOLECALLBACK
91   - EM_SETOPTIONS
92   + EM_SETPARAFORMAT
93   - EM_SETPUNCTUATION 1.0asian
94   + EM_SETREADONLY no beep on modification attempt
95   - EM_SETRECT
96   - EM_SETRECTNP (EM_SETRECT without repainting) - not supported in RICHEDIT
97   + EM_SETSEL
98   - EM_SETTARGETDEVICE
99   - EM_SETTEXTMODE 2.0
100   - EM_SETUNDOLIMIT 2.0
101   - EM_SETWORDBREAKPROC
102   - EM_SETWORDBREAKPROCEX
103   - EM_SETWORDWRAPMODE 1.0asian
104   - EM_STOPGROUPTYPING 2.0
105   - EM_STREAMIN
106   - EM_STREAMOUT
107   - EM_UNDO
108   + WM_CHAR
109   + WM_CLEAR
110   + WM_COPY
111   + WM_CUT
112   + WM_GETDLGCODE (the current implementation is incomplete)
113   + WM_GETTEXT (ANSI&Unicode)
114   + WM_GETTEXTLENGTH (ANSI version sucks)
115   + WM_PASTE
116   - WM_SETFONT
117   + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
118   - WM_STYLECHANGING
119   - WM_STYLECHANGED (things like read-only flag)
120   - WM_UNICHAR
121   
122   Notifications
123   
124   * EN_CHANGE (sent from the wrong place)
125   - EN_CORRECTTEXT
126   - EN_DROPFILES
127   - EN_ERRSPACE
128   - EN_HSCROLL
129   - EN_IMECHANGE
130   + EN_KILLFOCUS
131   - EN_LINK
132   - EN_MAXTEXT
133   - EN_MSGFILTER
134   - EN_OLEOPFAILED
135   - EN_PROTECTED
136   - EN_REQUESTRESIZE
137   - EN_SAVECLIPBOARD
138   + EN_SELCHANGE 
139   + EN_SETFOCUS
140   - EN_STOPNOUNDO
141   * EN_UPDATE (sent from the wrong place)
142   - EN_VSCROLL
143   
144   Styles
145   
146   - ES_AUTOHSCROLL
147   - ES_AUTOVSCROLL
148   - ES_CENTER
149   - ES_DISABLENOSCROLL (scrollbar is always visible)
150   - ES_EX_NOCALLOLEINIT
151   - ES_LEFT
152   - ES_MULTILINE (currently single line controls aren't supported)
153   - ES_NOIME
154   - ES_READONLY (I'm not sure if beeping is the proper behaviour)
155   - ES_RIGHT
156   - ES_SAVESEL
157   - ES_SELFIME
158   - ES_SUNKEN
159   - ES_VERTICAL
160   - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
161   - WS_SETFONT
162   - WS_HSCROLL
163   - WS_VSCROLL
164 */
165
166 /*
167  * RICHED20 TODO (incomplete):
168  *
169  * - font caching
170  * - add remaining CHARFORMAT/PARAFORMAT fields
171  * - right/center align should strip spaces from the beginning
172  * - more advanced navigation (Ctrl-arrows, PageUp/PageDn)
173  * - tabs
174  * - pictures (not just smiling faces that lack API support ;-) )
175  * - OLE objects
176  * - calculate heights of pictures (half-done)
177  * - EM_STREAMIN/EM_STREAMOUT
178  * - horizontal scrolling (not even started)
179  * - fix scrollbars and refresh (it sucks bigtime)
180  * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
181  * - should remember maximum row width for wrap hysteresis
182  * - find/replace
183  * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
184  * - italic cursor with italic fonts
185  * - IME
186  * - most notifications aren't sent at all (the most important ones are)
187  * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
188  * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
189  * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
190  * - bugs in end-of-text handling (the gray bar) could get me in jail ;-)
191  * - determination of row size
192  * - end-of-paragraph marks should be of reasonable size
193  *
194  * Bugs that are probably fixed, but not so easy to verify:
195  * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
196  * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
197  * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
198  * - caret shouldn't be displayed when selection isn't empty
199  * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
200  * - undo for setting default format (done, might be buggy)
201  * - styles might be not released properly (looks like they work like charm, but who knows?
202  *
203  */
204
205 #include "editor.h"
206 #include "ole2.h"
207 #include "richole.h"
208 #include "winreg.h"
209 #define NO_SHLWAPI_STREAM 
210 #include "shlwapi.h"
211
212 #include "rtf.h"
213  
214 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
215
216 int me_debug = 0;
217 HANDLE me_heap = NULL;
218
219 ME_TextBuffer *ME_MakeText() {
220   
221   ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
222
223   ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
224   ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
225   
226   p1->prev = NULL;
227   p1->next = p2;
228   p2->prev = p1;
229   p2->next = NULL;
230   p1->member.para.next_para = p2;
231   p2->member.para.prev_para = p1;
232   p2->member.para.nCharOfs = 0;  
233   
234   buf->pFirst = p1;
235   buf->pLast = p2;
236   buf->pCharStyle = NULL;
237   
238   return buf;
239 }
240
241 #define STREAMIN_BUFFER_SIZE 4096 /* M$ compatibility */
242
243 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, EDITSTREAM *stream, ME_Style *style)
244 {
245   BYTE buffer[STREAMIN_BUFFER_SIZE+1];
246   WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
247   
248   TRACE("%08lx %p\n", dwFormat, stream);
249   stream->dwError = 0;
250   
251   do {
252     long nDataSize = 0, nWideChars = 0;
253     stream->dwError = stream->pfnCallback(stream->dwCookie, 
254       (dwFormat & SF_UNICODE ? (BYTE *)wszText : buffer), 
255       STREAMIN_BUFFER_SIZE, &nDataSize);
256     
257     if (stream->dwError)
258       break;
259     if (!nDataSize)
260       break;
261       
262     if (!(dwFormat & SF_UNICODE))
263     {
264       /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
265       nWideChars = MultiByteToWideChar(CP_ACP, 0, buffer, nDataSize, wszText, STREAMIN_BUFFER_SIZE);
266     }
267     else
268       nWideChars = nDataSize>>1;
269     ME_InsertTextFromCursor(editor, 0, wszText, nWideChars, style);
270     if (nDataSize<STREAMIN_BUFFER_SIZE)
271       break;
272   } while(1);
273   ME_CommitUndo(editor);
274   ME_Repaint(editor);
275   return 0;
276 }
277
278 void ME_RTFCharAttrHook(RTF_Info *info)
279 {
280   CHARFORMAT2W fmt;
281   fmt.cbSize = sizeof(fmt);
282   fmt.dwMask = 0;
283   
284   switch(info->rtfMinor)
285   {
286     case rtfPlain:
287       /* FIXME add more flags once they're implemented */
288       fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
289       fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
290       fmt.yHeight = 12*20; /* 12pt */
291       fmt.wWeight = 400;
292       break;
293     case rtfBold:
294       fmt.dwMask = CFM_BOLD;
295       fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
296       break;
297     case rtfItalic:
298       fmt.dwMask = CFM_ITALIC;
299       fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
300       break;
301     case rtfUnderline:
302       fmt.dwMask = CFM_UNDERLINE;
303       fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
304       break;
305     case rtfStrikeThru:
306       fmt.dwMask = CFM_STRIKEOUT;
307       fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
308       break;
309     case rtfBackColor:
310       fmt.dwMask = CFM_BACKCOLOR;
311       fmt.dwEffects = 0;
312       if (info->rtfParam == 0)
313         fmt.dwEffects = CFE_AUTOBACKCOLOR;
314       else if (info->rtfParam != rtfNoParam)
315       {
316         RTFColor *c = RTFGetColor(info, info->rtfParam);
317         fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
318       }
319       break;
320     case rtfForeColor:
321       fmt.dwMask = CFM_COLOR;
322       fmt.dwEffects = 0;
323       if (info->rtfParam == 0)
324         fmt.dwEffects = CFE_AUTOCOLOR;
325       else if (info->rtfParam != rtfNoParam)
326       {
327         RTFColor *c = RTFGetColor(info, info->rtfParam);
328         fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
329       }
330       break;
331     case rtfFontNum:
332       if (info->rtfParam != rtfNoParam)
333       {
334         RTFFont *f = RTFGetFont(info, info->rtfParam);
335         if (f)
336         {
337           MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
338           fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
339           fmt.dwMask = CFM_FACE;
340         }
341       }
342       break;
343     case rtfFontSize:
344       fmt.dwMask = CFM_SIZE;
345       if (info->rtfParam != rtfNoParam)
346         fmt.yHeight = info->rtfParam*10;
347       break;
348   }
349   if (fmt.dwMask) {
350     ME_Style *style2;
351     RTFFlushOutputBuffer(info);
352     /* FIXME too slow ? how come ? */
353     style2 = ME_ApplyStyle(info->style, &fmt);
354     ME_ReleaseStyle(info->style);
355     info->style = style2;
356   }
357 }
358
359 void ME_RTFParAttrHook(RTF_Info *info)
360 {
361   PARAFORMAT2 fmt;
362   fmt.cbSize = sizeof(fmt);
363   fmt.dwMask = 0;
364   
365   switch(info->rtfMinor)
366   {
367   case rtfParDef: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
368     fmt.dwMask = PFM_ALIGNMENT;
369     fmt.wAlignment = PFA_LEFT;
370     break;
371   case rtfQuadLeft:
372   case rtfQuadJust:
373     fmt.dwMask = PFM_ALIGNMENT;
374     fmt.wAlignment = PFA_LEFT;
375     break;
376   case rtfQuadRight:
377     fmt.dwMask = PFM_ALIGNMENT;
378     fmt.wAlignment = PFA_RIGHT;
379     break;
380   case rtfQuadCenter:
381     fmt.dwMask = PFM_ALIGNMENT;
382     fmt.wAlignment = PFA_CENTER;
383     break;
384   }  
385   if (fmt.dwMask) {
386     RTFFlushOutputBuffer(info);
387     /* FIXME too slow ? how come ?*/
388     ME_SetSelectionParaFormat(info->editor, &fmt);
389   }
390 }
391
392 void ME_RTFReadHook(RTF_Info *info) {
393   switch(info->rtfClass)
394   {
395     case rtfGroup:
396       switch(info->rtfMajor)
397       {
398         case rtfBeginGroup:
399           if (info->stackTop < maxStack) {
400             memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
401             info->stack[info->stackTop].codePage = info->codePage;
402             info->stack[info->stackTop].unicodeLength = info->unicodeLength;
403           }
404           info->stackTop++;
405           break;
406         case rtfEndGroup:
407         {
408           ME_Style *s;
409           RTFFlushOutputBuffer(info);
410           info->stackTop--;
411           /* FIXME too slow ? how come ? */
412           s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
413           ME_ReleaseStyle(info->style);
414           info->style = s;
415           info->codePage = info->stack[info->stackTop].codePage;
416           info->unicodeLength = info->stack[info->stackTop].unicodeLength;
417           break;
418         }
419       }
420       break;
421     case rtfControl:
422       switch(info->rtfMajor)
423       {
424         case rtfCharAttr:
425           ME_RTFCharAttrHook(info);
426           break;
427         case rtfParAttr:
428           ME_RTFParAttrHook(info);
429           break;
430       }
431       break;
432   }
433 }
434
435 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
436 {
437   RTF_Info parser;
438   ME_Style *style;
439   int from, to, to2, nUndoMode;
440   ME_UndoItem *pUI;
441   int nEventMask = editor->nEventMask;
442
443   TRACE("%p %p\n", stream, editor->hWnd);
444   editor->nEventMask = 0;
445   
446   ME_GetSelection(editor, &from, &to);
447   if (format & SFF_SELECTION) {
448     style = ME_GetSelectionInsertStyle(editor);
449
450     ME_InternalDeleteText(editor, from, to-from);
451   }
452   else {
453     style = editor->pBuffer->pDefaultStyle;
454     ME_AddRefStyle(style);
455     SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);    
456     ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
457     from = to = 0;
458     ME_ClearTempStyle(editor);
459     /* FIXME restore default paragraph formatting ! */
460   }
461   
462   nUndoMode = editor->nUndoMode;
463   editor->nUndoMode = umIgnore;
464   if (format & SF_RTF) {
465     /* setup the RTF parser */
466     memset(&parser, 0, sizeof parser);
467     RTFSetEditStream(&parser, stream);
468     parser.rtfFormat = format&(SF_TEXT|SF_RTF);
469     parser.hwndEdit = editor->hWnd;
470     parser.editor = editor;
471     parser.style = style;
472     WriterInit(&parser);
473     RTFInit(&parser);
474     RTFSetReadHook(&parser, ME_RTFReadHook);
475     BeginFile(&parser);
476   
477     /* do the parsing */
478     RTFRead(&parser);
479     RTFFlushOutputBuffer(&parser);
480     RTFDestroy(&parser);
481
482     style = parser.style;
483   }
484   else if (format & SF_TEXT)
485     ME_StreamInText(editor, format, stream, style);
486   else
487     ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
488   ME_GetSelection(editor, &to, &to2);
489   /* put the cursor at the top */
490   if (!(format & SFF_SELECTION))
491     SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
492   else
493   {
494     /* FIXME where to put cursor now ? */
495   }
496   
497   editor->nUndoMode = nUndoMode;
498   pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
499   TRACE("from %d to %d\n", from, to);
500   if (pUI && from < to)
501   {
502     pUI->nStart = from;
503     pUI->nLen = to-from;
504   }
505   ME_CommitUndo(editor);
506   ME_ReleaseStyle(style); 
507   editor->nEventMask = nEventMask;
508   InvalidateRect(editor->hWnd, NULL, TRUE);
509   ME_UpdateRepaint(editor);
510   if (!(format & SFF_SELECTION)) {
511     ME_ClearTempStyle(editor);
512   }
513   ME_MoveCaret(editor);
514   ME_SendSelChange(editor);
515
516   return 0;
517 }
518
519
520 ME_DisplayItem *
521 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
522 {
523   ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
524   
525   while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
526     item = ME_FindItemFwd(item, diParagraph);
527
528   if (!item)
529     return item;
530
531   nOffset -= item->member.para.nCharOfs;
532   if (nItemType == diParagraph) {
533     if (nItemOffset)
534       *nItemOffset = nOffset;
535     return item;
536   }
537   
538   do {
539     item = ME_FindItemFwd(item, diRun);
540   } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
541   if (item) {
542     nOffset -= item->member.run.nCharOfs;
543     if (nItemOffset)
544       *nItemOffset = nOffset;
545   }
546   return item;
547 }
548
549
550 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
551   ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
552   HDC hDC;
553   int i;
554   ed->hWnd = hWnd;
555   ed->pBuffer = ME_MakeText();
556   hDC = GetDC(hWnd);
557   ME_MakeFirstParagraph(hDC, ed->pBuffer);
558   ReleaseDC(hWnd, hDC);
559   ed->bCaretShown = FALSE;
560   ed->nCursors = 3;
561   ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
562   ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
563   ed->pCursors[0].nOffset = 0;
564   ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
565   ed->pCursors[1].nOffset = 0;
566   ed->nLastTotalLength = ed->nTotalLength = 0;
567   ed->nUDArrowX = -1;
568   ed->nSequence = 0;
569   ed->rgbBackColor = -1;
570   ed->bCaretAtEnd = FALSE;
571   ed->nEventMask = 0;
572   ed->nModifyStep = 0;
573   ed->pUndoStack = ed->pRedoStack = NULL;
574   ed->nUndoMode = umAddToUndo;
575   ed->nParagraphs = 1;
576   ed->nLastSelStart = ed->nLastSelEnd = 0;
577   ed->nScrollPosY = 0;
578   for (i=0; i<HFONT_CACHE_SIZE; i++)
579   {
580     ed->pFontCache[i].nRefs = 0;
581     ed->pFontCache[i].nAge = 0;
582     ed->pFontCache[i].hFont = NULL;
583   }
584   ME_CheckCharOffsets(ed);
585   return ed;
586 }
587
588 typedef struct tagME_GlobalDestStruct
589 {
590   HGLOBAL hData;
591   int nLength;
592 } ME_GlobalDestStruct;
593
594 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
595 {
596   ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
597   int nMaxSize;
598   BYTE *pDest;
599   
600   nMaxSize = GlobalSize(pData->hData);
601   if (pData->nLength+cb+1 >= cb)
602   {
603     /* round up to 2^17 */
604     int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
605     pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
606   }
607   pDest = (BYTE *)GlobalLock(pData->hData);
608   memcpy(pDest + pData->nLength, lpBuff, cb);
609   pData->nLength += cb;
610   pDest[pData->nLength] = '\0';
611   GlobalUnlock(pData->hData);
612   *pcb = cb;
613   
614   return 0;
615 }
616
617 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
618 {
619   ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
620   int i;
621   WORD *pSrc, *pDest;
622   
623   cb = cb >> 1;
624   pDest = (WORD *)lpBuff;
625   pSrc = (WORD *)GlobalLock(pData->hData);
626   for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
627     pDest[i] = pSrc[pData->nLength+i];
628   }    
629   pData->nLength += i;
630   *pcb = 2*i;
631   GlobalUnlock(pData->hData);
632   return 0;
633 }
634
635 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
636 {
637   ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
638   int i;
639   BYTE *pSrc, *pDest;
640   
641   pDest = lpBuff;
642   pSrc = (BYTE *)GlobalLock(pData->hData);
643   for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
644     pDest[i] = pSrc[pData->nLength+i];
645   }    
646   pData->nLength += i;
647   *pcb = i;
648   GlobalUnlock(pData->hData);
649   return 0;
650 }
651
652 void ME_DestroyEditor(ME_TextEditor *editor)
653 {
654   ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
655   ME_DisplayItem *p = pFirst, *pNext = NULL;
656   int i;
657   
658   ME_ClearTempStyle(editor);
659   ME_EmptyUndoStack(editor);
660   while(p) {
661     pNext = p->next;
662     ME_DestroyDisplayItem(p);    
663     p = pNext;
664   }
665   ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
666   for (i=0; i<HFONT_CACHE_SIZE; i++)
667   {
668     if (editor->pFontCache[i].hFont)
669       DeleteObject(editor->pFontCache[i].hFont);
670   }
671     
672   FREE_OBJ(editor);
673 }
674
675 #define UNSUPPORTED_MSG(e) \
676   case e: \
677     FIXME(#e ": stub\n"); \
678     return DefWindowProcW(hWnd, msg, wParam, lParam);
679
680 /******************************************************************
681  *        RichEditANSIWndProc (RICHED20.10)
682  */
683 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
684   HDC hDC;
685   PAINTSTRUCT ps;
686   SCROLLINFO si;
687   ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
688   TRACE("msg %d %08x %08lx\n", msg, wParam, lParam);
689   switch(msg) {
690   
691   UNSUPPORTED_MSG(EM_AUTOURLDETECT)
692   UNSUPPORTED_MSG(EM_CANPASTE)
693   UNSUPPORTED_MSG(EM_CHARFROMPOS)
694   UNSUPPORTED_MSG(EM_DISPLAYBAND)
695   UNSUPPORTED_MSG(EM_EXLIMITTEXT)
696   UNSUPPORTED_MSG(EM_EXLINEFROMCHAR)
697   UNSUPPORTED_MSG(EM_FINDTEXT)
698   UNSUPPORTED_MSG(EM_FINDTEXTEX)
699   UNSUPPORTED_MSG(EM_FINDWORDBREAK)
700   UNSUPPORTED_MSG(EM_FMTLINES)
701   UNSUPPORTED_MSG(EM_FORMATRANGE)
702   UNSUPPORTED_MSG(EM_GETFIRSTVISIBLELINE)
703   UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
704   /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
705   UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
706   UNSUPPORTED_MSG(EM_GETLIMITTEXT)
707   UNSUPPORTED_MSG(EM_GETLINE)
708   UNSUPPORTED_MSG(EM_GETLINECOUNT)
709   /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
710   UNSUPPORTED_MSG(EM_GETOPTIONS)
711   UNSUPPORTED_MSG(EM_GETRECT)
712   UNSUPPORTED_MSG(EM_GETREDONAME)
713   UNSUPPORTED_MSG(EM_GETTEXTMODE)
714   UNSUPPORTED_MSG(EM_GETUNDONAME)
715   UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
716   UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
717   UNSUPPORTED_MSG(EM_HIDESELECTION)
718   UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
719   UNSUPPORTED_MSG(EM_LINEFROMCHAR)
720   UNSUPPORTED_MSG(EM_LINEINDEX)
721   UNSUPPORTED_MSG(EM_LINELENGTH)
722   UNSUPPORTED_MSG(EM_LINESCROLL)
723   UNSUPPORTED_MSG(EM_PASTESPECIAL)
724 /*  UNSUPPORTED_MSG(EM_POSFROMCHARS) missing in Wine headers */
725   UNSUPPORTED_MSG(EM_REQUESTRESIZE)
726   UNSUPPORTED_MSG(EM_SCROLL)
727   UNSUPPORTED_MSG(EM_SCROLLCARET)
728   UNSUPPORTED_MSG(EM_SELECTIONTYPE)
729   UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
730   UNSUPPORTED_MSG(EM_SETOLECALLBACK)
731   UNSUPPORTED_MSG(EM_SETOPTIONS)
732   UNSUPPORTED_MSG(EM_SETRECT)
733   UNSUPPORTED_MSG(EM_SETRECTNP)
734   UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
735   UNSUPPORTED_MSG(EM_SETTEXTMODE)
736   UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
737   UNSUPPORTED_MSG(EM_SETWORDBREAKPROC)
738   UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
739   UNSUPPORTED_MSG(WM_SETFONT)
740   UNSUPPORTED_MSG(WM_STYLECHANGING)
741   UNSUPPORTED_MSG(WM_STYLECHANGED)
742 /*  UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
743     
744 /* Messages specific to Richedit controls */
745   
746   case EM_STREAMIN:
747    return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
748   case EM_STREAMOUT:
749    return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
750   case WM_GETDLGCODE:
751   {
752     UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
753     if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
754       code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
755     return code;
756   }
757   case WM_NCCREATE:
758   {
759     CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
760     editor = ME_MakeEditor(hWnd);
761     SetWindowLongW(hWnd, 0, (long)editor);
762     pcs = 0; /* ignore */
763     return TRUE;
764   }
765   case EM_EMPTYUNDOBUFFER:
766     ME_EmptyUndoStack(editor);
767     return 0;
768   case EM_GETSEL:
769   {
770     ME_GetSelection(editor, (int *)wParam, (int *)lParam);
771     if (!((wParam|lParam) & 0xFFFF0000))
772       return (lParam<<16)|wParam;
773     return -1;
774   }
775   case EM_EXGETSEL:
776   {
777     CHARRANGE *pRange = (CHARRANGE *)lParam;
778     ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
779     return 0;
780   }
781   case EM_CANUNDO:
782     return editor->pUndoStack != NULL;
783   case EM_CANREDO:
784     return editor->pRedoStack != NULL;
785   case EM_UNDO:
786     ME_Undo(editor);
787     return 0;
788   case EM_REDO:
789     ME_Redo(editor);
790     return 0;
791   case EM_SETSEL:
792   {
793     ME_SetSelection(editor, wParam, lParam);
794     ME_Repaint(editor);
795     ME_SendSelChange(editor);
796     return 0;
797   }
798   case EM_EXSETSEL:
799   {
800     CHARRANGE *pRange = (CHARRANGE *)lParam;
801     ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
802     /* FIXME optimize */
803     ME_Repaint(editor);
804     ME_SendSelChange(editor);
805     return 0;
806   }
807   case EM_SETBKGNDCOLOR:
808   {
809     LRESULT lColor = ME_GetBackColor(editor);
810     if (wParam)
811       editor->rgbBackColor = -1;
812     else
813       editor->rgbBackColor = lParam; 
814     InvalidateRect(hWnd, NULL, TRUE);
815     UpdateWindow(hWnd);
816     return lColor;
817   }
818   case EM_GETMODIFY:
819     return editor->nModifyStep == 0 ? 0 : 1;
820   case EM_SETMODIFY:
821   {
822     if (wParam)
823       editor->nModifyStep = 0x80000000;
824     else
825       editor->nModifyStep = 0;
826     
827     return 0;
828   }
829   case EM_SETREADONLY:
830   {
831     long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
832     if (wParam)
833       nStyle |= ES_READONLY;
834     else
835       nStyle &= ~ES_READONLY;
836     SetWindowLongW(hWnd, GWL_STYLE, nStyle);
837     ME_Repaint(editor);
838     return 0;
839   }
840   case EM_SETEVENTMASK:
841     editor->nEventMask = lParam;
842     return 0;
843   case EM_GETEVENTMASK:
844     return editor->nEventMask;
845   case EM_SETCHARFORMAT:
846   {
847     CHARFORMAT2W buf, *p;
848     BOOL bRepaint = TRUE;
849     p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
850     if (!wParam)
851       ME_SetDefaultCharFormat(editor, p);
852     else if (wParam == (SCF_WORD | SCF_SELECTION))
853       FIXME("word selection not supported\n");
854     else if (wParam == SCF_ALL)
855       ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
856     else {
857       int from, to;
858       ME_GetSelection(editor, &from, &to);
859       bRepaint = (from != to);
860       ME_SetSelectionCharFormat(editor, p);
861     }
862     ME_CommitUndo(editor);
863     if (bRepaint)
864       ME_UpdateRepaint(editor);
865     return 0;
866   }
867   case EM_GETCHARFORMAT:
868   {
869     CHARFORMAT2W tmp;
870     tmp.cbSize = sizeof(tmp);
871     if (!wParam)
872       ME_GetDefaultCharFormat(editor, &tmp);
873     else
874       ME_GetSelectionCharFormat(editor, &tmp);
875     ME_CopyToCFAny((CHARFORMAT2W *)lParam, &tmp);
876     return 0;
877   }
878   case EM_SETPARAFORMAT:
879     ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
880     ME_UpdateRepaint(editor);
881     ME_CommitUndo(editor);
882     return 0;
883   case EM_GETPARAFORMAT:
884     ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
885     return 0;
886   case WM_CLEAR:
887   {
888     int from, to;
889     ME_GetSelection(editor, &from, &to);
890     ME_InternalDeleteText(editor, from, to-from);
891     ME_CommitUndo(editor);
892     ME_UpdateRepaint(editor);
893     return 0;
894   }
895   case EM_REPLACESEL:
896   {
897     int from, to;
898     ME_Style *style;
899     LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
900     size_t len = lstrlenW(wszText);
901     TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
902     
903     ME_GetSelection(editor, &from, &to);
904     style = ME_GetSelectionInsertStyle(editor);
905     ME_InternalDeleteText(editor, from, to-from);
906     ME_InsertTextFromCursor(editor, 0, wszText, len, style);
907     ME_ReleaseStyle(style);
908     ME_EndToUnicode(hWnd, wszText);
909     /* drop temporary style if line end */
910     /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */  
911     if (len>0 && wszText[len-1] == '\n')
912       ME_ClearTempStyle(editor);
913       
914     ME_CommitUndo(editor);
915     if (!wParam)
916       ME_EmptyUndoStack(editor);
917     ME_UpdateRepaint(editor);
918     return 0;
919   }
920   case WM_SETTEXT:
921   {
922     LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
923     TRACE("WM_SETTEXT - %s\n", (char *)(wszText)); /* debugstr_w() */
924     ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
925     /* uses default style! */
926     ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
927     ME_EndToUnicode(hWnd, wszText);
928     ME_CommitUndo(editor);
929     ME_EmptyUndoStack(editor);
930     ME_UpdateRepaint(editor);
931     return 0;
932   }
933   case WM_PASTE:
934   {    
935     DWORD dwFormat = 0;
936     EDITSTREAM es;
937     ME_GlobalDestStruct gds;
938     UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
939     UINT cf = 0;
940
941     if (!OpenClipboard(hWnd))
942       return 0;
943     if (IsClipboardFormatAvailable(nRTFFormat))
944       cf = nRTFFormat, dwFormat = SF_RTF;
945     else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
946       cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
947     else
948       return 0;
949
950     gds.hData = GetClipboardData(cf);
951     gds.nLength = 0;
952     es.dwCookie = (DWORD)&gds;
953     es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
954     SendMessageW(hWnd, EM_STREAMIN, dwFormat|SFF_SELECTION, (LPARAM)&es);
955     
956     CloseClipboard();
957     return 0;
958   }
959   case WM_CUT:
960   case WM_COPY:
961   {
962     int from, to, pars;
963     WCHAR *data;
964     HANDLE hData;
965     EDITSTREAM es;
966     ME_GlobalDestStruct gds;
967     
968     if (!OpenClipboard(hWnd))
969       return 0;
970       
971     EmptyClipboard();
972     ME_GetSelection(editor, &from, &to);
973     pars = ME_CountParagraphsBetween(editor, from, to);
974     hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
975     data = (WCHAR *)GlobalLock(hData);
976     ME_GetTextW(editor, data, from, to-from, TRUE);
977     GlobalUnlock(hData);
978
979     gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
980     gds.nLength = 0;
981     es.dwCookie = (DWORD)&gds;
982     es.pfnCallback = ME_AppendToHGLOBAL;
983     SendMessageW(hWnd, EM_STREAMOUT, SFF_SELECTION|SF_RTF, (LPARAM)&es);
984     GlobalReAlloc(gds.hData, gds.nLength+1, 0);
985     
986     SetClipboardData(CF_UNICODETEXT, hData);    
987     SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds.hData);
988     
989     CloseClipboard();
990     if (msg == WM_CUT)
991     {
992       ME_InternalDeleteText(editor, from, to-from);
993       ME_CommitUndo(editor);
994       ME_UpdateRepaint(editor);
995     }
996     return 0;
997   }
998   case WM_GETTEXTLENGTH:
999     return ME_GetTextLength(editor);
1000   case WM_GETTEXT:
1001   {
1002     TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1003     tr.chrg.cpMin = 0;
1004     tr.chrg.cpMax = wParam-1;
1005     tr.lpstrText = (WCHAR *)lParam;
1006     return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1007   }
1008   case EM_GETSELTEXT:
1009   {
1010     int from, to;
1011     TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1012     ME_GetSelection(editor, &from, &to);
1013     tr.chrg.cpMin = from;
1014     tr.chrg.cpMax = to;
1015     tr.lpstrText = (WCHAR *)lParam;
1016     return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1017   }
1018   case EM_GETTEXTRANGE:
1019   {
1020     TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
1021     if (IsWindowUnicode(hWnd))
1022       return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
1023     else
1024     {
1025       int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1026       WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1027       int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, FALSE);
1028       /* FIXME this is a potential security hole (buffer overrun) 
1029          if you know more about wchar->mbyte conversion please explain
1030       */
1031       WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1032       FREE_OBJ(p);
1033       return nChars;
1034     }
1035     return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
1036   }
1037   case WM_CREATE:
1038     ME_CommitUndo(editor);
1039     ME_WrapMarkedParagraphs(editor);
1040     ME_MoveCaret(editor);
1041     return 0;
1042   case WM_DESTROY:
1043     ME_DestroyEditor(editor);
1044     SetWindowLongW(hWnd, 0, 0);
1045     return 0;
1046   case WM_LBUTTONDOWN:
1047     SetFocus(hWnd);
1048     ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1049     SetCapture(hWnd);
1050     break;
1051   case WM_MOUSEMOVE:
1052     if (GetCapture() == hWnd)
1053       ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1054     break;
1055   case WM_LBUTTONUP:
1056     if (GetCapture() == hWnd)
1057       ReleaseCapture();
1058     break;
1059   case WM_PAINT:
1060     hDC = BeginPaint(hWnd, &ps);
1061     ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
1062     EndPaint(hWnd, &ps);
1063     break;
1064   case WM_SETFOCUS:
1065     ME_ShowCaret(editor);
1066     ME_SendOldNotify(editor, EN_SETFOCUS);
1067     return 0;
1068   case WM_KILLFOCUS:
1069     ME_HideCaret(editor);
1070     ME_SendOldNotify(editor, EN_KILLFOCUS);
1071     return 0;
1072   case WM_ERASEBKGND:
1073   {
1074     HDC hDC = (HDC)wParam;
1075     RECT rc;
1076     COLORREF rgbBG = ME_GetBackColor(editor);
1077     if (GetUpdateRect(hWnd,&rc,TRUE))
1078     {
1079       HBRUSH hbr = CreateSolidBrush(rgbBG);
1080       FillRect(hDC, &rc, hbr);
1081       DeleteObject(hbr);
1082     }
1083     return 1;
1084   }
1085   case WM_COMMAND:
1086     TRACE("editor wnd command = %d\n", LOWORD(wParam));
1087     return 0;
1088   case WM_KEYDOWN:
1089     if (ME_ArrowKey(editor, LOWORD(wParam), GetKeyState(VK_CONTROL)<0)) {
1090       ME_CommitUndo(editor);
1091       ME_EnsureVisible(editor, editor->pCursors[0].pRun);
1092       HideCaret(hWnd);
1093       ME_MoveCaret(editor);
1094       ShowCaret(hWnd);
1095       return 0;
1096     }
1097     if (GetKeyState(VK_CONTROL)<0)
1098     {
1099       if (LOWORD(wParam)=='W')
1100       {
1101         CHARFORMAT2W chf;
1102         char buf[2048];
1103         ME_GetSelectionCharFormat(editor, &chf);
1104         ME_DumpStyleToBuf(&chf, buf);
1105         MessageBoxA(NULL, buf, "Style dump", MB_OK);
1106       }
1107       if (LOWORD(wParam)=='Q')
1108       {
1109         ME_CheckCharOffsets(editor);
1110       }
1111     }
1112     goto do_default;
1113   case WM_CHAR: 
1114   {
1115     WCHAR wstr;
1116     if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
1117       MessageBeep(MB_ICONERROR);
1118       return 0; /* FIXME really 0 ? */
1119     }
1120     wstr = LOWORD(wParam);
1121     if (((unsigned)wstr)>=' ' || wstr=='\r') {
1122       /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
1123       ME_Style *style = ME_GetInsertStyle(editor, 0);
1124       ME_SaveTempStyle(editor);
1125       ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
1126       ME_ReleaseStyle(style);
1127       ME_CommitUndo(editor);
1128       ME_UpdateRepaint(editor);
1129     }
1130     return 0;
1131   }
1132   case WM_VSCROLL: 
1133   {
1134     int nPos = editor->nScrollPosY;
1135     si.cbSize = sizeof(SCROLLINFO);
1136     si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
1137     GetScrollInfo(hWnd, SB_VERT, &si);
1138     switch(LOWORD(wParam)) {
1139     case SB_LINEUP:
1140       nPos -= 24; /* FIXME follow the original */
1141       if (nPos<0) nPos = 0;
1142       break;
1143     case SB_LINEDOWN:
1144     {
1145       int nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1146       nPos += 24; /* FIXME follow the original */
1147       if (nPos>=nEnd) nPos = nEnd;
1148       break;
1149     }
1150     case SB_PAGEUP:
1151       nPos -= editor->sizeWindow.cy;
1152       if (nPos<0) nPos = 0;
1153       break;
1154     case SB_PAGEDOWN:
1155       nPos += editor->sizeWindow.cy;
1156       if (nPos>=editor->nTotalLength) nPos = editor->nTotalLength-1;
1157       break;
1158     case SB_THUMBTRACK:
1159     case SB_THUMBPOSITION:
1160       nPos = si.nTrackPos;
1161       break;
1162     }
1163     if (nPos != editor->nScrollPosY) {
1164       ScrollWindow(hWnd, 0, editor->nScrollPosY-nPos, NULL, NULL);
1165       editor->nScrollPosY = nPos;
1166       SetScrollPos(hWnd, SB_VERT, nPos, FALSE);
1167       UpdateWindow(hWnd);
1168     }
1169     break;
1170   }
1171   case WM_SIZE:
1172   {
1173     ME_MarkAllForWrapping(editor);
1174     ME_WrapMarkedParagraphs(editor);
1175     ME_UpdateScrollBar(editor);
1176     ME_Repaint(editor);
1177     return DefWindowProcW(hWnd, msg, wParam, lParam);
1178   }
1179   case EM_GETOLEINTERFACE:
1180   {
1181     LPVOID *ppvObj = (LPVOID*) lParam;
1182     FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
1183     return CreateIRichEditOle(ppvObj);
1184   }
1185   default:
1186   do_default:
1187     return DefWindowProcW(hWnd, msg, wParam, lParam);
1188   }
1189   return 0L;
1190 }
1191
1192 /******************************************************************
1193  *        RichEdit10ANSIWndProc (RICHED20.9)
1194  */
1195 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1196 {
1197   /* FIXME: this is NOT the same as 2.0 version */
1198   return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
1199 }
1200
1201 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
1202 {
1203   HWND hWnd = editor->hWnd;
1204   SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
1205 }
1206
1207 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
1208 {
1209   ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1210   int i = 0;
1211   
1212   while(item && item->member.para.next_para->member.para.nCharOfs <= from)
1213     item = item->member.para.next_para;
1214   if (!item)
1215     return 0;
1216   while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
1217     item = item->member.para.next_para;
1218     i++;
1219   }
1220   return i;
1221 }
1222
1223
1224 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
1225 {
1226   ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1227   int nWritten = 0;
1228   
1229   if (!item) {
1230     *buffer = L'\0';
1231     return 0;
1232   }
1233   assert(item);    
1234   
1235   if (nStart)
1236   {
1237     int nLen = ME_StrLen(item->member.run.strText) - nStart;
1238     if (nLen > nChars)
1239       nLen = nChars;
1240     CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
1241     nChars -= nLen;
1242     nWritten += nLen;
1243     if (!nChars)
1244       return nWritten;
1245     buffer += nLen;
1246     nStart = 0;
1247     item = ME_FindItemFwd(item, diRun);
1248   }
1249   
1250   while(nChars && item)
1251   {
1252     int nLen = ME_StrLen(item->member.run.strText);
1253     if (nLen > nChars)
1254       nLen = nChars;
1255       
1256     if (item->member.run.nFlags & MERF_ENDPARA)
1257     {
1258       if (bCRLF) {
1259         *buffer++ = '\r';
1260         nWritten++;
1261       }        
1262       *buffer = '\n';
1263       assert(nLen == 1);
1264     }
1265     else      
1266       CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
1267     nChars -= nLen;
1268     nWritten += nLen;
1269     buffer += nLen;    
1270       
1271     if (!nChars)
1272     {
1273       *buffer = L'\0';
1274       return nWritten;
1275     }
1276     item = ME_FindItemFwd(item, diRun);
1277   }
1278   *buffer = L'\0';
1279   return nWritten;  
1280 }
1281
1282 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
1283 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
1284
1285 void ME_RegisterEditorClass(HINSTANCE hInstance)
1286 {
1287   BOOL bResult;
1288   WNDCLASSW wcW;
1289   WNDCLASSA wcA;
1290   
1291   wcW.style = CS_HREDRAW | CS_VREDRAW;
1292   wcW.lpfnWndProc = RichEditANSIWndProc;
1293   wcW.cbClsExtra = 0;
1294   wcW.cbWndExtra = 4;
1295   wcW.hInstance = NULL; /* hInstance would register DLL-local class */
1296   wcW.hIcon = NULL;
1297   wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1298   wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1299   wcW.lpszMenuName = NULL;
1300   wcW.lpszClassName = wszClassName;
1301   bResult = RegisterClassW(&wcW);  
1302   assert(bResult);
1303   wcW.lpszClassName = wszClassName50;
1304   bResult = RegisterClassW(&wcW);  
1305   assert(bResult);
1306
1307   wcA.style = CS_HREDRAW | CS_VREDRAW;
1308   wcA.lpfnWndProc = RichEditANSIWndProc;
1309   wcA.cbClsExtra = 0;
1310   wcA.cbWndExtra = 4;
1311   wcA.hInstance = NULL; /* hInstance would register DLL-local class */
1312   wcA.hIcon = NULL;
1313   wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1314   wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1315   wcA.lpszMenuName = NULL;
1316   wcA.lpszClassName = "RichEdit20A";
1317   bResult = RegisterClassA(&wcA);  
1318   assert(bResult);
1319   wcA.lpszClassName = "RichEdit50A";
1320   bResult = RegisterClassA(&wcA);  
1321   assert(bResult);
1322 }
1323
1324 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1325 {
1326     TRACE("\n");
1327     switch (fdwReason)
1328     {
1329     case DLL_PROCESS_ATTACH:
1330       DisableThreadLibraryCalls(hinstDLL);
1331       me_heap = HeapCreate (0, 0x10000, 0);
1332       ME_RegisterEditorClass(hinstDLL);
1333       break;
1334
1335     case DLL_PROCESS_DETACH:
1336       UnregisterClassW(wszClassName, 0);
1337       UnregisterClassW(wszClassName50, 0);
1338       UnregisterClassA("RichEdit20A", 0);
1339       UnregisterClassA("RichEdit50A", 0);
1340       HeapDestroy (me_heap);
1341       me_heap = NULL;
1342       break;
1343     }
1344     return TRUE;
1345 }
1346
1347 /******************************************************************
1348  *        CreateTextServices (RICHED20.4)
1349  *
1350  * FIXME should be ITextHost instead of void*
1351  */
1352 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
1353     IUnknown **ppUnk)
1354 {
1355   FIXME("stub\n");
1356   /* FIXME should support aggregation */
1357   if (punkOuter)
1358     return CLASS_E_NOAGGREGATION;
1359     
1360   return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
1361 }
1362
1363 /******************************************************************
1364  *        REExtendedRegisterClass (RICHED20.8)
1365  *
1366  * FIXME undocumented
1367  */
1368 void WINAPI REExtendedRegisterClass(void)
1369 {
1370   FIXME("stub\n");
1371 }