introduced a common base class for both MSW and generic wxTreeCtrl implementations

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@35879 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2005-10-10 20:25:02 +00:00
parent d2dfef5f3f
commit 8cee4a304f
6 changed files with 724 additions and 763 deletions

View File

@@ -16,7 +16,6 @@
#include "wx/scrolwin.h"
#include "wx/pen.h"
#include "wx/imaglist.h"
// -----------------------------------------------------------------------------
// forward declaration
@@ -35,12 +34,14 @@ class WXDLLEXPORT wxTextCtrl;
// wxGenericTreeCtrl - the tree control
// -----------------------------------------------------------------------------
class WXDLLEXPORT wxGenericTreeCtrl : public wxScrolledWindow
class WXDLLEXPORT wxGenericTreeCtrl : public wxTreeCtrlBase,
public wxScrollHelper
{
public:
// creation
// --------
wxGenericTreeCtrl() { Init(); }
wxGenericTreeCtrl() : wxTreeCtrlBase(), wxScrollHelper(this) { Init(); }
wxGenericTreeCtrl(wxWindow *parent, wxWindowID id = wxID_ANY,
const wxPoint& pos = wxDefaultPosition,
@@ -48,6 +49,8 @@ public:
long style = wxTR_DEFAULT_STYLE,
const wxValidator &validator = wxDefaultValidator,
const wxString& name = wxTreeCtrlNameStr)
: wxTreeCtrlBase(),
wxScrollHelper(this)
{
Init();
Create(parent, id, pos, size, style, validator, name);
@@ -62,290 +65,127 @@ public:
const wxValidator &validator = wxDefaultValidator,
const wxString& name = wxTreeCtrlNameStr);
// accessors
// ---------
// get the total number of items in the control
size_t GetCount() const;
// implement base class pure virtuals
// ----------------------------------
// indent is the number of pixels the children are indented relative to
// the parents position. SetIndent() also redraws the control
// immediately.
unsigned int GetIndent() const { return m_indent; }
void SetIndent(unsigned int indent);
virtual size_t GetCount() const;
// spacing is the number of pixels between the start and the Text
unsigned int GetSpacing() const { return m_spacing; }
void SetSpacing(unsigned int spacing);
virtual unsigned int GetIndent() const { return m_indent; }
virtual void SetIndent(unsigned int indent);
// image list: these functions allow to associate an image list with
// the control and retrieve it. Note that when assigned with
// SetImageList, the control does _not_ delete
// the associated image list when it's deleted in order to allow image
// lists to be shared between different controls. If you use
// AssignImageList, the control _does_ delete the image list.
//
// The normal image list is for the icons which correspond to the
// normal tree item state (whether it is selected or not).
// Additionally, the application might choose to show a state icon
// which corresponds to an app-defined item state (for example,
// checked/unchecked) which are taken from the state image list.
virtual wxImageList *GetImageList() const;
virtual wxImageList *GetStateImageList() const;
virtual wxImageList *GetButtonsImageList() const;
virtual void SetImageList(wxImageList *imageList);
virtual void SetStateImageList(wxImageList *imageList);
virtual void SetButtonsImageList(wxImageList *imageList);
virtual void AssignImageList(wxImageList *imageList);
virtual void AssignStateImageList(wxImageList *imageList);
virtual void AssignButtonsImageList(wxImageList *imageList);
virtual void SetDropEffectAboveItem( bool above = false ) { m_dropEffectAboveItem = above; }
virtual bool GetDropEffectAboveItem() const { return m_dropEffectAboveItem; }
// Functions to work with tree ctrl items.
// accessors
// ---------
// retrieve item's label
wxString GetItemText(const wxTreeItemId& item) const;
// get one of the images associated with the item (normal by default)
virtual wxString GetItemText(const wxTreeItemId& item) const;
virtual int GetItemImage(const wxTreeItemId& item,
wxTreeItemIcon which = wxTreeItemIcon_Normal) const;
// get the data associated with the item
wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
virtual wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
virtual wxColour GetItemTextColour(const wxTreeItemId& item) const;
virtual wxColour GetItemBackgroundColour(const wxTreeItemId& item) const;
virtual wxFont GetItemFont(const wxTreeItemId& item) const;
// get the item's text colour
wxColour GetItemTextColour(const wxTreeItemId& item) const;
virtual void SetItemText(const wxTreeItemId& item, const wxString& text);
virtual void SetItemImage(const wxTreeItemId& item,
int image,
wxTreeItemIcon which = wxTreeItemIcon_Normal);
virtual void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
// get the item's background colour
wxColour GetItemBackgroundColour(const wxTreeItemId& item) const;
virtual void SetItemHasChildren(const wxTreeItemId& item, bool has = true);
virtual void SetItemBold(const wxTreeItemId& item, bool bold = true);
virtual void SetItemDropHighlight(const wxTreeItemId& item, bool highlight = true);
virtual void SetItemTextColour(const wxTreeItemId& item, const wxColour& col);
virtual void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& col);
virtual void SetItemFont(const wxTreeItemId& item, const wxFont& font);
// get the item's font
wxFont GetItemFont(const wxTreeItemId& item) const;
virtual bool IsVisible(const wxTreeItemId& item) const;
virtual bool ItemHasChildren(const wxTreeItemId& item) const;
virtual bool IsExpanded(const wxTreeItemId& item) const;
virtual bool IsSelected(const wxTreeItemId& item) const;
virtual bool IsBold(const wxTreeItemId& item) const;
// modifiers
// ---------
// set item's label
void SetItemText(const wxTreeItemId& item, const wxString& text);
// get one of the images associated with the item (normal by default)
virtual void SetItemImage(const wxTreeItemId& item, int image,
wxTreeItemIcon which = wxTreeItemIcon_Normal);
// associate some data with the item
void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
// force appearance of [+] button near the item. This is useful to
// allow the user to expand the items which don't have any children now
// - but instead add them only when needed, thus minimizing memory
// usage and loading time.
void SetItemHasChildren(const wxTreeItemId& item, bool has = true);
// the item will be shown in bold
void SetItemBold(const wxTreeItemId& item, bool bold = true);
// the item will be shown with a drop highlight
void SetItemDropHighlight(const wxTreeItemId& item, bool highlight = true);
// set the item's text colour
void SetItemTextColour(const wxTreeItemId& item, const wxColour& col);
// set the item's background colour
void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& col);
// set the item's font (should be of the same height for all items)
void SetItemFont(const wxTreeItemId& item, const wxFont& font);
// set the window font
virtual bool SetFont( const wxFont &font );
// set the styles. No need to specify a GetWindowStyle here since
// the base wxWindow member function will do it for us
void SetWindowStyle(const long styles);
// item status inquiries
// ---------------------
// is the item visible (it might be outside the view or not expanded)?
bool IsVisible(const wxTreeItemId& item) const;
// does the item has any children?
bool HasChildren(const wxTreeItemId& item) const
{ return ItemHasChildren(item); }
bool ItemHasChildren(const wxTreeItemId& item) const;
// is the item expanded (only makes sense if HasChildren())?
bool IsExpanded(const wxTreeItemId& item) const;
// is this item currently selected (the same as has focus)?
bool IsSelected(const wxTreeItemId& item) const;
// is item text in bold font?
bool IsBold(const wxTreeItemId& item) const;
// does the layout include space for a button?
// number of children
// ------------------
// if 'recursively' is false, only immediate children count, otherwise
// the returned number is the number of all items in this branch
size_t GetChildrenCount(const wxTreeItemId& item,
bool recursively = true) const;
virtual size_t GetChildrenCount(const wxTreeItemId& item,
bool recursively = true) const;
// navigation
// ----------
// wxTreeItemId.IsOk() will return false if there is no such item
virtual wxTreeItemId GetRootItem() const { return m_anchor; }
virtual wxTreeItemId GetSelection() const { return m_current; }
virtual size_t GetSelections(wxArrayTreeItemIds&) const;
// get the root tree item
wxTreeItemId GetRootItem() const { return m_anchor; }
virtual wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
virtual wxTreeItemId GetFirstChild(const wxTreeItemId& item,
wxTreeItemIdValue& cookie) const;
virtual wxTreeItemId GetNextChild(const wxTreeItemId& item,
wxTreeItemIdValue& cookie) const;
virtual wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
virtual wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
virtual wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
// get the item currently selected (may return NULL if no selection)
wxTreeItemId GetSelection() const { return m_current; }
virtual wxTreeItemId GetFirstVisibleItem() const;
virtual wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
virtual wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
// get the items currently selected, return the number of such item
size_t GetSelections(wxArrayTreeItemIds&) const;
// get the parent of this item (may return NULL if root)
wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
// for this enumeration function you must pass in a "cookie" parameter
// which is opaque for the application but is necessary for the library
// to make these functions reentrant (i.e. allow more than one
// enumeration on one and the same object simultaneously). Of course,
// the "cookie" passed to GetFirstChild() and GetNextChild() should be
// the same!
// get the first child of this item
wxTreeItemId GetFirstChild(const wxTreeItemId& item,
wxTreeItemIdValue& cookie) const;
// get the next child
wxTreeItemId GetNextChild(const wxTreeItemId& item,
wxTreeItemIdValue& cookie) const;
// get the last child of this item - this method doesn't use cookies
wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
// get the next sibling of this item
wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
// get the previous sibling
wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
// get first visible item
wxTreeItemId GetFirstVisibleItem() const;
// get the next visible item: item must be visible itself!
// see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
// get the previous visible item: item must be visible itself!
wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
// Only for internal use right now, but should probably be public
wxTreeItemId GetNext(const wxTreeItemId& item) const;
// operations
// ----------
// add the root node to the tree
virtual wxTreeItemId AddRoot(const wxString& text,
int image = -1, int selectedImage = -1,
wxTreeItemData *data = NULL);
// insert a new item in as the first child of the parent
virtual wxTreeItemId PrependItem(const wxTreeItemId& parent,
const wxString& text,
int image = -1, int selectedImage = -1,
wxTreeItemData *data = NULL);
virtual void Delete(const wxTreeItemId& item);
virtual void DeleteChildren(const wxTreeItemId& item);
virtual void DeleteAllItems();
// insert a new item after a given one
virtual wxTreeItemId InsertItem(const wxTreeItemId& parent,
const wxTreeItemId& idPrevious,
const wxString& text,
int image = -1, int selectedImage = -1,
wxTreeItemData *data = NULL);
virtual void Expand(const wxTreeItemId& item);
virtual void Collapse(const wxTreeItemId& item);
virtual void CollapseAndReset(const wxTreeItemId& item);
virtual void Toggle(const wxTreeItemId& item);
// insert a new item before the one with the given index
virtual wxTreeItemId InsertItem(const wxTreeItemId& parent,
size_t index,
const wxString& text,
int image = -1, int selectedImage = -1,
wxTreeItemData *data = NULL);
virtual void Unselect();
virtual void UnselectAll();
virtual void SelectItem(const wxTreeItemId& item, bool select = true);
// insert a new item in as the last child of the parent
virtual wxTreeItemId AppendItem(const wxTreeItemId& parent,
const wxString& text,
int image = -1, int selectedImage = -1,
wxTreeItemData *data = NULL);
virtual void EnsureVisible(const wxTreeItemId& item);
virtual void ScrollTo(const wxTreeItemId& item);
// delete this item and associated data if any
void Delete(const wxTreeItemId& item);
// delete all children (but don't delete the item itself)
// NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
void DeleteChildren(const wxTreeItemId& item);
// delete all items from the tree
// NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
void DeleteAllItems();
virtual wxTextCtrl *EditLabel(const wxTreeItemId& item,
wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl));
virtual wxTextCtrl *GetEditControl() const;
virtual void EndEditLabel(const wxTreeItemId& item,
bool discardChanges = false);
virtual void SortChildren(const wxTreeItemId& item);
// items geometry
// --------------
virtual bool GetBoundingRect(const wxTreeItemId& item,
wxRect& rect,
bool textOnly = false) const;
// this version specific methods
// -----------------------------
wxImageList *GetButtonsImageList() const { return m_imageListButtons; }
void SetButtonsImageList(wxImageList *imageList);
void AssignButtonsImageList(wxImageList *imageList);
void SetDropEffectAboveItem( bool above = false ) { m_dropEffectAboveItem = above; }
bool GetDropEffectAboveItem() const { return m_dropEffectAboveItem; }
wxTreeItemId GetNext(const wxTreeItemId& item) const;
// expand this item
void Expand(const wxTreeItemId& item);
// expand this item and all subitems recursively
void ExpandAll(const wxTreeItemId& item);
// collapse the item without removing its children
void Collapse(const wxTreeItemId& item);
// collapse the item and remove all children
void CollapseAndReset(const wxTreeItemId& item);
// toggles the current state
void Toggle(const wxTreeItemId& item);
// remove the selection from currently selected item (if any)
void Unselect();
// or from the given one (multiselect mode only)
void UnselectItem(const wxTreeItemId& item) { SelectItem(item, false); }
// or from all
void UnselectAll();
// select this item
void SelectItem(const wxTreeItemId& item, bool select = true);
// toggle the item selection
void ToggleItemSelection(const wxTreeItemId& item)
{
SelectItem(item, !IsSelected(item));
}
// make sure this item is visible (expanding the parent item and/or
// scrolling to this item if necessary)
void EnsureVisible(const wxTreeItemId& item);
// scroll to this item (but don't expand its parent)
void ScrollTo(const wxTreeItemId& item);
void AdjustMyScrollbars();
// The first function is more portable (because easier to implement
// on other platforms), but the second one returns some extra info.
wxTreeItemId HitTest(const wxPoint& point)
{ int dummy; return HitTest(point, dummy); }
wxTreeItemId HitTest(const wxPoint& point, int& flags);
// get the bounding rectangle of the item (or of its label only)
bool GetBoundingRect(const wxTreeItemId& item,
wxRect& rect,
bool textOnly = false) const;
// Start editing the item label: this (temporarily) replaces the item
// with a one line edit control. The item will be selected if it hadn't
// been before.
void EditLabel( const wxTreeItemId& item ) { Edit( item ); }
void Edit( const wxTreeItemId& item );
// returns a pointer to the text edit control if the item is being
// edited, NULL otherwise (it's assumed that no more than one item may
// be edited simultaneously)
wxTextCtrl* GetEditControl() const;
// sorting
// this function is called to compare 2 items and should return -1, 0
// or +1 if the first item is less than, equal to or greater than the
// second one. The base class version performs alphabetic comparaison
// of item labels (GetText)
virtual int OnCompareItems(const wxTreeItemId& item1,
const wxTreeItemId& item2);
// sort the children of this item using OnCompareItems
//
// NB: this function is not reentrant and not MT-safe (FIXME)!
void SortChildren(const wxTreeItemId& item);
#if WXWIN_COMPATIBILITY_2_6
// use EditLabel() instead
void Edit( const wxTreeItemId& item ) { EditLabel(item); }
#endif // WXWIN_COMPATIBILITY_2_6
#if WXWIN_COMPATIBILITY_2_4
// deprecated functions: use Set/GetItemImage directly
@@ -362,8 +202,6 @@ public:
long& cookie) const;
#endif // WXWIN_COMPATIBILITY_2_4
virtual bool ShouldInheritColours() const { return false; }
// implementation only from now on
// overridden base class virtuals
@@ -373,6 +211,9 @@ public:
virtual void Freeze();
virtual void Thaw();
virtual bool SetFont( const wxFont &font );
virtual void SetWindowStyle(const long styles);
// callbacks
void OnPaint( wxPaintEvent &event );
void OnSetFocus( wxFocusEvent &event );
@@ -393,6 +234,10 @@ public:
GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL);
// implementation helpers
void AdjustMyScrollbars();
WX_FORWARD_TO_SCROLL_HELPER()
protected:
friend class wxGenericTreeItem;
friend class wxTreeRenameTimer;
@@ -408,21 +253,16 @@ protected:
// A hint to select a parent item after deleting a child
*m_select_me;
unsigned short m_indent;
unsigned short m_spacing;
int m_lineHeight;
wxPen m_dottedPen;
wxBrush *m_hilightBrush,
*m_hilightUnfocusedBrush;
bool m_hasFocus;
bool m_dirty;
bool m_ownsImageListNormal,
m_ownsImageListState,
m_ownsImageListButtons;
bool m_ownsImageListButtons;
bool m_isDragging; // true between BEGIN/END drag events
bool m_lastOnSame; // last click on the same item as prev
wxImageList *m_imageListNormal,
*m_imageListState,
*m_imageListButtons;
wxImageList *m_imageListButtons;
int m_freezeCount;
int m_dragCount;
@@ -455,11 +295,18 @@ protected:
bool unselect_others = true,
bool extended_select = false);
wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
size_t previous,
const wxString& text,
int image, int selectedImage,
wxTreeItemData *data);
virtual wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
size_t previous,
const wxString& text,
int image,
int selectedImage,
wxTreeItemData *data);
virtual wxTreeItemId DoInsertAfter(const wxTreeItemId& parent,
const wxTreeItemId& idPrevious,
const wxString& text,
int image = -1, int selImage = -1,
wxTreeItemData *data = NULL);
virtual wxTreeItemId DoHitTest(const wxPoint& point, int& flags);
// called by wxTextTreeCtrl when it marks itself for deletion
void ResetTextControl();

