~ubuntu-branches/ubuntu/oneiric/phatch/oneiric

« back to all changes in this revision

Viewing changes to phatch/pyWx/lib/graphics.py

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2008-02-13 23:48:47 UTC
  • Revision ID: james.westby@ubuntu.com-20080213234847-mp6vc4y88a9rz5qz
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2008 www.stani.be
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation, either version 3 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program.  If not, see http://www.gnu.org/licenses/
 
15
 
 
16
import zlib
 
17
from cStringIO import StringIO
 
18
from urllib2 import urlopen
 
19
 
 
20
import wx
 
21
 
 
22
from formField import is_www_file
 
23
 
 
24
def bitmap(icon,size=(48,48),client=wx.ART_OTHER):
 
25
    if icon[:4]=='ART_':
 
26
        return wx.ArtProvider_GetBitmap(getattr(wx,icon),client,size)
 
27
    else:
 
28
        return wx.BitmapFromImage(image(icon))
 
29
            
 
30
def image(icon,size=(48,48)):
 
31
    if icon[:4]=='ART_':
 
32
        return wx.ImageFromBitmap(bitmap(icon,size))
 
33
    else:
 
34
        return wx.ImageFromStream(StringIO(zlib.decompress(icon)))
 
35
           
 
36
CACHE = {}
 
37
 
 
38
def bitmap_open(x,height=64):
 
39
    try:
 
40
        return CACHE[(x,height)]
 
41
    except KeyError:
 
42
        pass
 
43
    if is_www_file(x):
 
44
        im  = wx.ImageFromStream(StringIO(urlopen(x).read()))
 
45
    else:
 
46
        im  = wx.Image(x)
 
47
    im  = CACHE[(x,height)] = im.Rescale(
 
48
            float(height)*im.GetWidth()/im.GetHeight(),height
 
49
        ).ConvertToBitmap()
 
50
    return im
 
51