1 // Copyright (C) 2007 Mark Pustjens <pustjens@dds.nl>
2 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
4 // This file is part of CenterIM.
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/>.
20 /// Panel class implementation.
22 /// @ingroup cppconsui
26 #include "ColorScheme.h"
33 Panel::Panel(int w
, int h
, const char *text
)
34 : Widget(w
, h
), title_(nullptr), title_width_(0)
44 int Panel::draw(Curses::ViewPort area
, Error
&error
)
48 // Calculate title width.
49 int draw_title_width
= 0;
51 draw_title_width
= real_width_
- 4;
52 draw_title_width
= std::min(draw_title_width
, title_width_
);
54 // Calculate horizontal line length (one segment width).
56 int extra
= draw_title_width
? 4 : 2;
57 if (real_width_
> draw_title_width
+ extra
)
58 hline_len
= (real_width_
- draw_title_width
- extra
) / 2;
60 if (draw_title_width
) {
62 DRAW(getAttributes(ColorScheme::PROPERTY_PANEL_TITLE
, &attrs
, error
));
63 DRAW(area
.attrOn(attrs
, error
));
64 DRAW(area
.addString(2 + hline_len
, 0, draw_title_width
, title_
, error
));
65 DRAW(area
.attrOff(attrs
, error
));
69 DRAW(getAttributes(ColorScheme::PROPERTY_PANEL_LINE
, &attrs
, error
));
70 DRAW(area
.attrOn(attrs
, error
));
72 // Draw top horizontal line.
73 for (i
= 1; i
< 1 + hline_len
; ++i
)
74 DRAW(area
.addLineChar(i
, 0, Curses::LINE_HLINE
, error
));
75 for (i
= 1 + hline_len
+ extra
- 2 + draw_title_width
; i
< real_width_
- 1;
77 DRAW(area
.addLineChar(i
, 0, Curses::LINE_HLINE
, error
));
79 // Draw bottom horizontal line.
80 for (i
= 1; i
< real_width_
- 1; ++i
)
81 DRAW(area
.addLineChar(i
, real_height_
- 1, Curses::LINE_HLINE
, error
));
83 // Draw left and right vertical line.
84 for (i
= 1; i
< real_height_
- 1; ++i
)
85 DRAW(area
.addLineChar(0, i
, Curses::LINE_VLINE
, error
));
86 for (i
= 1; i
< real_height_
- 1; ++i
)
87 DRAW(area
.addLineChar(real_width_
- 1, i
, Curses::LINE_VLINE
, error
));
90 DRAW(area
.addLineChar(0, 0, Curses::LINE_ULCORNER
, error
));
91 DRAW(area
.addLineChar(real_width_
- 1, 0, Curses::LINE_URCORNER
, error
));
92 DRAW(area
.addLineChar(0, real_height_
- 1, Curses::LINE_LLCORNER
, error
));
93 DRAW(area
.addLineChar(
94 real_width_
- 1, real_height_
- 1, Curses::LINE_LRCORNER
, error
));
96 DRAW(area
.attrOff(attrs
, error
));
101 void Panel::setTitle(const char *new_title
)
103 std::size_t size
= 1;
104 if (new_title
!= nullptr)
105 size
+= std::strlen(new_title
);
106 auto new_storage
= new char[size
];
107 if (new_title
!= nullptr)
108 std::strcpy(new_storage
, new_title
);
110 new_storage
[0] = '\0';
113 title_
= new_storage
;
115 title_width_
= Curses::onScreenWidth(title_
);
119 } // namespace CppConsUI
121 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: