// WindowTabCtrl.cpp : implementation file // #include "stdafx.h" #include "resource.h" // main symbols #include "WindowTabCtrl.h" #include "ViewManager.h" #include "MainFrm.h" #include "ChildFrm.h" #include "IDEView.h" //#include "PopupMenu.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define CX_BORDER 1 #define CY_BORDER 1 ///////////////////////////////////////////////////////////////////////////// // CWindowTabCtrl CWindowTabCtrl::CWindowTabCtrl() : m_bDisplayIcons(TRUE) { m_crSelColor = RGB(0, 0, 255); m_crUnselColor = RGB(50, 50, 50); m_rcDocModified = RGB(255, 0, 0); } CWindowTabCtrl::~CWindowTabCtrl() { m_SelFont.DeleteObject(); m_UnselFont.DeleteObject(); } BEGIN_MESSAGE_MAP(CWindowTabCtrl, CTabCtrl) //{{AFX_MSG_MAP(CWindowTabCtrl) ON_NOTIFY_REFLECT(TCN_SELCHANGE, OnSelchange) ON_WM_CREATE() ON_WM_RBUTTONDOWN() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CWindowTabCtrl message handlers void CWindowTabCtrl::OnSelchange(NMHDR* pNMHDR, LRESULT* pResult) { if (pNMHDR->hwndFrom == NULL) return; // just to avoid level 4 compilation warning int idx = GetCurSel(); TCITEM tci; tci.mask = TCIF_PARAM; GetItem(idx, &tci); CView* pView = reinterpret_cast(tci.lParam); static_cast(AfxGetMainWnd())->MDIActivate(pView->GetParent()); CMainFrame *pMainFrm = (CMainFrame*)AfxGetApp()->m_pMainWnd; CMDIChildWnd *pChildFrm = (CMDIChildWnd *)pMainFrm->GetActiveFrame(); //pMainFrm->m_pActiveView = (CSummaryView *)static_cast(AfxGetMainWnd())->GetActiveView(); pMainFrm->m_pActiveView = (CFormView *)pChildFrm->GetActiveView(); CString strPath = ((CChildFrame *)pChildFrm)->m_strPath; g_strViewName = strPath; ((CIDEView *)pMainFrm->m_pActiveView)->ChangeView(); pMainFrm->GetCurPicVarToSArray( strPath ); *pResult = 0; } void CWindowTabCtrl::SetIconDisplay(BOOL bDisplayIt) { m_bDisplayIcons = bDisplayIt; } int CWindowTabCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CTabCtrl::OnCreate(lpCreateStruct) == -1) { TRACE0(_T("OnCreate function call failed!")); return -1; } ModifyStyle(0, TCS_OWNERDRAWFIXED); HFONT hFont = reinterpret_cast(GetStockObject(DEFAULT_GUI_FONT)); CFont* pFont = CFont::FromHandle(hFont); SetFont(pFont); return 0; } void CWindowTabCtrl::PreSubclassWindow() { CTabCtrl::PreSubclassWindow(); ModifyStyle(0, TCS_OWNERDRAWFIXED); //TODO must go! } void CWindowTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CRect rcTab = CRect(&(lpDrawItemStruct->rcItem)); int nTabIndex = lpDrawItemStruct->itemID; if (nTabIndex < 0) return; BOOL bSelected = (nTabIndex == GetCurSel()); TCHAR szLabel[_MAX_PATH]; TCITEM tci; tci.mask = TCIF_TEXT | TCIF_IMAGE | TCIF_PARAM; tci.pszText = szLabel; tci.cchTextMax = _MAX_PATH - 1; if (!GetItem(nTabIndex, &tci)) return; CDC dc; if (!dc.Attach(lpDrawItemStruct->hDC)) return; DWORD dwStyle = GetStyle(); rcTab.NormalizeRect(); if ((dwStyle & TCS_BOTTOM) == 0) { rcTab.DeflateRect(CX_BORDER, CY_BORDER); rcTab.top += ::GetSystemMetrics(SM_CYEDGE); } dc.SetBkMode(TRANSPARENT); dc.FillSolidRect(rcTab, ::GetSysColor(COLOR_BTNFACE)); CView* pView = reinterpret_cast(tci.lParam); CDocument* pDoc = pView->GetDocument(); // Draw image if (m_bDisplayIcons) { CImageList* pImageList = GetImageList(); CMDIChildWnd* pViewFrame = static_cast(pView->GetParent()); HICON hIcon = reinterpret_cast(GetClassLong(pViewFrame->m_hWnd, GCL_HICONSM)); pImageList->Replace(nTabIndex, hIcon); if (pImageList && tci.iImage >= 0) { rcTab.left += dc.GetTextExtent(_T(" ")).cx; // Set a little margin // Get the height of image IMAGEINFO info; pImageList->GetImageInfo(tci.iImage, &info); CRect ImageRect(info.rcImage); int nYpos = rcTab.top; pImageList->Draw(&dc, tci.iImage, CPoint(rcTab.left, nYpos), ILD_TRANSPARENT); rcTab.left += ImageRect.Width(); } } //if (bSelected) //{ // if (pDoc->IsModified()) // dc.SetTextColor(m_rcDocModified); // else // dc.SetTextColor(m_crSelColor); // dc.SelectObject(&m_SelFont); // rcTab.top -= ::GetSystemMetrics(SM_CYEDGE); //} //else //{ // if (pDoc->IsModified()) // dc.SetTextColor(m_rcDocModified); // else // dc.SetTextColor(m_crUnselColor); // dc.SelectObject(&m_UnselFont); //} dc.DrawText(szLabel, rcTab, DT_SINGLELINE | DT_VCENTER | DT_CENTER); dc.Detach(); } ///////////////////////////////////////////////////////////////////////////// // CWindowTabCtrl operations void CWindowTabCtrl::SetColors(COLORREF bSelColor, COLORREF bUnselColor, COLORREF rcDocModified) { m_crSelColor = bSelColor; m_crUnselColor = bUnselColor; m_rcDocModified = rcDocModified; Invalidate(); } void CWindowTabCtrl::SetFonts(CFont* pSelFont, CFont* pUnselFont) { ASSERT(pSelFont && pUnselFont); LOGFONT lFont; int nSelHeight, nUnselHeight; m_SelFont.DeleteObject(); m_UnselFont.DeleteObject(); pSelFont->GetLogFont(&lFont); m_SelFont.CreateFontIndirect(&lFont); nSelHeight = lFont.lfHeight; pUnselFont->GetLogFont(&lFont); m_UnselFont.CreateFontIndirect(&lFont); nUnselHeight = lFont.lfHeight; SetFont((nSelHeight > nUnselHeight)? &m_SelFont : &m_UnselFont); } void CWindowTabCtrl::SetFonts(int nSelWeight, BOOL bSelItalic, BOOL bSelUnderline, int nUnselWeight, BOOL bUnselItalic, BOOL bUnselUnderline) { // Free any memory currently used by the fonts. m_SelFont.DeleteObject(); m_UnselFont.DeleteObject(); // Get the current font LOGFONT lFont; CFont *pFont = GetFont(); if (pFont) pFont->GetLogFont(&lFont); else { NONCLIENTMETRICS ncm; ncm.cbSize = sizeof(NONCLIENTMETRICS); VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0)); lFont = ncm.lfMessageFont; } // Create the "Selected" font lFont.lfWeight = static_cast(nSelWeight); lFont.lfItalic = static_cast(bSelItalic); lFont.lfUnderline = static_cast(bSelUnderline); m_SelFont.CreateFontIndirect(&lFont); // Create the "Unselected" font lFont.lfWeight = static_cast(nUnselWeight); lFont.lfItalic = static_cast(bUnselItalic); lFont.lfUnderline = static_cast(bUnselUnderline); m_UnselFont.CreateFontIndirect(&lFont); SetFont((nSelWeight > nUnselWeight) ? &m_SelFont : &m_UnselFont); } void CWindowTabCtrl::OnRButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default // Select the tab which is clicked and activate the attached view TCHITTESTINFO hti; hti.pt = CPoint(GetMessagePos()); ScreenToClient(&hti.pt); int iTab = HitTest(&hti); if (iTab < 0) return; SetCurSel(iTab); // Select it TCITEM tci; tci.mask = TCIF_PARAM; GetItem(iTab, &tci); CView* pView = reinterpret_cast(tci.lParam); static_cast(AfxGetMainWnd())->MDIActivate(pView->GetParent()); // Now, process the popup menu for the tab control POINT ptScreen = point; // Convert the mouse point to screen coordinates since that is what is used by // the TrackPopupMenu() function. ClientToScreen(&ptScreen); #if 0 CMenu cMenu; cMenu.CreatePopupMenu(); cMenu.AppendMenu(MF_STRING, ID_FILE_CLOSE, _T("¹Ø±Õ´°Ìå(&C)")); cMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, ptScreen.x, ptScreen.y, AfxGetMainWnd()); cMenu.DestroyMenu(); #endif CTabCtrl::OnRButtonDown(nFlags, point); }