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

  1. /*
  2.  * PATRON.CPP
  3.  * Patron Chapter 20
  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.  
  102.     return;
  103.     }
  104.  
  105.  
  106.  
  107.  
  108. /*
  109.  * CPatronFrame::Init
  110.  *
  111.  * Purpose:
  112.  *  Call OleInitialize then calling down into the base class
  113.  *  initialization.
  114.  *
  115.  * Parameters:
  116.  *  pFI             PFRAMEINIT containing initialization
  117.  *                  parameters.
  118.  *
  119.  * Return Value:
  120.  *  BOOL            TRUE if initialization succeeded,
  121.  *                  FALSE otherwise.
  122.  */
  123.  
  124. BOOL CPatronFrame::Init(PFRAMEINIT pFI)
  125.     {
  126.     HRESULT     hr;
  127.  
  128.     CHECKVER_OLE;
  129.  
  130.     if (FAILED(OleInitialize(NULL)))
  131.         return FALSE;
  132.  
  133.     m_fInitialized=TRUE;
  134.  
  135.     //Lock the data transfer object factory as an optimization.
  136.     hr=CoGetClassObject(CLSID_DataTransferObject
  137.         , CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory
  138.         , (PPVOID)&m_pIClassDataTran);
  139.  
  140.     if (SUCCEEDED(hr))
  141.         m_pIClassDataTran->LockServer(TRUE);
  142.  
  143.     return CFrame::Init(pFI);
  144.     }
  145.  
  146.  
  147.  
  148.  
  149.  
  150. /*
  151.  * CPatronFrame::CreateCClient
  152.  *
  153.  * Purpose:
  154.  *  Constructs a new client specific to the application.
  155.  *
  156.  * Parameters:
  157.  *  None
  158.  *
  159.  * Return Value:
  160.  *  PCClient        Pointer to the new client object.
  161.  */
  162.  
  163. PCClient CPatronFrame::CreateCClient(void)
  164.     {
  165.     return (PCClient)(new CPatronClient(m_hInst, this));
  166.     }
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. /*
  174.  * CPatronFrame::RegisterAllClasses
  175.  *
  176.  * Purpose:
  177.  *  Registers all classes used in this application.
  178.  *
  179.  * Parameters:
  180.  *  None
  181.  *
  182.  * Return Value:
  183.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  184.  */
  185.  
  186. BOOL CPatronFrame::RegisterAllClasses(void)
  187.     {
  188.     WNDCLASS        wc;
  189.  
  190.     //First let the standard frame do its thing
  191.     if (!CFrame::RegisterAllClasses())
  192.         return FALSE;
  193.  
  194.     //We need double-clicks now and for object activation.
  195.     wc.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  196.     wc.hInstance     = m_hInst;
  197.     wc.cbClsExtra    = 0;
  198.     wc.lpfnWndProc   = PagesWndProc;
  199.     wc.cbWndExtra    = CBPAGESWNDEXTRA;
  200.     wc.hIcon         = NULL;
  201.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  202.     wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
  203.     wc.lpszMenuName  = NULL;
  204.     wc.lpszClassName = SZCLASSPAGES;
  205.  
  206.     if (!RegisterClass(&wc))
  207.         return FALSE;
  208.  
  209.     return TRUE;
  210.     }
  211.  
  212.  
  213.  
  214.  
  215.  
  216. /*
  217.  * CPatronFrame::OnCommand
  218.  *
  219.  * Purpose:
  220.  *  WM_COMMAND handler for the Patron frame window that processes
  221.  *  extra File menu items as well as the Page menu.
  222.  *
  223.  * Parameters:
  224.  *  hWnd            HWND of the frame window.
  225.  *  wParam          WPARAM of the message.
  226.  *  lParam          LPARAM of the message.
  227.  *
  228.  * Return Value:
  229.  *  LRESULT         Return value for the message.
  230.  */
  231.  
  232. LRESULT CPatronFrame::OnCommand(HWND hWnd, WPARAM wParam
  233.     , LPARAM lParam)
  234.     {
  235.     PCPatronDoc     pDoc;
  236.  
  237.     COMMANDPARAMS(wID, wCode, hWndMsg);
  238.  
  239.     /*
  240.      * Don't bother with anything during first initialization,
  241.      * skipping many toolbar notifications.
  242.      */
  243.     if (m_fInit)
  244.         return 0L;
  245.  
  246.     pDoc=(PCPatronDoc)m_pCL->ActiveDocument();
  247.  
  248.     if (NULL!=pDoc && (IDM_VERBMIN <= wID) && (IDM_VERBMAX >= wID))
  249.         {
  250.         pDoc->ActivateObject(wID-IDM_VERBMIN);
  251.         return 0L;
  252.         }
  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.         case IDM_EDITINSERTOBJECT:
  273.             pDoc->InsertObject(m_hWnd);
  274.             return 0L;
  275.  
  276.         case IDM_EDITCONVERT:
  277.             pDoc->ConvertObject(m_hWnd);
  278.             return 0L;
  279.  
  280.         //CHAPTER20MOD
  281.         case IDM_EDITLINKS:
  282.             pDoc->EditLinks(m_hWnd);
  283.             return 0L;
  284.         //End CHAPTER20MOD
  285.  
  286.         case IDM_PAGENEWPAGE:
  287.             pDoc->NewPage();
  288.             break;
  289.  
  290.         case IDM_PAGEDELETEPAGE:
  291.             pDoc->DeletePage();
  292.             break;
  293.  
  294.         case IDM_PAGENEXTPAGE:
  295.             pDoc->NextPage();
  296.             break;
  297.  
  298.         case IDM_PAGEPREVIOUSPAGE:
  299.             pDoc->PreviousPage();
  300.             break;
  301.  
  302.         case IDM_PAGEFIRSTPAGE:
  303.             pDoc->FirstPage();
  304.             break;
  305.  
  306.         case IDM_PAGELASTPAGE:
  307.             pDoc->LastPage();
  308.             break;
  309.  
  310.         //CHAPTER20MOD
  311.         case IDM_PAGESHOWOBJECTS:
  312.             {
  313.             BOOL    fTemp;
  314.  
  315.             //First get the current state, then toggle it.
  316.             fTemp=pDoc->ShowOrQueryObjectTypes(TRUE, FALSE);
  317.             pDoc->ShowOrQueryObjectTypes(FALSE, !fTemp);
  318.             }
  319.             break;
  320.         //End CHAPTER20MOD
  321.  
  322.         default:
  323.            return CFrame::OnCommand(hWnd, wParam, lParam);
  324.         }
  325.  
  326.     return 0L;
  327.     }
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336. /*
  337.  * CPatronFrame::CreateToolbar
  338.  *
  339.  * Purpose:
  340.  *  Procedure to create all the necessary toolbar buttons.
  341.  *
  342.  * Parameters:
  343.  *  None
  344.  *
  345.  * Return Value:
  346.  *  UINT            Number of tools added to the bar.
  347.  */
  348.  
  349. UINT CPatronFrame::CreateToolbar(void)
  350.     {
  351.     UINT            iLast;
  352.     UINT            uState=GIZMO_NORMAL;
  353.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  354.  
  355.     //Insert the standard ones.
  356.     iLast=CFrame::CreateToolbar();
  357.  
  358.     //Remove Undo:  we don't use it.
  359.     m_pTB->Remove(IDM_EDITUNDO);
  360.  
  361.     /*
  362.      * Insert Print File Import in the 5th position and account
  363.      * for it in iLast.
  364.      */
  365.     m_pTB->Add(utCmd, 4, IDM_FILEPRINT, m_dxB, m_dyB
  366.         , NULL, NULL, 6, uState);
  367.  
  368.     iLast++;
  369.  
  370.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  371.         , NULL, NULL, 0, uState);
  372.  
  373.     //Add New Page, and Delete Page
  374.     m_pTB->Add(utCmd, iLast++, IDM_PAGENEWPAGE, m_dxB, m_dyB
  375.         , NULL, m_hBmp, 2, uState);
  376.     m_pTB->Add(utCmd, iLast++, IDM_PAGEDELETEPAGE, m_dxB, m_dyB
  377.         , NULL, m_hBmp, 3, uState);
  378.  
  379.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  380.         , NULL, NULL, 0, uState);
  381.  
  382.     //First, Prev, Next, Last pages.
  383.     m_pTB->Add(utCmd, iLast++, IDM_PAGEFIRSTPAGE, m_dxB, m_dyB
  384.         , NULL, m_hBmp, 4, uState);
  385.     m_pTB->Add(utCmd, iLast++, IDM_PAGEPREVIOUSPAGE, m_dxB, m_dyB
  386.         , NULL, m_hBmp, 5, uState);
  387.     m_pTB->Add(utCmd, iLast++, IDM_PAGENEXTPAGE, m_dxB, m_dyB
  388.         , NULL, m_hBmp, 6, uState);
  389.     m_pTB->Add(utCmd, iLast++, IDM_PAGELASTPAGE, m_dxB, m_dyB
  390.         , NULL, m_hBmp, 7, uState);
  391.  
  392.     return iLast;
  393.     }
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401. /*
  402.  * CPatronFrame::UpdateMenus
  403.  *
  404.  * Purpose:
  405.  *  Handles the WM_INITMENU message for the frame window.  Depending
  406.  *  on the existence of an active window, menu items are selectively
  407.  *  enabled and disabled.
  408.  *
  409.  * Parameters:
  410.  *  hMenu           HMENU of the menu to intialize
  411.  *  iMenu           UINT position of the menu.
  412.  *
  413.  * Return Value:
  414.  *  None
  415.  */
  416.  
  417. void CPatronFrame::UpdateMenus(HMENU hMenu, UINT iMenu)
  418.     {
  419.     PCPatronDoc     pDoc;
  420.     BOOL            fOK=FALSE;
  421.     BOOL            fCallDefault=TRUE;
  422.     UINT            uTemp;
  423.     UINT            uTempE;
  424.     UINT            uTempD;
  425.  
  426.     pDoc=(PCPatronDoc)m_pCL->ActiveDocument();
  427.  
  428.     uTempE=MF_ENABLED | MF_BYCOMMAND;
  429.     uTempD=MF_DISABLED | MF_GRAYED | MF_BYCOMMAND;
  430.     uTemp=((NULL!=pDoc) ? uTempE : uTempD);
  431.  
  432.     if (m_phMenu[0]==hMenu)
  433.         {
  434.         EnableMenuItem(hMenu, IDM_FILEPRINT, uTemp);
  435.  
  436.         if (NULL!=pDoc)
  437.             fOK=pDoc->FQueryPrinterSetup();
  438.  
  439.         EnableMenuItem(hMenu, IDM_FILEPRINTERSETUP
  440.             , (fOK) ? uTempE : uTempD);
  441.         }
  442.  
  443.     if (m_phMenu[1]==hMenu)
  444.         {
  445.         if (NULL!=pDoc)
  446.             fOK=pDoc->FQueryPaste();
  447.  
  448.         EnableMenuItem(hMenu, IDM_EDITPASTE
  449.             , (fOK) ? uTempE : uTempD);
  450.         EnableMenuItem(hMenu, IDM_EDITPASTESPECIAL
  451.             , (fOK) ? uTempE : uTempD);
  452.  
  453.         //Cut, Copy, Delete depends on there being a selection.
  454.         if (NULL!=pDoc)
  455.             fOK=pDoc->FQueryObjectSelected(hMenu);
  456.         else
  457.             fOK=FALSE;
  458.  
  459.         EnableMenuItem(hMenu, IDM_EDITCUT, (fOK) ? uTempE : uTempD);
  460.         EnableMenuItem(hMenu, IDM_EDITCOPY
  461.             , (fOK) ? uTempE : uTempD);
  462.         EnableMenuItem(hMenu, IDM_EDITDELETEOBJECT
  463.             , (fOK) ? uTempE : uTempD);
  464.  
  465.         EnableMenuItem(hMenu, IDM_EDITINSERTOBJECT, uTemp);
  466.  
  467.         //CHAPTER20MOD
  468.         if (NULL!=pDoc)
  469.             fOK=pDoc->FQueryEnableEditLinks();
  470.         else
  471.             fOK=FALSE;
  472.  
  473.         EnableMenuItem(hMenu, IDM_EDITLINKS
  474.             , (fOK) ? uTempE : uTempD);
  475.         //End CHAPTER20MOD
  476.  
  477.         //We did the whole menu...
  478.         fCallDefault=FALSE;
  479.         }
  480.  
  481.     //Page menu
  482.     if (m_phMenu[2]==hMenu)
  483.         {
  484.         EnableMenuItem(hMenu, IDM_PAGENEWPAGE,      uTemp);
  485.         EnableMenuItem(hMenu, IDM_PAGEDELETEPAGE,   uTemp);
  486.         EnableMenuItem(hMenu, IDM_PAGENEXTPAGE,     uTemp);
  487.         EnableMenuItem(hMenu, IDM_PAGEPREVIOUSPAGE, uTemp);
  488.         EnableMenuItem(hMenu, IDM_PAGEFIRSTPAGE,    uTemp);
  489.         EnableMenuItem(hMenu, IDM_PAGELASTPAGE,     uTemp);
  490.  
  491.         //CHAPTER20MOD
  492.         //Check the Show Objects command or not.
  493.         if (NULL!=pDoc)
  494.             fOK=pDoc->ShowOrQueryObjectTypes(TRUE, FALSE);
  495.         else
  496.             fOK=FALSE;
  497.  
  498.         CheckMenuItem(hMenu, IDM_PAGESHOWOBJECTS, MF_BYCOMMAND
  499.             | ((fOK) ? MF_CHECKED : MF_UNCHECKED));
  500.         EnableMenuItem(hMenu, IDM_PAGESHOWOBJECTS, uTemp);
  501.         //End CHAPTER20MOD
  502.         }
  503.  
  504.     if (fCallDefault)
  505.         CFrame::UpdateMenus(hMenu, iMenu);
  506.  
  507.     return;
  508.     }
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515. /*
  516.  * CPatronFrame::UpdateToolbar
  517.  *
  518.  * Purpose:
  519.  *  Enables and disables tools depending on whether we have
  520.  *  a document or not.
  521.  *
  522.  * Parameters:
  523.  *  None
  524.  *
  525.  * Return Value:
  526.  *  None
  527.  */
  528.  
  529. void CPatronFrame::UpdateToolbar(void)
  530.     {
  531.     PCDocument  pDoc;
  532.     BOOL        fEnable;
  533.  
  534.     //Let the default hack on its tools.
  535.     CFrame::UpdateToolbar();
  536.  
  537.     pDoc=m_pCL->ActiveDocument();
  538.     fEnable=(NULL!=pDoc);
  539.  
  540.     //No document, disable just about everything
  541.     m_pTB->Enable(IDM_FILEPRINT,        fEnable);
  542.     m_pTB->Enable(IDM_FILEPRINTERSETUP, fEnable);
  543.  
  544.     m_pTB->Enable(IDM_PAGENEWPAGE,      fEnable);
  545.     m_pTB->Enable(IDM_PAGEDELETEPAGE,   fEnable);
  546.     m_pTB->Enable(IDM_PAGEFIRSTPAGE,    fEnable);
  547.     m_pTB->Enable(IDM_PAGEPREVIOUSPAGE, fEnable);
  548.     m_pTB->Enable(IDM_PAGENEXTPAGE,     fEnable);
  549.     m_pTB->Enable(IDM_PAGELASTPAGE,     fEnable);
  550.  
  551.     return;
  552.     }
  553.  
  554.  
  555.  
  556. /*
  557.  * CPatronFrame::FMessageHook
  558.  *
  559.  * Purpose:
  560.  *  Override of CFrame::FMessageHook so we can specifically trap
  561.  *  WM_MENUSELECT messages for the Object verb menu to provide some
  562.  *  meaningful information on the status strip.
  563.  *
  564.  * Parameters:
  565.  *  <WndProc Parameters>
  566.  *  pLRes           LRESULT * in which to store the return value
  567.  *                  for the message.
  568.  *
  569.  * Return Value:
  570.  *  BOOL            TRUE to prevent further processing,
  571.  *                  FALSE otherwise.
  572.  */
  573.  
  574. BOOL CPatronFrame::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
  575.     , LPARAM lParam, LRESULT *pLRes)
  576.     {
  577.     BOOL        fRet=FALSE;
  578.  
  579.     *pLRes=0;
  580.  
  581.     MENUSELECTPARAMS(wItem, wMenuFlags, hMenu);
  582.  
  583.     //If this is the wrong message, nothing to do.
  584.     if (WM_MENUSELECT!=iMsg)
  585.         return FALSE;
  586.  
  587.     //This happens when there's no menu selection.
  588.     if (-1==wMenuFlags)
  589.         return FALSE;
  590.  
  591.     if (MF_POPUP & wMenuFlags)
  592.         {
  593.         /*
  594.          * If this is the cascade verb menu itself, display the same
  595.          * message.  m_phMenu[1] contains the current edit menu
  596.          * handle.
  597.          */
  598.         if (0!=wItem)
  599.             {
  600.             fRet=((HMENU)wItem==GetSubMenu(m_phMenu[1]
  601.                 , MENUPOS_OBJECT));
  602.             }
  603.         }
  604.     else
  605.         {
  606.         /*
  607.          * If the ID is in the verb range, use
  608.          * IDS_ITEMMESSAGEEDITOBJECT message
  609.          */
  610.         fRet=(IDM_VERBMIN <= wItem && IDM_VERBMAX >= wItem);
  611.         }
  612.  
  613.     if (fRet)
  614.         m_pSL->MessageDisplay(IDM_EDITOBJECT);
  615.  
  616.     return fRet;
  617.     }
  618.