2006-05-27 10:09:40 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: power.cpp
|
|
|
|
// Purpose: wxWidgets power management sample
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2006-05-27
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (C) 2006 Vadim Zeitlin <vadim@wxwindows.org>
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/frame.h"
|
2008-06-21 01:10:40 -04:00
|
|
|
#include "wx/log.h"
|
2006-05-27 10:09:40 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/textctrl.h"
|
|
|
|
#include "wx/msgdlg.h"
|
|
|
|
|
|
|
|
#include "wx/power.h"
|
|
|
|
|
|
|
|
#if !defined(__WXMSW__) && !defined(__WXPM__)
|
|
|
|
#include "../sample.xpm"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// main frame class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MyFrame : public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyFrame()
|
2009-07-23 16:30:22 -04:00
|
|
|
: wxFrame(NULL, wxID_ANY, wxT("wxWidgets Power Management Sample"),
|
2006-05-27 10:09:40 -04:00
|
|
|
wxDefaultPosition, wxSize(500, 200))
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, wxT(""),
|
2006-05-27 10:09:40 -04:00
|
|
|
wxDefaultPosition, wxDefaultSize,
|
|
|
|
wxTE_MULTILINE | wxTE_READONLY);
|
|
|
|
m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(text));
|
|
|
|
|
2006-05-27 10:29:30 -04:00
|
|
|
CreateStatusBar();
|
|
|
|
|
2006-05-27 10:09:40 -04:00
|
|
|
SetIcon(wxICON(sample));
|
|
|
|
|
2006-05-27 10:29:30 -04:00
|
|
|
UpdatePowerSettings(wxPOWER_UNKNOWN, wxBATTERY_UNKNOWN_STATE);
|
|
|
|
|
2006-05-27 10:09:40 -04:00
|
|
|
Show();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MyFrame()
|
|
|
|
{
|
|
|
|
delete wxLog::SetActiveTarget(m_logOld);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2006-05-27 10:29:30 -04:00
|
|
|
void OnIdle(wxIdleEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
const wxPowerType powerType = wxGetPowerType();
|
|
|
|
const wxBatteryState batteryState = wxGetBatteryState();
|
|
|
|
if ( powerType != m_powerType || batteryState != m_batteryState )
|
|
|
|
{
|
|
|
|
UpdatePowerSettings(powerType, batteryState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef wxHAS_POWER_EVENTS
|
2006-05-27 10:09:40 -04:00
|
|
|
void OnSuspending(wxPowerEvent& event)
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxLogMessage(wxT("System suspend starting..."));
|
|
|
|
if ( wxMessageBox(wxT("Veto suspend?"), wxT("Please answer"),
|
2006-05-27 10:09:40 -04:00
|
|
|
wxYES_NO, this) == wxYES )
|
|
|
|
{
|
|
|
|
event.Veto();
|
2009-07-23 16:30:22 -04:00
|
|
|
wxLogMessage(wxT("Vetoed suspend."));
|
2006-05-27 10:09:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnSuspended(wxPowerEvent& WXUNUSED(event))
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxLogMessage(wxT("System is going to suspend."));
|
2006-05-27 10:09:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnSuspendCancel(wxPowerEvent& WXUNUSED(event))
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxLogMessage(wxT("System suspend was cancelled."));
|
2006-05-27 10:09:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnResume(wxPowerEvent& WXUNUSED(event))
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxLogMessage(wxT("System resumed from suspend."));
|
2006-05-27 10:09:40 -04:00
|
|
|
}
|
2006-05-27 10:29:30 -04:00
|
|
|
#endif // wxHAS_POWER_EVENTS
|
|
|
|
|
|
|
|
|
|
|
|
void UpdatePowerSettings(wxPowerType powerType, wxBatteryState batteryState)
|
|
|
|
{
|
|
|
|
wxString powerStr;
|
|
|
|
switch ( m_powerType = powerType )
|
|
|
|
{
|
|
|
|
case wxPOWER_SOCKET:
|
2009-07-23 16:30:22 -04:00
|
|
|
powerStr = wxT("wall");
|
2006-05-27 10:29:30 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case wxPOWER_BATTERY:
|
2009-07-23 16:30:22 -04:00
|
|
|
powerStr = wxT("battery");
|
2006-05-27 10:29:30 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-07-23 16:30:22 -04:00
|
|
|
wxFAIL_MSG(wxT("unknown wxPowerType value"));
|
2006-05-27 10:29:30 -04:00
|
|
|
// fall through
|
|
|
|
|
|
|
|
case wxPOWER_UNKNOWN:
|
2009-07-23 16:30:22 -04:00
|
|
|
powerStr = wxT("psychic");
|
2006-05-27 10:29:30 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString batteryStr;
|
|
|
|
switch ( m_batteryState = batteryState )
|
|
|
|
{
|
|
|
|
case wxBATTERY_NORMAL_STATE:
|
2009-07-23 16:30:22 -04:00
|
|
|
batteryStr = wxT("charged");
|
2006-05-27 10:29:30 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case wxBATTERY_LOW_STATE:
|
2009-07-23 16:30:22 -04:00
|
|
|
batteryStr = wxT("low");
|
2006-05-27 10:29:30 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case wxBATTERY_CRITICAL_STATE:
|
2009-07-23 16:30:22 -04:00
|
|
|
batteryStr = wxT("critical");
|
2006-05-27 10:29:30 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case wxBATTERY_SHUTDOWN_STATE:
|
2009-07-23 16:30:22 -04:00
|
|
|
batteryStr = wxT("empty");
|
2006-05-27 10:29:30 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-07-23 16:30:22 -04:00
|
|
|
wxFAIL_MSG(wxT("unknown wxBatteryState value"));
|
2006-05-27 10:29:30 -04:00
|
|
|
// fall through
|
|
|
|
|
|
|
|
case wxBATTERY_UNKNOWN_STATE:
|
2009-07-23 16:30:22 -04:00
|
|
|
batteryStr = wxT("unknown");
|
2006-05-27 10:29:30 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetStatusText(wxString::Format(
|
2009-07-23 16:30:22 -04:00
|
|
|
wxT("System is on %s power, battery state is %s"),
|
2006-05-27 10:29:30 -04:00
|
|
|
powerStr.c_str(),
|
|
|
|
batteryStr.c_str()));
|
|
|
|
}
|
2006-05-27 10:09:40 -04:00
|
|
|
|
2006-05-27 10:29:30 -04:00
|
|
|
wxPowerType m_powerType;
|
|
|
|
wxBatteryState m_batteryState;
|
2006-05-27 10:09:40 -04:00
|
|
|
|
|
|
|
wxLog *m_logOld;
|
|
|
|
|
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
};
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2006-05-27 10:29:30 -04:00
|
|
|
EVT_IDLE(MyFrame::OnIdle)
|
|
|
|
|
|
|
|
#ifdef wxHAS_POWER_EVENTS
|
2006-05-27 10:09:40 -04:00
|
|
|
EVT_POWER_SUSPENDING(MyFrame::OnSuspending)
|
|
|
|
EVT_POWER_SUSPENDED(MyFrame::OnSuspended)
|
|
|
|
EVT_POWER_SUSPEND_CANCEL(MyFrame::OnSuspendCancel)
|
|
|
|
EVT_POWER_RESUME(MyFrame::OnResume)
|
2006-05-27 10:29:30 -04:00
|
|
|
#endif // wxHAS_POWER_EVENTS
|
2006-05-27 10:09:40 -04:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// main application class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MyApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool OnInit()
|
|
|
|
{
|
|
|
|
new MyFrame;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IMPLEMENT_APP(MyApp)
|