- Added debugging.
[wine] / dlls / richedit / richedit.c
1 /*
2  * RichEdit32  functions
3  *
4  * This module is a simple wrapper for the edit controls.
5  * At the point, it is good only for application who use the RICHEDIT 
6  * control to display RTF text.
7  *
8  * Copyright 2000 by Jean-Claude Batista
9  * 
10  */
11  
12 #include <string.h>
13 #include "windef.h"
14 #include "winbase.h"
15 #include "wingdi.h"
16 #include "winreg.h"
17 #include "winerror.h"
18 #include "riched32.h"
19 #include "richedit.h"
20 #include "charlist.h"
21 #define NO_SHLWAPI_STREAM
22 #include "shlwapi.h"
23
24 #include "rtf.h"
25 #include "rtf2text.h"
26 #include "debugtools.h"
27
28 #define ID_EDIT      1
29
30 DEFAULT_DEBUG_CHANNEL(richedit);
31
32 HANDLE RICHED32_hHeap = (HANDLE)NULL;
33 /* LPSTR  RICHED32_aSubclass = (LPSTR)NULL; */
34
35 #define DPRINTF_EDIT_MSG32(str) \
36         TRACE(\
37                      "32 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n"\
38                      , \
39                      hwnd, (UINT)wParam, (UINT)lParam)
40
41
42 /***********************************************************************
43  * RICHED32_LibMain [Internal] Initializes the internal 'RICHED32.DLL'.
44  *
45  * PARAMS
46  *     hinstDLL    [I] handle to the DLL's instance
47  *     fdwReason   [I]
48  *     lpvReserved [I] reserved, must be NULL
49  *
50  * RETURNS
51  *     Success: TRUE
52  *     Failure: FALSE
53  */
54
55 BOOL WINAPI
56 RICHED32_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
57 {
58     TRACE("\n");
59     switch (fdwReason)
60     {
61     case DLL_PROCESS_ATTACH:
62         /* create private heap */
63         RICHED32_hHeap = HeapCreate (0, 0x10000, 0);
64         /* register the Rich Edit class */
65         RICHED32_Register ();
66         break;
67
68     case DLL_PROCESS_DETACH:
69         /* unregister all common control classes */
70         RICHED32_Unregister ();
71         HeapDestroy (RICHED32_hHeap);
72         RICHED32_hHeap = (HANDLE)NULL;
73         break;
74     }
75     return TRUE;
76 }
77
78 /*
79  *
80  * DESCRIPTION:
81  * Window procedure of the RichEdit control.
82  *
83  */
84 static LRESULT WINAPI RICHED32_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
85                                    LPARAM lParam)
86 {
87     int RTFToBuffer(char* pBuffer, int nBufferSize);
88     LONG newstyle = 0;
89     LONG style = 0;  
90
91     static HWND hwndEdit;
92     static HWND hwndParent;
93     static char* rtfBuffer;
94     int rtfBufferSize;
95
96     TRACE("\n"); 
97    
98     switch (uMsg)
99     {
100  
101     case WM_CREATE :           
102             DPRINTF_EDIT_MSG32("WM_CREATE");
103                      
104             /* remove SCROLLBARS from the current window style */
105             hwndParent = ((LPCREATESTRUCTA) lParam)->hwndParent;
106
107             newstyle = style = ((LPCREATESTRUCTA) lParam)->style;
108             newstyle &= ~WS_HSCROLL;
109             newstyle &= ~WS_VSCROLL;
110             newstyle &= ~ES_AUTOHSCROLL;
111             newstyle &= ~ES_AUTOVSCROLL;
112                                   
113             hwndEdit = CreateWindowA ("edit", ((LPCREATESTRUCTA) lParam)->lpszName,
114                                    style, 0, 0, 0, 0,
115                                    hwnd, (HMENU) ID_EDIT,
116                                    ((LPCREATESTRUCTA) lParam)->hInstance, NULL) ;
117         
118             SetWindowLongA(hwnd,GWL_STYLE, newstyle);              
119             return 0 ;
120           
121     case WM_SETFOCUS :
122             DPRINTF_EDIT_MSG32("WM_SETFOCUS");            
123             SetFocus (hwndEdit) ;
124             return 0 ;
125
126     case WM_SIZE :             
127             DPRINTF_EDIT_MSG32("WM_SIZE");
128             MoveWindow (hwndEdit, 0, 0, LOWORD (lParam), HIWORD (lParam), TRUE) ;
129             return 0 ;
130           
131     case WM_COMMAND :
132         DPRINTF_EDIT_MSG32("WM_COMMAND");
133         switch(HIWORD(wParam)) {
134                 case EN_CHANGE:
135                 case EN_HSCROLL:
136                 case EN_KILLFOCUS:
137                 case EN_SETFOCUS:
138                 case EN_UPDATE:
139                 case EN_VSCROLL:
140                         return SendMessageA(hwndParent, WM_COMMAND, 
141                                 wParam, (LPARAM)(hwnd));
142                 
143                 case EN_ERRSPACE:
144                 case EN_MAXTEXT:
145                         MessageBoxA (hwnd, "RichEdit control out of space.",
146                                   "ERROR", MB_OK | MB_ICONSTOP) ;
147                         return 0 ;
148                 }
149      
150     case EM_STREAMIN:                           
151             DPRINTF_EDIT_MSG32("EM_STREAMIN");
152             
153             /* setup the RTF parser */
154             RTFSetEditStream(( EDITSTREAM*)lParam);
155             WriterInit();
156             RTFInit ();
157             BeginFile();            
158
159             /* do the parsing */
160             RTFRead ();
161             
162             rtfBufferSize = RTFToBuffer(NULL, 0);
163             rtfBuffer = HeapAlloc(RICHED32_hHeap, 0,rtfBufferSize*sizeof(char));
164             if(rtfBuffer)
165             {
166                 RTFToBuffer(rtfBuffer, rtfBufferSize);
167                 SetWindowTextA(hwndEdit,rtfBuffer);
168                 HeapFree(RICHED32_hHeap, 0,rtfBuffer);
169             }
170             else
171                 WARN("Not enough memory for a allocating rtfBuffer\n");
172                 
173             return 0;   
174
175 /* Message specific to Richedit controls */
176
177     case EM_AUTOURLDETECT:
178             DPRINTF_EDIT_MSG32("EM_AUTOURLDETECT");
179             return 0;
180
181     case EM_CANPASTE:
182             DPRINTF_EDIT_MSG32("EM_CANPASTE");
183             return 0;
184
185     case EM_CANREDO:
186             DPRINTF_EDIT_MSG32("EM_CANREDO");
187             return 0;
188
189     case EM_DISPLAYBAND:
190             DPRINTF_EDIT_MSG32("EM_DISPLAYBAND");
191             return 0;
192
193     case EM_EXGETSEL:
194             DPRINTF_EDIT_MSG32("EM_EXGETSEL");
195             return 0;
196
197     case EM_EXLIMITTEXT:
198             DPRINTF_EDIT_MSG32("EM_EXLIMITTEXT");
199             return 0;
200
201     case EM_EXLINEFROMCHAR:
202             DPRINTF_EDIT_MSG32("EM_EXLINEFROMCHAR");
203             return 0;
204
205     case EM_EXSETSEL:
206             DPRINTF_EDIT_MSG32("EM_EXSETSEL");
207             return 0;
208
209     case EM_FINDTEXT:
210             DPRINTF_EDIT_MSG32("EM_FINDTEXT");
211             return 0;
212
213     case EM_FINDTEXTEX:
214             DPRINTF_EDIT_MSG32("EM_FINDTEXTEX");
215             return 0;
216
217     case EM_FINDTEXTEXW:
218             DPRINTF_EDIT_MSG32("EM_FINDTEXTEXW");
219             return 0;
220
221     case EM_FINDTEXTW:
222             DPRINTF_EDIT_MSG32("EM_FINDTEXTW");
223             return 0;
224
225     case EM_FINDWORDBREAK:
226             DPRINTF_EDIT_MSG32("EM_FINDWORDBREAK");
227             return 0;
228
229     case EM_FORMATRANGE:
230             DPRINTF_EDIT_MSG32("EM_FORMATRANGE");
231             return 0;
232
233     case EM_GETAUTOURLDETECT:
234             DPRINTF_EDIT_MSG32("EM_GETAUTOURLDETECT");
235             return 0;
236
237     case EM_GETBIDIOPTIONS:
238             DPRINTF_EDIT_MSG32("EM_GETBIDIOPTIONS");
239             return 0;
240
241     case EM_GETCHARFORMAT:
242             DPRINTF_EDIT_MSG32("EM_GETCHARFORMAT");
243             return 0;
244
245     case EM_GETEDITSTYLE:
246             DPRINTF_EDIT_MSG32("EM_GETEDITSTYLE");
247             return 0;
248
249     case EM_GETEVENTMASK:
250             DPRINTF_EDIT_MSG32("EM_GETEVENTMASK");
251             return 0;
252
253     case EM_GETIMECOLOR:
254             DPRINTF_EDIT_MSG32("EM_GETIMECOLOR");
255             return 0;
256
257     case EM_GETIMECOMPMODE:
258             DPRINTF_EDIT_MSG32("EM_GETIMECOMPMODE");
259             return 0;
260
261     case EM_GETIMEOPTIONS:
262             DPRINTF_EDIT_MSG32("EM_GETIMEOPTIONS");
263             return 0;
264
265     case EM_GETLANGOPTIONS:
266             DPRINTF_EDIT_MSG32("EM_GETLANGOPTIONS");
267             return 0;
268
269     case EM_GETOLEINTERFACE:
270             DPRINTF_EDIT_MSG32("EM_GETOLEINTERFACE");
271             return 0;
272
273     case EM_GETOPTIONS:
274             DPRINTF_EDIT_MSG32("EM_GETOPTIONS");
275             return 0;
276
277     case EM_GETPARAFORMAT:
278             DPRINTF_EDIT_MSG32("EM_GETPARAFORMAT");
279             return 0;
280
281     case EM_GETPUNCTUATION:
282             DPRINTF_EDIT_MSG32("EM_GETPUNCTUATION");
283             return 0;
284
285     case EM_GETREDONAME:
286             DPRINTF_EDIT_MSG32("EM_GETREDONAME");
287             return 0;
288
289     case EM_GETSCROLLPOS:
290             DPRINTF_EDIT_MSG32("EM_GETSCROLLPOS");
291             return 0;
292
293     case EM_GETSELTEXT:
294             DPRINTF_EDIT_MSG32("EM_GETSELTEXT");
295             return 0;
296
297     case EM_GETTEXTEX:
298             DPRINTF_EDIT_MSG32("EM_GETTEXTEX");
299             return 0;
300
301     case EM_GETTEXTLENGTHEX:
302             DPRINTF_EDIT_MSG32("EM_GETTEXTLENGTHEX");
303             return 0;
304
305     case EM_GETTEXTMODE:
306             DPRINTF_EDIT_MSG32("EM_GETTEXTMODE");
307             return 0;
308
309     case EM_GETTEXTRANGE:
310             DPRINTF_EDIT_MSG32("EM_GETTEXTRANGE");
311             return 0;
312
313     case EM_GETTYPOGRAPHYOPTIONS:
314             DPRINTF_EDIT_MSG32("EM_GETTYPOGRAPHYOPTIONS");
315             return 0;
316
317     case EM_GETUNDONAME:
318             DPRINTF_EDIT_MSG32("EM_GETUNDONAME");
319             return 0;
320
321     case EM_GETWORDBREAKPROCEX:
322             DPRINTF_EDIT_MSG32("EM_GETWORDBREAKPROCEX");
323             return 0;
324
325     case EM_GETWORDWRAPMODE:
326             DPRINTF_EDIT_MSG32("EM_GETWORDWRAPMODE");
327             return 0;
328
329     case EM_GETZOOM:
330             DPRINTF_EDIT_MSG32("EM_GETZOOM");
331             return 0;
332
333     case EM_HIDESELECTION:
334             DPRINTF_EDIT_MSG32("EM_HIDESELECTION");
335             return 0;
336
337     case EM_PASTESPECIAL:
338             DPRINTF_EDIT_MSG32("EM_PASTESPECIAL");
339             return 0;
340
341     case EM_RECONVERSION:
342             DPRINTF_EDIT_MSG32("EM_RECONVERSION");
343             return 0;
344
345     case EM_REDO:
346             DPRINTF_EDIT_MSG32("EM_REDO");
347             return 0;
348
349     case EM_REQUESTRESIZE:
350             DPRINTF_EDIT_MSG32("EM_REQUESTRESIZE");
351             return 0;
352
353     case EM_SELECTIONTYPE:
354             DPRINTF_EDIT_MSG32("EM_SELECTIONTYPE");
355             return 0;
356
357     case EM_SETBIDIOPTIONS:
358             DPRINTF_EDIT_MSG32("EM_SETBIDIOPTIONS");
359             return 0;
360
361     case EM_SETBKGNDCOLOR:
362             DPRINTF_EDIT_MSG32("EM_SETBKGNDCOLOR");
363             return 0;
364
365     case EM_SETCHARFORMAT:
366             DPRINTF_EDIT_MSG32("EM_SETCHARFORMAT");
367             return 0;
368
369     case EM_SETEDITSTYLE:
370             DPRINTF_EDIT_MSG32("EM_SETEDITSTYLE");
371             return 0;
372
373     case EM_SETEVENTMASK:
374             DPRINTF_EDIT_MSG32("EM_SETEVENTMASK");
375             return 0;
376
377     case EM_SETFONTSIZE:
378             DPRINTF_EDIT_MSG32("EM_SETFONTSIZE");
379             return 0;
380
381     case EM_SETIMECOLOR:
382             DPRINTF_EDIT_MSG32("EM_SETIMECOLO");
383             return 0;
384
385     case EM_SETIMEOPTIONS:
386             DPRINTF_EDIT_MSG32("EM_SETIMEOPTIONS");
387             return 0;
388
389     case EM_SETLANGOPTIONS:
390             DPRINTF_EDIT_MSG32("EM_SETLANGOPTIONS");
391             return 0;
392
393     case EM_SETOLECALLBACK:
394             DPRINTF_EDIT_MSG32("EM_SETOLECALLBACK");
395             return 0;
396
397     case EM_SETOPTIONS:
398             DPRINTF_EDIT_MSG32("EM_SETOPTIONS");
399             return 0;
400
401     case EM_SETPALETTE:
402             DPRINTF_EDIT_MSG32("EM_SETPALETTE");
403             return 0;
404
405     case EM_SETPARAFORMAT:
406             DPRINTF_EDIT_MSG32("EM_SETPARAFORMAT");
407             return 0;
408
409     case EM_SETPUNCTUATION:
410             DPRINTF_EDIT_MSG32("EM_SETPUNCTUATION");
411             return 0;
412
413     case EM_SETSCROLLPOS:
414             DPRINTF_EDIT_MSG32("EM_SETSCROLLPOS");
415             return 0;
416
417     case EM_SETTARGETDEVICE:
418             DPRINTF_EDIT_MSG32("EM_SETTARGETDEVICE");
419             return 0;
420
421     case EM_SETTEXTEX:
422             DPRINTF_EDIT_MSG32("EM_SETTEXTEX");
423             return 0;
424
425     case EM_SETTEXTMODE:
426             DPRINTF_EDIT_MSG32("EM_SETTEXTMODE");
427             return 0;
428
429     case EM_SETTYPOGRAPHYOPTIONS:
430             DPRINTF_EDIT_MSG32("EM_SETTYPOGRAPHYOPTIONS");
431             return 0;
432
433     case EM_SETUNDOLIMIT:
434             DPRINTF_EDIT_MSG32("EM_SETUNDOLIMIT");
435             return 0;
436
437     case EM_SETWORDBREAKPROCEX:
438             DPRINTF_EDIT_MSG32("EM_SETWORDBREAKPROCEX");
439             return 0;
440
441     case EM_SETWORDWRAPMODE:
442             DPRINTF_EDIT_MSG32("EM_SETWORDWRAPMODE");
443             return 0;
444
445     case EM_SETZOOM:
446             DPRINTF_EDIT_MSG32("EM_SETZOOM");
447             return 0;
448
449     case EM_SHOWSCROLLBAR:
450             DPRINTF_EDIT_MSG32("EM_SHOWSCROLLBAR");
451             return 0;
452
453     case EM_STOPGROUPTYPING:
454             DPRINTF_EDIT_MSG32("EM_STOPGROUPTYPING");
455             return 0;
456
457     case EM_STREAMOUT:
458             DPRINTF_EDIT_MSG32("EM_STREAMOUT");
459             return 0;
460
461 /* Messaged dispatched to the edit control */
462     case EM_CANUNDO:
463     case EM_CHARFROMPOS:
464     case EM_EMPTYUNDOBUFFER:
465     case EM_FMTLINES:
466     case EM_GETFIRSTVISIBLELINE:
467     case EM_GETHANDLE:
468 /*    case EM_GETIMESTATUS:*/
469     case EM_GETLIMITTEXT:
470     case EM_GETLINE:
471     case EM_GETLINECOUNT:
472     case EM_GETMARGINS:
473     case EM_GETMODIFY:
474     case EM_GETPASSWORDCHAR:
475     case EM_GETRECT:
476     case EM_GETSEL:
477     case EM_GETTHUMB:
478     case EM_GETWORDBREAKPROC:
479     case EM_LINEFROMCHAR:
480     case EM_LINEINDEX:
481     case EM_LINELENGTH:
482     case EM_LINESCROLL:
483     case EM_POSFROMCHAR:
484     case EM_REPLACESEL:
485     case EM_SCROLL:
486     case EM_SCROLLCARET:
487     case EM_SETHANDLE:
488 /*    case EM_SETIMESTATUS:*/
489     case EM_SETLIMITTEXT:
490     case EM_SETMARGINS:
491     case EM_SETMODIFY:
492     case EM_SETPASSWORDCHAR:
493     case EM_SETREADONLY:
494     case EM_SETRECT:
495     case EM_SETRECTNP:
496     case EM_SETSEL:
497     case EM_SETTABSTOPS:
498     case EM_SETWORDBREAKPROC:
499     case EM_UNDO:
500
501     case WM_STYLECHANGING:
502     case WM_STYLECHANGED:
503     case WM_NCCALCSIZE:
504     case WM_GETTEXT:
505     case WM_GETTEXTLENGTH:
506     case WM_SETTEXT:
507             return SendMessageA( hwndEdit, uMsg, wParam, lParam);
508
509     /* Messages known , but ignored. */ 
510     case WM_NCPAINT:
511         DPRINTF_EDIT_MSG32("WM_NCPAINT");
512         return DefWindowProcA( hwnd,uMsg,wParam,lParam);
513     case WM_PAINT:
514         DPRINTF_EDIT_MSG32("WM_PAINT");
515         return DefWindowProcA( hwnd,uMsg,wParam,lParam);
516     case WM_ERASEBKGND:
517         DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
518         return DefWindowProcA( hwnd,uMsg,wParam,lParam);
519     case WM_KILLFOCUS:
520         DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
521         return DefWindowProcA( hwnd,uMsg,wParam,lParam);
522     case WM_DESTROY:
523         DPRINTF_EDIT_MSG32("WM_DESTROY");
524         return DefWindowProcA( hwnd,uMsg,wParam,lParam);
525     case WM_CHILDACTIVATE:             
526         DPRINTF_EDIT_MSG32("WM_CHILDACTIVATE");
527         return DefWindowProcA( hwnd,uMsg,wParam,lParam);
528
529     case WM_WINDOWPOSCHANGING:
530         DPRINTF_EDIT_MSG32("WM_WINDOWPOSCHANGING");
531         return DefWindowProcA( hwnd,uMsg,wParam,lParam);
532     case WM_WINDOWPOSCHANGED:
533         DPRINTF_EDIT_MSG32("WM_WINDOWPOSCHANGED");
534         return DefWindowProcA( hwnd,uMsg,wParam,lParam);
535 /*    case WM_INITIALUPDATE:
536         DPRINTF_EDIT_MSG32("WM_INITIALUPDATE");
537         return DefWindowProcA( hwnd,uMsg,wParam,lParam); */
538     case WM_CTLCOLOREDIT:
539         DPRINTF_EDIT_MSG32("WM_CTLCOLOREDIT");
540         return DefWindowProcA( hwnd,uMsg,wParam,lParam);
541     case WM_SETCURSOR:
542         DPRINTF_EDIT_MSG32("WM_SETCURSOR");
543         return DefWindowProcA( hwnd,uMsg,wParam,lParam);
544     case WM_MOVE:
545         DPRINTF_EDIT_MSG32("WM_MOVE");
546         return DefWindowProcA( hwnd,uMsg,wParam,lParam);
547     case WM_SHOWWINDOW:
548         DPRINTF_EDIT_MSG32("WM_SHOWWINDOW");
549         return DefWindowProcA( hwnd,uMsg,wParam,lParam);
550
551     }
552
553
554     FIXME("Unknown message %d\n", uMsg);
555     return DefWindowProcA( hwnd,uMsg,wParam,lParam);
556 }
557
558 /***********************************************************************
559  * DllGetVersion [RICHED32.2]
560  *
561  * Retrieves version information of the 'RICHED32.DLL'
562  *
563  * PARAMS
564  *     pdvi [O] pointer to version information structure.
565  *
566  * RETURNS
567  *     Success: S_OK
568  *     Failure: E_INVALIDARG
569  *
570  * NOTES
571  *     Returns version of a comctl32.dll from IE4.01 SP1.
572  */
573
574 HRESULT WINAPI
575 RICHED32_DllGetVersion (DLLVERSIONINFO *pdvi)
576 {
577     TRACE("\n");
578
579     if (pdvi->cbSize != sizeof(DLLVERSIONINFO)) {
580  
581         return E_INVALIDARG;
582     }
583
584     pdvi->dwMajorVersion = 4;
585     pdvi->dwMinorVersion = 0;
586     pdvi->dwBuildNumber = 0;
587     pdvi->dwPlatformID = 0;
588
589     return S_OK;
590 }
591
592 /***
593  * DESCRIPTION:
594  * Registers the window class.
595  * 
596  * PARAMETER(S):
597  * None
598  *
599  * RETURN:
600  * None
601  */
602 VOID RICHED32_Register(void)
603 {
604     WNDCLASSA wndClass; 
605
606     TRACE("\n");
607
608     ZeroMemory(&wndClass, sizeof(WNDCLASSA));
609     wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
610     wndClass.lpfnWndProc = (WNDPROC)RICHED32_WindowProc;
611     wndClass.cbClsExtra = 0;
612     wndClass.cbWndExtra = 0; /*(sizeof(RICHED32_INFO *);*/
613     wndClass.hCursor = LoadCursorA(0, IDC_ARROWA);
614     wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
615     wndClass.lpszClassName = RICHEDIT_CLASS10A;//WC_RICHED32A;
616
617     RegisterClassA (&wndClass);
618 }
619
620 /***
621  * DESCRIPTION:
622  * Unregisters the window class.
623  * 
624  * PARAMETER(S):
625  * None
626  *
627  * RETURN:
628  * None
629  */
630 VOID RICHED32_Unregister(void)
631 {
632     TRACE("\n");
633
634     UnregisterClassA(RICHEDIT_CLASS10A, (HINSTANCE)NULL);
635 }