comparison dwtx/draw2d/ViewportLayout.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 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 dwtx.draw2d.ViewportLayout;
14
15 import dwt.dwthelper.utils;
16
17 import dwtx.draw2d.geometry.Dimension;
18 import dwtx.draw2d.geometry.Insets;
19 import dwtx.draw2d.geometry.Point;
20 import dwtx.draw2d.geometry.Rectangle;
21 import dwtx.draw2d.AbstractHintLayout;
22 import dwtx.draw2d.IFigure;
23 import dwtx.draw2d.Viewport;
24
25 /**
26 * Layout for a viewport. A viewport is a flexible window
27 * onto a figure.
28 */
29 public class ViewportLayout
30 : AbstractHintLayout
31 {
32
33 /**
34 * Returns the minimum size required by the input viewport figure. Since viewport is
35 * flexible, the minimum size required would be the just the size of the borders.
36 * @see AbstractHintLayout#calculateMinimumSize(IFigure, int, int)
37 */
38 protected Dimension calculateMinimumSize(IFigure figure, int wHint, int hHint) {
39 Viewport viewport = cast(Viewport)figure;
40 Dimension min = new Dimension();
41 Insets insets = viewport.getInsets();
42 return min.getExpanded(insets.getWidth(), insets.getHeight());
43 }
44
45 /**
46 * Calculates and returns the preferred size of the figure based on the given hints. The
47 * given wHint is ignored unless the viewport (parent) is tracking width. The same is true
48 * for the height hint.
49 * @param parent the Viewport whose preferred size is to be calculated
50 * @param wHint the width hint
51 * @param hHint the height hint
52 * @return the Preferred size of the given viewport
53 * @since 2.0
54 */
55 protected Dimension calculatePreferredSize(IFigure parent, int wHint, int hHint) {
56 Viewport viewport = cast(Viewport)parent;
57 Insets insets = viewport.getInsets();
58 IFigure contents = viewport.getContents();
59
60 if (viewport.getContentsTracksWidth() && wHint > -1)
61 wHint = Math.max(0, wHint - insets.getWidth());
62 else
63 wHint = -1;
64 if (viewport.getContentsTracksHeight() && hHint > -1)
65 hHint = Math.max(0, hHint - insets.getHeight());
66 else
67 hHint = -1;
68
69 if (contents is null) {
70 return new Dimension(insets.getWidth(), insets.getHeight());
71 } else {
72 Dimension minSize = contents.getMinimumSize(wHint, hHint);
73 if (wHint > -1)
74 wHint = Math.max(wHint, minSize.width);
75 if (hHint > -1)
76 hHint = Math.max(hHint, minSize.height);
77 return contents
78 .getPreferredSize(wHint, hHint)
79 .getExpanded(insets.getWidth(), insets.getHeight());
80 }
81
82 //Layout currently does not union border's preferred size.
83 }
84
85 /**
86 * @see dwtx.draw2d.AbstractHintLayout#isSensitiveHorizontally(IFigure)
87 */
88 protected bool isSensitiveHorizontally(IFigure parent) {
89 return (cast(Viewport)parent).getContentsTracksWidth();
90 }
91
92 /**
93 * @see dwtx.draw2d.AbstractHintLayout#isSensitiveHorizontally(IFigure)
94 */
95 protected bool isSensitiveVertically(IFigure parent) {
96 return (cast(Viewport)parent).getContentsTracksHeight();
97 }
98
99 /**
100 * @see dwtx.draw2d.LayoutManager#layout(IFigure)
101 */
102 public void layout(IFigure figure) {
103 Viewport viewport = cast(Viewport)figure;
104 IFigure contents = viewport.getContents();
105
106 if (contents is null) return;
107 Point p = viewport.getClientArea().getLocation();
108
109 p.translate(viewport.getViewLocation().getNegated());
110
111 // Calculate the hints
112 Rectangle hints = viewport.getClientArea();
113 int wHint = viewport.getContentsTracksWidth() ? hints.width : -1;
114 int hHint = viewport.getContentsTracksHeight() ? hints.height : -1;
115
116 Dimension newSize = viewport.getClientArea().getSize();
117 Dimension min = contents.getMinimumSize(wHint, hHint);
118 Dimension pref = contents.getPreferredSize(wHint, hHint);
119
120 if (viewport.getContentsTracksHeight())
121 newSize.height = Math.max(newSize.height, min.height);
122 else
123 newSize.height = Math.max(newSize.height, pref.height);
124
125 if (viewport.getContentsTracksWidth())
126 newSize.width = Math.max(newSize.width, min.width);
127 else
128 newSize.width = Math.max(newSize.width, pref.width);
129
130 contents.setBounds(new Rectangle(p, newSize));
131 }
132
133 }