Bug 487039

Summary: StyledText with auto scroll does not redraw scollbars correctly
Product: [Eclipse Project] Platform Reporter: RĂ¼diger Herrmann <ruediger.herrmann>
Component: SWTAssignee: Platform-SWT-Inbox <platform-swt-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: info, Lars.Vogel, ruediger.herrmann
Version: 4.5.1Keywords: helpwanted
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Attachments:
Description Flags
Screenshot none

Description RĂ¼diger Herrmann CLA 2016-02-02 13:28:14 EST
Created attachment 259497 [details]
Screenshot

I use a StyledText that is 'configured' to automatically show or hide scrollbars as necessary. A Resize and a Modify listener is used to show/hide the scrollbars.

When the StyledText is shown for the first time the area in which usually the vertical scrollbar is located in appears black (see the attached screenshot).

The problem can be reproduced with this snippet:

public class StyledTextAutoScrollBug {

  public static void main( String[] args ) {
    Display display = new Display();
    Shell shell = new Shell( display );
    shell.setLayout( new FillLayout() );
    int style = SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL;
    StyledText textWidget = new StyledText( shell, style );
  
    new TextFieldAutoScrollBar( textWidget ).install();
    
    shell.setSize( 300, 250 );
    shell.open();
    while( !shell.isDisposed() ) {
      if( !display.readAndDispatch() ) {
        display.sleep();
      }
    }
    display.dispose();
  }
  
  static class TextFieldAutoScrollBar {
    private final StyledText text;

    TextFieldAutoScrollBar( StyledText text ) {
      this.text = text;
    }

    void install() {
      Listener scrollBarListener = new ScrollBarListener();
      text.addListener( SWT.Resize, scrollBarListener );
      text.addListener( SWT.Modify, scrollBarListener );
      updateScrollBars();
//      relayout();
    }

    private void updateScrollBars() {
      Rectangle trim = computeTrim();
      Point size = computeSize();
      text.getHorizontalBar().setVisible( trim.width <= size.x );
      text.getVerticalBar().setVisible( trim.height <= size.y );
    }

    private void relayout() {
      text.getParent().layout( true );
    }

    private Rectangle computeTrim() {
      Rectangle clientArea = text.getClientArea();
      return text.computeTrim( clientArea.x, clientArea.y, clientArea.width, clientArea.height );
    }

    private Point computeSize() {
      return text.computeSize( SWT.DEFAULT, SWT.DEFAULT, true );
    }

    private class ScrollBarListener implements Listener {
      @Override
      public void handleEvent( Event event ) {
        text.getParent().setRedraw( false );
        try {
          updateScrollBars();
          if( event.type == SWT.Modify ) {
            relayout();
            text.showSelection();
          }
        } finally {
          text.getParent().setRedraw( true );
        }
      }
    }
  }

}


If a re-layout of the parent is enforced like this
  styldText.getParent().layout( true );
the problem disappears. In the snippet above, disable/enable the call to layout() in install() .