summaryrefslogtreecommitdiff
path: root/gameui/TextEntryBox.cpp
blob: a78b48c196454530437e35eae46091bd77445417 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

// Author: Matthew D. Campbell ([email protected]), 2003

#include <vgui/KeyCode.h>

#include "CvarTextEntry.h"
#include "TextEntryBox.h"
#include <vgui_controls/TextEntry.h>

// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>

#ifndef max
#define max(a,b)            (((a) > (b)) ? (a) : (b))
#endif

using namespace vgui;

//--------------------------------------------------------------------------------------------------------------
CTextEntryBox::CTextEntryBox(const char *title, const char *queryText, const char *entryText, bool isCvar, vgui::Panel *parent) : QueryBox(title, queryText,parent)
{
	if (isCvar)
	{
		m_pEntry = m_pCvarEntry = new CCvarTextEntry( this, "TextEntry", entryText );
	}
	else
	{
		m_pEntry = new TextEntry( this, "TextEntry" );
		m_pCvarEntry = NULL;
	}
	m_pEntry->SetTabPosition(3);
	m_pEntry->RequestFocus();
	m_pEntry->GotoTextEnd();
}

//--------------------------------------------------------------------------------------------------------------
CTextEntryBox::~CTextEntryBox()
{
	delete m_pEntry;
}

//--------------------------------------------------------------------------------------------------------------
void CTextEntryBox::ShowWindow(Frame *pFrameOver)
{
	BaseClass::ShowWindow( pFrameOver );

	m_pEntry->RequestFocus();

	InvalidateLayout();
}

//--------------------------------------------------------------------------------------------------------------
void CTextEntryBox::PerformLayout()
{
	BaseClass::PerformLayout();

	int x, y, wide, tall;
	GetClientArea(x, y, wide, tall);
	wide += x;
	tall += y;

	const int borderW = 10;

	int labelW, labelH;
	int entryW, entryH;
	m_pMessageLabel->GetSize( labelW, labelH );

	entryW = max(120, wide - borderW - borderW - borderW - labelW);
	entryH = max(24, labelH);
	m_pEntry->SetSize( entryW, entryH );

	int boxWidth, boxTall;
	GetSize(boxWidth, boxTall);
	if (boxWidth < labelW + entryW + borderW*3)
		SetSize( labelW + entryW + borderW*3, boxTall );

	m_pMessageLabel->GetPos( x, y );
	m_pMessageLabel->SetPos( borderW, y - (entryH - labelH)/2 );

	m_pEntry->SetPos( borderW + m_pMessageLabel->GetWide() + borderW, y - (entryH - labelH) );
}

//--------------------------------------------------------------------------------------------------------------
void CTextEntryBox::OnCommand(const char *command)
{
	if (!stricmp(command, "Ok"))
	{
		if (m_pCvarEntry)
		{
			m_pCvarEntry->ApplyChanges( true );
		}
	}

	BaseClass::OnCommand(command);
	
}

//--------------------------------------------------------------------------------------------------------------
void CTextEntryBox::OnKeyCodeTyped(KeyCode code)
{
	if (code == KEY_ESCAPE)
	{
		OnCommand("Cancel");
	}
	if (code == KEY_ENTER)
	{
		OnCommand("Ok");
	}
	else
	{
		BaseClass::OnKeyCodeTyped(code);
	}
}