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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
//-----------------------------------------------------------------------------
// Name: error.mm
// Developer: Wolfire Games LLC
// Description:
// License: Read below
//-----------------------------------------------------------------------------
//
// Copyright 2022 Wolfire Games LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-----------------------------------------------------------------------------
#include <Internal/error.h>
#include <Internal/config.h>
#include <UserInput/input.h>
#include <Logging/logdata.h>
#include <map>
#include <string>
#include <SDL.h>
#import <AppKit/NSAlert.h>
#import <AppKit/NSTextField.h>
#import <AppKit/NSPanel.h>
extern std::map<std::string, int> error_message_history;
std::string QueryString( std::string title, std::string contents )
{
bool old_mouse = Input::Instance()->GetGrabMouse();
Input::Instance()->SetGrabMouse(false);
UIShowCursor(1);
NSString *contentsString = [NSString stringWithUTF8String:contents.c_str()];
NSAlert *alert = [NSAlert alertWithMessageText:contentsString
defaultButton:@"OK"
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@""];
NSTextField *input = [[[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)] autorelease];
[input setStringValue:@""];
[alert setAccessoryView:input];
[[alert window] makeFirstResponder:input];
NSInteger button = [alert runModal];
Input::Instance()->SetGrabMouse(old_mouse);
UIShowCursor(0);
if (button == NSAlertDefaultReturn) {
[input validateEditing];
std::string return_value = [[input stringValue] UTF8String];
return return_value;
} else if (button == NSAlertAlternateReturn) {
return "";
} else {
return "";
}
}
// Basic alert code in case we need it
/*NSAlert *alert = [NSAlert alertWithMessageText:@"Could not open connection to Xgrid server."
defaultButton:@"OK" alternateButton:nil otherButton:nil
informativeTextWithFormat:
@"Check that controller host name was entered correctly, and that it is "
@"available."];
[alert runModal];*/
bool Query(std::string title, std::string contents)
{
bool old_mouse = Input::Instance()->GetGrabMouse();
Input::Instance()->SetGrabMouse(false);
UIShowCursor(1);
NSString *contentsString = [NSString stringWithUTF8String:contents.c_str()];
NSAlert *alert = [NSAlert alertWithMessageText:contentsString
defaultButton:@"Yes"
alternateButton:@"No"
otherButton:nil
informativeTextWithFormat:@""];
NSInteger button = [alert runModal];
Input::Instance()->SetGrabMouse(old_mouse);
UIShowCursor(0);
return (button == NSAlertDefaultReturn);
}
ErrorResponse DisplayError(const char* title, const char* contents, ErrorType type, bool allow_repetition)
{
LOGI << "Displaying message: " << title << ", " << contents << std::endl;
if( config["no_dialogues"].toBool() )
{
return _continue;
}
if(!allow_repetition && error_message_history[contents]) {
return _continue;
}
error_message_history[contents]++;
bool old_mouse = Input::Instance()->GetGrabMouse();
Input::Instance()->SetGrabMouse(false);
UIShowCursor(1);
NSString *errStr = [NSString stringWithUTF8String:title];
NSString *messageStr = [NSString stringWithUTF8String:contents];
NSAlert *alert = [NSAlert alertWithMessageText:errStr
defaultButton:type == _ok_cancel_retry ? @"Continue" : @"Ok"
alternateButton:type == _ok ? nil : @"Cancel"
otherButton:type == _ok_cancel_retry ? @"Retry" : nil
informativeTextWithFormat:messageStr];
NSInteger button = [alert runModal];
if (button == NSAlertDefaultReturn) {
Input::Instance()->SetGrabMouse(old_mouse);
UIShowCursor(0);
return _continue;
} else if (button == NSAlertAlternateReturn) {
exit(1);
} else {
Input::Instance()->SetGrabMouse(old_mouse);
UIShowCursor(0);
return _retry;
}
}
ErrorResponse DisplayFormatError(ErrorType type,
bool allow_repetition,
const char* title,
const char* fmtcontents,
... ) {
static const int kBufSize = 2048;
char err_buf[kBufSize];
va_list args;
va_start(args, fmtcontents);
VFormatString(err_buf, kBufSize, fmtcontents, args);
va_end(args);
return DisplayError(title, err_buf, type, allow_repetition);
}
|