home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap07 / patron / patron.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  10KB  |  448 lines

  1. /*
  2.  * PATRON.CPP
  3.  * Patron Chapter 7
  4.  *
  5.  * Frame window class for Patron.
  6.  *
  7.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Microsoft
  10.  * Internet  :  kraigb@microsoft.com
  11.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  12.  */
  13.  
  14.  
  15. //CHAPTER7MOD
  16. #define INITGUIDS
  17. //End CHAPTER7MOD
  18. #include "patron.h"
  19.  
  20.  
  21.  
  22. /*
  23.  * WinMain
  24.  *
  25.  * Purpose:
  26.  *  Main entry point of application.  Should register the app class
  27.  *  if a previous instance has not done so and do any other one-time
  28.  *  initializations.
  29.  */
  30.  
  31. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev
  32.     , LPSTR pszCmdLine, int nCmdShow)
  33.     {
  34.     PCPatronFrame   pFR;
  35.     FRAMEINIT       fi;
  36.     WPARAM          wRet=0;
  37.  
  38.     //Attempt to allocate and initialize the application
  39.     pFR=new CPatronFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  40.  
  41.     if (NULL==pFR)
  42.         return -1;
  43.  
  44.     fi.idsMin=IDS_FRAMEMIN;
  45.     fi.idsMax=IDS_FRAMEMAX;
  46.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  47.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  48.     fi.idStatMenuMin=ID_MENUFILE;
  49.     fi.idStatMenuMax=ID_MENUHELP;
  50.     fi.iPosWindowMenu=WINDOW_MENU;
  51.     fi.cMenus=CMENUS;
  52.  
  53.     fi.x=CW_USEDEFAULT;
  54.     fi.y=CW_USEDEFAULT;
  55.     fi.cx=CW_USEDEFAULT;
  56.     fi.cy=CW_USEDEFAULT;
  57.  
  58.     //If we can initialize pFR, start chugging messages
  59.     if (pFR->Init(&fi))
  60.         wRet=pFR->MessageLoop();
  61.  
  62.     delete pFR;
  63.     return wRet;
  64.     }
  65.  
  66.  
  67.  
  68.  
  69. /*
  70.  * CPatronFrame::CPatronFrame
  71.  * CPatronFrame::~CPatronFrame
  72.  *
  73.  * Constructor Parameters:
  74.  *  hInst           HINSTANCE from WinMain
  75.  *  hInstPrev       HINSTANCE from WinMain
  76.  *  pszCmdLine      LPSTR from WinMain
  77.  *  nCmdShow        int from WInMain
  78.  */
  79.  
  80. CPatronFrame::CPatronFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  81.     , LPSTR pszCmdLine, int nCmdShow)
  82.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  83.     {
  84.     //CHAPTER7MOD
  85.     m_fInitialized=FALSE;
  86.     //End CHAPTER7MOD
  87.     return;
  88.     }
  89.  
  90.  
  91. CPatronFrame::~CPatronFrame(void)
  92.     {
  93.     //CHAPTER7MOD
  94.     if (m_fInitialized)
  95.         OleUninitialize();
  96.     //End CHAPTER7MOD
  97.     return;
  98.     }
  99.  
  100.  
  101.  
  102.  
  103. //CHAPTER7MOD
  104. /*
  105.  * CPatronFrame::Init
  106.  *
  107.  * Purpose:
  108.  *  Call OleInitialize then calling down into the base class
  109.  *  initialization.
  110.  *
  111.  * Parameters:
  112.  *  pFI             PFRAMEINIT containing initialization
  113.  *                  parameters.
  114.  *
  115.  * Return Value:
  116.  *  BOOL            TRUE if initialization succeeded,
  117.  *                  FALSE otherwise.
  118.  */
  119.  
  120. BOOL CPatronFrame::Init(PFRAMEINIT pFI)
  121.     {
  122.     CHECKVER_OLE;
  123.  
  124.     if (FAILED(OleInitialize(NULL)))
  125.         return FALSE;
  126.  
  127.     m_fInitialized=TRUE;
  128.  
  129.     return CFrame::Init(pFI);
  130.     }
  131. //End CHAPTER7MOD
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. /*
  139.  * CPatronFrame::CreateCClient
  140.  *
  141.  * Purpose:
  142.  *  Constructs a new client specific to the application.
  143.  *
  144.  * Parameters:
  145.  *  None
  146.  *
  147.  * Return Value:
  148.  *  PCClient        Pointer to the new client object.
  149.  */
  150.  
  151. PCClient CPatronFrame::CreateCClient(void)
  152.     {
  153.     return (PCClient)(new CPatronClient(m_hInst, this));
  154.     }
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161. /*
  162.  * CPatronFrame::RegisterAllClasses
  163.  *
  164.  * Purpose:
  165.  *  Registers all classes used in this application.
  166.  *
  167.  * Parameters:
  168.  *  None
  169.  *
  170.  * Return Value:
  171.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  172.  */
  173.  
  174. BOOL CPatronFrame::RegisterAllClasses(void)
  175.     {
  176.     WNDCLASS        wc;
  177.  
  178.     //First let the standard frame do its thing
  179.     if (!CFrame::RegisterAllClasses())
  180.         return FALSE;
  181.  
  182.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  183.     wc.hInstance     = m_hInst;
  184.     wc.cbClsExtra    = 0;
  185.     wc.lpfnWndProc   = PagesWndProc;
  186.     wc.cbWndExtra    = CBPAGESWNDEXTRA;
  187.     wc.hIcon         = NULL;
  188.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  189.     wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
  190.     wc.lpszMenuName  = NULL;
  191.     wc.lpszClassName = SZCLASSPAGES;
  192.  
  193.     if (!RegisterClass(&wc))
  194.         return FALSE;
  195.  
  196.     return TRUE;
  197.     }
  198.  
  199.  
  200.  
  201.  
  202.  
  203. /*
  204.  * CPatronFrame::OnCommand
  205.  *
  206.  * Purpose:
  207.  *  WM_COMMAND handler for the Patron frame window that processes
  208.  *  extra File menu items as well as the Page menu.
  209.  *
  210.  * Parameters:
  211.  *  hWnd            HWND of the frame window.
  212.  *  wParam          WPARAM of the message.
  213.  *  lParam          LPARAM of the message.
  214.  *
  215.  * Return Value:
  216.  *  LRESULT         Return value for the message.
  217.  */
  218.  
  219. LRESULT CPatronFrame::OnCommand(HWND hWnd, WPARAM wParam
  220.     , LPARAM lParam)
  221.     {
  222.     PCPatronDoc     pDoc;
  223.  
  224.     COMMANDPARAMS(wID, wCode, hWndMsg);
  225.  
  226.     /*
  227.      * Don't bother with anything during first initialization,
  228.      * skipping many toolbar notifications.
  229.      */
  230.     if (m_fInit)
  231.         return 0L;
  232.  
  233.     pDoc=(PCPatronDoc)m_pCL->ActiveDocument();
  234.  
  235.     switch (wID)
  236.         {
  237.         case IDM_FILEPRINT:
  238.             pDoc->Print(m_hWnd);
  239.             return 0L;
  240.  
  241.         case IDM_FILEPRINTERSETUP:
  242.             pDoc->PrinterSetup(m_hWnd, FALSE);
  243.             return 0L;
  244.  
  245.  
  246.         case IDM_PAGENEWPAGE:
  247.             pDoc->NewPage();
  248.             break;
  249.  
  250.         case IDM_PAGEDELETEPAGE:
  251.             pDoc->DeletePage();
  252.             break;
  253.  
  254.         case IDM_PAGENEXTPAGE:
  255.             pDoc->NextPage();
  256.             break;
  257.  
  258.         case IDM_PAGEPREVIOUSPAGE:
  259.             pDoc->PreviousPage();
  260.             break;
  261.  
  262.         case IDM_PAGEFIRSTPAGE:
  263.             pDoc->FirstPage();
  264.             break;
  265.  
  266.         case IDM_PAGELASTPAGE:
  267.             pDoc->LastPage();
  268.             break;
  269.  
  270.  
  271.         default:
  272.            return CFrame::OnCommand(hWnd, wParam, lParam);
  273.         }
  274.  
  275.     return 0L;
  276.     }
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285. /*
  286.  * CPatronFrame::CreateToolbar
  287.  *
  288.  * Purpose:
  289.  *  Procedure to create all the necessary toolbar buttons.
  290.  *
  291.  * Parameters:
  292.  *  None
  293.  *
  294.  * Return Value:
  295.  *  UINT            Number of tools added to the bar.
  296.  */
  297.  
  298. UINT CPatronFrame::CreateToolbar(void)
  299.     {
  300.     UINT            iLast;
  301.     UINT            uState=GIZMO_NORMAL;
  302.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  303.  
  304.     //Insert the standard ones.
  305.     iLast=CFrame::CreateToolbar();
  306.  
  307.     //Remove Undo:  we don't use it.
  308.     m_pTB->Remove(IDM_EDITUNDO);
  309.  
  310.     /*
  311.      * Insert Print File Import in the 5th position and account
  312.      * for it in iLast.
  313.      */
  314.     m_pTB->Add(utCmd, 4, IDM_FILEPRINT, m_dxB, m_dyB
  315.         , NULL, NULL, 6, uState);
  316.  
  317.     iLast++;
  318.  
  319.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  320.         , NULL, NULL, 0, uState);
  321.  
  322.     //Add New Page, and Delete Page
  323.     m_pTB->Add(utCmd, iLast++, IDM_PAGENEWPAGE, m_dxB, m_dyB
  324.         , NULL, m_hBmp, 2, uState);
  325.     m_pTB->Add(utCmd, iLast++, IDM_PAGEDELETEPAGE, m_dxB, m_dyB
  326.         , NULL, m_hBmp, 3, uState);
  327.  
  328.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  329.         , NULL, NULL, 0, uState);
  330.  
  331.     //First, Prev, Next, Last pages.
  332.     m_pTB->Add(utCmd, iLast++, IDM_PAGEFIRSTPAGE, m_dxB, m_dyB
  333.         , NULL, m_hBmp, 4, uState);
  334.     m_pTB->Add(utCmd, iLast++, IDM_PAGEPREVIOUSPAGE, m_dxB, m_dyB
  335.         , NULL, m_hBmp, 5, uState);
  336.     m_pTB->Add(utCmd, iLast++, IDM_PAGENEXTPAGE, m_dxB, m_dyB
  337.         , NULL, m_hBmp, 6, uState);
  338.     m_pTB->Add(utCmd, iLast++, IDM_PAGELASTPAGE, m_dxB, m_dyB
  339.         , NULL, m_hBmp, 7, uState);
  340.  
  341.     return iLast;
  342.     }
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350. /*
  351.  * CPatronFrame::UpdateMenus
  352.  *
  353.  * Purpose:
  354.  *  Handles the WM_INITMENU message for the frame window.  Depending
  355.  *  on the existence of an active window, menu items are selectively
  356.  *  enabled and disabled.
  357.  *
  358.  * Parameters:
  359.  *  hMenu           HMENU of the menu to intialize
  360.  *  iMenu           UINT position of the menu.
  361.  *
  362.  * Return Value:
  363.  *  None
  364.  */
  365.  
  366. void CPatronFrame::UpdateMenus(HMENU hMenu, UINT iMenu)
  367.     {
  368.     PCPatronDoc     pDoc;
  369.     BOOL            fOK=FALSE;
  370.     BOOL            fCallDefault=TRUE;
  371.     UINT            uTemp;
  372.     UINT            uTempE;
  373.     UINT            uTempD;
  374.  
  375.     pDoc=(PCPatronDoc)m_pCL->ActiveDocument();
  376.  
  377.     uTempE=MF_ENABLED | MF_BYCOMMAND;
  378.     uTempD=MF_DISABLED | MF_GRAYED | MF_BYCOMMAND;
  379.     uTemp=((NULL!=pDoc) ? uTempE : uTempD);
  380.  
  381.     //File menu
  382.     if (m_phMenu[0]==hMenu)
  383.         {
  384.         EnableMenuItem(hMenu, IDM_FILEPRINT, uTemp);
  385.         EnableMenuItem(hMenu, IDM_FILEPRINTERSETUP, uTemp);
  386.         }
  387.  
  388.     //Page menu
  389.     if (m_phMenu[2]==hMenu)
  390.         {
  391.         EnableMenuItem(hMenu, IDM_PAGENEWPAGE,      uTemp);
  392.         EnableMenuItem(hMenu, IDM_PAGEDELETEPAGE,   uTemp);
  393.         EnableMenuItem(hMenu, IDM_PAGENEXTPAGE,     uTemp);
  394.         EnableMenuItem(hMenu, IDM_PAGEPREVIOUSPAGE, uTemp);
  395.         EnableMenuItem(hMenu, IDM_PAGEFIRSTPAGE,    uTemp);
  396.         EnableMenuItem(hMenu, IDM_PAGELASTPAGE,     uTemp);
  397.         }
  398.  
  399.     if (fCallDefault)
  400.         CFrame::UpdateMenus(hMenu, iMenu);
  401.  
  402.     return;
  403.     }
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410. /*
  411.  * CPatronFrame::UpdateToolbar
  412.  *
  413.  * Purpose:
  414.  *  Enables and disables tools depending on whether we have
  415.  *  a document or not.
  416.  *
  417.  * Parameters:
  418.  *  None
  419.  *
  420.  * Return Value:
  421.  *  None
  422.  */
  423.  
  424. void CPatronFrame::UpdateToolbar(void)
  425.     {
  426.     PCDocument  pDoc;
  427.     BOOL        fEnable;
  428.  
  429.     //Let the default hack on its tools.
  430.     CFrame::UpdateToolbar();
  431.  
  432.     pDoc=m_pCL->ActiveDocument();
  433.     fEnable=(NULL!=pDoc);
  434.  
  435.     //No document, disable just about everything
  436.     m_pTB->Enable(IDM_FILEPRINT,        fEnable);
  437.     m_pTB->Enable(IDM_FILEPRINTERSETUP, fEnable);
  438.  
  439.     m_pTB->Enable(IDM_PAGENEWPAGE,      fEnable);
  440.     m_pTB->Enable(IDM_PAGEDELETEPAGE,   fEnable);
  441.     m_pTB->Enable(IDM_PAGEFIRSTPAGE,    fEnable);
  442.     m_pTB->Enable(IDM_PAGEPREVIOUSPAGE, fEnable);
  443.     m_pTB->Enable(IDM_PAGENEXTPAGE,     fEnable);
  444.     m_pTB->Enable(IDM_PAGELASTPAGE,     fEnable);
  445.  
  446.     return;
  447.     }
  448.