We can't cache the unscaled font's hfont, since the mapping mode may
[wine] / dlls / commdlg / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "winbase.h"
23 #include "wine/winbase16.h"
24 #include "commdlg.h"
25 #include "cderr.h"
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
29
30 #include "cdlg.h"
31
32
33 HINSTANCE       COMDLG32_hInstance = 0;
34 static HINSTANCE16 COMDLG32_hInstance16;
35
36 static DWORD    COMDLG32_TlsIndex;
37
38 HINSTANCE       SHELL32_hInstance = 0;
39 HINSTANCE       SHFOLDER_hInstance = 0;
40
41 /* ITEMIDLIST */
42 LPITEMIDLIST (WINAPI *COMDLG32_PIDL_ILClone) (LPCITEMIDLIST);
43 LPITEMIDLIST (WINAPI *COMDLG32_PIDL_ILCombine)(LPCITEMIDLIST,LPCITEMIDLIST);
44 LPITEMIDLIST (WINAPI *COMDLG32_PIDL_ILGetNext)(LPITEMIDLIST);
45 BOOL (WINAPI *COMDLG32_PIDL_ILRemoveLastID)(LPCITEMIDLIST);
46 BOOL (WINAPI *COMDLG32_PIDL_ILIsEqual)(LPCITEMIDLIST, LPCITEMIDLIST);
47
48 /* SHELL */
49 LPVOID (WINAPI *COMDLG32_SHAlloc)(DWORD);
50 DWORD (WINAPI *COMDLG32_SHFree)(LPVOID);
51 BOOL (WINAPI *COMDLG32_SHGetFolderPathA)(HWND,int,HANDLE,DWORD,LPSTR);
52 BOOL (WINAPI *COMDLG32_SHGetFolderPathW)(HWND,int,HANDLE,DWORD,LPWSTR);
53
54 /***********************************************************************
55  *      DllMain  (COMDLG32.init)
56  *
57  *    Initialization code for the COMDLG32 DLL
58  *
59  * RETURNS:
60  *      FALSE if sibling could not be loaded or instantiated twice, TRUE
61  *      otherwise.
62  */
63 static char * GPA_string = "Failed to get entry point %s for hinst = 0x%08x\n";
64 #define GPA(dest, hinst, name) \
65         if(!(dest = (void*)GetProcAddress(hinst,name)))\
66         { \
67           ERR(GPA_string, debugstr_a(name), hinst); \
68           return FALSE; \
69         }
70
71 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD Reason, LPVOID Reserved)
72 {
73         TRACE("(%08x, %08lx, %p)\n", hInstance, Reason, Reserved);
74
75         switch(Reason)
76         {
77         case DLL_PROCESS_ATTACH:
78                 COMDLG32_hInstance = hInstance;
79                 DisableThreadLibraryCalls(hInstance);
80
81                 COMDLG32_hInstance16 = LoadLibrary16("commdlg.dll");
82                 COMDLG32_TlsIndex = 0xffffffff;
83
84                 SHELL32_hInstance = GetModuleHandleA("SHELL32.DLL");
85
86                 if (!SHELL32_hInstance)
87                 {
88                         ERR("loading of shell32 failed\n");
89                         return FALSE;
90                 }
91
92                 /* ITEMIDLIST */
93                 GPA(COMDLG32_PIDL_ILIsEqual, SHELL32_hInstance, (LPCSTR)21L);
94                 GPA(COMDLG32_PIDL_ILCombine, SHELL32_hInstance, (LPCSTR)25L);
95                 GPA(COMDLG32_PIDL_ILGetNext, SHELL32_hInstance, (LPCSTR)153L);
96                 GPA(COMDLG32_PIDL_ILClone, SHELL32_hInstance, (LPCSTR)18L);
97                 GPA(COMDLG32_PIDL_ILRemoveLastID, SHELL32_hInstance, (LPCSTR)17L);
98
99                 /* SHELL */
100
101                 GPA(COMDLG32_SHAlloc, SHELL32_hInstance, (LPCSTR)196L);
102                 GPA(COMDLG32_SHFree, SHELL32_hInstance, (LPCSTR)195L);
103                 /* for the first versions of shell32 SHGetFolderPathA is in SHFOLDER.DLL */
104                 COMDLG32_SHGetFolderPathA = (void*)GetProcAddress(SHELL32_hInstance,"SHGetFolderPathA");
105                 if (!COMDLG32_SHGetFolderPathA)
106                 {
107                   SHFOLDER_hInstance = LoadLibraryA("SHFOLDER.DLL");
108                   GPA(COMDLG32_SHGetFolderPathA, SHFOLDER_hInstance,"SHGetFolderPathA");
109                 }
110
111                 /* for the first versions of shell32 SHGetFolderPathW is in SHFOLDER.DLL */
112                 COMDLG32_SHGetFolderPathW = (void*)GetProcAddress(SHELL32_hInstance,"SHGetFolderPathW");
113                 if (!COMDLG32_SHGetFolderPathW)
114                 {
115                   SHFOLDER_hInstance = LoadLibraryA("SHFOLDER.DLL");
116                   GPA(COMDLG32_SHGetFolderPathW, SHFOLDER_hInstance,"SHGetFolderPathW");
117                 }
118
119                 break;
120
121         case DLL_PROCESS_DETACH:
122             if (COMDLG32_TlsIndex != 0xffffffff) TlsFree(COMDLG32_TlsIndex);
123             if(COMDLG32_hInstance16) FreeLibrary16(COMDLG32_hInstance16);
124             if(SHFOLDER_hInstance) FreeLibrary(SHFOLDER_hInstance);
125             break;
126         }
127         return TRUE;
128 }
129 #undef GPA
130
131 /***********************************************************************
132  *      COMDLG32_AllocMem                       (internal)
133  * Get memory for internal datastructure plus stringspace etc.
134  *      RETURNS
135  *              Pointer to a heap block: Succes
136  *              NULL: Failure
137  */
138 LPVOID COMDLG32_AllocMem(
139         int size        /* [in] Block size to allocate */
140 ) {
141         LPVOID ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
142         if(!ptr)
143         {
144                 COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
145                 return NULL;
146         }
147         return ptr;
148 }
149
150
151 /***********************************************************************
152  *      COMDLG32_SetCommDlgExtendedError        (internal)
153  *
154  * Used to set the thread's local error value if a comdlg32 function fails.
155  */
156 void COMDLG32_SetCommDlgExtendedError(DWORD err)
157 {
158         TRACE("(%08lx)\n", err);
159         if (COMDLG32_TlsIndex == 0xffffffff)
160           COMDLG32_TlsIndex = TlsAlloc();
161         if (COMDLG32_TlsIndex != 0xffffffff)
162           TlsSetValue(COMDLG32_TlsIndex, (void *)err);
163         else
164           FIXME("No Tls Space\n");
165 }
166
167
168 /***********************************************************************
169  *      CommDlgExtendedError                    (COMMDLG.26)
170  *      CommDlgExtendedError                    (COMDLG32.@)
171  *
172  * Get the thread's local error value if a comdlg32 function fails.
173  *      RETURNS
174  *              Current error value which might not be valid
175  *              if a previous call succeeded.
176  */
177 DWORD WINAPI CommDlgExtendedError(void)
178 {
179         if (COMDLG32_TlsIndex != 0xffffffff)
180           return (DWORD)TlsGetValue(COMDLG32_TlsIndex);
181         else
182           return 0; /* we never set an error, so there isn't one */
183 }