File size: 14,180 Bytes
347aff8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
  Native File Dialog Extended
  Repository: https://github.com/btzy/nativefiledialog-extended
  License: Zlib
  Author: Bernard Teo

  This header is a thin C++ wrapper for nfd.h.
  C++ projects can choose to use this header instead of nfd.h directly.

  Refer to documentation on nfd.h for instructions on how to use these functions.
*/

#ifndef _NFD_HPP
#define _NFD_HPP

#include <nfd.h>
#include <cstddef>  // for std::size_t
#include <memory>   // for std::unique_ptr
#ifdef NFD_THROWS_EXCEPTIONS
#include <stdexcept>
#endif

namespace NFD {

inline nfdresult_t Init() noexcept {
    return ::NFD_Init();
}

inline void Quit() noexcept {
    ::NFD_Quit();
}

inline void FreePath(nfdnchar_t* outPath) noexcept {
    ::NFD_FreePathN(outPath);
}

inline nfdresult_t OpenDialog(nfdnchar_t*& outPath,
                              const nfdnfilteritem_t* filterList = nullptr,
                              nfdfiltersize_t filterCount = 0,
                              const nfdnchar_t* defaultPath = nullptr,
                              nfdwindowhandle_t parentWindow = {}) noexcept {
    const nfdopendialognargs_t args{filterList, filterCount, defaultPath, parentWindow};
    return ::NFD_OpenDialogN_With(&outPath, &args);
}

inline nfdresult_t OpenDialogMultiple(const nfdpathset_t*& outPaths,
                                      const nfdnfilteritem_t* filterList = nullptr,
                                      nfdfiltersize_t filterCount = 0,
                                      const nfdnchar_t* defaultPath = nullptr,
                                      nfdwindowhandle_t parentWindow = {}) noexcept {
    const nfdopendialognargs_t args{filterList, filterCount, defaultPath, parentWindow};
    return ::NFD_OpenDialogMultipleN_With(&outPaths, &args);
}

inline nfdresult_t SaveDialog(nfdnchar_t*& outPath,
                              const nfdnfilteritem_t* filterList = nullptr,
                              nfdfiltersize_t filterCount = 0,
                              const nfdnchar_t* defaultPath = nullptr,
                              const nfdnchar_t* defaultName = nullptr,
                              nfdwindowhandle_t parentWindow = {}) noexcept {
    const nfdsavedialognargs_t args{
        filterList, filterCount, defaultPath, defaultName, parentWindow};
    return ::NFD_SaveDialogN_With(&outPath, &args);
}

inline nfdresult_t PickFolder(nfdnchar_t*& outPath,
                              const nfdnchar_t* defaultPath = nullptr,
                              nfdwindowhandle_t parentWindow = {}) noexcept {
    const nfdpickfoldernargs_t args{defaultPath, parentWindow};
    return ::NFD_PickFolderN_With(&outPath, &args);
}

inline nfdresult_t PickFolderMultiple(const nfdpathset_t*& outPaths,
                                      const nfdnchar_t* defaultPath = nullptr,
                                      nfdwindowhandle_t parentWindow = {}) noexcept {
    const nfdpickfoldernargs_t args{defaultPath, parentWindow};
    return ::NFD_PickFolderMultipleN_With(&outPaths, &args);
}

inline const char* GetError() noexcept {
    return ::NFD_GetError();
}

inline void ClearError() noexcept {
    ::NFD_ClearError();
}

namespace PathSet {
inline nfdresult_t Count(const nfdpathset_t* pathSet, nfdpathsetsize_t& count) noexcept {
    return ::NFD_PathSet_GetCount(pathSet, &count);
}

inline nfdresult_t GetPath(const nfdpathset_t* pathSet,
                           nfdpathsetsize_t index,
                           nfdnchar_t*& outPath) noexcept {
    return ::NFD_PathSet_GetPathN(pathSet, index, &outPath);
}

inline void FreePath(nfdnchar_t* filePath) noexcept {
    ::NFD_PathSet_FreePathN(filePath);
}

inline void Free(const nfdpathset_t* pathSet) noexcept {
    ::NFD_PathSet_Free(pathSet);
}
}  // namespace PathSet

#ifdef NFD_DIFFERENT_NATIVE_FUNCTIONS
/* we need the C++ bindings for the UTF-8 functions as well, because there are different functions
 * for them */

inline void FreePath(nfdu8char_t* outPath) noexcept {
    ::NFD_FreePathU8(outPath);
}

inline nfdresult_t OpenDialog(nfdu8char_t*& outPath,
                              const nfdu8filteritem_t* filterList = nullptr,
                              nfdfiltersize_t filterCount = 0,
                              const nfdu8char_t* defaultPath = nullptr,
                              nfdwindowhandle_t parentWindow = {}) noexcept {
    const nfdopendialogu8args_t args{filterList, filterCount, defaultPath, parentWindow};
    return ::NFD_OpenDialogU8_With(&outPath, &args);
}

inline nfdresult_t OpenDialogMultiple(const nfdpathset_t*& outPaths,
                                      const nfdu8filteritem_t* filterList = nullptr,
                                      nfdfiltersize_t filterCount = 0,
                                      const nfdu8char_t* defaultPath = nullptr,
                                      nfdwindowhandle_t parentWindow = {}) noexcept {
    const nfdopendialogu8args_t args{filterList, filterCount, defaultPath, parentWindow};
    return ::NFD_OpenDialogMultipleU8_With(&outPaths, &args);
}

inline nfdresult_t SaveDialog(nfdu8char_t*& outPath,
                              const nfdu8filteritem_t* filterList = nullptr,
                              nfdfiltersize_t filterCount = 0,
                              const nfdu8char_t* defaultPath = nullptr,
                              const nfdu8char_t* defaultName = nullptr,
                              nfdwindowhandle_t parentWindow = {}) noexcept {
    const nfdsavedialogu8args_t args{
        filterList, filterCount, defaultPath, defaultName, parentWindow};
    return ::NFD_SaveDialogU8_With(&outPath, &args);
}

inline nfdresult_t PickFolder(nfdu8char_t*& outPath,
                              const nfdu8char_t* defaultPath = nullptr,
                              nfdwindowhandle_t parentWindow = {}) noexcept {
    const nfdpickfolderu8args_t args{defaultPath, parentWindow};
    return ::NFD_PickFolderU8_With(&outPath, &args);
}

inline nfdresult_t PickFolderMultiple(const nfdpathset_t*& outPaths,
                                      const nfdu8char_t* defaultPath = nullptr,
                                      nfdwindowhandle_t parentWindow = {}) noexcept {
    const nfdpickfolderu8args_t args{defaultPath, parentWindow};
    return ::NFD_PickFolderMultipleU8_With(&outPaths, &args);
}

namespace PathSet {
inline nfdresult_t GetPath(const nfdpathset_t* pathSet,
                           nfdpathsetsize_t index,
                           nfdu8char_t*& outPath) noexcept {
    return ::NFD_PathSet_GetPathU8(pathSet, index, &outPath);
}
inline void FreePath(nfdu8char_t* filePath) noexcept {
    ::NFD_PathSet_FreePathU8(filePath);
}
}  // namespace PathSet
#endif

// smart objects

class Guard {
   public:
#ifndef NFD_THROWS_EXCEPTIONS
    inline Guard() noexcept {
        Init();  // always assume that initialization succeeds
    }
#else
    inline Guard() {
        if (!Init()) {
            throw std::runtime_error(GetError());
        }
    }
#endif
    inline ~Guard() noexcept { Quit(); }

