comparison dwt/custom/CBannerLayout.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 f565d3a95c0a
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 * Port to the D Programming language:
12 * Jacob Carlborg <jacob.carlborg@gmail.com>
13 *******************************************************************************/
14 module dwt.custom.CBannerLayout;
15
16 import Math = tango.math.Math;
17
18 import dwt.DWT;
19 import dwt.custom.CBanner;
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 import dwt.widgets.Scrollable;
26
27 /**
28 * This class provides the layout for CBanner
29 *
30 * @see CBanner
31 */
32 class CBannerLayout : Layout {
33
34 protected Point computeSize (Composite composite, int wHint, int hHint, bool flushCache) {
35 CBanner banner = cast(CBanner) composite;
36 Control left = banner.left;
37 Control right = banner.right;
38 Control bottom = banner.bottom;
39 bool showCurve = left !is null && right !is null;
40 int height = hHint;
41 int width = wHint;
42
43 // Calculate component sizes
44 Point bottomSize = new Point(0, 0);
45 if (bottom !is null) {
46 int trim = computeTrim(bottom);
47 int w = wHint is DWT.DEFAULT ? DWT.DEFAULT : Math.max(0, width - trim);
48 bottomSize = computeChildSize(bottom, w, DWT.DEFAULT, flushCache);
49 }
50 Point rightSize = new Point(0, 0);
51 if (right !is null) {
52 int trim = computeTrim(right);
53 int w = DWT.DEFAULT;
54 if (banner.rightWidth !is DWT.DEFAULT) {
55 w = banner.rightWidth - trim;
56 if (left !is null) {
57 w = Math.min(w, width - banner.curve_width + 2 * banner.curve_indent - CBanner.MIN_LEFT - trim);
58 }
59 w = Math.max(0, w);
60 }
61 rightSize = computeChildSize(right, w, DWT.DEFAULT, flushCache);
62 if (wHint !is DWT.DEFAULT) {
63 width -= rightSize.x + banner.curve_width - 2 * banner.curve_indent;
64 }
65 }
66 Point leftSize = new Point(0, 0);
67 if (left !is null) {
68 int trim = computeTrim(left);
69 int w = wHint is DWT.DEFAULT ? DWT.DEFAULT : Math.max(0, width - trim);
70 leftSize = computeChildSize(left, w, DWT.DEFAULT, flushCache);
71 }
72
73 // Add up sizes
74 width = leftSize.x + rightSize.x;
75 height = bottomSize.y;
76 if (bottom !is null && (left !is null || right !is null)) {
77 height += CBanner.BORDER_STRIPE + 2;
78 }
79 if (left !is null) {
80 if (right is null) {
81 height += leftSize.y;
82 }
83 else {
84 height += Math.max(leftSize.y, banner.rightMinHeight is DWT.DEFAULT ? rightSize.y : banner.rightMinHeight);
85 }
86 }
87 else {
88 height += rightSize.y;
89 }
90 if (showCurve) {
91 width += banner.curve_width - 2 * banner.curve_indent;
92 height += CBanner.BORDER_TOP + CBanner.BORDER_BOTTOM + 2 * CBanner.BORDER_STRIPE;
93 }
94
95 if (wHint !is DWT.DEFAULT)
96 width = wHint;
97 if (hHint !is DWT.DEFAULT)
98 height = hHint;
99
100 return new Point(width, height);
101 }
102
103 Point computeChildSize (Control control, int wHint, int hHint, bool flushCache) {
104 Object data = control.getLayoutData();
105 if (data is null || !(cast(CLayoutData) data)) {
106 data = new CLayoutData();
107 control.setLayoutData(data);
108 }
109 return (cast(CLayoutData) data).computeSize(control, wHint, hHint, flushCache);
110 }
111
112 int computeTrim (Control c) {
113 if (cast(Scrollable) c) {
114 Rectangle rect = (cast(Scrollable) c).computeTrim(0, 0, 0, 0);
115 return rect.width;
116 }
117 return c.getBorderWidth() * 2;
118 }
119
120 protected bool flushCache (Control control) {
121 Object data = control.getLayoutData();
122 if (data !is null && cast(CLayoutData) data)
123 (cast(CLayoutData) data).flushCache();
124 return true;
125 }
126
127 protected void layout (Composite composite, bool flushCache) {
128 CBanner banner = cast(CBanner) composite;
129 Control left = banner.left;
130 Control right = banner.right;
131 Control bottom = banner.bottom;
132
133 Point size = banner.getSize();
134 bool showCurve = left !is null && right !is null;
135 int width = size.x - 2 * banner.getBorderWidth();
136 int height = size.y - 2 * banner.getBorderWidth();
137
138 Point bottomSize = new Point(0, 0);
139 if (bottom !is null) {
140 int trim = computeTrim(bottom);
141 int w = Math.max(0, width - trim);
142 bottomSize = computeChildSize(bottom, w, DWT.DEFAULT, flushCache);
143 height -= bottomSize.y + CBanner.BORDER_STRIPE + 2;
144 }
145 if (showCurve)
146 height -= CBanner.BORDER_TOP + CBanner.BORDER_BOTTOM + 2 * CBanner.BORDER_STRIPE;
147 height = Math.max(0, height);
148 Point rightSize = new Point(0, 0);
149 if (right !is null) {
150 int trim = computeTrim(right);
151 int w = DWT.DEFAULT;
152 if (banner.rightWidth !is DWT.DEFAULT) {
153 w = banner.rightWidth - trim;
154 if (left !is null) {
155 w = Math.min(w, width - banner.curve_width + 2 * banner.curve_indent - CBanner.MIN_LEFT - trim);
156 }
157 w = Math.max(0, w);
158 }
159 rightSize = computeChildSize(right, w, DWT.DEFAULT, flushCache);
160 width = width - (rightSize.x - banner.curve_indent + banner.curve_width - banner.curve_indent);
161 }
162 Point leftSize = new Point(0, 0);
163 if (left !is null) {
164 int trim = computeTrim(left);
165 int w = Math.max(0, width - trim);
166 leftSize = computeChildSize(left, w, DWT.DEFAULT, flushCache);
167 }
168
169 int x = 0;
170 int y = 0;
171 int oldStart = banner.curveStart;
172 Rectangle leftRect = null;
173 Rectangle rightRect = null;
174 Rectangle bottomRect = null;
175 if (bottom !is null) {
176 bottomRect = new Rectangle(x, y + size.y - bottomSize.y, bottomSize.x, bottomSize.y);
177 }
178 if (showCurve)
179 y += CBanner.BORDER_TOP + CBanner.BORDER_STRIPE;
180 if (left !is null) {
181 leftRect = new Rectangle(x, y, leftSize.x, leftSize.y);
182 banner.curveStart = x + leftSize.x - banner.curve_indent;
183 x += leftSize.x - banner.curve_indent + banner.curve_width - banner.curve_indent;
184 }
185 if (right !is null) {
186 if (left !is null) {
187 rightSize.y = Math.max(leftSize.y, banner.rightMinHeight is DWT.DEFAULT ? rightSize.y : banner.rightMinHeight);
188 }
189 rightRect = new Rectangle(x, y, rightSize.x, rightSize.y);
190 }
191 if (banner.curveStart < oldStart) {
192 banner.redraw(banner.curveStart - CBanner.CURVE_TAIL, 0, oldStart + banner.curve_width - banner.curveStart + CBanner.CURVE_TAIL + 5,
193 size.y, false);
194 }
195 if (banner.curveStart > oldStart) {
196 banner.redraw(oldStart - CBanner.CURVE_TAIL, 0, banner.curveStart + banner.curve_width - oldStart + CBanner.CURVE_TAIL + 5, size.y, false);
197 }
198 /*
199 * The paint events must be flushed in order to make the curve draw smoothly
200 * while the user drags the divider.
201 * On Windows, it is necessary to flush the paints before the children are
202 * resized because otherwise the children (particularly toolbars) will flash.
203 */
204 banner.update();
205 banner.curveRect = new Rectangle(banner.curveStart, 0, banner.curve_width, size.y);
206 if (bottomRect !is null)
207 bottom.setBounds(bottomRect);
208 if (rightRect !is null)
209 right.setBounds(rightRect);
210 if (leftRect !is null)
211 left.setBounds(leftRect);
212 }
213 }