root/branches/0.2/controlexample/dialogtab.d

Revision 46, 19.4 kB (checked in by JJR, 4 years ago)

* 0.2 Branch added: New working DWT version submitted by Shawn Liu.

Line 
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 module controlexample.dialogtab;
12
13 private {
14 import swt.all;
15 import controlexample.tab;
16 import controlexample.controlexample;
17 }
18
19
20 class DialogTab : Tab {
21     /* Example widgets and groups that contain them */
22     Group dialogStyleGroup, resultGroup;
23     Text textWidget;
24    
25     /* Style widgets added to the "Style" group */
26     Combo dialogCombo;
27     Button okButton, cancelButton;
28     Button yesButton, noButton;
29     Button retryButton;
30     Button abortButton, ignoreButton;
31     Button iconErrorButton, iconInformationButton, iconQuestionButton;
32     Button iconWarningButton, iconWorkingButton;
33     Button modelessButton, primaryModalButton, applicationModalButton, systemModalButton;
34     Button saveButton, openButton, multiButton;
35
36     static String [] FilterExtensions   = ["*.d", "*.txt", "*.bat", "*.doc", "*.*"];
37     static String [] FilterNames;
38     /**
39      * Creates the Tab within a given instance_ren of ControlExample.
40      */
41     this(ControlExample instance_ren) {
42         if(FilterNames is null) {
43             FilterNames ~= ControlExample.getResourceString("FilterName_0");
44             FilterNames ~= ControlExample.getResourceString("FilterName_1");
45             FilterNames ~= ControlExample.getResourceString("FilterName_2");
46             FilterNames ~= ControlExample.getResourceString("FilterName_3");
47             FilterNames ~= ControlExample.getResourceString("FilterName_4");
48         }
49         super(instance_ren);
50     }
51
52     /**
53      * Handle a button style selection event.
54      *
55      * @param event the selection event
56      */
57     void buttonStyleSelected(SelectionEvent event) {
58         /*
59          * Only certain combinations of button styles are
60          * supported for various dialogs.  Make sure the
61          * control widget reflects only valid combinations.
62          */
63         okButton.setEnabled (
64             !(yesButton.getSelection () || noButton.getSelection () ||
65                 retryButton.getSelection () || abortButton.getSelection () ||
66                     ignoreButton.getSelection ()));
67         cancelButton.setEnabled (
68             !(abortButton.getSelection () || ignoreButton.getSelection () ||
69                 (yesButton.getSelection () != noButton.getSelection ())));
70         yesButton.setEnabled (
71             !(okButton.getSelection () || retryButton.getSelection () ||
72                 abortButton.getSelection () || ignoreButton.getSelection () ||
73                     (cancelButton.getSelection () && !yesButton.getSelection () && !noButton.getSelection ())));
74         noButton.setEnabled (
75             !(okButton.getSelection () || retryButton.getSelection () ||
76                 abortButton.getSelection () || ignoreButton.getSelection () ||
77                     (cancelButton.getSelection () && !yesButton.getSelection () && !noButton.getSelection ())));
78         retryButton.setEnabled (
79             !(okButton.getSelection() || yesButton.getSelection() || noButton.getSelection ()));
80         abortButton.setEnabled (
81             !(okButton.getSelection () || cancelButton.getSelection () ||
82                 yesButton.getSelection () || noButton.getSelection ()));
83         ignoreButton.setEnabled (
84             !(okButton.getSelection () || cancelButton.getSelection () |
85                 yesButton.getSelection () || noButton.getSelection ()));
86     }
87    
88     /**
89      * Handle the create button selection event.
90      *
91      * @param event org.eclipse.swt.events.SelectionEvent
92      */
93     void createButtonSelected(SelectionEvent event) {
94    
95         /* Compute the appropriate dialog style */
96         int style = getDefaultStyle();
97         if (okButton.getEnabled () && okButton.getSelection ()) style |= SWT.OK;
98         if (cancelButton.getEnabled () && cancelButton.getSelection ()) style |= SWT.CANCEL;
99         if (yesButton.getEnabled () && yesButton.getSelection ()) style |= SWT.YES;
100         if (noButton.getEnabled () && noButton.getSelection ()) style |= SWT.NO;
101         if (retryButton.getEnabled () && retryButton.getSelection ()) style |= SWT.RETRY;
102         if (abortButton.getEnabled () && abortButton.getSelection ()) style |= SWT.ABORT;
103         if (ignoreButton.getEnabled () && ignoreButton.getSelection ()) style |= SWT.IGNORE;
104         if (iconErrorButton.getEnabled () && iconErrorButton.getSelection ()) style |= SWT.ICON_ERROR;
105         if (iconInformationButton.getEnabled () && iconInformationButton.getSelection ()) style |= SWT.ICON_INFORMATION;
106         if (iconQuestionButton.getEnabled () && iconQuestionButton.getSelection ()) style |= SWT.ICON_QUESTION;
107         if (iconWarningButton.getEnabled () && iconWarningButton.getSelection ()) style |= SWT.ICON_WARNING;
108         if (iconWorkingButton.getEnabled () && iconWorkingButton.getSelection ()) style |= SWT.ICON_WORKING;
109         if (primaryModalButton.getEnabled () && primaryModalButton.getSelection ()) style |= SWT.PRIMARY_MODAL;
110         if (applicationModalButton.getEnabled () && applicationModalButton.getSelection ()) style |= SWT.APPLICATION_MODAL;
111         if (systemModalButton.getEnabled () && systemModalButton.getSelection ()) style |= SWT.SYSTEM_MODAL;
112         if (saveButton.getEnabled () && saveButton.getSelection ()) style |= SWT.SAVE;
113         if (openButton.getEnabled () && openButton.getSelection ()) style |= SWT.OPEN;
114         if (multiButton.getEnabled () && multiButton.getSelection ()) style |= SWT.MULTI;
115    
116         /* Open the appropriate dialog type */
117         String name = dialogCombo.getText ();
118         Shell shell = tabFolderPage.getShell ();
119        
120         if (std.string.cmp(name, ControlExample.getResourceString("ColorDialog")) == 0) {
121             auto ColorDialog dialog = new ColorDialog (shell ,style);
122             dialog.setRGB (new RGB (100, 100, 100));
123             dialog.setText (ControlExample.getResourceString("Title"));
124             RGB result = dialog.open ();
125             textWidget.append (ControlExample.getResourceString("ColorDialog") ~ Text.DELIMITER);
126             if(result is null ) { textWidget.append (Text.DELIMITER); return; }
127             textWidget.append ("Result: " ~ result.toString() ~ Text.DELIMITER ~ Text.DELIMITER);
128             return;
129         }
130        
131         if (std.string.cmp(name, ControlExample.getResourceString("DirectoryDialog")) == 0) {
132             auto DirectoryDialog dialog = new DirectoryDialog (shell, style);
133             dialog.setMessage (ControlExample.getResourceString("Example_string"));
134             dialog.setText (ControlExample.getResourceString("Title"));
135             String result = dialog.open ();
136             textWidget.append (ControlExample.getResourceString("DirectoryDialog") ~ Text.DELIMITER);
137             if(result is null ) { textWidget.append (Text.DELIMITER); return; }
138             textWidget.append ("Result: " ~ result ~ Text.DELIMITER ~ Text.DELIMITER);
139             return;
140         }
141        
142         if (std.string.cmp(name, ControlExample.getResourceString("FileDialog")) == 0) {
143             auto FileDialog dialog = new FileDialog (shell, style);
144 //          dialog.setFileName (ControlExample.getResourceString("readme_txt"));
145             dialog.setFilterNames (FilterNames);
146             dialog.setFilterExtensions (FilterExtensions);
147 //          dialog.setText (ControlExample.getResourceString("Title"));
148             String result = dialog.open();
149             textWidget.append (ControlExample.getResourceString("FileDialog") ~ Text.DELIMITER);
150             if(result is null ) { textWidget.append (Text.DELIMITER); return; }
151             textWidget.append("Result: " ~ result ~ Text.DELIMITER);
152             String[] sArr = dialog.getFileNames();
153             if(sArr.length > 1 ){
154                 Util.trace("file count = ", sArr.length);
155                 for(int i=1; i<sArr.length; ++i)
156                     textWidget.append(sArr[i] ~ Text.DELIMITER);
157             }
158             textWidget.append(Text.DELIMITER);
159             return;
160         }
161        
162         if (std.string.cmp(name, ControlExample.getResourceString("FontDialog")) == 0) {
163             auto FontDialog dialog = new FontDialog (shell, style);
164             dialog.setText (ControlExample.getResourceString("Title"));
165             FontData result = dialog.open ();
166             textWidget.append (ControlExample.getResourceString("FontDialog") ~ Text.DELIMITER);
167             if(result is null ) { textWidget.append (Text.DELIMITER); return; }
168             textWidget.append ("Result: " ~ result.toString() ~ Text.DELIMITER ~ Text.DELIMITER);
169             return;
170         }
171        
172         if (std.string.cmp(name, ControlExample.getResourceString("PrintDialog")) == 0) {
173             auto PrintDialog dialog = new PrintDialog (shell, style);
174             dialog.setText(ControlExample.getResourceString("Title"));
175             PrinterData result = dialog.open ();
176             textWidget.append (ControlExample.getResourceString("PrintDialog") ~ Text.DELIMITER);
177             if(result is null ) { textWidget.append (Text.DELIMITER); return; }
178             textWidget.append ("Result: " ~ result.toString() ~ Text.DELIMITER ~ Text.DELIMITER);
179             return;
180         }
181    
182         if (std.string.cmp(name, ControlExample.getResourceString("MessageBox")) == 0) {
183             MessageBox dialog = new MessageBox (shell, style);
184             dialog.setMessage (ControlExample.getResourceString("Example_string"));
185             dialog.setText (ControlExample.getResourceString("Title"));
186             int result = dialog.open ();
187             textWidget.append (ControlExample.getResourceString("MessageBox") ~ Text.DELIMITER);
188             /*
189              * The resulting integer depends on the original
190              * dialog style.  Decode the result and display it.
191              */
192             switch (result) {
193                 case SWT.OK:
194                     textWidget.append ("Result: SWT.OK");
195                     break;
196                 case SWT.YES:
197                     textWidget.append ("Result: SWT.YES");
198                     break;
199                 case SWT.NO:
200                     textWidget.append ("Result: SWT.NO");
201                     break;
202                 case SWT.CANCEL:
203                     textWidget.append ("Result: SWT.CANCEL");
204                     break;
205                 case SWT.ABORT:
206                     textWidget.append ("Result: SWT.ABORT");
207                     break;
208                 case SWT.RETRY:
209                     textWidget.append ("Result: SWT.RETRY");
210                     break;
211                 case SWT.IGNORE:
212                     textWidget.append ("Result: SWT.IGNORE");
213                     break;
214                 default:
215                     textWidget.append ("Result: " ~ std.string.toString(result));
216                     break;
217             }
218             textWidget.append (Text.DELIMITER ~ Text.DELIMITER);
219         }
220     }
221    
222     /**
223      * Creates the "Control" group.
224      */
225     void createControlGroup () {
226         /*
227          * Create the "Control" group.  This is the group on the
228          * right half of each example tab.  It consists of the
229          * style group, the display group and the size group.
230          */         
231         controlGroup = new Group (tabFolderPage, SWT.NONE);
232         GridLayout gridLayout= new GridLayout ();
233         controlGroup.setLayout(gridLayout);
234         gridLayout.numColumns = 2;
235         gridLayout.makeColumnsEqualWidth = true;
236         controlGroup.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
237         controlGroup.setText (ControlExample.getResourceString("Parameters"));
238        
239         /*
240          * Create a group to hold the dialog style combo box and
241          * create dialog button.
242          */
243         dialogStyleGroup = new Group (controlGroup, SWT.NONE);
244         dialogStyleGroup.setLayout (new GridLayout ());
245         GridData gridData = new GridData (GridData.HORIZONTAL_ALIGN_CENTER);
246         gridData.horizontalSpan = 2;
247         dialogStyleGroup.setLayoutData (gridData);
248         dialogStyleGroup.setText (ControlExample.getResourceString("Dialog_Type"));
249     }
250    
251     /**
252      * Creates the "Control" widget children.
253      */
254     void createControlWidgets () {
255    
256         /* Create the combo */
257         String [] strings;
258         strings ~= ControlExample.getResourceString("ColorDialog");
259         strings ~= ControlExample.getResourceString("DirectoryDialog");
260         strings ~= ControlExample.getResourceString("FileDialog");
261         strings ~= ControlExample.getResourceString("FontDialog");
262         strings ~= ControlExample.getResourceString("PrintDialog");
263         strings ~= ControlExample.getResourceString("MessageBox");
264
265         dialogCombo = new Combo (dialogStyleGroup, SWT.READ_ONLY);
266         dialogCombo.setItems (strings);
267         dialogCombo.setText (strings [0]);
268    
269         /* Create the create dialog button */
270         Button createButton = new Button(dialogStyleGroup, SWT.NONE);
271         createButton.setText (ControlExample.getResourceString("Create_Dialog"));
272         createButton.setLayoutData (new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
273    
274         /* Create a group for the various dialog button style controls */
275         Group buttonStyleGroup = new Group (controlGroup, SWT.NONE);
276         buttonStyleGroup.setLayout (new GridLayout ());
277         buttonStyleGroup.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
278         buttonStyleGroup.setText (ControlExample.getResourceString("Button_Styles"));
279    
280         /* Create the button style buttons */
281         okButton = new Button (buttonStyleGroup, SWT.CHECK);
282         okButton.setText ("SWT.OK");
283         cancelButton = new Button (buttonStyleGroup, SWT.CHECK);
284         cancelButton.setText ("SWT.CANCEL");
285         yesButton = new Button (buttonStyleGroup, SWT.CHECK);
286         yesButton.setText ("SWT.YES");
287         noButton = new Button (buttonStyleGroup, SWT.CHECK);
288         noButton.setText ("SWT.NO");
289         retryButton = new Button (buttonStyleGroup, SWT.CHECK);
290         retryButton.setText ("SWT.RETRY");
291         abortButton = new Button (buttonStyleGroup, SWT.CHECK);
292         abortButton.setText ("SWT.ABORT");
293         ignoreButton = new Button (buttonStyleGroup, SWT.CHECK);
294         ignoreButton.setText ("SWT.IGNORE");
295    
296         /* Create a group for the icon style controls */
297         Group iconStyleGroup = new Group (controlGroup, SWT.NONE);
298         iconStyleGroup.setLayout (new GridLayout ());
299         iconStyleGroup.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
300         iconStyleGroup.setText (ControlExample.getResourceString("Icon_Styles"));
301    
302         /* Create the icon style buttons */
303         iconErrorButton = new Button (iconStyleGroup, SWT.RADIO);
304         iconErrorButton.setText ("SWT.ICON_ERROR");
305         iconInformationButton = new Button (iconStyleGroup, SWT.RADIO);
306         iconInformationButton.setText ("SWT.ICON_INFORMATION");
307         iconQuestionButton = new Button (iconStyleGroup, SWT.RADIO);
308         iconQuestionButton.setText ("SWT.ICON_QUESTION");
309         iconWarningButton = new Button (iconStyleGroup, SWT.RADIO);
310         iconWarningButton.setText ("SWT.ICON_WARNING");
311         iconWorkingButton = new Button (iconStyleGroup, SWT.RADIO);
312         iconWorkingButton.setText ("SWT.ICON_WORKING");
313    
314         /* Create a group for the modal style controls */
315         Group modalStyleGroup = new Group (controlGroup, SWT.NONE);
316         modalStyleGroup.setLayout (new GridLayout ());
317         modalStyleGroup.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
318         modalStyleGroup.setText (ControlExample.getResourceString("Modal_Styles"));
319    
320         /* Create the modal style buttons */
321         modelessButton = new Button (modalStyleGroup, SWT.RADIO);
322         modelessButton.setText ("SWT.MODELESS");
323         primaryModalButton = new Button (modalStyleGroup, SWT.RADIO);
324         primaryModalButton.setText ("SWT.PRIMARY_MODAL");
325         applicationModalButton = new Button (modalStyleGroup, SWT.RADIO);
326         applicationModalButton.setText ("SWT.APPLICATION_MODAL");
327         systemModalButton = new Button (modalStyleGroup, SWT.RADIO);
328         systemModalButton.setText ("SWT.SYSTEM_MODAL");
329    
330         /* Create a group for the file dialog style controls */
331         Group fileDialogStyleGroup = new Group (controlGroup, SWT.NONE);
332         fileDialogStyleGroup.setLayout (new GridLayout ());
333         fileDialogStyleGroup.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
334         fileDialogStyleGroup.setText (ControlExample.getResourceString("File_Dialog_Styles"));
335    
336         /* Create the file dialog style buttons */
337         openButton = new Button(fileDialogStyleGroup, SWT.RADIO);
338         openButton.setText("SWT.OPEN");
339         saveButton = new Button (fileDialogStyleGroup, SWT.RADIO);
340         saveButton.setText ("SWT.SAVE");
341         multiButton = new Button(fileDialogStyleGroup, SWT.CHECK);
342         multiButton.setText("SWT.MULTI");
343    
344         /* Create the orientation group */
345         if (RTL_SUPPORT_ENABLE) {
346             createOrientationGroup();
347         }
348        
349         /* Add the listeners */
350         dialogCombo.handleSelection(this, &dialogSelected);
351         createButton.handleSelection(this, &createButtonSelected);
352         okButton.handleSelection (this, &buttonStyleSelected);
353         cancelButton.handleSelection (this, &buttonStyleSelected);
354         yesButton.handleSelection (this, &buttonStyleSelected);
355         noButton.handleSelection (this, &buttonStyleSelected);
356         retryButton.handleSelection (this, &buttonStyleSelected);
357         abortButton.handleSelection (this, &buttonStyleSelected);
358         ignoreButton.handleSelection (this, &buttonStyleSelected);
359    
360         /* Set default values for style buttons */
361         okButton.setEnabled (false);
362         cancelButton.setEnabled (false);
363         yesButton.setEnabled (false);
364         noButton.setEnabled (false);
365         retryButton.setEnabled (false);
366         abortButton.setEnabled (false);
367         ignoreButton.setEnabled (false);
368         iconErrorButton.setEnabled (false);
369         iconInformationButton.setEnabled (false);
370         iconQuestionButton.setEnabled (false);
371         iconWarningButton.setEnabled (false);
372         iconWorkingButton.setEnabled (false);
373         saveButton.setEnabled (false);
374         openButton.setEnabled (false);
375         openButton.setSelection (true);
376         multiButton.setEnabled (false);
377         iconInformationButton.setSelection (true);
378         modelessButton.setSelection (true);
379     }
380    
381     /**
382      * Creates the "Example" group.
383      */
384     void createExampleGroup () {
385         super.createExampleGroup ();
386        
387         /*
388          * Create a group for the text widget to display
389          * the results returned by the example dialogs.
390          */
391         resultGroup = new Group (exampleGroup, SWT.NONE);
392         resultGroup.setLayout (new GridLayout ());
393         resultGroup.setLayoutData (new GridData (GridData.FILL_BOTH));
394         resultGroup.setText (ControlExample.getResourceString("Dialog_Result"));
395     }
396    
397     /**
398      * Creates the "Example" widgets.
399      */
400     void createExampleWidgets () {
401         /*
402          * Create a multi lined, scrolled text widget for output.
403          */
404         textWidget = new Text(resultGroup, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
405         GridData gridData = new GridData (GridData.FILL_BOTH);
406         textWidget.setLayoutData (gridData);   
407     }
408    
409     /**
410      * The platform dialogs do not have SWT listeners.
411      */
412     void createListenersGroup () {
413     }
414
415     /**
416      * Handle a dialog type combo selection event.
417      *
418      * @param event the selection event
419      */
420     void dialogSelected (SelectionEvent event) {
421    
422         /* Enable/Disable the buttons */
423         String name = dialogCombo.getText ();
424         BOOL isMessageBox = std.string.cmp(name, ControlExample.getResourceString("MessageBox"))==0;
425         BOOL isFileDialog = std.string.cmp(name, ControlExample.getResourceString("FileDialog")) == 0;
426         okButton.setEnabled (isMessageBox);
427         cancelButton.setEnabled (isMessageBox);
428         yesButton.setEnabled (isMessageBox);
429         noButton.setEnabled (isMessageBox);
430         retryButton.setEnabled (isMessageBox);
431         abortButton.setEnabled (isMessageBox);
432         ignoreButton.setEnabled (isMessageBox);
433         iconErrorButton.setEnabled (isMessageBox);
434         iconInformationButton.setEnabled (isMessageBox);
435         iconQuestionButton.setEnabled (isMessageBox);
436         iconWarningButton.setEnabled (isMessageBox);
437         iconWorkingButton.setEnabled  (isMessageBox);
438         saveButton.setEnabled (isFileDialog);
439         openButton.setEnabled (isFileDialog);
440         multiButton.setEnabled (isFileDialog);
441    
442         /* Unselect the buttons */
443         if (!isMessageBox) {
444             okButton.setSelection (false);
445             cancelButton.setSelection (false);
446             yesButton.setSelection (false);
447             noButton.setSelection (false);
448             retryButton.setSelection (false);
449             abortButton.setSelection (false);
450             ignoreButton.setSelection (false);
451         }
452     }
453    
454     /**
455      * Gets the "Example" widget children.
456      */
457     Control [] getExampleWidgets () {<