1 /*******************************************************************************
2  * Copyright (c) 2018 Red Hat and others. All rights reserved.
3  * The contents of this file are made available under the terms
4  * of the GNU Lesser General Public License (LGPL) Version 2.1 that
5  * accompanies this distribution (lgpl-v21.txt).  The LGPL is also
6  * available at http://www.gnu.org/licenses/lgpl.html.  If the version
7  * of the LGPL at http://www.gnu.org is different to the version of
8  * the LGPL accompanying this distribution and there is any conflict
9  * between the two license versions, the terms of the LGPL accompanying
10  * this distribution shall govern.
11  *
12  * Contributors:
13  *     Red Hat - initial API and implementation
14  *******************************************************************************/
15 package org.eclipse.swt.tests.gtk.snippets;
16 
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.MouseAdapter;
19 import org.eclipse.swt.events.MouseEvent;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.swt.widgets.Shell;
24 
25 
26 /*
27  * Title: Bug 514483 - [wayland] Launchbar filter closes when clicked
28  * How to run: launch snippet and follow onscreen instructions
29  * Bug description: The X-Y coordinates are relative to the child shell.
30  * Expected results: The X-Y coordinates are relative to the parent shell.
31  * GTK Version(s): GTK3.16+ (Wayland)
32  */
33 public class Bug514483_getCursorLocation
34 {
main(String[] args)35 	public static void main(String[] args)
36 	{
37 		final Display display = new Display();
38 		Shell shell = new Shell(display);
39 		shell.setBounds(0, 0, 600, 600);
40 
41 		Label parentShellLabel = new Label(shell, SWT.None);
42 		parentShellLabel.setText("Parent widget.shell.\n"
43 				+ "INSTRUCTIONS:\n"
44 				+ "- Parent widget.shell should be maximized.\n"
45 				+ "- Child widget.shell should be at x400 y400 (in yellow square).\n"
46 				+ "- Click inside the child widget.shell, observe result coordinates below.\n"
47 				+ "\n"
48 				+ "The bug is that x,y is not relative to parent, but relative to child-widget.shell itself (0-200 range).\n"
49 				+ "Expected coordinates: between ~400 to ~600. (i.e, relative to parent's x,y.)\n"
50 				+ "Result Coordinates:");
51 		parentShellLabel.setBounds(0, 0, 600, 200);
52 
53 		final Label resultLbl = new Label(shell, SWT.None);
54 		resultLbl.setBounds(0,180, 600, 100);
55 		resultLbl.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
56 
57 		Label childShellLocation = new Label(shell, SWT.None);
58 		childShellLocation.setText("Child Shell should be here.\nIf it is not, move it here \nmanually");
59 		childShellLocation.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
60 		childShellLocation.setBounds(400, 400, 200, 160);
61 
62 		Shell childShell = new Shell(shell, SWT.ON_TOP);
63 		childShell.setBackground(display.getSystemColor(SWT.COLOR_DARK_YELLOW));
64 
65 		MouseAdapter clickListener = new MouseAdapter() {
66 			@Override
67 			public void mouseDown(MouseEvent e) {
68 				Point loc = display.getCursorLocation();
69 				resultLbl.setText(loc.toString());
70 				if (loc.x > 300 && loc.x < 700 && loc.y > 300 && loc.y < 700) // give user some slack.
71 					resultLbl.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
72 				else
73 					resultLbl.setBackground(display.getSystemColor(SWT.COLOR_RED));
74 			}
75 		};
76 		childShell.addMouseListener(clickListener);
77 
78 //		display.addFilter(SWT.KeyDown, new Listener() {
79 //			  public void handleEvent(Event e) {
80 //			    if (e.type == SWT.KeyDown) {
81 //			      switch (e.keyCode) {
82 //			        case SWT.F1:
83 //			          System.out.println("Passed");
84 //			          break;
85 //			        case SWT.F2:
86 //			          System.out.println("Failed");
87 //			          break;
88 //			        case SWT.F3:
89 //			          System.out.println("Skipped");
90 //			          break;
91 //			        case SWT.F4:
92 //			          System.out.println("Exit Test suite");
93 //			          break;
94 //			      }
95 //			    }
96 //			  }
97 //			});
98 
99 		shell.open();
100 		childShell.open();
101 		childShell.setBounds(400, 400, 200, 200);
102 		while (!shell.isDisposed())
103 		{
104 			if (!display.readAndDispatch()) display.sleep();
105 		}
106 		display.dispose();
107 	}
108 }