forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuBar.cpp
252 lines (221 loc) · 6.68 KB
/
MenuBar.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include <vgui/IInput.h>
#include <vgui/IPanel.h>
#include <vgui/IScheme.h>
#include <vgui/IBorder.h>
#include <vgui/ISurface.h>
#include <vgui/KeyCode.h>
#include <KeyValues.h>
#include <vgui_controls/MenuBar.h>
#include <vgui_controls/MenuButton.h>
#include <vgui_controls/Label.h>
#include <vgui_controls/Controls.h>
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
using namespace vgui;
enum
{
MENUBARINDENT = 4, // indent from top and bottom of panel.
};
DECLARE_BUILD_FACTORY( MenuBar );
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
MenuBar::MenuBar(Panel *parent, const char *panelName) :
Panel(parent, panelName),
m_nRightEdge( 0 )
{
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
MenuBar::~MenuBar()
{
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void MenuBar::AddButton(MenuButton *button)
{
button->SetParent(this);
button->AddActionSignalTarget(this);
m_pMenuButtons.AddToTail(button);
}
//-----------------------------------------------------------------------------
// This will add the menu to the menu bar
//-----------------------------------------------------------------------------
void MenuBar::AddMenu( const char *pButtonName, Menu *pMenu )
{
MenuButton *pMenuButton = new MenuButton(this, pButtonName, pButtonName);
pMenuButton->SetMenu(pMenu);
AddButton(pMenuButton);
}
//-----------------------------------------------------------------------------
// Purpose: Handle key presses, Activate shortcuts
//-----------------------------------------------------------------------------
void MenuBar::OnKeyCodeTyped(KeyCode code)
{
switch(code)
{
case KEY_RIGHT:
{
// iterate the menu items looking for one that is open
// if we find one open, open the one to the right
for (int i = 0; i < m_pMenuButtons.Count() - 1; i++)
{
MenuButton *panel = m_pMenuButtons[i];
if (panel->IsDepressed())
{
m_pMenuButtons[i]->DoClick();
m_pMenuButtons[i+1]->DoClick();
break;
}
}
break;
}
case KEY_LEFT:
{
// iterate the menu items looking for one that is open
// if we find one open, open the one to the left
for (int i = 1; i < m_pMenuButtons.Count(); i++)
{
MenuButton *panel = m_pMenuButtons[i];
if (panel->IsDepressed())
{
m_pMenuButtons[i]->DoClick();
m_pMenuButtons[i-1]->DoClick();
break;
}
}
break;
}
default:
{
break;
}
}
// don't chain back
}
//-----------------------------------------------------------------------------
// Purpose: Handle key presses, Activate shortcuts
// Input : code -
//-----------------------------------------------------------------------------
void MenuBar::OnKeyTyped(wchar_t unichar)
{
if (unichar)
{
// iterate the menu items looking for one with the matching hotkey
for (int i = 0; i < m_pMenuButtons.Count(); i++)
{
MenuButton *panel = m_pMenuButtons[i];
if (panel->IsVisible())
{
Panel *hot = panel->HasHotkey(unichar);
if (hot)
{
// post a message to the menuitem telling it it's hotkey was pressed
PostMessage(hot, new KeyValues("Hotkey"));
return;
}
}
}
}
// don't chain back
}
void MenuBar::Paint()
{
IScheme *pScheme = scheme()->GetIScheme( GetScheme() );
for ( int i = 0; i < m_pMenuButtons.Count(); i++)
{
if (!m_pMenuButtons[i]->IsArmed())
m_pMenuButtons[i]->SetDefaultBorder(NULL);
else
{
m_pMenuButtons[i]->SetDefaultBorder(pScheme->GetBorder( "ButtonBorder"));
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Message map
//-----------------------------------------------------------------------------
void MenuBar::ApplySchemeSettings(IScheme *pScheme)
{
BaseClass::ApplySchemeSettings(pScheme);
// get the borders we need
SetBorder(pScheme->GetBorder("ButtonBorder"));
// get the background color
SetBgColor(pScheme->GetColor( "MenuBar.BgColor", GetBgColor() ));
}
//-----------------------------------------------------------------------------
// Purpose: Reformat according to the new layout
//-----------------------------------------------------------------------------
void MenuBar::PerformLayout()
{
int nBarWidth, nBarHeight;
GetSize( nBarWidth, nBarHeight );
// Now position + resize all buttons
int x = MENUBARINDENT;
for ( int i = 0; i < m_pMenuButtons.Count(); ++i )
{
int nWide, nTall;
m_pMenuButtons[i]->GetContentSize(nWide, nTall);
m_pMenuButtons[i]->SetPos( x, MENUBARINDENT );
m_pMenuButtons[i]->SetSize( nWide + Label::Content, nBarHeight - 2 * MENUBARINDENT );
x += nWide + MENUBARINDENT;
}
m_nRightEdge = x;
}
//-----------------------------------------------------------------------------
// Purpose: Get the size of the menus in the bar (so other children can be added to menu bar)
// Input : w -
// int&h -
//-----------------------------------------------------------------------------
void MenuBar::GetContentSize( int& w, int&h )
{
w = m_nRightEdge + 2;
h = GetTall();
}
//-----------------------------------------------------------------------------
// Purpose: Message map
//-----------------------------------------------------------------------------
void MenuBar::OnMenuClose()
{
RequestFocus();
}
//-----------------------------------------------------------------------------
// Purpose: Message map
//-----------------------------------------------------------------------------
void MenuBar::OnCursorEnteredMenuButton(int VPanel)
{
VPANEL menuButton = (VPANEL)VPanel;
// see if we had a menu open
for ( int i = 0; i < m_pMenuButtons.Count(); i++)
{
// one of our buttons was pressed.
if (m_pMenuButtons[i]->IsDepressed())
{
int oldbutton = i;
// now see if menuButton is one of ours.
for ( int j = 0; j < m_pMenuButtons.Count(); j++)
{
MenuButton *button = static_cast<MenuButton *>(ipanel()->GetPanel(menuButton, GetModuleName()));
// it is one of ours.
if ( button == m_pMenuButtons[j])
{
// if its a different button than the one we already had open,
if (j != oldbutton)
{
// close this menu and open the one we just entered
m_pMenuButtons[oldbutton]->DoClick();
button->DoClick();
}
}
}
}
}
}