UIActiveX.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. #include "StdAfx.h"
  2. namespace DuiLib {
  3. /////////////////////////////////////////////////////////////////////////////////////
  4. //
  5. //
  6. class CActiveXCtrl;
  7. /////////////////////////////////////////////////////////////////////////////////////
  8. //
  9. //
  10. class CActiveXWnd : public CWindowWnd
  11. {
  12. public:
  13. CActiveXWnd() : m_iLayeredTick(0), m_bDrawCaret(false) {}
  14. HWND Init(CActiveXCtrl* pOwner, HWND hWndParent);
  15. LPCTSTR GetWindowClassName() const;
  16. void OnFinalMessage(HWND hWnd);
  17. LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
  18. protected:
  19. void DoVerb(LONG iVerb);
  20. LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  21. LRESULT OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  22. LRESULT OnMouseActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  23. LRESULT OnSetFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  24. LRESULT OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  25. LRESULT OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  26. LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  27. LRESULT OnPrint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  28. protected:
  29. enum {
  30. DEFAULT_TIMERID = 20,
  31. };
  32. CActiveXCtrl* m_pOwner;
  33. int m_iLayeredTick;
  34. bool m_bDrawCaret;
  35. };
  36. /////////////////////////////////////////////////////////////////////////////////////
  37. //
  38. //
  39. class CActiveXEnum : public IEnumUnknown
  40. {
  41. public:
  42. CActiveXEnum(IUnknown* pUnk) : m_pUnk(pUnk), m_dwRef(1), m_iPos(0)
  43. {
  44. m_pUnk->AddRef();
  45. }
  46. ~CActiveXEnum()
  47. {
  48. m_pUnk->Release();
  49. }
  50. LONG m_iPos;
  51. ULONG m_dwRef;
  52. IUnknown* m_pUnk;
  53. STDMETHOD_(ULONG,AddRef)()
  54. {
  55. return ++m_dwRef;
  56. }
  57. STDMETHOD_(ULONG,Release)()
  58. {
  59. LONG lRef = --m_dwRef;
  60. if( lRef == 0 ) delete this;
  61. return lRef;
  62. }
  63. STDMETHOD(QueryInterface)(REFIID riid, LPVOID *ppvObject)
  64. {
  65. *ppvObject = NULL;
  66. if( riid == IID_IUnknown ) *ppvObject = static_cast<IEnumUnknown*>(this);
  67. else if( riid == IID_IEnumUnknown ) *ppvObject = static_cast<IEnumUnknown*>(this);
  68. if( *ppvObject != NULL ) AddRef();
  69. return *ppvObject == NULL ? E_NOINTERFACE : S_OK;
  70. }
  71. STDMETHOD(Next)(ULONG celt, IUnknown **rgelt, ULONG *pceltFetched)
  72. {
  73. if( pceltFetched != NULL ) *pceltFetched = 0;
  74. if( ++m_iPos > 1 ) return S_FALSE;
  75. *rgelt = m_pUnk;
  76. (*rgelt)->AddRef();
  77. if( pceltFetched != NULL ) *pceltFetched = 1;
  78. return S_OK;
  79. }
  80. STDMETHOD(Skip)(ULONG celt)
  81. {
  82. m_iPos += celt;
  83. return S_OK;
  84. }
  85. STDMETHOD(Reset)(void)
  86. {
  87. m_iPos = 0;
  88. return S_OK;
  89. }
  90. STDMETHOD(Clone)(IEnumUnknown **ppenum)
  91. {
  92. return E_NOTIMPL;
  93. }
  94. };
  95. /////////////////////////////////////////////////////////////////////////////////////
  96. //
  97. //
  98. class CActiveXFrameWnd : public IOleInPlaceFrame
  99. {
  100. public:
  101. CActiveXFrameWnd(CActiveXUI* pOwner) : m_dwRef(1), m_pOwner(pOwner), m_pActiveObject(NULL)
  102. {
  103. }
  104. ~CActiveXFrameWnd()
  105. {
  106. if( m_pActiveObject != NULL ) m_pActiveObject->Release();
  107. }
  108. ULONG m_dwRef;
  109. CActiveXUI* m_pOwner;
  110. IOleInPlaceActiveObject* m_pActiveObject;
  111. // IUnknown
  112. STDMETHOD_(ULONG,AddRef)()
  113. {
  114. return ++m_dwRef;
  115. }
  116. STDMETHOD_(ULONG,Release)()
  117. {
  118. ULONG lRef = --m_dwRef;
  119. if( lRef == 0 ) delete this;
  120. return lRef;
  121. }
  122. STDMETHOD(QueryInterface)(REFIID riid, LPVOID *ppvObject)
  123. {
  124. *ppvObject = NULL;
  125. if( riid == IID_IUnknown ) *ppvObject = static_cast<IOleInPlaceFrame*>(this);
  126. else if( riid == IID_IOleWindow ) *ppvObject = static_cast<IOleWindow*>(this);
  127. else if( riid == IID_IOleInPlaceFrame ) *ppvObject = static_cast<IOleInPlaceFrame*>(this);
  128. else if( riid == IID_IOleInPlaceUIWindow ) *ppvObject = static_cast<IOleInPlaceUIWindow*>(this);
  129. if( *ppvObject != NULL ) AddRef();
  130. return *ppvObject == NULL ? E_NOINTERFACE : S_OK;
  131. }
  132. // IOleInPlaceFrameWindow
  133. STDMETHOD(InsertMenus)(HMENU /*hmenuShared*/, LPOLEMENUGROUPWIDTHS /*lpMenuWidths*/)
  134. {
  135. return S_OK;
  136. }
  137. STDMETHOD(SetMenu)(HMENU /*hmenuShared*/, HOLEMENU /*holemenu*/, HWND /*hwndActiveObject*/)
  138. {
  139. return S_OK;
  140. }
  141. STDMETHOD(RemoveMenus)(HMENU /*hmenuShared*/)
  142. {
  143. return S_OK;
  144. }
  145. STDMETHOD(SetStatusText)(LPCOLESTR /*pszStatusText*/)
  146. {
  147. return S_OK;
  148. }
  149. STDMETHOD(EnableModeless)(BOOL /*fEnable*/)
  150. {
  151. return S_OK;
  152. }
  153. STDMETHOD(TranslateAccelerator)(LPMSG /*lpMsg*/, WORD /*wID*/)
  154. {
  155. return S_FALSE;
  156. }
  157. // IOleWindow
  158. STDMETHOD(GetWindow)(HWND* phwnd)
  159. {
  160. if( m_pOwner == NULL ) return E_UNEXPECTED;
  161. *phwnd = m_pOwner->GetManager()->GetPaintWindow();
  162. return S_OK;
  163. }
  164. STDMETHOD(ContextSensitiveHelp)(BOOL /*fEnterMode*/)
  165. {
  166. return S_OK;
  167. }
  168. // IOleInPlaceUIWindow
  169. STDMETHOD(GetBorder)(LPRECT /*lprectBorder*/)
  170. {
  171. return S_OK;
  172. }
  173. STDMETHOD(RequestBorderSpace)(LPCBORDERWIDTHS /*pborderwidths*/)
  174. {
  175. return INPLACE_E_NOTOOLSPACE;
  176. }
  177. STDMETHOD(SetBorderSpace)(LPCBORDERWIDTHS /*pborderwidths*/)
  178. {
  179. return S_OK;
  180. }
  181. STDMETHOD(SetActiveObject)(IOleInPlaceActiveObject* pActiveObject, LPCOLESTR /*pszObjName*/)
  182. {
  183. if( pActiveObject != NULL ) pActiveObject->AddRef();
  184. if( m_pActiveObject != NULL ) m_pActiveObject->Release();
  185. m_pActiveObject = pActiveObject;
  186. return S_OK;
  187. }
  188. };
  189. /////////////////////////////////////////////////////////////////////////////////////
  190. //
  191. class CActiveXCtrl :
  192. public IOleClientSite,
  193. public IOleInPlaceSiteWindowless,
  194. public IOleControlSite,
  195. public IObjectWithSite,
  196. public IOleContainer
  197. {
  198. friend class CActiveXUI;
  199. friend class CActiveXWnd;
  200. public:
  201. CActiveXCtrl();
  202. ~CActiveXCtrl();
  203. // IUnknown
  204. STDMETHOD_(ULONG,AddRef)();
  205. STDMETHOD_(ULONG,Release)();
  206. STDMETHOD(QueryInterface)(REFIID riid, LPVOID *ppvObject);
  207. // IObjectWithSite
  208. STDMETHOD(SetSite)(IUnknown *pUnkSite);
  209. STDMETHOD(GetSite)(REFIID riid, LPVOID* ppvSite);
  210. // IOleClientSite
  211. STDMETHOD(SaveObject)(void);
  212. STDMETHOD(GetMoniker)(DWORD dwAssign, DWORD dwWhichMoniker, IMoniker** ppmk);
  213. STDMETHOD(GetContainer)(IOleContainer** ppContainer);
  214. STDMETHOD(ShowObject)(void);
  215. STDMETHOD(OnShowWindow)(BOOL fShow);
  216. STDMETHOD(RequestNewObjectLayout)(void);
  217. // IOleInPlaceSiteWindowless
  218. STDMETHOD(CanWindowlessActivate)(void);
  219. STDMETHOD(GetCapture)(void);
  220. STDMETHOD(SetCapture)(BOOL fCapture);
  221. STDMETHOD(GetFocus)(void);
  222. STDMETHOD(SetFocus)(BOOL fFocus);
  223. STDMETHOD(GetDC)(LPCRECT pRect, DWORD grfFlags, HDC* phDC);
  224. STDMETHOD(ReleaseDC)(HDC hDC);
  225. STDMETHOD(InvalidateRect)(LPCRECT pRect, BOOL fErase);
  226. STDMETHOD(InvalidateRgn)(HRGN hRGN, BOOL fErase);
  227. STDMETHOD(ScrollRect)(INT dx, INT dy, LPCRECT pRectScroll, LPCRECT pRectClip);
  228. STDMETHOD(AdjustRect)(LPRECT prc);
  229. STDMETHOD(OnDefWindowMessage)(UINT msg, WPARAM wParam, LPARAM lParam, LRESULT* plResult);
  230. // IOleInPlaceSiteEx
  231. STDMETHOD(OnInPlaceActivateEx)(BOOL *pfNoRedraw, DWORD dwFlags);
  232. STDMETHOD(OnInPlaceDeactivateEx)(BOOL fNoRedraw);
  233. STDMETHOD(RequestUIActivate)(void);
  234. // IOleInPlaceSite
  235. STDMETHOD(CanInPlaceActivate)(void);
  236. STDMETHOD(OnInPlaceActivate)(void);
  237. STDMETHOD(OnUIActivate)(void);
  238. STDMETHOD(GetWindowContext)(IOleInPlaceFrame** ppFrame, IOleInPlaceUIWindow** ppDoc, LPRECT lprcPosRect, LPRECT lprcClipRect, LPOLEINPLACEFRAMEINFO lpFrameInfo);
  239. STDMETHOD(Scroll)(SIZE scrollExtant);
  240. STDMETHOD(OnUIDeactivate)(BOOL fUndoable);
  241. STDMETHOD(OnInPlaceDeactivate)(void);
  242. STDMETHOD(DiscardUndoState)( void);
  243. STDMETHOD(DeactivateAndUndo)( void);
  244. STDMETHOD(OnPosRectChange)(LPCRECT lprcPosRect);
  245. // IOleWindow
  246. STDMETHOD(GetWindow)(HWND* phwnd);
  247. STDMETHOD(ContextSensitiveHelp)(BOOL fEnterMode);
  248. // IOleControlSite
  249. STDMETHOD(OnControlInfoChanged)(void);
  250. STDMETHOD(LockInPlaceActive)(BOOL fLock);
  251. STDMETHOD(GetExtendedControl)(IDispatch** ppDisp);
  252. STDMETHOD(TransformCoords)(POINTL* pPtlHimetric, POINTF* pPtfContainer, DWORD dwFlags);
  253. STDMETHOD(TranslateAccelerator)(MSG* pMsg, DWORD grfModifiers);
  254. STDMETHOD(OnFocus)(BOOL fGotFocus);
  255. STDMETHOD(ShowPropertyFrame)(void);
  256. // IOleContainer
  257. STDMETHOD(EnumObjects)(DWORD grfFlags, IEnumUnknown** ppenum);
  258. STDMETHOD(LockContainer)(BOOL fLock);
  259. // IParseDisplayName
  260. STDMETHOD(ParseDisplayName)(IBindCtx* pbc, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
  261. protected:
  262. HRESULT CreateActiveXWnd();
  263. protected:
  264. LONG m_dwRef;
  265. CActiveXUI* m_pOwner;
  266. CActiveXWnd* m_pWindow;
  267. IUnknown* m_pUnkSite;
  268. IViewObject* m_pViewObject;
  269. IOleInPlaceObjectWindowless* m_pInPlaceObject;
  270. bool m_bLocked;
  271. bool m_bFocused;
  272. bool m_bCaptured;
  273. bool m_bUIActivated;
  274. bool m_bInPlaceActive;
  275. bool m_bWindowless;
  276. };
  277. CActiveXCtrl::CActiveXCtrl() :
  278. m_dwRef(1),
  279. m_pOwner(NULL),
  280. m_pWindow(NULL),
  281. m_pUnkSite(NULL),
  282. m_pViewObject(NULL),
  283. m_pInPlaceObject(NULL),
  284. m_bLocked(false),
  285. m_bFocused(false),
  286. m_bCaptured(false),
  287. m_bWindowless(true),
  288. m_bUIActivated(false),
  289. m_bInPlaceActive(false)
  290. {
  291. }
  292. CActiveXCtrl::~CActiveXCtrl()
  293. {
  294. if( m_pWindow != NULL ) {
  295. ::DestroyWindow(*m_pWindow);
  296. delete m_pWindow;
  297. }
  298. if( m_pUnkSite != NULL ) m_pUnkSite->Release();
  299. if( m_pViewObject != NULL ) m_pViewObject->Release();
  300. if( m_pInPlaceObject != NULL ) m_pInPlaceObject->Release();
  301. }
  302. STDMETHODIMP CActiveXCtrl::QueryInterface(REFIID riid, LPVOID *ppvObject)
  303. {
  304. *ppvObject = NULL;
  305. if( riid == IID_IUnknown ) *ppvObject = static_cast<IOleWindow*>(this);
  306. else if( riid == IID_IOleClientSite ) *ppvObject = static_cast<IOleClientSite*>(this);
  307. else if( riid == IID_IOleInPlaceSiteWindowless ) *ppvObject = static_cast<IOleInPlaceSiteWindowless*>(this);
  308. else if( riid == IID_IOleInPlaceSiteEx ) *ppvObject = static_cast<IOleInPlaceSiteEx*>(this);
  309. else if( riid == IID_IOleInPlaceSite ) *ppvObject = static_cast<IOleInPlaceSite*>(this);
  310. else if( riid == IID_IOleWindow ) *ppvObject = static_cast<IOleWindow*>(this);
  311. else if( riid == IID_IOleControlSite ) *ppvObject = static_cast<IOleControlSite*>(this);
  312. else if( riid == IID_IOleContainer ) *ppvObject = static_cast<IOleContainer*>(this);
  313. else if( riid == IID_IObjectWithSite ) *ppvObject = static_cast<IObjectWithSite*>(this);
  314. if( *ppvObject != NULL ) AddRef();
  315. return *ppvObject == NULL ? E_NOINTERFACE : S_OK;
  316. }
  317. STDMETHODIMP_(ULONG) CActiveXCtrl::AddRef()
  318. {
  319. return ++m_dwRef;
  320. }
  321. STDMETHODIMP_(ULONG) CActiveXCtrl::Release()
  322. {
  323. LONG lRef = --m_dwRef;
  324. if( lRef == 0 ) delete this;
  325. return lRef;
  326. }
  327. STDMETHODIMP CActiveXCtrl::SetSite(IUnknown *pUnkSite)
  328. {
  329. DUITRACE(_T("AX: CActiveXCtrl::SetSite"));
  330. if( m_pUnkSite != NULL ) {
  331. m_pUnkSite->Release();
  332. m_pUnkSite = NULL;
  333. }
  334. if( pUnkSite != NULL ) {
  335. m_pUnkSite = pUnkSite;
  336. m_pUnkSite->AddRef();
  337. }
  338. return S_OK;
  339. }
  340. STDMETHODIMP CActiveXCtrl::GetSite(REFIID riid, LPVOID* ppvSite)
  341. {
  342. DUITRACE(_T("AX: CActiveXCtrl::GetSite"));
  343. if( ppvSite == NULL ) return E_POINTER;
  344. *ppvSite = NULL;
  345. if( m_pUnkSite == NULL ) return E_FAIL;
  346. return m_pUnkSite->QueryInterface(riid, ppvSite);
  347. }
  348. STDMETHODIMP CActiveXCtrl::SaveObject(void)
  349. {
  350. DUITRACE(_T("AX: CActiveXCtrl::SaveObject"));
  351. return E_NOTIMPL;
  352. }
  353. STDMETHODIMP CActiveXCtrl::GetMoniker(DWORD dwAssign, DWORD dwWhichMoniker, IMoniker** ppmk)
  354. {
  355. DUITRACE(_T("AX: CActiveXCtrl::GetMoniker"));
  356. if( ppmk != NULL ) *ppmk = NULL;
  357. return E_NOTIMPL;
  358. }
  359. STDMETHODIMP CActiveXCtrl::GetContainer(IOleContainer** ppContainer)
  360. {
  361. DUITRACE(_T("AX: CActiveXCtrl::GetContainer"));
  362. if( ppContainer == NULL ) return E_POINTER;
  363. *ppContainer = NULL;
  364. HRESULT Hr = E_NOTIMPL;
  365. if( m_pUnkSite != NULL ) Hr = m_pUnkSite->QueryInterface(IID_IOleContainer, (LPVOID*) ppContainer);
  366. if( FAILED(Hr) ) Hr = QueryInterface(IID_IOleContainer, (LPVOID*) ppContainer);
  367. return Hr;
  368. }
  369. STDMETHODIMP CActiveXCtrl::ShowObject(void)
  370. {
  371. DUITRACE(_T("AX: CActiveXCtrl::ShowObject"));
  372. if( m_pOwner == NULL ) return E_UNEXPECTED;
  373. HDC hDC = ::GetDC(m_pOwner->m_hwndHost);
  374. if( hDC == NULL ) return E_FAIL;
  375. if( m_pViewObject != NULL ) m_pViewObject->Draw(DVASPECT_CONTENT, -1, NULL, NULL, NULL, hDC, (RECTL*) &m_pOwner->m_rcItem, (RECTL*) &m_pOwner->m_rcItem, NULL, NULL);
  376. ::ReleaseDC(m_pOwner->m_hwndHost, hDC);
  377. return S_OK;
  378. }
  379. STDMETHODIMP CActiveXCtrl::OnShowWindow(BOOL fShow)
  380. {
  381. DUITRACE(_T("AX: CActiveXCtrl::OnShowWindow"));
  382. return E_NOTIMPL;
  383. }
  384. STDMETHODIMP CActiveXCtrl::RequestNewObjectLayout(void)
  385. {
  386. DUITRACE(_T("AX: CActiveXCtrl::RequestNewObjectLayout"));
  387. return E_NOTIMPL;
  388. }
  389. STDMETHODIMP CActiveXCtrl::CanWindowlessActivate(void)
  390. {
  391. DUITRACE(_T("AX: CActiveXCtrl::CanWindowlessActivate"));
  392. return S_OK; // Yes, we can!!
  393. }
  394. STDMETHODIMP CActiveXCtrl::GetCapture(void)
  395. {
  396. DUITRACE(_T("AX: CActiveXCtrl::GetCapture"));
  397. if( m_pOwner == NULL ) return E_UNEXPECTED;
  398. return m_bCaptured ? S_OK : S_FALSE;
  399. }
  400. STDMETHODIMP CActiveXCtrl::SetCapture(BOOL fCapture)
  401. {
  402. DUITRACE(_T("AX: CActiveXCtrl::SetCapture"));
  403. if( m_pOwner == NULL ) return E_UNEXPECTED;
  404. m_bCaptured = (fCapture == TRUE);
  405. if( fCapture ) ::SetCapture(m_pOwner->m_hwndHost); else ::ReleaseCapture();
  406. return S_OK;
  407. }
  408. STDMETHODIMP CActiveXCtrl::GetFocus(void)
  409. {
  410. DUITRACE(_T("AX: CActiveXCtrl::GetFocus"));
  411. if( m_pOwner == NULL ) return E_UNEXPECTED;
  412. return m_bFocused ? S_OK : S_FALSE;
  413. }
  414. STDMETHODIMP CActiveXCtrl::SetFocus(BOOL fFocus)
  415. {
  416. DUITRACE(_T("AX: CActiveXCtrl::SetFocus"));
  417. if( m_pOwner == NULL ) return E_UNEXPECTED;
  418. if( fFocus ) m_pOwner->SetFocus();
  419. m_bFocused = (fFocus == TRUE);
  420. return S_OK;
  421. }
  422. STDMETHODIMP CActiveXCtrl::GetDC(LPCRECT pRect, DWORD grfFlags, HDC* phDC)
  423. {
  424. DUITRACE(_T("AX: CActiveXCtrl::GetDC"));
  425. if( phDC == NULL ) return E_POINTER;
  426. if( m_pOwner == NULL ) return E_UNEXPECTED;
  427. if( m_bWindowless ) return S_FALSE;
  428. *phDC = ::GetDC(m_pOwner->m_hwndHost);
  429. if( (grfFlags & OLEDC_PAINTBKGND) != 0 ) {
  430. CDuiRect rcItem = m_pOwner->GetPos();
  431. if( !m_bWindowless ) rcItem.ResetOffset();
  432. ::FillRect(*phDC, &rcItem, (HBRUSH) (COLOR_WINDOW + 1));
  433. }
  434. return S_OK;
  435. }
  436. STDMETHODIMP CActiveXCtrl::ReleaseDC(HDC hDC)
  437. {
  438. DUITRACE(_T("AX: CActiveXCtrl::ReleaseDC"));
  439. if( m_pOwner == NULL ) return E_UNEXPECTED;
  440. ::ReleaseDC(m_pOwner->m_hwndHost, hDC);
  441. return S_OK;
  442. }
  443. STDMETHODIMP CActiveXCtrl::InvalidateRect(LPCRECT pRect, BOOL fErase)
  444. {
  445. DUITRACE(_T("AX: CActiveXCtrl::InvalidateRect"));
  446. if( m_pOwner == NULL ) return E_UNEXPECTED;
  447. if( m_pOwner->m_hwndHost == NULL ) return E_FAIL;
  448. return ::InvalidateRect(m_pOwner->m_hwndHost, pRect, fErase) ? S_OK : E_FAIL;
  449. }
  450. STDMETHODIMP CActiveXCtrl::InvalidateRgn(HRGN hRGN, BOOL fErase)
  451. {
  452. DUITRACE(_T("AX: CActiveXCtrl::InvalidateRgn"));
  453. if( m_pOwner == NULL ) return E_UNEXPECTED;
  454. return ::InvalidateRgn(m_pOwner->m_hwndHost, hRGN, fErase) ? S_OK : E_FAIL;
  455. }
  456. STDMETHODIMP CActiveXCtrl::ScrollRect(INT dx, INT dy, LPCRECT pRectScroll, LPCRECT pRectClip)
  457. {
  458. DUITRACE(_T("AX: CActiveXCtrl::ScrollRect"));
  459. return S_OK;
  460. }
  461. STDMETHODIMP CActiveXCtrl::AdjustRect(LPRECT prc)
  462. {
  463. DUITRACE(_T("AX: CActiveXCtrl::AdjustRect"));
  464. return S_OK;
  465. }
  466. STDMETHODIMP CActiveXCtrl::OnDefWindowMessage(UINT msg, WPARAM wParam, LPARAM lParam, LRESULT* plResult)
  467. {
  468. DUITRACE(_T("AX: CActiveXCtrl::OnDefWindowMessage"));
  469. if( m_pOwner == NULL ) return E_UNEXPECTED;
  470. *plResult = ::DefWindowProc(m_pOwner->m_hwndHost, msg, wParam, lParam);
  471. return S_OK;
  472. }
  473. STDMETHODIMP CActiveXCtrl::OnInPlaceActivateEx(BOOL* pfNoRedraw, DWORD dwFlags)
  474. {
  475. DUITRACE(_T("AX: CActiveXCtrl::OnInPlaceActivateEx"));
  476. ASSERT(m_pInPlaceObject==NULL);
  477. if( m_pOwner == NULL ) return E_UNEXPECTED;
  478. if( m_pOwner->m_pUnk == NULL ) return E_UNEXPECTED;
  479. ::OleLockRunning(m_pOwner->m_pUnk, TRUE, FALSE);
  480. HWND hWndFrame = m_pOwner->GetManager()->GetPaintWindow();
  481. HRESULT Hr = E_FAIL;
  482. if( (dwFlags & ACTIVATE_WINDOWLESS) != 0 ) {
  483. m_bWindowless = true;
  484. Hr = m_pOwner->m_pUnk->QueryInterface(IID_IOleInPlaceObjectWindowless, (LPVOID*) &m_pInPlaceObject);
  485. m_pOwner->m_hwndHost = hWndFrame;
  486. m_pOwner->GetManager()->AddMessageFilter(m_pOwner);
  487. }
  488. if( FAILED(Hr) ) {
  489. m_bWindowless = false;
  490. Hr = CreateActiveXWnd();
  491. if( FAILED(Hr) ) return Hr;
  492. Hr = m_pOwner->m_pUnk->QueryInterface(IID_IOleInPlaceObject, (LPVOID*) &m_pInPlaceObject);
  493. }
  494. if( m_pInPlaceObject != NULL && !m_pOwner->IsMFC() ) {
  495. CDuiRect rcItem = m_pOwner->m_rcItem;
  496. if( !m_bWindowless ) rcItem.ResetOffset();
  497. m_pInPlaceObject->SetObjectRects(&rcItem, &rcItem);
  498. }
  499. m_bInPlaceActive = SUCCEEDED(Hr);
  500. return Hr;
  501. }
  502. STDMETHODIMP CActiveXCtrl::OnInPlaceDeactivateEx(BOOL fNoRedraw)
  503. {
  504. DUITRACE(_T("AX: CActiveXCtrl::OnInPlaceDeactivateEx"));
  505. m_bInPlaceActive = false;
  506. if( m_pInPlaceObject != NULL ) {
  507. m_pInPlaceObject->Release();
  508. m_pInPlaceObject = NULL;
  509. }
  510. if( m_pWindow != NULL ) {
  511. ::DestroyWindow(*m_pWindow);
  512. delete m_pWindow;
  513. m_pWindow = NULL;
  514. }
  515. return S_OK;
  516. }
  517. STDMETHODIMP CActiveXCtrl::RequestUIActivate(void)
  518. {
  519. DUITRACE(_T("AX: CActiveXCtrl::RequestUIActivate"));
  520. return S_OK;
  521. }
  522. STDMETHODIMP CActiveXCtrl::CanInPlaceActivate(void)
  523. {
  524. DUITRACE(_T("AX: CActiveXCtrl::CanInPlaceActivate"));
  525. return S_OK;
  526. }
  527. STDMETHODIMP CActiveXCtrl::OnInPlaceActivate(void)
  528. {
  529. DUITRACE(_T("AX: CActiveXCtrl::OnInPlaceActivate"));
  530. BOOL bDummy = FALSE;
  531. return OnInPlaceActivateEx(&bDummy, 0);
  532. }
  533. STDMETHODIMP CActiveXCtrl::OnUIActivate(void)
  534. {
  535. DUITRACE(_T("AX: CActiveXCtrl::OnUIActivate"));
  536. m_bUIActivated = true;
  537. return S_OK;
  538. }
  539. STDMETHODIMP CActiveXCtrl::GetWindowContext(IOleInPlaceFrame** ppFrame, IOleInPlaceUIWindow** ppDoc, LPRECT lprcPosRect, LPRECT lprcClipRect, LPOLEINPLACEFRAMEINFO lpFrameInfo)
  540. {
  541. DUITRACE(_T("AX: CActiveXCtrl::GetWindowContext"));
  542. if( ppDoc == NULL ) return E_POINTER;
  543. if( ppFrame == NULL ) return E_POINTER;
  544. if( lprcPosRect == NULL ) return E_POINTER;
  545. if( lprcClipRect == NULL ) return E_POINTER;
  546. if (m_pWindow)
  547. {
  548. ::GetClientRect(m_pWindow->GetHWND(),lprcPosRect);
  549. ::GetClientRect(m_pWindow->GetHWND(),lprcClipRect);
  550. }
  551. *ppFrame = new CActiveXFrameWnd(m_pOwner);
  552. *ppDoc = NULL;
  553. ACCEL ac = { 0 };
  554. HACCEL hac = ::CreateAcceleratorTable(&ac, 1);
  555. lpFrameInfo->cb = sizeof(OLEINPLACEFRAMEINFO);
  556. lpFrameInfo->fMDIApp = FALSE;
  557. lpFrameInfo->hwndFrame = m_pOwner->GetManager()->GetPaintWindow();
  558. lpFrameInfo->haccel = hac;
  559. lpFrameInfo->cAccelEntries = 1;
  560. return S_OK;
  561. }
  562. STDMETHODIMP CActiveXCtrl::Scroll(SIZE scrollExtant)
  563. {
  564. DUITRACE(_T("AX: CActiveXCtrl::Scroll"));
  565. return E_NOTIMPL;
  566. }
  567. STDMETHODIMP CActiveXCtrl::OnUIDeactivate(BOOL fUndoable)
  568. {
  569. DUITRACE(_T("AX: CActiveXCtrl::OnUIDeactivate"));
  570. m_bUIActivated = false;
  571. return S_OK;
  572. }
  573. STDMETHODIMP CActiveXCtrl::OnInPlaceDeactivate(void)
  574. {
  575. DUITRACE(_T("AX: CActiveXCtrl::OnInPlaceDeactivate"));
  576. return OnInPlaceDeactivateEx(TRUE);
  577. }
  578. STDMETHODIMP CActiveXCtrl::DiscardUndoState(void)
  579. {
  580. DUITRACE(_T("AX: CActiveXCtrl::DiscardUndoState"));
  581. return E_NOTIMPL;
  582. }
  583. STDMETHODIMP CActiveXCtrl::DeactivateAndUndo(void)
  584. {
  585. DUITRACE(_T("AX: CActiveXCtrl::DeactivateAndUndo"));
  586. return E_NOTIMPL;
  587. }
  588. STDMETHODIMP CActiveXCtrl::OnPosRectChange(LPCRECT lprcPosRect)
  589. {
  590. DUITRACE(_T("AX: CActiveXCtrl::OnPosRectChange"));
  591. return E_NOTIMPL;
  592. }
  593. STDMETHODIMP CActiveXCtrl::GetWindow(HWND* phwnd)
  594. {
  595. DUITRACE(_T("AX: CActiveXCtrl::GetWindow"));
  596. if( m_pOwner == NULL ) return E_UNEXPECTED;
  597. if( m_pOwner->m_hwndHost == NULL ) CreateActiveXWnd();
  598. if( m_pOwner->m_hwndHost == NULL ) return E_FAIL;
  599. *phwnd = m_pOwner->m_hwndHost;
  600. return S_OK;
  601. }
  602. STDMETHODIMP CActiveXCtrl::ContextSensitiveHelp(BOOL fEnterMode)
  603. {
  604. DUITRACE(_T("AX: CActiveXCtrl::ContextSensitiveHelp"));
  605. return S_OK;
  606. }
  607. STDMETHODIMP CActiveXCtrl::OnControlInfoChanged(void)
  608. {
  609. DUITRACE(_T("AX: CActiveXCtrl::OnControlInfoChanged"));
  610. return S_OK;
  611. }
  612. STDMETHODIMP CActiveXCtrl::LockInPlaceActive(BOOL fLock)
  613. {
  614. DUITRACE(_T("AX: CActiveXCtrl::LockInPlaceActive"));
  615. return S_OK;
  616. }
  617. STDMETHODIMP CActiveXCtrl::GetExtendedControl(IDispatch** ppDisp)
  618. {
  619. DUITRACE(_T("AX: CActiveXCtrl::GetExtendedControl"));
  620. if( ppDisp == NULL ) return E_POINTER;
  621. if( m_pOwner == NULL ) return E_UNEXPECTED;
  622. if( m_pOwner->m_pUnk == NULL ) return E_UNEXPECTED;
  623. return m_pOwner->m_pUnk->QueryInterface(IID_IDispatch, (LPVOID*) ppDisp);
  624. }
  625. STDMETHODIMP CActiveXCtrl::TransformCoords(POINTL* pPtlHimetric, POINTF* pPtfContainer, DWORD dwFlags)
  626. {
  627. DUITRACE(_T("AX: CActiveXCtrl::TransformCoords"));
  628. return S_OK;
  629. }
  630. STDMETHODIMP CActiveXCtrl::TranslateAccelerator(MSG *pMsg, DWORD grfModifiers)
  631. {
  632. DUITRACE(_T("AX: CActiveXCtrl::TranslateAccelerator"));
  633. return S_FALSE;
  634. }
  635. STDMETHODIMP CActiveXCtrl::OnFocus(BOOL fGotFocus)
  636. {
  637. DUITRACE(_T("AX: CActiveXCtrl::OnFocus"));
  638. m_bFocused = (fGotFocus == TRUE);
  639. return S_OK;
  640. }
  641. STDMETHODIMP CActiveXCtrl::ShowPropertyFrame(void)
  642. {
  643. DUITRACE(_T("AX: CActiveXCtrl::ShowPropertyFrame"));
  644. return E_NOTIMPL;
  645. }
  646. STDMETHODIMP CActiveXCtrl::EnumObjects(DWORD grfFlags, IEnumUnknown** ppenum)
  647. {
  648. DUITRACE(_T("AX: CActiveXCtrl::EnumObjects"));
  649. if( ppenum == NULL ) return E_POINTER;
  650. if( m_pOwner == NULL ) return E_UNEXPECTED;
  651. *ppenum = new CActiveXEnum(m_pOwner->m_pUnk);
  652. return S_OK;
  653. }
  654. STDMETHODIMP CActiveXCtrl::LockContainer(BOOL fLock)
  655. {
  656. DUITRACE(_T("AX: CActiveXCtrl::LockContainer"));
  657. m_bLocked = fLock != FALSE;
  658. return S_OK;
  659. }
  660. STDMETHODIMP CActiveXCtrl::ParseDisplayName(IBindCtx *pbc, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut)
  661. {
  662. DUITRACE(_T("AX: CActiveXCtrl::ParseDisplayName"));
  663. return E_NOTIMPL;
  664. }
  665. HRESULT CActiveXCtrl::CreateActiveXWnd()
  666. {
  667. if( m_pWindow != NULL ) return S_OK;
  668. m_pWindow = new CActiveXWnd;
  669. if( m_pWindow == NULL ) return E_OUTOFMEMORY;
  670. m_pOwner->m_hwndHost = m_pWindow->Init(this, m_pOwner->GetManager()->GetPaintWindow());
  671. return S_OK;
  672. }
  673. /////////////////////////////////////////////////////////////////////////////////////
  674. //
  675. //
  676. HWND CActiveXWnd::Init(CActiveXCtrl* pOwner, HWND hWndParent)
  677. {
  678. m_pOwner = pOwner;
  679. UINT uStyle = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  680. Create(hWndParent, _T("UIActiveX"), uStyle, 0L, 0,0,0,0, NULL);
  681. return m_hWnd;
  682. }
  683. LPCTSTR CActiveXWnd::GetWindowClassName() const
  684. {
  685. return _T("ActiveXWnd");
  686. }
  687. void CActiveXWnd::OnFinalMessage(HWND hWnd)
  688. {
  689. if( m_pOwner->m_pOwner->GetManager()->IsLayered() ) {
  690. m_pOwner->m_pOwner->GetManager()->RemoveNativeWindow(hWnd);
  691. }
  692. }
  693. void CActiveXWnd::DoVerb(LONG iVerb)
  694. {
  695. if( m_pOwner == NULL ) return;
  696. if( m_pOwner->m_pOwner == NULL ) return;
  697. IOleObject* pUnk = NULL;
  698. m_pOwner->m_pOwner->GetControl(IID_IOleObject, (LPVOID*) &pUnk);
  699. if( pUnk == NULL ) return;
  700. CSafeRelease<IOleObject> RefOleObject = pUnk;
  701. IOleClientSite* pOleClientSite = NULL;
  702. m_pOwner->QueryInterface(IID_IOleClientSite, (LPVOID*) &pOleClientSite);
  703. CSafeRelease<IOleClientSite> RefOleClientSite = pOleClientSite;
  704. pUnk->DoVerb(iVerb, NULL, pOleClientSite, 0, m_hWnd, &m_pOwner->m_pOwner->GetPos());
  705. }
  706. LRESULT CActiveXWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  707. {
  708. LRESULT lRes=0;
  709. BOOL bHandled = TRUE;
  710. switch( uMsg ) {
  711. case WM_CREATE: lRes = OnCreate(uMsg, wParam, lParam, bHandled); break;
  712. case WM_TIMER: lRes = OnTimer(uMsg, wParam, lParam, bHandled); break;
  713. case WM_PAINT: lRes = OnPaint(uMsg, wParam, lParam, bHandled); break;
  714. case WM_PRINT: lRes = OnPrint(uMsg, wParam, lParam, bHandled); break;
  715. case WM_SETFOCUS: lRes = OnSetFocus(uMsg, wParam, lParam, bHandled); break;
  716. case WM_KILLFOCUS: lRes = OnKillFocus(uMsg, wParam, lParam, bHandled); break;
  717. case WM_ERASEBKGND: lRes = OnEraseBkgnd(uMsg, wParam, lParam, bHandled); break;
  718. case WM_MOUSEACTIVATE: lRes = OnMouseActivate(uMsg, wParam, lParam, bHandled); break;
  719. case WM_MOUSEWHEEL: break;
  720. default:
  721. bHandled = FALSE;
  722. }
  723. if( !bHandled ) return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  724. return lRes;
  725. }
  726. LRESULT CActiveXWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  727. {
  728. if( m_pOwner->m_pOwner->GetManager()->IsLayered() ) {
  729. ::SetTimer(m_hWnd, CARET_TIMERID, ::GetCaretBlinkTime(), NULL);
  730. }
  731. return 0;
  732. }
  733. LRESULT CActiveXWnd::OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  734. {
  735. if (wParam == DEFAULT_TIMERID) {
  736. if (m_pOwner->m_pOwner->GetManager()->IsLayered()) {
  737. //m_pOwner->m_pOwner->GetManager()->AddNativeWindow(m_pOwner->m_pOwner, m_hWnd);
  738. m_iLayeredTick += 1;
  739. if (m_iLayeredTick >= 10) {
  740. m_iLayeredTick = 0;
  741. m_bDrawCaret = !m_bDrawCaret;
  742. }
  743. }
  744. return 0;
  745. }
  746. bHandled = FALSE;
  747. return 0;
  748. }
  749. LRESULT CActiveXWnd::OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  750. {
  751. if( m_pOwner->m_pViewObject == NULL ) bHandled = FALSE;
  752. return 1;
  753. }
  754. LRESULT CActiveXWnd::OnMouseActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  755. {
  756. IOleObject* pUnk = NULL;
  757. m_pOwner->m_pOwner->GetControl(IID_IOleObject, (LPVOID*) &pUnk);
  758. if( pUnk == NULL ) return 0;
  759. CSafeRelease<IOleObject> RefOleObject = pUnk;
  760. DWORD dwMiscStatus = 0;
  761. pUnk->GetMiscStatus(DVASPECT_CONTENT, &dwMiscStatus);
  762. if( (dwMiscStatus & OLEMISC_NOUIACTIVATE) != 0 ) return 0;
  763. if( !m_pOwner->m_bInPlaceActive ) DoVerb(OLEIVERB_INPLACEACTIVATE);
  764. bHandled = FALSE;
  765. return 0;
  766. }
  767. LRESULT CActiveXWnd::OnSetFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  768. {
  769. bHandled = FALSE;
  770. m_pOwner->m_bFocused = true;
  771. if( !m_pOwner->m_bUIActivated ) DoVerb(OLEIVERB_UIACTIVATE);
  772. return 0;
  773. }
  774. LRESULT CActiveXWnd::OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  775. {
  776. bHandled = FALSE;
  777. m_pOwner->m_bFocused = false;
  778. return 0;
  779. }
  780. LRESULT CActiveXWnd::OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  781. {
  782. PAINTSTRUCT ps = { 0 };
  783. ::BeginPaint(m_hWnd, &ps);
  784. ::EndPaint(m_hWnd, &ps);
  785. return 1;
  786. }
  787. LRESULT CActiveXWnd::OnPrint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  788. {
  789. RECT rcClient;
  790. ::GetClientRect(m_hWnd, &rcClient);
  791. m_pOwner->m_pViewObject->Draw(DVASPECT_CONTENT, -1, NULL, NULL, NULL, (HDC)wParam, (RECTL*) &rcClient, NULL, NULL, NULL);
  792. if (m_bDrawCaret ) {
  793. RECT rcPos = m_pOwner->m_pOwner->GetPos();
  794. GUITHREADINFO guiThreadInfo;
  795. guiThreadInfo.cbSize = sizeof(GUITHREADINFO);
  796. ::GetGUIThreadInfo(NULL, &guiThreadInfo);
  797. if (guiThreadInfo.hwndCaret) {
  798. POINT ptCaret;
  799. ptCaret.x = guiThreadInfo.rcCaret.left;
  800. ptCaret.y = guiThreadInfo.rcCaret.top;
  801. ::ClientToScreen(guiThreadInfo.hwndCaret, &ptCaret);
  802. ::ScreenToClient(m_pOwner->m_pOwner->GetManager()->GetPaintWindow(), &ptCaret);
  803. if( ::PtInRect(&rcPos, ptCaret) ) {
  804. RECT rcCaret;
  805. rcCaret = guiThreadInfo.rcCaret;
  806. rcCaret.right = rcCaret.left;
  807. CRenderEngine::DrawLine((HDC)wParam, rcCaret, 1, 0xFF000000);
  808. }
  809. }
  810. }
  811. return 1;
  812. }
  813. /////////////////////////////////////////////////////////////////////////////////////
  814. //
  815. //
  816. IMPLEMENT_DUICONTROL(CActiveXUI)
  817. CActiveXUI::CActiveXUI() : m_pUnk(NULL), m_pControl(NULL), m_hwndHost(NULL), m_bCreated(false), m_bDelayCreate(true), m_bMFC(false)
  818. {
  819. m_clsid = IID_NULL;
  820. }
  821. CActiveXUI::~CActiveXUI()
  822. {
  823. ReleaseControl();
  824. }
  825. LPCTSTR CActiveXUI::GetClass() const
  826. {
  827. return _T("ActiveXUI");
  828. }
  829. LPVOID CActiveXUI::GetInterface(LPCTSTR pstrName)
  830. {
  831. if( _tcscmp(pstrName, DUI_CTR_ACTIVEX) == 0 ) return static_cast<CActiveXUI*>(this);
  832. return CControlUI::GetInterface(pstrName);
  833. }
  834. HWND CActiveXUI::GetHostWindow() const
  835. {
  836. return m_hwndHost;
  837. }
  838. static void PixelToHiMetric(const SIZEL* lpSizeInPix, LPSIZEL lpSizeInHiMetric)
  839. {
  840. #define HIMETRIC_PER_INCH 2540
  841. #define MAP_PIX_TO_LOGHIM(x,ppli) MulDiv(HIMETRIC_PER_INCH, (x), (ppli))
  842. #define MAP_LOGHIM_TO_PIX(x,ppli) MulDiv((ppli), (x), HIMETRIC_PER_INCH)
  843. int nPixelsPerInchX; // Pixels per logical inch along width
  844. int nPixelsPerInchY; // Pixels per logical inch along height
  845. HDC hDCScreen = ::GetDC(NULL);
  846. nPixelsPerInchX = ::GetDeviceCaps(hDCScreen, LOGPIXELSX);
  847. nPixelsPerInchY = ::GetDeviceCaps(hDCScreen, LOGPIXELSY);
  848. ::ReleaseDC(NULL, hDCScreen);
  849. lpSizeInHiMetric->cx = MAP_PIX_TO_LOGHIM(lpSizeInPix->cx, nPixelsPerInchX);
  850. lpSizeInHiMetric->cy = MAP_PIX_TO_LOGHIM(lpSizeInPix->cy, nPixelsPerInchY);
  851. }
  852. void CActiveXUI::SetVisible(bool bVisible)
  853. {
  854. CControlUI::SetVisible(bVisible);
  855. if( m_hwndHost != NULL && !m_pControl->m_bWindowless )
  856. ::ShowWindow(m_hwndHost, IsVisible() ? SW_SHOW : SW_HIDE);
  857. }
  858. void CActiveXUI::SetInternVisible(bool bVisible)
  859. {
  860. CControlUI::SetInternVisible(bVisible);
  861. if( m_hwndHost != NULL && !m_pControl->m_bWindowless )
  862. ::ShowWindow(m_hwndHost, IsVisible() ? SW_SHOW : SW_HIDE);
  863. }
  864. void CActiveXUI::SetPos(RECT rc, bool bNeedInvalidate)
  865. {
  866. CControlUI::SetPos(rc, bNeedInvalidate);
  867. if( !m_bCreated ) DoCreateControl();
  868. if( m_pUnk == NULL ) return;
  869. if( m_pControl == NULL ) return;
  870. SIZEL hmSize = { 0 };
  871. SIZEL pxSize = { 0 };
  872. pxSize.cx = m_rcItem.right - m_rcItem.left;
  873. pxSize.cy = m_rcItem.bottom - m_rcItem.top;
  874. PixelToHiMetric(&pxSize, &hmSize);
  875. if( m_pUnk != NULL ) {
  876. m_pUnk->SetExtent(DVASPECT_CONTENT, &hmSize);
  877. }
  878. if( m_pControl->m_pInPlaceObject != NULL ) {
  879. CDuiRect rcItem = m_rcItem;
  880. if( !m_pControl->m_bWindowless ) rcItem.ResetOffset();
  881. m_pControl->m_pInPlaceObject->SetObjectRects(&rcItem, &rcItem);
  882. }
  883. if( !m_pControl->m_bWindowless ) {
  884. ASSERT(m_pControl->m_pWindow);
  885. ::MoveWindow(*m_pControl->m_pWindow, m_rcItem.left, m_rcItem.top, m_rcItem.right - m_rcItem.left, m_rcItem.bottom - m_rcItem.top, TRUE);
  886. }
  887. }
  888. void CActiveXUI::Move(SIZE szOffset, bool bNeedInvalidate)
  889. {
  890. CControlUI::Move(szOffset, bNeedInvalidate);
  891. if( !m_pControl->m_bWindowless ) {
  892. ASSERT(m_pControl->m_pWindow);
  893. ::MoveWindow(*m_pControl->m_pWindow, m_rcItem.left, m_rcItem.top, m_rcItem.right - m_rcItem.left, m_rcItem.bottom - m_rcItem.top, TRUE);
  894. }
  895. }
  896. bool CActiveXUI::DoPaint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
  897. {
  898. if( m_pControl != NULL && m_pControl->m_bWindowless && m_pControl->m_pViewObject != NULL )
  899. {
  900. m_pControl->m_pViewObject->Draw(DVASPECT_CONTENT, -1, NULL, NULL, NULL, hDC, (RECTL*) &m_rcItem, (RECTL*) &m_rcItem, NULL, NULL);
  901. }
  902. return true;
  903. }
  904. void CActiveXUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  905. {
  906. if( _tcscmp(pstrName, _T("clsid")) == 0 ) CreateControl(pstrValue);
  907. else if( _tcscmp(pstrName, _T("modulename")) == 0 ) SetModuleName(pstrValue);
  908. else if( _tcscmp(pstrName, _T("delaycreate")) == 0 ) SetDelayCreate(_tcscmp(pstrValue, _T("true")) == 0);
  909. else CControlUI::SetAttribute(pstrName, pstrValue);
  910. }
  911. LRESULT CActiveXUI::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled)
  912. {
  913. if( m_pControl == NULL ) return 0;
  914. ASSERT(m_pControl->m_bWindowless);
  915. if( !m_pControl->m_bInPlaceActive ) return 0;
  916. if( m_pControl->m_pInPlaceObject == NULL ) return 0;
  917. if( !IsMouseEnabled() && uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST ) return 0;
  918. bool bWasHandled = true;
  919. if( (uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST) || uMsg == WM_SETCURSOR ) {
  920. // Mouse message only go when captured or inside rect
  921. DWORD dwHitResult = m_pControl->m_bCaptured ? HITRESULT_HIT : HITRESULT_OUTSIDE;
  922. if( dwHitResult == HITRESULT_OUTSIDE && m_pControl->m_pViewObject != NULL ) {
  923. IViewObjectEx* pViewEx = NULL;
  924. m_pControl->m_pViewObject->QueryInterface(IID_IViewObjectEx, (LPVOID*) &pViewEx);
  925. if( pViewEx != NULL ) {
  926. POINT ptMouse = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  927. pViewEx->QueryHitPoint(DVASPECT_CONTENT, &m_rcItem, ptMouse, 0, &dwHitResult);
  928. pViewEx->Release();
  929. }
  930. }
  931. if( dwHitResult != HITRESULT_HIT ) return 0;
  932. if( uMsg == WM_SETCURSOR ) bWasHandled = false;
  933. }
  934. else if( uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST ) {
  935. // Keyboard messages just go when we have focus
  936. if( !IsFocused() ) return 0;
  937. }
  938. else {
  939. switch( uMsg ) {
  940. case WM_HELP:
  941. case WM_CONTEXTMENU:
  942. bWasHandled = false;
  943. break;
  944. default:
  945. return 0;
  946. }
  947. }
  948. LRESULT lResult = 0;
  949. HRESULT Hr = m_pControl->m_pInPlaceObject->OnWindowMessage(uMsg, wParam, lParam, &lResult);
  950. if( Hr == S_OK ) bHandled = bWasHandled;
  951. return lResult;
  952. }
  953. bool CActiveXUI::IsDelayCreate() const
  954. {
  955. return m_bDelayCreate;
  956. }
  957. void CActiveXUI::SetDelayCreate(bool bDelayCreate)
  958. {
  959. if( m_bDelayCreate == bDelayCreate ) return;
  960. if( bDelayCreate == false ) {
  961. if( m_bCreated == false && m_clsid != IID_NULL ) DoCreateControl();
  962. }
  963. m_bDelayCreate = bDelayCreate;
  964. }
  965. bool CActiveXUI::IsMFC() const
  966. {
  967. return m_bMFC;
  968. }
  969. void CActiveXUI::SetMFC(bool bMFC/* = false*/)
  970. {
  971. if( m_bMFC == bMFC ) return;
  972. m_bMFC = bMFC;
  973. }
  974. bool CActiveXUI::CreateControl(LPCTSTR pstrCLSID)
  975. {
  976. CLSID clsid = { 0 };
  977. OLECHAR szCLSID[100] = { 0 };
  978. #ifndef _UNICODE
  979. ::MultiByteToWideChar(::GetACP(), 0, pstrCLSID, -1, szCLSID, lengthof(szCLSID) - 1);
  980. #else
  981. _tcsncpy(szCLSID, pstrCLSID, lengthof(szCLSID) - 1);
  982. #endif
  983. if( pstrCLSID[0] == '{' ) ::CLSIDFromString(szCLSID, &clsid);
  984. else ::CLSIDFromProgID(szCLSID, &clsid);
  985. return CreateControl(clsid);
  986. }
  987. bool CActiveXUI::CreateControl(const CLSID clsid)
  988. {
  989. ASSERT(clsid!=IID_NULL);
  990. if( clsid == IID_NULL ) return false;
  991. m_bCreated = false;
  992. m_clsid = clsid;
  993. if( !m_bDelayCreate ) DoCreateControl();
  994. return true;
  995. }
  996. void CActiveXUI::ReleaseControl()
  997. {
  998. // ÒƳýÏûÏ¢Á´
  999. if(m_pManager != NULL) m_pManager->RemoveMessageFilter(this);
  1000. if( m_pUnk != NULL ) {
  1001. IObjectWithSite* pSite = NULL;
  1002. m_pUnk->QueryInterface(IID_IObjectWithSite, (LPVOID*) &pSite);
  1003. if( pSite != NULL ) {
  1004. pSite->SetSite(NULL);
  1005. pSite->Release();
  1006. }
  1007. if(!IsMFC()) {
  1008. m_pUnk->Close(OLECLOSE_NOSAVE);
  1009. }
  1010. m_pUnk->SetClientSite(NULL);
  1011. m_pUnk->Release();
  1012. m_pUnk = NULL;
  1013. }
  1014. // Ïú»ÙCActiveXCtrl
  1015. if( m_pControl != NULL ) {
  1016. m_pControl->m_pOwner = NULL;
  1017. m_pControl->Release();
  1018. m_pControl = NULL;
  1019. }
  1020. m_hwndHost = NULL;
  1021. }
  1022. typedef HRESULT (__stdcall *DllGetClassObjectFunc)(REFCLSID rclsid, REFIID riid, LPVOID* ppv);
  1023. bool CActiveXUI::DoCreateControl()
  1024. {
  1025. ReleaseControl();
  1026. // At this point we'll create the ActiveX control
  1027. m_bCreated = true;
  1028. IOleControl* pOleControl = NULL;
  1029. HRESULT Hr = -1;
  1030. if( !m_sModuleName.IsEmpty() ) {
  1031. HMODULE hModule = ::LoadLibrary((LPCTSTR)m_sModuleName);
  1032. if( hModule != NULL ) {
  1033. IClassFactory* aClassFactory = NULL;
  1034. DllGetClassObjectFunc aDllGetClassObjectFunc = (DllGetClassObjectFunc)::GetProcAddress(hModule, "DllGetClassObject");
  1035. Hr = aDllGetClassObjectFunc(m_clsid, IID_IClassFactory, (LPVOID*)&aClassFactory);
  1036. if( SUCCEEDED(Hr) ) {
  1037. Hr = aClassFactory->CreateInstance(NULL, IID_IOleObject, (LPVOID*)&pOleControl);
  1038. }
  1039. aClassFactory->Release();
  1040. }
  1041. }
  1042. if( FAILED(Hr) ) {
  1043. Hr = ::CoCreateInstance(m_clsid, NULL, CLSCTX_ALL, IID_IOleControl, (LPVOID*)&pOleControl);
  1044. }
  1045. ASSERT(SUCCEEDED(Hr));
  1046. if( FAILED(Hr) ) return false;
  1047. pOleControl->QueryInterface(IID_IOleObject, (LPVOID*) &m_pUnk);
  1048. pOleControl->Release();
  1049. if( m_pUnk == NULL ) return false;
  1050. // Create the host too
  1051. m_pControl = new CActiveXCtrl();
  1052. m_pControl->m_pOwner = this;
  1053. // More control creation stuff
  1054. DWORD dwMiscStatus = 0;
  1055. m_pUnk->GetMiscStatus(DVASPECT_CONTENT, &dwMiscStatus);
  1056. IOleClientSite* pOleClientSite = NULL;
  1057. m_pControl->QueryInterface(IID_IOleClientSite, (LPVOID*) &pOleClientSite);
  1058. CSafeRelease<IOleClientSite> RefOleClientSite = pOleClientSite;
  1059. // Initialize control
  1060. if( (dwMiscStatus & OLEMISC_SETCLIENTSITEFIRST) != 0 ) m_pUnk->SetClientSite(pOleClientSite);
  1061. IPersistStreamInit* pPersistStreamInit = NULL;
  1062. m_pUnk->QueryInterface(IID_IPersistStreamInit, (LPVOID*) &pPersistStreamInit);
  1063. if( pPersistStreamInit != NULL ) {
  1064. Hr = pPersistStreamInit->InitNew();
  1065. pPersistStreamInit->Release();
  1066. }
  1067. if( FAILED(Hr) ) return false;
  1068. if( (dwMiscStatus & OLEMISC_SETCLIENTSITEFIRST) == 0 ) m_pUnk->SetClientSite(pOleClientSite);
  1069. // Grab the view...
  1070. Hr = m_pUnk->QueryInterface(IID_IViewObjectEx, (LPVOID*) &m_pControl->m_pViewObject);
  1071. if( FAILED(Hr) ) Hr = m_pUnk->QueryInterface(IID_IViewObject2, (LPVOID*) &m_pControl->m_pViewObject);
  1072. if( FAILED(Hr) ) Hr = m_pUnk->QueryInterface(IID_IViewObject, (LPVOID*) &m_pControl->m_pViewObject);
  1073. // Activate and done...
  1074. m_pUnk->SetHostNames(OLESTR("UIActiveX"), NULL);
  1075. if( m_pManager != NULL ) m_pManager->SendNotify((CControlUI*)this, DUI_MSGTYPE_SHOWACTIVEX, 0, 0, false);
  1076. if( (dwMiscStatus & OLEMISC_INVISIBLEATRUNTIME) == 0 ) {
  1077. try
  1078. {
  1079. if(m_pManager != NULL) Hr = m_pUnk->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, pOleClientSite, 0, m_pManager->GetPaintWindow(), &m_rcItem);
  1080. }
  1081. catch (...)
  1082. {
  1083. }
  1084. }
  1085. IObjectWithSite* pSite = NULL;
  1086. m_pUnk->QueryInterface(IID_IObjectWithSite, (LPVOID*) &pSite);
  1087. if( pSite != NULL ) {
  1088. pSite->SetSite(static_cast<IOleClientSite*>(m_pControl));
  1089. pSite->Release();
  1090. }
  1091. return SUCCEEDED(Hr);
  1092. }
  1093. HRESULT CActiveXUI::GetControl(const IID iid, LPVOID* ppRet)
  1094. {
  1095. ASSERT(ppRet!=NULL);
  1096. ASSERT(*ppRet==NULL);
  1097. if( ppRet == NULL ) return E_POINTER;
  1098. if( m_pUnk == NULL ) return E_PENDING;
  1099. return m_pUnk->QueryInterface(iid, (LPVOID*) ppRet);
  1100. }
  1101. CLSID CActiveXUI::GetClisd() const
  1102. {
  1103. return m_clsid;
  1104. }
  1105. CDuiString CActiveXUI::GetModuleName() const
  1106. {
  1107. return m_sModuleName;
  1108. }
  1109. void CActiveXUI::SetModuleName(LPCTSTR pstrText)
  1110. {
  1111. m_sModuleName = pstrText;
  1112. }
  1113. } // namespace DuiLib