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