Mercurial > projects > dwt-linux
annotate dwt/widgets/MessageBox.d @ 238:380bad9f6852
reverted char[] to String
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 05 May 2008 00:42:55 +0200 |
parents | e3472c527a14 |
children | ce446666f5a2 |
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 /******************************************************************************* |
83 | 2 * Copyright (c) 2000, 2007 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 | |
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 import tango.stdc.stringz; | |
27 | |
28 /** | |
29 * Instances of this class are used to inform or warn the user. | |
30 * <dl> | |
31 * <dt><b>Styles:</b></dt> | |
32 * <dd>ICON_ERROR, ICON_INFORMATION, ICON_QUESTION, ICON_WARNING, ICON_WORKING</dd> | |
33 * <dd>OK, OK | CANCEL</dd> | |
34 * <dd>YES | NO, YES | NO | CANCEL</dd> | |
35 * <dd>RETRY | CANCEL</dd> | |
36 * <dd>ABORT | RETRY | IGNORE</dd> | |
37 * <dt><b>Events:</b></dt> | |
38 * <dd>(none)</dd> | |
39 * </dl> | |
40 * <p> | |
41 * Note: Only one of the styles ICON_ERROR, ICON_INFORMATION, ICON_QUESTION, | |
42 * ICON_WARNING and ICON_WORKING may be specified. | |
43 * </p><p> | |
44 * IMPORTANT: This class is intended to be subclassed <em>only</em> | |
45 * within the DWT implementation. | |
46 * </p> | |
47 */ | |
48 public class MessageBox : Dialog { | |
49 | |
238 | 50 String message = ""; |
83 | 51 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
|
52 private bool allowNullParent = false; |
83 | 53 /** |
54 * Constructs a new instance of this class given only its parent. | |
55 * | |
56 * @param parent a shell which will be the parent of the new instance | |
57 * | |
58 * @exception IllegalArgumentException <ul> | |
59 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
60 * </ul> | |
61 * @exception DWTException <ul> | |
62 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
63 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
64 * </ul> | |
65 */ | |
66 public this (Shell parent) { | |
67 this (parent, DWT.OK | DWT.ICON_INFORMATION | DWT.APPLICATION_MODAL); | |
68 } | |
69 | |
70 /** | |
71 * Constructs a new instance of this class given its parent | |
72 * and a style value describing its behavior and appearance. | |
73 * <p> | |
74 * The style value is either one of the style constants defined in | |
75 * class <code>DWT</code> which is applicable to instances of this | |
76 * class, or must be built by <em>bitwise OR</em>'ing together | |
77 * (that is, using the <code>int</code> "|" operator) two or more | |
78 * of those <code>DWT</code> style constants. The class description | |
79 * lists the style constants that are applicable to the class. | |
80 * Style bits are also inherited from superclasses. | |
81 * | |
82 * @param parent a shell which will be the parent of the new instance | |
83 * @param style the style of dialog to construct | |
84 * | |
85 * @exception IllegalArgumentException <ul> | |
86 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
87 * </ul> | |
88 * @exception DWTException <ul> | |
89 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
90 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
91 * </ul> | |
92 */ | |
93 public this (Shell parent, int style) { | |
94 super(parent, checkStyle(style)); | |
95 checkSubclass (); | |
96 } | |
97 | |
208
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
98 /++ |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
99 + 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
|
100 +/ |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
101 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
|
102 allowNullParent = true; |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
103 super (parent, checkStyle (style)); |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
104 checkSubclass (); |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
105 } |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
106 // PORT |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
107 // 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
|
108 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
|
109 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
|
110 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
|
111 } |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
112 } |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
113 |
83 | 114 /** |
115 * Returns the dialog's message, or an empty string if it does not have one. | |
116 * The message is a description of the purpose for which the dialog was opened. | |
117 * This message will be visible in the dialog while it is open. | |
118 * | |
119 * @return the message | |
120 */ | |
238 | 121 public String getMessage () { |
83 | 122 return message; |
123 } | |
124 | |
125 /** | |
126 * Sets the dialog's message, which is a description of | |
127 * the purpose for which it was opened. This message will be | |
128 * visible on the dialog while it is open. | |
129 * | |
130 * @param string the message | |
131 * | |
132 * @exception IllegalArgumentException <ul> | |
133 * <li>ERROR_NULL_ARGUMENT - if the string is null</li> | |
134 * </ul> | |
135 */ | |
238 | 136 public void setMessage (String string) { |
83 | 137 if (string is null) error (DWT.ERROR_NULL_ARGUMENT); |
138 message = string; | |
139 } | |
140 | |
141 /** | |
142 * Makes the dialog visible and brings it to the front | |
143 * of the display. | |
144 * | |
145 * @return the ID of the button that was selected to dismiss the | |
146 * message box (e.g. DWT.OK, DWT.CANCEL, etc.) | |
147 * | |
148 * @exception DWTException <ul> | |
149 * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li> | |
150 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li> | |
151 * </ul> | |
152 */ | |
153 public int open () { | |
154 GtkWidget* parentHandle = (parent !is null) ? parent.topHandle() : null; | |
155 int dialogFlags = OS.GTK_DIALOG_DESTROY_WITH_PARENT; | |
156 if ((style & (DWT.PRIMARY_MODAL | DWT.APPLICATION_MODAL | DWT.SYSTEM_MODAL)) !is 0) { | |
157 dialogFlags |= OS.GTK_DIALOG_MODAL; | |
158 } | |
159 int messageType = OS.GTK_MESSAGE_INFO; | |
160 if ((style & (DWT.ICON_WARNING)) !is 0) messageType = OS.GTK_MESSAGE_WARNING; | |
161 if ((style & (DWT.ICON_QUESTION)) !is 0) messageType = OS.GTK_MESSAGE_QUESTION; | |
162 if ((style & (DWT.ICON_ERROR)) !is 0) messageType = OS.GTK_MESSAGE_ERROR; | |
163 | |
164 char* buffer = toStringz( fixPercent (message)); | |
165 handle = cast(GtkWidget*)OS.gtk_message_dialog_new(parentHandle, dialogFlags, messageType, 0, buffer); | |
166 if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES); | |
167 if (parentHandle !is null) { | |
168 auto pixbufs = OS.gtk_window_get_icon_list (parentHandle); | |
169 if (pixbufs !is null) { | |
170 OS.gtk_window_set_icon_list (handle, pixbufs); | |
171 OS.g_list_free (pixbufs); | |
172 } | |
173 } | |
174 createButtons(); | |
175 buffer = toStringz(title); | |
176 OS.gtk_window_set_title(handle,buffer); | |
177 Display display = parent !is null ? parent.getDisplay (): Display.getCurrent (); | |
178 display.addIdleProc (); | |
179 int result = OS.gtk_dialog_run (handle); | |
180 display.removeIdleProc (); | |
181 OS.gtk_widget_destroy (handle); | |
182 return result; | |
183 } | |
184 | |
185 private void createButtons() { | |
186 if ((style & DWT.OK) !is 0) OS.gtk_dialog_add_button(handle, "gtk-ok".ptr, DWT.OK); | |
187 if ((style & DWT.CANCEL) !is 0) OS.gtk_dialog_add_button(handle, "gtk-cancel".ptr, DWT.CANCEL); | |
188 if ((style & DWT.YES) !is 0) OS.gtk_dialog_add_button(handle, "gtk-yes".ptr, DWT.YES); | |
189 if ((style & DWT.NO) !is 0) OS.gtk_dialog_add_button(handle, "gtk-no".ptr, DWT.NO); | |
190 if ((style & DWT.ABORT) !is 0) OS.gtk_dialog_add_button(handle, toStringz( DWT.getMessage("SWT_Abort")), DWT.ABORT); | |
191 if ((style & DWT.RETRY) !is 0) OS.gtk_dialog_add_button(handle, toStringz( DWT.getMessage("SWT_Retry")), DWT.RETRY); | |
192 if ((style & DWT.IGNORE) !is 0) OS.gtk_dialog_add_button(handle, toStringz( DWT.getMessage("SWT_Ignore")), DWT.IGNORE); | |
193 } | |
194 | |
195 private static int checkStyle (int style) { | |
196 int mask = (DWT.YES | DWT.NO | DWT.OK | DWT.CANCEL | DWT.ABORT | DWT.RETRY | DWT.IGNORE); | |
197 int bits = style & mask; | |
198 if (bits is DWT.OK || bits is DWT.CANCEL || bits is (DWT.OK | DWT.CANCEL)) return style; | |
199 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; | |
200 if (bits is (DWT.RETRY | DWT.CANCEL) || bits is (DWT.ABORT | DWT.RETRY | DWT.IGNORE)) return style; | |
201 style = (style & ~mask) | DWT.OK; | |
202 return style; | |
203 } | |
204 | |
238 | 205 char[] fixPercent (String string) { |
83 | 206 int i = 0, j = 0; |
238 | 207 char [] result = new String( string.length * 2 ); |
83 | 208 while (i < string.length) { |
209 switch (string [i]) { | |
210 case '%': | |
211 result [j++] = '%'; | |
212 break; | |
213 default: | |
214 } | |
215 result [j++] = string [i++]; | |
216 } | |
217 return result[ 0 .. j ]; | |
218 } | |
208
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
219 |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
220 |
210 | 221 /++ |
222 + DWT extension | |
223 +/ | |
238 | 224 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
|
225 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
|
226 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
|
227 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
|
228 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
|
229 } |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
230 return msgBox.open(); |
83 | 231 } |
208
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
232 |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
233 /// DWT extension |
238 | 234 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
|
235 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
|
236 } |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
237 /// 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
|
238 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
|
239 |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
240 /// DWT extension |
238 | 241 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
|
242 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
|
243 } |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
244 /// DWT extension |
238 | 245 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
|
246 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
|
247 } |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
248 |
fff9f748b33b
Added convinience methods in MessageBox now they are same as in dwt-win
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
249 } |