简体   繁体   English

计算Composite的最小大小

[英]Compute the minimum size of a Composite

I need to calculate the minimum or default size of a Composite where it can display all components without clipping. 我需要计算一个Composite的最小或默认大小,它可以显示所有组件而不会剪切。

I only seem to find methods to calculate the preferred size of the Composite . 我似乎只找到计算Composite首选大小的方法。 This means that a Table or other scrolled composite will have a preferred size which displays the full contents without scrolling. 这意味着Table或其他滚动复合将具有首选大小,其显示完整内容而不滚动。 The ScrolledComposite will immediately go into scroll mode, which is not what I want. ScrolledComposite将立即进入滚动模式,这不是我想要的。

GridLayout manages to do this by treating the GridData hints as minimum width/height, allowing grabbing any extra space available. GridLayout通过将GridData提示视为最小宽度/高度来设法实现此目的,允许抓取任何可用的额外空间。

The problem is related to this one: SWT - computingSize for Multi-line textfield inside ScrolledComposite 问题与此有关: SWT - 用于ScrolledComposite内的多行文本字段的computingSize

Control#computeSize(int, int) should be what you are searching for: Control#computeSize(int, int)应该是您要搜索的内容:

Point size = comp.computeSize(SWT.DEFAULT, SWT.DEFAULT);
System.out.println(size.x + " " + size.y);

I've managed to find the solution. 我设法找到了解决方案。

The key was two different things: 关键是两件事:

  1. Make sure to set the resize listener on both the content (if CHILDREN are added and layout() is called) and the ScrolledComposite (if it is resized from outside its children) 确保在内容上设置调整大小侦听器(如果添加了CHILDREN并调用了layout())和ScrolledComposite (如果从子ScrolledComposite外部调整大小)
  2. Make sure to set both GridData.grab and GridData.hint . 确保设置GridData.grabGridData.hint The hint will make sure the composite assumes this size when you do computeSize() , while grab makes sure it will grab any extra space that is available. 当你执行computeSize() ,提示将确保复合假定这个大小,而抓取确保它将获取任何可用的额外空间。

Code sample is below: 代码示例如下:

public static void main (String [] args) {
  Display display = new Display ();
  Shell shell = new Shell(display);
  ScrolledComposite sc = new ScrolledComposite(shell, SWT.NONE);
  Composite foo = new Composite(sc, SWT.NONE);
  foo.setLayout(new GridLayout(1, false));
  StyledText text = new StyledText(foo, SWT.NONE);
  text.setText("Ipsum dolor etc... \n etc... \n etc....");
  GridDataFactory.fillDefaults().grab(true, true).hint(40, 40).applyTo(text);

  Listener l = new Listener() {
     public void handleEvent(Event e) {
         Point size = sc.getSize();
         Point cUnrestrainedSize = content.computeSize(SWT.DEFAULT, SWT.DEFAULT);
         if(size.y >= cUnrestrainedSize.y && size.x >= cUnrestrainedSize.x) {
           content.setSize(size);
           return;
         }
         // does not fit
         Rectangle hostRect = getBounds();
         int border = getBorderWidth();
         hostRect.width -= 2*border;
         hostRect.width -= getVerticalBar().getSize().x;
         hostRect.height -= 2*border;
         hostRect.height -= getHorizontalBar().getSize().y;
         c.setSize(
           Math.max(cUnrestrainedSize.x, hostRect.width),
           Math.max(cUnrestrainedSize.y, hostRect.height)
         );
     }
  }
  sc.addListener(SWT.Resize, l);
  foo.addListener(SWT.Resize, l);

  shell.open ();
  while (!shell.isDisposed ()) {
    if (!display.readAndDispatch ()) display.sleep ();
  }
  display.dispose ();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM