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) 2007: Edouard TISSERANT and Laurent BESSARD
Laurent@814: #
andrej@1571: # See COPYING file for copyrights details.
andrej@1571: #
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.
andrej@1571: #
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.
andrej@1571: #
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 wx
Laurent@1164: import wx.lib.agw.customtreectrl as CT
Laurent@814: 
Laurent@1164: from util.BitmapLibrary import GetBitmap
Laurent@1164: 
Laurent@1164: # Customize CustomTreeItem for adding icon on item left
Laurent@1164: CT.GenericTreeItem._ExtraImage = None
Laurent@1164: 
andrej@1736: 
Laurent@1164: def SetExtraImage(self, image):
Laurent@1188:     self._type = (1 if image is not None else 0)
Laurent@1164:     self._ExtraImage = image
Laurent@1188: 
andrej@1749: 
Laurent@1164: CT.GenericTreeItem.SetExtraImage = SetExtraImage
Laurent@1164: 
Laurent@1164: _DefaultGetCurrentCheckedImage = CT.GenericTreeItem.GetCurrentCheckedImage
andrej@1736: 
andrej@1736: 
Laurent@1164: def GetCurrentCheckedImage(self):
Laurent@1164:     if self._ExtraImage is not None:
Laurent@1164:         return self._ExtraImage
Laurent@1164:     return _DefaultGetCurrentCheckedImage(self)
andrej@1749: 
andrej@1749: 
Laurent@1164: CT.GenericTreeItem.GetCurrentCheckedImage = GetCurrentCheckedImage
Laurent@1164: 
andrej@1736: 
Laurent@1164: class CustomTree(CT.CustomTreeCtrl):
andrej@1730: 
Laurent@814:     def __init__(self, *args, **kwargs):
Laurent@1164:         CT.CustomTreeCtrl.__init__(self, *args, **kwargs)
andrej@1730: 
Laurent@814:         self.BackgroundBitmap = None
andrej@1745:         self.BackgroundAlign = wx.ALIGN_LEFT | wx.ALIGN_TOP
andrej@1730: 
Laurent@814:         self.AddMenu = None
Laurent@814:         self.Enabled = False
andrej@1730: 
Laurent@1164:         self.Bind(wx.EVT_SCROLLWIN, self.OnScroll)
Laurent@814:         self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
andrej@1730: 
Laurent@814:     def SetBackgroundBitmap(self, bitmap, align):
Laurent@814:         self.BackgroundBitmap = bitmap
Laurent@814:         self.BackgroundAlign = align
andrej@1730: 
Laurent@1164:     def SetImageListCheck(self, sizex, sizey, imglist=None):
Laurent@1164:         CT.CustomTreeCtrl.SetImageListCheck(self, sizex, sizey, imglist=None)
andrej@1730: 
Laurent@1164:         self.ExtraImages = {}
Laurent@1164:         for image in ["function", "functionBlock", "program"]:
Laurent@1164:             self.ExtraImages[image] = self._imageListCheck.Add(GetBitmap(image.upper()))
andrej@1730: 
Laurent@1164:     def SetItemExtraImage(self, item, bitmap):
Laurent@1188:         dc = wx.ClientDC(self)
Laurent@1164:         image = self.ExtraImages.get(bitmap)
Laurent@1164:         if image is not None:
Laurent@1164:             item.SetExtraImage(image)
Laurent@1188:         else:
Laurent@1188:             item.SetExtraImage(None)
Laurent@1188:         self.CalculateSize(item, dc)
andrej@1730:         self.RefreshLine(item)
andrej@1730: 
Laurent@814:     def SetAddMenu(self, add_menu):
Laurent@814:         self.AddMenu = add_menu
andrej@1730: 
Laurent@814:     def Enable(self, enabled):
Laurent@814:         self.Enabled = enabled
andrej@1730: 
Laurent@814:     def GetBitmapRect(self):
Laurent@814:         client_size = self.GetClientSize()
Laurent@814:         bitmap_size = self.BackgroundBitmap.GetSize()
andrej@1730: 
Laurent@814:         if self.BackgroundAlign & wx.ALIGN_RIGHT:
Laurent@814:             x = client_size[0] - bitmap_size[0]
Laurent@814:         elif self.BackgroundAlign & wx.ALIGN_CENTER_HORIZONTAL:
Laurent@814:             x = (client_size[0] - bitmap_size[0]) / 2
Laurent@814:         else:
Laurent@814:             x = 0
andrej@1730: 
Laurent@814:         if self.BackgroundAlign & wx.ALIGN_BOTTOM:
Laurent@814:             y = client_size[1] - bitmap_size[1]
Laurent@814:         elif self.BackgroundAlign & wx.ALIGN_CENTER_VERTICAL:
Laurent@814:             y = (client_size[1] - bitmap_size[1]) / 2
Laurent@814:         else:
Laurent@814:             y = 0
andrej@1730: 
Laurent@814:         return wx.Rect(x, y, bitmap_size[0], bitmap_size[1])
andrej@1730: 
Laurent@814:     def OnLeftUp(self, event):
Laurent@814:         if self.Enabled:
Laurent@814:             pos = event.GetPosition()
andrej@1847:             _item, flags = self.HitTest(pos)
andrej@1730: 
Laurent@814:             bitmap_rect = self.GetBitmapRect()
andrej@1766:             if ((bitmap_rect.InsideXY(pos.x, pos.y) or
andrej@1766:                  flags & wx.TREE_HITTEST_NOWHERE) and self.AddMenu is not None):
Laurent@1165:                 wx.CallAfter(self.PopupMenuXY, self.AddMenu, pos.x, pos.y)
Laurent@814:         event.Skip()
andrej@1730: 
Laurent@1165:     def OnEraseBackground(self, event):
Laurent@1165:         dc = event.GetDC()
Laurent@814: 
Laurent@1165:         if not dc:
Laurent@1165:             dc = wx.ClientDC(self)
Laurent@1165:             rect = self.GetUpdateRegion().GetBox()
Laurent@1165:             dc.SetClippingRect(rect)
andrej@1730: 
Laurent@1165:         dc.Clear()
andrej@1730: 
Laurent@1165:         bitmap_rect = self.GetBitmapRect()
Laurent@1165:         dc.DrawBitmap(self.BackgroundBitmap, bitmap_rect.x, bitmap_rect.y)
andrej@1730: 
Laurent@814:     def OnScroll(self, event):
Laurent@1164:         wx.CallAfter(self.Refresh)
Laurent@814:         event.Skip()
Laurent@814: 
Laurent@1164:     def OnSize(self, event):
Laurent@1164:         CT.CustomTreeCtrl.OnSize(self, event)
Laurent@1164:         self.Refresh()