-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileDialogNative.cpp
More file actions
110 lines (95 loc) · 3.87 KB
/
FileDialogNative.cpp
File metadata and controls
110 lines (95 loc) · 3.87 KB
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
/*
Copyright (c) 2024-2025 Toksisitee. All rights reserved.
This work is licensed under the terms of the MIT license.
For a copy, refer to license.md or https://opensource.org/licenses/MIT
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*/
#include <windows.h>
#include <shobjidl.h>
#include <commdlg.h>
#include <iostream>
#include "Utils.h"
#include "FileDialogNative.h"
namespace FileDialog
{
std::optional<std::string> OpenFile( Filter eFilter )
{
HRESULT hr;
IFileOpenDialog* pFileOpen;
std::optional<std::string> sFilePath = std::nullopt;
hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
if ( SUCCEEDED( hr ) ) {
hr = CoCreateInstance( CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen) );
if ( SUCCEEDED( hr ) ) {
std::vector<COMDLG_FILTERSPEC> fileTypes;
switch ( eFilter ) {
case Filter::DAT:
fileTypes = { { L"DAT Files", L"*.dat" } };
break;
case Filter::BMP:
fileTypes = { { L"BMP Files", L"*.bmp" } };
break;
case Filter::ALL:
default:
fileTypes = { { L"All Files", L"*.*" } };
break;
}
pFileOpen->SetFileTypes( static_cast<UINT>(fileTypes.size()), fileTypes.data() );
pFileOpen->SetFileTypeIndex( 1 );
hr = pFileOpen->Show( NULL );
if ( SUCCEEDED( hr ) ) {
IShellItem* pItem;
hr = pFileOpen->GetResult( &pItem );
if ( SUCCEEDED( hr ) ) {
PWSTR pszFilePath;
hr = pItem->GetDisplayName( SIGDN_FILESYSPATH, &pszFilePath );
if ( SUCCEEDED( hr ) ) {
sFilePath = Util::wcstombs( pszFilePath, _MAX_PATH );
CoTaskMemFree( pszFilePath );
}
pItem->Release();
}
}
pFileOpen->Release();
}
CoUninitialize();
}
return sFilePath;
}
std::optional<std::string> OpenFolder()
{
HRESULT hr;
IFileOpenDialog* pFolderOpen;
std::optional<std::string> sFolderPath = std::nullopt;
hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
if ( SUCCEEDED( hr ) ) {
hr = CoCreateInstance( CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&pFolderOpen) );
if ( SUCCEEDED( hr ) ) {
DWORD dwOptions;
hr = pFolderOpen->GetOptions( &dwOptions );
if ( SUCCEEDED( hr ) ) {
pFolderOpen->SetOptions( dwOptions | FOS_PICKFOLDERS );
}
hr = pFolderOpen->Show( NULL );
if ( SUCCEEDED( hr ) ) {
IShellItem* pItem;
hr = pFolderOpen->GetResult( &pItem );
if ( SUCCEEDED( hr ) ) {
PWSTR pszFolderPath;
hr = pItem->GetDisplayName( SIGDN_FILESYSPATH, &pszFolderPath );
if ( SUCCEEDED( hr ) ) {
sFolderPath = Util::wcstombs( pszFolderPath, _MAX_PATH );
CoTaskMemFree( pszFolderPath );
}
pItem->Release();
}
}
pFolderOpen->Release();
}
CoUninitialize();
}
return sFolderPath;
}
}