Mercurial > projects > dwt-linux
view dwt/widgets/MessageBox.d @ 315:21959e16bc1e
Improved Listeners access functions.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 16 Sep 2008 15:28:21 +0200 |
parents | c0d810de7093 |
children |
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: * Frank Benoit <benoit@tionex.de> *******************************************************************************/ module dwt.widgets.MessageBox; import dwt.dwthelper.utils; import dwt.DWT; import dwt.DWTException; import dwt.internal.gtk.OS; import dwt.widgets.Dialog; import dwt.widgets.Shell; import dwt.widgets.Display; /** * 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 { alias Dialog.checkStyle checkStyle; String message = ""; GtkWidget* handle; 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, checkStyle (parent, checkStyle (style))); checkSubclass (); } /++ + DWT extension, a MessageBox with no parent +/ public this (int style) { allowNullParent = true; super (parent, checkStyle (parent, checkStyle (style))); checkSubclass (); } // PORT // actually, the parent can be null override void checkParent (Shell parent){ if( !allowNullParent ){ super.checkParent( parent ); } } /** * 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; } /** * 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 * */ public void setMessage (String string) { // DWT extension: allow null for zero length string //if (string is null) error (DWT.ERROR_NULL_ARGUMENT); message = string; } /** * 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 () { GtkWidget* parentHandle = (parent !is null) ? parent.topHandle() : null; int dialogFlags = OS.GTK_DIALOG_DESTROY_WITH_PARENT; if ((style & (DWT.PRIMARY_MODAL | DWT.APPLICATION_MODAL | DWT.SYSTEM_MODAL)) !is 0) { dialogFlags |= OS.GTK_DIALOG_MODAL; } int messageType = OS.GTK_MESSAGE_INFO; if ((style & (DWT.ICON_WARNING)) !is 0) messageType = OS.GTK_MESSAGE_WARNING; if ((style & (DWT.ICON_QUESTION)) !is 0) messageType = OS.GTK_MESSAGE_QUESTION; if ((style & (DWT.ICON_ERROR)) !is 0) messageType = OS.GTK_MESSAGE_ERROR; char* buffer = toStringz( fixPercent (message)); handle = cast(GtkWidget*)OS.gtk_message_dialog_new(parentHandle, dialogFlags, messageType, 0, buffer); if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES); if (parentHandle !is null) { auto pixbufs = OS.gtk_window_get_icon_list (parentHandle); if (pixbufs !is null) { OS.gtk_window_set_icon_list (handle, pixbufs); OS.g_list_free (pixbufs); } } createButtons(); buffer = toStringz(title); OS.gtk_window_set_title(handle,buffer); Display display = parent !is null ? parent.getDisplay (): Display.getCurrent (); display.addIdleProc (); Dialog oldModal = null; if (OS.gtk_window_get_modal (handle)) { oldModal = display.getModalDialog (); display.setModalDialog (this); } int signalId = 0; int /*long*/ hookId = 0; CallbackData emissionData; emissionData.display = display; emissionData.data = handle; if ((style & DWT.RIGHT_TO_LEFT) !is 0) { signalId = OS.g_signal_lookup (OS.map.ptr, OS.GTK_TYPE_WIDGET()); hookId = OS.g_signal_add_emission_hook (signalId, 0, &Display.emissionFunc, &emissionData, null); } int response = OS.gtk_dialog_run (handle); if ((style & DWT.RIGHT_TO_LEFT) !is 0) { OS.g_signal_remove_emission_hook (signalId, hookId); } if (OS.gtk_window_get_modal (handle)) { display.setModalDialog (oldModal); } display.removeIdleProc (); OS.gtk_widget_destroy (handle); return response; } private void createButtons() { if ((style & DWT.OK) !is 0) OS.gtk_dialog_add_button(handle, "gtk-ok".ptr, DWT.OK); if ((style & DWT.CANCEL) !is 0) OS.gtk_dialog_add_button(handle, "gtk-cancel".ptr, DWT.CANCEL); if ((style & DWT.YES) !is 0) OS.gtk_dialog_add_button(handle, "gtk-yes".ptr, DWT.YES); if ((style & DWT.NO) !is 0) OS.gtk_dialog_add_button(handle, "gtk-no".ptr, DWT.NO); if ((style & DWT.ABORT) !is 0) OS.gtk_dialog_add_button(handle, toStringz( DWT.getMessage("SWT_Abort")), DWT.ABORT); if ((style & DWT.RETRY) !is 0) OS.gtk_dialog_add_button(handle, toStringz( DWT.getMessage("SWT_Retry")), DWT.RETRY); if ((style & DWT.IGNORE) !is 0) OS.gtk_dialog_add_button(handle, toStringz( DWT.getMessage("SWT_Ignore")), DWT.IGNORE); } private 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; } char[] fixPercent (String string) { int i = 0, j = 0; char [] result = new String( string.length * 2 ); while (i < string.length) { switch (string [i]) { case '%': result [j++] = '%'; break; default: } result [j++] = string [i++]; } return result[ 0 .. j ]; } /++ + 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 ); } }