Team Fortress 2 Source Code as on 22/4/2020
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.

323 lines
11 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Creates a HTML control
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef HTML_H
  8. #define HTML_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include <vgui/VGUI.h>
  13. #include <vgui/IImage.h>
  14. #include <vgui_controls/Panel.h>
  15. #include <vgui_controls/PHandle.h>
  16. #include <vgui_controls/FileOpenDialog.h>
  17. #include <vgui_controls/TextEntry.h>
  18. #include <tier1/utlmap.h>
  19. #ifndef VERSION_SAFE_STEAM_API_INTERFACES
  20. #define VERSION_SAFE_STEAM_API_INTERFACES
  21. #endif
  22. #include "steam/steam_api.h"
  23. namespace vgui
  24. {
  25. //-----------------------------------------------------------------------------
  26. // Purpose: Control to display HTML content
  27. // This control utilises a hidden IE window to render a HTML page for you.
  28. // It can load any valid URL (i.e local files or web pages), you cannot dynamically change the
  29. // content however (internally to the control that is).
  30. //-----------------------------------------------------------------------------
  31. class HTML: public Panel
  32. {
  33. DECLARE_CLASS_SIMPLE( HTML, Panel );
  34. // TODO::STYLE
  35. //DECLARE_STYLE_BASE( "HTML" );
  36. public:
  37. HTML(Panel *parent,const char *name, bool allowJavaScript = false, bool bPopupWindow = false);
  38. ~HTML();
  39. // IHTML pass through functions
  40. virtual void OpenURL( const char *URL, const char *pchPostData, bool bForce = false );
  41. virtual bool StopLoading();
  42. virtual bool Refresh();
  43. virtual void OnMove();
  44. virtual void RunJavascript( const char *pchScript );
  45. virtual void GoBack();
  46. virtual void GoForward();
  47. virtual bool BCanGoBack();
  48. virtual bool BCanGoFoward();
  49. // event functions you can override and specialize behavior of
  50. virtual bool OnStartRequest( const char *url, const char *target, const char *pchPostData, bool bIsRedirect );
  51. virtual void OnFinishRequest(const char *url, const char *pageTitle, const CUtlMap < CUtlString, CUtlString > &headers ) {}
  52. virtual void OnSetHTMLTitle( const char *pchTitle ) {}
  53. virtual void OnLinkAtPosition( const char *pchURL ) {}
  54. virtual void OnURLChanged( const char *url, const char *pchPostData, bool bIsRedirect ) {}
  55. virtual bool OnOpenNewTab( const char *pchURL, bool bForeground ) { return false; }
  56. // configuration
  57. virtual void SetScrollbarsEnabled(bool state);
  58. virtual void SetContextMenuEnabled(bool state);
  59. virtual void SetViewSourceEnabled( bool state );
  60. virtual void NewWindowsOnly( bool state );
  61. bool IsScrolledToBottom();
  62. bool IsScrollbarVisible();
  63. // url handlers, lets you have web page links fire vgui events
  64. // use to have custom web page links, eg. "steam://open/subscriptionpage"
  65. // message contains "CustomURL", "url"
  66. virtual void AddCustomURLHandler(const char *customProtocolName, vgui::Panel *target);
  67. // overridden to paint our special web browser texture
  68. virtual void Paint();
  69. // pass messages to the texture component to tell it about resizes
  70. virtual void OnSizeChanged(int wide,int tall);
  71. // pass mouse clicks through
  72. virtual void OnMousePressed(MouseCode code);
  73. virtual void OnMouseReleased(MouseCode code);
  74. virtual void OnCursorMoved(int x,int y);
  75. virtual void OnMouseDoublePressed(MouseCode code);
  76. virtual void OnKeyTyped(wchar_t unichar);
  77. virtual void OnKeyCodeTyped(KeyCode code);
  78. virtual void OnKeyCodeReleased(KeyCode code);
  79. virtual void PerformLayout();
  80. virtual void OnMouseWheeled(int delta);
  81. virtual void PostChildPaint();
  82. /* message posting:
  83. "HTMLSliderMoved" - indicates the scrollbar has moved
  84. "OnURLChanged" - indicates a new URL is being loaded
  85. "url"
  86. "postdata"
  87. "OnFinishRequest" - indicates all url loaded has completed
  88. "HTMLBackRequested" - mouse4 has been pressed on the dialog
  89. "HTMLForwardRequested" - mouse5 has been pressed on the dialog
  90. "SecurityStatus" - indicates the SSL status of the page (disabled,good,bad)
  91. "url"
  92. "secure" - true if an ssl page
  93. "certerror" - true if there is a cert error loading the page
  94. "isevcert" - true if this is an EV style cert
  95. "certname" - name of the entity this cert is issued to
  96. */
  97. MESSAGE_FUNC_INT( OnSetCursorVGUI, "SetCursor", cursor );
  98. virtual void OnCommand( const char *pchCommand );
  99. void AddHeader( const char *pchHeader, const char *pchValue );
  100. void OnKillFocus();
  101. void OnSetFocus();
  102. void Find( const char *pchSubStr );
  103. void StopFind();
  104. void FindNext();
  105. void FindPrevious();
  106. void ShowFindDialog();
  107. void HideFindDialog();
  108. bool FindDialogVisible();
  109. int HorizontalScrollMax() { return m_scrollHorizontal.m_nMax; }
  110. int VerticalScrollMax() { return m_scrollVertical.m_nMax; }
  111. void GetLinkAtPosition( int x, int y );
  112. void HidePopup();
  113. #ifdef DBGFLAG_VALIDATE
  114. virtual void Validate( CValidator &validator, const char *pchName )
  115. {
  116. ValidateObj( m_CustomURLHandlers );
  117. BaseClass::Validate( validator, pchName );
  118. }
  119. #endif // DBGFLAG_VALIDATE
  120. ISteamHTMLSurface *SteamHTMLSurface() { return m_SteamAPIContext.SteamHTMLSurface(); }
  121. void OnHTMLMouseMoved( int x, int y )
  122. {
  123. if ( m_SteamAPIContext.SteamHTMLSurface() )
  124. m_SteamAPIContext.SteamHTMLSurface()->MouseMove( m_unBrowserHandle, x, y );
  125. }
  126. protected:
  127. virtual void ApplySchemeSettings( IScheme *pScheme );
  128. vgui::Menu *m_pContextMenu;
  129. private:
  130. STEAM_CALLBACK( HTML, BrowserNeedsPaint, HTML_NeedsPaint_t, m_NeedsPaint );
  131. STEAM_CALLBACK( HTML, BrowserStartRequest, HTML_StartRequest_t, m_StartRequest );
  132. STEAM_CALLBACK( HTML, BrowserURLChanged, HTML_URLChanged_t, m_URLChanged );
  133. STEAM_CALLBACK( HTML, BrowserFinishedRequest, HTML_FinishedRequest_t, m_FinishedRequest );
  134. STEAM_CALLBACK( HTML, BrowserOpenNewTab, HTML_OpenLinkInNewTab_t, m_LinkInNewTab );
  135. STEAM_CALLBACK( HTML, BrowserSetHTMLTitle, HTML_ChangedTitle_t, m_ChangeTitle );
  136. STEAM_CALLBACK( HTML, BrowserPopupHTMLWindow, HTML_NewWindow_t, m_NewWindow );
  137. STEAM_CALLBACK( HTML, BrowserFileLoadDialog, HTML_FileOpenDialog_t, m_FileLoadDialog );
  138. STEAM_CALLBACK( HTML, BrowserSearchResults, HTML_SearchResults_t, m_SearchResults );
  139. STEAM_CALLBACK( HTML, BrowserClose, HTML_CloseBrowser_t, m_CloseBrowser );
  140. STEAM_CALLBACK( HTML, BrowserHorizontalScrollBarSizeResponse, HTML_HorizontalScroll_t, m_HorizScroll );
  141. STEAM_CALLBACK( HTML, BrowserVerticalScrollBarSizeResponse, HTML_VerticalScroll_t, m_VertScroll );
  142. STEAM_CALLBACK( HTML, BrowserLinkAtPositionResponse, HTML_LinkAtPosition_t, m_LinkAtPosResp );
  143. STEAM_CALLBACK( HTML, BrowserJSAlert, HTML_JSAlert_t, m_JSAlert );
  144. STEAM_CALLBACK( HTML, BrowserJSConfirm, HTML_JSConfirm_t, m_JSConfirm );
  145. STEAM_CALLBACK( HTML, BrowserCanGoBackandForward, HTML_CanGoBackAndForward_t, m_CanGoBackForward );
  146. STEAM_CALLBACK( HTML, BrowserSetCursor, HTML_SetCursor_t, m_SetCursor );
  147. STEAM_CALLBACK( HTML, BrowserStatusText, HTML_StatusText_t, m_StatusText );
  148. STEAM_CALLBACK( HTML, BrowserShowToolTip, HTML_ShowToolTip_t, m_ShowTooltip );
  149. STEAM_CALLBACK( HTML, BrowserUpdateToolTip, HTML_UpdateToolTip_t, m_UpdateTooltip );
  150. STEAM_CALLBACK( HTML, BrowserHideToolTip, HTML_HideToolTip_t, m_HideTooltip );
  151. void OnBrowserReady( HTML_BrowserReady_t *pBrowserReady, bool bIOFailure );
  152. void PostURL(const char *URL, const char *pchPostData, bool force);
  153. virtual void BrowserResize();
  154. void UpdateSizeAndScrollBars();
  155. MESSAGE_FUNC( OnSliderMoved, "ScrollBarSliderMoved" );
  156. MESSAGE_FUNC_CHARPTR( OnFileSelected, "FileSelected", fullpath );
  157. MESSAGE_FUNC( OnFileSelectionCancelled, "FileSelectionCancelled" );
  158. MESSAGE_FUNC_PTR( OnTextChanged, "TextChanged", panel );
  159. MESSAGE_FUNC_PTR( OnEditNewLine, "TextNewLine", panel );
  160. MESSAGE_FUNC_INT( DismissJSDialog, "DismissJSDialog", result );
  161. vgui::Panel *m_pInteriorPanel;
  162. vgui::ScrollBar *_hbar,*_vbar;
  163. vgui::DHANDLE<vgui::FileOpenDialog> m_hFileOpenDialog;
  164. class CHTMLFindBar : public vgui::EditablePanel
  165. {
  166. DECLARE_CLASS_SIMPLE( CHTMLFindBar, EditablePanel );
  167. public:
  168. CHTMLFindBar( HTML *parent );
  169. void SetText( const char *pchText ) { m_pFindBar->SetText( pchText ); }
  170. void GetText( char *pText, int ccText ) { m_pFindBar->GetText( pText, ccText ); }
  171. void OnCommand( const char *pchCmd );
  172. void ShowCountLabel() { m_pFindCountLabel->SetVisible( true ); }
  173. void HideCountLabel() { m_pFindCountLabel->SetVisible( false ); }
  174. void SetHidden( bool bState ) { m_bHidden = bState; }
  175. bool BIsHidden() { return m_bHidden; }
  176. private:
  177. vgui::TextEntry *m_pFindBar;
  178. vgui::HTML *m_pParent;
  179. vgui::Label *m_pFindCountLabel;
  180. bool m_bHidden;
  181. };
  182. CHTMLFindBar *m_pFindBar;
  183. int m_iMouseX,m_iMouseY; // where the mouse is on the control
  184. int m_iScrollBorderX,m_iScrollBorderY;
  185. int m_iWideLastHTMLSize, m_iTalLastHTMLSize;
  186. int m_iCopyLinkMenuItemID;
  187. bool m_bScrollBarEnabled;
  188. bool m_bContextMenuEnabled;
  189. int m_iScrollbarSize;
  190. bool m_bNewWindowsOnly;
  191. int m_nViewSourceAllowedIndex;
  192. CUtlString m_sDragURL;
  193. int m_iDragStartX, m_iDragStartY;
  194. struct CustomURLHandler_t
  195. {
  196. PHandle hPanel;
  197. char url[32];
  198. };
  199. CUtlVector<CustomURLHandler_t> m_CustomURLHandlers;
  200. int m_iHTMLTextureID; // vgui texture id
  201. // Track the texture width and height requested so we can tell
  202. // when the size has changed and reallocate the texture.
  203. int m_allocedTextureWidth;
  204. int m_allocedTextureHeight;
  205. bool m_bNeedsFullTextureUpload;
  206. CUtlString m_sCurrentURL; // the url of our current page
  207. // find in page state
  208. bool m_bInFind;
  209. CUtlString m_sLastSearchString;
  210. bool m_bCanGoBack; // cache of forward and back state
  211. bool m_bCanGoForward;
  212. struct LinkAtPos_t
  213. {
  214. LinkAtPos_t() { m_nX = m_nY = 0; }
  215. uint32 m_nX;
  216. uint32 m_nY;
  217. CUtlString m_sURL;
  218. };
  219. LinkAtPos_t m_LinkAtPos; // cache for link at pos requests, because the request is async
  220. bool m_bRequestingDragURL; // true if we need a response for a drag url loc
  221. bool m_bRequestingCopyLink; // true if we wanted to copy the link under the cursor
  222. struct ScrollData_t
  223. {
  224. ScrollData_t()
  225. {
  226. m_bVisible = false;
  227. m_nMax = m_nScroll = 0;
  228. }
  229. bool operator==( ScrollData_t const &src ) const
  230. {
  231. return m_bVisible == src.m_bVisible &&
  232. m_nMax == src.m_nMax &&
  233. m_nScroll == src.m_nScroll;
  234. }
  235. bool operator!=( ScrollData_t const &src ) const
  236. {
  237. return !operator==(src);
  238. }
  239. bool m_bVisible; // is the scroll bar visible
  240. int m_nMax; // most amount of pixels we can scroll
  241. int m_nScroll; // currently scrolled amount of pixels
  242. float m_flZoom; // zoom level this scroll bar is for
  243. };
  244. ScrollData_t m_scrollHorizontal; // details of horizontal scroll bar
  245. ScrollData_t m_scrollVertical; // details of vertical scroll bar
  246. float m_flZoom; // current page zoom level
  247. CUtlString m_sPendingURLLoad; // cache of url to load if we get a PostURL before the cef object is mage
  248. CUtlString m_sPendingPostData; // cache of the post data for above
  249. struct CustomCursorCache_t
  250. {
  251. CustomCursorCache_t() {}
  252. CustomCursorCache_t( const void *pchData ) { m_pchData = pchData; }
  253. float m_CacheTime; // the time we cached the cursor
  254. CursorCode m_Cursor; // the vgui handle to it
  255. const void *m_pchData; // the pointer to the cursor char data so we can detect the same cursor being used
  256. bool operator==(const CustomCursorCache_t& rhs) const
  257. {
  258. return m_pchData == rhs.m_pchData ;
  259. }
  260. };
  261. CUtlVector<CustomCursorCache_t> m_vecHCursor;
  262. CSteamAPIContext m_SteamAPIContext;
  263. HHTMLBrowser m_unBrowserHandle;
  264. CCallResult< HTML, HTML_BrowserReady_t > m_SteamCallResultBrowserReady;
  265. };
  266. } // namespace vgui
  267. #endif // HTML_H