1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet9"
5     dependency "dwt" path="../../../../../../" version="*"
6     libs \
7       "atk-1.0" \
8       "cairo" \
9       "dl" \
10       "fontconfig" \
11       "gdk-x11-2.0" \
12       "gdk_pixbuf-2.0" \
13       "gio-2.0" \
14       "glib-2.0" \
15       "gmodule-2.0" \
16       "gobject-2.0" \
17       "gthread-2.0" \
18       "gtk-x11-2.0" \
19       "pango-1.0" \
20       "pangocairo-1.0" \
21       "X11" \
22       "Xcomposite" \
23       "Xcursor" \
24       "Xdamage" \
25       "Xext" \
26       "Xfixes" \
27       "Xi" \
28       "Xinerama" \
29       "Xrandr" \
30       "Xrender" \
31       "Xtst" \
32       platform="linux"
33 +/
34 
35 /*******************************************************************************
36  * Copyright (c) 2000, 2016 IBM Corporation and others.
37  * All rights reserved. This program and the accompanying materials
38  * are made available under the terms of the Eclipse Public License v1.0
39  * which accompanies this distribution, and is available at
40  * http://www.eclipse.org/legal/epl-v10.html
41  *
42  * Contributors:
43  *     IBM Corporation - initial API and implementation
44  * D Port
45  *     Jesse Phillips <Jesse.K.Phillips+D> gmail.com
46  *******************************************************************************/
47 module org.eclipse.swt.snippets.Snippet9;
48 
49 /*
50  * Composite example snippet: scroll a child control automatically
51  *
52  * For a list of all SWT example snippets see
53  * http://www.eclipse.org/swt/snippets/
54  */
55 import org.eclipse.swt.SWT;
56 import org.eclipse.swt.events.PaintListener;
57 import org.eclipse.swt.graphics.Color;
58 import org.eclipse.swt.graphics.Point;
59 import org.eclipse.swt.graphics.Rectangle;
60 import org.eclipse.swt.widgets.Composite;
61 import org.eclipse.swt.widgets.Display;
62 import org.eclipse.swt.widgets.Event;
63 import org.eclipse.swt.widgets.Listener;
64 import org.eclipse.swt.widgets.ScrollBar;
65 import org.eclipse.swt.widgets.Shell;
66 import java.lang.all;
67 
68 void main () {
69     auto display = new Display ();
70     auto shell = new Shell
71        (display, SWT.SHELL_TRIM | SWT.H_SCROLL | SWT.V_SCROLL);
72     auto composite = new Composite (shell, SWT.BORDER);
73     composite.setSize (700, 600);
74     auto red = display.getSystemColor (SWT.COLOR_RED);
75     composite.addPaintListener (new class PaintListener {
76         public void paintControl (PaintEvent e) {
77             e.gc.setBackground (red);
78             e.gc.fillOval (5, 5, 690, 590);
79         }
80     });
81     auto hBar = shell.getHorizontalBar ();
82     hBar.addListener (SWT.Selection, new class Listener {
83         public void handleEvent (Event e) {
84             auto location = composite.getLocation ();
85             location.x = -hBar.getSelection ();
86             composite.setLocation (location);
87         }
88     });
89     ScrollBar vBar = shell.getVerticalBar ();
90     vBar.addListener (SWT.Selection, new class Listener {
91         public void handleEvent (Event e) {
92             Point location = composite.getLocation ();
93             location.y = -vBar.getSelection ();
94             composite.setLocation (location);
95         }
96     });
97     shell.addListener (SWT.Resize,  new class Listener {
98         public void handleEvent (Event e) {
99             Point size = composite.getSize ();
100             Rectangle rect = shell.getClientArea ();
101             hBar.setMaximum (size.x);
102             vBar.setMaximum (size.y);
103             hBar.setThumb (Math.min (size.x, rect.width));
104             vBar.setThumb (Math.min (size.y, rect.height));
105             int hPage = size.x - rect.width;
106             int vPage = size.y - rect.height;
107             int hSelection = hBar.getSelection ();
108             int vSelection = vBar.getSelection ();
109             Point location = composite.getLocation ();
110             if (hSelection >= hPage) {
111                 if (hPage <= 0) hSelection = 0;
112                 location.x = -hSelection;
113             }
114             if (vSelection >= vPage) {
115                 if (vPage <= 0) vSelection = 0;
116                 location.y = -vSelection;
117             }
118             composite.setLocation (location);
119         }
120     });
121     shell.setSize(600, 500);
122     shell.open ();
123     while (!shell.isDisposed()) {
124         if (!display.readAndDispatch ()) display.sleep ();
125     }
126     display.dispose ();
127 }