aboutsummaryrefslogtreecommitdiff
path: root/core/src/Utils/Dialog/Dialog_macos.mm
blob: c0164a0af68c5514874e3f39960612e44ed41605 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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