Skip to content

Commit c1712f2

Browse files
committed
Use Rectangle.OfFloat in Widgets getter/setter
This commit introduces the use of Rectangle.OfFloat in win32 Widgets' getters and setter for a better precision. contributes to #62
1 parent f19d02a commit c1712f2

36 files changed

+157
-152
lines changed

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,11 @@ public void setHeight(float height) {
460460
this.residualHeight = height - this.height;
461461
}
462462

463+
public static Rectangle.OfFloat from(Rectangle rectangle) {
464+
if(rectangle instanceof Rectangle.OfFloat rectangleOfFloat) return rectangleOfFloat;
465+
return new Rectangle.OfFloat(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
466+
}
467+
463468
}
464469

465470
/**

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ public Rectangle getBounds () {
127127
Rectangle getBoundsInPixels () {
128128
if (image != null) {
129129
Rectangle rect = DPIUtil.scaleUp(image.getBounds(), getZoom());
130-
return new Rectangle (getXInPixels(), getYInPixels(), rect.width, rect.height);
130+
return new Rectangle.OfFloat (getXInPixels(), getYInPixels(), rect.width, rect.height);
131131
}
132132
if (width == 0) {
133133
int [] buffer = new int [1];
134134
if (OS.SystemParametersInfo (OS.SPI_GETCARETWIDTH, 0, buffer, 0)) {
135-
return new Rectangle (getXInPixels(), getYInPixels(), buffer [0], getHeightInPixels());
135+
return new Rectangle.OfFloat (getXInPixels(), getYInPixels(), buffer [0], getHeightInPixels());
136136
}
137137
}
138-
return new Rectangle (getXInPixels(), getYInPixels(), getWidthInPixels(), getHeightInPixels());
138+
return new Rectangle.OfFloat (getXInPixels(), getYInPixels(), getWidthInPixels(), getHeightInPixels());
139139
}
140140

141141
/**
@@ -185,7 +185,7 @@ public Image getImage () {
185185
*/
186186
public Point getLocation () {
187187
checkWidget();
188-
return new Point (x, y);
188+
return new Point.OfFloat (x, y);
189189
}
190190

191191
/**
@@ -221,15 +221,15 @@ public Point getSize () {
221221
Point getSizeInPixels () {
222222
if (image != null) {
223223
Rectangle rect = DPIUtil.scaleUp(image.getBounds(), getZoom());
224-
return new Point (rect.width, rect.height);
224+
return new Point.OfFloat (rect.width, rect.height);
225225
}
226226
if (width == 0) {
227227
int [] buffer = new int [1];
228228
if (OS.SystemParametersInfo (OS.SPI_GETCARETWIDTH, 0, buffer, 0)) {
229-
return new Point (buffer [0], getHeightInPixels());
229+
return new Point.OfFloat (buffer [0], getHeightInPixels());
230230
}
231231
}
232-
return new Point (getWidthInPixels(), getHeightInPixels());
232+
return new Point.OfFloat (getWidthInPixels(), getHeightInPixels());
233233
}
234234

235235
private int getWidthInPixels() {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ public void clearSelection () {
667667
if ((style & SWT.SIMPLE) != 0 && (style & SWT.H_SCROLL) != 0) {
668668
height += getSystemMetrics (OS.SM_CYHSCROLL);
669669
}
670-
return new Point (width, height);
670+
return new Point.OfFloat (width, height);
671671
}
672672

673673
/**
@@ -960,7 +960,7 @@ Point getCaretLocationInPixels () {
960960
point.x = OS.GET_X_LPARAM (caretPos);
961961
point.y = OS.GET_Y_LPARAM (caretPos);
962962
OS.MapWindowPoints (hwndText, handle, point, 1);
963-
return new Point (point.x, point.y);
963+
return new Point.OfFloat (point.x, point.y);
964964
}
965965

966966
/**
@@ -1283,11 +1283,11 @@ String getSegmentsText (String text, Event event) {
12831283
public Point getSelection () {
12841284
checkWidget ();
12851285
if ((style & SWT.DROP_DOWN) != 0 && (style & SWT.READ_ONLY) != 0) {
1286-
return new Point (0, OS.GetWindowTextLength (handle));
1286+
return new Point.OfFloat (0, OS.GetWindowTextLength (handle));
12871287
}
12881288
int [] start = new int [1], end = new int [1];
12891289
OS.SendMessage (handle, OS.CB_GETEDITSEL, start, end);
1290-
return new Point (untranslateOffset (start [0]), untranslateOffset (end [0]));
1290+
return new Point.OfFloat (untranslateOffset (start [0]), untranslateOffset (end [0]));
12911291
}
12921292

12931293
/**

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
221221
int zoom = getZoom();
222222
size = DPIUtil.scaleUp(layout.computeSize (this, DPIUtil.scaleDown(wHint, zoom), DPIUtil.scaleDown(hHint, zoom), changed), zoom);
223223
} else {
224-
size = new Point (wHint, hHint);
224+
size = new Point.OfFloat (wHint, hHint);
225225
}
226226
} else {
227227
size = minimumSize (wHint, hHint, changed);
@@ -236,7 +236,7 @@ Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
236236
*/
237237
int zoom = getZoom();
238238
Rectangle trim = DPIUtil.scaleUp(computeTrim (0, 0, DPIUtil.scaleDown(size.x, zoom), DPIUtil.scaleDown(size.y, zoom)), zoom);
239-
return new Point (trim.width, trim.height);
239+
return new Point.OfFloat (trim.width, trim.height);
240240
}
241241

