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 / chap01 / patron / patron.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  9KB  |  404 lines

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