@@ -1691,7 +1691,8 @@ public Control getCursorControl () {
1691
1691
*/
1692
1692
public Point getCursorLocation () {
1693
1693
checkDevice ();
1694
- return DPIUtil .autoScaleDown (getCursorLocationInPixels ());
1694
+ Point cursorLocationInPixels = getCursorLocationInPixels ();
1695
+ return translateLocationInPointInDisplayCoordinateSystem (cursorLocationInPixels .x , cursorLocationInPixels .y );
1695
1696
}
1696
1697
1697
1698
Point getCursorLocationInPixels () {
@@ -2170,18 +2171,25 @@ Monitor getMonitor (long hmonitor) {
2170
2171
OS .GetMonitorInfo (hmonitor , lpmi );
2171
2172
Monitor monitor = new Monitor ();
2172
2173
monitor .handle = hmonitor ;
2173
- Rectangle boundsInPixels = new Rectangle (lpmi .rcMonitor_left , lpmi .rcMonitor_top , lpmi .rcMonitor_right - lpmi .rcMonitor_left ,lpmi .rcMonitor_bottom - lpmi .rcMonitor_top );
2174
- monitor .setBounds (DPIUtil .autoScaleDown (boundsInPixels ));
2175
- Rectangle clientAreaInPixels = new Rectangle (lpmi .rcWork_left , lpmi .rcWork_top , lpmi .rcWork_right - lpmi .rcWork_left , lpmi .rcWork_bottom - lpmi .rcWork_top );
2176
- monitor .setClientArea (DPIUtil .autoScaleDown (clientAreaInPixels ));
2174
+ Rectangle boundsInPixels = new Rectangle (lpmi .rcMonitor_left , lpmi .rcMonitor_top , lpmi .rcMonitor_right - lpmi .rcMonitor_left ,lpmi .rcMonitor_bottom - lpmi .rcMonitor_top );
2175
+ Rectangle clientAreaInPixels = new Rectangle (lpmi .rcWork_left , lpmi .rcWork_top , lpmi .rcWork_right - lpmi .rcWork_left , lpmi .rcWork_bottom - lpmi .rcWork_top );
2177
2176
int [] dpiX = new int [1 ];
2178
2177
int [] dpiY = new int [1 ];
2179
2178
int result = OS .GetDpiForMonitor (monitor .handle , OS .MDT_EFFECTIVE_DPI , dpiX , dpiY );
2180
2179
result = (result == OS .S_OK ) ? DPIUtil .mapDPIToZoom (dpiX [0 ]) : 100 ;
2180
+
2181
+ int autoscaleZoom ;
2182
+ if (DPIUtil .isAutoScaleOnRuntimeActive ()) {
2183
+ autoscaleZoom = DPIUtil .getZoomForAutoscaleProperty (result );
2184
+ } else {
2185
+ autoscaleZoom = DPIUtil .getDeviceZoom ();
2186
+ }
2181
2187
if (result == 0 ) {
2182
2188
System .err .println ("***WARNING: GetDpiForMonitor: SWT could not get valid monitor scaling factor." );
2183
2189
result = 100 ;
2184
2190
}
2191
+ monitor .setBounds (getMonitorBoundsInPointsInDisplayCoordinateSystem (boundsInPixels , autoscaleZoom ));
2192
+ monitor .setClientArea (getMonitorBoundsInPointsInDisplayCoordinateSystem (clientAreaInPixels , autoscaleZoom ));
2185
2193
/*
2186
2194
* Always return true monitor zoom value as fetched from native, else will lead
2187
2195
* to scaling issue on OS Win8.1 and above, for more details refer bug 537614.
@@ -2190,6 +2198,13 @@ Monitor getMonitor (long hmonitor) {
2190
2198
return monitor ;
2191
2199
}
2192
2200
2201
+ private Rectangle getMonitorBoundsInPointsInDisplayCoordinateSystem (Rectangle boundsInPixels , int zoom ) {
2202
+ Rectangle bounds = DPIUtil .scaleDown (boundsInPixels , zoom );
2203
+ bounds .x = boundsInPixels .x ;
2204
+ bounds .y = boundsInPixels .y ;
2205
+ return bounds ;
2206
+ }
2207
+
2193
2208
/**
2194
2209
* Returns an array of monitors attached to the device.
2195
2210
*
@@ -2969,9 +2984,7 @@ boolean isValidThread () {
2969
2984
public Point map (Control from , Control to , Point point ) {
2970
2985
checkDevice ();
2971
2986
if (point == null ) error (SWT .ERROR_NULL_ARGUMENT );
2972
- int zoom = getZoomLevelForMapping (from , to );
2973
- point = DPIUtil .scaleUp (point , zoom );
2974
- return DPIUtil .scaleDown (mapInPixels (from , to , point ), zoom );
2987
+ return map (from , to , point .x , point .y );
2975
2988
}
2976
2989
2977
2990
Point mapInPixels (Control from , Control to , Point point ) {
@@ -3016,10 +3029,18 @@ Point mapInPixels (Control from, Control to, Point point) {
3016
3029
*/
3017
3030
public Point map (Control from , Control to , int x , int y ) {
3018
3031
checkDevice ();
3019
- int zoom = getZoomLevelForMapping (from , to );
3020
- x = DPIUtil .scaleUp (x , zoom );
3021
- y = DPIUtil .scaleUp (y , zoom );
3022
- return DPIUtil .scaleDown (mapInPixels (from , to , x , y ), zoom );
3032
+ Point mappedPointInPoints ;
3033
+ if (from == null ) {
3034
+ Point mappedPointInpixels = mapInPixels (from , to , getPixelsFromPoint (to .getShell ().getMonitor (), x , y ));
3035
+ mappedPointInPoints = DPIUtil .scaleDown (mappedPointInpixels , to .getZoom ());
3036
+ } else if (to == null ) {
3037
+ Point mappedPointInpixels = mapInPixels (from , to , DPIUtil .scaleUp (new Point (x , y ), from .getZoom ()));
3038
+ mappedPointInPoints = getPointFromPixels (from .getShell ().getMonitor (), mappedPointInpixels .x , mappedPointInpixels .y );
3039
+ } else {
3040
+ Point mappedPointInpixels = mapInPixels (from , to , DPIUtil .scaleUp (new Point (x , y ), from .getZoom ()));
3041
+ mappedPointInPoints = DPIUtil .scaleDown (mappedPointInpixels , to .getZoom ());
3042
+ }
3043
+ return mappedPointInPoints ;
3023
3044
}
3024
3045
3025
3046
Point mapInPixels (Control from , Control to , int x , int y ) {
@@ -3035,15 +3056,6 @@ Point mapInPixels (Control from, Control to, int x, int y) {
3035
3056
return new Point (point .x , point .y );
3036
3057
}
3037
3058
3038
- private int getZoomLevelForMapping (Control from , Control to ) {
3039
- if (from != null && from .isDisposed ()) error (SWT .ERROR_INVALID_ARGUMENT );
3040
- if (to != null && to .isDisposed ()) error (SWT .ERROR_INVALID_ARGUMENT );
3041
- if (to != null ) {
3042
- return to .getZoom ();
3043
- }
3044
- return from .getZoom ();
3045
- }
3046
-
3047
3059
/**
3048
3060
* Maps a point from one coordinate system to another.
3049
3061
* When the control is null, coordinates are mapped to
@@ -3083,9 +3095,7 @@ private int getZoomLevelForMapping(Control from, Control to) {
3083
3095
public Rectangle map (Control from , Control to , Rectangle rectangle ) {
3084
3096
checkDevice ();
3085
3097
if (rectangle == null ) error (SWT .ERROR_NULL_ARGUMENT );
3086
- int zoom = getZoomLevelForMapping (from , to );
3087
- rectangle = DPIUtil .scaleUp (rectangle , zoom );
3088
- return DPIUtil .scaleDown (mapInPixels (from , to , rectangle ), zoom );
3098
+ return map (from , to , rectangle .x , rectangle .y , rectangle .width , rectangle .height );
3089
3099
}
3090
3100
3091
3101
Rectangle mapInPixels (Control from , Control to , Rectangle rectangle ) {
@@ -3132,12 +3142,18 @@ Rectangle mapInPixels (Control from, Control to, Rectangle rectangle) {
3132
3142
*/
3133
3143
public Rectangle map (Control from , Control to , int x , int y , int width , int height ) {
3134
3144
checkDevice ();
3135
- int zoom = getZoomLevelForMapping (from , to );
3136
- x = DPIUtil .scaleUp (x , zoom );
3137
- y = DPIUtil .scaleUp (y , zoom );
3138
- width = DPIUtil .scaleUp (width , zoom );
3139
- height = DPIUtil .scaleUp (height , zoom );
3140
- return DPIUtil .scaleDown (mapInPixels (from , to , x , y , width , height ), zoom );
3145
+ Rectangle mappedRectangleInPoints ;
3146
+ if (from == null ) {
3147
+ Rectangle mappedRectangleInPixels = mapInPixels (from , to , translateRectangleInPixelsInDisplayCoordinateSystem (x , y , width , height , to .getShell ().getMonitor ()));
3148
+ mappedRectangleInPoints = DPIUtil .scaleDown (mappedRectangleInPixels , to .getZoom ());
3149
+ } else if (to == null ) {
3150
+ Rectangle mappedRectangleInPixels = mapInPixels (from , to , DPIUtil .scaleUp (new Rectangle (x , y , width , height ), from .getZoom ()));
3151
+ mappedRectangleInPoints = translateRectangleInPointsInDisplayCoordinateSystem (mappedRectangleInPixels .x , mappedRectangleInPixels .y , mappedRectangleInPixels .width , mappedRectangleInPixels .height , from .getShell ().getMonitor ());
3152
+ } else {
3153
+ Rectangle mappedRectangleInPixels = mapInPixels (from , to , DPIUtil .scaleUp (new Rectangle (x , y , width , height ), from .getZoom ()));
3154
+ mappedRectangleInPoints = DPIUtil .scaleDown (mappedRectangleInPixels , to .getZoom ());
3155
+ }
3156
+ return mappedRectangleInPoints ;
3141
3157
}
3142
3158
3143
3159
Rectangle mapInPixels (Control from , Control to , int x , int y , int width , int height ) {
@@ -3155,6 +3171,53 @@ Rectangle mapInPixels (Control from, Control to, int x, int y, int width, int he
3155
3171
return new Rectangle (rect .left , rect .top , rect .right - rect .left , rect .bottom - rect .top );
3156
3172
}
3157
3173
3174
+ Point translateLocationInPixelsInDisplayCoordinateSystem (int x , int y ) {
3175
+ Monitor monitor = getContainingMonitor (x , y );
3176
+ return getPixelsFromPoint (monitor , x , y );
3177
+ }
3178
+
3179
+ Point translateLocationInPointInDisplayCoordinateSystem (int x , int y ) {
3180
+ Monitor monitor = getContainingMonitorInPixelsCoordinate (x , y );
3181
+ return getPointFromPixels (monitor , x , y );
3182
+ }
3183
+
3184
+ Rectangle translateRectangleInPixelsInDisplayCoordinateSystemByContainment (int x , int y , int width , int height ) {
3185
+ Monitor monitorByLocation = getContainingMonitor (x , y );
3186
+ Monitor monitorByContainment = getContainingMonitor (x , y , width , height );
3187
+ return translateRectangleInPixelsInDisplayCoordinateSystem (x , y , width , height , monitorByLocation , monitorByContainment );
3188
+ }
3189
+
3190
+ private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem (int x , int y , int width , int height , Monitor monitor ) {
3191
+ return translateRectangleInPixelsInDisplayCoordinateSystem (x , y , width , height , monitor , monitor );
3192
+ }
3193
+
3194
+ private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem (int x , int y , int width , int height , Monitor monitorOfLocation , Monitor monitorOfArea ) {
3195
+ Point topLeft = getPixelsFromPoint (monitorOfLocation , x , y );
3196
+ int zoom = DPIUtil .getZoomForAutoscaleProperty (monitorOfArea .zoom );
3197
+ int widthInPixels = DPIUtil .scaleUp (width , zoom );
3198
+ int heightInPixels = DPIUtil .scaleUp (height , zoom );
3199
+ return new Rectangle (topLeft .x , topLeft .y , widthInPixels , heightInPixels );
3200
+ }
3201
+
3202
+ Rectangle translateRectangleInPointsInDisplayCoordinateSystemByContainment (int x , int y , int widthInPixels , int heightInPixels ) {
3203
+ Monitor monitorByLocation = getContainingMonitor (x , y );
3204
+ Monitor monitorByContainment = getContainingMonitor (x , y , widthInPixels , heightInPixels );
3205
+ return translateRectangleInPointsInDisplayCoordinateSystem (x , y , widthInPixels , heightInPixels , monitorByLocation , monitorByContainment );
3206
+ }
3207
+
3208
+ private Rectangle translateRectangleInPointsInDisplayCoordinateSystem (int x , int y , int widthInPixels , int heightInPixels , Monitor monitor ) {
3209
+ return translateRectangleInPointsInDisplayCoordinateSystem (x , y , widthInPixels , heightInPixels , monitor , monitor );
3210
+ }
3211
+
3212
+
3213
+ private Rectangle translateRectangleInPointsInDisplayCoordinateSystem (int x , int y , int widthInPixels , int heightInPixels , Monitor monitorOfLocation , Monitor monitorOfArea ) {
3214
+ Point topLeft = getPointFromPixels (monitorOfLocation , x , y );
3215
+ int zoom = DPIUtil .getZoomForAutoscaleProperty (monitorOfArea .zoom );
3216
+ int width = DPIUtil .scaleDown (widthInPixels , zoom );
3217
+ int height = DPIUtil .scaleDown (heightInPixels , zoom );
3218
+ return new Rectangle (topLeft .x , topLeft .y , width , height );
3219
+ }
3220
+
3158
3221
long messageProc (long hwnd , long msg , long wParam , long lParam ) {
3159
3222
switch ((int )msg ) {
3160
3223
case SWT_RUNASYNC : {
@@ -4380,7 +4443,8 @@ public void sendPostExternalEventDispatchEvent () {
4380
4443
*/
4381
4444
public void setCursorLocation (int x , int y ) {
4382
4445
checkDevice ();
4383
- setCursorLocationInPixels (DPIUtil .autoScaleUp (x ), DPIUtil .autoScaleUp (y ));
4446
+ Point cursorLocationInPixels = translateLocationInPixelsInDisplayCoordinateSystem (x , y );
4447
+ setCursorLocationInPixels (cursorLocationInPixels .x , cursorLocationInPixels .y );
4384
4448
}
4385
4449
4386
4450
void setCursorLocationInPixels (int x , int y ) {
@@ -5310,4 +5374,63 @@ private boolean setDPIAwareness(int desiredDpiAwareness) {
5310
5374
return true ;
5311
5375
}
5312
5376
5377
+ private Monitor getContainingMonitor (int x , int y ) {
5378
+ Monitor [] monitors = getMonitors ();
5379
+ for (Monitor currentMonitor : monitors ) {
5380
+ Rectangle clientArea = currentMonitor .getClientArea ();
5381
+ if (clientArea .contains (x , y )) {
5382
+ return currentMonitor ;
5383
+ }
5384
+ }
5385
+ return getPrimaryMonitor ();
5386
+ }
5387
+
5388
+ private Monitor getContainingMonitor (int x , int y , int width , int height ) {
5389
+ Rectangle rectangle = new Rectangle (x , y , width , height );
5390
+ Monitor [] monitors = getMonitors ();
5391
+ Monitor selectedMonitor = getPrimaryMonitor ();
5392
+ int highestArea = 0 ;
5393
+ for (Monitor currentMonitor : monitors ) {
5394
+ Rectangle clientArea = currentMonitor .getClientArea ();
5395
+ Rectangle intersection = clientArea .intersection (rectangle );
5396
+ int area = intersection .width * intersection .height ;
5397
+ if (area > highestArea ) {
5398
+ selectedMonitor = currentMonitor ;
5399
+ highestArea = area ;
5400
+ }
5401
+ }
5402
+ return selectedMonitor ;
5403
+ }
5404
+
5405
+ private Monitor getContainingMonitorInPixelsCoordinate (int xInPixels , int yInPixels ) {
5406
+ Monitor [] monitors = getMonitors ();
5407
+ for (Monitor current : monitors ) {
5408
+ Rectangle clientArea = getMonitorClientAreaInPixels (current );
5409
+ if (clientArea .contains (xInPixels , yInPixels )) {
5410
+ return current ;
5411
+ }
5412
+ }
5413
+ return getPrimaryMonitor ();
5414
+ }
5415
+
5416
+ private Rectangle getMonitorClientAreaInPixels (Monitor monitor ) {
5417
+ int zoom = DPIUtil .getZoomForAutoscaleProperty (monitor .zoom );
5418
+ int widthInPixels = DPIUtil .scaleUp (monitor .clientWidth , zoom );
5419
+ int heightInPixels = DPIUtil .scaleUp (monitor .clientHeight , zoom );
5420
+ return new Rectangle (monitor .clientX , monitor .clientY , widthInPixels , heightInPixels );
5421
+ }
5422
+
5423
+ private Point getPixelsFromPoint (Monitor monitor , int x , int y ) {
5424
+ int zoom = DPIUtil .getZoomForAutoscaleProperty (monitor .zoom );
5425
+ int mappedX = DPIUtil .scaleUp (x - monitor .clientX , zoom ) + monitor .clientX ;
5426
+ int mappedY = DPIUtil .scaleUp (y - monitor .clientY , zoom ) + monitor .clientY ;
5427
+ return new Point (mappedX , mappedY );
5428
+ }
5429
+
5430
+ private Point getPointFromPixels (Monitor monitor , int x , int y ) {
5431
+ int zoom = DPIUtil .getZoomForAutoscaleProperty (monitor .zoom );
5432
+ int mappedX = DPIUtil .scaleDown (x - monitor .clientX , zoom ) + monitor .clientX ;
5433
+ int mappedY = DPIUtil .scaleDown (y - monitor .clientY , zoom ) + monitor .clientY ;
5434
+ return new Point (mappedX , mappedY );
5435
+ }
5313
5436
}
0 commit comments