power.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. // declarations
  11. // ============================================================================
  12. // ----------------------------------------------------------------------------
  13. // headers
  14. // ----------------------------------------------------------------------------
  15. #include "wx/wxprec.h"
  16. #ifdef __BORLANDC__
  17. #pragma hdrstop
  18. #endif
  19. #ifndef WX_PRECOMP
  20. #include "wx/app.h"
  21. #include "wx/frame.h"
  22. #include "wx/log.h"
  23. #endif
  24. #include "wx/textctrl.h"
  25. #include "wx/msgdlg.h"
  26. #include "wx/power.h"
  27. #ifndef wxHAS_IMAGES_IN_RESOURCES
  28. #include "../sample.xpm"
  29. #endif
  30. // ----------------------------------------------------------------------------
  31. // main frame class
  32. // ----------------------------------------------------------------------------
  33. class MyFrame : public wxFrame
  34. {
  35. public:
  36. MyFrame()
  37. : wxFrame(NULL, wxID_ANY, wxT("wxWidgets Power Management Sample"),
  38. wxDefaultPosition, wxSize(500, 200))
  39. {
  40. wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, wxT(""),
  41. wxDefaultPosition, wxDefaultSize,
  42. wxTE_MULTILINE | wxTE_READONLY);
  43. m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(text));
  44. CreateStatusBar();
  45. SetIcon(wxICON(sample));
  46. UpdatePowerSettings(wxPOWER_UNKNOWN, wxBATTERY_UNKNOWN_STATE);
  47. Show();
  48. }
  49. virtual ~MyFrame()
  50. {
  51. delete wxLog::SetActiveTarget(m_logOld);
  52. }
  53. private:
  54. void OnIdle(wxIdleEvent& WXUNUSED(event))
  55. {
  56. const wxPowerType powerType = wxGetPowerType();
  57. const wxBatteryState batteryState = wxGetBatteryState();
  58. if ( powerType != m_powerType || batteryState != m_batteryState )
  59. {
  60. UpdatePowerSettings(powerType, batteryState);
  61. }
  62. }
  63. #ifdef wxHAS_POWER_EVENTS
  64. void OnSuspending(wxPowerEvent& event)
  65. {
  66. wxLogMessage(wxT("System suspend starting..."));
  67. if ( wxMessageBox(wxT("Veto suspend?"), wxT("Please answer"),
  68. wxYES_NO, this) == wxYES )
  69. {
  70. event.Veto();
  71. wxLogMessage(wxT("Vetoed suspend."));
  72. }
  73. }
  74. void OnSuspended(wxPowerEvent& WXUNUSED(event))
  75. {
  76. wxLogMessage(wxT("System is going to suspend."));
  77. }
  78. void OnSuspendCancel(wxPowerEvent& WXUNUSED(event))
  79. {
  80. wxLogMessage(wxT("System suspend was cancelled."));
  81. }
  82. void OnResume(wxPowerEvent& WXUNUSED(event))
  83. {
  84. wxLogMessage(wxT("System resumed from suspend."));
  85. }
  86. #endif // wxHAS_POWER_EVENTS
  87. void UpdatePowerSettings(wxPowerType powerType, wxBatteryState batteryState)
  88. {
  89. wxString powerStr;
  90. switch ( m_powerType = powerType )
  91. {
  92. case wxPOWER_SOCKET:
  93. powerStr = wxT("wall");
  94. break;
  95. case wxPOWER_BATTERY:
  96. powerStr = wxT("battery");
  97. break;
  98. default:
  99. wxFAIL_MSG(wxT("unknown wxPowerType value"));
  100. // fall through
  101. case wxPOWER_UNKNOWN:
  102. powerStr = wxT("psychic");
  103. break;
  104. }
  105. wxString batteryStr;
  106. switch ( m_batteryState = batteryState )
  107. {
  108. case wxBATTERY_NORMAL_STATE:
  109. batteryStr = wxT("charged");
  110. break;
  111. case wxBATTERY_LOW_STATE:
  112. batteryStr = wxT("low");
  113. break;
  114. case wxBATTERY_CRITICAL_STATE:
  115. batteryStr = wxT("critical");
  116. break;
  117. case wxBATTERY_SHUTDOWN_STATE:
  118. batteryStr = wxT("empty");
  119. break;
  120. default:
  121. wxFAIL_MSG(wxT("unknown wxBatteryState value"));
  122. // fall through
  123. case wxBATTERY_UNKNOWN_STATE:
  124. batteryStr = wxT("unknown");
  125. break;
  126. }
  127. SetStatusText(wxString::Format(
  128. wxT("System is on %s power, battery state is %s"),
  129. powerStr.c_str(),
  130. batteryStr.c_str()));
  131. }
  132. wxPowerType m_powerType;
  133. wxBatteryState m_batteryState;
  134. wxLog *m_logOld;
  135. wxDECLARE_EVENT_TABLE();
  136. };
  137. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  138. EVT_IDLE(MyFrame::OnIdle)
  139. #ifdef wxHAS_POWER_EVENTS
  140. EVT_POWER_SUSPENDING(MyFrame::OnSuspending)
  141. EVT_POWER_SUSPENDED(MyFrame::OnSuspended)
  142. EVT_POWER_SUSPEND_CANCEL(MyFrame::OnSuspendCancel)
  143. EVT_POWER_RESUME(MyFrame::OnResume)
  144. #endif // wxHAS_POWER_EVENTS
  145. wxEND_EVENT_TABLE()
  146. // ----------------------------------------------------------------------------
  147. // main application class
  148. // ----------------------------------------------------------------------------
  149. class MyApp : public wxApp
  150. {
  151. public:
  152. virtual bool OnInit()
  153. {
  154. new MyFrame;
  155. return true;
  156. }
  157. };
  158. IMPLEMENT_APP(MyApp)