1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets power management sample
4 // Author: Vadim Zeitlin
7 // Copyright: (C) 2006 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
30 #include "wx/textctrl.h"
31 #include "wx/msgdlg.h"
35 #if !defined(__WXMSW__) && !defined(__WXPM__)
36 #include "../sample.xpm"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 class MyFrame
: public wxFrame
47 : wxFrame(NULL
, wxID_ANY
, _T("wxWidgets Power Management Sample"),
48 wxDefaultPosition
, wxSize(500, 200))
50 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, _T(""),
51 wxDefaultPosition
, wxDefaultSize
,
52 wxTE_MULTILINE
| wxTE_READONLY
);
53 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
57 SetIcon(wxICON(sample
));
59 UpdatePowerSettings(wxPOWER_UNKNOWN
, wxBATTERY_UNKNOWN_STATE
);
66 delete wxLog::SetActiveTarget(m_logOld
);
70 void OnIdle(wxIdleEvent
& WXUNUSED(event
))
72 const wxPowerType powerType
= wxGetPowerType();
73 const wxBatteryState batteryState
= wxGetBatteryState();
74 if ( powerType
!= m_powerType
|| batteryState
!= m_batteryState
)
76 UpdatePowerSettings(powerType
, batteryState
);
80 #ifdef wxHAS_POWER_EVENTS
81 void OnSuspending(wxPowerEvent
& event
)
83 wxLogMessage(_T("System suspend starting..."));
84 if ( wxMessageBox(_T("Veto suspend?"), _T("Please answer"),
85 wxYES_NO
, this) == wxYES
)
88 wxLogMessage(_T("Vetoed suspend."));
92 void OnSuspended(wxPowerEvent
& WXUNUSED(event
))
94 wxLogMessage(_T("System is going to suspend."));
97 void OnSuspendCancel(wxPowerEvent
& WXUNUSED(event
))
99 wxLogMessage(_T("System suspend was cancelled."));
102 void OnResume(wxPowerEvent
& WXUNUSED(event
))
104 wxLogMessage(_T("System resumed from suspend."));
106 #endif // wxHAS_POWER_EVENTS
109 void UpdatePowerSettings(wxPowerType powerType
, wxBatteryState batteryState
)
112 switch ( m_powerType
= powerType
)
115 powerStr
= _T("wall");
118 case wxPOWER_BATTERY
:
119 powerStr
= _T("battery");
123 wxFAIL_MSG(_T("unknown wxPowerType value"));
126 case wxPOWER_UNKNOWN
:
127 powerStr
= _T("psychic");
132 switch ( m_batteryState
= batteryState
)
134 case wxBATTERY_NORMAL_STATE
:
135 batteryStr
= _T("charged");
138 case wxBATTERY_LOW_STATE
:
139 batteryStr
= _T("low");
142 case wxBATTERY_CRITICAL_STATE
:
143 batteryStr
= _T("critical");
146 case wxBATTERY_SHUTDOWN_STATE
:
147 batteryStr
= _T("empty");
151 wxFAIL_MSG(_T("unknown wxBatteryState value"));
154 case wxBATTERY_UNKNOWN_STATE
:
155 batteryStr
= _T("unknown");
159 SetStatusText(wxString::Format(
160 _T("System is on %s power, battery state is %s"),
162 batteryStr
.c_str()));
165 wxPowerType m_powerType
;
166 wxBatteryState m_batteryState
;
170 DECLARE_EVENT_TABLE()
173 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
174 EVT_IDLE(MyFrame::OnIdle
)
176 #ifdef wxHAS_POWER_EVENTS
177 EVT_POWER_SUSPENDING(MyFrame::OnSuspending
)
178 EVT_POWER_SUSPENDED(MyFrame::OnSuspended
)
179 EVT_POWER_SUSPEND_CANCEL(MyFrame::OnSuspendCancel
)
180 EVT_POWER_RESUME(MyFrame::OnResume
)
181 #endif // wxHAS_POWER_EVENTS
184 // ----------------------------------------------------------------------------
185 // main application class
186 // ----------------------------------------------------------------------------
188 class MyApp
: public wxApp
191 virtual bool OnInit()