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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>
#include <vgui/IInput.h>
#include <vgui/ILocalize.h>
#include <vgui/IPanel.h>
#include <vgui/ISurface.h>
#include <vgui/IScheme.h>
#include <KeyValues.h>
#include <vgui_controls/Label.h>
#include <vgui_controls/Image.h>
#include <vgui_controls/TextImage.h>
#include <vgui_controls/Controls.h>
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
using namespace vgui;
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
DECLARE_BUILD_FACTORY_DEFAULT_TEXT( Label, Label );
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
Label::Label(Panel *parent, const char *panelName, const char *text) : BaseClass(parent, panelName)
{
Init();
_textImage = new TextImage(text);
_textImage->SetColor(Color(0, 0, 0, 0));
SetText(text);
_textImageIndex = AddImage(_textImage, 0);
REGISTER_COLOR_AS_OVERRIDABLE( _disabledFgColor2, "disabledfgcolor2_override" );
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
Label::Label(Panel *parent, const char *panelName, const wchar_t *wszText) : BaseClass(parent, panelName)
{
Init();
_textImage = new TextImage(wszText);
_textImage->SetColor(Color(0, 0, 0, 0));
SetText(wszText);
_textImageIndex = AddImage(_textImage, 0);
REGISTER_COLOR_AS_OVERRIDABLE( _disabledFgColor2, "disabledfgcolor2_override" );
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
Label::~Label()
{
delete _textImage;
delete [] _associateName;
delete [] _fontOverrideName;
}
//-----------------------------------------------------------------------------
// Purpose: Construct the label
//-----------------------------------------------------------------------------
void Label::Init()
{
_contentAlignment = a_west;
_textColorState = CS_NORMAL;
_textInset[0] = 0;
_textInset[1] = 0;
_hotkey = 0;
_associate = NULL;
_associateName = NULL;
_fontOverrideName = NULL;
m_bWrap = false;
m_bCenterWrap = false;
m_bAutoWideToContents = false;
m_bUseProportionalInsets = false;
m_bAutoWideDirty = false;
// SetPaintBackgroundEnabled(false);
}
//-----------------------------------------------------------------------------
// Purpose: Set whether the text is displayed bright or dull
//-----------------------------------------------------------------------------
void Label::SetTextColorState(EColorState state)
{
if (_textColorState != state)
{
_textColorState = state;
InvalidateLayout();
}
}
//-----------------------------------------------------------------------------
// Purpose: Return the full size of the contained content
//-----------------------------------------------------------------------------
void Label::GetContentSize(int &wide, int &tall)
{
if( GetFont() == INVALID_FONT ) // we haven't loaded our font yet, so load it now
{
IScheme *pScheme = scheme()->GetIScheme( GetScheme() );
if ( pScheme )
{
SetFont( pScheme->GetFont( "Default", IsProportional() ) );
}
}
int tx0, ty0, tx1, ty1;
ComputeAlignment(tx0, ty0, tx1, ty1);
// the +8 is padding to the content size
// the code which uses it should really set that itself;
// however a lot of existing code relies on this
wide = (tx1 - tx0) + _textInset[0];
// get the size of the text image and remove it
int iWide, iTall;
_textImage->GetSize(iWide, iTall);
wide -= iWide;
// get the full, untruncated (no elipsis) size of the text image.
_textImage->GetContentSize(iWide, iTall);
wide += iWide;
// addin the image offsets as well
for (int i=0; i < _imageDar.Size(); i++)
wide += _imageDar[i].offset;
tall = max((ty1 - ty0) + _textInset[1], iTall);
}
//-----------------------------------------------------------------------------
// Purpose: Calculate the keyboard key that is a hotkey
//-----------------------------------------------------------------------------
wchar_t Label::CalculateHotkey(const char *text)
{
for (const char *ch = text; *ch != 0; ch++)
{
if (*ch == '&')
{
// get the next character
ch++;
if (*ch == '&')
{
// just an &
continue;
}
else if (*ch == 0)
{
break;
}
else if (V_isalnum(*ch))
{
// found the hotkey
return (wchar_t)tolower(*ch);
}
}
}
return '\0';
}
wchar_t Label::CalculateHotkey(const wchar_t *text)
{
if( text )
{
for (const wchar_t *ch = text; *ch != 0; ch++)
{
if (*ch == '&')
{
// get the next character
ch++;
if (*ch == '&')
{
// just an &
continue;
}
else if (*ch == 0)
{
break;
}
else if (iswalnum(*ch))
{
// found the hotkey
return (wchar_t)towlower(*ch);
}
}
}
}
return '\0';
}
//-----------------------------------------------------------------------------
// Purpose: Check if this label has a hotkey that is the key passed in.
//-----------------------------------------------------------------------------
Panel *Label::HasHotkey(wchar_t key)
{
#ifdef VGUI_HOTKEYS_ENABLED
if ( iswalnum( key ) )
key = towlower( key );
if (_hotkey == key)
return this;
#endif
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Set the hotkey for this label
//-----------------------------------------------------------------------------
void Label::SetHotkey(wchar_t ch)
{
_hotkey = ch;
}
wchar_t Label::GetHotKey()
{
return _hotkey;
}
//-----------------------------------------------------------------------------
// Purpose: Handle a hotkey by passing on focus to associate
//-----------------------------------------------------------------------------
void Label::OnHotkeyPressed()
{
// we can't accept focus, but if we are associated to a control give it to that
if (_associate.Get() && !IsBuildModeActive())
{
_associate->RequestFocus();
}
}
//-----------------------------------------------------------------------------
// Purpose: Redirect mouse pressed to giving focus to associate
//-----------------------------------------------------------------------------
void Label::OnMousePressed(MouseCode code)
{
if (_associate.Get() && !IsBuildModeActive())
{
_associate->RequestFocus();
}
}
//-----------------------------------------------------------------------------
// Purpose: Return the text in the label
//-----------------------------------------------------------------------------
void Label::GetText(char *textOut, int bufferLen)
{
_textImage->GetText(textOut, bufferLen);
}
//-----------------------------------------------------------------------------
// Purpose: Return the text in the label
//-----------------------------------------------------------------------------
void Label::GetText(wchar_t *textOut, int bufLenInBytes)
{
_textImage->GetText(textOut, bufLenInBytes);
}
//-----------------------------------------------------------------------------
// Purpose: Take the string and looks it up in the localization file
// to convert it to unicode
// Setting the text will not set the size of the label.
// Set the size explicitly or use setToContent()
//-----------------------------------------------------------------------------
void Label::SetText(const char *text)
{
// if set to null, just make blank
if (!text)
{
text = "";
}
// let the text image do the translation itself
_textImage->SetText(text);
if( text[0] == '#' )
{
SetHotkey(CalculateHotkey(g_pVGuiLocalize->Find(text)));
}
else
{
SetHotkey(CalculateHotkey(text));
}
m_bAutoWideDirty = m_bAutoWideToContents;
InvalidateLayout();
Repaint();
}
//-----------------------------------------------------------------------------
// Purpose: Set unicode text directly
//-----------------------------------------------------------------------------
void Label::SetText(const wchar_t *unicodeString, bool bClearUnlocalizedSymbol)
{
m_bAutoWideDirty = m_bAutoWideToContents;
if ( unicodeString && _textImage->GetUText() && !Q_wcscmp(unicodeString,_textImage->GetUText()) )
return;
_textImage->SetText(unicodeString, bClearUnlocalizedSymbol);
//!! need to calculate hotkey from translated string
SetHotkey(CalculateHotkey(unicodeString));
InvalidateLayout(); // possible that the textimage needs to expand
Repaint();
}
//-----------------------------------------------------------------------------
// Purpose: updates localized text
//-----------------------------------------------------------------------------
void Label::OnDialogVariablesChanged(KeyValues *dialogVariables )
{
StringIndex_t index = _textImage->GetUnlocalizedTextSymbol();
if (index != INVALID_LOCALIZE_STRING_INDEX)
{
// reconstruct the string from the variables
wchar_t buf[1024];
g_pVGuiLocalize->ConstructString(buf, sizeof(buf), index, dialogVariables);
SetText(buf);
}
}
//-----------------------------------------------------------------------------
// Purpose: Additional offset at the Start of the text (from whichever side it is aligned)
//-----------------------------------------------------------------------------
void Label::SetTextInset(int xInset, int yInset)
{
_textInset[0] = xInset;
_textInset[1] = yInset;
int wide, tall;
GetSize( wide, tall);
_textImage->SetDrawWidth(wide - _textInset[0]);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Label::GetTextInset(int *xInset, int *yInset )
{
if ( xInset )
{
*xInset = _textInset[0];
}
if ( yInset )
{
*yInset = _textInset[1];
}
}
//-----------------------------------------------------------------------------
// Purpose: Set the enabled state
//-----------------------------------------------------------------------------
void Label::SetEnabled(bool state)
{
Panel::SetEnabled(state);
}
//-----------------------------------------------------------------------------
// Purpose: Calculates where in the panel the content resides
// Input : &tx0 - [out] position of the content
// &ty0 -
// &tx1 -
// &ty1 -
// Note: horizontal alignment is west if the image dar has
// more than one image in it, this is because we use image sizes
// to determine layout in classes for example, Menu.
//-----------------------------------------------------------------------------
void Label::ComputeAlignment(int &tx0, int &ty0, int &tx1, int &ty1)
{
int wide, tall;
GetPaintSize(wide, tall);
int tWide,tTall;
// text bounding box
tx0 = 0;
ty0 = 0;
// loop through all the images and calculate the complete bounds
int maxX = 0, maxY = 0;
int actualXAlignment = _contentAlignment;
for (int i = 0; i < _imageDar.Count(); i++)
{
TImageInfo &imageInfo = _imageDar[i];
IImage *image = imageInfo.image;
if (!image)
continue; // skip over null images
// add up the bounds
int iWide, iTall;
image->GetSize(iWide, iTall);
if (iWide > wide) // if the image is larger than the label just do a west alignment
actualXAlignment = Label::a_west;
// get the max height
maxY = max(maxY, iTall);
maxX += iWide;
// add the offset to x
maxX += imageInfo.offset;
}
tWide = maxX;
tTall = maxY;
// x align text
switch (actualXAlignment)
{
// left
case Label::a_northwest:
case Label::a_west:
case Label::a_southwest:
{
tx0 = 0;
break;
}
// center
case Label::a_north:
case Label::a_center:
case Label::a_south:
{
tx0 = (wide - tWide) / 2;
break;
}
// right
case Label::a_northeast:
case Label::a_east:
case Label::a_southeast:
{
tx0 = wide - tWide;
break;
}
}
// y align text
switch (_contentAlignment)
{
//top
case Label::a_northwest:
case Label::a_north:
case Label::a_northeast:
{
ty0 = 0;
break;
}
// center
case Label::a_west:
case Label::a_center:
case Label::a_east:
{
ty0 = (tall - tTall) / 2;
break;
}
// south
case Label::a_southwest:
case Label::a_south:
case Label::a_southeast:
{
ty0 = tall - tTall;
break;
}
}
tx1 = tx0 + tWide;
ty1 = ty0 + tTall;
}
//-----------------------------------------------------------------------------
// Purpose: overridden main drawing function for the panel
//-----------------------------------------------------------------------------
void Label::Paint()
{
int tx0, ty0, tx1, ty1;
ComputeAlignment(tx0, ty0, tx1, ty1);
// calculate who our associate is if we haven't already
if (_associateName)
{
SetAssociatedControl(FindSiblingByName(_associateName));
delete [] _associateName;
_associateName = NULL;
}
int labelWide, labelTall;
GetSize(labelWide, labelTall);
int x = tx0, y = _textInset[1] + ty0;
int imageYPos = 0; // a place to save the y offset for when we draw the disable version of the image
// draw the set of images
for (int i = 0; i < _imageDar.Count(); i++)
{
TImageInfo &imageInfo = _imageDar[i];
IImage *image = imageInfo.image;
if (!image)
continue; // skip over null images
// add the offset to x
x += imageInfo.offset;
// if this is the text image then add its inset to the left or from the right
if (i == _textImageIndex)
{
switch ( _contentAlignment )
{
// left
case Label::a_northwest:
case Label::a_west:
case Label::a_southwest:
{
x += _textInset[0];
break;
}
// right
case Label::a_northeast:
case Label::a_east:
case Label::a_southeast:
{
x -= _textInset[0];
break;
}
}
}
// see if the image is in a fixed position
if (imageInfo.xpos >= 0)
{
x = imageInfo.xpos;
}
// draw
imageYPos = y;
image->SetPos(x, y);
// fix up y for center-aligned text
if (_contentAlignment == Label::a_west || _contentAlignment == Label::a_center || _contentAlignment == Label::a_east)
{
int iw, it;
image->GetSize(iw, it);
if (it < (ty1 - ty0))
{
imageYPos = ((ty1 - ty0) - it) / 2 + y;
image->SetPos(x, ((ty1 - ty0) - it) / 2 + y);
}
}
// don't resize the image unless its too big
if (imageInfo.width >= 0)
{
int w, t;
image->GetSize(w, t);
if (w > imageInfo.width)
{
image->SetSize(imageInfo.width, t);
}
}
// if it's the basic text image then draw specially
if (image == _textImage)
{
if (IsEnabled())
{
if (_associate.Get() && ipanel()->HasParent(input()->GetFocus(), _associate->GetVPanel()))
{
_textImage->SetColor(_associateColor);
}
else
{
_textImage->SetColor(GetFgColor());
}
_textImage->Paint();
}
else
{
// draw disabled version, with embossed look
// offset image
_textImage->SetPos(x + 1, imageYPos + 1);
_textImage->SetColor(_disabledFgColor1);
_textImage->Paint();
surface()->DrawFlushText();
// overlayed image
_textImage->SetPos(x, imageYPos);
_textImage->SetColor(_disabledFgColor2);
_textImage->Paint();
}
}
else
{
image->Paint();
}
// add the image size to x
int wide, tall;
image->GetSize(wide, tall);
x += wide;
}
}
//-----------------------------------------------------------------------------
// Purpose: Helper function, draws a simple line with dashing parameters
//-----------------------------------------------------------------------------
void Label::DrawDashedLine(int x0, int y0, int x1, int y1, int dashLen, int gapLen)
{
// work out which way the line goes
if ((x1 - x0) > (y1 - y0))
{
// x direction line
while (1)
{
if (x0 + dashLen > x1)
{
// draw partial
surface()->DrawFilledRect(x0, y0, x1, y1);
}
else
{
surface()->DrawFilledRect(x0, y0, x0 + dashLen, y1);
}
x0 += dashLen;
if (x0 + gapLen > x1)
break;
x0 += gapLen;
}
}
else
{
// y direction
while (1)
{
if (y0 + dashLen > y1)
{
// draw partial
surface()->DrawFilledRect(x0, y0, x1, y1);
}
else
{
surface()->DrawFilledRect(x0, y0, x1, y0 + dashLen);
}
y0 += dashLen;
if (y0 + gapLen > y1)
break;
y0 += gapLen;
}
}
}
void Label::SetContentAlignment(Alignment alignment)
{
_contentAlignment=alignment;
Repaint();
}
//-----------------------------------------------------------------------------
// Purpose: Size the width of the label to its contents - only works from in ApplySchemeSettings or PerformLayout()
//-----------------------------------------------------------------------------
void Label::SizeToContents()
{
int wide, tall;
GetContentSize(wide, tall);
SetSize(wide, tall);
}
//-----------------------------------------------------------------------------
// Purpose: Set the font the text is drawn in
//-----------------------------------------------------------------------------
void Label::SetFont(HFont font)
{
_textImage->SetFont(font);
Repaint();
}
//-----------------------------------------------------------------------------
// Purpose: Resond to resizing of the panel
//-----------------------------------------------------------------------------
void Label::OnSizeChanged(int wide, int tall)
{
InvalidateLayout();
Panel::OnSizeChanged(wide, tall);
}
//-----------------------------------------------------------------------------
// Purpose: Get the font the textImage is drawn in.
//-----------------------------------------------------------------------------
HFont Label::GetFont()
{
return _textImage->GetFont();
}
//-----------------------------------------------------------------------------
// Purpose: Set the foreground color of the Label
//-----------------------------------------------------------------------------
void Label::SetFgColor(Color color)
{
if (!(GetFgColor() == color))
{
BaseClass::SetFgColor(color);
_textImage->SetColor(color);
Repaint();
}
}
//-----------------------------------------------------------------------------
// Purpose: Get the foreground color of the Label
//-----------------------------------------------------------------------------
Color Label::GetFgColor()
{
Color clr = Panel::GetFgColor();
return clr;
}
//-----------------------------------------------------------------------------
// Purpose: Set the foreground color 1 color of the Label
//-----------------------------------------------------------------------------
void Label::SetDisabledFgColor1(Color color)
{
_disabledFgColor1 = color;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Label::SetDisabledFgColor2(Color color)
{
_disabledFgColor2 = color;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
Color Label::GetDisabledFgColor1()
{
return _disabledFgColor1;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
Color Label::GetDisabledFgColor2()
{
return _disabledFgColor2;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
TextImage *Label::GetTextImage()
{
return _textImage;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool Label::RequestInfo(KeyValues *outputData)
{
if (!stricmp(outputData->GetName(), "GetText"))
{
wchar_t wbuf[256];
_textImage->GetText(wbuf, 255);
outputData->SetWString("text", wbuf);
return true;
}
return Panel::RequestInfo(outputData);
}
//-----------------------------------------------------------------------------
// Purpose: Sets the text from the message
//-----------------------------------------------------------------------------
void Label::OnSetText(KeyValues *params)
{
KeyValues *pkvText = params->FindKey("text", false);
if (!pkvText)
return;
if (pkvText->GetDataType() == KeyValues::TYPE_STRING)
{
SetText(pkvText->GetString());
}
else if (pkvText->GetDataType() == KeyValues::TYPE_WSTRING)
{
SetText(pkvText->GetWString());
}
}
//-----------------------------------------------------------------------------
// Purpose: Add an image to the list
// returns the index the image was placed in
//-----------------------------------------------------------------------------
int Label::AddImage(IImage *image, int offset)
{
int newImage = _imageDar.AddToTail();
_imageDar[newImage].image = image;
_imageDar[newImage].offset = (short)offset;
_imageDar[newImage].xpos = -1;
_imageDar[newImage].width = -1;
InvalidateLayout();
return newImage;
}
//-----------------------------------------------------------------------------
// Purpose: removes all images from the list
// user is responsible for the memory
//-----------------------------------------------------------------------------
void Label::ClearImages()
{
_imageDar.RemoveAll();
_textImageIndex = -1;
}
void Label::ResetToSimpleTextImage()
{
ClearImages();
_textImageIndex = AddImage(_textImage, 0);
}
//-----------------------------------------------------------------------------
// Purpose: Multiple image handling
// Images are drawn from left to right across the label, ordered by index
// By default there is a TextImage in position 0
// Set the contents of an IImage in the IImage array.
//-----------------------------------------------------------------------------
void Label::SetImageAtIndex(int index, IImage *image, int offset)
{
EnsureImageCapacity(index);
// Assert( image );
if ( _imageDar[index].image != image || _imageDar[index].offset != offset)
{
_imageDar[index].image = image;
_imageDar[index].offset = (short)offset;
InvalidateLayout();
}
}
//-----------------------------------------------------------------------------
// Purpose: Get an IImage in the IImage array.
//-----------------------------------------------------------------------------
IImage *Label::GetImageAtIndex(int index)
{
if ( _imageDar.IsValidIndex( index ) )
return _imageDar[index].image;
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Get the number of images in the array.
//-----------------------------------------------------------------------------
int Label::GetImageCount()
{
return _imageDar.Count();
}
//-----------------------------------------------------------------------------
// Purpose: Move where the default text image is within the image array
// (it starts in position 0)
// Input : newIndex -
// Output : int - the index the default text image was previously in
//-----------------------------------------------------------------------------
int Label::SetTextImageIndex(int newIndex)
{
if (newIndex == _textImageIndex)
return _textImageIndex;
EnsureImageCapacity(newIndex);
int oldIndex = _textImageIndex;
if ( _textImageIndex >= 0 )
{
_imageDar[_textImageIndex].image = NULL;
}
if (newIndex > -1)
{
_imageDar[newIndex].image = _textImage;
}
_textImageIndex = newIndex;
return oldIndex;
}
//-----------------------------------------------------------------------------
// Purpose: Ensure that the maxIndex will be a valid index
//-----------------------------------------------------------------------------
void Label::EnsureImageCapacity(int maxIndex)
{
while (_imageDar.Size() <= maxIndex)
{
AddImage(NULL, 0);
}
}
//-----------------------------------------------------------------------------
// Purpose: Set the offset in pixels before the image
//-----------------------------------------------------------------------------
void Label::SetImagePreOffset(int index, int preOffset)
{
if (_imageDar.IsValidIndex(index) && _imageDar[index].offset != preOffset)
{
_imageDar[index].offset = (short)preOffset;
InvalidateLayout();
}
}
//-----------------------------------------------------------------------------
// Purpose: fixes the layout bounds of the image within the label
//-----------------------------------------------------------------------------
void Label::SetImageBounds(int index, int x, int width)
{
_imageDar[index].xpos = (short)x;
_imageDar[index].width = (short)width;
}
//-----------------------------------------------------------------------------
// Purpose: Labels can be associated with controls, and alter behaviour based on the associates behaviour
// If the associate is disabled, so are we
// If the associate has focus, we may alter how we draw
// If we get a hotkey press or focus message, we forward the focus to the associate
//-----------------------------------------------------------------------------
void Label::SetAssociatedControl(Panel *control)
{
if (control != this)
{
_associate = control;
}
else
{
// don't let the associate ever be set to be ourself
_associate = NULL;
}
}
//-----------------------------------------------------------------------------
// Purpose: Called after a panel requests focus to fix up the whole chain
//-----------------------------------------------------------------------------
void Label::OnRequestFocus(VPANEL subFocus, VPANEL defaultPanel)
{
if (_associate.Get() && subFocus == GetVPanel() && !IsBuildModeActive())
{
// we've received focus; pass the focus onto the associate instead
_associate->RequestFocus();
}
else
{
BaseClass::OnRequestFocus(subFocus, defaultPanel);
}
}
//-----------------------------------------------------------------------------
// Purpose: sets custom settings from the scheme file
//-----------------------------------------------------------------------------
void Label::ApplySchemeSettings(IScheme *pScheme)
{
BaseClass::ApplySchemeSettings(pScheme);
if (_fontOverrideName)
{
// use the custom specified font since we have one set
SetFont(pScheme->GetFont(_fontOverrideName, IsProportional()));
}
if ( GetFont() == INVALID_FONT )
{
SetFont( pScheme->GetFont( "Default", IsProportional() ) );
}
if ( m_bWrap || m_bCenterWrap )
{
//tell it how big it is
int wide, tall;
Panel::GetSize(wide, tall);
wide -= _textInset[0]; // take inset into account
_textImage->SetSize(wide, tall);
_textImage->RecalculateNewLinePositions();
}
else
{
// if you don't set the size of the image, many, many buttons will break - we might want to look into fixing this all over the place later
int wide, tall;
_textImage->GetContentSize(wide, tall);
_textImage->SetSize(wide, tall);
}
if ( m_bAutoWideToContents )
{
m_bAutoWideDirty = true;
HandleAutoSizing();
}
// clear out any the images, since they will have been invalidated
for (int i = 0; i < _imageDar.Count(); i++)
{
IImage *image = _imageDar[i].image;
if (!image)
continue; // skip over null images
if (i == _textImageIndex)
continue;
_imageDar[i].image = NULL;
}
SetDisabledFgColor1(GetSchemeColor("Label.DisabledFgColor1", pScheme));
SetDisabledFgColor2(GetSchemeColor("Label.DisabledFgColor2", pScheme));
SetBgColor(GetSchemeColor("Label.BgColor", pScheme));
switch (_textColorState)
{
case CS_DULL:
SetFgColor(GetSchemeColor("Label.TextDullColor", pScheme));
break;
case CS_BRIGHT:
SetFgColor(GetSchemeColor("Label.TextBrightColor", pScheme));
break;
case CS_NORMAL:
default:
SetFgColor(GetSchemeColor("Label.TextColor", pScheme));
break;
}
_associateColor = GetSchemeColor("Label.SelectedTextColor", pScheme);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Label::GetSettings( KeyValues *outResourceData )
{
// panel settings
Panel::GetSettings( outResourceData );
// label settings
char buf[256];
_textImage->GetUnlocalizedText( buf, 255 );
if (!strnicmp(buf, "#var_", 5))
{
// strip off the variable prepender on save
outResourceData->SetString( "labelText", buf + 5 );
}
else
{
outResourceData->SetString( "labelText", buf );
}
const char *alignmentString = "";
switch ( _contentAlignment )
{
case a_northwest: alignmentString = "north-west"; break;
case a_north: alignmentString = "north"; break;
case a_northeast: alignmentString = "north-east"; break;
case a_center: alignmentString = "center"; break;
case a_east: alignmentString = "east"; break;
case a_southwest: alignmentString = "south-west"; break;
case a_south: alignmentString = "south"; break;
case a_southeast: alignmentString = "south-east"; break;
case a_west:
default: alignmentString = "west"; break;
}
outResourceData->SetString( "textAlignment", alignmentString );
if (_associate)
{
outResourceData->SetString("associate", _associate->GetName());
}
outResourceData->SetInt("dulltext", (int)(_textColorState == CS_DULL));
outResourceData->SetInt("brighttext", (int)(_textColorState == CS_BRIGHT));
if (_fontOverrideName)
{
outResourceData->SetString("font", _fontOverrideName);
}
outResourceData->SetInt("wrap", ( m_bWrap ? 1 : 0 ));
outResourceData->SetInt("centerwrap", ( m_bCenterWrap ? 1 : 0 ));
if ( m_bUseProportionalInsets )
{
outResourceData->SetInt("textinsetx", scheme()->GetProportionalNormalizedValueEx( GetScheme(), _textInset[0] ) );
outResourceData->SetInt("textinsety", _textInset[1]);
}
else
{
outResourceData->SetInt("textinsetx", _textInset[0]);
outResourceData->SetInt("textinsety", _textInset[1]);
}
outResourceData->SetInt("auto_wide_tocontents", ( m_bAutoWideToContents ? 1 : 0 ));
outResourceData->SetInt("use_proportional_insets", ( m_bUseProportionalInsets ? 1 : 0 ));
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void Label::ApplySettings( KeyValues *inResourceData )
{
BaseClass::ApplySettings( inResourceData );
// label settings
const char *labelText = inResourceData->GetString( "labelText", NULL );
if ( labelText )
{
if (labelText[0] == '%' && labelText[strlen(labelText) - 1] == '%')
{
// it's a variable, set it to be a special variable localized string
wchar_t unicodeVar[256];
g_pVGuiLocalize->ConvertANSIToUnicode(labelText, unicodeVar, sizeof(unicodeVar));
char var[256];
_snprintf(var, sizeof(var), "#var_%s", labelText);
g_pVGuiLocalize->AddString(var + 1, unicodeVar, "");
SetText(var);
}
else
{
SetText(labelText);
}
}
// text alignment
const char *alignmentString = inResourceData->GetString( "textAlignment", "" );
int align = -1;
if ( !stricmp(alignmentString, "north-west") )
{
align = a_northwest;
}
else if ( !stricmp(alignmentString, "north") )
{
align = a_north;
}
else if ( !stricmp(alignmentString, "north-east") )
{
align = a_northeast;
}
else if ( !stricmp(alignmentString, "west") )
{
align = a_west;
}
else if ( !stricmp(alignmentString, "center") )
{
align = a_center;
}
else if ( !stricmp(alignmentString, "east") )
{
align = a_east;
}
else if ( !stricmp(alignmentString, "south-west") )
{
align = a_southwest;
}
else if ( !stricmp(alignmentString, "south") )
{
align = a_south;
}
else if ( !stricmp(alignmentString, "south-east") )
{
align = a_southeast;
}
if ( align != -1 )
{
SetContentAlignment( (Alignment)align );
}
// the control we are to be associated with may not have been created yet,
// so keep a pointer to it's name and calculate it when we can
const char *associateName = inResourceData->GetString("associate", "");
if (associateName[0] != 0)
{
int len = Q_strlen(associateName) + 1;
_associateName = new char[ len ];
Q_strncpy( _associateName, associateName, len );
}
if (inResourceData->GetInt("dulltext", 0) == 1)
{
SetTextColorState(CS_DULL);
}
else if (inResourceData->GetInt("brighttext", 0) == 1)
{
SetTextColorState(CS_BRIGHT);
}
else
{
SetTextColorState(CS_NORMAL);
}
// font settings
const char *overrideFont = inResourceData->GetString("font", "");
IScheme *pScheme = scheme()->GetIScheme( GetScheme() );
if (*overrideFont)
{
delete [] _fontOverrideName;
int len = Q_strlen(overrideFont) + 1;
_fontOverrideName = new char[ len ];
Q_strncpy(_fontOverrideName, overrideFont, len );
SetFont(pScheme->GetFont(_fontOverrideName, IsProportional()));
}
else if (_fontOverrideName)
{
delete [] _fontOverrideName;
_fontOverrideName = NULL;
SetFont(pScheme->GetFont("Default", IsProportional()));
}
bool bWrapText = inResourceData->GetInt("centerwrap", 0) > 0;
SetCenterWrap( bWrapText );
m_bAutoWideToContents = inResourceData->GetInt("auto_wide_tocontents", 0) > 0;
bWrapText = inResourceData->GetInt("wrap", 0) > 0;
SetWrap( bWrapText );
int inset_x = inResourceData->GetInt("textinsetx", _textInset[0]);
int inset_y = inResourceData->GetInt("textinsety", _textInset[1]);
// Had to play it safe and add a new key for backwards compatibility
m_bUseProportionalInsets = inResourceData->GetInt("use_proportional_insets", 0) > 0;
if ( m_bUseProportionalInsets )
{
inset_x = scheme()->GetProportionalScaledValueEx( GetScheme(), inset_x );
}
SetTextInset( inset_x, inset_y );
bool bAllCaps = inResourceData->GetInt("allcaps", 0) > 0;
SetAllCaps( bAllCaps );
InvalidateLayout(true);
}
//-----------------------------------------------------------------------------
// Purpose: Returns a description of the label string
//-----------------------------------------------------------------------------
const char *Label::GetDescription( void )
{
static char buf[1024];
Q_snprintf(buf, sizeof(buf), "%s, string labelText, string associate, alignment textAlignment, int wrap, int dulltext, int brighttext, string font", BaseClass::GetDescription());
return buf;
}
//-----------------------------------------------------------------------------
// Purpose: If a label has images in _imageDar, the size
// must take those into account as well as the textImage part
// Textimage part will shrink ONLY if there is not enough room.
//-----------------------------------------------------------------------------
void Label::PerformLayout()
{
int wide, tall;
Panel::GetSize(wide, tall);
wide -= _textInset[0]; // take inset into account
// if we just have a textImage, this is trivial.
if (_imageDar.Count() == 1 && _textImageIndex == 0)
{
if ( m_bWrap || m_bCenterWrap )
{
int twide, ttall;
_textImage->GetContentSize(twide, ttall);
_textImage->SetSize(wide, ttall);
}
else
{
int twide, ttall;
_textImage->GetContentSize(twide, ttall);
// tell the textImage how much space we have to draw in
if ( wide < twide)
_textImage->SetSize(wide, ttall);
else
_textImage->SetSize(twide, ttall);
}
HandleAutoSizing();
HandleAutoSizing();
return;
}
// assume the images in the dar cannot be resized, and if
// the images + the textimage are too wide we shring the textimage part
if (_textImageIndex < 0)
return;
// get the size of the images
int widthOfImages = 0;
for (int i = 0; i < _imageDar.Count(); i++)
{
TImageInfo &imageInfo = _imageDar[i];
IImage *image = imageInfo.image;
if (!image)
continue; // skip over null images
if (i == _textImageIndex)
continue;
// add up the bounds
int iWide, iTall;
image->GetSize(iWide, iTall);
widthOfImages += iWide;
}
// so this is how much room we have to draw the textimage part
int spaceAvail = wide - widthOfImages;
// if we have no space at all just leave everything as is.
if (spaceAvail < 0)
return;
int twide, ttall;
_textImage->GetSize (twide, ttall);
// tell the textImage how much space we have to draw in
_textImage->SetSize(spaceAvail, ttall);
HandleAutoSizing();
}
void Label::SetWrap( bool bWrap )
{
m_bWrap = bWrap;
_textImage->SetWrap( m_bWrap );
InvalidateLayout();
}
void Label::SetCenterWrap( bool bWrap )
{
m_bCenterWrap = bWrap;
_textImage->SetCenterWrap( m_bCenterWrap );
InvalidateLayout();
}
void Label::SetAllCaps( bool bAllCaps )
{
m_bAllCaps = bAllCaps;
_textImage->SetAllCaps( m_bAllCaps );
InvalidateLayout();
}
void Label::HandleAutoSizing( void )
{
if ( m_bAutoWideDirty )
{
m_bAutoWideDirty = false;
// Only change our width to match our content
int wide, tall;
GetContentSize(wide, tall);
SetSize(wide, GetTall());
}
}
|