controls/CustomTree.py
author Laurent Bessard
Wed, 30 May 2012 12:20:27 +0200
changeset 699 649399ffdaf0
parent 687 629680fb0582
child 714 131ea7f237b9
permissions -rw-r--r--
Fix bug with cut/copy/paste on PythonEditor
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
     1
#!/usr/bin/env python
f10449b18dbe refactoring
laurent
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
f10449b18dbe refactoring
laurent
parents:
diff changeset
     3
f10449b18dbe refactoring
laurent
parents:
diff changeset
     4
#This library is free software; you can redistribute it and/or
f10449b18dbe refactoring
laurent
parents:
diff changeset
     5
#modify it under the terms of the GNU General Public
f10449b18dbe refactoring
laurent
parents:
diff changeset
     6
#License as published by the Free Software Foundation; either
f10449b18dbe refactoring
laurent
parents:
diff changeset
     7
#version 2.1 of the License, or (at your option) any later version.
f10449b18dbe refactoring
laurent
parents:
diff changeset
     8
#
f10449b18dbe refactoring
laurent
parents:
diff changeset
     9
#This library is distributed in the hope that it will be useful,
f10449b18dbe refactoring
laurent
parents:
diff changeset
    10
#but WITHOUT ANY WARRANTY; without even the implied warranty of
f10449b18dbe refactoring
laurent
parents:
diff changeset
    11
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
f10449b18dbe refactoring
laurent
parents:
diff changeset
    12
#General Public License for more details.
f10449b18dbe refactoring
laurent
parents:
diff changeset
    13
#
f10449b18dbe refactoring
laurent
parents:
diff changeset
    14
#You should have received a copy of the GNU General Public
f10449b18dbe refactoring
laurent
parents:
diff changeset
    15
#License along with this library; if not, write to the Free Software
f10449b18dbe refactoring
laurent
parents:
diff changeset
    16
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
f10449b18dbe refactoring
laurent
parents:
diff changeset
    17
f10449b18dbe refactoring
laurent
parents:
diff changeset
    18
import wx
f10449b18dbe refactoring
laurent
parents:
diff changeset
    19
f10449b18dbe refactoring
laurent
parents:
diff changeset
    20
class CustomTree(wx.TreeCtrl):
f10449b18dbe refactoring
laurent
parents:
diff changeset
    21
    
f10449b18dbe refactoring
laurent
parents:
diff changeset
    22
    def __init__(self, *args, **kwargs):
f10449b18dbe refactoring
laurent
parents:
diff changeset
    23
        wx.TreeCtrl.__init__(self, *args, **kwargs)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    24
        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    25
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    26
        self.BackgroundBitmap = None
f10449b18dbe refactoring
laurent
parents:
diff changeset
    27
        self.BackgroundAlign = wx.ALIGN_LEFT|wx.ALIGN_TOP
f10449b18dbe refactoring
laurent
parents:
diff changeset
    28
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    29
        self.AddMenu = None
687
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    30
        self.Enabled = False
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
    31
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    32
        if wx.Platform == '__WXMSW__':
f10449b18dbe refactoring
laurent
parents:
diff changeset
    33
            self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    34
        else:
f10449b18dbe refactoring
laurent
parents:
diff changeset
    35
            self.Bind(wx.EVT_PAINT, self.OnPaint)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    36
            self.Bind(wx.EVT_SIZE, self.OnResize)
687
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    37
            self.Bind(wx.EVT_SCROLL, self.OnScroll)
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
    38
        self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    39
    
f10449b18dbe refactoring
laurent
parents:
diff changeset
    40
    def SetBackgroundBitmap(self, bitmap, align):
f10449b18dbe refactoring
laurent
parents:
diff changeset
    41
        self.BackgroundBitmap = bitmap
f10449b18dbe refactoring
laurent
parents:
diff changeset
    42
        self.BackgroundAlign = align
f10449b18dbe refactoring
laurent
parents:
diff changeset
    43
    
f10449b18dbe refactoring
laurent
parents:
diff changeset
    44
    def SetAddMenu(self, add_menu):
f10449b18dbe refactoring
laurent
parents:
diff changeset
    45
        self.AddMenu = add_menu
f10449b18dbe refactoring
laurent
parents:
diff changeset
    46
    
687
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    47
    def Enable(self, enabled):
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    48
        self.Enabled = enabled
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    49
    
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
    50
    def GetBitmapRect(self):
f10449b18dbe refactoring
laurent
parents:
diff changeset
    51
        client_size = self.GetClientSize()
