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