# Inspired By And Heavily Based On wxGenericTreeCtrl.
#
# Andrea Gavana, @ 17 May 2006
-# Latest Revision: 26 May 2006, 22.30 CET
+# Latest Revision: 16 Apr 2007, 11.00 CET
#
#
# TODO List
* Mac OS (Thanks to John Jackson).
-Latest Revision: Andrea Gavana @ 26 May 2006, 22.30 CET
-Version 0.8
+Latest Revision: Andrea Gavana @ 16 Apr 2007, 11.00 CET
+Version 1.0
"""
else:
- raise "\n ERROR: You Must Create An Image List To Use Images!"
+ raise Exception("\n ERROR: You Must Create An Image List To Use Images!")
checkimage = item.GetCurrentCheckedImage()
self._wnd = wnd # are we holding a window?
if wnd:
- if wnd.GetSizer(): # the window is a complex one hold by a sizer
- size = wnd.GetBestSize()
- else: # simple window, without sizers
- size = wnd.GetSize()
-
- # We have to bind the wx.EVT_SET_FOCUS for the associated window
- # No other solution to handle the focus changing from an item in
- # CustomTreeCtrl and the window associated to an item
- # Do better strategies exist?
- self._wnd.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
-
- self._height = size.GetHeight() + 2
- self._width = size.GetWidth()
- self._windowsize = size
-
- # We don't show the window if the item is collapsed
- if self._isCollapsed:
- self._wnd.Show(False)
-
- # The window is enabled only if the item is enabled
- self._wnd.Enable(self._enabled)
- self._windowenabled = self._enabled
+ self.SetWindow(wnd)
def IsOk(self):
self._wnd = wnd
+ if wnd.GetSizer(): # the window is a complex one hold by a sizer
+ size = wnd.GetBestSize()
+ else: # simple window, without sizers
+ size = wnd.GetSize()
+
+ # We have to bind the wx.EVT_SET_FOCUS for the associated window
+ # No other solution to handle the focus changing from an item in
+ # CustomTreeCtrl and the window associated to an item
+ # Do better strategies exist?
+ self._wnd.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
+
+ self._height = size.GetHeight() + 2
+ self._width = size.GetWidth()
+ self._windowsize = size
+
+ # We don't show the window if the item is collapsed
+ if self._isCollapsed:
+ self._wnd.Show(False)
+
+ # The window is enabled only if the item is enabled
+ self._wnd.Enable(self._enabled)
+ self._windowenabled = self._enabled
+
def GetWindow(self):
"""Returns the window associated to the item."""
return self._wnd
+ def DeleteWindow(self):
+ """Deletes the window associated to the item (if any)."""
+
+ if self._wnd:
+ self._wnd.Destroy()
+ self._wnd = None
+
+
def GetWindowEnabled(self):
"""Returns whether the associated window is enabled or not."""
if not self._wnd:
- raise "\nERROR: This Item Has No Window Associated"
+ raise Exception("\nERROR: This Item Has No Window Associated")
return self._windowenabled
"""Sets whether the associated window is enabled or not."""
if not self._wnd:
- raise "\nERROR: This Item Has No Window Associated"
+ raise Exception("\nERROR: This Item Has No Window Associated")
self._windowenabled = enable
self._wnd.Enable(enable)
class CustomTreeCtrl(wx.PyScrolledWindow):
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize,
- style=0, ctstyle=TR_DEFAULT_STYLE, validator=wx.DefaultValidator,
+ style=TR_DEFAULT_STYLE, ctstyle=0, validator=wx.DefaultValidator,
name="CustomTreeCtrl"):
"""
Default class constructor.
size: window size. If the default size (-1, -1) is specified then the window is sized appropriately.
- style: the underlying wx.ScrolledWindow style
+ style: the underlying wx.ScrolledWindow style + CustomTreeCtrl window style. This can be one of:
- ctstyle: CustomTreeCtrl window style. This can be one of:
TR_NO_BUTTONS
TR_HAS_BUTTONS # draw collapsed/expanded btns
TR_NO_LINES # don't draw lines at all
TR_AUTO_CHECK_PARENT # only meaningful for checkboxes
TR_AUTO_TOGGLE_CHILD # only meaningful for checkboxes
+ ctstyle: kept for backward compatibility.
+
validator: window validator.
name: window name.
"""
+
+ style = style | ctstyle
self._current = self._key_current = self._anchor = self._select_me = None
self._hasFocus = False
btnshadow = wx.SystemSettings_GetColour(wx.SYS_COLOUR_BTNSHADOW)
self._hilightUnfocusedBrush = wx.Brush(btnshadow)
r, g, b = btnshadow.Red(), btnshadow.Green(), btnshadow.Blue()
- backcolour = ((r >> 1) - 20, (g >> 1) - 20, (b >> 1) - 20)
+ backcolour = (max((r >> 1) - 20, 0),
+ max((g >> 1) - 20, 0),
+ max((b >> 1) - 20, 0))
backcolour = wx.Colour(backcolour[0], backcolour[1], backcolour[2])
self._hilightUnfocusedBrush2 = wx.Brush(backcolour)
self._itemWithWindow = []
if wx.Platform == "__WXMAC__":
- ctstyle &= ~TR_LINES_AT_ROOT
- ctstyle |= TR_NO_LINES
+ style &= ~TR_LINES_AT_ROOT
+ style |= TR_NO_LINES
platform, major, minor = wx.GetOsVersion()
if major < 10:
- ctstyle |= TR_ROW_LINES
+ style |= TR_ROW_LINES
- self._windowStyle = ctstyle
+ self._windowStyle = style
# Create the default check image list
self.SetImageListCheck(13, 13)
"""Toggles the item selection."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
self.SelectItem(item, not self.IsSelected(item))
"""Enables/disables an item."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
if item.IsEnabled() == enable:
return
self.RefreshLine(item)
- def IsEnabled(self, item):
+ def IsItemEnabled(self, item):
"""Returns whether an item is enabled or disabled."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.IsEnabled()
"""Returns whether an item is checked or not."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.IsChecked()
"""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
# Should we raise an error here?!?
if item.GetType() == 0:
"""Transverses the tree and toggles the items. Meaningful only for check items."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
child, cookie = self.GetFirstChild(item)
"""Transverses the tree and checks/unchecks the items. Meaningful only for check items."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
(child, cookie) = self.GetFirstChild(item)
Meaningful only for check items."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
parent = item.GetParent()
if not parent or parent.GetType() != 1:
"""Programatically check/uncheck item children. Does not generate EVT_TREE_CHECK* events."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
if checked == None:
self.AutoToggleChild(item)
"""Starts editing an item label."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
self.Edit(item)
"""Returns whether an item has children or not."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return len(item.GetChildren()) > 0
"""Gets the item children count."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.GetChildrenCount(recursively)
"""Returns the item text."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.GetText()
- def GetItemImage(self, item, which):
+ def GetItemImage(self, item, which=TreeItemIcon_Normal):
"""Returns the item image."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.GetImage(which)
"""Returns the data associated to an item."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.GetData()
"""Returns the item text colour."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.Attr().GetTextColour()
"""Returns the item background colour."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.Attr().GetBackgroundColour()
"""Returns the item font."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.Attr().GetFont()
"""Returns whether an item is hypertext or not."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.IsHyperText()
"""Sets the item text."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
dc = wx.ClientDC(self)
item.SetText(text)
"""Sets the item image, depending on the item state."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
item.SetImage(image, which)
"""Sets the data associated to an item."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
item.SetData(data)
"""Forces the appearance of the button next to the item."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
item.SetHasPlus(has)
self.RefreshLine(item)
"""Sets the item font bold/unbold."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
# avoid redrawing the tree if no real change
if item.IsBold() != bold:
"""Sets the item font italic/non-italic."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
if item.IsItalic() != italic:
itemFont = self.GetItemFont(item)
"""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
if highlight:
bg = wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHT)
"""Sets the item text colour."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
if self.GetItemTextColour(item) == col:
return
"""Sets the item background colour."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
item.Attr().SetBackgroundColour(col)
self.RefreshLine(item)
"""Sets whether the item is hypertext or not."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
item.SetHyperText(hyper)
self.RefreshLine(item)
"""Sets the item font."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
if self.GetItemFont(item) == font:
return
"""Sets whether an hypertext item was visited."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
item.SetVisited(visited)
self.RefreshLine(item)
"""Returns whether an hypertext item was visited."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.GetVisited()
"""Returns the window associated to the item (if any)."""
if not item:
- raise "\nERROR: Invalid Item"
+ raise Exception("\nERROR: Invalid Item")
return item.GetWindow()
"""Returns whether the window associated to the item is enabled."""
if not item:
- raise "\nERROR: Invalid Item"
+ raise Exception("\nERROR: Invalid Item")
return item.GetWindowEnabled()
"""Enables/disables the window associated to the item."""
if not item:
- raise "\nERROR: Invalid Item"
+ raise Exception("\nERROR: Invalid Item")
item.SetWindowEnabled(enable)
"""
if not item:
- raise "\nERROR: Invalid Item"
+ raise Exception("\nERROR: Invalid Item")
return item.GetType()
"""Returns whether the item is visible or not."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
# An item is only visible if it's not a descendant of a collapsed item
parent = item.GetParent()
"""Returns whether the item has children or not."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
# consider that the item does have children if it has the "+" button: it
# might not have them (if it had never been expanded yet) but then it
"""Returns whether the item is expanded or not."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.IsExpanded()
"""Returns whether the item is selected or not."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.IsSelected()
"""Returns whether the item font is bold or not."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.IsBold()
"""Returns whether the item font is italic or not."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.IsItalic()
"""Gets the item parent."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
return item.GetParent()
"""Gets the item first child."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
cookie = 0
return self.GetNextChild(item, cookie)
"""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
children = item.GetChildren()
"""Gets the item last child."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
children = item.GetChildren()
return (len(children) == 0 and [None] or [children[-1]])[0]
"""Gets the next sibling of an item."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
i = item
parent = i.GetParent()
"""Gets the previous sibling of an item."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
i = item
parent = i.GetParent()
"""Gets the next item. Only for internal use right now."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
i = item
"""Returns the next visible item."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
id = item
def GetPrevVisible(self, item):
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
- raise "\nERROR: Not Implemented"
+ raise Exception("\nERROR: Not Implemented")
return None
"""Actually inserts an item in the tree."""
if wnd is not None and not (self._windowStyle & TR_HAS_VARIABLE_ROW_HEIGHT):
- raise "\nERROR: In Order To Append/Insert Controls You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT"
+ raise Exception("\nERROR: In Order To Append/Insert Controls You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT")
if text.find("\n") >= 0 and not (self._windowStyle & TR_HAS_VARIABLE_ROW_HEIGHT):
- raise "\nERROR: In Order To Append/Insert A MultiLine Text You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT"
+ raise Exception("\nERROR: In Order To Append/Insert A MultiLine Text You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT")
if ct_type < 0 or ct_type > 2:
- raise "\nERROR: Item Type Should Be 0 (Normal), 1 (CheckBox) or 2 (RadioButton). "
+ raise Exception("\nERROR: Item Type Should Be 0 (Normal), 1 (CheckBox) or 2 (RadioButton). ")
parent = parentId
"""Adds a root to the CustomTreeCtrl. Only one root must exist."""
if self._anchor:
- raise "\nERROR: Tree Can Have Only One Root"
+ raise Exception("\nERROR: Tree Can Have Only One Root")
if wnd is not None and not (self._windowStyle & TR_HAS_VARIABLE_ROW_HEIGHT):
- raise "\nERROR: In Order To Append/Insert Controls You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT"
+ raise Exception("\nERROR: In Order To Append/Insert Controls You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT")
if text.find("\n") >= 0 and not (self._windowStyle & TR_HAS_VARIABLE_ROW_HEIGHT):
- raise "\nERROR: In Order To Append/Insert A MultiLine Text You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT"
+ raise Exception("\nERROR: In Order To Append/Insert A MultiLine Text You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT")
if ct_type < 0 or ct_type > 2:
- raise "\nERROR: Item Type Should Be 0 (Normal), 1 (CheckBox) or 2 (RadioButton). "
+ raise Exception("\nERROR: Item Type Should Be 0 (Normal), 1 (CheckBox) or 2 (RadioButton). ")
self._dirty = True # do this first so stuff below doesn't cause flicker
"""Appends an item as a first child of parent."""
if wnd is not None and not (self._windowStyle & TR_HAS_VARIABLE_ROW_HEIGHT):
- raise "\nERROR: In Order To Append/Insert Controls You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT"
+ raise Exception("\nERROR: In Order To Append/Insert Controls You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT")
if text.find("\n") >= 0 and not (self._windowStyle & TR_HAS_VARIABLE_ROW_HEIGHT):
- raise "\nERROR: In Order To Append/Insert A MultiLine Text You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT"
+ raise Exception("\nERROR: In Order To Append/Insert A MultiLine Text You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT")
return self.DoInsertItem(parent, 0, text, ct_type, wnd, image, selImage, data)
"""Auxiliary function to cope with the C++ hideous multifunction."""
if wnd is not None and not (self._windowStyle & TR_HAS_VARIABLE_ROW_HEIGHT):
- raise "\nERROR: In Order To Append/Insert Controls You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT"
+ raise Exception("\nERROR: In Order To Append/Insert Controls You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT")
if text.find("\n") >= 0 and not (self._windowStyle & TR_HAS_VARIABLE_ROW_HEIGHT):
- raise "\nERROR: In Order To Append/Insert A MultiLine Text You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT"
+ raise Exception("\nERROR: In Order To Append/Insert A MultiLine Text You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT")
parent = parentId
try:
index = parent.GetChildren().index(idPrevious)
except:
- raise "ERROR: Previous Item In CustomTreeCtrl.InsertItem() Is Not A Sibling"
+ raise Exception("ERROR: Previous Item In CustomTreeCtrl.InsertItem() Is Not A Sibling")
return self.DoInsertItem(parentId, index+1, text, ct_type, wnd, image, selImage, data)
"""Auxiliary function to cope with the C++ hideous multifunction."""
if wnd is not None and not (self._windowStyle & TR_HAS_VARIABLE_ROW_HEIGHT):
- raise "\nERROR: In Order To Append/Insert Controls You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT"
+ raise Exception("\nERROR: In Order To Append/Insert Controls You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT")
if text.find("\n") >= 0 and not (self._windowStyle & TR_HAS_VARIABLE_ROW_HEIGHT):
- raise "\nERROR: In Order To Append/Insert A MultiLine Text You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT"
+ raise Exception("\nERROR: In Order To Append/Insert A MultiLine Text You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT")
parent = parentId
"""Inserts an item after the given previous."""
if wnd is not None and not (self._windowStyle & TR_HAS_VARIABLE_ROW_HEIGHT):
- raise "\nERROR: In Order To Append/Insert Controls You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT"
+ raise Exception("\nERROR: In Order To Append/Insert Controls You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT")
if text.find("\n") >= 0 and not (self._windowStyle & TR_HAS_VARIABLE_ROW_HEIGHT):
- raise "\nERROR: In Order To Append/Insert A MultiLine Text You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT"
+ raise Exception("\nERROR: In Order To Append/Insert A MultiLine Text You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT")
if type(input) == type(1):
return self.InsertItemByIndex(parentId, input, text, ct_type, wnd, image, selImage, data)
"""Appends an item as a last child of its parent."""
if wnd is not None and not (self._windowStyle & TR_HAS_VARIABLE_ROW_HEIGHT):
- raise "\nERROR: In Order To Append/Insert Controls You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT"
+ raise Exception("\nERROR: In Order To Append/Insert Controls You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT")
if text.find("\n") >= 0 and not (self._windowStyle & TR_HAS_VARIABLE_ROW_HEIGHT):
- raise "\nERROR: In Order To Append/Insert A MultiLine Text You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT"
+ raise Exception("\nERROR: In Order To Append/Insert A MultiLine Text You Have To Use The Style TR_HAS_VARIABLE_ROW_HEIGHT")
parent = parentId
event = TreeEvent(wxEVT_TREE_DELETE_ITEM, self.GetId())
event._item = item
event.SetEventObject(self)
- self.ProcessEvent(event)
+ self.GetEventHandler().ProcessEvent(event)
def IsDescendantOf(self, parent, item):
"""Delete item children."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
self._dirty = True # do this first so stuff below doesn't cause flicker
"""Delete an item."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
self._dirty = True # do this first so stuff below doesn't cause flicker
"""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
if self.HasFlag(TR_HIDE_ROOT) and item == self.GetRootItem():
- raise "\nERROR: Can't Expand An Hidden Root. "
+ raise Exception("\nERROR: Can't Expand An Hidden Root. ")
if not item.HasPlus():
return
event._item = item
event.SetEventObject(self)
- if self.ProcessEvent(event) and not event.IsAllowed():
+ if self.GetEventHandler().ProcessEvent(event) and not event.IsAllowed():
# cancelled by program
return
self.HideWindows()
event.SetEventType(wxEVT_TREE_ITEM_EXPANDED)
- self.ProcessEvent(event)
+ self.GetEventHandler().ProcessEvent(event)
- def ExpandAll(self, item):
- """Expands all the items."""
+ def ExpandAllChildren(self, item):
+ """Expands all the items children of the input item."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
if not self.HasFlag(TR_HIDE_ROOT) or item != self.GetRootItem():
self.Expand(item)
child, cookie = self.GetFirstChild(item)
while child:
- self.ExpandAll(child)
+ self.ExpandAllChildren(child)
child, cookie = self.GetNextChild(item, cookie)
+ def ExpandAll(self):
+ """Expands all CustomTreeCtrl items."""
+
+ if self._anchor:
+ self.ExpandAllChildren(self._anchor)
+
+
def Collapse(self, item):
"""
Collapse an item, sending a EVT_TREE_ITEM_COLLAPSING and
"""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
if self.HasFlag(TR_HIDE_ROOT) and item == self.GetRootItem():
- raise "\nERROR: Can't Collapse An Hidden Root. "
+ raise Exception("\nERROR: Can't Collapse An Hidden Root. ")
if not item.IsExpanded():
return
event = TreeEvent(wxEVT_TREE_ITEM_COLLAPSING, self.GetId())
event._item = item
event.SetEventObject(self)
- if self.ProcessEvent(event) and not event.IsAllowed():
+ if self.GetEventHandler().ProcessEvent(event) and not event.IsAllowed():
# cancelled by program
return
self.HideWindows()
event.SetEventType(wxEVT_TREE_ITEM_COLLAPSED)
- self.ProcessEvent(event)
+ self.GetEventHandler().ProcessEvent(event)
def CollapseAndReset(self, item):
# the tree might not have the root item at all
if rootItem:
self.UnselectAllChildren(rootItem)
-
+
+ self.Unselect()
# Recursive function !
# To stop we must have crt_item<last_item
"""Actually selects/unselects an item, sending a EVT_TREE_SEL_CHANGED event."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
self._select_me = None
"""Selects/deselects an item."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
if select:
if item.IsSelected():
array.append(item)
- if item.HasChildren():
+ if item.HasChildren() and item.IsExpanded():
for child in item.GetChildren():
array = self.FillArray(child, array)
"""Ensure that an item is visible in CustomTreeCtrl."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
# first expand all parent branches
parent = item.GetParent()
"""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
children = item.GetChildren()
self._imageListNormal = imageList
self._ownsImageListNormal = False
self._dirty = True
+
# Don't do any drawing if we're setting the list to NULL,
# since we may be in the process of deleting the tree control.
if imageList:
self.CalculateLineHeight()
- # We gray out the image list to use the grayed icons with disabled items
- self._grayedImageList = wx.ImageList(16, 16, True, 0)
-
- for ii in xrange(imageList.GetImageCount()):
-
- bmp = imageList.GetBitmap(ii)
- image = wx.ImageFromBitmap(bmp)
- image = GrayOut(image)
- newbmp = wx.BitmapFromImage(image)
- self._grayedImageList.Add(newbmp)
+ # We gray out the image list to use the grayed icons with disabled items
+ sz = imageList.GetSize(0)
+ self._grayedImageList = wx.ImageList(sz[0], sz[1], True, 0)
+
+ for ii in xrange(imageList.GetImageCount()):
+ bmp = imageList.GetBitmap(ii)
+ image = wx.ImageFromBitmap(bmp)
+ image = GrayOut(image)
+ newbmp = wx.BitmapFromImage(image)
+ self._grayedImageList.Add(newbmp)
def SetStateImageList(self, imageList):
r2, g2, b2 = int(bottom.Red()), int(bottom.Green()), int(bottom.Blue())
flrect = float(filRect.height)
+ if flrect < 1:
+ flrect = self._lineHeight
rstep = float((r2 - r1)) / flrect
gstep = float((g2 - g1)) / flrect
dc.SetTextForeground(self.GetHyperTextVisitedColour())
else:
dc.SetTextForeground(self.GetHyperTextNewColour())
-
+
text_w, text_h, dummy = dc.GetMultiLineTextExtent(item.GetText())
image = item.GetCurrentImage()
wx.RendererNative.Get().DrawItemSelectionRect(self, dc, itemrect, flags)
else:
dc.DrawRectangleRect(itemrect)
-
+
else:
if item.IsSelected():
if wnd:
wndx = wcheck + image_w + item.GetX() + text_w + 4
xa, ya = self.CalcScrolledPosition((0, item.GetY()))
+ wndx += xa
+ if item.GetHeight() > item.GetWindowSize()[1]:
+ ya += (item.GetHeight() - item.GetWindowSize()[1])/2
+
if not wnd.IsShown():
wnd.Show()
if wnd.GetPosition() != (wndx, ya):
# draw line down to last child
origY += self.GetLineHeight(children[0])>>1
oldY += self.GetLineHeight(children[n-1])>>1
+ oldPen = dc.GetPen()
+ dc.SetPen(self._dottedPen)
dc.DrawLine(3, origY, 3, oldY)
+ dc.SetPen(oldPen)
return y
keyCode = event.GetKeyCode()
if keyCode in [ord("+"), wx.WXK_ADD]: # "+"
- if self._current.HasPlus() and not self.IsExpanded(self._current) and self.IsEnabled(self._current):
+ if self._current.HasPlus() and not self.IsExpanded(self._current) and self.IsItemEnabled(self._current):
self.Expand(self._current)
elif keyCode in [ord("*"), wx.WXK_MULTIPLY]: # "*"
- if not self.IsExpanded(self._current) and self.IsEnabled(self._current):
+ if not self.IsExpanded(self._current) and self.IsItemEnabled(self._current):
# expand all
self.ExpandAll(self._current)
elif keyCode in [wx.WXK_RETURN, wx.WXK_SPACE]:
- if not self.IsEnabled(self._current):
+ if not self.IsItemEnabled(self._current):
event.Skip()
return
if prev:
current = self._key_current
# TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
- if current == self.GetFirstChild(prev)[0] and self.IsEnabled(prev):
+ if current == self.GetFirstChild(prev)[0] and self.IsItemEnabled(prev):
# otherwise we return to where we came from
self.DoSelectItem(prev, unselect_others, extended_select)
self._key_current = prev
current = prev
# Try to get the previous siblings and see if they are active
- while prev and not self.IsEnabled(prev):
+ while prev and not self.IsItemEnabled(prev):
prev = self.GetPrevSibling(prev)
if not prev:
# No previous siblings active: go to the parent and up
prev = self.GetItemParent(current)
- while prev and not self.IsEnabled(prev):
+ while prev and not self.IsItemEnabled(prev):
prev = self.GetItemParent(prev)
if prev:
if self.IsExpanded(self._current):
self.Collapse(self._current)
else:
- if prev and self.IsEnabled(prev):
+ if prev and self.IsItemEnabled(prev):
self.DoSelectItem(prev, unselect_others, extended_select)
elif keyCode == wx.WXK_RIGHT:
# also expand the item if it wasn't expanded yet
if self.IsExpanded(self._current) and self.HasChildren(self._current):
child, cookie = self.GetFirstChild(self._key_current)
- if self.IsEnabled(child):
+ if self.IsItemEnabled(child):
self.DoSelectItem(child, unselect_others, extended_select)
self._key_current = child
else:
current = self.GetItemParent(current)
if current:
next = self.GetNextSibling(current)
- if not next or not self.IsEnabled(next):
+ if not next or not self.IsItemEnabled(next):
next = None
else:
- while next and not self.IsEnabled(next):
+ while next and not self.IsItemEnabled(next):
next = self.GetNext(next)
if next:
last = lastChild
- if last and self.IsEnabled(last):
+ if last and self.IsItemEnabled(last):
self.DoSelectItem(last, unselect_others, extended_select)
if not prev:
return
- if self.IsEnabled(prev):
+ if self.IsItemEnabled(prev):
self.DoSelectItem(prev, unselect_others, extended_select)
else:
# no such item
return
- if self.IsEnabled(id):
+ if self.IsItemEnabled(id):
self.SelectItem(id)
self._findPrefix += ch
while 1:
child = sibling(item)
- if (child and self.IsEnabled(child)) or not child:
+ if (child and self.IsItemEnabled(child)) or not child:
break
item = child
# Tha's not a radiobutton... but some of its children can be
# inactive
child, cookie = self.GetFirstChild(item)
- while child and not self.IsEnabled(child):
+ while child and not self.IsItemEnabled(child):
child, cookie = self.GetNextChild(item, cookie)
- if child and self.IsEnabled(child):
+ if child and self.IsItemEnabled(child):
return child
return None
flags = TREE_HITTEST_NOWHERE
return None, flags
- if not self.IsEnabled(hit):
+ if not self.IsItemEnabled(hit):
return None, flags
return hit, flags
"""Gets the bounding rectangle of the item."""
if not item:
- raise "\nERROR: Invalid Tree Item. "
+ raise Exception("\nERROR: Invalid Tree Item. ")
i = item
item.SetHeight(total_h)
else:
item.SetWidth(item.GetWindowSize()[0]+image_w+text_w+wcheck+2)
+ item.SetHeight(max(total_h, item.GetWindowSize()[1]))
def CalculateLevel(self, item, dc, level, y):
"""Thaw CustomTreeCtrl."""
if self._freezeCount == 0:
- raise "\nERROR: Thawing Unfrozen Tree Control?"
+ raise Exception("\nERROR: Thawing Unfrozen Tree Control?")
self._freezeCount = self._freezeCount - 1
attr.font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
return attr
+ GetClassDefaultAttributes = classmethod(GetClassDefaultAttributes)
+