]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/widgets/MessageBox.java
Remove invalid SHA-256-Digests
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / widgets / MessageBox.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2015 IBM Corporation and others.
3  *
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/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.widgets;
15
16
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.internal.win32.*;
19
20 /**
21  * Instances of this class are used to inform or warn the user.
22  * <dl>
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>
30  * <dd>(none)</dd>
31  * </dl>
32  * <p>
33  * Note: Only one of the styles ICON_ERROR, ICON_INFORMATION, ICON_QUESTION,
34  * ICON_WARNING and ICON_WORKING may be specified.
35  * </p><p>
36  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
37  * </p>
38  *
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.
42  */
43 public  class MessageBox extends Dialog {
44         String message = "";
45
46 /**
47  * Constructs a new instance of this class given only its parent.
48  *
49  * @param parent a shell which will be the parent of the new instance
50  *
51  * @exception IllegalArgumentException <ul>
52  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
53  * </ul>
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>
57  * </ul>
58  */
59 public MessageBox (Shell parent) {
60         this (parent, SWT.OK | SWT.ICON_INFORMATION | SWT.APPLICATION_MODAL);
61 }
62
63 /**
64  * Constructs a new instance of this class given its parent
65  * and a style value describing its behavior and appearance.
66  * <p>
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.
74  *
75  * @param parent a shell which will be the parent of the new instance
76  * @param style the style of dialog to construct
77  *
78  * @exception IllegalArgumentException <ul>
79  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
80  * </ul>
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>
84  * </ul>
85  *
86  * @see SWT#ICON_ERROR
87  * @see SWT#ICON_INFORMATION
88  * @see SWT#ICON_QUESTION
89  * @see SWT#ICON_WARNING
90  * @see SWT#ICON_WORKING
91  * @see SWT#OK
92  * @see SWT#CANCEL
93  * @see SWT#YES
94  * @see SWT#NO
95  * @see SWT#ABORT
96  * @see SWT#RETRY
97  * @see SWT#IGNORE
98  */
99 public MessageBox (Shell parent, int style) {
100         super (parent, checkStyle (parent, checkStyle (style)));
101         checkSubclass ();
102 }
103
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;
111         return style;
112 }
113
114 /**
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.
118  *
119  * @return the message
120  */
121 public String getMessage () {
122         return message;
123 }
124
125 /**
126  * Makes the dialog visible and brings it to the front
127  * of the display.
128  *
129  * @return the ID of the button that was selected to dismiss the
130  *         message box (e.g. SWT.OK, SWT.CANCEL, etc.)
131  *
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>
135  * </ul>
136  */
137 public int open () {
138
139         /* Compute the MessageBox style */
140         int buttonBits = 0;
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;
148
149         int iconBits = 0;
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;
155
156         int modalBits = 0;
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;
160
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;
166                 }
167         }
168
169         /*
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.
173         */
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;
179         }
180
181         /*
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.
188         */
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);
195         }
196
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 ();
206
207         /* Clear the temporarily dialog modal parent */
208         if ((bits & OS.MB_TASKMODAL) != 0) {
209                 display.setModalDialog (oldModal);
210         }
211
212         /*
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.
217         */
218 //      if (hwndOwner != 0) OS.UpdateWindow (hwndOwner);
219
220         /* Compute and return the result */
221         if (code != 0) {
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;
226                 }
227                 if (type == OS.MB_YESNO) {
228                         return (code == OS.IDYES) ? SWT.YES : SWT.NO;
229                 }
230                 if (type == OS.MB_YESNOCANCEL) {
231                         if (code == OS.IDYES) return SWT.YES;
232                         if (code == OS.IDNO) return SWT.NO;
233                         return SWT.CANCEL;
234                 }
235                 if (type == OS.MB_RETRYCANCEL) {
236                         return (code == OS.IDRETRY) ? SWT.RETRY : SWT.CANCEL;
237                 }
238                 if (type == OS.MB_ABORTRETRYIGNORE) {
239                         if (code == OS.IDRETRY) return SWT.RETRY;
240                         if (code == OS.IDABORT) return SWT.ABORT;
241                         return SWT.IGNORE;
242                 }
243         }
244         return SWT.CANCEL;
245 }
246
247 /**
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.
251  *
252  * @param string the message
253  *
254  * @exception IllegalArgumentException <ul>
255  *    <li>ERROR_NULL_ARGUMENT - if the string is null</li>
256  * </ul>
257  */
258 public void setMessage (String string) {
259         if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
260         message = string;
261 }
262
263 }