Skip to content

Commit df66a8b

Browse files
ShahzaibIbrahimHeikoKlare
authored andcommitted
Images not aligned properly in Expand Bar
When adding image larger than the header size in expand bar, it was drawn outside of the bar on the top. This was due to wrong calculation and mixing of points and pixels
1 parent d59358c commit df66a8b

File tree

1 file changed

+37
-31
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets

1 file changed

+37
-31
lines changed

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

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class ExpandItem extends Item {
4343
boolean expanded, hover;
4444
int x, y, width, height;
4545
int imageHeight, imageWidth;
46+
private static final int IMAGE_MARGIN = 8;
4647
static final int TEXT_INSET = 6;
4748
static final int BORDER = 1;
4849
static final int CHEVRON_SIZE = 24;
@@ -183,9 +184,13 @@ private void drawChevron (long hDC, RECT rect) {
183184

184185
void drawItem (GC gc, long hTheme, RECT clipRect, boolean drawFocus) {
185186
long hDC = gc.handle;
186-
int headerHeight = parent.getBandHeight ();
187+
int headerHeightinPixels = getHeaderHeightInPixels();
188+
int zoom = getZoom();
189+
int imageHeightInPixels = DPIUtil.scaleUp(imageHeight, zoom);
190+
int imageWidthInPixels = DPIUtil.scaleUp(imageWidth, zoom);
191+
187192
RECT rect = new RECT ();
188-
OS.SetRect (rect, x, y, x + width, y + headerHeight);
193+
OS.SetRect (rect, x, y, x + width, y + headerHeightinPixels);
189194
if (hTheme != 0) {
190195
OS.DrawThemeBackground (hTheme, hDC, OS.EBP_NORMALGROUPHEAD, 0, rect, clipRect);
191196
} else {
@@ -194,14 +199,10 @@ void drawItem (GC gc, long hTheme, RECT clipRect, boolean drawFocus) {
194199
OS.SelectObject (hDC, oldBrush);
195200
}
196201
if (image != null) {
197-
int zoom = getZoom();
198202
rect.left += ExpandItem.TEXT_INSET;
199-
if (imageHeight > headerHeight) {
200-
gc.drawImage (image, DPIUtil.scaleDown(rect.left, zoom), DPIUtil.scaleDown(rect.top + headerHeight - imageHeight, zoom));
201-
} else {
202-
gc.drawImage (image, DPIUtil.scaleDown(rect.left, zoom), DPIUtil.scaleDown(rect.top + (headerHeight - imageHeight) / 2, zoom));
203-
}
204-
rect.left += imageWidth;
203+
int yInPoints = DPIUtil.scaleDown(rect.top + ((headerHeightinPixels - imageHeightInPixels) / 2), zoom);
204+
gc.drawImage (image, DPIUtil.scaleDown(rect.left, zoom), yInPoints);
205+
rect.left += imageWidthInPixels;
205206
}
206207
if (text.length () > 0) {
207208
rect.left += ExpandItem.TEXT_INSET;
@@ -213,8 +214,7 @@ void drawItem (GC gc, long hTheme, RECT clipRect, boolean drawFocus) {
213214
} else {
214215
buffer = (RLE + text).toCharArray ();
215216
}
216-
}
217-
else {
217+
} else {
218218
buffer = text.toCharArray ();
219219
}
220220
if (hTheme != 0) {
@@ -227,7 +227,7 @@ void drawItem (GC gc, long hTheme, RECT clipRect, boolean drawFocus) {
227227
}
228228
int chevronSize = ExpandItem.CHEVRON_SIZE;
229229
rect.left = rect.right - chevronSize;
230-
rect.top = y + (headerHeight - chevronSize) / 2;
230+
rect.top = y;
231231
rect.bottom = rect.top + chevronSize;
232232
if (hTheme != 0) {
233233
int partID = expanded ? OS.EBP_NORMALGROUPCOLLAPSE : OS.EBP_NORMALGROUPEXPAND;
@@ -237,18 +237,18 @@ void drawItem (GC gc, long hTheme, RECT clipRect, boolean drawFocus) {
237237
drawChevron (hDC, rect);
238238
}
239239
if (drawFocus) {
240-
OS.SetRect (rect, x + 1, y + 1, x + width - 2, y + headerHeight - 2);
240+
OS.SetRect (rect, x + 1, y + 1, x + width - 2, y + headerHeightinPixels - 2);
241241
OS.DrawFocusRect (hDC, rect);
242242
}
243243
if (expanded) {
244244
if (!parent.isAppThemed ()) {
245245
long pen = OS.CreatePen (OS.PS_SOLID, 1, OS.GetSysColor (OS.COLOR_BTNFACE));
246246
long oldPen = OS.SelectObject (hDC, pen);
247247
int [] points = {
248-
x, y + headerHeight,
249-
x, y + headerHeight + height,
250-
x + width - 1, y + headerHeight + height,
251-
x + width - 1, y + headerHeight - 1};
248+
x, y + headerHeightinPixels,
249+
x, y + headerHeightinPixels + height,
250+
x + width - 1, y + headerHeightinPixels + height,
251+
x + width - 1, y + headerHeightinPixels - 1};
252252
OS.Polyline (hDC, points, points.length / 2);
253253
OS.SelectObject (hDC, oldPen);
254254
OS.DeleteObject (pen);
@@ -310,7 +310,13 @@ public int getHeaderHeight () {
310310
}
311311

312312
int getHeaderHeightInPixels () {
313-
return Math.max (parent.getBandHeight (), imageHeight);
313+
int headerHeightInPixels = parent.getBandHeight();
314+
int imageHeightInPixels = DPIUtil.scaleUp(imageHeight, getZoom());
315+
int imageHeaderDiff = headerHeightInPixels - imageHeightInPixels;
316+
if (imageHeaderDiff < IMAGE_MARGIN) {
317+
headerHeightInPixels = imageHeightInPixels + IMAGE_MARGIN;
318+
}
319+
return headerHeightInPixels;
314320
}
315321

316322
/**
@@ -372,17 +378,20 @@ boolean isHover (int x, int y) {
372378

373379
void redraw (boolean all) {
374380
long parentHandle = parent.handle;
375-
int headerHeight = parent.getBandHeight ();
381+
int headerHeightInPixels = getHeaderHeightInPixels();
382+
int zoom = getZoom();
383+
int imageHeightInPixels = DPIUtil.scaleUp(imageHeight, zoom);
384+
int imageWidthInPixels = DPIUtil.scaleUp(imageWidth, zoom);
376385
RECT rect = new RECT ();
377-
int left = all ? x : x + width - headerHeight;
378-
OS.SetRect (rect, left, y, x + width, y + headerHeight);
386+
int left = all ? x : x + width - headerHeightInPixels;
387+
OS.SetRect (rect, left, y, x + width, y + headerHeightInPixels);
379388
OS.InvalidateRect (parentHandle, rect, true);
380-
if (imageHeight > headerHeight) {
381-
OS.SetRect (rect, x + ExpandItem.TEXT_INSET, y + headerHeight - imageHeight, x + ExpandItem.TEXT_INSET + imageWidth, y);
389+
if (imageHeightInPixels > headerHeightInPixels) {
390+
OS.SetRect (rect, x + ExpandItem.TEXT_INSET, y + headerHeightInPixels - imageHeightInPixels, x + ExpandItem.TEXT_INSET + imageWidthInPixels, y);
382391
OS.InvalidateRect (parentHandle, rect, true);
383392
}
384393
if (!parent.isAppThemed ()) {
385-
OS.SetRect (rect, x, y + headerHeight, x + width, y + headerHeight + height + 1);
394+
OS.SetRect (rect, x, y + headerHeightInPixels, x + width, y + headerHeightInPixels + height + 1);
386395
OS.InvalidateRect (parentHandle, rect, true);
387396
}
388397
}
@@ -401,11 +410,8 @@ void releaseWidget () {
401410

402411
void setBoundsInPixels (int x, int y, int width, int height, boolean move, boolean size) {
403412
redraw (true);
404-
int headerHeight = parent.getBandHeight ();
413+
int headerHeightInPixels = getHeaderHeightInPixels();
405414
if (move) {
406-
if (imageHeight > headerHeight) {
407-
y += (imageHeight - headerHeight);
408-
}
409415
this.x = x;
410416
this.y = y;
411417
redraw (true);
@@ -421,8 +427,8 @@ void setBoundsInPixels (int x, int y, int width, int height, boolean move, boole
421427
width = Math.max (0, width - BORDER * 2);
422428
height = Math.max (0, height - BORDER);
423429
}
424-
if (move && size) control.setBoundsInPixels (x, y + headerHeight, width, height);
425-
if (move && !size) control.setLocationInPixels (x, y + headerHeight);
430+
if (move && size) control.setBoundsInPixels (x, y + headerHeightInPixels, width, height);
431+
if (move && !size) control.setLocationInPixels (x, y + headerHeightInPixels);
426432
if (!move && size) control.setSizeInPixels (width, height);
427433
}
428434
}
@@ -504,7 +510,7 @@ public void setImage (Image image) {
504510
super.setImage (image);
505511
int oldImageHeight = imageHeight;
506512
if (image != null) {
507-
Rectangle bounds = image.getBoundsInPixels ();
513+
Rectangle bounds = image.getBounds();
508514
imageHeight = bounds.height;
509515
imageWidth = bounds.width;
510516
} else {

0 commit comments

Comments
 (0)