Project: SWT
Bug 130375 Background Inheritance Incorrect in Composite Hierarchies
Reported: 2006-03-03 14:08:24 | Status: resolved fixed

Opened by Brian Farn 2006.03.03 Eclipse Version: 3.2.0 M5a, Build id: I20060223-1656 Description ----------- A top composite with an image background has a background mode of INHERIT_DEFAULT. A label parented by the top composite correctly inherits the top composite's background. The top composite has a child composite with a background mode of INHERIT_DEFAULT. A label parented by the child composite inherits the top composite's background, but seems to inherit an incorrect region of the top composite background (possible pixel mapping error). Sample UI Hierarchy ------------------- Shell Composite: Has a background image, background mode is INHERIT_DEFAULT Label: Correctly displays background of parent composite Text: Displayed correctly Composite: Background mode is INHERIT_DEFAULT, correctly displays background of parent composite Label: Incorrectly displays background of top composite Text: Displayed correctly Sample code ----------- package tc; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class TransparentMain { protected TransparentCompositeBackground _transparentBackground = null; public static void main( String[] args ) { new TransparentMain(); } public TransparentMain(){ Shell _shell = new Shell(); _shell.setSize( 500, 250 ); _shell.setText( "Transparent Main" ); FillLayout layout = new FillLayout(); _shell.setLayout( layout ); _transparentBackground = new TransparentCompositeBackground( _shell ); Display display = Display.getDefault(); _shell.setLocation( 300, 300 ); _shell.open(); while( _shell.isDisposed() == false ){ if( display.readAndDispatch() == false ) display.sleep(); } } public class TransparentCompositeBackground extends Canvas { protected ImageData _imageData = null; /** * */ public TransparentCompositeBackground( Composite compositeParent) { super( compositeParent, SWT.NULL ); this.setBackgroundMode( SWT.INHERIT_DEFAULT ); Image image = createImage(); this.setBackgroundImage( image ); GridLayout layout = new GridLayout( 1, false ); setLayout( layout ); Label label = new Label( this, SWT.NULL ); GridData gridData = new GridData( GridData.FILL_HORIZONTAL ); label.setLayoutData( gridData ); label.setText( "A label and text on a composite" ); Text text = new Text( this, SWT.BORDER ); gridData = new GridData( GridData.FILL_HORIZONTAL ); text.setLayoutData( gridData ); Composite composite1 = new Composite( this, SWT.BORDER ); composite1.setBackgroundMode( SWT.INHERIT_DEFAULT ); gridData = new GridData( GridData.FILL_HORIZONTAL ); composite1.setLayoutData( gridData ); layout = new GridLayout( 1, false ); layout.marginHeight = 20; layout.marginWidth = 20; composite1.setLayout( layout ); Label label2 = new Label( composite1, SWT.NULL ); gridData = new GridData( GridData.FILL_HORIZONTAL ); label2.setLayoutData( gridData ); label2.setText( "A label and text on a sub-composite" ); Text text2 = new Text( composite1, SWT.BORDER ); gridData = new GridData( GridData.FILL_HORIZONTAL ); text2.setLayoutData( gridData ); } public Image createImage(){ Image image = null; GC gc = null; Rectangle rect = getBounds(); if( rect.isEmpty() ){ rect.width = 400; rect.height = 400; } image = new Image( Display.getCurrent(), rect.width, rect.height ); gc = new GC( image ); _imageData = image.getImageData(); paintImageData( rect ); if( image != null ) image.dispose(); image = new Image( Display.getCurrent(), _imageData ); if( gc != null ) gc.dispose(); return image; } public void paintImageData( Rectangle rect ){ for( int i=0; i<rect.width; ++i ){ for( int j=0; j<rect.height; ++j ){ int r = (255 - ( i % 255 )) << 8; int g = (255 - ( j % 255 )) << 16; int b = ((i + j)%255) << 24; _imageData.setPixel( i, j, r + g + b ); } } } } }

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