home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / general / dynamenu / mdichild.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  4.6 KB  |  158 lines

  1. // mdichild.cpp
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12. //
  13. // Purpose: implementation of the CDynaMDIChildWnd class
  14. //
  15. // Functions:
  16. //   Most of this file was generated by AppWizard.  The functions
  17. //   which contain code specific to this sample are:
  18. //
  19. //      CDynaMDIChildWnd::OnMDIActivate()    -- handle activation/deactivation
  20. //      CDynaMDIChildWnd::RefreshColorMenu() -- update Color submenu contents
  21.  
  22. #include "stdafx.h"
  23. #include "dynamenu.h"
  24. #include "mdichild.h"
  25. #include "dmdoc.h"      // for color menu definitions
  26.  
  27. #ifdef _DEBUG
  28. #undef THIS_FILE
  29. static char BASED_CODE THIS_FILE[] = __FILE__;
  30. #endif
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CDynaMDIChildWnd
  34.  
  35. IMPLEMENT_DYNCREATE(CDynaMDIChildWnd, CMDIChildWnd)
  36.  
  37. CDynaMDIChildWnd::CDynaMDIChildWnd()
  38. {
  39. }
  40.  
  41. CDynaMDIChildWnd::~CDynaMDIChildWnd()
  42. {
  43. }
  44.  
  45.  
  46. BEGIN_MESSAGE_MAP(CDynaMDIChildWnd, CMDIChildWnd)
  47.     //{{AFX_MSG_MAP(CDynaMDIChildWnd)
  48.     ON_WM_MDIACTIVATE()
  49.     //}}AFX_MSG_MAP
  50. END_MESSAGE_MAP()
  51.  
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CDynaMDIChildWnd message handlers
  55.  
  56.  
  57. //***********************************************************************
  58. // Function: CDynaMDIChildWnd::OnMDIActivate()
  59. //
  60. // Purpose:
  61. //    OnMDIActivate is called for the MDI child window being
  62. //    deactivated and the child window being activated.
  63. //
  64. //    We override this function so that we can update the Color submenu
  65. //    when a child window is activated.  A call to RefreshColorMenu()
  66. //    performs the actual update.
  67. //
  68. // Parameters:
  69. //    bActivate      -- TRUE if child window being activated, otherwise
  70. //                      FALSE
  71. //    pActivateWnd   -- pointer to MDI child window being activated
  72. //    pDeactivateWnd -- pointer to MDI child window being deactivated
  73. //
  74. // Returns:
  75. //    none
  76. //
  77. // Comments:
  78. //    see the CWnd::OnMDIActivate() docs for further information.
  79. //
  80. //***********************************************************************
  81. void CDynaMDIChildWnd::OnMDIActivate(BOOL bActivate, CWnd* pActivateWnd, CWnd* pDeactivateWnd)
  82. {
  83.     // call the base class to let standard processing switch to
  84.     // the top-level menu associated with this window
  85.     CMDIChildWnd::OnMDIActivate(bActivate, pActivateWnd, pDeactivateWnd);
  86.  
  87.     // do our modifications, if we are being activated
  88.     if (bActivate)
  89.         RefreshColorMenu();
  90. }
  91.  
  92. //***********************************************************************
  93. // Function: CDynaMDIChildWnd::RefreshColorMenu()
  94. //
  95. // Purpose:
  96. //    RefreshColorMenu updates the Color submenu so that it only
  97. //    contains the text colors which are enabled for the active document.
  98. //
  99. //    The Color submenu is located by searching for a submenu with
  100. //    ID_COLOR_OPTIONS as its first menu item.
  101. //
  102. // Parameters:
  103. //    none
  104. //
  105. // Returns:
  106. //    none
  107. //
  108. // Comments:
  109. //    none
  110. //
  111. //***********************************************************************
  112. void CDynaMDIChildWnd::RefreshColorMenu()
  113. {
  114.     // Get the active document
  115.     CDynaMenuDoc* pDoc = (CDynaMenuDoc*)GetActiveDocument();
  116.     ASSERT_KINDOF(CDynaMenuDoc, pDoc);
  117.  
  118.     // Locate the color submenu
  119.     CMenu* pColorMenu = NULL;
  120.     CMenu* pTopMenu = AfxGetMainWnd()->GetMenu();
  121.     int iPos;
  122.     for (iPos = pTopMenu->GetMenuItemCount()-1; iPos >= 0; iPos--)
  123.     {
  124.         CMenu* pMenu = pTopMenu->GetSubMenu(iPos);
  125.         if (pMenu && pMenu->GetMenuItemID(0) == ID_COLOR_OPTIONS)
  126.         {
  127.             pColorMenu = pMenu;
  128.             break;
  129.         }
  130.     }
  131.     ASSERT(pColorMenu != NULL);
  132.  
  133.     // Update the color submenu to reflect the text colors available to
  134.     // the active document
  135.  
  136.     // First, delete all items but ID_COLOR_OPTIONS at position 0
  137.     for (iPos = pColorMenu->GetMenuItemCount()-1; iPos > 0; iPos--)
  138.         pColorMenu->DeleteMenu(iPos, MF_BYPOSITION);
  139.  
  140.     // Then, add a separator and an item for each available text color
  141.     BOOL bNeedSeparator = TRUE;
  142.     for (int i = 0; i < NUM_TEXTCOLOR; i++)
  143.     {
  144.         if (pDoc->m_abAllowColor[i] == TRUE)
  145.         {
  146.             if (bNeedSeparator)
  147.             {
  148.                 pColorMenu->AppendMenu(MF_SEPARATOR);
  149.                 bNeedSeparator = FALSE;
  150.             }
  151.             CString strColor;
  152.             strColor.LoadString(pDoc->m_aColorDef[i].m_nString);
  153.             pColorMenu->AppendMenu(MF_STRING,
  154.                 pDoc->m_aColorDef[i].m_nID, strColor);
  155.         }
  156.     }
  157. }
  158.