0
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2007 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
|
4
|
10 * Port to the D programming language:
|
0
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtexamples.controlexample.ControlExample;
|
|
14
|
|
15 import dwt.DWT;
|
|
16 import dwt.graphics.Image;
|
|
17 import dwt.graphics.ImageData;
|
|
18 import dwt.graphics.Point;
|
|
19 import dwt.graphics.Rectangle;
|
|
20 import dwt.layout.FillLayout;
|
|
21 import dwt.widgets.Composite;
|
|
22 import dwt.widgets.Display;
|
|
23 import dwt.widgets.Shell;
|
|
24 import dwt.widgets.TabFolder;
|
|
25 import dwt.widgets.TabItem;
|
|
26 import dwt.dwthelper.ResourceBundle;
|
|
27 import dwt.dwthelper.ByteArrayInputStream;
|
|
28
|
|
29 import dwtexamples.controlexample.Tab;
|
|
30 import dwtexamples.controlexample.ButtonTab;
|
|
31 import dwtexamples.controlexample.CanvasTab;
|
|
32 import dwtexamples.controlexample.ComboTab;
|
|
33 import dwtexamples.controlexample.CoolBarTab;
|
|
34 //import dwtexamples.controlexample.DateTimeTab;
|
3
|
35 version(linux){
|
|
36 import dwtexamples.controlexample.DialogTab;
|
|
37 }
|
0
|
38 import dwtexamples.controlexample.ExpandBarTab;
|
|
39 import dwtexamples.controlexample.GroupTab;
|
|
40 import dwtexamples.controlexample.LabelTab;
|
|
41 import dwtexamples.controlexample.LinkTab;
|
|
42 import dwtexamples.controlexample.ListTab;
|
|
43 import dwtexamples.controlexample.MenuTab;
|
|
44 import dwtexamples.controlexample.ProgressBarTab;
|
|
45 import dwtexamples.controlexample.SashTab;
|
|
46 import dwtexamples.controlexample.ScaleTab;
|
|
47 import dwtexamples.controlexample.ShellTab;
|
|
48 import dwtexamples.controlexample.SliderTab;
|
|
49 import dwtexamples.controlexample.SpinnerTab;
|
|
50 import dwtexamples.controlexample.TabFolderTab;
|
|
51 import dwtexamples.controlexample.TableTab;
|
|
52 import dwtexamples.controlexample.TextTab;
|
|
53 import dwtexamples.controlexample.ToolBarTab;
|
|
54 import dwtexamples.controlexample.ToolTipTab;
|
|
55 import dwtexamples.controlexample.TreeTab;
|
|
56
|
|
57
|
|
58 import tango.core.Exception;
|
|
59 import tango.text.convert.Format;
|
|
60 import tango.io.Stdout;
|
|
61 import Math = tango.math.Math;
|
|
62
|
|
63
|
|
64 /*** Linker workaround start ***/
|
|
65 import tango.io.Stdout;
|
|
66 import tango.math.Math;
|
|
67 import tango.text.convert.Format;
|
|
68 import tango.util.Convert;
|
|
69 import tango.util.PathUtil;
|
|
70 /*** Linker workaround end ***/
|
|
71
|
|
72 version(JIVE){
|
|
73 import jive.stacktrace;
|
|
74 }
|
|
75
|
|
76 version( CONTROL_EXAMPLE_MAIN ){
|
|
77 void main(){
|
|
78 Stdout.formatln( "The ControlExample: still work left" );
|
|
79 Stdout.formatln( "todo: search for //PORTING_LEFT" );
|
|
80 Stdout.formatln( "todo: Implement Get/Set API reflection" );
|
|
81 Stdout.formatln( "todo: DateTimeTab not implemented" );
|
|
82 Stdout.formatln( "todo: ExpandBarTab looks strange" );
|
3
|
83 Stdout.formatln( "" );
|
|
84 Stdout.formatln( "On linux GTK:" );
|
|
85 Stdout.formatln( "bug: ProgressBarTab crash on vertical" );
|
|
86 Stdout.formatln( " in java it behaves the same" );
|
|
87 Stdout.formatln( "bug: SliderTab horizontal arrow buttons are too high." );
|
|
88 Stdout.formatln( " in java it behaves the same" );
|
|
89 Stdout.formatln( " Known bug:" );
|
|
90 Stdout.formatln( " https://bugs.eclipse.org/bugs/show_bug.cgi?id=197402" );
|
|
91 Stdout.formatln( " http://bugzilla.gnome.org/show_bug.cgi?id=475909" );
|
|
92 Stdout.formatln( "" );
|
0
|
93 Stdout.formatln( "please report problems" );
|
|
94 ControlExample.main( null );
|
|
95 }
|
|
96 }
|
|
97
|
|
98 public class ControlExample {
|
|
99 private static ResourceBundle resourceBundle;
|
|
100 private static const char[] resourceData = import( "controlexample.properties" );
|
|
101
|
|
102 private ShellTab shellTab;
|
|
103 private TabFolder tabFolder;
|
|
104 private Tab [] tabs;
|
|
105 Image images[];
|
|
106
|
|
107 static const int ciClosedFolder = 0, ciOpenFolder = 1, ciTarget = 2, ciBackground = 3, ciParentBackground = 4;
|
|
108
|
|
109 static const byte[][] imageData = [
|
|
110 cast(byte[]) import( "closedFolder.gif" ),
|
|
111 cast(byte[]) import( "openFolder.gif" ),
|
|
112 cast(byte[]) import( "target.gif" ),
|
|
113 cast(byte[]) import( "backgroundImage.png" ),
|
|
114 cast(byte[]) import( "parentBackgroundImage.png" )
|
|
115 ];
|
|
116 static const int[] imageTypes = [
|
|
117 DWT.ICON,
|
|
118 DWT.ICON,
|
|
119 DWT.ICON,
|
|
120 DWT.BITMAP,
|
|
121 DWT.BITMAP];
|
|
122
|
|
123 bool startup = true;
|
|
124
|
|
125 static this(){
|
|
126 resourceBundle = ResourceBundle.getBundleFromData( resourceData ); //$NON-NLS-1$
|
|
127 }
|
|
128
|
|
129 /**
|
|
130 * Creates an instance of a ControlExample embedded inside
|
|
131 * the supplied parent Composite.
|
|
132 *
|
|
133 * @param parent the container of the example
|
|
134 */
|
|
135 public this(Composite parent) {
|
|
136 initResources();
|
|
137 tabFolder = new TabFolder (parent, DWT.NONE);
|
|
138 tabs = createTabs();
|
|
139 for (int i=0; i<tabs.length; i++) {
|
|
140 TabItem item = new TabItem (tabFolder, DWT.NONE);
|
|
141 item.setText (tabs [i].getTabText ());
|
|
142 item.setControl (tabs [i].createTabFolderPage (tabFolder));
|
|
143 item.setData (tabs [i]);
|
|
144 }
|
|
145 startup = false;
|
|
146 }
|
|
147
|
|
148 /**
|
|
149 * Answers the set of example Tabs
|
|
150 */
|
|
151 Tab[] createTabs() {
|
3
|
152 version(Windows){
|
|
153 return [ cast(Tab)
|
|
154 new ButtonTab (this),
|
|
155 new CanvasTab (this),
|
|
156 new ComboTab (this),
|
|
157 new CoolBarTab (this),
|
|
158 //new DateTimeTab (this), // DateTime Control not implemented
|
|
159 //new DialogTab (this), // Dialog not implemented
|
|
160 new ExpandBarTab (this),
|
|
161 new GroupTab (this),
|
|
162 new LabelTab (this),
|
|
163 new LinkTab (this),
|
|
164 new ListTab (this),
|
|
165 new MenuTab (this),
|
|
166 //new ProgressBarTab (this), // crash on start
|
|
167 new SashTab (this),
|
|
168 //new ScaleTab (this), // crash on start
|
|
169 shellTab = new ShellTab(this),
|
|
170 //new SliderTab (this), // crash on start
|
|
171 //new SpinnerTab (this), // crash on start
|
|
172 new TabFolderTab (this),
|
|
173 new TableTab (this),
|
|
174 new TextTab (this),
|
|
175 new ToolBarTab (this),
|
|
176 new ToolTipTab (this),
|
|
177 new TreeTab (this)
|
|
178 ];
|
|
179 } else { // linux
|
0
|
180 return [ cast(Tab)
|
|
181 new ButtonTab (this),
|
|
182 new CanvasTab (this),
|
|
183 new ComboTab (this),
|
|
184 new CoolBarTab (this),
|
|
185 //new DateTimeTab (this),
|
|
186 new DialogTab (this),
|
|
187 new ExpandBarTab (this),
|
|
188 new GroupTab (this),
|
|
189 new LabelTab (this),
|
|
190 new LinkTab (this),
|
|
191 new ListTab (this),
|
|
192 new MenuTab (this),
|
|
193 new ProgressBarTab (this),
|
|
194 new SashTab (this),
|
|
195 new ScaleTab (this),
|
|
196 shellTab = new ShellTab(this),
|
|
197 new SliderTab (this),
|
|
198 new SpinnerTab (this),
|
|
199 new TabFolderTab (this),
|
|
200 new TableTab (this),
|
|
201 new TextTab (this),
|
|
202 new ToolBarTab (this),
|
|
203 new ToolTipTab (this),
|
|
204 new TreeTab (this)
|
|
205 ];
|
3
|
206 }
|
0
|
207 }
|
|
208
|
|
209 /**
|
|
210 * Disposes of all resources associated with a particular
|
|
211 * instance of the ControlExample.
|
|
212 */
|
|
213 public void dispose() {
|
|
214 /*
|
|
215 * Destroy any shells that may have been created
|
|
216 * by the Shells tab. When a shell is disposed,
|
|
217 * all child shells are also disposed. Therefore
|
|
218 * it is necessary to check for disposed shells
|
|
219 * in the shells list to avoid disposing a shell
|
|
220 * twice.
|
|
221 */
|
|
222 if (shellTab !is null) shellTab.closeAllShells ();
|
|
223 shellTab = null;
|
|
224 tabFolder = null;
|
|
225 freeResources();
|
|
226 }
|
|
227
|
|
228 /**
|
|
229 * Frees the resources
|
|
230 */
|
|
231 void freeResources() {
|
|
232 if (images !is null) {
|
|
233 for (int i = 0; i < images.length; ++i) {
|
|
234 final Image image = images[i];
|
|
235 if (image !is null) image.dispose();
|
|
236 }
|
|
237 images = null;
|
|
238 }
|
|
239 }
|
|
240
|
|
241 /**
|
|
242 * Gets a string from the resource bundle.
|
|
243 * We don't want to crash because of a missing String.
|
|
244 * Returns the key if not found.
|
|
245 */
|
|
246 static char[] getResourceString(char[] key) {
|
|
247 char[] res;
|
|
248 try {
|
|
249 res = resourceBundle.getString(key);
|
|
250 } catch (NoSuchElementException e) {
|
|
251 return key;
|
|
252 }
|
|
253 if( res is null ){
|
|
254 return "!" ~ key ~ "!"; //$NON-NLS-1$ //$NON-NLS-2$
|
|
255 }
|
|
256 return res;
|
|
257 }
|
|
258
|
|
259 // /**
|
|
260 // * Gets a string from the resource bundle and binds it
|
|
261 // * with the given arguments. If the key is not found,
|
|
262 // * return the key.
|
|
263 // */
|
|
264 // static char[] getResourceString(char[] key, Object[] args) {
|
|
265 // char[] res;
|
|
266 // try {
|
|
267 // res = Format(getResourceString(key), args);
|
|
268 // } catch (NoSuchElementException e) {
|
|
269 // return key;
|
|
270 // }
|
|
271 // if( res is null ){
|
|
272 // return "!" ~ key ~ "!"; //$NON-NLS-1$ //$NON-NLS-2$
|
|
273 // }
|
|
274 // return res;
|
|
275 // }
|
|
276
|
|
277 /**
|
|
278 * Loads the resources
|
|
279 */
|
|
280 void initResources() {
|
|
281 //final Class clazz = ControlExample.class;
|
|
282 if (resourceBundle !is null) {
|
|
283 try {
|
|
284 if (images is null) {
|
|
285 images = new Image[imageData.length];
|
|
286
|
|
287 for (int i = 0; i < imageData.length; ++i) {
|
|
288 InputStream sourceStream = new ByteArrayInputStream( imageData[i] );
|
|
289 ImageData source = new ImageData(sourceStream);
|
|
290 if (imageTypes[i] is DWT.ICON) {
|
|
291 ImageData mask = source.getTransparencyMask();
|
|
292 images[i] = new Image(null, source, mask);
|
|
293 } else {
|
|
294 images[i] = new Image(null, source);
|
|
295 }
|
|
296 try {
|
|
297 sourceStream.close();
|
|
298 } catch (IOException e) {
|
|
299 Stderr.formatln( "Stacktrace: {}", e.toString );
|
|
300 }
|
|
301 }
|
|
302 }
|
|
303 return;
|
3
|
304 } catch (Exception t) {
|
0
|
305 Stdout.formatln( "ups {}", t );
|
|
306 }
|
|
307 }
|
|
308 char[] error = (resourceBundle !is null) ?
|
|
309 getResourceString("error.CouldNotLoadResources") :
|
|
310 "Unable to load resources"; //$NON-NLS-1$
|
|
311 freeResources();
|
3
|
312 throw new Exception(error);
|
0
|
313 }
|
|
314
|
|
315 /**
|
|
316 * Invokes as a standalone program.
|
|
317 */
|
|
318 public static void main(char[][] args) {
|
|
319 Display display = new Display();
|
|
320 Shell shell = new Shell(display, DWT.SHELL_TRIM);
|
|
321 shell.setLayout(new FillLayout());
|
|
322 ControlExample instance = new ControlExample(shell);
|
|
323 shell.setText(getResourceString("window.title"));
|
|
324 setShellSize(instance, shell);
|
|
325 shell.open();
|
|
326 while (! shell.isDisposed()) {
|
|
327 if (! display.readAndDispatch()) display.sleep();
|
|
328 }
|
|
329 instance.dispose();
|
|
330 display.dispose();
|
|
331 }
|
|
332
|
|
333 /**
|
|
334 * Grabs input focus.
|
|
335 */
|
|
336 public void setFocus() {
|
|
337 tabFolder.setFocus();
|
|
338 }
|
|
339
|
|
340 /**
|
|
341 * Sets the size of the shell to it's "packed" size,
|
|
342 * unless that makes it larger than the monitor it is being displayed on,
|
|
343 * in which case just set the shell size to be slightly smaller than the monitor.
|
|
344 */
|
|
345 static void setShellSize(ControlExample instance, Shell shell) {
|
|
346 Point size = shell.computeSize(DWT.DEFAULT, DWT.DEFAULT);
|
|
347 Rectangle monitorArea = shell.getMonitor().getClientArea();
|
|
348 /* Workaround: if the tab folder is wider than the screen,
|
|
349 * carbon clips instead of somehow scrolling the tab items.
|
|
350 * We try to recover some width by using shorter tab names. */
|
|
351 if (size.x > monitorArea.width && DWT.getPlatform()=="carbon") {
|
|
352 TabItem [] tabItems = instance.tabFolder.getItems();
|
|
353 for (int i=0; i<tabItems.length; i++) {
|
|
354 tabItems[i].setText (instance.tabs [i].getShortTabText ());
|
|
355 }
|
|
356 size = shell.computeSize(DWT.DEFAULT, DWT.DEFAULT);
|
|
357 }
|
|
358 shell.setSize(Math.min(size.x, monitorArea.width), Math.min(size.y, monitorArea.height));
|
|
359 }
|
|
360 }
|
|
361
|