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