Mercurial > projects > dwt-mac
comparison dwt/widgets/ScrollBar.d @ 45:d8635bb48c7c
Merge with SWT 3.5
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Mon, 01 Dec 2008 17:07:00 +0100 |
parents | 642f460a0908 |
children | 34237ae5156a |
comparison
equal
deleted
inserted
replaced
44:ca5e494f2bbf | 45:d8635bb48c7c |
---|---|
1 /******************************************************************************* | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
3 * All rights reserved. This program and the accompanying materials | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | 4 * are made available under the terms of the Eclipse Public License v1.0 |
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * | 10 * |
11 * Port to the D programming language: | 11 * Port to the D programming language: |
12 * Jacob Carlborg <jacob.carlborg@gmail.com> | 12 * Jacob Carlborg <doob@me.com> |
13 *******************************************************************************/ | 13 *******************************************************************************/ |
14 module dwt.widgets.ScrollBar; | 14 module dwt.widgets.ScrollBar; |
15 | 15 |
16 | 16 |
17 import dwt.DWT; | 17 import dwt.DWT; |
18 import dwt.DWTException; | 18 import dwt.DWTException; |
19 import dwt.events.SelectionEvent; | 19 import dwt.events.SelectionEvent; |
20 import dwt.events.SelectionListener; | 20 import dwt.events.SelectionListener; |
21 import dwt.graphics.Point; | 21 import dwt.graphics.Point; |
22 import dwt.internal.cocoa.NSRect; | |
22 import dwt.internal.cocoa.NSScroller; | 23 import dwt.internal.cocoa.NSScroller; |
23 import dwt.internal.cocoa.OS; | 24 import dwt.internal.cocoa.OS; |
24 import dwt.internal.cocoa.id; | 25 import dwt.internal.cocoa.id; |
25 | 26 |
26 import dwt.dwthelper.utils; | 27 import dwt.dwthelper.utils; |
90 * | 91 * |
91 * @see Slider | 92 * @see Slider |
92 * @see Scrollable | 93 * @see Scrollable |
93 * @see Scrollable#getHorizontalBar | 94 * @see Scrollable#getHorizontalBar |
94 * @see Scrollable#getVerticalBar | 95 * @see Scrollable#getVerticalBar |
96 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample</a> | |
97 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
95 */ | 98 */ |
96 public class ScrollBar : Widget { | 99 public class ScrollBar : Widget { |
97 NSScroller view; | 100 NSScroller view; |
98 Scrollable parent; | 101 Scrollable parent; |
99 int minimum, maximum, thumb; | 102 int minimum, maximum = 100, thumb = 10; |
100 int increment = 1; | 103 int increment = 1; |
101 int pageIncrement = 10; | 104 int pageIncrement = 10; |
102 id target; | 105 id target; |
103 String actionSelector;; | 106 String actionSelector;; |
104 | 107 |
148 | 151 |
149 static int checkStyle (int style) { | 152 static int checkStyle (int style) { |
150 return checkBits (style, DWT.HORIZONTAL, DWT.VERTICAL, 0, 0, 0, 0); | 153 return checkBits (style, DWT.HORIZONTAL, DWT.VERTICAL, 0, 0, 0, 0); |
151 } | 154 } |
152 | 155 |
153 void createWidget () { | 156 void deregister () { |
154 maximum = 100; | 157 super.deregister (); |
155 thumb = 10; | 158 display.removeWidget (view); |
156 super.createWidget(); | |
157 } | 159 } |
158 | 160 |
159 /** | 161 /** |
160 * Returns <code>true</code> if the receiver is enabled, and | 162 * Returns <code>true</code> if the receiver is enabled, and |
161 * <code>false</code> otherwise. A disabled control is typically | 163 * <code>false</code> otherwise. A disabled control is typically |
266 * </ul> | 268 * </ul> |
267 */ | 269 */ |
268 public int getSelection () { | 270 public int getSelection () { |
269 checkWidget(); | 271 checkWidget(); |
270 NSScroller widget = cast(NSScroller)view; | 272 NSScroller widget = cast(NSScroller)view; |
271 float value = widget.floatValue(); | 273 double value = widget.doubleValue(); |
272 return cast(int)((maximum - thumb - minimum) * value + minimum); | 274 return cast(int)((maximum - thumb - minimum) * value + minimum); |
273 } | 275 } |
274 | 276 |
275 /** | 277 /** |
276 * Returns a point describing the receiver's size. The | 278 * Returns a point describing the receiver's size. The |
285 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | 287 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
286 * </ul> | 288 * </ul> |
287 */ | 289 */ |
288 public Point getSize () { | 290 public Point getSize () { |
289 checkWidget(); | 291 checkWidget(); |
290 // return getControlSize (handle); | 292 NSRect rect = ((NSScroller)view).frame(); |
291 return new Point(0, 0); | 293 return new Point((int)rect.width, (int)rect.height); |
292 } | 294 } |
293 | 295 |
294 /** | 296 /** |
295 * Returns the size of the receiver's thumb relative to the | 297 * Returns the size of the receiver's thumb relative to the |
296 * difference between its maximum and minimum values. | 298 * difference between its maximum and minimum values. |
393 if (eventTable is null) return; | 395 if (eventTable is null) return; |
394 eventTable.unhook(DWT.Selection, listener); | 396 eventTable.unhook(DWT.Selection, listener); |
395 eventTable.unhook(DWT.DefaultSelection,listener); | 397 eventTable.unhook(DWT.DefaultSelection,listener); |
396 } | 398 } |
397 | 399 |
400 void register () { | |
401 super.register (); | |
402 display.addWidget (view, this); | |
403 } | |
404 | |
398 void releaseHandle () { | 405 void releaseHandle () { |
399 super.releaseHandle (); | 406 super.releaseHandle (); |
407 if (view !is null) view.release(); | |
400 view = null; | 408 view = null; |
401 } | 409 } |
402 | 410 |
403 void releaseParent () { | 411 void releaseParent () { |
404 super.releaseParent (); | 412 super.releaseParent (); |
405 if (parent.horizontalBar is this) parent.horizontalBar = null; | 413 if (parent.horizontalBar is this) parent.horizontalBar = null; |
406 if (parent.verticalBar is this) parent.verticalBar = null; | 414 if (parent.verticalBar is this) parent.verticalBar = null; |
407 parent.resizeClientArea (); | |
408 } | 415 } |
409 | 416 |
410 void releaseWidget () { | 417 void releaseWidget () { |
411 super.releaseWidget (); | 418 super.releaseWidget (); |
412 parent = null; | 419 parent = null; |
482 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | 489 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
483 * </ul> | 490 * </ul> |
484 */ | 491 */ |
485 public void setEnabled (bool enabled) { | 492 public void setEnabled (bool enabled) { |
486 checkWidget(); | 493 checkWidget(); |
487 // if (enabled) { | 494 if (enabled) { |
488 // if ((state & DISABLED) is 0) return; | 495 if ((state & DISABLED) is 0) return; |
489 // state &= ~DISABLED; | 496 state &= ~DISABLED; |
490 // OS.EnableControl (handle); | 497 } else { |
491 // } else { | 498 if ((state & DISABLED) !is 0) return; |
492 // if ((state & DISABLED) !is 0) return; | 499 state |= DISABLED; |
493 // state |= DISABLED; | 500 } |
494 // OS.DisableControl (handle); | 501 view.setEnabled(enabled); |
495 // } | |
496 } | 502 } |
497 | 503 |
498 /** | 504 /** |
499 * Sets the maximum. If this value is negative or less than or | 505 * Sets the maximum. If this value is negative or less than or |
500 * equal to the minimum, the value is ignored. If necessary, first | 506 * equal to the minimum, the value is ignored. If necessary, first |
629 if (minimum < 0) return; | 635 if (minimum < 0) return; |
630 if (maximum < 0) return; | 636 if (maximum < 0) return; |
631 if (thumb < 1) return; | 637 if (thumb < 1) return; |
632 if (increment < 1) return; | 638 if (increment < 1) return; |
633 if (pageIncrement < 1) return; | 639 if (pageIncrement < 1) return; |
634 thumb = Math.min (thumb, maximum - minimum); | 640 this.thumb = thumb = Math.min (thumb, maximum - minimum); |
641 this.maximum = maximum; | |
642 this.minimum = minimum; | |
635 this.increment = increment; | 643 this.increment = increment; |
636 this.pageIncrement = pageIncrement; | 644 this.pageIncrement = pageIncrement; |
637 updateBar(selection, minimum, maximum, thumb); | 645 updateBar (selection, minimum, maximum, thumb); |
638 } | 646 } |
639 | 647 |
640 /** | 648 /** |
641 * Marks the receiver as visible if the argument is <code>true</code>, | 649 * Marks the receiver as visible if the argument is <code>true</code>, |
642 * and marks it invisible otherwise. | 650 * and marks it invisible otherwise. |
653 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | 661 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
654 * </ul> | 662 * </ul> |
655 */ | 663 */ |
656 public void setVisible (bool visible) { | 664 public void setVisible (bool visible) { |
657 checkWidget(); | 665 checkWidget(); |
658 //TODO visibility | |
659 parent.setScrollBarVisible (this, visible); | 666 parent.setScrollBarVisible (this, visible); |
660 } | 667 } |
661 | 668 |
662 void updateBar(int selection, int minimum, int maximum, int thumb) { | 669 void updateBar (int selection, int minimum, int maximum, int thumb) { |
663 NSScroller widget = cast(NSScroller)view; | 670 NSScroller widget = cast(NSScroller) view; |
664 selection = Math.max(minimum, Math.min(maximum - thumb, selection)); | 671 selection = Math.max (minimum, Math.min (maximum - thumb, selection)); |
665 int range = maximum - thumb - minimum; | 672 int range = maximum - thumb - minimum; |
666 float fraction = range < 0 ? 1 : cast(float)(selection - minimum) / range; | 673 float fraction = range <= 0 ? 1 : cast(float) (selection - minimum) / range; |
667 CGFloat knob = minimum is maximum ? 1 : cast(CGFloat)(thumb - minimum) / maximum - minimum; | 674 CGFloat knob = range <= 0 ? 1 : cast(CGFloat) thumb / (maximum - minimum); |
668 widget.setFloatValue(fraction, knob); | 675 widget.setFloatValue (fraction, knob); |
669 widget.setEnabled(range > 0); | 676 widget.setEnabled (range > 0); |
670 } | 677 } |
671 | 678 |
672 } | 679 } |