This repository has been archived by the owner on Dec 9, 2020. It is now read-only.
/
Panel.cpp
132 lines (109 loc) · 3.25 KB
/
Panel.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
/*
* Copyright (C) 2007 by Mark Pustjens <pustjens@dds.nl>
* Copyright (C) 2010-2012 by CenterIM developers
*
* This file is part of CenterIM.
*
* CenterIM is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CenterIM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* @file
* Panel class implementation.
*
* @ingroup cppconsui
*/
#include "Panel.h"
namespace CppConsUI
{
Panel::Panel(int w, int h, const char *text)
: Widget(w, h), title(NULL), title_width(0)
{
SetTitle(text);
}
Panel::~Panel()
{
if (title)
g_free(title);
}
void Panel::Draw()
{
ProceedUpdateArea();
if (!area)
return;
int realw = area->getmaxx();
int realh = area->getmaxy();
int attrs, i;
// calc title width
int draw_title_width = 0;
if (realw > 4)
draw_title_width = realw - 4;
draw_title_width = MIN(draw_title_width, title_width);
// calc horizontal line length (one segment width)
int hline_len = 0;
int extra = draw_title_width ? 4 : 2;
if (realw > draw_title_width + extra)
hline_len = (realw - draw_title_width - extra) / 2;
if (draw_title_width) {
// draw title
attrs = GetColorPair("panel", "title");
area->attron(attrs);
area->mvaddstring(2 + hline_len, 0, draw_title_width, title);
area->attroff(attrs);
}
// draw lines
attrs = GetColorPair("panel", "line");
area->attron(attrs);
int wa = (realw >= width || width == AUTOSIZE) && realw > 1 ? 1 : 0;
int ha = (realh >= height || height == AUTOSIZE) && realh > 1 ? 1 : 0;
// draw top horizontal line
for (i = 1; i < 1 + hline_len; i++)
area->mvaddlinechar(i, 0, Curses::LINE_HLINE);
for (i = 1 + hline_len + extra - 2 + draw_title_width; i < realw - 1 * wa;
i++)
area->mvaddlinechar(i, 0, Curses::LINE_HLINE);
// draw bottom horizontal line
if (ha)
for (i = 1; i < realw - 1 * wa; i++)
area->mvaddlinechar(i, realh - 1, Curses::LINE_HLINE);
// draw left and right vertical line
for (i = 1; i < realh - 1 * ha; i++)
area->mvaddlinechar(0, i, Curses::LINE_VLINE);
if (wa)
for (i = 1; i < realh - 1 * ha; i++)
area->mvaddlinechar(realw - 1, i, Curses::LINE_VLINE);
// draw corners
area->mvaddlinechar(0, 0, Curses::LINE_ULCORNER);
if (wa)
area->mvaddlinechar(realw - 1, 0, Curses::LINE_URCORNER);
if (ha)
area->mvaddlinechar(0, realh - 1, Curses::LINE_LLCORNER);
if (wa && ha)
area->mvaddlinechar(realw - 1, realh - 1, Curses::LINE_LRCORNER);
area->attroff(attrs);
}
void Panel::SetTitle(const char *text)
{
if (title)
g_free(title);
title = g_strdup(text);
title_width = title ? Curses::onscreen_width(title) : 0;
Redraw();
}
const char *Panel::GetTitle() const
{
return title;
}
} // namespace CppConsUI
/* vim: set tabstop=2 shiftwidth=2 textwidth=78 expandtab : */