242242
/**
@@ -356,7 +356,7 @@ int applyThemeBackground () {
356356
public void drawBackground (GC gc, int x, int y, int width, int height, int offsetX, int offsetY) {
357357
checkWidget ();
358358
int zoom = getZoom();
359-
Rectangle rectangle = DPIUtil.scaleUp(new Rectangle(x, y, width, height), zoom);
359+
Rectangle rectangle = DPIUtil.scaleUp(new Rectangle.OfFloat(x, y, width, height), zoom);
360360
offsetX = DPIUtil.scaleUp(offsetX, zoom);
361361
offsetY = DPIUtil.scaleUp(offsetY, zoom);
362362
drawBackgroundInPixels(gc, rectangle.x, rectangle.y, rectangle.width, rectangle.height, offsetX, offsetY);
@@ -886,7 +886,7 @@ Point minimumSize (int wHint, int hHint, boolean changed) {
886886
width = Math.max (width, rect.x - clientArea.x + rect.width);
887887
height = Math.max (height, rect.y - clientArea.y + rect.height);
888888
}
889-
return new Point (width, height);
889+
return new Point.OfFloat (width, height);
890890
}
891891

892892
@Override
@@ -1539,7 +1539,7 @@ LRESULT WM_PAINT (long wParam, long lParam) {
15391539

15401540
Event event = new Event ();
15411541
event.gc = gc;
1542-
event.setBounds(DPIUtil.scaleDown(new Rectangle(ps.left, ps.top, width, height), getZoom()));
1542+
event.setBounds(DPIUtil.scaleDown(new Rectangle.OfFloat(ps.left, ps.top, width, height), getZoom()));
15431543
sendEvent (SWT.Paint, event);
15441544
if (data.focusDrawn && !isDisposed ()) updateUIState ();
15451545
gc.dispose ();
@@ -1622,7 +1622,7 @@ LRESULT WM_PAINT (long wParam, long lParam) {
16221622
if ((style & (SWT.DOUBLE_BUFFERED | SWT.NO_BACKGROUND | SWT.TRANSPARENT)) == 0) {
16231623
drawBackground (gc.handle, rect);
16241624
}
1625-
event.setBounds(DPIUtil.scaleDown(new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top), zoom));
1625+
event.setBounds(DPIUtil.scaleDown(new Rectangle.OfFloat(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top), zoom));
16261626
event.count = count - 1 - i;
16271627
sendEvent (SWT.Paint, event);
16281628
}
@@ -1632,7 +1632,7 @@ LRESULT WM_PAINT (long wParam, long lParam) {
16321632
OS.SetRect (rect, ps.left, ps.top, ps.right, ps.bottom);
16331633
drawBackground (gc.handle, rect);
16341634
}
1635-
event.setBounds(DPIUtil.scaleDown(new Rectangle(ps.left, ps.top, width, height), zoom));
1635+
event.setBounds(DPIUtil.scaleDown(new Rectangle.OfFloat(ps.left, ps.top, width, height), zoom));
16361636
sendEvent (SWT.Paint, event);
16371637
}
16381638
// widget could be disposed at this point
@@ -1707,7 +1707,7 @@ LRESULT WM_PRINTCLIENT (long wParam, long lParam) {
17071707
GC gc = createNewGC(wParam, data);
17081708
Event event = new Event ();
17091709
event.gc = gc;
1710-
event.setBounds(DPIUtil.scaleDown(new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top), getZoom()));
1710+
event.setBounds(DPIUtil.scaleDown(new Rectangle.OfFloat(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top), getZoom()));
17111711
sendEvent (SWT.Paint, event);
17121712
event.gc = null;
17131713
gc.dispose ();

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
631631
int border = getBorderWidthInPixels ();
632632
width += border * 2;
633633
height += border * 2;
634-
return new Point (width, height);
634+
return new Point.OfFloat (width, height);
635635
}
636636

637637
Widget computeTabGroup () {
@@ -1210,7 +1210,7 @@ Rectangle getBoundsInPixels () {
12101210
OS.MapWindowPoints (0, hwndParent, rect, 2);
12111211
int width = rect.right - rect.left;
12121212
int height = rect.bottom - rect.top;
1213-
return new Rectangle (rect.left, rect.top, width, height);
1213+
return new Rectangle.OfFloat (rect.left, rect.top, width, height);
12141214
}
12151215

12161216
int getCodePage () {
@@ -1381,7 +1381,7 @@ Point getLocationInPixels () {
13811381
OS.GetWindowRect (topHandle (), rect);
13821382
long hwndParent = parent == null ? 0 : parent.handle;
13831383
OS.MapWindowPoints (0, hwndParent, rect, 2);
1384-
return new Point (rect.left, rect.top);
1384+
return new Point.OfFloat (rect.left, rect.top);
13851385
}
13861386

13871387
/**
@@ -1537,7 +1537,7 @@ Point getSizeInPixels () {
15371537
OS.GetWindowRect (topHandle (), rect);
15381538
int width = rect.right - rect.left;
15391539
int height = rect.bottom - rect.top;
1540-
return new Point (width, height);
1540+
return new Point.OfFloat (width, height);
15411541
}
15421542

15431543
/**
@@ -2450,7 +2450,7 @@ public void redraw (int x, int y, int width, int height, boolean all) {
24502450
checkWidget ();
24512451
int zoom = getZoom();
24522452
if (width <= 0 || height <= 0) return;
2453-
Rectangle rectangle = DPIUtil.scaleUp(new Rectangle(x, y, width, height), zoom);
2453+
Rectangle rectangle = DPIUtil.scaleUp(new Rectangle.OfFloat(x, y, width, height), zoom);
24542454

24552455
RECT rect = new RECT ();
24562456
OS.SetRect (rect, rectangle.x, rectangle.y, rectangle.x + rectangle.width, rectangle.y + rectangle.height);
@@ -3186,7 +3186,7 @@ void setBackgroundPixel (int pixel) {
31863186
* </ul>
31873187
*/
31883188
public void setBounds(int x, int y, int width, int height) {
3189-
setBounds(new Rectangle(x, y, width, height));
3189+
setBounds(new Rectangle.OfFloat(x, y, width, height));
31903190
}
31913191

