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 org.eclipse.swt.widgets.MessageBox; 14 15 import java.lang.all; 16 17 18 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.SWTException; 21 import org.eclipse.swt.internal.gtk.OS; 22 import org.eclipse.swt.widgets.Dialog; 23 import org.eclipse.swt.widgets.Shell; 24 import org.eclipse.swt.widgets.Display; 25 26 27 /** 28 * Instances of this class are used to inform or warn the user. 29 * <dl> 30 * <dt><b>Styles:</b></dt> 31 * <dd>ICON_ERROR, ICON_INFORMATION, ICON_QUESTION, ICON_WARNING, ICON_WORKING</dd> 32 * <dd>OK, OK | CANCEL</dd> 33 * <dd>YES | NO, YES | NO | CANCEL</dd> 34 * <dd>RETRY | CANCEL</dd> 35 * <dd>ABORT | RETRY | IGNORE</dd> 36 * <dt><b>Events:</b></dt> 37 * <dd>(none)</dd> 38 * </dl> 39 * <p> 40 * Note: Only one of the styles ICON_ERROR, ICON_INFORMATION, ICON_QUESTION, 41 * ICON_WARNING and ICON_WORKING may be specified. 42 * </p><p> 43 * IMPORTANT: This class is intended to be subclassed <em>only</em> 44 * within the SWT implementation. 45 * </p> 46 * 47 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample, Dialog tab</a> 48 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> 49 */ 50 public class MessageBox : Dialog { 51 52 alias Dialog.checkStyle checkStyle; 53 54 String message = ""; 55 GtkWidget* handle; 56 private bool allowNullParent = false; 57 /** 58 * Constructs a new instance of this class given only its parent. 59 * 60 * @param parent a shell which will be the parent of the new instance 61 * 62 * @exception IllegalArgumentException <ul> 63 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> 64 * </ul> 65 * @exception SWTException <ul> 66 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> 67 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> 68 * </ul> 69 */ 70 public this (Shell parent) { 71 this (parent, SWT.OK | SWT.ICON_INFORMATION | SWT.APPLICATION_MODAL); 72 } 73 74 /** 75 * Constructs a new instance of this class given its parent 76 * and a style value describing its behavior and appearance. 77 * <p> 78 * The style value is either one of the style constants defined in 79 * class <code>SWT</code> which is applicable to instances of this 80 * class, or must be built by <em>bitwise OR</em>'ing together 81 * (that is, using the <code>int</code> "|" operator) two or more 82 * of those <code>SWT</code> style constants. The class description 83 * lists the style constants that are applicable to the class. 84 * Style bits are also inherited from superclasses. 85 * 86 * @param parent a shell which will be the parent of the new instance 87 * @param style the style of dialog to construct 88 * 89 * @exception IllegalArgumentException <ul> 90 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> 91 * </ul> 92 * @exception SWTException <ul> 93 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> 94 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> 95 * </ul> 96 */ 97 public this (Shell parent, int style) { 98 super (parent, checkStyle (parent, checkStyle (style))); 99 checkSubclass (); 100 } 101 102 /++ 103 + SWT extension, a MessageBox with no parent 104 +/ 105 public this (int style) { 106 allowNullParent = true; 107 super (parent, checkStyle (parent, checkStyle (style))); 108 checkSubclass (); 109 } 110 // PORT 111 // actually, the parent can be null 112 override void checkParent (Shell parent){ 113 if( !allowNullParent ){ 114 super.checkParent( parent ); 115 } 116 } 117 118 /** 119 * Returns the dialog's message, or an empty string if it does not have one. 120 * The message is a description of the purpose for which the dialog was opened. 121 * This message will be visible in the dialog while it is open. 122 * 123 * @return the message 124 */ 125 public String getMessage () { 126 return message; 127 } 128 129 /** 130 * Sets the dialog's message, which is a description of 131 * the purpose for which it was opened. This message will be 132 * visible on the dialog while it is open. 133 * 134 * @param string the message 135 * 136 */ 137 public void setMessage (String string) { 138 // SWT extension: allow null for zero length string 139 //if (string is null) error (SWT.ERROR_NULL_ARGUMENT); 140 message = string; 141 } 142 143 /** 144 * Makes the dialog visible and brings it to the front 145 * of the display. 146 * 147 * @return the ID of the button that was selected to dismiss the 148 * message box (e.g. SWT.OK, SWT.CANCEL, etc.) 149 * 150 * @exception SWTException <ul> 151 * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li> 152 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li> 153 * </ul> 154 */ 155 public int open () { 156 GtkWidget* parentHandle = (parent !is null) ? parent.topHandle() : null; 157 int dialogFlags = OS.GTK_DIALOG_DESTROY_WITH_PARENT; 158 if ((style & (SWT.PRIMARY_MODAL | SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) !is 0) { 159 dialogFlags |= OS.GTK_DIALOG_MODAL; 160 } 161 int messageType = OS.GTK_MESSAGE_INFO; 162 if ((style & (SWT.ICON_WARNING)) !is 0) messageType = OS.GTK_MESSAGE_WARNING; 163 if ((style & (SWT.ICON_QUESTION)) !is 0) messageType = OS.GTK_MESSAGE_QUESTION; 164 if ((style & (SWT.ICON_ERROR)) !is 0) messageType = OS.GTK_MESSAGE_ERROR; 165 166 char* buffer = toStringz( fixPercent (message)); 167 handle = cast(GtkWidget*)OS.gtk_message_dialog_new(parentHandle, dialogFlags, messageType, 0, buffer); 168 if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES); 169 if (parentHandle !is null) { 170 auto pixbufs = OS.gtk_window_get_icon_list (parentHandle); 171 if (pixbufs !is null) { 172 OS.gtk_window_set_icon_list (handle, pixbufs); 173 OS.g_list_free (pixbufs); 174 } 175 } 176 createButtons(); 177 buffer = toStringz(title); 178 OS.gtk_window_set_title(handle,buffer); 179 Display display = parent !is null ? parent.getDisplay (): Display.getCurrent (); 180 display.addIdleProc (); 181 Dialog oldModal = null; 182 if (OS.gtk_window_get_modal (handle)) { 183 oldModal = display.getModalDialog (); 184 display.setModalDialog (this); 185 } 186 int signalId = 0; 187 ptrdiff_t hookId = 0; 188 CallbackData emissionData; 189 emissionData.display = display; 190 emissionData.data = handle; 191 if ((style & SWT.RIGHT_TO_LEFT) !is 0) { 192 signalId = OS.g_signal_lookup (OS.map.ptr, OS.GTK_TYPE_WIDGET()); 193 hookId = OS.g_signal_add_emission_hook (signalId, 0, &Display.emissionFunc, &emissionData, null); 194 } 195 int response = OS.gtk_dialog_run (handle); 196 if ((style & SWT.RIGHT_TO_LEFT) !is 0) { 197 OS.g_signal_remove_emission_hook (signalId, hookId); 198 } 199 if (OS.gtk_window_get_modal (handle)) { 200 display.setModalDialog (oldModal); 201 } 202 display.removeIdleProc (); 203 OS.gtk_widget_destroy (handle); 204 return response; 205 } 206 207 private void createButtons() { 208 if ((style & SWT.OK) !is 0) OS.gtk_dialog_add_button(handle, "gtk-ok".ptr, SWT.OK); 209 if ((style & SWT.CANCEL) !is 0) OS.gtk_dialog_add_button(handle, "gtk-cancel".ptr, SWT.CANCEL); 210 if ((style & SWT.YES) !is 0) OS.gtk_dialog_add_button(handle, "gtk-yes".ptr, SWT.YES); 211 if ((style & SWT.NO) !is 0) OS.gtk_dialog_add_button(handle, "gtk-no".ptr, SWT.NO); 212 if ((style & SWT.ABORT) !is 0) OS.gtk_dialog_add_button(handle, toStringz( SWT.getMessage("SWT_Abort")), SWT.ABORT); 213 if ((style & SWT.RETRY) !is 0) OS.gtk_dialog_add_button(handle, toStringz( SWT.getMessage("SWT_Retry")), SWT.RETRY); 214 if ((style & SWT.IGNORE) !is 0) OS.gtk_dialog_add_button(handle, toStringz( SWT.getMessage("SWT_Ignore")), SWT.IGNORE); 215 } 216 217 private static int checkStyle (int style) { 218 int mask = (SWT.YES | SWT.NO | SWT.OK | SWT.CANCEL | SWT.ABORT | SWT.RETRY | SWT.IGNORE); 219 int bits = style & mask; 220 if (bits is SWT.OK || bits is SWT.CANCEL || bits is (SWT.OK | SWT.CANCEL)) return style; 221 if (bits is SWT.YES || bits is SWT.NO || bits is (SWT.YES | SWT.NO) || bits is (SWT.YES | SWT.NO | SWT.CANCEL)) return style; 222 if (bits is (SWT.RETRY | SWT.CANCEL) || bits is (SWT.ABORT | SWT.RETRY | SWT.IGNORE)) return style; 223 style = (style & ~mask) | SWT.OK; 224 return style; 225 } 226 227 char[] fixPercent (String string) { 228 int i = 0, j = 0; 229 char [] result = new char[ string.length * 2 ]; 230 while (i < string.length) { 231 switch (string [i]) { 232 case '%': 233 result [j++] = '%'; 234 break; 235 default: 236 } 237 result [j++] = string [i++]; 238 } 239 return result[ 0 .. j ]; 240 } 241 242 243 /++ 244 + SWT extension 245 +/ 246 public static int showMessageBox(String str, String title, Shell shell, int style) { 247 MessageBox msgBox = (shell is null ) ? new MessageBox( style ) : new MessageBox(shell, style); 248 msgBox.setMessage(str); 249 if(title !is null){ 250 msgBox.setText(title); 251 } 252 return msgBox.open(); 253 } 254 255 /// SWT extension 256 public static int showInfo(String str, String title = null, Shell shell = null) { 257 return showMessageBox( str, title, shell, SWT.OK | SWT.ICON_INFORMATION ); 258 } 259 /// SWT extension 260 alias showInfo showInformation; 261 262 /// SWT extension 263 public static int showWarning(String str, String title = null, Shell shell = null) { 264 return showMessageBox( str, title, shell, SWT.OK | SWT.ICON_WARNING ); 265 } 266 /// SWT extension 267 public static int showError(String str, String title = null, Shell shell = null) { 268 return showMessageBox( str, title, shell, SWT.OK | SWT.ICON_ERROR ); 269 } 270 271 }