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