/xbmc/dialogs/GUIDialogBoxBase.cpp

http://github.com/xbmc/xbmc · C++ · 186 lines · 155 code · 21 blank · 10 comment · 26 complexity · a61f25e337c9a1f3e824ef20106dce4c MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2018 Team Kodi
  3. * This file is part of Kodi - https://kodi.tv
  4. *
  5. * SPDX-License-Identifier: GPL-2.0-or-later
  6. * See LICENSES/README.md for more information.
  7. */
  8. #include "GUIDialogBoxBase.h"
  9. #include "Application.h"
  10. #include "guilib/LocalizeStrings.h"
  11. #include "threads/SingleLock.h"
  12. #include "utils/StringUtils.h"
  13. #include "utils/Variant.h"
  14. #define CONTROL_HEADING 1
  15. #define CONTROL_LINES_START 2
  16. #define CONTROL_TEXTBOX 9
  17. CGUIDialogBoxBase::CGUIDialogBoxBase(int id, const std::string &xmlFile)
  18. : CGUIDialog(id, xmlFile)
  19. {
  20. m_bConfirmed = false;
  21. m_loadType = KEEP_IN_MEMORY;
  22. m_hasTextbox = false;
  23. }
  24. CGUIDialogBoxBase::~CGUIDialogBoxBase(void) = default;
  25. bool CGUIDialogBoxBase::OnMessage(CGUIMessage& message)
  26. {
  27. switch ( message.GetMessage() )
  28. {
  29. case GUI_MSG_WINDOW_INIT:
  30. {
  31. CGUIDialog::OnMessage(message);
  32. m_bConfirmed = false;
  33. return true;
  34. }
  35. break;
  36. }
  37. return CGUIDialog::OnMessage(message);
  38. }
  39. bool CGUIDialogBoxBase::IsConfirmed() const
  40. {
  41. return m_bConfirmed;
  42. }
  43. void CGUIDialogBoxBase::SetHeading(CVariant heading)
  44. {
  45. std::string label = GetLocalized(heading);
  46. CSingleLock lock(m_section);
  47. if (label != m_strHeading)
  48. {
  49. m_strHeading = label;
  50. SetInvalid();
  51. }
  52. }
  53. void CGUIDialogBoxBase::SetLine(unsigned int iLine, CVariant line)
  54. {
  55. std::string label = GetLocalized(line);
  56. CSingleLock lock(m_section);
  57. std::vector<std::string> lines = StringUtils::Split(m_text, '\n');
  58. if (iLine >= lines.size())
  59. lines.resize(iLine+1);
  60. lines[iLine] = label;
  61. std::string text = StringUtils::Join(lines, "\n");
  62. SetText(text);
  63. }
  64. void CGUIDialogBoxBase::SetText(CVariant text)
  65. {
  66. std::string label = GetLocalized(text);
  67. CSingleLock lock(m_section);
  68. StringUtils::Trim(label, "\n");
  69. if (label != m_text)
  70. {
  71. m_text = label;
  72. SetInvalid();
  73. }
  74. }
  75. void CGUIDialogBoxBase::SetChoice(int iButton, const CVariant &choice) // iButton == 0 for no, 1 for yes
  76. {
  77. if (iButton < 0 || iButton >= DIALOG_MAX_CHOICES)
  78. return;
  79. std::string label = GetLocalized(choice);
  80. CSingleLock lock(m_section);
  81. if (label != m_strChoices[iButton])
  82. {
  83. m_strChoices[iButton] = label;
  84. SetInvalid();
  85. }
  86. }
  87. void CGUIDialogBoxBase::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
  88. {
  89. if (m_bInvalidated)
  90. { // take a copy of our labels to save holding the lock for too long
  91. std::string heading, text;
  92. std::vector<std::string> choices;
  93. choices.reserve(DIALOG_MAX_CHOICES);
  94. {
  95. CSingleLock lock(m_section);
  96. heading = m_strHeading;
  97. text = m_text;
  98. for (const std::string& choice : m_strChoices)
  99. choices.push_back(choice);
  100. }
  101. SET_CONTROL_LABEL(CONTROL_HEADING, heading);
  102. if (m_hasTextbox)
  103. {
  104. SET_CONTROL_LABEL(CONTROL_TEXTBOX, text);
  105. }
  106. else
  107. {
  108. std::vector<std::string> lines = StringUtils::Split(text, "\n", DIALOG_MAX_LINES);
  109. lines.resize(DIALOG_MAX_LINES);
  110. for (size_t i = 0 ; i < lines.size(); ++i)
  111. SET_CONTROL_LABEL(CONTROL_LINES_START + i, lines[i]);
  112. }
  113. for (size_t i = 0 ; i < choices.size() ; ++i)
  114. SET_CONTROL_LABEL(CONTROL_CHOICES_START + i, choices[i]);
  115. }
  116. CGUIDialog::Process(currentTime, dirtyregions);
  117. }
  118. void CGUIDialogBoxBase::OnInitWindow()
  119. {
  120. // set focus to default
  121. m_lastControlID = m_defaultControl;
  122. m_hasTextbox = false;
  123. const CGUIControl *control = GetControl(CONTROL_TEXTBOX);
  124. if (control && control->GetControlType() == CGUIControl::GUICONTROL_TEXTBOX)
  125. m_hasTextbox = true;
  126. // set initial labels
  127. {
  128. CSingleLock lock(m_section);
  129. for (int i = 0 ; i < DIALOG_MAX_CHOICES ; ++i)
  130. {
  131. if (m_strChoices[i].empty())
  132. m_strChoices[i] = GetDefaultLabel(CONTROL_CHOICES_START + i);
  133. }
  134. }
  135. CGUIDialog::OnInitWindow();
  136. }
  137. void CGUIDialogBoxBase::OnDeinitWindow(int nextWindowID)
  138. {
  139. // make sure we set default labels for heading, lines and choices
  140. {
  141. CSingleLock lock(m_section);
  142. m_strHeading.clear();
  143. m_text.clear();
  144. for (std::string& choice : m_strChoices)
  145. choice.clear();
  146. }
  147. CGUIDialog::OnDeinitWindow(nextWindowID);
  148. }
  149. std::string CGUIDialogBoxBase::GetLocalized(const CVariant &var) const
  150. {
  151. if (var.isString())
  152. return var.asString();
  153. else if (var.isInteger() && var.asInteger())
  154. return g_localizeStrings.Get((uint32_t)var.asInteger());
  155. return "";
  156. }
  157. std::string CGUIDialogBoxBase::GetDefaultLabel(int controlId) const
  158. {
  159. int labelId = GetDefaultLabelID(controlId);
  160. return labelId != -1 ? g_localizeStrings.Get(labelId) : "";
  161. }
  162. int CGUIDialogBoxBase::GetDefaultLabelID(int controlId) const
  163. {
  164. return -1;
  165. }