]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/splashscreen.py
23878d53e27427fddd6d4fdc69bbe85500e62ea3
[wxWidgets.git] / wxPython / wxPython / lib / splashscreen.py
1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.splashscreen
3 # Purpose: A simple frame that can display a bitmap and closes itself
4 # after a specified timeout or a mouse click.
5 #
6 # Author: Mike Fletcher, Robin Dunn
7 #
8 # Created: 19-Nov-1999
9 # RCS-ID: $Id$
10 # Copyright: (c) 1999 by Total Control Software
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------
13
14 from wxPython.wx import *
15
16 #----------------------------------------------------------------------
17
18 def bitmapFromFile(filename):
19 '''Non-portable test for bitmap type...'''
20 import imghdr
21 BITMAPTYPEGUESSDICT = {
22 "bmp" :wxBITMAP_TYPE_BMP,
23 "png" :wxBITMAP_TYPE_PNG,
24 "jpeg":wxBITMAP_TYPE_JPEG,
25 "gif" :wxBITMAP_TYPE_GIF,
26 "xbm" :wxBITMAP_TYPE_XBM,
27 }
28 # following assumes bitmap type if we cannot resolve image type
29 typ = BITMAPTYPEGUESSDICT.get(imghdr.what(filename), wxBITMAP_TYPE_BMP)
30 bitmap = wxImage(filename, typ).ConvertToBitmap()
31 return bitmap
32
33 #----------------------------------------------------------------------
34
35 class SplashScreen(wxFrame):
36 def __init__(self, parent, ID=-1, title="SplashScreen",
37 style=wxSIMPLE_BORDER|wxSTAY_ON_TOP,
38 duration=1500, bitmapfile="bitmaps/splashscreen.bmp",
39 callback = None):
40 '''
41 parent, ID, title, style -- see wxFrame
42 duration -- milliseconds to display the splash screen
43 bitmapfile -- absolute or relative pathname, extension used for type negotiation
44 callback -- if specified, is called when timer completes, callback is responsible for closing the splash screen
45 '''
46 ### Loading bitmap
47 self.bitmap = bmp = bitmapFromFile(bitmapfile)
48 ### Determine size of bitmap to size window...
49 size = (bmp.GetWidth(), bmp.GetHeight())
50 # size of screen
51 width = wxSystemSettings_GetSystemMetric(wxSYS_SCREEN_X)
52 height = wxSystemSettings_GetSystemMetric(wxSYS_SCREEN_Y)
53 pos = ((width-size[0])/2, (height-size[1])/2)
54
55 # check for overflow...
56 if pos[0] < 0:
57 size = (wxSystemSettings_GetSystemMetric(wxSYS_SCREEN_X), size[1])
58 if pos[1] < 0:
59 size = (size[0], wxSystemSettings_GetSystemMetric(wxSYS_SCREEN_Y))
60
61 wxFrame.__init__(self, parent, ID, title, pos, size, style)
62 EVT_LEFT_DOWN(self, self.OnMouseClick)
63 EVT_CLOSE(self, self.OnCloseWindow)
64 EVT_PAINT(self, self.OnPaint)
65 EVT_ERASE_BACKGROUND(self, self.OnEraseBG)
66
67 self.Show(true)
68 #dc = wxClientDC(self)
69 #dc.DrawBitmap(self.bitmap, 0,0, false)
70
71 class SplashTimer(wxTimer):
72 def __init__(self, targetFunction):
73 self.Notify = targetFunction
74 wxTimer.__init__(self)
75
76 if callback is None:
77 callback = self.OnSplashExitDefault
78
79 self.timer = SplashTimer(callback)
80 self.timer.Start(duration, 1) # one-shot only
81
82 def OnPaint(self, event):
83 dc = wxPaintDC(self)
84 dc.DrawBitmap(self.bitmap, 0,0, false)
85
86 def OnEraseBG(self, event):
87 pass
88
89 def OnSplashExitDefault(self, event=None):
90 self.Close(true)
91
92 def OnCloseWindow(self, event=None):
93 self.Show(false)
94 self.timer.Stop()
95 del self.timer
96 self.Destroy()
97
98 def OnMouseClick(self, event):
99 self.timer.Notify()
100
101 #----------------------------------------------------------------------
102
103
104 if __name__ == "__main__":
105 class DemoApp(wxApp):
106 def OnInit(self):
107 wxImage_AddHandler(wxJPEGHandler())
108 wxImage_AddHandler(wxPNGHandler())
109 wxImage_AddHandler(wxGIFHandler())
110 self.splash = SplashScreen(NULL, bitmapfile="splashscreen.jpg", callback=self.OnSplashExit)
111 self.splash.Show(true)
112 self.SetTopWindow(self.splash)
113 return true
114 def OnSplashExit(self, event=None):
115 print "Yay! Application callback worked!"
116 self.splash.Close(true)
117 del self.splash
118 ### Build working windows here...
119 def test(sceneGraph=None):
120 app = DemoApp(0)
121 app.MainLoop()
122 test()