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.

232 lines
8.7 KiB

  1. //=========== Copyright Valve Corporation, All rights reserved. ===============//
  2. //
  3. // Purpose:
  4. //=============================================================================//
  5. #ifndef PANORAMA_LABEL_H
  6. #define PANORAMA_LABEL_H
  7. #ifdef _WIN32
  8. #pragma once
  9. #endif
  10. #include "panel2d.h"
  11. #include "panorama/localization/ilocalize.h"
  12. #include "panorama/text/iuitextlayout.h"
  13. namespace panorama
  14. {
  15. DECLARE_PANEL_EVENT0( CopySelectedLabelText );
  16. //-----------------------------------------------------------------------------
  17. // Purpose: Label
  18. //-----------------------------------------------------------------------------
  19. class CLabel : public CPanel2D, public ILocalizationStringSizeResolver
  20. {
  21. DECLARE_PANEL2D( CLabel, CPanel2D );
  22. public:
  23. CLabel( CPanel2D *parent, const char * pchPanelID );
  24. virtual ~CLabel();
  25. virtual void Paint();
  26. virtual bool BSetProperties( const CUtlVector< ParsedPanelProperty_t > &vecProperties );
  27. virtual void SetupJavascriptObjectTemplate() OVERRIDE;
  28. enum ETextType
  29. {
  30. k_ETextTypePlain,
  31. k_ETextTypeUnlocalized,
  32. k_ETextTypeHTML
  33. };
  34. virtual void SetText( const char *pchValue, ETextType eTextType = k_ETextTypePlain );
  35. virtual void AppendText( const char *pchValue, ETextType eTextType = k_ETextTypePlain );
  36. virtual const char *PchGetText() const { return m_pLocText ? m_pLocText->String() : ""; }
  37. bool OnLocalizationChanged( const CPanelPtr< IUIPanel > &pPanel, const ILocalizationString *pString ); // can't be virtual as it is an event catcher
  38. void SetMaxChars( EStringTruncationStyle eTruncationStyle, uint32 nMaxChars );
  39. void SetStyleForRange( int iStartIndex, int iEndIndex, CPanoramaSymbol symStyle );
  40. virtual bool BRequiresContentClipLayer() { return m_bMayDrawOutsideBounds; }
  41. virtual bool BAcceptsFocus() { return false; }
  42. virtual bool GetContextUIBounds( float *pflX, float *pflY, float *pflWidth, float *pflHeight ) OVERRIDE;
  43. bool BHasSelection() { return m_nSelectionEndIndex != -1; }
  44. void CopySelectionToClipboard();
  45. bool OnCopySelectedLabelText( const CPanelPtr< IUIPanel > &pPanel );
  46. // for cloning
  47. virtual bool IsClonable() { return AreChildrenClonable(); }
  48. virtual CPanel2D *Clone();
  49. // Get the count of anchor tags in this label, will be zero if not HTML
  50. uint32 GetHREFCount();
  51. // Get vector of all url targets in the label
  52. void GetHREFTargets( CUtlVector< CUtlString > &vecTargets );
  53. // enable or disable selection of text via mouse clicks
  54. void SetAllowTextSelection( bool bAllow ) { m_bAllowTextSelection = bAllow; }
  55. #ifdef DBGFLAG_VALIDATE
  56. virtual void ValidateClientPanel( CValidator &validator, const tchar *pchName ) OVERRIDE;
  57. void ValidateLinkVector( CValidator &validator, CUtlVector< CUtlString > *pvecLinks );
  58. #endif
  59. // Allow us to recalc HTML style flags on scale factor changes
  60. virtual void OnUIScaleFactorChanged( float flScaleFactor ) OVERRIDE;
  61. virtual void GetDebugPropertyInfo( CUtlVector< DebugPropertyOutput_t *> *pvecProperties ) OVERRIDE;
  62. enum EHTMLFormatFlag
  63. {
  64. k_EHTMLFormatTagNone = 0,
  65. k_EHTMLFormatTagAnchor = 1,
  66. k_EHTMLFormatTagBold = 1 << 1,
  67. k_EHTMLFormatTagItalics = 1 << 2,
  68. k_EHTMLFormatTagEmphasized = 1 << 3,
  69. k_EHTMLFormatTagStrong = 1 << 4,
  70. k_EHTMLFormatTagSpan = 1 << 5,
  71. k_EHTMLFormatTagHeader1 = 1 << 6,
  72. k_EHTMLFormatTagHeader2 = 1 << 7,
  73. k_EHTMLFormatTagFont = 1 << 8,
  74. k_EHTMLFormatTagPre = 1 << 9,
  75. };
  76. protected:
  77. virtual void OnContentSizeTraverse( float *pflContentWidth, float *pflContentHeight, float flMaxWidth, float flMaxHeight, bool bFinalDimensions );
  78. virtual void OnLayoutTraverse( float flFinalWidth, float flFinalHeight );
  79. virtual void OnStylesChanged();
  80. virtual void OnMouseMove( float flMouseX, float flMouseY );
  81. virtual bool OnMouseButtonDown( const MouseData_t &code );
  82. virtual bool OnMouseButtonUp( const MouseData_t &code );
  83. bool EventStyleFlagsChanged( const CPanelPtr< IUIPanel > &pPanel );
  84. bool OnFindLongestStringForLocVariable( const CPanelPtr< IUIPanel > &pPanel );
  85. virtual void InitClonedPanel( CPanel2D *pPanel );
  86. virtual void SetTextFromJS(const char *pchValue)
  87. {
  88. if( m_bParseAsHTML )
  89. SetText( pchValue, k_ETextTypeHTML );
  90. else
  91. SetText( pchValue, k_ETextTypeUnlocalized );
  92. }
  93. bool BParseAsHTML() const { return m_bParseAsHTML; }
  94. void SetParseAsHTML( bool bParseAsHTML ) { m_bParseAsHTML = bParseAsHTML; }
  95. private:
  96. struct TextRangeFormat_t
  97. {
  98. static const Color k_colorUnspecified;
  99. TextRangeFormat_t()
  100. {
  101. m_iStartChar = -1;
  102. m_iEndChar = -1;
  103. m_unHTMLFormatFlags = 0;
  104. m_iHREF = -1;
  105. m_iMouseOver = -1;
  106. m_iMouseOut = -1;
  107. m_iContextMenu = -1;
  108. m_pStyle = NULL;
  109. m_unStyleFlags = 0;
  110. m_color = k_colorUnspecified;
  111. m_bChildOwner = false;
  112. }
  113. void CopyWithoutRecalcData( const TextRangeFormat_t &rhs );
  114. #ifdef DBGFLAG_VALIDATE
  115. virtual void Validate( CValidator &validator, const tchar *pchName );
  116. #endif
  117. int m_iStartChar;
  118. int m_iEndChar;
  119. uint m_unHTMLFormatFlags;
  120. CUtlVector< CPanoramaSymbol > m_vecClasses;
  121. int m_iHREF; // index into m_vecParsedHREFs if clickable. Multiple ranges could have the same URL, so hold indexes instead of copy strings
  122. int m_iMouseOver; // index into m_vecParsedMouseOvers if clickable. Multiple ranges could have the same URL, so hold indexes instead of copy strings
  123. int m_iMouseOut; // index into m_vecParsedMouseOuts if clickable. Multiple ranges could have the same URL, so hold indexes instead of copy strings
  124. int m_iContextMenu; // index into m_vecParsedContextMenus if clickable. Multiple ranges could have the same URL, so hold indexes instead of copy strings
  125. IUIPanelStyle *m_pStyle;
  126. uint m_unStyleFlags;
  127. Color m_color;
  128. CUtlString m_strChildID;
  129. bool m_bChildOwner;
  130. struct RangeFormatBox_t
  131. {
  132. Vector2D topLeft;
  133. Vector2D bottomRight;
  134. };
  135. CUtlVector< RangeFormatBox_t > m_vecBoundingBoxes;
  136. };
  137. void SetTextInternal( const char *pchValue, ETextType eTextType, bool bTrustedSource );
  138. void SetFromHTMLInternal( const char *pchText, bool bAppend, bool bTrusted );
  139. int ParseStringFromTag( const char *pchTag, const char *pchString, CUtlVector<CUtlString> **ppVecStrings );
  140. int ParseHREFFromTag( const char *pchTag );
  141. int ParseMouseOverFromTag( const char *pchTag );
  142. int ParseMouseOutFromTag( const char *pchTag );
  143. int ParseContextMenuFromTag( const char *pchTag );
  144. void UpdateTextRangeStyles();
  145. TextRangeFormat_t &AppendTextRangeFormat( int iStartChar, int iEndChar, uint unHTMLFormatFlags, const CUtlVector< CPanoramaSymbol > &vecStyles, int iHREF, int iMouseOver, int iMouseOut, int iContextMenu, const Color &color, const char *pszChildID, bool bChildOwner );
  146. void RemoveTextRangeFormats();
  147. bool BCoordsInTextRange( const TextRangeFormat_t &rangeFormat, float flX, float flY );
  148. int FindTextRangeAt( float flX, float flY );
  149. IUITextLayout *CreateTextLayout( float flWidth, float flHeight, bool bUseChildDesiredSize, const char *pchOptStringToUse = NULL );
  150. IUITextLayout *CreateCurrentLayoutTextLayout();
  151. virtual int ResolveStringLengthInPixels( const char *pchString );
  152. bool HandleAnchorTagEvent( CUtlVector< CUtlString > *pvecLinks, int iLinkIndex );
  153. static void CloneLinksVector( CUtlVector< CUtlString > *pSourceLinks, CUtlVector< CUtlString > **ppDestinationLinks );
  154. bool m_bContentSizeDirty;
  155. float m_flMaxWidthLastContentSize;
  156. float m_flMaxHeightLastContentSize;
  157. float m_flLastUIScaleFactor;
  158. bool m_bLeftMouseIsDown;
  159. Vector2D m_LastMousePos;
  160. bool m_bSelectionRectDirty;
  161. int32 m_nSelectionStartIndex;
  162. int32 m_nSelectionEndIndex;
  163. CUtlVector<IUITextLayout::HitTestRegionRect_t> m_vecSelectionRects;
  164. bool m_bMayDrawOutsideBounds;
  165. bool m_bAllowTextSelection;
  166. CMutableLocalizationString m_pLocText;
  167. CMutableLocalizationString m_pLocTextHTML; // the base string with html markup preserved
  168. uint32 m_nMaxChars; // max chars to store in this label
  169. EStringTruncationStyle m_eStringTruncationStyle; // how to truncate our text
  170. bool m_bParseAsHTML; // if true, set text will be parsed as html
  171. CUtlVector< TextRangeFormat_t > m_vecTextRangeFormats; // list of parsed text range formats
  172. CUtlVector< CUtlString > *m_pvecParsedHREFs; // parsed href.. indexes are added to text range formats. Multiple ranges could have same URL.
  173. CUtlVector< CUtlString > *m_pvecParsedMouseOvers; // parsed onmouseover.. indexes are added to text range formats. Multiple ranges could have same URL.
  174. CUtlVector< CUtlString > *m_pvecParsedMouseOuts; // parsed onmouseout.. indexes are added to text range formats. Multiple ranges could have same URL.
  175. CUtlVector< CUtlString > *m_pvecParsedContextMenus; // parsed oncontextmenu.. indexes are added to text range formats. Multiple ranges could have same URL.
  176. TextRangeFormat_t *m_pLastHoverRange; // last text range the mouse was hovering over
  177. TextRangeFormat_t *m_pMouseDownRange; // index into m_vecTextRangeFormats
  178. static uint32 s_unNextInlineImageID;
  179. };
  180. } // namespace panorama
  181. #endif // PANORAMA_LABEL_H