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 "GUIDialogSlider.h"
10 
11 #include "ServiceBroker.h"
12 #include "guilib/GUIComponent.h"
13 #include "guilib/GUISliderControl.h"
14 #include "guilib/GUIWindowManager.h"
15 #include "guilib/LocalizeStrings.h"
16 #include "input/Key.h"
17 
18 #define CONTROL_HEADING 10
19 #define CONTROL_SLIDER  11
20 #define CONTROL_LABEL   12
21 
CGUIDialogSlider(void)22 CGUIDialogSlider::CGUIDialogSlider(void)
23     : CGUIDialog(WINDOW_DIALOG_SLIDER, "DialogSlider.xml")
24 {
25   m_callback = NULL;
26   m_callbackData = NULL;
27   m_loadType = KEEP_IN_MEMORY;
28 }
29 
30 CGUIDialogSlider::~CGUIDialogSlider(void) = default;
31 
OnAction(const CAction & action)32 bool CGUIDialogSlider::OnAction(const CAction &action)
33 {
34   if (action.GetID() == ACTION_SELECT_ITEM)
35   {
36     Close();
37     return true;
38   }
39   return CGUIDialog::OnAction(action);
40 }
41 
OnMessage(CGUIMessage & message)42 bool CGUIDialogSlider::OnMessage(CGUIMessage& message)
43 {
44   switch ( message.GetMessage() )
45   {
46   case GUI_MSG_CLICKED:
47     if (message.GetSenderId() == CONTROL_SLIDER)
48     {
49       CGUISliderControl *slider = dynamic_cast<CGUISliderControl *>(GetControl(CONTROL_SLIDER));
50       if (slider && m_callback)
51       {
52         m_callback->OnSliderChange(m_callbackData, slider);
53         SET_CONTROL_LABEL(CONTROL_LABEL, slider->GetDescription());
54       }
55     }
56     break;
57   case GUI_MSG_WINDOW_DEINIT:
58     m_callback = NULL;
59     m_callbackData = NULL;
60     break;
61   }
62   return CGUIDialog::OnMessage(message);
63 }
64 
SetSlider(const std::string & label,float value,float min,float delta,float max,ISliderCallback * callback,void * callbackData)65 void CGUIDialogSlider::SetSlider(const std::string &label, float value, float min, float delta, float max, ISliderCallback *callback, void *callbackData)
66 {
67   SET_CONTROL_LABEL(CONTROL_HEADING, label);
68   CGUISliderControl *slider = dynamic_cast<CGUISliderControl *>(GetControl(CONTROL_SLIDER));
69   m_callback = callback;
70   m_callbackData = callbackData;
71   if (slider)
72   {
73     slider->SetType(SLIDER_CONTROL_TYPE_FLOAT);
74     slider->SetFloatRange(min, max);
75     slider->SetFloatInterval(delta);
76     slider->SetFloatValue(value);
77     if (m_callback)
78     {
79       m_callback->OnSliderChange(m_callbackData, slider);
80       SET_CONTROL_LABEL(CONTROL_LABEL, slider->GetDescription());
81     }
82   }
83 }
84 
OnWindowLoaded()85 void CGUIDialogSlider::OnWindowLoaded()
86 {
87   // ensure our callbacks are NULL, incase we were loaded via some non-standard means
88   m_callback = NULL;
89   m_callbackData = NULL;
90   CGUIDialog::OnWindowLoaded();
91 }
92 
SetModalityType(DialogModalityType type)93 void CGUIDialogSlider::SetModalityType(DialogModalityType type)
94 {
95   m_modalityType = type;
96 }
97 
ShowAndGetInput(const std::string & label,float value,float min,float delta,float max,ISliderCallback * callback,void * callbackData)98 void CGUIDialogSlider::ShowAndGetInput(const std::string &label, float value, float min, float delta, float max, ISliderCallback *callback, void *callbackData)
99 {
100   // grab the slider dialog
101   CGUIDialogSlider *slider = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogSlider>(WINDOW_DIALOG_SLIDER);
102   if (!slider)
103     return;
104 
105   // set the label and value
106   slider->Initialize();
107   slider->SetSlider(label, value, min, delta, max, callback, callbackData);
108   slider->SetModalityType(DialogModalityType::MODAL);
109   slider->Open();
110 }
111 
Display(int label,float value,float min,float delta,float max,ISliderCallback * callback)112 void CGUIDialogSlider::Display(int label, float value, float min, float delta, float max, ISliderCallback *callback)
113 {
114   // grab the slider dialog
115   CGUIDialogSlider *slider = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogSlider>(WINDOW_DIALOG_SLIDER);
116   if (!slider)
117     return;
118 
119   // set the label and value
120   slider->Initialize();
121   slider->SetAutoClose(1000);
122   slider->SetSlider(g_localizeStrings.Get(label), value, min, delta, max, callback, NULL);
123   slider->SetModalityType(DialogModalityType::MODELESS);
124   slider->Open();
125 }
126