1 #!/usr/bin/env dub 2 /+ 3 dub.sdl: 4 name "snippet46" 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 * Thomas Demmer <t_demmer AT web DOT de> 47 *******************************************************************************/ 48 module org.eclipse.swt.snippets.Snippet46; 49 50 /* 51 * Composite example snippet: intercept mouse events (drag a button with the mouse) 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.graphics.Point; 58 import org.eclipse.swt.graphics.Rectangle; 59 import org.eclipse.swt.widgets.Button; 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.Shell; 65 import org.eclipse.swt.layout.FillLayout; 66 67 import java.lang.all; 68 69 void main () { 70 Display display = new Display (); 71 Shell shell = new Shell (display); 72 Composite composite = new Composite (shell, SWT.NONE); 73 composite.setEnabled (false); 74 composite.setLayout (new FillLayout ()); 75 Button button = new Button (composite, SWT.PUSH); 76 button.setText ("Button"); 77 composite.pack (); 78 composite.setLocation (10, 10); 79 Point[1] offset; 80 Listener listener = new class Listener{ 81 public void handleEvent (Event event) { 82 switch (event.type) { 83 case SWT.MouseDown: 84 Rectangle rect = composite.getBounds (); 85 if (rect.contains (event.x, event.y)) { 86 Point pt1 = composite.toDisplay (0, 0); 87 Point pt2 = shell.toDisplay (event.x, event.y); 88 offset [0] = new Point (pt2.x - pt1.x, pt2.y - pt1.y); 89 } 90 break; 91 case SWT.MouseMove: 92 if (offset [0] !is null) { 93 Point pt = offset [0]; 94 composite.setLocation (event.x - pt.x, event.y - pt.y); 95 } 96 break; 97 case SWT.MouseUp: 98 offset [0] = null; 99 break; 100 default: 101 } 102 } 103 }; 104 shell.addListener (SWT.MouseDown, listener); 105 shell.addListener (SWT.MouseUp, listener); 106 shell.addListener (SWT.MouseMove, listener); 107 shell.setSize (300, 300); 108 shell.open (); 109 while (!shell.isDisposed ()) { 110 if (!display.readAndDispatch ()) display.sleep (); 111 } 112 display.dispose (); 113 } 114