25
|
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.ScrolledCompositeLayout;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17
|
|
18 import org.eclipse.swt.SWT;
|
|
19 import org.eclipse.swt.graphics.Point;
|
|
20 import org.eclipse.swt.graphics.Rectangle;
|
|
21 import org.eclipse.swt.widgets.Composite;
|
|
22 import org.eclipse.swt.widgets.Control;
|
|
23 import org.eclipse.swt.widgets.Layout;
|
|
24 import org.eclipse.swt.widgets.ScrollBar;
|
|
25 import org.eclipse.swt.custom.ScrolledComposite;
|
|
26
|
|
27
|
|
28 /**
|
|
29 * This class provides the layout for ScrolledComposite
|
|
30 *
|
|
31 * @see ScrolledComposite
|
|
32 */
|
|
33 class ScrolledCompositeLayout : Layout {
|
|
34
|
|
35 bool inLayout = false;
|
|
36 static final int DEFAULT_WIDTH = 64;
|
|
37 static final int DEFAULT_HEIGHT = 64;
|
|
38
|
|
39 protected override Point computeSize(Composite composite, int wHint, int hHint, bool flushCache) {
|
|
40 ScrolledComposite sc = cast(ScrolledComposite)composite;
|
|
41 Point size = new Point(DEFAULT_WIDTH, DEFAULT_HEIGHT);
|
|
42 if (sc.content !is null) {
|
|
43 Point preferredSize = sc.content.computeSize(wHint, hHint, flushCache);
|
|
44 Point currentSize = sc.content.getSize();
|
|
45 size.x = sc.getExpandHorizontal() ? preferredSize.x : currentSize.x;
|
|
46 size.y = sc.getExpandVertical() ? preferredSize.y : currentSize.y;
|
|
47 }
|
|
48 size.x = Math.max(size.x, sc.minWidth);
|
|
49 size.y = Math.max(size.y, sc.minHeight);
|
|
50 if (wHint !is SWT.DEFAULT) size.x = wHint;
|
|
51 if (hHint !is SWT.DEFAULT) size.y = hHint;
|
|
52 return size;
|
|
53 }
|
|
54
|
|
55 protected override bool flushCache(Control control) {
|
|
56 return true;
|
|
57 }
|
|
58
|
|
59 protected override void layout(Composite composite, bool flushCache) {
|
|
60 if (inLayout) return;
|
|
61 ScrolledComposite sc = cast(ScrolledComposite)composite;
|
|
62 if (sc.content is null) return;
|
|
63 ScrollBar hBar = sc.getHorizontalBar();
|
|
64 ScrollBar vBar = sc.getVerticalBar();
|
|
65 if (hBar !is null) {
|
|
66 if (hBar.getSize().y >= sc.getSize().y) {
|
|
67 return;
|
|
68 }
|
|
69 }
|
|
70 if (vBar !is null) {
|
|
71 if (vBar.getSize().x >= sc.getSize().x) {
|
|
72 return;
|
|
73 }
|
|
74 }
|
|
75 inLayout = true;
|
|
76 Rectangle contentRect = sc.content.getBounds();
|
|
77 if (!sc.alwaysShowScroll) {
|
|
78 bool hVisible = sc.needHScroll(contentRect, false);
|
|
79 bool vVisible = sc.needVScroll(contentRect, hVisible);
|
|
80 if (!hVisible && vVisible) hVisible = sc.needHScroll(contentRect, vVisible);
|
|
81 if (hBar !is null) hBar.setVisible(hVisible);
|
|
82 if (vBar !is null) vBar.setVisible(vVisible);
|
|
83 }
|
|
84 Rectangle hostRect = sc.getClientArea();
|
|
85 if (sc.expandHorizontal) {
|
|
86 contentRect.width = Math.max(sc.minWidth, hostRect.width);
|
|
87 }
|
|
88 if (sc.expandVertical) {
|
|
89 contentRect.height = Math.max(sc.minHeight, hostRect.height);
|
|
90 }
|
|
91
|
|
92 if (hBar !is null) {
|
|
93 hBar.setMaximum (contentRect.width);
|
|
94 hBar.setThumb (Math.min (contentRect.width, hostRect.width));
|
|
95 int hPage = contentRect.width - hostRect.width;
|
|
96 int hSelection = hBar.getSelection ();
|
|
97 if (hSelection >= hPage) {
|
|
98 if (hPage <= 0) {
|
|
99 hSelection = 0;
|
|
100 hBar.setSelection(0);
|
|
101 }
|
|
102 contentRect.x = -hSelection;
|
|
103 }
|
|
104 }
|
|
105
|
|
106 if (vBar !is null) {
|
|
107 vBar.setMaximum (contentRect.height);
|
|
108 vBar.setThumb (Math.min (contentRect.height, hostRect.height));
|
|
109 int vPage = contentRect.height - hostRect.height;
|
|
110 int vSelection = vBar.getSelection ();
|
|
111 if (vSelection >= vPage) {
|
|
112 if (vPage <= 0) {
|
|
113 vSelection = 0;
|
|
114 vBar.setSelection(0);
|
|
115 }
|
|
116 contentRect.y = -vSelection;
|
|
117 }
|
|
118 }
|
|
119
|
|
120 sc.content.setBounds (contentRect);
|
|
121 inLayout = false;
|
|
122 }
|
|
123 }
|