atl: Implement AtlAxCreateControl and AtlAxCreateControlEx.
[wine] / dlls / atl / atl_main.c
1 /*
2  * Implementation of Active Template Library (atl.dll)
3  *
4  * Copyright 2004 Aric Stewart for CodeWeavers
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "wine/debug.h"
32 #include "objbase.h"
33 #include "objidl.h"
34 #include "ole2.h"
35 #include "atlbase.h"
36 #include "atliface.h"
37 #include "atlwin.h"
38
39 #include "wine/unicode.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(atl);
42
43 HINSTANCE hInst;
44
45 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
46 {
47     TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
48
49     if (fdwReason == DLL_PROCESS_ATTACH) {
50         DisableThreadLibraryCalls(hinstDLL);
51         hInst = hinstDLL;
52     }
53     return TRUE;
54 }
55
56 #define ATLVer1Size 100
57
58 HRESULT WINAPI AtlModuleInit(_ATL_MODULEA* pM, _ATL_OBJMAP_ENTRYA* p, HINSTANCE h)
59 {
60     INT i;
61     UINT size;
62
63     FIXME("SEMI-STUB (%p %p %p)\n",pM,p,h);
64
65     size = pM->cbSize;
66     if  (size != sizeof(_ATL_MODULEA) && size != ATLVer1Size)
67     {
68         FIXME("Unknown structure version (size %i)\n",size);
69         return E_INVALIDARG;
70     }
71     
72     memset(pM,0,pM->cbSize);
73     pM->cbSize = size;
74     pM->m_hInst = h;
75     pM->m_hInstResource = h;
76     pM->m_hInstTypeLib = h;
77     pM->m_pObjMap = p;
78     pM->m_hHeap = GetProcessHeap();
79
80     InitializeCriticalSection(&pM->u.m_csTypeInfoHolder);
81     InitializeCriticalSection(&pM->m_csWindowCreate);
82     InitializeCriticalSection(&pM->m_csObjMap);
83
84     /* call mains */
85     i = 0;
86     if (pM->m_pObjMap != NULL  && size > ATLVer1Size)
87     {
88         while (pM->m_pObjMap[i].pclsid != NULL)
89         {
90             TRACE("Initializing object %i %p\n",i,p[i].pfnObjectMain);
91             if (p[i].pfnObjectMain)
92                 p[i].pfnObjectMain(TRUE);
93             i++;
94         }
95     }
96
97     return S_OK;
98 }
99
100 HRESULT WINAPI AtlModuleLoadTypeLib(_ATL_MODULEA *pM, LPCOLESTR lpszIndex, 
101                                     BSTR *pbstrPath, ITypeLib **ppTypeLib)
102 {
103     FIXME("(%p, %s, %p, %p): stub\n", pM, debugstr_w(lpszIndex), pbstrPath, ppTypeLib);
104     return E_FAIL;
105 }
106
107 HRESULT WINAPI AtlModuleTerm(_ATL_MODULEA* pM)
108 {
109     _ATL_TERMFUNC_ELEM *iter = pM->m_pTermFuncs, *tmp;
110
111     TRACE("(%p)\n", pM);
112
113     while(iter) {
114         iter->pFunc(iter->dw);
115         tmp = iter;
116         iter = iter->pNext;
117         HeapFree(GetProcessHeap(), 0, tmp);
118     }
119
120     HeapFree(GetProcessHeap(), 0, pM);
121
122     return S_OK;
123 }
124
125 HRESULT WINAPI AtlModuleAddTermFunc(_ATL_MODULEW *pM, _ATL_TERMFUNC *pFunc, DWORD_PTR dw)
126 {
127     _ATL_TERMFUNC_ELEM *termfunc_elem;
128
129     TRACE("(%p %p %ld)\n", pM, pFunc, dw);
130
131     termfunc_elem = HeapAlloc(GetProcessHeap(), 0, sizeof(_ATL_TERMFUNC_ELEM));
132     termfunc_elem->pFunc = pFunc;
133     termfunc_elem->dw = dw;
134     termfunc_elem->pNext = pM->m_pTermFuncs;
135
136     pM->m_pTermFuncs = termfunc_elem;
137
138     return S_OK;
139 }
140
141 HRESULT WINAPI AtlModuleRegisterClassObjects(_ATL_MODULEA *pM, DWORD dwClsContext,
142                                              DWORD dwFlags)
143 {
144     HRESULT hRes = S_OK;
145     int i=0;
146
147     TRACE("(%p %i %i)\n",pM, dwClsContext, dwFlags);
148
149     if (pM == NULL)
150         return E_INVALIDARG;
151
152     while(pM->m_pObjMap[i].pclsid != NULL)
153     {
154         IUnknown* pUnknown;
155         _ATL_OBJMAP_ENTRYA *obj = &(pM->m_pObjMap[i]);
156         HRESULT rc;
157
158         TRACE("Registering object %i\n",i);
159         if (obj->pfnGetClassObject)
160         {
161             rc = obj->pfnGetClassObject(obj->pfnCreateInstance, &IID_IUnknown,
162                                    (LPVOID*)&pUnknown);
163             if (SUCCEEDED (rc) )
164             {
165                 CoRegisterClassObject(obj->pclsid, pUnknown, dwClsContext,
166                                       dwFlags, &obj->dwRegister);
167                 if (pUnknown)
168                     IUnknown_Release(pUnknown);
169             }
170         }
171         i++;
172     }
173
174    return hRes;
175 }
176
177 HRESULT WINAPI AtlModuleUnregisterServerEx(_ATL_MODULEA* pM, BOOL bUnRegTypeLib, const CLSID* pCLSID)
178 {
179     FIXME("(%p, %i, %p) stub\n", pM, bUnRegTypeLib, pCLSID);
180     return S_OK;
181 }
182
183
184 IUnknown* WINAPI AtlComPtrAssign(IUnknown** pp, IUnknown *p)
185 {
186     TRACE("(%p %p)\n", pp, p);
187
188     if (p) IUnknown_AddRef(p);
189     if (*pp) IUnknown_Release(*pp);
190     *pp = p;
191     return p;
192 }
193
194
195 HRESULT WINAPI AtlInternalQueryInterface(LPVOID this, const _ATL_INTMAP_ENTRY* pEntries,  REFIID iid, LPVOID* ppvObject)
196 {
197     int i = 0;
198     HRESULT rc = E_NOINTERFACE;
199     TRACE("(%p, %p, %p, %p)\n",this, pEntries, iid, ppvObject);
200
201     if (IsEqualGUID(iid,&IID_IUnknown))
202     {
203         TRACE("Returning IUnknown\n");
204         *ppvObject = this;
205         IUnknown_AddRef((IUnknown*)this);
206         return S_OK;
207     }
208
209     while (pEntries[i].pFunc != 0)
210     {
211         TRACE("Trying entry %i (%p %i %p)\n",i,pEntries[i].piid,
212               pEntries[i].dw, pEntries[i].pFunc);
213
214         if (pEntries[i].piid && IsEqualGUID(iid,pEntries[i].piid))
215         {
216             TRACE("MATCH\n");
217             if (pEntries[i].pFunc == (_ATL_CREATORARGFUNC*)1)
218             {
219                 TRACE("Offset\n");
220                 *ppvObject = ((LPSTR)this+pEntries[i].dw);
221                 IUnknown_AddRef((IUnknown*)this);
222                 rc = S_OK;
223             }
224             else
225             {
226                 TRACE("Function\n");
227                 rc = pEntries[i].pFunc(this, iid, ppvObject,0);
228             }
229             break;
230         }
231         i++;
232     }
233     TRACE("Done returning (0x%x)\n",rc);
234     return rc;
235 }
236
237 /***********************************************************************
238  *           AtlModuleRegisterServer         [ATL.@]
239  *
240  */
241 HRESULT WINAPI AtlModuleRegisterServer(_ATL_MODULEW* pM, BOOL bRegTypeLib, const CLSID* clsid) 
242 {
243     FIXME("%p %d %s\n", pM, bRegTypeLib, debugstr_guid(clsid));
244     return S_OK;
245 }
246
247 /***********************************************************************
248  *           AtlAdvise         [ATL.@]
249  */
250 HRESULT WINAPI AtlAdvise(IUnknown *pUnkCP, IUnknown *pUnk, const IID *iid, LPDWORD pdw)
251 {
252     FIXME("%p %p %p %p\n", pUnkCP, pUnk, iid, pdw);
253     return E_FAIL;
254 }
255
256 /***********************************************************************
257  *           AtlUnadvise         [ATL.@]
258  */
259 HRESULT WINAPI AtlUnadvise(IUnknown *pUnkCP, const IID *iid, DWORD dw)
260 {
261     FIXME("%p %p %d\n", pUnkCP, iid, dw);
262     return S_OK;
263 }
264
265 /***********************************************************************
266  *           AtlFreeMarshalStream         [ATL.@]
267  */
268 HRESULT WINAPI AtlFreeMarshalStream(IStream *stm)
269 {
270     FIXME("%p\n", stm);
271     return S_OK;
272 }
273
274 /***********************************************************************
275  *           AtlMarshalPtrInProc         [ATL.@]
276  */
277 HRESULT WINAPI AtlMarshalPtrInProc(IUnknown *pUnk, const IID *iid, IStream **pstm)
278 {
279     FIXME("%p %p %p\n", pUnk, iid, pstm);
280     return E_FAIL;
281 }
282
283 /***********************************************************************
284  *           AtlUnmarshalPtr              [ATL.@]
285  */
286 HRESULT WINAPI AtlUnmarshalPtr(IStream *stm, const IID *iid, IUnknown **ppUnk)
287 {
288     FIXME("%p %p %p\n", stm, iid, ppUnk);
289     return E_FAIL;
290 }
291
292 /***********************************************************************
293  *           AtlModuleGetClassObject              [ATL.@]
294  */
295 HRESULT WINAPI AtlModuleGetClassObject(_ATL_MODULEW *pm, REFCLSID rclsid,
296                                        REFIID riid, LPVOID *ppv)
297 {
298     int i;
299
300     TRACE("%p %s %s %p\n", pm, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
301
302     if (pm == NULL)
303         return E_INVALIDARG;
304
305     for (i = 0; pm->m_pObjMap[i].pclsid != NULL; i++)
306     {
307         if (IsEqualCLSID(pm->m_pObjMap[i].pclsid, rclsid))
308         {
309             _ATL_OBJMAP_ENTRYW *obj = &pm->m_pObjMap[i];
310
311             TRACE("found object %i\n", i);
312             if (obj->pfnGetClassObject)
313                 return obj->pfnGetClassObject(obj->pfnCreateInstance, riid, ppv);
314         }
315     }
316
317     WARN("no class object found for %s\n", debugstr_guid(rclsid));
318
319     return E_FAIL;
320 }
321
322 /***********************************************************************
323  *           AtlModuleGetClassObject              [ATL.@]
324  */
325 HRESULT WINAPI AtlModuleRegisterTypeLib(_ATL_MODULEW *pm, LPCOLESTR lpszIndex)
326 {
327     FIXME("%p %s\n", pm, debugstr_w(lpszIndex));
328     return E_FAIL;
329 }
330
331 /***********************************************************************
332  *           AtlModuleRevokeClassObjects          [ATL.@]
333  */
334 HRESULT WINAPI AtlModuleRevokeClassObjects(_ATL_MODULEW *pm)
335 {
336     FIXME("%p\n", pm);
337     return E_FAIL;
338 }
339
340 /***********************************************************************
341  *           AtlModuleUnregisterServer           [ATL.@]
342  */
343 HRESULT WINAPI AtlModuleUnregisterServer(_ATL_MODULEW *pm, const CLSID *clsid)
344 {
345     FIXME("%p %s\n", pm, debugstr_guid(clsid));
346     return E_FAIL;
347 }
348
349 /***********************************************************************
350  *           AtlModuleRegisterWndClassInfoW           [ATL.@]
351  *
352  * PARAMS
353  *  pm   [IO] Information about the module registering the window.
354  *  wci  [IO] Information about the window being registered.
355  *  pProc [O] Window procedure of the registered class.
356  *
357  * RETURNS
358  *  Atom representing the registered class.
359  *
360  * NOTES
361  *  Can be called multiple times without error, unlike RegisterClassEx().
362  *
363  *  If the class name is NULL then it a class with a name of "ATLxxxxxxxx" is
364  *  registered, where the x's represent an unique value.
365  *  
366  */
367 ATOM WINAPI AtlModuleRegisterWndClassInfoW(_ATL_MODULEW *pm, _ATL_WNDCLASSINFOW *wci, WNDPROC *pProc)
368 {
369     ATOM atom;
370
371     FIXME("%p %p %p semi-stub\n", pm, wci, pProc);
372
373     atom = wci->m_atom;
374     if (!atom)
375     {
376         WNDCLASSEXW wc;
377
378         TRACE("wci->m_wc.lpszClassName = %s\n", debugstr_w(wci->m_wc.lpszClassName));
379
380         if (!wci->m_wc.lpszClassName)
381         {
382             static const WCHAR szFormat[] = {'A','T','L','%','0','8','x',0};
383             sprintfW(wci->m_szAutoName, szFormat, (UINT)(UINT_PTR)wci);
384             TRACE("auto-generated class name %s\n", debugstr_w(wci->m_szAutoName));
385             wci->m_wc.lpszClassName = wci->m_szAutoName;
386         }
387
388         atom = GetClassInfoExW(pm->m_hInst, wci->m_wc.lpszClassName, &wc);
389         if (!atom)
390             atom = RegisterClassExW(&wci->m_wc);
391
392         wci->pWndProc = wci->m_wc.lpfnWndProc;
393         wci->m_atom = atom;
394     }
395     *pProc = wci->pWndProc;
396
397     TRACE("returning 0x%04x\n", atom);
398     return atom;
399 }
400
401 void WINAPI AtlHiMetricToPixel(const SIZEL* lpHiMetric, SIZEL* lpPix)
402 {
403     HDC dc = GetDC(NULL);
404     lpPix->cx = lpHiMetric->cx * GetDeviceCaps( dc, LOGPIXELSX ) / 100;
405     lpPix->cy = lpHiMetric->cy * GetDeviceCaps( dc, LOGPIXELSY ) / 100;
406     ReleaseDC( NULL, dc );
407 }
408
409 void WINAPI AtlPixelToHiMetric(const SIZEL* lpPix, SIZEL* lpHiMetric)
410 {
411     HDC dc = GetDC(NULL);
412     lpHiMetric->cx = 100 * lpPix->cx / GetDeviceCaps( dc, LOGPIXELSX );
413     lpHiMetric->cy = 100 * lpPix->cy / GetDeviceCaps( dc, LOGPIXELSY );
414     ReleaseDC( NULL, dc );
415 }
416
417 /***********************************************************************
418  *           AtlModuleAddCreateWndData          [ATL.@]
419  */
420 void WINAPI AtlModuleAddCreateWndData(_ATL_MODULEW *pM, _AtlCreateWndData *pData, LPVOID pvObject)
421 {
422     TRACE("(%p, %p, %p)\n", pM, pData, pvObject);
423
424     pData->m_pThis = pvObject;
425     pData->m_dwThreadID = GetCurrentThreadId();
426     pData->m_pNext = pM->m_pCreateWndList;
427     pM->m_pCreateWndList = pData;
428 }
429
430 /***********************************************************************
431  *           AtlModuleExtractCreateWndData      [ATL.@]
432  *
433  *  NOTE: I failed to find any good description of this function.
434  *        Tests show that this function extracts one of _AtlCreateWndData
435  *        records from the current thread from a list 
436  *        
437  */
438 LPVOID WINAPI AtlModuleExtractCreateWndData(_ATL_MODULEW *pM)
439 {
440     _AtlCreateWndData **ppData;
441
442     TRACE("(%p)\n", pM);
443
444     for(ppData = &pM->m_pCreateWndList; *ppData!=NULL; ppData = &(*ppData)->m_pNext)
445     {
446         if ((*ppData)->m_dwThreadID == GetCurrentThreadId())
447         {
448             _AtlCreateWndData *pData = *ppData;
449             *ppData = pData->m_pNext;
450             return pData->m_pThis;
451         }
452     }
453     return NULL;
454 }