Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

668 lines
16 KiB

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