Cured a bug in wxSplashScreen whereby frame wasn't sized correctly,
[wxWidgets.git] / src / generic / splash.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: splash.cpp
3 // Purpose: wxSplashScreen class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 28/6/2000
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "splash.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_SPLASH
24
25 #ifndef WX_PRECOMP
26 #include "wx/dcmemory.h"
27 #endif
28
29 #include "wx/splash.h"
30
31 /*
32 * wxSplashScreen
33 */
34
35 #define wxSPLASH_TIMER_ID 9999
36
37 BEGIN_EVENT_TABLE(wxSplashScreen, wxFrame)
38 EVT_TIMER(wxSPLASH_TIMER_ID, wxSplashScreen::OnNotify)
39 EVT_CLOSE(wxSplashScreen::OnCloseWindow)
40 END_EVENT_TABLE()
41
42 /* Note that unless we pass a non-default size to the frame, SetClientSize
43 * won't work properly under Windows, and the splash screen frame is sized
44 * slightly too small.
45 */
46
47 wxSplashScreen::wxSplashScreen(const wxBitmap& bitmap, long splashStyle, int milliseconds, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style):
48 wxFrame(parent, id, wxEmptyString, wxPoint(0, 0), wxSize(100, 100), style)
49 {
50 m_window = NULL;
51 m_splashStyle = splashStyle;
52 m_milliseconds = milliseconds;
53
54 m_window = new wxSplashScreenWindow(bitmap, this, -1, pos, size, wxNO_BORDER);
55
56 SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
57
58 if (m_splashStyle & wxSPLASH_CENTRE_ON_PARENT)
59 CentreOnParent();
60 else if (m_splashStyle & wxSPLASH_CENTRE_ON_SCREEN)
61 CentreOnScreen();
62
63 if (m_splashStyle & wxSPLASH_TIMEOUT)
64 {
65 m_timer.SetOwner(this, wxSPLASH_TIMER_ID);
66 m_timer.Start(milliseconds, TRUE);
67 }
68
69 Show(TRUE);
70 m_window->SetFocus();
71 wxYield(); // Without this, you see a blank screen for an instant
72 }
73
74 wxSplashScreen::~wxSplashScreen()
75 {
76 m_timer.Stop();
77 }
78
79 void wxSplashScreen::OnNotify(wxTimerEvent& WXUNUSED(event))
80 {
81 Close(TRUE);
82 }
83
84 void wxSplashScreen::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
85 {
86 m_timer.Stop();
87 this->Destroy();
88 }
89
90 /*
91 * wxSplashScreenWindow
92 */
93
94 BEGIN_EVENT_TABLE(wxSplashScreenWindow, wxWindow)
95 //EVT_PAINT(wxSplashScreenWindow::OnPaint)
96 EVT_ERASE_BACKGROUND(wxSplashScreenWindow::OnEraseBackground)
97 EVT_CHAR(wxSplashScreenWindow::OnChar)
98 EVT_MOUSE_EVENTS(wxSplashScreenWindow::OnMouseEvent)
99 END_EVENT_TABLE()
100
101 wxSplashScreenWindow::wxSplashScreenWindow(const wxBitmap& bitmap, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style):
102 wxWindow(parent, id, pos, size, style)
103 {
104 m_bitmap = bitmap;
105 }
106
107 void wxSplashScreenWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
108 {
109 wxPaintDC dc(this);
110 if (m_bitmap.Ok())
111 dc.DrawBitmap(m_bitmap, 0, 0);
112 }
113
114 static void wxDrawSplashBitmap(wxDC& dc, const wxBitmap& bitmap, int WXUNUSED(x), int WXUNUSED(y))
115 {
116 wxMemoryDC dcMem;
117
118 #ifndef __WXGTK__
119 bool hiColour = (wxDisplayDepth() >= 16) ;
120
121 if (bitmap.GetPalette() && !hiColour)
122 {
123 dc.SetPalette(* bitmap.GetPalette());
124 dcMem.SetPalette(* bitmap.GetPalette());
125 }
126 #endif
127
128 dcMem.SelectObject(bitmap);
129 dc.Blit(0, 0, bitmap.GetWidth(), bitmap.GetHeight(), & dcMem, 0, 0);
130 dcMem.SelectObject(wxNullBitmap);
131
132 #ifndef __WXGTK__
133 if (bitmap.GetPalette() && !hiColour)
134 {
135 dc.SetPalette(wxNullPalette);
136 dcMem.SetPalette(wxNullPalette);
137 }
138 #endif
139 }
140
141 void wxSplashScreenWindow::OnEraseBackground(wxEraseEvent& event)
142 {
143 if (event.GetDC())
144 {
145 if (m_bitmap.Ok())
146 {
147 wxDrawSplashBitmap(* event.GetDC(), m_bitmap, 0, 0);
148 }
149 }
150 else
151 {
152 wxClientDC dc(this);
153 if (m_bitmap.Ok())
154 {
155 wxDrawSplashBitmap(dc, m_bitmap, 0, 0);
156 }
157 }
158 }
159
160 void wxSplashScreenWindow::OnMouseEvent(wxMouseEvent& event)
161 {
162 if (event.LeftDown() || event.RightDown())
163 GetParent()->Close(TRUE);
164 }
165
166 void wxSplashScreenWindow::OnChar(wxKeyEvent& WXUNUSED(event))
167 {
168 GetParent()->Close(TRUE);
169 }
170
171 #endif // wxUSE_SPLASH