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"
31 #include "wx/textctrl.h"
32 #include "wx/msgdlg.h"
36 #if !defined(__WXMSW__) && !defined(__WXPM__)
37 #include "../sample.xpm"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 class MyFrame
: public wxFrame
48 : wxFrame(NULL
, wxID_ANY
, wxT("wxWidgets Power Management Sample"),
49 wxDefaultPosition
, wxSize(500, 200))
51 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, wxT(""),
52 wxDefaultPosition
, wxDefaultSize
,
53 wxTE_MULTILINE
| wxTE_READONLY
);
54 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
58 SetIcon(wxICON(sample
));
60 UpdatePowerSettings(wxPOWER_UNKNOWN
, wxBATTERY_UNKNOWN_STATE
);
67 delete wxLog::SetActiveTarget(m_logOld
);
71 void OnIdle(wxIdleEvent
& WXUNUSED(event
))
73 const wxPowerType powerType
= wxGetPowerType();
74 const wxBatteryState batteryState
= wxGetBatteryState();
75 if ( powerType
!= m_powerType
|| batteryState
!= m_batteryState
)
77 UpdatePowerSettings(powerType
, batteryState
);
81 #ifdef wxHAS_POWER_EVENTS
82 void OnSuspending(wxPowerEvent
& event
)
84 wxLogMessage(wxT("System suspend starting..."));
85 if ( wxMessageBox(wxT("Veto suspend?"), wxT("Please answer"),
86 wxYES_NO
, this) == wxYES
)
89 wxLogMessage(wxT("Vetoed suspend."));
93 void OnSuspended(wxPowerEvent
& WXUNUSED(event
))
95 wxLogMessage(wxT("System is going to suspend."));
98 void OnSuspendCancel(wxPowerEvent
& WXUNUSED(event
))
100 wxLogMessage(wxT("System suspend was cancelled."));
103 void OnResume(wxPowerEvent
& WXUNUSED(event
))
105 wxLogMessage(wxT("System resumed from suspend."));
107 #endif // wxHAS_POWER_EVENTS
110 void UpdatePowerSettings(wxPowerType powerType
, wxBatteryState batteryState
)
113 switch ( m_powerType
= powerType
)
116 powerStr
= wxT("wall");
119 case wxPOWER_BATTERY
:
120 powerStr
= wxT("battery");
124 wxFAIL_MSG(wxT("unknown wxPowerType value"));
127 case wxPOWER_UNKNOWN
:
128 powerStr
= wxT("psychic");
133 switch ( m_batteryState
= batteryState
)
135 case wxBATTERY_NORMAL_STATE
:
136 batteryStr
= wxT("charged");
139 case wxBATTERY_LOW_STATE
:
140 batteryStr
= wxT("low");
143 case wxBATTERY_CRITICAL_STATE
:
144 batteryStr
= wxT("critical");
147 case wxBATTERY_SHUTDOWN_STATE
:
148 batteryStr
= wxT("empty");
152 wxFAIL_MSG(wxT("unknown wxBatteryState value"));
155 case wxBATTERY_UNKNOWN_STATE
:
156 batteryStr
= wxT("unknown");
160 SetStatusText(wxString::Format(
161 wxT("System is on %s power, battery state is %s"),
163 batteryStr
.c_str()));
166 wxPowerType m_powerType
;
167 wxBatteryState m_batteryState
;
171 DECLARE_EVENT_TABLE()
174 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
175 EVT_IDLE(MyFrame::OnIdle
)
177 #ifdef wxHAS_POWER_EVENTS
178 EVT_POWER_SUSPENDING(MyFrame::OnSuspending
)
179 EVT_POWER_SUSPENDED(MyFrame::OnSuspended
)
180 EVT_POWER_SUSPEND_CANCEL(MyFrame::OnSuspendCancel
)
181 EVT_POWER_RESUME(MyFrame::OnResume
)
182 #endif // wxHAS_POWER_EVENTS
185 // ----------------------------------------------------------------------------
186 // main application class
187 // ----------------------------------------------------------------------------
189 class MyApp
: public wxApp
192 virtual bool OnInit()