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 / chap22 / patron / patron.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  37.4 KB  |  1,584 lines

  1. /*
  2.  * PATRON.CPP
  3.  * Patron Chapter 22
  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. ULONG       g_cObj=0;
  19. ULONG       g_cLock=0;
  20. HWND        g_hWnd=NULL;
  21. BOOL        g_fUser=TRUE;
  22.  
  23.  
  24. //CHAPTER22MOD
  25. /*
  26.  * The in-place site needs to have access to the frame and its
  27.  * IOleInPlaceFrame interface.  A global, pardon me, is the simplest
  28.  * way to achieve this.
  29.  */
  30.  
  31. PCPatronFrame   g_pFR;
  32. //End CHAPTER22MOD
  33.  
  34.  
  35.  
  36. /*
  37.  * WinMain
  38.  *
  39.  * Purpose:
  40.  *  Main entry point of application.  Should register the app class
  41.  *  if a previous instance has not done so and do any other one-time
  42.  *  initializations.
  43.  */
  44.  
  45. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev
  46.     , LPSTR pszCmdLine, int nCmdShow)
  47.     {
  48.     PCPatronFrame   pFR;
  49.     FRAMEINIT       fi;
  50.     WPARAM          wRet=0;
  51.  
  52.     SETMESSAGEQUEUE;
  53.  
  54.     //Attempt to allocate and initialize the application
  55.     pFR=new CPatronFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  56.  
  57.     if (NULL==pFR)
  58.         return -1;
  59.  
  60.     //CHAPTER22MOD
  61.     g_pFR=pFR;
  62.     //End CHAPTER22MOD
  63.  
  64.     fi.idsMin=IDS_FRAMEMIN;
  65.     fi.idsMax=IDS_FRAMEMAX;
  66.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  67.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  68.     fi.idStatMenuMin=ID_MENUFILE;
  69.     fi.idStatMenuMax=ID_MENUHELP;
  70.     fi.iPosWindowMenu=WINDOW_MENU;
  71.     fi.cMenus=CMENUS;
  72.  
  73.     fi.x=CW_USEDEFAULT;
  74.     fi.y=CW_USEDEFAULT;
  75.     fi.cx=CW_USEDEFAULT;
  76.     fi.cy=CW_USEDEFAULT;
  77.  
  78.     //If we can initialize pFR, start chugging messages
  79.     if (pFR->Init(&fi))
  80.         wRet=pFR->MessageLoop();
  81.  
  82.     delete pFR;
  83.     return wRet;
  84.     }
  85.  
  86.  
  87.  
  88. /*
  89.  * ObjectDestroyed
  90.  *
  91.  * Purpose:
  92.  *  Function for the Patron Document object to call when it gets
  93.  *  destroyed.  We destroy the main window if the proper conditions
  94.  *  are met for shutdown.
  95.  */
  96.  
  97. void ObjectDestroyed(void)
  98.     {
  99.     g_cObj--;
  100.  
  101.     //No more objects, no locks, no user control, shut the app down.
  102.     if (0==g_cObj && 0==g_cLock && IsWindow(g_hWnd) && !g_fUser)
  103.         PostMessage(g_hWnd, WM_CLOSE, 0, 0L);
  104.  
  105.     return;
  106.     }
  107.  
  108.  
  109.  
  110.  
  111.  
  112. /*
  113.  * CPatronFrame::CPatronFrame
  114.  * CPatronFrame::~CPatronFrame
  115.  *
  116.  * Constructor Parameters:
  117.  *  hInst           HINSTANCE from WinMain
  118.  *  hInstPrev       HINSTANCE from WinMain
  119.  *  pszCmdLine      LPSTR from WinMain
  120.  *  nCmdShow        int from WInMain
  121.  */
  122.  
  123. CPatronFrame::CPatronFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  124.     , LPSTR pszCmdLine, int nCmdShow)
  125.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  126.     {
  127.     m_fInitialized=FALSE;
  128.     m_pIClassDataTran=NULL;
  129.  
  130.     m_pDocCreated=NULL;
  131.     m_fEmbedding=FALSE;
  132.  
  133.     m_dwRegCO=0;
  134.     m_pIClassFactory=NULL;
  135.  
  136.     //CHAPTER22MOD
  137.     m_cRef=0;
  138.     m_hAccelIP=NULL;
  139.     m_hWndObj=NULL;
  140.     m_hMenuOrg=NULL;
  141.     m_hMenuTop=NULL;
  142.     m_fOurToolsShowing=TRUE;
  143.     m_fInContextHelp=FALSE;
  144.     m_pIOleIPActiveObject=NULL;
  145.     //End CHAPTER22MOD
  146.  
  147.     return;
  148.     }
  149.  
  150.  
  151. CPatronFrame::~CPatronFrame(void)
  152.     {
  153.     //Opposite of CoRegisterClassObject, takes class factory ref to 1
  154.     if (0L!=m_dwRegCO)
  155.         CoRevokeClassObject(m_dwRegCO);
  156.  
  157.     //This should be the last Release, which frees the class factory.
  158.     ReleaseInterface(m_pIClassFactory);
  159.  
  160.     if (NULL!=m_pIClassDataTran)
  161.         {
  162.         m_pIClassDataTran->LockServer(FALSE);
  163.         m_pIClassDataTran->Release();
  164.         }
  165.  
  166.     OleFlushClipboard();
  167.  
  168.     if (m_fInitialized)
  169.         OleUninitialize();
  170.  
  171.     return;
  172.     }
  173.  
  174.  
  175.  
  176.  
  177. /*
  178.  * CPatronFrame::Init
  179.  *
  180.  * Purpose:
  181.  *  Call OleInitialize then calling down into the base class
  182.  *  initialization.
  183.  *
  184.  * Parameters:
  185.  *  pFI             PFRAMEINIT containing initialization
  186.  *                  parameters.
  187.  *
  188.  * Return Value:
  189.  *  BOOL            TRUE if initialization succeeded,
  190.  *                  FALSE otherwise.
  191.  */
  192.  
  193. BOOL CPatronFrame::Init(PFRAMEINIT pFI)
  194.     {
  195.     HRESULT     hr;
  196.     BOOL        fRet;
  197.  
  198.     CHECKVER_OLE;
  199.  
  200.     if (FAILED(OleInitialize(NULL)))
  201.         return FALSE;
  202.  
  203.     m_fInitialized=TRUE;
  204.  
  205.     //Lock the data transfer object factory as an optimization.
  206.     hr=CoGetClassObject(CLSID_DataTransferObject
  207.         , CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory
  208.         , (PPVOID)&m_pIClassDataTran);
  209.  
  210.     if (SUCCEEDED(hr))
  211.         m_pIClassDataTran->LockServer(TRUE);
  212.  
  213.     //Check for command line flags
  214.     ParseCommandLine();
  215.  
  216.     if (NULL!=m_ppszCmdArgs)
  217.         {
  218.         if(0==lstrcmpi(m_ppszCmdArgs[0], TEXT("-Embedding"))
  219.            || 0==lstrcmpi(m_ppszCmdArgs[0], TEXT("/Embedding")))
  220.             m_fEmbedding=TRUE;
  221.         }
  222.  
  223.     g_fUser=!m_fEmbedding;
  224.  
  225.     if (m_fEmbedding)
  226.         {
  227.         HRESULT     hr;
  228.  
  229.         m_pIClassFactory=new CLinkClassFactory(this);
  230.  
  231.         if (NULL==m_pIClassFactory)
  232.             return FALSE;
  233.  
  234.         //Since we hold on to this, we should AddRef it.
  235.         m_pIClassFactory->AddRef();
  236.  
  237.         hr=CoRegisterClassObject(CLSID_PatronPages, m_pIClassFactory
  238.             , CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &m_dwRegCO);
  239.  
  240.         if (FAILED(hr))
  241.             return FALSE;
  242.         }
  243.  
  244.     //CHAPTER22MOD
  245.     //Load in-place accelerators
  246.     m_hAccelIP=LoadAccelerators(m_hInst
  247.         , MAKEINTRESOURCE(IDR_INPLACEACCELERATORS));
  248.  
  249.     if (NULL==m_hAccelIP)
  250.         return FALSE;
  251.  
  252.     fRet=CFrame::Init(pFI);
  253.  
  254.     m_hMenuOrg=GetMenu(m_hWnd);
  255.     return fRet;
  256.     //End CHAPTER22MOD
  257.     }
  258.  
  259.  
  260.  
  261.  
  262.  
  263. /*
  264.  * CPatronFrame::CreateCClient
  265.  *
  266.  * Purpose:
  267.  *  Constructs a new client specific to the application.
  268.  *
  269.  * Parameters:
  270.  *  None
  271.  *
  272.  * Return Value:
  273.  *  PCClient        Pointer to the new client object.
  274.  */
  275.  
  276. PCClient CPatronFrame::CreateCClient(void)
  277.     {
  278.     return (PCClient)(new CPatronClient(m_hInst, this));
  279.     }
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286. /*
  287.  * CPatronFrame::RegisterAllClasses
  288.  *
  289.  * Purpose:
  290.  *  Registers all classes used in this application.
  291.  *
  292.  * Parameters:
  293.  *  None
  294.  *
  295.  * Return Value:
  296.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  297.  */
  298.  
  299. BOOL CPatronFrame::RegisterAllClasses(void)
  300.     {
  301.     WNDCLASS        wc;
  302.  
  303.     //First let the standard frame do its thing
  304.     if (!CFrame::RegisterAllClasses())
  305.         return FALSE;
  306.  
  307.     //We need double-clicks now and for object activation.
  308.     wc.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  309.     wc.hInstance     = m_hInst;
  310.     wc.cbClsExtra    = 0;
  311.     wc.lpfnWndProc   = PagesWndProc;
  312.     wc.cbWndExtra    = CBPAGESWNDEXTRA;
  313.     wc.hIcon         = NULL;
  314.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  315.     wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
  316.     wc.lpszMenuName  = NULL;
  317.     wc.lpszClassName = SZCLASSPAGES;
  318.  
  319.     if (!RegisterClass(&wc))
  320.         return FALSE;
  321.  
  322.     return TRUE;
  323.     }
  324.  
  325.  
  326.  
  327.  
  328. /*
  329.  * CPatronFrame::PreShowInit
  330.  *
  331.  * Purpose:
  332.  *  Called from Init before intially showing the window.  We do
  333.  *  whatever else we want here, modifying m_nCmdShow as necessary
  334.  *  which affects ShowWindow in Init.
  335.  *
  336.  * Parameters:
  337.  *  None
  338.  *
  339.  * Return Value:
  340.  *  BOOL            TRUE if this successful, FALSE otherwise.
  341.  */
  342.  
  343. BOOL CPatronFrame::PreShowInit(void)
  344.     {
  345.     //Base class does nothing
  346.     CFrame::PreShowInit();
  347.  
  348.     //Save the window handle for shutdown if necessary.
  349.     g_hWnd=m_hWnd;
  350.  
  351.     //If we're -Embedding, don't show the window initially.
  352.     if (m_fEmbedding)
  353.         m_nCmdShow=SW_HIDE;
  354.  
  355.     return TRUE;
  356.     }
  357.  
  358.  
  359.  
  360.  
  361.  
  362. /*
  363.  * CPatronFrame::OnCommand
  364.  *
  365.  * Purpose:
  366.  *  WM_COMMAND handler for the Patron frame window that processes
  367.  *  extra File menu items as well as the Page menu.
  368.  *
  369.  * Parameters:
  370.  *  hWnd            HWND of the frame window.
  371.  *  wParam          WPARAM of the message.
  372.  *  lParam          LPARAM of the message.
  373.  *
  374.  * Return Value:
  375.  *  LRESULT         Return value for the message.
  376.  */
  377.  
  378. LRESULT CPatronFrame::OnCommand(HWND hWnd, WPARAM wParam
  379.     , LPARAM lParam)
  380.     {
  381.     PCPatronDoc     pDoc;
  382.  
  383.     COMMANDPARAMS(wID, wCode, hWndMsg);
  384.  
  385.     /*
  386.      * Don't bother with anything during first initialization,
  387.      * skipping many toolbar notifications.
  388.      */
  389.     if (m_fInit)
  390.         return 0L;
  391.  
  392.     pDoc=(PCPatronDoc)m_pCL->ActiveDocument();
  393.  
  394.     //CHAPTER22MOD
  395.     //Also check for the open command now too.
  396.     if (NULL!=pDoc && ((IDM_VERBMIN <= wID) && (IDM_VERBMAX >= wID)
  397.         || IDM_OPENOBJECT==wID))
  398.         {
  399.         MSG     msg;
  400.         DWORD   dw;
  401.         LONG    iVerb=(long)(wID-IDM_VERBMIN);
  402.  
  403.         //Include a message for in-place objects.
  404.         msg.hwnd=NULL;
  405.         msg.message=WM_COMMAND;
  406.         msg.wParam=wParam;
  407.         msg.lParam=lParam;
  408.         msg.time=GetMessageTime();
  409.  
  410.         dw=GetMessagePos();
  411.         SETPOINT(msg.pt, LOWORD(dw), HIWORD(dw));
  412.  
  413.         if (IDM_OPENOBJECT==wID)
  414.             iVerb=OLEIVERB_OPEN;
  415.  
  416.         pDoc->ActivateObject(iVerb, &msg);
  417.         return 0L;
  418.         }
  419.     //End CHAPTER22MOD
  420.  
  421.  
  422.     switch (wID)
  423.         {
  424.         case IDM_FILEPRINT:
  425.             pDoc->Print(m_hWnd);
  426.             return 0L;
  427.  
  428.         case IDM_FILEPRINTERSETUP:
  429.             pDoc->PrinterSetup(m_hWnd, FALSE);
  430.             return 0L;
  431.  
  432.         case IDM_EDITPASTESPECIAL:
  433.             pDoc->PasteSpecial(m_hWnd);
  434.             return 0L;
  435.  
  436.         case IDM_EDITDELETEOBJECT:
  437.             pDoc->Delete();
  438.             return 0L;
  439.  
  440.         case IDM_EDITINSERTOBJECT:
  441.             pDoc->InsertObject(m_hWnd);
  442.             return 0L;
  443.  
  444.         case IDM_EDITCONVERT:
  445.             pDoc->ConvertObject(m_hWnd);
  446.             return 0L;
  447.  
  448.         case IDM_EDITLINKS:
  449.             pDoc->EditLinks(m_hWnd);
  450.             return 0L;
  451.  
  452.         case IDM_PAGENEWPAGE:
  453.             pDoc->NewPage();
  454.             break;
  455.  
  456.         case IDM_PAGEDELETEPAGE:
  457.             pDoc->DeletePage();
  458.             break;
  459.  
  460.         case IDM_PAGENEXTPAGE:
  461.             pDoc->NextPage();
  462.             break;
  463.  
  464.         case IDM_PAGEPREVIOUSPAGE:
  465.             pDoc->PreviousPage();
  466.             break;
  467.  
  468.         case IDM_PAGEFIRSTPAGE:
  469.             pDoc->FirstPage();
  470.             break;
  471.  
  472.         case IDM_PAGELASTPAGE:
  473.             pDoc->LastPage();
  474.             break;
  475.  
  476.         case IDM_PAGESHOWOBJECTS:
  477.             {
  478.             BOOL    fTemp;
  479.  
  480.             //First get the current state, then toggle it.
  481.             fTemp=pDoc->ShowOrQueryObjectTypes(TRUE, FALSE);
  482.             pDoc->ShowOrQueryObjectTypes(FALSE, !fTemp);
  483.             }
  484.             break;
  485.  
  486.         //CHAPTER22MOD
  487.         case IDM_ENTERCONTEXTHELP:
  488.         case IDM_ESCAPECONTEXTHELP:
  489.             //Notify the object on entry and exit.
  490.             ContextSensitiveHelp(IDM_ENTERCONTEXTHELP==wID);
  491.             break;
  492.         //End CHAPTER22MOD
  493.  
  494.         default:
  495.            return CFrame::OnCommand(hWnd, wParam, lParam);
  496.         }
  497.  
  498.     return 0L;
  499.     }
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508. /*
  509.  * CPatronFrame::CreateToolbar
  510.  *
  511.  * Purpose:
  512.  *  Procedure to create all the necessary toolbar buttons.
  513.  *
  514.  * Parameters:
  515.  *  None
  516.  *
  517.  * Return Value:
  518.  *  UINT            Number of tools added to the bar.
  519.  */
  520.  
  521. UINT CPatronFrame::CreateToolbar(void)
  522.     {
  523.     UINT            iLast;
  524.     UINT            uState=GIZMO_NORMAL;
  525.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  526.  
  527.     //Insert the standard ones.
  528.     iLast=CFrame::CreateToolbar();
  529.  
  530.     //Remove Undo:  we don't use it.
  531.     m_pTB->Remove(IDM_EDITUNDO);
  532.  
  533.     /*
  534.      * Insert Print File Import in the 5th position and account
  535.      * for it in iLast.
  536.      */
  537.     m_pTB->Add(utCmd, 4, IDM_FILEPRINT, m_dxB, m_dyB
  538.         , NULL, NULL, 6, uState);
  539.  
  540.     iLast++;
  541.  
  542.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  543.         , NULL, NULL, 0, uState);
  544.  
  545.     //Add New Page, and Delete Page
  546.     m_pTB->Add(utCmd, iLast++, IDM_PAGENEWPAGE, m_dxB, m_dyB
  547.         , NULL, m_hBmp, 2, uState);
  548.     m_pTB->Add(utCmd, iLast++, IDM_PAGEDELETEPAGE, m_dxB, m_dyB
  549.         , NULL, m_hBmp, 3, uState);
  550.  
  551.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  552.         , NULL, NULL, 0, uState);
  553.  
  554.     //First, Prev, Next, Last pages.
  555.     m_pTB->Add(utCmd, iLast++, IDM_PAGEFIRSTPAGE, m_dxB, m_dyB
  556.         , NULL, m_hBmp, 4, uState);
  557.     m_pTB->Add(utCmd, iLast++, IDM_PAGEPREVIOUSPAGE, m_dxB, m_dyB
  558.         , NULL, m_hBmp, 5, uState);
  559.     m_pTB->Add(utCmd, iLast++, IDM_PAGENEXTPAGE, m_dxB, m_dyB
  560.         , NULL, m_hBmp, 6, uState);
  561.     m_pTB->Add(utCmd, iLast++, IDM_PAGELASTPAGE, m_dxB, m_dyB
  562.         , NULL, m_hBmp, 7, uState);
  563.  
  564.     return iLast;
  565.     }
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573. /*
  574.  * CPatronFrame::UpdateMenus
  575.  *
  576.  * Purpose:
  577.  *  Handles the WM_INITMENU message for the frame window.  Depending
  578.  *  on the existence of an active window, menu items are selectively
  579.  *  enabled and disabled.
  580.  *
  581.  * Parameters:
  582.  *  hMenu           HMENU of the menu to intialize
  583.  *  iMenu           UINT position of the menu.
  584.  *
  585.  * Return Value:
  586.  *  None
  587.  */
  588.  
  589. void CPatronFrame::UpdateMenus(HMENU hMenu, UINT iMenu)
  590.     {
  591.     PCPatronDoc     pDoc;
  592.     BOOL            fOK=FALSE;
  593.     BOOL            fCallDefault=TRUE;
  594.     UINT            uTemp;
  595.     UINT            uTempE;
  596.     UINT            uTempD;
  597.  
  598.     pDoc=(PCPatronDoc)m_pCL->ActiveDocument();
  599.  
  600.     uTempE=MF_ENABLED | MF_BYCOMMAND;
  601.     uTempD=MF_DISABLED | MF_GRAYED | MF_BYCOMMAND;
  602.     uTemp=((NULL!=pDoc) ? uTempE : uTempD);
  603.  
  604.     if (m_phMenu[0]==hMenu)
  605.         {
  606.         EnableMenuItem(hMenu, IDM_FILEPRINT, uTemp);
  607.  
  608.         if (NULL!=pDoc)
  609.             fOK=pDoc->FQueryPrinterSetup();
  610.  
  611.         EnableMenuItem(hMenu, IDM_FILEPRINTERSETUP
  612.             , (fOK) ? uTempE : uTempD);
  613.         }
  614.  
  615.     if (m_phMenu[1]==hMenu)
  616.         {
  617.         if (NULL!=pDoc)
  618.             fOK=pDoc->FQueryPaste();
  619.  
  620.         EnableMenuItem(hMenu, IDM_EDITPASTE
  621.             , (fOK) ? uTempE : uTempD);
  622.         EnableMenuItem(hMenu, IDM_EDITPASTESPECIAL
  623.             , (fOK) ? uTempE : uTempD);
  624.  
  625.         //Cut, Copy, Delete depends on there being a selection.
  626.         if (NULL!=pDoc)
  627.             fOK=pDoc->FQueryObjectSelected(hMenu);
  628.         else
  629.             fOK=FALSE;
  630.  
  631.         EnableMenuItem(hMenu, IDM_EDITCUT, (fOK) ? uTempE : uTempD);
  632.         EnableMenuItem(hMenu, IDM_EDITCOPY
  633.             , (fOK) ? uTempE : uTempD);
  634.         EnableMenuItem(hMenu, IDM_EDITDELETEOBJECT
  635.             , (fOK) ? uTempE : uTempD);
  636.  
  637.         EnableMenuItem(hMenu, IDM_EDITINSERTOBJECT, uTemp);
  638.  
  639.         if (NULL!=pDoc)
  640.             fOK=pDoc->FQueryEnableEditLinks();
  641.         else
  642.             fOK=FALSE;
  643.  
  644.         EnableMenuItem(hMenu, IDM_EDITLINKS
  645.             , (fOK) ? uTempE : uTempD);
  646.  
  647.         //We did the whole menu...
  648.         fCallDefault=FALSE;
  649.         }
  650.  
  651.     //Page menu
  652.     if (m_phMenu[2]==hMenu)
  653.         {
  654.         EnableMenuItem(hMenu, IDM_PAGENEWPAGE,      uTemp);
  655.         EnableMenuItem(hMenu, IDM_PAGEDELETEPAGE,   uTemp);
  656.         EnableMenuItem(hMenu, IDM_PAGENEXTPAGE,     uTemp);
  657.         EnableMenuItem(hMenu, IDM_PAGEPREVIOUSPAGE, uTemp);
  658.         EnableMenuItem(hMenu, IDM_PAGEFIRSTPAGE,    uTemp);
  659.         EnableMenuItem(hMenu, IDM_PAGELASTPAGE,     uTemp);
  660.  
  661.         //Check the Show Objects command or not.
  662.         if (NULL!=pDoc)
  663.             fOK=pDoc->ShowOrQueryObjectTypes(TRUE, FALSE);
  664.         else
  665.             fOK=FALSE;
  666.  
  667.         CheckMenuItem(hMenu, IDM_PAGESHOWOBJECTS, MF_BYCOMMAND
  668.             | ((fOK) ? MF_CHECKED : MF_UNCHECKED));
  669.         EnableMenuItem(hMenu, IDM_PAGESHOWOBJECTS, uTemp);
  670.         }
  671.  
  672.     if (fCallDefault)
  673.         CFrame::UpdateMenus(hMenu, iMenu);
  674.  
  675.     return;
  676.     }
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683. /*
  684.  * CPatronFrame::UpdateToolbar
  685.  *
  686.  * Purpose:
  687.  *  Enables and disables tools depending on whether we have
  688.  *  a document or not.
  689.  *
  690.  * Parameters:
  691.  *  None
  692.  *
  693.  * Return Value:
  694.  *  None
  695.  */
  696.  
  697. void CPatronFrame::UpdateToolbar(void)
  698.     {
  699.     PCDocument  pDoc;
  700.     BOOL        fEnable;
  701.  
  702.     //Let the default hack on its tools.
  703.     CFrame::UpdateToolbar();
  704.  
  705.     pDoc=m_pCL->ActiveDocument();
  706.     fEnable=(NULL!=pDoc);
  707.  
  708.     //No document, disable just about everything
  709.     m_pTB->Enable(IDM_FILEPRINT,        fEnable);
  710.     m_pTB->Enable(IDM_FILEPRINTERSETUP, fEnable);
  711.  
  712.     m_pTB->Enable(IDM_PAGENEWPAGE,      fEnable);
  713.     m_pTB->Enable(IDM_PAGEDELETEPAGE,   fEnable);
  714.     m_pTB->Enable(IDM_PAGEFIRSTPAGE,    fEnable);
  715.     m_pTB->Enable(IDM_PAGEPREVIOUSPAGE, fEnable);
  716.     m_pTB->Enable(IDM_PAGENEXTPAGE,     fEnable);
  717.     m_pTB->Enable(IDM_PAGELASTPAGE,     fEnable);
  718.  
  719.     return;
  720.     }
  721.  
  722.  
  723.  
  724.  
  725.  
  726. //CHAPTER22MOD
  727. /*
  728.  * CPatronFrame::MessageLoop
  729.  *
  730.  * Purpose:
  731.  *  Override of standard message loop function in CLASSLIB to use
  732.  *  in-place accelerators when necessary.
  733.  *
  734.  * Parameters:
  735.  *  None
  736.  *
  737.  * Return Value:
  738.  *  WPARAM          Contents of msg.wParam from WM_QUIT.
  739.  */
  740.  
  741. WPARAM CPatronFrame::MessageLoop(void)
  742.     {
  743.     MSG     msg;
  744.  
  745.     while (GetMessage(&msg, NULL, 0,0 ))
  746.         {
  747.         HACCEL      hAccel=m_hAccel;
  748.  
  749.         //Always give the object first crack at translation.
  750.         if (NULL!=m_pIOleIPActiveObject)
  751.             {
  752.             HRESULT     hr;
  753.  
  754.             hAccel=m_hAccelIP;
  755.             hr=m_pIOleIPActiveObject->TranslateAccelerator(&msg);
  756.  
  757.             //If the object translated the accelerator, we're done
  758.             if (NOERROR==hr)
  759.                 continue;
  760.             }
  761.  
  762.         if (!m_pCL->TranslateAccelerator(&msg))
  763.             {
  764.             //hAccel is either the normal ones or the in-place ones.
  765.  
  766.             if (!::TranslateAccelerator(m_hWnd, hAccel, &msg))
  767.                 {
  768.                 TranslateMessage(&msg);
  769.                 DispatchMessage(&msg);
  770.                 }
  771.             }
  772.         }
  773.  
  774.     return msg.wParam;
  775.     }
  776. //End CHAPTER22MOD
  777.  
  778.  
  779.  
  780.  
  781. /*
  782.  * CPatronFrame::FMessageHook
  783.  *
  784.  * Purpose:
  785.  *  Override of CFrame::FMessageHook so we can specifically trap
  786.  *  WM_MENUSELECT messages for the Object verb menu to provide some
  787.  *  meaningful information on the status strip.
  788.  *
  789.  * Parameters:
  790.  *  <WndProc Parameters>
  791.  *  pLRes           LRESULT * in which to store the return value
  792.  *                  for the message.
  793.  *
  794.  * Return Value:
  795.  *  BOOL            TRUE to prevent further processing,
  796.  *                  FALSE otherwise.
  797.  */
  798.  
  799. BOOL CPatronFrame::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
  800.     , LPARAM lParam, LRESULT *pLRes)
  801.     {
  802.     BOOL        fRet=FALSE;
  803.     //CHAPTER22MOD
  804.     int         dx, dy;
  805.     //End CHAPTER22MOD
  806.     MENUSELECTPARAMS(wItem, wMenuFlags, hMenu);
  807.  
  808.     *pLRes=0;
  809.  
  810.     //CHAPTER22MOD
  811.     /*
  812.      * We use a switch now because we previously only processed
  813.      * WM_MENUSELECT which did not use a case statement.
  814.      */
  815.  
  816.     switch (iMsg)
  817.         {
  818.         case WM_SIZE:
  819.             if (NULL!=m_pIOleIPActiveObject)
  820.                 {
  821.                 RECT        rc;
  822.  
  823.                 GetClientRect(m_hWnd, &rc);
  824.                 m_pIOleIPActiveObject->ResizeBorder(&rc
  825.                     , this, TRUE);
  826.                 }
  827.  
  828.             /*
  829.              * If we're not showing our tools, don't let CLASSLIB
  830.              * resize the client, instead doing it ourselves.
  831.              */
  832.             if (m_fOurToolsShowing)
  833.                 return FALSE;
  834.  
  835.             m_fSizing=TRUE;
  836.             dx=LOWORD(lParam);
  837.             dy=HIWORD(lParam);
  838.  
  839.             //Change the Toolbar and StatStrip widths to match
  840.             m_pTB->OnSize(hWnd);
  841.             m_pSL->OnSize(hWnd);
  842.  
  843.             //Adjust the client properly, remembering the StatStrip.
  844.             m_pCL->OnSize(m_cxLeft, m_cyTop, dx-m_cxLeft-m_cxRight
  845.                 , dy-m_cyTop-m_cyBottom-CYSTATSTRIP);
  846.  
  847.             m_fSizing=FALSE;
  848.             return TRUE;
  849.  
  850.         case WM_ACTIVATEAPP:
  851.             if (NULL!=m_pIOleIPActiveObject)
  852.                 {
  853.                 m_pIOleIPActiveObject->OnFrameWindowActivate
  854.                     ((BOOL)wParam);
  855.                 }
  856.  
  857.             return FALSE;
  858.  
  859.         case WM_SETFOCUS:
  860.             if (NULL!=m_pIOleIPActiveObject)
  861.                 {
  862.                 HWND    hWndObj;
  863.  
  864.                 m_pIOleIPActiveObject->GetWindow(&hWndObj);
  865.                 SetFocus(hWndObj);
  866.                 }
  867.  
  868.             return TRUE;
  869.  
  870.         case WM_CLOSE:
  871.             if (NULL!=m_pDocCreated)
  872.                 {
  873.                 if (m_pDocCreated->FDirtyGet())
  874.                     {
  875.                     CHourglass  wait;
  876.                     m_pDocCreated->Save(0, NULL);
  877.                     }
  878.                 }
  879.  
  880.             return FALSE;
  881.  
  882.         case WM_MENUSELECT:
  883.             break;      //Continue processing below.
  884.  
  885.         default:
  886.             return FALSE;
  887.         }
  888.     //End CHAPTER22MOD
  889.  
  890.     //This happens when there's no menu selection.
  891.     if (-1==wMenuFlags)
  892.         return FALSE;
  893.  
  894.     if (MF_POPUP & wMenuFlags)
  895.         {
  896.         /*
  897.          * If this is the cascade verb menu itself, display the same
  898.          * message.  m_phMenu[1] contains the current edit menu
  899.          * handle.
  900.          */
  901.         if (0!=wItem)
  902.             {
  903.             fRet=((HMENU)wItem==GetSubMenu(m_phMenu[1]
  904.                 , MENUPOS_OBJECT));
  905.             }
  906.         }
  907.     else
  908.         {
  909.         /*
  910.          * If the ID is in the verb range, use
  911.          * IDS_ITEMMESSAGEEDITOBJECT message
  912.          */
  913.         fRet=(IDM_VERBMIN <= wItem && IDM_VERBMAX >= wItem);
  914.         }
  915.  
  916.     if (fRet)
  917.         m_pSL->MessageDisplay(IDM_EDITOBJECT);
  918.  
  919.     return fRet;
  920.     }
  921.  
  922.  
  923.  
  924.  
  925. //CHAPTER22MOD
  926.  
  927. /*
  928.  * CPatronFrame::ShowUIAndTools
  929.  *
  930.  * Purpose:
  931.  *  Installs or removes the in-place user interface which includes
  932.  *  the menus and the tools.  The tools may be affected outside of
  933.  *  the menu.
  934.  *
  935.  * Parameters:
  936.  *  fShow           BOOL indicating to show (TRUE) or hide (FALSE)
  937.  *  fMenu           BOOl indicating if the menu is also affected.
  938.  *
  939.  * Return Value:
  940.  *  None
  941.  */
  942.  
  943. void CPatronFrame::ShowUIAndTools(BOOL fShow, BOOL fMenu)
  944.     {
  945.     HWND    hWndTB;
  946.  
  947.     //This is the only menu case...restore our original menu
  948.     if (fMenu && fShow)
  949.         SetMenu(NULL, NULL, NULL);
  950.  
  951.     /*
  952.      * If we're trying to hide our tools but the object didn't
  953.      * want any space, then just leave our tools up and active.
  954.      */
  955.  
  956.     hWndTB=m_pTB->Window();
  957.     ShowWindow(hWndTB, fShow ? SW_SHOW : SW_HIDE);
  958.  
  959.     if (fShow)
  960.         {
  961.         InvalidateRect(hWndTB, NULL, TRUE);
  962.         UpdateWindow(hWndTB);
  963.         }
  964.  
  965.     m_fOurToolsShowing=fShow;
  966.     return;
  967.     }
  968.  
  969.  
  970.  
  971.  
  972. /*
  973.  * CPatronFrame::ReinstateUI
  974.  *
  975.  * Purpose:
  976.  *  Reinstalls the application's normal toolbar and menu and
  977.  *  readjusts the position of the client window and the documents
  978.  *  within it.
  979.  *
  980.  * Parameters:
  981.  *  None
  982.  *
  983.  * Return Value:
  984.  *  None
  985.  */
  986.  
  987. void CPatronFrame::ReinstateUI(void)
  988.     {
  989.     BORDERWIDTHS    bw;
  990.  
  991.     ShowUIAndTools(TRUE, TRUE);
  992.  
  993.     SetRect((LPRECT)&bw, 0, m_cyBar, 0, 0);
  994.     SetBorderSpace(&bw);
  995.     return;
  996.     }
  997.  
  998.  
  999.  
  1000. /*
  1001.  * CPatronFrame::QueryInterface
  1002.  * CPatronFrame::AddRef
  1003.  * CPatronFrame::Release
  1004.  */
  1005.  
  1006. STDMETHODIMP CPatronFrame::QueryInterface(REFIID riid, PPVOID ppv)
  1007.     {
  1008.     //We only know IUnknown and IOleInPlaceFrame
  1009.     *ppv=NULL;
  1010.  
  1011.     if (IID_IUnknown==riid || IID_IOleInPlaceUIWindow==riid
  1012.         || IID_IOleWindow==riid || IID_IOleInPlaceFrame==riid)
  1013.         *ppv=(LPOLEINPLACEFRAME)this;
  1014.  
  1015.     if (NULL!=*ppv)
  1016.         {
  1017.         ((LPUNKNOWN)*ppv)->AddRef();
  1018.         return NOERROR;
  1019.         }
  1020.  
  1021.     return ResultFromScode(E_NOINTERFACE);
  1022.     }
  1023.  
  1024.  
  1025. STDMETHODIMP_(ULONG) CPatronFrame::AddRef(void)
  1026.     {
  1027.     return ++m_cRef;
  1028.     }
  1029.  
  1030. STDMETHODIMP_(ULONG) CPatronFrame::Release(void)
  1031.     {
  1032.     /*
  1033.      * We don't do anything with this since we're not controlled
  1034.      * by a reference count as far as in-place stuff is concerned.
  1035.      */
  1036.     return --m_cRef;
  1037.     }
  1038.  
  1039.  
  1040.  
  1041.  
  1042. /*
  1043.  * CPatronFrame::GetWindow
  1044.  *
  1045.  * Purpose:
  1046.  *  Retrieves the handle of the window associated with the object
  1047.  *  on which this interface is implemented.
  1048.  *
  1049.  * Parameters:
  1050.  *  phWnd           HWND * in which to store the window handle.
  1051.  *
  1052.  * Return Value:
  1053.  *  HRESULT         NOERROR if successful, E_FAIL if there is no
  1054.  *                  window.
  1055.  */
  1056.  
  1057. STDMETHODIMP CPatronFrame::GetWindow(HWND *phWnd)
  1058.     {
  1059.     *phWnd=m_hWnd;
  1060.     return NOERROR;
  1061.     }
  1062.  
  1063.  
  1064.  
  1065.  
  1066. /*
  1067.  * CPatronFrame::ContextSensitiveHelp
  1068.  *
  1069.  * Purpose:
  1070.  *  Instructs the object on which this interface is implemented to
  1071.  *  enter or leave a context-sensitive help mode.
  1072.  *
  1073.  * Parameters:
  1074.  *  fEnterMode      BOOL TRUE to enter the mode, FALSE otherwise.
  1075.  *
  1076.  * Return Value:
  1077.  *  HRESULT         NOERROR
  1078.  */
  1079.  
  1080. STDMETHODIMP CPatronFrame::ContextSensitiveHelp(BOOL fEnterMode)
  1081.     {
  1082.     /*
  1083.      * Don't bother if there is no active object since we don't do
  1084.      * context help on our own.
  1085.      */
  1086.     if (NULL==m_pIOleIPActiveObject)
  1087.         return NOERROR;
  1088.  
  1089.     /*
  1090.      * If the state changes, an MDI frame should call the same
  1091.      * function in all documents.  An SDI frame should just call
  1092.      * the active object, if it has one.
  1093.      */
  1094.  
  1095.     if (m_fInContextHelp!=fEnterMode)
  1096.         {
  1097.         m_fInContextHelp=fEnterMode;
  1098.  
  1099.        #ifdef MDI
  1100.         ((PCPatronClient)m_pCL)->CallContextHelpOnDocuments
  1101.            (fEnterMode);
  1102.        #else
  1103.         m_pIOleIPActiveObject->ContextSensitiveHelp(fEnterMode);
  1104.        #endif
  1105.         }
  1106.  
  1107.     return NOERROR;
  1108.     }
  1109.  
  1110.  
  1111.  
  1112.  
  1113. /*
  1114.  * CPatronFrame::GetBorder
  1115.  *
  1116.  * Purpose:
  1117.  *  Returns the rectangle in which the container is willing to
  1118.  *  negotiate about an object's adornments.
  1119.  *
  1120.  * Parameters:
  1121.  *  prcBorder       LPRECT in which to store the rectangle.
  1122.  *
  1123.  * Return Value:
  1124.  *  HRESULT         NOERROR if all is well, INPLACE_E_NOTOOLSPACE
  1125.  *                  if there is no negotiable space.
  1126.  */
  1127.  
  1128. STDMETHODIMP CPatronFrame::GetBorder(LPRECT prcBorder)
  1129.     {
  1130.     if (NULL==prcBorder)
  1131.         return ResultFromScode(E_INVALIDARG);
  1132.  
  1133.     /*
  1134.      * We return all the client area space sans the StatStrip,
  1135.      * which we control
  1136.      */
  1137.     GetClientRect(m_hWnd, prcBorder);
  1138.     prcBorder->bottom-=CYSTATSTRIP;
  1139.  
  1140.     return NOERROR;
  1141.     }
  1142.  
  1143.  
  1144.  
  1145.  
  1146. /*
  1147.  * CPatronFrame::RequestBorderSpace
  1148.  *
  1149.  * Purpose:
  1150.  *  Asks the container if it can surrender the amount of space
  1151.  *  in pBW that the object would like for it's adornments.  The
  1152.  *  container does nothing but validate the spaces on this call.
  1153.  *
  1154.  * Parameters:
  1155.  *  pBW             LPCBORDERWIDTHS containing the requested space.
  1156.  *                  The values are the amount of space requested
  1157.  *                  from each side of the relevant window.
  1158.  *
  1159.  * Return Value:
  1160.  *  HRESULT         NOERROR if we can give up space,
  1161.  *                  INPLACE_E_NOTOOLSPACE otherwise.
  1162.  */
  1163.  
  1164. STDMETHODIMP CPatronFrame::RequestBorderSpace(LPCBORDERWIDTHS pBW)
  1165.     {
  1166.     //Everything is fine with us, so always return an OK.
  1167.     return NOERROR;
  1168.     }
  1169.  
  1170.  
  1171.  
  1172.  
  1173. /*
  1174.  * CPatronFrame::SetBorderSpace
  1175.  *
  1176.  * Purpose:
  1177.  *  Called when the object now officially requests that the
  1178.  *  container surrender border space it previously allowed
  1179.  *  in RequestBorderSpace.  The container should resize windows
  1180.  *  appropriately to surrender this space.
  1181.  *
  1182.  * Parameters:
  1183.  *  pBW             LPCBORDERWIDTHS containing the amount of space
  1184.  *                  from each side of the relevant window that the
  1185.  *                  object is now reserving.
  1186.  *
  1187.  * Return Value:
  1188.  *  HRESULT         NOERROR
  1189.  */
  1190.  
  1191. STDMETHODIMP CPatronFrame::SetBorderSpace(LPCBORDERWIDTHS pBW)
  1192.     {
  1193.     RECT            rc;
  1194.     POINT           pt1, pt2;
  1195.     PCPatronDoc     pDoc;
  1196.  
  1197.     pDoc=(PCPatronDoc)m_pCL->ActiveDocument();
  1198.  
  1199.     /*
  1200.      * If pBW is NULL, the object is not interested in tools, so we
  1201.      * can just leave ours up if we want.
  1202.      */
  1203.  
  1204.     if (NULL==pBW)
  1205.         {
  1206.         if (NULL!=pDoc)
  1207.             {
  1208.             pDoc->NoObjectFrameTools(TRUE, TRUE);
  1209.  
  1210.             /*
  1211.              * In some cases IOleInPlaceSite::OnUIActivate might
  1212.              * have been called before SetBorderSpace, so we might
  1213.              * have already hidden our tools (OnUIActivate calls
  1214.              * pDoc->NoObjectFrameTools before we set it to TRUE
  1215.              * here).  So we have to insure they are visible now
  1216.              * by a call to ShowUIAndTools.
  1217.              */
  1218.             if (!m_fOurToolsShowing)
  1219.                 ShowUIAndTools(TRUE, FALSE);
  1220.             }
  1221.  
  1222.         return NOERROR;
  1223.         }
  1224.  
  1225.     if (NULL!=pDoc)
  1226.         pDoc->NoObjectFrameTools(FALSE, TRUE);
  1227.  
  1228.     /*
  1229.      * This tells CFrame::FMessageHook (WM_SIZE) how much space
  1230.      * to reserve off the sides when resizing the client when
  1231.      * the frame is resized.
  1232.      */
  1233.  
  1234.     m_cyTop   =pBW->top;
  1235.     m_cyBottom=pBW->bottom;
  1236.     m_cxLeft  =pBW->left;
  1237.     m_cxRight =pBW->right;
  1238.  
  1239.  
  1240.     //Get the current offset of the client
  1241.     GetWindowRect(m_pCL->Window(), &rc);
  1242.     SETPOINT(pt1, rc.left, rc.top);
  1243.     SETPOINT(pt2, rc.right, rc.bottom);
  1244.     ScreenToClient(m_hWnd, &pt1);
  1245.     ScreenToClient(m_hWnd, &pt2);
  1246.  
  1247.     /*
  1248.      * Now move the client, keeping documents steady. pBW->left-pt.x
  1249.      * and pBW->top-pt.y are the deltas for the documents.
  1250.      */
  1251.  
  1252.     GetClientRect(m_hWnd, &rc);
  1253.     rc.left+=pBW->left;
  1254.     rc.right-=pBW->right;
  1255.     rc.top+=pBW->top;
  1256.     rc.bottom-=pBW->bottom+CYSTATSTRIP; //Remember the status line
  1257.  
  1258.     //Only bother the client if necessary.
  1259.     if (!(pt1.x==rc.left && pt1.y==rc.top
  1260.         && pt2.x==rc.right && pt2.y==rc.bottom))
  1261.         {
  1262.         ((PCPatronClient)m_pCL)->MoveWithoutFamily(&rc
  1263.             , pBW->left-pt1.x, pBW->top-pt1.y);
  1264.         }
  1265.  
  1266.     return NOERROR;
  1267.     }
  1268.  
  1269.  
  1270.  
  1271.  
  1272. /*
  1273.  * CPatronFrame::SetActiveObject
  1274.  *
  1275.  * Purpose:
  1276.  *  Provides the container with the object's IOleInPlaceActiveObject
  1277.  *  pointer
  1278.  *
  1279.  * Parameters:
  1280.  *  pIIPActiveObj   LPOLEINPLACEACTIVEOBJECT of interest.
  1281.  *  pszObj          LPCOLESTR naming the object.  Not used.
  1282.  *
  1283.  * Return Value:
  1284.  *  HRESULT         NOERROR
  1285.  */
  1286.  
  1287. STDMETHODIMP CPatronFrame::SetActiveObject
  1288.     (LPOLEINPLACEACTIVEOBJECT pIIPActiveObj, LPCOLESTR pszObj)
  1289.     {
  1290.     if (NULL!=m_pIOleIPActiveObject)
  1291.         m_pIOleIPActiveObject->Release();
  1292.  
  1293.     //NULLs m_pIOleIPActiveObject if pIIPActiveObj is NULL
  1294.     m_pIOleIPActiveObject=pIIPActiveObj;
  1295.  
  1296.     if (NULL!=m_pIOleIPActiveObject)
  1297.         m_pIOleIPActiveObject->AddRef();
  1298.  
  1299.     return NOERROR;
  1300.     }
  1301.  
  1302.  
  1303.  
  1304.  
  1305. /*
  1306.  * CPatronFrame::InsertMenus
  1307.  *
  1308.  * Purpose:
  1309.  *  Instructs the container to place its in-place menu items where
  1310.  *  necessary in the given menu and to fill in elements 0, 2, and 4
  1311.  *  of the OLEMENUGROUPWIDTHS array to indicate how many top-level
  1312.  *  items are in each group.
  1313.  *
  1314.  * Parameters:
  1315.  *  hMenu           HMENU in which to add popups.
  1316.  *  pMGW            LPOLEMENUGROUPWIDTHS in which to store the
  1317.  *                  width of each container menu group.
  1318.  *
  1319.  * Return Value:
  1320.  *  HRESULT         NOERROR
  1321.  */
  1322.  
  1323. STDMETHODIMP CPatronFrame::InsertMenus(HMENU hMenu
  1324.     , LPOLEMENUGROUPWIDTHS pMGW)
  1325.     {
  1326.     /*
  1327.      * Here Patron needs to copy it's File, Page, and Window menu
  1328.      * items into the object-supplied menu, meaning that we have
  1329.      * three menu groups.  The actual handles of these popup menus
  1330.      * are already in CPatronFrame::m_phMenu where File is element
  1331.      * 0 and Page is element 2, and in m_hMenuWindow which is the
  1332.      * Window menu.  The latter we do only for MDI, of course.
  1333.      */
  1334.  
  1335.     InsertMenu(hMenu, 0, MF_BYPOSITION | MF_POPUP, (UINT)m_phMenu[0]
  1336.         , PSZ(IDS_FILEMENU));
  1337.     InsertMenu(hMenu, 1, MF_BYPOSITION | MF_POPUP, (UINT)m_phMenu[2]
  1338.         , PSZ(IDS_PAGEMENU));
  1339.  
  1340.     pMGW->width[0]=1;
  1341.     pMGW->width[2]=1;
  1342.  
  1343.    #ifdef MDI
  1344.     InsertMenu(hMenu, 2, MF_BYPOSITION | MF_POPUP
  1345.        , (UINT)m_hMenuWindow, PSZ(IDS_WINDOWMENU));
  1346.  
  1347.     pMGW->width[4]=1;
  1348.    #else
  1349.     pMGW->width[4]=0;
  1350.    #endif
  1351.  
  1352.     return NOERROR;
  1353.     }
  1354.  
  1355.  
  1356.  
  1357.  
  1358. /*
  1359.  * CPatronFrame::SetMenu
  1360.  *
  1361.  * Purpose:
  1362.  *  Instructs the container to replace whatever menu it's currently
  1363.  *  using with the given menu and to call OleSetMenuDescritor so OLE
  1364.  *  knows to whom to dispatch messages.
  1365.  *
  1366.  * Parameters:
  1367.  *  hMenu           HMENU to show.
  1368.  *  hOLEMenu        HOLEMENU to the menu descriptor.
  1369.  *  hWndObj         HWND of the active object to which messages are
  1370.  *                  dispatched.
  1371.  * Return Value:
  1372.  *  HRESULT         NOERROR
  1373.  */
  1374.  
  1375. STDMETHODIMP CPatronFrame::SetMenu(HMENU hMenu
  1376.     , HOLEMENU hOLEMenu, HWND hWndObj)
  1377.     {
  1378.     HRESULT         hr;
  1379.     PCPatronClient  pCL=(PCPatronClient)m_pCL;
  1380.  
  1381.     /*
  1382.      * Our responsibilities here are to put the menu on the frame
  1383.      * window and call OleSetMenuDescriptor.
  1384.      * CPatronClient::SetMenu which we call here takes care of
  1385.      * MDI/SDI differences.
  1386.      *
  1387.      * We also want to save the object's hWnd for use in WM_SETFOCUS
  1388.      * processing.
  1389.      */
  1390.  
  1391.     if (NULL==hMenu)
  1392.         {
  1393.         m_hWndObj=NULL;
  1394.  
  1395.         //Prevent redundant calls, or debug warnings on startup.
  1396.         if (NULL==m_hMenuTop)
  1397.             return NOERROR;
  1398.  
  1399.         hMenu=m_hMenuTop;
  1400.         m_hMenuTop=NULL;
  1401.         }
  1402.     else
  1403.         {
  1404.         m_hMenuTop=m_hMenuOrg;
  1405.         m_hWndObj=hWndObj;
  1406.         }
  1407.  
  1408.     pCL->SetMenu(m_hWnd, hMenu, m_hMenuWindow);
  1409.     hr=OleSetMenuDescriptor(hOLEMenu, m_hWnd, hWndObj, NULL, NULL);
  1410.     return hr;
  1411.     }
  1412.  
  1413.  
  1414.  
  1415.  
  1416. /*
  1417.  * CPatronFrame::RemoveMenus
  1418.  *
  1419.  * Purpose:
  1420.  *  Asks the container to remove any menus it put into hMenu in
  1421.  *  InsertMenus.
  1422.  *
  1423.  * Parameters:
  1424.  *  hMenu           HMENU from which to remove the container's
  1425.  *                  items.
  1426.  *
  1427.  * Return Value:
  1428.  *  HRESULT         NOERROR
  1429.  */
  1430.  
  1431. STDMETHODIMP CPatronFrame::RemoveMenus(HMENU hMenu)
  1432.     {
  1433.     int         cItems, i, j;
  1434.     HMENU       hMenuT;
  1435.  
  1436.     /*
  1437.      * To be defensive, loop through this menu removing anything
  1438.      * we recognize (that is, anything in m_phMenu) just in case
  1439.      * the server didn't clean it up right.  At least we can
  1440.      * give ourselves the prophylactic benefit.
  1441.      */
  1442.  
  1443.     if (NULL==hMenu)
  1444.         return NOERROR;
  1445.  
  1446.     cItems=GetMenuItemCount(hMenu);
  1447.  
  1448.     /*
  1449.      * Walk backwards down the menu.  For each popup, see if it
  1450.      * matches any other popup we know about, and if so, remove
  1451.      * it from the shared menu.
  1452.      */
  1453.     for (i=cItems; i >=0; i--)
  1454.         {
  1455.         hMenuT=GetSubMenu(hMenu, i);
  1456.  
  1457.         for (j=0; j <= CMENUS; j++)
  1458.             {
  1459.             if (hMenuT==m_phMenu[j])
  1460.                 RemoveMenu(hMenu, i, MF_BYPOSITION);
  1461.             }
  1462.         }
  1463.  
  1464.     //The menu should now be empty.
  1465.     return NOERROR;
  1466.     }
  1467.  
  1468.  
  1469.  
  1470.  
  1471. /*
  1472.  * CPatronFrame::SetStatusText
  1473.  *
  1474.  * Purpose:
  1475.  *  Asks the container to place some text in a status line, if one
  1476.  *  exists.  If the container does not have a status line it
  1477.  *  should return E_FAIL here in which case the object could
  1478.  *  display its own.
  1479.  *
  1480.  * Parameters:
  1481.  *  pszText         LPCOLESTR to display.
  1482.  *
  1483.  * Return Value:
  1484.  *  HRESULT         NOERROR if successful, S_TRUNCATED if not all
  1485.  *                  of the text could be displayed, or E_FAIL if
  1486.  *                  the container has no status line.
  1487.  */
  1488.  
  1489. STDMETHODIMP CPatronFrame::SetStatusText(LPCOLESTR pszText)
  1490.     {
  1491.     /*
  1492.      * Just send this to the StatStrip.  Unfortunately it won't tell
  1493.      * us about truncation.  Oh well, we'll just act like it worked.
  1494.      */
  1495.    #ifdef WIN32ANSI
  1496.     TCHAR       szTemp[256];
  1497.  
  1498.     WideCharToMultiByte(CP_ACP, 0, pszText, -1, szTemp, 256
  1499.         , NULL, NULL);
  1500.     m_pSL->MessageSet(szTemp);
  1501.    #else
  1502.     m_pSL->MessageSet((LPTSTR)pszText);
  1503.    #endif
  1504.  
  1505.     return NOERROR;
  1506.     }
  1507.  
  1508.  
  1509.  
  1510.  
  1511. /*
  1512.  * CPatronFrame::EnableModeless
  1513.  *
  1514.  * Purpose:
  1515.  *  Instructs the container to show or hide any modeless popup
  1516.  *  windows that it may be using.
  1517.  *
  1518.  * Parameters:
  1519.  *  fEnable         BOOL indicating to enable/show the windows
  1520.  *                  (TRUE) or to hide them (FALSE).
  1521.  *
  1522.  * Return Value:
  1523.  *  HRESULT         NOERROR
  1524.  */
  1525.  
  1526. STDMETHODIMP CPatronFrame::EnableModeless(BOOL fEnable)
  1527.     {
  1528.     return NOERROR;
  1529.     }
  1530.  
  1531.  
  1532.  
  1533.  
  1534. /*
  1535.  * CPatronFrame::TranslateAccelerator
  1536.  *
  1537.  * Purpose:
  1538.  *  When dealing with an in-place object from an EXE server, this
  1539.  *  is called to give the container a chance to process accelerators
  1540.  *  after the server has looked at the message.
  1541.  *
  1542.  * Parameters:
  1543.  *  pMSG            LPMSG for the container to examine.
  1544.  *  wID             WORD the identifier in the container's
  1545.  *                  accelerator table (from IOleInPlaceSite
  1546.  *                  ::GetWindowContext) for this message (OLE does
  1547.  *                  some translation before calling).
  1548.  *
  1549.  * Return Value:
  1550.  *  HRESULT         NOERROR if the keystroke was used,
  1551.  *                  S_FALSE otherwise.
  1552.  */
  1553.  
  1554. STDMETHODIMP CPatronFrame::TranslateAccelerator(LPMSG pMSG, WORD wID)
  1555.     {
  1556.     SCODE       sc;
  1557.  
  1558.     /*
  1559.      * wID already has anything translated from m_hAccelIP for us,
  1560.      * so we can just check for the commands we want and process
  1561.      * them instead of calling TranslateAccelerator which would be
  1562.      * redundant and which also has a possibility of dispatching to
  1563.      * the wrong window.
  1564.      */
  1565.     if ((IDM_PAGENEWPAGE <= wID && IDM_PAGELASTPAGE >= wID)
  1566.         || IDM_OPENOBJECT==wID || IDM_ENTERCONTEXTHELP==wID
  1567.         || IDM_ESCAPECONTEXTHELP==wID)
  1568.         {
  1569.         //wID properly expands to 32-bits
  1570.         OnCommand(m_hWnd, (WPARAM)wID, 0L);
  1571.         sc=S_OK;
  1572.         }
  1573.    #ifdef MDI
  1574.     else if (TranslateMDISysAccel(m_pCL->Window(), pMSG))
  1575.         sc=S_OK;
  1576.    #endif
  1577.     else
  1578.         sc=S_FALSE;
  1579.  
  1580.     return ResultFromScode(sc);
  1581.     }
  1582.  
  1583. //End CHAPTER22MOD
  1584.