|
| 1 | +## Message Box API |
| 2 | + |
| 3 | +Displaying a message box is done using the `pfd::message` class. It can be provided a title, a |
| 4 | +message text, a `choice` representing which buttons need to be rendered, and an `icon` for the |
| 5 | +message: |
| 6 | + |
| 7 | +```cpp |
| 8 | +pfd::message::message(std::string const &title, |
| 9 | + std::string const &text, |
| 10 | + pfd::choice choice = pfd::choice::ok_cancel, |
| 11 | + pfd::icon icon = pfd::icon::info); |
| 12 | + |
| 13 | +enum class pfd::choice { ok, ok_cancel, yes_no, yes_no_cancel }; |
| 14 | + |
| 15 | +enum class pfd::icon { info, warning, error, question }; |
| 16 | +``` |
| 17 | +
|
| 18 | +The pressed button is queried using `pfd::message::result()`. If the dialog box is closed by any |
| 19 | +other means, the `pfd::button::cancel` is assumed: |
| 20 | +
|
| 21 | +```cpp |
| 22 | +pfd::button pfd::message::result(); |
| 23 | +
|
| 24 | +enum class pfd::button { ok, cancel, yes, no }; |
| 25 | +``` |
| 26 | + |
| 27 | +It is possible to ask the dialog box whether the user took action using the `pfd::message::ready()` |
| 28 | +method, with an optional `timeout` argument. If the user did not press a button within `timeout` |
| 29 | +milliseconds, the function will return `false`: |
| 30 | + |
| 31 | +```cpp |
| 32 | +bool pfd::message::ready(int timeout = pfd::default_wait_timeout); |
| 33 | +``` |
| 34 | +
|
| 35 | +## Example 1: simple notification |
| 36 | +
|
| 37 | +The `pfd::message` destructor waits for user action, so this operation will block until the user |
| 38 | +closes the message box: |
| 39 | +
|
| 40 | +```cpp |
| 41 | +pfd::message("Problem", "An error occurred while doing things", |
| 42 | + pfd::choice::ok, pfd::icon::error); |
| 43 | +``` |
| 44 | + |
| 45 | +## Example 2: retrieving the pressed button |
| 46 | + |
| 47 | +Using `pfd::message::result()` will also wait for user action before returning. This operation will block and return the user choice: |
| 48 | + |
| 49 | +```cpp |
| 50 | +// Ask for user opinion |
| 51 | +auto button = pfd::message("Action requested", "Do you want to proceed with things?", |
| 52 | + pfd::choice::yes_no, pfd::icon::question).result(); |
| 53 | +// Do something with button… |
| 54 | +``` |
| 55 | + |
| 56 | +## Example 3: asynchronous message box |
| 57 | + |
| 58 | +Using `pfd::message::ready()` allows the application to perform other tasks while waiting for |
| 59 | +user input: |
| 60 | + |
| 61 | +```cpp |
| 62 | +// Message box with nice message |
| 63 | +auto box = pfd::message("Unsaved Files", "Do you want to save the current " |
| 64 | + "document before closing the application?", |
| 65 | + pfd::choice::yes_no_cancel, |
| 66 | + pfd::icon::warning); |
| 67 | + |
| 68 | +// Do something while waiting for user input |
| 69 | +while (!box.ready(1000)) |
| 70 | + std::cout << "Waited 1 second for user input...\n"; |
| 71 | + |
| 72 | +// Act depending on the selected button |
| 73 | +switch (box.result()) |
| 74 | +{ |
| 75 | + case pfd::button::yes: std::cout << "User agreed.\n"; break; |
| 76 | + case pfd::button::no: std::cout << "User disagreed.\n"; break; |
| 77 | + case pfd::button::cancel: std::cout << "User freaked out.\n"; break; |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Screenshots |
| 82 | + |
| 83 | +Windows 10: |
| 84 | + |
| 85 | + |
| 86 | +Mac OS X (dark theme): |
| 87 | + |
| 88 | + |
| 89 | +Mac OS X (light theme): |
| 90 | + |
| 91 | + |
| 92 | +Linux (GNOME desktop): |
| 93 | + |
| 94 | + |
| 95 | +Linux (KDE desktop): |
| 96 | + |
| 97 | + |
0 commit comments