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 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.graphics.Point; 19 import org.eclipse.swt.graphics.Rectangle; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Control; 22 import org.eclipse.swt.widgets.Layout; 23 24 /** 25 * This Layout stacks all the controls one on top of the other and resizes all controls 26 * to have the same size and location. 27 * The control specified in topControl is visible and all other controls are not visible. 28 * Users must set the topControl value to flip between the visible items and then call 29 * layout() on the composite which has the StackLayout. 30 * 31 * <p> Here is an example which places ten buttons in a stack layout and 32 * flips between them: 33 * 34 * <pre><code> 35 * public static void main(String[] args) { 36 * Display display = new Display(); 37 * Shell shell = new Shell(display); 38 * shell.setLayout(new GridLayout()); 39 * 40 * final Composite parent = new Composite(shell, SWT.NONE); 41 * parent.setLayoutData(new GridData(GridData.FILL_BOTH)); 42 * final StackLayout layout = new StackLayout(); 43 * parent.setLayout(layout); 44 * final Button[] bArray = new Button[10]; 45 * for (int i = 0; i < 10; i++) { 46 * bArray[i] = new Button(parent, SWT.PUSH); 47 * bArray[i].setText("Button "+i); 48 * } 49 * layout.topControl = bArray[0]; 50 * 51 * Button b = new Button(shell, SWT.PUSH); 52 * b.setText("Show Next Button"); 53 * final int[] index = new int[1]; 54 * b.addListener(SWT.Selection, new Listener(){ 55 * public void handleEvent(Event e) { 56 * index[0] = (index[0] + 1) % 10; 57 * layout.topControl = bArray[index[0]]; 58 * parent.layout(); 59 * } 60 * }); 61 * 62 * shell.open(); 63 * while (shell !is null && !shell.isDisposed()) { 64 * if (!display.readAndDispatch()) 65 * display.sleep(); 66 * } 67 * } 68 * </code></pre> 69 * 70 * @see <a href="http://www.eclipse.org/swt/snippets/#stacklayout">StackLayout snippets</a> 71 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: LayoutExample</a> 72 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> 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 SWT.DEFAULT) width = wHint; 110 if (hHint !is SWT.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 132 String getName () { 133 String string = this.classinfo.name; 134 auto index = string.lastIndexOf ('.'); 135 if (index is -1 ) 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 */ 145 public override String toString () { 146 String string = getName ()~" {"; 147 if (marginWidth !is 0) string ~= "marginWidth="~String_valueOf(marginWidth)~" "; 148 if (marginHeight !is 0) string ~= "marginHeight="~String_valueOf(marginHeight)~" "; 149 if (topControl !is null) string ~= "topControl="~String_valueOf(topControl)~" "; 150 string = string.trim(); 151 string ~= "}"; 152 return string; 153 } 154 }