Mercurial > projects > dwt-mac
comparison dwt/custom/StackLayout.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 6337764516f1 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
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 *******************************************************************************/ | |
11 module dwt.custom; | |
12 | |
13 | |
14 import dwt.*; | |
15 import dwt.graphics.*; | |
16 import dwt.widgets.*; | |
17 | |
18 /** | |
19 * This Layout stacks all the controls one on top of the other and resizes all controls | |
20 * to have the same size and location. | |
21 * 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 | |
23 * layout() on the composite which has the StackLayout. | |
24 * | |
25 * <p> Here is an example which places ten buttons in a stack layout and | |
26 * flips between them: | |
27 * | |
28 * <pre><code> | |
29 * public static void main(String[] args) { | |
30 * Display display = new Display(); | |
31 * Shell shell = new Shell(display); | |
32 * shell.setLayout(new GridLayout()); | |
33 * | |
34 * final Composite parent = new Composite(shell, DWT.NONE); | |
35 * parent.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
36 * final StackLayout layout = new StackLayout(); | |
37 * parent.setLayout(layout); | |
38 * final Button[] bArray = new Button[10]; | |
39 * for (int i = 0; i < 10; i++) { | |
40 * bArray[i] = new Button(parent, DWT.PUSH); | |
41 * bArray[i].setText("Button "+i); | |
42 * } | |
43 * layout.topControl = bArray[0]; | |
44 * | |
45 * Button b = new Button(shell, DWT.PUSH); | |
46 * b.setText("Show Next Button"); | |
47 * final int[] index = new int[1]; | |
48 * b.addListener(DWT.Selection, new Listener(){ | |
49 * public void handleEvent(Event e) { | |
50 * index[0] = (index[0] + 1) % 10; | |
51 * layout.topControl = bArray[index[0]]; | |
52 * parent.layout(); | |
53 * } | |
54 * }); | |
55 * | |
56 * shell.open(); | |
57 * while (shell !is null && !shell.isDisposed()) { | |
58 * if (!display.readAndDispatch()) | |
59 * display.sleep(); | |
60 * } | |
61 * } | |
62 * </code></pre> | |
63 */ | |
64 | |
65 public class StackLayout : Layout { | |
66 | |
67 /** | |
68 * marginWidth specifies the number of pixels of horizontal margin | |
69 * that will be placed along the left and right edges of the layout. | |
70 * | |
71 * The default value is 0. | |
72 */ | |
73 public int marginWidth = 0; | |
74 /** | |
75 * marginHeight specifies the number of pixels of vertical margin | |
76 * that will be placed along the top and bottom edges of the layout. | |
77 * | |
78 * The default value is 0. | |
79 */ | |
80 public int marginHeight = 0; | |
81 | |
82 /** | |
83 * 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. | |
85 */ | |
86 public Control topControl; | |
87 | |
88 protected Point computeSize(Composite composite, int wHint, int hHint, bool flushCache) { | |
89 Control children[] = composite.getChildren(); | |
90 int maxWidth = 0; | |
91 int maxHeight = 0; | |
92 for (int i = 0; i < children.length; i++) { | |
93 Point size = children[i].computeSize(wHint, hHint, flushCache); | |
94 maxWidth = Math.max(size.x, maxWidth); | |
95 maxHeight = Math.max(size.y, maxHeight); | |
96 } | |
97 int width = maxWidth + 2 * marginWidth; | |
98 int height = maxHeight + 2 * marginHeight; | |
99 if (wHint !is DWT.DEFAULT) width = wHint; | |
100 if (hHint !is DWT.DEFAULT) height = hHint; | |
101 return new Point(width, height); | |
102 } | |
103 | |
104 protected bool flushCache(Control control) { | |
105 return true; | |
106 } | |
107 | |
108 protected void layout(Composite composite, bool flushCache) { | |
109 Control children[] = composite.getChildren(); | |
110 Rectangle rect = composite.getClientArea(); | |
111 rect.x += marginWidth; | |
112 rect.y += marginHeight; | |
113 rect.width -= 2 * marginWidth; | |
114 rect.height -= 2 * marginHeight; | |
115 for (int i = 0; i < children.length; i++) { | |
116 children[i].setBounds(rect); | |
117 children[i].setVisible(children[i] is topControl); | |
118 | |
119 } | |
120 } | |
121 | |
122 String getName () { | |
123 String String = getClass ().getName (); | |
124 int index = String.lastIndexOf ('.'); | |
125 if (index is -1) return String; | |
126 return String.substring (index + 1, String.length ()); | |
127 } | |
128 | |
129 /** | |
130 * Returns a String containing a concise, human-readable | |
131 * description of the receiver. | |
132 * | |
133 * @return a String representation of the layout | |
134 */ | |
135 public String toString () { | |
136 String String = getName ()+" {"; | |
137 if (marginWidth !is 0) String += "marginWidth="+marginWidth+" "; | |
138 if (marginHeight !is 0) String += "marginHeight="+marginHeight+" "; | |
139 if (topControl !is null) String += "topControl="+topControl+" "; | |
140 String = String.trim(); | |
141 String += "}"; | |
142 return String; | |
143 } | |
144 } |