1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/////////////////////////////////////////////////////////////////////////////
// Name: power.cpp
// Purpose: wxWidgets power management sample
// Author: Vadim Zeitlin
// Created: 2006-05-27
// 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"
#include "wx/log.h"
#endif
#include "wx/textctrl.h"
#include "wx/msgdlg.h"
#include "wx/power.h"
#ifndef wxHAS_IMAGES_IN_RESOURCES
#include "../sample.xpm"
#endif
// ----------------------------------------------------------------------------
// main frame class
// ----------------------------------------------------------------------------
class MyFrame : public wxFrame
{
public:
MyFrame()
: wxFrame(NULL, wxID_ANY, wxT("wxWidgets Power Management Sample"),
wxDefaultPosition, wxSize(500, 200))
{
wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, wxT(""),
wxDefaultPosition, wxDefaultSize,
wxTE_MULTILINE | wxTE_READONLY);
m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(text));
CreateStatusBar();
SetIcon(wxICON(sample));
UpdatePowerSettings(wxPOWER_UNKNOWN, wxBATTERY_UNKNOWN_STATE);
Show();
}
virtual ~MyFrame()
{
delete wxLog::SetActiveTarget(m_logOld);
}
private:
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
void OnSuspending(wxPowerEvent& event)
{
wxLogMessage(wxT("System suspend starting..."));
if ( wxMessageBox(wxT("Veto suspend?"), wxT("Please answer"),
wxYES_NO, this) == wxYES )
{
event.Veto();
wxLogMessage(wxT("Vetoed suspend."));
}
}
void OnSuspended(wxPowerEvent& WXUNUSED(event))
{
wxLogMessage(wxT("System is going to suspend."));
}
void OnSuspendCancel(wxPowerEvent& WXUNUSED(event))
{
wxLogMessage(wxT("System suspend was cancelled."));
}
void OnResume(wxPowerEvent& WXUNUSED(event))
{
wxLogMessage(wxT("System resumed from suspend."));
}
#endif // wxHAS_POWER_EVENTS
void UpdatePowerSettings(wxPowerType powerType, wxBatteryState batteryState)
{
wxString powerStr;
switch ( m_powerType = powerType )
{
case wxPOWER_SOCKET:
powerStr = wxT("wall");
break;
case wxPOWER_BATTERY:
powerStr = wxT("battery");
break;
default:
wxFAIL_MSG(wxT("unknown wxPowerType value"));
// fall through
case wxPOWER_UNKNOWN:
powerStr = wxT("psychic");
break;
}
wxString batteryStr;
switch ( m_batteryState = batteryState )
{
case wxBATTERY_NORMAL_STATE:
batteryStr = wxT("charged");
break;
case wxBATTERY_LOW_STATE:
batteryStr = wxT("low");
break;
case wxBATTERY_CRITICAL_STATE:
batteryStr = wxT("critical");
break;
case wxBATTERY_SHUTDOWN_STATE:
batteryStr = wxT("empty");
break;
default:
wxFAIL_MSG(wxT("unknown wxBatteryState value"));
// fall through
case wxBATTERY_UNKNOWN_STATE:
batteryStr = wxT("unknown");
break;
}
SetStatusText(wxString::Format(
wxT("System is on %s power, battery state is %s"),
powerStr.c_str(),
batteryStr.c_str()));
}
wxPowerType m_powerType;
wxBatteryState m_batteryState;
wxLog *m_logOld;
wxDECLARE_EVENT_TABLE();
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_IDLE(MyFrame::OnIdle)
#ifdef wxHAS_POWER_EVENTS
EVT_POWER_SUSPENDING(MyFrame::OnSuspending)
EVT_POWER_SUSPENDED(MyFrame::OnSuspended)
EVT_POWER_SUSPEND_CANCEL(MyFrame::OnSuspendCancel)
EVT_POWER_RESUME(MyFrame::OnResume)
#endif // wxHAS_POWER_EVENTS
wxEND_EVENT_TABLE()
// ----------------------------------------------------------------------------
// main application class
// ----------------------------------------------------------------------------
class MyApp : public wxApp
{
public:
virtual bool OnInit()
{
new MyFrame;
return true;
}
};
IMPLEMENT_APP(MyApp)
|