Skip to content

Commit

Permalink
Adapt GetSystemMetrics calls to be DPI dependent
Browse files Browse the repository at this point in the history
Writing GetSystemMetrics method in Widget class so that the child
controls can use it to get System Metrics to be DPI dependent

Contributes to:
#62 &
#127
  • Loading branch information
ShahzaibIbrahim authored and fedejeanne committed Jun 18, 2024
1 parent a3cf373 commit 25b32ef
Show file tree
Hide file tree
Showing 21 changed files with 104 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,13 @@ public class Button extends Control {
ImageList imageList;
boolean ignoreMouse, grayed, useDarkModeExplorerTheme;
static final int MARGIN = 4;
static final int CHECK_WIDTH, CHECK_HEIGHT;
private final int checkWidth, checkHeight;
static final int ICON_WIDTH = 128, ICON_HEIGHT = 128;
static /*final*/ boolean COMMAND_LINK = false;
static final char[] STRING_WITH_ZERO_CHAR = new char[] {'0'};
static final long ButtonProc;
static final TCHAR ButtonClass = new TCHAR (0, "BUTTON", true);
static {
long hBitmap = OS.LoadBitmap (0, OS.OBM_CHECKBOXES);
if (hBitmap == 0) {
CHECK_WIDTH = OS.GetSystemMetrics (OS.SM_CXVSCROLL);
CHECK_HEIGHT = OS.GetSystemMetrics (OS.SM_CYVSCROLL);
} else {
BITMAP bitmap = new BITMAP ();
OS.GetObject (hBitmap, BITMAP.sizeof, bitmap);
OS.DeleteObject (hBitmap);
CHECK_WIDTH = bitmap.bmWidth / 4;
CHECK_HEIGHT = bitmap.bmHeight / 3;
}
WNDCLASS lpWndClass = new WNDCLASS ();
OS.GetClassInfo (0, ButtonClass, lpWndClass);
ButtonProc = lpWndClass.lpfnWndProc;
Expand Down Expand Up @@ -119,6 +108,17 @@ public class Button extends Control {
*/
public Button (Composite parent, int style) {
super (parent, checkStyle (style));
long hBitmap = OS.LoadBitmap (0, OS.OBM_CHECKBOXES);
if (hBitmap == 0) {
checkWidth = getSystemMetrics(OS.SM_CXVSCROLL);
checkHeight = getSystemMetrics (OS.SM_CYVSCROLL);
} else {
BITMAP bitmap = new BITMAP ();
OS.GetObject (hBitmap, BITMAP.sizeof, bitmap);
OS.DeleteObject (hBitmap);
checkWidth = bitmap.bmWidth / 4;
checkHeight = bitmap.bmHeight / 3;
}
}

void _setImage (Image image) {
Expand Down Expand Up @@ -315,11 +315,11 @@ else if ((style & SWT.RIGHT) != 0) {
int width = 0, height = 0, border = getBorderWidthInPixels ();
if ((style & SWT.ARROW) != 0) {
if ((style & (SWT.UP | SWT.DOWN)) != 0) {
width += OS.GetSystemMetrics (OS.SM_CXVSCROLL);
height += OS.GetSystemMetrics (OS.SM_CYVSCROLL);
width += getSystemMetrics (OS.SM_CXVSCROLL);
height += getSystemMetrics (OS.SM_CYVSCROLL);
} else {
width += OS.GetSystemMetrics (OS.SM_CXHSCROLL);
height += OS.GetSystemMetrics (OS.SM_CYHSCROLL);
width += getSystemMetrics (OS.SM_CXHSCROLL);
height += getSystemMetrics (OS.SM_CYHSCROLL);
}
} else {
if ((style & SWT.COMMAND) != 0) {
Expand Down Expand Up @@ -373,7 +373,7 @@ else if ((style & SWT.RIGHT) != 0) {
flags = OS.DT_CALCRECT | OS.DT_WORDBREAK;
rect.right = wHint - width - 2 * border;
if (isRadioOrCheck()) {
rect.right -= CHECK_WIDTH + 3;
rect.right -= checkWidth + 3;
} else {
rect.right -= 6;
}
Expand All @@ -392,8 +392,8 @@ else if ((style & SWT.RIGHT) != 0) {
OS.ReleaseDC (handle, hDC);
}
if (isRadioOrCheck()) {
width += CHECK_WIDTH + extra;
height = Math.max (height, CHECK_HEIGHT + 3);
width += checkWidth + extra;
height = Math.max (height, checkHeight + 3);
}
if ((style & (SWT.PUSH | SWT.TOGGLE)) != 0) {
width += 12; height += 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,8 @@ public void clearSelection () {
width += pcbi.itemLeft + (pcbi.buttonRight - pcbi.buttonLeft);
height = (pcbi.buttonBottom - pcbi.buttonTop) + pcbi.buttonTop * 2;
} else {
int border = OS.GetSystemMetrics (OS.SM_CXEDGE);
width += OS.GetSystemMetrics (OS.SM_CXVSCROLL) + border * 2;
int border = getSystemMetrics (OS.SM_CXEDGE);
width += getSystemMetrics (OS.SM_CXVSCROLL) + border * 2;
int textHeight = (int)OS.SendMessage (handle, OS.CB_GETITEMHEIGHT, -1, 0);
if ((style & SWT.DROP_DOWN) != 0) {
height = textHeight + 6;
Expand All @@ -665,7 +665,7 @@ public void clearSelection () {
}
}
if ((style & SWT.SIMPLE) != 0 && (style & SWT.H_SCROLL) != 0) {
height += OS.GetSystemMetrics (OS.SM_CYHSCROLL);
height += getSystemMetrics (OS.SM_CYHSCROLL);
}
return new Point (width, height);
}
Expand Down Expand Up @@ -2188,7 +2188,7 @@ void setScrollWidth (int scrollWidth) {
OS.SendMessage (handle, OS.CB_SETDROPPEDWIDTH, 0, 0);
OS.SendMessage (handle, OS.CB_SETHORIZONTALEXTENT, scrollWidth, 0);
} else {
scrollWidth += OS.GetSystemMetrics (OS.SM_CYHSCROLL);
scrollWidth += getSystemMetrics (OS.SM_CYHSCROLL);
OS.SendMessage (handle, OS.CB_SETDROPPEDWIDTH, scrollWidth, 0);
OS.SendMessage (handle, OS.CB_SETHORIZONTALEXTENT, 0, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1854,7 +1854,7 @@ LRESULT wmNCPaint (long hwnd, long wParam, long lParam) {
rect.right -= rect.left;
rect.bottom -= rect.top;
rect.left = rect.top = 0;
int border = OS.GetSystemMetrics (OS.SM_CXEDGE);
int border = getSystemMetrics (OS.SM_CXEDGE);
OS.ExcludeClipRect (hDC, border, border, rect.right - border, rect.bottom - border);
OS.DrawThemeBackground (display.hEditTheme (), hDC, OS.EP_EDITTEXT, OS.ETS_NORMAL, rect, null);
OS.ReleaseDC (hwnd, hDC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ void createHandle () {
}

void checkGesture () {
int value = OS.GetSystemMetrics (OS.SM_DIGITIZER);
int value =getSystemMetrics (OS.SM_DIGITIZER);
if ((value & (OS.NID_READY | OS.NID_MULTI_INPUT)) != 0) {
/*
* Feature in Windows 7: All gestures are enabled by default except GID_ROTATE.
Expand Down Expand Up @@ -1161,8 +1161,8 @@ public int getBorderWidth () {
int getBorderWidthInPixels () {
long borderHandle = borderHandle ();
int bits1 = OS.GetWindowLong (borderHandle, OS.GWL_EXSTYLE);
if ((bits1 & OS.WS_EX_CLIENTEDGE) != 0) return OS.GetSystemMetrics (OS.SM_CXEDGE);
if ((bits1 & OS.WS_EX_STATICEDGE) != 0) return OS.GetSystemMetrics (OS.SM_CXBORDER);
if ((bits1 & OS.WS_EX_CLIENTEDGE) != 0) return getSystemMetrics (OS.SM_CXEDGE);
if ((bits1 & OS.WS_EX_STATICEDGE) != 0) return getSystemMetrics (OS.SM_CXBORDER);
int bits2 = OS.GetWindowLong (borderHandle, OS.GWL_STYLE);

if ((bits2 & OS.WS_BORDER) != 0) {
Expand All @@ -1172,9 +1172,9 @@ int getBorderWidthInPixels () {
* saves screen space, but could break some layouts.
*/
if (isUseWsBorder ())
return OS.GetSystemMetrics (OS.SM_CXEDGE);
return getSystemMetrics (OS.SM_CXEDGE);

return OS.GetSystemMetrics (OS.SM_CXBORDER);
return getSystemMetrics (OS.SM_CXBORDER);
}

return 0;
Expand Down Expand Up @@ -2284,10 +2284,10 @@ void printWidget (long hwnd, long hdc, GC gc) {
hwndInsertAfter = OS.HWND_TOP;
}
if (fixPrintWindow) {
int x = OS.GetSystemMetrics (OS.SM_XVIRTUALSCREEN);
int y = OS.GetSystemMetrics (OS.SM_YVIRTUALSCREEN);
int width = OS.GetSystemMetrics (OS.SM_CXVIRTUALSCREEN);
int height = OS.GetSystemMetrics (OS.SM_CYVIRTUALSCREEN);
int x =getSystemMetrics (OS.SM_XVIRTUALSCREEN);
int y =getSystemMetrics (OS.SM_YVIRTUALSCREEN);
int width =getSystemMetrics (OS.SM_CXVIRTUALSCREEN);
int height =getSystemMetrics (OS.SM_CYVIRTUALSCREEN);
int flags = OS.SWP_NOSIZE | OS.SWP_NOZORDER | OS.SWP_NOACTIVATE | OS.SWP_DRAWFRAME;
if ((bits1 & OS.WS_VISIBLE) != 0) {
OS.DefWindowProc (hwnd, OS.WM_SETREDRAW, 0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ protected void checkSubclass () {
width = size.cx;
height = size.cy;
// TODO: Can maybe use DTM_GETDATETIMEPICKERINFO for this
int upDownHeight = OS.GetSystemMetrics (OS.SM_CYVSCROLL) + 7;
int upDownHeight = getSystemMetrics (OS.SM_CYVSCROLL) + 7;
height = Math.max (height, upDownHeight);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ Control computeTabRoot () {
OS.AdjustWindowRectEx (rect, bits1, hasMenu, bits2);

/* Get the size of the scroll bars */
if (horizontalBar != null) rect.bottom += OS.GetSystemMetrics (OS.SM_CYHSCROLL);
if (verticalBar != null) rect.right += OS.GetSystemMetrics (OS.SM_CXVSCROLL);
if (horizontalBar != null) rect.bottom += getSystemMetrics (OS.SM_CYHSCROLL);
if (verticalBar != null) rect.right += getSystemMetrics (OS.SM_CXVSCROLL);

/* Compute the height of the menu bar */
if (hasMenu) {
Expand All @@ -326,7 +326,7 @@ Control computeTabRoot () {
OS.SendMessage (handle, OS.WM_NCCALCSIZE, 0, testRect);
while ((testRect.bottom - testRect.top) < height) {
if (testRect.bottom - testRect.top == 0) break;
rect.top -= OS.GetSystemMetrics (OS.SM_CYMENU) - OS.GetSystemMetrics (OS.SM_CYBORDER);
rect.top -= getSystemMetrics (OS.SM_CYMENU) - getSystemMetrics (OS.SM_CYBORDER);
OS.SetRect (testRect, 0, 0, rect.right - rect.left, rect.bottom - rect.top);
OS.SendMessage (handle, OS.WM_NCCALCSIZE, 0, testRect);
}
Expand Down Expand Up @@ -466,8 +466,8 @@ void fixDecorations (Decorations newDecorations, Control control, Menu [] menus)
* the window restored. There is no fix for this problem at
* this time.
*/
if (horizontalBar != null) width -= OS.GetSystemMetrics (OS.SM_CYHSCROLL);
if (verticalBar != null) height -= OS.GetSystemMetrics (OS.SM_CXVSCROLL);
if (horizontalBar != null) width -= getSystemMetrics (OS.SM_CYHSCROLL);
if (verticalBar != null) height -= getSystemMetrics (OS.SM_CXVSCROLL);
RECT rect = new RECT ();
int bits1 = OS.GetWindowLong (handle, OS.GWL_STYLE);
int bits2 = OS.GetWindowLong (handle, OS.GWL_EXSTYLE);
Expand Down Expand Up @@ -902,11 +902,11 @@ void setImages (Image image, Image [] images) {
datas [i] = images [i].getImageData (getZoom());
}
images = bestImages;
sort (images, datas, OS.GetSystemMetrics (OS.SM_CXSMICON), OS.GetSystemMetrics (OS.SM_CYSMICON), depth);
sort (images, datas, getSystemMetrics (OS.SM_CXSMICON), getSystemMetrics (OS.SM_CYSMICON), depth);
}
smallIcon = images [0];
if (images.length > 1) {
sort (images, datas, OS.GetSystemMetrics (OS.SM_CXICON), OS.GetSystemMetrics (OS.SM_CYICON), depth);
sort (images, datas, getSystemMetrics (OS.SM_CXICON), getSystemMetrics (OS.SM_CYICON), depth);
}
largeIcon = images [0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ static int checkStyle (int style) {
checkWidget ();
int width = 0, height = 0, border = getBorderWidthInPixels ();
if ((style & SWT.SEPARATOR) != 0) {
int lineWidth = OS.GetSystemMetrics (OS.SM_CXBORDER);
int lineWidth = getSystemMetrics (OS.SM_CXBORDER);
if ((style & SWT.HORIZONTAL) != 0) {
width = DEFAULT_WIDTH; height = lineWidth * 2;
} else {
Expand Down Expand Up @@ -535,7 +535,7 @@ void wmDrawChildSeparator(DRAWITEMSTRUCT struct) {
if ((style & SWT.SHADOW_NONE) != 0) return;

RECT rect = new RECT ();
int lineWidth = OS.GetSystemMetrics (OS.SM_CXBORDER);
int lineWidth = getSystemMetrics (OS.SM_CXBORDER);
int flags = (style & SWT.SHADOW_IN) != 0 ? OS.EDGE_SUNKEN : OS.EDGE_ETCHED;
if ((style & SWT.HORIZONTAL) != 0) {
int bottom = struct.top + Math.max (lineWidth * 2, (struct.bottom - struct.top) / 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ static int checkStyle (int style) {
width += border * 2 + INSET;
height += border * 2;
if ((style & SWT.V_SCROLL) != 0) {
width += OS.GetSystemMetrics (OS.SM_CXVSCROLL);
width += getSystemMetrics (OS.SM_CXVSCROLL);
}
if ((style & SWT.H_SCROLL) != 0) {
height += OS.GetSystemMetrics (OS.SM_CYHSCROLL);
height += getSystemMetrics (OS.SM_CYHSCROLL);
}
return new Point (width, height);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ LRESULT wmTimer (long wParam, long lParam) {
if (!success) return null;
if (OS.PtInRect (rect, pt)) {
// Mouse cursor is within the bounds of menu item
selectedMenuItem.showTooltip (pt.x, pt.y + OS.GetSystemMetrics(OS.SM_CYCURSOR) / 2 + 5);
selectedMenuItem.showTooltip (pt.x, pt.y + getSystemMetrics(OS.SM_CYCURSOR) / 2 + 5);
} else {
/*
* Mouse cursor is outside the bounds of the menu item:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ static int checkStyle (int style) {
int border = getBorderWidthInPixels ();
int width = border * 2, height = border * 2;
if ((style & SWT.HORIZONTAL) != 0) {
width += OS.GetSystemMetrics (OS.SM_CXHSCROLL) * 10;
height += OS.GetSystemMetrics (OS.SM_CYHSCROLL);
width += getSystemMetrics (OS.SM_CXHSCROLL) * 10;
height += getSystemMetrics (OS.SM_CYHSCROLL);
} else {
width += OS.GetSystemMetrics (OS.SM_CXVSCROLL);
height += OS.GetSystemMetrics (OS.SM_CYVSCROLL) * 10;
width += getSystemMetrics (OS.SM_CXVSCROLL);
height += getSystemMetrics (OS.SM_CYVSCROLL) * 10;
}
if (wHint != SWT.DEFAULT) width = wHint + (border * 2);
if (hHint != SWT.DEFAULT) height = hHint + (border * 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ static int checkStyle (int style) {
RECT rect = new RECT ();
OS.SendMessage (handle, OS.TBM_GETTHUMBRECT, 0, rect);
if ((style & SWT.HORIZONTAL) != 0) {
width += OS.GetSystemMetrics (OS.SM_CXHSCROLL) * 10;
int scrollY = OS.GetSystemMetrics (OS.SM_CYHSCROLL);
width += getSystemMetrics (OS.SM_CXHSCROLL) * 10;
int scrollY = getSystemMetrics (OS.SM_CYHSCROLL);
height += (rect.top * 2) + scrollY + (scrollY / 3);
} else {
int scrollX = OS.GetSystemMetrics (OS.SM_CXVSCROLL);
int scrollX = getSystemMetrics (OS.SM_CXVSCROLL);
width += (rect.left * 2) + scrollX + (scrollX / 3);
height += OS.GetSystemMetrics (OS.SM_CYVSCROLL) * 10;
height += getSystemMetrics (OS.SM_CYVSCROLL) * 10;
}
if (wHint != SWT.DEFAULT) width = wHint + (border * 2);
if (hHint != SWT.DEFAULT) height = hHint + (border * 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ Rectangle getBounds () {
if ((style & SWT.HORIZONTAL) != 0) {
y = rect.bottom - rect.top;
width = rect.right - rect.left;
height = OS.GetSystemMetrics (OS.SM_CYHSCROLL);
height = getSystemMetrics (OS.SM_CYHSCROLL);
} else {
x = rect.right - rect.left;
width = OS.GetSystemMetrics (OS.SM_CXVSCROLL);
width = getSystemMetrics (OS.SM_CXVSCROLL);
height = rect.bottom - rect.top;
}
return new Rectangle (x, y, width, height);
Expand Down Expand Up @@ -365,9 +365,9 @@ Point getSizeInPixels () {
int width, height;
if ((style & SWT.HORIZONTAL) != 0) {
width = rect.right - rect.left;
height = OS.GetSystemMetrics (OS.SM_CYHSCROLL);
height = getSystemMetrics (OS.SM_CYHSCROLL);
} else {
width = OS.GetSystemMetrics (OS.SM_CXVSCROLL);
width = getSystemMetrics (OS.SM_CXVSCROLL);
height = rect.bottom - rect.top;
}
return new Point (width, height);
Expand Down Expand Up @@ -468,7 +468,7 @@ Rectangle getThumbTrackBoundsInPixels () {
int x = 0, y = 0, width, height;
if ((style & SWT.HORIZONTAL) != 0) {
OS.GetScrollBarInfo(parent.handle, OS.OBJID_HSCROLL, info);
int size = OS.GetSystemMetrics (OS.SM_CYHSCROLL);
int size = getSystemMetrics (OS.SM_CYHSCROLL);
y = info.rcScrollBar.top;
width = info.rcScrollBar.right - info.rcScrollBar.left;
height = size;
Expand All @@ -481,7 +481,7 @@ Rectangle getThumbTrackBoundsInPixels () {
}
} else {
OS.GetScrollBarInfo(parent.handle, OS.OBJID_VSCROLL, info);
int size = OS.GetSystemMetrics (OS.SM_CYVSCROLL);
int size = getSystemMetrics (OS.SM_CYVSCROLL);
x = info.rcScrollBar.left;
width = size;
height = info.rcScrollBar.bottom - info.rcScrollBar.top;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ Rectangle computeTrimInPixels (int x, int y, int width, int height) {
int bits1 = OS.GetWindowLong (scrolledHandle, OS.GWL_STYLE);
int bits2 = OS.GetWindowLong (scrolledHandle, OS.GWL_EXSTYLE);
OS.AdjustWindowRectEx (rect, bits1, false, bits2);
if (horizontalBar != null) rect.bottom += OS.GetSystemMetrics (OS.SM_CYHSCROLL);
if (verticalBar != null) rect.right += OS.GetSystemMetrics (OS.SM_CXVSCROLL);
if (horizontalBar != null) rect.bottom += getSystemMetrics (OS.SM_CYHSCROLL);
if (verticalBar != null) rect.right += getSystemMetrics (OS.SM_CXVSCROLL);
int nWidth = rect.right - rect.left, nHeight = rect.bottom - rect.top;
return new Rectangle (rect.left, rect.top, nWidth, nHeight);
}
Expand Down
Loading

0 comments on commit 25b32ef

Please sign in to comment.