root/branches/0.2/controlexample/buttontab.d

Revision 46, 7.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.buttontab;
12
13 private {
14 import swt.all;
15 import controlexample.alignabletab;
16 import controlexample.controlexample;
17 }
18 /**
19  * <code>ButtonTab</code> is the class that
20  * demonstrates SWT buttons.
21  */
22 class ButtonTab : AlignableTab {
23
24     /* Example widgets and groups that contain them */
25     Button button1, button2, button3, button4, button5, button6;
26     Group textButtonGroup, imageButtonGroup;
27
28     /* Alignment widgets added to the "Control" group */
29     Button upButton, downButton;
30
31     /* Style widgets added to the "Style" group */
32     Button pushButton, checkButton, radioButton, toggleButton, arrowButton, flatButton;
33    
34     /**
35      * Creates the Tab within a given instance_ren of ControlExample.
36      */
37     this(ControlExample instance_ren) {
38         super(instance_ren);
39     }
40    
41     /**
42      * Creates the "Control" group.
43      */
44     void createControlGroup () {
45         super.createControlGroup ();
46    
47         /* Create the controls */
48         upButton = new Button (alignmentGroup, SWT.RADIO);
49         upButton.setText (ControlExample.getResourceString("Up"));
50         downButton = new Button (alignmentGroup, SWT.RADIO);
51         downButton.setText (ControlExample.getResourceString("Down"));
52    
53         /* Add the listeners */
54         void widgetSelected(SelectionEvent event) {
55             ButtonTab c = cast(ButtonTab)event.cData;
56             if (!(cast(Button) event.widget).getSelection()) return;
57             c.setExampleWidgetAlignment ();
58         };
59        
60         upButton.handleSelection(this, &widgetSelected);
61         downButton.handleSelection(this, &widgetSelected);
62     }
63    
64     /**
65      * Creates the "Example" group.
66      */
67     void createExampleGroup () {
68         super.createExampleGroup ();
69        
70         /* Create a group for text buttons */
71         textButtonGroup = new Group(exampleGroup, SWT.NONE);
72         GridLayout gridLayout = new GridLayout ();
73         textButtonGroup.setLayout(gridLayout);
74         gridLayout.numColumns = 3;
75         textButtonGroup.setLayoutData (new GridData (GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
76         textButtonGroup.setText (ControlExample.getResourceString("Text_Buttons"));
77    
78         /* Create a group for the image buttons */
79         imageButtonGroup = new Group(exampleGroup, SWT.NONE);
80         gridLayout = new GridLayout();
81         imageButtonGroup.setLayout(gridLayout);
82         gridLayout.numColumns = 3;
83         imageButtonGroup.setLayoutData (new GridData (GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
84         imageButtonGroup.setText (ControlExample.getResourceString("Image_Buttons"));
85     }
86    
87     /**
88      * Creates the "Example" widgets.
89      */
90     void createExampleWidgets () {
91    
92         /* Compute the widget style */
93         int style = getDefaultStyle();
94         if (pushButton.getSelection()) style |= SWT.PUSH;
95         if (checkButton.getSelection()) style |= SWT.CHECK;
96         if (radioButton.getSelection()) style |= SWT.RADIO;
97         if (toggleButton.getSelection()) style |= SWT.TOGGLE;
98         if (flatButton.getSelection()) style |= SWT.FLAT;
99         if (borderButton.getSelection()) style |= SWT.BORDER;
100         if (leftButton.getSelection()) style |= SWT.LEFT;
101         if (rightButton.getSelection()) style |= SWT.RIGHT;
102         if (arrowButton.getSelection()) {
103             style |= SWT.ARROW;
104             if (upButton.getSelection()) style |= SWT.UP;
105             if (downButton.getSelection()) style |= SWT.DOWN;
106         } else {
107             if (centerButton.getSelection()) style |= SWT.CENTER;           
108         }
109    
110         /* Create the example widgets */
111         button1 = new Button(textButtonGroup, style);
112         button1.setText(ControlExample.getResourceString("One"));
113         button2 = new Button(textButtonGroup, style);
114         button2.setText(ControlExample.getResourceString("Two"));
115         button3 = new Button(textButtonGroup, style);
116         button3.setText(ControlExample.getResourceString("Three"));
117         button4 = new Button(imageButtonGroup, style);
118         button4.setImage(instance_ren.images[ControlExample.ciClosedFolder]);
119         button5 = new Button(imageButtonGroup, style);
120         button5.setImage(instance_ren.images[ControlExample.ciOpenFolder]);
121         button6 = new Button(imageButtonGroup, style);
122         button6.setImage(instance_ren.images[ControlExample.ciTarget]);
123     }
124    
125     /**
126      * Creates the "Style" group.
127      */
128     void createStyleGroup() {
129         super.createStyleGroup ();
130    
131         /* Create the extra widgets */
132         pushButton = new Button (styleGroup, SWT.RADIO);
133         pushButton.setText("SWT.PUSH");
134         checkButton = new Button (styleGroup, SWT.RADIO);
135         checkButton.setText ("SWT.CHECK");
136         radioButton = new Button (styleGroup, SWT.RADIO);
137         radioButton.setText ("SWT.RADIO");
138         toggleButton = new Button (styleGroup, SWT.RADIO);
139         toggleButton.setText ("SWT.TOGGLE");
140         arrowButton = new Button (styleGroup, SWT.RADIO);
141         arrowButton.setText ("SWT.ARROW");
142         flatButton = new Button (styleGroup, SWT.CHECK);
143         flatButton.setText ("SWT.FLAT");
144         borderButton = new Button (styleGroup, SWT.CHECK);
145         borderButton.setText ("SWT.BORDER");
146     }
147    
148     /**
149      * Gets the "Example" widget children.
150      */
151     Control [] getExampleWidgets () {
152         Control[] controls;
153         controls ~= button1;
154         controls ~= button2;
155         controls ~= button3;
156         controls ~= button4;
157         controls ~= button5;
158         controls ~= button6;
159         return controls;   
160     }
161    
162     /**
163      * Gets the text for the tab folder item.
164      */
165     String getTabText () {
166         return "Button";
167     }
168    
169     /**
170      * Sets the alignment of the "Example" widgets.
171      */
172     void setExampleWidgetAlignment () {
173         int alignment = 0;
174         if (leftButton.getSelection ()) alignment = SWT.LEFT;
175         if (centerButton.getSelection ()) alignment = SWT.CENTER;
176         if (rightButton.getSelection ()) alignment = SWT.RIGHT;
177         if (upButton.getSelection ()) alignment = SWT.UP;
178         if (downButton.getSelection ()) alignment = SWT.DOWN;
179         button1.setAlignment (alignment);
180         button2.setAlignment (alignment);
181         button3.setAlignment (alignment);
182         button4.setAlignment (alignment);
183         button5.setAlignment (alignment);
184         button6.setAlignment (alignment);
185     }
186    
187     /**
188      * Sets the state of the "Example" widgets.
189      */
190     void setExampleWidgetState () {
191         super.setExampleWidgetState ();
192         if (arrowButton.getSelection ()) {
193             upButton.setEnabled (true);
194             centerButton.setEnabled (false);
195             downButton.setEnabled (true);
196         } else {
197             upButton.setEnabled (false);
198             centerButton.setEnabled (true);
199             downButton.setEnabled (false);
200         }
201         upButton.setSelection ((button1.getStyle () & SWT.UP) != 0);
202         downButton.setSelection ((button1.getStyle () & SWT.DOWN) != 0);
203         pushButton.setSelection ((button1.getStyle () & SWT.PUSH) != 0);
204         checkButton.setSelection ((button1.getStyle () & SWT.CHECK) != 0);
205         radioButton.setSelection ((button1.getStyle () & SWT.RADIO) != 0);
206         toggleButton.setSelection ((button1.getStyle () & SWT.TOGGLE) != 0);
207         arrowButton.setSelection ((button1.getStyle () & SWT.ARROW) != 0);
208         flatButton.setSelection ((button1.getStyle () & SWT.FLAT) != 0);
209         borderButton.setSelection ((button1.getStyle () & SWT.BORDER) != 0);
210     }
211 }
Note: See TracBrowser for help on using the browser.