Skip to content

Commit 87df443

Browse files
Deprecating Display#getDPI
Having the scale factor being based on the screen DPI leads to unexpected result e.g. Image too big/small. Having a screen dpi independent factor leads to consistent results
1 parent 13934ee commit 87df443

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,18 @@ public int getDepth () {
521521
* @exception SWTException <ul>
522522
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
523523
* </ul>
524+
*
525+
* @deprecated <p> This method returns a single global DPI value
526+
* that does not reflect per-monitor DPI settings on modern operating systems.
527+
* In environments with different scaling factors across monitors, it may provide
528+
* a misleading or meaningless result, as it does not correspond to the actual DPI
529+
* of any specific monitor.</p>
530+
*
531+
* <p>Note: While deprecated for general {@code Device} instances like {@code Display},
532+
* this method may still be validly used when called on a {@code Printer} instance,
533+
* where a single global DPI value is meaningful and expected.</p>
524534
*/
535+
@Deprecated
525536
public Point getDPI () {
526537
checkDevice ();
527538
long hDC = internal_new_GC (null);

examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/imageanalyzer/ImageAnalyzer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,16 +1165,16 @@ void menuPrint() {
11651165
if (image == null) return;
11661166

11671167
try {
1168+
final int DOTS_PER_INCH = 96;
11681169
// Ask the user to specify the printer.
11691170
PrintDialog dialog = new PrintDialog(shell, SWT.NONE);
11701171
if (printerData != null) dialog.setPrinterData(printerData);
11711172
printerData = dialog.open();
11721173
if (printerData == null) return;
11731174

11741175
Printer printer = new Printer(printerData);
1175-
Point screenDPI = display.getDPI();
11761176
Point printerDPI = printer.getDPI();
1177-
int scaleFactor = printerDPI.x / screenDPI.x;
1177+
int scaleFactor = printerDPI.x / DOTS_PER_INCH;
11781178
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
11791179
if (printer.startJob(currentName)) {
11801180
if (printer.startPage()) {

examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet361.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public void getName(AccessibleEvent e) {
146146
}
147147

148148
private static void performPrintAction(final Display display, final Shell shell) {
149+
final int DOTS_PER_INCH = 96;
149150
Rectangle r = composite.getBounds();
150151
Point p = shell.toDisplay(r.x, r.y);
151152
org.eclipse.swt.graphics.Image snapshotImage
@@ -159,9 +160,8 @@ private static void performPrintAction(final Display display, final Shell shell)
159160
data = dialog.open();
160161
if (data != null) {
161162
Printer printer = new Printer(data);
162-
Point screenDPI = display.getDPI();
163163
Point printerDPI = printer.getDPI();
164-
int scaleFactor = printerDPI.x / screenDPI.x;
164+
int scaleFactor = printerDPI.x / DOTS_PER_INCH;
165165
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
166166
if (printer.startJob("Print Image")) {
167167
ImageData imageData = snapshotImage.getImageData();

examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public int getGcStyle() {
185185

186186
createSeparator(shell);
187187

188-
new Label (shell, SWT.NONE).setText ("5. 50x50 box\n(Display#getDPI(): " + display.getDPI().x + ")");
188+
new Label (shell, SWT.NONE).setText ("5. 50x50 box");
189189
Label box= new Label (shell, SWT.NONE);
190190
box.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
191191
box.setLayoutData (new GridData (50, 50));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,7 @@ public void test_wake() {
15731573
/* custom */
15741574
boolean disposeExecRan;
15751575

1576+
@SuppressWarnings("deprecation")
15761577
@Test
15771578
public void test_getDPI() {
15781579
Display display = new Display();

0 commit comments

Comments
 (0)