Trying hard to make things compile.
[wxWidgets.git] / src / msw / mslu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/mslu.cpp
3 // Purpose: Fixes for bugs in MSLU
4 // Author: Vaclav Slavik
5 // Modified by:
6 // Created: 2002/02/17
7 // RCS-ID: $Id$
8 // Copyright: (c) 2002 Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #include <dir.h>
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/defs.h"
26 #endif
27
28 //------------------------------------------------------------------------
29
30 #if !wxUSE_UNICODE_MSLU
31
32 bool wxUsingUnicowsDll()
33 {
34 return false;
35 }
36
37 #else
38
39 //------------------------------------------------------------------------
40 //
41 // NB: MSLU only covers Win32 API, it doesn't provide Unicode implementation of
42 // libc functions. Unfortunately, some of MSVCRT wchar_t functions
43 // (e.g. _wopen) don't work on Windows 9x, so we have to workaround it
44 // by calling the char version. We still want to use wchar_t version on
45 // NT/2000/XP, though, because they allow for Unicode file names.
46 //
47 // Moreover, there are bugs in unicows.dll, of course. We have to
48 // workaround them, too.
49 //
50 //------------------------------------------------------------------------
51
52 #include "wx/msw/private.h"
53 #include "wx/msw/mslu.h"
54
55 #include <stdio.h>
56 #include <io.h>
57 #include <sys/stat.h>
58
59 #ifdef __VISUALC__
60 #include <direct.h>
61 #endif
62
63 // Undef redirection macros defined in wx/msw/mslu.h:
64 #undef DrawStateW
65 #undef GetOpenFileNameW
66 #undef GetSaveFileNameW
67
68 // Returns true if we are running under Unicode emulation in Win9x environment.
69 // Workaround hacks take effect only if this condition is met
70 bool wxUsingUnicowsDll()
71 {
72 return (wxGetOsVersion() == wxWIN95);
73 }
74
75 //------------------------------------------------------------------------
76 // Wrongly implemented functions from unicows.dll
77 //------------------------------------------------------------------------
78
79 #if wxUSE_GUI
80
81 WXDLLEXPORT int wxMSLU_DrawStateW(WXHDC dc, WXHBRUSH br, WXFARPROC outputFunc,
82 WXLPARAM lData, WXWPARAM wData,
83 int x, int y, int cx, int cy,
84 unsigned int flags)
85 {
86 // VS: There's yet another bug in MSLU: DrawStateW behaves like if it was
87 // expecting char*, not wchar_t* input. We have to use DrawStateA
88 // explicitly.
89
90 if ( wxUsingUnicowsDll() )
91 {
92 return DrawStateA((HDC)dc, (HBRUSH)br, (DRAWSTATEPROC)outputFunc,
93 (LPARAM)(const char*)
94 wxConvLocal.cWX2MB((const wxChar*)lData),
95 wData, x, y, cx, cy, flags);
96 }
97 else
98 {
99 return DrawStateW((HDC)dc, (HBRUSH)br, (DRAWSTATEPROC)outputFunc,
100 lData, wData, x, y, cx, cy, flags);
101 }
102 }
103
104 static void wxFixOPENFILENAME(LPOPENFILENAME ofn)
105 {
106 #ifdef OFN_EXPLORER
107 // VS: there's a bug in unicows.dll - when multiple files are selected,
108 // of.nFileOffset doesn't point to the first filename but rather to
109 // the last component of directory name. This bug is known to MSLU
110 // developers, but they are not going to fix it: "this is a true
111 // limitation, that we have decided to live with" and "working
112 // harder on this case just did not seem worth the effort"...
113 //
114 // Our only option is to try to fix it ourselves:
115
116 if ( (ofn->Flags & OFN_ALLOWMULTISELECT) &&
117 ofn->lpstrFile[ofn->nFileOffset-1] != wxT('\0') )
118 {
119 if ( wxDirExists(ofn->lpstrFile) )
120 {
121 // 1st component is dir => multiple files selected
122 ofn->nFileOffset = wxStrlen(ofn->lpstrFile)+1;
123 }
124 }
125 #endif
126 }
127
128 WXDLLEXPORT int wxMSLU_GetOpenFileNameW(void *ofn)
129 {
130 int ret = GetOpenFileName((LPOPENFILENAME)ofn);
131 if ( wxUsingUnicowsDll() && ret != 0 )
132 wxFixOPENFILENAME((LPOPENFILENAME)ofn);
133 return ret;
134 }
135
136 WXDLLEXPORT int wxMSLU_GetSaveFileNameW(void *ofn)
137 {
138 int ret = GetSaveFileName((LPOPENFILENAME)ofn);
139 if ( wxUsingUnicowsDll() && ret != 0 )
140 wxFixOPENFILENAME((LPOPENFILENAME)ofn);
141 return ret;
142 }
143
144 #endif // wxUSE_GUI
145
146 //------------------------------------------------------------------------
147 // Missing libc file manipulation functions in Win9x
148 //------------------------------------------------------------------------
149
150 #if wxUSE_BASE
151
152 WXDLLIMPEXP_BASE int wxMSLU__trename(const wxChar *oldname,
153 const wxChar *newname)
154 {
155 if ( wxUsingUnicowsDll() )
156 return rename(wxConvFile.cWX2MB(oldname), wxConvFile.cWX2MB(newname));
157 else
158 return _trename(oldname, newname);
159 }
160
161 WXDLLIMPEXP_BASE int wxMSLU__tremove(const wxChar *name)
162 {
163 if ( wxUsingUnicowsDll() )
164 return remove(wxConvFile.cWX2MB(name));
165 else
166 return _tremove(name);
167 }
168
169 #if defined( __VISUALC__ ) \
170 || ( defined(__MINGW32__) && wxCHECK_W32API_VERSION( 0, 5 ) ) \
171 || ( defined(__MWERKS__) && defined(__WXMSW__) ) \
172 || ( defined(__BORLANDC__) && (__BORLANDC__ > 0x460) )
173
174 WXDLLIMPEXP_BASE int wxMSLU__wopen(const wxChar *name, int flags, int mode)
175 {
176 if ( wxUsingUnicowsDll() )
177 #ifdef __BORLANDC__
178 return open(wxConvFile.cWX2MB(name), flags, mode);
179 #else
180 return _open(wxConvFile.cWX2MB(name), flags, mode);
181 #endif
182 else
183 return _wopen(name, flags, mode);
184 }
185
186 WXDLLIMPEXP_BASE int wxMSLU__waccess(const wxChar *name, int mode)
187 {
188 if ( wxUsingUnicowsDll() )
189 return _access(wxConvFile.cWX2MB(name), mode);
190 else
191 return _waccess(name, mode);
192 }
193
194 WXDLLIMPEXP_BASE int wxMSLU__wmkdir(const wxChar *name)
195 {
196 if ( wxUsingUnicowsDll() )
197 return _mkdir(wxConvFile.cWX2MB(name));
198 else
199 return _wmkdir(name);
200 }
201
202 WXDLLIMPEXP_BASE int wxMSLU__wrmdir(const wxChar *name)
203 {
204 if ( wxUsingUnicowsDll() )
205 return _rmdir(wxConvFile.cWX2MB(name));
206 else
207 return _wrmdir(name);
208 }
209
210 WXDLLIMPEXP_BASE int wxMSLU__wstat(const wxChar *name, struct _stat *buffer)
211 {
212 if ( wxUsingUnicowsDll() )
213 return _stat((const char*)wxConvFile.cWX2MB(name), buffer);
214 else
215 return _wstat(name, buffer);
216 }
217
218 WXDLLIMPEXP_BASE int wxMSLU__wstati64(const wxChar *name, struct _stati64 *buffer)
219 {
220 if ( wxUsingUnicowsDll() )
221 return _stati64((const char*)wxConvFile.cWX2MB(name), buffer);
222 else
223 return _wstati64(name, buffer);
224 }
225
226 #endif // compilers having wopen() &c
227
228 #endif // wxUSE_BASE
229
230 #endif // wxUSE_UNICODE_MSLU