1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets power management sample
4 // Author: Vadim Zeitlin
6 // Copyright: (C) 2006 Vadim Zeitlin <vadim@wxwindows.org>
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 #include "wx/wxprec.h"
30 #include "wx/textctrl.h"
31 #include "wx/msgdlg.h"
35 #ifndef wxHAS_IMAGES_IN_RESOURCES
36 #include "../sample.xpm"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 class MyFrame
: public wxFrame
47 : wxFrame(NULL
, wxID_ANY
, wxT("wxWidgets Power Management Sample"),
48 wxDefaultPosition
, wxSize(500, 200))
50 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, wxT(""),
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(wxT("System suspend starting..."));
84 if ( wxMessageBox(wxT("Veto suspend?"), wxT("Please answer"),
85 wxYES_NO
, this) == wxYES
)
88 wxLogMessage(wxT("Vetoed suspend."));
92 void OnSuspended(wxPowerEvent
& WXUNUSED(event
))
94 wxLogMessage(wxT("System is going to suspend."));
97 void OnSuspendCancel(wxPowerEvent
& WXUNUSED(event
))
99 wxLogMessage(wxT("System suspend was cancelled."));
102 void OnResume(wxPowerEvent
& WXUNUSED(event
))
104 wxLogMessage(wxT("System resumed from suspend."));
106 #endif // wxHAS_POWER_EVENTS
109 void UpdatePowerSettings(wxPowerType powerType
, wxBatteryState batteryState
)
112 switch ( m_powerType
= powerType
)
115 powerStr
= wxT("wall");
118 case wxPOWER_BATTERY
:
119 powerStr
= wxT("battery");
123 wxFAIL_MSG(wxT("unknown wxPowerType value"));
126 case wxPOWER_UNKNOWN
:
127 powerStr
= wxT("psychic");
132 switch ( m_batteryState
= batteryState
)
134 case wxBATTERY_NORMAL_STATE
:
135 batteryStr
= wxT("charged");
138 case wxBATTERY_LOW_STATE
:
139 batteryStr
= wxT("low");
142 case wxBATTERY_CRITICAL_STATE
:
143 batteryStr
= wxT("critical");
146 case wxBATTERY_SHUTDOWN_STATE
:
147 batteryStr
= wxT("empty");
151 wxFAIL_MSG(wxT("unknown wxBatteryState value"));
154 case wxBATTERY_UNKNOWN_STATE
:
155 batteryStr
= wxT("unknown");
159 SetStatusText(wxString::Format(
160 wxT("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()