splash.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: splash.cpp
  3. // Purpose: wxSplashScreen sample
  4. // Author: Wlodzimierz ABX Skiba
  5. // Modified by:
  6. // Created: 04/08/2004
  7. // Copyright: (c) Wlodzimierz Skiba
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. // for all others, include the necessary headers (this file is usually all you
  22. // need because it includes almost all "standard" wxWidgets headers)
  23. #ifndef WX_PRECOMP
  24. #include "wx/wx.h"
  25. #endif
  26. #include "wx/image.h"
  27. #include "wx/splash.h"
  28. #include "wx/mediactrl.h"
  29. // ----------------------------------------------------------------------------
  30. // resources
  31. // ----------------------------------------------------------------------------
  32. // the application icon (under Windows and OS/2 it is in resources and even
  33. // though we could still include the XPM here it would be unused)
  34. #ifndef wxHAS_IMAGES_IN_RESOURCES
  35. #include "../sample.xpm"
  36. #endif
  37. // for smartphone, pda and other small screens use resized embedded image
  38. // instead of full colour png dedicated to desktops
  39. #include "mobile.xpm"
  40. // ----------------------------------------------------------------------------
  41. // private classes
  42. // ----------------------------------------------------------------------------
  43. // Define a new application type, each program should derive a class from wxApp
  44. class MyApp : public wxApp
  45. {
  46. public:
  47. // override base class virtuals
  48. // ----------------------------
  49. // this one is called on application startup and is a good place for the app
  50. // initialization (doing it here and not in the ctor allows to have an error
  51. // return: if OnInit() returns false, the application terminates)
  52. virtual bool OnInit();
  53. };
  54. // Define a new frame type: this is going to be our main frame
  55. class MyFrame : public wxFrame
  56. {
  57. public:
  58. // ctor(s)
  59. MyFrame(const wxString& title);
  60. // event handlers (these functions should _not_ be virtual)
  61. void OnQuit(wxCommandEvent& event);
  62. void OnAbout(wxCommandEvent& event);
  63. bool m_isPda;
  64. private:
  65. // any class wishing to process wxWidgets events must use this macro
  66. wxDECLARE_EVENT_TABLE();
  67. };
  68. // ----------------------------------------------------------------------------
  69. // constants
  70. // ----------------------------------------------------------------------------
  71. // IDs for the controls and the menu commands
  72. enum
  73. {
  74. Minimal_Run = wxID_HIGHEST + 1
  75. };
  76. // ----------------------------------------------------------------------------
  77. // event tables and other macros for wxWidgets
  78. // ----------------------------------------------------------------------------
  79. // the event tables connect the wxWidgets events with the functions (event
  80. // handlers) which process them. It can be also done at run-time, but for the
  81. // simple menu events like this the static method is much simpler.
  82. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  83. EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
  84. EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
  85. wxEND_EVENT_TABLE()
  86. // Create a new application object: this macro will allow wxWidgets to create
  87. // the application object during program execution (it's better than using a
  88. // static object for many reasons) and also implements the accessor function
  89. // wxGetApp() which will return the reference of the right type (i.e. MyApp and
  90. // not wxApp)
  91. IMPLEMENT_APP(MyApp)
  92. // ============================================================================
  93. // implementation
  94. // ============================================================================
  95. // ----------------------------------------------------------------------------
  96. // the application class
  97. // ----------------------------------------------------------------------------
  98. // 'Main program' equivalent: the program execution "starts" here
  99. bool MyApp::OnInit()
  100. {
  101. if ( !wxApp::OnInit() )
  102. return false;
  103. wxImage::AddHandler(new wxPNGHandler);
  104. // create the main application window
  105. MyFrame *frame = new MyFrame(wxT("wxSplashScreen sample application"));
  106. wxBitmap bitmap;
  107. if (frame->m_isPda)
  108. bitmap = wxBitmap(mobile_xpm);
  109. bool ok = frame->m_isPda
  110. ? bitmap.IsOk()
  111. : bitmap.LoadFile(wxT("splash.png"), wxBITMAP_TYPE_PNG);
  112. if (ok)
  113. {
  114. new wxSplashScreen(bitmap,
  115. wxSPLASH_CENTRE_ON_SCREEN|wxSPLASH_TIMEOUT,
  116. 6000, frame, wxID_ANY, wxDefaultPosition, wxDefaultSize,
  117. wxSIMPLE_BORDER|wxSTAY_ON_TOP);
  118. }
  119. #if !defined(__WXGTK20__)
  120. // we don't need it at least on wxGTK with GTK+ 2.12.9
  121. wxYield();
  122. #endif
  123. // and show it (the frames, unlike simple controls, are not shown when
  124. // created initially)
  125. frame->Show(true);
  126. // success: wxApp::OnRun() will be called which will enter the main message
  127. // loop and the application will run. If we returned false here, the
  128. // application would exit immediately.
  129. return true;
  130. }
  131. // ----------------------------------------------------------------------------
  132. // main frame
  133. // ----------------------------------------------------------------------------
  134. // frame constructor
  135. MyFrame::MyFrame(const wxString& title)
  136. : wxFrame(NULL, wxID_ANY, title)
  137. {
  138. m_isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
  139. // set the frame icon
  140. SetIcon(wxICON(sample));
  141. #if wxUSE_MENUS
  142. // create a menu bar
  143. wxMenu *menuFile = new wxMenu;
  144. // the "About" item should be in the help menu
  145. wxMenu *helpMenu = new wxMenu;
  146. helpMenu->Append(wxID_ABOUT, wxT("&About\tF1"), wxT("Show about frame"));
  147. menuFile->Append(wxID_EXIT, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
  148. // now append the freshly created menu to the menu bar...
  149. wxMenuBar *menuBar = new wxMenuBar();
  150. menuBar->Append(menuFile, wxT("&File"));
  151. menuBar->Append(helpMenu, wxT("&Help"));
  152. // ... and attach this menu bar to the frame
  153. SetMenuBar(menuBar);
  154. #endif // wxUSE_MENUS
  155. #if wxUSE_STATUSBAR
  156. // create a status bar just for fun (by default with 1 pane only)
  157. CreateStatusBar(2);
  158. SetStatusText(wxT("Welcome to wxWidgets!"));
  159. #endif // wxUSE_STATUSBAR
  160. }
  161. // event handlers
  162. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  163. {
  164. // true is to force the frame to close
  165. Close(true);
  166. }
  167. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  168. {
  169. wxBitmap bitmap;
  170. if (m_isPda) bitmap = wxBitmap(mobile_xpm);
  171. bool ok = m_isPda
  172. ? bitmap.IsOk()
  173. : bitmap.LoadFile(wxT("splash.png"), wxBITMAP_TYPE_PNG);
  174. if (ok)
  175. {
  176. wxImage image = bitmap.ConvertToImage();
  177. // do not scale on already small screens
  178. if (!m_isPda)
  179. image.Rescale( bitmap.GetWidth()/2, bitmap.GetHeight()/2 );
  180. bitmap = wxBitmap(image);
  181. wxSplashScreen *splash = new wxSplashScreen(bitmap,
  182. wxSPLASH_CENTRE_ON_PARENT | wxSPLASH_NO_TIMEOUT,
  183. 0, this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
  184. wxSIMPLE_BORDER|wxSTAY_ON_TOP);
  185. wxWindow *win = splash->GetSplashWindow();
  186. #if wxUSE_MEDIACTRL
  187. wxMediaCtrl *media = new wxMediaCtrl( win, wxID_EXIT, wxT("press.mpg"), wxPoint(2,2));
  188. media->Play();
  189. #else
  190. wxStaticText *text = new wxStaticText( win,
  191. wxID_EXIT,
  192. wxT("click somewhere\non this image"),
  193. wxPoint(m_isPda ? 0 : 13,
  194. m_isPda ? 0 : 11)
  195. );
  196. text->SetBackgroundColour(*wxWHITE);
  197. text->SetForegroundColour(*wxBLACK);
  198. wxFont font = text->GetFont();
  199. font.SetPointSize(2*font.GetPointSize()/3);
  200. text->SetFont(font);
  201. #endif
  202. }
  203. }