Laurent@814: #!/usr/bin/env python
Laurent@814: # -*- coding: utf-8 -*-
Laurent@814: 
Laurent@814: #This library is free software; you can redistribute it and/or
Laurent@814: #modify it under the terms of the GNU General Public
Laurent@814: #License as published by the Free Software Foundation; either
Laurent@814: #version 2.1 of the License, or (at your option) any later version.
Laurent@814: #
Laurent@814: #This library is distributed in the hope that it will be useful,
Laurent@814: #but WITHOUT ANY WARRANTY; without even the implied warranty of
Laurent@814: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Laurent@814: #General Public License for more details.
Laurent@814: #
Laurent@814: #You should have received a copy of the GNU General Public
Laurent@814: #License along with this library; if not, write to the Free Software
Laurent@814: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Laurent@814: 
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: 
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: 
Laurent@1164: CT.GenericTreeItem.SetExtraImage = SetExtraImage
Laurent@1164: 
Laurent@1164: _DefaultGetCurrentCheckedImage = CT.GenericTreeItem.GetCurrentCheckedImage
Laurent@1164: def GetCurrentCheckedImage(self):
Laurent@1164:     if self._ExtraImage is not None:
Laurent@1164:         return self._ExtraImage
Laurent@1164:     return _DefaultGetCurrentCheckedImage(self)
Laurent@1164: CT.GenericTreeItem.GetCurrentCheckedImage = GetCurrentCheckedImage
Laurent@1164: 
Laurent@1164: class CustomTree(CT.CustomTreeCtrl):
Laurent@814:     
Laurent@814:     def __init__(self, *args, **kwargs):
Laurent@1164:         CT.CustomTreeCtrl.__init__(self, *args, **kwargs)
Laurent@814:         
Laurent@814:         self.BackgroundBitmap = None
Laurent@814:         self.BackgroundAlign = wx.ALIGN_LEFT|wx.ALIGN_TOP
Laurent@814:         
Laurent@814:         self.AddMenu = None
Laurent@814:         self.Enabled = False
Laurent@814:         
Laurent@1164:         self.Bind(wx.EVT_SCROLLWIN, self.OnScroll)
Laurent@814:         self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
Laurent@814:     
Laurent@814:     def SetBackgroundBitmap(self, bitmap, align):
Laurent@814:         self.BackgroundBitmap = bitmap
Laurent@814:         self.BackgroundAlign = align
Laurent@814:     
Laurent@1164:     def SetImageListCheck(self, sizex, sizey, imglist=None):
Laurent@1164:         CT.CustomTreeCtrl.SetImageListCheck(self, sizex, sizey, imglist=None)
Laurent@1164:         
Laurent@1164:         self.ExtraImages = {}
Laurent@1164:         for image in ["function", "functionBlock", "program"]:
Laurent@1164:             self.ExtraImages[image] = self._imageListCheck.Add(GetBitmap(image.upper()))
Laurent@1164:     
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)
Laurent@1188:         self.RefreshLine(item)   
Laurent@1188:         
Laurent@814:     def SetAddMenu(self, add_menu):
Laurent@814:         self.AddMenu = add_menu
Laurent@814:     
Laurent@814:     def Enable(self, enabled):
Laurent@814:         self.Enabled = enabled
Laurent@814:     
Laurent@814:     def GetBitmapRect(self):
Laurent@814:         client_size = self.GetClientSize()
Laurent@814:         bitmap_size = self.BackgroundBitmap.GetSize()
Laurent@814:         
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
Laurent@814:         
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
Laurent@814:         
Laurent@814:         return wx.Rect(x, y, bitmap_size[0], bitmap_size[1])
Laurent@814:     
Laurent@814:     def OnLeftUp(self, event):
Laurent@814:         if self.Enabled:
Laurent@814:             pos = event.GetPosition()
Laurent@814:             item, flags = self.HitTest(pos)
Laurent@814:             
Laurent@814:             bitmap_rect = self.GetBitmapRect()
Laurent@814:             if (bitmap_rect.InsideXY(pos.x, pos.y) or 
Laurent@814:                 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()
Laurent@1165:     
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)
Laurent@1165:         
Laurent@1165:         dc.Clear()
Laurent@1165:         
Laurent@1165:         bitmap_rect = self.GetBitmapRect()
Laurent@1165:         dc.DrawBitmap(self.BackgroundBitmap, bitmap_rect.x, bitmap_rect.y)
Laurent@1165:     
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()