SITE.CPP
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:20k
源码类别:

Windows编程

开发平台:

Visual C++

  1. //**********************************************************************
  2. // File name: SITE.CPP
  3. //
  4. //      Implementation file for CSimpleSite
  5. //
  6. // Functions:
  7. //
  8. //      See SITE.H for class definition
  9. //
  10. // Copyright (c) 1992 - 1997 Microsoft Corporation. All rights reserved.
  11. //**********************************************************************
  12. #include "pre.h"
  13. #include "iocs.h"
  14. #include "ias.h"
  15. #include "ioipf.h"
  16. #include "ioips.h"
  17. #include "app.h"
  18. #include "site.h"
  19. #include "doc.h"
  20. #define HIMETRIC_PER_INCH 2540
  21. #define MAP_LOGHIM_TO_PIX(x,ppli) MulDiv((ppli), (x), HIMETRIC_PER_INCH)
  22. int XformWidthInHimetricToPixels(HDC hDC, int iWidthInHiMetric)
  23. {
  24.     int     iXppli;     //Pixels per logical inch along width
  25.     int     iWidthInPix;
  26.     BOOL    fSystemDC=FALSE;
  27.     if (NULL==hDC)
  28.     {
  29.         hDC=GetDC(NULL);
  30.         fSystemDC=TRUE;
  31.     }
  32.     iXppli = GetDeviceCaps (hDC, LOGPIXELSX);
  33.     //We got logical HIMETRIC along the display, convert them to pixel units
  34.     iWidthInPix = MAP_LOGHIM_TO_PIX(iWidthInHiMetric, iXppli);
  35.     if (fSystemDC)
  36.         ReleaseDC(NULL, hDC);
  37.     return iWidthInPix;
  38. }
  39. int XformHeightInHimetricToPixels(HDC hDC, int iHeightInHiMetric)
  40. {
  41.     int     iYppli;     //Pixels per logical inch along height
  42.     int     iHeightInPix;
  43.     BOOL    fSystemDC=FALSE;
  44.     if (NULL==hDC)
  45.     {
  46.         hDC=GetDC(NULL);
  47.         fSystemDC=TRUE;
  48.     }
  49.     iYppli = GetDeviceCaps (hDC, LOGPIXELSY);
  50.     //* We got logical HIMETRIC along the display, convert them to pixel units
  51.     iHeightInPix = MAP_LOGHIM_TO_PIX(iHeightInHiMetric, iYppli);
  52.     if (fSystemDC)
  53.         ReleaseDC(NULL, hDC);
  54.     return iHeightInPix;
  55. }
  56. //**********************************************************************
  57. //
  58. // OleFree
  59. //
  60. // Purpose:
  61. //
  62. //      free memory using the currently active IMalloc* allocator
  63. //
  64. // Parameters:
  65. //
  66. //      LPVOID pmem - pointer to memory allocated using IMalloc
  67. //
  68. // Return Value:
  69. //
  70. //      None
  71. //
  72. // Comments:
  73. //
  74. //********************************************************************
  75. void OleFree(LPVOID pmem)
  76. {
  77.     LPMALLOC pmalloc;
  78.     if (pmem == NULL)
  79.         return;
  80.     if (FAILED(CoGetMalloc(MEMCTX_TASK, &pmalloc)))
  81.         return;
  82.     pmalloc->Free(pmem);
  83.     pmalloc->Release();
  84. }
  85. //**********************************************************************
  86. //
  87. // CSimpleSite::Create
  88. //
  89. // Purpose:
  90. //
  91. //      Creation routine for CSimpleSite
  92. //
  93. // Parameters:
  94. //
  95. //      CSimpleDoc FAR *lpDoc   - Pointer to CSimpleDoc
  96. //
  97. // Return Value:
  98. //
  99. //      None
  100. //
  101. // Function Calls:
  102. //      Function                    Location
  103. //
  104. //      IStorage::CreateStorage     OLE API
  105. //      assert                      C Runtime
  106. //
  107. // Comments:
  108. //
  109. //********************************************************************
  110. CSimpleSite FAR * CSimpleSite::Create(CSimpleDoc FAR *lpDoc)
  111. {
  112.         CSimpleSite FAR * lpTemp = new CSimpleSite(lpDoc);
  113.         if (!lpTemp)
  114.                 return NULL;
  115.         // create a sub-storage for the object
  116.   //@@WTK WIN32, UNICODE
  117.         //HRESULT hErr = lpDoc->m_lpStorage->CreateStorage( "Object",
  118.         HRESULT hErr = lpDoc->m_lpStorage->CreateStorage( OLESTR("Object"),
  119.                                 STGM_READWRITE | STGM_TRANSACTED | STGM_SHARE_EXCLUSIVE,
  120.                                 0,
  121.                                 0,
  122.                                 &lpTemp->m_lpObjStorage);
  123.         assert(hErr == NOERROR);
  124.         if (hErr != NOERROR)
  125.                 {
  126.                 delete lpTemp;
  127.                 return NULL;
  128.                 }
  129.         // we will add one ref count on our Site. later when we want to destroy
  130.         // the Site object we will release this  ref count. when the Site's ref
  131.         // count goes to 0, it will be deleted.
  132.         lpTemp->AddRef();
  133.         return lpTemp;
  134. }
  135. //**********************************************************************
  136. //
  137. // CSimpleSite::CSimpleSite
  138. //
  139. // Purpose:
  140. //
  141. //      Constructor for CSimpleSite
  142. //
  143. // Parameters:
  144. //
  145. //      CSimpleDoc FAR *lpDoc   - Pointer to CSimpleDoc
  146. //
  147. // Return Value:
  148. //
  149. //      None
  150. //
  151. // Function Calls:
  152. //      Function                    Location
  153. //
  154. // Comments:
  155. //
  156. //********************************************************************
  157. #pragma warning(disable : 4355)  // turn off this warning.  This warning
  158.                                                                 // tells us that we are passing this in
  159.                                                                 // an initializer, before "this" is through
  160.                                                                 // initializing.  This is ok, because
  161.                                                                 // we just store the ptr in the other
  162.                                                                 // constructors
  163. CSimpleSite::CSimpleSite (CSimpleDoc FAR *lpDoc) : m_OleClientSite(this),
  164.                                                                                                  m_AdviseSink(this),
  165.                                                                                                  m_OleInPlaceSite(this)
  166. #pragma warning (default : 4355)  // Turn the warning back on
  167. {
  168.         // remember the pointer to the doc
  169.         m_lpDoc = lpDoc;
  170.         // clear the reference count
  171.         m_dwDrawAspect = DVASPECT_CONTENT;
  172.         m_lpOleObject = NULL;
  173.         m_lpInPlaceObject = NULL;
  174.         m_hwndIPObj = NULL;
  175.         m_fInPlaceActive = FALSE;
  176.         m_fObjectOpen = FALSE;
  177. }
  178. //**********************************************************************
  179. //
  180. // CSimpleSite::~CSimpleSite
  181. //
  182. // Purpose:
  183. //
  184. //      Destructor for CSimpleSite
  185. //
  186. // Parameters:
  187. //
  188. //      None
  189. //
  190. // Return Value:
  191. //
  192. //      None
  193. //
  194. // Function Calls:
  195. //      Function                                Location
  196. //
  197. //      OutputDebugString                       Windows API
  198. //      IOleObject::Release                     Object
  199. //      IStorage::Release                       OLE API
  200. //
  201. // Comments:
  202. //
  203. //********************************************************************
  204. CSimpleSite::~CSimpleSite ()
  205. {
  206.         OutputDebugString ("In CSimpleSite's Destructor rn");
  207.         if (m_lpOleObject)
  208.            m_lpOleObject->Release();
  209.         if (m_lpObjStorage)
  210.            m_lpObjStorage->Release();
  211. }
  212. //**********************************************************************
  213. //
  214. // CSimpleSite::CloseOleObject
  215. //
  216. // Purpose:
  217. //
  218. //      Call IOleObject::Close on the object of the CSimpleSite
  219. //
  220. // Parameters:
  221. //
  222. //      None
  223. //
  224. // Return Value:
  225. //
  226. //      None
  227. //
  228. // Function Calls:
  229. //      Function                                Location
  230. //
  231. //      OutputDebugString                       Windows API
  232. //      IOleObject::QueryInterface              Object
  233. //      IOleObject::Close                       Object
  234. //      IOleInPlaceObject::UIDeactivate         Object
  235. //      IOleInPlaceObject::InPlaceDeactivate    Object
  236. //      IOleInPlaceObject::Release              Object
  237. //
  238. // Comments:
  239. //
  240. //********************************************************************
  241. void CSimpleSite::CloseOleObject (void)
  242. {
  243.         CStabilize stabilize(this);
  244.         LPOLEINPLACEOBJECT lpObject;
  245.         LPVIEWOBJECT lpViewObject = NULL;
  246.         OutputDebugString ("In CSimpleSite::CloseOleObject rn");
  247.         if (m_lpOleObject)
  248.            {
  249.            if (m_fInPlaceActive)
  250.                    {
  251.                    m_lpOleObject->QueryInterface(IID_IOleInPlaceObject, (LPVOID FAR *)&lpObject);
  252.                    lpObject->UIDeactivate();
  253.                    // don't need to worry about inside-out because the object
  254.                    // is going away.
  255.                    lpObject->InPlaceDeactivate();
  256.                    lpObject->Release();
  257.                    }
  258.            m_lpOleObject->Close(OLECLOSE_NOSAVE);
  259.            }
  260. }
  261. //**********************************************************************
  262. //
  263. // CSimpleSite::UnloadOleObject
  264. //
  265. // Purpose:
  266. //
  267. //      Close and release all pointers to the object of the CSimpleSite
  268. //
  269. // Parameters:
  270. //
  271. //      None
  272. //
  273. // Return Value:
  274. //
  275. //      None
  276. //
  277. // Function Calls:
  278. //      Function                                Location
  279. //
  280. //      OutputDebugString                       Windows API
  281. //      CSimpleSite::CloseOleObject             SITE.CPP
  282. //      IOleObject::QueryInterface              Object
  283. //      IViewObject::SetAdvise                  Object
  284. //      IViewObject::Release                    Object
  285. //      IStorage::Release                       OLE API
  286. //
  287. // Comments:
  288. //
  289. //********************************************************************
  290. void CSimpleSite::UnloadOleObject (void)
  291. {
  292.         CStabilize stabilize(this);
  293.         OutputDebugString ("In CSimpleSite::UnloadOleObject rn");
  294.         if (m_lpOleObject)
  295.            {
  296.            LPVIEWOBJECT lpViewObject;
  297.            CloseOleObject();    // ensure object is closed; NOP if already closed
  298.            m_lpOleObject->QueryInterface(IID_IViewObject, (LPVOID FAR *)&lpViewObject);
  299.            if (lpViewObject)
  300.                    {
  301.                    // Remove the view advise
  302.                    lpViewObject->SetAdvise(m_dwDrawAspect, 0, NULL);
  303.                    lpViewObject->Release();
  304.                    }
  305.            m_lpOleObject->Release();
  306.            m_lpOleObject = NULL;
  307.            }
  308. }
  309. //**********************************************************************
  310. //
  311. // CSimpleSite::QueryInterface
  312. //
  313. // Purpose:
  314. //
  315. //      Used for interface negotiation of the container Site.
  316. //
  317. // Parameters:
  318. //
  319. //      REFIID riid         -   A reference to the interface that is
  320. //                              being queried.
  321. //
  322. //      LPVOID FAR* ppvObj  -   An out parameter to return a pointer to
  323. //                              the interface.
  324. //
  325. // Return Value:
  326. //
  327. //      S_OK    -   The interface is supported.
  328. //      S_FALSE -   The interface is not supported
  329. //
  330. // Function Calls:
  331. //      Function                    Location
  332. //
  333. //      OutputDebugString           Windows API
  334. //      IsEqualIID                  OLE API
  335. //                   OLE API
  336. //      CSimpleSite::AddRef          OBJ.CPP
  337. //      COleClientSite::AddRef      IOCS.CPP
  338. //      CAdviseSink::AddRef         IAS.CPP
  339. //
  340. // Comments:
  341. //
  342. //
  343. //********************************************************************
  344. STDMETHODIMP CSimpleSite::QueryInterface(REFIID riid, LPVOID FAR* ppvObj)
  345. {
  346.         CStabilize stabilize(this);
  347.         OutputDebugString("In CSimpleSite::QueryInterfacern");
  348.         *ppvObj = NULL;     // must set out pointer parameters to NULL
  349.         if ( riid == IID_IUnknown)
  350.                 {
  351.                 AddRef();
  352.                 *ppvObj = this;
  353.                 return S_OK;
  354.                 }
  355.         if ( riid == IID_IOleClientSite)
  356.                 {
  357.                 m_OleClientSite.AddRef();
  358.                 *ppvObj = &m_OleClientSite;
  359.                 return S_OK;
  360.                 }
  361.         if ( riid == IID_IAdviseSink)
  362.                 {
  363.                 m_AdviseSink.AddRef();
  364.                 *ppvObj = &m_AdviseSink;
  365.                 return S_OK;
  366.                 }
  367.         if ( riid == IID_IOleInPlaceSite)
  368.                 {
  369.                 m_OleInPlaceSite.AddRef();
  370.                 *ppvObj = &m_OleInPlaceSite;
  371.                 return S_OK;
  372.                 }
  373.         // Not a supported interface
  374.         return E_NOINTERFACE;
  375. }
  376. //**********************************************************************
  377. //
  378. // CSimpleSite::AddRef
  379. //
  380. // Purpose:
  381. //
  382. //      Increments the reference count of the container Site.
  383. //
  384. // Parameters:
  385. //
  386. //      None
  387. //
  388. // Return Value:
  389. //
  390. //      ULONG   -   The new reference count of the site.
  391. //
  392. // Function Calls:
  393. //      Function                    Location
  394. //
  395. //      OutputDebugString           Windows API
  396. //
  397. // Comments:
  398. //
  399. //********************************************************************
  400. STDMETHODIMP_(ULONG) CSimpleSite::AddRef()
  401. {
  402.         OutputDebugString("In CSimpleSite::AddRefrn");
  403.         return SafeAddRef();
  404. }
  405. //**********************************************************************
  406. //
  407. // CSimpleSite::Release
  408. //
  409. // Purpose:
  410. //
  411. //      Decrements the reference count of the container Site
  412. //
  413. // Parameters:
  414. //
  415. //      None
  416. //
  417. // Return Value:
  418. //
  419. //      ULONG   -   The new reference count of the Site.
  420. //
  421. // Function Calls:
  422. //      Function                    Location
  423. //
  424. //      OutputDebugString           Windows API
  425. //
  426. // Comments:
  427. //
  428. //********************************************************************
  429. STDMETHODIMP_(ULONG) CSimpleSite::Release()
  430. {
  431.         OutputDebugString("In CSimpleSite::Releasern");
  432.         return(SafeRelease());
  433. }
  434. //**********************************************************************
  435. //
  436. // CSimpleSite::InitObject
  437. //
  438. // Purpose:
  439. //
  440. //      Used to initialize a newly create object (can't be done in the
  441. //      constructor).
  442. //
  443. // Parameters:
  444. //
  445. //      BOOL fCreateNew -   TRUE if insert NEW object
  446. //                          FALSE if create object FROM FILE
  447. //
  448. // Return Value:
  449. //
  450. //      None
  451. //
  452. // Function Calls:
  453. //      Function                        Location
  454. //
  455. //      IOleObject::SetHostNames        Object
  456. //      IOleObject::QueryInterface      Object
  457. //      IViewObject2::GetExtent         Object
  458. //      IOleObject::DoVerb              Object
  459. //      IViewObject::SetAdvise          Object
  460. //      IViewObject::Release            Object
  461. //      GetClientRect                   Windows API
  462. //      OleSetContainedObject           OLE API
  463. //
  464. // Comments:
  465. //
  466. //********************************************************************
  467. void CSimpleSite::InitObject(BOOL fCreateNew)
  468. {
  469.         CStabilize stabilize(this);
  470.         LPVIEWOBJECT2 lpViewObject2;
  471.         RECT rect;
  472.         // Set a View Advise
  473.         m_lpOleObject->QueryInterface(IID_IViewObject2,(LPVOID FAR *)&lpViewObject2);
  474.         lpViewObject2->SetAdvise(m_dwDrawAspect, ADVF_PRIMEFIRST, &m_AdviseSink);
  475.         // get the initial size of the object
  476.         lpViewObject2->GetExtent(m_dwDrawAspect, -1 /*lindex*/, NULL /*ptd*/, &m_sizel);
  477.         GetObjRect(&rect);  // get the rectangle of the object in pixels
  478.         lpViewObject2->Release();
  479.         // give the object the name of the container app/document
  480.   //@@WTK WIN32, UNICODE
  481.         //m_lpOleObject->SetHostNames("Simple Application", "Simple OLE 2.0 In-Place Container");
  482.         m_lpOleObject->SetHostNames(OLESTR("Simple Application"),
  483.                         OLESTR("Simple OLE 2.0 In-Place Container"));
  484.         // inform object handler/DLL object that it is used in the embedding container's context
  485.         OleSetContainedObject(m_lpOleObject, TRUE);
  486.         if (fCreateNew) {
  487.            // force new object to save to guarantee valid object in our storage.
  488.            // OLE 1.0 objects may close w/o saving. this is NOT necessary if the
  489.            // object is created FROM FILE; its data in storage is already valid.
  490.            m_OleClientSite.SaveObject();
  491.            // we only want to DoVerb(SHOW) if this is an InsertNew object.
  492.            // we should NOT DoVerb(SHOW) if the object is created FromFile.
  493.            m_lpOleObject->DoVerb(
  494.                            OLEIVERB_SHOW,
  495.                            NULL,
  496.                            &m_OleClientSite,
  497.                            -1,
  498.                            m_lpDoc->m_hDocWnd,
  499.                            &rect);
  500.         }
  501. }
  502. //**********************************************************************
  503. //
  504. // CSimpleSite::PaintObj
  505. //
  506. // Purpose:
  507. //
  508. //      Paints the object
  509. //
  510. // Parameters:
  511. //
  512. //      HDC hDC     - Device context of the document window
  513. //
  514. // Return Value:
  515. //
  516. // Function Calls:
  517. //      Function                        Location
  518. //
  519. //      IOleObject::QueryInterface      Object
  520. //      IViewObject::GetColorSet        Object
  521. //      IViewObject::Release            Object
  522. //      SetMapMode                      Windows API
  523. //      LPtoDP                          Windows API
  524. //      CreateHatchBrush                Windows API
  525. //      SelectObject                    Windows API
  526. //      DeleteObject                    Windows API
  527. //      CreatePalette                   Windows API
  528. //      SelectPalette                   Windows API
  529. //      RealizePalette                  Windows API
  530. //      OleDraw                         OLE API
  531. //
  532. // Comments:
  533. //
  534. //********************************************************************
  535. void CSimpleSite::PaintObj(HDC hDC)
  536. {
  537. RECT rect;
  538.         CStabilize stabilize(this);
  539.         // need to check to make sure there is a valid object
  540.         // available.  This is needed if there is a paint msg
  541.         // between the time that CSimpleSite is instantiated
  542.         // and OleUIInsertObject returns.
  543.         if (!m_lpOleObject)
  544.                 return;
  545.         // convert it to pixels
  546.         GetObjRect(&rect);
  547.         LPLOGPALETTE pColorSet = NULL;
  548.         LPVIEWOBJECT lpView = NULL;
  549.         // get a pointer to IViewObject
  550.         m_lpOleObject->QueryInterface(IID_IViewObject,(LPVOID FAR *) &lpView);
  551.         // if the QI succeeds, get the LOGPALETTE for the object
  552.         if (lpView)
  553.                 lpView->GetColorSet(m_dwDrawAspect, -1, NULL, NULL, NULL, &pColorSet);
  554.         HPALETTE hPal=NULL;
  555.         HPALETTE hOldPal=NULL;
  556.         // if a LOGPALETTE was returned (not guarateed), create the palette and
  557.         // realize it.  NOTE: A smarter application would want to get the LOGPALETTE
  558.         // for each of its visible objects, and try to create a palette that
  559.         // satisfies all of the visible objects.  ALSO: OleFree() is used to
  560.         // free the returned LOGPALETTE.
  561.         if ((pColorSet))
  562.                 {
  563.                 //@@WTK WIN32, UNICODE
  564.                 //hPal = CreatePalette((const LPLOGPALETTE) pColorSet);
  565.                 hPal = CreatePalette( pColorSet);
  566.                 hOldPal = SelectPalette(hDC, hPal, FALSE);
  567.                 RealizePalette(hDC);
  568.                 OleFree(pColorSet);
  569.                 }
  570.         // draw the object
  571.         OleDraw(m_lpOleObject, m_dwDrawAspect, hDC, &rect);
  572.         // if the object is open, draw a hatch rect.
  573.         if (m_fObjectOpen)
  574.                 {
  575.                 HBRUSH hBrush = CreateHatchBrush ( HS_BDIAGONAL, RGB(0,0,0) );
  576.                 HBRUSH hOldBrush = (HBRUSH) SelectObject (hDC, hBrush);
  577.                 SetROP2(hDC, R2_MASKPEN);
  578.                 Rectangle (hDC, rect.left, rect.top, rect.right, rect.bottom);
  579.                 SelectObject(hDC, hOldBrush);
  580.                 DeleteObject(hBrush);
  581.                 }
  582.         // if we created a palette, restore the old one, and destroy
  583.         // the object.
  584.         if (hPal)
  585.                 {
  586.                 SelectPalette(hDC,hOldPal,FALSE);
  587.                 DeleteObject(hPal);
  588.                 }
  589.         // if a view pointer was successfully returned, it needs to be released.
  590.         if (lpView)
  591.                 lpView->Release();
  592. }
  593. //**********************************************************************
  594. //
  595. // CSimpleSite::GetObjRect
  596. //
  597. // Purpose:
  598. //
  599. //      Retrieves the rect of the object in pixels
  600. //
  601. // Parameters:
  602. //
  603. //      LPRECT lpRect - Rect structure filled with object's rect in pixels
  604. //
  605. // Return Value:
  606. //
  607. // Function Calls:
  608. //      Function                        Location
  609. //
  610. //      XformWidthInHimetricToPixels    OUTLUI Function
  611. //      XformHeightInHimetricToPixels   OUTLUI Function
  612. //
  613. // Comments:
  614. //
  615. //********************************************************************
  616. void CSimpleSite::GetObjRect(LPRECT lpRect)
  617. {
  618.         // convert it to pixels
  619.         lpRect->left = lpRect->top = 0;
  620.         lpRect->right = XformWidthInHimetricToPixels(NULL,(int)m_sizel.cx);
  621.         lpRect->bottom = XformHeightInHimetricToPixels(NULL,(int)m_sizel.cy);
  622. }