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