1 // This file is part of the utui library, a terminal UI framework.
3 // Copyright (C) 2006 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
11 /// Default constructor.
12 CLabel::CLabel (rctext_t s
)
16 SetFlag (f_NoTabOrder
);
19 /// Autogen factory for dialog descriptions.
20 /*static*/ pwidget_t
CLabel::Factory (widival_t v
)
22 return (new CLabel (v
.text
));
25 /// Draws the label in the window.
26 void CLabel::OnDraw (CGC
& gc
)
28 static const EColor colors
[3] = { black
, cyan
, red
};
29 gc
.Color (colors
[0], colors
[1]);
33 for (uoff_t i
= 0; i
< Text().size(); i
+= line
.size() + 1) {
34 line
.link (Text().iat(i
), Text().iat (Text().find ('\n', i
)));
35 DrawTextWithAccel (gc
, 0, y
++, line
, colors
);
39 /// Returns the recommended size for the window.
40 void CLabel::SizeHints (rrect_t lr
) const
45 for (uoff_t i
= 0; i
< Text().size(); i
+= line
.size() + 1) {
46 line
.link (Text().iat(i
), Text().iat (Text().find ('\n', i
)));
48 lr
[1][0] = max (lr
[1][0], lr
[0][0] + coord_t(TextWithAccelSize(line
)));
50 simd::pmin (lrbr
, lr
[1]);
53 /// Draws text \p s with & accelerators in different color.
54 coord_t
CLabel::DrawTextWithAccel (CGC
& gc
, coord_t x
, coord_t y
, rctext_t s
, ptextcols_t col
) const
56 for (string::utf8_iterator ic
= s
.utf8_begin(); ic
< s
.utf8_end(); ++ic
) {
58 const bool bAccel (*ic
== '&');
60 gc
.FgColor (col
[bAccel
* 2]);
65 /// Returns the size of the label text after contracting & accelerators.
66 size_t CLabel::TextWithAccelSize (rctext_t s
) const
68 return (s
.length() - count(s
, '&'));
71 /// \brief Returns the accelerator key for this item.
72 /// Searches for an & followed by a character, and decode it as utf8.
73 wchar_t CLabel::FocusAccelKey (void) const
75 uoff_t ia
= Text().find ('&');
76 return (ia
< Text().size() ? tolower(*utf8in(Text().iat(ia
+1))) : 0);