WindowTabCtrl.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // WindowTabCtrl.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h" // main symbols
  5. #include "WindowTabCtrl.h"
  6. #include "ViewManager.h"
  7. #include "MainFrm.h"
  8. #include "ChildFrm.h"
  9. #include "IDEView.h"
  10. //#include "PopupMenu.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. #define CX_BORDER 1
  17. #define CY_BORDER 1
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CWindowTabCtrl
  20. CWindowTabCtrl::CWindowTabCtrl() : m_bDisplayIcons(TRUE)
  21. {
  22. m_crSelColor = RGB(0, 0, 255);
  23. m_crUnselColor = RGB(50, 50, 50);
  24. m_rcDocModified = RGB(255, 0, 0);
  25. }
  26. CWindowTabCtrl::~CWindowTabCtrl()
  27. {
  28. m_SelFont.DeleteObject();
  29. m_UnselFont.DeleteObject();
  30. }
  31. BEGIN_MESSAGE_MAP(CWindowTabCtrl, CTabCtrl)
  32. //{{AFX_MSG_MAP(CWindowTabCtrl)
  33. ON_NOTIFY_REFLECT(TCN_SELCHANGE, OnSelchange)
  34. ON_WM_CREATE()
  35. ON_WM_RBUTTONDOWN()
  36. //}}AFX_MSG_MAP
  37. END_MESSAGE_MAP()
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CWindowTabCtrl message handlers
  40. void CWindowTabCtrl::OnSelchange(NMHDR* pNMHDR, LRESULT* pResult)
  41. {
  42. if (pNMHDR->hwndFrom == NULL)
  43. return; // just to avoid level 4 compilation warning
  44. int idx = GetCurSel();
  45. TCITEM tci;
  46. tci.mask = TCIF_PARAM;
  47. GetItem(idx, &tci);
  48. CView* pView = reinterpret_cast<CView*>(tci.lParam);
  49. static_cast<CMDIFrameWnd*>(AfxGetMainWnd())->MDIActivate(pView->GetParent());
  50. CMainFrame *pMainFrm = (CMainFrame*)AfxGetApp()->m_pMainWnd;
  51. CMDIChildWnd *pChildFrm = (CMDIChildWnd *)pMainFrm->GetActiveFrame();
  52. //pMainFrm->m_pActiveView = (CSummaryView *)static_cast<CMDIFrameWnd*>(AfxGetMainWnd())->GetActiveView();
  53. pMainFrm->m_pActiveView = (CFormView *)pChildFrm->GetActiveView();
  54. CString strPath = ((CChildFrame *)pChildFrm)->m_strPath;
  55. g_strViewName = strPath;
  56. ((CIDEView *)pMainFrm->m_pActiveView)->ChangeView();
  57. pMainFrm->GetCurPicVarToSArray( strPath );
  58. *pResult = 0;
  59. }
  60. void CWindowTabCtrl::SetIconDisplay(BOOL bDisplayIt)
  61. {
  62. m_bDisplayIcons = bDisplayIt;
  63. }
  64. int CWindowTabCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
  65. {
  66. if (CTabCtrl::OnCreate(lpCreateStruct) == -1)
  67. {
  68. TRACE0(_T("OnCreate function call failed!"));
  69. return -1;
  70. }
  71. ModifyStyle(0, TCS_OWNERDRAWFIXED);
  72. HFONT hFont = reinterpret_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT));
  73. CFont* pFont = CFont::FromHandle(hFont);
  74. SetFont(pFont);
  75. return 0;
  76. }
  77. void CWindowTabCtrl::PreSubclassWindow()
  78. {
  79. CTabCtrl::PreSubclassWindow();
  80. ModifyStyle(0, TCS_OWNERDRAWFIXED); //TODO must go!
  81. }
  82. void CWindowTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  83. {
  84. CRect rcTab = CRect(&(lpDrawItemStruct->rcItem));
  85. int nTabIndex = lpDrawItemStruct->itemID;
  86. if (nTabIndex < 0)
  87. return;
  88. BOOL bSelected = (nTabIndex == GetCurSel());
  89. TCHAR szLabel[_MAX_PATH];
  90. TCITEM tci;
  91. tci.mask = TCIF_TEXT | TCIF_IMAGE | TCIF_PARAM;
  92. tci.pszText = szLabel;
  93. tci.cchTextMax = _MAX_PATH - 1;
  94. if (!GetItem(nTabIndex, &tci))
  95. return;
  96. CDC dc;
  97. if (!dc.Attach(lpDrawItemStruct->hDC))
  98. return;
  99. DWORD dwStyle = GetStyle();
  100. rcTab.NormalizeRect();
  101. if ((dwStyle & TCS_BOTTOM) == 0)
  102. {
  103. rcTab.DeflateRect(CX_BORDER, CY_BORDER);
  104. rcTab.top += ::GetSystemMetrics(SM_CYEDGE);
  105. }
  106. dc.SetBkMode(TRANSPARENT);
  107. dc.FillSolidRect(rcTab, ::GetSysColor(COLOR_BTNFACE));
  108. CView* pView = reinterpret_cast<CView*>(tci.lParam);
  109. CDocument* pDoc = pView->GetDocument();
  110. // Draw image
  111. if (m_bDisplayIcons)
  112. {
  113. CImageList* pImageList = GetImageList();
  114. CMDIChildWnd* pViewFrame = static_cast<CMDIChildWnd*>(pView->GetParent());
  115. HICON hIcon = reinterpret_cast<HICON>(GetClassLong(pViewFrame->m_hWnd, GCL_HICONSM));
  116. pImageList->Replace(nTabIndex, hIcon);
  117. if (pImageList && tci.iImage >= 0)
  118. {
  119. rcTab.left += dc.GetTextExtent(_T(" ")).cx; // Set a little margin
  120. // Get the height of image
  121. IMAGEINFO info;
  122. pImageList->GetImageInfo(tci.iImage, &info);
  123. CRect ImageRect(info.rcImage);
  124. int nYpos = rcTab.top;
  125. pImageList->Draw(&dc, tci.iImage, CPoint(rcTab.left, nYpos), ILD_TRANSPARENT);
  126. rcTab.left += ImageRect.Width();
  127. }
  128. }
  129. //if (bSelected)
  130. //{
  131. // if (pDoc->IsModified())
  132. // dc.SetTextColor(m_rcDocModified);
  133. // else
  134. // dc.SetTextColor(m_crSelColor);
  135. // dc.SelectObject(&m_SelFont);
  136. // rcTab.top -= ::GetSystemMetrics(SM_CYEDGE);
  137. //}
  138. //else
  139. //{
  140. // if (pDoc->IsModified())
  141. // dc.SetTextColor(m_rcDocModified);
  142. // else
  143. // dc.SetTextColor(m_crUnselColor);
  144. // dc.SelectObject(&m_UnselFont);
  145. //}
  146. dc.DrawText(szLabel, rcTab, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
  147. dc.Detach();
  148. }
  149. /////////////////////////////////////////////////////////////////////////////
  150. // CWindowTabCtrl operations
  151. void CWindowTabCtrl::SetColors(COLORREF bSelColor, COLORREF bUnselColor,
  152. COLORREF rcDocModified)
  153. {
  154. m_crSelColor = bSelColor;
  155. m_crUnselColor = bUnselColor;
  156. m_rcDocModified = rcDocModified;
  157. Invalidate();
  158. }
  159. void CWindowTabCtrl::SetFonts(CFont* pSelFont, CFont* pUnselFont)
  160. {
  161. ASSERT(pSelFont && pUnselFont);
  162. LOGFONT lFont;
  163. int nSelHeight, nUnselHeight;
  164. m_SelFont.DeleteObject();
  165. m_UnselFont.DeleteObject();
  166. pSelFont->GetLogFont(&lFont);
  167. m_SelFont.CreateFontIndirect(&lFont);
  168. nSelHeight = lFont.lfHeight;
  169. pUnselFont->GetLogFont(&lFont);
  170. m_UnselFont.CreateFontIndirect(&lFont);
  171. nUnselHeight = lFont.lfHeight;
  172. SetFont((nSelHeight > nUnselHeight)? &m_SelFont : &m_UnselFont);
  173. }
  174. void CWindowTabCtrl::SetFonts(int nSelWeight, BOOL bSelItalic, BOOL bSelUnderline,
  175. int nUnselWeight, BOOL bUnselItalic, BOOL bUnselUnderline)
  176. {
  177. // Free any memory currently used by the fonts.
  178. m_SelFont.DeleteObject();
  179. m_UnselFont.DeleteObject();
  180. // Get the current font
  181. LOGFONT lFont;
  182. CFont *pFont = GetFont();
  183. if (pFont)
  184. pFont->GetLogFont(&lFont);
  185. else
  186. {
  187. NONCLIENTMETRICS ncm;
  188. ncm.cbSize = sizeof(NONCLIENTMETRICS);
  189. VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
  190. sizeof(NONCLIENTMETRICS), &ncm, 0));
  191. lFont = ncm.lfMessageFont;
  192. }
  193. // Create the "Selected" font
  194. lFont.lfWeight = static_cast<LONG>(nSelWeight);
  195. lFont.lfItalic = static_cast<BYTE>(bSelItalic);
  196. lFont.lfUnderline = static_cast<BYTE>(bSelUnderline);
  197. m_SelFont.CreateFontIndirect(&lFont);
  198. // Create the "Unselected" font
  199. lFont.lfWeight = static_cast<LONG>(nUnselWeight);
  200. lFont.lfItalic = static_cast<BYTE>(bUnselItalic);
  201. lFont.lfUnderline = static_cast<BYTE>(bUnselUnderline);
  202. m_UnselFont.CreateFontIndirect(&lFont);
  203. SetFont((nSelWeight > nUnselWeight) ? &m_SelFont : &m_UnselFont);
  204. }
  205. void CWindowTabCtrl::OnRButtonDown(UINT nFlags, CPoint point)
  206. {
  207. // TODO: Add your message handler code here and/or call default
  208. // Select the tab which is clicked and activate the attached view
  209. TCHITTESTINFO hti;
  210. hti.pt = CPoint(GetMessagePos());
  211. ScreenToClient(&hti.pt);
  212. int iTab = HitTest(&hti);
  213. if (iTab < 0)
  214. return;
  215. SetCurSel(iTab); // Select it
  216. TCITEM tci;
  217. tci.mask = TCIF_PARAM;
  218. GetItem(iTab, &tci);
  219. CView* pView = reinterpret_cast<CView*>(tci.lParam);
  220. static_cast<CMDIFrameWnd*>(AfxGetMainWnd())->MDIActivate(pView->GetParent());
  221. // Now, process the popup menu for the tab control
  222. POINT ptScreen = point;
  223. // Convert the mouse point to screen coordinates since that is what is used by
  224. // the TrackPopupMenu() function.
  225. ClientToScreen(&ptScreen);
  226. #if 0
  227. CMenu cMenu;
  228. cMenu.CreatePopupMenu();
  229. cMenu.AppendMenu(MF_STRING, ID_FILE_CLOSE, _T("¹Ø±Õ´°Ìå(&C)"));
  230. cMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, ptScreen.x, ptScreen.y, AfxGetMainWnd());
  231. cMenu.DestroyMenu();
  232. #endif
  233. CTabCtrl::OnRButtonDown(nFlags, point);
  234. }