Mercurial > projects > dwt-win
annotate dwt/custom/StackLayout.d @ 212:ab60f3309436
reverted the char[] to String and use the an alias.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 05 May 2008 00:12:38 +0200 |
parents | a5afe31f5cdd |
children | fd9c62a2998e |
rev | line source |
---|---|
155 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2006 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 dwt.custom.StackLayout; | |
14 | |
15 | |
16 | |
17 import dwt.DWT; | |
18 import dwt.graphics.Point; | |
19 import dwt.graphics.Rectangle; | |
20 import dwt.widgets.Composite; | |
21 import dwt.widgets.Control; | |
22 import dwt.widgets.Layout; | |
23 | |
24 import tango.util.Convert; | |
25 static import tango.text.Util; | |
26 import dwt.dwthelper.utils; | |
27 | |
28 /** | |
29 * This Layout stacks all the controls one on top of the other and resizes all controls | |
30 * to have the same size and location. | |
31 * The control specified in topControl is visible and all other controls are not visible. | |
32 * Users must set the topControl value to flip between the visible items and then call | |
33 * layout() on the composite which has the StackLayout. | |
34 * | |
35 * <p> Here is an example which places ten buttons in a stack layout and | |
36 * flips between them: | |
37 * | |
38 * <pre><code> | |
39 * public static void main(String[] args) { | |
40 * Display display = new Display(); | |
41 * Shell shell = new Shell(display); | |
42 * shell.setLayout(new GridLayout()); | |
43 * | |
44 * final Composite parent = new Composite(shell, DWT.NONE); | |
45 * parent.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
46 * final StackLayout layout = new StackLayout(); | |
47 * parent.setLayout(layout); | |
48 * final Button[] bArray = new Button[10]; | |
49 * for (int i = 0; i < 10; i++) { | |
50 * bArray[i] = new Button(parent, DWT.PUSH); | |
51 * bArray[i].setText("Button "+i); | |
52 * } | |
53 * layout.topControl = bArray[0]; | |
54 * | |
55 * Button b = new Button(shell, DWT.PUSH); | |
56 * b.setText("Show Next Button"); | |
57 * final int[] index = new int[1]; | |
58 * b.addListener(DWT.Selection, new Listener(){ | |
59 * public void handleEvent(Event e) { | |
60 * index[0] = (index[0] + 1) % 10; | |
61 * layout.topControl = bArray[index[0]]; | |
62 * parent.layout(); | |
63 * } | |
64 * }); | |
65 * | |
66 * shell.open(); | |
67 * while (shell !is null && !shell.isDisposed()) { | |
68 * if (!display.readAndDispatch()) | |
69 * display.sleep(); | |
70 * } | |
71 * } | |
72 * </code></pre> | |
73 */ | |
74 | |
75 public class StackLayout : Layout { | |
76 | |
77 /** | |
78 * marginWidth specifies the number of pixels of horizontal margin | |
79 * that will be placed along the left and right edges of the layout. | |
80 * | |
81 * The default value is 0. | |
82 */ | |
83 public int marginWidth = 0; | |
84 /** | |
85 * marginHeight specifies the number of pixels of vertical margin | |
86 * that will be placed along the top and bottom edges of the layout. | |
87 * | |
88 * The default value is 0. | |
89 */ | |
90 public int marginHeight = 0; | |
91 | |
92 /** | |
93 * topControl the Control that is displayed at the top of the stack. | |
94 * All other controls that are children of the parent composite will not be visible. | |
95 */ | |
96 public Control topControl; | |
97 | |
98 protected override Point computeSize(Composite composite, int wHint, int hHint, bool flushCache) { | |
99 Control children[] = composite.getChildren(); | |
100 int maxWidth = 0; | |
101 int maxHeight = 0; | |
102 for (int i = 0; i < children.length; i++) { | |
103 Point size = children[i].computeSize(wHint, hHint, flushCache); | |
104 maxWidth = Math.max(size.x, maxWidth); | |
105 maxHeight = Math.max(size.y, maxHeight); | |
106 } | |
107 int width = maxWidth + 2 * marginWidth; | |
108 int height = maxHeight + 2 * marginHeight; | |
109 if (wHint !is DWT.DEFAULT) width = wHint; | |
110 if (hHint !is DWT.DEFAULT) height = hHint; | |
111 return new Point(width, height); | |
112 } | |
113 | |
114 protected override bool flushCache(Control control) { | |
115 return true; | |
116 } | |
117 | |
118 protected override void layout(Composite composite, bool flushCache) { | |
119 Control children[] = composite.getChildren(); | |
120 Rectangle rect = composite.getClientArea(); | |
121 rect.x += marginWidth; | |
122 rect.y += marginHeight; | |
123 rect.width -= 2 * marginWidth; | |
124 rect.height -= 2 * marginHeight; | |
125 for (int i = 0; i < children.length; i++) { | |
126 children[i].setBounds(rect); | |
127 children[i].setVisible(children[i] is topControl); | |
128 | |
129 } | |
130 } | |
131 | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
132 String getName () { |
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
133 String string = this.classinfo.name; |
155 | 134 int index = tango.text.Util.locatePrior( string ,'.'); |
135 if (index is string.length ) return string; | |
136 return string[ index + 1 .. $ ]; | |
137 } | |
138 | |
139 /** | |
140 * Returns a string containing a concise, human-readable | |
141 * description of the receiver. | |
142 * | |
143 * @return a string representation of the layout | |
144 */ | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
145 public override String toString () { |
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
146 String string = getName ()~" {"; |
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
147 if (marginWidth !is 0) string ~= "marginWidth="~to!(String)(marginWidth)~" "; |
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
148 if (marginHeight !is 0) string ~= "marginHeight="~to!(String)(marginHeight)~" "; |
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
149 if (topControl !is null) string ~= "topControl="~to!(String)(topControl)~" "; |
155 | 150 string = tango.text.Util.trim(string); |
151 string ~= "}"; | |
152 return string; | |
153 } | |
154 } |