Fix build on systems that have a separate libintl library
[centerim5.git] / cppconsui / Label.cpp
blob1e718492e3f75706a06f30628d88aa50b966c299
1 // Copyright (C) 2007 Mark Pustjens <pustjens@dds.nl>
2 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
3 //
4 // This file is part of CenterIM.
5 //
6 // CenterIM is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // CenterIM is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
19 /// @file
20 /// Label class implementation.
21 ///
22 /// @ingroup cppconsui
24 #include "Label.h"
26 #include "ColorScheme.h"
28 #include <cstring>
30 namespace CppConsUI {
32 Label::Label(int w, int h, const char *text) : Widget(w, h), text_(nullptr)
34 setText(text);
37 Label::Label(const char *text) : Widget(AUTOSIZE, AUTOSIZE), text_(nullptr)
39 setText(text);
42 Label::~Label()
44 delete[] text_;
47 int Label::draw(Curses::ViewPort area, Error &error)
49 int attrs;
50 DRAW(getAttributes(ColorScheme::PROPERTY_LABEL_TEXT, &attrs, error));
51 DRAW(area.attrOn(attrs, error));
53 // Print text.
54 int y = 0;
55 const char *start, *end;
56 start = end = text_;
57 int printed;
58 while (*end != '\0') {
59 if (*end == '\n') {
60 DRAW(area.addString(
61 0, y, real_width_ * (real_height_ - y), start, end, error, &printed));
62 y += (printed / real_width_) + 1;
63 start = end + 1;
65 ++end;
67 DRAW(
68 area.addString(0, y, real_width_ * (real_height_ - y), start, end, error));
70 DRAW(area.attrOff(attrs, error));
72 return 0;
75 void Label::setText(const char *new_text)
77 std::size_t size = 1;
78 if (new_text != nullptr)
79 size += std::strlen(new_text);
80 auto new_storage = new char[size];
81 if (new_text != nullptr)
82 std::strcpy(new_storage, new_text);
83 else
84 new_storage[0] = '\0';
86 delete[] text_;
87 text_ = new_storage;
89 // Update wish height.
90 int h = 1;
91 for (const char *cur = text_; *cur; ++cur)
92 if (*cur == '\n')
93 ++h;
94 setWishHeight(h);
96 redraw();
99 } // namespace CppConsUI
101 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: