| 1 |
## File Open API |
| 2 |
|
| 3 |
The `pfd::open_file` class handles file opening dialogs. It can be provided a title, a starting |
| 4 |
directory and/or pre-selected file, an optional filter for recognised file types, and an optional |
| 5 |
flag to allow multiple selection: |
| 6 |
|
| 7 |
```cpp |
| 8 |
pfd::open_file::open_file(std::string const &title, |
| 9 |
std::string const &initial_path, |
| 10 |
std::vector<std::string> filters = { "All Files", "*" }, |
| 11 |
pfd::opt option = pfd::opt::none); |
| 12 |
``` |
| 13 |
|
| 14 |
The `option` parameter can be `pfd::opt::multiselect` to allow selecting multiple files. |
| 15 |
|
| 16 |
The selected files are queried using `pfd::open_file::result()`. If the user canceled the |
| 17 |
operation, the returned list is empty: |
| 18 |
|
| 19 |
```cpp |
| 20 |
std::vector<std::string> pfd::open_file::result(); |
| 21 |
``` |
| 22 |
|
| 23 |
It is possible to ask the file open dialog whether the user took action using the |
| 24 |
`pfd::message::ready()` method, with an optional `timeout` argument. If the user did not validate |
| 25 |
the dialog within `timeout` milliseconds, the function will return `false`: |
| 26 |
|
| 27 |
```cpp |
| 28 |
bool pfd::open_file::ready(int timeout = pfd::default_wait_timeout); |
| 29 |
``` |
| 30 |
|
| 31 |
## Example 1: simple file selection |
| 32 |
|
| 33 |
Using `pfd::open_file::result()` will wait for user action before returning. This operation will |
| 34 |
block and return the user choice: |
| 35 |
|
| 36 |
```cpp |
| 37 |
auto selection = pfd::open_file("Select a file").result(); |
| 38 |
if (!selection.empty()) |
| 39 |
std::cout << "User selected file " << selection[0] << "\n"; |
| 40 |
``` |
| 41 |
|
| 42 |
## Example 2: filters |
| 43 |
|
| 44 |
The filter list enumerates filter names and corresponded space-separated wildcard lists. It |
| 45 |
defaults to `{ "All Files", "*" }`, but here is how to use other options: |
| 46 |
|
| 47 |
```cpp |
| 48 |
auto selection = pfd::open_file("Select a file", ".", |
| 49 |
{ "Image Files", "*.png *.jpg *.jpeg *.bmp", |
| 50 |
"Audio Files", "*.wav *.mp3", |
| 51 |
"All Files", "*" }, |
| 52 |
pfd::opt::multiselect).result(); |
| 53 |
// Do something with selection |
| 54 |
for (auto const &filename : dialog.result()) |
| 55 |
std::cout << "Selected file: " << filename << "\n"; |
| 56 |
``` |
| 57 |
|
| 58 |
## Example 3: asynchronous file open |
| 59 |
|
| 60 |
Using `pfd::open_file::ready()` allows the application to perform other tasks while waiting for |
| 61 |
user input: |
| 62 |
|
| 63 |
```cpp |
| 64 |
// File open dialog |
| 65 |
auto dialog = pfd::open_file("Select file to open"); |
| 66 |
|
| 67 |
// Do something while waiting for user input |
| 68 |
while (!dialog.ready(1000)) |
| 69 |
std::cout << "Waited 1 second for user input...\n"; |
| 70 |
|
| 71 |
// Act depending on the user choice |
| 72 |
std::cout << "Number of selected files: " << dialog.result().size() << "\n"; |
| 73 |
``` |
| 74 |
|
| 75 |
## Screenshots |
| 76 |
|
| 77 |
Windows 10: |
| 78 |
 |
| 79 |
|
| 80 |
Mac OS X (dark theme): |
| 81 |
 |
| 82 |
|
| 83 |
Mac OS X (light theme): |
| 84 |
 |
| 85 |
|
| 86 |
Linux (GNOME desktop): |
| 87 |
 |
| 88 |
|
| 89 |
Linux (KDE desktop): |
| 90 |
 |