home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c05 / owndraw / mainfrm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  5.9 KB  |  225 lines

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "OwnDraw.h"
  6.  
  7. #include "ODrawMnu.h"
  8. #include "BMapMenu.H"
  9.  
  10. #include "ODrawDoc.H"
  11. #include "ODraView.H"
  12.  
  13. #include "MainFrm.h"
  14.  
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20.  
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CMainFrame
  23.  
  24. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  25.  
  26. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  27.     //{{AFX_MSG_MAP(CMainFrame)
  28.         // NOTE - the ClassWizard will add and remove mapping macros here.
  29.         //    DO NOT EDIT what you see in these blocks of generated code !
  30.     ON_WM_CREATE()
  31.     //}}AFX_MSG_MAP
  32.     ON_COMMAND_RANGE(IDR_COLOR_FIRST, IDR_COLOR_LAST, OnColors)
  33.     ON_UPDATE_COMMAND_UI_RANGE(IDR_COLOR_FIRST, IDR_COLOR_LAST, OnUpdateColors)
  34. END_MESSAGE_MAP()
  35.  
  36. void CMainFrame::OnUpdateColors(CCmdUI* pCmdUI)
  37. {
  38.     COwnerDrawMenusDoc * pDoc;
  39.     pDoc = (COwnerDrawMenusDoc *) GetActiveDocument();
  40.     
  41.     // This will set a check mark on the bitmap menu.
  42.     pCmdUI->SetCheck(pDoc->GetColor() == IDtoColorRef(pCmdUI->m_nID));
  43. }
  44.  
  45. static UINT indicators[] =
  46. {
  47.     ID_SEPARATOR,
  48.     ID_INDICATOR_CAPS,
  49.     ID_INDICATOR_NUM,
  50.     ID_INDICATOR_SCRL,
  51. };
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CMainFrame construction/destruction
  55.  
  56. CMainFrame::CMainFrame()
  57. {
  58. }
  59.  
  60. CMainFrame::~CMainFrame()
  61. {
  62. }
  63.  
  64. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  65. {
  66.     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  67.         return -1;
  68.     
  69.     if (!m_wndToolBar.Create(this) ||
  70.         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  71.     {
  72.         TRACE0("Failed to create toolbar\n");
  73.         return -1;      // fail to create
  74.     }
  75.  
  76.     if (!m_wndStatusBar.Create(this) ||
  77.         !m_wndStatusBar.SetIndicators(indicators,
  78.           sizeof(indicators)/sizeof(UINT)))
  79.     {
  80.         TRACE0("Failed to create status bar\n");
  81.         return -1;      // fail to create
  82.     }
  83.  
  84.     m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  85.         CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  86.  
  87.     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  88.     EnableDocking(CBRS_ALIGN_ANY);
  89.     DockControlBar(&m_wndToolBar);
  90.  
  91.     AttachOwnerDrawMenu();
  92.     AttachBitmapMenu();
  93.  
  94.     return 0;
  95. }
  96.  
  97. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  98. {
  99.     return CFrameWnd::PreCreateWindow(cs);
  100. }
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CMainFrame diagnostics
  104.  
  105. #ifdef _DEBUG
  106. void CMainFrame::AssertValid() const
  107. {
  108.     CFrameWnd::AssertValid();
  109. }
  110.  
  111. void CMainFrame::Dump(CDumpContext& dc) const
  112. {
  113.     CFrameWnd::Dump(dc);
  114. }
  115.  
  116. #endif //_DEBUG
  117.  
  118. /////////////////////////////////////////////////////////////////////////////
  119. // CMainFrame message handlers
  120.  
  121. void CMainFrame::AttachBitmapMenu()
  122. {
  123.     // Append 4 menus to the empty menu object m_bitmapMenu
  124.     // (which is an object of a custom class and is a member
  125.     // of the frame class).  Each appended menu has its own bitmap.
  126.     // For simplicity's sake, each bitmap menu item will use
  127.     // the same ID as the corresponding owner draw menus.
  128.     for (int iColor = 0; iColor <= (IDR_COLOR_LAST-IDR_COLOR_FIRST); iColor++)
  129.     {
  130.         // Skip Cyan, which is the 3rd color.
  131.         if (3 == iColor) continue;
  132.         m_bitmapMenu.AppendBitmapMenuItem(IDR_COLOR_FIRST + iColor);
  133.     }
  134.  
  135.     // Replace the specified menu item with a color popup
  136.     //  (note: will only work once)
  137.     CMenu* pMenuBar = GetMenu();
  138.     ASSERT(pMenuBar != NULL);
  139.     
  140.     TCHAR szString[256];
  141.     // Colors is the 5th (4th from 0) menu. We want its string.
  142.     pMenuBar->GetMenuString(4, szString, sizeof(szString),
  143.                             MF_BYPOSITION);
  144.  
  145.     // Make the newly created menu become the 5th menu's popup.
  146.     pMenuBar = GetMenu();
  147.     ASSERT(pMenuBar != NULL);
  148.     pMenuBar->ModifyMenu(4, MF_BYPOSITION | MF_POPUP,
  149.                         (UINT)m_bitmapMenu.m_hMenu, szString);
  150. }
  151.  
  152. void CMainFrame::AttachOwnerDrawMenu()
  153. {
  154.     // Append 4 menus to the empty menu object m_colorMenu
  155.     // (which is an object of a custom class and is a member
  156.     // of the frame class).  Each appended menu is owner drawn.
  157.     for (int iColor = 0; iColor <= (IDR_COLOR_LAST-IDR_COLOR_FIRST); iColor++)
  158.     {
  159.         // Skip Cyan, which is the 3rd color.
  160.         if (3 == iColor) continue;
  161.  
  162.         // 3 bit encoded RGB values
  163.         BYTE red = (BYTE)(((iColor & 4) != 0) * 255);
  164.         BYTE green = (BYTE)(((iColor & 2) != 0) * 255);
  165.         BYTE blue = (BYTE)((iColor & 1) * 255);
  166.     
  167.         // Each appended menu has an RGB value associated with it
  168.         // that will be passed to DrawItem in the itemData variable.
  169.         // See DrawItem.
  170.         m_ownerDrawMenu.AppendMenu(MF_ENABLED | MF_OWNERDRAW, 
  171.                 IDR_COLOR_FIRST + iColor, (LPCTSTR)RGB(red, green, blue));
  172.     }
  173.  
  174.     // Replace the specified menu item with a color popup
  175.     //  (note: will only work once)
  176.     CMenu* pMenuBar = GetMenu();
  177.     ASSERT(pMenuBar != NULL);
  178.     
  179.     TCHAR szString[256];
  180.     // Colors is the 4th (3rd from 0) menu. We want its string.
  181.     pMenuBar->GetMenuString(3, szString, sizeof(szString),
  182.                             MF_BYPOSITION);
  183.  
  184.     // Make the newly created menu become the 4th menu's popup.
  185.     pMenuBar = GetMenu();
  186.     ASSERT(pMenuBar != NULL);
  187.     pMenuBar->ModifyMenu(3, MF_BYPOSITION | MF_POPUP,
  188.                         (UINT)m_ownerDrawMenu.m_hMenu, szString);
  189. }
  190.  
  191. void CMainFrame::OnColors(UINT nID) 
  192. {
  193.     // Change the color, which is stored in the document.
  194.     COwnerDrawMenusDoc * pDoc;
  195.     pDoc = (COwnerDrawMenusDoc *) GetActiveDocument();
  196.     pDoc->SetColor(IDtoColorRef(nID));
  197.  
  198.     // Invalidate the view to force a redraw.
  199.     COwnerDrawMenusView * pView;
  200.     pView = (COwnerDrawMenusView *) GetActiveView();
  201.     pView->Invalidate();
  202. }
  203.  
  204. const COLORREF BLACK=RGB(0,0,0);
  205. const COLORREF RED=RGB(255,0,0);
  206. const COLORREF GREEN=RGB(0,255,0);
  207. const COLORREF BLUE=RGB(0,0,255);
  208.  
  209. // This function converts one of the 4 Command IDs
  210. // to a COLORREF value.
  211. COLORREF CMainFrame::IDtoColorRef(int nID)
  212. {
  213.     switch (nID)
  214.     {
  215.         case IDR_COLOR_RED:
  216.             return RED;
  217.         case IDR_COLOR_GREEN:
  218.             return GREEN;        
  219.         case IDR_COLOR_BLUE:
  220.             return BLUE;
  221.         default:
  222.             return BLACK;        
  223.     }
  224. }
  225.