-
Notifications
You must be signed in to change notification settings - Fork 50
/
uimessagedialog.cpp
129 lines (116 loc) · 2.88 KB
/
uimessagedialog.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
// uimessagedialog.cpp
//
// Copyright (c) 2019-2024 Kristofer Berggren
// All rights reserved.
//
// nchat is distributed under the MIT license, see LICENSE for details.
#include "uimessagedialog.h"
#include "numutil.h"
#include "strutil.h"
#include "timeutil.h"
#include "uicolorconfig.h"
#include "uicontroller.h"
#include "uikeyconfig.h"
#include "uimodel.h"
#include "uiview.h"
UiMessageDialog::UiMessageDialog(const UiDialogParams& p_Params, const std::string& p_Message)
: UiDialog(p_Params)
, m_Message(p_Message)
{
m_Model->SetMessageDialogActive(true);
m_View->Draw();
curs_set(0);
}
UiMessageDialog::~UiMessageDialog()
{
m_Model->SetMessageDialogActive(false);
}
bool UiMessageDialog::Run()
{
Draw();
while (m_Running)
{
wint_t key = UiController::GetKey(50);
if (key != 0)
{
KeyHandler(key);
}
}
return m_Result;
}
void UiMessageDialog::KeyHandler(wint_t p_Key)
{
static wint_t keyCancel = UiKeyConfig::GetKey("cancel");
static wint_t keyQuit = UiKeyConfig::GetKey("quit");
static wint_t keyOtherCommandsHelp = UiKeyConfig::GetKey("other_commands_help");
static wint_t keyOk = UiKeyConfig::GetKey("ok");
static wint_t keyTerminalFocusIn = UiKeyConfig::GetKey("terminal_focus_in");
static wint_t keyTerminalFocusOut = UiKeyConfig::GetKey("terminal_focus_out");
static wint_t keyTerminalResize = UiKeyConfig::GetKey("terminal_resize");
bool isDirty = true;
if (p_Key == keyTerminalResize)
{
Cleanup();
m_Model->SetHelpOffset(0);
m_View->Init();
m_View->Draw();
curs_set(0);
Init();
}
else if (p_Key == keyTerminalFocusIn)
{
m_Model->SetTerminalActive(true);
}
else if (p_Key == keyTerminalFocusOut)
{
m_Model->SetTerminalActive(false);
}
else if (p_Key == keyCancel)
{
m_Result = false;
m_Running = false;
}
else if (p_Key == keyQuit)
{
m_Result = false;
m_Running = false;
}
else if (p_Key == keyOk)
{
m_Result = true;
m_Running = false;
}
else if (p_Key == keyOtherCommandsHelp)
{
m_Model->SetHelpOffset(m_Model->GetHelpOffset() + 1);
m_View->Draw();
curs_set(0);
}
else
{
isDirty = false;
}
if (isDirty)
{
Draw();
}
}
void UiMessageDialog::Draw()
{
static int colorPair = UiColorConfig::GetColorPair("dialog_color");
static int attribute = UiColorConfig::GetAttribute("dialog_attr");
werase(m_Win);
wbkgd(m_Win, colorPair | ' ');
wattron(m_Win, attribute | colorPair);
std::vector<std::wstring> wlines;
wlines = StrUtil::WordWrap(StrUtil::ToWString(m_Message), m_W, false, false, false, 2);
for (int i = 0; i < std::min(m_H - 1, (int)wlines.size()); ++i)
{
const std::wstring& wdisp = wlines.at(i);
int w = std::min((int)wdisp.size(), m_W);
int x = std::max(0, ((m_W - w) / 2));
mvwaddnwstr(m_Win, i + 1, x, wdisp.c_str(), w);
}
wattroff(m_Win, attribute | colorPair);
wrefresh(m_Win);
}