Cleaned up dll startup routines now that we have separate address
[wine] / dlls / richedit / richedit.c
1 /*
2  * RichEdit32  functions
3  *
4  * This module is a simple wrap-arround the edit controls.
5  * At the point, it is good only for application who use the RICHEDIT control to 
6  * display RTF text.
7  *
8  * Copyright 2000 by Jean-Claude Batista
9  * 
10  */
11  
12 #include "windef.h"
13 #include "winbase.h"
14 #include "heap.h"
15 #include "debugtools.h"
16 #include "winerror.h"
17 #include "riched32.h"
18 #include "richedit.h"
19 #include "charlist.h"
20 #include "shlwapi.h"
21
22 #include "rtf.h"
23 #include "rtf2text.h"
24
25 #define ID_EDIT      1
26
27 DEFAULT_DEBUG_CHANNEL(richedit);
28
29 HANDLE RICHED32_hHeap = (HANDLE)NULL;
30 /* LPSTR  RICHED32_aSubclass = (LPSTR)NULL; */
31
32 /***********************************************************************
33  * RICHED32_LibMain [Internal] Initializes the internal 'RICHED32.DLL'.
34  *
35  * PARAMS
36  *     hinstDLL    [I] handle to the DLL's instance
37  *     fdwReason   [I]
38  *     lpvReserved [I] reserved, must be NULL
39  *
40  * RETURNS
41  *     Success: TRUE
42  *     Failure: FALSE
43  */
44
45 BOOL WINAPI
46 RICHED32_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
47 {
48     switch (fdwReason)
49     {
50     case DLL_PROCESS_ATTACH:
51         /* create private heap */
52         RICHED32_hHeap = HeapCreate (0, 0x10000, 0);
53         /* register the Rich Edit class */
54         RICHED32_Register ();
55         break;
56
57     case DLL_PROCESS_DETACH:
58         /* unregister all common control classes */
59         RICHED32_Unregister ();
60         HeapDestroy (RICHED32_hHeap);
61         RICHED32_hHeap = (HANDLE)NULL;
62         break;
63     }
64     return TRUE;
65 }
66
67 /*
68  *
69  * DESCRIPTION:
70  * Window procedure of the RichEdit control.
71  *
72  */
73 static LRESULT WINAPI RICHED32_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
74                                    LPARAM lParam)
75 {
76     int RTFToBuffer(char* pBuffer, int nBufferSize);
77     LONG newstyle = 0;
78     LONG style = 0;  
79
80     static HWND hwndEdit;
81     static char* rtfBuffer;
82     int rtfBufferSize;
83     
84     switch (uMsg)
85     {
86  
87     case WM_CREATE :           
88             
89             /* remove SCROLLBARS from the current window style */
90             newstyle = style = ((LPCREATESTRUCTA) lParam)->style;
91             newstyle &= ~WS_HSCROLL;
92             newstyle &= ~WS_VSCROLL;
93             newstyle &= ~ES_AUTOHSCROLL;
94             newstyle &= ~ES_AUTOVSCROLL;
95                                   
96             hwndEdit = CreateWindowA ("edit", ((LPCREATESTRUCTA) lParam)->lpszName,
97                                    style, 0, 0, 0, 0,
98                                    hwnd, (HMENU) ID_EDIT,
99                                    ((LPCREATESTRUCTA) lParam)->hInstance, NULL) ;
100         
101             SetWindowLongA(hwnd,GWL_STYLE, newstyle);              
102             return 0 ;
103           
104     case WM_SETFOCUS :            
105             SetFocus (hwndEdit) ;
106             return 0 ;
107           
108     case WM_SIZE :             
109             MoveWindow (hwndEdit, 0, 0, LOWORD (lParam), HIWORD (lParam), TRUE) ;
110             return 0 ;
111           
112     case WM_COMMAND :
113             if (LOWORD (wParam) == ID_EDIT)
114                  if (HIWORD (wParam) == EN_ERRSPACE || 
115                            HIWORD (wParam) == EN_MAXTEXT)
116
117                       MessageBoxA (hwnd, "RichEdit control out of space.",
118                                   "ERROR", MB_OK | MB_ICONSTOP) ;
119             return 0 ;
120      
121     case EM_STREAMIN:                               
122             
123             /* setup the RTF parser */
124             RTFSetEditStream(( EDITSTREAM*)lParam);
125             WriterInit();
126             RTFInit ();
127             BeginFile();            
128
129             /* do the parsing */
130             RTFRead ();
131             
132             rtfBufferSize = RTFToBuffer(NULL, 0);
133             rtfBuffer = HeapAlloc(RICHED32_hHeap, 0,rtfBufferSize*sizeof(char));
134             if(rtfBuffer)
135             {
136                 RTFToBuffer(rtfBuffer, rtfBufferSize);
137                 SetWindowTextA(hwndEdit,rtfBuffer);
138                 HeapFree(RICHED32_hHeap, 0,rtfBuffer);
139             }
140             else
141                 WARN("Not enough memory for a allocating rtfBuffer\n");
142                 
143             return 0;   
144     }
145     /*return SendMessageA( hwndEdit,uMsg,wParam,lParam);*/
146     return DefWindowProcA( hwnd,uMsg,wParam,lParam);
147 }
148
149 /***********************************************************************
150  * DllGetVersion [COMCTL32.25]
151  *
152  * Retrieves version information of the 'COMCTL32.DLL'
153  *
154  * PARAMS
155  *     pdvi [O] pointer to version information structure.
156  *
157  * RETURNS
158  *     Success: S_OK
159  *     Failure: E_INVALIDARG
160  *
161  * NOTES
162  *     Returns version of a comctl32.dll from IE4.01 SP1.
163  */
164
165 HRESULT WINAPI
166 RICHED32_DllGetVersion (DLLVERSIONINFO *pdvi)
167 {
168     if (pdvi->cbSize != sizeof(DLLVERSIONINFO)) {
169  
170         return E_INVALIDARG;
171     }
172
173     pdvi->dwMajorVersion = 4;
174     pdvi->dwMinorVersion = 0;
175     pdvi->dwBuildNumber = 0;
176     pdvi->dwPlatformID = 0;
177
178     return S_OK;
179 }
180
181 /***
182  * DESCRIPTION:
183  * Registers the window class.
184  * 
185  * PARAMETER(S):
186  * None
187  *
188  * RETURN:
189  * None
190  */
191 VOID RICHED32_Register(void)
192 {
193     WNDCLASSA wndClass; 
194
195     ZeroMemory(&wndClass, sizeof(WNDCLASSA));
196     wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
197     wndClass.lpfnWndProc = (WNDPROC)RICHED32_WindowProc;
198     wndClass.cbClsExtra = 0;
199     wndClass.cbWndExtra = 0; /*(sizeof(RICHED32_INFO *);*/
200     wndClass.hCursor = LoadCursorA(0, IDC_ARROWA);
201     wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
202     wndClass.lpszClassName = RICHEDIT_CLASS10A;//WC_RICHED32A;
203
204     RegisterClassA (&wndClass);
205 }
206
207 /***
208  * DESCRIPTION:
209  * Unregisters the window class.
210  * 
211  * PARAMETER(S):
212  * None
213  *
214  * RETURN:
215  * None
216  */
217 VOID RICHED32_Unregister(void)
218 {
219     UnregisterClassA(RICHEDIT_CLASS10A, (HINSTANCE)NULL);
220 }