aboutsummaryrefslogtreecommitdiff
path: root/core/src/Utils/Dialog/Dialog_macos.mm
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-03-27 23:01:07 -0700
committerrtk0c <[email protected]>2021-03-27 23:01:07 -0700
commit442d2d75d71bbc057e667edc301a79fa1cc813be (patch)
treeb5d1e5068a4d481bc6bcd72dca851ac7a85bf7e4 /core/src/Utils/Dialog/Dialog_macos.mm
Initial setup
Diffstat (limited to 'core/src/Utils/Dialog/Dialog_macos.mm')
-rw-r--r--core/src/Utils/Dialog/Dialog_macos.mm113
1 files changed, 113 insertions, 0 deletions
diff --git a/core/src/Utils/Dialog/Dialog_macos.mm b/core/src/Utils/Dialog/Dialog_macos.mm
new file mode 100644
index 0000000..c0164a0
--- /dev/null
+++ b/core/src/Utils/Dialog/Dialog_macos.mm
@@ -0,0 +1,113 @@
+// Adapted from https://github.com/aaronmjacobs/Boxer/blob/master/src/boxer_osx.mm
+#include "Dialog.hpp"
+
+#import <Cocoa/Cocoa.h>
+
+namespace Dialog {
+namespace {
+
+ NSString* const kOkStr = @"OK";
+ NSString* const kCancelStr = @"Cancel";
+ NSString* const kYesStr = @"Yes";
+ NSString* const kNoStr = @"No";
+ NSString* const kQuitStr = @"Quit";
+
+ NSAlertStyle GetAlertStyle(Style style) {
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
+ switch (style) {
+ case Style::Info:
+ return NSAlertStyleInformational;
+ case Style::Warning:
+ return NSAlertStyleWarning;
+ case Style::Error:
+ return NSAlertStyleCritical;
+ case Style::Question:
+ return NSAlertStyleWarning;
+ default:
+ return NSAlertStyleInformational;
+ }
+#else
+ switch (style) {
+ case Style::Info:
+ return NSInformationalAlertStyle;
+ case Style::Warning:
+ return NSWarningAlertStyle;
+ case Style::Error:
+ return NSCriticalAlertStyle;
+ case Style::Question:
+ return NSWarningAlertStyle;
+ default:
+ return NSInformationalAlertStyle;
+ }
+#endif
+ }
+
+ void SetButtons(NSAlert* alert, Buttons buttons) {
+ switch (buttons) {
+ case Buttons::OK:
+ [alert addButtonWithTitle:kOkStr];
+ break;
+ case Buttons::OKCancel:
+ [alert addButtonWithTitle:kOkStr];
+ [alert addButtonWithTitle:kCancelStr];
+ break;
+ case Buttons::YesNo:
+ [alert addButtonWithTitle:kYesStr];
+ [alert addButtonWithTitle:kNoStr];
+ break;
+ case Buttons::Quit:
+ [alert addButtonWithTitle:kQuitStr];
+ break;
+ default:
+ [alert addButtonWithTitle:kOkStr];
+ }
+ }
+
+ Selection GetSelection(int index, Buttons buttons) {
+ switch (buttons) {
+ case Buttons::OK:
+ return index == NSAlertFirstButtonReturn ? Selection::OK : Selection::None;
+ case Buttons::OKCancel:
+ if (index == NSAlertFirstButtonReturn) {
+ return Selection::OK;
+ } else if (index == NSAlertSecondButtonReturn) {
+ return Selection::Cancel;
+ } else {
+ return Selection::None;
+ }
+ case Buttons::YesNo:
+ if (index == NSAlertFirstButtonReturn) {
+ return Selection::Yes;
+ } else if (index == NSAlertSecondButtonReturn) {
+ return Selection::No;
+ } else {
+ return Selection::None;
+ }
+ case Buttons::Quit:
+ return index == NSAlertFirstButtonReturn ? Selection::Quit : Selection::None;
+ default:
+ return Selection::None;
+ }
+ }
+
+} // namespace
+
+Selection show(const char* message, const char* title, Style style, Buttons buttons) {
+ NSAlert* alert = [[NSAlert alloc] init];
+
+ [alert setMessageText:[NSString stringWithCString:title encoding:[NSString defaultCStringEncoding]]];
+ [alert setInformativeText:[NSString stringWithCString:message encoding:[NSString defaultCStringEncoding]]];
+
+ [alert setAlertStyle:GetAlertStyle(style)];
+ SetButtons(alert, buttons);
+
+ // Force the alert to appear on top of any other windows
+ [[alert window] setLevel:NSModalPanelWindowLevel];
+
+ Selection selection = GetSelection([alert runModal], buttons);
+ [alert release];
+
+ return selection;
+}
+
+} // namespace Dialog