root/dwt/widgets/Dialog.d

Revision 246:fd9c62a2998e, 8.6 kB (checked in by Frank Benoit <benoit@tionex.de>, 5 months ago)

Updater SWT 3.4M7 to 3.4

Line 
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 dwt.widgets.Dialog;
14
15
16 import dwt.DWT;
17 import dwt.DWTException;
18 import dwt.widgets.Shell;
19 import dwt.widgets.Display;
20 import dwt.widgets.Widget;
21 import dwt.dwthelper.utils;
22
23 /**
24  * This class is the abstract superclass of the classes
25  * that represent the built in platform dialogs.
26  * A <code>Dialog</code> typically contains other widgets
27  * that are not accessible. A <code>Dialog</code> is not
28  * a <code>Widget</code>.
29  * <p>
30  * This class can also be used as the abstract superclass
31  * for user-designed dialogs. Such dialogs usually consist
32  * of a Shell with child widgets. The basic template for a
33  * user-defined dialog typically looks something like this:
34  * <pre><code>
35  * public class MyDialog extends Dialog {
36  *  Object result;
37  *
38  *  public MyDialog (Shell parent, int style) {
39  *      super (parent, style);
40  *  }
41  *  public MyDialog (Shell parent) {
42  *      this (parent, 0); // your default style bits go here (not the Shell's style bits)
43  *  }
44  *  public Object open () {
45  *      Shell parent = getParent();
46  *      Shell shell = new Shell(parent, DWT.DIALOG_TRIM | DWT.APPLICATION_MODAL);
47  *      shell.setText(getText());
48  *      // Your code goes here (widget creation, set result, etc).
49  *      shell.open();
50  *      Display display = parent.getDisplay();
51  *      while (!shell.isDisposed()) {
52  *          if (!display.readAndDispatch()) display.sleep();
53  *      }
54  *      return result;
55  *  }
56  * }
57  * </pre></code>
58  * <p>
59  * Note: The <em>modality</em> styles supported by this class
60  * are treated as <em>HINT</em>s, because not all are supported
61  * by every subclass on every platform. If a modality style is
62  * not supported, it is "upgraded" to a more restrictive modality
63  * style that is supported.  For example, if <code>PRIMARY_MODAL</code>
64  * is not supported by a particular dialog, it would be upgraded to
65  * <code>APPLICATION_MODAL</code>. In addition, as is the case
66  * for shells, the window manager for the desktop on which the
67  * instance is visible has ultimate control over the appearance
68  * and behavior of the instance, including its modality.
69  * <dl>
70  * <dt><b>Styles:</b></dt>
71  * <dd>APPLICATION_MODAL, PRIMARY_MODAL, SYSTEM_MODAL</dd>
72  * <dt><b>Events:</b></dt>
73  * <dd>(none)</dd>
74  * </dl>
75  * <p>
76  * Note: Only one of the styles APPLICATION_MODAL, PRIMARY_MODAL,
77  * and SYSTEM_MODAL may be specified.
78  * </p>
79  *
80  * @see Shell
81  * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample</a>
82  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
83  */
84
85 public abstract class Dialog {
86     int style;
87     Shell parent;
88     String title;
89
90 /**
91  * Constructs a new instance of this class given only its
92  * parent.
93  *
94  * @param parent a shell which will be the parent of the new instance
95  *
96  * @exception IllegalArgumentException <ul>
97  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
98  * </ul>
99  * @exception DWTException <ul>
100  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
101  * </ul>
102  */
103 public this (Shell parent) {
104     this (parent, DWT.PRIMARY_MODAL);
105 }
106
107 /**
108  * Constructs a new instance of this class given its parent
109  * and a style value describing its behavior and appearance.
110  * <p>
111  * The style value is either one of the style constants defined in
112  * class <code>DWT</code> which is applicable to instances of this
113  * class, or must be built by <em>bitwise OR</em>'ing together
114  * (that is, using the <code>int</code> "|" operator) two or more
115  * of those <code>DWT</code> style constants. The class description
116  * lists the style constants that are applicable to the class.
117  * Style bits are also inherited from superclasses.
118  *
119  * @param parent a shell which will be the parent of the new instance
120  * @param style the style of dialog to construct
121  *
122  * @exception IllegalArgumentException <ul>
123  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
124  * </ul>
125  * @exception DWTException <ul>
126  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
127  * </ul>
128  *
129  * @see DWT#PRIMARY_MODAL
130  * @see DWT#APPLICATION_MODAL
131  * @see DWT#SYSTEM_MODAL
132  */
133 public this (Shell parent, int style) {
134     checkParent (parent);
135     this.parent = parent;
136     this.style = style;
137     title = "";
138 }
139
140 /**
141  * Checks that this class can be subclassed.
142  * <p>
143  * IMPORTANT: See the comment in <code>Widget.checkSubclass()</code>.
144  * </p>
145  *
146  * @exception DWTException <ul>
147  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
148  * </ul>
149  *
150  * @see Widget#checkSubclass
151  */
152 protected void checkSubclass () {
153     //PORTING_TODO: implement Display.isValidClass and Class?
154     /+if (!Display.isValidClass (getClass ())) {
155         error (DWT.ERROR_INVALID_SUBCLASS);
156     }+/
157 }
158
159 /**
160  * Throws an exception if the specified widget can not be
161  * used as a parent for the receiver.
162  *
163  * @exception IllegalArgumentException <ul>
164  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
165  *    <li>ERROR_INVALID_ARGUMENT - if the parent is disposed</li>
166  * </ul>
167  * @exception DWTException <ul>
168  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
169  * </ul>
170  */
171 void checkParent (Shell parent) {
172     if (parent is null) error (DWT.ERROR_NULL_ARGUMENT);
173     parent.checkWidget ();
174 }
175
176 static int checkStyle (Shell parent, int style) {
177     if ((style & (DWT.PRIMARY_MODAL | DWT.APPLICATION_MODAL | DWT.SYSTEM_MODAL)) is 0) {
178         style |= DWT.APPLICATION_MODAL;
179     }
180     style &= ~DWT.MIRRORED;
181     if ((style & (DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT)) is 0) {
182         if (parent !is null) {
183             if ((parent.style & DWT.LEFT_TO_RIGHT) !is 0) style |= DWT.LEFT_TO_RIGHT;
184             if ((parent.style & DWT.RIGHT_TO_LEFT) !is 0) style |= DWT.RIGHT_TO_LEFT;
185         }
186     }
187     return Widget.checkBits (style, DWT.LEFT_TO_RIGHT, DWT.RIGHT_TO_LEFT, 0, 0, 0, 0);
188 }
189
190 /**
191  * Does whatever dialog specific cleanup is required, and then
192  * uses the code in <code>DWTError.error</code> to handle the error.
193  *
194  * @param code the descriptive error code
195  *
196  * @see DWT#error(int)
197  */
198 void error (int code) {
199     DWT.error(code);
200 }
201
202 /**
203  * Returns the receiver's parent, which must be a <code>Shell</code>
204  * or null.
205  *
206  * @return the receiver's parent
207  *
208  * @exception DWTException <ul>
209  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
210  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
211  * </ul>
212  */
213 public Shell getParent () {
214     return parent;
215 }
216
217 /**
218  * Returns the receiver's style information.
219  * <p>
220  * Note that, the value which is returned by this method <em>may
221  * not match</em> the value which was provided to the constructor
222  * when the receiver was created.
223  * </p>
224  *
225  * @return the style bits
226  *
227  * @exception DWTException <ul>
228  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
229  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
230  * </ul>
231  */
232 public int getStyle () {
233     return style;
234 }
235
236 /**
237  * Returns the receiver's text, which is the string that the
238  * window manager will typically display as the receiver's
239  * <em>title</em>. If the text has not previously been set,
240  * returns an empty string.
241  *
242  * @return the text
243  *
244  * @exception DWTException <ul>
245  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
246  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
247  * </ul>
248  */
249 public String getText () {
250     return title;
251 }
252
253 /**
254  * Sets the receiver's text, which is the string that the
255  * window manager will typically display as the receiver's
256  * <em>title</em>, to the argument, which must not be null.
257  *
258  * @param string the new text
259  *
260  * @exception DWTException <ul>
261  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
262  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
263  * </ul>
264  */
265 public void setText (String string) {
266     // DWT extension: allow null string
267     //if (string is null) error (DWT.ERROR_NULL_ARGUMENT);
268     title = string;
269 }
270
271 }
Note: See TracBrowser for help on using the browser.