2 * Copyright (C) 2005-2013 Team XBMC
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)
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 "GUIDialogProgress.h"
22 #include "guilib/GUIProgressControl.h"
23 #include "Application.h"
24 #include "GUIInfoManager.h"
25 #include "guilib/GUIWindowManager.h"
26 #include "guilib/LocalizeStrings.h"
27 #include "threads/SingleLock.h"
28 #include "utils/log.h"
32 #define CONTROL_CANCEL_BUTTON 10
33 #define CONTROL_PROGRESS_BAR 20
35 CGUIDialogProgress::CGUIDialogProgress(void)
36 : CGUIDialogBoxBase(WINDOW_DIALOG_PROGRESS, "DialogProgress.xml")
45 CGUIDialogProgress::~CGUIDialogProgress(void)
50 void CGUIDialogProgress::SetCanCancel(bool bCanCancel)
52 m_bCanCancel = bCanCancel;
53 CGUIMessage msg(bCanCancel ? GUI_MSG_VISIBLE : GUI_MSG_HIDDEN, GetID(), CONTROL_CANCEL_BUTTON);
54 CSingleTryLock tryLock(g_graphicsContext);
58 g_windowManager.SendThreadMessage(msg, GetID());
61 void CGUIDialogProgress::StartModal()
63 CSingleLock lock(g_graphicsContext);
65 CLog::Log(LOGDEBUG, "DialogProgress::StartModal called %s", m_active ? "(already running)!" : "");
68 // set running before it's routed, else the auto-show code
69 // could show it as well if we are in a different thread from
70 // the main rendering thread (this should really be handled via
71 // a thread message though IMO)
75 g_windowManager.RouteToWindow(this);
77 // active this window...
78 CGUIMessage msg(GUI_MSG_WINDOW_INIT, 0, 0);
80 ShowProgressBar(false);
84 while (m_active && IsAnimating(ANIM_TYPE_WINDOW_OPEN))
87 // we should have rendered at least once by now - if we haven't, then
88 // we must be running from fullscreen video or similar where the
89 // calling thread handles rendering (ie not main app thread) but
90 // is waiting on this routine before rendering begins
96 void CGUIDialogProgress::Progress()
100 g_windowManager.ProcessRenderLoop();
104 void CGUIDialogProgress::ProgressKeys()
108 g_application.FrameMove(true);
112 bool CGUIDialogProgress::OnMessage(CGUIMessage& message)
114 switch ( message.GetMessage() )
117 case GUI_MSG_WINDOW_DEINIT:
121 case GUI_MSG_CLICKED:
123 int iControl = message.GetSenderId();
124 if (iControl == CONTROL_CANCEL_BUTTON && m_bCanCancel && !m_bCanceled)
126 string strHeading = m_strHeading;
127 strHeading.append(" : ");
128 strHeading.append(g_localizeStrings.Get(16024));
129 CGUIDialogBoxBase::SetHeading(strHeading);
136 return CGUIDialog::OnMessage(message);
139 bool CGUIDialogProgress::OnBack(int actionID)
149 void CGUIDialogProgress::OnWindowLoaded()
151 CGUIDialog::OnWindowLoaded();
152 const CGUIControl *control = GetControl(CONTROL_PROGRESS_BAR);
153 if (control && control->GetControlType() == CGUIControl::GUICONTROL_PROGRESS)
155 // make sure we have the appropriate info set
156 CGUIProgressControl *progress = (CGUIProgressControl *)control;
157 if (!progress->GetInfo())
158 progress->SetInfo(SYSTEM_PROGRESS_BAR);
162 void CGUIDialogProgress::SetPercentage(int iPercentage)
164 if (iPercentage < 0) iPercentage = 0;
165 if (iPercentage > 100) iPercentage = 100;
167 m_percentage = iPercentage;
170 void CGUIDialogProgress::SetProgressMax(int iMax)
176 void CGUIDialogProgress::SetProgressAdvance(int nSteps/*=1*/)
180 if (m_iCurrent>m_iMax)
183 SetPercentage((m_iCurrent*100)/m_iMax);
186 bool CGUIDialogProgress::Abort()
188 return m_active ? m_bCanceled : false;
191 void CGUIDialogProgress::ShowProgressBar(bool bOnOff)
193 CGUIMessage msg(bOnOff ? GUI_MSG_VISIBLE : GUI_MSG_HIDDEN, GetID(), CONTROL_PROGRESS_BAR);
194 CSingleTryLock tryLock(g_graphicsContext);
195 if(tryLock.IsOwner())
198 g_windowManager.SendThreadMessage(msg, GetID());
201 int CGUIDialogProgress::GetDefaultLabelID(int controlId) const
203 if (controlId == CONTROL_CANCEL_BUTTON)
205 return CGUIDialogBoxBase::GetDefaultLabelID(controlId);