View File

@@ -49,7 +49,7 @@ WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr *, wxMapTreeAttr);
// wxTreeCtrl
// ----------------------------------------------------------------------------
class WXDLLEXPORT wxTreeCtrl : public wxControl
class WXDLLEXPORT wxTreeCtrl : public wxTreeCtrlBase
{
public:
// creation
@@ -75,285 +75,114 @@ public:
const wxValidator& validator = wxDefaultValidator,
const wxString& name = wxTreeCtrlNameStr);
// accessors
// ---------
// implement base class pure virtuals
// ----------------------------------
// get the total number of items in the control
size_t GetCount() const;
virtual size_t GetCount() const;
// indent is the number of pixels the children are indented relative to
// the parents position. SetIndent() also redraws the control
// immediately.
unsigned int GetIndent() const;
void SetIndent(unsigned int indent);
// spacing is the number of pixels between the start and the Text
// not implemented under wxMSW
unsigned int GetSpacing() const { return 18; } // return wxGTK default
void SetSpacing(unsigned int WXUNUSED(spacing)) { }
// image list: these functions allow to associate an image list with
// the control and retrieve it. Note that the control does _not_ delete
// the associated image list when it's deleted in order to allow image
// lists to be shared between different controls.
//
// The normal image list is for the icons which correspond to the
// normal tree item state (whether it is selected or not).
// Additionally, the application might choose to show a state icon
// which corresponds to an app-defined item state (for example,
// checked/unchecked) which are taken from the state image list.
virtual wxImageList *GetImageList() const;
virtual wxImageList *GetStateImageList() const;
virtual unsigned int GetIndent() const;
virtual void SetIndent(unsigned int indent);
virtual void SetImageList(wxImageList *imageList);
virtual void SetStateImageList(wxImageList *imageList);
virtual void AssignImageList(wxImageList *imageList);
virtual void AssignStateImageList(wxImageList *imageList);
// Functions to work with tree ctrl items. Unfortunately, they can _not_ be
// member functions of wxTreeItem because they must know the tree the item
// belongs to for Windows implementation and storing the pointer to
// wxTreeCtrl in each wxTreeItem is just too much waste.
// accessors
// ---------
// retrieve items label
wxString GetItemText(const wxTreeItemId& item) const;
// get one of the images associated with the item (normal by default)
virtual wxString GetItemText(const wxTreeItemId& item) const;
virtual int GetItemImage(const wxTreeItemId& item,
wxTreeItemIcon which = wxTreeItemIcon_Normal) const;
// get the data associated with the item
wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
wxTreeItemIcon which = wxTreeItemIcon_Normal) const;
virtual wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
virtual wxColour GetItemTextColour(const wxTreeItemId& item) const;
virtual wxColour GetItemBackgroundColour(const wxTreeItemId& item) const;
virtual wxFont GetItemFont(const wxTreeItemId& item) const;
// get the item's text colour
wxColour GetItemTextColour(const wxTreeItemId& item) const;
// get the item's background colour
wxColour GetItemBackgroundColour(const wxTreeItemId& item) const;
// get the item's font
wxFont GetItemFont(const wxTreeItemId& item) const;
// modifiers
// ---------
// set items label
void SetItemText(const wxTreeItemId& item, const wxString& text);
// get one of the images associated with the item (normal by default)
virtual void SetItemText(const wxTreeItemId& item, const wxString& text);
virtual void SetItemImage(const wxTreeItemId& item, int image,
wxTreeItemIcon which = wxTreeItemIcon_Normal);
// associate some data with the item
void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
// force appearance of [+] button near the item. This is useful to
// allow the user to expand the items which don't have any children now
// - but instead add them only when needed, thus minimizing memory
// usage and loading time.
void SetItemHasChildren(const wxTreeItemId& item, bool has = true);
// the item will be shown in bold
void SetItemBold(const wxTreeItemId& item, bool bold = true);
// the item will be shown with a drop highlight
void SetItemDropHighlight(const wxTreeItemId& item, bool highlight = true);
// set the items text colour
void SetItemTextColour(const wxTreeItemId& item, const wxColour& col);
// set the items background colour
void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& col);
// set the items font (should be of the same height for all items)
void SetItemFont(const wxTreeItemId& item, const wxFont& font);
virtual void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
virtual void SetItemHasChildren(const wxTreeItemId& item, bool has = true);
virtual void SetItemBold(const wxTreeItemId& item, bool bold = true);
virtual void SetItemDropHighlight(const wxTreeItemId& item,
bool highlight = true);
virtual void SetItemTextColour(const wxTreeItemId& item,
const wxColour& col);
virtual void SetItemBackgroundColour(const wxTreeItemId& item,
const wxColour& col);
virtual void SetItemFont(const wxTreeItemId& item, const wxFont& font);
// item status inquiries
// ---------------------
// is the item visible (it might be outside the view or not expanded)?
bool IsVisible(const wxTreeItemId& item) const;
// does the item has any children?
bool ItemHasChildren(const wxTreeItemId& item) const;
// is the item expanded (only makes sense if HasChildren())?
bool IsExpanded(const wxTreeItemId& item) const;
// is this item currently selected (the same as has focus)?
bool IsSelected(const wxTreeItemId& item) const;
// is item text in bold font?
bool IsBold(const wxTreeItemId& item) const;
virtual bool IsVisible(const wxTreeItemId& item) const;
virtual bool ItemHasChildren(const wxTreeItemId& item) const;
virtual bool IsExpanded(const wxTreeItemId& item) const;
virtual bool IsSelected(const wxTreeItemId& item) const;
virtual bool IsBold(const wxTreeItemId& item) const;
// number of children
// ------------------
// if 'recursively' is false, only immediate children count, otherwise
// the returned number is the number of all items in this branch
size_t GetChildrenCount(const wxTreeItemId& item,
bool recursively = true) const;
virtual size_t GetChildrenCount(const wxTreeItemId& item,
bool recursively = true) const;
// navigation
// ----------
// wxTreeItemId.IsOk() will return false if there is no such item
virtual wxTreeItemId GetRootItem() const;
virtual wxTreeItemId GetSelection() const;
virtual size_t GetSelections(wxArrayTreeItemIds& selections) const;
// get the root tree item
wxTreeItemId GetRootItem() const;
virtual wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
virtual wxTreeItemId GetFirstChild(const wxTreeItemId& item,
wxTreeItemIdValue& cookie) const;
virtual wxTreeItemId GetNextChild(const wxTreeItemId& item,
wxTreeItemIdValue& cookie) const;
virtual wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
// get the item currently selected (may return NULL if no selection)
wxTreeItemId GetSelection() const;
virtual wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
virtual wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
// get the items currently selected, return the number of such item
//
// NB: this operation is expensive and can take a long time for a
// control with a lot of items (~ O(number of items)).
size_t GetSelections(wxArrayTreeItemIds& selections) const;
// get the parent of this item (may return NULL if root)
wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
// for this enumeration function you must pass in a "cookie" parameter
// which is opaque for the application but is necessary for the library
// to make these functions reentrant (i.e. allow more than one
// enumeration on one and the same object simultaneously). Of course,
// the "cookie" passed to GetFirstChild() and GetNextChild() should be
// the same!
// get the first child of this item
wxTreeItemId GetFirstChild(const wxTreeItemId& item,
wxTreeItemIdValue& cookie) const;
// get the next child
wxTreeItemId GetNextChild(const wxTreeItemId& item,
wxTreeItemIdValue& cookie) const;
// get the last child of this item - this method doesn't use cookies
wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
// get the next sibling of this item
wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
// get the previous sibling
wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
// get first visible item
wxTreeItemId GetFirstVisibleItem() const;
// get the next visible item: item must be visible itself!
// see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
// get the previous visible item: item must be visible itself!
wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
virtual wxTreeItemId GetFirstVisibleItem() const;
virtual wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
virtual wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
// operations
// ----------
// add the root node to the tree
virtual wxTreeItemId AddRoot(const wxString& text,
int image = -1, int selectedImage = -1,
wxTreeItemData *data = NULL);
// insert a new item in as the first child of the parent
virtual wxTreeItemId PrependItem(const wxTreeItemId& parent,
const wxString& text,
int image = -1, int selectedImage = -1,
wxTreeItemData *data = NULL);
// insert a new item after a given one
virtual wxTreeItemId InsertItem(const wxTreeItemId& parent,
const wxTreeItemId& idPrevious,
const wxString& text,
int image = -1, int selectedImage = -1,
wxTreeItemData *data = NULL);
const wxTreeItemId& idPrevious,
const wxString& text,
int image = -1, int selectedImage = -1,
wxTreeItemData *data = NULL);
// insert a new item before the one with the given index
virtual wxTreeItemId InsertItem(const wxTreeItemId& parent,
size_t index,
const wxString& text,
int image = -1, int selectedImage = -1,
wxTreeItemData *data = NULL);
virtual void Delete(const wxTreeItemId& item);
virtual void DeleteChildren(const wxTreeItemId& item);
virtual void DeleteAllItems();
// insert a new item in as the last child of the parent
virtual wxTreeItemId AppendItem(const wxTreeItemId& parent,
const wxString& text,
int image = -1, int selectedImage = -1,
wxTreeItemData *data = NULL);
virtual void Expand(const wxTreeItemId& item);
virtual void Collapse(const wxTreeItemId& item);
virtual void CollapseAndReset(const wxTreeItemId& item);
virtual void Toggle(const wxTreeItemId& item);
// delete this item and associated data if any
void Delete(const wxTreeItemId& item);
// delete all children (but don't delete the item itself)
// NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
void DeleteChildren(const wxTreeItemId& item);
// delete all items from the tree
// NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
void DeleteAllItems();
virtual void Unselect();
virtual void UnselectAll();
virtual void SelectItem(const wxTreeItemId& item, bool select = true);
// expand this item
void Expand(const wxTreeItemId& item);
// collapse the item without removing its children
void Collapse(const wxTreeItemId& item);
// collapse the item and remove all children
void CollapseAndReset(const wxTreeItemId& item);
// toggles the current state
void Toggle(const wxTreeItemId& item);
virtual void EnsureVisible(const wxTreeItemId& item);
virtual void ScrollTo(const wxTreeItemId& item);
// remove the selection from currently selected item (if any)
void Unselect();
// unselect all items (only makes sense for multiple selection control)
void UnselectAll();
// select this item
void SelectItem(const wxTreeItemId& item, bool select = true);
// unselect this item
void UnselectItem(const wxTreeItemId& item);
// toggle item selection
void ToggleItemSelection(const wxTreeItemId& item);
// make sure this item is visible (expanding the parent item and/or
// scrolling to this item if necessary)
void EnsureVisible(const wxTreeItemId& item);
// scroll to this item (but don't expand its parent)
void ScrollTo(const wxTreeItemId& item);
// start editing the item label: this (temporarily) replaces the item
// with a one line edit control. The item will be selected if it hadn't
// been before. textCtrlClass parameter allows you to create an edit
// control of arbitrary user-defined class deriving from wxTextCtrl.
wxTextCtrl* EditLabel(const wxTreeItemId& item,
virtual wxTextCtrl *EditLabel(const wxTreeItemId& item,
wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl));
// returns the same pointer as StartEdit() if the item is being edited,
// NULL otherwise (it's assumed that no more than one item may be
// edited simultaneously)
wxTextCtrl* GetEditControl() const;
// end editing and accept or discard the changes to item label
void EndEditLabel(const wxTreeItemId& WXUNUSED(item),
bool discardChanges = false)
virtual wxTextCtrl *GetEditControl() const;
virtual void EndEditLabel(const wxTreeItemId& WXUNUSED(item),
bool discardChanges = false)
{
DoEndEditLabel(discardChanges);
}
// sorting
// this function is called to compare 2 items and should return -1, 0
// or +1 if the first item is less than, equal to or greater than the
// second one. The base class version performs alphabetic comparaison
// of item labels (GetText)
virtual int OnCompareItems(const wxTreeItemId& item1,
const wxTreeItemId& item2);
// sort the children of this item using OnCompareItems
//
// NB: this function is not reentrant and not MT-safe (FIXME)!
void SortChildren(const wxTreeItemId& item);
virtual void SortChildren(const wxTreeItemId& item);
// helpers
// -------
// determine to which item (if any) belongs the given point (the
// coordinates specified are relative to the client area of tree ctrl)
// and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
// constants.
//
// The first function is more portable (because easier to implement
// on other platforms), but the second one returns some extra info.
wxTreeItemId HitTest(const wxPoint& point)
{ int dummy; return HitTest(point, dummy); }
wxTreeItemId HitTest(const wxPoint& point, int& flags);
// get the bounding rectangle of the item (or of its label only)
bool GetBoundingRect(const wxTreeItemId& item,
wxRect& rect,
bool textOnly = false) const;
virtual bool GetBoundingRect(const wxTreeItemId& item,
wxRect& rect,
bool textOnly = false) const;
// deprecated
// ----------
@@ -392,8 +221,6 @@ public:
// implementation
// --------------
virtual bool ShouldInheritColours() const { return false; }
virtual wxVisualAttributes GetDefaultAttributes() const
{
return GetClassDefaultAttributes(GetWindowVariant());
@@ -421,9 +248,6 @@ public:
int GetState(const wxTreeItemId& node);
protected:
virtual wxSize DoGetBestSize() const;
// SetImageList helper
void SetAnyImageList(wxImageList *imageList, int which);
@@ -453,11 +277,17 @@ private:
inline void DoExpand(const wxTreeItemId& item, int flag);
wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
wxTreeItemId hInsertAfter,
const wxString& text,
int image, int selectedImage,
wxTreeItemData *data);
virtual wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
size_t pos,
const wxString& text,
int image, int selectedImage,
wxTreeItemData *data);
virtual wxTreeItemId DoInsertAfter(const wxTreeItemId& parent,
const wxTreeItemId& idPrevious,
const wxString& text,
int image = -1, int selImage = -1,
wxTreeItemData *data = NULL);
virtual wxTreeItemId DoHitTest(const wxPoint& point, int& flags);
int DoGetItemImageFromData(const wxTreeItemId& item,
wxTreeItemIcon which) const;

