summaryrefslogtreecommitdiffstats
path: root/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/MessageBox.java
blob: 1fe7f41ebc4d9bd17b82d027cd805472540add46 (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
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
package org.eclipse.swt.widgets;

/*
 * Copyright (c) 2000, 2002 IBM Corp.  All rights reserved.
 * This file is made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 */

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.internal.carbon.*;

public  class MessageBox extends Dialog {
	String message = "";
	

public MessageBox (Shell parent) {
	this (parent, SWT.OK | SWT.ICON_INFORMATION | SWT.APPLICATION_MODAL);
}

public MessageBox (Shell parent, int style) {
	super (parent, checkStyle (style));
	checkSubclass ();
}

static int checkStyle (int style) {
	if ((style & (SWT.PRIMARY_MODAL | SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) == 0) style |= SWT.APPLICATION_MODAL;
	int mask = (SWT.YES | SWT.NO | SWT.OK | SWT.CANCEL | SWT.ABORT | SWT.RETRY | SWT.IGNORE);
	int bits = style & mask;
	if (bits == SWT.OK || bits == SWT.CANCEL || bits == (SWT.OK | SWT.CANCEL)) return style;
	if (bits == SWT.YES || bits == SWT.NO || bits == (SWT.YES | SWT.NO) || bits == (SWT.YES | SWT.NO | SWT.CANCEL)) return style;
	if (bits == (SWT.RETRY | SWT.CANCEL) || bits == (SWT.ABORT | SWT.RETRY | SWT.IGNORE)) return style;
	style = (style & ~mask) | SWT.OK;
	return style;
}

public String getMessage () {
	return message;
}

int getCFString (String id) {
	String string = SWT.getMessage(id);
	char [] buffer = new char [string.length ()];
	string.getChars (0, buffer.length, buffer, 0);
	return OS.CFStringCreateWithCharacters (OS.kCFAllocatorDefault, buffer, buffer.length);
}

public int open () {
	short alertType = OS.kAlertPlainAlert;
	if ((style & SWT.ICON_ERROR) != 0) alertType = OS.kAlertStopAlert;
	if ((style & SWT.ICON_INFORMATION) != 0) alertType = OS.kAlertNoteAlert;
	if ((style & SWT.ICON_QUESTION) != 0) alertType = OS.kAlertNoteAlert;
	if ((style & SWT.ICON_WARNING) != 0) alertType = OS.kAlertCautionAlert;
	if ((style & SWT.ICON_WORKING) != 0) alertType = OS.kAlertNoteAlert;
	
	int error = 0;
	int explanation = 0;
	String errorString = (title != "") ? title : ((message != "") ? message : null);
	String explanationString = (title == "") ? null : ((message != "") ? message : null);
	if (errorString != null) {
		char [] buffer = new char [errorString.length ()];
		errorString.getChars (0, buffer.length, buffer, 0);
		error = OS.CFStringCreateWithCharacters (OS.kCFAllocatorDefault, buffer, buffer.length);
	} 
	if (explanationString != null) {
		char [] buffer = new char [explanationString.length ()];
		explanationString.getChars (0, buffer.length, buffer, 0);
		explanation = OS.CFStringCreateWithCharacters (OS.kCFAllocatorDefault, buffer, buffer.length);		
	}
	
	AlertStdCFStringAlertParamRec param = new AlertStdCFStringAlertParamRec ();
	param.version = OS.kStdCFStringAlertVersionOne;
	param.position = (short)OS.kWindowAlertPositionParentWindowScreen;
	int defaultStr = 0, cancelStr = 0, otherStr = 0;
	int mask = (SWT.YES | SWT.NO | SWT.OK | SWT.CANCEL | SWT.ABORT | SWT.RETRY | SWT.IGNORE);
	int bits = style & mask;
	switch (bits) {
		case SWT.OK:
			param.defaultButton = (short)OS.kAlertStdAlertOKButton;
			param.defaultText = OS.kAlertDefaultOKText;
			break;
		case SWT.CANCEL:
			param.defaultButton = (short)OS.kAlertStdAlertOKButton;
			param.defaultText = defaultStr = getCFString ("SWT_Cancel");
			break;
		case SWT.OK | SWT.CANCEL:
			param.defaultButton = (short)OS.kAlertStdAlertOKButton;
			param.defaultText = OS.kAlertDefaultOKText;
			param.cancelButton = (short)OS.kAlertStdAlertCancelButton;
			param.cancelText = OS.kAlertDefaultCancelText;
			break;
		case SWT.YES:
			param.defaultButton = (short)OS.kAlertStdAlertOKButton;
			param.defaultText = defaultStr = getCFString ("SWT_Yes");
			break;
		case SWT.NO:
			param.cancelButton = (short)OS.kAlertStdAlertOKButton;
			param.cancelText = defaultStr = getCFString ("SWT_No");
			break;
		case SWT.YES | SWT.NO:
			param.defaultButton = (short)OS.kAlertStdAlertOKButton;
			param.defaultText = defaultStr = getCFString ("SWT_Yes");
			param.cancelButton = (short)OS.kAlertStdAlertCancelButton;
			param.cancelText = cancelStr = getCFString ("SWT_No");
			break;
		case SWT.YES | SWT.NO | SWT.CANCEL:				
			param.defaultButton = (short)OS.kAlertStdAlertOKButton;
			param.defaultText = defaultStr = getCFString ("SWT_Yes");
			param.otherText = cancelStr = getCFString ("SWT_No");
			param.cancelButton = (short)OS.kAlertStdAlertCancelButton;
			param.cancelText = OS.kAlertDefaultCancelText;
			break;
		case SWT.RETRY | SWT.CANCEL:
			param.defaultButton = (short)OS.kAlertStdAlertOKButton;
			param.defaultText = defaultStr = getCFString ("SWT_Retry");
			param.cancelButton = (short)OS.kAlertStdAlertCancelButton;
			param.cancelText = OS.kAlertDefaultCancelText;
			break;
		case SWT.ABORT | SWT.RETRY | SWT.IGNORE:
			param.defaultButton = (short)OS.kAlertStdAlertOKButton;
			param.defaultText = defaultStr = getCFString ("SWT_Abort");
			param.otherText = cancelStr = getCFString ("SWT_Retry");
			param.cancelButton = (short)OS.kAlertStdAlertCancelButton;
			param.cancelText = otherStr = getCFString ("SWT_Ignore");
			break;
	}
	
	int [] dialogRef= new int [1];
	int result = OS.CreateStandardAlert (alertType, error, explanation, param, dialogRef);
	if (error != 0) OS.CFRelease(error);
	if (explanation != 0) OS.CFRelease(explanation);
	if (defaultStr != 0) OS.CFRelease(defaultStr);
	if (cancelStr != 0) OS.CFRelease(cancelStr);
	if (otherStr != 0) OS.CFRelease(otherStr);
	
	if (dialogRef[0] != 0) {
		short [] outItemHit = new short [1];
		OS.RunStandardAlert(dialogRef[0], 0, outItemHit);
		if (outItemHit [0] != 0) {
			switch (bits) {
				case SWT.OK:
					return SWT.OK;
				case SWT.CANCEL:
					return SWT.CANCEL;
				case SWT.OK | SWT.CANCEL:
					if (outItemHit [0] == OS.kAlertStdAlertOKButton) return SWT.OK;
					return SWT.CANCEL;
				case SWT.YES:
					return SWT.YES;
				case SWT.NO:
					return SWT.NO;
				case SWT.YES | SWT.NO:
					if (outItemHit [0] == OS.kAlertStdAlertOKButton) return SWT.YES;
					return SWT.NO;
				case SWT.YES | SWT.NO | SWT.CANCEL:
					if (outItemHit [0] == OS.kAlertStdAlertOKButton) return SWT.YES;
					if (outItemHit [0] == OS.kAlertStdAlertOtherButton) return SWT.NO;
					return SWT.CANCEL;
				case SWT.RETRY | SWT.CANCEL:
					if (outItemHit [0] == OS.kAlertStdAlertOKButton) return SWT.RETRY;
					return SWT.CANCEL;
				case SWT.ABORT | SWT.RETRY | SWT.IGNORE:
					if (outItemHit [0] == OS.kAlertStdAlertOKButton) return SWT.ABORT;
					if (outItemHit [0] == OS.kAlertStdAlertOtherButton) return SWT.RETRY;
					return SWT.IGNORE;
			}
		}
	}
	return SWT.CANCEL;
}

public void setMessage (String string) {
	message = string;
}

}