added missing files
[swftools.git] / wx / lib / embeddedimage.py
1 #----------------------------------------------------------------------
2 # Name:        wx.lib.embeddedimage
3 # Purpose:     Defines a class used for embedding PNG images in Python
4 #              code. The primary method of using this module is via
5 #              the code generator in wx.tools.img2py.
6 #
7 # Author:      Anthony Tuininga
8 #
9 # Created:     26-Nov-2007
10 # RCS-ID:      $Id: embeddedimage.py 59672 2009-03-20 20:59:42Z RD $
11 # Copyright:   (c) 2007 by Anthony Tuininga
12 # Licence:     wxWindows license
13 #----------------------------------------------------------------------
14
15 import base64
16 import cStringIO
17 import wx
18
19 try:
20     b64decode = base64.b64decode
21 except AttributeError:
22     b64decode = base64.decodestring
23     
24
25 class PyEmbeddedImage(object):
26     """
27     PyEmbeddedImage is primarily intended to be used by code generated
28     by img2py as a means of embedding image data in a python module so
29     the image can be used at runtime without needing to access the
30     image from an image file.  This makes distributing icons and such
31     that an application uses simpler since tools like py2exe will
32     automatically bundle modules that are imported, and the
33     application doesn't have to worry about how to locate the image
34     files on the user's filesystem.
35
36     The class can also be used for image data that may be acquired
37     from some other source at runtime, such as over the network or
38     from a database.  In this case pass False for isBase64 (unless the
39     data actually is base64 encoded.)  Any image type that
40     wx.ImageFromStream can handle should be okay.
41     """
42
43     def __init__(self, data, isBase64=True):
44         self.data = data
45         self.isBase64 = isBase64
46
47     def GetBitmap(self):
48         return wx.BitmapFromImage(self.GetImage())
49
50     def GetData(self):
51         if self.isBase64:
52             data = b64decode(self.data)
53         return data
54
55     def GetIcon(self):
56         icon = wx.EmptyIcon()
57         icon.CopyFromBitmap(self.GetBitmap())
58         return icon
59
60     def GetImage(self):
61         stream = cStringIO.StringIO(self.GetData())
62         return wx.ImageFromStream(stream)
63
64     # added for backwards compatibility
65     getBitmap = GetBitmap
66     getData = GetData
67     getIcon = GetIcon
68     getImage = GetImage
69
70     # define properties, for convenience
71     Bitmap = property(GetBitmap)
72     Icon = property(GetIcon)
73     Image = property(GetImage)
74