controls/CustomTree.py
changeset 1168 7838595559ba
parent 1165 99972084890d
child 1177 4cbbc58b91b4
equal deleted inserted replaced
1160:e9e9d3193894 1168:7838595559ba
    14 #You should have received a copy of the GNU General Public
    14 #You should have received a copy of the GNU General Public
    15 #License along with this library; if not, write to the Free Software
    15 #License along with this library; if not, write to the Free Software
    16 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    16 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17 
    17 
    18 import wx
    18 import wx
       
    19 import wx.lib.agw.customtreectrl as CT
    19 
    20 
    20 class CustomTree(wx.TreeCtrl):
    21 from util.BitmapLibrary import GetBitmap
       
    22 
       
    23 # Customize CustomTreeItem for adding icon on item left
       
    24 CT.GenericTreeItem._ExtraImage = None
       
    25 
       
    26 def SetExtraImage(self, image):
       
    27     self._type = 1
       
    28     self._ExtraImage = image
       
    29 CT.GenericTreeItem.SetExtraImage = SetExtraImage
       
    30 
       
    31 _DefaultGetCurrentCheckedImage = CT.GenericTreeItem.GetCurrentCheckedImage
       
    32 def GetCurrentCheckedImage(self):
       
    33     if self._ExtraImage is not None:
       
    34         return self._ExtraImage
       
    35     return _DefaultGetCurrentCheckedImage(self)
       
    36 CT.GenericTreeItem.GetCurrentCheckedImage = GetCurrentCheckedImage
       
    37 
       
    38 class CustomTree(CT.CustomTreeCtrl):
    21     
    39     
    22     def __init__(self, *args, **kwargs):
    40     def __init__(self, *args, **kwargs):
    23         wx.TreeCtrl.__init__(self, *args, **kwargs)
    41         CT.CustomTreeCtrl.__init__(self, *args, **kwargs)
    24         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
    42         #self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
    25         
    43         
    26         self.BackgroundBitmap = None
    44         self.BackgroundBitmap = None
    27         self.BackgroundAlign = wx.ALIGN_LEFT|wx.ALIGN_TOP
    45         self.BackgroundAlign = wx.ALIGN_LEFT|wx.ALIGN_TOP
    28         
    46         
    29         self.AddMenu = None
    47         self.AddMenu = None
    30         self.Enabled = False
    48         self.Enabled = False
    31         
    49         
    32         if wx.Platform == '__WXMSW__':
    50         self.Bind(wx.EVT_SCROLLWIN, self.OnScroll)
    33             self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
       
    34         else:
       
    35             self.Bind(wx.EVT_PAINT, self.OnPaint)
       
    36             self.Bind(wx.EVT_SIZE, self.OnResize)
       
    37             self.Bind(wx.EVT_SCROLL, self.OnScroll)
       
    38         self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
    51         self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
    39     
    52     
    40     def SetBackgroundBitmap(self, bitmap, align):
    53     def SetBackgroundBitmap(self, bitmap, align):
    41         self.BackgroundBitmap = bitmap
    54         self.BackgroundBitmap = bitmap
    42         self.BackgroundAlign = align
    55         self.BackgroundAlign = align
       
    56     
       
    57     def SetImageListCheck(self, sizex, sizey, imglist=None):
       
    58         CT.CustomTreeCtrl.SetImageListCheck(self, sizex, sizey, imglist=None)
       
    59         
       
    60         self.ExtraImages = {}
       
    61         for image in ["function", "functionBlock", "program"]:
       
    62             self.ExtraImages[image] = self._imageListCheck.Add(GetBitmap(image.upper()))
       
    63     
       
    64     def SetItemExtraImage(self, item, bitmap):
       
    65         image = self.ExtraImages.get(bitmap)
       
    66         if image is not None:
       
    67             dc = wx.ClientDC(self)
       
    68             item.SetExtraImage(image)
       
    69             self.CalculateSize(item, dc)
       
    70             self.RefreshLine(item)
    43     
    71     
    44     def SetAddMenu(self, add_menu):
    72     def SetAddMenu(self, add_menu):
    45         self.AddMenu = add_menu
    73         self.AddMenu = add_menu
    46     
    74     
    47     def Enable(self, enabled):
    75     def Enable(self, enabled):
    65         else:
    93         else:
    66             y = 0
    94             y = 0
    67         
    95         
    68         return wx.Rect(x, y, bitmap_size[0], bitmap_size[1])
    96         return wx.Rect(x, y, bitmap_size[0], bitmap_size[1])
    69     
    97     
    70     def RefreshBackground(self, refresh_base=False):
       
    71         dc = wx.ClientDC(self)
       
    72         dc.Clear()
       
    73         
       
    74         bitmap_rect = self.GetBitmapRect()
       
    75         dc.DrawBitmap(self.BackgroundBitmap, bitmap_rect.x, bitmap_rect.y)
       
    76         
       
    77         if refresh_base:
       
    78             self.Refresh(False)
       
    79     
       
    80     def OnEraseBackground(self, event):
       
    81         self.RefreshBackground(True)
       
    82     
       
    83     def OnLeftUp(self, event):
    98     def OnLeftUp(self, event):
    84         if self.Enabled:
    99         if self.Enabled:
    85             pos = event.GetPosition()
   100             pos = event.GetPosition()
    86             item, flags = self.HitTest(pos)
   101             item, flags = self.HitTest(pos)
    87             
   102             
    88             bitmap_rect = self.GetBitmapRect()
   103             bitmap_rect = self.GetBitmapRect()
    89             if (bitmap_rect.InsideXY(pos.x, pos.y) or 
   104             if (bitmap_rect.InsideXY(pos.x, pos.y) or 
    90                 flags & wx.TREE_HITTEST_NOWHERE) and self.AddMenu is not None:
   105                 flags & wx.TREE_HITTEST_NOWHERE) and self.AddMenu is not None:
    91                 self.PopupMenuXY(self.AddMenu, pos.x, pos.y)
   106                 wx.CallAfter(self.PopupMenuXY, self.AddMenu, pos.x, pos.y)
       
   107         event.Skip()
       
   108     
       
   109     def OnEraseBackground(self, event):
       
   110         dc = event.GetDC()
       
   111 
       
   112         if not dc:
       
   113             dc = wx.ClientDC(self)
       
   114             rect = self.GetUpdateRegion().GetBox()
       
   115             dc.SetClippingRect(rect)
       
   116         
       
   117         dc.Clear()
       
   118         
       
   119         bitmap_rect = self.GetBitmapRect()
       
   120         dc.DrawBitmap(self.BackgroundBitmap, bitmap_rect.x, bitmap_rect.y)
       
   121     
       
   122     def OnScroll(self, event):
       
   123         wx.CallAfter(self.Refresh)
    92         event.Skip()
   124         event.Skip()
    93 
   125 
    94     def OnScroll(self, event):
   126     def OnSize(self, event):
    95         self.RefreshBackground(True)
   127         CT.CustomTreeCtrl.OnSize(self, event)
    96         event.Skip()
   128         self.Refresh()
    97 
       
    98     def OnResize(self, event):
       
    99         self.RefreshBackground(True)
       
   100         event.Skip()
       
   101     
       
   102     def OnPaint(self, event):
       
   103         self.RefreshBackground()
       
   104         event.Skip()