feat: add message box dialogs
This commit is contained in:
@@ -17,6 +17,7 @@ namespace gui::dialogs {
|
||||
|
||||
inline constexpr const char* LIBRARY_PATH = "0:/os/libdialogs.lib";
|
||||
inline constexpr const char* RUN_REQUEST_SYMBOL = "dialogs_run_request";
|
||||
inline constexpr const char* MESSAGE_BOX_SYMBOL = "dialogs_message_box";
|
||||
|
||||
enum RequestKind : uint8_t {
|
||||
REQUEST_KIND_FILE = 1,
|
||||
@@ -33,6 +34,21 @@ enum PrintDialogMode : uint8_t {
|
||||
PRINT_DIALOG_SUBMIT = 2,
|
||||
};
|
||||
|
||||
enum MessageBoxButtons : uint8_t {
|
||||
MESSAGE_BOX_OK = 1,
|
||||
MESSAGE_BOX_OK_CANCEL = 2,
|
||||
MESSAGE_BOX_YES_NO = 3,
|
||||
MESSAGE_BOX_YES_NO_CANCEL = 4,
|
||||
};
|
||||
|
||||
enum MessageBoxResult : uint8_t {
|
||||
MESSAGE_BOX_RESULT_NONE = 0,
|
||||
MESSAGE_BOX_RESULT_OK = 1,
|
||||
MESSAGE_BOX_RESULT_CANCEL = 2,
|
||||
MESSAGE_BOX_RESULT_YES = 3,
|
||||
MESSAGE_BOX_RESULT_NO = 4,
|
||||
};
|
||||
|
||||
struct Request {
|
||||
uint8_t kind;
|
||||
uint8_t mode;
|
||||
@@ -75,10 +91,12 @@ inline void reset_result(Result* res) {
|
||||
#ifndef GUI_DIALOGS_LIBRARY_BUILD
|
||||
|
||||
using RunRequestFn = bool (*)(const Request*, Result*);
|
||||
using MessageBoxFn = uint8_t (*)(const char*, const char*, uint8_t);
|
||||
|
||||
struct Runtime {
|
||||
LibHandle* handle;
|
||||
RunRequestFn run_request;
|
||||
MessageBoxFn message_box;
|
||||
};
|
||||
|
||||
inline Runtime g_runtime = {};
|
||||
@@ -101,6 +119,24 @@ inline RunRequestFn resolve_run_request(char* out_message, int out_message_len)
|
||||
return g_runtime.run_request;
|
||||
}
|
||||
|
||||
inline MessageBoxFn resolve_message_box(char* out_message, int out_message_len) {
|
||||
if (g_runtime.message_box) return g_runtime.message_box;
|
||||
|
||||
if (!g_runtime.handle) {
|
||||
g_runtime.handle = libloader::dlopen(LIBRARY_PATH);
|
||||
if (!g_runtime.handle) {
|
||||
if (out_message && out_message_len > 0)
|
||||
safe_copy(out_message, out_message_len, "failed to load dialog library");
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
g_runtime.message_box = (MessageBoxFn)libloader::dlsym(g_runtime.handle, MESSAGE_BOX_SYMBOL);
|
||||
if (!g_runtime.message_box && out_message && out_message_len > 0)
|
||||
safe_copy(out_message, out_message_len, "failed to resolve message box entry point");
|
||||
return g_runtime.message_box;
|
||||
}
|
||||
|
||||
inline bool run_request(const Request* req, Result* out_res, char* out_message, int out_message_len) {
|
||||
if (out_message && out_message_len > 0) out_message[0] = '\0';
|
||||
if (!req) {
|
||||
@@ -202,6 +238,22 @@ inline bool configure_print(const char* title,
|
||||
return ok;
|
||||
}
|
||||
|
||||
inline MessageBoxResult message_box(const char* title,
|
||||
const char* message,
|
||||
MessageBoxButtons buttons = MESSAGE_BOX_OK,
|
||||
char* out_message = nullptr, int out_message_len = 0) {
|
||||
if (out_message && out_message_len > 0) out_message[0] = '\0';
|
||||
MessageBoxFn fn = resolve_message_box(out_message, out_message_len);
|
||||
if (!fn) return MESSAGE_BOX_RESULT_NONE;
|
||||
|
||||
uint8_t result = fn(title ? title : "Message",
|
||||
message ? message : "",
|
||||
(uint8_t)buttons);
|
||||
if (result == MESSAGE_BOX_RESULT_NONE && out_message && out_message_len > 0 && !out_message[0])
|
||||
safe_copy(out_message, out_message_len, "message box invocation failed");
|
||||
return (MessageBoxResult)result;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace gui::dialogs
|
||||
|
||||
Reference in New Issue
Block a user