Making a tree control user editable

The tree control can be set up to allow the user to edit the text display of the various tree items. This allows you to make the control interactive, so the user can make changes to the data on the fly. This may be useful in a DOM or XML editor, allowing the user to change the document directly from the structured view.

This functionality is enabled by creating the tree control with the style flag wx.TR_EDIT_LABELS. With this flag set, the tree control behaves similarly to the editable list control. Selecting a tree item gives the user a text control to edit the text. Pressing escape cancels the edit without changing the item. Leaving the edit session in any other way (such as pressing Enter or clicking outside the text control) okays the edit and changes the tree item.

You can programmatically start an edit on a particular tree item with the method EditLabel(item). The item parameter is the wx.TreeItemId of the item you wish to edit, which triggers the edit process in exactly the same way as if the user had selected the item. To stop the edit from your program, use the method EndEditLabel(cancelEdit). As of this writing, EndEditLabel() only works under Windows systems. In this case, you don't need to pass the item ID—there can only be one active edit at a time, so that's the one that will be ended. The can-celEdit parameter is a Boolean. If it is True, the edit is canceled, and the tree item is not changed, if it is False, the edit is accepted normally. If for some reason you need access to the actual text edit control being used, call the method Get-EditControl() which returns the wx.TextCtrl instance being used for the current edit, or None if there is no current edit. Currently, this method only works on Windows systems.

When an edit session begins, either by user selection or by a call to Edit-Label(), a wx.TreeEvent with event type wx.EVT_TREE_BEGIN_LABEL_EDIT is triggered. If this event is vetoed with the event Veto() method, the edit does not actually start. When the session ends, either by a user click or a call to EndEdit-Label(), an event of type wx.EVT_TREE_END_LABEL_EDlT is triggered. This event can also be vetoed, in which case the edit is canceled, and the tree item is not changed.

Was this article helpful?

0 0

Post a comment