Mercurial > projects > dwt-linux
annotate dwt/widgets/MessageBox.d @ 259:c0d810de7093
Update SWT 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 29 Jun 2008 14:33:38 +0200 |
parents | 5a30aa9820f3 |
children |
rev | line source |
---|---|
208
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
1 /******************************************************************************* |
259 | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
83 | 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 | |
108 | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | |
83 | 12 *******************************************************************************/ |
13 module dwt.widgets.MessageBox; | |
14 | |
238 | 15 import dwt.dwthelper.utils; |
16 | |
83 | 17 |
18 | |
19 import dwt.DWT; | |
20 import dwt.DWTException; | |
21 import dwt.internal.gtk.OS; | |
22 import dwt.widgets.Dialog; | |
23 import dwt.widgets.Shell; | |
24 import dwt.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 DWT implementation. | |
45 * </p> | |
259 | 46 * |
47 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample, Dialog tab</a> | |
48 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
83 | 49 */ |
50 public class MessageBox : Dialog { | |
51 | |
240 | 52 alias Dialog.checkStyle checkStyle; |
53 | |
238 | 54 String message = ""; |
83 | 55 GtkWidget* handle; |
208
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
56 private bool allowNullParent = false; |
83 | 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 DWTException <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, DWT.OK | DWT.ICON_INFORMATION | DWT.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>DWT</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>DWT</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 DWTException <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) { | |
240 | 98 super (parent, checkStyle (parent, checkStyle (style))); |
83 | 99 checkSubclass (); |
100 } | |
101 | |
208
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
102 /++ |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
103 + DWT extension, a MessageBox with no parent |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
104 +/ |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
105 public this (int style) { |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
106 allowNullParent = true; |
240 | 107 super (parent, checkStyle (parent, checkStyle (style))); |
208
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
108 checkSubclass (); |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
109 } |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
110 // PORT |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
111 // actually, the parent can be null |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
112 override void checkParent (Shell parent){ |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
113 if( !allowNullParent ){ |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
114 super.checkParent( parent ); |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
115 } |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
116 } |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
117 |
83 | 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 */ | |
238 | 125 public String getMessage () { |
83 | 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 */ | |
238 | 137 public void setMessage (String string) { |
255
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
240
diff
changeset
|
138 // DWT extension: allow null for zero length string |
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
240
diff
changeset
|
139 //if (string is null) error (DWT.ERROR_NULL_ARGUMENT); |
83 | 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. DWT.OK, DWT.CANCEL, etc.) | |
149 * | |
150 * @exception DWTException <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 & (DWT.PRIMARY_MODAL | DWT.APPLICATION_MODAL | DWT.SYSTEM_MODAL)) !is 0) { | |
159 dialogFlags |= OS.GTK_DIALOG_MODAL; | |
160 } | |
161 int messageType = OS.GTK_MESSAGE_INFO; | |
162 if ((style & (DWT.ICON_WARNING)) !is 0) messageType = OS.GTK_MESSAGE_WARNING; | |
163 if ((style & (DWT.ICON_QUESTION)) !is 0) messageType = OS.GTK_MESSAGE_QUESTION; | |
164 if ((style & (DWT.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) DWT.error(DWT.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 (); | |
240 | 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 int /*long*/ hookId = 0; | |
188 CallbackData emissionData; | |
189 emissionData.display = display; | |
190 emissionData.data = handle; | |
191 if ((style & DWT.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 & DWT.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 } | |
83 | 202 display.removeIdleProc (); |
203 OS.gtk_widget_destroy (handle); | |
240 | 204 return response; |
83 | 205 } |
206 | |
207 private void createButtons() { | |
208 if ((style & DWT.OK) !is 0) OS.gtk_dialog_add_button(handle, "gtk-ok".ptr, DWT.OK); | |
209 if ((style & DWT.CANCEL) !is 0) OS.gtk_dialog_add_button(handle, "gtk-cancel".ptr, DWT.CANCEL); | |
210 if ((style & DWT.YES) !is 0) OS.gtk_dialog_add_button(handle, "gtk-yes".ptr, DWT.YES); | |
211 if ((style & DWT.NO) !is 0) OS.gtk_dialog_add_button(handle, "gtk-no".ptr, DWT.NO); | |
212 if ((style & DWT.ABORT) !is 0) OS.gtk_dialog_add_button(handle, toStringz( DWT.getMessage("SWT_Abort")), DWT.ABORT); | |
213 if ((style & DWT.RETRY) !is 0) OS.gtk_dialog_add_button(handle, toStringz( DWT.getMessage("SWT_Retry")), DWT.RETRY); | |
214 if ((style & DWT.IGNORE) !is 0) OS.gtk_dialog_add_button(handle, toStringz( DWT.getMessage("SWT_Ignore")), DWT.IGNORE); | |
215 } | |
216 | |
217 private static int checkStyle (int style) { | |
218 int mask = (DWT.YES | DWT.NO | DWT.OK | DWT.CANCEL | DWT.ABORT | DWT.RETRY | DWT.IGNORE); | |
219 int bits = style & mask; | |
220 if (bits is DWT.OK || bits is DWT.CANCEL || bits is (DWT.OK | DWT.CANCEL)) return style; | |
221 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; | |
222 if (bits is (DWT.RETRY | DWT.CANCEL) || bits is (DWT.ABORT | DWT.RETRY | DWT.IGNORE)) return style; | |
223 style = (style & ~mask) | DWT.OK; | |
224 return style; | |
225 } | |
226 | |
238 | 227 char[] fixPercent (String string) { |
83 | 228 int i = 0, j = 0; |
238 | 229 char [] result = new String( string.length * 2 ); |
83 | 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 } | |
208
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
241 |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
242 |
210 | 243 /++ |
244 + DWT extension | |
245 +/ | |
238 | 246 public static int showMessageBox(String str, String title, Shell shell, int style) { |
208
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
247 MessageBox msgBox = (shell is null ) ? new MessageBox( style ) : new MessageBox(shell, style); |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
248 msgBox.setMessage(str); |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
249 if(title !is null){ |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
250 msgBox.setText(title); |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
251 } |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
252 return msgBox.open(); |
83 | 253 } |
208
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
254 |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
255 /// DWT extension |
238 | 256 public static int showInfo(String str, String title = null, Shell shell = null) { |
208
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
257 return showMessageBox( str, title, shell, DWT.OK | DWT.ICON_INFORMATION ); |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
258 } |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
259 /// DWT extension |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
260 alias showInfo showInformation; |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
261 |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
262 /// DWT extension |
238 | 263 public static int showWarning(String str, String title = null, Shell shell = null) { |
208
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
264 return showMessageBox( str, title, shell, DWT.OK | DWT.ICON_WARNING ); |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
265 } |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
266 /// DWT extension |
238 | 267 public static int showError(String str, String title = null, Shell shell = null) { |
208
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
268 return showMessageBox( str, title, shell, DWT.OK | DWT.ICON_ERROR ); |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
269 } |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
270 |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
271 } |