Bug 265201 - Table item background color bug in Vista
Summary: Table item background color bug in Vista
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.5   Edit
Hardware: PC Windows Vista
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Steve Northover CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-02-17 14:43 EST by Bruno Medeiros CLA
Modified: 2019-09-06 16:09 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Bruno Medeiros CLA 2009-02-17 14:43:37 EST
The snippet pasted below showcases a bug with setting a table item background color in Vista: The drawn background will not fill the entire cell, but will be missing about 2 pixels of width to the left.
If the cell background is drawn using a custom EraseItem listener, it will be drawn fine. If a Tree is used instead of a Table, it will also work fine.

---- ----

package scratch.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class Table_EraseItem_VistaIssue {
	
	protected static Display display;
	protected static Shell shell;
	protected static Table table;
	
	public static void main(String[] args) {
		display = new Display();
		shell = new Shell(display);
		shell.setLayout(new FillLayout());
		run();
		
		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
	
	
	public static void run() {
		table = new Table(shell, SWT.FULL_SELECTION | SWT.MULTI);
		new TableColumn(table, SWT.NONE).setWidth(100);
		new TableColumn(table, SWT.NONE).setWidth(100);
		new TableColumn(table, SWT.NONE).setWidth(100);
		
		table.addListener(SWT.EraseItem, new Listener() {
			public void handleEvent(Event event) {
				doErase(event);
			}
		});

		for (int i = 0; i < 6; i++) {
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(0, "Foo " + i);
			item.setText(1, "Row " + i + " (BLAH) ");
			item.setText(2, "Foo " + i);
			if(i == 2 || i == 4) {
				item.setBackground(display.getSystemColor(SWT.COLOR_MAGENTA));
			}
		}
	}
	
	private static void doErase(Event event) {
		int rowIx = table.indexOf((TableItem) event.item);

		TableItem item = (TableItem) event.item;
		GC gc = event.gc;

		if(rowIx == 2) {
			Color background = item.getBackground(event.index);
			gc.setBackground(background);
			gc.fillRectangle(event.x, event.y, event.width, event.height);
//			event.detail &= ~SWT.BACKGROUND;
		}
	}
}
Comment 1 Felipe Heidrich CLA 2009-02-17 17:18:46 EST
You are drawing using event.x, event.y, event.width, event.height
there is nothing in the API saying this is the area where the OS draws the background color, or selection, or text, etc. This is the area of the item. 

To be consistent, you have to draw all tha background yourself (using whatever spacing you like for margin), or let the OS draw all the background.
Comment 2 Bruno Medeiros CLA 2009-02-18 06:49:29 EST
(In reply to comment #1)
> You are drawing using event.x, event.y, event.width, event.height
> there is nothing in the API saying this is the area where the OS draws the
> background color, or selection, or text, etc. This is the area of the item. 
> 

If the background area of a widget/item without trimmings or border is not the entire area of the item, then what is it? I've never heard of it being anything else than that.

If I draw the background using the same method for all cells, it will look consistent only in the sense that all cells will look the same on that particular table. But it will still be wrong in the case where I let the OS draw all the backgrounds, because a ghost margin will appear than does not appear in other platforms, or even in Vista but using a Tree instead of a Table.
Comment 3 Steve Northover CLA 2009-02-18 09:55:12 EST
Felipe, do you understand what the "ghost margin" is?  Is it the "2 pixels of width to the left".  Please investigate.
Comment 4 Felipe Heidrich CLA 2009-02-18 10:32:41 EST
when the OS draws the row background it doesn't start at x==0 (in my machine it is actually x==4). This is consisntent with the selection rectangle and the focus rectangle. I believe these 4 pixels on the left is what Bruno refers to as the ghost margin.

When he draws the background using custom draw he uses event.x which is zero. This causes his custom row background and the normal row background to be different.

Bruno, if you change your code to use
gc.fillRectangle(item.getBounds()); then you are okay.
Comment 5 Bruno Medeiros CLA 2009-02-18 13:00:26 EST
Oops, I mistyped, I actually meant "2 pixels of width to the right" on my first post.

(In reply to comment #4)
> when the OS draws the row background it doesn't start at x==0 (in my machine it
> is actually x==4). This is consisntent with the selection rectangle and the
> focus rectangle. I believe these 4 pixels on the left is what Bruno refers to
> as the ghost margin.
> 

I know exactly what you mean, but that's not what I'm referring to in this issue. Those 4 pixels of margin appear on Windows XP as well as Vista, appear on both Table and Tree widgets, and appear only on cells on the first column.

My ghost margin is 2 pixels, appears on any cell of any column, and on the *right* as I corrected. If you run the snippet, you should see it right away. Row 2 will appear just as expected (full background), but row 4 (where the OS draws the background) will have 2 pixels of width missing on each cell.




Comment 6 Felipe Heidrich CLA 2009-02-18 14:35:02 EST
I see, it only happens on vista.

Steve, maybe this is well-know problem, please advise.
Comment 7 Mike Wilson CLA 2009-05-05 13:33:35 EDT
Changing Version tag to something more believable.
Comment 8 Eclipse Webmaster CLA 2019-09-06 16:09:36 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.