Merge pull request #4630 from Red-F/gotham-resume-pvr-lastplayedposition
[vuplus_xbmc] / xbmc / dialogs / GUIDialogBoxBase.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
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.
9  *
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.
14  *
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/>.
18  *
19  */
20
21 #include "Application.h"
22 #include "GUIDialogBoxBase.h"
23 #include "guilib/LocalizeStrings.h"
24 #include "threads/SingleLock.h"
25 #include "utils/StringUtils.h"
26
27 using namespace std;
28
29 #define CONTROL_HEADING 1
30 #define CONTROL_LINES_START 2
31 #define CONTROL_TEXTBOX     9
32 #define CONTROL_CHOICES_START 10
33
34 CGUIDialogBoxBase::CGUIDialogBoxBase(int id, const CStdString &xmlFile)
35     : CGUIDialog(id, xmlFile)
36 {
37   m_bConfirmed = false;
38   m_loadType = KEEP_IN_MEMORY;
39   m_hasTextbox = false;
40 }
41
42 CGUIDialogBoxBase::~CGUIDialogBoxBase(void)
43 {
44 }
45
46 bool CGUIDialogBoxBase::OnMessage(CGUIMessage& message)
47 {
48   switch ( message.GetMessage() )
49   {
50   case GUI_MSG_WINDOW_INIT:
51     {
52       CGUIDialog::OnMessage(message);
53       m_bConfirmed = false;
54       return true;
55     }
56     break;
57   }
58   return CGUIDialog::OnMessage(message);
59 }
60
61 bool CGUIDialogBoxBase::IsConfirmed() const
62 {
63   return m_bConfirmed;
64 }
65
66 void CGUIDialogBoxBase::SetHeading(const CVariant& heading)
67 {
68   std::string label = GetLocalized(heading);
69   CSingleLock lock(m_section);
70   if (label != m_strHeading)
71   {
72     m_strHeading = label;
73     SetInvalid();
74   }
75 }
76
77 void CGUIDialogBoxBase::SetLine(unsigned int iLine, const CVariant& line)
78 {
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);
87 }
88
89 void CGUIDialogBoxBase::SetText(const CVariant& text)
90 {
91   std::string label = GetLocalized(text);
92   CSingleLock lock(m_section);
93   StringUtils::Trim(label, "\n");
94   if (label != m_text)
95   {
96     m_text = label;
97     SetInvalid();
98   }
99 }
100
101 void CGUIDialogBoxBase::SetChoice(int iButton, const CVariant &choice) // iButton == 0 for no, 1 for yes
102 {
103   if (iButton < 0 || iButton >= DIALOG_MAX_CHOICES)
104     return;
105
106   std::string label = GetLocalized(choice);
107   CSingleLock lock(m_section);
108   if (label != m_strChoices[iButton])
109   {
110     m_strChoices[iButton] = label;
111     SetInvalid();
112   }
113 }
114
115 void CGUIDialogBoxBase::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
116 {
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);
122     {
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]);
128     }
129     SET_CONTROL_LABEL(CONTROL_HEADING, heading);
130     if (m_hasTextbox)
131     {
132       SET_CONTROL_LABEL(CONTROL_TEXTBOX, text);
133     }
134     else
135     {
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]);
140     }
141     for (size_t i = 0 ; i < choices.size() ; ++i)
142       SET_CONTROL_LABEL(CONTROL_CHOICES_START + i, choices[i]);
143   }
144   CGUIDialog::Process(currentTime, dirtyregions);
145 }
146
147 void CGUIDialogBoxBase::OnInitWindow()
148 {
149   // set focus to default
150   m_lastControlID = m_defaultControl;
151
152   m_hasTextbox = false;
153   const CGUIControl *control = GetControl(CONTROL_TEXTBOX);
154   if (control && control->GetControlType() == CGUIControl::GUICONTROL_TEXTBOX)
155     m_hasTextbox = true;
156
157   // set initial labels
158   {
159     CSingleLock lock(m_section);
160     for (int i = 0 ; i < DIALOG_MAX_CHOICES ; ++i)
161     {
162       if (m_strChoices[i].empty())
163         m_strChoices[i] = GetDefaultLabel(CONTROL_CHOICES_START + i);
164     }
165   }
166   CGUIDialog::OnInitWindow();
167 }
168
169 void CGUIDialogBoxBase::OnDeinitWindow(int nextWindowID)
170 {
171   // make sure we set default labels for heading, lines and choices
172   {
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();
178   }
179
180   CGUIDialog::OnDeinitWindow(nextWindowID);
181 }
182
183 CStdString CGUIDialogBoxBase::GetLocalized(const CVariant &var) const
184 {
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 "";
190 }
191
192 CStdString CGUIDialogBoxBase::GetDefaultLabel(int controlId) const
193 {
194   int labelId = GetDefaultLabelID(controlId);
195   return labelId != -1 ? g_localizeStrings.Get(labelId) : "";
196 }
197
198 int CGUIDialogBoxBase::GetDefaultLabelID(int controlId) const
199 {
200   return -1;
201 }