- Unknown destinations are now correctly skipped (so loading an RTF
[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   CHARFORMAT2A 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           strncpy(fmt.szFaceName, f->rtfFName, sizeof(fmt.szFaceName)-1);
338           fmt.szFaceName[sizeof(fmt.szFaceName)-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     RTFFlushOutputBuffer(info);
351     SendMessageW(info->hwndEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
352   }
353 }
354
355 void ME_RTFParAttrHook(RTF_Info *info)
356 {
357   PARAFORMAT2 fmt;
358   fmt.cbSize = sizeof(fmt);
359   fmt.dwMask = 0;
360   
361   switch(info->rtfMinor)
362   {
363   case rtfParDef: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
364     fmt.dwMask = PFM_ALIGNMENT;
365     fmt.wAlignment = PFA_LEFT;
366     break;
367   case rtfQuadLeft:
368   case rtfQuadJust:
369     fmt.dwMask = PFM_ALIGNMENT;
370     fmt.wAlignment = PFA_LEFT;
371     break;
372   case rtfQuadRight:
373     fmt.dwMask = PFM_ALIGNMENT;
374     fmt.wAlignment = PFA_RIGHT;
375     break;
376   case rtfQuadCenter:
377     fmt.dwMask = PFM_ALIGNMENT;
378     fmt.wAlignment = PFA_CENTER;
379     break;
380   }  
381   if (fmt.dwMask) {
382     RTFFlushOutputBuffer(info);
383     SendMessageW(info->hwndEdit, EM_SETPARAFORMAT, 0, (LPARAM)&fmt);
384   }
385 }
386
387 void ME_RTFReadHook(RTF_Info *info) {
388   switch(info->rtfClass)
389   {
390     case rtfGroup:
391       switch(info->rtfMajor)
392       {
393         case rtfBeginGroup:
394           if (info->formatStackTop < maxCharFormatStack) {
395             info->formatStack[info->formatStackTop].cbSize = sizeof(info->formatStack[0]);
396             SendMessageW(info->hwndEdit, EM_GETCHARFORMAT, 1, (LPARAM)&info->formatStack[info->formatStackTop]);
397           }
398           info->formatStackTop++;
399           break;
400         case rtfEndGroup:
401           RTFFlushOutputBuffer(info);
402           info->formatStackTop--;
403           if (info->formatStackTop < maxCharFormatStack) {
404             SendMessageW(info->hwndEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&info->formatStack[info->formatStackTop]);
405           }
406           break;
407       }
408       break;
409     case rtfControl:
410       switch(info->rtfMajor)
411       {
412         case rtfCharAttr:
413           ME_RTFCharAttrHook(info);
414           break;
415         case rtfParAttr:
416           ME_RTFParAttrHook(info);
417           break;
418       }
419       break;
420   }
421 }
422
423 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
424 {
425   RTF_Info parser;
426   ME_Style *style;
427   int from, to, to2, nUndoMode;
428   ME_UndoItem *pUI;
429
430   TRACE("%p %p\n", stream, editor->hWnd);
431   
432   ME_GetSelection(editor, &from, &to);
433   if (format & SFF_SELECTION) {
434     style = ME_GetSelectionInsertStyle(editor);
435
436     ME_InternalDeleteText(editor, from, to-from);
437   }
438   else {
439     style = editor->pBuffer->pDefaultStyle;
440     ME_AddRefStyle(style);
441     SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);    
442     ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
443     from = to = 0;
444     ME_ClearTempStyle(editor);
445   }
446   
447   nUndoMode = editor->nUndoMode;
448   editor->nUndoMode = umIgnore;
449   if (format & SF_RTF) {
450     /* setup the RTF parser */
451     memset(&parser, 0, sizeof parser);
452     RTFSetEditStream(&parser, stream);
453     parser.rtfFormat = format&(SF_TEXT|SF_RTF);
454     parser.hwndEdit = editor->hWnd;
455     WriterInit(&parser);
456     RTFInit(&parser);
457     RTFSetReadHook(&parser, ME_RTFReadHook);
458     BeginFile(&parser);
459   
460     /* do the parsing */
461     RTFRead(&parser);
462     RTFFlushOutputBuffer(&parser);
463   }
464   else if (format & SF_TEXT)
465     ME_StreamInText(editor, format, stream, style);
466   else
467     ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
468   ME_GetSelection(editor, &to, &to2);
469   /* put the cursor at the top */
470   if (!(format & SFF_SELECTION))
471     SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
472   else
473   {
474     /* FIXME where to put cursor now ? */
475   }
476   
477   editor->nUndoMode = nUndoMode;
478   pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
479   TRACE("from %d to %d\n", from, to);
480   if (pUI && from < to)
481   {
482     pUI->nStart = from;
483     pUI->nLen = to-from;
484   }
485   ME_CommitUndo(editor);
486   ME_ReleaseStyle(style);
487
488   return 0;
489 }
490
491
492 ME_DisplayItem *
493 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
494 {
495   ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
496   
497   while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
498     item = ME_FindItemFwd(item, diParagraph);
499
500   if (!item)
501     return item;
502
503   nOffset -= item->member.para.nCharOfs;
504   if (nItemType == diParagraph) {
505     if (nItemOffset)
506       *nItemOffset = nOffset;
507     return item;
508   }
509   
510   do {
511     item = ME_FindItemFwd(item, diRun);
512   } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
513   if (item) {
514     nOffset -= item->member.run.nCharOfs;
515     if (nItemOffset)
516       *nItemOffset = nOffset;
517   }
518   return item;
519 }
520
521
522 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
523   ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
524   HDC hDC;
525   int i;
526   ed->hWnd = hWnd;
527   ed->pBuffer = ME_MakeText();
528   hDC = GetDC(hWnd);
529   ME_MakeFirstParagraph(hDC, ed->pBuffer);
530   ReleaseDC(hWnd, hDC);
531   ed->bCaretShown = FALSE;
532   ed->nCursors = 3;
533   ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
534   ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
535   ed->pCursors[0].nOffset = 0;
536   ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
537   ed->pCursors[1].nOffset = 0;
538   ed->nLastTotalLength = ed->nTotalLength = 0;
539   ed->nScrollPos = 0;
540   ed->nUDArrowX = -1;
541   ed->nSequence = 0;
542   ed->rgbBackColor = -1;
543   ed->bCaretAtEnd = FALSE;
544   ed->nEventMask = 0;
545   ed->nModifyStep = 0;
546   ed->pUndoStack = ed->pRedoStack = NULL;
547   ed->nUndoMode = umAddToUndo;
548   ed->nParagraphs = 1;
549   ed->nLastSelStart = ed->nLastSelEnd = 0;
550   for (i=0; i<HFONT_CACHE_SIZE; i++)
551   {
552     ed->pFontCache[i].nRefs = 0;
553     ed->pFontCache[i].nAge = 0;
554     ed->pFontCache[i].hFont = NULL;
555   }
556   ME_CheckCharOffsets(ed);
557   return ed;
558 }
559
560 typedef struct tagME_GlobalDestStruct
561 {
562   HGLOBAL hData;
563   int nLength;
564 } ME_GlobalDestStruct;
565
566 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
567 {
568   ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
569   int nMaxSize;
570   BYTE *pDest;
571   
572   nMaxSize = GlobalSize(pData->hData);
573   if (pData->nLength+cb+1 >= cb)
574   {
575     /* round up to 2^17 */
576     int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
577     pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
578   }
579   pDest = (BYTE *)GlobalLock(pData->hData);
580   memcpy(pDest + pData->nLength, lpBuff, cb);
581   pData->nLength += cb;
582   pDest[pData->nLength] = '\0';
583   GlobalUnlock(pData->hData);
584   *pcb = cb;
585   
586   return 0;
587 }
588
589 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
590 {
591   ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
592   int i;
593   WORD *pSrc, *pDest;
594   
595   cb = cb >> 1;
596   pDest = (WORD *)lpBuff;
597   pSrc = (WORD *)GlobalLock(pData->hData);
598   for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
599     pDest[i] = pSrc[pData->nLength+i];
600   }    
601   pData->nLength += i;
602   *pcb = 2*i;
603   GlobalUnlock(pData->hData);
604   return 0;
605 }
606
607 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
608 {
609   ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
610   int i;
611   BYTE *pSrc, *pDest;
612   
613   pDest = lpBuff;
614   pSrc = (BYTE *)GlobalLock(pData->hData);
615   for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
616     pDest[i] = pSrc[pData->nLength+i];
617   }    
618   pData->nLength += i;
619   *pcb = i;
620   GlobalUnlock(pData->hData);
621   return 0;
622 }
623
624 void ME_DestroyEditor(ME_TextEditor *editor)
625 {
626   ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
627   ME_DisplayItem *p = pFirst, *pNext = NULL;
628   int i;
629   
630   ME_ClearTempStyle(editor);
631   ME_EmptyUndoStack(editor);
632   while(p) {
633     pNext = p->next;
634     ME_DestroyDisplayItem(p);    
635     p = pNext;
636   }
637   ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
638   for (i=0; i<HFONT_CACHE_SIZE; i++)
639   {
640     if (editor->pFontCache[i].hFont)
641       DeleteObject(editor->pFontCache[i].hFont);
642   }
643     
644   FREE_OBJ(editor);
645 }
646
647 #define UNSUPPORTED_MSG(e) \
648   case e: \
649     FIXME(#e ": stub\n"); \
650     return DefWindowProcW(hWnd, msg, wParam, lParam);
651
652 /******************************************************************
653  *        RichEditANSIWndProc (RICHED20.10)
654  */
655 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
656   HDC hDC;
657   PAINTSTRUCT ps;
658   SCROLLINFO si;
659   ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
660   TRACE("msg %d %08x %08lx\n", msg, wParam, lParam);
661   switch(msg) {
662   
663   UNSUPPORTED_MSG(EM_AUTOURLDETECT)
664   UNSUPPORTED_MSG(EM_CANPASTE)
665   UNSUPPORTED_MSG(EM_CHARFROMPOS)
666   UNSUPPORTED_MSG(EM_DISPLAYBAND)
667   UNSUPPORTED_MSG(EM_EXLIMITTEXT)
668   UNSUPPORTED_MSG(EM_EXLINEFROMCHAR)
669   UNSUPPORTED_MSG(EM_FINDTEXT)
670   UNSUPPORTED_MSG(EM_FINDTEXTEX)
671   UNSUPPORTED_MSG(EM_FINDWORDBREAK)
672   UNSUPPORTED_MSG(EM_FMTLINES)
673   UNSUPPORTED_MSG(EM_FORMATRANGE)
674   UNSUPPORTED_MSG(EM_GETFIRSTVISIBLELINE)
675   UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
676   /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
677   UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
678   UNSUPPORTED_MSG(EM_GETLIMITTEXT)
679   UNSUPPORTED_MSG(EM_GETLINE)
680   UNSUPPORTED_MSG(EM_GETLINECOUNT)
681   /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
682   UNSUPPORTED_MSG(EM_GETOPTIONS)
683   UNSUPPORTED_MSG(EM_GETRECT)
684   UNSUPPORTED_MSG(EM_GETREDONAME)
685   UNSUPPORTED_MSG(EM_GETTEXTMODE)
686   UNSUPPORTED_MSG(EM_GETUNDONAME)
687   UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
688   UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
689   UNSUPPORTED_MSG(EM_HIDESELECTION)
690   UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
691   UNSUPPORTED_MSG(EM_LINEFROMCHAR)
692   UNSUPPORTED_MSG(EM_LINEINDEX)
693   UNSUPPORTED_MSG(EM_LINELENGTH)
694   UNSUPPORTED_MSG(EM_LINESCROLL)
695   UNSUPPORTED_MSG(EM_PASTESPECIAL)
696 /*  UNSUPPORTED_MSG(EM_POSFROMCHARS) missing in Wine headers */
697   UNSUPPORTED_MSG(EM_REQUESTRESIZE)
698   UNSUPPORTED_MSG(EM_SCROLL)
699   UNSUPPORTED_MSG(EM_SCROLLCARET)
700   UNSUPPORTED_MSG(EM_SELECTIONTYPE)
701   UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
702   UNSUPPORTED_MSG(EM_SETOLECALLBACK)
703   UNSUPPORTED_MSG(EM_SETOPTIONS)
704   UNSUPPORTED_MSG(EM_SETRECT)
705   UNSUPPORTED_MSG(EM_SETRECTNP)
706   UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
707   UNSUPPORTED_MSG(EM_SETTEXTMODE)
708   UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
709   UNSUPPORTED_MSG(EM_SETWORDBREAKPROC)
710   UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
711   UNSUPPORTED_MSG(WM_SETFONT)
712   UNSUPPORTED_MSG(WM_STYLECHANGING)
713   UNSUPPORTED_MSG(WM_STYLECHANGED)
714 /*  UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
715     
716 /* Messages specific to Richedit controls */
717   
718   case EM_STREAMIN:
719    return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
720   case EM_STREAMOUT:
721    return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
722   case WM_GETDLGCODE:
723   {
724     UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
725     if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
726       code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
727     return code;
728   }
729   case WM_NCCREATE:
730   {
731     CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
732     editor = ME_MakeEditor(hWnd);
733     SetWindowLongW(hWnd, 0, (long)editor);
734     pcs = 0; /* ignore */
735     return TRUE;
736   }
737   case EM_EMPTYUNDOBUFFER:
738     ME_EmptyUndoStack(editor);
739     return 0;
740   case EM_GETSEL:
741   {
742     ME_GetSelection(editor, (int *)wParam, (int *)lParam);
743     if (!((wParam|lParam) & 0xFFFF0000))
744       return (lParam<<16)|wParam;
745     return -1;
746   }
747   case EM_EXGETSEL:
748   {
749     CHARRANGE *pRange = (CHARRANGE *)lParam;
750     ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
751     return 0;
752   }
753   case EM_CANUNDO:
754     return editor->pUndoStack != NULL;
755   case EM_CANREDO:
756     return editor->pRedoStack != NULL;
757   case EM_UNDO:
758     ME_Undo(editor);
759     return 0;
760   case EM_REDO:
761     ME_Redo(editor);
762     return 0;
763   case EM_SETSEL:
764   {
765     ME_SetSelection(editor, wParam, lParam);
766     ME_Repaint(editor);
767     ME_SendSelChange(editor);
768     return 0;
769   }
770   case EM_EXSETSEL:
771   {
772     CHARRANGE *pRange = (CHARRANGE *)lParam;
773     ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
774     /* FIXME optimize */
775     ME_Repaint(editor);
776     ME_SendSelChange(editor);
777     return 0;
778   }
779   case EM_SETBKGNDCOLOR:
780   {
781     LRESULT lColor = ME_GetBackColor(editor);
782     if (wParam)
783       editor->rgbBackColor = -1;
784     else
785       editor->rgbBackColor = lParam; 
786     InvalidateRect(hWnd, NULL, TRUE);
787     UpdateWindow(hWnd);
788     return lColor;
789   }
790   case EM_GETMODIFY:
791     return editor->nModifyStep == 0 ? 0 : 1;
792   case EM_SETMODIFY:
793   {
794     if (wParam)
795       editor->nModifyStep = 0x80000000;
796     else
797       editor->nModifyStep = 0;
798     
799     return 0;
800   }
801   case EM_SETREADONLY:
802   {
803     long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
804     if (wParam)
805       nStyle |= ES_READONLY;
806     else
807       nStyle &= ~ES_READONLY;
808     SetWindowLongW(hWnd, GWL_STYLE, nStyle);
809     ME_Repaint(editor);
810     return 0;
811   }
812   case EM_SETEVENTMASK:
813     editor->nEventMask = lParam;
814     return 0;
815   case EM_GETEVENTMASK:
816     return editor->nEventMask;
817   case EM_SETCHARFORMAT:
818   {
819     CHARFORMAT2W buf, *p;
820     p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
821     if (!wParam)
822       ME_SetDefaultCharFormat(editor, p);
823     else if (wParam == (SCF_WORD | SCF_SELECTION))
824       FIXME("word selection not supported\n");
825     else if (wParam == SCF_ALL)
826       ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
827     else
828       ME_SetSelectionCharFormat(editor, p);
829     ME_CommitUndo(editor);
830     ME_UpdateRepaint(editor);
831     return 0;
832   }
833   case EM_GETCHARFORMAT:
834   {
835     CHARFORMAT2W tmp;
836     tmp.cbSize = sizeof(tmp);
837     if (!wParam)
838       ME_GetDefaultCharFormat(editor, &tmp);
839     else
840       ME_GetSelectionCharFormat(editor, &tmp);
841     ME_CopyToCFAny((CHARFORMAT2W *)lParam, &tmp);
842     return 0;
843   }
844   case EM_SETPARAFORMAT:
845     ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
846     ME_CommitUndo(editor);
847     return 0;
848   case EM_GETPARAFORMAT:
849     ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
850     return 0;
851   case WM_CLEAR:
852   {
853     int from, to;
854     ME_GetSelection(editor, &from, &to);
855     ME_InternalDeleteText(editor, from, to-from);
856     ME_CommitUndo(editor);
857     ME_UpdateRepaint(editor);
858     return 0;
859   }
860   case EM_REPLACESEL:
861   {
862     int from, to;
863     ME_Style *style;
864     LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
865     size_t len = lstrlenW(wszText);
866     TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
867     
868     ME_GetSelection(editor, &from, &to);
869     style = ME_GetSelectionInsertStyle(editor);
870     ME_InternalDeleteText(editor, from, to-from);
871     ME_InsertTextFromCursor(editor, 0, wszText, len, style);
872     ME_ReleaseStyle(style);
873     ME_EndToUnicode(hWnd, wszText);
874     /* drop temporary style if line end */
875     /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */  
876     if (len>0 && wszText[len-1] == '\n')
877       ME_ClearTempStyle(editor);
878       
879     ME_CommitUndo(editor);
880     if (!wParam)
881       ME_EmptyUndoStack(editor);
882     ME_UpdateRepaint(editor);
883     return 0;
884   }
885   case WM_SETTEXT:
886   {
887     LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
888     TRACE("WM_SETTEXT - %s\n", (char *)(wszText)); /* debugstr_w() */
889     ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
890     /* uses default style! */
891     ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
892     ME_EndToUnicode(hWnd, wszText);
893     ME_CommitUndo(editor);
894     ME_EmptyUndoStack(editor);
895     ME_UpdateRepaint(editor);
896     return 0;
897   }
898   case WM_PASTE:
899   {    
900     DWORD dwFormat = 0;
901     EDITSTREAM es;
902     ME_GlobalDestStruct gds;
903     UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
904     UINT cf = 0;
905
906     if (!OpenClipboard(hWnd))
907       return 0;
908     if (IsClipboardFormatAvailable(nRTFFormat))
909       cf = nRTFFormat, dwFormat = SF_RTF;
910     else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
911       cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
912     else
913       return 0;
914
915     gds.hData = GetClipboardData(cf);
916     gds.nLength = 0;
917     es.dwCookie = (DWORD)&gds;
918     es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
919     SendMessageW(hWnd, EM_STREAMIN, dwFormat|SFF_SELECTION, (LPARAM)&es);
920     
921     CloseClipboard();
922     return 0;
923   }
924   case WM_CUT:
925   case WM_COPY:
926   {
927     int from, to, pars;
928     WCHAR *data;
929     HANDLE hData;
930     EDITSTREAM es;
931     ME_GlobalDestStruct gds;
932     
933     if (!OpenClipboard(hWnd))
934       return 0;
935       
936     EmptyClipboard();
937     ME_GetSelection(editor, &from, &to);
938     pars = ME_CountParagraphsBetween(editor, from, to);
939     hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
940     data = (WCHAR *)GlobalLock(hData);
941     ME_GetTextW(editor, data, from, to-from, TRUE);
942     GlobalUnlock(hData);
943
944     gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
945     gds.nLength = 0;
946     es.dwCookie = (DWORD)&gds;
947     es.pfnCallback = ME_AppendToHGLOBAL;
948     SendMessageW(hWnd, EM_STREAMOUT, SFF_SELECTION|SF_RTF, (LPARAM)&es);
949     GlobalReAlloc(gds.hData, gds.nLength+1, 0);
950     
951     SetClipboardData(CF_UNICODETEXT, hData);    
952     SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds.hData);
953     
954     CloseClipboard();
955     if (msg == WM_CUT)
956     {
957       ME_InternalDeleteText(editor, from, to-from);
958       ME_CommitUndo(editor);
959       ME_UpdateRepaint(editor);
960     }
961     return 0;
962   }
963   case WM_GETTEXTLENGTH:
964     return ME_GetTextLength(editor);
965   case WM_GETTEXT:
966   {
967     TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
968     tr.chrg.cpMin = 0;
969     tr.chrg.cpMax = wParam-1;
970     tr.lpstrText = (WCHAR *)lParam;
971     return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
972   }
973   case EM_GETSELTEXT:
974   {
975     int from, to;
976     TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
977     ME_GetSelection(editor, &from, &to);
978     tr.chrg.cpMin = from;
979     tr.chrg.cpMax = to;
980     tr.lpstrText = (WCHAR *)lParam;
981     return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
982   }
983   case EM_GETTEXTRANGE:
984   {
985     TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
986     if (IsWindowUnicode(hWnd))
987       return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
988     else
989     {
990       int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
991       WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
992       int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, FALSE);
993       /* FIXME this is a potential security hole (buffer overrun) 
994          if you know more about wchar->mbyte conversion please explain
995       */
996       WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
997       FREE_OBJ(p);
998       return nChars;
999     }
1000     return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
1001   }
1002   case WM_CREATE:
1003     ME_CommitUndo(editor);
1004     ME_WrapMarkedParagraphs(editor);
1005     ME_MoveCaret(editor);
1006     return 0;
1007   case WM_DESTROY:
1008     ME_DestroyEditor(editor);
1009     SetWindowLongW(hWnd, 0, 0);
1010     return 0;
1011   case WM_LBUTTONDOWN:
1012     SetFocus(hWnd);
1013     ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1014     SetCapture(hWnd);
1015     break;
1016   case WM_MOUSEMOVE:
1017     if (GetCapture() == hWnd)
1018       ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1019     break;
1020   case WM_LBUTTONUP:
1021     if (GetCapture() == hWnd)
1022       ReleaseCapture();
1023     break;
1024   case WM_PAINT:
1025     hDC = BeginPaint(hWnd, &ps);
1026     ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
1027     EndPaint(hWnd, &ps);
1028     break;
1029   case WM_SETFOCUS:
1030     ME_ShowCaret(editor);
1031     ME_SendOldNotify(editor, EN_SETFOCUS);
1032     return 0;
1033   case WM_KILLFOCUS:
1034     ME_HideCaret(editor);
1035     ME_SendOldNotify(editor, EN_KILLFOCUS);
1036     return 0;
1037   case WM_ERASEBKGND:
1038   {
1039     HDC hDC = (HDC)wParam;
1040     RECT rc;
1041     COLORREF rgbBG = ME_GetBackColor(editor);
1042     if (GetUpdateRect(hWnd,&rc,TRUE))
1043     {
1044       HBRUSH hbr = CreateSolidBrush(rgbBG);
1045       FillRect(hDC, &rc, hbr);
1046       DeleteObject(hbr);
1047     }
1048     return 1;
1049   }
1050   case WM_COMMAND:
1051     TRACE("editor wnd command = %d\n", LOWORD(wParam));
1052     return 0;
1053   case WM_KEYDOWN:
1054     if (ME_ArrowKey(editor, LOWORD(wParam), GetKeyState(VK_CONTROL)<0)) {
1055       ME_CommitUndo(editor);
1056       ME_EnsureVisible(editor, editor->pCursors[0].pRun);
1057       HideCaret(hWnd);
1058       ME_MoveCaret(editor);
1059       ShowCaret(hWnd);
1060       return 0;
1061     }
1062     if (GetKeyState(VK_CONTROL)<0)
1063     {
1064       if (LOWORD(wParam)=='W')
1065       {
1066         CHARFORMAT2W chf;
1067         char buf[2048];
1068         ME_GetSelectionCharFormat(editor, &chf);
1069         ME_DumpStyleToBuf(&chf, buf);
1070         MessageBoxA(NULL, buf, "Style dump", MB_OK);
1071       }
1072       if (LOWORD(wParam)=='Q')
1073       {
1074         ME_CheckCharOffsets(editor);
1075       }
1076     }
1077     goto do_default;
1078   case WM_CHAR: 
1079   {
1080     WCHAR wstr;
1081     if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
1082       MessageBeep(MB_ICONERROR);
1083       return 0; /* FIXME really 0 ? */
1084     }
1085     wstr = LOWORD(wParam);
1086     if (((unsigned)wstr)>=' ' || wstr=='\r') {
1087       /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
1088       ME_Style *style = ME_GetInsertStyle(editor, 0);
1089       ME_SaveTempStyle(editor);
1090       ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
1091       ME_ReleaseStyle(style);
1092       ME_CommitUndo(editor);
1093       ME_UpdateRepaint(editor);
1094     }
1095     return 0;
1096   }
1097   case WM_VSCROLL: 
1098   {
1099     si.cbSize = sizeof(SCROLLINFO);
1100     si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
1101     GetScrollInfo(hWnd, SB_VERT, &si);
1102     switch(LOWORD(wParam)) {
1103     case SB_THUMBTRACK:
1104       SetScrollPos(hWnd, SB_VERT, si.nTrackPos, FALSE);
1105       ScrollWindow(hWnd, 0, si.nPos-si.nTrackPos, NULL, NULL);
1106       /* InvalidateRect(hWnd, NULL, TRUE); */
1107       UpdateWindow(hWnd);
1108       break;
1109     }
1110     break;
1111   }
1112   case WM_SIZE:
1113   {
1114     ME_MarkAllForWrapping(editor);
1115     ME_Repaint(editor);
1116     return DefWindowProcW(hWnd, msg, wParam, lParam);
1117   }
1118   case EM_GETOLEINTERFACE:
1119   {
1120     LPVOID *ppvObj = (LPVOID*) lParam;
1121     FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
1122     return CreateIRichEditOle(ppvObj);
1123   }
1124   default:
1125   do_default:
1126     return DefWindowProcW(hWnd, msg, wParam, lParam);
1127   }
1128   return 0L;
1129 }
1130
1131 /******************************************************************
1132  *        RichEdit10ANSIWndProc (RICHED20.9)
1133  */
1134 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1135 {
1136   /* FIXME: this is NOT the same as 2.0 version */
1137   return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
1138 }
1139
1140 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
1141 {
1142   HWND hWnd = editor->hWnd;
1143   SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
1144 }
1145
1146 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
1147 {
1148   ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1149   int i = 0;
1150   
1151   while(item && item->member.para.next_para->member.para.nCharOfs <= from)
1152     item = item->member.para.next_para;
1153   if (!item)
1154     return 0;
1155   while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
1156     item = item->member.para.next_para;
1157     i++;
1158   }
1159   return i;
1160 }
1161
1162
1163 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
1164 {
1165   ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1166   int nWritten = 0;
1167   
1168   if (!item) {
1169     *buffer = L'\0';
1170     return 0;
1171   }
1172   assert(item);    
1173   
1174   if (nStart)
1175   {
1176     int nLen = ME_StrLen(item->member.run.strText) - nStart;
1177     if (nLen > nChars)
1178       nLen = nChars;
1179     CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
1180     nChars -= nLen;
1181     nWritten += nLen;
1182     if (!nChars)
1183       return nWritten;
1184     buffer += nLen;
1185     nStart = 0;
1186     item = ME_FindItemFwd(item, diRun);
1187   }
1188   
1189   while(nChars && item)
1190   {
1191     int nLen = ME_StrLen(item->member.run.strText);
1192     if (nLen > nChars)
1193       nLen = nChars;
1194       
1195     if (item->member.run.nFlags & MERF_ENDPARA)
1196     {
1197       if (bCRLF) {
1198         *buffer++ = '\r';
1199         nWritten++;
1200       }        
1201       *buffer = '\n';
1202       assert(nLen == 1);
1203     }
1204     else      
1205       CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
1206     nChars -= nLen;
1207     nWritten += nLen;
1208     buffer += nLen;    
1209       
1210     if (!nChars)
1211     {
1212       *buffer = L'\0';
1213       return nWritten;
1214     }
1215     item = ME_FindItemFwd(item, diRun);
1216   }
1217   *buffer = L'\0';
1218   return nWritten;  
1219 }
1220
1221 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
1222 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
1223
1224 void ME_RegisterEditorClass(HINSTANCE hInstance)
1225 {
1226   BOOL bResult;
1227   WNDCLASSW wcW;
1228   WNDCLASSA wcA;
1229   
1230   wcW.style = CS_HREDRAW | CS_VREDRAW;
1231   wcW.lpfnWndProc = RichEditANSIWndProc;
1232   wcW.cbClsExtra = 0;
1233   wcW.cbWndExtra = 4;
1234   wcW.hInstance = NULL; /* hInstance would register DLL-local class */
1235   wcW.hIcon = NULL;
1236   wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1237   wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1238   wcW.lpszMenuName = NULL;
1239   wcW.lpszClassName = wszClassName;
1240   bResult = RegisterClassW(&wcW);  
1241   assert(bResult);
1242   wcW.lpszClassName = wszClassName50;
1243   bResult = RegisterClassW(&wcW);  
1244   assert(bResult);
1245
1246   wcA.style = CS_HREDRAW | CS_VREDRAW;
1247   wcA.lpfnWndProc = RichEditANSIWndProc;
1248   wcA.cbClsExtra = 0;
1249   wcA.cbWndExtra = 4;
1250   wcA.hInstance = NULL; /* hInstance would register DLL-local class */
1251   wcA.hIcon = NULL;
1252   wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1253   wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1254   wcA.lpszMenuName = NULL;
1255   wcA.lpszClassName = "RichEdit20A";
1256   bResult = RegisterClassA(&wcA);  
1257   assert(bResult);
1258   wcA.lpszClassName = "RichEdit50A";
1259   bResult = RegisterClassA(&wcA);  
1260   assert(bResult);
1261 }
1262
1263 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1264 {
1265     TRACE("\n");
1266     switch (fdwReason)
1267     {
1268     case DLL_PROCESS_ATTACH:
1269       DisableThreadLibraryCalls(hinstDLL);
1270       me_heap = HeapCreate (0, 0x10000, 0);
1271       ME_RegisterEditorClass(hinstDLL);
1272       break;
1273
1274     case DLL_PROCESS_DETACH:
1275       UnregisterClassW(wszClassName, 0);
1276       UnregisterClassW(wszClassName50, 0);
1277       UnregisterClassA("RichEdit20A", 0);
1278       UnregisterClassA("RichEdit50A", 0);
1279       HeapDestroy (me_heap);
1280       me_heap = NULL;
1281       break;
1282     }
1283     return TRUE;
1284 }
1285
1286 /******************************************************************
1287  *        CreateTextServices (RICHED20.4)
1288  *
1289  * FIXME should be ITextHost instead of void*
1290  */
1291 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
1292     IUnknown **ppUnk)
1293 {
1294   FIXME("stub\n");
1295   /* FIXME should support aggregation */
1296   if (punkOuter)
1297     return CLASS_E_NOAGGREGATION;
1298     
1299   return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
1300 }
1301
1302 /******************************************************************
1303  *        REExtendedRegisterClass (RICHED20.8)
1304  *
1305  * FIXME undocumented
1306  */
1307 void WINAPI REExtendedRegisterClass(void)
1308 {
1309   FIXME("stub\n");
1310 }