-
Notifications
You must be signed in to change notification settings - Fork 2
/
qcocoamessagebox.mm
238 lines (179 loc) · 7.86 KB
/
qcocoamessagebox.mm
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "pch.h"
#include "qcocoamessagebox.h"
#include "qcocoaicon.h"
#import <AppKit/AppKit.h>
int processMessageBoxResult(NSAlert *alert, NSInteger retCode, QCocoaMessageBox *pMsgBox);
// CocoaMessageBoxHandler
@interface CocoaMessageBoxHandler : NSObject {
}
-(id)initWithLoop:(QEventLoop*)lp;
@property (readonly, nonatomic, nonnull) QEventLoop *loop;
@end
@implementation CocoaMessageBoxHandler
@synthesize loop;
-(id)initWithLoop:(QEventLoop*)lp
{
self = [super init];
loop = lp;
return self;
}
- (void) alertDidEnd:(NSAlert *) alert returnCode:(NSInteger) returnCode contextInfo:(void *) contextInfo
{
Q_UNUSED(contextInfo);
Q_UNUSED(alert);
[self loop]->exit( static_cast<int>(returnCode));
}
@end
// end of CocoaMessageBoxHandler
int QCocoaMessageBox::exec()
{
// Make us the active application
[NSApp activateIgnoringOtherApps:YES];
QMacAutoReleasePool pool;
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
QString txt = text();
QString infoText = informativeText();
if (txt.size())
[alert setMessageText: txt.toNSString()];
if (infoText.size())
[alert setInformativeText: infoText.toNSString()];
if (icon() == QMessageBox::Critical)
alert.alertStyle = NSAlertStyleCritical;
else if (!iconPixmap().isNull()) //NSAlertStyleWarning почему-то не меняет иконки поэтому буду использовать этот метод
alert.icon = QCocoaIcon::imageFromQIcon(iconPixmap());
QCheckBox *checkBtn = checkBox();
if (checkBtn)
{
[alert setShowsSuppressionButton:YES];
// use small checkbox, it's prettier
NSCell *cell = [[alert suppressionButton] cell];
[cell setControlSize:NSControlSizeSmall];
[cell setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
if (checkBtn->isChecked())
[cell setState: NSOnState];
// если не указывать текст QCheckBox то будет дефолтный от macOS
if (checkBtn->text().size())
[cell setTitle: checkBtn->text().toNSString()];
}
for (QAbstractButton *btn : buttons())
{
QMessageBox::ButtonRole role = buttonRole(btn);
NSButton *addedButton = [alert addButtonWithTitle: btn->text().toNSString()];
if (role == QMessageBox::RejectRole)
[addedButton setKeyEquivalent:[NSString stringWithFormat:@"%C", 0x1b]]; // Escape key
}
NSModalResponse retCode = 0;
if (QObject::parent())
{
QEventLoop loop;
CocoaMessageBoxHandler *handler = [[CocoaMessageBoxHandler alloc] initWithLoop: &loop];
QWidget *parent = static_cast<QWidget *>( QObject::parent() );
NSView *view = reinterpret_cast<NSView *>( parent->winId());
NSWindow *wnd = [view window];
[alert beginSheetModalForWindow:wnd modalDelegate:handler didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil ];
retCode = loop.exec();
[handler release];
}
else
retCode = [alert runModal];
return processMessageBoxResult(alert, retCode, this);
}
int processMessageBoxResult(NSAlert *alert, NSInteger retCode, QCocoaMessageBox *pMsgBox)
{
int nReturnCode = QMessageBox::NoButton;
QCheckBox *checkBtn = pMsgBox->checkBox();
if (checkBtn && [[alert suppressionButton] state] == NSOnState)
checkBtn->setChecked(true);
QAbstractButton *btn = nullptr;
int nButtonIndex = static_cast<int>(retCode) - NSAlertFirstButtonReturn;
if (nButtonIndex < pMsgBox->buttons().size() && nButtonIndex >=0)
btn = pMsgBox->buttons().at(nButtonIndex);
if (btn)
{
if (pMsgBox->button(QMessageBox::Ok) == btn)
nReturnCode = QMessageBox::Ok;
else if (pMsgBox->button(QMessageBox::Open) == btn)
nReturnCode = QMessageBox::Open;
else if (pMsgBox->button(QMessageBox::Save) == btn)
nReturnCode = QMessageBox::Save;
else if (pMsgBox->button(QMessageBox::Cancel) == btn)
nReturnCode = QMessageBox::Cancel;
else if (pMsgBox->button(QMessageBox::Close) == btn)
nReturnCode = QMessageBox::Close;
else if (pMsgBox->button(QMessageBox::Discard) == btn)
nReturnCode = QMessageBox::Discard;
else if (pMsgBox->button(QMessageBox::Apply) == btn)
nReturnCode = QMessageBox::Apply;
else if (pMsgBox->button(QMessageBox::Reset) == btn)
nReturnCode = QMessageBox::Reset;
else if (pMsgBox->button(QMessageBox::RestoreDefaults) == btn)
nReturnCode = QMessageBox::RestoreDefaults;
else if (pMsgBox->button(QMessageBox::Help) == btn)
nReturnCode = QMessageBox::Help;
else if (pMsgBox->button(QMessageBox::SaveAll) == btn)
nReturnCode = QMessageBox::SaveAll;
else if (pMsgBox->button(QMessageBox::Yes) == btn)
nReturnCode = QMessageBox::Yes;
else if (pMsgBox->button(QMessageBox::YesToAll) == btn)
nReturnCode = QMessageBox::YesToAll;
else if (pMsgBox->button(QMessageBox::No) == btn)
nReturnCode = QMessageBox::No;
else if (pMsgBox->button(QMessageBox::NoToAll) == btn)
nReturnCode = QMessageBox::NoToAll;
else if (pMsgBox->button(QMessageBox::Abort) == btn)
nReturnCode = QMessageBox::Abort;
else if (pMsgBox->button(QMessageBox::Retry) == btn)
nReturnCode = QMessageBox::Retry;
else if (pMsgBox->button(QMessageBox::Ignore) == btn)
nReturnCode = QMessageBox::Ignore;
else if (pMsgBox->button(QMessageBox::Ignore) == btn)
nReturnCode = QMessageBox::Ignore;
else
{
// if the message box uses standard buttons, we need to return QMessageBox::StandardButton
// otherwise, the user must determine which button was clicked via the QMessageBox::clickedButton method
btn->click();
}
}
return nReturnCode;
}
QMessageBox::StandardButton QCocoaMessageBox::critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
{
QCocoaMessageBox msgBox(parent);
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle(title);
msgBox.setText(text);
msgBox.setStandardButtons(buttons);
msgBox.setDefaultButton(defaultButton);
return (QMessageBox::StandardButton)msgBox.exec();
}
QMessageBox::StandardButton QCocoaMessageBox::information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
{
QCocoaMessageBox msgBox(parent);
msgBox.setIcon(QMessageBox::Information);
msgBox.setWindowTitle(title);
msgBox.setText(text);
msgBox.setStandardButtons(buttons);
msgBox.setDefaultButton(defaultButton);
return (QMessageBox::StandardButton)msgBox.exec();
}
QMessageBox::StandardButton QCocoaMessageBox::question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
{
QCocoaMessageBox msgBox(parent);
msgBox.setIcon(QMessageBox::Question);
msgBox.setWindowTitle(title);
msgBox.setText(text);
msgBox.setStandardButtons(buttons);
msgBox.setDefaultButton(defaultButton);
return (QMessageBox::StandardButton)msgBox.exec();
}
QMessageBox::StandardButton QCocoaMessageBox::warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
{
QCocoaMessageBox msgBox(parent);
msgBox.setIcon(QMessageBox::Warning);
msgBox.setWindowTitle(title);
msgBox.setText(text);
msgBox.setStandardButtons(buttons);
msgBox.setDefaultButton(defaultButton);
return (QMessageBox::StandardButton)msgBox.exec();
}