comparison dwt/custom/StackLayout.d @ 41:6337764516f1

Sync dwt/custom with dwt-linux (took copy of complete folder)
author Frank Benoit <benoit@tionex.de>
date Tue, 07 Oct 2008 16:29:55 +0200
parents 380af2bdd8e5
children
comparison
equal deleted inserted replaced
40:fbe68c33eeee 41:6337764516f1
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 IBM Corporation and others. 2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
10 *******************************************************************************/ 12 *******************************************************************************/
11 module dwt.custom; 13 module dwt.custom.StackLayout;
14
15 import dwt.dwthelper.utils;
12 16
13 17
14 import dwt.*; 18
15 import dwt.graphics.*; 19 import dwt.DWT;
16 import dwt.widgets.*; 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;
17 28
18 /** 29 /**
19 * This Layout stacks all the controls one on top of the other and resizes all controls 30 * This Layout stacks all the controls one on top of the other and resizes all controls
20 * to have the same size and location. 31 * to have the same size and location.
21 * The control specified in topControl is visible and all other controls are not visible. 32 * The control specified in topControl is visible and all other controls are not visible.
22 * Users must set the topControl value to flip between the visible items and then call 33 * Users must set the topControl value to flip between the visible items and then call
23 * layout() on the composite which has the StackLayout. 34 * layout() on the composite which has the StackLayout.
24 * 35 *
25 * <p> Here is an example which places ten buttons in a stack layout and 36 * <p> Here is an example which places ten buttons in a stack layout and
26 * flips between them: 37 * flips between them:
27 * 38 *
28 * <pre><code> 39 * <pre><code>
29 * public static void main(String[] args) { 40 * public static void main(String[] args) {
30 * Display display = new Display(); 41 * Display display = new Display();
31 * Shell shell = new Shell(display); 42 * Shell shell = new Shell(display);
32 * shell.setLayout(new GridLayout()); 43 * shell.setLayout(new GridLayout());
33 * 44 *
34 * final Composite parent = new Composite(shell, DWT.NONE); 45 * final Composite parent = new Composite(shell, DWT.NONE);
35 * parent.setLayoutData(new GridData(GridData.FILL_BOTH)); 46 * parent.setLayoutData(new GridData(GridData.FILL_BOTH));
36 * final StackLayout layout = new StackLayout(); 47 * final StackLayout layout = new StackLayout();
37 * parent.setLayout(layout); 48 * parent.setLayout(layout);
38 * final Button[] bArray = new Button[10]; 49 * final Button[] bArray = new Button[10];
39 * for (int i = 0; i &lt; 10; i++) { 50 * for (int i = 0; i &lt; 10; i++) {
40 * bArray[i] = new Button(parent, DWT.PUSH); 51 * bArray[i] = new Button(parent, DWT.PUSH);
41 * bArray[i].setText("Button "+i); 52 * bArray[i].setText("Button "+i);
42 * } 53 * }
43 * layout.topControl = bArray[0]; 54 * layout.topControl = bArray[0];
44 * 55 *
45 * Button b = new Button(shell, DWT.PUSH); 56 * Button b = new Button(shell, DWT.PUSH);
46 * b.setText("Show Next Button"); 57 * b.setText("Show Next Button");
47 * final int[] index = new int[1]; 58 * final int[] index = new int[1];
48 * b.addListener(DWT.Selection, new Listener(){ 59 * b.addListener(DWT.Selection, new Listener(){
49 * public void handleEvent(Event e) { 60 * public void handleEvent(Event e) {
50 * index[0] = (index[0] + 1) % 10; 61 * index[0] = (index[0] + 1) % 10;
51 * layout.topControl = bArray[index[0]]; 62 * layout.topControl = bArray[index[0]];
52 * parent.layout(); 63 * parent.layout();
53 * } 64 * }
54 * }); 65 * });
55 * 66 *
56 * shell.open(); 67 * shell.open();
57 * while (shell !is null && !shell.isDisposed()) { 68 * while (shell !is null && !shell.isDisposed()) {
58 * if (!display.readAndDispatch()) 69 * if (!display.readAndDispatch())
59 * display.sleep(); 70 * display.sleep();
60 * } 71 * }
61 * } 72 * }
62 * </code></pre> 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>
63 */ 78 */
64 79
65 public class StackLayout : Layout { 80 public class StackLayout : Layout {
66 81
67 /** 82 /**
68 * marginWidth specifies the number of pixels of horizontal margin 83 * marginWidth specifies the number of pixels of horizontal margin
69 * that will be placed along the left and right edges of the layout. 84 * that will be placed along the left and right edges of the layout.
70 * 85 *
71 * The default value is 0. 86 * The default value is 0.
83 * topControl the Control that is displayed at the top of the stack. 98 * topControl the Control that is displayed at the top of the stack.
84 * All other controls that are children of the parent composite will not be visible. 99 * All other controls that are children of the parent composite will not be visible.
85 */ 100 */
86 public Control topControl; 101 public Control topControl;
87 102
88 protected Point computeSize(Composite composite, int wHint, int hHint, bool flushCache) { 103 protected override Point computeSize(Composite composite, int wHint, int hHint, bool flushCache) {
89 Control children[] = composite.getChildren(); 104 Control children[] = composite.getChildren();
90 int maxWidth = 0; 105 int maxWidth = 0;
91 int maxHeight = 0; 106 int maxHeight = 0;
92 for (int i = 0; i < children.length; i++) { 107 for (int i = 0; i < children.length; i++) {
93 Point size = children[i].computeSize(wHint, hHint, flushCache); 108 Point size = children[i].computeSize(wHint, hHint, flushCache);
99 if (wHint !is DWT.DEFAULT) width = wHint; 114 if (wHint !is DWT.DEFAULT) width = wHint;
100 if (hHint !is DWT.DEFAULT) height = hHint; 115 if (hHint !is DWT.DEFAULT) height = hHint;
101 return new Point(width, height); 116 return new Point(width, height);
102 } 117 }
103 118
104 protected bool flushCache(Control control) { 119 protected override bool flushCache(Control control) {
105 return true; 120 return true;
106 } 121 }
107 122
108 protected void layout(Composite composite, bool flushCache) { 123 protected override void layout(Composite composite, bool flushCache) {
109 Control children[] = composite.getChildren(); 124 Control children[] = composite.getChildren();
110 Rectangle rect = composite.getClientArea(); 125 Rectangle rect = composite.getClientArea();
111 rect.x += marginWidth; 126 rect.x += marginWidth;
112 rect.y += marginHeight; 127 rect.y += marginHeight;
113 rect.width -= 2 * marginWidth; 128 rect.width -= 2 * marginWidth;
114 rect.height -= 2 * marginHeight; 129 rect.height -= 2 * marginHeight;
115 for (int i = 0; i < children.length; i++) { 130 for (int i = 0; i < children.length; i++) {
116 children[i].setBounds(rect); 131 children[i].setBounds(rect);
117 children[i].setVisible(children[i] is topControl); 132 children[i].setVisible(children[i] is topControl);
118 133
119 } 134 }
120 } 135 }
121 136
122 String getName () { 137 String getName () {
123 String String = getClass ().getName (); 138 String string = this.classinfo.name;
124 int index = String.lastIndexOf ('.'); 139 int index = tango.text.Util.locatePrior( string ,'.');
125 if (index is -1) return String; 140 if (index is string.length ) return string;
126 return String.substring (index + 1, String.length ()); 141 return string[ index + 1 .. $ ];
127 } 142 }
128 143
129 /** 144 /**
130 * Returns a String containing a concise, human-readable 145 * Returns a string containing a concise, human-readable
131 * description of the receiver. 146 * description of the receiver.
132 * 147 *
133 * @return a String representation of the layout 148 * @return a string representation of the layout
134 */ 149 */
135 public String toString () { 150 public override String toString () {
136 String String = getName ()+" {"; 151 String string = getName ()~" {";
137 if (marginWidth !is 0) String += "marginWidth="+marginWidth+" "; 152 if (marginWidth !is 0) string ~= "marginWidth="~to!(String)(marginWidth)~" ";
138 if (marginHeight !is 0) String += "marginHeight="+marginHeight+" "; 153 if (marginHeight !is 0) string ~= "marginHeight="~to!(String)(marginHeight)~" ";
139 if (topControl !is null) String += "topControl="+topControl+" "; 154 if (topControl !is null) string ~= "topControl="~to!(String)(topControl)~" ";
140 String = String.trim(); 155 string = tango.text.Util.trim(string);
141 String += "}"; 156 string ~= "}";
142 return String; 157 return string;
143 } 158 }
144 } 159 }