ncgutter.cpp

来自「管理项目进度工具的原代码」· C++ 代码 · 共 2,008 行 · 第 1/3 页

CPP
2,008 行
字号

	// to avoid creating excessively large bitmaps we'll render the client header straight
	// into the window dc ie only the non-client gutter & header is rendered on the bitmap
	CWindowDC dc(GetCWnd());
	CDC dcMem;
	CDC* pOutputDC = &dc; // default backup plan in case mem dc or bitmap cannot be created
	CBitmap* pBMOld = NULL;

//	static int nCount = 0;
//	TRACE ("CNcGutter::OnNcPaint(%d)\n", ++nCount);

	if (GetGutterWidth() > 0 && dcMem.CreateCompatibleDC(NULL) && 
		PrepareBitmap(&dc, &m_bmNonClient, rGutter, FALSE))
	{		
		pOutputDC = &dcMem;
		pBMOld = dcMem.SelectObject(&m_bmNonClient);
	}

	pOutputDC->FillSolidRect(rGutter, ::GetSysColor(COLOR_WINDOW));

	// iterate the top level items
	CFont* pOldFont = PrepareFont(pOutputDC, FALSE);
				
	int nItem = 1;
	CRect rItem;
	DWORD dwItem = GetFirstVisibleTopLevelItem(nItem);
				
	while (dwItem)
	{
		CString sPrevPos;

		NcDrawItem(pOutputDC, dwItem, 0, 0, nItem, rGutter, rItem, TRUE);
					
		dwItem = GetNextItem(dwItem);
		nItem++;
					
		if (rItem.bottom >= rGutter.bottom)
			break;
	}
			
	// see if anyone wants to do any final drawing
	CRect rItems(rGutter);
	rItems.bottom = min(rGutter.bottom, rItem.bottom);

	PostNcDraw(pOutputDC, rItems);

	// cleanup			
	pOutputDC->SelectObject(pOldFont);

	// non-client header
	if (HasStyle(NCGS_SHOWHEADER))
	{
		GetHeaderRect(rHeader, GHR_NONCLIENT, FALSE);
		NcDrawHeader(pOutputDC, rHeader, NONCLIENT, &ptCursor);
	}

	// blt to window dc if nec
	if (pOutputDC == &dcMem)
	{
		dc.BitBlt(rGutter.left, rGutter.top, rGutter.Width(), rGutter.Height(),
				  &dcMem, rGutter.left, rGutter.top, SRCCOPY);

		dcMem.SelectObject(pBMOld); // V.V.IMPORTANT
	}

	// two items we render direct to the window dc
	
	// 1. the client header
	if (HasStyle(NCGS_SHOWHEADER))
	{
		CFont* pOldFont = PrepareFont(&dc, FALSE);

		GetHeaderRect(rHeader, GHR_CLIENT, FALSE);
		NcDrawHeader(&dc, rHeader, CLIENT, &ptCursor);

		// cleanup			
		dc.SelectObject(pOldFont);
	}
				
	// 2. if the window base does not match the client bottom then 
	// paint the extra bit gray (means there is a horz scrollbar)
	if (rGutter.bottom != rClient.bottom)
	{
		rGutter.top = rClient.bottom;

		// allow for vert scrollbar when columns are on right
		if (HasStyle(NCGS_RIGHTCOLUMNS) && (GetStyle() & WS_VSCROLL))
			rGutter.left -= ::GetSystemMetrics(SM_CXVSCROLL);

		if (CThemed().IsNonClientThemed())
		{
			CThemed th;

			if (th.Open(GetCWnd(), "SCROLLBAR"))
				th.DrawBackground(&dc, SBP_LOWERTRACKHORZ, SCRBS_NORMAL, rGutter);
			else
				dc.FillSolidRect(rGutter, ::GetSysColor(COLOR_SCROLLBAR));
		}
		else
			dc.FillSolidRect(rGutter, ::GetSysColor(COLOR_SCROLLBAR));
	}
}

