home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / MFCITER.ZIP / MainFrm.cpp next >
Encoding:
C/C++ Source or Header  |  1999-02-22  |  6.7 KB  |  218 lines

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "MFCIteratorDemo.h"
  6.  
  7. #include "MainFrm.h"
  8.  
  9. /******************************************************************************/
  10. // DEMO code
  11.  
  12. // Include the iterator headers:
  13. #include "NestedMFCIterator.h"          
  14. #include "MFCIterators.h"
  15.  
  16. #include < algorithm >              // std library algorithm classes header
  17.  
  18. // End of DEMO code section
  19. /******************************************************************************/
  20.  
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. /******************************************************************************/
  28. // DEMO code
  29.  
  30. using std::for_each;        // We're using the std library 'for_each' algorithm
  31.  
  32. namespace {                 // start anonymous namespace for file-scope types
  33.  
  34. // Create an iterator type for all views in a CDocTemplate
  35. typedef NestedMFCIter < ViewIter, DocIter, CDocTemplate > DocTemplateViewIter;
  36.  
  37. // Create an iterator type for all views in a CWinApp
  38. typedef NestedMFCIter < DocTemplateViewIter, CDocTemplateIter, CWinApp > AppViewIter;   
  39.  
  40.  
  41. // Define a predicate class to set the parent frame of a view to a desired show state
  42. class WindowShow  {   
  43. public:
  44.     WindowShow(int nCmdShow) : m_CmdShow(nCmdShow) {}   // constructor
  45.  
  46.     // Predicate function   
  47.     void operator()(CView* pView) { pView->GetParentFrame()->ShowWindow(m_CmdShow); }
  48.  
  49. private:      
  50.     int m_CmdShow;  // Stores the desired show state   
  51. };
  52.  
  53. // An alternative predicate function that toggles the individual window's minimised state.
  54. // Try passing this to for_each in OnWindowToggleMinimiseAll, instead of class WindowShow.
  55. // Note that when passing a function, the parentheses are omitted, so the call will look
  56. // like this:
  57. //
  58. //      for_each(AppViewIter(pApp), AppViewIter::end(), WindowShow);
  59. //
  60. void WindowShow(CView* pView) 
  61.     CFrameWnd* pFrame =  pView->GetParentFrame();
  62.     pFrame->ShowWindow(pFrame->IsIconic() ? SW_RESTORE : SW_SHOWMINNOACTIVE); 
  63. }
  64.  
  65. // This may be of interest too. Try uncommenting line 2 in OnWindowToggleMinimiseAll().
  66. // A class to lock window updating until it goes out of scope
  67. class CWaitWindowUpDate {
  68. public:
  69.     CWaitWindowUpDate(CWnd* pWnd) : m_pWnd(pWnd) { ASSERT(pWnd); pWnd->LockWindowUpdate(); }
  70.     ~CWaitWindowUpDate()                         { m_pWnd->UnlockWindowUpdate(); }
  71. private:
  72.     CWnd* m_pWnd;
  73. };
  74.  
  75. } // end of anonymous namespace
  76.  
  77. // End of DEMO code section
  78. /******************************************************************************/
  79.  
  80. /////////////////////////////////////////////////////////////////////////////
  81. // CMainFrame
  82.  
  83. IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
  84.  
  85. BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
  86.     //{{AFX_MSG_MAP(CMainFrame)
  87.     ON_WM_CREATE()
  88.     ON_COMMAND(ID_WINDOW_TOGGLE_MINIMISEALL, OnWindowToggleMinimiseAll)
  89.     //}}AFX_MSG_MAP
  90. END_MESSAGE_MAP()
  91.  
  92. static UINT indicators[] =
  93. {
  94.     ID_SEPARATOR,           // status line indicator
  95.     ID_INDICATOR_CAPS,
  96.     ID_INDICATOR_NUM,
  97.     ID_INDICATOR_SCRL,
  98. };
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CMainFrame construction/destruction
  102.  
  103. CMainFrame::CMainFrame()
  104. {
  105.     // TODO: add member initialization code here
  106.     
  107. }
  108.  
  109. CMainFrame::~CMainFrame()
  110. {
  111. }
  112.  
  113. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  114. {
  115.     if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
  116.         return -1;
  117.     
  118.     if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
  119.         | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
  120.         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  121.     {
  122.         TRACE0("Failed to create toolbar\n");
  123.         return -1;      // fail to create
  124.     }
  125.  
  126.     if (!m_wndStatusBar.Create(this) ||
  127.         !m_wndStatusBar.SetIndicators(indicators,
  128.           sizeof(indicators)/sizeof(UINT)))
  129.     {
  130.         TRACE0("Failed to create status bar\n");
  131.         return -1;      // fail to create
  132.     }
  133.  
  134.     // TODO: Delete these three lines if you don't want the toolbar to
  135.     //  be dockable
  136.     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  137.     EnableDocking(CBRS_ALIGN_ANY);
  138.     DockControlBar(&m_wndToolBar);
  139.  
  140.     return 0;
  141. }
  142.  
  143. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  144. {
  145.     if( !CMDIFrameWnd::PreCreateWindow(cs) )
  146.         return FALSE;
  147.     // TODO: Modify the Window class or styles here by modifying
  148.     //  the CREATESTRUCT cs
  149.  
  150.     return TRUE;
  151. }
  152.  
  153. /////////////////////////////////////////////////////////////////////////////
  154. // CMainFrame diagnostics
  155.  
  156. #ifdef _DEBUG
  157. void CMainFrame::AssertValid() const
  158. {
  159.     CMDIFrameWnd::AssertValid();
  160. }
  161.  
  162. void CMainFrame::Dump(CDumpContext& dc) const
  163. {
  164.     CMDIFrameWnd::Dump(dc);
  165. }
  166.  
  167. #endif //_DEBUG
  168.  
  169. /////////////////////////////////////////////////////////////////////////////
  170. // CMainFrame message handlers
  171.  
  172. /******************************************************************************/
  173. // DEMO code
  174.  
  175. // Toggle all views between minimised and restored using std::for_each() and the
  176. // application view iterator 'AppViewIter' (defined on line 36 above), together
  177. // with the WindowShow predicate class.
  178.  
  179. void CMainFrame::OnWindowToggleMinimiseAll() 
  180. {
  181.     CWaitCursor         cwc;        // Wait cursor until end of scope
  182. //  CWaitWindowUpDate   cwu(this);  // Wait to update window until end of scope
  183.  
  184.     // Flag for show state
  185.     static int show(SW_SHOWMINNOACTIVE);
  186.  
  187.     // Get application (to be the outermost container)
  188.     CWinApp* pApp = AfxGetApp();
  189.  
  190.     // Do WindowShow on every view in the application
  191.     for_each(AppViewIter(pApp), AppViewIter::end(), WindowShow(show));
  192.  
  193.     // Toggle the show state flag
  194.     show = (show == SW_SHOWMINNOACTIVE) ? SW_RESTORE : SW_SHOWMINNOACTIVE;
  195. }
  196.  
  197. // An alternative way of doing this would be to toggle the individual state of each
  198. // window, minimising normal windows and restoring minimised windows. In this case,
  199. // the static 'show' flag would not be required, and the WindowShow class could be
  200. // replaced by a WindowShow function that would call IsIconic() on the parent frame:
  201. //
  202. //  void WindowShow(CView* pView) 
  203. //  { 
  204. //      CFrameWnd* pFrame =  pView->GetParentFrame();
  205. //      pFrame->ShowWindow(pFrame->IsIconic() ? SW_RESTORE : SW_SHOWMINNOACTIVE); 
  206. //  }
  207. //
  208.  
  209. // End of DEMO code section
  210. /******************************************************************************/
  211.  
  212. BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
  213. {
  214.     dynamic_cast<CMFCIteratorDemoApp*>(AfxGetApp())->OnAppAbout();  
  215.     return CMDIFrameWnd::OnCreateClient(lpcs, pContext);
  216. }
  217.