comparison examples/controlexample/ControlExample.d @ 78:4a04b6759f98

Clean up directory names
author John Reimer <terminal.node@gmail.com>
date Sat, 10 May 2008 13:32:45 -0700
parents
children 8a1930f94cbb
comparison
equal deleted inserted replaced
76:04f122e90b0a 78:4a04b6759f98
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 *******************************************************************************/
13 module examples.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 examples.controlexample.Tab;
30 import examples.controlexample.ButtonTab;
31 import examples.controlexample.CanvasTab;
32 import examples.controlexample.ComboTab;
33 import examples.controlexample.CoolBarTab;
34 import examples.controlexample.DateTimeTab;
35 import examples.controlexample.DialogTab;
36 import examples.controlexample.ExpandBarTab;
37 import examples.controlexample.GroupTab;
38 import examples.controlexample.LabelTab;
39 import examples.controlexample.LinkTab;
40 import examples.controlexample.ListTab;
41 import examples.controlexample.MenuTab;
42 import examples.controlexample.ProgressBarTab;
43 import examples.controlexample.SashTab;
44 import examples.controlexample.ScaleTab;
45 import examples.controlexample.ShellTab;
46 import examples.controlexample.SliderTab;
47 import examples.controlexample.SpinnerTab;
48 import examples.controlexample.TabFolderTab;
49 import examples.controlexample.TableTab;
50 import examples.controlexample.TextTab;
51 import examples.controlexample.ToolBarTab;
52 import examples.controlexample.ToolTipTab;
53 import examples.controlexample.TreeTab;
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;
76 if( auto factory = ClassInfo.find( "dwtexamples.controlexample.CustomControlExample.CustomControlExampleFactory" )){
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( "bug: ProgressBarTab crash on vertical" );
111 Stdout.formatln( " in java it behaves the same" );
112 Stdout.formatln( "bug: SliderTab horizontal arrow buttons are too high." );
113 Stdout.formatln( " in java it behaves the same" );
114 Stdout.formatln( " Known bug:" );
115 Stdout.formatln( " https://bugs.eclipse.org/bugs/show_bug.cgi?id=197402" );
116 Stdout.formatln( " http://bugzilla.gnome.org/show_bug.cgi?id=475909" );
117 Stdout.formatln( "" );
118 }
119 Stdout.formatln( "please report problems" );
120 auto res = new ControlExample( shell );
121 shell.setText(ControlExample.getResourceString("window.title"));
122 return res;
123 }
124 }
125
126 public class ControlExample {
127 private static ResourceBundle resourceBundle;
128 private static const char[] resourceData = import( "dwtexamples.controlexample.controlexample.properties" );
129
130 private ShellTab shellTab;
131 private TabFolder tabFolder;
132 private Tab [] tabs;
133 Image images[];
134
135 static const int ciClosedFolder = 0, ciOpenFolder = 1, ciTarget = 2, ciBackground = 3, ciParentBackground = 4;
136
137 static const byte[][] imageData = [
138 cast(byte[]) import( "dwtexamples.controlexample.closedFolder.gif" ),
139 cast(byte[]) import( "dwtexamples.controlexample.openFolder.gif" ),
140 cast(byte[]) import( "dwtexamples.controlexample.target.gif" ),
141 cast(byte[]) import( "dwtexamples.controlexample.backgroundImage.png" ),
142 cast(byte[]) import( "dwtexamples.controlexample.parentBackgroundImage.png" )
143 ];
144 static const int[] imageTypes = [
145 DWT.ICON,
146 DWT.ICON,
147 DWT.ICON,
148 DWT.BITMAP,
149 DWT.BITMAP];
150
151 bool startup = true;
152
153 static this(){
154 resourceBundle = ResourceBundle.getBundleFromData( resourceData ); //$NON-NLS-1$
155 }
156
157 /**
158 * Creates an instance of a ControlExample embedded inside
159 * the supplied parent Composite.
160 *
161 * @param parent the container of the example
162 */
163 public this(Composite parent) {
164 initResources();
165 tabFolder = new TabFolder (parent, DWT.NONE);
166 tabs = createTabs();
167 for (int i=0; i<tabs.length; i++) {
168 TabItem item = new TabItem (tabFolder, DWT.NONE);
169 item.setText (tabs [i].getTabText ());
170 item.setControl (tabs [i].createTabFolderPage (tabFolder));
171 item.setData (tabs [i]);
172 }
173 startup = false;
174 }
175
176 /**
177 * Answers the set of example Tabs
178 */
179 Tab[] createTabs() {
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 ];
206 }
207
208 /**
209 * Disposes of all resources associated with a particular
210 * instance of the ControlExample.
211 */
212 public void dispose() {
213 /*
214 * Destroy any shells that may have been created
215 * by the Shells tab. When a shell is disposed,
216 * all child shells are also disposed. Therefore
217 * it is necessary to check for disposed shells
218 * in the shells list to avoid disposing a shell
219 * twice.
220 */
221 if (shellTab !is null) shellTab.closeAllShells ();
222 shellTab = null;
223 tabFolder = null;
224 freeResources();
225 }
226
227 /**
228 * Frees the resources
229 */
230 void freeResources() {
231 if (images !is null) {
232 for (int i = 0; i < images.length; ++i) {
233 final Image image = images[i];
234 if (image !is null) image.dispose();
235 }
236 images = null;
237 }
238 }
239
240 /**
241 * Gets a string from the resource bundle.
242 * We don't want to crash because of a missing String.
243 * Returns the key if not found.
244 */
245 static char[] getResourceString(char[] key) {
246 char[] res;
247 try {
248 res = resourceBundle.getString(key);
249 } catch (MissingResourceException e) {
250 return key;
251 } catch (NoSuchElementException e) {
252 return key;
253 }
254 if( res is null && !resourceBundle.hasString(key)){
255 return "!" ~ key ~ "!"; //$NON-NLS-1$ //$NON-NLS-2$
256 }
257 return res;
258 }
259
260 // /**
261 // * Gets a string from the resource bundle and binds it
262 // * with the given arguments. If the key is not found,
263 // * return the key.
264 // */
265 // static char[] getResourceString(char[] key, Object[] args) {
266 // char[] res;
267 // try {
268 // res = Format(getResourceString(key), args);
269 // } catch (NoSuchElementException e) {
270 // return key;
271 // }
272 // if( res is null ){
273 // return "!" ~ key ~ "!"; //$NON-NLS-1$ //$NON-NLS-2$
274 // }
275 // return res;
276 // }
277
278 /**
279 * Loads the resources
280 */
281 void initResources() {
282 //final Class clazz = ControlExample.class;
283 if (resourceBundle !is null) {
284 try {
285 if (images is null) {
286 images = new Image[imageData.length];
287
288 for (int i = 0; i < imageData.length; ++i) {
289 InputStream sourceStream = new ByteArrayInputStream( imageData[i] );
290 ImageData source = new ImageData(sourceStream);
291 if (imageTypes[i] is DWT.ICON) {
292 ImageData mask = source.getTransparencyMask();
293 images[i] = new Image(null, source, mask);
294 } else {
295 images[i] = new Image(null, source);
296 }
297 try {
298 sourceStream.close();
299 } catch (IOException e) {
300 Stderr.formatln( "Stacktrace: {}", e.toString );
301 }
302 }
303 }
304 return;
305 } catch (Exception t) {
306 Stdout.formatln( "ups {}", t );
307 }
308 }
309 char[] error = (resourceBundle !is null) ?
310 getResourceString("error.CouldNotLoadResources") :
311 "Unable to load resources"; //$NON-NLS-1$
312 freeResources();
313 throw new Exception(error);
314 }
315
316 /**
317 * Grabs input focus.
318 */
319 public void setFocus() {
320 tabFolder.setFocus();
321 }
322
323 /**
324 * Sets the size of the shell to it's "packed" size,
325 * unless that makes it larger than the monitor it is being displayed on,
326 * in which case just set the shell size to be slightly smaller than the monitor.
327 */
328 static void setShellSize(ControlExample instance, Shell shell) {
329 Point size = shell.computeSize(DWT.DEFAULT, DWT.DEFAULT);
330 Rectangle monitorArea = shell.getMonitor().getClientArea();
331 /* Workaround: if the tab folder is wider than the screen,
332 * carbon clips instead of somehow scrolling the tab items.
333 * We try to recover some width by using shorter tab names. */
334 if (size.x > monitorArea.width && DWT.getPlatform()=="carbon") {
335 TabItem [] tabItems = instance.tabFolder.getItems();
336 for (int i=0; i<tabItems.length; i++) {
337 tabItems[i].setText (instance.tabs [i].getShortTabText ());
338 }
339 size = shell.computeSize(DWT.DEFAULT, DWT.DEFAULT);
340 }
341 shell.setSize(Math.min(size.x, monitorArea.width), Math.min(size.y, monitorArea.height));
342 }
343 }
344