forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuItem.cpp
647 lines (565 loc) · 19.4 KB
/
MenuItem.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include <vgui/IScheme.h>
#include <vgui/IVGui.h>
#include "vgui/ISurface.h"
#include <KeyValues.h>
#include <vgui_controls/Controls.h>
#include <vgui_controls/Menu.h>
#include <vgui_controls/MenuItem.h>
#include <vgui_controls/TextImage.h>
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
using namespace vgui;
//-----------------------------------------------------------------------------
// Purpose: Check box image
//-----------------------------------------------------------------------------
class MenuItemCheckImage : public TextImage
{
public:
MenuItemCheckImage(MenuItem *item) : TextImage( "g" )
{
_menuItem = item;
SetSize(20, 13);
}
virtual void Paint()
{
DrawSetTextFont(GetFont());
// draw background
DrawSetTextColor(_menuItem->GetBgColor());
DrawPrintChar(0, 0, 'g');
// draw check
if (_menuItem->IsChecked())
{
if (_menuItem->IsEnabled())
{
DrawSetTextColor(_menuItem->GetButtonFgColor());
DrawPrintChar(0, 2, 'a');
}
else if (!_menuItem->IsEnabled())
{
// draw disabled version, with embossed look
// offset image
DrawSetTextColor(_menuItem->GetDisabledFgColor1());
DrawPrintChar(1, 3, 'a');
// overlayed image
DrawSetTextColor(_menuItem->GetDisabledFgColor2());
DrawPrintChar(0, 2, 'a');
}
}
}
private:
MenuItem *_menuItem;
};
DECLARE_BUILD_FACTORY_DEFAULT_TEXT( MenuItem, MenuItem );
//-----------------------------------------------------------------------------
// Purpose: Constructor
// Input: parent - the parent of this menu item, usually a menu
// text - the name of the menu item as it appears in the menu
// cascadeMenu - if this item triggers the opening of a cascading menu
// provide a pointer to it.
// MenuItems cannot be both checkable and trigger a cascade menu.
//-----------------------------------------------------------------------------
MenuItem::MenuItem(Menu *parent, const char *panelName, const char *text, Menu *cascadeMenu, bool checkable) : Button(parent, panelName, text)
{
m_pCascadeMenu = cascadeMenu;
m_bCheckable = checkable;
SetButtonActivationType(ACTIVATE_ONRELEASED);
m_pUserData = NULL;
m_pCurrentKeyBinding = NULL;
// only one arg should be passed in.
Assert (!(cascadeMenu && checkable));
Init();
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
// Input: parent - the parent of this menu item, usually a menu
// text - the name of the menu item as it appears in the menu
// cascadeMenu - if this item triggers the opening of a cascading menu
// provide a pointer to it.
// MenuItems cannot be both checkable and trigger a cascade menu.
//-----------------------------------------------------------------------------
MenuItem::MenuItem(Menu *parent, const char *panelName, const wchar_t *wszText, Menu *cascadeMenu, bool checkable) : Button(parent, panelName, wszText)
{
m_pCascadeMenu = cascadeMenu;
m_bCheckable = checkable;
SetButtonActivationType(ACTIVATE_ONRELEASED);
m_pUserData = NULL;
m_pCurrentKeyBinding = NULL;
// only one arg should be passed in.
Assert (!(cascadeMenu && checkable));
Init();
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
MenuItem::~MenuItem()
{
delete m_pCascadeMenu;
delete m_pCascadeArrow;
delete m_pCheck;
if (m_pUserData)
{
m_pUserData->deleteThis();
}
delete m_pCurrentKeyBinding;
}
//-----------------------------------------------------------------------------
// Purpose: Basic initializer
//-----------------------------------------------------------------------------
void MenuItem::Init( void )
{
m_pCascadeArrow = NULL;
m_pCheck = NULL;
if (m_pCascadeMenu)
{
m_pCascadeMenu->SetParent(this);
m_pCascadeArrow = new TextImage("4"); // this makes a right pointing arrow.
m_pCascadeMenu->AddActionSignalTarget(this);
}
else if (m_bCheckable)
{
// move the text image over so we have room for the check
SetTextImageIndex(1);
m_pCheck = new MenuItemCheckImage(this);
SetImageAtIndex(0, m_pCheck, CHECK_INSET);
SetChecked(false);
}
SetButtonBorderEnabled( false );
SetUseCaptureMouse( false );
SetContentAlignment( Label::a_west );
// note menus handle all the sizing of menuItem panels
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
Menu *MenuItem::GetParentMenu()
{
return (Menu *)GetParent();
}
//-----------------------------------------------------------------------------
// Purpose: Layout the Textimage and the Arrow part of the menuItem
//-----------------------------------------------------------------------------
void MenuItem::PerformLayout()
{
Button::PerformLayout();
// make the arrow image match the button layout.
// this will make it brighten and dim like the menu buttons.
if (m_pCascadeArrow)
{
m_pCascadeArrow->SetColor(GetButtonFgColor());
}
}
//-----------------------------------------------------------------------------
// Purpose: Close the cascading menu if we have one.
//-----------------------------------------------------------------------------
void MenuItem::CloseCascadeMenu()
{
if (m_pCascadeMenu)
{
if (m_pCascadeMenu->IsVisible())
{
m_pCascadeMenu->SetVisible(false);
}
// disarm even if menu wasn't visible!
SetArmed(false);
}
}
//-----------------------------------------------------------------------------
// Purpose: Handle cursor moving in a menuItem.
//-----------------------------------------------------------------------------
void MenuItem::OnCursorMoved(int x, int y)
{
// if menu is in keymode and we moved the mouse
// highlight this item
if (GetParentMenu()->GetMenuMode() == Menu::KEYBOARD)
{
OnCursorEntered();
}
// chain up to parent
CallParentFunction(new KeyValues("OnCursorMoved", "x", x, "y", y));
}
//-----------------------------------------------------------------------------
// Purpose: Handle mouse cursor entering a menuItem.
//-----------------------------------------------------------------------------
void MenuItem::OnCursorEntered()
{
// post a message to the parent menu.
// forward the message on to the parent of this menu.
KeyValues *msg = new KeyValues ("CursorEnteredMenuItem");
// tell the parent this menuitem is the one that was entered so it can highlight it
msg->SetInt("VPanel", GetVPanel());
ivgui()->PostMessage(GetVParent(), msg, NULL);
}
//-----------------------------------------------------------------------------
// Purpose: Handle mouse cursor exiting a menuItem.
//-----------------------------------------------------------------------------
void MenuItem::OnCursorExited()
{
// post a message to the parent menu.
// forward the message on to the parent of this menu.
KeyValues *msg = new KeyValues ("CursorExitedMenuItem");
// tell the parent this menuitem is the one that was entered so it can unhighlight it
msg->SetInt("VPanel", GetVPanel());
ivgui()->PostMessage(GetVParent(), msg, NULL);
}
//-----------------------------------------------------------------------------
// Purpose: Handle mouse cursor exiting a menuItem.
//-----------------------------------------------------------------------------
void MenuItem::OnKeyCodeReleased(KeyCode code)
{
if (GetParentMenu()->GetMenuMode() == Menu::KEYBOARD && m_pCascadeMenu)
{
return;
}
// only disarm if we are not opening a cascading menu using keys.
Button::OnKeyCodeReleased(code);
}
//-----------------------------------------------------------------------------
// Purpose: Highlight a menu item
// Menu item buttons highlight if disabled, but won't activate.
//-----------------------------------------------------------------------------
void MenuItem::ArmItem()
{
// close all other menus
GetParentMenu()->CloseOtherMenus(this);
// arm the menuItem.
Button::SetArmed(true);
// When you have a submenu with no scroll bar the menu
// border will not be drawn correctly. This fixes it.
Menu *parent = GetParentMenu();
if ( parent )
{
parent->ForceCalculateWidth();
}
Repaint();
}
//-----------------------------------------------------------------------------
// Purpose: Unhighlight a menu item
//-----------------------------------------------------------------------------
void MenuItem::DisarmItem()
{
// normal behaviour is that the button becomes unarmed
// do not unarm if there is a cascading menu. CloseCascadeMenu handles this.
// and the menu handles it since we close at different times depending
// on whether menu is handling mouse or key events.
if (!m_pCascadeMenu)
{
Button::OnCursorExited();
}
// When you have a submenu with no scroll bar the menu
// border will not be drawn correctly. This fixes it.
Menu *parent = GetParentMenu();
if ( parent )
{
parent->ForceCalculateWidth();
}
Repaint();
}
bool MenuItem::IsItemArmed()
{
return Button::IsArmed();
}
//-----------------------------------------------------------------------------
// Purpose: Pass kill focus events up to parent, This will tell all panels
// in the hierarchy to hide themselves, and enables cascading menus to
// all disappear on selecting an item at the end of the tree.
//-----------------------------------------------------------------------------
void MenuItem::OnKillFocus()
{
GetParentMenu()->OnKillFocus();
}
//-----------------------------------------------------------------------------
// Purpose: fire the menu item as if it has been selected and
// Tell the owner that it is closing
//-----------------------------------------------------------------------------
void MenuItem::FireActionSignal()
{
// cascading menus items don't trigger the parent menu to disappear
// (they trigger the cascading menu to open/close when cursor is moved over/off them)
if (!m_pCascadeMenu)
{
KeyValues *kv = new KeyValues("MenuItemSelected");
kv->SetPtr("panel", this);
ivgui()->PostMessage(GetVParent(), kv, GetVPanel());
// ivgui()->PostMessage(GetVParent(), new KeyValues("MenuItemSelected"), GetVPanel());
Button::FireActionSignal();
// toggle the check next to the item if it is checkable
if (m_bCheckable)
{
SetChecked( !m_bChecked );
}
}
else
{
// if we are in keyboard mode, open the child menu.
if (GetParentMenu()->GetMenuMode() == Menu::KEYBOARD)
{
OpenCascadeMenu();
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Opens the cascading menu.
//-----------------------------------------------------------------------------
void MenuItem::OpenCascadeMenu()
{
if (m_pCascadeMenu)
{
// perform layout on menu, this way it will open in the right spot
// if the window's been moved
m_pCascadeMenu->PerformLayout();
m_pCascadeMenu->SetVisible(true);
ArmItem();
}
}
//-----------------------------------------------------------------------------
// Purpse: Return true if this item triggers a cascading menu
//-----------------------------------------------------------------------------
bool MenuItem::HasMenu()
{
return (m_pCascadeMenu != NULL);
}
//-----------------------------------------------------------------------------
// Purpose: Apply the resource scheme to the menu.
//-----------------------------------------------------------------------------
void MenuItem::ApplySchemeSettings(IScheme *pScheme)
{
// chain back first
Button::ApplySchemeSettings(pScheme);
// get color settings
SetDefaultColor(GetSchemeColor("Menu.TextColor", GetFgColor(), pScheme), GetSchemeColor("Menu.BgColor", GetBgColor(), pScheme));
SetArmedColor(GetSchemeColor("Menu.ArmedTextColor", GetFgColor(), pScheme), GetSchemeColor("Menu.ArmedBgColor", GetBgColor(), pScheme));
SetDepressedColor(GetSchemeColor("Menu.ArmedTextColor", GetFgColor(), pScheme), GetSchemeColor("Menu.ArmedBgColor", GetBgColor(), pScheme));
SetTextInset(atoi(pScheme->GetResourceString("Menu.TextInset")), 0);
// reload images since applyschemesettings in label wipes them out.
if ( m_pCascadeArrow )
{
m_pCascadeArrow->SetFont(pScheme->GetFont("Marlett", IsProportional() ));
m_pCascadeArrow->ResizeImageToContent();
AddImage(m_pCascadeArrow, 0);
}
else if (m_bCheckable)
{
( static_cast<MenuItemCheckImage *>(m_pCheck) )->SetFont( pScheme->GetFont("Marlett", IsProportional()));
SetImageAtIndex(0, m_pCheck, CHECK_INSET);
( static_cast<MenuItemCheckImage *>(m_pCheck) )->ResizeImageToContent();
}
if ( m_pCurrentKeyBinding )
{
m_pCurrentKeyBinding->SetFont(pScheme->GetFont("Default", IsProportional() ));
m_pCurrentKeyBinding->ResizeImageToContent();
}
// Have the menu redo the layout
// Get the parent to resize
Menu * parent = GetParentMenu();
if ( parent )
{
parent->ForceCalculateWidth();
}
}
//-----------------------------------------------------------------------------
// Purpose: Return the size of the text portion of the label.
// for normal menu items this is the same as the label size, but for
// cascading menus it gives you the size of the text portion only, without
// the arrow.
//-----------------------------------------------------------------------------
void MenuItem::GetTextImageSize(int &wide, int &tall)
{
GetTextImage()->GetSize(wide, tall);
}
//-----------------------------------------------------------------------------
// Purpose: Set the size of the text portion of the label.
// For normal menu items this is the same as the label size, but for
// cascading menus it sizes textImage portion only, without
// the arrow.
//-----------------------------------------------------------------------------
void MenuItem::SetTextImageSize(int wide, int tall)
{
GetTextImage()->SetSize(wide, tall);
}
//-----------------------------------------------------------------------------
// Purpose: Return the size of the arrow portion of the label.
// If the menuItem is not a cascading menu, 0 is returned.
//-----------------------------------------------------------------------------
void MenuItem::GetArrowImageSize(int &wide, int &tall)
{
wide = 0, tall = 0;
if (m_pCascadeArrow)
{
m_pCascadeArrow->GetSize(wide, tall);
return;
}
}
//-----------------------------------------------------------------------------
// Purpose: Return the size of the check portion of the label.
//-----------------------------------------------------------------------------
void MenuItem::GetCheckImageSize(int &wide, int &tall)
{
wide = 0, tall = 0;
if (m_pCheck)
{
// resize the image to the contents size
( static_cast<MenuItemCheckImage *>(m_pCheck) )->ResizeImageToContent();
m_pCheck->GetSize(wide, tall);
// include the inset for the check, since nobody but us know about the inset
wide += CHECK_INSET;
return;
}
}
//-----------------------------------------------------------------------------
// Purpose: Return a the menu that this menuItem contains
// This is useful when the parent menu's commands must be
// sent through all menus that are open as well (like hotkeys)
//-----------------------------------------------------------------------------
Menu *MenuItem::GetMenu()
{
return m_pCascadeMenu;
}
//-----------------------------------------------------------------------------
// Purpose: Get the border style for the button. Menu items have no border so
// return null.
//-----------------------------------------------------------------------------
IBorder *MenuItem::GetBorder(bool depressed, bool armed, bool selected, bool keyfocus)
{
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Set the menu to key mode if a child menu goes into keymode
//-----------------------------------------------------------------------------
void MenuItem::OnKeyModeSet()
{
// send the message to this parent in case this is a cascading menu
ivgui()->PostMessage(GetVParent(), new KeyValues("KeyModeSet"), GetVPanel());
}
//-----------------------------------------------------------------------------
// Purpose: Return if this menuitem is checkable or not
// This is used by menus to perform the layout properly.
//-----------------------------------------------------------------------------
bool MenuItem::IsCheckable()
{
return m_bCheckable;
}
//-----------------------------------------------------------------------------
// Purpose: Return if this menuitem is checked or not
//-----------------------------------------------------------------------------
bool MenuItem::IsChecked()
{
return m_bChecked;
}
//-----------------------------------------------------------------------------
// Purpose: Set the checked state of a checkable menuitem
// Does nothing if item is not checkable
//-----------------------------------------------------------------------------
void MenuItem::SetChecked(bool state)
{
if (m_bCheckable)
{
m_bChecked = state;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool MenuItem::CanBeDefaultButton(void)
{
return false;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
KeyValues *MenuItem::GetUserData()
{
if ( HasMenu() )
{
return m_pCascadeMenu->GetItemUserData( m_pCascadeMenu->GetActiveItem() );
}
else
{
return m_pUserData;
}
}
//-----------------------------------------------------------------------------
// Purpose: sets the user data
//-----------------------------------------------------------------------------
void MenuItem::SetUserData(const KeyValues *kv)
{
if (m_pUserData)
{
m_pUserData->deleteThis();
m_pUserData = NULL;
}
if ( kv )
{
m_pUserData = kv->MakeCopy();
}
}
//-----------------------------------------------------------------------------
// Purpose: Passing in NULL removes this object
// Input : *keyName -
//-----------------------------------------------------------------------------
void MenuItem::SetCurrentKeyBinding( char const *keyName )
{
if ( !keyName )
{
delete m_pCurrentKeyBinding;
m_pCurrentKeyBinding = NULL;
return;
}
if ( !m_pCurrentKeyBinding )
{
m_pCurrentKeyBinding = new TextImage( keyName );
}
else
{
char curtext[ 256 ];
m_pCurrentKeyBinding->GetText( curtext, sizeof( curtext ) );
if ( !Q_strcmp( curtext, keyName ) )
return;
m_pCurrentKeyBinding->SetText( keyName );
}
InvalidateLayout( false, true );
}
#define KEYBINDING_INSET 5
void MenuItem::Paint()
{
BaseClass::Paint();
if ( !m_pCurrentKeyBinding )
return;
int w, h;
GetSize( w, h );
int iw, ih;
m_pCurrentKeyBinding->GetSize( iw, ih );
int x = w - iw - KEYBINDING_INSET;
int y = ( h - ih ) / 2;
if ( IsEnabled() )
{
m_pCurrentKeyBinding->SetPos( x, y );
m_pCurrentKeyBinding->SetColor( GetButtonFgColor() );
m_pCurrentKeyBinding->Paint();
}
else
{
m_pCurrentKeyBinding->SetPos( x + 1 , y + 1 );
m_pCurrentKeyBinding->SetColor( GetDisabledFgColor1() );
m_pCurrentKeyBinding->Paint();
surface()->DrawFlushText();
m_pCurrentKeyBinding->SetPos( x, y );
m_pCurrentKeyBinding->SetColor( GetDisabledFgColor2() );
m_pCurrentKeyBinding->Paint();
}
}
void MenuItem::GetContentSize( int& cw, int &ch )
{
BaseClass::GetContentSize( cw, ch );
if ( !m_pCurrentKeyBinding )
return;
int iw, ih;
m_pCurrentKeyBinding->GetSize( iw, ih );
cw += iw + KEYBINDING_INSET;
ch = max( ch, ih );
}