Skip to content

Commit

Permalink
Correctly report the required size to paint the PGroup control
Browse files Browse the repository at this point in the history
Currently if the the content of the PGroup is smaller than the title
area width the text is cropped and possibly not readable because the
PGroup does not report the title area with as it preferred width.

This now changes the computation so that the hints are passed to the
render and then it could report the desired size.
  • Loading branch information
laeubi committed Dec 25, 2024
1 parent 2843cbe commit 36b4ba8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public Rectangle getClientArea()
*/
public Rectangle computeTrim(int x, int y, int width, int height)
{
Rectangle area = new Rectangle(x, y, width, height);
Rectangle area = new Rectangle(x, y, Math.max(0, width), Math.max(0, height));
area.x -= margin;
area.y -= titleHeight;
area.width += (2 * margin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,16 +612,20 @@ public void setText(String text)
/**
* @see org.eclipse.swt.widgets.Control#computeSize(int, int, boolean)
*/
public Point computeSize(int arg0, int arg1, boolean arg2)
{
checkWidget();
if (getExpanded())
return super.computeSize(arg0, arg1, arg2);

Rectangle trim = strategy.computeTrim(0, 0, 0, 0);
trim.width = super.computeSize(arg0, arg1, arg2).x;
return new Point(trim.width, Math.max(trim.height, arg1));
}
@Override
public Point computeSize(int wHint, int hHint, boolean changed) {
checkWidget();
if(changed) {
strategy.update();
}
Rectangle trim = strategy.computeTrim(0, 0, wHint, 0);
Point controlSize = super.computeSize(wHint, hHint, changed);
if(!getExpanded()) {
controlSize.y = Math.max(trim.height, hHint);
}
controlSize.x = Math.max(Math.max(controlSize.x, trim.width), wHint);
return controlSize;
}

/**
* Returns the expanded/collapsed state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class RectangleGroupStrategy extends AbstractGroupStrategy
private Color g2;

private int titleHeight;
private int titleWidth;

private int fontHeight;

Expand Down Expand Up @@ -577,16 +578,27 @@ public Rectangle getClientArea()
* @see org.eclipse.nebula.widgets.pgroup.AbstractGroupStrategy#computeTrim(int, int, int, int)
*/
@Override
public Rectangle computeTrim(int x, int y, int width, int height)
public Rectangle computeTrim(int x, int y, int widthHint, int heightHint)
{
Rectangle area = new Rectangle(x, y, width, height);

Rectangle area = new Rectangle(x, y, Math.max(0, widthHint), Math.max(0, heightHint));
area.x -= margin;
area.y -= titleHeight;
area.width += (2 * margin);
area.width += (2 * margin);
PGroup group = getGroup();
if(widthHint == SWT.DEFAULT) {
// This indicates the caller wants the preferred size to be computed so we should give a clue how much space the control will take up without hiding anything
area.width += titleWidth + 2 * hMargin + betweenSpacing;
AbstractRenderer toggleRenderer = group.getToggleRenderer();
if(toggleRenderer != null) {
Point p = toggleRenderer.getSize();
area.width += p.x;
}
}
area.height += titleHeight;
if (getGroup().getExpanded())
if(group.getExpanded())
{
if ((getGroup().getStyle() & SWT.SMOOTH) != 0)
if((group.getStyle() & SWT.SMOOTH) != 0)
{
area.height += 5;
}
Expand Down Expand Up @@ -762,44 +774,37 @@ public void setBorderColor(Color borderColor)
this.borderColor = borderColor;
}

@Override
public void update()
{
GC gc = new GC(getGroup());

titleHeight = 0;
@Override
public void update() {

int imageHeight = 0;
if (getGroup().getImage() != null) {
imageHeight = getGroup().getImage().getBounds().height;
PGroup group = getGroup();
GC gc = new GC(group);
try {
String text = group.getText();
Point extent = gc.stringExtent(text);
titleWidth = extent.x;
System.out.println("'" + text + "' = " + extent);
int imageHeight = 0;
if(group.getImage() != null) {
imageHeight = group.getImage().getBounds().height;
}
if((group.getImagePosition() & SWT.TOP) == 0) {
titleHeight = Math.max(gc.getFontMetrics().getHeight() + (2 * titleTextMargin), imageHeight);
titleHeight += (2 * vMargin);
} else {
titleHeight = Math.max(gc.getFontMetrics().getHeight() + (2 * titleTextMargin) + (2 * vMargin), imageHeight + 1);
}
if(group.getToggleRenderer() != null) {
int toggleHeight = group.getToggleRenderer().getSize().y;
titleHeight = Math.max(toggleHeight + (2 * vMargin), titleHeight);
}
fontHeight = gc.getFontMetrics().getHeight();
titleAreaHeight = fontHeight + (2 * titleTextMargin) + (2 * vMargin);
if(group.getToggleRenderer() != null) {
titleAreaHeight = Math.max(titleAreaHeight, group.getToggleRenderer().getSize().y + (2 * vMargin));
}
} finally {
gc.dispose();
}
if ((getGroup().getImagePosition() & SWT.TOP) == 0)
{
titleHeight = Math.max(gc.getFontMetrics().getHeight() + (2 * titleTextMargin),
imageHeight);
titleHeight += (2 * vMargin);
}
else
{
titleHeight = Math.max(gc.getFontMetrics().getHeight() + (2 * titleTextMargin)
+ (2 * vMargin), imageHeight + 1);
}
if (getGroup().getToggleRenderer() != null)
{
int toggleHeight = getGroup().getToggleRenderer().getSize().y;
titleHeight = Math.max(toggleHeight + (2 * vMargin), titleHeight);
}

fontHeight = gc.getFontMetrics().getHeight();

titleAreaHeight = fontHeight + (2 * titleTextMargin) + (2 * vMargin);
if (getGroup().getToggleRenderer() != null)
{
titleAreaHeight = Math.max(titleAreaHeight, getGroup().getToggleRenderer()
.getSize().y
+ (2 * vMargin));
}

gc.dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public Rectangle getClientArea()
*/
public Rectangle computeTrim(int x, int y, int width, int height)
{
Rectangle area = new Rectangle(x, y, width, height);
Rectangle area = new Rectangle(x, y, Math.max(0, width), Math.max(0, height));
area.y -= titleHeight;
area.height += titleHeight;
return area;
Expand Down

0 comments on commit 36b4ba8

Please sign in to comment.