1 /*******************************************************************************
2 * Copyright (c) 2000, 2015 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.widgets;
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.internal.win32.*;
21 * Instances of this class are used to inform or warn the user.
23 * <dt><b>Styles:</b></dt>
24 * <dd>ICON_ERROR, ICON_INFORMATION, ICON_QUESTION, ICON_WARNING, ICON_WORKING</dd>
25 * <dd>OK, OK | CANCEL</dd>
26 * <dd>YES | NO, YES | NO | CANCEL</dd>
27 * <dd>RETRY | CANCEL</dd>
28 * <dd>ABORT | RETRY | IGNORE</dd>
29 * <dt><b>Events:</b></dt>
33 * Note: Only one of the styles ICON_ERROR, ICON_INFORMATION, ICON_QUESTION,
34 * ICON_WARNING and ICON_WORKING may be specified.
36 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
39 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample, Dialog tab</a>
40 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
41 * @noextend This class is not intended to be subclassed by clients.
43 public class MessageBox extends Dialog {
47 * Constructs a new instance of this class given only its parent.
49 * @param parent a shell which will be the parent of the new instance
51 * @exception IllegalArgumentException <ul>
52 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
54 * @exception SWTException <ul>
55 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
56 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
59 public MessageBox (Shell parent) {
60 this (parent, SWT.OK | SWT.ICON_INFORMATION | SWT.APPLICATION_MODAL);
64 * Constructs a new instance of this class given its parent
65 * and a style value describing its behavior and appearance.
67 * The style value is either one of the style constants defined in
68 * class <code>SWT</code> which is applicable to instances of this
69 * class, or must be built by <em>bitwise OR</em>'ing together
70 * (that is, using the <code>int</code> "|" operator) two or more
71 * of those <code>SWT</code> style constants. The class description
72 * lists the style constants that are applicable to the class.
73 * Style bits are also inherited from superclasses.
75 * @param parent a shell which will be the parent of the new instance
76 * @param style the style of dialog to construct
78 * @exception IllegalArgumentException <ul>
79 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
81 * @exception SWTException <ul>
82 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
83 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
87 * @see SWT#ICON_INFORMATION
88 * @see SWT#ICON_QUESTION
89 * @see SWT#ICON_WARNING
90 * @see SWT#ICON_WORKING
99 public MessageBox (Shell parent, int style) {
100 super (parent, checkStyle (parent, checkStyle (style)));
104 static int checkStyle (int style) {
105 int mask = (SWT.YES | SWT.NO | SWT.OK | SWT.CANCEL | SWT.ABORT | SWT.RETRY | SWT.IGNORE);
106 int bits = style & mask;
107 if (bits == SWT.OK || bits == SWT.CANCEL || bits == (SWT.OK | SWT.CANCEL)) return style;
108 if (bits == SWT.YES || bits == SWT.NO || bits == (SWT.YES | SWT.NO) || bits == (SWT.YES | SWT.NO | SWT.CANCEL)) return style;
109 if (bits == (SWT.RETRY | SWT.CANCEL) || bits == (SWT.ABORT | SWT.RETRY | SWT.IGNORE)) return style;
110 style = (style & ~mask) | SWT.OK;
115 * Returns the dialog's message, or an empty string if it does not have one.
116 * The message is a description of the purpose for which the dialog was opened.
117 * This message will be visible in the dialog while it is open.
119 * @return the message
121 public String getMessage () {
126 * Makes the dialog visible and brings it to the front
129 * @return the ID of the button that was selected to dismiss the
130 * message box (e.g. SWT.OK, SWT.CANCEL, etc.)
132 * @exception SWTException <ul>
133 * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li>
134 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li>
139 /* Compute the MessageBox style */
141 if ((style & SWT.OK) == SWT.OK) buttonBits = OS.MB_OK;
142 if ((style & (SWT.OK | SWT.CANCEL)) == (SWT.OK | SWT.CANCEL)) buttonBits = OS.MB_OKCANCEL;
143 if ((style & (SWT.YES | SWT.NO)) == (SWT.YES | SWT.NO)) buttonBits = OS.MB_YESNO;
144 if ((style & (SWT.YES | SWT.NO | SWT.CANCEL)) == (SWT.YES | SWT.NO | SWT.CANCEL)) buttonBits = OS.MB_YESNOCANCEL;
145 if ((style & (SWT.RETRY | SWT.CANCEL)) == (SWT.RETRY | SWT.CANCEL)) buttonBits = OS.MB_RETRYCANCEL;
146 if ((style & (SWT.ABORT | SWT.RETRY | SWT.IGNORE)) == (SWT.ABORT | SWT.RETRY | SWT.IGNORE)) buttonBits = OS.MB_ABORTRETRYIGNORE;
147 if (buttonBits == 0) buttonBits = OS.MB_OK;
150 if ((style & SWT.ICON_ERROR) != 0) iconBits = OS.MB_ICONERROR;
151 if ((style & SWT.ICON_INFORMATION) != 0) iconBits = OS.MB_ICONINFORMATION;
152 if ((style & SWT.ICON_QUESTION) != 0) iconBits = OS.MB_ICONQUESTION;
153 if ((style & SWT.ICON_WARNING) != 0) iconBits = OS.MB_ICONWARNING;
154 if ((style & SWT.ICON_WORKING) != 0) iconBits = OS.MB_ICONINFORMATION;
157 if ((style & SWT.PRIMARY_MODAL) != 0) modalBits = OS.MB_APPLMODAL;
158 if ((style & SWT.APPLICATION_MODAL) != 0) modalBits = OS.MB_TASKMODAL;
159 if ((style & SWT.SYSTEM_MODAL) != 0) modalBits = OS.MB_SYSTEMMODAL;
161 int bits = buttonBits | iconBits | modalBits;
162 if ((style & SWT.RIGHT_TO_LEFT) != 0) bits |= OS.MB_RTLREADING | OS.MB_RIGHT;
163 if ((style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT)) == 0) {
164 if (parent != null && (parent.style & SWT.MIRRORED) != 0) {
165 bits |= OS.MB_RTLREADING | OS.MB_RIGHT;
170 * Feature in Windows. System modal is not supported
171 * on Windows 95 and NT. The fix is to convert system
172 * modal to task modal.
174 if ((bits & OS.MB_SYSTEMMODAL) != 0) {
175 bits |= OS.MB_TASKMODAL;
176 bits &= ~OS.MB_SYSTEMMODAL;
177 /* Force a system modal message box to the front */
178 bits |= OS.MB_TOPMOST;
182 * Feature in Windows. In order for MB_TASKMODAL to work,
183 * the parent HWND of the MessageBox () call must be NULL.
184 * If the parent is not NULL, MB_TASKMODAL behaves the
185 * same as MB_APPLMODAL. The fix to set the parent HWND
186 * anyway and not rely on MB_MODAL to work by making the
187 * parent be temporarily modal.
189 long hwndOwner = parent != null ? parent.handle : 0;
190 Display display = parent != null ? parent.getDisplay (): Display.getCurrent ();
191 Dialog oldModal = null;
192 if ((bits & OS.MB_TASKMODAL) != 0) {
193 oldModal = display.getModalDialog ();
194 display.setModalDialog (this);
197 /* Open the message box */
198 display.sendPreExternalEventDispatchEvent ();
199 /* Use the character encoding for the default locale */
200 TCHAR buffer1 = new TCHAR (0, message, true);
201 TCHAR buffer2 = new TCHAR (0, title, true);
202 display.externalEventLoop = true;
203 int code = OS.MessageBox (hwndOwner, buffer1, buffer2, bits);
204 display.externalEventLoop = false;
205 display.sendPostExternalEventDispatchEvent ();
207 /* Clear the temporarily dialog modal parent */
208 if ((bits & OS.MB_TASKMODAL) != 0) {
209 display.setModalDialog (oldModal);
213 * This code is intentionally commented. On some
214 * platforms, the owner window is repainted right
215 * away when a dialog window exits. This behavior
216 * is currently unspecified.
218 // if (hwndOwner != 0) OS.UpdateWindow (hwndOwner);
220 /* Compute and return the result */
222 int type = bits & 0x0F;
223 if (type == OS.MB_OK) return SWT.OK;
224 if (type == OS.MB_OKCANCEL) {
225 return (code == OS.IDOK) ? SWT.OK : SWT.CANCEL;
227 if (type == OS.MB_YESNO) {
228 return (code == OS.IDYES) ? SWT.YES : SWT.NO;
230 if (type == OS.MB_YESNOCANCEL) {
231 if (code == OS.IDYES) return SWT.YES;
232 if (code == OS.IDNO) return SWT.NO;
235 if (type == OS.MB_RETRYCANCEL) {
236 return (code == OS.IDRETRY) ? SWT.RETRY : SWT.CANCEL;
238 if (type == OS.MB_ABORTRETRYIGNORE) {
239 if (code == OS.IDRETRY) return SWT.RETRY;
240 if (code == OS.IDABORT) return SWT.ABORT;
248 * Sets the dialog's message, which is a description of
249 * the purpose for which it was opened. This message will be
250 * visible on the dialog while it is open.
252 * @param string the message
254 * @exception IllegalArgumentException <ul>
255 * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
258 public void setMessage (String string) {
259 if (string == null) error (SWT.ERROR_NULL_ARGUMENT);