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.

225 lines
6.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef LABEL_H
  8. #define LABEL_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "utlvector.h"
  13. #include "vgui/VGUI.h"
  14. #include "vgui_controls/Panel.h"
  15. #include "vgui_controls/PHandle.h"
  16. namespace vgui
  17. {
  18. //-----------------------------------------------------------------------------
  19. // Purpose: Contains and displays a set of images
  20. // By default starts with one TextImage
  21. //-----------------------------------------------------------------------------
  22. class Label : public Panel
  23. {
  24. DECLARE_CLASS_SIMPLE( Label, Panel );
  25. public:
  26. // Constructors
  27. Label(Panel *parent, const char *panelName, const char *text);
  28. Label(Panel *parent, const char *panelName, const wchar_t *wszText);
  29. ~Label();
  30. public:
  31. // Take the string and looks it up in the localization file to convert it to unicode
  32. virtual void SetText(const char *tokenName);
  33. // Set unicode text directly
  34. virtual void SetText(const wchar_t *unicodeString, bool bClearUnlocalizedSymbol = false );
  35. // Get the current text
  36. virtual void GetText(OUT_Z_BYTECAP(bufferLen) char *textOut, int bufferLen);
  37. virtual void GetText(OUT_Z_BYTECAP(bufLenInBytes) wchar_t *textOut, int bufLenInBytes);
  38. // Content alignment
  39. // Get the size of the content within the label
  40. virtual void GetContentSize(int &wide, int &tall);
  41. // Set how the content aligns itself within the label
  42. // alignment code, used to determine how the images are layed out within the Label
  43. enum Alignment
  44. {
  45. a_northwest = 0,
  46. a_north,
  47. a_northeast,
  48. a_west,
  49. a_center,
  50. a_east,
  51. a_southwest,
  52. a_south,
  53. a_southeast,
  54. };
  55. virtual void SetContentAlignment(Alignment alignment);
  56. virtual void SetEnabled(bool state);
  57. // Additional offset at the Start of the text (from whichever sides it is aligned)
  58. virtual void SetTextInset(int xInset, int yInset);
  59. virtual void GetTextInset(int *xInset, int *yInset );
  60. // Text colors
  61. virtual void SetFgColor(Color color);
  62. virtual Color GetFgColor();
  63. // colors to use when the label is disabled
  64. virtual void SetDisabledFgColor1(Color color);
  65. virtual void SetDisabledFgColor2(Color color);
  66. virtual Color GetDisabledFgColor1();
  67. virtual Color GetDisabledFgColor2();
  68. // Set whether the text is displayed bright or dull
  69. enum EColorState
  70. {
  71. CS_NORMAL,
  72. CS_DULL,
  73. CS_BRIGHT,
  74. };
  75. virtual void SetTextColorState(EColorState state);
  76. // Font
  77. virtual void SetFont(HFont font);
  78. virtual HFont GetFont();
  79. // Hotkey
  80. virtual Panel *HasHotkey(wchar_t key);
  81. virtual void SetHotkey(wchar_t key);
  82. virtual wchar_t GetHotKey();
  83. // Labels can be associated with controls, and alter behaviour based on the associates behaviour
  84. // If the associate is disabled, so are we
  85. // If the associate has focus, we may alter how we draw
  86. // If we get a hotkey press or focus message, we forward the focus to the associate
  87. virtual void SetAssociatedControl(Panel *control);
  88. // Multiple image handling
  89. // Images are drawn from left to right across the label, ordered by index
  90. // By default there is a TextImage in position 0 (see GetTextImage()/SetTextImageIndex())
  91. virtual int AddImage(IImage *image, int preOffset); // Return the index the image was placed in
  92. virtual void SetImageAtIndex(int index, IImage *image, int preOffset);
  93. virtual void SetImagePreOffset(int index, int preOffset); // Set the offset in pixels before the image
  94. virtual IImage *GetImageAtIndex(int index);
  95. virtual int GetImageCount();
  96. virtual void ClearImages();
  97. virtual void ResetToSimpleTextImage();
  98. // fixes the layout bounds of the image within the label
  99. virtual void SetImageBounds(int index, int x, int width);
  100. // Teturns a pointer to the default text image
  101. virtual TextImage *GetTextImage();
  102. // Moves where the default text image is within the image array (it starts in position 0)
  103. // Setting it to -1 removes it from the image list
  104. // Returns the index the default text image was previously in
  105. virtual int SetTextImageIndex(int newIndex);
  106. // Message handling
  107. // outputData - keyName is the name of the attribute requested.
  108. // for Labels "text" is an option that returns the default text image text
  109. // returns true on success in finding the requested value. false on failure.
  110. virtual bool RequestInfo(KeyValues *outputData);
  111. /* INFO HANDLING
  112. "GetText"
  113. returns:
  114. "text" - text contained in the label
  115. */
  116. /* CUSTOM MESSAGE HANDLING
  117. "SetText"
  118. input: "text" - label text is set to be this string
  119. */
  120. virtual void SizeToContents();
  121. // the +8 is padding to the content size
  122. // the code which uses it should really set that itself;
  123. // however a lot of existing code relies on this
  124. enum Padding
  125. {
  126. Content = 8,
  127. };
  128. void SetWrap( bool bWrap );
  129. void SetCenterWrap( bool bWrap );
  130. void SetAllCaps( bool bAllCaps );
  131. protected:
  132. virtual void PerformLayout();
  133. virtual wchar_t CalculateHotkey(const char *text);
  134. virtual wchar_t CalculateHotkey(const wchar_t *text);
  135. virtual void ComputeAlignment(int &tx0, int &ty0, int &tx1, int &ty1);
  136. virtual void Paint();
  137. MESSAGE_FUNC_PARAMS( OnSetText, "SetText", params );
  138. virtual void DrawDashedLine(int x0, int y0, int x1, int y1, int dashLen, int gapLen);
  139. virtual void OnRequestFocus(VPANEL subFocus, VPANEL defaultPanel);
  140. MESSAGE_FUNC( OnHotkeyPressed, "Hotkey" );
  141. virtual void OnMousePressed(MouseCode code);
  142. virtual void OnSizeChanged(int wide, int tall);
  143. // makes sure that the maxIndex will be a valid index
  144. virtual void EnsureImageCapacity(int maxIndex);
  145. // editing
  146. virtual void ApplySchemeSettings(IScheme *pScheme);
  147. virtual void GetSettings( KeyValues *outResourceData );
  148. virtual void ApplySettings( KeyValues *inResourceData );
  149. virtual const char *GetDescription( void );
  150. MESSAGE_FUNC_PARAMS( OnDialogVariablesChanged, "DialogVariables", dialogVariables );
  151. void HandleAutoSizing( void );
  152. private:
  153. void Init();
  154. Alignment _contentAlignment;
  155. TextImage *_textImage; // this is the textImage, if the full text will not
  156. // fit we put as much as we can and add an elipsis (...)
  157. struct TImageInfo
  158. {
  159. IImage *image;
  160. short offset;
  161. short xpos;
  162. short width;
  163. };
  164. CUtlVector<TImageInfo> _imageDar;
  165. int _textInset[2];
  166. Color _disabledFgColor1;
  167. Color _disabledFgColor2;
  168. Color _associateColor;
  169. int _textImageIndex; // index in the image array that the default _textimage resides
  170. EColorState _textColorState;
  171. PHandle _associate;
  172. char *_associateName;
  173. char *_fontOverrideName;
  174. wchar_t _hotkey; // the hotkey contained in the text
  175. bool m_bWrap;
  176. bool m_bCenterWrap;
  177. bool m_bAllCaps;
  178. bool m_bAutoWideToContents;
  179. bool m_bAutoWideDirty;
  180. bool m_bUseProportionalInsets;
  181. };
  182. } // namespace vgui
  183. #endif // LABEL_H