/** * @file XTPWebBrowserCtrl.h * * @copyright * (c) 1998-2025 Codejock Software, All Rights Reserved. * * This source file is the property of Codejock Software and must not be * redistributed by any means without the explicit written permission of * Codejock Software. * * The use of this source code is governed by the terms and conditions specified * in the Toolkit Pro license agreement. Codejock Software grants you, as a * single software developer, the limited right to use this software on one * computer only. * * Contact Information: * support@codejock.com * http://www.codejock.com * */ #if !defined(__XTPWEBBROWSERCTRL_H__) /** @cond */ # define __XTPWEBBROWSERCTRL_H__ /** @endcond */ # if _MSC_VER >= 1000 # pragma once # endif // _MSC_VER >= 1000 # include "Common/Base/Diagnostic/XTPDisableAdvancedWarnings.h" # include # include "Common/Base/Diagnostic/XTPEnableAdvancedWarnings.h" # include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" /** @cond */ class CXTPWebBrowserProvider; /** @endcond */ /** * @brief * Defines possible WebBrowser providers. */ enum XTPWebBrowserProvider { xtpWebBrowserInvalid = -1, /**< Specifies an invalid WebBrowser provider. */ xtpWebBrowserShellExplorer, /**< Specifies the ShellExplorer WebBrowser provider, which is also used by the Microsoft Internet Explorer browser. */ xtpWebBrowserWebView, /**< Specifies the WebView2 WebBrowser provider, which is also used by the Microsoft Edge browser. */ xtpWebBrowserLatest = xtpWebBrowserWebView /**< Specifies the latest supported WebBrowser provider. */ }; /** * @brief * Implements the WebBrowser control that is capable of hosting different supported Web * browsers. The class provides a narrow interface that gives access to the most frequently * needed functionality that is provided by each supported provider. The unified event handling * interface simplifies event processing. */ class _XTP_EXT_CLASS CXTPWebBrowserCtrl : public CWnd , private IXTPWebBrowserHost { /** @cond */ DECLARE_DYNAMIC(CXTPWebBrowserCtrl) /** @endcond */ public: /** * @brief * Handles control construction. * @param nProvider Specifies the provider to initialize by default. It is not guaranteed that * the specified provider will be activated as at the point of construction it * is not yet known which of the providers are available and supported. In case * the specified provider is impossible to activate the ShellExplorer provider * will be activated by default. */ CXTPWebBrowserCtrl(XTPWebBrowserProvider nProvider = xtpWebBrowserLatest); /** * @brief * Handles control destruction. */ virtual ~CXTPWebBrowserCtrl(); /** * @brief * Registers the window class if it has not already been registered. * @param hInstance Instance of the resource where the control is located. * @return * TRUE if the window class was successfully registered, otherwise FALSE. */ BOOL RegisterWindowClass(HINSTANCE hInstance = NULL); /** * @brief * Creates a preview control window object. * @param dwStyle Bitwise combination (OR) of window styles; otherwise NULL for * the default window style. * @param rect The size and location of the window relative to the screen or * the parent window. * @param pParentWnd For a child window, pointer to the parent window; otherwise, a * pointer to the owner window if the window has an owner. * @param nID For a child window, the window ID; otherwise, the ID of a menu * for the window. * @param pContext CCreateContext object pointer or NULL. * @return * TRUE if the method was successful, otherwise FALSE. */ virtual BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL); /** * @brief * Creates a preview control window object. * @param dwExStyle Bitwise combination (OR) of extended window styles; otherwise * NULL for the default extended window style. * @param dwStyle Bitwise combination (OR) of window styles; otherwise NULL for * the default window style. * @param x The initial horizontal distance of the window from the left side * of the screen or the parent window. * @param y The initial vertical distance of the window from the top of the * screen or the parent window. * @param nWidth The width, in pixels, of the window. * @param nHeight The height, in pixels, of the window. * @param hWndParent For a child window, the handle to the parent window; otherwise, * the handle of the owner window if the window has an owner. * @param nIDorHMenu For a child window, the window ID; otherwise, the ID of a menu * for the window. * @param lpParam Pointer to user data that is passed to the CWnd::OnCreate method * in the lpCreateParams field. * @return * TRUE if the method was successful, otherwise FALSE. */ virtual BOOL CreateEx(DWORD dwExStyle, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam = NULL); /** * @brief * Creates a preview control window object. * @param dwExStyle Bitwise combination (OR) of extended window styles; otherwise * NULL for the default extended window style. * @param dwStyle Bitwise combination (OR) of window styles; otherwise NULL for * the default window style. * @param rect The size and location of the window relative to the screen or * the parent window. * @param pParentWnd For a child window, pointer to the parent window; otherwise, a * pointer to the owner window if the window has an owner. * @param nID For a child window, the window ID; otherwise, the ID of a menu * for the window. * @param lpParam Pointer to user data that is passed to the CWnd::OnCreate method * in the lpCreateParams field. * @return * TRUE if the method was successful, otherwise FALSE. */ virtual BOOL CreateEx(DWORD dwExStyle, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, LPVOID lpParam = NULL); using CWnd::Create; using CWnd::CreateEx; /** * @brief * Gets custom properties that providers can use if provided. * @param strName Property name. * @return * Current property value if it is found or NULL otherwise. * @details * Custom properties are held by the control object and don't belong * to the current provider set, however they can be accessed by any provider. * Currently supported properties include: * "UDF" The string path value that indicates the UserDefaultFolder * for WebView2 provider. * "OverrideKeyPath" The string registry key path value relative to * HKEY_CURRENT_USER that speicifies a registry key * to override default Internet Explorer settings. * "OptionKeyPath" The string registry key path value relative to * HKEY_CURRENT_USER that speicifies a registry key * to use as a default location of Internet Explorer * settings. * In order to apply custom properties to a provider that is already created * the CXTPWebBrowserCtrl::ReCreateProvider method must be called. * @see * CXTPWebBrowserCtrl::ReCreateProvider */ virtual const COleVariant* GetProperty(const CString& strName) const; /** * @brief * Sets custom properties that providers can use if provided. * @param strName Property name. * @param pvtValue Property value. The control will hold the ownership * of the allocated data. NULL pointer value will cause * removal of the previously set property value. * @details * Custom properties are held by the control object and don't belong * to the current provider set, however they can be accessed by any provider. * Currently supported properties include: * "UDF" The string path value that indicates the UserDefaultFolder * for WebView2 provider. * "OverrideKeyPath" The string registry key path value relative to * HKEY_CURRENT_USER that speicifies a registry key * to override default Internet Explorer settings. * "OptionKeyPath" The string registry key path value relative to * HKEY_CURRENT_USER that speicifies a registry key * to use as a default location of Internet Explorer * settings. * In order to apply custom properties to a provider that is already created * the CXTPWebBrowserCtrl::ReCreateProvider method must be called. * @see * CXTPWebBrowserCtrl::ReCreateProvider */ void SetProperty(const CString& strName, COleVariant* pvtValue); /** * @brief * Re-creates the current provider, i.e. the actual Web browser instance * will be destroyed and re-created. The external object will be automatically * bound if assigned, custom properties won't be affected, and the previously * navigated location will be restored. * @return * TRUE if a provider is re-created successfully, FALSE otherwise. */ BOOL ReCreateProvider(); /** * @brief * Obtains the active WebBrowser provider type. * @return * The type of of the active WebBrowser provider. */ XTPWebBrowserProvider GetProviderType() const; /** * @brief * Sets the active WebBrowser provider type. * @param nProvider A new provider type as defined by XTPWebBrowserProvider enumeration. * @return * TRUE if the WebBrowser provider has been changed. * @details * All previously set provider specific options will get reset. */ BOOL SetProviderType(XTPWebBrowserProvider nProvider); /** * @brief * Obtains the active WebBrowser provider object or NULL if no provider has been activated * yet or provider activation hasn't been completed. * @return * A pointer to the base WebBrowser provider implementation. The pointer can be cast to the * actual WebBrowser provider implementation if extended access is required. */ CXTPWebBrowserProvider* GetProvider() const; /** * @brief * Obtains the native WebBrowser provider object or NULL if no provider is activated or the * current provider does not provide access to its core object via IUnknown. * @return * Obtains the native WebBrowser provider object. * @details * The ShellExplorer provider will return IWebBrowserApp object. * The WebView provider will return ICoreWebView2Controller object. */ IUnknown* GetBrowserObject() const; /** * @brief * Gets the event sink interface for handling WebBrowser events. * @return * The current even sink interface pointer. * @details * The control does not manage even sink object lifetime. */ IXTPWebBrowserEventSink* GetEventSink() const; /** * @brief * Sets the event sink interface for handling WebBrowser events. * @param pEventSink The new event sink interface pointer or NULL to disable event handling. * @details * The control does not manage even sink object lifetime. */ void SetEventSink(IXTPWebBrowserEventSink* pEventSink); /** * @brief * Performs navigation to the specified URL. * @param pUrl The URL value to navigate to. * @return * S_OK if navigation is succeeded or an error code otherwise. * @details * If a provider is no yet activated the navigation will be postponed until a provider is * activated. */ HRESULT Navigate(LPCTSTR pUrl); /** * @brief * Loads specified HTML code. * @param pHtml The HTML code pointer to load. * @return * S_OK if success or an error code otherwise. * @details * To clear off the currently loaded web page navigation to 'about:blank' * is performed prior to loading the specified HTML content. */ HRESULT LoadHtml(LPCTSTR pHtml); /** * @brief * Waits until an initiated navigation completes. * @return * S_OK if success or an error code otherwise. If an error code * is returned it doesn't indicate a navigation error, it only * indicate inability to wait for some reason. */ HRESULT WaitUntilReady(); /** * @brief * Executes a script by the active WebBrowser provider. * @param pText Script code text. * @param pvtResult The optional pointer to the variant object that is supposed to receive the * result of the executed script. * @return * S_OK if script execution or its attempt are successful or an error code otherwise. * @details * The language of the script code is provider specific. Currently each provider supports * JavaScript by default. */ HRESULT ExecuteScript(LPCWSTR pText, CComVariant* pvtResult = NULL); /** * @brief * Gets the object to be used by the "window.external" propery in the script execution * context. * @return * The current external object. * @details * The WebBrowser control holds a reference to the specified external object until a new * object is set or the control is destructed. */ virtual IDispatch* GetExternal() const; /** * @brief * Sets the object to be used by the "window.external" propery in the script execution * context. * @param pExternal The new external object to be used. * @details * The WebBrowser control holds a reference to the specified external object until a new * object is set or the control is destructed. */ void SetExternal(IDispatch* pExternal); protected: /** @cond */ DECLARE_MESSAGE_MAP() virtual BOOL PreCreateWindow(CREATESTRUCT& cs); virtual void PreSubclassWindow(); virtual BOOL OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult); /** @endcond */ private: // IXTPWebBrowserHost overrides virtual IUnknown* GetHostUnknown() const; virtual CWnd& GetHostWindow() const; virtual void ShellExplorer_OnStatusTextChange(LPCTSTR lpszText); virtual void ShellExplorer_OnProgressChange(long nProgress, long nProgressMax); virtual void ShellExplorer_OnCommandStateChange(long nCommand, BOOL bEnable); virtual void ShellExplorer_OnDownloadBegin(); virtual void ShellExplorer_OnDownloadComplete(); virtual void ShellExplorer_OnTitleChange(LPCTSTR lpszText); virtual void ShellExplorer_OnPropertyChange(LPCTSTR lpszProperty); virtual void ShellExplorer_OnNewWindow2(LPDISPATCH* ppDisp, BOOL* pbCancel); virtual void ShellExplorer_OnQuit(); virtual void ShellExplorer_OnVisible(BOOL bVisible); virtual void ShellExplorer_OnToolBar(BOOL bToolBar); virtual void ShellExplorer_OnMenuBar(BOOL bMenuBar); virtual void ShellExplorer_OnStatusBar(BOOL bStatusBar); virtual void ShellExplorer_OnFullScreen(BOOL bFullScreen); virtual void ShellExplorer_OnTheaterMode(BOOL bTheaterMode); virtual void ShellExplorer_OnNavigateComplete2(LPDISPATCH pDisp, VARIANT* pvtURL); virtual void ShellExplorer_OnBeforeNavigate2(LPDISPATCH pDisp, VARIANT* pvtURL, VARIANT* pvtFlags, VARIANT* pvtTargetFrameName, VARIANT* pvtPostData, VARIANT* pvtHeaders, BOOL* pbCancel); virtual void ShellExplorer_OnDocumentComplete(LPDISPATCH pDisp, VARIANT* pvtURL); virtual void ShellExplorer_OnNavigateError(LPDISPATCH pDisp, VARIANT* pvtURL, VARIANT* pvtTargetFrameName, VARIANT* pvtStatusCode, BOOL* pbCancel); virtual void ShellExplorer_OnWindowSetResizable(BOOL bResizable); virtual void ShellExplorer_OnWindowSetLeft(long nLeft); virtual void ShellExplorer_OnWindowSetTop(long nTop); virtual void ShellExplorer_OnWindowSetWidth(long nWidth); virtual void ShellExplorer_OnWindowSetHeight(long nHeight); virtual void ShellExplorer_OnWindowClosing(BOOL bIsChildWindow, BOOL FAR* pbCancel); virtual void ShellExplorer_OnSetSecureLockIcon(long nSecureLockIcon); virtual HRESULT WebView2_NavigationStarting(ICoreWebView2NavigationStartingEventArgs* args); virtual HRESULT WebView2_ContentLoading(ICoreWebView2ContentLoadingEventArgs* args); virtual HRESULT WebView2_SourceChanged(ICoreWebView2SourceChangedEventArgs* args); virtual HRESULT WebView2_HistoryChanged(IUnknown* args); virtual HRESULT WebView2_NavigationCompleted(ICoreWebView2NavigationCompletedEventArgs* args); virtual HRESULT WebView2_FrameNavigationStarting(ICoreWebView2NavigationStartingEventArgs* args); virtual HRESULT WebView2_FrameNavigationCompleted(ICoreWebView2NavigationCompletedEventArgs* args); virtual HRESULT WebView2_ScriptDialogOpening(ICoreWebView2ScriptDialogOpeningEventArgs* args); virtual HRESULT WebView2_PermissionRequested(ICoreWebView2PermissionRequestedEventArgs* args); virtual HRESULT WebView2_ProcessFailed(ICoreWebView2ProcessFailedEventArgs* args); virtual HRESULT WebView2_NewWindowRequested(ICoreWebView2NewWindowRequestedEventArgs* args); virtual HRESULT WebView2_DocumentTitleChanged(IUnknown* args); virtual HRESULT WebView2_ContainsFullScreenElementChanged(IUnknown* args); virtual HRESULT WebView2_WebResourceRequested(ICoreWebView2WebResourceRequestedEventArgs* args); virtual HRESULT WebView2_WindowCloseRequested(IUnknown* args); private: BOOL IsValid() const; BOOL InitializeProvider(); void ReleaseProvider(); BOOL FallbackProvider(); afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnDestroy(); afx_msg void OnWindowPosChanging(WINDOWPOS* lpwndpos); afx_msg void OnWindowPosChanged(WINDOWPOS* lpwndpos); private: static const UINT m_WM_INITIALIZE; XTPWebBrowserProvider m_nProvider; CXTPWebBrowserProvider* m_pProvider; BOOL m_bSubclassed; CString m_strPendlingUrl; IXTPWebBrowserEventSink* m_pEventSink; IDispatch* m_pExternal; CMap m_mapProps; }; /** @cond */ AFX_INLINE CXTPWebBrowserProvider* CXTPWebBrowserCtrl::GetProvider() const { return const_cast(this)->m_pProvider; } AFX_INLINE XTPWebBrowserProvider CXTPWebBrowserCtrl::GetProviderType() const { return m_nProvider; } AFX_INLINE void CXTPWebBrowserCtrl::SetEventSink(IXTPWebBrowserEventSink* pEventSink) { m_pEventSink = pEventSink; } AFX_INLINE IXTPWebBrowserEventSink* CXTPWebBrowserCtrl::GetEventSink() const { return m_pEventSink; } AFX_INLINE IDispatch* CXTPWebBrowserCtrl::GetExternal() const { return m_pExternal; } /** @endcond */ # include "Common/Base/Diagnostic/XTPEnableNoisyWarnings.h" /** @cond */ #endif // #if !defined(__XTPWEBBROWSERCTRL_H__) /** @endcond */