forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabel.cpp
1392 lines (1207 loc) · 36.1 KB
/
Label.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
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
//========= 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 )