aboutsummaryrefslogtreecommitdiff
path: root/CPP/Windows/CommonDialog.cpp
blob: 7a92d5f9b65b4bba7b4fa21f3f98e2101457cd2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Windows/CommonDialog.cpp

#include "StdAfx.h"

#include "../Common/MyBuffer.h"

#ifdef UNDER_CE
#include <commdlg.h>
#endif

#ifndef _UNICODE
#include "../Common/StringConvert.h"
#endif

#include "CommonDialog.h"
#include "Defs.h"
// #include "FileDir.h"

#ifndef _UNICODE
extern bool g_IsNT;
#endif

namespace NWindows {

/*
  GetSaveFileName()
  GetOpenFileName()
  OPENFILENAME

(lpstrInitialDir) : the initial directory.
DOCs: the algorithm for selecting the initial directory varies on different platforms:
{
  Win2000/XP/Vista:
    1. If lpstrFile contains a path, that path is the initial directory.
    2. Otherwise, lpstrInitialDir specifies the initial directory.

  Win7:
    If lpstrInitialDir has the same value as was passed the first time
    the application used an Open or Save As dialog box, the path
    most recently selected by the user is used as the initial directory.
}

Win10:
 in:
  function supports (lpstrInitialDir) path with super prefix "\\\\?\\"
  function supports (lpstrInitialDir) path with long path
  function doesn't support absolute (lpstrFile) path with super prefix "\\\\?\\"
  function doesn't support absolute (lpstrFile) path with long path
 out: the path with super prefix "\\\\?\\" will be returned, if selected path is long

WinXP-64 and Win10: if no filters, the system shows all files.
    but DOCs say: If all three members are zero or NULL,
        the system does not use any filters and does not
        show any files in the file list control of the dialog box.

in Win7+: GetOpenFileName() and GetSaveFileName()
    do not support pstrCustomFilter feature anymore
*/

#ifdef UNDER_CE
#define MY_OFN_PROJECT  0x00400000
#define MY_OFN_SHOW_ALL 0x01000000
#endif


/*
structures
  OPENFILENAMEW
  OPENFILENAMEA
contain additional members:
#if (_WIN32_WINNT >= 0x0500)
  void *pvReserved;
  DWORD dwReserved;
  DWORD FlagsEx;
#endif

If we compile the source code with (_WIN32_WINNT >= 0x0500), some functions
will not work at NT 4.0, if we use sizeof(OPENFILENAME).
We try to use reduced structure OPENFILENAME_NT4.
*/

// #if defined(_WIN64) || (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0500)
#if defined(__GNUC__) && (__GNUC__ <= 9) || defined(Z7_OLD_WIN_SDK)
  #ifndef _UNICODE
  #define my_compatib_OPENFILENAMEA       OPENFILENAMEA
  #endif
  #define my_compatib_OPENFILENAMEW       OPENFILENAMEW

  // MinGW doesn't support some required macros. So we define them here:
  #ifndef CDSIZEOF_STRUCT
  #define CDSIZEOF_STRUCT(structname, member)  (((int)((LPBYTE)(&((structname*)0)->member) - ((LPBYTE)((structname*)0)))) + sizeof(((structname*)0)->member))
  #endif
  #ifndef _UNICODE
  #ifndef OPENFILENAME_SIZE_VERSION_400A
  #define OPENFILENAME_SIZE_VERSION_400A  CDSIZEOF_STRUCT(OPENFILENAMEA,lpTemplateName)
  #endif
  #endif
  #ifndef OPENFILENAME_SIZE_VERSION_400W
  #define OPENFILENAME_SIZE_VERSION_400W  CDSIZEOF_STRUCT(OPENFILENAMEW,lpTemplateName)
  #endif
  