f10449b18dbe refactoring
laurent
parents:
diff changeset
    52
        bitmap_size = self.BackgroundBitmap.GetSize()
f10449b18dbe refactoring
laurent
parents:
diff changeset
    53
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    54
        if self.BackgroundAlign & wx.ALIGN_RIGHT:
f10449b18dbe refactoring
laurent
parents:
diff changeset
    55
            x = client_size[0] - bitmap_size[0]
f10449b18dbe refactoring
laurent
parents:
diff changeset
    56
        elif self.BackgroundAlign & wx.ALIGN_CENTER_HORIZONTAL:
f10449b18dbe refactoring
laurent
parents:
diff changeset
    57
            x = (client_size[0] - bitmap_size[0]) / 2
f10449b18dbe refactoring
laurent
parents:
diff changeset
    58
        else:
f10449b18dbe refactoring
laurent
parents:
diff changeset
    59
            x = 0
f10449b18dbe refactoring
laurent
parents:
diff changeset
    60
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    61
        if self.BackgroundAlign & wx.ALIGN_BOTTOM:
f10449b18dbe refactoring
laurent
parents:
diff changeset
    62
            y = client_size[1] - bitmap_size[1]
f10449b18dbe refactoring
laurent
parents:
diff changeset
    63
        elif self.BackgroundAlign & wx.ALIGN_CENTER_VERTICAL:
f10449b18dbe refactoring
laurent
parents:
diff changeset
    64
            y = (client_size[1] - bitmap_size[1]) / 2
f10449b18dbe refactoring
laurent
parents:
diff changeset
    65
        else:
f10449b18dbe refactoring
laurent
parents:
diff changeset
    66
            y = 0
f10449b18dbe refactoring
laurent
parents:
diff changeset
    67
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    68
        return wx.Rect(x, y, bitmap_size[0], bitmap_size[1])
f10449b18dbe refactoring
laurent
parents:
diff changeset
    69
    
f10449b18dbe refactoring
laurent
parents:
diff changeset
    70
    def RefreshBackground(self, refresh_base=False):
f10449b18dbe refactoring
laurent
parents:
diff changeset
    71
        dc = wx.ClientDC(self)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    72
        dc.Clear()
f10449b18dbe refactoring
laurent
parents:
diff changeset
    73
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    74
        bitmap_rect = self.GetBitmapRect()
f10449b18dbe refactoring
laurent
parents:
diff changeset
    75
        dc.DrawBitmap(self.BackgroundBitmap, bitmap_rect.x, bitmap_rect.y)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    76
        
f10449b18dbe refactoring
laurent
parents:
diff changeset
    77
        if refresh_base:
f10449b18dbe refactoring
laurent
parents:
diff changeset
    78
            self.Refresh(False)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    79
    
f10449b18dbe refactoring
laurent
parents:
diff changeset
    80
    def OnEraseBackground(self, event):
f10449b18dbe refactoring
laurent
parents:
diff changeset
    81
        self.RefreshBackground(True)
f10449b18dbe refactoring
laurent
parents:
diff changeset
    82
    
f10449b18dbe refactoring
laurent
parents:
diff changeset
    83
    def OnLeftUp(self, event):
687
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    84
        if self.Enabled:
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    85
            pos = event.GetPosition()
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    86
            item, flags = self.HitTest(pos)
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    87
            
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    88
            bitmap_rect = self.GetBitmapRect()
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    89
            if (bitmap_rect.InsideXY(pos.x, pos.y) or 
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    90
                flags & wx.TREE_HITTEST_NOWHERE) and self.AddMenu is not None:
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    91
                self.PopupMenuXY(self.AddMenu, pos.x, pos.y)
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    92
        event.Skip()
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    93
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    94
    def OnScroll(self, event):
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    95
        print "scroll event"
629680fb0582 refactoring
laurent
parents: 684
diff changeset
    96
        self.RefreshBackground(True)
684
f10449b18dbe refactoring
laurent
parents:
diff changeset
    97
        event.Skip()
f10449b18dbe refactoring
laurent
parents:
diff changeset
    98
f10449b18dbe refactoring
laurent
parents:
diff changeset
    99
    def OnResize(self, event):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   100
        self.RefreshBackground(True)
f10449b18dbe refactoring
laurent
parents:
diff changeset
   101
        event.Skip()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   102
    
f10449b18dbe refactoring
laurent
parents:
diff changeset
   103
    def OnPaint(self, event):
f10449b18dbe refactoring
laurent
parents:
diff changeset
   104
        self.RefreshBackground()
f10449b18dbe refactoring
laurent
parents:
diff changeset
   105
        event.Skip()