Skip to content

Commit 8aeba54

Browse files
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 5dd0c28 commit 8aeba54

File tree

3 files changed

+37
-32
lines changed

3 files changed

+37
-32
lines changed

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

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,13 @@ private void drawChevron (long hDC, RECT rect) {
183183

184184
void drawItem (GC gc, long hTheme, RECT clipRect, boolean drawFocus) {
185185
long hDC = gc.handle;
186-
int headerHeight = parent.getBandHeight ();
186+
int headerHeightinPixels = getHeaderHeightInPixels();
187+
int zoom = getZoom();
188+
int imageHeightInPixels = DPIUtil.scaleUp(imageHeight, zoom);
189+
int imageWidthInPixels = DPIUtil.scaleUp(imageWidth, zoom);
190+
187191
RECT rect = new RECT ();
188-
OS.SetRect (rect, x, y, x + width, y + headerHeight);
192+
OS.SetRect (rect, x, y, x + width, y + headerHeightinPixels);
189193
if (hTheme != 0) {
190194
OS.DrawThemeBackground (hTheme, hDC, OS.EBP_NORMALGROUPHEAD, 0, rect, clipRect);
191195
} else {
@@ -194,14 +198,10 @@ void drawItem (GC gc, long hTheme, RECT clipRect, boolean drawFocus) {
194198
OS.SelectObject (hDC, oldBrush);
195199
}
196200
if (image != null) {
197-
int zoom = getZoom();
198201
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;
202+
int yInPoints = DPIUtil.scaleDown(rect.top + ((headerHeightinPixels - imageHeightInPixels) / 2), zoom);
203+
gc.drawImage (image, DPIUtil.scaleDown(rect.left, zoom), yInPoints);
204+
rect.left += imageWidthInPixels;
205205
}
206206
if (text.length () > 0) {
207207
rect.left += ExpandItem.TEXT_INSET;
@@ -213,8 +213,7 @@ void drawItem (GC gc, long hTheme, RECT clipRect, boolean drawFocus) {
213213
} else {
214214
buffer = (RLE + text).toCharArray ();
215215
}
216-
}
217-
else {
216+
} else {
218217
buffer = text.toCharArray ();
219218
}
220219
if (hTheme != 0) {
@@ -227,7 +226,7 @@ void drawItem (GC gc, long hTheme, RECT clipRect, boolean drawFocus) {
227226
}
228227
int chevronSize = ExpandItem.CHEVRON_SIZE;
229228
rect.left = rect.right - chevronSize;
230-
rect.top = y + (headerHeight - chevronSize) / 2;
229+
rect.top = y;
231230
rect.bottom = rect.top + chevronSize;
232231
if (hTheme != 0) {
233232
int partID = expanded ? OS.EBP_NORMALGROUPCOLLAPSE : OS.EBP_NORMALGROUPEXPAND;
@@ -237,18 +236,18 @@ void drawItem (GC gc, long hTheme, RECT clipRect, boolean drawFocus) {
237236
drawChevron (hDC, rect);
238237
}
239238
if (drawFocus) {
240-
OS.SetRect (rect, x + 1, y + 1, x + width - 2, y + headerHeight - 2);
239+
OS.SetRect (rect, x + 1, y + 1, x + width - 2, y + headerHeightinPixels - 2);
241240
OS.DrawFocusRect (hDC, rect);
242241
}
243242
if (expanded) {
244243
if (!parent.isAppThemed ()) {
245244
long pen = OS.CreatePen (OS.PS_SOLID, 1, OS.GetSysColor (OS.COLOR_BTNFACE));
246245
long oldPen = OS.SelectObject (hDC, pen);
247246
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};
247+
x, y + headerHeightinPixels,
248+
x, y + headerHeightinPixels + height,
249+
x + width - 1, y + headerHeightinPixels + height,
250+
x + width - 1, y + headerHeightinPixels - 1};
252251
OS.Polyline (hDC, points, points.length / 2);
253252
OS.SelectObject (hDC, oldPen);
254253
OS.DeleteObject (pen);
@@ -310,7 +309,12 @@ public int getHeaderHeight () {
310309
}
311310

312311
int getHeaderHeightInPixels () {
313-
return Math.max (parent.getBandHeight (), imageHeight);
312+
int headerHeightInPixels = parent.getBandHeight ();
313+
int imageHeightInPixels = DPIUtil.scaleUp(imageHeight, getZoom());
314+
if(imageHeightInPixels > headerHeightInPixels) {
315+
headerHeightInPixels = imageHeightInPixels + headerHeightInPixels;
316+
}
317+
return headerHeightInPixels;
314318
}
315319

316320
/**
@@ -372,17 +376,20 @@ boolean isHover (int x, int y) {
372376

373377
void redraw (boolean all) {
374378
long parentHandle = parent.handle;
375-
int headerHeight = parent.getBandHeight ();
379+
int headerHeightInPixels = getHeaderHeightInPixels();
380+
int zoom = getZoom();
381+
int imageHeightInPixels = DPIUtil.scaleUp(imageHeight, zoom);
382+
int imageWidthInPixels = DPIUtil.scaleUp(imageWidth, zoom);
376383
RECT rect = new RECT ();
377-
int left = all ? x : x + width - headerHeight;
378-
OS.SetRect (rect, left, y, x + width, y + headerHeight);
384+
int left = all ? x : x + width - headerHeightInPixels;
385+
OS.SetRect (rect, left, y, x + width, y + headerHeightInPixels);
379386
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);
387+
if (imageHeightInPixels > headerHeightInPixels) {
388+
OS.SetRect (rect, x + ExpandItem.TEXT_INSET, y + headerHeightInPixels - imageHeightInPixels, x + ExpandItem.TEXT_INSET + imageWidthInPixels, y);
382389
OS.InvalidateRect (parentHandle, rect, true);
383390
}
384391
if (!parent.isAppThemed ()) {
385-
OS.SetRect (rect, x, y + headerHeight, x + width, y + headerHeight + height + 1);
392+
OS.SetRect (rect, x, y + headerHeightInPixels, x + width, y + headerHeightInPixels + height + 1);
386393
OS.InvalidateRect (parentHandle, rect, true);
387394
}
388395
}
@@ -401,11 +408,8 @@ void releaseWidget () {
401408

402409
void setBoundsInPixels (int x, int y, int width, int height, boolean move, boolean size) {
403410
redraw (true);
404-
int headerHeight = parent.getBandHeight ();
411+
int headerHeightInPixels = getHeaderHeightInPixels();
405412
if (move) {
406-
if (imageHeight > headerHeight) {
407-
y += (imageHeight - headerHeight);
408-
}
409413
this.x = x;
410414
this.y = y;
411415
redraw (true);
@@ -421,8 +425,8 @@ void setBoundsInPixels (int x, int y, int width, int height, boolean move, boole
421425
width = Math.max (0, width - BORDER * 2);
422426
height = Math.max (0, height - BORDER);
423427
}
424-
if (move && size) control.setBoundsInPixels (x, y + headerHeight, width, height);
425-
if (move && !size) control.setLocationInPixels (x, y + headerHeight);
428+
if (move && size) control.setBoundsInPixels (x, y + headerHeightInPixels, width, height);
429+
if (move && !size) control.setLocationInPixels (x, y + headerHeightInPixels);
426430
if (!move && size) control.setSizeInPixels (width, height);
427431
}
428432
}
@@ -504,7 +508,7 @@ public void setImage (Image image) {
504508
super.setImage (image);
505509
int oldImageHeight = imageHeight;
506510
if (image != null) {
507-
Rectangle bounds = image.getBoundsInPixels ();
511+
Rectangle bounds = image.getBounds();
508512
imageHeight = bounds.height;
509513
imageWidth = bounds.width;
510514
} else {

examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/ControlExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class ControlExample {
4444
static final int ciClosedFolder = 0, ciOpenFolder = 1, ciTarget = 2, ciBackground = 3, ciParentBackground = 4;
4545
static final String[] imageLocations = {
4646
"closedFolder.gif", //$NON-NLS-1$
47-
"openFolder.gif", //$NON-NLS-1$
47+
"pixelated.png", //$NON-NLS-1$
4848
"target.gif", //$NON-NLS-1$
4949
"backgroundImage.png", //$NON-NLS-1$
5050
"parentBackgroundImage.png"}; //$NON-NLS-1$

examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/ExpandBarTab.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ void createExampleWidgets () {
8080
item.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
8181
item.setControl(composite);
8282
item.setImage(instance.images[ControlExample.ciClosedFolder]);
83+
item.setExpanded(true);
8384

8485
// Second item
8586
composite = new Composite (expandBar1, SWT.NONE);

0 commit comments

Comments
 (0)