changeset 83:dd064bf5311e

MessageBox
author Frank Benoit <benoit@tionex.de>
date Tue, 15 Jan 2008 19:42:22 +0100
parents 4cdaacfb8649
children c098425dc1a3
files dwt/widgets/MessageBox.d
diffstat 1 files changed, 198 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwt/widgets/MessageBox.d	Tue Jan 15 19:42:22 2008 +0100
@@ -0,0 +1,198 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2007 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
+ *******************************************************************************/
+module dwt.widgets.MessageBox;
+
+
+
+import dwt.DWT;
+import dwt.DWTException;
+import dwt.internal.gtk.OS;
+import dwt.widgets.Dialog;
+import dwt.widgets.Shell;
+import dwt.widgets.Display;
+
+import tango.stdc.stringz;
+
+/**
+ * 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>
+ */
+public class MessageBox : Dialog {
+
+    char[] message = "";
+    GtkWidget* handle;
+/**
+ * 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(style));
+    checkSubclass ();
+}
+
+/**
+ * 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 char[] 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
+ *
+ * @exception IllegalArgumentException <ul>
+ *    <li>ERROR_NULL_ARGUMENT - if the string is null</li>
+ * </ul>
+ */
+public void setMessage (char[] 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 ();
+    int result = OS.gtk_dialog_run (handle);
+    display.removeIdleProc ();
+    OS.gtk_widget_destroy (handle);
+    return result;
+}
+
+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 (char[] string) {
+    int i = 0, j = 0;
+    char [] result = new char[]( string.length * 2 );
+    while (i < string.length) {
+        switch (string [i]) {
+            case '%':
+                result [j++] = '%';
+                break;
+            default:
+        }
+        result [j++] = string [i++];
+    }
+    return result[ 0 .. j ];
+}
+}