Merge branch 'horizontals'
[swftools.git] / wx / lib / utils.py
1 import wx
2
3 def blank_bmp(w, h, color="white"):
4     bmp = wx.EmptyBitmap(max(w,1) , max(h,1))
5     clear_bmp(bmp, color)
6     return bmp
7
8 def clear_bmp(bmp, color):
9     dc = wx.MemoryDC()
10     dc.SelectObject(bmp)
11     dc.SetBackground(wx.Brush(color))
12     dc.Clear()
13
14
15 # Taken and adapt from django.utils.encoding
16 class GPdf2SwfUnicodeDecodeError(UnicodeDecodeError):
17     def __init__(self, obj, *args):
18         self.obj = obj
19         UnicodeDecodeError.__init__(self, *args)
20
21     def __str__(self):
22         original = UnicodeDecodeError.__str__(self)
23         return '%s. You passed in %r (%s)' % (original, self.obj,
24                 type(self.obj))
25
26 def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'):
27     try:
28         if not isinstance(s, basestring,):
29             if hasattr(s, '__unicode__'):
30                 s = unicode(s)
31             else:
32                 try:
33                     s = unicode(str(s), encoding, errors)
34                 except UnicodeEncodeError:
35                     if not isinstance(s, Exception):
36                         raise
37                     # If we get to here, the caller has passed in an Exception
38                     # subclass populated with non-ASCII data without special
39                     # handling to display as a string. We need to handle this
40                     # without raising a further exception. We do an
41                     # approximation to what the Exception's standard str()
42                     # output should be.
43                     s = ' '.join([force_unicode(arg, encoding, strings_only,
44                             errors) for arg in s])
45         elif not isinstance(s, unicode):
46             # Note: We use .decode() here, instead of unicode(s, encoding,
47             # errors), so that if s is a SafeString, it ends up being a
48             # SafeUnicode at the end.
49             s = s.decode(encoding, errors)
50     except UnicodeDecodeError, e:
51         raise GPdf2SwfUnicodeDecodeError(s, *e.args)
52         #raise UnicodeDecodeError(*e.args)
53     return s
54
55