Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / samples / power / power.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: power.cpp
3 // Purpose: wxWidgets power management sample
4 // Author: Vadim Zeitlin
5 // Created: 2006-05-27
6 // Copyright: (C) 2006 Vadim Zeitlin <vadim@wxwindows.org>
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ============================================================================
11 // declarations
12 // ============================================================================
13
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/app.h"
26 #include "wx/frame.h"
27 #include "wx/log.h"
28 #endif
29
30 #include "wx/textctrl.h"
31 #include "wx/msgdlg.h"
32
33 #include "wx/power.h"
34
35 #ifndef wxHAS_IMAGES_IN_RESOURCES
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, wxT("wxWidgets Power Management Sample"),
48 wxDefaultPosition, wxSize(500, 200))
49 {
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));
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(wxT("System suspend starting..."));
84 if ( wxMessageBox(wxT("Veto suspend?"), wxT("Please answer"),
85 wxYES_NO, this) == wxYES )
86 {
87 event.Veto();
88 wxLogMessage(wxT("Vetoed suspend."));
89 }
90 }
91
92 void OnSuspended(wxPowerEvent& WXUNUSED(event))
93 {
94 wxLogMessage(wxT("System is going to suspend."));
95 }
96
97 void OnSuspendCancel(wxPowerEvent& WXUNUSED(event))
98 {
99 wxLogMessage(wxT("System suspend was cancelled."));
100 }
101
102 void OnResume(wxPowerEvent& WXUNUSED(event))
103 {
104 wxLogMessage(wxT("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 = wxT("wall");
116 break;
117
118 case wxPOWER_BATTERY:
119 powerStr = wxT("battery");
120 break;
121
122 default:
123 wxFAIL_MSG(wxT("unknown wxPowerType value"));
124 // fall through
125
126 case wxPOWER_UNKNOWN:
127 powerStr = wxT("psychic");
128 break;
129 }
130
131 wxString batteryStr;
132 switch ( m_batteryState = batteryState )
133 {
134 case wxBATTERY_NORMAL_STATE:
135 batteryStr = wxT("charged");
136 break;
137
138 case wxBATTERY_LOW_STATE:
139 batteryStr = wxT("low");
140 break;
141
142 case wxBATTERY_CRITICAL_STATE:
143 batteryStr = wxT("critical");
144 break;
145
146 case wxBATTERY_SHUTDOWN_STATE:
147 batteryStr = wxT("empty");
148 break;
149
150 default:
151 wxFAIL_MSG(wxT("unknown wxBatteryState value"));
152 // fall through
153
154 case wxBATTERY_UNKNOWN_STATE:
155 batteryStr = wxT("unknown");
156 break;
157 }
158
159 SetStatusText(wxString::Format(
160 wxT("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)