comdlg32: Factor out some path building code from FILEDLG95_OnOpen.
[wine] / dlls / comdlg32 / cdlg32.c
1 /*
2  *  Common Dialog Boxes interface (32 bit)
3  *  Find/Replace
4  *
5  * Copyright 1999 Bertho A. Stultiens
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdarg.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "objbase.h"
31 #include "rpcproxy.h"
32 #include "commdlg.h"
33 #include "cderr.h"
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
37
38 #include "cdlg.h"
39
40
41 HINSTANCE       COMDLG32_hInstance = 0;
42
43 static DWORD COMDLG32_TlsIndex = TLS_OUT_OF_INDEXES;
44
45 static HINSTANCE        SHELL32_hInstance;
46 static HINSTANCE        SHFOLDER_hInstance;
47
48 /* ITEMIDLIST */
49 LPITEMIDLIST (WINAPI *COMDLG32_PIDL_ILClone) (LPCITEMIDLIST);
50 LPITEMIDLIST (WINAPI *COMDLG32_PIDL_ILCombine)(LPCITEMIDLIST,LPCITEMIDLIST);
51 LPITEMIDLIST (WINAPI *COMDLG32_PIDL_ILGetNext)(LPITEMIDLIST);
52 BOOL (WINAPI *COMDLG32_PIDL_ILRemoveLastID)(LPCITEMIDLIST);
53 BOOL (WINAPI *COMDLG32_PIDL_ILIsEqual)(LPCITEMIDLIST, LPCITEMIDLIST);
54 UINT (WINAPI *COMDLG32_PIDL_ILGetSize)(LPCITEMIDLIST);
55
56 /* SHELL */
57 LPVOID (WINAPI *COMDLG32_SHAlloc)(DWORD);
58 DWORD (WINAPI *COMDLG32_SHFree)(LPVOID);
59 BOOL (WINAPI *COMDLG32_SHGetFolderPathW)(HWND,int,HANDLE,DWORD,LPWSTR);
60
61 /***********************************************************************
62  *      DllMain  (COMDLG32.init)
63  *
64  *    Initialization code for the COMDLG32 DLL
65  *
66  * RETURNS:
67  *      FALSE if sibling could not be loaded or instantiated twice, TRUE
68  *      otherwise.
69  */
70 static const char GPA_string[] = "Failed to get entry point %s for hinst = %p\n";
71 #define GPA(dest, hinst, name) \
72         if(!(dest = (void*)GetProcAddress(hinst,name)))\
73         { \
74           ERR(GPA_string, debugstr_a(name), hinst); \
75           return FALSE; \
76         }
77
78 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD Reason, LPVOID Reserved)
79 {
80         TRACE("(%p, %d, %p)\n", hInstance, Reason, Reserved);
81
82         switch(Reason)
83         {
84         case DLL_PROCESS_ATTACH:
85                 COMDLG32_hInstance = hInstance;
86                 DisableThreadLibraryCalls(hInstance);
87
88                 SHELL32_hInstance = GetModuleHandleA("SHELL32.DLL");
89
90                 /* ITEMIDLIST */
91                 GPA(COMDLG32_PIDL_ILIsEqual, SHELL32_hInstance, (LPCSTR)21L);
92                 GPA(COMDLG32_PIDL_ILCombine, SHELL32_hInstance, (LPCSTR)25L);
93                 GPA(COMDLG32_PIDL_ILGetNext, SHELL32_hInstance, (LPCSTR)153L);
94                 GPA(COMDLG32_PIDL_ILClone, SHELL32_hInstance, (LPCSTR)18L);
95                 GPA(COMDLG32_PIDL_ILRemoveLastID, SHELL32_hInstance, (LPCSTR)17L);
96                 GPA(COMDLG32_PIDL_ILGetSize, SHELL32_hInstance, (LPCSTR)152L);
97
98                 /* SHELL */
99
100                 GPA(COMDLG32_SHAlloc, SHELL32_hInstance, (LPCSTR)196L);
101                 GPA(COMDLG32_SHFree, SHELL32_hInstance, (LPCSTR)195L);
102
103                 /* for the first versions of shell32 SHGetFolderPathW is in SHFOLDER.DLL */
104                 COMDLG32_SHGetFolderPathW = (void*)GetProcAddress(SHELL32_hInstance,"SHGetFolderPathW");
105                 if (!COMDLG32_SHGetFolderPathW)
106                 {
107                   SHFOLDER_hInstance = LoadLibraryA("SHFOLDER.DLL");
108                   GPA(COMDLG32_SHGetFolderPathW, SHFOLDER_hInstance,"SHGetFolderPathW");
109                 }
110
111                 break;
112
113         case DLL_PROCESS_DETACH:
114             if (COMDLG32_TlsIndex != TLS_OUT_OF_INDEXES) TlsFree(COMDLG32_TlsIndex);
115             if(SHFOLDER_hInstance) FreeLibrary(SHFOLDER_hInstance);
116             break;
117         }
118         return TRUE;
119 }
120 #undef GPA
121
122 /***********************************************************************
123  *      COMDLG32_AllocMem                       (internal)
124  * Get memory for internal datastructure plus stringspace etc.
125  *      RETURNS
126  *              Success: Pointer to a heap block
127  *              Failure: null
128  */
129 LPVOID COMDLG32_AllocMem(
130         int size        /* [in] Block size to allocate */
131 ) {
132         LPVOID ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
133         if(!ptr)
134         {
135                 COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
136                 return NULL;
137         }
138         return ptr;
139 }
140
141
142 /***********************************************************************
143  *      COMDLG32_SetCommDlgExtendedError        (internal)
144  *
145  * Used to set the thread's local error value if a comdlg32 function fails.
146  */
147 void COMDLG32_SetCommDlgExtendedError(DWORD err)
148 {
149         TRACE("(%08x)\n", err);
150         if (COMDLG32_TlsIndex == TLS_OUT_OF_INDEXES)
151           COMDLG32_TlsIndex = TlsAlloc();
152         if (COMDLG32_TlsIndex != TLS_OUT_OF_INDEXES)
153           TlsSetValue(COMDLG32_TlsIndex, (LPVOID)(DWORD_PTR)err);
154         else
155           FIXME("No Tls Space\n");
156 }
157
158
159 /***********************************************************************
160  *      CommDlgExtendedError                    (COMDLG32.@)
161  *
162  * Get the thread's local error value if a comdlg32 function fails.
163  *      RETURNS
164  *              Current error value which might not be valid
165  *              if a previous call succeeded.
166  */
167 DWORD WINAPI CommDlgExtendedError(void)
168 {
169         if (COMDLG32_TlsIndex != TLS_OUT_OF_INDEXES)
170           return (DWORD_PTR)TlsGetValue(COMDLG32_TlsIndex);
171         else
172           return 0; /* we never set an error, so there isn't one */
173 }
174
175 /*************************************************************************
176  * Implement the CommDlg32 class factory
177  *
178  * (Taken from shdocvw/factory.c; based on implementation in
179  *  ddraw/main.c)
180  */
181 typedef struct
182 {
183     IClassFactory IClassFactory_iface;
184     HRESULT (*cf)(IUnknown*, REFIID, void**);
185 } IClassFactoryImpl;
186
187 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
188 {
189     return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
190 }
191
192 /*************************************************************************
193  * CDLGCF_QueryInterface (IUnknown)
194  */
195 static HRESULT WINAPI CDLGCF_QueryInterface(IClassFactory* iface,
196                                             REFIID riid, void **ppobj)
197 {
198     TRACE("%p (%s %p)\n", iface, debugstr_guid(riid), ppobj);
199
200     if(!ppobj)
201         return E_POINTER;
202
203     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid))
204     {
205         *ppobj = iface;
206         IClassFactory_AddRef(iface);
207         return S_OK;
208     }
209
210     WARN("Interface not supported.\n");
211
212     *ppobj = NULL;
213     return E_NOINTERFACE;
214 }
215
216 /*************************************************************************
217  * CDLGCF_AddRef (IUnknown)
218  */
219 static ULONG WINAPI CDLGCF_AddRef(IClassFactory *iface)
220 {
221     return 2; /* non-heap based object */
222 }
223
224 /*************************************************************************
225  * CDLGCF_Release (IUnknown)
226  */
227 static ULONG WINAPI CDLGCF_Release(IClassFactory *iface)
228 {
229     return 1; /* non-heap based object */
230 }
231
232 /*************************************************************************
233  * CDLGCF_CreateInstance (IClassFactory)
234  */
235 static HRESULT WINAPI CDLGCF_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
236                                             REFIID riid, void **ppobj)
237 {
238     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
239     return This->cf(pOuter, riid, ppobj);
240 }
241
242 /*************************************************************************
243  * CDLGCF_LockServer (IClassFactory)
244  */
245 static HRESULT WINAPI CDLGCF_LockServer(IClassFactory *iface, BOOL dolock)
246 {
247     TRACE("%p (%d)\n", iface, dolock);
248     return S_OK;
249 }
250
251 static const IClassFactoryVtbl CDLGCF_Vtbl =
252 {
253     CDLGCF_QueryInterface,
254     CDLGCF_AddRef,
255     CDLGCF_Release,
256     CDLGCF_CreateInstance,
257     CDLGCF_LockServer
258 };
259
260 /*************************************************************************
261  *              DllGetClassObject (COMMDLG32.@)
262  */
263 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **ppv)
264 {
265     static IClassFactoryImpl FileOpenDlgClassFactory = {{&CDLGCF_Vtbl}, FileOpenDialog_Constructor};
266     static IClassFactoryImpl FileSaveDlgClassFactory = {{&CDLGCF_Vtbl}, FileSaveDialog_Constructor};
267
268     TRACE("%s, %s, %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
269
270     if(IsEqualGUID(&CLSID_FileOpenDialog, rclsid))
271         return IClassFactory_QueryInterface(&FileOpenDlgClassFactory.IClassFactory_iface, riid, ppv);
272
273     if(IsEqualGUID(&CLSID_FileSaveDialog, rclsid))
274         return IClassFactory_QueryInterface(&FileSaveDlgClassFactory.IClassFactory_iface, riid, ppv);
275
276     return CLASS_E_CLASSNOTAVAILABLE;
277 }
278
279 /***********************************************************************
280  *          DllRegisterServer (COMMDLG32.@)
281  */
282 HRESULT WINAPI DllRegisterServer(void)
283 {
284     return __wine_register_resources(COMDLG32_hInstance, NULL);
285 }
286
287 /***********************************************************************
288  *          DllUnregisterServer (COMMDLG32.@)
289  */
290 HRESULT WINAPI DllUnregisterServer(void)
291 {
292     return __wine_unregister_resources(COMDLG32_hInstance, NULL);
293 }