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@814: Laurent@814: class CustomTree(wx.TreeCtrl): Laurent@814: Laurent@814: def __init__(self, *args, **kwargs): Laurent@814: wx.TreeCtrl.__init__(self, *args, **kwargs) Laurent@814: self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM) 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@814: if wx.Platform == '__WXMSW__': Laurent@814: self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) Laurent@814: else: Laurent@814: self.Bind(wx.EVT_PAINT, self.OnPaint) Laurent@814: self.Bind(wx.EVT_SIZE, self.OnResize) Laurent@814: self.Bind(wx.EVT_SCROLL, 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@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 RefreshBackground(self, refresh_base=False): Laurent@814: dc = wx.ClientDC(self) Laurent@814: dc.Clear() Laurent@814: Laurent@814: bitmap_rect = self.GetBitmapRect() Laurent@814: dc.DrawBitmap(self.BackgroundBitmap, bitmap_rect.x, bitmap_rect.y) Laurent@814: Laurent@814: if refresh_base: Laurent@814: self.Refresh(False) Laurent@814: Laurent@814: def OnEraseBackground(self, event): Laurent@814: self.RefreshBackground(True) 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@814: self.PopupMenuXY(self.AddMenu, pos.x, pos.y) Laurent@814: event.Skip() Laurent@814: Laurent@814: def OnScroll(self, event): Laurent@814: self.RefreshBackground(True) Laurent@814: event.Skip() Laurent@814: Laurent@814: def OnResize(self, event): Laurent@814: self.RefreshBackground(True) Laurent@814: event.Skip() Laurent@814: Laurent@814: def OnPaint(self, event): Laurent@814: self.RefreshBackground() Laurent@814: event.Skip()