Don't include windows.h internally.
[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 DWORD  RICHED32_dwProcessesAttached = 0;
31 /* LPSTR  RICHED32_aSubclass = (LPSTR)NULL; */
32 HMODULE RICHED32_hModule = 0;
33
34 /***********************************************************************
35  * RICHED32_LibMain [Internal] Initializes the internal 'RICHED32.DLL'.
36  *
37  * PARAMS
38  *     hinstDLL    [I] handle to the DLL's instance
39  *     fdwReason   [I]
40  *     lpvReserved [I] reserved, must be NULL
41  *
42  * RETURNS
43  *     Success: TRUE
44  *     Failure: FALSE
45  */
46
47 BOOL WINAPI
48 RICHED32_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
49 {
50         
51     switch (fdwReason) {
52             case DLL_PROCESS_ATTACH:
53
54             if (RICHED32_dwProcessesAttached == 0) {
55
56                     /* This will be wrong for any other process attching in this address-space! */
57                     RICHED32_hModule = (HMODULE)hinstDLL;
58
59                         /* create private heap */
60                         RICHED32_hHeap = HeapCreate (0, 0x10000, 0);
61
62                 }
63                 
64                 /* register the Rich Edit class */
65                 RICHED32_Register ();
66
67                 RICHED32_dwProcessesAttached++;
68                 break;
69
70             case DLL_PROCESS_DETACH:
71                 RICHED32_dwProcessesAttached--;
72
73                 /* unregister all common control classes */      
74                 RICHED32_Unregister ();
75
76                 if (RICHED32_dwProcessesAttached == 0) {
77                     HeapDestroy (RICHED32_hHeap);
78                     RICHED32_hHeap = (HANDLE)NULL;
79                 }
80                 break;      
81     }
82
83     return TRUE;
84 }
85
86 /*
87  *
88  * DESCRIPTION:
89  * Window procedure of the RichEdit control.
90  *
91  */
92 static LRESULT WINAPI RICHED32_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
93                                    LPARAM lParam)
94 {
95     int RTFToBuffer(char* pBuffer, int nBufferSize);
96     LONG newstyle = 0;
97     LONG style = 0;  
98
99     static HWND hwndEdit;
100     static char* rtfBuffer;
101     int rtfBufferSize;
102     
103     switch (uMsg)
104     {
105  
106     case WM_CREATE :           
107             
108             /* remove SCROLLBARS from the current window style */
109             newstyle = style = ((LPCREATESTRUCTA) lParam)->style;
110             newstyle &= ~WS_HSCROLL;
111             newstyle &= ~WS_VSCROLL;
112             newstyle &= ~ES_AUTOHSCROLL;
113             newstyle &= ~ES_AUTOVSCROLL;
114                                   
115             hwndEdit = CreateWindowA ("edit", ((LPCREATESTRUCTA) lParam)->lpszName,
116                                    style, 0, 0, 0, 0,
117                                    hwnd, (HMENU) ID_EDIT,
118                                    ((LPCREATESTRUCTA) lParam)->hInstance, NULL) ;
119         
120             SetWindowLongA(hwnd,GWL_STYLE, newstyle);              
121             return 0 ;
122           
123     case WM_SETFOCUS :            
124             SetFocus (hwndEdit) ;
125             return 0 ;
126           
127     case WM_SIZE :             
128             MoveWindow (hwndEdit, 0, 0, LOWORD (lParam), HIWORD (lParam), TRUE) ;
129             return 0 ;
130           
131     case WM_COMMAND :
132             if (LOWORD (wParam) == ID_EDIT)
133                  if (HIWORD (wParam) == EN_ERRSPACE || 
134                            HIWORD (wParam) == EN_MAXTEXT)
135
136                       MessageBoxA (hwnd, "RichEdit control out of space.",
137                                   "ERROR", MB_OK | MB_ICONSTOP) ;
138             return 0 ;
139      
140     case EM_STREAMIN:                               
141             
142             /* setup the RTF parser */
143             RTFSetEditStream(( EDITSTREAM*)lParam);
144             WriterInit();
145             RTFInit ();
146             BeginFile();            
147
148             /* do the parsing */
149             RTFRead ();
150             
151             rtfBufferSize = RTFToBuffer(NULL, 0);
152             rtfBuffer = HeapAlloc(RICHED32_hHeap, 0,rtfBufferSize*sizeof(char));
153             if(rtfBuffer)
154             {
155                 RTFToBuffer(rtfBuffer, rtfBufferSize);
156                 SetWindowTextA(hwndEdit,rtfBuffer);
157                 HeapFree(RICHED32_hHeap, 0,rtfBuffer);
158             }
159             else
160                 WARN("Not enough memory for a allocating rtfBuffer\n");
161                 
162             return 0;   
163     }
164     /*return SendMessageA( hwndEdit,uMsg,wParam,lParam);*/
165     return DefWindowProcA( hwnd,uMsg,wParam,lParam);
166 }
167
168 /***********************************************************************
169  * DllGetVersion [COMCTL32.25]
170  *
171  * Retrieves version information of the 'COMCTL32.DLL'
172  *
173  * PARAMS
174  *     pdvi [O] pointer to version information structure.
175  *
176  * RETURNS
177  *     Success: S_OK
178  *     Failure: E_INVALIDARG
179  *
180  * NOTES
181  *     Returns version of a comctl32.dll from IE4.01 SP1.
182  */
183
184 HRESULT WINAPI
185 RICHED32_DllGetVersion (DLLVERSIONINFO *pdvi)
186 {
187     if (pdvi->cbSize != sizeof(DLLVERSIONINFO)) {
188  
189         return E_INVALIDARG;
190     }
191
192     pdvi->dwMajorVersion = 4;
193     pdvi->dwMinorVersion = 0;
194     pdvi->dwBuildNumber = 0;
195     pdvi->dwPlatformID = 0;
196
197     return S_OK;
198 }
199
200 /***
201  * DESCRIPTION:
202  * Registers the window class.
203  * 
204  * PARAMETER(S):
205  * None
206  *
207  * RETURN:
208  * None
209  */
210 VOID RICHED32_Register(void)
211 {
212     WNDCLASSA wndClass; 
213
214     ZeroMemory(&wndClass, sizeof(WNDCLASSA));
215     wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
216     wndClass.lpfnWndProc = (WNDPROC)RICHED32_WindowProc;
217     wndClass.cbClsExtra = 0;
218     wndClass.cbWndExtra = 0; /*(sizeof(RICHED32_INFO *);*/
219     wndClass.hCursor = LoadCursorA(0, IDC_ARROWA);
220     wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
221     wndClass.lpszClassName = RICHEDIT_CLASS10A;//WC_RICHED32A;
222
223     RegisterClassA (&wndClass);
224 }
225
226 /***
227  * DESCRIPTION:
228  * Unregisters the window class.
229  * 
230  * PARAMETER(S):
231  * None
232  *
233  * RETURN:
234  * None
235  */
236 VOID RICHED32_Unregister(void)
237 {
238     UnregisterClassA(RICHEDIT_CLASS10A, (HINSTANCE)NULL);
239 }