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