View File

@@ -14,6 +14,387 @@
#include "wx/treebase.h"
class WXDLLEXPORT wxImageList;
// ----------------------------------------------------------------------------
// wxTreeCtrlBase
// ----------------------------------------------------------------------------
class WXDLLEXPORT wxTreeCtrlBase : public wxControl
{
public:
wxTreeCtrlBase()
{
m_imageListNormal =
m_imageListState = NULL;
m_ownsImageListNormal =
m_ownsImageListState = false;
// arbitrary default
m_spacing = 18;
}
virtual ~wxTreeCtrlBase();
// accessors
// ---------
// get the total number of items in the control
virtual size_t GetCount() const = 0;
// indent is the number of pixels the children are indented relative to
// the parents position. SetIndent() also redraws the control
// immediately.
virtual unsigned int GetIndent() const = 0;
virtual void SetIndent(unsigned int indent) = 0;
// spacing is the number of pixels between the start and the Text
// (has no effect under wxMSW)
unsigned int GetSpacing() const { return m_spacing; }
void SetSpacing(unsigned int spacing) { m_spacing = spacing; }
// image list: these functions allow to associate an image list with
// the control and retrieve it. Note that the control does _not_ delete
// the associated image list when it's deleted in order to allow image
// lists to be shared between different controls.
//
// The normal image list is for the icons which correspond to the
// normal tree item state (whether it is selected or not).
// Additionally, the application might choose to show a state icon
// which corresponds to an app-defined item state (for example,
// checked/unchecked) which are taken from the state image list.
wxImageList *GetImageList() const { return m_imageListNormal; }
wxImageList *GetStateImageList() const { return m_imageListState; }
virtual void SetImageList(wxImageList *imageList) = 0;
virtual void SetStateImageList(wxImageList *imageList) = 0;
void AssignImageList(wxImageList *imageList)
{
SetImageList(imageList);
m_ownsImageListNormal = true;
}
void AssignStateImageList(wxImageList *imageList)
{
SetStateImageList(imageList);
m_ownsImageListState = true;
}
// Functions to work with tree ctrl items. Unfortunately, they can _not_ be
// member functions of wxTreeItem because they must know the tree the item
// belongs to for Windows implementation and storing the pointer to
// wxTreeCtrl in each wxTreeItem is just too much waste.
// accessors
// ---------
// retrieve items label
virtual wxString GetItemText(const wxTreeItemId& item) const = 0;
// get one of the images associated with the item (normal by default)
virtual int GetItemImage(const wxTreeItemId& item,
wxTreeItemIcon which = wxTreeItemIcon_Normal) const = 0;
// get the data associated with the item
virtual wxTreeItemData *GetItemData(const wxTreeItemId& item) const = 0;
// get the item's text colour
virtual wxColour GetItemTextColour(const wxTreeItemId& item) const = 0;
// get the item's background colour
virtual wxColour GetItemBackgroundColour(const wxTreeItemId& item) const = 0;
// get the item's font
virtual wxFont GetItemFont(const wxTreeItemId& item) const = 0;
// modifiers
// ---------
// set items label
virtual void SetItemText(const wxTreeItemId& item, const wxString& text) = 0;
// get one of the images associated with the item (normal by default)
virtual void SetItemImage(const wxTreeItemId& item,
int image,
wxTreeItemIcon which = wxTreeItemIcon_Normal) = 0;
// associate some data with the item
virtual void SetItemData(const wxTreeItemId& item, wxTreeItemData *data) = 0;
// force appearance of [+] button near the item. This is useful to
// allow the user to expand the items which don't have any children now
// - but instead add them only when needed, thus minimizing memory
// usage and loading time.
virtual void SetItemHasChildren(const wxTreeItemId& item,
bool has = true) = 0;
// the item will be shown in bold
virtual void SetItemBold(const wxTreeItemId& item, bool bold = true) = 0;
// the item will be shown with a drop highlight
virtual void SetItemDropHighlight(const wxTreeItemId& item,
bool highlight = true) = 0;
// set the items text colour
virtual void SetItemTextColour(const wxTreeItemId& item,
const wxColour& col) = 0;
// set the items background colour
virtual void SetItemBackgroundColour(const wxTreeItemId& item,
const wxColour& col) = 0;
// set the items font (should be of the same height for all items)
virtual void SetItemFont(const wxTreeItemId& item,
const wxFont& font) = 0;
// item status inquiries
// ---------------------
// is the item visible (it might be outside the view or not expanded)?
virtual bool IsVisible(const wxTreeItemId& item) const = 0;
// does the item has any children?
virtual bool ItemHasChildren(const wxTreeItemId& item) const = 0;
// same as above
bool HasChildren(const wxTreeItemId& item) const
{ return ItemHasChildren(item); }
// is the item expanded (only makes sense if HasChildren())?
virtual bool IsExpanded(const wxTreeItemId& item) const = 0;
// is this item currently selected (the same as has focus)?
virtual bool IsSelected(const wxTreeItemId& item) const = 0;
// is item text in bold font?
virtual bool IsBold(const wxTreeItemId& item) const = 0;
// number of children
// ------------------
// if 'recursively' is false, only immediate children count, otherwise
// the returned number is the number of all items in this branch
virtual size_t GetChildrenCount(const wxTreeItemId& item,
bool recursively = true) const = 0;
// navigation
// ----------
// wxTreeItemId.IsOk() will return false if there is no such item
// get the root tree item
virtual wxTreeItemId GetRootItem() const = 0;
// get the item currently selected (may return NULL if no selection)
virtual wxTreeItemId GetSelection() const = 0;
// get the items currently selected, return the number of such item
//
// NB: this operation is expensive and can take a long time for a
// control with a lot of items (~ O(number of items)).
virtual size_t GetSelections(wxArrayTreeItemIds& selections) const = 0;
// get the parent of this item (may return NULL if root)
virtual wxTreeItemId GetItemParent(const wxTreeItemId& item) const = 0;
// for this enumeration function you must pass in a "cookie" parameter
// which is opaque for the application but is necessary for the library
// to make these functions reentrant (i.e. allow more than one
// enumeration on one and the same object simultaneously). Of course,
// the "cookie" passed to GetFirstChild() and GetNextChild() should be
// the same!
// get the first child of this item
virtual wxTreeItemId GetFirstChild(const wxTreeItemId& item,
wxTreeItemIdValue& cookie) const = 0;
// get the next child
virtual wxTreeItemId GetNextChild(const wxTreeItemId& item,
wxTreeItemIdValue& cookie) const = 0;
// get the last child of this item - this method doesn't use cookies
virtual wxTreeItemId GetLastChild(const wxTreeItemId& item) const = 0;
// get the next sibling of this item
virtual wxTreeItemId GetNextSibling(const wxTreeItemId& item) const = 0;
// get the previous sibling
virtual wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const = 0;
// get first visible item
virtual wxTreeItemId GetFirstVisibleItem() const = 0;
// get the next visible item: item must be visible itself!
// see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
virtual wxTreeItemId GetNextVisible(const wxTreeItemId& item) const = 0;
// get the previous visible item: item must be visible itself!
virtual wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const = 0;
// operations
// ----------
// add the root node to the tree
virtual wxTreeItemId AddRoot(const wxString& text,
int image = -1, int selImage = -1,
wxTreeItemData *data = NULL) = 0;
// insert a new item in as the first child of the parent
wxTreeItemId PrependItem(const wxTreeItemId& parent,
const wxString& text,
int image = -1, int selImage = -1,
wxTreeItemData *data = NULL)
{
return DoInsertItem(parent, 0u, text, image, selImage, data);
}
// insert a new item after a given one
wxTreeItemId InsertItem(const wxTreeItemId& parent,
const wxTreeItemId& idPrevious,
const wxString& text,
int image = -1, int selImage = -1,
wxTreeItemData *data = NULL)
{
return DoInsertAfter(parent, idPrevious, text, image, selImage, data);
}
// insert a new item before the one with the given index
wxTreeItemId InsertItem(const wxTreeItemId& parent,
size_t pos,
const wxString& text,
int image = -1, int selImage = -1,
wxTreeItemData *data = NULL)
{
return DoInsertItem(parent, pos, text, image, selImage, data);
}
// insert a new item in as the last child of the parent
wxTreeItemId AppendItem(const wxTreeItemId& parent,
const wxString& text,
int image = -1, int selImage = -1,
wxTreeItemData *data = NULL)
{
return DoInsertItem(parent, (size_t)-1, text, image, selImage, data);
}
// delete this item and associated data if any
virtual void Delete(const wxTreeItemId& item) = 0;
// delete all children (but don't delete the item itself)
// NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
virtual void DeleteChildren(const wxTreeItemId& item) = 0;
// delete all items from the tree
// NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
virtual void DeleteAllItems() = 0;
// expand this item
virtual void Expand(const wxTreeItemId& item) = 0;
// collapse the item without removing its children
virtual void Collapse(const wxTreeItemId& item) = 0;
// collapse the item and remove all children
virtual void CollapseAndReset(const wxTreeItemId& item) = 0;
// toggles the current state
virtual void Toggle(const wxTreeItemId& item) = 0;
// remove the selection from currently selected item (if any)
virtual void Unselect() = 0;
// unselect all items (only makes sense for multiple selection control)
virtual void UnselectAll() = 0;
// select this item
virtual void SelectItem(const wxTreeItemId& item, bool select = true) = 0;
// unselect this item
void UnselectItem(const wxTreeItemId& item) { SelectItem(item, false); }
// toggle item selection
void ToggleItemSelection(const wxTreeItemId& item)
{
SelectItem(item, !IsSelected(item));
}
// make sure this item is visible (expanding the parent item and/or
// scrolling to this item if necessary)
virtual void EnsureVisible(const wxTreeItemId& item) = 0;
// scroll to this item (but don't expand its parent)
virtual void ScrollTo(const wxTreeItemId& item) = 0;
// start editing the item label: this (temporarily) replaces the item
// with a one line edit control. The item will be selected if it hadn't
// been before. textCtrlClass parameter allows you to create an edit
// control of arbitrary user-defined class deriving from wxTextCtrl.
virtual wxTextCtrl *EditLabel(const wxTreeItemId& item,
wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl)) = 0;
// returns the same pointer as StartEdit() if the item is being edited,
// NULL otherwise (it's assumed that no more than one item may be
// edited simultaneously)
virtual wxTextCtrl *GetEditControl() const = 0;
// end editing and accept or discard the changes to item label
virtual void EndEditLabel(const wxTreeItemId& item,
bool discardChanges = false) = 0;
// sorting
// -------
// this function is called to compare 2 items and should return -1, 0
// or +1 if the first item is less than, equal to or greater than the
// second one. The base class version performs alphabetic comparaison
// of item labels (GetText)
virtual int OnCompareItems(const wxTreeItemId& item1,
const wxTreeItemId& item2)
{
return wxStrcmp(GetItemText(item1), GetItemText(item2));
}
// sort the children of this item using OnCompareItems
//
// NB: this function is not reentrant and not MT-safe (FIXME)!
virtual void SortChildren(const wxTreeItemId& item) = 0;
// items geometry
// --------------
// determine to which item (if any) belongs the given point (the
// coordinates specified are relative to the client area of tree ctrl)
// and, in the second variant, fill the flags parameter with a bitmask
// of wxTREE_HITTEST_xxx constants.
wxTreeItemId HitTest(const wxPoint& point)
{ int dummy; return DoHitTest(point, dummy); }
wxTreeItemId HitTest(const wxPoint& point, int& flags)
{ return DoHitTest(point, flags); }
// get the bounding rectangle of the item (or of its label only)
virtual bool GetBoundingRect(const wxTreeItemId& item,
wxRect& rect,
bool textOnly = false) const = 0;
// implementation
// --------------
virtual bool ShouldInheritColours() const { return false; }
protected:
virtual wxSize DoGetBestSize() const;
// common part of Append/Prepend/InsertItem()
//
// pos is the position at which to insert the item or (size_t)-1 to append
// it to the end
virtual wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
size_t pos,
const wxString& text,
int image, int selImage,
wxTreeItemData *data) = 0;
// and this function implements overloaded InsertItem() taking wxTreeItemId
// (it can't be called InsertItem() as we'd have virtual function hiding
// problem in derived classes then)
virtual wxTreeItemId DoInsertAfter(const wxTreeItemId& parent,
const wxTreeItemId& idPrevious,
const wxString& text,
int image = -1, int selImage = -1,
wxTreeItemData *data = NULL) = 0;
// real HitTest() implementation: again, can't be called just HitTest()
// because it's overloaded and so the non-virtual overload would be hidden
virtual wxTreeItemId DoHitTest(const wxPoint& point, int& flags) = 0;
wxImageList *m_imageListNormal, // images for tree elements
*m_imageListState; // special images for app defined states
bool m_ownsImageListNormal,
m_ownsImageListState;
// spacing between left border and the text
unsigned short m_spacing;
DECLARE_NO_COPY_CLASS(wxTreeCtrlBase)
};
// ----------------------------------------------------------------------------
// include the platform-dependent wxTreeCtrl class
// ----------------------------------------------------------------------------
@@ -36,11 +417,5 @@
#include "wx/generic/treectlg.h"
#endif
/*
#if !defined(__WXMSW__)
#define wxTreeCtrl wxGenericTreeCtrl
#endif
*/
#endif // _WX_TREECTRL_H_BASE_