void CNcGutter::NcDrawHeaderColumn(CDC* pDC, int nColumn, CRect rColumn, CThemed* pTheme, LPPOINT pCursor)
{
	COLUMNDESC* pCD = m_aColumns[nColumn];

	BOOL bDown = (nColumn == m_nHeaderColDown);
	BOOL bPressed = (pCD->bPressed || bDown);
	BOOL bSorted = (pCD->nSortDir != NCGSORT_NONE);
	
	if (!pTheme)
	{
		if (bPressed)
		{
			pDC->FillSolidRect(rColumn, ::GetSysColor(COLOR_3DHIGHLIGHT));
			pDC->Draw3dRect(rColumn, ::GetSysColor(COLOR_3DFACE), ::GetSysColor(COLOR_3DSHADOW));
		}
		else
			pDC->Draw3dRect(rColumn, ::GetSysColor(COLOR_3DHIGHLIGHT), ::GetSysColor(COLOR_3DSHADOW));
	}
	else
	{
		BOOL bHot = pCursor ? rColumn.PtInRect(*pCursor) : FALSE;
		BOOL bClickable = (bHot && pCD->bClickable);

		if (!bPressed)
			rColumn.right++;
		
		pTheme->DrawBackground(pDC, HP_HEADERITEM, bPressed ? HIS_PRESSED : 
								(bClickable ? HIS_HOT : HIS_NORMAL), rColumn);
	}
	
	// text
	if (!pCD->sTitle.IsEmpty())
	{
		const UINT DEFFLAGS = DT_END_ELLIPSIS | DT_BOTTOM | DT_SINGLELINE;

		CRect rText(rColumn);

		rText.bottom -= (pCD->bSymbolFont ? 1 : 2) + (pTheme ? 1 : 0);

		switch (pCD->nTextAlign)
		{
		case DT_LEFT:
			rText.left += NCG_COLPADDING;
			break;
			
		case DT_RIGHT:
			rText.right -= NCG_COLPADDING;
			break;
		}
		
		if (bDown)
			rText.OffsetRect(1, 1);
		
		if (bSorted)
			rText.right -= (SORTWIDTH + 2);

		if (pCD->hFont)
		{
			CFont* pOldFont = PrepareFont(pDC, TRUE, pCD->hFont);
			pDC->DrawText(pCD->sTitle, rText, DEFFLAGS | pCD->nTextAlign);
			pDC->SelectObject(pOldFont);
		}
		else
			pDC->DrawText(pCD->sTitle, rText, DEFFLAGS | pCD->nTextAlign);

		// adjust for sort arrow
		if (bSorted)
		{
			if (pCD->nTextAlign & DT_CENTER)
				rColumn.left = ((rText.left + rText.right + pDC->GetTextExtent(pCD->sTitle).cx) / 2) + 2;

			else if (pCD->nTextAlign & DT_RIGHT)
				rColumn.left = rText.right + 2;
			else
				rColumn.left = rText.left + pDC->GetTextExtent(pCD->sTitle).cx + 2;
		}
	}

	// draw sort direction if required
	if (bSorted)
	{
		int nMidY = (bDown ? 1 : 0) + (rColumn.top + rColumn.bottom) / 2;
		POINT ptArrow[3] = { { 0, 0 }, { 3, (int)pCD->nSortDir * 3 }, { 7, -(int)pCD->nSortDir } };
		
		// translate the arrow to the appropriate location
		int nPoint = 3;
		
		while (nPoint--)
		{
			ptArrow[nPoint].x += rColumn.left + 3 + (bDown ? 1 : 0);
			ptArrow[nPoint].y += nMidY;
		}
		pDC->Polyline(ptArrow, 3);
	}
	
}

