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