Skip to content

Commit 918b82c

Browse files
committed
[Win32] Pass point values to size computation methods
This increase precision of size computation by reducing the number of pixel/point conversions. In particular, the minimumSize() method unnecessarily calculated in pixels even though all relevant values are in points. Inside the computeSizeInPixels() methods, several conversions were done as most other called methods operate on point values.
1 parent 4722846 commit 918b82c

File tree

21 files changed

+167
-114
lines changed

21 files changed

+167
-114
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Button.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,11 @@ else if ((style & SWT.RIGHT) != 0) {
322322
return margin;
323323
}
324324

325-
@Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
325+
@Override
326+
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
326327
checkWidget ();
328+
int zoom = getZoom();
329+
Point hintInPixels = Win32DPIUtils.pointToPixelAsSize(hintInPoints, zoom);
327330
int width = 0, height = 0, border = getBorderWidthInPixels ();
328331
if ((style & SWT.ARROW) != 0) {
329332
if ((style & (SWT.UP | SWT.DOWN)) != 0) {
@@ -336,8 +339,8 @@ else if ((style & SWT.RIGHT) != 0) {
336339
} else {
337340
if ((style & SWT.COMMAND) != 0) {
338341
SIZE size = new SIZE ();
339-
if (wHint != SWT.DEFAULT) {
340-
size.cx = wHint;
342+
if (hintInPoints.x != SWT.DEFAULT) {
343+
size.cx = hintInPixels.x;
341344
OS.SendMessage (handle, OS.BCM_GETIDEALSIZE, 0, size);
342345
width = size.cx;
343346
height = size.cy;
@@ -381,9 +384,9 @@ else if ((style & SWT.RIGHT) != 0) {
381384
char [] buffer = text.toCharArray ();
382385
RECT rect = new RECT ();
383386
int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE;
384-
if ((style & SWT.WRAP) != 0 && wHint != SWT.DEFAULT) {
387+
if ((style & SWT.WRAP) != 0 && hintInPoints.x != SWT.DEFAULT) {
385388
flags = OS.DT_CALCRECT | OS.DT_WORDBREAK;
386-
rect.right = wHint - width - 2 * border;
389+
rect.right = hintInPixels.x - width - 2 * border;
387390
if (isRadioOrCheck()) {
388391
rect.right -= checkWidth + 3;
389392
} else {
@@ -412,8 +415,8 @@ else if ((style & SWT.RIGHT) != 0) {
412415
}
413416
}
414417
}
415-
if (wHint != SWT.DEFAULT) width = wHint;
416-
if (hHint != SWT.DEFAULT) height = hHint;
418+
if (hintInPoints.x != SWT.DEFAULT) width = hintInPixels.x;
419+
if (hintInPoints.y != SWT.DEFAULT) height = hintInPixels.y;
417420
width += border * 2;
418421
height += border * 2;
419422
return new Point (width, height);

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Combo.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,13 @@ public void clearSelection () {
592592
OS.SendMessage (handle, OS.CB_SETEDITSEL, 0, -1);
593593
}
594594

595-
@Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
595+
@Override
596+
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
596597
checkWidget ();
598+
int zoom = getZoom();
599+
Point hintInPixels = Win32DPIUtils.pointToPixelAsSize(hintInPoints, zoom);
597600
int width = 0, height = 0;
598-
if (wHint == SWT.DEFAULT) {
601+
if (hintInPoints.x == SWT.DEFAULT) {
599602
long newFont, oldFont = 0;
600603
long hDC = OS.GetDC (handle);
601604
newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
@@ -627,7 +630,7 @@ public void clearSelection () {
627630
if (newFont != 0) OS.SelectObject (hDC, oldFont);
628631
OS.ReleaseDC (handle, hDC);
629632
}
630-
if (hHint == SWT.DEFAULT) {
633+
if (hintInPoints.y == SWT.DEFAULT) {
631634
if ((style & SWT.SIMPLE) != 0) {
632635
int count = (int)OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0);
633636
int itemHeight = (int)OS.SendMessage (handle, OS.CB_GETITEMHEIGHT, 0, 0);
@@ -636,8 +639,8 @@ public void clearSelection () {
636639
}
637640
if (width == 0) width = DEFAULT_WIDTH;
638641
if (height == 0) height = DEFAULT_HEIGHT;
639-
if (wHint != SWT.DEFAULT) width = wHint;
640-
if (hHint != SWT.DEFAULT) height = hHint;
642+
if (hintInPoints.x != SWT.DEFAULT) width = hintInPixels.x;
643+
if (hintInPoints.y != SWT.DEFAULT) height = hintInPixels.y;
641644
if ((style & SWT.READ_ONLY) != 0) {
642645
width += 8;
643646
} else {

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Composite.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -207,31 +207,29 @@ protected void checkSubclass () {
207207
}
208208

209209
@Override
210-
Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
210+
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
211211
display.runSkin ();
212-
Point size;
212+
Point sizeInPoints;
213213
if (layout != null) {
214-
if (wHint == SWT.DEFAULT || hHint == SWT.DEFAULT) {
214+
if (hintInPoints.x == SWT.DEFAULT || hintInPoints.y == SWT.DEFAULT) {
215215
changed |= (state & LAYOUT_CHANGED) != 0;
216216
state &= ~LAYOUT_CHANGED;
217-
int zoom = getZoom();
218-
size = Win32DPIUtils.pointToPixelAsSize(layout.computeSize (this, DPIUtil.pixelToPoint(wHint, zoom), DPIUtil.pixelToPoint(hHint, zoom), changed), zoom);
217+
sizeInPoints = layout.computeSize (this, hintInPoints.x, hintInPoints.y, changed);
219218
} else {
220-
size = new Point (wHint, hHint);
219+
sizeInPoints = hintInPoints;
221220
}
222221
} else {
223-
size = minimumSize (wHint, hHint, changed);
224-
if (size.x == 0) size.x = DEFAULT_WIDTH;
225-
if (size.y == 0) size.y = DEFAULT_HEIGHT;
222+
sizeInPoints = minimumSize (hintInPoints, changed);
223+
if (sizeInPoints.x == 0) sizeInPoints.x = DEFAULT_WIDTH;
224+
if (sizeInPoints.y == 0) sizeInPoints.y = DEFAULT_HEIGHT;
226225
}
227-
if (wHint != SWT.DEFAULT) size.x = wHint;
228-
if (hHint != SWT.DEFAULT) size.y = hHint;
226+
if (hintInPoints.x != SWT.DEFAULT) sizeInPoints.x = hintInPoints.x;
227+
if (hintInPoints.y != SWT.DEFAULT) sizeInPoints.y = hintInPoints.y;
229228
/*
230229
* Since computeTrim can be overridden by subclasses, we cannot
231230
* call computeTrimInPixels directly.
232231
*/
233-
int zoom = getZoom();
234-
Rectangle trim = Win32DPIUtils.pointToPixel(computeTrim (0, 0, DPIUtil.pixelToPoint(size.x, zoom), DPIUtil.pixelToPoint(size.y, zoom)), zoom);
232+
Rectangle trim = Win32DPIUtils.pointToPixel(computeTrim (0, 0, sizeInPoints.x, sizeInPoints.y), getZoom());
235233
return new Point (trim.width, trim.height);
236234
}
237235

@@ -869,16 +867,15 @@ void markLayout (boolean changed, boolean all) {
869867
}
870868
}
871869

872-
Point minimumSize (int wHint, int hHint, boolean changed) {
870+
Point minimumSize (Point hintInPoints, boolean changed) {
873871
/*
874872
* Since getClientArea can be overridden by subclasses, we cannot
875873
* call getClientAreaInPixels directly.
876874
*/
877-
int zoom = getZoom();
878-
Rectangle clientArea = Win32DPIUtils.pointToPixel(getClientArea (), zoom);
875+
Rectangle clientArea = getClientArea ();
879876
int width = 0, height = 0;
880877
for (Control element : _getChildren ()) {
881-
Rectangle rect = Win32DPIUtils.pointToPixel(element.getBounds (), zoom);
878+
Rectangle rect = element.getBounds ();
882879
width = Math.max (width, rect.x - clientArea.x + rect.width);
883880
height = Math.max (height, rect.y - clientArea.y + rect.height);
884881
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -619,18 +619,18 @@ public Point computeSize (int wHint, int hHint) {
619619
public Point computeSize (int wHint, int hHint, boolean changed){
620620
checkWidget ();
621621
int zoom = getZoom();
622-
wHint = (wHint != SWT.DEFAULT ? DPIUtil.pointToPixel(wHint, zoom) : wHint);
623-
hHint = (hHint != SWT.DEFAULT ? DPIUtil.pointToPixel(hHint, zoom) : hHint);
624622
//We should never return a size that is to small, RoundingMode.UP ensures we at worst case report
625623
//a size that is a bit too large by half a point
626-
return Win32DPIUtils.pixelToPointAsConservativeSize(computeSizeInPixels(wHint, hHint, changed), zoom);
624+
return Win32DPIUtils.pixelToPointAsConservativeSize(computeSizeInPixels(new Point.OfFloat(wHint, hHint, RoundingMode.UP), changed), zoom);
627625
}
628626

629-
Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
627+
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
630628
int width = DEFAULT_WIDTH;
631629
int height = DEFAULT_HEIGHT;
632-
if (wHint != SWT.DEFAULT) width = wHint;
633-
if (hHint != SWT.DEFAULT) height = hHint;
630+
int zoom = getZoom();
631+
Point hintInPixels = Win32DPIUtils.pointToPixelAsSize(hintInPoints, zoom);
632+
if (hintInPoints.x != SWT.DEFAULT) width = hintInPixels.x;
633+
if (hintInPoints.y != SWT.DEFAULT) height = hintInPixels.y;
634634
int border = getBorderWidthInPixels ();
635635
width += border * 2;
636636
height += border * 2;

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/CoolBar.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,14 @@ protected void checkSubclass () {
140140
if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
141141
}
142142

143-
@Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
143+
@Override
144+
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
145+
int zoom = getZoom();
146+
Point hintInPixels = Win32DPIUtils.pointToPixelAsSize(hintInPoints, zoom);
144147
int width = 0, height = 0;
145148
int border = getBorderWidthInPixels ();
146-
int newWidth = wHint == SWT.DEFAULT ? 0x3FFF : wHint + (border * 2);
147-
int newHeight = hHint == SWT.DEFAULT ? 0x3FFF : hHint + (border * 2);
149+
int newWidth = hintInPoints.x == SWT.DEFAULT ? 0x3FFF : hintInPixels.x + (border * 2);
150+
int newHeight = hintInPoints.y == SWT.DEFAULT ? 0x3FFF : hintInPixels.y + (border * 2);
148151
int count = (int)OS.SendMessage (handle, OS.RB_GETBANDCOUNT, 0, 0);
149152
if (count != 0) {
150153
ignoreResize = true;
@@ -189,8 +192,8 @@ protected void checkSubclass () {
189192
width = height;
190193
height = tmp;
191194
}
192-
if (wHint != SWT.DEFAULT) width = wHint;
193-
if (hHint != SWT.DEFAULT) height = hHint;
195+
if (hintInPoints.x != SWT.DEFAULT) width = hintInPixels.x;
196+
if (hintInPoints.y != SWT.DEFAULT) height = hintInPixels.y;
194197
height += border * 2;
195198
width += border * 2;
196199
return new Point (width, height);
@@ -1225,7 +1228,7 @@ void handleDPIChange(Event event, float scalingFactor) {
12251228
item.setControl(control);
12261229
}
12271230

1228-
Point preferredControlSize = item.getControl().computeSizeInPixels(SWT.DEFAULT, SWT.DEFAULT, true);
1231+
Point preferredControlSize = item.getControl().computeSizeInPixels(new Point(SWT.DEFAULT, SWT.DEFAULT), true);
12291232
int controlWidth = preferredControlSize.x;
12301233
int controlHeight = preferredControlSize.y;
12311234
if (((style & SWT.VERTICAL) != 0)) {

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/DateTime.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.eclipse.swt.*;
1717
import org.eclipse.swt.events.*;
1818
import org.eclipse.swt.graphics.*;
19+
import org.eclipse.swt.internal.*;
1920
import org.eclipse.swt.internal.win32.*;
2021

2122
/**
@@ -217,10 +218,13 @@ protected void checkSubclass () {
217218
if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
218219
}
219220

220-
@Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
221+
@Override
222+
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
221223
checkWidget ();
224+
int zoom = getZoom();
225+
Point hintInPixels = Win32DPIUtils.pointToPixelAsSize(hintInPoints, zoom);
222226
int width = 0, height = 0;
223-
if (wHint == SWT.DEFAULT || hHint == SWT.DEFAULT) {
227+
if (hintInPoints.x == SWT.DEFAULT || hintInPoints.y == SWT.DEFAULT) {
224228
if ((style & SWT.CALENDAR) != 0) {
225229
RECT rect = new RECT ();
226230
OS.SendMessage(handle, OS.MCM_GETMINREQRECT, 0, rect);
@@ -244,8 +248,8 @@ protected void checkSubclass () {
244248
}
245249
if (width == 0) width = DEFAULT_WIDTH;
246250
if (height == 0) height = DEFAULT_HEIGHT;
247-
if (wHint != SWT.DEFAULT) width = wHint;
248-
if (hHint != SWT.DEFAULT) height = hHint;
251+
if (hintInPoints.x != SWT.DEFAULT) width = hintInPixels.x;
252+
if (hintInPoints.y != SWT.DEFAULT) height = hintInPixels.y;
249253
int border = getBorderWidthInPixels ();
250254
width += border * 2;
251255
height += border * 2;

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ExpandBar.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,12 @@ static int checkStyle (int style) {
126126
return style | SWT.NO_BACKGROUND;
127127
}
128128

129-
@Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
129+
@Override
130+
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
131+
int zoom = getZoom();
132+
Point hintInPixels = Win32DPIUtils.pointToPixelAsSize(hintInPoints, zoom);
130133
int height = 0, width = 0;
131-
if (wHint == SWT.DEFAULT || hHint == SWT.DEFAULT) {
134+
if (hintInPoints.x == SWT.DEFAULT || hintInPoints.y == SWT.DEFAULT) {
132135
if (itemCount > 0) {
133136
long hDC = OS.GetDC (handle);
134137
long hTheme = 0;
@@ -168,8 +171,8 @@ static int checkStyle (int style) {
168171
}
169172
if (width == 0) width = DEFAULT_WIDTH;
170173
if (height == 0) height = DEFAULT_HEIGHT;
171-
if (wHint != SWT.DEFAULT) width = wHint;
172-
if (hHint != SWT.DEFAULT) height = hHint;
174+
if (hintInPoints.x != SWT.DEFAULT) width = hintInPixels.x;
175+
if (hintInPoints.y != SWT.DEFAULT) height = hintInPixels.y;
173176
Rectangle trim = computeTrimInPixels (0, 0, width, height);
174177
return new Point (trim.width, trim.height);
175178
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Group.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ protected void checkSubclass () {
138138
if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
139139
}
140140

141-
@Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
141+
@Override
142+
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
142143
checkWidget ();
143-
Point size = super.computeSizeInPixels (wHint, hHint, changed);
144+
Point size = super.computeSizeInPixels (hintInPoints, changed);
144145
int length = text.length ();
145146
if (length != 0) {
146147
String string = fixText (false);

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Label.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,11 @@ static int checkStyle (int style) {
130130
return checkBits (style, SWT.LEFT, SWT.CENTER, SWT.RIGHT, 0, 0, 0);
131131
}
132132

133-
@Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
133+
@Override
134+
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
134135
checkWidget ();
136+
int zoom = getZoom();
137+
Point hintInPixels = Win32DPIUtils.pointToPixelAsSize(hintInPoints, zoom);
135138
int width = 0, height = 0, border = getBorderWidthInPixels ();
136139
if ((style & SWT.SEPARATOR) != 0) {
137140
int lineWidth = getSystemMetrics (OS.SM_CXBORDER);
@@ -140,8 +143,8 @@ static int checkStyle (int style) {
140143
} else {
141144
width = lineWidth * 2; height = DEFAULT_HEIGHT;
142145
}
143-
if (wHint != SWT.DEFAULT) width = wHint;
144-
if (hHint != SWT.DEFAULT) height = hHint;
146+
if (hintInPoints.x != SWT.DEFAULT) width = hintInPixels.x;
147+
if (hintInPoints.y != SWT.DEFAULT) height = hintInPixels.y;
145148
width += border * 2; height += border * 2;
146149
return new Point (width, height);
147150
}
@@ -161,9 +164,9 @@ static int checkStyle (int style) {
161164
} else {
162165
RECT rect = new RECT ();
163166
int flags = OS.DT_CALCRECT | OS.DT_EDITCONTROL | OS.DT_EXPANDTABS;
164-
if ((style & SWT.WRAP) != 0 && wHint != SWT.DEFAULT) {
167+
if ((style & SWT.WRAP) != 0 && hintInPoints.x != SWT.DEFAULT) {
165168
flags |= OS.DT_WORDBREAK;
166-
rect.right = Math.max (0, wHint - width);
169+
rect.right = Math.max (0, hintInPixels.x - width);
167170
}
168171
char [] buffer = new char [length + 1];
169172
OS.GetWindowText (handle, buffer, length + 1);
@@ -174,8 +177,8 @@ static int checkStyle (int style) {
174177
if (newFont != 0) OS.SelectObject (hDC, oldFont);
175178
OS.ReleaseDC (handle, hDC);
176179
}
177-
if (wHint != SWT.DEFAULT) width = wHint;
178-
if (hHint != SWT.DEFAULT) height = hHint;
180+
if (hintInPoints.x != SWT.DEFAULT) width = hintInPixels.x;
181+
if (hintInPoints.y != SWT.DEFAULT) height = hintInPixels.y;
179182
width += border * 2;
180183
height += border * 2;
181184
return new Point (width, height);

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Link.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,10 @@ long callWindowProc (long hwnd, int msg, long wParam, long lParam) {
159159
}
160160

161161
@Override
162-
Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
162+
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
163163
checkWidget ();
164+
int zoom = getZoom();
165+
Point hintInPixels = Win32DPIUtils.pointToPixelAsSize(hintInPoints, zoom);
164166
int width, height;
165167
/*
166168
* When the text is empty, LM_GETIDEALSIZE returns zero width and height,
@@ -178,13 +180,13 @@ Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
178180
OS.ReleaseDC (handle, hDC);
179181
} else {
180182
SIZE size = new SIZE ();
181-
int maxWidth = (wHint == SWT.DEFAULT) ? 0x7fffffff : wHint;
183+
int maxWidth = (hintInPoints.x == SWT.DEFAULT) ? 0x7fffffff : hintInPixels.x;
182184
OS.SendMessage (handle, OS.LM_GETIDEALSIZE, maxWidth, size);
183185
width = size.cx;
184186
height = size.cy;
185187
}
186-
if (wHint != SWT.DEFAULT) width = wHint;
187-
if (hHint != SWT.DEFAULT) height = hHint;
188+
if (hintInPoints.x != SWT.DEFAULT) width = hintInPixels.x;
189+
if (hintInPoints.y != SWT.DEFAULT) height = hintInPixels.y;
188190
int border = getBorderWidthInPixels ();
189191
width += border * 2;
190192
height += border * 2;

0 commit comments

Comments
 (0)