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