1 /******************************************************************************* 2 * Copyright (c) 2018 Red Hat and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * Red Hat - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.swt.tests.gtk.snippets; 15 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.events.MouseAdapter; 18 import org.eclipse.swt.events.MouseEvent; 19 import org.eclipse.swt.graphics.Point; 20 import org.eclipse.swt.widgets.Display; 21 import org.eclipse.swt.widgets.Label; 22 import org.eclipse.swt.widgets.Shell; 23 24 public class Bug_514483_getCursorLocation 25 { main(String[] args)26 public static void main(String[] args) 27 { 28 final Display display = new Display(); 29 Shell shell = new Shell(display); 30 shell.setBounds(0, 0, 600, 600); 31 32 Label parentShellLabel = new Label(shell, SWT.None); 33 parentShellLabel.setText("Parent widget.shell.\n" 34 + "INSTRUCTIONS:\n" 35 + "- Parent widget.shell should be maximized.\n" 36 + "- Child widget.shell should be at x400 y400 (in yellow square).\n" 37 + "- Click inside the child widget.shell, observe result coordinates below.\n" 38 + "\n" 39 + "The bug is that x,y is not relative to parent, but relative to child-widget.shell itself (0-200 range).\n" 40 + "Expected coordinates: between ~400 to ~600. (i.e, relative to parent's x,y.)\n" 41 + "Result Coordinates:"); 42 parentShellLabel.setBounds(0, 0, 600, 200); 43 44 final Label resultLbl = new Label(shell, SWT.None); 45 resultLbl.setBounds(0,180, 600, 100); 46 resultLbl.setBackground(display.getSystemColor(SWT.COLOR_YELLOW)); 47 48 Label childShellLocation = new Label(shell, SWT.None); 49 childShellLocation.setText("Child Shell should be here.\nIf it is not, move it here \nmanually"); 50 childShellLocation.setBackground(display.getSystemColor(SWT.COLOR_YELLOW)); 51 childShellLocation.setBounds(400, 400, 200, 160); 52 53 Shell childShell = new Shell(shell, SWT.ON_TOP); 54 childShell.setBackground(display.getSystemColor(SWT.COLOR_DARK_YELLOW)); 55 56 MouseAdapter clickListener = new MouseAdapter() { 57 @Override 58 public void mouseDown(MouseEvent e) { 59 Point loc = display.getCursorLocation(); 60 resultLbl.setText(loc.toString()); 61 if (loc.x > 300 && loc.x < 700 && loc.y > 300 && loc.y < 700) // give user some slack. 62 resultLbl.setBackground(display.getSystemColor(SWT.COLOR_GREEN)); 63 else 64 resultLbl.setBackground(display.getSystemColor(SWT.COLOR_RED)); 65 } 66 }; 67 childShell.addMouseListener(clickListener); 68 69 // display.addFilter(SWT.KeyDown, new Listener() { 70 // public void handleEvent(Event e) { 71 // if (e.type == SWT.KeyDown) { 72 // switch (e.keyCode) { 73 // case SWT.F1: 74 // System.out.println("Passed"); 75 // break; 76 // case SWT.F2: 77 // System.out.println("Failed"); 78 // break; 79 // case SWT.F3: 80 // System.out.println("Skipped"); 81 // break; 82 // case SWT.F4: 83 // System.out.println("Exit Test suite"); 84 // break; 85 // } 86 // } 87 // } 88 // }); 89 90 shell.open(); 91 childShell.open(); 92 childShell.setBounds(400, 400, 200, 200); 93 while (!shell.isDisposed()) 94 { 95 if (!display.readAndDispatch()) display.sleep(); 96 } 97 display.dispose(); 98 } 99 }