PageRenderTime 48ms CodeModel.GetById 16ms app.highlight 26ms RepoModel.GetById 2ms app.codeStats 0ms

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