root/branches/0.2/controlexample/tab.d

Revision 46, 32.3 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.tab;
12
13 private {
14 import controlexample.controlexample;
15 import swt.all;
16 }
17
18
19 /**
20  * <code>Tab</code> is the abstract superclass of every page
21  * in the example's tab folder.  Each page in the tab folder
22  * describes a control.
23  *
24  * A Tab itself is not a control but instead provides a
25  * hierarchy with which to share code that is common to
26  * every page in the folder.
27  *
28  * A typical page in a Tab contains a two column composite.
29  * The left column contains the "Example" group.  The right
30  * column contains "Control" group.  The "Control" group
31  * contains controls that allow the user to interact with
32  * the example control.  The "Control" group typically
33  * contains a "Style", "Other" and "Size" group.  Subclasses
34  * can override these defaults to augment a group or stop
35  * a group from being created.
36  */
37 abstract class Tab {
38     /* Common control buttons */
39     Button borderButton, enabledButton, visibleButton;
40     Button preferredButton, tooSmallButton, smallButton, largeButton, fillButton;
41    
42
43     /* Common groups and composites */
44     Composite tabFolderPage;
45     Group exampleGroup, controlGroup, listenersGroup, otherGroup, sizeGroup, styleGroup, colorGroup;
46    
47     // <Shawn> listenCheckbox in listenersGroup;
48     Button listenCheckbox;
49    
50     /* Controlling instance_ren */
51     ControlExample instance_ren;
52
53     /* Sizing constants for the "Size" group */
54     static int TOO_SMALL_SIZE   = 10;
55     static int SMALL_SIZE       = 50;
56     static int LARGE_SIZE       = 200;
57    
58     /* Right-to-left support */
59     static BOOL RTL_SUPPORT_ENABLE = false;
60     Group orientationGroup;
61     Button rtlButton, ltrButton, defaultOrietationButton;
62
63     /* Controls and resources for the "Colors" group */
64     Button foregroundButton, backgroundButton, fontButton;
65     Image foregroundImage, backgroundImage;
66     Color foregroundColor, backgroundColor;
67     Font font;
68
69     /* Event logging variables and controls */
70     Text eventConsole;
71     BOOL logging = false;
72     BOOL [] eventsFilter;
73
74     static String [] EVENT_NAMES = [
75         "KeyDown", "KeyUp",
76         "MouseDown", "MouseUp", "MouseMove", "MouseEnter", "MouseExit", "MouseDoubleClick",
77         "Paint", "Move", "Resize", "Dispose",
78         "Selection", "DefaultSelection",
79         "FocusIn", "FocusOut",
80         "Expand", "Collapse",
81         "Iconify", "Deiconify", "Close",
82         "Show", "Hide",
83         "Modify", "Verify",
84         "Activate", "Deactivate",
85         "Help", "DragDetect", "Arm", "Traverse", "MouseHover",
86         "HardKeyDown", "HardKeyUp",
87         "MenuDetect"
88     ];
89
90     /**
91      * Creates the Tab within a given instance_ren of ControlExample.
92      */
93     this(ControlExample instance_ren) {
94         this.instance_ren = instance_ren;
95     }
96
97     /**
98      * Creates the "Control" group.  The "Control" group
99      * is typically the right hand column in the tab.
100      */
101     void createControlGroup () {
102    
103         /*
104          * Create the "Control" group.  This is the group on the
105          * right half of each example tab.  It consists of the
106          * "Style" group, the "Other" group and the "Size" group.
107          */
108         controlGroup = new Group (tabFolderPage, SWT.NONE);
109         controlGroup.setLayout (new GridLayout (2, true));
110         controlGroup.setLayoutData (new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
111         controlGroup.setText (ControlExample.getResourceString("Parameters"));
112    
113         /* Create individual groups inside the "Control" group */
114         createStyleGroup ();
115         createOtherGroup ();
116         createSizeGroup ();
117         createColorGroup ();
118         if (RTL_SUPPORT_ENABLE) {
119             createOrientationGroup ();
120         }
121    
122         /*
123          * For each Button child in the style group, add a selection
124          * listener that will recreate the example controls.  If the
125          * style group button is a RADIO button, ensure that the radio
126          * button is selected before recreating the example controls.
127          * When the user selects a RADIO button, the current RADIO
128          * button in the group is deselected and the new RADIO button
129          * is selected automatically.  The listeners are notified for
130          * both these operations but typically only do work when a RADIO
131          * button is selected.
132          */
133          
134         void widgetSelected (SelectionEvent event) {
135             Tab tab = cast(Tab)event.cData;
136             assert(tab);
137             if ((event.widget.getStyle () & SWT.RADIO) != 0) {
138                 if (!(cast(Button) event.widget).getSelection ()) return;
139             }
140             tab.recreateExampleWidgets ();
141         };
142      
143        
144         Control [] children = styleGroup.getChildren ();
145         for (int i=0; i<children.length; i++) {
146             if ( cast(Button)children[i]) {
147                 Button button = cast(Button) children [i];
148                 button.handleSelection (this, &widgetSelected);
149             }
150         }
151         if (RTL_SUPPORT_ENABLE) {
152             rtlButton.handleSelection (this, &widgetSelected);
153             ltrButton.handleSelection (this, &widgetSelected);     
154             defaultOrietationButton.handleSelection (this, &widgetSelected);
155         }
156     }
157    
158     /**
159      * Creates the "Control" widget children.
160      * Subclasses override this method to augment
161      * the standard controls created in the "Style",
162      * "Other" and "Size" groups.
163      */
164     void createControlWidgets () {
165     }
166    
167     /**
168      * Creates the "Colors" group. This is typically
169      * a child of the "Control" group. Subclasses override
170      * this method to customize and set system colors.
171      */
172     void createColorGroup () {
173         /* Create the group */
174         colorGroup = new Group(controlGroup, SWT.NONE);
175         colorGroup.setLayout (new GridLayout (2, false));
176         colorGroup.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
177         colorGroup.setText (ControlExample.getResourceString ("Colors"));
178         Label label = new Label(colorGroup, SWT.NONE);
179         label.setText (ControlExample.getResourceString ("Foreground_Color"));
180         foregroundButton = new Button (colorGroup, SWT.PUSH);
181         label = new Label(colorGroup, SWT.NONE);
182         label.setText (ControlExample.getResourceString ("Background_Color"));
183         backgroundButton = new Button (colorGroup, SWT.PUSH);
184         fontButton = new Button (colorGroup, SWT.PUSH);
185         fontButton.setText(ControlExample.getResourceString("Font"));
186         fontButton.setLayoutData(new GridData (GridData.HORIZONTAL_ALIGN_FILL));
187         Button defaultsButton = new Button (colorGroup, SWT.PUSH);
188         defaultsButton.setText(ControlExample.getResourceString("Defaults"));
189
190         Shell shell = controlGroup.getShell ();
191         /* Create images to display current colors */
192         int imageSize = 12;
193         Display display = shell.getDisplay ();
194         foregroundImage = new Image (display, imageSize, imageSize);
195         backgroundImage = new Image (display, imageSize, imageSize);   
196
197         /* Add listeners to set the colors and font */
198         foregroundButton.setImage(foregroundImage); // sets the size of the button     
199         foregroundButton.handleSelection(this, delegate(SelectionEvent event) {
200             Tab c = cast(Tab)event.cData;
201             ColorDialog foregroundDialog = new ColorDialog (c.controlGroup.getShell());
202             Color oldColor = c.foregroundColor;
203             if (oldColor is null) {
204                 Control [] controls = c.getExampleWidgets ();
205                 if (controls.length > 0) oldColor = controls [0].getForeground ();
206             }
207             if (oldColor !== null) foregroundDialog.setRGB(oldColor.getRGB()); // seed dialog with current color
208             RGB rgb = foregroundDialog.open();
209             if (rgb is null) return;
210             oldColor = c.foregroundColor; // save old foreground color to dispose when done
211             c.foregroundColor = new Color (event.display, rgb);
212             c.setExampleWidgetForeground ();
213             if (oldColor !== null) oldColor.dispose ();
214         });
215        
216         backgroundButton.setImage(backgroundImage); // sets the size of the button
217         backgroundButton.handleSelection(this, delegate(SelectionEvent event){
218             Tab c = cast(Tab)event.cData;
219             ColorDialog backgroundDialog = new ColorDialog (c.controlGroup.getShell());
220             Color oldColor = c.backgroundColor;
221             if (oldColor is null) {
222                 Control [] controls = c.getExampleWidgets ();
223                 if (controls.length > 0) oldColor = controls [0].getBackground (); // seed dialog with current color
224             }
225             if (oldColor !== null) backgroundDialog.setRGB(oldColor.getRGB());
226             RGB rgb = backgroundDialog.open();
227             if (rgb is null) return;
228             oldColor = c.backgroundColor; // save old background color to dispose when done
229             c.backgroundColor = new Color (event.display, rgb);
230             c.setExampleWidgetBackground ();
231             if (oldColor !== null) oldColor.dispose ();
232         });
233        
234         fontButton.handleSelection(this, delegate(SelectionEvent event){
235             Tab c = cast(Tab)event.cData;
236             FontDialog fontDialog = new FontDialog (c.controlGroup.getShell());
237             Font oldFont = c.font;
238             if (oldFont is null) {
239                 Control [] controls = c.getExampleWidgets ();
240                 if (controls.length > 0) oldFont = controls [0].getFont ();
241             }
242             if (oldFont !== null) fontDialog.setFontList(oldFont.getFontData()); // seed dialog with current font
243             FontData fontData = fontDialog.open ();
244             if (fontData is null) return;
245             oldFont = c.font; // dispose old font when done
246             c.font = new Font (event.display, fontData);
247             c.setExampleWidgetFont ();
248             c.setExampleWidgetSize ();
249             if (oldFont !== null) oldFont.dispose ();       
250         });
251         defaultsButton.handleEvent(this, SWT.Selection, &resetColorsAndFonts);
252
253         shell.handleEvent(this, SWT.Selection, delegate(Event e) {
254             Tab tab = cast(Tab)e.cData;
255             if (tab.foregroundImage !== null) tab.foregroundImage.dispose();
256             if (tab.backgroundImage !== null) tab.backgroundImage.dispose();
257             if (tab.foregroundColor !== null) tab.foregroundColor.dispose();
258             if (tab.backgroundColor !== null) tab.backgroundColor.dispose();
259             if (tab.font !== null) font.dispose();
260             tab.foregroundColor = null;
261             tab.backgroundColor = null;
262             tab.font = null;               
263         });
264     }
265    
266     /**
267      * Creates the "Other" group.  This is typically
268      * a child of the "Control" group.
269      */
270     void createOtherGroup () {
271         /* Create the group */
272         otherGroup = new Group (controlGroup, SWT.NONE);
273         otherGroup.setLayout (new GridLayout ());
274         otherGroup.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
275         otherGroup.setText (ControlExample.getResourceString("Other"));
276    
277         /* Create the controls */
278         enabledButton = new Button(otherGroup, SWT.CHECK);
279         enabledButton.setText(ControlExample.getResourceString("Enabled"));
280         visibleButton = new Button(otherGroup, SWT.CHECK);
281         visibleButton.setText(ControlExample.getResourceString("Visible"));
282    
283         /* Add the listeners */
284         enabledButton.handleEvent(this, SWT.Selection, &setExampleWidgetEnabled);
285         visibleButton.handleEvent(this, SWT.Selection, &setExampleWidgetVisibility);
286    
287         /* Set the default state */
288         enabledButton.setSelection(true);
289         visibleButton.setSelection(true);
290     }
291    
292     /**
293      * Create the event console popup menu.
294      */
295     void createEventConsolePopup () {
296         Menu popup = new Menu (eventConsole.getShell (), SWT.POP_UP);
297         eventConsole.setMenu (popup);
298
299         MenuItem cut = new MenuItem (popup, SWT.PUSH);
300         cut.setText (ControlExample.getResourceString("MenuItem_Cut"));
301         cut.handleEvent (eventConsole, SWT.Selection, delegate(Event e){
302             Text txt = cast(Text)e.cData;
303             txt.cut();
304         });
305        
306         MenuItem copy = new MenuItem (popup, SWT.PUSH);
307         copy.setText (ControlExample.getResourceString("MenuItem_Copy"));
308         copy.handleEvent(eventConsole, SWT.Selection, delegate(Event e){
309             Text txt = cast(Text)e.cData;
310             txt.copy();
311         });
312        
313         MenuItem paste = new MenuItem (popup, SWT.PUSH);
314         paste.setText (ControlExample.getResourceString("MenuItem_Paste"));
315         paste.handleEvent(eventConsole, SWT.Selection, delegate(Event e){
316             Text txt = cast(Text)e.cData;
317             txt.paste();
318         });
319         new MenuItem (popup, SWT.SEPARATOR);
320         MenuItem selectAll = new MenuItem (popup, SWT.PUSH);
321         selectAll.setText(ControlExample.getResourceString("MenuItem_SelectAll"));
322         selectAll.handleEvent(eventConsole, SWT.Selection, delegate(Event e){
323             Text txt = cast(Text)e.cData;
324             txt.selectAll();
325         });
326     }
327
328     /**
329      * Creates the "Example" group.  The "Example" group
330      * is typically the left hand column in the tab.
331      */
332     void createExampleGroup () {
333         exampleGroup = new Group (tabFolderPage, SWT.NONE);
334         GridLayout gridLayout = new GridLayout ();
335         exampleGroup.setLayout (gridLayout);
336         exampleGroup.setLayoutData (new GridData (GridData.FILL_BOTH));
337     }
338    
339     /**
340      * Creates the "Example" widget children of the "Example" group.
341      * Subclasses override this method to create the particular
342      * example control.
343      */
344     void createExampleWidgets () {
345         /* Do nothing */
346     }
347    
348     /**
349      * Creates and opens the "Listener selection" dialog.
350      */
351     void createListenerSelectionDialog () {
352         Shell dialog = new Shell (tabFolderPage.getShell (), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
353         dialog.setText (ControlExample.getResourceString ("Select_Listeners"));
354         dialog.setLayout (new GridLayout (2, false));
355         Table table = new Table (dialog, SWT.BORDER | SWT.V_SCROLL | SWT.CHECK);
356         GridData data = new GridData(GridData.FILL_BOTH);
357         data.verticalSpan = 2;
358         table.setLayoutData(data);
359         for (int i = 0; i < EVENT_NAMES.length; i++) {
360             TableItem item = new TableItem (table, SWT.NONE);
361             item.setText (EVENT_NAMES[i]);
362             item.setChecked (eventsFilter[i]);
363         }
364         String [] customNames = getCustomEventNames ();
365         for (int i = 0; i < customNames.length; i++) {
366             TableItem item = new TableItem (table, SWT.NONE);
367             item.setText (customNames[i]);
368             item.setChecked (eventsFilter[EVENT_NAMES.length + i]);
369         }
370        
371         Button selectAll = new Button (dialog, SWT.PUSH);
372         selectAll.setText(ControlExample.getResourceString ("Select_All"));
373         selectAll.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
374         selectAll.handleSelection(this, delegate(SelectionEvent e){
375             Tab c = cast(Tab)e.cData;
376             TableItem [] items = table.getItems();
377             for (int i = 0; i < c.EVENT_NAMES.length; i++) {
378                 items[i].setChecked(true);
379             }
380             for (int i = 0; i < c.getCustomEventNames().length; i++) {
381                 items[c.EVENT_NAMES.length + i].setChecked(true);
382             }
383         });
384
385        
386         Button deselectAll = new Button (dialog, SWT.PUSH);
387         deselectAll.setText(ControlExample.getResourceString ("Deselect_All"));
388         deselectAll.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING));
389         deselectAll.handleSelection(this, delegate(SelectionEvent e){
390             Tab c = cast(Tab)e.cData;
391             TableItem [] items = table.getItems();
392             for (int i = 0; i < c.EVENT_NAMES.length; i++) {
393                 items[i].setChecked(false);
394             }
395             for (int i = 0; i < c.getCustomEventNames().length; i++) {
396                 items[c.EVENT_NAMES.length + i].setChecked(false);
397             }
398         });
399        
400         Label filler = new Label(dialog, SWT.NONE);
401         Button ok = new Button (dialog, SWT.PUSH);
402         ok.setText(ControlExample.getResourceString ("OK"));
403         dialog.setDefaultButton(ok);
404         ok.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
405         ok.handleSelection(this, delegate(SelectionEvent e){
406             Tab t = cast(Tab)e.cData;
407             TableItem [] items = table.getItems();
408             for (int i = 0; i < t.EVENT_NAMES.length; i++) {
409                 eventsFilter[i] = items[i].getChecked();
410             }
411             for (int i = 0; i < t.getCustomEventNames().length; i++) {
412                 eventsFilter[t.EVENT_NAMES.length + i] = items[t.EVENT_NAMES.length + i].getChecked();
413             }
414             dialog.dispose();
415         });
416        
417         dialog.pack ();
418         dialog.open ();
419         while (! dialog.isDisposed()) {
420             if (! dialog.getDisplay().readAndDispatch())
421                 dialog.getDisplay().sleep();
422         }
423     }
424
425     /**
426      * Creates the "Listeners" group.  The "Listeners" group
427      * goes below the "Example" and "Control" groups.
428      */
429     void createListenersGroup () {
430         listenersGroup = new Group (tabFolderPage, SWT.NONE);
431         listenersGroup.setLayout (new GridLayout (3, false));
432         GridData gridData = new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL);
433         gridData.horizontalSpan = 2;
434         listenersGroup.setLayoutData (gridData);
435         listenersGroup.setText (ControlExample.getResourceString ("Listeners"));
436
437         /*
438          * Create the button to access the 'Listeners' dialog.
439          */
440         Button listenersButton = new Button (listenersGroup, SWT.PUSH);
441         listenersButton.setText (ControlExample.getResourceString ("Select_Listeners"));
442         listenersButton.handleSelection(this, delegate(SelectionEvent e){
443             Tab c = cast(Tab)e.cData;
444             c.createListenerSelectionDialog ();
445             c.recreateExampleWidgets ();
446         });
447        
448         /*
449          * Create the checkbox to add/remove listeners to/from the example widgets.
450          */
451         listenCheckbox = new Button (listenersGroup, SWT.CHECK);
452         listenCheckbox.setText (ControlExample.getResourceString ("Listen"));
453         void onListCheckbox(SelectionEvent e){
454             Tab c = cast(Tab)e.cData;
455             c.logging = c.listenCheckbox.getSelection ();
456             c.recreateExampleWidgets ();
457         }
458         listenCheckbox.handleSelection(this, &onListCheckbox);
459         /*
460          * Create the button to clear the text.
461          */
462         Button clearButton = new Button (listenersGroup, SWT.PUSH);
463         clearButton.setText (ControlExample.getResourceString ("Clear"));
464         clearButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
465         clearButton.handleSelection(this, delegate(SelectionEvent e){
466             Tab c = cast(Tab)e.cData;
467             c.eventConsole.setText ("");
468         });
469        
470         /* Initialize the eventsFilter to log all events. */
471         int customEventCount = getCustomEventNames ().length;
472         eventsFilter = new BOOL [EVENT_NAMES.length + customEventCount];
473         for (int i = 0; i < EVENT_NAMES.length + customEventCount; i++) {
474             eventsFilter [i] = true;
475         }
476
477         /* Create the event console Text. */
478         eventConsole = new Text (listenersGroup, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
479         GridData data = new GridData (GridData.FILL_BOTH);
480         data.horizontalSpan = 3;
481         data.heightHint = 80;
482         eventConsole.setLayoutData (data);
483         createEventConsolePopup ();
484
485         eventConsole.handleKeyDown(this, delegate(KeyEvent e) {
486             Tab c = cast(Tab)e.cData;
487             if ((e.keyCode == 'A' || e.keyCode == 'a') && (e.stateMask & SWT.MOD1) != 0) {
488                 c.eventConsole.selectAll ();
489                 e.doit = false;
490             }
491