controls/CustomTree.py
changeset 684 f10449b18dbe
child 687 629680fb0582
equal deleted inserted replaced
683:37882f34f9cb 684:f10449b18dbe
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #This library is free software; you can redistribute it and/or
       
     5 #modify it under the terms of the GNU General Public
       
     6 #License as published by the Free Software Foundation; either
       
     7 #version 2.1 of the License, or (at your option) any later version.
       
     8 #
       
     9 #This library is distributed in the hope that it will be useful,
       
    10 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    12 #General Public License for more details.
       
    13 #
       
    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
       
    16 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    17 
       
    18 import wx
       
    19 
       
    20 class CustomTree(wx.TreeCtrl):
       
    21     
       
    22     def __init__(self, *args, **kwargs):
       
    23         wx.TreeCtrl.__init__(self, *args, **kwargs)
       
    24         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
       
    25         
       
    26         self.BackgroundBitmap = None
       
    27         self.BackgroundAlign = wx.ALIGN_LEFT|wx.ALIGN_TOP
       
    28         
       
    29         self.AddMenu = None
       
    30         
       
    31         if wx.Platform == '__WXMSW__':
       
    32             self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
       
    33         else:
       
    34             self.Bind(wx.EVT_PAINT, self.OnPaint)
       
    35             self.Bind(wx.EVT_SIZE, self.OnResize)
       
    36         self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
       
    37     
       
    38     def SetBackgroundBitmap(self, bitmap, align):
       
    39         self.BackgroundBitmap = bitmap
       
    40         self.BackgroundAlign = align
       
    41     
       
    42     def SetAddMenu(self, add_menu):
       
    43         self.AddMenu = add_menu
       
    44     
       
    45     def GetBitmapRect(self):
       
    46         client_size = self.GetClientSize()
       
    47         bitmap_size = self.BackgroundBitmap.GetSize()
       
    48         
       
    49         if self.BackgroundAlign & wx.ALIGN_RIGHT:
       
    50             x = client_size[0] - bitmap_size[0]
       
    51         elif self.BackgroundAlign & wx.ALIGN_CENTER_HORIZONTAL:
       
    52             x = (client_size[0] - bitmap_size[0]) / 2
       
    53         else:
       
    54             x = 0
       
    55         
       
    56         if self.BackgroundAlign & wx.ALIGN_BOTTOM:
       
    57             y = client_size[1] - bitmap_size[1]
       
    58         elif self.BackgroundAlign & wx.ALIGN_CENTER_VERTICAL:
       
    59             y = (client_size[1] - bitmap_size[1]) / 2
       
    60         else:
       
    61             y = 0
       
    62         
       
    63         return wx.Rect(x, y, bitmap_size[0], bitmap_size[1])
       
    64     
       
    65     def RefreshBackground(self, refresh_base=False):
       
    66         dc = wx.ClientDC(self)
       
    67         dc.Clear()
       
    68         
       
    69         bitmap_rect = self.GetBitmapRect()
       
    70         dc.DrawBitmap(self.BackgroundBitmap, bitmap_rect.x, bitmap_rect.y)
       
    71         
       
    72         if refresh_base:
       
    73             self.Refresh(False)
       
    74     
       
    75     def OnEraseBackground(self, event):
       
    76         self.RefreshBackground(True)
       
    77     
       
    78     def OnLeftUp(self, event):
       
    79         pos = event.GetPosition()
       
    80         item, flags = self.HitTest(pos)
       
    81         
       
    82         bitmap_rect = self.GetBitmapRect()
       
    83         if (bitmap_rect.InsideXY(pos.x, pos.y) or 
       
    84             flags & wx.TREE_HITTEST_NOWHERE) and self.AddMenu is not None:
       
    85             self.PopupMenuXY(self.AddMenu, pos.x, pos.y)
       
    86         event.Skip()
       
    87 
       
    88     def OnResize(self, event):
       
    89         self.RefreshBackground(True)
       
    90         event.Skip()
       
    91     
       
    92     def OnPaint(self, event):
       
    93         self.RefreshBackground()
       
    94         event.Skip()