util/BitmapLibrary.py
changeset 1784 64beb9e9c749
parent 1782 5b6ad7a7fd9d
child 1881 091005ec69c4
equal deleted inserted replaced
1729:31e63e25b4cc 1784:64beb9e9c749
    24 
    24 
    25 import os
    25 import os
    26 
    26 
    27 import wx
    27 import wx
    28 
    28 
    29 #-------------------------------------------------------------------------------
    29 # -------------------------------------------------------------------------------
    30 #                            Library Structures
    30 #                            Library Structures
    31 #-------------------------------------------------------------------------------
    31 # -------------------------------------------------------------------------------
    32 
    32 
    33 BitmapLibrary = {}
    33 BitmapLibrary = {}
    34 BitmapFolders = []
    34 BitmapFolders = []
    35 
    35 
    36 #-------------------------------------------------------------------------------
    36 # -------------------------------------------------------------------------------
    37 #                             Library Helpers
    37 #                             Library Helpers
    38 #-------------------------------------------------------------------------------
    38 # -------------------------------------------------------------------------------
       
    39 
    39 
    40 
    40 def AddBitmapFolder(path):
    41 def AddBitmapFolder(path):
    41     if os.path.exists(path) and os.path.isdir(path) and path not in BitmapFolders:
    42     if os.path.exists(path) and os.path.isdir(path) and path not in BitmapFolders:
    42         BitmapFolders.append(path)
    43         BitmapFolders.append(path)
       
    44 
    43 
    45 
    44 def SearchBitmap(bmp_name):
    46 def SearchBitmap(bmp_name):
    45     for folder in BitmapFolders:
    47     for folder in BitmapFolders:
    46         bmp_path = os.path.join(folder, bmp_name + ".png")
    48         bmp_path = os.path.join(folder, bmp_name + ".png")
    47         if os.path.isfile(bmp_path):
    49         if os.path.isfile(bmp_path):
    48             return wx.Bitmap(bmp_path)
    50             return wx.Bitmap(bmp_path)
    49     return None
    51     return None
    50     
    52 
       
    53 
    51 def GetBitmap(bmp_name1, bmp_name2=None, size=None):
    54 def GetBitmap(bmp_name1, bmp_name2=None, size=None):
    52     bmp = BitmapLibrary.get((bmp_name1, bmp_name2, size))
    55     bmp = BitmapLibrary.get((bmp_name1, bmp_name2, size))
    53     if bmp is not None:
    56     if bmp is not None:
    54         return bmp
    57         return bmp
    55     
    58 
    56     if bmp_name2 is None:
    59     if bmp_name2 is None:
    57         bmp = SearchBitmap(bmp_name1)
    60         bmp = SearchBitmap(bmp_name1)
    58     else:
    61     else:
    59         # Bitmap with two icon
    62         # Bitmap with two icon
    60         bmp1 = SearchBitmap(bmp_name1)
    63         bmp1 = SearchBitmap(bmp_name1)
    61         bmp2 = SearchBitmap(bmp_name2)
    64         bmp2 = SearchBitmap(bmp_name2)
    62         
    65 
    63         if bmp1 is not None and bmp2 is not None:
    66         if bmp1 is not None and bmp2 is not None:
    64             # Calculate bitmap size
    67             # Calculate bitmap size
    65             width = bmp1.GetWidth() + bmp2.GetWidth() - 1
    68             width = bmp1.GetWidth() + bmp2.GetWidth() - 1
    66             height = max(bmp1.GetHeight(), bmp2.GetHeight())
    69             height = max(bmp1.GetHeight(), bmp2.GetHeight())
    67             
    70 
    68             # Create bitmap with both icons
    71             # Create bitmap with both icons
    69             bmp = wx.EmptyBitmap(width, height)
    72             bmp = wx.EmptyBitmap(width, height)
    70             dc = wx.MemoryDC()
    73             dc = wx.MemoryDC()
    71             dc.SelectObject(bmp)
    74             dc.SelectObject(bmp)
    72             dc.Clear()
    75             dc.Clear()
    73             dc.DrawBitmap(bmp1, 0, 0)
    76             dc.DrawBitmap(bmp1, 0, 0)
    74             dc.DrawBitmap(bmp2, bmp1.GetWidth() - 1, 0)
    77             dc.DrawBitmap(bmp2, bmp1.GetWidth() - 1, 0)
    75             dc.Destroy()
    78             dc.Destroy()
    76         
    79 
    77         elif bmp1 is not None:
    80         elif bmp1 is not None:
    78             bmp = bmp1
    81             bmp = bmp1
    79         elif bmp2 is not None:
    82         elif bmp2 is not None:
    80             bmp = bmp2
    83             bmp = bmp2
    81     
    84 
    82     if bmp is not None:
    85     if bmp is not None:
    83         BitmapLibrary[(bmp_name1, bmp_name2, size)] = bmp
    86         BitmapLibrary[(bmp_name1, bmp_name2, size)] = bmp
    84         
    87 
    85     return bmp
    88     return bmp