1 11 package org.eclipse.swt.widgets; 12 13 14 import org.eclipse.swt.internal.win32.*; 15 import org.eclipse.swt.*; 16 import org.eclipse.swt.graphics.*; 17 18 32 33 public abstract class Scrollable extends Control { 34 ScrollBar horizontalBar, verticalBar; 35 36 39 Scrollable () { 40 } 41 42 71 public Scrollable (Composite parent, int style) { 72 super (parent, style); 73 } 74 75 int callWindowProc (int hwnd, int msg, int wParam, int lParam) { 76 if (handle == 0) return 0; 77 return OS.DefWindowProc (hwnd, msg, wParam, lParam); 78 } 79 80 107 public Rectangle computeTrim (int x, int y, int width, int height) { 108 checkWidget (); 109 int scrolledHandle = scrolledHandle (); 110 RECT rect = new RECT (); 111 OS.SetRect (rect, x, y, x + width, y + height); 112 int bits1 = OS.GetWindowLong (scrolledHandle, OS.GWL_STYLE); 113 int bits2 = OS.GetWindowLong (scrolledHandle, OS.GWL_EXSTYLE); 114 OS.AdjustWindowRectEx (rect, bits1, false, bits2); 115 if (horizontalBar != null) rect.bottom += OS.GetSystemMetrics (OS.SM_CYHSCROLL); 116 if (verticalBar != null) rect.right += OS.GetSystemMetrics (OS.SM_CXVSCROLL); 117 int nWidth = rect.right - rect.left, nHeight = rect.bottom - rect.top; 118 return new Rectangle (rect.left, rect.top, nWidth, nHeight); 119 } 120 121 ScrollBar createScrollBar (int type) { 122 ScrollBar bar = new ScrollBar (this, type); 123 if ((state & CANVAS) != 0) { 124 bar.setMaximum (100); 125 bar.setThumb (10); 126 } 127 return bar; 128 } 129 130 void createWidget () { 131 super.createWidget (); 132 if ((style & SWT.H_SCROLL) != 0) horizontalBar = createScrollBar (SWT.H_SCROLL); 133 if ((style & SWT.V_SCROLL) != 0) verticalBar = createScrollBar (SWT.V_SCROLL); 134 } 135 136 150 public Rectangle getClientArea () { 151 checkWidget (); 152 forceResize (); 153 RECT rect = new RECT (); 154 int scrolledHandle = scrolledHandle (); 155 OS.GetClientRect (scrolledHandle, rect); 156 int x = rect.left, y = rect.top; 157 int width = rect.right - rect.left; 158 int height = rect.bottom - rect.top; 159 if (scrolledHandle != handle) { 160 OS.GetClientRect (handle, rect); 161 OS.MapWindowPoints(handle, scrolledHandle, rect, 2); 162 x = -rect.left; 163 y = -rect.top; 164 } 165 return new Rectangle (x, y, width, height); 166 } 167 168 179 public ScrollBar getHorizontalBar () { 180 checkWidget (); 181 return horizontalBar; 182 } 183 184 195 public ScrollBar getVerticalBar () { 196 checkWidget (); 197 return verticalBar; 198 } 199 200 void releaseChildren (boolean destroy) { 201 if (horizontalBar != null) { 202 horizontalBar.release (false); 203 horizontalBar = null; 204 } 205 if (verticalBar != null) { 206 verticalBar.release (false); 207 verticalBar = null; 208 } 209 super.releaseChildren (destroy); 210 } 211 212 int scrolledHandle () { 213 return handle; 214 } 215 216 int widgetExtStyle () { 217 return super.widgetExtStyle (); 218 225 } 229 230 int widgetStyle () { 231 int bits = super.widgetStyle () | OS.WS_TABSTOP; 232 if ((style & SWT.H_SCROLL) != 0) bits |= OS.WS_HSCROLL; 233 if ((style & SWT.V_SCROLL) != 0) bits |= OS.WS_VSCROLL; 234 return bits; 235 } 236 237 TCHAR windowClass () { 238 return display.windowClass; 239 } 240 241 int windowProc () { 242 return display.windowProc; 243 } 244 245 LRESULT WM_HSCROLL (int wParam, int lParam) { 246 LRESULT result = super.WM_HSCROLL (wParam, lParam); 247 if (result != null) return result; 248 249 256 if (horizontalBar != null && (lParam == 0 || lParam == handle)) { 257 return wmScroll (horizontalBar, (state & CANVAS) != 0, handle, OS.WM_HSCROLL, wParam, lParam); 258 } 259 return result; 260 } 261 262 LRESULT WM_MOUSEWHEEL (int wParam, int lParam) { 263 LRESULT result = super.WM_MOUSEWHEEL (wParam, lParam); 264 if (result != null) return result; 265 266 269 if ((state & CANVAS) != 0) { 270 if ((wParam & (OS.MK_SHIFT | OS.MK_CONTROL)) != 0) return result; 271 boolean vertical = verticalBar != null && verticalBar.getEnabled (); 272 boolean horizontal = horizontalBar != null && horizontalBar.getEnabled (); 273 int msg = (vertical) ? OS.WM_VSCROLL : (horizontal) ? OS.WM_HSCROLL : 0; 274 if (msg == 0) return result; 275 int [] value = new int [1]; 276 OS.SystemParametersInfo (OS.SPI_GETWHEELSCROLLLINES, 0, value, 0); 277 int delta = (short) (wParam >> 16); 278 int code = 0, count = 0; 279 if (value [0] == OS.WHEEL_PAGESCROLL) { 280 code = delta < 0 ? OS.SB_PAGEDOWN : OS.SB_PAGEUP; 281 count = Math.abs (delta / OS.WHEEL_DELTA); 282 } else { 283 code = delta < 0 ? OS.SB_LINEDOWN : OS.SB_LINEUP; 284 delta = Math.abs (delta); 285 if (delta < OS.WHEEL_DELTA) return result; 286 if (msg == OS.WM_VSCROLL) { 287 count = value [0] * delta / OS.WHEEL_DELTA; 288 } else { 289 count = delta / OS.WHEEL_DELTA; 290 } 291 } 292 for (int i=0; i<count; i++) { 293 OS.SendMessage (handle, msg, code, 0); 294 } 295 return LRESULT.ZERO; 296 } 297 298 309 int vPosition = verticalBar == null ? 0 : verticalBar.getSelection (); 310 int hPosition = horizontalBar == null ? 0 : horizontalBar.getSelection (); 311 int code = callWindowProc (handle, OS.WM_MOUSEWHEEL, wParam, lParam); 312 if (verticalBar != null) { 313 int position = verticalBar.getSelection (); 314 if (position != vPosition) { 315 Event event = new Event (); 316 event.detail = position < vPosition ? SWT.PAGE_UP : SWT.PAGE_DOWN; 317 verticalBar.sendEvent (SWT.Selection, event); 318 } 319 } 320 if (horizontalBar != null) { 321 int position = horizontalBar.getSelection (); 322 if (position != hPosition) { 323 Event event = new Event (); 324 event.detail = position < hPosition ? SWT.PAGE_UP : SWT.PAGE_DOWN; 325 horizontalBar.sendEvent (SWT.Selection, event); 326 } 327 } 328 return new LRESULT (code); 329 } 330 331 LRESULT WM_SIZE (int wParam, int lParam) { 332 int code = callWindowProc (handle, OS.WM_SIZE, wParam, lParam); 333 super.WM_SIZE (wParam, lParam); 334 if (code == 0) return LRESULT.ZERO; 336 return new LRESULT (code); 337 } 338 339 LRESULT WM_VSCROLL (int wParam, int lParam) { 340 LRESULT result = super.WM_VSCROLL (wParam, lParam); 341 if (result != null) return result; 342 349 if (verticalBar != null && (lParam == 0 || lParam == handle)) { 350 return wmScroll (verticalBar, (state & CANVAS) != 0, handle, OS.WM_VSCROLL, wParam, lParam); 351 } 352 return result; 353 } 354 355 LRESULT wmScroll (ScrollBar bar, boolean update, int hwnd, int msg, int wParam, int lParam) { 356 LRESULT result = null; 357 if (update) { 358 int type = msg == OS.WM_HSCROLL ? OS.SB_HORZ : OS.SB_VERT; 359 SCROLLINFO info = new SCROLLINFO (); 360 info.cbSize = SCROLLINFO.sizeof; 361 info.fMask = OS.SIF_TRACKPOS | OS.SIF_POS | OS.SIF_RANGE; 362 OS.GetScrollInfo (hwnd, type, info); 363 info.fMask = OS.SIF_POS; 364 int code = wParam & 0xFFFF; 365 switch (code) { 366 case OS.SB_ENDSCROLL: return null; 367 case OS.SB_THUMBPOSITION: 368 case OS.SB_THUMBTRACK: 369 375 info.nPos = info.nTrackPos; 376 break; 377 case OS.SB_TOP: 378 info.nPos = info.nMin; 379 break; 380 case OS.SB_BOTTOM: 381 info.nPos = info.nMax; 382 break; 383 case OS.SB_LINEDOWN: 384 info.nPos += bar.getIncrement (); 385 break; 386 case OS.SB_LINEUP: 387 int increment = bar.getIncrement (); 388 info.nPos = Math.max (info.nMin, info.nPos - increment); 389 break; 390 case OS.SB_PAGEDOWN: 391 info.nPos += bar.getPageIncrement (); 392 break; 393 case OS.SB_PAGEUP: 394 int pageIncrement = bar.getPageIncrement (); 395 info.nPos = Math.max (info.nMin, info.nPos - pageIncrement); 396 break; 397 } 398 OS.SetScrollInfo (hwnd, type, info, true); 399 } else { 400 int code = callWindowProc (hwnd, msg, wParam, lParam); 401 result = code == 0 ? LRESULT.ZERO : new LRESULT (code); 402 } 403 bar.wmScrollChild (wParam, lParam); 404 return result; 405 } 406 407 } 408 | Popular Tags |