home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / BOCOLE.PAK / BOLEIPS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  19.6 KB  |  768 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectComponents
  3. // Copyright (c) 1994, 1996 by Borland International, All Rights Reserved
  4. //
  5. // $Revision:   2.4  $
  6. //
  7. //     Implements the Bolero half of the OLE2 in-process server object.
  8. //    BOleIps objects impersonate the client application from the point of 
  9. //    view of the Bolero customer who's writing a server object (IPart)
  10. //----------------------------------------------------------------------------
  11. #include "BOleIPS.h"
  12. #include "BOleCMan.h"
  13.  
  14.  
  15. //**************************************************************************
  16. //
  17. // BOleInProcHandler implementation
  18. //
  19. //**************************************************************************
  20.  
  21. BOleInProcHandler::BOleInProcHandler(BOleClassManager *pFact,
  22.     IBUnknownMain * pOuter, BOleService *pSvc ) : BOleComponent(pFact, pOuter)
  23. {
  24.     pService = pSvc;
  25.     pDefHandler = NULL;
  26.     pDefIPAO = NULL;
  27.     pDefSite = NULL;
  28.     pAdviseView = NULL;
  29.     dwAdviseFlags = 0L;
  30.     dwAdviseAspects = 0L;
  31. }
  32.  
  33. BOleInProcHandler::~BOleInProcHandler ()
  34. {
  35.     if (pDefHandler)
  36.         pDefHandler->Release();
  37.  
  38.     if (pAdviseView)
  39.         pAdviseView->Release();
  40. }
  41.  
  42. HRESULT _IFUNC BOleInProcHandler::QueryInterfaceMain(REFIID iid, LPVOID FAR *ppv)
  43. {
  44.     HRESULT hr = ResultFromScode(E_NOINTERFACE);
  45.     *ppv = NULL;
  46.  
  47.     // interfaces
  48.     if (SUCCEEDED(hr = IViewObject2_QueryInterface(this, iid, ppv))) {
  49.     }
  50.     else if (SUCCEEDED(hr = IOleInPlaceActiveObject_QueryInterface(this, iid, ppv))) {
  51.     }
  52.     else if (SUCCEEDED(hr = IBSite_QueryInterface(this, iid, ppv))) {
  53.     }
  54.     // base classes
  55.     else if (SUCCEEDED(hr = BOleComponent::QueryInterfaceMain(iid, ppv))) {
  56.     }
  57.     // helpers
  58.     else if (pDefHandler && SUCCEEDED(hr = pDefHandler->QueryInterface(iid, ppv))) {
  59.     }
  60.     return hr;
  61. }
  62.  
  63.  
  64. //**************************************************************************
  65. //
  66. //  IViewObject implementation
  67. //
  68. //**************************************************************************
  69.  
  70. HRESULT _IFUNC BOleInProcHandler::Draw (DWORD dwDrawAspect, LONG lindex,
  71.     void FAR* pvAspect, DVTARGETDEVICE FAR * ptd,
  72.     HDC hicTargetDev,
  73.     HDC hdcDraw,
  74.     LPCRECTL lprcBounds,
  75.     LPCRECTL lprcWBounds,
  76.     BOOL(CALLBACK * pfnContinue)(DWORD),
  77.     DWORD dwContinue)
  78. {
  79.     HRESULT hr = ResultFromScode(VIEW_E_DRAW);
  80.     // Let provider Draw
  81.     // unless its iconic which the handler does for us
  82.     //
  83.     if (dwDrawAspect != DVASPECT_ICON) {
  84.         if (SUCCEEDED(pProvider->Draw(hdcDraw, lprcBounds,
  85.             // lprcWBounds is NULL unless this is a metafile
  86.             //
  87.             lprcWBounds ? lprcWBounds : lprcBounds,
  88.             (BOleAspect) dwDrawAspect))) {
  89.             hr = NOERROR;
  90.         }
  91.     }
  92.     
  93.     // If this failed let the default handler render the Metafile
  94.     //
  95.     if (!SUCCEEDED(hr)) {
  96.         IViewObject *pVO;
  97.         if (SUCCEEDED(pDefHandler->QueryInterface(IID_IViewObject, &(LPVOID)pVO))) {
  98.             hr = pVO->Draw(dwDrawAspect, lindex, pvAspect, ptd, hicTargetDev, hdcDraw, lprcBounds, lprcWBounds, pfnContinue, dwContinue);
  99.             pVO->Release();
  100.         }
  101.     }
  102.     return hr;
  103. }
  104.  
  105. HRESULT _IFUNC BOleInProcHandler::GetColorSet (
  106.     DWORD dwDrawAspect,
  107.     LONG lindex,
  108.     void FAR* pvAspect,
  109.     DVTARGETDEVICE FAR * ptd,
  110.     HDC hicTargetDev,
  111.     LPLOGPALETTE FAR* ppColorSet)
  112. {
  113.     // what color pallete will the object use when rendering
  114.     return pPart->GetPalette (ppColorSet);
  115. }
  116.  
  117. HRESULT _IFUNC BOleInProcHandler::Freeze (
  118.     DWORD dwDrawAspect,
  119.     LONG lindex,
  120.     void FAR* pvAspect,
  121.     DWORD FAR* pdwFreeze)
  122. {
  123.     HRESULT hr;
  124.     IViewObject *pVO;
  125.     if (SUCCEEDED(pDefHandler->QueryInterface(IID_IViewObject, &(LPVOID)pVO))) {
  126.         hr = pVO->Freeze(dwDrawAspect, lindex, pvAspect, pdwFreeze);
  127.         pVO->Release();
  128.         return hr;
  129.     }
  130.     // the object shouldn't change the rendering
  131.     return ResultFromScode (E_FAIL);
  132. }
  133.  
  134. HRESULT _IFUNC BOleInProcHandler::Unfreeze (DWORD dwFreeze)
  135. {
  136.     HRESULT hr;
  137.     IViewObject *pVO;
  138.     if (SUCCEEDED(pDefHandler->QueryInterface(IID_IViewObject, &(LPVOID)pVO))) {
  139.         hr = pVO->Unfreeze(dwFreeze);
  140.         pVO->Release();
  141.         return hr;
  142.     }
  143.     // ok it can change again
  144.     return NOERROR;
  145. }
  146.  
  147. HRESULT _IFUNC BOleInProcHandler::SetAdvise (DWORD aspects, DWORD advf, LPADVISESINK pAdvSink)
  148. {
  149.     dwAdviseAspects = aspects;
  150.     dwAdviseFlags = advf;
  151.     if (pAdviseView)
  152.         pAdviseView->Release();
  153.     pAdviseView = pAdvSink;
  154.   
  155.     if (pAdviseView)
  156.       pAdviseView->AddRef ();
  157.  
  158.  
  159.     if (advf | ADVF_PRIMEFIRST)
  160.         // Cause advises now
  161.         //
  162.         Invalidate(BOLE_INVAL_DATA | BOLE_INVAL_VIEW);
  163.  
  164.     return NOERROR;
  165. }
  166.  
  167. HRESULT _IFUNC BOleInProcHandler::GetAdvise (DWORD FAR* pAspects,
  168.     DWORD FAR* pAdvf, LPADVISESINK FAR* ppAdvSink)
  169. {
  170.     if (ppAdvSink)  {
  171.         pAdviseView->AddRef();
  172.  
  173.         *ppAdvSink = pAdviseView;
  174.         *pAspects = dwAdviseAspects;
  175.         *pAdvf = dwAdviseFlags;
  176.         return NOERROR;
  177.     }
  178.   else {
  179.       return ResultFromScode (E_INVALIDARG);
  180.   }
  181. }
  182.     
  183. HRESULT _IFUNC BOleInProcHandler::GetExtent(DWORD dwDrawAspect,
  184.     LONG lindex, DVTARGETDEVICE FAR *ptd, LPSIZEL lpsizel)
  185. {
  186.     if (dwDrawAspect != DVASPECT_CONTENT)
  187.         return ResultFromScode (E_FAIL);
  188.  
  189.     if (!lpsizel)
  190.         return ResultFromScode (E_INVALIDARG);
  191.  
  192.     SIZE s;
  193.     HRESULT hr = pProvider->GetPartSize (&s);
  194.  
  195.     lpsizel->cx = MAP_PIX_TO_LOGHIM (s.cx, BOleService::pixPerIn.x);
  196.     lpsizel->cy = MAP_PIX_TO_LOGHIM (s.cy, BOleService::pixPerIn.y);
  197.  
  198.     return hr;
  199. }
  200.  
  201. //**************************************************************************
  202. //
  203. // IBSite methods (override this interface on aggregated BOleIPSServer
  204. // to implement Invalidate).
  205. //
  206. //**************************************************************************
  207.  
  208. HRESULT _IFUNC BOleInProcHandler::Init(PIBDataProvider pDP, 
  209.     PIBPart pP, LPCOLESTR szProgId, BOOL fHatchWnd)
  210. {
  211.     HRESULT hr;
  212.     // Initialize back pointers
  213.     //
  214.     pProvider = pDP;
  215.     pPart = pP;
  216.  
  217.     CLSID cid;
  218.     if (SUCCEEDED(hr = CLSIDFromProgID(szProgId, &cid))) {
  219.     
  220.         // create the default handler, which will launch the exe server
  221.         //
  222.         OleCreateEmbeddingHelper(cid, AsPIUnknown(pObjOuter),
  223.             EMBDHLP_INPROC_HANDLER | EMBDHLP_DELAYCREATE, NULL, IID_IUnknown,
  224.             &(LPVOID)pDefHandler);
  225.  
  226.         // initialize default IPS  
  227.         //
  228.         hr = pDefHandler->QueryInterface(IID_IBSite, &(LPVOID)pDefSite);
  229.         if (SUCCEEDED(hr)) {
  230.             pDefSite->Release();     // prevent aggregation deadlock
  231.             hr = pDefSite->Init(pDP, pP, szProgId, fHatchWnd);
  232.         }
  233.     }
  234.     return hr;
  235. }
  236.  
  237. HRESULT _IFUNC BOleInProcHandler::SiteShow(BOOL b)
  238. {
  239.     return pDefSite->SiteShow(b);
  240. }
  241.  
  242.  
  243.  
  244. HRESULT _IFUNC BOleInProcHandler::DiscardUndo()
  245. {
  246.     return pDefSite->DiscardUndo();
  247. }
  248.  
  249. HRESULT _IFUNC BOleInProcHandler::GetSiteRect(LPRECT r1, LPRECT r2)
  250. {
  251.     return pDefSite->GetSiteRect(r1,r2);
  252. }
  253.  
  254. HRESULT _IFUNC BOleInProcHandler::SetSiteRect(LPCRECT r)
  255. {
  256.     return pDefSite->SetSiteRect(r);
  257. }
  258.  
  259. HRESULT _IFUNC BOleInProcHandler::SetSiteExtent(LPCSIZE s)
  260. {
  261.     return pDefSite->SetSiteExtent(s);
  262. }
  263.  
  264. void _IFUNC BOleInProcHandler::OnSetFocus(BOOL b)
  265. {
  266.     pDefSite->OnSetFocus(b);
  267. }
  268.  
  269. void _IFUNC BOleInProcHandler::Disconnect ()
  270. {
  271.     pDefSite->Disconnect ();
  272. }
  273.  
  274. HRESULT _IFUNC BOleInProcHandler::GetZoom( BOleScaleFactor *pScale)
  275. {
  276.     return pDefSite->GetZoom(pScale);
  277. }
  278.  
  279. void _IFUNC BOleInProcHandler::Invalidate( BOleInvalidate b)
  280. {
  281.     if ((b & BOLE_INVAL_VIEW) && pAdviseView) {
  282.         pAdviseView->OnViewChange(dwAdviseAspects, -1);
  283.         if (dwAdviseFlags == ADVF_ONLYONCE) {
  284.             pAdviseView->Release();
  285.             pAdviseView = NULL;
  286.             dwAdviseAspects = 0L;
  287.             dwAdviseFlags = 0L;
  288.         }
  289.     }
  290.     if (b)  // VIEW or DATA or PERSIST remains invalid
  291.         pDefSite->Invalidate(b);
  292. }
  293.  
  294. //**************************************************************************
  295. //
  296. // IOleInPlaceActiveObject
  297. //
  298. // This interface is overridden to pre-process translator and other messages
  299. // from the container's message loop.
  300. //
  301. //**************************************************************************
  302.  
  303. IOleInPlaceActiveObject * BOleInProcHandler::DefaultIPAO()
  304. {
  305.     if (!pDefIPAO) {
  306.         // Initialize pointer to default in place active object implementation
  307.         // 
  308.         if (SUCCEEDED(pDefHandler->QueryInterface(IID_IOleInPlaceActiveObject,
  309.             &(LPVOID) pDefIPAO))) {
  310.             pDefIPAO->Release();    // prevent aggregation deadlock
  311.         }
  312.     }
  313.     return pDefIPAO;
  314. }
  315.  
  316. HRESULT _IFUNC BOleInProcHandler::GetWindow(HWND FAR *phwnd)
  317. {
  318.     return DefaultIPAO()->GetWindow (phwnd);
  319. }
  320.  
  321. HRESULT _IFUNC BOleInProcHandler::ContextSensitiveHelp(BOOL fEnterMode)
  322. {
  323.     return DefaultIPAO()->ContextSensitiveHelp (fEnterMode);
  324. }
  325.  
  326. //    Allow DLL Server to translate accelerators
  327. //
  328. HRESULT _IFUNC BOleInProcHandler::TranslateAccelerator (LPMSG lpmsg)
  329. {
  330.     return pService->GetApplication()->Accelerator(lpmsg);
  331. }
  332.  
  333. HRESULT _IFUNC BOleInProcHandler::OnFrameWindowActivate (BOOL fActivate)
  334. {
  335.     return DefaultIPAO()->OnFrameWindowActivate (fActivate);
  336. }
  337.  
  338. HRESULT _IFUNC BOleInProcHandler::OnDocWindowActivate (BOOL fActivate)
  339. {
  340.     return DefaultIPAO()->OnDocWindowActivate (fActivate);
  341. }
  342.  
  343. HRESULT _IFUNC BOleInProcHandler::ResizeBorder (LPCRECT lprectBorder,
  344.         LPOLEINPLACEUIWINDOW lpUIWindow, BOOL fFrameWindow)         
  345. {
  346.     return DefaultIPAO()->ResizeBorder (lprectBorder, lpUIWindow, fFrameWindow);
  347. }
  348.  
  349. HRESULT _IFUNC BOleInProcHandler::EnableModeless (BOOL fEnable)
  350. {
  351.     return DefaultIPAO()->EnableModeless (fEnable);
  352. }
  353.             
  354.         
  355.  
  356. //**************************************************************************
  357. //
  358. // BOleInProcServer implementation
  359. //
  360. //**************************************************************************
  361.  
  362. BOleInProcServer::BOleInProcServer(BOleClassManager *pFact,
  363.     IBUnknownMain * pOuter, BOleService *pSvc ) : BOleSite(pFact, pOuter, pSvc)
  364. {
  365.     pService = pSvc;
  366.     pDefHandler = NULL;
  367.     pAdviseView = NULL;
  368.     dwAdviseFlags = 0L;
  369.     dwAdviseAspects = 0L;
  370.     exCon = 0L;
  371.     fRunningMode = FALSE;
  372. }
  373.  
  374. BOleInProcServer::~BOleInProcServer ()
  375. {
  376.     if (pDefHandler)
  377.         pDefHandler->Release();
  378.  
  379.     if (pAdviseView)
  380.         pAdviseView->Release();
  381. }
  382.  
  383. HRESULT _IFUNC BOleInProcServer::QueryInterfaceMain(REFIID iid, LPVOID FAR *ppv)
  384. {
  385.     HRESULT hr = ResultFromScode(E_NOINTERFACE);
  386.     *ppv = NULL;
  387.  
  388.     // interfaces
  389.     if (SUCCEEDED(hr = IViewObject2_QueryInterface(this, iid, ppv))) {
  390.     }
  391.     else if (SUCCEEDED(hr = IRunnableObject_QueryInterface(this, iid, ppv))) {
  392.     }
  393.     else if (SUCCEEDED(hr = IExternalConnection_QueryInterface(this, iid, ppv))) {
  394.     }
  395.     // base classes
  396.     else if (SUCCEEDED(hr = BOleSite::QueryInterfaceMain(iid, ppv))) {
  397.     }
  398.     // helpers
  399.     else if (pDefHandler && SUCCEEDED(hr = pDefHandler->QueryInterface(iid, ppv))) {
  400.     }
  401.     return hr;
  402. }
  403.  
  404.  
  405. //**************************************************************************
  406. //
  407. //  IViewObject implementation
  408. //
  409. //**************************************************************************
  410.  
  411. HRESULT _IFUNC BOleInProcServer::Draw (DWORD dwDrawAspect, LONG lindex,
  412.     void FAR* pvAspect, DVTARGETDEVICE FAR * ptd,
  413.     HDC hicTargetDev,
  414.     HDC hdcDraw,
  415.     LPCRECTL lprcBounds,
  416.     LPCRECTL lprcWBounds,
  417.     BOOL(CALLBACK * pfnContinue)(DWORD),
  418.     DWORD dwContinue)
  419. {
  420.     HRESULT hr = ResultFromScode(VIEW_E_DRAW);
  421.     // Let provider Draw unless its iconic which the handler does for us
  422.     //
  423.     if (dwDrawAspect == DVASPECT_CONTENT) {
  424.         if (SUCCEEDED(pProvider->Draw(hdcDraw, lprcBounds,
  425.             // lprcWBounds is NULL unless this is a metafile
  426.             //
  427.             lprcWBounds ? lprcWBounds : lprcBounds,
  428.             (BOleAspect) dwDrawAspect))) {
  429.             hr = NOERROR;
  430.         }
  431.     }
  432.     
  433.     // If this failed let the default handler render the Metafile
  434.     //
  435.     if (!SUCCEEDED(hr)) {
  436.         IViewObject *pVO;
  437.         if (SUCCEEDED(pDefHandler->QueryInterface(IID_IViewObject, &(LPVOID)pVO))) {
  438.             hr = pVO->Draw(dwDrawAspect, lindex, pvAspect, ptd, hicTargetDev,
  439.                            hdcDraw, lprcBounds, lprcWBounds, pfnContinue,
  440.                            dwContinue);
  441.             pVO->Release();
  442.         }
  443.     }
  444.     return hr;
  445. }
  446.  
  447. HRESULT _IFUNC BOleInProcServer::GetColorSet (
  448.     DWORD dwDrawAspect,
  449.     LONG lindex,
  450.     void FAR* pvAspect,
  451.     DVTARGETDEVICE FAR * ptd,
  452.     HDC hicTargetDev,
  453.     LPLOGPALETTE FAR* ppColorSet)
  454. {
  455.     // what color pallete will the object use when rendering
  456.     return pPart->GetPalette (ppColorSet);
  457. }
  458.  
  459. HRESULT _IFUNC BOleInProcServer::Freeze (
  460.     DWORD dwDrawAspect,
  461.     LONG lindex,
  462.     void FAR* pvAspect,
  463.     DWORD FAR* pdwFreeze)
  464. {
  465.     HRESULT hr;
  466.     IViewObject *pVO;
  467.     if (SUCCEEDED(pDefHandler->QueryInterface(IID_IViewObject, &(LPVOID)pVO))) {
  468.         hr = pVO->Freeze(dwDrawAspect, lindex, pvAspect, pdwFreeze);
  469.         pVO->Release();
  470.         return hr;
  471.     }
  472.     // the object shouldn't change the rendering
  473.     return ResultFromScode (E_FAIL);
  474. }
  475.  
  476. HRESULT _IFUNC BOleInProcServer::Unfreeze (DWORD dwFreeze)
  477. {
  478.     HRESULT hr;
  479.     IViewObject *pVO;
  480.     if (SUCCEEDED(pDefHandler->QueryInterface(IID_IViewObject, &(LPVOID)pVO))) {
  481.         hr = pVO->Unfreeze(dwFreeze);
  482.         pVO->Release();
  483.         return hr;
  484.     }
  485.     // ok it can change again
  486.     return NOERROR;
  487. }
  488.  
  489. HRESULT _IFUNC BOleInProcServer::SetAdvise (DWORD aspects, DWORD advf, LPADVISESINK pAdvSink)
  490. {
  491.     dwAdviseAspects = aspects;
  492.     dwAdviseFlags = advf;
  493.     if (pAdviseView)
  494.         pAdviseView->Release();
  495.     pAdviseView = pAdvSink;
  496.  
  497.     if (pAdviseView)
  498.         pAdviseView->AddRef ();
  499.  
  500.  
  501.     if (advf & ADVF_PRIMEFIRST)
  502.         // Cause advises now
  503.         //
  504.         Invalidate(BOLE_INVAL_DATA | BOLE_INVAL_VIEW);
  505.  
  506.     return NOERROR;
  507. }
  508.  
  509. HRESULT _IFUNC BOleInProcServer::GetAdvise (DWORD FAR* pAspects,
  510.     DWORD FAR* pAdvf, LPADVISESINK FAR* ppAdvSink)
  511. {
  512.     if (ppAdvSink)  {
  513.         pAdviseView->AddRef();
  514.         
  515.         *ppAdvSink = pAdviseView;
  516.         *pAspects = dwAdviseAspects;
  517.         *pAdvf = dwAdviseFlags;
  518.         return NOERROR;
  519.     }
  520.   else {
  521.       return ResultFromScode (E_INVALIDARG);
  522.   }
  523. }
  524.     
  525. HRESULT _IFUNC BOleInProcServer::GetExtent(DWORD dwDrawAspect,
  526.     LONG lindex, DVTARGETDEVICE FAR *ptd, LPSIZEL lpsizel)
  527. {
  528.     HRESULT hr;
  529.     if (dwDrawAspect != DVASPECT_CONTENT) {
  530.         // Get extent from the cache.
  531.         //
  532.         IViewObject2 *pVO;
  533.         if (SUCCEEDED(pDefHandler->QueryInterface(IID_IViewObject2, &(LPVOID)pVO))) {
  534.             hr = pVO->GetExtent(dwDrawAspect, lindex, ptd, lpsizel);
  535.             pVO->Release();
  536.         }
  537.     }
  538.     else {
  539.         hr = BOleSite::GetExtent(dwDrawAspect, lpsizel);
  540.     }
  541.     return hr;
  542. }
  543.  
  544. HRESULT _IFUNC BOleInProcServer::GetExtent(DWORD dwDrawAspect, LPSIZEL lpsizel)
  545. {
  546.     HRESULT hr;
  547.     if (dwDrawAspect != DVASPECT_CONTENT) {
  548.         // Get extent from the cache.
  549.         //
  550.         IViewObject2 *pVO;
  551.         if (SUCCEEDED(pDefHandler->QueryInterface(IID_IViewObject2, &(LPVOID)pVO))) {
  552.             hr = pVO->GetExtent(dwDrawAspect, -1, NULL, lpsizel);
  553.             pVO->Release();
  554.         }
  555.     }
  556.     else {
  557.         hr = BOleSite::GetExtent(dwDrawAspect, lpsizel);
  558.     }
  559.     return hr;
  560. }
  561.  
  562. //**************************************************************************
  563. //  Override IPersistStorage to allow OleCache to be persistent.
  564. //
  565.  
  566. HRESULT _IFUNC BOleInProcServer::InitNew(IStorage* pStg)
  567. {
  568.     LPPERSISTSTORAGE pPersistCache;
  569.     if (SUCCEEDED(pDefHandler->QueryInterface(IID_IPersistStorage, &(LPVOID) pPersistCache))) {
  570.         pPersistCache->InitNew(pStg);
  571.         pPersistCache->Release();
  572.     }
  573.     return BOleSite::InitNew(pStg);
  574. }
  575.  
  576. HRESULT _IFUNC BOleInProcServer::Load(IStorage* pStg)
  577. {
  578.     LPPERSISTSTORAGE pPersistCache;
  579.     if (SUCCEEDED(pDefHandler->QueryInterface(IID_IPersistStorage, &(LPVOID) pPersistCache))) {
  580.         pPersistCache->Load(pStg);
  581.         pPersistCache->Release();
  582.     }
  583.     return BOleSite::Load(pStg);
  584. }
  585.  
  586. HRESULT _IFUNC BOleInProcServer::Save(IStorage* pStgSave,BOOL fSameAsLoad)
  587. {
  588.     LPOLECACHE2 pCache;
  589.     if (SUCCEEDED(pDefHandler->QueryInterface(IID_IOleCache2, &(LPVOID)pCache))) {
  590.         pCache->UpdateCache(this, ADVFCACHE_ONSAVE, NULL);
  591.         pCache->Release();
  592.     }
  593.     LPPERSISTSTORAGE pPersistCache;
  594.     if (SUCCEEDED(pDefHandler->QueryInterface(IID_IPersistStorage, &(LPVOID)pPersistCache))) {
  595.         pPersistCache->Save(pStgSave, fSameAsLoad);
  596.         pPersistCache->Release();
  597.     }
  598.     return BOleSite::Save(pStgSave, fSameAsLoad);
  599. }
  600.  
  601. HRESULT _IFUNC BOleInProcServer::SaveCompleted(IStorage* pStgSaved)
  602. {
  603.     LPPERSISTSTORAGE pPersistCache;
  604.     if (SUCCEEDED(pDefHandler->QueryInterface(IID_IPersistStorage, &(LPVOID) pPersistCache))) {
  605.         pPersistCache->SaveCompleted(pStgSaved);
  606.         pPersistCache->Release();
  607.     }
  608.     return BOleSite::SaveCompleted(pStgSaved);
  609. }
  610.  
  611. HRESULT _IFUNC BOleInProcServer::HandsOffStorage()
  612. {
  613.     LPPERSISTSTORAGE pPersistCache;
  614.     if (SUCCEEDED(pDefHandler->QueryInterface(IID_IPersistStorage, &(LPVOID) pPersistCache))) {
  615.         pPersistCache->HandsOffStorage();
  616.         pPersistCache->Release();
  617.     }
  618.     return BOleSite::HandsOffStorage();
  619. }
  620.  
  621.  
  622. //**************************************************************************
  623. //
  624. // IBSite methods (override this interface on aggregated BOleIPSServer
  625. // to implement Invalidate).
  626. //
  627. //**************************************************************************
  628.  
  629. HRESULT _IFUNC BOleInProcServer::Init(PIBDataProvider pDP, 
  630.     PIBPart pP, LPCOLESTR szProgId, BOOL fHatchWnd)
  631. {
  632.     // Initialize back pointers
  633.     //
  634.     pProvider = pDP;
  635.     pPart = pP;
  636.  
  637.     CLSID cid = CLSID_NULL;
  638.     
  639.     if (SUCCEEDED(CLSIDFromProgID(szProgId, &cid))) {
  640.         // create the default handler
  641.         //
  642.         CreateDataCache(AsPIUnknown(pObjOuter),cid, IID_IUnknown,
  643.             &(LPVOID)pDefHandler);
  644.     }
  645.  
  646.     // initialize default IPS
  647.     //
  648.     return BOleSite::Init(pDP, pP, szProgId, fHatchWnd);
  649. }
  650.  
  651. void _IFUNC BOleInProcServer::Invalidate( BOleInvalidate b)
  652. {
  653.     if ((b & BOLE_INVAL_VIEW) && pAdviseView) {
  654.         pAdviseView->OnViewChange(dwAdviseAspects, -1);
  655.         if (dwAdviseFlags == ADVF_ONLYONCE) {
  656.             pAdviseView->Release();
  657.             pAdviseView = NULL;
  658.             dwAdviseAspects = 0L;
  659.             dwAdviseFlags = 0L;
  660.         }
  661.     }
  662.     if (b)  // VIEW or DATA or PERSIST remains invalid
  663.         BOleSite::Invalidate(b);
  664. }
  665.  
  666. //**************************************************************************
  667. //
  668. // IOleInPlaceActiveObject
  669. //
  670. // This interface is overridden to pre-process translator and other messages
  671. // from the container's message loop.
  672. //
  673. //**************************************************************************
  674.  
  675.  
  676. //    Allow DLL Server to translate accelerators
  677. //
  678. HRESULT _IFUNC BOleInProcServer::TranslateAccelerator (LPMSG lpmsg)
  679. {
  680.     return pService->GetApplication()->Accelerator(lpmsg);
  681. }
  682.  
  683. HRESULT _IFUNC BOleInProcServer::EnumVerbs(IEnumOLEVERB* FAR* ppenumOleVerb)
  684. {
  685.     return OleRegEnumVerbs(cid, ppenumOleVerb);
  686. }
  687.  
  688. HRESULT _IFUNC BOleInProcServer::GetUserType(DWORD dwFormOfType, LPOLESTR FAR* pszUserType)
  689. {
  690.     return OleRegGetUserType(cid, dwFormOfType, pszUserType);
  691. }
  692.  
  693. HRESULT _IFUNC BOleInProcServer::GetMiscStatus(DWORD dwAspect, DWORD FAR* pdwStatus)
  694. {
  695.     return OleRegGetMiscStatus(cid, dwAspect, pdwStatus);
  696. }    
  697.  
  698. HRESULT _IFUNC BOleInProcServer::SetExtent (DWORD dwDrawAspect, LPSIZEL lpsizel)
  699. {
  700.     HRESULT hr = BOleSite::SetExtent(dwDrawAspect, lpsizel);
  701.     return hr;
  702. }
  703.  
  704. // IRunnableObject methods
  705. //
  706. HRESULT _IFUNC BOleInProcServer::GetRunningClass(LPCLSID lpClassID)
  707. {
  708.     *lpClassID = cid;
  709.     return NOERROR;
  710. }
  711.  
  712. HRESULT _IFUNC BOleInProcServer::Run(LPBINDCTX pbc)
  713. {
  714.     // Enter running mode by setting a flag and locking the container
  715.     //
  716.     if (!fRunningMode) {
  717.         fRunningMode = TRUE;
  718.         // Should register the moniker here.
  719.         // Don't register if this is a clone (pPart is NULL)
  720.         // otherwise we would link to what's on the clipboard...
  721.         //
  722.         if (pPart)
  723.             SetMoniker(OLEWHICHMK_OBJFULL, NULLP);        // register
  724.     }
  725.     
  726.     return NOERROR;
  727. }
  728.  
  729. BOOL _IFUNC BOleInProcServer::IsRunning()
  730. {
  731.     return fRunningMode;
  732. }
  733.  
  734. HRESULT _IFUNC BOleInProcServer::LockRunning(BOOL fLock, BOOL fLastCloses)
  735. {
  736.     return Lock(fLock, fLastCloses);
  737. }
  738.  
  739. HRESULT _IFUNC BOleInProcServer::SetContainedObject(BOOL fContained)
  740. {
  741.     return NOERROR;
  742. }
  743.  
  744. // IExternalConnection implementation
  745. //
  746. DWORD _IFUNC BOleInProcServer::AddConnection (DWORD flag, DWORD reserved)
  747. {
  748.      return (flag & EXTCONN_STRONG) ? ++exCon : 0;
  749. }
  750.  
  751. DWORD _IFUNC BOleInProcServer::ReleaseConnection (DWORD flag, DWORD reserved, BOOL fLastReleaseCloses)
  752. {
  753.      if (flag & EXTCONN_STRONG) {
  754.          --exCon;
  755.          if( exCon == 0 && fLastReleaseCloses){
  756.             Close(OLECLOSE_SAVEIFDIRTY);
  757.          }
  758.          return exCon;
  759.      }
  760.     return 0;
  761. }
  762.  
  763. HRESULT _IFUNC BOleInProcServer::Close (DWORD dwSaveOption)
  764. {
  765.     fRunningMode = FALSE;
  766.     return BOleSite::Close(dwSaveOption);
  767. }
  768.