controls/CustomTree.py
changeset 1784 64beb9e9c749
parent 1766 c1e5b9f19483
child 1847 6198190bc121
equal deleted inserted replaced
1729:31e63e25b4cc 1784:64beb9e9c749
    28 from util.BitmapLibrary import GetBitmap
    28 from util.BitmapLibrary import GetBitmap
    29 
    29 
    30 # Customize CustomTreeItem for adding icon on item left
    30 # Customize CustomTreeItem for adding icon on item left
    31 CT.GenericTreeItem._ExtraImage = None
    31 CT.GenericTreeItem._ExtraImage = None
    32 
    32 
       
    33 
    33 def SetExtraImage(self, image):
    34 def SetExtraImage(self, image):
    34     self._type = (1 if image is not None else 0)
    35     self._type = (1 if image is not None else 0)
    35     self._ExtraImage = image
    36     self._ExtraImage = image
    36 
    37 
       
    38 
    37 CT.GenericTreeItem.SetExtraImage = SetExtraImage
    39 CT.GenericTreeItem.SetExtraImage = SetExtraImage
    38 
    40 
    39 _DefaultGetCurrentCheckedImage = CT.GenericTreeItem.GetCurrentCheckedImage
    41 _DefaultGetCurrentCheckedImage = CT.GenericTreeItem.GetCurrentCheckedImage
       
    42 
       
    43 
    40 def GetCurrentCheckedImage(self):
    44 def GetCurrentCheckedImage(self):
    41     if self._ExtraImage is not None:
    45     if self._ExtraImage is not None:
    42         return self._ExtraImage
    46         return self._ExtraImage
    43     return _DefaultGetCurrentCheckedImage(self)
    47     return _DefaultGetCurrentCheckedImage(self)
       
    48 
       
    49 
    44 CT.GenericTreeItem.GetCurrentCheckedImage = GetCurrentCheckedImage
    50 CT.GenericTreeItem.GetCurrentCheckedImage = GetCurrentCheckedImage
    45 
    51 
       
    52 
    46 class CustomTree(CT.CustomTreeCtrl):
    53 class CustomTree(CT.CustomTreeCtrl):
    47     
    54 
    48     def __init__(self, *args, **kwargs):
    55     def __init__(self, *args, **kwargs):
    49         CT.CustomTreeCtrl.__init__(self, *args, **kwargs)
    56         CT.CustomTreeCtrl.__init__(self, *args, **kwargs)
    50         
    57 
    51         self.BackgroundBitmap = None
    58         self.BackgroundBitmap = None
    52         self.BackgroundAlign = wx.ALIGN_LEFT|wx.ALIGN_TOP
    59         self.BackgroundAlign = wx.ALIGN_LEFT | wx.ALIGN_TOP
    53         
    60 
    54         self.AddMenu = None
    61         self.AddMenu = None
    55         self.Enabled = False
    62         self.Enabled = False
    56         
    63 
    57         self.Bind(wx.EVT_SCROLLWIN, self.OnScroll)
    64         self.Bind(wx.EVT_SCROLLWIN, self.OnScroll)
    58         self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
    65         self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
    59     
    66 
    60     def SetBackgroundBitmap(self, bitmap, align):
    67     def SetBackgroundBitmap(self, bitmap, align):
    61         self.BackgroundBitmap = bitmap
    68         self.BackgroundBitmap = bitmap
    62         self.BackgroundAlign = align
    69         self.BackgroundAlign = align
    63     
    70 
    64     def SetImageListCheck(self, sizex, sizey, imglist=None):
    71     def SetImageListCheck(self, sizex, sizey, imglist=None):
    65         CT.CustomTreeCtrl.SetImageListCheck(self, sizex, sizey, imglist=None)
    72         CT.CustomTreeCtrl.SetImageListCheck(self, sizex, sizey, imglist=None)
    66         
    73 
    67         self.ExtraImages = {}
    74         self.ExtraImages = {}
    68         for image in ["function", "functionBlock", "program"]:
    75         for image in ["function", "functionBlock", "program"]:
    69             self.ExtraImages[image] = self._imageListCheck.Add(GetBitmap(image.upper()))
    76             self.ExtraImages[image] = self._imageListCheck.Add(GetBitmap(image.upper()))
    70     
    77 
    71     def SetItemExtraImage(self, item, bitmap):
    78     def SetItemExtraImage(self, item, bitmap):
    72         dc = wx.ClientDC(self)
    79         dc = wx.ClientDC(self)
    73         image = self.ExtraImages.get(bitmap)
    80         image = self.ExtraImages.get(bitmap)
    74         if image is not None:
    81         if image is not None:
    75             item.SetExtraImage(image)
    82             item.SetExtraImage(image)
    76         else:
    83         else:
    77             item.SetExtraImage(None)
    84             item.SetExtraImage(None)
    78         self.CalculateSize(item, dc)
    85         self.CalculateSize(item, dc)
    79         self.RefreshLine(item)   
    86         self.RefreshLine(item)
    80         
    87 
    81     def SetAddMenu(self, add_menu):
    88     def SetAddMenu(self, add_menu):
    82         self.AddMenu = add_menu
    89         self.AddMenu = add_menu
    83     
    90 
    84     def Enable(self, enabled):
    91     def Enable(self, enabled):
    85         self.Enabled = enabled
    92         self.Enabled = enabled
    86     
    93 
    87     def GetBitmapRect(self):
    94     def GetBitmapRect(self):
    88         client_size = self.GetClientSize()
    95         client_size = self.GetClientSize()
    89         bitmap_size = self.BackgroundBitmap.GetSize()
    96         bitmap_size = self.BackgroundBitmap.GetSize()
    90         
    97 
    91         if self.BackgroundAlign & wx.ALIGN_RIGHT:
    98         if self.BackgroundAlign & wx.ALIGN_RIGHT:
    92             x = client_size[0] - bitmap_size[0]
    99             x = client_size[0] - bitmap_size[0]
    93         elif self.BackgroundAlign & wx.ALIGN_CENTER_HORIZONTAL:
   100         elif self.BackgroundAlign & wx.ALIGN_CENTER_HORIZONTAL:
    94             x = (client_size[0] - bitmap_size[0]) / 2
   101             x = (client_size[0] - bitmap_size[0]) / 2
    95         else:
   102         else:
    96             x = 0
   103             x = 0
    97         
   104 
    98         if self.BackgroundAlign & wx.ALIGN_BOTTOM:
   105         if self.BackgroundAlign & wx.ALIGN_BOTTOM:
    99             y = client_size[1] - bitmap_size[1]
   106             y = client_size[1] - bitmap_size[1]
   100         elif self.BackgroundAlign & wx.ALIGN_CENTER_VERTICAL:
   107         elif self.BackgroundAlign & wx.ALIGN_CENTER_VERTICAL:
   101             y = (client_size[1] - bitmap_size[1]) / 2
   108             y = (client_size[1] - bitmap_size[1]) / 2
   102         else:
   109         else:
   103             y = 0
   110             y = 0
   104         
   111 
   105         return wx.Rect(x, y, bitmap_size[0], bitmap_size[1])
   112         return wx.Rect(x, y, bitmap_size[0], bitmap_size[1])
   106     
   113 
   107     def OnLeftUp(self, event):
   114     def OnLeftUp(self, event):
   108         if self.Enabled:
   115         if self.Enabled:
   109             pos = event.GetPosition()
   116             pos = event.GetPosition()
   110             item, flags = self.HitTest(pos)
   117             item, flags = self.HitTest(pos)
   111             
   118 
   112             bitmap_rect = self.GetBitmapRect()
   119             bitmap_rect = self.GetBitmapRect()
   113             if (bitmap_rect.InsideXY(pos.x, pos.y) or 
   120             if ((bitmap_rect.InsideXY(pos.x, pos.y) or
   114                 flags & wx.TREE_HITTEST_NOWHERE) and self.AddMenu is not None:
   121                  flags & wx.TREE_HITTEST_NOWHERE) and self.AddMenu is not None):
   115                 wx.CallAfter(self.PopupMenuXY, self.AddMenu, pos.x, pos.y)
   122                 wx.CallAfter(self.PopupMenuXY, self.AddMenu, pos.x, pos.y)
   116         event.Skip()
   123         event.Skip()
   117     
   124 
   118     def OnEraseBackground(self, event):
   125     def OnEraseBackground(self, event):
   119         dc = event.GetDC()
   126         dc = event.GetDC()
   120 
   127 
   121         if not dc:
   128         if not dc:
   122             dc = wx.ClientDC(self)
   129             dc = wx.ClientDC(self)
   123             rect = self.GetUpdateRegion().GetBox()
   130             rect = self.GetUpdateRegion().GetBox()
   124             dc.SetClippingRect(rect)
   131             dc.SetClippingRect(rect)
   125         
   132 
   126         dc.Clear()
   133         dc.Clear()
   127         
   134 
   128         bitmap_rect = self.GetBitmapRect()
   135         bitmap_rect = self.GetBitmapRect()
   129         dc.DrawBitmap(self.BackgroundBitmap, bitmap_rect.x, bitmap_rect.y)
   136         dc.DrawBitmap(self.BackgroundBitmap, bitmap_rect.x, bitmap_rect.y)
   130     
   137 
   131     def OnScroll(self, event):
   138     def OnScroll(self, event):
   132         wx.CallAfter(self.Refresh)
   139         wx.CallAfter(self.Refresh)
   133         event.Skip()
   140         event.Skip()
   134 
   141 
   135     def OnSize(self, event):
   142     def OnSize(self, event):