void CNcGutter::NcDrawHeader(CDC* pDC, const CRect& rHeader, HCHDRPART nPart, const LPPOINT pCursor)
{
	if (!HasStyle(NCGS_SHOWHEADER))
		return;

	CThemed th;
	BOOL bThemed = th.AreControlsThemed() && th.Open(GetCWnd(), "HEADER");

	if (!bThemed)
		pDC->FillSolidRect(rHeader, ::GetSysColor(COLOR_3DFACE));

	pDC->SetBkMode(TRANSPARENT);

	CFont* pOldFont = PrepareFont(pDC, TRUE); // default header font

	CRect rColumn(rHeader);

	if (nPart == NONCLIENT)
	{
		int nNumCols = m_aColumns.GetSize() - 1; // omit last column == client
		rColumn.right = rColumn.left;

		for (int nCol = 0; nCol < nNumCols; nCol++)
		{
			COLUMNDESC* pCD = m_aColumns[nCol];

			if (!pCD->nWidth)
				continue;

			rColumn.left = rColumn.right;
			rColumn.right = min(rHeader.right, rColumn.left + (int)pCD->nWidth);

			NcDrawHeaderColumn(pDC, nCol, rColumn, bThemed ? &th : NULL, bThemed ? pCursor : NULL);

			// stop if we've gone passed the end of the available space
			if (rColumn.right >= rHeader.right)
				break;
		}
	}
	else // the rest is over the client area
	{
		int nCol = GetColumnIndex(NCG_CLIENTCOLUMNID);
		ASSERT (nCol != -1);

		NcDrawHeaderColumn(pDC, nCol, rColumn, bThemed ? &th : NULL, bThemed ? pCursor : NULL);
	}

	// cleanup
	pDC->SelectObject(pOldFont);
}

void CNcGutter::OnPrint(HDC hdc, UINT& nFlags)
{
	if (nFlags & PRF_CLIENT)
	{
		// render the control to a bitmap for flicker free
		CRect rClient;
		GetClientRect(rClient);
		rClient.InflateRect(1, 1);

		CDC dcMem, dc;

		if (dcMem.CreateCompatibleDC(NULL))
		{
			dc.Attach(hdc);

			if (PrepareBitmap(&dc, &m_bmClient, rClient, TRUE))
			{
				CBitmap* pBMOld = dcMem.SelectObject(&m_bmClient);
				dcMem.FillSolidRect(rClient, GetSysColor(COLOR_WINDOW));

				CSubclassWnd::WindowProc(GetHwnd(), WM_PRINT, (WPARAM)(HDC)dcMem, PRF_CLIENT);

				dc.BitBlt(0, 0, rClient.right, rClient.bottom, &dcMem, 0, 0, SRCCOPY);

				nFlags &= ~PRF_CLIENT; // we just drawn this bit

				// cleanup
				dcMem.SelectObject(pBMOld); // V.V.IMPORTANT
			}

			dc.Detach();
		}
	}
}

DWORD CNcGutter::GetFirstVisibleTopLevelItem(int& nItem) const
{
	// try hook window then parent
	UINT nID = GetDlgCtrlID();

	DWORD dwItem = (DWORD)::SendMessage(GetParent(), WM_NCG_GETFIRSTVISIBLETOPLEVELITEM, nID, (LPARAM)(LPINT)&nItem);

	if (!dwItem)
		dwItem = (DWORD)SendMessage(WM_NCG_GETFIRSTVISIBLETOPLEVELITEM, nID, (LPARAM)(LPINT)&nItem);

	// else
	return dwItem;
}

