Mercurial > projects > dwt-addons
comparison dwtx/draw2d/ScrollPaneSolver.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.ScrollPaneSolver; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 import dwtx.draw2d.geometry.Dimension; | |
18 import dwtx.draw2d.geometry.Insets; | |
19 import dwtx.draw2d.geometry.Rectangle; | |
20 import dwtx.draw2d.Viewport; | |
21 | |
22 /** | |
23 * This class handles the calculation of solving for the area of a | |
24 * {@link dwtx.draw2d.ScrollPane}'s viewport and insets. Also determines if | |
25 * the horizontal and vertical scrollbars should be visible. | |
26 */ | |
27 public class ScrollPaneSolver { | |
28 | |
29 /** Scrollbar visibility constants -- never show scrollbars **/ | |
30 public static const int NEVER = 0; | |
31 /** Scrollbar visibility constants -- show scrollbars automatically **/ | |
32 public static const int AUTOMATIC = 1; | |
33 /** Scrollbar visibility constants -- always show scrollbars **/ | |
34 public static const int ALWAYS = 2; | |
35 | |
36 /** | |
37 * Container class for the results of ScrollPaneSolver's solve method | |
38 */ | |
39 public static class Result { | |
40 /** Show horizontal scrollbar bool **/ | |
41 public bool showH; | |
42 | |
43 /** Show vertical scrollbar bool **/ | |
44 public bool showV; | |
45 | |
46 /** Area of ScrollPane's viewport **/ | |
47 public Rectangle viewportArea; | |
48 | |
49 /** Insets of ScrollPane **/ | |
50 public Insets insets; | |
51 } | |
52 | |
53 /** | |
54 * Solves for the viewport area, insets, and visibility of horizontal and vertical | |
55 * scrollbars of a ScrollPane | |
56 * @param clientArea The ScrollPane's client area | |
57 * @param viewport The ScrollPane's Viewport | |
58 * @param hVis Horizontal scrollbar visibility | |
59 * @param vVis Vertical scrollbar visibility | |
60 * @param vBarWidth Width of vertical scrollbar | |
61 * @param hBarHeight Height of horizontal scrollbar | |
62 * @return the Result | |
63 */ | |
64 public static Result solve(Rectangle clientArea, Viewport viewport, int hVis, int vVis, | |
65 int vBarWidth, int hBarHeight) { | |
66 Result result = new Result(); | |
67 result.insets = new Insets(); | |
68 result.insets.bottom = hBarHeight; | |
69 result.insets.right = vBarWidth; | |
70 | |
71 Dimension available = clientArea.getSize(); | |
72 Dimension guaranteed = (new Dimension(available)).shrink( | |
73 (vVis is NEVER ? 0 : result.insets.right), | |
74 (hVis is NEVER ? 0 : result.insets.bottom)); | |
75 guaranteed.width = Math.max(guaranteed.width, 0); | |
76 guaranteed.height = Math.max(guaranteed.height, 0); | |
77 int wHint = guaranteed.width; | |
78 int hHint = guaranteed.height; | |
79 | |
80 Dimension preferred = viewport.getPreferredSize(wHint, hHint).getCopy(); | |
81 Insets viewportInsets = viewport.getInsets(); | |
82 /* | |
83 * This was calling viewport.getMinimumSize(), but viewport's minimum size was really | |
84 * small, and wasn't a function of its contents. | |
85 */ | |
86 Dimension viewportMinSize = new Dimension( | |
87 viewportInsets.getWidth(), viewportInsets.getHeight()); | |
88 if (viewport.getContents() !is null) { | |
89 if (viewport.getContentsTracksHeight() && hHint > -1) | |
90 hHint = Math.max(0, hHint - viewportInsets.getHeight()); | |
91 if (viewport.getContentsTracksWidth() && wHint > -1) | |
92 wHint = Math.max(0, wHint - viewportInsets.getWidth()); | |
93 viewportMinSize.expand( | |
94 viewport.getContents().getMinimumSize(wHint, hHint)); | |
95 } | |
96 | |
97 /* | |
98 * Adjust preferred size if tracking flags set. Basically, tracking is "compress view | |
99 * until its minimum size is reached". | |
100 */ | |
101 if (viewport.getContentsTracksHeight()) | |
102 preferred.height = viewportMinSize.height; | |
103 if (viewport.getContentsTracksWidth()) | |
104 preferred.width = viewportMinSize.width; | |
105 | |
106 bool none = available.contains(preferred), | |
107 both = !none && preferred.containsProper(guaranteed), | |
108 showV = both || preferred.height > available.height, | |
109 showH = both || preferred.width > available.width; | |
110 | |
111 //Adjust for visibility override flags | |
112 result.showV = vVis !is NEVER && (showV || vVis is ALWAYS); | |
113 result.showH = hVis !is NEVER && (showH || hVis is ALWAYS); | |
114 | |
115 if (!result.showV) | |
116 result.insets.right = 0; | |
117 if (!result.showH) | |
118 result.insets.bottom = 0; | |
119 result.viewportArea = clientArea.getCropped(result.insets); | |
120 viewport.setBounds(result.viewportArea); | |
121 return result; | |
122 } | |
123 | |
124 } |