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.

158 lines
4.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <stdio.h>
  8. #include "vgui/ISurface.h"
  9. #include "vgui/ISystem.h"
  10. #include "vgui/MouseCode.h"
  11. #include "vgui/Cursor.h"
  12. #include "KeyValues.h"
  13. #include "vgui_controls/URLLabel.h"
  14. #include "vgui_controls/TextImage.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. using namespace vgui;
  18. vgui::Panel *URLLabel_Factory()
  19. {
  20. return new URLLabel(NULL, NULL, "URLLabel", NULL);
  21. }
  22. DECLARE_BUILD_FACTORY_CUSTOM( URLLabel, URLLabel_Factory );
  23. //-----------------------------------------------------------------------------
  24. // Purpose: constructor
  25. //-----------------------------------------------------------------------------
  26. URLLabel::URLLabel(Panel *parent, const char *panelName, const char *text, const char *pszURL) : Label(parent, panelName, text)
  27. {
  28. m_pszURL = NULL;
  29. m_bUnderline = false;
  30. m_iURLSize = 0;
  31. if (pszURL && strlen(pszURL) > 0)
  32. {
  33. SetURL(pszURL);
  34. }
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Purpose: unicode constructor
  38. //-----------------------------------------------------------------------------
  39. URLLabel::URLLabel(Panel *parent, const char *panelName, const wchar_t *wszText, const char *pszURL) : Label(parent, panelName, wszText)
  40. {
  41. m_pszURL = NULL;
  42. m_bUnderline = false;
  43. m_iURLSize = 0;
  44. if (pszURL && strlen(pszURL) > 0)
  45. {
  46. SetURL(pszURL);
  47. }
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Purpose: destructor
  51. //-----------------------------------------------------------------------------
  52. URLLabel::~URLLabel()
  53. {
  54. if (m_pszURL)
  55. delete [] m_pszURL;
  56. }
  57. //-----------------------------------------------------------------------------
  58. // Purpose: sets the URL
  59. //-----------------------------------------------------------------------------
  60. void URLLabel::SetURL(const char *pszURL)
  61. {
  62. int iNewURLSize = strlen(pszURL);
  63. if (iNewURLSize > m_iURLSize || !m_pszURL)
  64. {
  65. delete [] m_pszURL;
  66. m_pszURL = new char [iNewURLSize + 1];
  67. }
  68. strcpy(m_pszURL, pszURL);
  69. m_iURLSize = iNewURLSize;
  70. }
  71. //-----------------------------------------------------------------------------
  72. // Purpose: If we were left clicked on, launch the URL
  73. //-----------------------------------------------------------------------------
  74. void URLLabel::OnMousePressed(MouseCode code)
  75. {
  76. if (code == MOUSE_LEFT)
  77. {
  78. if (m_pszURL)
  79. {
  80. system()->ShellExecute("open", m_pszURL);
  81. }
  82. }
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose: Applies resouce settings
  86. //-----------------------------------------------------------------------------
  87. void URLLabel::ApplySettings(KeyValues *inResourceData)
  88. {
  89. BaseClass::ApplySettings(inResourceData);
  90. const char *pszURL = inResourceData->GetString("URLText", NULL);
  91. if (pszURL)
  92. {
  93. if (pszURL[0] == '#')
  94. {
  95. // it's a localized url, look it up
  96. const wchar_t *ws = g_pVGuiLocalize->Find(pszURL + 1);
  97. if (ws)
  98. {
  99. char localizedUrl[512];
  100. g_pVGuiLocalize->ConvertUnicodeToANSI(ws, localizedUrl, sizeof(localizedUrl));
  101. SetURL(localizedUrl);
  102. }
  103. }
  104. else
  105. {
  106. SetURL(pszURL);
  107. }
  108. }
  109. }
  110. //-----------------------------------------------------------------------------
  111. // Purpose: saves them to disk
  112. //-----------------------------------------------------------------------------
  113. void URLLabel::GetSettings( KeyValues *outResourceData )
  114. {
  115. BaseClass::GetSettings(outResourceData);
  116. if (m_pszURL)
  117. {
  118. outResourceData->SetString("URLText", m_pszURL);
  119. }
  120. }
  121. //-----------------------------------------------------------------------------
  122. // Purpose: Returns a description of the label string
  123. //-----------------------------------------------------------------------------
  124. const char *URLLabel::GetDescription( void )
  125. {
  126. static char buf[1024];
  127. _snprintf(buf, sizeof(buf), "%s, string URLText", BaseClass::GetDescription());
  128. return buf;
  129. }
  130. //-----------------------------------------------------------------------------
  131. // Purpose: scheme settings
  132. //-----------------------------------------------------------------------------
  133. void URLLabel::ApplySchemeSettings(IScheme *pScheme)
  134. {
  135. // set our font to be underlined by default
  136. // the Label::ApplySchemeSettings() will override it if override set in scheme file
  137. SetFont( pScheme->GetFont( "DefaultUnderline", IsProportional() ) );
  138. BaseClass::ApplySchemeSettings(pScheme);
  139. SetCursor(dc_hand);
  140. }