void CNcGutter::NcDrawItem(CDC* pDC, DWORD dwItem, DWORD dwParentItem, int nLevel, int nPos, 
						   const CRect& rGutter, CRect& rItem /* out */, BOOL bDrawChildren)
{
	rItem = GetWindowItemRect(dwItem);
							
	if (!rItem.IsRectEmpty())
	{
		if (!bDrawChildren)
		{
			if (HasStyle(NCGS_RIGHTCOLUMNS))
				rItem.left = rGutter.left;
			else
				rItem.right = rGutter.right;

			pDC->FillSolidRect(rItem, GetSysColor(COLOR_WINDOW));
		}

		NCGDRAWITEM ncgDI;
		
		ncgDI.pDC = pDC;
		ncgDI.dwItem = dwItem;
		ncgDI.dwParentItem = dwParentItem;
		ncgDI.nLevel = nLevel;
		ncgDI.nItemPos = nPos;
		ncgDI.rWindow = &rGutter;
		ncgDI.rItem = &rItem;
		ncgDI.bSelected = IsItemSelected(dwItem);
		
		UINT nID = GetDlgCtrlID();
		
		// draw each column in turn
		rItem.right = rItem.left;
		int nNumCols = m_aColumns.GetSize() - 1; // omit last column == client
		
		for (int nCol = 0; nCol < nNumCols; nCol++)
		{
			const COLUMNDESC* pCD = m_aColumns[nCol];

			ncgDI.nColID = pCD->nColID;
			ncgDI.nTextAlign = pCD->nTextAlign;

			rItem.left = rItem.right;
			
			// if we're out of the window rect then stop
			if (rItem.left > rGutter.right)
				break;
			
			// don't bother with zero width columns
			int nColWidth = (int)pCD->nWidth;
			
			if (!nColWidth)
				continue;
			
			rItem.right += nColWidth;
			int nSaveDC = pDC->SaveDC();
			
			// try parent then hook window
			if (!::SendMessage(GetParent(), WM_NCG_DRAWITEM, nID, (LPARAM)&ncgDI))
				SendMessage(WM_NCG_DRAWITEM, nID, (LPARAM)&ncgDI);
			
			pDC->RestoreDC(nSaveDC);
		}

		// post draw for this item
		rItem.left = rGutter.left;
		rItem.right = rGutter.right;
		PostNcDrawItem(pDC, dwItem, rItem, nLevel, FALSE);

		// draw children
		if (bDrawChildren)
		{
			int nChild = 1;
			DWORD dwChild = GetFirstChildItem(dwItem);
			CRect rChild;
			
			while (dwChild)
			{
				NcDrawItem(pDC, dwChild, dwItem, nLevel + 1, nChild, rGutter, rChild, TRUE);
				
				dwChild = GetNextItem(dwChild);
				nChild++;
				
				// accumulate child rects into rItem
				rItem |= rChild;
				
				if (rItem.bottom >= rGutter.bottom)
					break;
			}
			
			// post draw only if we reached the last item
			CRect rItemTotal(rItem);
			rItemTotal.left = rGutter.left;
			rItemTotal.right = rGutter.right;
			
			if (!dwChild)
				PostNcDrawItem(pDC, dwItem, rItemTotal, nLevel, TRUE);
			
			rItem = rItemTotal;
		}
	}
}
void CNcGutter::RedrawItem(DWORD dwItem)
{
	// for speed we draw straight to the window DC so _don't_ use
	// this internally.
	CRect rGutter, rItem;

	GetGutterRect(rGutter, FALSE);

	// prepare the DC
	CWindowDC dc(GetCWnd());
	CFont* pOldFont = PrepareFont(&dc, FALSE);

	NcDrawItem(&dc, dwItem, GetParentItem(dwItem), -1, -1, rGutter, rItem, FALSE);

	// cleanup
	if (pOldFont)
		dc.SelectObject(pOldFont);
}

void CNcGutter::PostNcDrawItem(CDC* pDC, DWORD dwItem, const CRect& rItem, int nLevel, BOOL bParent)
{
	NCGDRAWITEM ncgDI;
		
	ncgDI.pDC = pDC;
	ncgDI.rItem = &rItem;
	ncgDI.nLevel = nLevel;

	if (bParent)
		ncgDI.dwParentItem = dwItem;
	else
		ncgDI.dwItem = dwItem;

	UINT nID = GetDlgCtrlID();

	// try parent then hook window
	if (!::SendMessage(GetParent(), WM_NCG_POSTDRAWITEM, nID, (LPARAM)&ncgDI))
		SendMessage(WM_NCG_POSTDRAWITEM, nID, (LPARAM)&ncgDI);
}

