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