Project: SWT
Bug 207052 Tree Listening to MeasureItem returns incorrect Client Area
Reported: 2007-10-22 09:50:29 | Status: resolved fixed

Build ID: M20070921-1145 Steps To Reproduce: Run the following snippet: public class IncorrectMeasureItem { public static void main (String [] args) { final Display display = new Display (); Shell shell = new Shell (display); shell.setLayout (new FillLayout (SWT.VERTICAL)); final Tree tree = new Tree (shell, SWT.BORDER); final Tree tree2 = new Tree (shell, SWT.BORDER); final TreeItem item = new TreeItem(tree, SWT.RIGHT); final TreeItem item2 = new TreeItem(tree2, SWT.RIGHT); for (int i = 0; i < 4; ++i) { TreeItem newItem = new TreeItem(tree, SWT.None); newItem.setText("Item" + i); newItem = new TreeItem(tree2, SWT.None); newItem.setText("Item" + i); } shell.addListener(SWT.Resize, new Listener() { public void handleEvent(Event event) { item.setText(tree.getClientArea().toString()); item2.setText(tree2.getClientArea().toString()); }}); final Listener measureItemListener = new Listener() { public void handleEvent(Event event) { //do nothing }}; final Button button = new Button(shell, SWT.CHECK); button.setText("MeasureItem Listener"); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (button.getSelection()) { tree.addListener(SWT.MeasureItem, measureItemListener); } else { tree.removeListener(SWT.MeasureItem, measureItemListener); } }}); shell.pack (); shell.open (); while (!shell.isDisposed()) { if (!display.readAndDispatch ()) display.sleep (); } display.dispose (); } } Resize the window horizontally and vertically, notice that they return the same client area. Click the checkbox to add a MeasureItem listener to the first tree. Resize the window larger vertically to notice that the trees return different width. (Noteably, tree1's width is tree2's width + 17). resize the window smaller vertically and note that when then vertical scroll bar appears, the width returned is equal for the two trees. Note that removing the listener by unchecking the checkbox does not remove the symptoms. To make this effect more drastic, install a TreeEditor on the items like snippet111. The changes below is setting editor.grabHorizontal = true, text = new Text(composite, SWT.RIGHT), and removing the text size calculation (since grabHorizontal will do it). Insert this snippet before the shell.pack(). final Color black = display.getSystemColor (SWT.COLOR_BLACK); final TreeItem [] lastItem = new TreeItem [1]; final TreeEditor editor = new TreeEditor (tree); editor.grabHorizontal = true; tree.addListener (SWT.Selection, new Listener () { public void handleEvent (Event event) { final TreeItem item = (TreeItem) event.item; if (item != null && item == lastItem [0]) { boolean showBorder = true; final Composite composite = new Composite (tree, SWT.NONE); if (showBorder) composite.setBackground (black); final Text text = new Text (composite, SWT.RIGHT); final int inset = showBorder ? 1 : 0; composite.addListener (SWT.Resize, new Listener () { public void handleEvent (Event e) { Rectangle rect = composite.getClientArea (); text.setBounds (rect.x + inset, rect.y + inset, rect.width - inset * 2, rect.height - inset * 2); } }); Listener textListener = new Listener () { public void handleEvent (final Event e) { switch (e.type) { case SWT.FocusOut: item.setText (text.getText ()); composite.dispose (); break; case SWT.Traverse: switch (e.detail) { case SWT.TRAVERSE_RETURN: item.setText (text.getText ()); //FALL THROUGH case SWT.TRAVERSE_ESCAPE: composite.dispose (); e.doit = false; } break; } } }; text.addListener (SWT.FocusOut, textListener); text.addListener (SWT.Traverse, textListener); editor.setEditor (composite, item); text.setText (item.getText ()); text.selectAll (); text.setFocus (); } lastItem [0] = item; } }); More information: The difference of 17 may be related to the width of a scroll bar, or could just be coincidental.

Files changed in fix:
bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java