comparison dwt/custom/ScrolledComposite.d @ 213:36f5cb12e1a2

Update to SWT 3.4M7
author Frank Benoit <benoit@tionex.de>
date Sat, 17 May 2008 17:34:28 +0200
parents ab60f3309436
children fd9c62a2998e
comparison
equal deleted inserted replaced
212:ab60f3309436 213:36f5cb12e1a2
13 module dwt.custom.ScrolledComposite; 13 module dwt.custom.ScrolledComposite;
14 14
15 15
16 import dwt.DWT; 16 import dwt.DWT;
17 import dwt.DWTException; 17 import dwt.DWTException;
18 import dwt.events.DisposeEvent;
19 import dwt.events.DisposeListener;
18 import dwt.graphics.Point; 20 import dwt.graphics.Point;
19 import dwt.graphics.Rectangle; 21 import dwt.graphics.Rectangle;
20 import dwt.widgets.Composite; 22 import dwt.widgets.Composite;
21 import dwt.widgets.Control; 23 import dwt.widgets.Control;
24 import dwt.widgets.Display;
22 import dwt.widgets.Event; 25 import dwt.widgets.Event;
23 import dwt.widgets.Layout; 26 import dwt.widgets.Layout;
24 import dwt.widgets.Listener; 27 import dwt.widgets.Listener;
25 import dwt.widgets.ScrollBar; 28 import dwt.widgets.ScrollBar;
29 import dwt.widgets.Shell;
26 import dwt.custom.ScrolledCompositeLayout; 30 import dwt.custom.ScrolledCompositeLayout;
27 import dwt.dwthelper.utils; 31 import dwt.dwthelper.utils;
28 32
29 /** 33 /**
30 * A ScrolledComposite provides scrollbars and will scroll its content when the user 34 * A ScrolledComposite provides scrollbars and will scroll its content when the user
111 */ 115 */
112 public class ScrolledComposite : Composite { 116 public class ScrolledComposite : Composite {
113 117
114 Control content; 118 Control content;
115 Listener contentListener; 119 Listener contentListener;
120 Listener filter;
116 121
117 int minHeight = 0; 122 int minHeight = 0;
118 int minWidth = 0; 123 int minWidth = 0;
119 bool expandHorizontal = false; 124 bool expandHorizontal = false;
120 bool expandVertical = false; 125 bool expandVertical = false;
121 bool alwaysShowScroll = false; 126 bool alwaysShowScroll = false;
127 bool showFocusedControl = false;
122 128
123 /** 129 /**
124 * Constructs a new instance of this class given its parent 130 * Constructs a new instance of this class given its parent
125 * and a style value describing its behavior and appearance. 131 * and a style value describing its behavior and appearance.
126 * <p> 132 * <p>
174 public void handleEvent(Event e) { 180 public void handleEvent(Event e) {
175 if (e.type !is DWT.Resize) return; 181 if (e.type !is DWT.Resize) return;
176 layout(false); 182 layout(false);
177 } 183 }
178 }; 184 };
185
186 filter = new class() Listener {
187 public void handleEvent(Event event) {
188 if ( auto control = cast(Control)event.widget ) {
189 if (contains(control)) showControl(control);
190 }
191 }
192 };
193
194 addDisposeListener(new class() DisposeListener {
195 public void widgetDisposed(DisposeEvent e) {
196 getDisplay().removeFilter(DWT.FocusIn, filter);
197 }
198 });
179 } 199 }
180 200
181 static int checkStyle (int style) { 201 static int checkStyle (int style) {
182 int mask = DWT.H_SCROLL | DWT.V_SCROLL | DWT.BORDER | DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT; 202 int mask = DWT.H_SCROLL | DWT.V_SCROLL | DWT.BORDER | DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT;
183 return style & mask; 203 return style & mask;
204 }
205
206 bool contains(Control control) {
207 if (control is null || control.isDisposed()) return false;
208
209 Composite parent = control.getParent();
210 while (parent !is null && !( null !is cast(Shell)parent )) {
211 if (this is parent) return true;
212 parent = parent.getParent();
213 }
214 return false;
184 } 215 }
185 216
186 /** 217 /**
187 * Returns the Always Show Scrollbars flag. True if the scrollbars are 218 * Returns the Always Show Scrollbars flag. True if the scrollbars are
188 * always shown even if they are not required. False if the scrollbars are only 219 * always shown even if they are not required. False if the scrollbars are only
273 * @return the control displayed in the content area 304 * @return the control displayed in the content area
274 */ 305 */
275 public Control getContent() { 306 public Control getContent() {
276 //checkWidget(); 307 //checkWidget();
277 return content; 308 return content;
309 }
310
311 /**
312 * Returns <code>true</code> if the receiver automatically scrolls to a focused child control
313 * to make it visible. Otherwise, returns <code>false</code>.
314 *
315 * @exception DWTException <ul>
316 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
317 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
318 * </ul>
319 *
320 * @since 3.4
321 */
322 public bool getShowFocusedControl() {
323 checkWidget();
324 return showFocusedControl;
278 } 325 }
279 326
280 void hScroll() { 327 void hScroll() {
281 if (content is null) return; 328 if (content is null) return;
282 Point location = content.getLocation (); 329 Point location = content.getLocation ();
584 */ 631 */
585 public void setMinWidth(int width) { 632 public void setMinWidth(int width) {
586 setMinSize(width, minHeight); 633 setMinSize(width, minHeight);
587 } 634 }
588 635
636 /**
637 * Configure the receiver to automatically scroll to a focused child control
638 * to make it visible.
639 *
640 * If show is <code>false</code>, show a focused control is off.
641 * By default, show a focused control is off.
642 *
643 * @param show <code>true</code> to show a focused control.
644 *
645 * @exception DWTException <ul>
646 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
647 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
648 * </ul>
649 *
650 * @since 3.4
651 */
652 public void setShowFocusedControl(bool show) {
653 checkWidget();
654 if (showFocusedControl is show) return;
655 Display display = getDisplay();
656 display.removeFilter(DWT.FocusIn, filter);
657 showFocusedControl = show;
658 if (!showFocusedControl) return;
659 display.addFilter(DWT.FocusIn, filter);
660 Control control = display.getFocusControl();
661 if (contains(control)) showControl(control);
662 }
663
664 /**
665 * Scrolls the content of the receiver so that the control is visible.
666 *
667 * @param control the control to be shown
668 *
669 * @exception IllegalArgumentException <ul>
670 * <li>ERROR_NULL_ARGUMENT - if the control is null</li>
671 * <li>ERROR_INVALID_ARGUMENT - if the control has been disposed</li>
672 * </ul>
673 * @exception DWTException <ul>
674 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
675 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
676 * </ul>
677 *
678 * @since 3.4
679 */
680 public void showControl(Control control) {
681 checkWidget ();
682 if (control is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
683 if (control.isDisposed ()) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
684 if (!contains(control)) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
685
686 Rectangle itemRect = getDisplay().map(control.getParent(), this, control.getBounds());
687 Rectangle area = getClientArea();
688 Point origin = getOrigin();
689 if (itemRect.x < 0) origin.x = Math.max(0, origin.x + itemRect.x);
690 if (itemRect.y < 0) origin.y = Math.max(0, origin.y + itemRect.y);
691 if (area.width < itemRect.x + itemRect.width) origin.x = Math.max(0, origin.x + itemRect.x + itemRect.width - area.width);
692 if (area.height < itemRect.y + itemRect.height) origin.y = Math.max(0, origin.y + itemRect.y + itemRect.height - area.height);
693 setOrigin(origin);
694 }
695
589 void vScroll() { 696 void vScroll() {
590 if (content is null) return; 697 if (content is null) return;
591 Point location = content.getLocation (); 698 Point location = content.getLocation ();
592 ScrollBar vBar = getVerticalBar (); 699 ScrollBar vBar = getVerticalBar ();
593 int vSelection = vBar.getSelection (); 700 int vSelection = vBar.getSelection ();