home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / winmenu.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  4KB  |  153 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_CORE1_SEG
  14. #pragma code_seg(AFX_CORE1_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // Map from HMENU to CMenu *
  26.  
  27. #include "fixalloc.h"
  28.  
  29. class CTempMenu : public CMenu
  30. {
  31.     DECLARE_DYNCREATE(CTempMenu)
  32.     DECLARE_FIXED_ALLOC(CTempMenu);
  33. };
  34.  
  35. CHandleMap* PASCAL afxMapHMENU(BOOL bCreate)
  36. {
  37.     AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
  38.     if (pState->m_pmapHMENU == NULL && bCreate)
  39.     {
  40.         BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  41. #ifndef _AFX_PORTABLE
  42.         _PNH pnhOldHandler = AfxSetNewHandler(&AfxCriticalNewHandler);
  43. #endif
  44.         pState->m_pmapHMENU = new CHandleMap(RUNTIME_CLASS(CTempMenu),
  45.             offsetof(CMenu, m_hMenu)),
  46.  
  47. #ifndef _AFX_PORTABLE
  48.         AfxSetNewHandler(pnhOldHandler);
  49. #endif
  50.         AfxEnableMemoryTracking(bEnable);
  51.     }
  52.     return pState->m_pmapHMENU;
  53. }
  54.  
  55. CMenu* PASCAL CMenu::FromHandle(HMENU hMenu)
  56. {
  57.     CHandleMap* pMap = afxMapHMENU(TRUE); // create map if not exist
  58.     ASSERT(pMap != NULL);
  59.     CMenu* pMenu = (CMenu*)pMap->FromHandle(hMenu);
  60.     ASSERT(pMenu == NULL || pMenu->m_hMenu == hMenu);
  61.     return pMenu;
  62. }
  63.  
  64. CMenu* PASCAL CMenu::FromHandlePermanent(HMENU hMenu)
  65. {
  66.     CHandleMap* pMap = afxMapHMENU();
  67.     CMenu* pMenu = NULL;
  68.     if (pMap != NULL)
  69.     {
  70.         // only look in the permanent map - does no allocations
  71.         pMenu = (CMenu*)pMap->LookupPermanent(hMenu);
  72.         ASSERT(pMenu == NULL || pMenu->m_hMenu == hMenu);
  73.     }
  74.     return pMenu;
  75. }
  76.  
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CMenu
  79.  
  80. #ifdef _DEBUG
  81. void CMenu::AssertValid() const
  82. {
  83.     CObject::AssertValid();
  84.     ASSERT(m_hMenu == NULL || ::IsMenu(m_hMenu));
  85. }
  86.  
  87. void CMenu::Dump(CDumpContext& dc) const
  88. {
  89.     CObject::Dump(dc);
  90.  
  91.     dc << "m_hMenu = " << (UINT)m_hMenu;
  92.     dc << "\n";
  93. }
  94. #endif
  95.  
  96. BOOL CMenu::Attach(HMENU hMenu)
  97. {
  98.     ASSERT(m_hMenu == NULL);        // only attach once, detach on destroy
  99.     if (hMenu == NULL)
  100.         return FALSE;
  101.     CHandleMap* pMap = afxMapHMENU(TRUE); // create map if not exist
  102.     ASSERT(pMap != NULL);
  103.     pMap->SetPermanent(m_hMenu = hMenu, this);
  104.     return TRUE;
  105. }
  106.  
  107. HMENU CMenu::Detach()
  108. {
  109.     HMENU hMenu;
  110.     if ((hMenu = m_hMenu) != NULL)
  111.     {
  112.         CHandleMap* pMap = afxMapHMENU(); // don't create if not exist
  113.         if (pMap != NULL)
  114.             pMap->RemoveHandle(m_hMenu);
  115.     }
  116.     m_hMenu = NULL;
  117.     return hMenu;
  118. }
  119.  
  120. BOOL CMenu::DestroyMenu()
  121. {
  122.     if (m_hMenu == NULL)
  123.         return FALSE;
  124.     return ::DestroyMenu(Detach());
  125. }
  126.  
  127. /////////////////////////////////////////////////////////////////////////////
  128. // Self-drawing menu items
  129.  
  130. void CMenu::DrawItem(LPDRAWITEMSTRUCT /* lpDrawItemStruct */)
  131. {
  132.     // default drawing does nothing
  133. }
  134.  
  135. void CMenu::MeasureItem(LPMEASUREITEMSTRUCT /* lpMeasureItemStruct */)
  136. {
  137.     // default drawing does nothing
  138. }
  139.  
  140. #ifdef AFX_INIT_SEG
  141. #pragma code_seg(AFX_INIT_SEG)
  142. #endif
  143.  
  144. IMPLEMENT_DYNCREATE(CMenu, CObject)
  145.  
  146. IMPLEMENT_DYNCREATE(CTempMenu, CMenu);
  147.  
  148. #pragma warning(disable: 4074)
  149. #pragma init_seg(compiler)
  150. IMPLEMENT_FIXED_ALLOC(CTempMenu, 64);
  151.  
  152. /////////////////////////////////////////////////////////////////////////////
  153.