laurent@684: #!/usr/bin/env python laurent@684: # -*- coding: utf-8 -*- laurent@684: laurent@684: #This library is free software; you can redistribute it and/or laurent@684: #modify it under the terms of the GNU General Public laurent@684: #License as published by the Free Software Foundation; either laurent@684: #version 2.1 of the License, or (at your option) any later version. laurent@684: # laurent@684: #This library is distributed in the hope that it will be useful, laurent@684: #but WITHOUT ANY WARRANTY; without even the implied warranty of laurent@684: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU laurent@684: #General Public License for more details. laurent@684: # laurent@684: #You should have received a copy of the GNU General Public laurent@684: #License along with this library; if not, write to the Free Software laurent@684: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA laurent@684: laurent@684: import wx laurent@684: laurent@684: class CustomTree(wx.TreeCtrl): laurent@684: laurent@684: def __init__(self, *args, **kwargs): laurent@684: wx.TreeCtrl.__init__(self, *args, **kwargs) laurent@684: self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM) laurent@684: laurent@684: self.BackgroundBitmap = None laurent@684: self.BackgroundAlign = wx.ALIGN_LEFT|wx.ALIGN_TOP laurent@684: laurent@684: self.AddMenu = None laurent@687: self.Enabled = False laurent@684: laurent@684: if wx.Platform == '__WXMSW__': laurent@684: self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) laurent@684: else: laurent@684: self.Bind(wx.EVT_PAINT, self.OnPaint) laurent@684: self.Bind(wx.EVT_SIZE, self.OnResize) laurent@687: self.Bind(wx.EVT_SCROLL, self.OnScroll) laurent@684: self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) laurent@684: laurent@684: def SetBackgroundBitmap(self, bitmap, align): laurent@684: self.BackgroundBitmap = bitmap laurent@684: self.BackgroundAlign = align laurent@684: laurent@684: def SetAddMenu(self, add_menu): laurent@684: self.AddMenu = add_menu laurent@684: laurent@687: def Enable(self, enabled): laurent@687: self.Enabled = enabled laurent@687: laurent@684: def GetBitmapRect(self): laurent@684: client_size = self.GetClientSize() laurent@684: bitmap_size = self.BackgroundBitmap.GetSize() laurent@684: laurent@684: if self.BackgroundAlign & wx.ALIGN_RIGHT: laurent@684: x = client_size[0] - bitmap_size[0] laurent@684: elif self.BackgroundAlign & wx.ALIGN_CENTER_HORIZONTAL: laurent@684: x = (client_size[0] - bitmap_size[0]) / 2 laurent@684: else: laurent@684: x = 0 laurent@684: laurent@684: if self.BackgroundAlign & wx.ALIGN_BOTTOM: laurent@684: y = client_size[1] - bitmap_size[1] laurent@684: elif self.BackgroundAlign & wx.ALIGN_CENTER_VERTICAL: laurent@684: y = (client_size[1] - bitmap_size[1]) / 2 laurent@684: else: laurent@684: y = 0 laurent@684: laurent@684: return wx.Rect(x, y, bitmap_size[0], bitmap_size[1]) laurent@684: laurent@684: def RefreshBackground(self, refresh_base=False): laurent@684: dc = wx.ClientDC(self) laurent@684: dc.Clear() laurent@684: laurent@684: bitmap_rect = self.GetBitmapRect() laurent@684: dc.DrawBitmap(self.BackgroundBitmap, bitmap_rect.x, bitmap_rect.y) laurent@684: laurent@684: if refresh_base: laurent@684: self.Refresh(False) laurent@684: laurent@684: def OnEraseBackground(self, event): laurent@684: self.RefreshBackground(True) laurent@684: laurent@684: def OnLeftUp(self, event): laurent@687: if self.Enabled: laurent@687: pos = event.GetPosition() laurent@687: item, flags = self.HitTest(pos) laurent@687: laurent@687: bitmap_rect = self.GetBitmapRect() laurent@687: if (bitmap_rect.InsideXY(pos.x, pos.y) or laurent@687: flags & wx.TREE_HITTEST_NOWHERE) and self.AddMenu is not None: laurent@687: self.PopupMenuXY(self.AddMenu, pos.x, pos.y) laurent@687: event.Skip() laurent@687: laurent@687: def OnScroll(self, event): laurent@687: self.RefreshBackground(True) laurent@684: event.Skip() laurent@684: laurent@684: def OnResize(self, event): laurent@684: self.RefreshBackground(True) laurent@684: event.Skip() laurent@684: laurent@684: def OnPaint(self, event): laurent@684: self.RefreshBackground() laurent@684: event.Skip()