]> git.saurik.com Git - wxWidgets.git/blame - utils/wxPython/lib/splashscreen.py
Fixed so the tree looks proper on GTK and also reduced flicker.
[wxWidgets.git] / utils / wxPython / lib / splashscreen.py
CommitLineData
9fb56e0c
RD
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
14from wxPython.wx import *
15
16#----------------------------------------------------------------------
17
18def 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
35class SplashScreen(wxFrame):
36 def __init__(self, parent, ID=-1, title="SplashScreen", style=wxSTAY_ON_TOP,
37 duration=1500, bitmapfile="bitmaps/splashscreen.bmp", callback = None):
38 '''
39 parent, ID, title, style -- see wxFrame
40 duration -- milliseconds to display the splash screen
41 bitmapfile -- absolute or relative pathname, extension used for type negotiation
42 callback -- if specified, is called when timer completes, callback is responsible for closing the splash screen
43 '''
44 ### Loading bitmap
45 self.bitmap = bmp = bitmapFromFile(bitmapfile)
46 ### Determine size of bitmap to size window...
47 size = (bmp.GetWidth(), bmp.GetHeight())
48 # size of screen
49 width = wxSystemSettings_GetSystemMetric(wxSYS_SCREEN_X)
50 height = wxSystemSettings_GetSystemMetric(wxSYS_SCREEN_Y)
51 pos = ((width-size[0])/2, (height-size[1])/2)
52
53 # check for overflow...
54 if pos[0] < 0:
55 size = (wxSystemSettings_GetSystemMetric(wxSYS_SCREEN_X), size[1])
56 if pos[1] < 0:
57 size = (size[0], wxSystemSettings_GetSystemMetric(wxSYS_SCREEN_Y))
58
59 wxFrame.__init__(self, parent, ID, title, pos, size, style)
60 self.Show(true)
61 dc = wxClientDC(self)
62 dc.DrawBitmap(self.bitmap, 0,0, false)
63
64 class SplashTimer(wxTimer):
65 def __init__(self, targetFunction):
66 self.Notify = targetFunction
67 wxTimer.__init__(self)
68
69 if callback is None:
70 callback = self.OnSplashExitDefault
71
72 self.timer = SplashTimer(callback)
73 self.timer.Start(duration, 1) # one-shot only
74 EVT_LEFT_DOWN(self, self.OnMouseClick)
75
76
77 def OnSplashExitDefault(self, event=None):
78 self.Close(true)
79
80
81 def OnCloseWindow(self, event=None):
82 self.Show(false)
83 self.timer.Stop()
84 del self.timer
85 self.Destroy()
86
87
88 def OnMouseClick(self, event):
89 self.timer.Notify()
90
91#----------------------------------------------------------------------
92
93
94if __name__ == "__main__":
95 class DemoApp(wxApp):
96 def OnInit(self):
97 wxImage_AddHandler(wxJPEGHandler())
98 wxImage_AddHandler(wxPNGHandler())
99 wxImage_AddHandler(wxGIFHandler())
100 self.splash = SplashScreen(NULL, bitmapfile="splashscreen.jpg", callback=self.OnSplashExit)
101 self.splash.Show(true)
102 self.SetTopWindow(self.splash)
103 return true
104 def OnSplashExit(self, event=None):
105 print "Yay! Application callback worked!"
106 self.splash.Close(true)
107 del self.splash
108 ### Build working windows here...
109 def test(sceneGraph=None):
110 app = DemoApp(0)
111 app.MainLoop()
112 test()