Mercurial > projects > dwt-mac
comparison dwt/widgets/Scrollable.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 649b8e223d5a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2007 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 *******************************************************************************/ | |
11 module dwt.widgets.Scrollable; | |
12 | |
13 import dwt.dwthelper.utils; | |
14 | |
15 | |
16 import dwt.DWT; | |
17 import dwt.DWTException; | |
18 import dwt.graphics.Rectangle; | |
19 import dwt.internal.cocoa.NSRect; | |
20 import dwt.internal.cocoa.NSScrollView; | |
21 import dwt.internal.cocoa.NSScroller; | |
22 import dwt.internal.cocoa.NSSize; | |
23 import dwt.internal.cocoa.NSView; | |
24 import dwt.internal.cocoa.OS; | |
25 import dwt.internal.cocoa.SWTScrollView; | |
26 | |
27 /** | |
28 * This class is the abstract superclass of all classes which | |
29 * represent controls that have standard scroll bars. | |
30 * <dl> | |
31 * <dt><b>Styles:</b></dt> | |
32 * <dd>H_SCROLL, V_SCROLL</dd> | |
33 * <dt><b>Events:</b> | |
34 * <dd>(none)</dd> | |
35 * </dl> | |
36 * <p> | |
37 * IMPORTANT: This class is intended to be subclassed <em>only</em> | |
38 * within the DWT implementation. | |
39 * </p> | |
40 */ | |
41 public abstract class Scrollable extends Control { | |
42 SWTScrollView scrollView; | |
43 ScrollBar horizontalBar, verticalBar; | |
44 | |
45 Scrollable () { | |
46 /* Do nothing */ | |
47 } | |
48 | |
49 /** | |
50 * Constructs a new instance of this class given its parent | |
51 * and a style value describing its behavior and appearance. | |
52 * <p> | |
53 * The style value is either one of the style constants defined in | |
54 * class <code>DWT</code> which is applicable to instances of this | |
55 * class, or must be built by <em>bitwise OR</em>'ing together | |
56 * (that is, using the <code>int</code> "|" operator) two or more | |
57 * of those <code>DWT</code> style constants. The class description | |
58 * lists the style constants that are applicable to the class. | |
59 * Style bits are also inherited from superclasses. | |
60 * </p> | |
61 * | |
62 * @param parent a composite control which will be the parent of the new instance (cannot be null) | |
63 * @param style the style of control to construct | |
64 * | |
65 * @exception IllegalArgumentException <ul> | |
66 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
67 * </ul> | |
68 * @exception DWTException <ul> | |
69 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
70 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
71 * </ul> | |
72 * | |
73 * @see DWT#H_SCROLL | |
74 * @see DWT#V_SCROLL | |
75 * @see Widget#checkSubclass | |
76 * @see Widget#getStyle | |
77 */ | |
78 public Scrollable (Composite parent, int style) { | |
79 super (parent, style); | |
80 } | |
81 | |
82 /** | |
83 * Given a desired <em>client area</em> for the receiver | |
84 * (as described by the arguments), returns the bounding | |
85 * rectangle which would be required to produce that client | |
86 * area. | |
87 * <p> | |
88 * In other words, it returns a rectangle such that, if the | |
89 * receiver's bounds were set to that rectangle, the area | |
90 * of the receiver which is capable of displaying data | |
91 * (that is, not covered by the "trimmings") would be the | |
92 * rectangle described by the arguments (relative to the | |
93 * receiver's parent). | |
94 * </p> | |
95 * | |
96 * @param x the desired x coordinate of the client area | |
97 * @param y the desired y coordinate of the client area | |
98 * @param width the desired width of the client area | |
99 * @param height the desired height of the client area | |
100 * @return the required bounds to produce the given client area | |
101 * | |
102 * @exception DWTException <ul> | |
103 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
104 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
105 * </ul> | |
106 * | |
107 * @see #getClientArea | |
108 */ | |
109 public Rectangle computeTrim (int x, int y, int width, int height) { | |
110 checkWidget(); | |
111 if (scrollView !is null) { | |
112 NSSize size = new NSSize(); | |
113 size.width = width; | |
114 size.height = height; | |
115 int border = hasBorder() ? OS.NSBezelBorder : OS.NSNoBorder; | |
116 size = NSScrollView.frameSizeForContentSize(size, (style & DWT.H_SCROLL) !is 0, (style & DWT.V_SCROLL) !is 0, border); | |
117 width = (int)size.width; | |
118 height = (int)size.height; | |
119 NSRect frame = scrollView.contentView().frame(); | |
120 x -= frame.x; | |
121 y -= frame.y; | |
122 } | |
123 return new Rectangle (x, y, width, height); | |
124 } | |
125 | |
126 ScrollBar createScrollBar (int style) { | |
127 if (scrollView is null) return null; | |
128 ScrollBar bar = new ScrollBar (); | |
129 bar.parent = this; | |
130 bar.style = style; | |
131 bar.display = display; | |
132 NSScroller scroller; | |
133 int actionSelector; | |
134 if ((style & DWT.H_SCROLL) !is 0) { | |
135 scroller = scrollView.horizontalScroller(); | |
136 actionSelector = OS.sel_sendHorizontalSelection; | |
137 } else { | |
138 scroller = scrollView.verticalScroller(); | |
139 actionSelector = OS.sel_sendVerticalSelection; | |
140 } | |
141 bar.view = scroller; | |
142 bar.createJNIRef(); | |
143 scroller.setTag(bar.jniRef); | |
144 if ((state & CANVAS) is 0) { | |
145 bar.target = scroller.target(); | |
146 bar.actionSelector = scroller.action(); | |
147 } | |
148 scroller.setTarget(scrollView); | |
149 scroller.setAction(actionSelector); | |
150 return bar; | |
151 } | |
152 | |
153 void createWidget () { | |
154 super.createWidget (); | |
155 if ((style & DWT.H_SCROLL) !is 0) horizontalBar = createScrollBar (DWT.H_SCROLL); | |
156 if ((style & DWT.V_SCROLL) !is 0) verticalBar = createScrollBar (DWT.V_SCROLL); | |
157 } | |
158 | |
159 /** | |
160 * Returns a rectangle which describes the area of the | |
161 * receiver which is capable of displaying data (that is, | |
162 * not covered by the "trimmings"). | |
163 * | |
164 * @return the client area | |
165 * | |
166 * @exception DWTException <ul> | |
167 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
168 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
169 * </ul> | |
170 * | |
171 * @see #computeTrim | |
172 */ | |
173 public Rectangle getClientArea () { | |
174 checkWidget(); | |
175 if (scrollView !is null) { | |
176 NSSize size = scrollView.contentSize(); | |
177 return new Rectangle(0, 0, (int)size.width, (int)size.height); | |
178 } else { | |
179 NSRect rect = view.bounds(); | |
180 return new Rectangle(0, 0, (int)rect.width, (int)rect.height); | |
181 } | |
182 } | |
183 | |
184 /** | |
185 * Returns the receiver's horizontal scroll bar if it has | |
186 * one, and null if it does not. | |
187 * | |
188 * @return the horizontal scroll bar (or null) | |
189 * | |
190 * @exception DWTException <ul> | |
191 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
192 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
193 * </ul> | |
194 */ | |
195 public ScrollBar getHorizontalBar () { | |
196 checkWidget(); | |
197 return horizontalBar; | |
198 } | |
199 | |
200 /** | |
201 * Returns the receiver's vertical scroll bar if it has | |
202 * one, and null if it does not. | |
203 * | |
204 * @return the vertical scroll bar (or null) | |
205 * | |
206 * @exception DWTException <ul> | |
207 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
208 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
209 * </ul> | |
210 */ | |
211 public ScrollBar getVerticalBar () { | |
212 checkWidget(); | |
213 return verticalBar; | |
214 } | |
215 | |
216 bool hooksKeys () { | |
217 return hooks (DWT.KeyDown) || hooks (DWT.KeyUp) || hooks (DWT.Traverse); | |
218 } | |
219 | |
220 | |
221 void releaseHandle () { | |
222 super.releaseHandle (); | |
223 if (scrollView !is null) { | |
224 scrollView.setTag(-1); | |
225 scrollView.release(); | |
226 } | |
227 scrollView = null; | |
228 } | |
229 | |
230 void releaseChildren (bool destroy) { | |
231 if (horizontalBar !is null) { | |
232 horizontalBar.release (false); | |
233 horizontalBar = null; | |
234 } | |
235 if (verticalBar !is null) { | |
236 verticalBar.release (false); | |
237 verticalBar = null; | |
238 } | |
239 super.releaseChildren (destroy); | |
240 } | |
241 | |
242 void resizeClientArea () { | |
243 // if (scrolledHandle is 0) return; | |
244 // if ((state & CANVAS) is 0) return; | |
245 // int vWidth = 0, hHeight = 0; | |
246 // int [] outMetric = new int [1]; | |
247 // OS.GetThemeMetric (OS.kThemeMetricScrollBarWidth, outMetric); | |
248 // bool isVisibleHBar = horizontalBar !is null && horizontalBar.getVisible (); | |
249 // bool isVisibleVBar = verticalBar !is null && verticalBar.getVisible (); | |
250 // if (isVisibleHBar) hHeight = outMetric [0]; | |
251 // if (isVisibleVBar) vWidth = outMetric [0]; | |
252 // int width, height; | |
253 // CGRect rect = new CGRect (); | |
254 // OS.HIViewGetBounds (scrolledHandle, rect); | |
255 // width = (int) rect.width; | |
256 // height = (int) rect.height; | |
257 // Rect inset = inset (); | |
258 // width = Math.max (0, width - vWidth - inset.left - inset.right); | |
259 // height = Math.max (0, height - hHeight - inset.top - inset.bottom); | |
260 // setBounds (handle, inset.left, inset.top, width, height, true, true, false); | |
261 // if (isVisibleHBar) { | |
262 // setBounds (horizontalBar.handle, inset.left, inset.top + height, width, hHeight, true, true, false); | |
263 // } | |
264 // if (isVisibleVBar) { | |
265 // setBounds (verticalBar.handle, inset.left + width, inset.top, vWidth, height, true, true, false); | |
266 // } | |
267 } | |
268 | |
269 void sendHorizontalSelection () { | |
270 horizontalBar.sendSelection (); | |
271 } | |
272 | |
273 bool sendMouseWheel (short wheelAxis, int wheelDelta) { | |
274 // if ((state & CANVAS) !is 0) { | |
275 // ScrollBar bar = wheelAxis is OS.kEventMouseWheelAxisX ? horizontalBar : verticalBar; | |
276 // if (bar !is null && bar.getEnabled ()) { | |
277 // bar.setSelection (Math.max (0, bar.getSelection () - bar.getIncrement () * wheelDelta)); | |
278 // Event event = new Event (); | |
279 // event.detail = wheelDelta > 0 ? DWT.PAGE_UP : DWT.PAGE_DOWN; | |
280 // bar.sendEvent (DWT.Selection, event); | |
281 // return true; | |
282 // } | |
283 // } | |
284 return false; | |
285 } | |
286 | |
287 void sendVerticalSelection () { | |
288 verticalBar.sendSelection (); | |
289 } | |
290 | |
291 bool setScrollBarVisible (ScrollBar bar, bool visible) { | |
292 if (scrollView is null) return false; | |
293 if ((state & CANVAS) is 0) return false; | |
294 if (visible) { | |
295 if ((bar.state & HIDDEN) is 0) return false; | |
296 bar.state &= ~HIDDEN; | |
297 } else { | |
298 if ((bar.state & HIDDEN) !is 0) return false; | |
299 bar.state |= HIDDEN; | |
300 } | |
301 resizeClientArea (); | |
302 // setVisible (bar.handle, visible); | |
303 bar.sendEvent (visible ? DWT.Show : DWT.Hide); | |
304 sendEvent (DWT.Resize); | |
305 return true; | |
306 } | |
307 | |
308 NSView topView () { | |
309 if (scrollView !is null) return scrollView; | |
310 return super.topView (); | |
311 } | |
312 } |