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