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