-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCMessageBox.cpp
More file actions
135 lines (113 loc) · 3.22 KB
/
CMessageBox.cpp
File metadata and controls
135 lines (113 loc) · 3.22 KB
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
133
134
135
#include "CMessageBox.h"
#include "CButton.h"
#include "CLabel.h"
#include <string.h>
#include <functional>
CMessageBox::CMessageBox(int x, int y, int w, int h) : CForm(x, y, w, h)
{
using namespace std::placeholders;
int panelW = mPanel->Width();
btnCancel = new CButton();
mPanel->AddWindow(btnCancel);
btnCancel->SetText("Cancel", CText::sFontSmall, 120, 40);
btnCancel->SetPosition(panelW - btnCancel->Width() - 5, mPanel->mTexH - btnCancel->mTexH - 10);
btnCancel->AddClickHandler(std::bind(&CMessageBox::OnButton, this, _1));
//btnCancel->AddClickHandler(std::bind(&CSettingsForm::OnButton, this, _1));
btnOK = new CButton();
mPanel->AddWindow(btnOK);
btnOK->SetText("OK", CText::sFontSmall, 120, 40);
btnOK->SetPosition(btnCancel->X - btnOK->Width() - 15, mPanel->mTexH - btnCancel->mTexH - 10);
btnOK->AddClickHandler(std::bind(&CMessageBox::OnButton, this, _1));
//button->AddClickHandler(std::bind(&CSettingsForm::OnButton, this, _1));
mMultiLine = new CMultiLine();
mPanel->AddWindow(mMultiLine);
}
void CMessageBox::AddLine(string text)
{
AddLine(text, CSurface::HAlignment::Left);
}
void CMessageBox::AddLine(string text, CSurface::HAlignment align)
{
mMultiLine->AddLine(text, align);
mMultiLine->X = (mPanel->Width() - mMultiLine->mTextSurface->Width()) / 2;
mMultiLine->Y = (btnOK->Y - mMultiLine->mTextSurface->Height()) / 2;
}
void CMessageBox::AddOnCloseHandler(std::function<void(CMessageBox*, MessageResult)> callback)
{
OnCloseHandler = callback;
}
void CMessageBox::OnButton(CButton* button)
{
if (button == btnCancel)
{
if (OnCloseHandler)
{
OnCloseHandler(this, MessageResult::Cancel);
OnCloseHandler = nullptr;
}
Hide();
}
else if (button == btnOK)
{
if (OnCloseHandler)
{
OnCloseHandler(this, MessageResult::OK);
OnCloseHandler = nullptr;
}
Hide();
}
}
void CMessageBox::OnClose(CIcon* icon)
{
CForm::OnClose(icon);
if (OnCloseHandler)
{
OnCloseHandler(this, MessageResult::Cancel);
OnCloseHandler = nullptr;
}
}
void CMessageBox::ShowMessage(std::string text, std::string caption, MessageStyle style, MessageAlign align)
{
char* line;
const char c[2] = "\n";
int panelW = mPanel->Width();
OnCloseHandler = nullptr;
switch (style)
{
case MessageStyle::OK:
btnCancel->mVisible = false;
btnCancel->mCanFocus = false;
btnOK->mVisible = true;
btnOK->mCanFocus = true;
btnOK->SetPosition(panelW - btnCancel->Width() - 5, mPanel->mTexH - btnCancel->mTexH - 10);
break;
case MessageStyle::OK_Cancel:
btnCancel->mVisible = true;
btnCancel->mCanFocus = true;
btnCancel->SetPosition(panelW - btnCancel->Width() - 5, mPanel->mTexH - btnCancel->mTexH - 10);
btnOK->mVisible = true;
btnOK->mCanFocus = true;
btnOK->SetPosition(btnCancel->X - btnOK->Width() - 15, mPanel->mTexH - btnCancel->mTexH - 10);
break;
}
line = strtok((char*)text.c_str(), c);
mTitleBar.SetTitle(caption);
mMultiLine->Clear();
while (line != NULL)
{
if (align == MessageAlign::Center)
{
AddLine(line, CSurface::HAlignment::Center);
}
else
{
AddLine(line);
}
line = strtok(NULL, c);
}
Show();
}
void CMessageBox::ShowMessage(std::string text, std::string caption, MessageStyle style)
{
ShowMessage(text, caption, style, MessageAlign::Left);
}