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.

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