DWORD CNcGutter::GetParentItem(DWORD dwItem) const
{
	UINT nID = GetDlgCtrlID();
		
	// try parent then hook window
	DWORD dwParentID = ::SendMessage(GetParent(), WM_NCG_GETPARENTITEM, nID, dwItem);

	if (!dwParentID)
		dwParentID = SendMessage(WM_NCG_GETPARENTITEM, nID, dwItem);

	return dwParentID;
}

void CNcGutter::PostNcDraw(CDC* pDC, const CRect& rWindow)
{
	NCGDRAWITEM ncgDI;
		
	ncgDI.pDC = pDC;
	ncgDI.rWindow = &rWindow;

	UINT nID = GetDlgCtrlID();
		
	// try parent then hook window
	if (!::SendMessage(GetParent(), WM_NCG_POSTNCDRAW, nID, (LPARAM)&ncgDI))
		SendMessage(WM_NCG_POSTNCDRAW, nID, (LPARAM)&ncgDI);
}

DWORD CNcGutter::GetNextItem(DWORD dwItem) const
{
	UINT nID = GetDlgCtrlID();

	// try parent then hook window
	DWORD dwNextItem = (DWORD)::SendMessage(GetParent(), WM_NCG_GETNEXTITEM, nID, dwItem);

	if (!dwNextItem)
		dwNextItem = (DWORD)SendMessage(WM_NCG_GETNEXTITEM, nID, dwItem);

	return dwNextItem;
}

int CNcGutter::RecalcGutterWidth()
{
	if (!IsHooked())
		return 0;

	// optimization
	if (!m_bSetRedraw)
		return GetGutterWidth();

	CWindowDC dc(GetCWnd());

	NCGRECALCCOLUMN ncrc;
	ncrc.pDC = &dc;

	// try hook window then parent
	UINT nID = GetDlgCtrlID();
	int nGutter = 0;
	int nCol = m_aColumns.GetSize() - 1; // omit last column == client

	while (nCol--)
	{
		// check if its a fixed size column
		COLUMNDESC* pCD = m_aColumns[nCol];

		if (pCD->bCalcWidth)
		{
			CFont* pOldFont = PrepareFont(&dc, FALSE);
			
			ncrc.nColID = pCD->nColID;
			ncrc.nWidth = 0;
			
			// try parent then hook window
			if (!::SendMessage(GetParent(), WM_NCG_RECALCCOLWIDTH, nID, (LPARAM)&ncrc))
				SendMessage(WM_NCG_RECALCCOLWIDTH, nID, (LPARAM)&ncrc);
			
			if (ncrc.nWidth > 0)
			{
				pCD->nWidth = max(ncrc.nWidth, pCD->nTextWidth) + 2 * NCG_COLPADDING;
				
				if (pCD->nSortDir != NCGSORT_NONE)
					pCD->nWidth += SORTWIDTH;
			}
			else
				pCD->nWidth = 0;
			
			if (pOldFont)
				dc.SelectObject(pOldFont);
		}
		nGutter += pCD->nWidth;
	}

	return nGutter;
}

CRect CNcGutter::GetWindowItemRect(DWORD dwItem) const
{
	UINT nID = GetDlgCtrlID();
	NCGITEMRECT ncgGI;
	ncgGI.dwItem = dwItem;

	// try parent then hook window
	if (!::SendMessage(GetParent(), WM_NCG_GETITEMRECT, nID, (LPARAM)&ncgGI))
		SendMessage(WM_NCG_GETITEMRECT, nID, (LPARAM)&ncgGI);

	// convert from client to window coords
	CRect rWindow, rGutter, rItem(ncgGI.rItem);
	GetGutterRect(rGutter, FALSE);
	GetWindowRectEx(rWindow, TRUE);

	CWnd* pWnd = GetCWnd();
	pWnd->ClientToScreen(rItem);

	rItem.OffsetRect(0, -rWindow.top);
	rItem.right = rGutter.right;
	rItem.left = rGutter.left;

	return rItem;
}

