Show cursor on exit
[utui.git] / ctrl / label.cc
blob4297ab9e506feafabfc3914387f20fe210e3ff20
1 // This file is part of the utui library, a terminal UI framework.
2 //
3 // Copyright (C) 2006 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
5 //
6 // label.cc
7 //
9 #include "label.h"
11 /// Default constructor.
12 CLabel::CLabel (rctext_t s)
13 : CWindow (),
14 m_Text (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]);
30 CWindow::OnDraw (gc);
31 string line;
32 coord_t y = 0;
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
42 Point2d lrbr (lr[1]);
43 string line;
44 lr[1] = lr[0];
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)));
47 ++ lr[1][1];
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) {
57 gc.Char (x, y, *ic);
58 const bool bAccel (*ic == '&');
59 x += !bAccel;
60 gc.FgColor (col[bAccel * 2]);
62 return (x);
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);