Skip to content

Deprecating Display#getDPI #2260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package org.eclipse.swt.graphics;

import org.eclipse.swt.*;
import org.eclipse.swt.internal.ExceptionStash;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.cocoa.*;

/**
Expand Down Expand Up @@ -244,7 +244,7 @@ public void dispose () {
}

destroy ();
disposed = true;
disposed = true;
if (tracking) {
synchronized (trackingLock) {
printErrors ();
Expand Down Expand Up @@ -391,7 +391,18 @@ public int getDepth () {
* @exception SWTException <ul>
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
* </ul>
*
* @deprecated <p>This method returns a single global DPI value
* that does not reflect per-monitor DPI settings on modern operating systems.
* In environments with different scaling factors across monitors, it may provide
* a misleading or meaningless result, as it does not correspond to the actual DPI
* of any specific monitor.</p>
*
* <p>Note: While deprecated for general {@code Device} instances like {@code Display},
* this method may still be validly used when called on a {@code Printer} instance,
* where a single global DPI value is meaningful and expected.</p>
*/
@Deprecated
public Point getDPI () {
checkDevice ();
return getScreenDPI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,18 @@ public int getDepth () {
* @exception SWTException <ul>
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
* </ul>
*
* @deprecated <p>This method returns a single global DPI value
* that does not reflect per-monitor DPI settings on modern operating systems.
* In environments with different scaling factors across monitors, it may provide
* a misleading or meaningless result, as it does not correspond to the actual DPI
* of any specific monitor.</p>
*
* <p>Note: While deprecated for general {@code Device} instances like {@code Display},
* this method may still be validly used when called on a {@code Printer} instance,
* where a single global DPI value is meaningful and expected.</p>
*/
@Deprecated
public Point getDPI () {
checkDevice ();
return getScreenDPI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,18 @@ public int getDepth () {
* @exception SWTException <ul>
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
* </ul>
*
* @deprecated <p>This method returns a single global DPI value
* that does not reflect per-monitor DPI settings on modern operating systems.
* In environments with different scaling factors across monitors, it may provide
* a misleading or meaningless result, as it does not correspond to the actual DPI
* of any specific monitor.</p>
*
* <p>Note: While deprecated for general {@code Device} instances like {@code Display},
* this method may still be validly used when called on a {@code Printer} instance,
* where a single global DPI value is meaningful and expected.</p>
*/
@Deprecated
public Point getDPI () {
checkDevice ();
long hDC = internal_new_GC (null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1165,16 +1165,16 @@ void menuPrint() {
if (image == null) return;

try {
final int DOTS_PER_INCH = 96;
// Ask the user to specify the printer.
PrintDialog dialog = new PrintDialog(shell, SWT.NONE);
if (printerData != null) dialog.setPrinterData(printerData);
printerData = dialog.open();
if (printerData == null) return;

Printer printer = new Printer(printerData);
Point screenDPI = display.getDPI();
Point printerDPI = printer.getDPI();
int scaleFactor = printerDPI.x / screenDPI.x;
int scaleFactor = printerDPI.x / DOTS_PER_INCH;
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
if (printer.startJob(currentName)) {
if (printer.startPage()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public void getName(AccessibleEvent e) {
}

private static void performPrintAction(final Display display, final Shell shell) {
final int DOTS_PER_INCH = 96;
Rectangle r = composite.getBounds();
Point p = shell.toDisplay(r.x, r.y);
org.eclipse.swt.graphics.Image snapshotImage
Expand All @@ -159,9 +160,8 @@ private static void performPrintAction(final Display display, final Shell shell)
data = dialog.open();
if (data != null) {
Printer printer = new Printer(data);
Point screenDPI = display.getDPI();
Point printerDPI = printer.getDPI();
int scaleFactor = printerDPI.x / screenDPI.x;
int scaleFactor = printerDPI.x / DOTS_PER_INCH;
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
if (printer.startJob("Print Image")) {
ImageData imageData = snapshotImage.getImageData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public int getGcStyle() {

createSeparator(shell);

new Label (shell, SWT.NONE).setText ("5. 50x50 box\n(Display#getDPI(): " + display.getDPI().x + ")");
new Label (shell, SWT.NONE).setText ("5. 50x50 box");
Label box= new Label (shell, SWT.NONE);
box.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
box.setLayoutData (new GridData (50, 50));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,7 @@ public void test_wake() {
/* custom */
boolean disposeExecRan;

@SuppressWarnings("deprecation")
@Test
public void test_getDPI() {
Display display = new Display();
Expand Down
Loading