From 42e943d126db1f5b5ec045d0c1351662277d5fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Wed, 25 Dec 2024 08:29:46 +0100 Subject: [PATCH] Correctly report the required size to paint the PGroup control 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. --- .../widgets/pgroup/FormGroupStrategy.java | 2 +- .../eclipse/nebula/widgets/pgroup/PGroup.java | 24 ++--- .../pgroup/RectangleGroupStrategy.java | 90 ++++++++++--------- .../widgets/pgroup/SimpleGroupStrategy.java | 2 +- 4 files changed, 63 insertions(+), 55 deletions(-) diff --git a/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/FormGroupStrategy.java b/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/FormGroupStrategy.java index 9d682770a..47d891fe8 100644 --- a/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/FormGroupStrategy.java +++ b/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/FormGroupStrategy.java @@ -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); diff --git a/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/PGroup.java b/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/PGroup.java index 4690b0d1c..67628fa9a 100644 --- a/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/PGroup.java +++ b/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/PGroup.java @@ -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. diff --git a/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/RectangleGroupStrategy.java b/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/RectangleGroupStrategy.java index 25e21c310..0e9ad4d88 100644 --- a/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/RectangleGroupStrategy.java +++ b/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/RectangleGroupStrategy.java @@ -58,6 +58,7 @@ public class RectangleGroupStrategy extends AbstractGroupStrategy private Color g2; private int titleHeight; + private int titleWidth; private int fontHeight; @@ -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; } @@ -762,44 +774,36 @@ 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; + 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(); - } + } } diff --git a/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/SimpleGroupStrategy.java b/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/SimpleGroupStrategy.java index 4e4ecfaf3..f3541bf34 100644 --- a/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/SimpleGroupStrategy.java +++ b/widgets/pgroup/org.eclipse.nebula.widgets.pgroup/src/org/eclipse/nebula/widgets/pgroup/SimpleGroupStrategy.java @@ -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;