View File

@@ -26,14 +26,7 @@
#if wxUSE_TREECTRL
#include "wx/treebase.h"
#include "wx/settings.h"
#include "wx/log.h"
#include "wx/intl.h"
#include "wx/dynarray.h"
#include "wx/arrimpl.cpp"
#include "wx/dcclient.h"
#include "wx/treectrl.h"
// ----------------------------------------------------------------------------
// events
@@ -86,5 +79,49 @@ wxTreeEvent::wxTreeEvent(const wxTreeEvent & event)
m_editCancelled = event.m_editCancelled;
}
// ----------------------------------------------------------------------------
// wxTreeCtrlBase
// ----------------------------------------------------------------------------
wxTreeCtrlBase::~wxTreeCtrlBase()
{
if (m_ownsImageListNormal)
delete m_imageListNormal;
if (m_ownsImageListState)
delete m_imageListState;
}
wxSize wxTreeCtrlBase::DoGetBestSize() const
{
wxSize size;
// this doesn't really compute the total bounding rectangle of all items
// but a not too bad guess of it which has the advantage of not having to
// examine all (potentially hundreds or thousands) items in the control
for ( wxTreeItemId item = GetRootItem();
item.IsOk();
item = GetLastChild(item) )
{
wxRect rect;
// last parameter is "true" to get only the dimensions of the text
// label, we don't want to get the entire item width as it's determined
// by the current size
if ( GetBoundingRect(item, rect, true) )
{
if ( size.x < rect.x + rect.width )
size.x = rect.x + rect.width;
if ( size.y < rect.y + rect.height )
size.y = rect.y + rect.height;
}
}
// need some minimal size even for empty tree
if ( !size.x || !size.y )
size = wxControl::DoGetBestSize();
return size;
}
#endif // wxUSE_TREECTRL

