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