Counter Strike : Global Offensive Source Code
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.

336 lines
12 KiB

  1. //========= Copyright � 1996-2005, 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. #ifndef VERSION_SAFE_STEAM_API_INTERFACES
  19. #define VERSION_SAFE_STEAM_API_INTERFACES
  20. #endif
  21. #include "steam/steam_api.h"
  22. #include <tier1/utlmap.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, HHTMLBrowser unBrowserHandleToDelete = INVALID_HTMLBROWSER );
  38. ~HTML();
  39. // IHTML pass through functions
  40. virtual void OpenURL( const char *URL, const char *pchPostData );
  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 ) {}
  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. #ifdef DBGFLAG_VALIDATE
  113. virtual void Validate( CValidator &validator, const char *pchName )
  114. {
  115. ValidateObj( m_CustomURLHandlers );
  116. BaseClass::Validate( validator, pchName );
  117. }
  118. #endif // DBGFLAG_VALIDATE
  119. void PaintComboBox();
  120. int BrowserGetIndex() { return m_iBrowser; }
  121. protected:
  122. virtual void ApplySchemeSettings( IScheme *pScheme );
  123. friend class HTMLComboBoxHost;
  124. vgui::Menu *m_pContextMenu;
  125. private:
  126. // IHTMLResponses callbacks from the browser engine
  127. ISteamHTMLSurface *SteamHTMLSurface() { return m_SteamAPIContext.SteamHTMLSurface(); }
  128. STEAM_CALLBACK( HTML, BrowserNeedsPaint, HTML_NeedsPaint_t, m_NeedsPaint );
  129. STEAM_CALLBACK( HTML, BrowserStartRequest, HTML_StartRequest_t, m_StartRequest );
  130. STEAM_CALLBACK( HTML, BrowserURLChanged, HTML_URLChanged_t, m_URLChanged );
  131. STEAM_CALLBACK( HTML, BrowserFinishedRequest, HTML_FinishedRequest_t, m_FinishedRequest );
  132. STEAM_CALLBACK( HTML, BrowserHorizontalScrollBarSizeResponse, HTML_HorizontalScroll_t, m_HorizScroll );
  133. STEAM_CALLBACK( HTML, BrowserVerticalScrollBarSizeResponse, HTML_VerticalScroll_t, m_VertScroll );
  134. STEAM_CALLBACK( HTML, BrowserCanGoBackandForward, HTML_CanGoBackAndForward_t, m_CanGoBackForward );
  135. STEAM_CALLBACK( HTML, BrowserSetCursor, HTML_SetCursor_t, m_SetCursor );
  136. STEAM_CALLBACK( HTML, BrowserClose, HTML_CloseBrowser_t, m_Close );
  137. STEAM_CALLBACK( HTML, BrowserFileOpenDialog, HTML_FileOpenDialog_t, m_FileOpenDialog );
  138. STEAM_CALLBACK( HTML, BrowserShowToolTip, HTML_ShowToolTip_t, m_ShowToolTip );
  139. STEAM_CALLBACK( HTML, BrowserUpdateToolTip, HTML_UpdateToolTip_t, m_UpdateToolTip );
  140. STEAM_CALLBACK( HTML, BrowserHideToolTip, HTML_HideToolTip_t, m_HideToolTip );
  141. STEAM_CALLBACK( HTML, BrowserSearchResults, HTML_SearchResults_t, m_SearchResults );
  142. STEAM_CALLBACK( HTML, BrowserLinkAtPositionResponse, HTML_LinkAtPosition_t, m_LinkAtPositionResponse );
  143. STEAM_CALLBACK( HTML, BrowserJSAlert, HTML_JSAlert_t, m_JSAlert );
  144. STEAM_CALLBACK( HTML, BrowserJSConfirm, HTML_JSConfirm_t, m_JSConfirm );
  145. STEAM_CALLBACK( HTML, BrowserPopupHTMLWindow, HTML_NewWindow_t, m_NewWindow );
  146. void OnBrowserReady(HTML_BrowserReady_t *pBrowserReady, bool bIOFailure);
  147. void PostURL( const char *URL, const char *pchPostData );
  148. virtual void BrowserResize();
  149. virtual void CalcScrollBars(int w,int h);
  150. void UpdateSizeAndScrollBars();
  151. MESSAGE_FUNC( OnSliderMoved, "ScrollBarSliderMoved" );
  152. MESSAGE_FUNC_CHARPTR( OnFileSelected, "FileSelected", fullpath );
  153. MESSAGE_FUNC( OnFileSelectionCancelled, "FileSelectionCancelled" );
  154. MESSAGE_FUNC_PTR( OnTextChanged, "TextChanged", panel );
  155. MESSAGE_FUNC_PTR( OnEditNewLine, "TextNewLine", panel );
  156. int ConvertMouseCodeToCEFCode( MouseCode code );
  157. int TranslateKeyModifiers();
  158. vgui::Panel *m_pInteriorPanel;
  159. vgui::ScrollBar *_hbar,*_vbar;
  160. vgui::DHANDLE<vgui::FileOpenDialog> m_hFileOpenDialog;
  161. class CHTMLFindBar : public vgui::EditablePanel
  162. {
  163. DECLARE_CLASS_SIMPLE( CHTMLFindBar, EditablePanel );
  164. public:
  165. CHTMLFindBar( HTML *parent );
  166. void SetText( const char *pchText ) { m_pFindBar->SetText( pchText ); }
  167. void GetText( char *pText, int ccText ) { m_pFindBar->GetText( pText, ccText ); }
  168. void OnCommand( const char *pchCmd );
  169. void ShowCountLabel() { m_pFindCountLabel->SetVisible( true ); }
  170. void HideCountLabel() { m_pFindCountLabel->SetVisible( false ); }
  171. void SetHidden( bool bState ) { m_bHidden = bState; }
  172. bool BIsHidden() { return m_bHidden; }
  173. private:
  174. vgui::TextEntry *m_pFindBar;
  175. vgui::HTML *m_pParent;
  176. vgui::Label *m_pFindCountLabel;
  177. bool m_bHidden;
  178. };
  179. CHTMLFindBar *m_pFindBar;
  180. int m_iMouseX,m_iMouseY; // where the mouse is on the control
  181. int m_iScrollBorderX,m_iScrollBorderY;
  182. int m_iWideLastHTMLSize, m_iTalLastHTMLSize;
  183. int m_iCopyLinkMenuItemID;
  184. bool m_bScrollBarEnabled;
  185. bool m_bContextMenuEnabled;
  186. int m_iScrollbarSize;
  187. bool m_bNewWindowsOnly;
  188. int m_nViewSourceAllowedIndex;
  189. CUtlString m_sDragURL;
  190. int m_iDragStartX, m_iDragStartY;
  191. struct CustomURLHandler_t
  192. {
  193. PHandle hPanel;
  194. char url[32];
  195. };
  196. CUtlVector<CustomURLHandler_t> m_CustomURLHandlers;
  197. int m_iBrowser; // our browser handle
  198. int m_iHTMLTextureID; // vgui texture id
  199. // Track the texture width and height requested so we can tell
  200. // when the size has changed and reallocate the texture.
  201. int m_allocedTextureWidth;
  202. int m_allocedTextureHeight;
  203. int m_iComboBoxTextureID; // vgui texture id of the combo box
  204. int m_allocedComboBoxWidth;
  205. int m_allocedComboBoxHeight;
  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_nX = m_nY = m_nWide = m_nTall = m_nMax = m_nScroll = 0;
  228. }
  229. bool operator==( ScrollData_t const &src ) const
  230. {
  231. return m_bVisible == src.m_bVisible &&
  232. m_nX == src.m_nX &&
  233. m_nY == src.m_nY &&
  234. m_nWide == src.m_nWide &&
  235. m_nTall == src.m_nTall &&
  236. m_nMax == src.m_nMax &&
  237. m_nScroll == src.m_nScroll;
  238. }
  239. bool operator!=( ScrollData_t const &src ) const
  240. {
  241. return !operator==(src);
  242. }
  243. bool m_bVisible; // is the scroll bar visible
  244. int m_nX; /// where cef put the scroll bar
  245. int m_nY;
  246. int m_nWide;
  247. int m_nTall; // how many pixels of scroll in the current scroll knob
  248. int m_nMax; // most amount of pixels we can scroll
  249. int m_nScroll; // currently scrolled amount of pixels
  250. float m_flZoom; // zoom level this scroll bar is for
  251. };
  252. ScrollData_t m_scrollHorizontal; // details of horizontal scroll bar
  253. ScrollData_t m_scrollVertical; // details of vertical scroll bar
  254. float m_flZoom; // current page zoom level
  255. CUtlString m_sPendingURLLoad; // cache of url to load if we get a PostURL before the cef object is mage
  256. CUtlString m_sPendingPostData; // cache of the post data for above
  257. struct CustomCursorCache_t
  258. {
  259. CustomCursorCache_t() {}
  260. CustomCursorCache_t( const void *pchData ) { m_pchData = pchData; }
  261. float m_CacheTime; // the time we cached the cursor
  262. CursorCode m_Cursor; // the vgui handle to it
  263. const void *m_pchData; // the pointer to the cursor char data so we can detect the same cursor being used
  264. bool operator==(const CustomCursorCache_t& rhs) const
  265. {
  266. return m_pchData == rhs.m_pchData ;
  267. }
  268. };
  269. CUtlVector<CustomCursorCache_t> m_vecHCursor;
  270. public:
  271. CSteamAPIContext m_SteamAPIContext;
  272. HHTMLBrowser m_unBrowserHandle;
  273. HHTMLBrowser m_unBrowserHandleToDelete; // Keep track of the browser to delete, cf comments in BrowserPopupHTMLWindow
  274. bool m_bPopupWindow;
  275. CCallResult< HTML, HTML_BrowserReady_t > m_SteamCallResultBrowserReady;
  276. };
  277. } // namespace vgui
  278. #endif // HTML_H