Source code of Windows XP (NT5)
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.

563 lines
18 KiB

  1. /*************************************************************************/
  2. /* Copyright (C) 1999 Microsoft Corporation */
  3. /* File: CHObj.cpp */
  4. /*************************************************************************/
  5. #include "stdafx.h"
  6. #include "chobj.h"
  7. /*************************************************************************/
  8. /* Implemntation of Class: CHostedObject */
  9. /*************************************************************************/
  10. /*************************************************************************/
  11. /* Function: CHostedObject */
  12. /* Description: Constructor that initializes member variables of the */
  13. /* object. */
  14. /*************************************************************************/
  15. CHostedObject::CHostedObject(BSTR strID, BSTR strPropBag, IUnknown* pUnknown){
  16. Init();
  17. m_strID = strID;
  18. m_strPropBag = strPropBag;
  19. m_pUnknown = pUnknown;
  20. ::ZeroMemory(&m_rcRawPos, sizeof(RECT));
  21. }/* end of function CHostedObject */
  22. /*************************************************************************/
  23. /* Function: Init */
  24. /* Description: Initializes the member variables */
  25. /*************************************************************************/
  26. void CHostedObject::Init(){
  27. m_pContainerObject = NULL;
  28. m_bWindowless = false;
  29. //m_fActive = false;
  30. m_fActive = true;
  31. m_fCapture = false;
  32. m_fFocus = false;
  33. m_fInputEnabled = true;
  34. ::ZeroMemory(&m_rcRawPos, sizeof(RECT));
  35. }/* end of function Init */
  36. /*************************************************************************/
  37. /* Function: Cleanup */
  38. /* Description: Cleans up the member variables */
  39. /*************************************************************************/
  40. void CHostedObject::Cleanup(){
  41. try {
  42. ATLTRACE2(atlTraceHosting, 0, TEXT("In the cleanup of the CHostedObject %ws\n"), GetID());
  43. #if 0 // cannot kill the container before its control is destructed
  44. if(NULL != m_pContainerObject){
  45. delete m_pContainerObject;
  46. }/* end of if statement */
  47. #endif
  48. Init();
  49. }
  50. catch(...){
  51. ATLTRACE(TEXT("Reference counting is off \n"));
  52. return;
  53. }/* end of if statement */
  54. }/* end of function Cleanup */
  55. /*************************************************************************/
  56. /* Function: CreateObject */
  57. /* Description: Creates the ActiveX Object via CoCreateInstance and */
  58. /* initializes it. If everything went OK the return the newly allocate */
  59. /* pObj. */
  60. /*************************************************************************/
  61. HRESULT CHostedObject::CreateObject(BSTR strObjectID, BSTR strProgID, BSTR strPropBag, CHostedObject** ppObj){
  62. HRESULT hr;
  63. CLSID tmpCLSID;
  64. if(NULL == ppObj){
  65. hr = E_POINTER;
  66. return(hr);
  67. }/* end of if statement */
  68. *ppObj = NULL; // set the return value to NULL
  69. hr = ::CLSIDFromProgID(strProgID, &tmpCLSID);
  70. if (FAILED(hr)){
  71. // Try to get CLSID from string if not prog ID
  72. HRESULT hrTmp = CLSIDFromString(strProgID, &tmpCLSID);
  73. if(FAILED(hrTmp)){
  74. if (hr == CO_E_CLASSSTRING) {
  75. // BUG#101663
  76. // We can not use %1!ls! for Win95.
  77. return DISP_E_EXCEPTION;
  78. }/* end of if statement */
  79. return hr;
  80. }/* end of if statement */
  81. }/* end of if statement */
  82. CComPtr<IUnknown> pUnknown;
  83. hr = pUnknown.CoCreateInstance(tmpCLSID);
  84. if (FAILED(hr)) {
  85. return(hr);
  86. }/* end of if statement */
  87. // everything went OK now allocate the object and set the variables to it
  88. *ppObj = new CHostedObject(strObjectID, strPropBag, pUnknown);
  89. if(NULL == *ppObj){ // in case we do not support exception handling
  90. hr = E_OUTOFMEMORY;
  91. }/* end of if statement */
  92. return (hr);
  93. }/* end of function CreateObject */
  94. /*************************************************************************/
  95. /* Function: AddObject */
  96. /* Description: Simmilar to create object, but does not create on. Takes */
  97. /* an existing IUnknown and wraps it, in the object structure. */
  98. /*************************************************************************/
  99. HRESULT CHostedObject::AddObject(BSTR strObjectID, BSTR strPropBag, LPUNKNOWN pUnknown,
  100. CHostedObject** ppObj){
  101. HRESULT hr = S_OK;
  102. if(NULL == ppObj){
  103. hr = E_POINTER;
  104. return(hr);
  105. }/* end of if statement */
  106. if(NULL == pUnknown){
  107. hr = E_POINTER;
  108. return(hr);
  109. }/* end of if statement */
  110. *ppObj = NULL; // set the return value to NULL
  111. // everything went OK now allocate the object and set the variables to it
  112. *ppObj = new CHostedObject(strObjectID, strPropBag, pUnknown);
  113. if(NULL == *ppObj){ // in case we do not support exception handling
  114. hr = E_OUTOFMEMORY;
  115. }/* end of if statement */
  116. return (hr);
  117. }/* end of function AddObject */
  118. /*************************************************************************/
  119. /* Function: GetID */
  120. /* Description: Returns the ID of the object */
  121. /*************************************************************************/
  122. BSTR CHostedObject::GetID(){
  123. return(m_strID);
  124. }/* end of function GetID */
  125. /*************************************************************************/
  126. /* Function: GetPropBag */
  127. /* Description: Returns the textual information that represents property */
  128. /* bag and is associate with the object. */
  129. /*************************************************************************/
  130. BSTR CHostedObject::GetPropBag(){
  131. return(m_strPropBag);
  132. }/* end of function GetPropBag */
  133. /*************************************************************************/
  134. /* Function: GetUnknown */
  135. /* Description: Gets the IUnknown stored in this object. */
  136. /*************************************************************************/
  137. IUnknown* CHostedObject::GetUnknown(){
  138. return(m_pUnknown);
  139. }/* end of function GetUnknown */
  140. /*************************************************************************/
  141. /* Function: GetViewObject */
  142. /* Description: Gets the IViewObject cached for this object. */
  143. /*************************************************************************/
  144. HRESULT CHostedObject::GetViewObject(IViewObjectEx** pIViewObject){
  145. HRESULT hr = S_OK;
  146. try {
  147. if(NULL == pIViewObject){
  148. throw(E_POINTER);
  149. }/* end of if statement */
  150. if(!m_spViewObject){
  151. hr = InitViewObject();
  152. if(FAILED(hr)){
  153. throw(hr);
  154. }/* end of if statement */
  155. }/* end of if statement */
  156. if(!m_spViewObject){
  157. *pIViewObject = NULL;
  158. hr = E_FAIL;
  159. throw(hr);
  160. }/* end of if statement */
  161. *pIViewObject = m_spViewObject;
  162. (*pIViewObject)->AddRef(); // giving it out have add reff
  163. }/* end of try statement */
  164. catch(HRESULT hrTmp){
  165. hr = hrTmp;
  166. }
  167. catch(...){
  168. hr = E_UNEXPECTED;
  169. }/* end of catch statemenmt */
  170. return(hr);
  171. }/* end of function GetViewObject */
  172. /*************************************************************************/
  173. /* Function: GetOleObject */
  174. /* Description: Gets the IOleObject cached for this object. */
  175. /*************************************************************************/
  176. HRESULT CHostedObject::GetOleObject(IOleObject** ppIOleObject){
  177. HRESULT hr = S_OK;
  178. try {
  179. if(NULL == ppIOleObject){
  180. throw(E_POINTER);
  181. }/* end of if statement */
  182. if(!m_pOleObject){
  183. if(!m_pUnknown){
  184. throw(E_UNEXPECTED);
  185. }/* end of if statement */
  186. // cache up the IOleObject
  187. hr = m_pUnknown->QueryInterface(&m_pOleObject);
  188. if(FAILED(hr)){
  189. throw(hr);
  190. }/* end of if statement */
  191. }/* end of if statement */
  192. #ifdef _DEBUG
  193. if(!m_pOleObject){ // sometimes we get OK from QI but IOleObject is NULL
  194. *ppIOleObject = NULL;
  195. throw(E_FAIL);
  196. }/* end of if statement */
  197. #endif
  198. *ppIOleObject = m_pOleObject;
  199. (*ppIOleObject)->AddRef();
  200. }/* end of try statement */
  201. catch(HRESULT hrTmp){
  202. hr = hrTmp;
  203. }
  204. catch(...){
  205. hr = E_UNEXPECTED;
  206. }/* end of catch statemenmt */
  207. return(hr);
  208. }/* end of function GetOleObject */
  209. /*************************************************************************/
  210. /* Function: GetTypeInfo */
  211. /* Description: Calls the IDispatch object TypeInfo */
  212. /*************************************************************************/
  213. HRESULT CHostedObject::GetTypeInfo(UINT itinfo, LCID lcid, ITypeInfo** pptinfo){
  214. HRESULT hr = S_OK;
  215. try {
  216. if(!m_pUnknown){
  217. throw(E_UNEXPECTED);
  218. }/* end of if statement */
  219. CComPtr<IDispatch> pDispatch;
  220. hr = m_pUnknown->QueryInterface(&pDispatch);
  221. if(FAILED(hr)){
  222. throw(hr);
  223. }/* end of if statement */
  224. hr = pDispatch->GetTypeInfo(itinfo, lcid, pptinfo);
  225. }/* end of try statement */
  226. catch(HRESULT hrTmp){
  227. hr = hrTmp;
  228. }
  229. catch(...){
  230. hr = E_UNEXPECTED;
  231. }/* end of catch statemenmt */
  232. return(hr);
  233. }/* end of funciton GetTypeInfo */
  234. /*************************************************************************/
  235. /* Function: GetPos */
  236. /* Description: Accessor to the position of the embedded object. */
  237. /*************************************************************************/
  238. HRESULT CHostedObject::GetPos(RECT* pRcPos){
  239. HRESULT hr = S_OK;
  240. if(NULL == pRcPos){
  241. hr = E_POINTER;
  242. return(hr);
  243. }/* end of if statement */
  244. // Get the raw rect for this object and then adjust it for the
  245. // offset of the container
  246. *pRcPos = m_rcRawPos;
  247. if(IsWindowless()){
  248. return(hr);
  249. }/* end of if statement */
  250. HWND hwnd = NULL;
  251. hr = GetWindow(&hwnd);
  252. if(FAILED(hr)){
  253. hr = S_FALSE;
  254. return(hr);
  255. }/* end of if statement */
  256. ::GetWindowRect(hwnd, pRcPos);
  257. m_rcRawPos = *pRcPos; // update our cached value
  258. return(hr);
  259. }/* end of function GetPos */
  260. /*************************************************************************/
  261. /* Function: SetRawPos */
  262. /* Description: We set the RawPosition, which is location relative to the*/
  263. /* container. Adjustment for the offset is done in GetPos function. */
  264. /*************************************************************************/
  265. HRESULT CHostedObject::SetRawPos(const RECT* pRcPos){
  266. HRESULT hr = S_OK;
  267. if(NULL == pRcPos){
  268. hr = E_POINTER;
  269. return(hr);
  270. }/* end of if statement */
  271. m_rcRawPos = *pRcPos;
  272. return(hr);
  273. }/* end of function SetRawPos */
  274. /*************************************************************************/
  275. /* Function: SetObjectRects */
  276. /* Description: Notififies the Controls about chaning rects, so they */
  277. /* will update their positions. */
  278. /*************************************************************************/
  279. HRESULT CHostedObject::SetObjectRects(RECT* prcPos){
  280. HRESULT hr = S_OK;
  281. RECT rc;
  282. if(NULL == prcPos){
  283. hr = GetPos(&rc);
  284. }
  285. else {
  286. rc = *prcPos;
  287. }/* end of if statement */
  288. if(FAILED(hr)){
  289. throw(hr);
  290. }/* end of if statement */
  291. CComPtr<IOleInPlaceObject> pIOlePlace;
  292. if(!m_pUnknown){
  293. hr = S_FALSE;
  294. return(hr); // do not have the object yet no way to set it
  295. }/* end of if statement */
  296. hr = m_pUnknown->QueryInterface(&pIOlePlace);
  297. if(FAILED(hr)){
  298. hr = S_FALSE;
  299. return(hr); // do not have the IOleInPlaceObject in other words not activated
  300. // object yet no way to set it, so lets not complain that
  301. // much, since the ATL would break on
  302. }/* end of if statement */
  303. // TODO: Pass down the clip rects evntaully, but right not used
  304. // in our controls
  305. hr = pIOlePlace->SetObjectRects(&rc, &rc);
  306. return(hr);
  307. }/* end of function SetObjectRects */
  308. /*************************************************************************/
  309. /* Function: InitViewObject */
  310. /* Description: Initializes the ViewObject. */
  311. /*************************************************************************/
  312. HRESULT CHostedObject::InitViewObject(){
  313. HRESULT hr = S_OK;
  314. if(!m_pUnknown){
  315. hr = E_UNEXPECTED;
  316. return(hr);
  317. }/* end of if statement */
  318. hr = m_pUnknown->QueryInterface(IID_IViewObjectEx, (void**) &m_spViewObject);
  319. if (FAILED(hr)){
  320. hr = m_pUnknown->QueryInterface(IID_IViewObject2, (void**) &m_spViewObject);
  321. if (FAILED(hr)){
  322. hr = m_pUnknown->QueryInterface(IID_IViewObject, (void**) &m_spViewObject);
  323. }/* end of if statement */
  324. }/* end of if statement */
  325. return(hr);
  326. }/* end of function InitViewObject */
  327. /*************************************************************************/
  328. /* Function: SetActive */
  329. /* Description: Sets the control flag to be active or not. */
  330. /* This disables it drawing in the container. */
  331. /*************************************************************************/
  332. HRESULT CHostedObject::SetActive(bool fActivate){
  333. HRESULT hr = S_OK;
  334. m_fActive = fActivate; // this might seem like a bug, but flag is important
  335. // even if we do not get to hide the windowed control
  336. if(IsWindowless()){
  337. return(hr); // do not have to deal with hiding and showing of the window
  338. // we just do not draw the nonactive objects in the container
  339. }/* end of if statement */
  340. HWND hwnd = NULL;
  341. hr = GetWindow(&hwnd);
  342. if(FAILED(hr)){
  343. return(hr);
  344. }/* end of if statement */
  345. if(::IsWindow(hwnd)){
  346. INT nShow = fActivate ? SW_SHOW : SW_HIDE;
  347. ::ShowWindow(hwnd, nShow);
  348. }/* end of if statement */
  349. return(hr);
  350. }/* end of function SetActive */
  351. /*************************************************************************/
  352. /* Function: GetWindow */
  353. /* Description: Gets the control window. */
  354. /*************************************************************************/
  355. HRESULT CHostedObject::GetWindow(HWND* pHwnd){
  356. HRESULT hr = S_OK;
  357. // now try to
  358. if(!m_pUnknown){
  359. hr = E_UNEXPECTED;
  360. return(hr);
  361. }/* end of if statement */
  362. CComPtr<IOleInPlaceObject> pOleInPObject;
  363. hr = m_pUnknown->QueryInterface(&pOleInPObject);
  364. if(FAILED(hr)){
  365. return(hr);
  366. }/* end of if statement */
  367. hr = pOleInPObject->GetWindow(pHwnd);
  368. if(FAILED(hr)){
  369. return(hr);
  370. }/* end of if statement */
  371. return(hr);
  372. }/* end of if statement */
  373. /*************************************************************************/
  374. /* Function: IsInputEnabled */
  375. /* Description: Function that lets us know if the input is enabled or not*/
  376. /*************************************************************************/
  377. bool CHostedObject::IsInputEnabled(){
  378. if(IsActive()){
  379. return m_fInputEnabled;
  380. }/* end of if statement */
  381. return false;
  382. }/* end of function IsInputEnabled */
  383. /*************************************************************************/
  384. /* Function: SetInputEnabled */
  385. /* Description: Sets the control flag to to disable or enable mouse and */
  386. /* keyboard input to a windowless controll. */
  387. /*************************************************************************/
  388. HRESULT CHostedObject::SetInputEnabled(bool fInputEnabled){
  389. HRESULT hr = E_FAIL;
  390. m_fInputEnabled = fInputEnabled; // this might seem like a bug, but flag is important
  391. // even if we do not get to hide the windowed control
  392. if(IsWindowless()){
  393. hr = S_OK;
  394. // we just do not draw the nonactive objects in the container
  395. }/* end of if statement */
  396. return(hr); // do not handle the windowed case
  397. }/* end of function SetInputEnabled */
  398. /*************************************************************************/
  399. /* End of file: CHObj.cpp */
  400. /*************************************************************************/