@@ -1704,7 +1704,12 @@ public Control getCursorControl () {
17041704 */
17051705public Point getCursorLocation () {
17061706 checkDevice ();
1707- return DPIUtil .autoScaleDown (getCursorLocationInPixels ());
1707+ Point cursorLocationInPixels = getCursorLocationInPixels ();
1708+ if (isRescalingAtRuntime ()) {
1709+ return translateLocationInPointInDisplayCoordinateSystem (cursorLocationInPixels .x , cursorLocationInPixels .y );
1710+ } else {
1711+ return DPIUtil .autoScaleDown (cursorLocationInPixels );
1712+ }
17081713}
17091714
17101715Point getCursorLocationInPixels () {
@@ -2183,14 +2188,24 @@ Monitor getMonitor (long hmonitor) {
21832188 OS .GetMonitorInfo (hmonitor , lpmi );
21842189 Monitor monitor = new Monitor ();
21852190 monitor .handle = hmonitor ;
2186- Rectangle boundsInPixels = new Rectangle (lpmi .rcMonitor_left , lpmi .rcMonitor_top , lpmi .rcMonitor_right - lpmi .rcMonitor_left ,lpmi .rcMonitor_bottom - lpmi .rcMonitor_top );
2187- monitor .setBounds (DPIUtil .autoScaleDown (boundsInPixels ));
2188- Rectangle clientAreaInPixels = new Rectangle (lpmi .rcWork_left , lpmi .rcWork_top , lpmi .rcWork_right - lpmi .rcWork_left , lpmi .rcWork_bottom - lpmi .rcWork_top );
2189- monitor .setClientArea (DPIUtil .autoScaleDown (clientAreaInPixels ));
2191+ Rectangle boundsInPixels = new Rectangle (lpmi .rcMonitor_left , lpmi .rcMonitor_top , lpmi .rcMonitor_right - lpmi .rcMonitor_left ,lpmi .rcMonitor_bottom - lpmi .rcMonitor_top );
2192+ Rectangle clientAreaInPixels = new Rectangle (lpmi .rcWork_left , lpmi .rcWork_top , lpmi .rcWork_right - lpmi .rcWork_left , lpmi .rcWork_bottom - lpmi .rcWork_top );
21902193 int [] dpiX = new int [1 ];
21912194 int [] dpiY = new int [1 ];
21922195 int result = OS .GetDpiForMonitor (monitor .handle , OS .MDT_EFFECTIVE_DPI , dpiX , dpiY );
21932196 result = (result == OS .S_OK ) ? DPIUtil .mapDPIToZoom (dpiX [0 ]) : 100 ;
2197+
2198+ int autoscaleZoom ;
2199+ if (DPIUtil .isAutoScaleOnRuntimeActive ()) {
2200+ autoscaleZoom = DPIUtil .getZoomForAutoscaleProperty (result );
2201+ monitor .setBounds (getMonitorBoundsInPointsInDisplayCoordinateSystem (boundsInPixels , autoscaleZoom ));
2202+ monitor .setClientArea (getMonitorBoundsInPointsInDisplayCoordinateSystem (clientAreaInPixels , autoscaleZoom ));
2203+ } else {
2204+ autoscaleZoom = DPIUtil .getDeviceZoom ();
2205+ monitor .setBounds (DPIUtil .scaleDown (boundsInPixels , autoscaleZoom ));
2206+ monitor .setClientArea (DPIUtil .scaleDown (clientAreaInPixels , autoscaleZoom ));
2207+
2208+ }
21942209 if (result == 0 ) {
21952210 System .err .println ("***WARNING: GetDpiForMonitor: SWT could not get valid monitor scaling factor." );
21962211 result = 100 ;
@@ -2203,6 +2218,13 @@ Monitor getMonitor (long hmonitor) {
22032218 return monitor ;
22042219}
22052220
2221+ private Rectangle getMonitorBoundsInPointsInDisplayCoordinateSystem (Rectangle boundsInPixels , int zoom ) {
2222+ Rectangle bounds = DPIUtil .scaleDown (boundsInPixels , zoom );
2223+ bounds .x = boundsInPixels .x ;
2224+ bounds .y = boundsInPixels .y ;
2225+ return bounds ;
2226+ }
2227+
22062228/**
22072229 * Returns an array of monitors attached to the device.
22082230 *
@@ -2944,9 +2966,13 @@ boolean isValidThread () {
29442966public Point map (Control from , Control to , Point point ) {
29452967 checkDevice ();
29462968 if (point == null ) error (SWT .ERROR_NULL_ARGUMENT );
2947- int zoom = getZoomLevelForMapping (from , to );
2948- point = DPIUtil .scaleUp (point , zoom );
2949- return DPIUtil .scaleDown (mapInPixels (from , to , point ), zoom );
2969+ if (isRescalingAtRuntime ()) {
2970+ return map (from , to , point .x , point .y );
2971+ } else {
2972+ int zoom = getZoomLevelForMapping (from , to );
2973+ point = DPIUtil .scaleUp (point , zoom );
2974+ return DPIUtil .scaleDown (mapInPixels (from , to , point ), zoom );
2975+ }
29502976}
29512977
29522978Point mapInPixels (Control from , Control to , Point point ) {
@@ -2991,10 +3017,25 @@ Point mapInPixels (Control from, Control to, Point point) {
29913017 */
29923018public Point map (Control from , Control to , int x , int y ) {
29933019 checkDevice ();
2994- int zoom = getZoomLevelForMapping (from , to );
2995- x = DPIUtil .scaleUp (x , zoom );
2996- y = DPIUtil .scaleUp (y , zoom );
2997- return DPIUtil .scaleDown (mapInPixels (from , to , x , y ), zoom );
3020+ if (isRescalingAtRuntime ()) {
3021+ Point mappedPointInPoints ;
3022+ if (from == null ) {
3023+ Point mappedPointInpixels = mapInPixels (from , to , getPixelsFromPoint (to .getShell ().getMonitor (), x , y ));
3024+ mappedPointInPoints = DPIUtil .scaleDown (mappedPointInpixels , to .getZoom ());
3025+ } else if (to == null ) {
3026+ Point mappedPointInpixels = mapInPixels (from , to , DPIUtil .scaleUp (new Point (x , y ), from .getZoom ()));
3027+ mappedPointInPoints = getPointFromPixels (from .getShell ().getMonitor (), mappedPointInpixels .x , mappedPointInpixels .y );
3028+ } else {
3029+ Point mappedPointInpixels = mapInPixels (from , to , DPIUtil .scaleUp (new Point (x , y ), from .getZoom ()));
3030+ mappedPointInPoints = DPIUtil .scaleDown (mappedPointInpixels , to .getZoom ());
3031+ }
3032+ return mappedPointInPoints ;
3033+ } else {
3034+ int zoom = getZoomLevelForMapping (from , to );
3035+ x = DPIUtil .scaleUp (x , zoom );
3036+ y = DPIUtil .scaleUp (y , zoom );
3037+ return DPIUtil .scaleDown (mapInPixels (from , to , x , y ), zoom );
3038+ }
29983039}
29993040
30003041Point mapInPixels (Control from , Control to , int x , int y ) {
@@ -3058,9 +3099,13 @@ private int getZoomLevelForMapping(Control from, Control to) {
30583099public Rectangle map (Control from , Control to , Rectangle rectangle ) {
30593100 checkDevice ();
30603101 if (rectangle == null ) error (SWT .ERROR_NULL_ARGUMENT );
3061- int zoom = getZoomLevelForMapping (from , to );
3062- rectangle = DPIUtil .scaleUp (rectangle , zoom );
3063- return DPIUtil .scaleDown (mapInPixels (from , to , rectangle ), zoom );
3102+ if (isRescalingAtRuntime ()) {
3103+ return map (from , to , rectangle .x , rectangle .y , rectangle .width , rectangle .height );
3104+ } else {
3105+ int zoom = getZoomLevelForMapping (from , to );
3106+ rectangle = DPIUtil .scaleUp (rectangle , zoom );
3107+ return DPIUtil .scaleDown (mapInPixels (from , to , rectangle ), zoom );
3108+ }
30643109}
30653110
30663111Rectangle mapInPixels (Control from , Control to , Rectangle rectangle ) {
@@ -3107,12 +3152,27 @@ Rectangle mapInPixels (Control from, Control to, Rectangle rectangle) {
31073152 */
31083153public Rectangle map (Control from , Control to , int x , int y , int width , int height ) {
31093154 checkDevice ();
3110- int zoom = getZoomLevelForMapping (from , to );
3111- x = DPIUtil .scaleUp (x , zoom );
3112- y = DPIUtil .scaleUp (y , zoom );
3113- width = DPIUtil .scaleUp (width , zoom );
3114- height = DPIUtil .scaleUp (height , zoom );
3115- return DPIUtil .scaleDown (mapInPixels (from , to , x , y , width , height ), zoom );
3155+ if (isRescalingAtRuntime ()) {
3156+ Rectangle mappedRectangleInPoints ;
3157+ if (from == null ) {
3158+ Rectangle mappedRectangleInPixels = mapInPixels (from , to , translateRectangleInPixelsInDisplayCoordinateSystem (x , y , width , height , to .getShell ().getMonitor ()));
3159+ mappedRectangleInPoints = DPIUtil .scaleDown (mappedRectangleInPixels , to .getZoom ());
3160+ } else if (to == null ) {
3161+ Rectangle mappedRectangleInPixels = mapInPixels (from , to , DPIUtil .scaleUp (new Rectangle (x , y , width , height ), from .getZoom ()));
3162+ mappedRectangleInPoints = translateRectangleInPointsInDisplayCoordinateSystem (mappedRectangleInPixels .x , mappedRectangleInPixels .y , mappedRectangleInPixels .width , mappedRectangleInPixels .height , from .getShell ().getMonitor ());
3163+ } else {
3164+ Rectangle mappedRectangleInPixels = mapInPixels (from , to , DPIUtil .scaleUp (new Rectangle (x , y , width , height ), from .getZoom ()));
3165+ mappedRectangleInPoints = DPIUtil .scaleDown (mappedRectangleInPixels , to .getZoom ());
3166+ }
3167+ return mappedRectangleInPoints ;
3168+ } else {
3169+ int zoom = getZoomLevelForMapping (from , to );
3170+ x = DPIUtil .scaleUp (x , zoom );
3171+ y = DPIUtil .scaleUp (y , zoom );
3172+ width = DPIUtil .scaleUp (width , zoom );
3173+ height = DPIUtil .scaleUp (height , zoom );
3174+ return DPIUtil .scaleDown (mapInPixels (from , to , x , y , width , height ), zoom );
3175+ }
31163176}
31173177
31183178Rectangle mapInPixels (Control from , Control to , int x , int y , int width , int height ) {
@@ -3130,6 +3190,57 @@ Rectangle mapInPixels (Control from, Control to, int x, int y, int width, int he
31303190 return new Rectangle (rect .left , rect .top , rect .right - rect .left , rect .bottom - rect .top );
31313191}
31323192
3193+ Point translateLocationInPixelsInDisplayCoordinateSystem (int x , int y ) {
3194+ Monitor monitor = getContainingMonitor (x , y );
3195+ return getPixelsFromPoint (monitor , x , y );
3196+ }
3197+
3198+ Point translateLocationInPointInDisplayCoordinateSystem (int x , int y ) {
3199+ Monitor monitor = getContainingMonitorInPixelsCoordinate (x , y );
3200+ return getPointFromPixels (monitor , x , y );
3201+ }
3202+
3203+ Rectangle translateRectangleInPixelsInDisplayCoordinateSystemByContainment (int x , int y , int width , int height ) {
3204+ Monitor monitorByLocation = getContainingMonitor (x , y );
3205+ Monitor monitorByContainment = getContainingMonitor (x , y , width , height );
3206+ return translateRectangleInPixelsInDisplayCoordinateSystem (x , y , width , height , monitorByLocation , monitorByContainment );
3207+ }
3208+
3209+ private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem (int x , int y , int width , int height , Monitor monitor ) {
3210+ return translateRectangleInPixelsInDisplayCoordinateSystem (x , y , width , height , monitor , monitor );
3211+ }
3212+
3213+ private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem (int x , int y , int width , int height , Monitor monitorOfLocation , Monitor monitorOfArea ) {
3214+ Point topLeft = getPixelsFromPoint (monitorOfLocation , x , y );
3215+ int zoom = getApplicableMonitorZoom (monitorOfArea );
3216+ int widthInPixels = DPIUtil .scaleUp (width , zoom );
3217+ int heightInPixels = DPIUtil .scaleUp (height , zoom );
3218+ return new Rectangle (topLeft .x , topLeft .y , widthInPixels , heightInPixels );
3219+ }
3220+
3221+ Rectangle translateRectangleInPointsInDisplayCoordinateSystemByContainment (int x , int y , int widthInPixels , int heightInPixels ) {
3222+ Monitor monitorByLocation = getContainingMonitor (x , y );
3223+ Monitor monitorByContainment = getContainingMonitor (x , y , widthInPixels , heightInPixels );
3224+ return translateRectangleInPointsInDisplayCoordinateSystem (x , y , widthInPixels , heightInPixels , monitorByLocation , monitorByContainment );
3225+ }
3226+
3227+ private Rectangle translateRectangleInPointsInDisplayCoordinateSystem (int x , int y , int widthInPixels , int heightInPixels , Monitor monitor ) {
3228+ return translateRectangleInPointsInDisplayCoordinateSystem (x , y , widthInPixels , heightInPixels , monitor , monitor );
3229+ }
3230+
3231+
3232+ private Rectangle translateRectangleInPointsInDisplayCoordinateSystem (int x , int y , int widthInPixels , int heightInPixels , Monitor monitorOfLocation , Monitor monitorOfArea ) {
3233+ Point topLeft = getPointFromPixels (monitorOfLocation , x , y );
3234+ int zoom = getApplicableMonitorZoom (monitorOfArea );
3235+ int width = DPIUtil .scaleDown (widthInPixels , zoom );
3236+ int height = DPIUtil .scaleDown (heightInPixels , zoom );
3237+ return new Rectangle (topLeft .x , topLeft .y , width , height );
3238+ }
3239+
3240+ private int getApplicableMonitorZoom (Monitor monitor ) {
3241+ return DPIUtil .getZoomForAutoscaleProperty (isRescalingAtRuntime () ? monitor .zoom : getDeviceZoom ());
3242+ }
3243+
31333244long messageProc (long hwnd , long msg , long wParam , long lParam ) {
31343245 switch ((int )msg ) {
31353246 case SWT_RUNASYNC : {
@@ -4355,7 +4466,12 @@ public void sendPostExternalEventDispatchEvent () {
43554466 */
43564467public void setCursorLocation (int x , int y ) {
43574468 checkDevice ();
4358- setCursorLocationInPixels (DPIUtil .autoScaleUp (x ), DPIUtil .autoScaleUp (y ));
4469+ if (isRescalingAtRuntime ()) {
4470+ Point cursorLocationInPixels = translateLocationInPixelsInDisplayCoordinateSystem (x , y );
4471+ setCursorLocationInPixels (cursorLocationInPixels .x , cursorLocationInPixels .y );
4472+ } else {
4473+ setCursorLocationInPixels (DPIUtil .autoScaleUp (x ), DPIUtil .autoScaleUp (y ));
4474+ }
43594475}
43604476
43614477void setCursorLocationInPixels (int x , int y ) {
@@ -5390,4 +5506,63 @@ private boolean setDPIAwareness(int desiredDpiAwareness) {
53905506 return true ;
53915507}
53925508
5509+ private Monitor getContainingMonitor (int x , int y ) {
5510+ Monitor [] monitors = getMonitors ();
5511+ for (Monitor currentMonitor : monitors ) {
5512+ Rectangle clientArea = currentMonitor .getClientArea ();
5513+ if (clientArea .contains (x , y )) {
5514+ return currentMonitor ;
5515+ }
5516+ }
5517+ return getPrimaryMonitor ();
5518+ }
5519+
5520+ private Monitor getContainingMonitor (int x , int y , int width , int height ) {
5521+ Rectangle rectangle = new Rectangle (x , y , width , height );
5522+ Monitor [] monitors = getMonitors ();
5523+ Monitor selectedMonitor = getPrimaryMonitor ();
5524+ int highestArea = 0 ;
5525+ for (Monitor currentMonitor : monitors ) {
5526+ Rectangle clientArea = currentMonitor .getClientArea ();
5527+ Rectangle intersection = clientArea .intersection (rectangle );
5528+ int area = intersection .width * intersection .height ;
5529+ if (area > highestArea ) {
5530+ selectedMonitor = currentMonitor ;
5531+ highestArea = area ;
5532+ }
5533+ }
5534+ return selectedMonitor ;
5535+ }
5536+
5537+ private Monitor getContainingMonitorInPixelsCoordinate (int xInPixels , int yInPixels ) {
5538+ Monitor [] monitors = getMonitors ();
5539+ for (Monitor current : monitors ) {
5540+ Rectangle clientArea = getMonitorClientAreaInPixels (current );
5541+ if (clientArea .contains (xInPixels , yInPixels )) {
5542+ return current ;
5543+ }
5544+ }
5545+ return getPrimaryMonitor ();
5546+ }
5547+
5548+ private Rectangle getMonitorClientAreaInPixels (Monitor monitor ) {
5549+ int zoom = getApplicableMonitorZoom (monitor );
5550+ int widthInPixels = DPIUtil .scaleUp (monitor .clientWidth , zoom );
5551+ int heightInPixels = DPIUtil .scaleUp (monitor .clientHeight , zoom );
5552+ return new Rectangle (monitor .clientX , monitor .clientY , widthInPixels , heightInPixels );
5553+ }
5554+
5555+ private Point getPixelsFromPoint (Monitor monitor , int x , int y ) {
5556+ int zoom = getApplicableMonitorZoom (monitor );
5557+ int mappedX = DPIUtil .scaleUp (x - monitor .clientX , zoom ) + monitor .clientX ;
5558+ int mappedY = DPIUtil .scaleUp (y - monitor .clientY , zoom ) + monitor .clientY ;
5559+ return new Point (mappedX , mappedY );
5560+ }
5561+
5562+ private Point getPointFromPixels (Monitor monitor , int x , int y ) {
5563+ int zoom = getApplicableMonitorZoom (monitor );
5564+ int mappedX = DPIUtil .scaleDown (x - monitor .clientX , zoom ) + monitor .clientX ;
5565+ int mappedY = DPIUtil .scaleDown (y - monitor .clientY , zoom ) + monitor .clientY ;
5566+ return new Point (mappedX , mappedY );
5567+ }
53935568}
0 commit comments