home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / WINMENU.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  3.5 KB  |  148 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 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. static CHandleMap* afxMapHMENU(BOOL bCreate = FALSE);
  28.  
  29. static CHandleMap* afxMapHMENU(BOOL bCreate)
  30. {
  31.     AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
  32.     if (pState->m_pmapHMENU == NULL && bCreate)
  33.     {
  34.         BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  35. #ifndef _AFX_PORTABLE
  36.         _PNH pnhOldHandler = AfxSetNewHandler(&AfxCriticalNewHandler);
  37. #endif
  38.         pState->m_pmapHMENU = new CHandleMap(RUNTIME_CLASS(CMenu),
  39.             offsetof(CMenu, m_hMenu)),
  40.  
  41. #ifndef _AFX_PORTABLE
  42.         AfxSetNewHandler(pnhOldHandler);
  43. #endif
  44.         AfxEnableMemoryTracking(bEnable);
  45.     }
  46.     return pState->m_pmapHMENU;
  47. }
  48.  
  49. void PASCAL CMenu::DeleteTempMap()
  50. {
  51.     CHandleMap* pMap = afxMapHMENU();
  52.     if (pMap != NULL)
  53.         pMap->DeleteTemp();
  54. }
  55.  
  56. CMenu* PASCAL CMenu::FromHandle(HMENU hMenu)
  57. {
  58.     CHandleMap* pMap = afxMapHMENU(TRUE); // create map if not exist
  59.     ASSERT(pMap != NULL);
  60.     CMenu* pMenu = (CMenu*)pMap->FromHandle(hMenu);
  61.     ASSERT(pMenu == NULL || pMenu->m_hMenu == hMenu);
  62.     return pMenu;
  63. }
  64.  
  65. CMenu* PASCAL CMenu::FromHandlePermanent(HMENU hMenu)
  66. {
  67.     CHandleMap* pMap = afxMapHMENU();
  68.     CMenu* pMenu = NULL;
  69.     if (pMap != NULL)
  70.     {
  71.         // only look in the permanent map - does no allocations
  72.         pMenu = (CMenu*)pMap->LookupPermanent(hMenu);
  73.         ASSERT(pMenu == NULL || pMenu->m_hMenu == hMenu);
  74.     }
  75.     return pMenu;
  76. }
  77.  
  78. /////////////////////////////////////////////////////////////////////////////
  79. // CMenu
  80.  
  81. #ifdef _DEBUG
  82. void CMenu::AssertValid() const
  83. {
  84.     CObject::AssertValid();
  85.     ASSERT(m_hMenu == NULL || ::IsMenu(m_hMenu));
  86. }
  87.  
  88. void CMenu::Dump(CDumpContext& dc) const
  89. {
  90.     CObject::Dump(dc);
  91.  
  92.     dc << "m_hMenu = " << (UINT)m_hMenu;
  93.     dc << "\n";
  94. }
  95. #endif
  96.  
  97. BOOL CMenu::Attach(HMENU hMenu)
  98. {
  99.     ASSERT(m_hMenu == NULL);        // only attach once, detach on destroy
  100.     if (hMenu == NULL)
  101.         return FALSE;
  102.     CHandleMap* pMap = afxMapHMENU(TRUE); // create map if not exist
  103.     ASSERT(pMap != NULL);
  104.     pMap->SetPermanent(m_hMenu = hMenu, this);
  105.     return TRUE;
  106. }
  107.  
  108. HMENU CMenu::Detach()
  109. {
  110.     HMENU hMenu;
  111.     if ((hMenu = m_hMenu) != NULL)
  112.     {
  113.         CHandleMap* pMap = afxMapHMENU(); // don't create if not exist
  114.         if (pMap != NULL)
  115.             pMap->RemoveHandle(m_hMenu);
  116.     }
  117.     m_hMenu = NULL;
  118.     return hMenu;
  119. }
  120.  
  121. BOOL CMenu::DestroyMenu()
  122. {
  123.     if (m_hMenu == NULL)
  124.         return FALSE;
  125.     return ::DestroyMenu(Detach());
  126. }
  127.  
  128. /////////////////////////////////////////////////////////////////////////////
  129. // Self-drawing menu items
  130.  
  131. void CMenu::DrawItem(LPDRAWITEMSTRUCT /* lpDrawItemStruct */)
  132. {
  133.     // default drawing does nothing
  134. }
  135.  
  136. void CMenu::MeasureItem(LPMEASUREITEMSTRUCT /* lpMeasureItemStruct */)
  137. {
  138.     // default drawing does nothing
  139. }
  140.  
  141. #ifdef AFX_INIT_SEG
  142. #pragma code_seg(AFX_INIT_SEG)
  143. #endif
  144.  
  145. IMPLEMENT_DYNCREATE(CMenu, CObject)
  146.  
  147. /////////////////////////////////////////////////////////////////////////////
  148.