if (selected) textColor = clrText = newTextClr; ignoreDrawSelected = true;
if (selected) { textColor = clrText = newTextClr; ignoreDrawSelected = true; }
LRESULT CDDS_ITEMPREPAINT (int wParam, int lParam) {	NMTVCUSTOMDRAW nmcd = new NMTVCUSTOMDRAW ();	OS.MoveMemory (nmcd, lParam, NMTVCUSTOMDRAW.sizeof);	/*	* Bug in Windows.  If the lParam field of TVITEM	* is changed during custom draw using TVM_SETITEM,	* the lItemlParam field of the NMTVCUSTOMDRAW struct	* is not updated until the next custom draw.  The	* fix is to query the field from the item instead	* of using the struct.	*/	int id = nmcd.lItemlParam;	if ((style & SWT.VIRTUAL) != 0) {		if (id == -1) {			TVITEM tvItem = new TVITEM ();			tvItem.mask = OS.TVIF_HANDLE | OS.TVIF_PARAM;			tvItem.hItem = nmcd.dwItemSpec;			OS.SendMessage (handle, OS.TVM_GETITEM, 0, tvItem);			id = tvItem.lParam;		}	}	TreeItem item = _getItem (nmcd.dwItemSpec, id);	/*	* Feature in Windows.  When a new tree item is inserted	* using TVM_INSERTITEM and the tree is using custom draw,	* a NM_CUSTOMDRAW is sent before TVM_INSERTITEM returns	* and before the item is added to the items array.  The	* fix is to check for null.	* 	* NOTE: This only happens on XP with the version 6.00 of	* COMCTL32.DLL,	*/	if (item == null) return null;	RECT clipRect = null;	int index = 0, count = 0;	if (hwndHeader != 0) {		index = OS.SendMessage (hwndHeader, OS.HDM_ORDERTOINDEX, 0, 0);		count = OS.SendMessage (hwndHeader, OS.HDM_GETITEMCOUNT, 0, 0);		if (count != 0) {			clipRect = new RECT ();			HDITEM hdItem = new HDITEM ();			hdItem.mask = OS.HDI_WIDTH;			OS.SendMessage (hwndHeader, OS.HDM_GETITEM, index, hdItem);			OS.SetRect (clipRect, nmcd.left, nmcd.top, nmcd.left + hdItem.cxy, nmcd.bottom);		}	}	int clrText = -1, clrTextBk = -1;	int hFont = item.cellFont != null ? item.cellFont [index] : -1;	if (hFont == -1) hFont = item.font;	if (OS.IsWindowEnabled (handle)) {		clrText = item.cellForeground != null ? item.cellForeground [index] : -1;		if (clrText == -1) clrText = item.foreground;		clrTextBk = item.cellBackground != null ? item.cellBackground [index] : -1;		if (clrTextBk == -1) clrTextBk = item.background;	}	int hDC = nmcd.hdc;	/*	* Feature in Windows.  When the mouse is pressed and the	* selection is first drawn for a tree, the previously	* selected item is redrawn but the the TVIS_SELECTED bits	* are not cleared.  When the user moves the mouse slightly	* and a drag and drop operation is not started, the item is	* drawn again and this time with TVIS_SELECTED is cleared.	* This means that an item that contains colored cells will	* not draw with the correct background until the mouse is	* moved.  The fix is to test for the selection colors and	* guess that the item is not selected.	* 	* NOTE: This code does not work when the foreground and	* background of the tree are set to the selection colors	* but this does not happen in a regular application.	*/	boolean selected = false;	if (OS.IsWindowEnabled (handle)) {		TVITEM tvItem = new TVITEM ();		tvItem.mask = OS.TVIF_HANDLE | OS.TVIF_STATE;		tvItem.hItem = item.handle;		OS.SendMessage (handle, OS.TVM_GETITEM, 0, tvItem);		if ((tvItem.state & (OS.TVIS_SELECTED | OS.TVIS_DROPHILITED)) != 0) {			selected = true;			if (handle == OS.GetFocus ()) {				if (OS.GetTextColor (hDC) != OS.GetSysColor (OS.COLOR_HIGHLIGHTTEXT)) {					selected = false;				} else {					if (OS.GetBkColor (hDC) != OS.GetSysColor (OS.COLOR_HIGHLIGHT)) {						selected = false;					}				}			}		}	}	if (OS.IsWindowVisible (handle) && nmcd.left < nmcd.right && nmcd.top < nmcd.bottom) {		if (linesVisible) {			RECT rect = new RECT ();			OS.SetRect (rect, nmcd.left, nmcd.top, nmcd.right, nmcd.bottom);			OS.DrawEdge (hDC, rect, OS.BDR_SUNKENINNER, OS.BF_BOTTOM);		}		//TODO - BUG - measure and erase sent when first column is clipped		if (hooks (SWT.MeasureItem)) {			RECT itemRect = item.getBounds (index, true, true, false, false, false, hDC);			int nSavedDC = OS.SaveDC (hDC);			GCData data = new GCData ();			data.device = display;			data.hFont = hFont;			GC gc = GC.win32_new (hDC, data);			Event event = new Event ();			event.item = item;			event.gc = gc;			event.index = index;			event.x = itemRect.left;			event.y = itemRect.top;			event.width = itemRect.right - itemRect.left;			event.height = itemRect.bottom - itemRect.top;			sendEvent (SWT.MeasureItem, event);			event.gc = null;			gc.dispose ();			OS.RestoreDC (hDC, nSavedDC);			if (isDisposed () || item.isDisposed ()) return null;			if (hwndHeader != 0) {				if (count == 0) {					if (event.x + event.width > scrollWidth) {						setScrollWidth (scrollWidth = event.x + event.width);					}				}			}			if (!ignoreItemHeight) {				if (event.height > getItemHeight ()) setItemHeight (event.height);				ignoreItemHeight = true;			}		}		textColor = -1;		ignoreDraw = ignoreDrawSelected = false;		if (hooks (SWT.EraseItem)) {			RECT cellRect = item.getBounds (index, true, true, true, true, true, hDC);			int nSavedDC = OS.SaveDC (hDC);			GCData data = new GCData ();			data.device = display;			data.foreground = clrText != -1 ? clrText : OS.GetTextColor (hDC);			data.background = clrTextBk != -1 ? clrTextBk : OS.GetBkColor (hDC);			data.hPen = OS.CreatePen (OS.PS_SOLID, 0, data.foreground);			data.hBrush = OS.CreateSolidBrush (data.background);			if (hFont != -1) data.hFont = hFont;			OS.SelectObject (hDC, data.hPen);			OS.SelectObject (hDC, data.hBrush);			GC gc = GC.win32_new (hDC, data);			Event event = new Event ();			event.index = index;			event.item = item;			event.gc = gc;			if (selected) event.detail |= SWT.SELECTED;			if ((nmcd.uItemState & OS.CDIS_FOCUS) != 0) event.detail |= SWT.FOCUSED;			event.x = cellRect.left;			event.y = cellRect.top;			event.width = cellRect.right - cellRect.left;			event.height = cellRect.bottom - cellRect.top;			gc.setClipping (event.x, event.y, event.width, event.height);			sendEvent (SWT.EraseItem, event);			event.gc = null;			int newTextClr = OS.GetTextColor (hDC);			gc.dispose ();			OS.RestoreDC (hDC, nSavedDC);			if (isDisposed () || item.isDisposed ()) return null;			ignoreDraw = !event.doit;			if (event.doit) {				if ((event.detail & SWT.SELECTED) != 0) {					if (!selected) {						textColor = clrText = OS.GetSysColor (OS.COLOR_HIGHLIGHTTEXT);					}					/*					* Feature in Windows.  When the tree has the style					* TVS_FULLROWSELECT, the background color for the					* entire row is filled when an item is painted,					* drawing on top of any custom drawing.  The fix					* is to emulate TVS_FULLROWSELECT.					*/					if ((style & SWT.FULL_SELECTION) != 0) {						RECT rect = new RECT ();						OS.SetRect (rect, nmcd.left, nmcd.top, nmcd.right, nmcd.bottom);						fillBackground (hDC, OS.GetBkColor (hDC), rect);					} else {						RECT textRect = item.getBounds (index, true, false, false, false, true, hDC);						drawBackground (hDC, textRect, OS.GetBkColor (hDC));					}				} else {					if (selected) textColor = clrText = newTextClr;					ignoreDrawSelected = true;				}			}			RECT itemRect = item.getBounds (index, true, true, false, false, false, hDC);			OS.SaveDC (hDC);			OS.SelectClipRgn (hDC, 0);			//TODO - bug in Windows selection or SWT itemRect			/*if (selected)*/ itemRect.right++;			if (linesVisible) itemRect.bottom++;			if (clipRect != null) {				OS.IntersectClipRect (hDC, clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);			}			OS.ExcludeClipRect (hDC, itemRect.left, itemRect.top, itemRect.right, itemRect.bottom);			return new LRESULT (OS.CDRF_DODEFAULT | OS.CDRF_NOTIFYPOSTPAINT);		}		/*		* Feature in Windows.  When the tree has the style		* TVS_FULLROWSELECT, the background color for the		* entire row is filled when an item is painted,		* drawing on top of any custom drawing.  The fix		* is to emulate TVS_FULLROWSELECT.		*/		if (selected) {			if ((style & SWT.FULL_SELECTION) != 0) {				int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);				if ((bits & OS.TVS_FULLROWSELECT) == 0) {					RECT rect = new RECT ();					OS.SetRect (rect, nmcd.left, nmcd.top, nmcd.right, nmcd.bottom);					fillBackground (hDC, OS.GetBkColor (hDC), rect);				}			}		}	}	if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {		if (sortColumn != null && sortDirection != SWT.NONE) {			if (findImageControl () == null) {				if (indexOf (sortColumn) == 0) clrTextBk = getSortColumnPixel ();			}		}	}	LRESULT result = null;	if (clrText == -1 && clrTextBk == -1 && hFont == -1) {		result = new LRESULT (OS.CDRF_DODEFAULT | OS.CDRF_NOTIFYPOSTPAINT);		} else {		result = new LRESULT (OS.CDRF_NEWFONT | OS.CDRF_NOTIFYPOSTPAINT);		if (hFont != -1) OS.SelectObject (hDC, hFont);		if (OS.IsWindowEnabled (handle) && OS.IsWindowVisible (handle)) {			/*			* Feature in Windows.  Windows does not fill the entire cell			* with the background color when TVS_FULLROWSELECT is not set.			* The fix is to fill the cell with the background color.			*/			if (clrTextBk != -1) {				int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);				if ((bits & OS.TVS_FULLROWSELECT) == 0) {					if (count != 0 && hwndHeader != 0) {						RECT rect = new RECT ();						HDITEM hdItem = new HDITEM ();						hdItem.mask = OS.HDI_WIDTH;						OS.SendMessage (hwndHeader, OS.HDM_GETITEM, index, hdItem);						OS.SetRect (rect, nmcd.left, nmcd.top, nmcd.left + hdItem.cxy, nmcd.bottom);						if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) {							RECT itemRect = new RECT ();							itemRect.left = item.handle;							if (OS.SendMessage (handle, OS.TVM_GETITEMRECT, 1, itemRect) != 0) {								rect.left = Math.min (itemRect.left, rect.right);							}						}						if ((style & SWT.FULL_SELECTION) != 0) {							if (!selected) fillBackground (hDC, clrTextBk, rect);						} else {							fillBackground (hDC, clrTextBk, rect);						}					} else {						if ((style & SWT.FULL_SELECTION) != 0) {							RECT rect = new RECT ();							OS.SetRect (rect, nmcd.left, nmcd.top, nmcd.right, nmcd.bottom);							if (!selected) fillBackground (hDC, clrTextBk, rect);						}					}				}			}			if (!selected) {				nmcd.clrText = clrText == -1 ? getForegroundPixel () : clrText;				nmcd.clrTextBk = clrTextBk == -1 ? getBackgroundPixel () : clrTextBk;				OS.MoveMemory (lParam, nmcd, NMTVCUSTOMDRAW.sizeof);			}		}	}	OS.SaveDC (hDC);	if (clipRect != null) {		int hRgn = OS.CreateRectRgn (clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);		OS.SelectClipRgn (hDC, hRgn);		OS.DeleteObject (hRgn);	}	return result;}