Merge pull request #4594 from FernetMenta/paplayer
[xbmc.git] / xbmc / dialogs / GUIDialogBoxBase.cpp
blob4e6f36fb9c3372c76da40f4268b0d790372df34e
1 /*
2 * Copyright (C) 2005-2013 Team XBMC
3 * http://xbmc.org
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "Application.h"
22 #include "GUIDialogBoxBase.h"
23 #include "guilib/LocalizeStrings.h"
24 #include "threads/SingleLock.h"
25 #include "utils/StringUtils.h"
27 using namespace std;
29 #define CONTROL_HEADING 1
30 #define CONTROL_LINES_START 2
31 #define CONTROL_TEXTBOX 9
32 #define CONTROL_CHOICES_START 10
34 CGUIDialogBoxBase::CGUIDialogBoxBase(int id, const CStdString &xmlFile)
35 : CGUIDialog(id, xmlFile)
37 m_bConfirmed = false;
38 m_loadType = KEEP_IN_MEMORY;
39 m_hasTextbox = false;
42 CGUIDialogBoxBase::~CGUIDialogBoxBase(void)
46 bool CGUIDialogBoxBase::OnMessage(CGUIMessage& message)
48 switch ( message.GetMessage() )
50 case GUI_MSG_WINDOW_INIT:
52 CGUIDialog::OnMessage(message);
53 m_bConfirmed = false;
54 return true;
56 break;
58 return CGUIDialog::OnMessage(message);
61 bool CGUIDialogBoxBase::IsConfirmed() const
63 return m_bConfirmed;
66 void CGUIDialogBoxBase::SetHeading(const CVariant& heading)
68 std::string label = GetLocalized(heading);
69 CSingleLock lock(m_section);
70 if (label != m_strHeading)
72 m_strHeading = label;
73 SetInvalid();
77 void CGUIDialogBoxBase::SetLine(unsigned int iLine, const CVariant& line)
79 std::string label = GetLocalized(line);
80 CSingleLock lock(m_section);
81 vector<string> lines = StringUtils::Split(m_text, "\n");
82 if (iLine >= lines.size())
83 lines.resize(iLine+1);
84 lines[iLine] = label;
85 std::string text = StringUtils::Join(lines, "\n");
86 SetText(text);
89 void CGUIDialogBoxBase::SetText(const CVariant& text)
91 std::string label = GetLocalized(text);
92 CSingleLock lock(m_section);
93 StringUtils::Trim(label, "\n");
94 if (label != m_text)
96 m_text = label;
97 SetInvalid();
101 void CGUIDialogBoxBase::SetChoice(int iButton, const CVariant &choice) // iButton == 0 for no, 1 for yes
103 if (iButton < 0 || iButton >= DIALOG_MAX_CHOICES)
104 return;
106 std::string label = GetLocalized(choice);
107 CSingleLock lock(m_section);
108 if (label != m_strChoices[iButton])
110 m_strChoices[iButton] = label;
111 SetInvalid();
115 void CGUIDialogBoxBase::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
117 if (m_bInvalidated)
118 { // take a copy of our labels to save holding the lock for too long
119 string heading, text;
120 vector<string> choices;
121 choices.reserve(DIALOG_MAX_CHOICES);
123 CSingleLock lock(m_section);
124 heading = m_strHeading;
125 text = m_text;
126 for (int i = 0; i < DIALOG_MAX_CHOICES; ++i)
127 choices.push_back(m_strChoices[i]);
129 SET_CONTROL_LABEL(CONTROL_HEADING, heading);
130 if (m_hasTextbox)
132 SET_CONTROL_LABEL(CONTROL_TEXTBOX, text);
134 else
136 vector<string> lines = StringUtils::Split(text, "\n", DIALOG_MAX_LINES);
137 lines.resize(DIALOG_MAX_LINES);
138 for (size_t i = 0 ; i < lines.size(); ++i)
139 SET_CONTROL_LABEL(CONTROL_LINES_START + i, lines[i]);
141 for (size_t i = 0 ; i < choices.size() ; ++i)
142 SET_CONTROL_LABEL(CONTROL_CHOICES_START + i, choices[i]);
144 CGUIDialog::Process(currentTime, dirtyregions);
147 void CGUIDialogBoxBase::OnInitWindow()
149 // set focus to default
150 m_lastControlID = m_defaultControl;
152 m_hasTextbox = false;
153 const CGUIControl *control = GetControl(CONTROL_TEXTBOX);
154 if (control && control->GetControlType() == CGUIControl::GUICONTROL_TEXTBOX)
155 m_hasTextbox = true;
157 // set initial labels
159 CSingleLock lock(m_section);
160 for (int i = 0 ; i < DIALOG_MAX_CHOICES ; ++i)
162 if (m_strChoices[i].empty())
163 m_strChoices[i] = GetDefaultLabel(CONTROL_CHOICES_START + i);
166 CGUIDialog::OnInitWindow();
169 void CGUIDialogBoxBase::OnDeinitWindow(int nextWindowID)
171 // make sure we set default labels for heading, lines and choices
173 CSingleLock lock(m_section);
174 m_strHeading.clear();
175 m_text.clear();
176 for (int i = 0 ; i < DIALOG_MAX_CHOICES ; ++i)
177 m_strChoices[i].clear();
180 CGUIDialog::OnDeinitWindow(nextWindowID);
183 CStdString CGUIDialogBoxBase::GetLocalized(const CVariant &var) const
185 if (var.isString())
186 return var.asString();
187 else if (var.isInteger() && var.asInteger())
188 return g_localizeStrings.Get((uint32_t)var.asInteger());
189 return "";
192 CStdString CGUIDialogBoxBase::GetDefaultLabel(int controlId) const
194 int labelId = GetDefaultLabelID(controlId);
195 return labelId != -1 ? g_localizeStrings.Get(labelId) : "";
198 int CGUIDialogBoxBase::GetDefaultLabelID(int controlId) const
200 return -1;