Laurent@814: #!/usr/bin/env python
Laurent@814: # -*- coding: utf-8 -*-
Laurent@814: 
andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for
andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
Laurent@814: #
andrej@1571: # Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD
Laurent@814: #
andrej@1571: # See COPYING file for copyrights details.
Laurent@814: #
andrej@1571: # This program is free software; you can redistribute it and/or
andrej@1571: # modify it under the terms of the GNU General Public License
andrej@1571: # as published by the Free Software Foundation; either version 2
andrej@1571: # of the License, or (at your option) any later version.
Laurent@814: #
andrej@1571: # This program is distributed in the hope that it will be useful,
andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of
andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
andrej@1571: # GNU General Public License for more details.
Laurent@814: #
andrej@1571: # You should have received a copy of the GNU General Public License
andrej@1571: # along with this program; if not, write to the Free Software
andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Laurent@814: 
andrej@1881: 
andrej@1881: from __future__ import absolute_import
Laurent@814: import os
Laurent@814: 
Laurent@814: import wx
Laurent@814: 
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: #                            Library Structures
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: 
Laurent@814: BitmapLibrary = {}
Laurent@814: BitmapFolders = []
Laurent@814: 
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: #                             Library Helpers
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: 
andrej@1736: 
Laurent@814: def AddBitmapFolder(path):
laurent@815:     if os.path.exists(path) and os.path.isdir(path) and path not in BitmapFolders:
Laurent@814:         BitmapFolders.append(path)
Laurent@814: 
andrej@1736: 
Laurent@814: def SearchBitmap(bmp_name):
Laurent@814:     for folder in BitmapFolders:
Laurent@814:         bmp_path = os.path.join(folder, bmp_name + ".png")
Laurent@814:         if os.path.isfile(bmp_path):
Laurent@814:             return wx.Bitmap(bmp_path)
Laurent@814:     return None
andrej@1735: 
andrej@1736: 
Laurent@814: def GetBitmap(bmp_name1, bmp_name2=None, size=None):
Laurent@814:     bmp = BitmapLibrary.get((bmp_name1, bmp_name2, size))
Laurent@814:     if bmp is not None:
Laurent@814:         return bmp
andrej@1735: 
Laurent@814:     if bmp_name2 is None:
Laurent@814:         bmp = SearchBitmap(bmp_name1)
Laurent@814:     else:
Laurent@814:         # Bitmap with two icon
Laurent@814:         bmp1 = SearchBitmap(bmp_name1)
Laurent@814:         bmp2 = SearchBitmap(bmp_name2)
andrej@1735: 
Laurent@814:         if bmp1 is not None and bmp2 is not None:
Laurent@814:             # Calculate bitmap size
Laurent@814:             width = bmp1.GetWidth() + bmp2.GetWidth() - 1
Laurent@814:             height = max(bmp1.GetHeight(), bmp2.GetHeight())
andrej@1735: 
Laurent@814:             # Create bitmap with both icons
Laurent@814:             bmp = wx.EmptyBitmap(width, height)
Laurent@814:             dc = wx.MemoryDC()
Laurent@814:             dc.SelectObject(bmp)
Laurent@814:             dc.Clear()
Laurent@814:             dc.DrawBitmap(bmp1, 0, 0)
Laurent@814:             dc.DrawBitmap(bmp2, bmp1.GetWidth() - 1, 0)
Laurent@814:             dc.Destroy()
andrej@1735: 
Laurent@814:         elif bmp1 is not None:
Laurent@814:             bmp = bmp1
Laurent@814:         elif bmp2 is not None:
Laurent@814:             bmp = bmp2
andrej@1735: 
Laurent@814:     if bmp is not None:
Laurent@814:         BitmapLibrary[(bmp_name1, bmp_name2, size)] = bmp
andrej@1735: 
Laurent@814:     return bmp