summaryrefslogtreecommitdiffstats
path: root/src/libs/7zip/win/CPP/Windows/CommonDialog.cpp
blob: 4ee7412d0270d9beaafc23aa097470c3b3171fd7 (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
// Windows/CommonDialog.cpp

#include "StdAfx.h"

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

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

#include "Windows/Defs.h"

#include "CommonDialog.h"

#ifndef _UNICODE
extern bool g_IsNT;
#endif

namespace NWindows{

#ifndef _UNICODE
class CDoubleZeroStringListA
{
  CRecordVector<int> m_Indexes;
  AString m_String;
public:
  void Add(LPCSTR s);
  void SetForBuffer(LPSTR buffer);
};

void CDoubleZeroStringListA::Add(LPCSTR s)
{
  m_String += s;
  m_Indexes.Add(m_String.Length());
  m_String += ' ';
}

void CDoubleZeroStringListA::SetForBuffer(LPSTR buffer)
{
  MyStringCopy(buffer, (const char *)m_String);
  for (int i = 0; i < m_Indexes.Size(); i++)
    buffer[m_Indexes[i]] = '\0';
}
#endif

class CDoubleZeroStringListW
{
  CRecordVector<int> m_Indexes;
  UString m_String;
public:
  void Add(LPCWSTR s);
  void SetForBuffer(LPWSTR buffer);
};

void CDoubleZeroStringListW::Add(LPCWSTR s)
{
  m_String += s;
  m_Indexes.Add(m_String.Length());
  m_String += L' ';
}

void CDoubleZeroStringListW::SetForBuffer(LPWSTR buffer)
{
  MyStringCopy(buffer, (const wchar_t *)m_String);
  for (int i = 0; i < m_Indexes.Size(); i++)
    buffer[m_Indexes[i]] = L'\0';
}

#define MY_OFN_PROJECT 0x00400000
#define MY_OFN_SHOW_ALL 0x01000000

bool MyGetOpenFileName(HWND hwnd, LPCWSTR title, LPCWSTR fullFileName,
    LPCWSTR s, UString &resPath
    #ifdef UNDER_CE
    , bool openFolder
    #endif
    )
{
  const int kBufferSize = MAX_PATH * 2;
  #ifndef _UNICODE
  if (!g_IsNT)
  {
    CHAR buffer[kBufferSize];
    MyStringCopy(buffer, (const char *)GetSystemString(fullFileName));
    OPENFILENAME info;
    info.lStructSize = sizeof(info);
    info.hwndOwner = hwnd;
    info.hInstance = 0;
    const int kFilterBufferSize = MAX_PATH;
    CHAR filterBuffer[kFilterBufferSize];
    CDoubleZeroStringListA doubleZeroStringList;
    doubleZeroStringList.Add(GetSystemString(s));
    doubleZeroStringList.Add("*.*");
    doubleZeroStringList.SetForBuffer(filterBuffer);
    info.lpstrFilter = filterBuffer;
    
    info.lpstrCustomFilter = NULL;
    info.nMaxCustFilter = 0;
    info.nFilterIndex = 0;
    
    info.lpstrFile = buffer;
    info.nMaxFile = kBufferSize;
    
    info.lpstrFileTitle = NULL;
    info.nMaxFileTitle = 0;
    
    info.lpstrInitialDir= NULL;

    info.lpstrTitle = 0;
    AString titleA;
    if (title != 0)
    {
      titleA = GetSystemString(title);
      info.lpstrTitle = titleA;
    }
    
    info.Flags = OFN_EXPLORER | OFN_HIDEREADONLY;
    info.nFileOffset = 0;
    info.nFileExtension = 0;
    info.lpstrDefExt = NULL;
    
    info.lCustData = 0;
    info.lpfnHook = NULL;
    info.lpTemplateName = NULL;
    
    bool res = BOOLToBool(::GetOpenFileNameA(&info));
    resPath = GetUnicodeString(buffer);
    return res;
  }
  else
  #endif
  {
    WCHAR buffer[kBufferSize];
    MyStringCopy(buffer, fullFileName);
    OPENFILENAMEW info;
    info.lStructSize = sizeof(info);
    info.hwndOwner = hwnd;
    info.hInstance = 0;
    const int kFilterBufferSize = MAX_PATH;
    WCHAR filterBuffer[kFilterBufferSize];
    CDoubleZeroStringListW doubleZeroStringList;
    doubleZeroStringList.Add(s);
    doubleZeroStringList.Add(L"*.*");
    doubleZeroStringList.SetForBuffer(filterBuffer);
    info.lpstrFilter = filterBuffer;
        
    info.lpstrCustomFilter = NULL;
    info.nMaxCustFilter = 0;
    info.nFilterIndex = 0;
    
    info.lpstrFile = buffer;
    info.nMaxFile = kBufferSize;
    
    info.lpstrFileTitle = NULL;
    info.nMaxFileTitle = 0;
    
    info.lpstrInitialDir= NULL;
       
    info.lpstrTitle = title;
    
    info.Flags = OFN_EXPLORER | OFN_HIDEREADONLY
        #ifdef UNDER_CE
        | (openFolder ? (MY_OFN_PROJECT | MY_OFN_SHOW_ALL) : 0)
        #endif
    ;

    info.nFileOffset = 0;
    info.nFileExtension = 0;
    info.lpstrDefExt = NULL;
    
    info.lCustData = 0;
    info.lpfnHook = NULL;
    info.lpTemplateName = NULL;
    
    bool res = BOOLToBool(::GetOpenFileNameW(&info));
    resPath = buffer;
    return res;
  }
}

}