SizingTabCtrlBar.cpp
上传用户:libwxy
上传日期:2007-01-02
资源大小:200k
文件大小:7k
源码类别:

工具条

开发平台:

Visual C++

  1. // SizingTabCtrlBar.cpp: Implementierungsdatei
  2. //
  3. #include "stdafx.h"
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #undef THIS_FILE
  7. static char THIS_FILE[] = __FILE__;
  8. #endif
  9. /////////////////////////////////////////////////////////////////////////////
  10. // CSizingTabCtrlBar
  11. CSizingTabCtrlBar::CSizingTabCtrlBar()
  12. {
  13. m_nActiveTab = 0;
  14. m_uiTabID = 0;
  15. m_uiTabImage = 0;
  16. }
  17. CSizingTabCtrlBar::~CSizingTabCtrlBar()
  18. {
  19. while(!m_views.IsEmpty())
  20. {
  21. TCB_ITEM *pMember=m_views.RemoveHead();
  22. delete pMember;
  23. }
  24. }
  25. BEGIN_MESSAGE_MAP(CSizingTabCtrlBar, CSizingControlBar)
  26. //{{AFX_MSG_MAP(CSizingTabCtrlBar)
  27. ON_WM_CREATE()
  28. ON_WM_SIZE()
  29. ON_NOTIFY_REFLECT(TCN_SELCHANGE, OnTabSelChange)
  30. //}}AFX_MSG_MAP
  31. END_MESSAGE_MAP()
  32. /////////////////////////////////////////////////////////////////////////////
  33. // Behandlungsroutinen f黵 Nachrichten CSizingTabCtrlBar 
  34. int CSizingTabCtrlBar::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  35. {
  36. if (CSizingControlBar::OnCreate(lpCreateStruct) == -1)
  37. return -1;
  38. //Create the Tab Control
  39. CRect rect;
  40. m_tabctrl.Create(WS_VISIBLE | WS_CHILD | TCS_BOTTOM, 
  41. rect, this, m_uiTabID);
  42. if (m_uiTabImage != 0)
  43. {
  44. m_images.Create(m_uiTabImage, 16, 1, RGB(255,0,255));
  45. m_tabctrl.SetImageList(&m_images);
  46. }
  47. // set "normal" GUI-font
  48. CFont *font = CFont::FromHandle((HFONT)::GetStockObject(DEFAULT_GUI_FONT));
  49. m_tabctrl.SetFont(font);
  50. return 0;
  51. }
  52. void CSizingTabCtrlBar::OnSize(UINT nType, int cx, int cy) 
  53. {
  54. CSizingControlBar::OnSize(nType, cx, cy);
  55. int bottom = (IsHorz() || IsFloating()) ? cy - 14 : cy - 18;
  56. m_tabctrl.MoveWindow(7, 7, cx - 14, bottom);
  57. CWnd *pWnd;
  58.     for (POSITION pos=m_views.GetHeadPosition(); pos; m_views.GetNext(pos))
  59.     {
  60.          pWnd=m_views.GetAt(pos)->pWnd;
  61.          pWnd->MoveWindow(9, 9, cx-19, bottom-26);
  62.     }
  63. }
  64. BOOL CSizingTabCtrlBar::AddView(LPCTSTR lpszLabel, CRuntimeClass *pViewClass, CCreateContext *pContext)
  65. {
  66. #ifdef _DEBUG
  67. ASSERT_VALID(this);
  68. ASSERT(pViewClass != NULL);
  69. ASSERT(pViewClass->IsDerivedFrom(RUNTIME_CLASS(CWnd)));
  70. ASSERT(AfxIsValidAddress(pViewClass, sizeof(CRuntimeClass), FALSE));
  71. #endif
  72. CCreateContext context;
  73. if (pContext == NULL)
  74. {
  75. // if no context specified, generate one from the currently selected
  76. //  client if possible
  77. CView* pOldView = NULL;
  78. if (pOldView != NULL && pOldView->IsKindOf(RUNTIME_CLASS(CView)))
  79. {
  80. // set info about last pane
  81. ASSERT(context.m_pCurrentFrame == NULL);
  82. context.m_pLastView = pOldView;
  83. context.m_pCurrentDoc = pOldView->GetDocument();
  84. if (context.m_pCurrentDoc != NULL)
  85. context.m_pNewDocTemplate =
  86. context.m_pCurrentDoc->GetDocTemplate();
  87. }
  88. pContext = &context;
  89. }
  90. CWnd* pWnd;
  91. TRY
  92. {
  93. pWnd = (CWnd*)pViewClass->CreateObject();
  94. if (pWnd == NULL)
  95. AfxThrowMemoryException();
  96. }
  97. CATCH_ALL(e)
  98. {
  99. TRACE0("Out of memory creating a view.n");
  100. // Note: DELETE_EXCEPTION(e) not required
  101. return FALSE;
  102. }
  103. END_CATCH_ALL
  104.     ASSERT_KINDOF(CWnd, pWnd);
  105. ASSERT(pWnd->m_hWnd == NULL);       // not yet created
  106. DWORD dwStyle = AFX_WS_DEFAULT_VIEW;
  107. CRect rect;
  108. // Create with the right size and position
  109. if (!pWnd->Create(NULL, NULL, dwStyle, rect, this, 0, pContext))
  110. {
  111. TRACE0("Warning: couldn't create client pane for view.n");
  112. // pWnd will be cleaned up by PostNcDestroy
  113. return FALSE;
  114. }
  115. m_pActiveView = (CView*) pWnd;
  116. TCB_ITEM *pMember=new TCB_ITEM;
  117. pMember->pWnd=pWnd;
  118. strcpy(pMember->szLabel, lpszLabel);
  119. m_views.AddTail(pMember);
  120. int nViews = m_views.GetCount();
  121. if (nViews!=1)
  122. {
  123. pWnd->EnableWindow(FALSE);
  124. pWnd->ShowWindow(SW_HIDE);
  125. }
  126. else
  127. {
  128. ((CFrameWnd *)GetParent())->SetActiveView((CView *)m_pActiveView);
  129. }
  130. TC_ITEM tci;
  131. tci.mask = TCIF_TEXT;
  132. tci.pszText = (LPTSTR)(LPCTSTR)lpszLabel;
  133. if (m_uiTabImage != 0)
  134. {
  135. tci.mask |= TCIF_IMAGE;
  136. tci.iImage = nViews-1;
  137. }
  138. m_tabctrl.InsertItem(nViews, &tci);
  139. return TRUE;
  140. }
  141. void CSizingTabCtrlBar::RemoveView(int nView)
  142. {
  143. ASSERT_VALID(this);
  144. ASSERT(nView >= 0);
  145. // remove the page from internal list
  146. m_views.RemoveAt(m_views.FindIndex(nView));
  147. }
  148. void CSizingTabCtrlBar::OnTabSelChange(NMHDR* pNMHDR, LRESULT* pResult)
  149. {
  150. SetActiveView(m_tabctrl.GetCurSel());
  151. }
  152. void CSizingTabCtrlBar::SetActiveView(int nNewTab)
  153. {
  154. ASSERT_VALID(this);
  155. ASSERT(nNewTab >= 0);
  156. if (nNewTab!=-1 && nNewTab!=m_nActiveTab)
  157. {
  158.         TCB_ITEM *newMember=m_views.GetAt(m_views.FindIndex(nNewTab));
  159.         TCB_ITEM *oldMember=NULL;
  160.         if (m_nActiveTab!=-1)
  161.         {
  162.             oldMember=m_views.GetAt(m_views.FindIndex(m_nActiveTab));
  163.             oldMember->pWnd->EnableWindow(FALSE);
  164.             oldMember->pWnd->ShowWindow(SW_HIDE);
  165.         }
  166.         newMember->pWnd->EnableWindow(TRUE);
  167.         newMember->pWnd->ShowWindow(SW_SHOW);
  168.         newMember->pWnd->SetFocus();
  169.         m_pActiveView = (CView *)newMember->pWnd;
  170. ((CFrameWnd *)GetParent())->SetActiveView(m_pActiveView);
  171.         m_nActiveTab = nNewTab;
  172. // select the tab (if tab programmatically changed)
  173. m_tabctrl.SetCurSel(m_nActiveTab);
  174.     }
  175. }
  176. void CSizingTabCtrlBar::SetActiveView(CRuntimeClass *pViewClass)
  177. {
  178. ASSERT_VALID(this);
  179. ASSERT(pViewClass != NULL);
  180. ASSERT(pViewClass->IsDerivedFrom(RUNTIME_CLASS(CWnd)));
  181. ASSERT(AfxIsValidAddress(pViewClass, sizeof(CRuntimeClass), FALSE));
  182. int nNewTab = 0;
  183. for (POSITION pos=m_views.GetHeadPosition(); pos; m_views.GetNext(pos))
  184. {
  185. TCB_ITEM *pMember=m_views.GetAt(pos);
  186. if (pMember->pWnd->IsKindOf(pViewClass))
  187. {
  188. //first hide old first view
  189.             m_pActiveView->EnableWindow(FALSE);
  190.             m_pActiveView->ShowWindow(SW_HIDE);
  191. // set new active view
  192. m_pActiveView = (CView*)pMember->pWnd;
  193. // enable, show, set focus to new view
  194. m_pActiveView->EnableWindow(TRUE);
  195. m_pActiveView->ShowWindow(SW_SHOW);
  196. m_pActiveView->SetFocus();
  197. ((CFrameWnd *)GetParent())->SetActiveView(m_pActiveView);
  198.         m_nActiveTab = nNewTab;
  199. // select the tab
  200. m_tabctrl.SetCurSel(m_nActiveTab);
  201. break;
  202. }
  203. nNewTab++;
  204.     }
  205. }
  206. CView* CSizingTabCtrlBar::GetActiveView()
  207. {
  208. return m_pActiveView;
  209. }
  210. CView* CSizingTabCtrlBar::GetView(int nView)
  211. {
  212. ASSERT_VALID(this);
  213. ASSERT(nView >= 0);
  214. if (nView!=-1)
  215. {
  216.         TCB_ITEM *pMember=m_views.GetAt(m_views.FindIndex(nView));
  217. return (CView*)pMember->pWnd;
  218. }
  219. else
  220. return NULL;
  221. }
  222. CView* CSizingTabCtrlBar::GetView(CRuntimeClass *pViewClass)
  223. {
  224. ASSERT_VALID(this);
  225. ASSERT(pViewClass != NULL);
  226. ASSERT(pViewClass->IsDerivedFrom(RUNTIME_CLASS(CWnd)));
  227. ASSERT(AfxIsValidAddress(pViewClass, sizeof(CRuntimeClass), FALSE));
  228. for (POSITION pos=m_views.GetHeadPosition(); pos; m_views.GetNext(pos))
  229. {
  230. TCB_ITEM *pMember=m_views.GetAt(pos);
  231. if (pMember->pWnd->IsKindOf(pViewClass))
  232. {
  233. return (CView*)pMember->pWnd;
  234. }
  235.     }
  236. return NULL;
  237. }