Merge pull request #22816 from CastagnaIT/fix_tx3g
[xbmc.git] / xbmc / dialogs / GUIDialogYesNo.cpp
blob56709c3024f57531d584877ae8b3c0cf8180245d
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "GUIDialogYesNo.h"
11 #include "ServiceBroker.h"
12 #include "guilib/GUIComponent.h"
13 #include "guilib/GUIWindowManager.h"
14 #include "input/Key.h"
15 #include "messaging/helpers/DialogHelper.h"
17 CGUIDialogYesNo::CGUIDialogYesNo(int overrideId /* = -1 */)
18 : CGUIDialogBoxBase(overrideId == -1 ? WINDOW_DIALOG_YES_NO : overrideId, "DialogConfirm.xml")
20 Reset();
23 CGUIDialogYesNo::~CGUIDialogYesNo() = default;
25 bool CGUIDialogYesNo::OnMessage(CGUIMessage& message)
27 switch ( message.GetMessage() )
29 case GUI_MSG_CLICKED:
31 int iControl = message.GetSenderId();
32 int iAction = message.GetParam1();
33 if (true || ACTION_SELECT_ITEM == iAction)
35 if (iControl == CONTROL_NO_BUTTON)
37 m_bConfirmed = false;
38 Close();
39 return true;
41 if (iControl == CONTROL_YES_BUTTON)
43 m_bConfirmed = true;
44 Close();
45 return true;
47 if (iControl == CONTROL_CUSTOM_BUTTON)
49 m_bConfirmed = false;
50 m_bCustom = true;
51 Close();
52 return true;
56 break;
58 return CGUIDialogBoxBase::OnMessage(message);
61 bool CGUIDialogYesNo::OnBack(int actionID)
63 m_bCanceled = true;
64 m_bConfirmed = false;
65 m_bCustom = false;
66 return CGUIDialogBoxBase::OnBack(actionID);
69 void CGUIDialogYesNo::OnInitWindow()
71 if (!m_strChoices[2].empty())
72 SET_CONTROL_VISIBLE(CONTROL_CUSTOM_BUTTON);
73 else
74 SET_CONTROL_HIDDEN(CONTROL_CUSTOM_BUTTON);
75 SET_CONTROL_HIDDEN(CONTROL_PROGRESS_BAR);
76 SET_CONTROL_FOCUS(m_defaultButtonId, 0);
78 CGUIDialogBoxBase::OnInitWindow();
81 bool CGUIDialogYesNo::ShowAndGetInput(const CVariant& heading,
82 const CVariant& line0,
83 const CVariant& line1,
84 const CVariant& line2,
85 bool& bCanceled)
87 return ShowAndGetInput(heading, line0, line1, line2, bCanceled, "", "", NO_TIMEOUT);
90 bool CGUIDialogYesNo::ShowAndGetInput(const CVariant& heading,
91 const CVariant& line0,
92 const CVariant& line1,
93 const CVariant& line2,
94 const CVariant& noLabel /* = "" */,
95 const CVariant& yesLabel /* = "" */)
97 bool bDummy(false);
98 return ShowAndGetInput(heading, line0, line1, line2, bDummy, noLabel, yesLabel, NO_TIMEOUT);
101 bool CGUIDialogYesNo::ShowAndGetInput(const CVariant& heading,
102 const CVariant& line0,
103 const CVariant& line1,
104 const CVariant& line2,
105 bool& bCanceled,
106 const CVariant& noLabel,
107 const CVariant& yesLabel,
108 unsigned int autoCloseTime)
110 CGUIDialogYesNo *dialog = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogYesNo>(WINDOW_DIALOG_YES_NO);
111 if (!dialog)
112 return false;
114 dialog->SetHeading(heading);
115 dialog->SetLine(0, line0);
116 dialog->SetLine(1, line1);
117 dialog->SetLine(2, line2);
118 if (autoCloseTime)
119 dialog->SetAutoClose(autoCloseTime);
120 dialog->SetChoice(0, !noLabel.empty() ? noLabel : 106);
121 dialog->SetChoice(1, !yesLabel.empty() ? yesLabel : 107);
122 dialog->SetChoice(2, "");
123 dialog->m_bCanceled = false;
124 dialog->m_defaultButtonId = CONTROL_NO_BUTTON;
125 dialog->Open();
127 bCanceled = dialog->m_bCanceled;
128 return (dialog->IsConfirmed()) ? true : false;
131 bool CGUIDialogYesNo::ShowAndGetInput(const CVariant& heading, const CVariant& text)
133 bool bDummy(false);
134 return ShowAndGetInput(heading, text, "", "", bDummy);
137 bool CGUIDialogYesNo::ShowAndGetInput(const CVariant& heading,
138 const CVariant& text,
139 bool& bCanceled,
140 const CVariant& noLabel /* = "" */,
141 const CVariant& yesLabel /* = "" */,
142 unsigned int autoCloseTime,
143 int defaultButtonId /* = CONTROL_NO_BUTTON */)
145 int result =
146 ShowAndGetInput(heading, text, noLabel, yesLabel, "", autoCloseTime, defaultButtonId);
148 bCanceled = result == -1;
149 return result == 1;
152 void CGUIDialogYesNo::Reset()
154 m_bConfirmed = false;
155 m_bCanceled = false;
156 m_bCustom = false;
157 m_bAutoClosed = false;
158 m_defaultButtonId = CONTROL_NO_BUTTON;
161 int CGUIDialogYesNo::GetResult() const
163 if (m_bCanceled)
164 return -1;
165 else if (m_bCustom)
166 return 2;
167 else if (IsConfirmed())
168 return 1;
169 else
170 return 0;
173 int CGUIDialogYesNo::ShowAndGetInput(const CVariant& heading,
174 const CVariant& text,
175 const CVariant& noLabel,
176 const CVariant& yesLabel,
177 const CVariant& customLabel,
178 unsigned int autoCloseTime,
179 int defaultButtonId /* = CONTROL_NO_BUTTON */)
181 CGUIDialogYesNo *dialog = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogYesNo>(WINDOW_DIALOG_YES_NO);
182 if (!dialog)
183 return false;
185 dialog->SetHeading(heading);
186 dialog->SetText(text);
187 if (autoCloseTime > 0)
188 dialog->SetAutoClose(autoCloseTime);
189 dialog->m_bCanceled = false;
190 dialog->m_bCustom = false;
191 dialog->m_defaultButtonId = defaultButtonId;
192 dialog->SetChoice(0, !noLabel.empty() ? noLabel : 106);
193 dialog->SetChoice(1, !yesLabel.empty() ? yesLabel : 107);
194 dialog->SetChoice(2, customLabel); // Button only visible when label is not empty
195 dialog->Open();
197 return dialog->GetResult();
200 int CGUIDialogYesNo::ShowAndGetInput(const KODI::MESSAGING::HELPERS::DialogYesNoMessage& options)
202 //Set default yes/no labels, these might be overwritten further down if specified
203 //by the caller
204 SetChoice(0, 106);
205 SetChoice(1, 107);
206 SetChoice(2, "");
207 if (!options.heading.isNull())
208 SetHeading(options.heading);
209 if (!options.text.isNull())
210 SetText(options.text);
211 if (!options.noLabel.isNull())
212 SetChoice(0, options.noLabel);
213 if (!options.yesLabel.isNull())
214 SetChoice(1, options.yesLabel);
215 if (!options.customLabel.isNull())
216 SetChoice(2, options.customLabel);
217 if (options.autoclose > 0)
218 SetAutoClose(options.autoclose);
219 m_bCanceled = false;
220 m_bCustom = false;
221 m_defaultButtonId = CONTROL_NO_BUTTON;
223 for (size_t i = 0; i < 3; ++i)
225 if (!options.lines[i].isNull())
226 SetLine(i, options.lines[i]);
229 Open();
231 return GetResult();
234 int CGUIDialogYesNo::GetDefaultLabelID(int controlId) const
236 if (controlId == CONTROL_NO_BUTTON)
237 return 106;
238 else if (controlId == CONTROL_YES_BUTTON)
239 return 107;
240 else if (controlId == CONTROL_CUSTOM_BUTTON)
241 return -1;
242 return CGUIDialogBoxBase::GetDefaultLabelID(controlId);