| 1 |
## File Open API |
| 2 |
|
| 3 |
The `pfd::save_file` class handles file saving 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::save_file::save_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::force_overwrite` to disable a potential warning when |
| 15 |
saving to an existing file. |
| 16 |
|
| 17 |
The selected file is queried using `pfd::save_file::result()`. If the user canceled the |
| 18 |
operation, the returned file name is empty: |
| 19 |
|
| 20 |
```cpp |
| 21 |
std::string pfd::save_file::result(); |
| 22 |
``` |
| 23 |
|
| 24 |
It is possible to ask the file save dialog whether the user took action using the |
| 25 |
`pfd::message::ready()` method, with an optional `timeout` argument. If the user did not validate |
| 26 |
the dialog within `timeout` milliseconds, the function will return `false`: |
| 27 |
|
| 28 |
```cpp |
| 29 |
bool pfd::save_file::ready(int timeout = pfd::default_wait_timeout); |
| 30 |
``` |
| 31 |
|
| 32 |
## Example 1: simple file selection |
| 33 |
|
| 34 |
Using `pfd::save_file::result()` will wait for user action before returning. This operation will |
| 35 |
block and return the user choice: |
| 36 |
|
| 37 |
```cpp |
| 38 |
auto destination = pfd::save_file("Select a file").result(); |
| 39 |
if (!destination.empty()) |
| 40 |
std::cout << "User selected file " << destination << "\n"; |
| 41 |
``` |
| 42 |
|
| 43 |
## Example 2: filters |
| 44 |
|
| 45 |
The filter list enumerates filter names and corresponded space-separated wildcard lists. It |
| 46 |
defaults to `{ "All Files", "*" }`, but here is how to use other options: |
| 47 |
|
| 48 |
```cpp |
| 49 |
auto destination = pfd::save_file("Select a file", ".", |
| 50 |
{ "Image Files", "*.png *.jpg *.jpeg *.bmp", |
| 51 |
"Audio Files", "*.wav *.mp3", |
| 52 |
"All Files", "*" }, |
| 53 |
pfd::opt::force_overwrite).result(); |
| 54 |
// Do something with destination |
| 55 |
std::cout << "Selected file: " << destination << "\n"; |
| 56 |
``` |
| 57 |
|
| 58 |
## Example 3: asynchronous file save |
| 59 |
|
| 60 |
Using `pfd::save_file::ready()` allows the application to perform other tasks while waiting for |
| 61 |
user input: |
| 62 |
|
| 63 |
```cpp |
| 64 |
// File save dialog |
| 65 |
auto dialog = pfd::save_file("Select file to save"); |
| 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 << "User selected file: " << dialog.result() << "\n"; |
| 73 |
``` |