View File

@@ -27,6 +27,7 @@
#if wxUSE_TREECTRL
#include "wx/treebase.h"
#include "wx/treectrl.h"
#include "wx/generic/treectlg.h"
#include "wx/timer.h"
#include "wx/textctrl.h"
@@ -84,6 +85,24 @@ class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl
public:
wxTreeTextCtrl(wxGenericTreeCtrl *owner, wxGenericTreeItem *item);
void EndEdit(bool discardChanges = false)
{
if ( discardChanges )
{
StopEditing();
}
else
{
m_aboutToFinish = true;
// Notify the owner about the changes
AcceptChanges();
// Even if vetoed, close the control (consistent with MSW)
Finish();
}
}
void StopEditing()
{
Finish();
@@ -419,7 +438,7 @@ void wxTreeTextCtrl::Finish()
m_finished = true;
m_owner->SetFocusIgnoringChildren();
m_owner->SetFocus();
}
}
@@ -428,11 +447,7 @@ void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
switch ( event.m_keyCode )
{
case WXK_RETURN:
m_aboutToFinish = true;
// Notify the owner about the changes
AcceptChanges();
// Even if vetoed, close the control (consistent with MSW)
Finish();
EndEdit();
break;
case WXK_ESCAPE:
@@ -695,9 +710,9 @@ int wxGenericTreeItem::GetCurrentImage() const
// wxGenericTreeCtrl implementation
// -----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl, wxScrolledWindow)
IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl, wxControl)
BEGIN_EVENT_TABLE(wxGenericTreeCtrl,wxScrolledWindow)
BEGIN_EVENT_TABLE(wxGenericTreeCtrl, wxTreeCtrlBase)
EVT_PAINT (wxGenericTreeCtrl::OnPaint)
EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse)
EVT_CHAR (wxGenericTreeCtrl::OnChar)
@@ -721,7 +736,10 @@ IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxGenericTreeCtrl)
void wxGenericTreeCtrl::Init()
{
m_current = m_key_current = m_anchor = m_select_me = (wxGenericTreeItem *) NULL;
m_current =
m_key_current =
m_anchor =
m_select_me = (wxGenericTreeItem *) NULL;
m_hasFocus = false;
m_dirty = false;
@@ -747,10 +765,8 @@ void wxGenericTreeCtrl::Init()
wxSOLID
);
m_imageListNormal = m_imageListButtons =
m_imageListState = (wxImageList *) NULL;
m_ownsImageListNormal = m_ownsImageListButtons =
m_ownsImageListState = false;
m_imageListButtons = NULL;
m_ownsImageListButtons = false;
m_dragCount = 0;
m_isDragging = false;
@@ -786,7 +802,7 @@ bool wxGenericTreeCtrl::Create(wxWindow *parent,
const wxPoint& pos,
const wxSize& size,
long style,
const wxValidator& wxVALIDATOR_PARAM(validator),
const wxValidator& validator,
const wxString& name )
{
#ifdef __WXMAC__
@@ -799,8 +815,11 @@ bool wxGenericTreeCtrl::Create(wxWindow *parent,
style |= wxTR_ROW_LINES;
#endif // __WXMAC__
wxScrolledWindow::Create( parent, id, pos, size,
style|wxHSCROLL|wxVSCROLL, name );
if ( !wxControl::Create( parent, id, pos, size,
style|wxHSCROLL|wxVSCROLL,
validator,
name ) )
return false;
// If the tree display has no buttons, but does have
// connecting lines, we can use a narrower layout.
@@ -811,17 +830,12 @@ bool wxGenericTreeCtrl::Create(wxWindow *parent,
m_spacing = 10;
}
#if wxUSE_VALIDATORS
SetValidator( validator );
#endif
wxVisualAttributes attr = GetDefaultAttributes();
SetOwnForegroundColour( attr.colFg );
SetOwnBackgroundColour( attr.colBg );
if (!m_hasFont)
SetOwnFont(attr.font);
// m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
m_dottedPen = wxPen( wxT("grey"), 0, 0 );
SetBestSize(size);
@@ -839,10 +853,6 @@ wxGenericTreeCtrl::~wxGenericTreeCtrl()
delete m_renameTimer;
delete m_findTimer;
if (m_ownsImageListNormal)
delete m_imageListNormal;
if (m_ownsImageListState)
delete m_imageListState;
if (m_ownsImageListButtons)
delete m_imageListButtons;
}
@@ -875,12 +885,6 @@ void wxGenericTreeCtrl::SetIndent(unsigned int indent)
m_dirty = true;
}
void wxGenericTreeCtrl::SetSpacing(unsigned int spacing)
{
m_spacing = (unsigned short) spacing;
m_dirty = true;
}
size_t
wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
bool recursively) const
@@ -1065,7 +1069,7 @@ void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font
bool wxGenericTreeCtrl::SetFont( const wxFont &font )
{
wxScrolledWindow::SetFont(font);
wxTreeCtrlBase::SetFont(font);
m_normalFont = font ;
m_boldFont = wxFont(m_normalFont.GetPointSize(),
@@ -1393,10 +1397,11 @@ wxTreeItemId wxGenericTreeCtrl::FindItem(const wxTreeItemId& idParent,
// -----------------------------------------------------------------------------
wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
size_t previous,
const wxString& text,
int image, int selImage,
wxTreeItemData *data)
size_t previous,
const wxString& text,
int image,
int selImage,
wxTreeItemData *data)
{
wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
if ( !parent )
@@ -1415,14 +1420,16 @@ wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
data->m_pItem = item;
}
parent->Insert( item, previous );
parent->Insert( item, previous == (size_t)-1 ? parent->GetChildren().size()
: previous );
return item;
}
wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
int image, int selImage,
wxTreeItemData *data)
int image,
int selImage,
wxTreeItemData *data)
{
wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
@@ -1453,19 +1460,11 @@ wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
return m_anchor;
}
wxTreeItemId wxGenericTreeCtrl::PrependItem(const wxTreeItemId& parent,
const wxString& text,
int image, int selImage,
wxTreeItemData *data)
{
return DoInsertItem(parent, 0u, text, image, selImage, data);
}
wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
const wxTreeItemId& idPrevious,
const wxString& text,
int image, int selImage,
wxTreeItemData *data)
wxTreeItemId wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId& parentId,
const wxTreeItemId& idPrevious,
const wxString& text,
int image, int selImage,
wxTreeItemData *data)
{
wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
if ( !parent )
@@ -1485,37 +1484,6 @@ wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
}
wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
size_t before,
const wxString& text,
int image, int selImage,
wxTreeItemData *data)
{
wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
if ( !parent )
{
// should we give a warning here?
return AddRoot(text, image, selImage, data);
}
return DoInsertItem(parentId, before, text, image, selImage, data);
}
wxTreeItemId wxGenericTreeCtrl::AppendItem(const wxTreeItemId& parentId,
const wxString& text,
int image, int selImage,
wxTreeItemData *data)
{
wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
if ( !parent )
{
// should we give a warning here?
return AddRoot(text, image, selImage, data);
}
return DoInsertItem( parent, parent->GetChildren().Count(), text,
image, selImage, data);
}
void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
{
@@ -2066,12 +2034,6 @@ static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
return s_treeBeingSorted->OnCompareItems(*item1, *item2);
}
int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
const wxTreeItemId& item2)
{
return wxStrcmp(GetItemText(item1), GetItemText(item2));
}
void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
{
wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
@@ -2093,21 +2055,6 @@ void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
//else: don't make the tree dirty as nothing changed
}
wxImageList *wxGenericTreeCtrl::GetImageList() const
{
return m_imageListNormal;
}
wxImageList *wxGenericTreeCtrl::GetButtonsImageList() const
{
return m_imageListButtons;
}
wxImageList *wxGenericTreeCtrl::GetStateImageList() const
{
return m_imageListState;
}
void wxGenericTreeCtrl::CalculateLineHeight()
{
wxClientDC dc(this);
@@ -2175,18 +2122,6 @@ void wxGenericTreeCtrl::SetButtonsImageList(wxImageList *imageList)
CalculateLineHeight();
}
void wxGenericTreeCtrl::AssignImageList(wxImageList *imageList)
{
SetImageList(imageList);
m_ownsImageListNormal = true;
}
void wxGenericTreeCtrl::AssignStateImageList(wxImageList *imageList)
{
SetStateImageList(imageList);
m_ownsImageListState = true;
}
void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList *imageList)
{
SetButtonsImageList(imageList);
@@ -2282,7 +2217,7 @@ void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
}
else
{
colBg = m_backgroundColour;
colBg = GetBackgroundColour();
}
dc.SetBrush(wxBrush(colBg, wxSOLID));
}
@@ -2900,7 +2835,7 @@ void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
}
}
wxTreeItemId wxGenericTreeCtrl::HitTest(const wxPoint& point, int& flags)
wxTreeItemId wxGenericTreeCtrl::DoHitTest(const wxPoint& point, int& flags)
{
// JACS: removed wxYieldIfNeeded() because it can cause the window
// to be deleted from under us if a close window event is pending
@@ -2951,9 +2886,10 @@ bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
return true;
}
void wxGenericTreeCtrl::Edit( const wxTreeItemId& item )
wxTextCtrl *wxGenericTreeCtrl::EditLabel(const wxTreeItemId& item,
wxClassInfo * WXUNUSED(textCtrlClass))
{
wxCHECK_RET( item.IsOk(), _T("can't edit an invalid item") );
wxCHECK_MSG( item.IsOk(), NULL, _T("can't edit an invalid item") );
wxGenericTreeItem *itemEdit = (wxGenericTreeItem *)item.m_pItem;
@@ -2963,7 +2899,7 @@ void wxGenericTreeCtrl::Edit( const wxTreeItemId& item )
if ( GetEventHandler()->ProcessEvent( te ) && !te.IsAllowed() )
{
// vetoed by user
return;
return NULL;
}
// We have to call this here because the label in
@@ -2976,9 +2912,12 @@ void wxGenericTreeCtrl::Edit( const wxTreeItemId& item )
wxYieldIfNeeded();
#endif
// TODO: use textCtrlClass here to create the control of correct class
m_textCtrl = new wxTreeTextCtrl(this, itemEdit);
m_textCtrl->SetFocus();
return m_textCtrl;
}
// returns a pointer to the text edit control if the item is being
@@ -2989,6 +2928,14 @@ wxTextCtrl* wxGenericTreeCtrl::GetEditControl() const
return m_textCtrl;
}
void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item),
bool discardChanges)
{
wxCHECK_RET( m_textCtrl, _T("not editing label") );
m_textCtrl->EndEdit(discardChanges);
}
bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem *item,
const wxString& value)
{

View File

@@ -606,9 +606,6 @@ bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively)
void wxTreeCtrl::Init()
{
m_imageListNormal = NULL;
m_imageListState = NULL;
m_ownsImageListNormal = m_ownsImageListState = false;
m_textCtrl = NULL;
m_hasAnyAttr = false;
m_dragImage = NULL;
@@ -772,9 +769,6 @@ wxTreeCtrl::~wxTreeCtrl()
// delete user data to prevent memory leaks
// also deletes hidden root node storage.
DeleteAllItems();
if (m_ownsImageListNormal) delete m_imageListNormal;
if (m_ownsImageListState) delete m_imageListState;
}
// ----------------------------------------------------------------------------
@@ -833,16 +827,6 @@ void wxTreeCtrl::SetIndent(unsigned int indent)
TreeView_SetIndent(GetHwnd(), indent);
}
wxImageList *wxTreeCtrl::GetImageList() const
{
return m_imageListNormal;
}
wxImageList *wxTreeCtrl::GetStateImageList() const
{
return m_imageListState;
}
void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
{
// no error return
@@ -867,18 +851,6 @@ void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
m_ownsImageListState = false;
}
void wxTreeCtrl::AssignImageList(wxImageList *imageList)
{
SetImageList(imageList);
m_ownsImageListNormal = true;
}
void wxTreeCtrl::AssignStateImageList(wxImageList *imageList)
{
SetStateImageList(imageList);
m_ownsImageListState = true;
}
size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
bool recursively) const
{
@@ -1629,11 +1601,11 @@ size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
// Usual operations
// ----------------------------------------------------------------------------
wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
wxTreeItemId hInsertAfter,
const wxString& text,
int image, int selectedImage,
wxTreeItemData *data)
wxTreeItemId wxTreeCtrl::DoInsertAfter(const wxTreeItemId& parent,
const wxTreeItemId& hInsertAfter,
const wxString& text,
int image, int selectedImage,
wxTreeItemData *data)
{
wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()),
wxTreeItemId(),
@@ -1711,8 +1683,12 @@ wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
int image, int selImage,
long insertAfter)
{
return DoInsertItem(parent, wxTreeItemId((void *)insertAfter), text,
image, selImage, NULL);
return DoInsertAfter(parent,
wxTreeItemId(wxUIntToPtr(insertAfter)),
text,
image,
selImage,
NULL);
}
wxImageList *wxTreeCtrl::GetImageList(int) const
@@ -1750,59 +1726,40 @@ wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
return TVI_ROOT;
}
return DoInsertItem(wxTreeItemId(), wxTreeItemId(),
text, image, selectedImage, data);
return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
text, image, selectedImage, data);
}
wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
const wxString& text,
int image, int selectedImage,
wxTreeItemData *data)
wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
size_t index,
const wxString& text,
int image, int selectedImage,
wxTreeItemData *data)
{
return DoInsertItem(parent, TVI_FIRST,
text, image, selectedImage, data);
}
wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
const wxTreeItemId& idPrevious,
const wxString& text,
int image, int selectedImage,
wxTreeItemData *data)
{
return DoInsertItem(parent, idPrevious, text, image, selectedImage, data);
}
wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
size_t index,
const wxString& text,
int image, int selectedImage,
wxTreeItemData *data)
{
// find the item from index
wxTreeItemIdValue cookie;
wxTreeItemId idPrev, idCur = GetFirstChild(parent, cookie);
while ( index != 0 && idCur.IsOk() )
wxTreeItemId idPrev;
if ( index == (size_t)-1 )
{
index--;
// special value: append to the end
idPrev = TVI_LAST;
}
else // find the item from index
{
wxTreeItemIdValue cookie;
wxTreeItemId idCur = GetFirstChild(parent, cookie);
while ( index != 0 && idCur.IsOk() )
{
index--;
idPrev = idCur;
idCur = GetNextChild(parent, cookie);
idPrev = idCur;
idCur = GetNextChild(parent, cookie);
}
// assert, not check: if the index is invalid, we will append the item
// to the end
wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") );
}
// assert, not check: if the index is invalid, we will append the item
// to the end
wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") );
return DoInsertItem(parent, idPrev, text, image, selectedImage, data);
}
wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
const wxString& text,
int image, int selectedImage,
wxTreeItemData *data)
{
return DoInsertItem(parent, TVI_LAST,
text, image, selectedImage, data);
return DoInsertAfter(parent, idPrev, text, image, selectedImage, data);
}
void wxTreeCtrl::Delete(const wxTreeItemId& item)
@@ -2080,7 +2037,7 @@ void wxTreeCtrl::DoEndEditLabel(bool discardChanges)
DeleteTextCtrl();
}
wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
wxTreeItemId wxTreeCtrl::DoHitTest(const wxPoint& point, int& flags)
{
TV_HITTESTINFO hitTestInfo;
hitTestInfo.pt.x = (int)point.x;
@@ -2137,38 +2094,6 @@ bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
}
}
wxSize wxTreeCtrl::DoGetBestSize() const
{
wxSize size;
// this doesn't really compute the total bounding rectangle of all items
// but a not too bad guess of it which has the advantage of not having to
// examine all (potentially hundreds or thousands) items in the control
for ( wxTreeItemId item = GetRootItem();
item.IsOk();
item = GetLastChild(item) )
{
wxRect rect;
// last parameter is "true" to get only the dimensions of the text
// label, we don't want to get the entire item width as it's determined
// by the current size
if ( GetBoundingRect(item, rect, true) )
{
if ( size.x < rect.x + rect.width )
size.x = rect.x + rect.width;
if ( size.y < rect.y + rect.height )
size.y = rect.y + rect.height;
}
}
// need some minimal size even for empty tree
if ( !size.x || !size.y )
size = wxControl::DoGetBestSize();
return size;
}
// ----------------------------------------------------------------------------
// sorting stuff
// ----------------------------------------------------------------------------