home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / gui / infobox / htmlbitmaps.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  10.7 KB  |  350 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. from __future__ import division
  6. import wx
  7. from util import traceguard
  8. from util.httplib2 import cached_content
  9. from gui import skin
  10. from gui.buddylist.renderers import get_buddy_icon
  11. from common import profile
  12. from common import pref
  13. import logging
  14. log = logging.getLogger('gui.htmlbitmaps')
  15. import PIL
  16. from wx import EVT_ERASE_BACKGROUND, EVT_PAINT, EVT_LEFT_UP, EVT_MOTION, RectS, ImageFromString, Panel, BitmapFromImage, AutoBufferedPaintDC, WHITE_BRUSH, TRANSPARENT_PEN, StockCursor, CURSOR_HAND, CURSOR_DEFAULT, Bitmap, BG_STYLE_CUSTOM, ImageFromBitmap, EmptyImage, GetApp, Size
  17. from wx.html import HtmlLinkInfo, HtmlLinkEvent
  18. HTML_ELEM_POS = (-1000, -1000)
  19.  
  20. class BitmapFromData(Panel):
  21.     
  22.     def __init__(self, parent, size = (-1, -1), padding = (0, 0), data = None, href = None):
  23.         self.href = href
  24.         traceguard.__enter__()
  25.         
  26.         try:
  27.             self.data = data.decode('base64')
  28.             self.image = ImageFromString(self.data)
  29.             self.tofinger = True
  30.             Panel.__init__(self, parent, size = size + padding, pos = HTML_ELEM_POS)
  31.             self.SetBackgroundStyle(BG_STYLE_CUSTOM)
  32.             self.bitmap = BitmapFromImage(self.image.Rescale(*size))
  33.             if not self.bitmap.IsOk():
  34.                 self.bitmap = wx.EmptyBitmap(*size)
  35.             
  36.             self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  37.             Bind = self.Bind
  38.             Bind(EVT_PAINT, self.OnPaint)
  39.             Bind(EVT_LEFT_UP, self.OnLeftUp)
  40.             Bind(EVT_MOTION, self.OnMouseMotion)
  41.         finally:
  42.             pass
  43.  
  44.  
  45.     
  46.     def OnPaint(self, event):
  47.         traceguard.__enter__()
  48.         
  49.         try:
  50.             dc = AutoBufferedPaintDC(self)
  51.             rect = RectS(self.Size)
  52.             dc.SetBrush(WHITE_BRUSH)
  53.             dc.SetPen(TRANSPARENT_PEN)
  54.             dc.DrawRectangleRect(rect)
  55.             dc.DrawBitmap(self.bitmap, 0, 0, True)
  56.         finally:
  57.             pass
  58.  
  59.  
  60.     
  61.     def OnMouseMotion(self, event):
  62.         inside = RectS(self.bitmap.Size).Contains(event.Position)
  63.         if inside and self.tofinger:
  64.             self.tofinger = False
  65.             self.SetCursor(StockCursor(CURSOR_HAND))
  66.         elif not inside and not (self.tofinger):
  67.             self.tofinger = True
  68.             self.SetCursor(StockCursor(CURSOR_DEFAULT))
  69.         
  70.  
  71.     
  72.     def OnLeftUp(self, event):
  73.         traceguard.__enter__()
  74.         
  75.         try:
  76.             if self.href is None:
  77.                 return None
  78.             
  79.             if RectS(self.bitmap.Size).Contains(event.Position):
  80.                 linkinfo = HtmlLinkInfo(self.href)
  81.                 evt = HtmlLinkEvent(-1, linkinfo)
  82.                 self.Parent.AddPendingEvent(evt)
  83.                 self.Top.Hide()
  84.         finally:
  85.             pass
  86.  
  87.  
  88.  
  89.  
  90. class BitmapFromSkin(Panel):
  91.     
  92.     def __init__(self, parent, key, size = (-1, -1), padding = (0, 0)):
  93.         traceguard.__enter__()
  94.         
  95.         try:
  96.             Panel.__init__(self, parent, pos = HTML_ELEM_POS)
  97.             incoming = skin.get(key)
  98.             self.bitmap = traceguard if isinstance(incoming, Bitmap) else incoming.GetBitmap(size)
  99.             if not self.bitmap.IsOk():
  100.                 self.bitmap = wx.EmptyBitmap(*size)
  101.             
  102.             self.Size = wx.Size(self.bitmap.Size.width, self.bitmap.Size.height + padding[0] + padding[1])
  103.             self.padding = padding
  104.             self.Bind(EVT_PAINT, self.OnPaint)
  105.             self.SetBackgroundStyle(BG_STYLE_CUSTOM)
  106.         finally:
  107.             pass
  108.  
  109.  
  110.     
  111.     def OnPaint(self, event):
  112.         traceguard.__enter__()
  113.         
  114.         try:
  115.             dc = AutoBufferedPaintDC(self)
  116.             rect = RectS(self.Size)
  117.             dc.Brush = WHITE_BRUSH
  118.             dc.Pen = TRANSPARENT_PEN
  119.             dc.DrawRectangleRect(rect)
  120.             dc.DrawBitmap(self.bitmap, 0, self.padding[0], True)
  121.         finally:
  122.             pass
  123.  
  124.  
  125.  
  126.  
  127. class BuddyIcon(Panel):
  128.     
  129.     def __init__(self, parent, contid, size = None):
  130.         traceguard.__enter__()
  131.         
  132.         try:
  133.             (proto, acct, buddy) = contid.split('/', 2)
  134.             contact = GetApp().StupidContactHack
  135.             icosize = pref('infobox.iconsize', 64)
  136.             self.bitmap = get_buddy_icon(contact, icosize, round_size = 0, grey_offline = False)
  137.             if not self.bitmap.IsOk():
  138.                 self.bitmap = wx.EmptyBitmap(*icosize)
  139.             
  140.             Panel.__init__(self, parent, size = (icosize, icosize), pos = HTML_ELEM_POS)
  141.             self.SetBackgroundStyle(BG_STYLE_CUSTOM)
  142.             self.statico = skin.get('statusicons.' + contact.status_orb).ResizedSmaller(16)
  143.             self.Bind(EVT_PAINT, self.OnPaint)
  144.             self.Bind(EVT_ERASE_BACKGROUND, (lambda e: pass))
  145.             self.SetBackgroundStyle(BG_STYLE_CUSTOM)
  146.         finally:
  147.             pass
  148.  
  149.  
  150.     
  151.     def OnPaint(self, event):
  152.         traceguard.__enter__()
  153.         
  154.         try:
  155.             dc = AutoBufferedPaintDC(self)
  156.             rect = RectS(self.Size)
  157.             statico = self.statico
  158.             dc.SetPen(TRANSPARENT_PEN)
  159.             dc.SetBrush(WHITE_BRUSH)
  160.             dc.DrawRectangleRect(rect)
  161.             dc.DrawBitmap(self.bitmap, 0, 0, True)
  162.             dc.DrawBitmap(statico, (rect.width - 16) + (16 - statico.Size.width) // 2, 0, True)
  163.         finally:
  164.             pass
  165.  
  166.  
  167.  
  168.  
  169. class BitmapFromWeb(Panel):
  170.     
  171.     def __init__(self, parent, url, size = None, href = None, padding = 8, keep_aspect = False, use_image_size = False):
  172.         traceguard.__enter__()
  173.         
  174.         try:
  175.             self.url = url
  176.             self.href = href
  177.             self.padding = padding
  178.             self.tofinger = True
  179.             if url.startswith('skin://'):
  180.                 skinpath = url[len('skin://'):]
  181.                 bitmap = skin.get(skinpath)
  182.                 if use_image_size:
  183.                     size = bitmap.Size
  184.                 
  185.                 if keep_aspect:
  186.                     size = _fix_size(size, bitmap.Size)
  187.                 
  188.                 bitmap = bitmap.Resized(size)
  189.             else:
  190.                 
  191.                 try:
  192.                     image = self._get_pil_url(self.url)
  193.                 except Exception:
  194.                     traceguard
  195.                     traceguard
  196.                     bitmap = None
  197.                     if size is None:
  198.                         size = (45, 0)
  199.                     
  200.                 except:
  201.                     size is None
  202.  
  203.                 if use_image_size:
  204.                     size = image.size
  205.                 
  206.                 if keep_aspect:
  207.                     size = _fix_size(size, image.size)
  208.                 
  209.                 bitmap = image.Resized(size).WXB
  210.             if bitmap is None or not bitmap.IsOk():
  211.                 self.bitmap = wx.EmptyBitmap(*size)
  212.             else:
  213.                 self.bitmap = bitmap
  214.             Panel.__init__(self, parent, size = size + (padding, padding), pos = HTML_ELEM_POS)
  215.             self._init_events()
  216.         finally:
  217.             pass
  218.  
  219.  
  220.     
  221.     def _get_pil_url(self, url):
  222.         
  223.         try:
  224.             f = _[2]
  225.             img = PIL.Image.open(f)
  226.             img.load()
  227.             return img
  228.         finally:
  229.             pass
  230.  
  231.  
  232.     
  233.     def _init_events(self):
  234.         self.SetBackgroundStyle(BG_STYLE_CUSTOM)
  235.         Bind = self.Bind
  236.         Bind(EVT_PAINT, self.OnPaint)
  237.         Bind(EVT_ERASE_BACKGROUND, (lambda e: pass))
  238.         Bind(EVT_LEFT_UP, self.OnLeftUp)
  239.         Bind(EVT_MOTION, self.OnMouseMotion)
  240.  
  241.     
  242.     def OnPaint(self, event):
  243.         dc = AutoBufferedPaintDC(self)
  244.         rect = RectS(self.Size)
  245.         dc.SetBrush(WHITE_BRUSH)
  246.         dc.SetPen(TRANSPARENT_PEN)
  247.         dc.DrawRectangleRect(rect)
  248.         dc.DrawBitmap(self.bitmap, 0, 0, True)
  249.  
  250.     
  251.     def OnMouseMotion(self, event):
  252.         if not self.href:
  253.             return None
  254.         
  255.         inside = RectS(self.bitmap.Size).Contains(event.Position)
  256.         if inside and self.tofinger:
  257.             self.tofinger = False
  258.             self.SetCursor(StockCursor(CURSOR_HAND))
  259.         elif not inside and not (self.tofinger):
  260.             self.tofinger = True
  261.             self.SetCursor(StockCursor(CURSOR_DEFAULT))
  262.         
  263.  
  264.     
  265.     def OnLeftUp(self, event):
  266.         if not self.href:
  267.             return None
  268.         
  269.         if RectS(self.bitmap.Size).Contains(event.Position):
  270.             linkinfo = HtmlLinkInfo(self.href)
  271.             evt = HtmlLinkEvent(-1, linkinfo)
  272.             self.Parent.AddPendingEvent(evt)
  273.             self.Top.Hide()
  274.         
  275.  
  276.  
  277.  
  278. class BitmapFromWebx2(BitmapFromWeb):
  279.     
  280.     def __init__(self, parent, url1, url2, size = None, href = None, padding = 8, keep_aspect = False, size1 = None, size2 = None, pos1 = (0, 0), pos2 = (3, 3)):
  281.         traceguard.__enter__()
  282.         
  283.         try:
  284.             self.url1 = url1
  285.             self.url2 = url2
  286.             self.href = href
  287.             self.padding = padding
  288.             self.tofinger = True
  289.             self.pos1 = pos1
  290.             self.pos2 = pos2
  291.             
  292.             try:
  293.                 image1 = self._get_pil_url(self.url1)
  294.                 image2 = self._get_pil_url(self.url2)
  295.             except Exception:
  296.                 traceguard
  297.                 traceguard
  298.                 bitmap1 = None
  299.                 bitmap2 = None
  300.                 if size is None:
  301.                     size = (45, 0)
  302.                 
  303.             except:
  304.                 size is None
  305.  
  306.             if keep_aspect:
  307.                 size1 = _fix_size(size1, image1.GetSize())
  308.                 size2 = _fix_size(size2, image1.GetSize())
  309.             
  310.             bitmap1 = image1.Resized(size1).WXB
  311.             bitmap2 = image2.Resized(size2).WXB
  312.             if bitmap1 is None or not bitmap1.IsOk():
  313.                 self.bitmap1 = wx.EmptyBitmap(*size1)
  314.             else:
  315.                 self.bitmap1 = bitmap1
  316.             if bitmap2 is None or not bitmap2.IsOk():
  317.                 self.bitmap2 = wx.EmptyBitmap(*size2)
  318.             else:
  319.                 self.bitmap2 = bitmap2
  320.             Panel.__init__(self, parent, size = size + (padding, padding), pos = HTML_ELEM_POS)
  321.             self._init_events()
  322.         finally:
  323.             pass
  324.  
  325.  
  326.     
  327.     def OnPaint(self, event):
  328.         dc = AutoBufferedPaintDC(self)
  329.         rect = RectS(self.Size)
  330.         dc.SetBrush(WHITE_BRUSH)
  331.         dc.SetPen(TRANSPARENT_PEN)
  332.         dc.DrawRectangleRect(rect)
  333.         dc.DrawBitmap(self.bitmap1, self.pos1[0], self.pos1[1], True)
  334.         dc.DrawBitmap(self.bitmap2, self.pos2[0], self.pos2[1], True)
  335.  
  336.  
  337.  
  338. def _fix_size(target, current, scaleto = 1):
  339.     (wt, ht) = target
  340.     (wc, hc) = current
  341.     ratio = wc / hc
  342.     ratio = ratio * scaleto
  343.     if wt:
  344.         ht = ratio ** -1 * wt
  345.     elif ht:
  346.         wt = ratio * ht
  347.     
  348.     return Size(wt, ht)
  349.  
  350.