|
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 self.Enabled = False |
|
31 |
|
32 if wx.Platform == '__WXMSW__': |
|
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) |
|
39 |
|
40 def SetBackgroundBitmap(self, bitmap, align): |
|
41 self.BackgroundBitmap = bitmap |
|
42 self.BackgroundAlign = align |
|
43 |
|
44 def SetAddMenu(self, add_menu): |
|
45 self.AddMenu = add_menu |
|
46 |
|
47 def Enable(self, enabled): |
|
48 self.Enabled = enabled |
|
49 |
|
50 def GetBitmapRect(self): |
|
51 client_size = self.GetClientSize() |
|
52 bitmap_size = self.BackgroundBitmap.GetSize() |
|
53 |
|
54 if self.BackgroundAlign & wx.ALIGN_RIGHT: |
|
55 x = client_size[0] - bitmap_size[0] |
|
56 elif self.BackgroundAlign & wx.ALIGN_CENTER_HORIZONTAL: |
|
57 x = (client_size[0] - bitmap_size[0]) / 2 |
|
58 else: |
|
59 x = 0 |
|
60 |
|
61 if self.BackgroundAlign & wx.ALIGN_BOTTOM: |
|
62 y = client_size[1] - bitmap_size[1] |
|
63 elif self.BackgroundAlign & wx.ALIGN_CENTER_VERTICAL: |
|
64 y = (client_size[1] - bitmap_size[1]) / 2 |
|
65 else: |
|
66 y = 0 |
|
67 |
|
68 return wx.Rect(x, y, bitmap_size[0], bitmap_size[1]) |
|
69 |
|
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): |
|
84 if self.Enabled: |
|
85 pos = event.GetPosition() |
|
86 item, flags = self.HitTest(pos) |
|
87 |
|
88 bitmap_rect = self.GetBitmapRect() |
|
89 if (bitmap_rect.InsideXY(pos.x, pos.y) or |
|
90 flags & wx.TREE_HITTEST_NOWHERE) and self.AddMenu is not None: |
|
91 self.PopupMenuXY(self.AddMenu, pos.x, pos.y) |
|
92 event.Skip() |
|
93 |
|
94 def OnScroll(self, event): |
|
95 self.RefreshBackground(True) |
|
96 event.Skip() |
|
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() |