controls/CustomTree.py
author Laurent Bessard
Fri, 24 May 2013 16:29:57 +0200
changeset 1173 ad09b4a755ce
parent 1165 99972084890d
child 1177 4cbbc58b91b4
permissions -rw-r--r--
Move RubberBand from GraphicCommons to individual file
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     1
#!/usr/bin/env python
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     3
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     4
#This library is free software; you can redistribute it and/or
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     5
#modify it under the terms of the GNU General Public
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     6
#License as published by the Free Software Foundation; either
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     7
#version 2.1 of the License, or (at your option) any later version.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     8
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     9
#This library is distributed in the hope that it will be useful,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    10
#but WITHOUT ANY WARRANTY; without even the implied warranty of
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    11
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    12
#General Public License for more details.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    13
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    14
#You should have received a copy of the GNU General Public
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    15
#License along with this library; if not, write to the Free Software
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    16
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    17
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    18
import wx
1164
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    19
import wx.lib.agw.customtreectrl as CT
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    20
1164
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    21
from util.BitmapLibrary import GetBitmap
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    22
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    23
# Customize CustomTreeItem for adding icon on item left
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    24
CT.GenericTreeItem._ExtraImage = None
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    25
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    26
def SetExtraImage(self, image):
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    27
    self._type = 1
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    28
    self._ExtraImage = image
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    29
CT.GenericTreeItem.SetExtraImage = SetExtraImage
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    30
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    31
_DefaultGetCurrentCheckedImage = CT.GenericTreeItem.GetCurrentCheckedImage
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    32
def GetCurrentCheckedImage(self):
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    33
    if self._ExtraImage is not None:
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    34
        return self._ExtraImage
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    35
    return _DefaultGetCurrentCheckedImage(self)
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    36
CT.GenericTreeItem.GetCurrentCheckedImage = GetCurrentCheckedImage
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    37
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    38
class CustomTree(CT.CustomTreeCtrl):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    39
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    40
    def __init__(self, *args, **kwargs):
1164
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    41
        CT.CustomTreeCtrl.__init__(self, *args, **kwargs)
1165
99972084890d Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents: 1164
diff changeset
    42
        #self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    43
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    44
        self.BackgroundBitmap = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    45
        self.BackgroundAlign = wx.ALIGN_LEFT|wx.ALIGN_TOP
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    46
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    47
        self.AddMenu = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    48
        self.Enabled = False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    49
        
1164
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    50
        self.Bind(wx.EVT_SCROLLWIN, self.OnScroll)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    51
        self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    52
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    53
    def SetBackgroundBitmap(self, bitmap, align):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    54
        self.BackgroundBitmap = bitmap
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    55
        self.BackgroundAlign = align
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    56
    
1164
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    57
    def SetImageListCheck(self, sizex, sizey, imglist=None):
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    58
        CT.CustomTreeCtrl.SetImageListCheck(self, sizex, sizey, imglist=None)
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    59
        
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    60
        self.ExtraImages = {}
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    61
        for image in ["function", "functionBlock", "program"]:
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    62
            self.ExtraImages[image] = self._imageListCheck.Add(GetBitmap(image.upper()))
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    63
    
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    64
    def SetItemExtraImage(self, item, bitmap):
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    65
        image = self.ExtraImages.get(bitmap)
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    66
        if image is not None:
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    67
            dc = wx.ClientDC(self)
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    68
            item.SetExtraImage(image)
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    69
            self.CalculateSize(item, dc)
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    70
            self.RefreshLine(item)
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
    71
    
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    72
    def SetAddMenu(self, add_menu):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    73
        self.AddMenu = add_menu
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    74
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    75
    def Enable(self, enabled):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    76
        self.Enabled = enabled
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    77
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    78
    def GetBitmapRect(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    79
        client_size = self.GetClientSize()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    80
        bitmap_size = self.BackgroundBitmap.GetSize()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    81
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    82
        if self.BackgroundAlign & wx.ALIGN_RIGHT:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    83
            x = client_size[0] - bitmap_size[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    84
        elif self.BackgroundAlign & wx.ALIGN_CENTER_HORIZONTAL:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    85
            x = (client_size[0] - bitmap_size[0]) / 2
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    86
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    87
            x = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    88
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    89
        if self.BackgroundAlign & wx.ALIGN_BOTTOM:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    90
            y = client_size[1] - bitmap_size[1]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    91
        elif self.BackgroundAlign & wx.ALIGN_CENTER_VERTICAL:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    92
            y = (client_size[1] - bitmap_size[1]) / 2
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    93
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    94
            y = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    95
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    96
        return wx.Rect(x, y, bitmap_size[0], bitmap_size[1])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    97
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    98
    def OnLeftUp(self, event):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    99
        if self.Enabled:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   100
            pos = event.GetPosition()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   101
            item, flags = self.HitTest(pos)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   102
            
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   103
            bitmap_rect = self.GetBitmapRect()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   104
            if (bitmap_rect.InsideXY(pos.x, pos.y) or 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   105
                flags & wx.TREE_HITTEST_NOWHERE) and self.AddMenu is not None:
1165
99972084890d Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents: 1164
diff changeset
   106
                wx.CallAfter(self.PopupMenuXY, self.AddMenu, pos.x, pos.y)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   107
        event.Skip()
1165
99972084890d Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents: 1164
diff changeset
   108
    
99972084890d Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents: 1164
diff changeset
   109
    def OnEraseBackground(self, event):
99972084890d Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents: 1164
diff changeset
   110
        dc = event.GetDC()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   111
1165
99972084890d Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents: 1164
diff changeset
   112
        if not dc:
99972084890d Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents: 1164
diff changeset
   113
            dc = wx.ClientDC(self)
99972084890d Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents: 1164
diff changeset
   114
            rect = self.GetUpdateRegion().GetBox()
99972084890d Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents: 1164
diff changeset
   115
            dc.SetClippingRect(rect)
99972084890d Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents: 1164
diff changeset
   116
        
99972084890d Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents: 1164
diff changeset
   117
        dc.Clear()
99972084890d Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents: 1164
diff changeset
   118
        
99972084890d Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents: 1164
diff changeset
   119
        bitmap_rect = self.GetBitmapRect()
99972084890d Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents: 1164
diff changeset
   120
        dc.DrawBitmap(self.BackgroundBitmap, bitmap_rect.x, bitmap_rect.y)
99972084890d Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents: 1164
diff changeset
   121
    
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   122
    def OnScroll(self, event):
1164
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
   123
        wx.CallAfter(self.Refresh)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   124
        event.Skip()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   125
1164
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
   126
    def OnSize(self, event):
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
   127
        CT.CustomTreeCtrl.OnSize(self, event)
8fd44bc05aae Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents: 814
diff changeset
   128
        self.Refresh()