Mercurial > projects > dwt-samples
annotate examples/controlexample/CTabFolderTab.d @ 161:eb84f9418bbf
Change module names to this not have examples in the FQN.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 01 Sep 2008 22:24:27 +0200 |
parents | 4a04b6759f98 |
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:
78
diff
changeset
|
13 module controlexample.CTabFolderTab; |
78 | 14 |
15 | |
16 | |
17 import dwt.DWT; | |
18 import dwt.custom.CTabFolder; | |
19 import dwt.custom.CTabFolder2Adapter; | |
20 import dwt.custom.CTabFolderEvent; | |
21 import dwt.custom.CTabItem; | |
22 import dwt.events.DisposeEvent; | |
23 import dwt.events.DisposeListener; | |
24 import dwt.events.SelectionAdapter; | |
25 import dwt.events.SelectionEvent; | |
26 import dwt.graphics.Color; | |
27 import dwt.graphics.Font; | |
28 import dwt.graphics.FontData; | |
29 import dwt.graphics.Image; | |
30 import dwt.graphics.RGB; | |
31 import dwt.layout.GridData; | |
32 import dwt.layout.GridLayout; | |
33 import dwt.widgets.Button; | |
34 import dwt.widgets.Event; | |
35 import dwt.widgets.Group; | |
36 import dwt.widgets.Item; | |
37 import dwt.widgets.Listener; | |
38 import dwt.widgets.TableItem; | |
39 import dwt.widgets.Text; | |
40 import dwt.widgets.Widget; | |
41 | |
161
eb84f9418bbf
Change module names to this not have examples in the FQN.
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
42 import controlexample.Tab; |
eb84f9418bbf
Change module names to this not have examples in the FQN.
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
43 import controlexample.ControlExample; |
78 | 44 |
45 import tango.text.convert.Format; | |
46 | |
47 class CTabFolderTab : Tab { | |
48 int lastSelectedTab = 0; | |
49 | |
50 /* Example widgets and groups that contain them */ | |
51 CTabFolder tabFolder1; | |
52 Group tabFolderGroup, itemGroup; | |
53 | |
54 /* Style widgets added to the "Style" group */ | |
55 Button topButton, bottomButton, flatButton, closeButton; | |
56 | |
57 static char[] [] CTabItems1; | |
58 | |
59 /* Controls and resources added to the "Fonts" group */ | |
60 static const int SELECTION_FOREGROUND_COLOR = 3; | |
61 static const int SELECTION_BACKGROUND_COLOR = 4; | |
62 static const int ITEM_FONT = 5; | |
63 Color selectionForegroundColor, selectionBackgroundColor; | |
64 Font itemFont; | |
65 | |
66 /* Other widgets added to the "Other" group */ | |
67 Button simpleTabButton, singleTabButton, imageButton, showMinButton, showMaxButton, unselectedCloseButton, unselectedImageButton; | |
68 | |
69 /** | |
70 * Creates the Tab within a given instance of ControlExample. | |
71 */ | |
72 this(ControlExample instance) { | |
73 super(instance); | |
74 if( CTabItems1 is null ){ | |
75 CTabItems1 = [ | |
76 ControlExample.getResourceString("CTabItem1_0"), | |
77 ControlExample.getResourceString("CTabItem1_1"), | |
78 ControlExample.getResourceString("CTabItem1_2")]; | |
79 } | |
80 } | |
81 | |
82 /** | |
83 * Creates the "Colors and Fonts" group. | |
84 */ | |
85 void createColorAndFontGroup () { | |
86 super.createColorAndFontGroup(); | |
87 | |
88 TableItem item = new TableItem(colorAndFontTable, DWT.None); | |
89 item.setText(ControlExample.getResourceString ("Selection_Foreground_Color")); | |
90 item = new TableItem(colorAndFontTable, DWT.None); | |
91 item.setText(ControlExample.getResourceString ("Selection_Background_Color")); | |
92 item = new TableItem(colorAndFontTable, DWT.None); | |
93 item.setText(ControlExample.getResourceString ("Item_Font")); | |
94 | |
95 shell.addDisposeListener(new class() DisposeListener { | |
96 public void widgetDisposed(DisposeEvent event) { | |
97 if (selectionBackgroundColor !is null) selectionBackgroundColor.dispose(); | |
98 if (selectionForegroundColor !is null) selectionForegroundColor.dispose(); | |
99 if (itemFont !is null) itemFont.dispose(); | |
100 selectionBackgroundColor = null; | |
101 selectionForegroundColor = null; | |
102 itemFont = null; | |
103 } | |
104 }); | |
105 } | |
106 | |
107 void changeFontOrColor(int index) { | |
108 switch (index) { | |
109 case SELECTION_FOREGROUND_COLOR: { | |
110 Color oldColor = selectionForegroundColor; | |
111 if (oldColor is null) oldColor = tabFolder1.getSelectionForeground(); | |
112 colorDialog.setRGB(oldColor.getRGB()); | |
113 RGB rgb = colorDialog.open(); | |
114 if (rgb is null) return; | |
115 oldColor = selectionForegroundColor; | |
116 selectionForegroundColor = new Color (display, rgb); | |
117 setSelectionForeground (); | |
118 if (oldColor !is null) oldColor.dispose (); | |
119 } | |
120 break; | |
121 case SELECTION_BACKGROUND_COLOR: { | |
122 Color oldColor = selectionBackgroundColor; | |
123 if (oldColor is null) oldColor = tabFolder1.getSelectionBackground(); | |
124 colorDialog.setRGB(oldColor.getRGB()); | |
125 RGB rgb = colorDialog.open(); | |
126 if (rgb is null) return; | |
127 oldColor = selectionBackgroundColor; | |
128 selectionBackgroundColor = new Color (display, rgb); | |
129 setSelectionBackground (); | |
130 if (oldColor !is null) oldColor.dispose (); | |
131 } | |
132 break; | |
133 case ITEM_FONT: { | |
134 Font oldFont = itemFont; | |
135 if (oldFont is null) oldFont = tabFolder1.getItem (0).getFont (); | |
136 fontDialog.setFontList(oldFont.getFontData()); | |
137 FontData fontData = fontDialog.open (); | |
138 if (fontData is null) return; | |
139 oldFont = itemFont; | |
140 itemFont = new Font (display, fontData); | |
141 setItemFont (); | |
142 setExampleWidgetSize (); | |
143 if (oldFont !is null) oldFont.dispose (); | |
144 } | |
145 break; | |
146 default: | |
147 super.changeFontOrColor(index); | |
148 } | |
149 } | |
150 | |
151 /** | |
152 * Creates the "Other" group. | |
153 */ | |
154 void createOtherGroup () { | |
155 super.createOtherGroup (); | |
156 | |
157 /* Create display controls specific to this example */ | |
158 simpleTabButton = new Button (otherGroup, DWT.CHECK); | |
159 simpleTabButton.setText (ControlExample.getResourceString("Set_Simple_Tabs")); | |
160 simpleTabButton.setSelection(true); | |
161 simpleTabButton.addSelectionListener (new class() SelectionAdapter { | |
162 public void widgetSelected (SelectionEvent event) { | |
163 setSimpleTabs(); | |
164 } | |
165 }); | |
166 | |
167 singleTabButton = new Button (otherGroup, DWT.CHECK); | |
168 singleTabButton.setText (ControlExample.getResourceString("Set_Single_Tabs")); | |
169 singleTabButton.setSelection(false); | |
170 singleTabButton.addSelectionListener (new class() SelectionAdapter { | |
171 public void widgetSelected (SelectionEvent event) { | |
172 setSingleTabs(); | |
173 } | |
174 }); | |
175 | |
176 showMinButton = new Button (otherGroup, DWT.CHECK); | |
177 showMinButton.setText (ControlExample.getResourceString("Set_Min_Visible")); | |
178 showMinButton.setSelection(false); | |
179 showMinButton.addSelectionListener (new class() SelectionAdapter { | |
180 public void widgetSelected (SelectionEvent event) { | |
181 setMinimizeVisible(); | |
182 } | |
183 }); | |
184 | |
185 showMaxButton = new Button (otherGroup, DWT.CHECK); | |
186 showMaxButton.setText (ControlExample.getResourceString("Set_Max_Visible")); | |
187 showMaxButton.setSelection(false); | |
188 showMaxButton.addSelectionListener (new class() SelectionAdapter { | |
189 public void widgetSelected (SelectionEvent event) { | |
190 setMaximizeVisible(); | |
191 } | |
192 }); | |
193 | |
194 imageButton = new Button (otherGroup, DWT.CHECK); | |
195 imageButton.setText (ControlExample.getResourceString("Set_Image")); | |
196 imageButton.addSelectionListener (new class() SelectionAdapter { | |
197 public void widgetSelected (SelectionEvent event) { | |
198 setImages(); | |
199 } | |
200 }); | |
201 | |
202 unselectedImageButton = new Button (otherGroup, DWT.CHECK); | |
203 unselectedImageButton.setText (ControlExample.getResourceString("Set_Unselected_Image_Visible")); | |
204 unselectedImageButton.setSelection(true); | |
205 unselectedImageButton.addSelectionListener (new class() SelectionAdapter { | |
206 public void widgetSelected (SelectionEvent event) { | |
207 setUnselectedImageVisible(); | |
208 } | |
209 }); | |
210 unselectedCloseButton = new Button (otherGroup, DWT.CHECK); | |
211 unselectedCloseButton.setText (ControlExample.getResourceString("Set_Unselected_Close_Visible")); | |
212 unselectedCloseButton.setSelection(true); | |
213 unselectedCloseButton.addSelectionListener (new class() SelectionAdapter { | |
214 public void widgetSelected (SelectionEvent event) { | |
215 setUnselectedCloseVisible(); | |
216 } | |
217 }); | |
218 } | |
219 | |
220 /** | |
221 * Creates the "Example" group. | |
222 */ | |
223 void createExampleGroup () { | |
224 super.createExampleGroup (); | |
225 | |
226 /* Create a group for the CTabFolder */ | |
227 tabFolderGroup = new Group (exampleGroup, DWT.NONE); | |
228 tabFolderGroup.setLayout (new GridLayout ()); | |
229 tabFolderGroup.setLayoutData (new GridData (DWT.FILL, DWT.FILL, true, true)); | |
230 tabFolderGroup.setText ("CTabFolder"); | |
231 } | |
232 | |
233 /** | |
234 * Creates the "Example" widgets. | |
235 */ | |
236 void createExampleWidgets () { | |
237 | |
238 /* Compute the widget style */ | |
239 int style = getDefaultStyle(); | |
240 if (topButton.getSelection ()) style |= DWT.TOP; | |
241 if (bottomButton.getSelection ()) style |= DWT.BOTTOM; | |
242 if (borderButton.getSelection ()) style |= DWT.BORDER; | |
243 if (flatButton.getSelection ()) style |= DWT.FLAT; | |
244 if (closeButton.getSelection ()) style |= DWT.CLOSE; | |
245 | |
246 /* Create the example widgets */ | |
247 tabFolder1 = new CTabFolder (tabFolderGroup, style); | |
248 for (int i = 0; i < CTabItems1.length; i++) { | |
249 CTabItem item = new CTabItem(tabFolder1, DWT.NONE); | |
250 item.setText(CTabItems1[i]); | |
251 Text text = new Text(tabFolder1, DWT.READ_ONLY); | |
252 text.setText(Format( "{}: {}", ControlExample.getResourceString("CTabItem_content"), i)); | |
253 item.setControl(text); | |
254 } | |
255 tabFolder1.addListener(DWT.Selection, new class() Listener { | |
256 public void handleEvent(Event event) { | |
257 lastSelectedTab = tabFolder1.getSelectionIndex(); | |
258 } | |
259 }); | |
260 tabFolder1.setSelection(lastSelectedTab); | |
261 } | |
262 | |
263 /** | |
264 * Creates the "Style" group. | |
265 */ | |
266 void createStyleGroup() { | |
267 super.createStyleGroup (); | |
268 | |
269 /* Create the extra widgets */ | |
270 topButton = new Button (styleGroup, DWT.RADIO); | |
271 topButton.setText ("DWT.TOP"); | |
272 topButton.setSelection(true); | |
273 bottomButton = new Button (styleGroup, DWT.RADIO); | |
274 bottomButton.setText ("DWT.BOTTOM"); | |
275 borderButton = new Button (styleGroup, DWT.CHECK); | |
276 borderButton.setText ("DWT.BORDER"); | |
277 flatButton = new Button (styleGroup, DWT.CHECK); | |
278 flatButton.setText ("DWT.FLAT"); | |
279 closeButton = new Button (styleGroup, DWT.CHECK); | |
280 closeButton.setText ("DWT.CLOSE"); | |
281 } | |
282 | |
283 /** | |
284 * Gets the list of custom event names. | |
285 * | |
286 * @return an array containing custom event names | |
287 */ | |
288 char[] [] getCustomEventNames () { | |
289 return ["CTabFolderEvent"]; | |
290 } | |
291 | |
292 /** | |
293 * Gets the "Example" widget children's items, if any. | |
294 * | |
295 * @return an array containing the example widget children's items | |
296 */ | |
297 Item [] getExampleWidgetItems () { | |
298 return tabFolder1.getItems(); | |
299 } | |
300 | |
301 /** | |
302 * Gets the "Example" widget children. | |
303 */ | |
304 Widget [] getExampleWidgets () { | |
305 return [ cast(Widget) tabFolder1]; | |
306 } | |
307 | |
308 /** | |
309 * Gets the text for the tab folder item. | |
310 */ | |
311 char[] getTabText () { | |
312 return "CTabFolder"; | |
313 } | |
314 | |
315 /** | |
316 * Hooks the custom listener specified by eventName. | |
317 */ | |
318 void hookCustomListener (char[] eventName) { | |
319 if (eventName is "CTabFolderEvent") { | |
320 tabFolder1.addCTabFolder2Listener (new class(eventName) CTabFolder2Adapter { | |
321 char[] name; | |
322 this( char[] name ){ this.name = name; } | |
323 public void close (CTabFolderEvent event) { | |
324 log (name, event); | |
325 } | |
326 }); | |
327 } | |
328 } | |
329 | |
330 /** | |
331 * Sets the foreground color, background color, and font | |
332 * of the "Example" widgets to their default settings. | |
333 * Also sets foreground and background color of the Node 1 | |
334 * TreeItems to default settings. | |
335 */ | |
336 void resetColorsAndFonts () { | |
337 super.resetColorsAndFonts (); | |
338 Color oldColor = selectionForegroundColor; | |
339 selectionForegroundColor = null; | |
340 setSelectionForeground (); | |
341 if (oldColor !is null) oldColor.dispose(); | |
342 oldColor = selectionBackgroundColor; | |
343 selectionBackgroundColor = null; | |
344 setSelectionBackground (); | |
345 if (oldColor !is null) oldColor.dispose(); | |
346 Font oldFont = itemFont; | |
347 itemFont = null; | |
348 setItemFont (); | |
349 if (oldFont !is null) oldFont.dispose(); | |
350 } | |
351 | |
352 /** | |
353 * Sets the state of the "Example" widgets. | |
354 */ | |
355 void setExampleWidgetState () { | |
356 super.setExampleWidgetState(); | |
357 setSimpleTabs(); | |
358 setSingleTabs(); | |
359 setImages(); | |
360 setMinimizeVisible(); | |
361 setMaximizeVisible(); | |
362 setUnselectedCloseVisible(); | |
363 setUnselectedImageVisible(); | |
364 setSelectionBackground (); | |
365 setSelectionForeground (); | |
366 setItemFont (); | |
367 setExampleWidgetSize(); | |
368 } | |
369 | |
370 /** | |
371 * Sets the shape that the CTabFolder will use to render itself. | |
372 */ | |
373 void setSimpleTabs () { | |
374 tabFolder1.setSimple (simpleTabButton.getSelection ()); | |
375 setExampleWidgetSize(); | |
376 } | |
377 | |
378 /** | |
379 * Sets the number of tabs that the CTabFolder should display. | |
380 */ | |
381 void setSingleTabs () { | |
382 tabFolder1.setSingle (singleTabButton.getSelection ()); | |
383 setExampleWidgetSize(); | |
384 } | |
385 /** | |
386 * Sets an image into each item of the "Example" widgets. | |
387 */ | |
388 void setImages () { | |
389 bool setImage = imageButton.getSelection (); | |
390 CTabItem items[] = tabFolder1.getItems (); | |
391 for (int i = 0; i < items.length; i++) { | |
392 if (setImage) { | |
393 items[i].setImage (instance.images[ControlExample.ciClosedFolder]); | |
394 } else { | |
395 items[i].setImage (null); | |
396 } | |
397 } | |
398 setExampleWidgetSize (); | |
399 } | |
400 /** | |
401 * Sets the visibility of the minimize button | |
402 */ | |
403 void setMinimizeVisible () { | |
404 tabFolder1.setMinimizeVisible(showMinButton.getSelection ()); | |
405 setExampleWidgetSize(); | |
406 } | |
407 /** | |
408 * Sets the visibility of the maximize button | |
409 */ | |
410 void setMaximizeVisible () { | |
411 tabFolder1.setMaximizeVisible(showMaxButton.getSelection ()); | |
412 setExampleWidgetSize(); | |
413 } | |
414 /** | |
415 * Sets the visibility of the close button on unselected tabs | |
416 */ | |
417 void setUnselectedCloseVisible () { | |
418 tabFolder1.setUnselectedCloseVisible(unselectedCloseButton.getSelection ()); | |
419 setExampleWidgetSize(); | |
420 } | |
421 /** | |
422 * Sets the visibility of the image on unselected tabs | |
423 */ | |
424 void setUnselectedImageVisible () { | |
425 tabFolder1.setUnselectedImageVisible(unselectedImageButton.getSelection ()); | |
426 setExampleWidgetSize(); | |
427 } | |
428 /** | |
429 * Sets the background color of CTabItem 0. | |
430 */ | |
431 void setSelectionBackground () { | |
432 if (!instance.startup) { | |
433 tabFolder1.setSelectionBackground(selectionBackgroundColor); | |
434 } | |
435 // Set the selection background item's image to match the background color of the selection. | |
436 Color color = selectionBackgroundColor; | |
437 if (color is null) color = tabFolder1.getSelectionBackground (); | |
438 TableItem item = colorAndFontTable.getItem(SELECTION_BACKGROUND_COLOR); | |
439 Image oldImage = item.getImage(); | |
440 if (oldImage !is null) oldImage.dispose(); | |
441 item.setImage (colorImage(color)); | |
442 } | |
443 | |
444 /** | |
445 * Sets the foreground color of CTabItem 0. | |
446 */ | |
447 void setSelectionForeground () { | |
448 if (!instance.startup) { | |
449 tabFolder1.setSelectionForeground(selectionForegroundColor); | |
450 } | |
451 // Set the selection foreground item's image to match the foreground color of the selection. | |
452 Color color = selectionForegroundColor; | |
453 if (color is null) color = tabFolder1.getSelectionForeground (); | |
454 TableItem item = colorAndFontTable.getItem(SELECTION_FOREGROUND_COLOR); | |
455 Image oldImage = item.getImage(); | |
456 if (oldImage !is null) oldImage.dispose(); | |
457 item.setImage (colorImage(color)); | |
458 } | |
459 | |
460 /** | |
461 * Sets the font of CTabItem 0. | |
462 */ | |
463 void setItemFont () { | |
464 if (!instance.startup) { | |
465 tabFolder1.getItem (0).setFont (itemFont); | |
466 setExampleWidgetSize(); | |
467 } | |
468 /* Set the font item's image to match the font of the item. */ | |
469 Font ft = itemFont; | |
470 if (ft is null) ft = tabFolder1.getItem (0).getFont (); | |
471 TableItem item = colorAndFontTable.getItem(ITEM_FONT); | |
472 Image oldImage = item.getImage(); | |
473 if (oldImage !is null) oldImage.dispose(); | |
474 item.setImage (fontImage(ft)); | |
475 item.setFont(ft); | |
476 colorAndFontTable.layout (); | |
477 } | |
478 } |