    // Not allowed to copy or move this class
    Guard(const Guard&) = delete;
    Guard& operator=(const Guard&) = delete;
};

template <typename T>
struct PathDeleter {
    inline void operator()(T* ptr) const noexcept { FreePath(ptr); }
};

typedef std::unique_ptr<nfdchar_t, PathDeleter<nfdchar_t>> UniquePath;
typedef std::unique_ptr<nfdnchar_t, PathDeleter<nfdnchar_t>> UniquePathN;
typedef std::unique_ptr<nfdu8char_t, PathDeleter<nfdu8char_t>> UniquePathU8;

struct PathSetDeleter {
    inline void operator()(const nfdpathset_t* ptr) const noexcept { PathSet::Free(ptr); }
};

typedef std::unique_ptr<const nfdpathset_t, PathSetDeleter> UniquePathSet;

template <typename T>
struct PathSetPathDeleter {
    inline void operator()(T* ptr) const noexcept { PathSet::FreePath(ptr); }
};

typedef std::unique_ptr<nfdchar_t, PathSetPathDeleter<nfdchar_t>> UniquePathSetPath;
typedef std::unique_ptr<nfdnchar_t, PathSetPathDeleter<nfdnchar_t>> UniquePathSetPathN;
typedef std::unique_ptr<nfdu8char_t, PathSetPathDeleter<nfdu8char_t>> UniquePathSetPathU8;

inline nfdresult_t OpenDialog(UniquePathN& outPath,
                              const nfdnfilteritem_t* filterList = nullptr,
                              nfdfiltersize_t filterCount = 0,
                              const nfdnchar_t* defaultPath = nullptr,
                              nfdwindowhandle_t parentWindow = {}) noexcept {
    nfdnchar_t* out;
    nfdresult_t res = OpenDialog(out, filterList, filterCount, defaultPath, parentWindow);
    if (res == NFD_OKAY) {
        outPath.reset(out);
    }
    return res;
}

inline nfdresult_t OpenDialogMultiple(UniquePathSet& outPaths,
                                      const nfdnfilteritem_t* filterList = nullptr,
                                      nfdfiltersize_t filterCount = 0,
                                      const nfdnchar_t* defaultPath = nullptr,
                                      nfdwindowhandle_t parentWindow = {}) noexcept {
    const nfdpathset_t* out;
    nfdresult_t res = OpenDialogMultiple(out, filterList, filterCount, defaultPath, parentWindow);
    if (res == NFD_OKAY) {
        outPaths.reset(out);
    }
    return res;
}

inline nfdresult_t SaveDialog(UniquePathN& outPath,
                              const nfdnfilteritem_t* filterList = nullptr,
                              nfdfiltersize_t filterCount = 0,
                              const nfdnchar_t* defaultPath = nullptr,
                              const nfdnchar_t* defaultName = nullptr,
                              nfdwindowhandle_t parentWindow = {}) noexcept {
    nfdnchar_t* out;
    nfdresult_t res =
        SaveDialog(out, filterList, filterCount, defaultPath, defaultName, parentWindow);
    if (res == NFD_OKAY) {
        outPath.reset(out);
    }
    return res;
}

inline nfdresult_t PickFolder(UniquePathN& outPath,
                              const nfdnchar_t* defaultPath = nullptr,
                              nfdwindowhandle_t parentWindow = {}) noexcept {
    nfdnchar_t* out;
    nfdresult_t res = PickFolder(out, defaultPath, parentWindow);
    if (res == NFD_OKAY) {
        outPath.reset(out);
    }
    return res;
}

inline nfdresult_t PickFolderMultiple(UniquePathSet& outPaths,
                                      const nfdnchar_t* defaultPath = nullptr,
                                      nfdwindowhandle_t parentWindow = {}) noexcept {
    const nfdpathset_t* out;
    nfdresult_t res = PickFolderMultiple(out, defaultPath, parentWindow);
    if (res == NFD_OKAY) {
        outPaths.reset(out);
    }
    return res;
}

#ifdef NFD_DIFFERENT_NATIVE_FUNCTIONS
inline nfdresult_t OpenDialog(UniquePathU8& outPath,
                              const nfdu8filteritem_t* filterList = nullptr,
                              nfdfiltersize_t filterCount = 0,
                              const nfdu8char_t* defaultPath = nullptr,
                              nfdwindowhandle_t parentWindow = {}) noexcept {
    nfdu8char_t* out;
    nfdresult_t res = OpenDialog(out, filterList, filterCount, defaultPath, parentWindow);
    if (res == NFD_OKAY) {
        outPath.reset(out);
    }
    return res;
}

inline nfdresult_t OpenDialogMultiple(UniquePathSet& outPaths,
                                      const nfdu8filteritem_t* filterList = nullptr,
                                      nfdfiltersize_t filterCount = 0,
                                      const nfdu8char_t* defaultPath = nullptr,
                                      nfdwindowhandle_t parentWindow = {}) noexcept {
    const nfdpathset_t* out;
    nfdresult_t res = OpenDialogMultiple(out, filterList, filterCount, defaultPath, parentWindow);
    if (res == NFD_OKAY) {
        outPaths.reset(out);
    }
    return res;
}

inline nfdresult_t SaveDialog(UniquePathU8& outPath,
                              const nfdu8filteritem_t* filterList = nullptr,
                              nfdfiltersize_t filterCount = 0,
                              const nfdu8char_t* defaultPath = nullptr,
                              const nfdu8char_t* defaultName = nullptr,
                              nfdwindowhandle_t parentWindow = {}) noexcept {
    nfdu8char_t* out;
    nfdresult_t res =
        SaveDialog(out, filterList, filterCount, defaultPath, defaultName, parentWindow);
    if (res == NFD_OKAY) {
        outPath.reset(out);
    }
    return res;
}

inline nfdresult_t PickFolder(UniquePathU8& outPath,
                              const nfdu8char_t* defaultPath = nullptr,
                              nfdwindowhandle_t parentWindow = {}) noexcept {
    nfdu8char_t* out;
    nfdresult_t res = PickFolder(out, defaultPath, parentWindow);
    if (res == NFD_OKAY) {
        outPath.reset(out);
    }
    return res;
}

inline nfdresult_t PickFolderMultiple(UniquePathSet& outPaths,
                                      const nfdu8char_t* defaultPath = nullptr,
                                      nfdwindowhandle_t parentWindow = {}) noexcept {
    const nfdpathset_t* out;
    nfdresult_t res = PickFolderMultiple(out, defaultPath, parentWindow);
    if (res == NFD_OKAY) {
        outPaths.reset(out);
    }
    return res;
}
#endif

namespace PathSet {
inline nfdresult_t Count(const UniquePathSet& uniquePathSet, nfdpathsetsize_t& count) noexcept {
    return Count(uniquePathSet.get(), count);
}
inline nfdresult_t GetPath(const UniquePathSet& uniquePathSet,
                           nfdpathsetsize_t index,
                           UniquePathSetPathN& outPath) noexcept {
    nfdnchar_t* out;
    nfdresult_t res = GetPath(uniquePathSet.get(), index, out);
    if (res == NFD_OKAY) {
        outPath.reset(out);
    }
    return res;
}
#ifdef NFD_DIFFERENT_NATIVE_FUNCTIONS
inline nfdresult_t GetPath(const UniquePathSet& uniquePathSet,
                           nfdpathsetsize_t index,
                           UniquePathSetPathU8& outPath) noexcept {
    nfdu8char_t* out;
    nfdresult_t res = GetPath(uniquePathSet.get(), index, out);
    if (res == NFD_OKAY) {
        outPath.reset(out);
    }
    return res;
}
#endif
}  // namespace PathSet

}  // namespace NFD

#endif