Skip to content

Commit e74c0fa

Browse files
committed
[Gtk] Prevent mapping to display coordinates on Gtk 4
Gtk 4 and wayland do not support global coordinates thus trying to use it always end up giving the top-left corner of the window which is totally not the wanted one. Adjusted tests for Shell.getLocation returning 0,0 on Gtk4.
1 parent e97143c commit e74c0fa

2 files changed

Lines changed: 70 additions & 57 deletions

File tree

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4032,6 +4032,9 @@ public Point map (Control from, Control to, int x, int y) {
40324032
if (from != null && from.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT);
40334033
if (to != null && to.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT);
40344034
Point point = new Point (x, y);
4035+
if (GTK.GTK4 && (to == null || from == null)) {
4036+
return point;
4037+
}
40354038
if (from == to) return point;
40364039
if (from != null) {
40374040
Point origin = GTK.GTK4 ? from.getSurfaceOrigin() : from.getWindowOrigin ();
@@ -4134,6 +4137,9 @@ public Rectangle map (Control from, Control to, int x, int y, int width, int hei
41344137
if (to != null && to.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT);
41354138
Rectangle rect = new Rectangle (x, y, width, height);
41364139
if (from == to) return rect;
4140+
if (GTK.GTK4 && (to == null || from == null)) {
4141+
return rect;
4142+
}
41374143
boolean fromRTL = false, toRTL = false;
41384144
if (from != null) {
41394145
Point origin = GTK.GTK4 ? from.getSurfaceOrigin () : from.getWindowOrigin ();

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Display.java

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,6 @@ public void test_mapLorg_eclipse_swt_widgets_ControlLorg_eclipse_swt_widgets_Con
544544
shell.setBounds(0,0,400,400);
545545
shell.open();
546546

547-
Point shellOffset = shell.getLocation();
548547
Point result;
549548

550549
result = display.map(button1, button2, 0, 0);
@@ -561,19 +560,22 @@ public void test_mapLorg_eclipse_swt_widgets_ControlLorg_eclipse_swt_widgets_Con
561560
result = display.map(button2, button1, 25, 35);
562561
assertEquals(new Point(225,135), result);
563562

564-
result = display.map(null, button2, 0, 0);
565-
assertEquals(new Point(-200 - shellOffset.x,-100 - shellOffset.y), result);
566-
result = display.map(null, button2, -2, -4);
567-
assertEquals(new Point(-202 - shellOffset.x,-104 - shellOffset.y), result);
568-
result = display.map(null, button2, 6, 8);
569-
assertEquals(new Point(-194 - shellOffset.x,-92 - shellOffset.y), result);
570-
571-
result = display.map(button2, null, 0, 0);
572-
assertEquals(new Point(shellOffset.x + 200,shellOffset.y + 100), result);
573-
result = display.map(button2, null, -3, -6);
574-
assertEquals(new Point(shellOffset.x + 197, shellOffset.y + 94), result);
575-
result = display.map(button2, null, 9, 12);
576-
assertEquals(new Point(shellOffset.x + 209,shellOffset.y + 112), result);
563+
if (!SwtTestUtil.isGTK4()) { //Shell.getLocation gives 0,0 on Gtk 4
564+
Point shellOffset = shell.getLocation();
565+
result = display.map(null, button2, 0, 0);
566+
assertEquals(new Point(-200 - shellOffset.x, -100 - shellOffset.y), result);
567+
result = display.map(null, button2, -2, -4);
568+
assertEquals(new Point(-202 - shellOffset.x, -104 - shellOffset.y), result);
569+
result = display.map(null, button2, 6, 8);
570+
assertEquals(new Point(-194 - shellOffset.x, -92 - shellOffset.y), result);
571+
572+
result = display.map(button2, null, 0, 0);
573+
assertEquals(new Point(shellOffset.x + 200, shellOffset.y + 100), result);
574+
result = display.map(button2, null, -3, -6);
575+
assertEquals(new Point(shellOffset.x + 197, shellOffset.y + 94), result);
576+
result = display.map(button2, null, 9, 12);
577+
assertEquals(new Point(shellOffset.x + 209, shellOffset.y + 112), result);
578+
}
577579

578580
Shell shellMapTest1 = new Shell(display,SWT.NO_TRIM);
579581
Button buttonTest1 = new Button(shellMapTest1, SWT.PUSH);
@@ -669,7 +671,6 @@ public void test_mapLorg_eclipse_swt_widgets_ControlLorg_eclipse_swt_widgets_Con
669671
shell.setBounds(0,0,400,400);
670672
shell.open();
671673

672-
Point shellOffset = shell.getLocation();
673674
Rectangle result;
674675

675676
result = display.map(button1, button2, 0, 0, 100, 100);
@@ -686,19 +687,22 @@ public void test_mapLorg_eclipse_swt_widgets_ControlLorg_eclipse_swt_widgets_Con
686687
result = display.map(button2, button1, 45, 55, 165, 175);
687688
assertEquals(new Rectangle(245,155,165,175), result);
688689

689-
result = display.map(null, button2, 0, 0, 100, 100);
690-
assertEquals(new Rectangle(-200 - shellOffset.x,-100 - shellOffset.y,100,100), result);
691-
result = display.map(null, button2, -2, -4, 106, 108);
692-
assertEquals(new Rectangle(-202 - shellOffset.x,-104 - shellOffset.y,106,108), result);
693-
result = display.map(null, button2, 10, 12, 114, 116);
694-
assertEquals(new Rectangle(-190 - shellOffset.x,-88 - shellOffset.y,114,116), result);
695-
696-
result = display.map(button2, null, 0, 0, 100, 100);
697-
assertEquals(new Rectangle(shellOffset.x + 200,shellOffset.y + 100,100,100), result);
698-
result = display.map(button2, null, -3, -6, 109, 112);
699-
assertEquals(new Rectangle(shellOffset.x + 197,shellOffset.y + 94,109,112), result);
700-
result = display.map(button2, null, 15, 18, 121, 124);
701-
assertEquals(new Rectangle(shellOffset.x + 215,shellOffset.y + 118,121,124), result);
690+
if (!SwtTestUtil.isGTK4()) { // Shell.getLocation gives 0,0 on Gtk 4
691+
Point shellOffset = shell.getLocation();
692+
result = display.map(null, button2, 0, 0, 100, 100);
693+
assertEquals(new Rectangle(-200 - shellOffset.x, -100 - shellOffset.y, 100, 100), result);
694+
result = display.map(null, button2, -2, -4, 106, 108);
695+
assertEquals(new Rectangle(-202 - shellOffset.x, -104 - shellOffset.y, 106, 108), result);
696+
result = display.map(null, button2, 10, 12, 114, 116);
697+
assertEquals(new Rectangle(-190 - shellOffset.x, -88 - shellOffset.y, 114, 116), result);
698+
699+
result = display.map(button2, null, 0, 0, 100, 100);
700+
assertEquals(new Rectangle(shellOffset.x + 200, shellOffset.y + 100, 100, 100), result);
701+
result = display.map(button2, null, -3, -6, 109, 112);
702+
assertEquals(new Rectangle(shellOffset.x + 197, shellOffset.y + 94, 109, 112), result);
703+
result = display.map(button2, null, 15, 18, 121, 124);
704+
assertEquals(new Rectangle(shellOffset.x + 215, shellOffset.y + 118, 121, 124), result);
705+
}
702706

703707
Shell shellMapTest1 = new Shell(display,SWT.NO_TRIM);
704708
Button buttonTest1 = new Button(shellMapTest1, SWT.PUSH);
@@ -798,8 +802,6 @@ public void test_mapLorg_eclipse_swt_widgets_ControlLorg_eclipse_swt_widgets_Con
798802

799803
Point result;
800804
Point point = new Point(0,0);
801-
Point shellOffset = shell.getLocation();
802-
803805

804806
result = display.map(button1, button2, point);
805807
assertEquals(new Point(-200,-100), result);
@@ -815,19 +817,22 @@ public void test_mapLorg_eclipse_swt_widgets_ControlLorg_eclipse_swt_widgets_Con
815817
result = display.map(button2, button1, new Point(25,35));
816818
assertEquals(new Point(225,135), result);
817819

818-
result = display.map(null, button2, point);
819-
assertEquals(new Point(-200 - shellOffset.x,-100 - shellOffset.y), result);
820-
result = display.map(null, button2, new Point(-2,-4));
821-
assertEquals(new Point(-202 - shellOffset.x,-104 - shellOffset.y), result);
822-
result = display.map(null, button2, new Point(6,8));
823-
assertEquals(new Point(-194 - shellOffset.x,-92 - shellOffset.y), result);
824-
825-
result = display.map(button2, null, point);
826-
assertEquals(new Point(shellOffset.x + 200,shellOffset.y + 100), result);
827-
result = display.map(button2, null, new Point(-3,-6));
828-
assertEquals(new Point(shellOffset.x + 197,shellOffset.y + 94), result);
829-
result = display.map(button2, null, new Point(9,12));
830-
assertEquals(new Point(shellOffset.x + 209,shellOffset.y + 112), result);
820+
if (!SwtTestUtil.isGTK4()) { // Shell.getLocation gives 0,0 on Gtk 4
821+
Point shellOffset = shell.getLocation();
822+
result = display.map(null, button2, point);
823+
assertEquals(new Point(-200 - shellOffset.x, -100 - shellOffset.y), result);
824+
result = display.map(null, button2, new Point(-2, -4));
825+
assertEquals(new Point(-202 - shellOffset.x, -104 - shellOffset.y), result);
826+
result = display.map(null, button2, new Point(6, 8));
827+
assertEquals(new Point(-194 - shellOffset.x, -92 - shellOffset.y), result);
828+
829+
result = display.map(button2, null, point);
830+
assertEquals(new Point(shellOffset.x + 200, shellOffset.y + 100), result);
831+
result = display.map(button2, null, new Point(-3, -6));
832+
assertEquals(new Point(shellOffset.x + 197, shellOffset.y + 94), result);
833+
result = display.map(button2, null, new Point(9, 12));
834+
assertEquals(new Point(shellOffset.x + 209, shellOffset.y + 112), result);
835+
}
831836

832837
Shell shellMapTest1 = new Shell(display,SWT.NO_TRIM);
833838
Button buttonTest1 = new Button(shellMapTest1, SWT.PUSH);
@@ -929,7 +934,6 @@ public void test_mapLorg_eclipse_swt_widgets_ControlLorg_eclipse_swt_widgets_Con
929934

930935
Rectangle result;
931936
Rectangle rect = new Rectangle(0,0,100,100);
932-
Point shellOffset = shell.getLocation();
933937

934938
result = display.map(button1, button2, rect);
935939
assertEquals(new Rectangle(-200,-100,100,100), result);
@@ -945,19 +949,22 @@ public void test_mapLorg_eclipse_swt_widgets_ControlLorg_eclipse_swt_widgets_Con
945949
result = display.map(button2, button1, new Rectangle(45, 55, 165, 175));
946950
assertEquals(new Rectangle(245,155,165,175), result);
947951

948-
result = display.map(null, button2, rect);
949-
assertEquals(new Rectangle(-200 - shellOffset.x,-100 - shellOffset.y,100,100), result);
950-
result = display.map(null, button2, new Rectangle(-2, -4, 106, 108));
951-
assertEquals(new Rectangle(-202 - shellOffset.x,-104 - shellOffset.y,106,108), result);
952-
result = display.map(null, button2, new Rectangle(10, 12, 114, 116));
953-
assertEquals(new Rectangle(-190 - shellOffset.x,-88 - shellOffset.y,114,116), result);
954-
955-
result = display.map(button2, null, rect);
956-
assertEquals(new Rectangle(shellOffset.x + 200,shellOffset.y + 100,100,100), result);
957-
result = display.map(button2, null, new Rectangle(-3, -6, 109, 112));
958-
assertEquals(new Rectangle(shellOffset.x + 197,shellOffset.y + 94,109,112), result);
959-
result = display.map(button2, null, new Rectangle(15, 18, 121, 124));
960-
assertEquals(new Rectangle(shellOffset.x + 215,shellOffset.y + 118,121,124), result);
952+
if (!SwtTestUtil.isGTK4()) { // Shell.getLocation gives 0,0 on Gtk 4
953+
Point shellOffset = shell.getLocation();
954+
result = display.map(null, button2, rect);
955+
assertEquals(new Rectangle(-200 - shellOffset.x, -100 - shellOffset.y, 100, 100), result);
956+
result = display.map(null, button2, new Rectangle(-2, -4, 106, 108));
957+
assertEquals(new Rectangle(-202 - shellOffset.x, -104 - shellOffset.y, 106, 108), result);
958+
result = display.map(null, button2, new Rectangle(10, 12, 114, 116));
959+
assertEquals(new Rectangle(-190 - shellOffset.x, -88 - shellOffset.y, 114, 116), result);
960+
961+
result = display.map(button2, null, rect);
962+
assertEquals(new Rectangle(shellOffset.x + 200, shellOffset.y + 100, 100, 100), result);
963+
result = display.map(button2, null, new Rectangle(-3, -6, 109, 112));
964+
assertEquals(new Rectangle(shellOffset.x + 197, shellOffset.y + 94, 109, 112), result);
965+
result = display.map(button2, null, new Rectangle(15, 18, 121, 124));
966+
assertEquals(new Rectangle(shellOffset.x + 215, shellOffset.y + 118, 121, 124), result);
967+
}
961968

962969
Shell shellMapTest1 = new Shell(display,SWT.NO_TRIM);
963970
Button buttonTest1 = new Button(shellMapTest1, SWT.PUSH);

0 commit comments

Comments
 (0)