  #ifndef _UNICODE
  #define my_compatib_OPENFILENAMEA_size OPENFILENAME_SIZE_VERSION_400A
  #endif
  #define my_compatib_OPENFILENAMEW_size OPENFILENAME_SIZE_VERSION_400W
#else
  #ifndef _UNICODE
  #define my_compatib_OPENFILENAMEA       OPENFILENAME_NT4A
  #define my_compatib_OPENFILENAMEA_size  sizeof(my_compatib_OPENFILENAMEA)
  #endif
  #define my_compatib_OPENFILENAMEW       OPENFILENAME_NT4W
  #define my_compatib_OPENFILENAMEW_size  sizeof(my_compatib_OPENFILENAMEW)
#endif
/*
#elif defined(UNDER_CE) || defined(_WIN64) || (_WIN32_WINNT < 0x0500)
// || !defined(WINVER)
  #ifndef _UNICODE
  #define my_compatib_OPENFILENAMEA       OPENFILENAMEA
  #define my_compatib_OPENFILENAMEA_size sizeof(OPENFILENAMEA)
  #endif
  #define my_compatib_OPENFILENAMEW       OPENFILENAMEW
  #define my_compatib_OPENFILENAMEW_size sizeof(OPENFILENAMEW)
#else

#endif
*/

#ifndef _UNICODE
#define CONV_U_To_A(dest, src, temp) AString temp; if (src) { temp = GetSystemString(src); dest = temp; }
#endif

bool CCommonDialogInfo::CommonDlg_BrowseForFile(LPCWSTR lpstrInitialDir, const UStringVector &filters)
{
  /* GetSaveFileName() and GetOpenFileName() could change current dir,
     if OFN_NOCHANGEDIR is not used.
     We can restore current dir manually, if it's required.
     22.02: we use OFN_NOCHANGEDIR. So we don't need to restore current dir manually. */
  // NFile::NDir::CCurrentDirRestorer curDirRestorer;

#ifndef _UNICODE
  if (!g_IsNT)
  {
    AString tempPath;
    AStringVector f;
    unsigned i;
    for (i = 0; i < filters.Size(); i++)
      f.Add(GetSystemString(filters[i]));
    unsigned size = f.Size() + 1;
    for (i = 0; i < f.Size(); i++)
      size += f[i].Len();
    CObjArray<char> filterBuf(size);
    // memset(filterBuf, 0, size * sizeof(char));
    {
      char *dest = filterBuf;
      for (i = 0; i < f.Size(); i++)
      {
        const AString &s = f[i];
        MyStringCopy(dest, s);
        dest += s.Len() + 1;
      }
      *dest = 0;
    }
    my_compatib_OPENFILENAMEA p;
    memset(&p, 0, sizeof(p));
    p.lStructSize = my_compatib_OPENFILENAMEA_size;
    p.hwndOwner = hwndOwner;
    if (size > 1)
    {
      p.lpstrFilter = filterBuf;
      p.nFilterIndex = (DWORD)(FilterIndex + 1);
    }

    CONV_U_To_A(p.lpstrInitialDir, lpstrInitialDir, initialDir_a)
    CONV_U_To_A(p.lpstrTitle, lpstrTitle, title_a)

    const AString filePath_a = GetSystemString(FilePath);
    const unsigned bufSize = MAX_PATH * 8
        + filePath_a.Len()
        + initialDir_a.Len();
    p.nMaxFile = bufSize;
    p.lpstrFile = tempPath.GetBuf(bufSize);
    MyStringCopy(p.lpstrFile, filePath_a);
    p.Flags =
          OFN_EXPLORER
        | OFN_HIDEREADONLY
        | OFN_NOCHANGEDIR;
    const BOOL b = SaveMode ?
        ::GetSaveFileNameA((LPOPENFILENAMEA)(void *)&p) :
        ::GetOpenFileNameA((LPOPENFILENAMEA)(void *)&p);
    if (!b)
      return false;
    {
      tempPath.ReleaseBuf_CalcLen(bufSize);
      FilePath = GetUnicodeString(tempPath);
      FilterIndex = (int)p.nFilterIndex - 1;
      return true;
    }
  }
  else
#endif
  {
    UString tempPath;
    unsigned size = filters.Size() + 1;
    unsigned i;
    for (i = 0; i < filters.Size(); i++)
      size += filters[i].Len();
    CObjArray<wchar_t> filterBuf(size);
    // memset(filterBuf, 0, size * sizeof(wchar_t));
    {
      wchar_t *dest = filterBuf;
      for (i = 0; i < filters.Size(); i++)
      {
        const UString &s = filters[i];
        MyStringCopy(dest, s);
        dest += s.Len() + 1;
      }
      *dest = 0;
      // if ((unsigned)(dest + 1 - filterBuf) != size) return false;
    }
    my_compatib_OPENFILENAMEW p;
    memset(&p, 0, sizeof(p));
    p.lStructSize = my_compatib_OPENFILENAMEW_size;
    p.hwndOwner = hwndOwner;
    if (size > 1)
    {
      p.lpstrFilter = filterBuf;
      p.nFilterIndex = (DWORD)(FilterIndex + 1);
    }
    unsigned bufSize = MAX_PATH * 8 + FilePath.Len();
    if (lpstrInitialDir)
    {
      p.lpstrInitialDir = lpstrInitialDir;
      bufSize += MyStringLen(lpstrInitialDir);
    }
    p.nMaxFile = bufSize;
    p.lpstrFile = tempPath.GetBuf(bufSize);
    MyStringCopy(p.lpstrFile, FilePath);
    p.lpstrTitle = lpstrTitle;
    p.Flags =
          OFN_EXPLORER
        | OFN_HIDEREADONLY
        | OFN_NOCHANGEDIR
        // | OFN_FORCESHOWHIDDEN // Win10 shows hidden items even without this flag
        // | OFN_PATHMUSTEXIST
      #ifdef UNDER_CE
        | (OpenFolderMode ? (MY_OFN_PROJECT | MY_OFN_SHOW_ALL) : 0)
      #endif
        ;
    const BOOL b = SaveMode ?
        ::GetSaveFileNameW((LPOPENFILENAMEW)(void *)&p) :
        ::GetOpenFileNameW((LPOPENFILENAMEW)(void *)&p);
    /* DOCs: lpstrFile :
        if the buffer is too small, then:
        - the function returns FALSE
        - the CommDlgExtendedError() returns FNERR_BUFFERTOOSMALL
        - the first two bytes of the lpstrFile buffer contain the
          required size, in bytes or characters. */
    if (!b)
      return false;
    {
      tempPath.ReleaseBuf_CalcLen(bufSize);
      FilePath = tempPath;
      FilterIndex = (int)p.nFilterIndex - 1;
      return true;
    }
  }
}

}