]> git.saurik.com Git - wxWidgets.git/blame - samples/power/power.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / samples / power / power.cpp
CommitLineData
355debca
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: power.cpp
3// Purpose: wxWidgets power management sample
4// Author: Vadim Zeitlin
5// Created: 2006-05-27
355debca
VZ
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"
5d4cca7f 27 #include "wx/log.h"
355debca
VZ
28#endif
29
30#include "wx/textctrl.h"
31#include "wx/msgdlg.h"
32
33#include "wx/power.h"
34
e7092398 35#ifndef wxHAS_IMAGES_IN_RESOURCES
355debca
VZ
36 #include "../sample.xpm"
37#endif
38
39// ----------------------------------------------------------------------------
40// main frame class
41// ----------------------------------------------------------------------------
42
43class MyFrame : public wxFrame
44{
45public:
46 MyFrame()
9a83f860 47 : wxFrame(NULL, wxID_ANY, wxT("wxWidgets Power Management Sample"),
355debca
VZ
48 wxDefaultPosition, wxSize(500, 200))
49 {
9a83f860 50 wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, wxT(""),
355debca
VZ
51 wxDefaultPosition, wxDefaultSize,
52 wxTE_MULTILINE | wxTE_READONLY);
53 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(text));
54
3032b7b5
VZ
55 CreateStatusBar();
56
355debca
VZ
57 SetIcon(wxICON(sample));
58
3032b7b5
VZ
59 UpdatePowerSettings(wxPOWER_UNKNOWN, wxBATTERY_UNKNOWN_STATE);
60
355debca
VZ
61 Show();
62 }
63
64 virtual ~MyFrame()
65 {
66 delete wxLog::SetActiveTarget(m_logOld);
67 }
68
69private:
3032b7b5
VZ
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
355debca
VZ
81 void OnSuspending(wxPowerEvent& event)
82 {
9a83f860
VZ
83 wxLogMessage(wxT("System suspend starting..."));
84 if ( wxMessageBox(wxT("Veto suspend?"), wxT("Please answer"),
355debca
VZ
85 wxYES_NO, this) == wxYES )
86 {
87 event.Veto();
9a83f860 88 wxLogMessage(wxT("Vetoed suspend."));
355debca
VZ
89 }
90 }
91
92 void OnSuspended(wxPowerEvent& WXUNUSED(event))
93 {
9a83f860 94 wxLogMessage(wxT("System is going to suspend."));
355debca
VZ
95 }
96
97 void OnSuspendCancel(wxPowerEvent& WXUNUSED(event))
98 {
9a83f860 99 wxLogMessage(wxT("System suspend was cancelled."));
355debca
VZ
100 }
101
102 void OnResume(wxPowerEvent& WXUNUSED(event))
103 {
9a83f860 104 wxLogMessage(wxT("System resumed from suspend."));
355debca 105 }
3032b7b5
VZ
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:
9a83f860 115 powerStr = wxT("wall");
3032b7b5
VZ
116 break;
117
118 case wxPOWER_BATTERY:
9a83f860 119 powerStr = wxT("battery");
3032b7b5
VZ
120 break;
121
122 default:
9a83f860 123 wxFAIL_MSG(wxT("unknown wxPowerType value"));
3032b7b5
VZ
124 // fall through
125
126 case wxPOWER_UNKNOWN:
9a83f860 127 powerStr = wxT("psychic");
3032b7b5
VZ
128 break;
129 }
130
131 wxString batteryStr;
132 switch ( m_batteryState = batteryState )
133 {
134 case wxBATTERY_NORMAL_STATE:
9a83f860 135 batteryStr = wxT("charged");
3032b7b5
VZ
136 break;
137
138 case wxBATTERY_LOW_STATE:
9a83f860 139 batteryStr = wxT("low");
3032b7b5
VZ
140 break;
141
142 case wxBATTERY_CRITICAL_STATE:
9a83f860 143 batteryStr = wxT("critical");
3032b7b5
VZ
144 break;
145
146 case wxBATTERY_SHUTDOWN_STATE:
9a83f860 147 batteryStr = wxT("empty");
3032b7b5
VZ
148 break;
149
150 default:
9a83f860 151 wxFAIL_MSG(wxT("unknown wxBatteryState value"));
3032b7b5
VZ
152 // fall through
153
154 case wxBATTERY_UNKNOWN_STATE:
9a83f860 155 batteryStr = wxT("unknown");
3032b7b5
VZ
156 break;
157 }
158
159 SetStatusText(wxString::Format(
9a83f860 160 wxT("System is on %s power, battery state is %s"),
3032b7b5
VZ
161 powerStr.c_str(),
162 batteryStr.c_str()));
163 }
355debca 164
3032b7b5
VZ
165 wxPowerType m_powerType;
166 wxBatteryState m_batteryState;
355debca
VZ
167
168 wxLog *m_logOld;
169
170 DECLARE_EVENT_TABLE()
171};
172
173BEGIN_EVENT_TABLE(MyFrame, wxFrame)
3032b7b5
VZ
174 EVT_IDLE(MyFrame::OnIdle)
175
176#ifdef wxHAS_POWER_EVENTS
355debca
VZ
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)
3032b7b5 181#endif // wxHAS_POWER_EVENTS
355debca
VZ
182END_EVENT_TABLE()
183
184// ----------------------------------------------------------------------------
185// main application class
186// ----------------------------------------------------------------------------
187
188class MyApp : public wxApp
189{
190public:
191 virtual bool OnInit()
192 {
193 new MyFrame;
194
195 return true;
196 }
197};
198
199IMPLEMENT_APP(MyApp)