31923192
void setBoundsInPixels (int x, int y, int width, int height) {
@@ -4029,7 +4029,7 @@ void subclass () {
40294029
public Point toControl (int x, int y) {
40304030
checkWidget ();
40314031
int zoom = getZoom();
4032-
Point displayPointInPixels = getDisplay().translateToDisplayCoordinates(new Point(x, y), zoom);
4032+
Point displayPointInPixels = getDisplay().translateToDisplayCoordinates(new Point.OfFloat(x, y), zoom);
40334033
final Point controlPointInPixels = toControlInPixels(displayPointInPixels.x, displayPointInPixels.y);
40344034
return DPIUtil.scaleDown(controlPointInPixels, zoom);
40354035
}
@@ -4038,7 +4038,7 @@ Point toControlInPixels (int x, int y) {
40384038
POINT pt = new POINT ();
40394039
pt.x = x; pt.y = y;
40404040
OS.ScreenToClient (handle, pt);
4041-
return new Point (pt.x, pt.y);
4041+
return new Point.OfFloat (pt.x, pt.y);
40424042
}
40434043

40444044
/**
@@ -4098,7 +4098,7 @@ Point toDisplayInPixels (int x, int y) {
40984098
POINT pt = new POINT ();
40994099
pt.x = x; pt.y = y;
41004100
OS.ClientToScreen (handle, pt);
4101-
return new Point (pt.x, pt.y);
4101+
return new Point.OfFloat (pt.x, pt.y);
41024102
}
41034103

41044104
/**

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ protected void checkSubclass () {
194194
if (hHint != SWT.DEFAULT) height = hHint;
195195
height += border * 2;
196196
width += border * 2;
197-
return new Point (width, height);
197+
return new Point.OfFloat (width, height);
198198
}
199199

200200
@Override
@@ -557,7 +557,7 @@ public int getItemCount () {
557557

558558
Point [] getItemSizesInPixels () {
559559
int count = (int)OS.SendMessage (handle, OS.RB_GETBANDCOUNT, 0, 0);
560-
Point [] sizes = new Point [count];
560+
Point [] sizes = new Point.OfFloat [count];
561561
REBARBANDINFO rbBand = new REBARBANDINFO ();
562562
rbBand.cbSize = REBARBANDINFO.sizeof;
563563
rbBand.fMask = OS.RBBIM_CHILDSIZE;
@@ -572,9 +572,9 @@ public int getItemCount () {
572572
rect.right += margins.cxRightWidth;
573573
if (!isLastItemOfRow(i)) rect.right += separator;
574574
if ((style & SWT.VERTICAL) != 0) {
575-
sizes [i] = new Point (rbBand.cyChild, rect.right - rect.left);
575+
sizes [i] = new Point.OfFloat (rbBand.cyChild, rect.right - rect.left);
576576
} else {
577-
sizes [i] = new Point (rect.right - rect.left, rbBand.cyChild);
577+
sizes [i] = new Point.OfFloat (rect.right - rect.left, rbBand.cyChild);
578578
}
579579
}
580580
return sizes;

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ Point computeSizeInPixels (int wHint, int hHint) {
201201
} else {
202202
width += parent.getMargin (index);
203203
}
204-
return new Point (width, height);
204+
return new Point.OfFloat (width, height);
205205
}
206206

207207
@Override
@@ -242,9 +242,9 @@ Rectangle getBoundsInPixels () {
242242
int width = rect.right - rect.left;
243243
int height = rect.bottom - rect.top;
244244
if ((parent.style & SWT.VERTICAL) != 0) {
245-
return new Rectangle (rect.top, rect.left, height, width);
245+
return new Rectangle.OfFloat (rect.top, rect.left, height, width);
246246
}
247-
return new Rectangle (rect.left, rect.top, width, height);
247+
return new Rectangle.OfFloat (rect.left, rect.top, width, height);
248248
}
249249

250250
Rectangle getClientArea () {
@@ -272,7 +272,7 @@ Rectangle getClientArea () {
272272
OS.SendMessage (hwnd, OS.RB_GETBANDINFO, index, rbBand);
273273
width = width - rbBand.cxHeader + 1;
274274
}
275-
return new Rectangle (x, y, Math.max (0, width), Math.max (0, height));
275+
return new Rectangle.OfFloat (x, y, Math.max (0, width), Math.max (0, height));
276276
}
277277

278278
/**
@@ -396,9 +396,9 @@ Point getPreferredSizeInPixels () {
396396
OS.SendMessage (hwnd, OS.RB_GETBANDINFO, index, rbBand);
397397
int width = rbBand.cxIdeal + parent.getMargin (index);
398398
if ((parent.style & SWT.VERTICAL) != 0) {
399-
return new Point (rbBand.cyMaxChild, width);
399+
return new Point.OfFloat (rbBand.cyMaxChild, width);
400400
}
401-
return new Point (width, rbBand.cyMaxChild);
401+
return new Point.OfFloat (width, rbBand.cyMaxChild);
402402
}
403403

404404
/**
@@ -502,9 +502,9 @@ Point getSizeInPixels() {
502502
int width = rect.right - rect.left;
503503
int height = rect.bottom - rect.top;
504504
if ((parent.style & SWT.VERTICAL) != 0) {
505-
return new Point (height, width);
505+
return new Point.OfFloat (height, width);
506506
}
507-
return new Point (width, height);
507+
return new Point.OfFloat (width, height);
508508
}
509509

510510
/**
@@ -623,9 +623,9 @@ Point getMinimumSizeInPixels () {
623623
rbBand.fMask = OS.RBBIM_CHILDSIZE;
624624
OS.SendMessage (hwnd, OS.RB_GETBANDINFO, index, rbBand);
625625
if ((parent.style & SWT.VERTICAL) != 0) {
626-
return new Point (rbBand.cyMinChild, rbBand.cxMinChild);
626+
return new Point.OfFloat (rbBand.cyMinChild, rbBand.cxMinChild);
627627
}
628-
return new Point (rbBand.cxMinChild, rbBand.cyMinChild);
628+
return new Point.OfFloat (rbBand.cxMinChild, rbBand.cyMinChild);
629629
}
630630

631631
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ protected void checkSubclass () {
249249
int border = getBorderWidthInPixels ();
250250
width += border * 2;
251251
height += border * 2;
252-
return new Point (width, height);
252+
return new Point.OfFloat (width, height);
253253
}
254254

255255
@Override

0 commit comments

Comments
 (0)