]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/splashscreen.py
8dfa14f4450c0d64b15fe02d2d62fdeed50f0402
[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 "jpg" :wxBITMAP_TYPE_JPEG,
26 "gif" :wxBITMAP_TYPE_GIF,
27 "xbm" :wxBITMAP_TYPE_XBM,
28 }
29 # following assumes bitmap type if we cannot resolve image type
30 typ = BITMAPTYPEGUESSDICT.get(imghdr.what(filename), wxBITMAP_TYPE_BMP)
31 bitmap = wxImage(filename, typ).ConvertToBitmap()
32 return bitmap
33
34 #----------------------------------------------------------------------
35
36 class SplashScreen(wxFrame):
37 def __init__(self, parent, ID=-1, title="SplashScreen",
38 style=wxSIMPLE_BORDER|wxSTAY_ON_TOP,
39 duration=1500, bitmapfile="bitmaps/splashscreen.bmp",
40 callback = None):
41 '''
42 parent, ID, title, style -- see wxFrame
43 duration -- milliseconds to display the splash screen
44 bitmapfile -- absolute or relative pathname, extension used for type negotiation
45 callback -- if specified, is called when timer completes, callback is responsible for closing the splash screen
46 '''
47 ### Loading bitmap
48 self.bitmap = bmp = bitmapFromFile(bitmapfile)
49 ### Determine size of bitmap to size window...
50 size = (bmp.GetWidth(), bmp.GetHeight())
51 # size of screen
52 width = wxSystemSettings_GetSystemMetric(wxSYS_SCREEN_X)
53 height = wxSystemSettings_GetSystemMetric(wxSYS_SCREEN_Y)
54 pos = ((width-size[0])/2, (height-size[1])/2)
55
56 # check for overflow...
57 if pos[0] < 0:
58 size = (wxSystemSettings_GetSystemMetric(wxSYS_SCREEN_X), size[1])
59 if pos[1] < 0:
60 size = (size[0], wxSystemSettings_GetSystemMetric(wxSYS_SCREEN_Y))
61
62 wxFrame.__init__(self, parent, ID, title, pos, size, style)
63 EVT_LEFT_DOWN(self, self.OnMouseClick)
64 EVT_CLOSE(self, self.OnCloseWindow)
65 EVT_PAINT(self, self.OnPaint)
66 EVT_ERASE_BACKGROUND(self, self.OnEraseBG)
67
68 self.Show(true)
69 #dc = wxClientDC(self)
70 #dc.DrawBitmap(self.bitmap, 0,0, false)
71
72 class SplashTimer(wxTimer):
73 def __init__(self, targetFunction):
74 self.Notify = targetFunction
75 wxTimer.__init__(self)
76
77 if callback is None:
78 callback = self.OnSplashExitDefault
79
80 self.timer = SplashTimer(callback)
81 self.timer.Start(duration, 1) # one-shot only
82
83 def OnPaint(self, event):
84 dc = wxPaintDC(self)
85 dc.DrawBitmap(self.bitmap, 0,0, false)
86
87 def OnEraseBG(self, event):
88 pass
89
90 def OnSplashExitDefault(self, event=None):
91 self.Close(true)
92
93 def OnCloseWindow(self, event=None):
94 self.Show(false)
95 self.timer.Stop()
96 del self.timer
97 self.Destroy()
98
99 def OnMouseClick(self, event):
100 self.timer.Notify()
101
102 #----------------------------------------------------------------------
103
104
105 if __name__ == "__main__":
106 class DemoApp(wxApp):
107 def OnInit(self):
108 wxImage_AddHandler(wxJPEGHandler())
109 wxImage_AddHandler(wxPNGHandler())
110 wxImage_AddHandler(wxGIFHandler())
111 self.splash = SplashScreen(NULL, bitmapfile="splashscreen.jpg", callback=self.OnSplashExit)
112 self.splash.Show(true)
113 self.SetTopWindow(self.splash)
114 return true
115 def OnSplashExit(self, event=None):
116 print "Yay! Application callback worked!"
117 self.splash.Close(true)
118 del self.splash
119 ### Build working windows here...
120 def test(sceneGraph=None):
121 app = DemoApp(0)
122 app.MainLoop()
123 test()