Mercurial > projects > dwt-addons
comparison dwtx/ui/forms/widgets/SharedScrolledComposite.d @ 75:5d489b9f966c
Fix continue porting
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 24 May 2008 05:11:16 +0200 |
parents | |
children | 4ac9946b9fb5 |
comparison
equal
deleted
inserted
replaced
74:dad2e11b8ae4 | 75:5d489b9f966c |
---|---|
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.ui.forms.widgets.SharedScrolledComposite; | |
14 | |
15 import dwtx.ui.forms.widgets.SizeCache; | |
16 | |
17 import dwt.DWT; | |
18 import dwt.custom.ScrolledComposite; | |
19 import dwt.graphics.Color; | |
20 import dwt.graphics.Font; | |
21 import dwt.graphics.Point; | |
22 import dwt.graphics.Rectangle; | |
23 import dwt.widgets.Composite; | |
24 import dwt.widgets.Control; | |
25 import dwt.widgets.Event; | |
26 import dwt.widgets.Listener; | |
27 import dwt.widgets.ScrollBar; | |
28 import dwtx.ui.internal.forms.widgets.FormUtil; | |
29 | |
30 import dwt.dwthelper.utils; | |
31 import dwt.dwthelper.Runnable; | |
32 | |
33 /** | |
34 * This class is used to provide common scrolling services to a number of | |
35 * controls in the toolkit. Classes that extend it are not required to implement | |
36 * any method. | |
37 * | |
38 * @since 3.0 | |
39 */ | |
40 public abstract class SharedScrolledComposite : ScrolledComposite { | |
41 private static const int H_SCROLL_INCREMENT = 5; | |
42 | |
43 private static const int V_SCROLL_INCREMENT = 64; | |
44 | |
45 private bool ignoreLayouts = true; | |
46 | |
47 private bool ignoreResizes = false; | |
48 | |
49 private bool expandHorizontal = false; | |
50 | |
51 private bool expandVertical = false; | |
52 | |
53 private SizeCache contentCache; | |
54 | |
55 private bool reflowPending = false; | |
56 | |
57 private bool delayedReflow = true; | |
58 | |
59 /** | |
60 * Creates the new instance. | |
61 * | |
62 * @param parent | |
63 * the parent composite | |
64 * @param style | |
65 * the style to use | |
66 */ | |
67 public this(Composite parent, int style) { | |
68 contentCache = new SizeCache(); | |
69 super(parent, style); | |
70 addListener(DWT.Resize, new class Listener { | |
71 public void handleEvent(Event e) { | |
72 if (!ignoreResizes) { | |
73 scheduleReflow(false); | |
74 } | |
75 } | |
76 }); | |
77 initializeScrollBars(); | |
78 } | |
79 | |
80 /** | |
81 * Sets the foreground of the control and its content. | |
82 * | |
83 * @param fg | |
84 * the new foreground color | |
85 */ | |
86 public void setForeground(Color fg) { | |
87 super.setForeground(fg); | |
88 if (getContent() !is null) | |
89 getContent().setForeground(fg); | |
90 } | |
91 | |
92 /** | |
93 * Sets the background of the control and its content. | |
94 * | |
95 * @param bg | |
96 * the new background color | |
97 */ | |
98 public void setBackground(Color bg) { | |
99 super.setBackground(bg); | |
100 if (getContent() !is null) | |
101 getContent().setBackground(bg); | |
102 } | |
103 | |
104 /** | |
105 * Sets the font of the form. This font will be used to render the title | |
106 * text. It will not affect the body. | |
107 */ | |
108 public void setFont(Font font) { | |
109 super.setFont(font); | |
110 if (getContent() !is null) | |
111 getContent().setFont(font); | |
112 } | |
113 | |
114 /** | |
115 * Overrides 'super' to pass the proper colors and font | |
116 */ | |
117 public void setContent(Control content) { | |
118 super.setContent(content); | |
119 if (content !is null) { | |
120 content.setForeground(getForeground()); | |
121 content.setBackground(getBackground()); | |
122 content.setFont(getFont()); | |
123 } | |
124 } | |
125 | |
126 /** | |
127 * If content is set, transfers focus to the content. | |
128 */ | |
129 public bool setFocus() { | |
130 bool result; | |
131 FormUtil.setFocusScrollingEnabled(this, false); | |
132 if (getContent() !is null) | |
133 result = getContent().setFocus(); | |
134 else | |
135 result = super.setFocus(); | |
136 FormUtil.setFocusScrollingEnabled(this, true); | |
137 return result; | |
138 } | |
139 | |
140 /* | |
141 * (non-Javadoc) | |
142 * | |
143 * @see dwt.widgets.Composite#layout(bool) | |
144 */ | |
145 public void layout(bool changed) { | |
146 if (ignoreLayouts) { | |
147 return; | |
148 } | |
149 | |
150 ignoreLayouts = true; | |
151 ignoreResizes = true; | |
152 super.layout(changed); | |
153 ignoreResizes = false; | |
154 } | |
155 | |
156 /* | |
157 * (non-Javadoc) | |
158 * | |
159 * @see dwt.custom.ScrolledComposite#setExpandHorizontal(bool) | |
160 */ | |
161 public void setExpandHorizontal(bool expand) { | |
162 expandHorizontal = expand; | |
163 super.setExpandHorizontal(expand); | |
164 } | |
165 | |
166 /* | |
167 * (non-Javadoc) | |
168 * | |
169 * @see dwt.custom.ScrolledComposite#setExpandVertical(bool) | |
170 */ | |
171 public void setExpandVertical(bool expand) { | |
172 expandVertical = expand; | |
173 super.setExpandVertical(expand); | |
174 } | |
175 | |
176 /** | |
177 * Recomputes the body layout and the scroll bars. The method should be used | |
178 * when changes somewhere in the form body invalidate the current layout | |
179 * and/or scroll bars. | |
180 * | |
181 * @param flushCache | |
182 * if <code>true</code>, drop the cached data | |
183 */ | |
184 public void reflow(bool flushCache) { | |
185 Composite c = cast(Composite) getContent(); | |
186 Rectangle clientArea = getClientArea(); | |
187 if (c is null) | |
188 return; | |
189 | |
190 contentCache.setControl(c); | |
191 if (flushCache) { | |
192 contentCache.flush(); | |
193 } | |
194 try { | |
195 setRedraw(false); | |
196 Point newSize = contentCache.computeSize(FormUtil.getWidthHint( | |
197 clientArea.width, c), FormUtil.getHeightHint(clientArea.height, | |
198 c)); | |
199 | |
200 // Point currentSize = c.getSize(); | |
201 if (!(expandHorizontal && expandVertical)) { | |
202 c.setSize(newSize); | |
203 } | |
204 | |
205 setMinSize(newSize); | |
206 FormUtil.updatePageIncrement(this); | |
207 | |
208 // reduce vertical scroll increment if necessary | |
209 ScrollBar vbar = getVerticalBar(); | |
210 if (vbar !is null) { | |
211 if (getClientArea().height - 5 < V_SCROLL_INCREMENT) | |
212 getVerticalBar().setIncrement(getClientArea().height - 5); | |
213 else | |
214 getVerticalBar().setIncrement(V_SCROLL_INCREMENT); | |
215 } | |
216 | |
217 ignoreLayouts = false; | |
218 layout(flushCache); | |
219 ignoreLayouts = true; | |
220 | |
221 contentCache.layoutIfNecessary(); | |
222 } finally { | |
223 setRedraw(true); | |
224 } | |
225 } | |
226 | |
227 private void updateSizeWhilePending() { | |
228 Control c = getContent(); | |
229 Rectangle area = getClientArea(); | |
230 setMinSize(area.width, c.getSize().y); | |
231 } | |
232 | |
233 private void scheduleReflow(bool flushCache) { | |
234 if (delayedReflow) { | |
235 if (reflowPending) { | |
236 updateSizeWhilePending(); | |
237 return; | |
238 } | |
239 getDisplay().asyncExec( dgRunnable( (bool flushCache) { | |
240 if (!isDisposed()) | |
241 reflow(flushCache); | |
242 reflowPending = false; | |
243 }, flushCache)); | |
244 reflowPending = true; | |
245 } else | |
246 reflow(flushCache); | |
247 } | |
248 | |
249 private void initializeScrollBars() { | |
250 ScrollBar hbar = getHorizontalBar(); | |
251 if (hbar !is null) { | |
252 hbar.setIncrement(H_SCROLL_INCREMENT); | |
253 } | |
254 ScrollBar vbar = getVerticalBar(); | |
255 if (vbar !is null) { | |
256 vbar.setIncrement(V_SCROLL_INCREMENT); | |
257 } | |
258 FormUtil.updatePageIncrement(this); | |
259 } | |
260 | |
261 /** | |
262 * Tests if the control uses delayed reflow. | |
263 * @return <code>true</code> if reflow requests will | |
264 * be delayed, <code>false</code> otherwise. | |
265 */ | |
266 public bool isDelayedReflow() { | |
267 return delayedReflow; | |
268 } | |
269 | |
270 /** | |
271 * Sets the delayed reflow feature. When used, | |
272 * it will schedule a reflow on resize requests | |
273 * and reject subsequent reflows until the | |
274 * scheduled one is performed. This improves | |
275 * performance by | |
276 * @param delayedReflow | |
277 * The delayedReflow to set. | |
278 */ | |
279 public void setDelayedReflow(bool delayedReflow) { | |
280 this.delayedReflow = delayedReflow; | |
281 } | |
282 } |