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 / chap17 / patron / patron.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  13.2 KB  |  587 lines

  1. /*
  2.  * PATRON.CPP
  3.  * Patron Chapter 17
  4.  *
  5.  * WinMain which is all we need for the basic application.
  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. #define INITGUIDS
  16. #include "patron.h"
  17.  
  18.  
  19. /*
  20.  * WinMain
  21.  *
  22.  * Purpose:
  23.  *  Main entry point of application.  Should register the app class
  24.  *  if a previous instance has not done so and do any other one-time
  25.  *  initializations.
  26.  */
  27.  
  28. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev
  29.     , LPSTR pszCmdLine, int nCmdShow)
  30.     {
  31.     PCPatronFrame   pFR;
  32.     FRAMEINIT       fi;
  33.     WPARAM          wRet=0;
  34.  
  35.     SETMESSAGEQUEUE;
  36.  
  37.     //Attempt to allocate and initialize the application
  38.     pFR=new CPatronFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  39.  
  40.     if (NULL==pFR)
  41.         return -1;
  42.  
  43.     fi.idsMin=IDS_FRAMEMIN;
  44.     fi.idsMax=IDS_FRAMEMAX;
  45.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  46.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  47.     fi.idStatMenuMin=ID_MENUFILE;
  48.     fi.idStatMenuMax=ID_MENUHELP;
  49.     fi.iPosWindowMenu=WINDOW_MENU;
  50.     fi.cMenus=CMENUS;
  51.  
  52.     fi.x=CW_USEDEFAULT;
  53.     fi.y=CW_USEDEFAULT;
  54.     fi.cx=CW_USEDEFAULT;
  55.     fi.cy=CW_USEDEFAULT;
  56.  
  57.     //If we can initialize pFR, start chugging messages
  58.     if (pFR->Init(&fi))
  59.         wRet=pFR->MessageLoop();
  60.  
  61.     delete pFR;
  62.     return wRet;
  63.     }
  64.  
  65.  
  66.  
  67.  
  68. /*
  69.  * CPatronFrame::CPatronFrame
  70.  * CPatronFrame::~CPatronFrame
  71.  *
  72.  * Constructor Parameters:
  73.  *  hInst           HINSTANCE from WinMain
  74.  *  hInstPrev       HINSTANCE from WinMain
  75.  *  pszCmdLine      LPSTR from WinMain
  76.  *  nCmdShow        int from WInMain
  77.  */
  78.  
  79. CPatronFrame::CPatronFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  80.     , LPSTR pszCmdLine, int nCmdShow)
  81.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  82.     {
  83.     m_fInitialized=FALSE;
  84.     m_pIClassDataTran=NULL;
  85.     return;
  86.     }
  87.  
  88.  
  89. CPatronFrame::~CPatronFrame(void)
  90.     {
  91.     if (NULL!=m_pIClassDataTran)
  92.         {
  93.         m_pIClassDataTran->LockServer(FALSE);
  94.         m_pIClassDataTran->Release();
  95.         }
  96.  
  97.     OleFlushClipboard();
  98.  
  99.     if (m_fInitialized)
  100.         OleUninitialize();
  101.     return;
  102.     }
  103.  
  104.  
  105.  
  106.  
  107. /*
  108.  * CPatronFrame::Init
  109.  *
  110.  * Purpose:
  111.  *  Call OleInitialize then calling down into the base class
  112.  *  initialization.
  113.  *
  114.  * Parameters:
  115.  *  pFI             PFRAMEINIT containing initialization
  116.  *                  parameters.
  117.  *
  118.  * Return Value:
  119.  *  BOOL            TRUE if initialization succeeded,
  120.  *                  FALSE otherwise.
  121.  */
  122.  
  123. BOOL CPatronFrame::Init(PFRAMEINIT pFI)
  124.     {
  125.     HRESULT     hr;
  126.  
  127.     CHECKVER_OLE;
  128.  
  129.     if (FAILED(OleInitialize(NULL)))
  130.         return FALSE;
  131.  
  132.     m_fInitialized=TRUE;
  133.  
  134.     hr=CoGetClassObject(CLSID_DataTransferObject
  135.         , CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory
  136.         , (PPVOID)&m_pIClassDataTran);
  137.  
  138.     if (SUCCEEDED(hr))
  139.         m_pIClassDataTran->LockServer(TRUE);
  140.  
  141.     return CFrame::Init(pFI);
  142.     }
  143.  
  144.  
  145.  
  146.  
  147.  
  148. /*
  149.  * CPatronFrame::CreateCClient
  150.  *
  151.  * Purpose:
  152.  *  Constructs a new client specific to the application.
  153.  *
  154.  * Parameters:
  155.  *  None
  156.  *
  157.  * Return Value:
  158.  *  PCClient        Pointer to the new client object.
  159.  */
  160.  
  161. PCClient CPatronFrame::CreateCClient(void)
  162.     {
  163.     return (PCClient)(new CPatronClient(m_hInst, this));
  164.     }
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. /*
  172.  * CPatronFrame::RegisterAllClasses
  173.  *
  174.  * Purpose:
  175.  *  Registers all classes used in this application.
  176.  *
  177.  * Parameters:
  178.  *  None
  179.  *
  180.  * Return Value:
  181.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  182.  */
  183.  
  184. BOOL CPatronFrame::RegisterAllClasses(void)
  185.     {
  186.     WNDCLASS        wc;
  187.  
  188.     //First let the standard frame do its thing
  189.     if (!CFrame::RegisterAllClasses())
  190.         return FALSE;
  191.  
  192.     //We need double-clicks now and for object activation.
  193.     wc.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  194.     wc.hInstance     = m_hInst;
  195.     wc.cbClsExtra    = 0;
  196.     wc.lpfnWndProc   = PagesWndProc;
  197.     wc.cbWndExtra    = CBPAGESWNDEXTRA;
  198.     wc.hIcon         = NULL;
  199.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  200.     wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
  201.     wc.lpszMenuName  = NULL;
  202.     wc.lpszClassName = SZCLASSPAGES;
  203.  
  204.     if (!RegisterClass(&wc))
  205.         return FALSE;
  206.  
  207.     return TRUE;
  208.     }
  209.  
  210.  
  211.  
  212.  
  213.  
  214. /*
  215.  * CPatronFrame::OnCommand
  216.  *
  217.  * Purpose:
  218.  *  WM_COMMAND handler for the Patron frame window that processes
  219.  *  extra File menu items as well as the Page menu.
  220.  *
  221.  * Parameters:
  222.  *  hWnd            HWND of the frame window.
  223.  *  wParam          WPARAM of the message.
  224.  *  lParam          LPARAM of the message.
  225.  *
  226.  * Return Value:
  227.  *  LRESULT         Return value for the message.
  228.  */
  229.  
  230. LRESULT CPatronFrame::OnCommand(HWND hWnd, WPARAM wParam
  231.     , LPARAM lParam)
  232.     {
  233.     PCPatronDoc     pDoc;
  234.  
  235.     COMMANDPARAMS(wID, wCode, hWndMsg);
  236.  
  237.     /*
  238.      * Don't bother with anything during first initialization,
  239.      * skipping many toolbar notifications.
  240.      */
  241.     if (m_fInit)
  242.         return 0L;
  243.  
  244.     pDoc=(PCPatronDoc)m_pCL->ActiveDocument();
  245.  
  246.     //CHAPTER17MOD
  247.     if (NULL!=pDoc && (IDM_VERBMIN <= wID) && (IDM_VERBMAX >= wID))
  248.         {
  249.         pDoc->ActivateObject(wID-IDM_VERBMIN);
  250.         return 0L;
  251.         }
  252.     //End CHAPTER17MOD
  253.  
  254.     switch (wID)
  255.         {
  256.         case IDM_FILEPRINT:
  257.             pDoc->Print(m_hWnd);
  258.             return 0L;
  259.  
  260.         case IDM_FILEPRINTERSETUP:
  261.             pDoc->PrinterSetup(m_hWnd, FALSE);
  262.             return 0L;
  263.  
  264.         case IDM_EDITPASTESPECIAL:
  265.             pDoc->PasteSpecial(m_hWnd);
  266.             return 0L;
  267.  
  268.         case IDM_EDITDELETEOBJECT:
  269.             pDoc->Delete();
  270.             return 0L;
  271.  
  272.         //CHAPTER17MOD
  273.         case IDM_EDITINSERTOBJECT:
  274.             pDoc->InsertObject(m_hWnd);
  275.             return 0L;
  276.  
  277.         case IDM_EDITCONVERT:
  278.             pDoc->ConvertObject(m_hWnd);
  279.             return 0L;
  280.         //End CHAPTER17MOD
  281.  
  282.         case IDM_PAGENEWPAGE:
  283.             pDoc->NewPage();
  284.             break;
  285.  
  286.         case IDM_PAGEDELETEPAGE:
  287.             pDoc->DeletePage();
  288.             break;
  289.  
  290.         case IDM_PAGENEXTPAGE:
  291.             pDoc->NextPage();
  292.             break;
  293.  
  294.         case IDM_PAGEPREVIOUSPAGE:
  295.             pDoc->PreviousPage();
  296.             break;
  297.  
  298.         case IDM_PAGEFIRSTPAGE:
  299.             pDoc->FirstPage();
  300.             break;
  301.  
  302.         case IDM_PAGELASTPAGE:
  303.             pDoc->LastPage();
  304.             break;
  305.  
  306.  
  307.         default:
  308.            return CFrame::OnCommand(hWnd, wParam, lParam);
  309.         }
  310.  
  311.     return 0L;
  312.     }
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321. /*
  322.  * CPatronFrame::CreateToolbar
  323.  *
  324.  * Purpose:
  325.  *  Procedure to create all the necessary toolbar buttons.
  326.  *
  327.  * Parameters:
  328.  *  None
  329.  *
  330.  * Return Value:
  331.  *  UINT            Number of tools added to the bar.
  332.  */
  333.  
  334. UINT CPatronFrame::CreateToolbar(void)
  335.     {
  336.     UINT            iLast;
  337.     UINT            uState=GIZMO_NORMAL;
  338.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  339.  
  340.     //Insert the standard ones.
  341.     iLast=CFrame::CreateToolbar();
  342.  
  343.     //Remove Undo:  we don't use it.
  344.     m_pTB->Remove(IDM_EDITUNDO);
  345.  
  346.     /*
  347.      * Insert Print File Import in the 5th position and account
  348.      * for it in iLast.
  349.      */
  350.     m_pTB->Add(utCmd, 4, IDM_FILEPRINT, m_dxB, m_dyB
  351.         , NULL, NULL, 6, uState);
  352.  
  353.     iLast++;
  354.  
  355.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  356.         , NULL, NULL, 0, uState);
  357.  
  358.     //Add New Page, and Delete Page
  359.     m_pTB->Add(utCmd, iLast++, IDM_PAGENEWPAGE, m_dxB, m_dyB
  360.         , NULL, m_hBmp, 2, uState);
  361.     m_pTB->Add(utCmd, iLast++, IDM_PAGEDELETEPAGE, m_dxB, m_dyB
  362.         , NULL, m_hBmp, 3, uState);
  363.  
  364.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  365.         , NULL, NULL, 0, uState);
  366.  
  367.     //First, Prev, Next, Last pages.
  368.     m_pTB->Add(utCmd, iLast++, IDM_PAGEFIRSTPAGE, m_dxB, m_dyB
  369.         , NULL, m_hBmp, 4, uState);
  370.     m_pTB->Add(utCmd, iLast++, IDM_PAGEPREVIOUSPAGE, m_dxB, m_dyB
  371.         , NULL, m_hBmp, 5, uState);
  372.     m_pTB->Add(utCmd, iLast++, IDM_PAGENEXTPAGE, m_dxB, m_dyB
  373.         , NULL, m_hBmp, 6, uState);
  374.     m_pTB->Add(utCmd, iLast++, IDM_PAGELASTPAGE, m_dxB, m_dyB
  375.         , NULL, m_hBmp, 7, uState);
  376.  
  377.     return iLast;
  378.     }
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386. /*
  387.  * CPatronFrame::UpdateMenus
  388.  *
  389.  * Purpose:
  390.  *  Handles the WM_INITMENU message for the frame window.  Depending
  391.  *  on the existence of an active window, menu items are selectively
  392.  *  enabled and disabled.
  393.  *
  394.  * Parameters:
  395.  *  hMenu           HMENU of the menu to intialize
  396.  *  iMenu           UINT position of the menu.
  397.  *
  398.  * Return Value:
  399.  *  None
  400.  */
  401.  
  402. void CPatronFrame::UpdateMenus(HMENU hMenu, UINT iMenu)
  403.     {
  404.     PCPatronDoc     pDoc;
  405.     BOOL            fOK=FALSE;
  406.     BOOL            fCallDefault=TRUE;
  407.     UINT            uTemp;
  408.     UINT            uTempE;
  409.     UINT            uTempD;
  410.  
  411.     pDoc=(PCPatronDoc)m_pCL->ActiveDocument();
  412.  
  413.     uTempE=MF_ENABLED | MF_BYCOMMAND;
  414.     uTempD=MF_DISABLED | MF_GRAYED | MF_BYCOMMAND;
  415.     uTemp=((NULL!=pDoc) ? uTempE : uTempD);
  416.  
  417.     if (m_phMenu[0]==hMenu)
  418.         {
  419.         EnableMenuItem(hMenu, IDM_FILEPRINT, uTemp);
  420.  
  421.         if (NULL!=pDoc)
  422.             fOK=pDoc->FQueryPrinterSetup();
  423.  
  424.         EnableMenuItem(hMenu, IDM_FILEPRINTERSETUP
  425.             , (fOK) ? uTempE : uTempD);
  426.         }
  427.  
  428.     if (m_phMenu[1]==hMenu)
  429.         {
  430.         if (NULL!=pDoc)
  431.             fOK=pDoc->FQueryPaste();
  432.  
  433.         EnableMenuItem(hMenu, IDM_EDITPASTE
  434.             , (fOK) ? uTempE : uTempD);
  435.         EnableMenuItem(hMenu, IDM_EDITPASTESPECIAL
  436.             , (fOK) ? uTempE : uTempD);
  437.  
  438.         //Cut, Copy, Delete depends on there being a selection.
  439.         if (NULL!=pDoc)
  440.             fOK=pDoc->FQueryObjectSelected(hMenu);
  441.         else
  442.             fOK=FALSE;
  443.  
  444.         EnableMenuItem(hMenu, IDM_EDITCUT, (fOK) ? uTempE : uTempD);
  445.         EnableMenuItem(hMenu, IDM_EDITCOPY
  446.             , (fOK) ? uTempE : uTempD);
  447.         EnableMenuItem(hMenu, IDM_EDITDELETEOBJECT
  448.             , (fOK) ? uTempE : uTempD);
  449.  
  450.         //CHAPTER17MOD
  451.         EnableMenuItem(hMenu, IDM_EDITINSERTOBJECT, uTemp);
  452.         //End CHAPTER17MOD
  453.  
  454.         //We did the whole menu...
  455.         fCallDefault=FALSE;
  456.         }
  457.  
  458.     //Page menu
  459.     if (m_phMenu[2]==hMenu)
  460.         {
  461.         EnableMenuItem(hMenu, IDM_PAGENEWPAGE,      uTemp);
  462.         EnableMenuItem(hMenu, IDM_PAGEDELETEPAGE,   uTemp);
  463.         EnableMenuItem(hMenu, IDM_PAGENEXTPAGE,     uTemp);
  464.         EnableMenuItem(hMenu, IDM_PAGEPREVIOUSPAGE, uTemp);
  465.         EnableMenuItem(hMenu, IDM_PAGEFIRSTPAGE,    uTemp);
  466.         EnableMenuItem(hMenu, IDM_PAGELASTPAGE,     uTemp);
  467.         }
  468.  
  469.     if (fCallDefault)
  470.         CFrame::UpdateMenus(hMenu, iMenu);
  471.  
  472.     return;
  473.     }
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480. /*
  481.  * CPatronFrame::UpdateToolbar
  482.  *
  483.  * Purpose:
  484.  *  Enables and disables tools depending on whether we have
  485.  *  a document or not.
  486.  *
  487.  * Parameters:
  488.  *  None
  489.  *
  490.  * Return Value:
  491.  *  None
  492.  */
  493.  
  494. void CPatronFrame::UpdateToolbar(void)
  495.     {
  496.     PCDocument  pDoc;
  497.     BOOL        fEnable;
  498.  
  499.     //Let the default hack on its tools.
  500.     CFrame::UpdateToolbar();
  501.  
  502.     pDoc=m_pCL->ActiveDocument();
  503.     fEnable=(NULL!=pDoc);
  504.  
  505.     //No document, disable just about everything
  506.     m_pTB->Enable(IDM_FILEPRINT,        fEnable);
  507.     m_pTB->Enable(IDM_FILEPRINTERSETUP, fEnable);
  508.  
  509.     m_pTB->Enable(IDM_PAGENEWPAGE,      fEnable);
  510.     m_pTB->Enable(IDM_PAGEDELETEPAGE,   fEnable);
  511.     m_pTB->Enable(IDM_PAGEFIRSTPAGE,    fEnable);
  512.     m_pTB->Enable(IDM_PAGEPREVIOUSPAGE, fEnable);
  513.     m_pTB->Enable(IDM_PAGENEXTPAGE,     fEnable);
  514.     m_pTB->Enable(IDM_PAGELASTPAGE,     fEnable);
  515.  
  516.     return;
  517.     }
  518.  
  519.  
  520.  
  521.  
  522. //CHAPTER17MOD
  523. /*
  524.  * CPatronFrame::FMessageHook
  525.  *
  526.  * Purpose:
  527.  *  Override of CFrame::FMessageHook so we can specifically trap
  528.  *  WM_MENUSELECT messages for the Object verb menu to provide some
  529.  *  meaningful information on the status strip.
  530.  *
  531.  * Parameters:
  532.  *  <WndProc Parameters>
  533.  *  pLRes           LRESULT * in which to store the return value
  534.  *                  for the message.
  535.  *
  536.  * Return Value:
  537.  *  BOOL            TRUE to prevent further processing,
  538.  *                  FALSE otherwise.
  539.  */
  540.  
  541. BOOL CPatronFrame::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
  542.     , LPARAM lParam, LRESULT *pLRes)
  543.     {
  544.     BOOL        fRet=FALSE;
  545.  
  546.     *pLRes=0;
  547.  
  548.     MENUSELECTPARAMS(wItem, wMenuFlags, hMenu);
  549.  
  550.     //If this is the wrong message, nothing to do.
  551.     if (WM_MENUSELECT!=iMsg)
  552.         return FALSE;
  553.  
  554.     //This happens when there's no menu selection.
  555.     if (-1==wMenuFlags)
  556.         return FALSE;
  557.  
  558.     if (MF_POPUP & wMenuFlags)
  559.         {
  560.         /*
  561.          * If this is the cascade verb menu itself, display the same
  562.          * message.  m_phMenu[1] contains the current edit menu
  563.          * handle.
  564.          */
  565.         if (0!=wItem)
  566.             {
  567.             fRet=((HMENU)wItem==GetSubMenu(m_phMenu[1]
  568.                 , MENUPOS_OBJECT));
  569.             }
  570.         }
  571.     else
  572.         {
  573.         /*
  574.          * If the ID is in the verb range, use
  575.          * IDS_ITEMMESSAGEEDITOBJECT message
  576.          */
  577.         fRet=(IDM_VERBMIN <= wItem && IDM_VERBMAX >= wItem);
  578.         }
  579.  
  580.     if (fRet)
  581.         m_pSL->MessageDisplay(IDM_EDITOBJECT);
  582.  
  583.     return fRet;
  584.     }
  585.  
  586. //End CHAPTER17MOD
  587.