@@ -1704,8 +1704,7 @@ public Control getCursorControl () {
17041704 */
17051705public Point getCursorLocation () {
17061706 checkDevice ();
1707- Point cursorLocationInPixels = getCursorLocationInPixels ();
1708- return translateLocationInPointInDisplayCoordinateSystem (cursorLocationInPixels .x , cursorLocationInPixels .y );
1707+ return DPIUtil .autoScaleDown (getCursorLocationInPixels ());
17091708}
17101709
17111710Point getCursorLocationInPixels () {
@@ -2184,25 +2183,18 @@ Monitor getMonitor (long hmonitor) {
21842183 OS .GetMonitorInfo (hmonitor , lpmi );
21852184 Monitor monitor = new Monitor ();
21862185 monitor .handle = hmonitor ;
2187- Rectangle boundsInPixels = new Rectangle (lpmi .rcMonitor_left , lpmi .rcMonitor_top , lpmi .rcMonitor_right - lpmi .rcMonitor_left ,lpmi .rcMonitor_bottom - lpmi .rcMonitor_top );
2188- Rectangle clientAreaInPixels = new Rectangle (lpmi .rcWork_left , lpmi .rcWork_top , lpmi .rcWork_right - lpmi .rcWork_left , lpmi .rcWork_bottom - lpmi .rcWork_top );
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 ));
21892190 int [] dpiX = new int [1 ];
21902191 int [] dpiY = new int [1 ];
21912192 int result = OS .GetDpiForMonitor (monitor .handle , OS .MDT_EFFECTIVE_DPI , dpiX , dpiY );
21922193 result = (result == OS .S_OK ) ? DPIUtil .mapDPIToZoom (dpiX [0 ]) : 100 ;
2193-
2194- int autoscaleZoom ;
2195- if (DPIUtil .isAutoScaleOnRuntimeActive ()) {
2196- autoscaleZoom = DPIUtil .getZoomForAutoscaleProperty (result );
2197- } else {
2198- autoscaleZoom = DPIUtil .getDeviceZoom ();
2199- }
22002194 if (result == 0 ) {
22012195 System .err .println ("***WARNING: GetDpiForMonitor: SWT could not get valid monitor scaling factor." );
22022196 result = 100 ;
22032197 }
2204- monitor .setBounds (getMonitorBoundsInPointsInDisplayCoordinateSystem (boundsInPixels , autoscaleZoom ));
2205- monitor .setClientArea (getMonitorBoundsInPointsInDisplayCoordinateSystem (clientAreaInPixels , autoscaleZoom ));
22062198 /*
22072199 * Always return true monitor zoom value as fetched from native, else will lead
22082200 * to scaling issue on OS Win8.1 and above, for more details refer bug 537614.
@@ -2211,13 +2203,6 @@ Monitor getMonitor (long hmonitor) {
22112203 return monitor ;
22122204}
22132205
2214- private Rectangle getMonitorBoundsInPointsInDisplayCoordinateSystem (Rectangle boundsInPixels , int zoom ) {
2215- Rectangle bounds = DPIUtil .scaleDown (boundsInPixels , zoom );
2216- bounds .x = boundsInPixels .x ;
2217- bounds .y = boundsInPixels .y ;
2218- return bounds ;
2219- }
2220-
22212206/**
22222207 * Returns an array of monitors attached to the device.
22232208 *
@@ -2959,7 +2944,9 @@ boolean isValidThread () {
29592944public Point map (Control from , Control to , Point point ) {
29602945 checkDevice ();
29612946 if (point == null ) error (SWT .ERROR_NULL_ARGUMENT );
2962- return map (from , to , point .x , point .y );
2947+ int zoom = getZoomLevelForMapping (from , to );
2948+ point = DPIUtil .scaleUp (point , zoom );
2949+ return DPIUtil .scaleDown (mapInPixels (from , to , point ), zoom );
29632950}
29642951
29652952Point mapInPixels (Control from , Control to , Point point ) {
@@ -3004,18 +2991,10 @@ Point mapInPixels (Control from, Control to, Point point) {
30042991 */
30052992public Point map (Control from , Control to , int x , int y ) {
30062993 checkDevice ();
3007- Point mappedPointInPoints ;
3008- if (from == null ) {
3009- Point mappedPointInpixels = mapInPixels (from , to , getPixelsFromPoint (to .getShell ().getMonitor (), x , y ));
3010- mappedPointInPoints = DPIUtil .scaleDown (mappedPointInpixels , to .getZoom ());
3011- } else if (to == null ) {
3012- Point mappedPointInpixels = mapInPixels (from , to , DPIUtil .scaleUp (new Point (x , y ), from .getZoom ()));
3013- mappedPointInPoints = getPointFromPixels (from .getShell ().getMonitor (), mappedPointInpixels .x , mappedPointInpixels .y );
3014- } else {
3015- Point mappedPointInpixels = mapInPixels (from , to , DPIUtil .scaleUp (new Point (x , y ), from .getZoom ()));
3016- mappedPointInPoints = DPIUtil .scaleDown (mappedPointInpixels , to .getZoom ());
3017- }
3018- return mappedPointInPoints ;
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 );
30192998}
30202999
30213000Point mapInPixels (Control from , Control to , int x , int y ) {
@@ -3031,6 +3010,15 @@ Point mapInPixels (Control from, Control to, int x, int y) {
30313010 return new Point (point .x , point .y );
30323011}
30333012
3013+ private int getZoomLevelForMapping (Control from , Control to ) {
3014+ if (from != null && from .isDisposed ()) error (SWT .ERROR_INVALID_ARGUMENT );
3015+ if (to != null && to .isDisposed ()) error (SWT .ERROR_INVALID_ARGUMENT );
3016+ if (to != null ) {
3017+ return to .getZoom ();
3018+ }
3019+ return from .getZoom ();
3020+ }
3021+
30343022/**
30353023 * Maps a point from one coordinate system to another.
30363024 * When the control is null, coordinates are mapped to
@@ -3070,7 +3058,9 @@ Point mapInPixels (Control from, Control to, int x, int y) {
30703058public Rectangle map (Control from , Control to , Rectangle rectangle ) {
30713059 checkDevice ();
30723060 if (rectangle == null ) error (SWT .ERROR_NULL_ARGUMENT );
3073- return map (from , to , rectangle .x , rectangle .y , rectangle .width , rectangle .height );
3061+ int zoom = getZoomLevelForMapping (from , to );
3062+ rectangle = DPIUtil .scaleUp (rectangle , zoom );
3063+ return DPIUtil .scaleDown (mapInPixels (from , to , rectangle ), zoom );
30743064}
30753065
30763066Rectangle mapInPixels (Control from , Control to , Rectangle rectangle ) {
@@ -3117,18 +3107,12 @@ Rectangle mapInPixels (Control from, Control to, Rectangle rectangle) {
31173107 */
31183108public Rectangle map (Control from , Control to , int x , int y , int width , int height ) {
31193109 checkDevice ();
3120- Rectangle mappedRectangleInPoints ;
3121- if (from == null ) {
3122- Rectangle mappedRectangleInPixels = mapInPixels (from , to , translateRectangleInPixelsInDisplayCoordinateSystem (x , y , width , height , to .getShell ().getMonitor ()));
3123- mappedRectangleInPoints = DPIUtil .scaleDown (mappedRectangleInPixels , to .getZoom ());
3124- } else if (to == null ) {
3125- Rectangle mappedRectangleInPixels = mapInPixels (from , to , DPIUtil .scaleUp (new Rectangle (x , y , width , height ), from .getZoom ()));
3126- mappedRectangleInPoints = translateRectangleInPointsInDisplayCoordinateSystem (mappedRectangleInPixels .x , mappedRectangleInPixels .y , mappedRectangleInPixels .width , mappedRectangleInPixels .height , from .getShell ().getMonitor ());
3127- } else {
3128- Rectangle mappedRectangleInPixels = mapInPixels (from , to , DPIUtil .scaleUp (new Rectangle (x , y , width , height ), from .getZoom ()));
3129- mappedRectangleInPoints = DPIUtil .scaleDown (mappedRectangleInPixels , to .getZoom ());
3130- }
3131- return mappedRectangleInPoints ;
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 );
31323116}
31333117
31343118Rectangle mapInPixels (Control from , Control to , int x , int y , int width , int height ) {
@@ -3146,57 +3130,6 @@ Rectangle mapInPixels (Control from, Control to, int x, int y, int width, int he
31463130 return new Rectangle (rect .left , rect .top , rect .right - rect .left , rect .bottom - rect .top );
31473131}
31483132
3149- Point translateLocationInPixelsInDisplayCoordinateSystem (int x , int y ) {
3150- Monitor monitor = getContainingMonitor (x , y );
3151- return getPixelsFromPoint (monitor , x , y );
3152- }
3153-
3154- Point translateLocationInPointInDisplayCoordinateSystem (int x , int y ) {
3155- Monitor monitor = getContainingMonitorInPixelsCoordinate (x , y );
3156- return getPointFromPixels (monitor , x , y );
3157- }
3158-
3159- Rectangle translateRectangleInPixelsInDisplayCoordinateSystemByContainment (int x , int y , int width , int height ) {
3160- Monitor monitorByLocation = getContainingMonitor (x , y );
3161- Monitor monitorByContainment = getContainingMonitor (x , y , width , height );
3162- return translateRectangleInPixelsInDisplayCoordinateSystem (x , y , width , height , monitorByLocation , monitorByContainment );
3163- }
3164-
3165- private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem (int x , int y , int width , int height , Monitor monitor ) {
3166- return translateRectangleInPixelsInDisplayCoordinateSystem (x , y , width , height , monitor , monitor );
3167- }
3168-
3169- private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem (int x , int y , int width , int height , Monitor monitorOfLocation , Monitor monitorOfArea ) {
3170- Point topLeft = getPixelsFromPoint (monitorOfLocation , x , y );
3171- int zoom = getApplicableMonitorZoom (monitorOfArea );
3172- int widthInPixels = DPIUtil .scaleUp (width , zoom );
3173- int heightInPixels = DPIUtil .scaleUp (height , zoom );
3174- return new Rectangle (topLeft .x , topLeft .y , widthInPixels , heightInPixels );
3175- }
3176-
3177- Rectangle translateRectangleInPointsInDisplayCoordinateSystemByContainment (int x , int y , int widthInPixels , int heightInPixels ) {
3178- Monitor monitorByLocation = getContainingMonitor (x , y );
3179- Monitor monitorByContainment = getContainingMonitor (x , y , widthInPixels , heightInPixels );
3180- return translateRectangleInPointsInDisplayCoordinateSystem (x , y , widthInPixels , heightInPixels , monitorByLocation , monitorByContainment );
3181- }
3182-
3183- private Rectangle translateRectangleInPointsInDisplayCoordinateSystem (int x , int y , int widthInPixels , int heightInPixels , Monitor monitor ) {
3184- return translateRectangleInPointsInDisplayCoordinateSystem (x , y , widthInPixels , heightInPixels , monitor , monitor );
3185- }
3186-
3187-
3188- private Rectangle translateRectangleInPointsInDisplayCoordinateSystem (int x , int y , int widthInPixels , int heightInPixels , Monitor monitorOfLocation , Monitor monitorOfArea ) {
3189- Point topLeft = getPointFromPixels (monitorOfLocation , x , y );
3190- int zoom = getApplicableMonitorZoom (monitorOfArea );
3191- int width = DPIUtil .scaleDown (widthInPixels , zoom );
3192- int height = DPIUtil .scaleDown (heightInPixels , zoom );
3193- return new Rectangle (topLeft .x , topLeft .y , width , height );
3194- }
3195-
3196- private int getApplicableMonitorZoom (Monitor monitor ) {
3197- return DPIUtil .getZoomForAutoscaleProperty (isRescalingAtRuntime () ? monitor .zoom : getDeviceZoom ());
3198- }
3199-
32003133long messageProc (long hwnd , long msg , long wParam , long lParam ) {
32013134 switch ((int )msg ) {
32023135 case SWT_RUNASYNC : {
@@ -4422,8 +4355,7 @@ public void sendPostExternalEventDispatchEvent () {
44224355 */
44234356public void setCursorLocation (int x , int y ) {
44244357 checkDevice ();
4425- Point cursorLocationInPixels = translateLocationInPixelsInDisplayCoordinateSystem (x , y );
4426- setCursorLocationInPixels (cursorLocationInPixels .x , cursorLocationInPixels .y );
4358+ setCursorLocationInPixels (DPIUtil .autoScaleUp (x ), DPIUtil .autoScaleUp (y ));
44274359}
44284360
44294361void setCursorLocationInPixels (int x , int y ) {
@@ -5458,63 +5390,4 @@ private boolean setDPIAwareness(int desiredDpiAwareness) {
54585390 return true ;
54595391}
54605392
5461- private Monitor getContainingMonitor (int x , int y ) {
5462- Monitor [] monitors = getMonitors ();
5463- for (Monitor currentMonitor : monitors ) {
5464- Rectangle clientArea = currentMonitor .getClientArea ();
5465- if (clientArea .contains (x , y )) {
5466- return currentMonitor ;
5467- }
5468- }
5469- return getPrimaryMonitor ();
5470- }
5471-
5472- private Monitor getContainingMonitor (int x , int y , int width , int height ) {
5473- Rectangle rectangle = new Rectangle (x , y , width , height );
5474- Monitor [] monitors = getMonitors ();
5475- Monitor selectedMonitor = getPrimaryMonitor ();
5476- int highestArea = 0 ;
5477- for (Monitor currentMonitor : monitors ) {
5478- Rectangle clientArea = currentMonitor .getClientArea ();
5479- Rectangle intersection = clientArea .intersection (rectangle );
5480- int area = intersection .width * intersection .height ;
5481- if (area > highestArea ) {
5482- selectedMonitor = currentMonitor ;
5483- highestArea = area ;
5484- }
5485- }
5486- return selectedMonitor ;
5487- }
5488-
5489- private Monitor getContainingMonitorInPixelsCoordinate (int xInPixels , int yInPixels ) {
5490- Monitor [] monitors = getMonitors ();
5491- for (Monitor current : monitors ) {
5492- Rectangle clientArea = getMonitorClientAreaInPixels (current );
5493- if (clientArea .contains (xInPixels , yInPixels )) {
5494- return current ;
5495- }
5496- }
5497- return getPrimaryMonitor ();
5498- }
5499-
5500- private Rectangle getMonitorClientAreaInPixels (Monitor monitor ) {
5501- int zoom = getApplicableMonitorZoom (monitor );
5502- int widthInPixels = DPIUtil .scaleUp (monitor .clientWidth , zoom );
5503- int heightInPixels = DPIUtil .scaleUp (monitor .clientHeight , zoom );
5504- return new Rectangle (monitor .clientX , monitor .clientY , widthInPixels , heightInPixels );
5505- }
5506-
5507- private Point getPixelsFromPoint (Monitor monitor , int x , int y ) {
5508- int zoom = getApplicableMonitorZoom (monitor );
5509- int mappedX = DPIUtil .scaleUp (x - monitor .clientX , zoom ) + monitor .clientX ;
5510- int mappedY = DPIUtil .scaleUp (y - monitor .clientY , zoom ) + monitor .clientY ;
5511- return new Point (mappedX , mappedY );
5512- }
5513-
5514- private Point getPointFromPixels (Monitor monitor , int x , int y ) {
5515- int zoom = getApplicableMonitorZoom (monitor );
5516- int mappedX = DPIUtil .scaleDown (x - monitor .clientX , zoom ) + monitor .clientX ;
5517- int mappedY = DPIUtil .scaleDown (y - monitor .clientY , zoom ) + monitor .clientY ;
5518- return new Point (mappedX , mappedY );
5519- }
55205393}
0 commit comments