-
Notifications
You must be signed in to change notification settings - Fork 50
/
uidialog.cpp
94 lines (77 loc) · 2.19 KB
/
uidialog.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
// uidialog.cpp
//
// Copyright (c) 2019-2023 Kristofer Berggren
// All rights reserved.
//
// nchat is distributed under the MIT license, see LICENSE for details.
#include "uidialog.h"
#include "strutil.h"
#include "uicolorconfig.h"
#include "uimodel.h"
#include "uiview.h"
UiDialog::UiDialog(const UiDialogParams& p_Params)
: m_View(p_Params.view)
, m_Model(p_Params.model)
, m_Title(p_Params.title)
, m_WReq(p_Params.wReq)
, m_HReq(p_Params.hReq)
{
Init();
curs_set(0);
}
UiDialog::~UiDialog()
{
Cleanup();
curs_set(1);
}
void UiDialog::Init()
{
int screenW = m_View->GetScreenWidth();
int screenH = m_View->GetScreenHeight();
int w = (m_WReq > 1.0f) ? m_WReq : (screenW * m_WReq);
int h = (m_HReq > 1.0f) ? m_HReq : (screenH * m_HReq);
int x = (screenW - w) / 2;
int y = (screenH - h) / 3;
m_BorderWin = newwin(h, w, y, x);
m_W = w - 4;
m_H = h - 2;
m_X = x + 2;
m_Y = y + 1;
DrawBorder();
m_Win = newwin(m_H, m_W, m_Y, m_X);
}
void UiDialog::Cleanup()
{
delwin(m_Win);
delwin(m_BorderWin);
}
void UiDialog::SetFooter(const std::string& p_Footer)
{
m_Footer = p_Footer;
DrawBorder();
}
void UiDialog::DrawBorder()
{
static int colorPair = UiColorConfig::GetColorPair("dialog_color");
static int attribute = UiColorConfig::GetAttribute("dialog_attr");
werase(m_BorderWin);
wbkgd(m_BorderWin, colorPair | ' ');
wattron(m_BorderWin, attribute | colorPair);
wborder(m_BorderWin, 0, 0, 0, 0, 0, 0, 0, 0);
const int maxTextWidth = m_W - 2;
std::string title = " " + m_Title.substr(0, maxTextWidth) + " ";
std::wstring wtitle = StrUtil::ToWString(title);
int titlex = std::max(0, (m_W - (int)wtitle.size()) / 2) + 2;
int titlew = std::min((int)wtitle.size(), m_W);
mvwaddnwstr(m_BorderWin, 0, titlex, wtitle.c_str(), titlew);
if (!m_Footer.empty())
{
std::string footer = " " + m_Footer.substr(0, maxTextWidth) + " ";
std::wstring wfooter = StrUtil::ToWString(footer);
int footerx = std::max(0, (m_W - (int)wfooter.size()) / 2) + 2;
int footerw = std::min((int)wfooter.size(), m_W);
mvwaddnwstr(m_BorderWin, m_H + 1, footerx, wfooter.c_str(), footerw);
}
wattroff(m_BorderWin, attribute | colorPair);
wrefresh(m_BorderWin);
}