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