root/dwt/widgets/MessageBox.d

Revision 320:da968414c383, 10.8 kB (checked in by Frank Benoit <benoit@tionex.de>, 1 month ago)

Merge changes SWT 3.4.1

Line 
1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  * Port to the D programming language:
11  *     Frank Benoit <benoit@tionex.de>
12  *******************************************************************************/
13 module dwt.widgets.MessageBox;
14
15 import dwt.DWT;
16 import dwt.DWTException;
17 import dwt.internal.win32.OS;
18
19 import dwt.widgets.Dialog;
20 import dwt.widgets.Shell;
21 import dwt.widgets.Display;
22 import dwt.dwthelper.utils;
23
24 /**
25  * Instances of this class are used to inform or warn the user.
26  * <dl>
27  * <dt><b>Styles:</b></dt>
28  * <dd>ICON_ERROR, ICON_INFORMATION, ICON_QUESTION, ICON_WARNING, ICON_WORKING</dd>
29  * <dd>OK, OK | CANCEL</dd>
30  * <dd>YES | NO, YES | NO | CANCEL</dd>
31  * <dd>RETRY | CANCEL</dd>
32  * <dd>ABORT | RETRY | IGNORE</dd>
33  * <dt><b>Events:</b></dt>
34  * <dd>(none)</dd>
35  * </dl>
36  * <p>
37  * Note: Only one of the styles ICON_ERROR, ICON_INFORMATION, ICON_QUESTION,
38  * ICON_WARNING and ICON_WORKING may be specified.
39  * </p><p>
40  * IMPORTANT: This class is intended to be subclassed <em>only</em>
41  * within the DWT implementation.
42  * </p>
43  *
44  * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample, Dialog tab</a>
45  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
46  */
47 public  class MessageBox : Dialog {
48
49     alias Dialog.checkStyle checkStyle;
50
51     String message = "";
52     private bool allowNullParent = false;
53
54 /**
55  * Constructs a new instance of this class given only its parent.
56  *
57  * @param parent a shell which will be the parent of the new instance
58  *
59  * @exception IllegalArgumentException <ul>
60  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
61  * </ul>
62  * @exception DWTException <ul>
63  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
64  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
65  * </ul>
66  */
67 public this (Shell parent) {
68     this (parent, DWT.OK | DWT.ICON_INFORMATION | DWT.APPLICATION_MODAL);
69 }
70
71 /**
72  * Constructs a new instance of this class given its parent
73  * and a style value describing its behavior and appearance.
74  * <p>
75  * The style value is either one of the style constants defined in
76  * class <code>DWT</code> which is applicable to instances of this
77  * class, or must be built by <em>bitwise OR</em>'ing together
78  * (that is, using the <code>int</code> "|" operator) two or more
79  * of those <code>DWT</code> style constants. The class description
80  * lists the style constants that are applicable to the class.
81  * Style bits are also inherited from superclasses.
82  *
83  * @param parent a shell which will be the parent of the new instance
84  * @param style the style of dialog to construct
85  *
86  * @exception IllegalArgumentException <ul>
87  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
88  * </ul>
89  * @exception DWTException <ul>
90  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
91  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
92  * </ul>
93  */
94 public this (Shell parent, int style) {
95     super (parent, checkStyle (parent, checkStyle (style)));
96     checkSubclass ();
97 }
98
99 /++
100  + DWT extension, a MessageBox with no parent
101  +/
102 public this (int style) {
103     allowNullParent = true;
104     this( null, style );
105 }
106 // PORT
107 // actually, the parent can be null
108 override void checkParent (Shell parent){
109     if( !allowNullParent ){
110         super.checkParent( parent );
111     }
112 }
113
114 static int checkStyle (int style) {
115     int mask = (DWT.YES | DWT.NO | DWT.OK | DWT.CANCEL | DWT.ABORT | DWT.RETRY | DWT.IGNORE);
116     int bits = style & mask;
117     if (bits is DWT.OK || bits is DWT.CANCEL || bits is (DWT.OK | DWT.CANCEL)) return style;
118     if (bits is DWT.YES || bits is DWT.NO || bits is (DWT.YES | DWT.NO) || bits is (DWT.YES | DWT.NO | DWT.CANCEL)) return style;
119     if (bits is (DWT.RETRY | DWT.CANCEL) || bits is (DWT.ABORT | DWT.RETRY | DWT.IGNORE)) return style;
120     style = (style & ~mask) | DWT.OK;
121     return style;
122 }
123
124 /**
125  * Returns the dialog's message, or an empty string if it does not have one.
126  * The message is a description of the purpose for which the dialog was opened.
127  * This message will be visible in the dialog while it is open.
128  *
129  * @return the message
130  */
131 public String getMessage () {
132     return message;
133 }
134
135 /**
136  * Makes the dialog visible and brings it to the front
137  * of the display.
138  *
139  * @return the ID of the button that was selected to dismiss the
140  *         message box (e.g. DWT.OK, DWT.CANCEL, etc.)
141  *
142  * @exception DWTException <ul>
143  *    <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li>
144  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li>
145  * </ul>
146  */
147 public int open () {
148
149     /* Compute the MessageBox style */
150     int buttonBits = 0;
151     if ((style & DWT.OK) is DWT.OK) buttonBits = OS.MB_OK;
152     if ((style & (DWT.OK | DWT.CANCEL)) is (DWT.OK | DWT.CANCEL)) buttonBits = OS.MB_OKCANCEL;
153     if ((style & (DWT.YES | DWT.NO)) is (DWT.YES | DWT.NO)) buttonBits = OS.MB_YESNO;
154     if ((style & (DWT.YES | DWT.NO | DWT.CANCEL)) is (DWT.YES | DWT.NO | DWT.CANCEL)) buttonBits = OS.MB_YESNOCANCEL;
155     if ((style & (DWT.RETRY | DWT.CANCEL)) is (DWT.RETRY | DWT.CANCEL)) buttonBits = OS.MB_RETRYCANCEL;
156     if ((style & (DWT.ABORT | DWT.RETRY | DWT.IGNORE)) is (DWT.ABORT | DWT.RETRY | DWT.IGNORE)) buttonBits = OS.MB_ABORTRETRYIGNORE;
157     if (buttonBits is 0) buttonBits = OS.MB_OK;
158
159     int iconBits = 0;
160     if ((style & DWT.ICON_ERROR) !is 0) iconBits = OS.MB_ICONERROR;
161     if ((style & DWT.ICON_INFORMATION) !is 0) iconBits = OS.MB_ICONINFORMATION;
162     if ((style & DWT.ICON_QUESTION) !is 0) iconBits = OS.MB_ICONQUESTION;
163     if ((style & DWT.ICON_WARNING) !is 0) iconBits = OS.MB_ICONWARNING;
164     if ((style & DWT.ICON_WORKING) !is 0) iconBits = OS.MB_ICONINFORMATION;
165
166     /* Only MB_APPLMODAL is supported on WinCE */
167     int modalBits = 0;
168     static if (OS.IsWinCE) {
169         if ((style & (DWT.PRIMARY_MODAL | DWT.APPLICATION_MODAL | DWT.SYSTEM_MODAL)) !is 0) {
170             modalBits = OS.MB_APPLMODAL;
171         }
172     } else {
173         if ((style & DWT.PRIMARY_MODAL) !is 0) modalBits = OS.MB_APPLMODAL;
174         if ((style & DWT.APPLICATION_MODAL) !is 0) modalBits = OS.MB_TASKMODAL;
175         if ((style & DWT.SYSTEM_MODAL) !is 0) modalBits = OS.MB_SYSTEMMODAL;
176     }
177
178     int bits = buttonBits | iconBits | modalBits;
179     if ((style & DWT.RIGHT_TO_LEFT) !is 0) bits |= OS.MB_RTLREADING | OS.MB_RIGHT;
180     if ((style & (DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT)) is 0) {
181         if (parent !is null && (parent.style & DWT.MIRRORED) !is 0) {
182             bits |= OS.MB_RTLREADING | OS.MB_RIGHT;
183         }
184     }
185
186     /*
187     * Feature in Windows.  System modal is not supported
188     * on Windows 95 and NT.  The fix is to convert system
189     * modal to task modal.
190     */
191     if ((bits & OS.MB_SYSTEMMODAL) !is 0) {
192         bits |= OS.MB_TASKMODAL;
193         bits &= ~OS.MB_SYSTEMMODAL;
194         /* Force a system modal message box to the front */
195         bits |= OS.MB_TOPMOST;
196     }
197
198     /*
199     * Feature in Windows.  In order for MB_TASKMODAL to work,
200     * the parent HWND of the MessageBox () call must be NULL.
201     * If the parent is not NULL, MB_TASKMODAL behaves the
202     * same as MB_APPLMODAL.  The fix to set the parent HWND
203     * anyway and not rely on MB_MODAL to work by making the
204     * parent be temporarily modal.
205     */
206     HWND hwndOwner = parent !is null ? parent.handle : null;
207     Dialog oldModal = null;
208     Display display = null;
209     if ((bits & OS.MB_TASKMODAL) !is 0) {
210         display = ( allowNullParent && parent is null ) ? Display.getCurrent() : parent.getDisplay ();
211         oldModal = display.getModalDialog ();
212         display.setModalDialog (this);
213     }
214
215     /* Open the message box */
216     /* Use the character encoding for the default locale */
217     TCHAR[] buffer1 = StrToTCHARs (0, message, true);
218     TCHAR[] buffer2 = StrToTCHARs (0, title, true);
219     int code = OS.MessageBox (hwndOwner, buffer1.ptr, buffer2.ptr, bits);
220
221     /* Clear the temporarily dialog modal parent */
222     if ((bits & OS.MB_TASKMODAL) !is 0) {
223         display.setModalDialog (oldModal);
224     }
225
226     /*
227     * This code is intentionally commented.  On some
228     * platforms, the owner window is repainted right
229     * away when a dialog window exits.  This behavior
230     * is currently unspecified.
231     */
232 //  if (hwndOwner !is 0) OS.UpdateWindow (hwndOwner);
233
234     /* Compute and return the result */
235     if (code !is 0) {
236         int type = bits & 0x0F;
237         if (type is OS.MB_OK) return DWT.OK;
238         if (type is OS.MB_OKCANCEL) {
239             return (code is OS.IDOK) ? DWT.OK : DWT.CANCEL;
240         }
241         if (type is OS.MB_YESNO) {
242             return (code is OS.IDYES) ? DWT.YES : DWT.NO;
243         }
244         if (type is OS.MB_YESNOCANCEL) {
245             if (code is OS.IDYES) return DWT.YES;
246             if (code is OS.IDNO) return DWT.NO;
247             return DWT.CANCEL;
248         }
249         if (type is OS.MB_RETRYCANCEL) {
250             return (code is OS.IDRETRY) ? DWT.RETRY : DWT.CANCEL;
251         }
252         if (type is OS.MB_ABORTRETRYIGNORE) {
253             if (code is OS.IDRETRY) return DWT.RETRY;
254             if (code is OS.IDABORT) return DWT.ABORT;
255             return DWT.IGNORE;
256         }
257     }
258     return DWT.CANCEL;
259 }
260
261 /**
262  * Sets the dialog's message, which is a description of
263  * the purpose for which it was opened. This message will be
264  * visible on the dialog while it is open.
265  *
266  * @param string the message
267  *
268  * @exception IllegalArgumentException <ul>
269  *    <li>ERROR_NULL_ARGUMENT - if the string is null</li>
270  * </ul>
271  */
272 public void setMessage (String string) {
273     if (string is null) error (DWT.ERROR_NULL_ARGUMENT);
274     message = string;
275 }
276
277 /++
278  + DWT extension
279  +/
280 public static int showMessageBox(String str, String title, Shell shell, int style) {
281     MessageBox msgBox = (shell is null ) ? new MessageBox( style ) : new MessageBox(shell, style);
282     msgBox.setMessage(str);
283     if(title !is null){
284         msgBox.setText(title);
285     }
286     return msgBox.open();
287 }
288
289 /// DWT extension
290 public static int showInfo(String str, String title = null, Shell shell = null) {
291     return showMessageBox( str, title, shell, DWT.OK | DWT.ICON_INFORMATION );
292 }
293 /// DWT extension
294 alias showInfo showInformation;
295
296 /// DWT extension
297 public static int showWarning(String str, String title = null, Shell shell = null) {
298     return showMessageBox( str, title, shell, DWT.OK | DWT.ICON_WARNING );
299 }
300 /// DWT extension
301 public static int showError(String str, String title = null, Shell shell = null) {
302     return showMessageBox( str, title, shell, DWT.OK | DWT.ICON_ERROR );
303 }
304
305 }
Note: See TracBrowser for help on using the browser.