DWORD CNcGutter::GetFirstChildItem(DWORD dwItem) const
{
	UINT nID = GetDlgCtrlID();

	// try parent then hook window
	DWORD dwChildItem = (DWORD)::SendMessage(GetParent(), WM_NCG_GETFIRSTCHILDITEM, nID, dwItem);

	if (!dwChildItem)
		dwChildItem = (DWORD)SendMessage(WM_NCG_GETFIRSTCHILDITEM, nID, dwItem);

	return dwChildItem;
}

BOOL CNcGutter::IsItemSelected(DWORD dwItem) const
{
	UINT nID = GetDlgCtrlID();
	NCGITEMSELECTION ncgIS = { dwItem, FALSE };

	// try parent then hook window
	if (!::SendMessage(GetParent(), WM_NCG_ISITEMSELECTED, nID, (LPARAM)&ncgIS))
		SendMessage(WM_NCG_ISITEMSELECTED, nID, (LPARAM)&ncgIS);

	// else
	return ncgIS.bSelected;
}

int CNcGutter::GetSelectedCount() const
{
	UINT nID = GetDlgCtrlID();

	// try parent then hook window
	int nCount = 0;
	
	if (!::SendMessage(GetParent(), WM_NCG_GETSELECTEDCOUNT, nID, (LPARAM)&nCount))
		SendMessage(WM_NCG_GETSELECTEDCOUNT, nID, (LPARAM)&nCount);

	return nCount;
}

BOOL CNcGutter::PrepareBitmap(CDC* pDC, CBitmap* pBitmap, const CRect& rect, BOOL /*bClient*/)
{
	BOOL bRecreate = !pBitmap->GetSafeHandle();

	if (!bRecreate)
	{
		BITMAP BM;

		bRecreate = (!pBitmap->GetBitmap(&BM));

		if (!bRecreate)
		{
			int nBitDepth = pDC->GetDeviceCaps(BITSPIXEL);

			bRecreate = (nBitDepth != BM.bmBitsPixel || 
						rect.right > BM.bmWidth ||
						rect.bottom > BM.bmHeight);
		}
	}

	if (bRecreate)
	{
		pBitmap->DeleteObject();
		pBitmap->CreateCompatibleBitmap(pDC, rect.right, rect.bottom);
	}

	return (NULL != pBitmap->GetSafeHandle());
}

void CNcGutter::UpdateHeaderHotRects()
{
	if (!HasStyle(NCGS_SHOWHEADER) || !CThemed().AreControlsThemed())
		return;

	CRect rWindow, rClient, rHeader;

	// get rects in client coords
	GetWindowRect(rWindow);
	ScreenToClient(rWindow);

	GetClientRect(rClient);

	// adjust top of window rect to be top of header (if showing)
	GetHeaderRect(rHeader, GHR_NONCLIENT, TRUE);
	ScreenToClient(rHeader);

	CRect rItem(rHeader);
	rItem.right = rItem.left;

	int nNumCols = m_aColumns.GetSize() - 1;  // omit last column == client

	for (int nCol = 0; nCol < nNumCols; nCol++)
	{
		rItem.left = rItem.right;
		rItem.right += m_aColumns[nCol]->nWidth;
		rItem.right = min(rItem.right, rWindow.right);
		
		m_hotTrack.UpdateRect(nCol, rItem);
		
		if (rItem.right == rWindow.right)
			break;
	}

	// client bit
	rItem.left = rClient.left;
	rItem.right = rClient.right;

	m_hotTrack.UpdateRect(nNumCols, rItem);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?