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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "GUIDialogBoxBase.h"
#include "guilib/GUIMessage.h"
#include "guilib/LocalizeStrings.h"
#include "utils/StringUtils.h"
#include "utils/Variant.h"
#include <mutex>
#define CONTROL_HEADING 1
#define CONTROL_LINES_START 2
#define CONTROL_TEXTBOX 9
CGUIDialogBoxBase::CGUIDialogBoxBase(int id, const std::string &xmlFile)
: CGUIDialog(id, xmlFile)
{
m_bConfirmed = false;
m_loadType = KEEP_IN_MEMORY;
m_hasTextbox = false;
}
CGUIDialogBoxBase::~CGUIDialogBoxBase(void) = default;
bool CGUIDialogBoxBase::OnMessage(CGUIMessage& message)
{
switch ( message.GetMessage() )
{
case GUI_MSG_WINDOW_INIT:
{
CGUIDialog::OnMessage(message);
m_bConfirmed = false;
return true;
}
break;
}
return CGUIDialog::OnMessage(message);
}
bool CGUIDialogBoxBase::IsConfirmed() const
{
return m_bConfirmed;
}
void CGUIDialogBoxBase::SetHeading(const CVariant& heading)
{
std::string label = GetLocalized(heading);
std::unique_lock<CCriticalSection> lock(m_section);
if (label != m_strHeading)
{
m_strHeading = label;
SetInvalid();
}
}
void CGUIDialogBoxBase::SetLine(unsigned int iLine, const CVariant& line)
{
std::string label = GetLocalized(line);
std::unique_lock<CCriticalSection> lock(m_section);
std::vector<std::string> lines = StringUtils::Split(m_text, '\n');
if (iLine >= lines.size())
lines.resize(iLine+1);
lines[iLine] = label;
std::string text = StringUtils::Join(lines, "\n");
SetText(text);
}
void CGUIDialogBoxBase::SetText(const CVariant& text)
{
std::string label = GetLocalized(text);
std::unique_lock<CCriticalSection> lock(m_section);
StringUtils::Trim(label, "\n");
if (label != m_text)
{
m_text = label;
SetInvalid();
}
}
void CGUIDialogBoxBase::SetChoice(int iButton, const CVariant &choice) // iButton == 0 for no, 1 for yes
{
if (iButton < 0 || iButton >= DIALOG_MAX_CHOICES)
return;
std::string label = GetLocalized(choice);
std::unique_lock<CCriticalSection> lock(m_section);
if (label != m_strChoices[iButton])
{
m_strChoices[iButton] = label;
SetInvalid();
}
}
void CGUIDialogBoxBase::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
{
if (m_bInvalidated)
{ // take a copy of our labels to save holding the lock for too long
std::string heading, text;
std::vector<std::string> choices;
choices.reserve(DIALOG_MAX_CHOICES);
{
std::unique_lock<CCriticalSection> lock(m_section);
heading = m_strHeading;
text = m_text;
for (const std::string& choice : m_strChoices)
choices.push_back(choice);
}
SET_CONTROL_LABEL(CONTROL_HEADING, heading);
if (m_hasTextbox)
{
SET_CONTROL_LABEL(CONTROL_TEXTBOX, text);
}
else
{
std::vector<std::string> lines = StringUtils::Split(text, "\n", DIALOG_MAX_LINES);
lines.resize(DIALOG_MAX_LINES);
for (size_t i = 0 ; i < lines.size(); ++i)
SET_CONTROL_LABEL(CONTROL_LINES_START + i, lines[i]);
}
for (size_t i = 0 ; i < choices.size() ; ++i)
SET_CONTROL_LABEL(CONTROL_CHOICES_START + i, choices[i]);
}
CGUIDialog::Process(currentTime, dirtyregions);
}
void CGUIDialogBoxBase::OnInitWindow()
{
// set focus to default
m_lastControlID = m_defaultControl;
m_hasTextbox = false;
const CGUIControl *control = GetControl(CONTROL_TEXTBOX);
if (control && control->GetControlType() == CGUIControl::GUICONTROL_TEXTBOX)
m_hasTextbox = true;
// set initial labels
{
std::unique_lock<CCriticalSection> lock(m_section);
for (int i = 0 ; i < DIALOG_MAX_CHOICES ; ++i)
{
if (m_strChoices[i].empty())
m_strChoices[i] = GetDefaultLabel(CONTROL_CHOICES_START + i);
}
}
CGUIDialog::OnInitWindow();
}
void CGUIDialogBoxBase::OnDeinitWindow(int nextWindowID)
{
// make sure we set default labels for heading, lines and choices
{
std::unique_lock<CCriticalSection> lock(m_section);
m_strHeading.clear();
m_text.clear();
for (std::string& choice : m_strChoices)
choice.clear();
}
CGUIDialog::OnDeinitWindow(nextWindowID);
}
std::string CGUIDialogBoxBase::GetLocalized(const CVariant &var) const
{
if (var.isString())
return var.asString();
else if (var.isInteger() && var.asInteger())
return g_localizeStrings.Get((uint32_t)var.asInteger());
return "";
}
std::string CGUIDialogBoxBase::GetDefaultLabel(int controlId) const
{
int labelId = GetDefaultLabelID(controlId);
return labelId != -1 ? g_localizeStrings.Get(labelId) : "";
}
int CGUIDialogBoxBase::GetDefaultLabelID(int controlId) const
{
return -1;
}
|