Mercurial > projects > dwt-mac
view dwt/widgets/MessageBox.d @ 128:07399639c0c8
Added DWT extension to dwt.widgets.MessageBox
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Sat, 17 Jan 2009 16:26:49 +0100 |
parents | 62202ce0039f |
children | 1a0129cab08e |
line wrap: on
line source
/******************************************************************************* * Copyright (c) 2000, 2008 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation * * Port to the D programming language: * Jacob Carlborg <doob@me.com> *******************************************************************************/ module dwt.widgets.MessageBox; import dwt.dwthelper.utils; import dwt.DWT; import dwt.DWTException; import dwt.internal.cocoa.NSAlert; import dwt.internal.cocoa.NSString; import dwt.internal.cocoa.NSWindow; import dwt.internal.cocoa.OS; import dwt.internal.objc.cocoa.Cocoa; import dwt.widgets.Dialog; import dwt.widgets.Shell; /** * Instances of this class are used to inform or warn the user. * <dl> * <dt><b>Styles:</b></dt> * <dd>ICON_ERROR, ICON_INFORMATION, ICON_QUESTION, ICON_WARNING, ICON_WORKING</dd> * <dd>OK, OK | CANCEL</dd> * <dd>YES | NO, YES | NO | CANCEL</dd> * <dd>RETRY | CANCEL</dd> * <dd>ABORT | RETRY | IGNORE</dd> * <dt><b>Events:</b></dt> * <dd>(none)</dd> * </dl> * <p> * Note: Only one of the styles ICON_ERROR, ICON_INFORMATION, ICON_QUESTION, * ICON_WARNING and ICON_WORKING may be specified. * </p><p> * IMPORTANT: This class is intended to be subclassed <em>only</em> * within the DWT implementation. * </p> * * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample, Dialog tab</a> * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> */ public class MessageBox : Dialog { String message = ""; private bool allowNullParent = false; /** * Constructs a new instance of this class given only its parent. * * @param parent a shell which will be the parent of the new instance * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> * </ul> * @exception DWTException <ul> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> * </ul> */ public this (Shell parent) { this (parent, DWT.OK | DWT.ICON_INFORMATION | DWT.APPLICATION_MODAL); } /** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. * <p> * The style value is either one of the style constants defined in * class <code>DWT</code> which is applicable to instances of this * class, or must be built by <em>bitwise OR</em>'ing together * (that is, using the <code>int</code> "|" operator) two or more * of those <code>DWT</code> style constants. The class description * lists the style constants that are applicable to the class. * Style bits are also inherited from superclasses. * * @param parent a shell which will be the parent of the new instance * @param style the style of dialog to construct * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> * </ul> * @exception DWTException <ul> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> * </ul> */ public this (Shell parent, int style) { super (parent, super.checkStyle (parent, checkStyle (style))); checkSubclass (); } /++ + DWT extension, a MessageBox with no parent +/ public this (int style) { allowNullParent = true; super (parent, super.checkStyle (parent, checkStyle (style))); checkSubclass (); } //PORT //actually, the parent can be null override void checkParent (Shell parent){ if( !allowNullParent ){ super.checkParent( parent ); } } static int checkStyle (int style) { int mask = (DWT.YES | DWT.NO | DWT.OK | DWT.CANCEL | DWT.ABORT | DWT.RETRY | DWT.IGNORE); int bits = style & mask; if (bits is DWT.OK || bits is DWT.CANCEL || bits is (DWT.OK | DWT.CANCEL)) return style; 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; if (bits is (DWT.RETRY | DWT.CANCEL) || bits is (DWT.ABORT | DWT.RETRY | DWT.IGNORE)) return style; style = (style & ~mask) | DWT.OK; return style; } /** * Returns the dialog's message, or an empty string if it does not have one. * The message is a description of the purpose for which the dialog was opened. * This message will be visible in the dialog while it is open. * * @return the message */ public String getMessage () { return message; } /** * Makes the dialog visible and brings it to the front * of the display. * * @return the ID of the button that was selected to dismiss the * message box (e.g. DWT.OK, DWT.CANCEL, etc.) * * @exception DWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li> * </ul> */ public int open () { NSAlertStyle alertType = OS.NSInformationalAlertStyle; if ((style & DWT.ICON_ERROR) !is 0) alertType = OS.NSCriticalAlertStyle; if ((style & DWT.ICON_INFORMATION) !is 0) alertType = OS.NSInformationalAlertStyle; if ((style & DWT.ICON_QUESTION) !is 0) alertType = OS.NSInformationalAlertStyle; if ((style & DWT.ICON_WARNING) !is 0) alertType = OS.NSWarningAlertStyle; if ((style & DWT.ICON_WORKING) !is 0) alertType = OS.NSInformationalAlertStyle; NSString defaultButton = null, alternateButton = null, otherButton = null; int mask = (DWT.YES | DWT.NO | DWT.OK | DWT.CANCEL | DWT.ABORT | DWT.RETRY | DWT.IGNORE); int bits = style & mask; switch (bits) { case DWT.OK: break; case DWT.CANCEL: defaultButton = NSString.stringWith(DWT.getMessage("DWT_Cancel")); break; case DWT.OK | DWT.CANCEL: alternateButton = NSString.stringWith(DWT.getMessage("DWT_Cancel")); break; case DWT.YES: defaultButton = NSString.stringWith(DWT.getMessage("DWT_Yes")); break; case DWT.NO: defaultButton = NSString.stringWith(DWT.getMessage("DWT_No")); break; case DWT.YES | DWT.NO: defaultButton = NSString.stringWith(DWT.getMessage("DWT_Yes")); alternateButton = NSString.stringWith(DWT.getMessage("DWT_No")); break; case DWT.YES | DWT.NO | DWT.CANCEL: defaultButton = NSString.stringWith(DWT.getMessage("DWT_Yes")); alternateButton = NSString.stringWith(DWT.getMessage("DWT_No")); otherButton = NSString.stringWith(DWT.getMessage("DWT_Cancel")); break; case DWT.RETRY | DWT.CANCEL: defaultButton = NSString.stringWith(DWT.getMessage("DWT_Retry")); alternateButton = NSString.stringWith(DWT.getMessage("DWT_Cancel")); break; case DWT.ABORT | DWT.RETRY | DWT.IGNORE: defaultButton = NSString.stringWith(DWT.getMessage("DWT_Abort")); alternateButton = NSString.stringWith(DWT.getMessage("DWT_Retry")); otherButton = NSString.stringWith(DWT.getMessage("DWT_Ignore")); break; default: } NSString title = NSString.stringWith(this.title !is null ? this.title : ""); NSString message = NSString.stringWith(this.message !is null ? this.message : ""); NSAlert alert = NSAlert.alertWithMessageText(NSString.stringWith(""), defaultButton, alternateButton, otherButton, message); (new NSWindow(alert.window().id)).setTitle(title); alert.setAlertStyle(alertType); NSInteger response = alert.runModal(); switch (bits) { case DWT.OK: switch (response) { case OS.NSAlertDefaultReturn: return DWT.OK; default: } break; case DWT.CANCEL: switch (response) { case OS.NSAlertDefaultReturn: return DWT.CANCEL; default: } break; case DWT.OK | DWT.CANCEL: switch (response) { case OS.NSAlertDefaultReturn: return DWT.OK; case OS.NSAlertAlternateReturn: return DWT.CANCEL; default: } break; case DWT.YES: switch (response) { case OS.NSAlertDefaultReturn: return DWT.YES; default: } break; case DWT.NO: switch (response) { case OS.NSAlertDefaultReturn: return DWT.NO; default: } break; case DWT.YES | DWT.NO: switch (response) { case OS.NSAlertDefaultReturn: return DWT.YES; case OS.NSAlertAlternateReturn: return DWT.NO; default: } break; case DWT.YES | DWT.NO | DWT.CANCEL: switch (response) { case OS.NSAlertDefaultReturn: return DWT.YES; case OS.NSAlertAlternateReturn: return DWT.NO; case OS.NSAlertOtherReturn: return DWT.CANCEL; default: } break; case DWT.RETRY | DWT.CANCEL: switch (response) { case OS.NSAlertDefaultReturn: return DWT.RETRY; case OS.NSAlertAlternateReturn: return DWT.CANCEL; default: } break; case DWT.ABORT | DWT.RETRY | DWT.IGNORE: switch (response) { case OS.NSAlertDefaultReturn: return DWT.ABORT; case OS.NSAlertAlternateReturn: return DWT.RETRY; case OS.NSAlertOtherReturn: return DWT.IGNORE; default: } break; default: } return DWT.CANCEL; } /** * Sets the dialog's message, which is a description of * the purpose for which it was opened. This message will be * visible on the dialog while it is open. * * @param string the message * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the string is null</li> * </ul> */ public void setMessage (String string) { //if (string is null) error (DWT.ERROR_NULL_ARGUMENT); message = string; } /++ + DWT extension +/ public static int showMessageBox(String str, String title, Shell shell, int style) { MessageBox msgBox = (shell is null ) ? new MessageBox( style ) : new MessageBox(shell, style); msgBox.setMessage(str); if(title !is null){ msgBox.setText(title); } return msgBox.open(); } /// DWT extension public static int showInfo(String str, String title = null, Shell shell = null) { return showMessageBox( str, title, shell, DWT.OK | DWT.ICON_INFORMATION ); } /// DWT extension alias showInfo showInformation; /// DWT extension public static int showWarning(String str, String title = null, Shell shell = null) { return showMessageBox( str, title, shell, DWT.OK | DWT.ICON_WARNING ); } /// DWT extension public static int showError(String str, String title = null, Shell shell = null) { return showMessageBox( str, title, shell, DWT.OK | DWT.ICON_ERROR ); } }