diff --git a/include/wx/msw/treectrl.h b/include/wx/msw/treectrl.h index 647cc4cec3..f8b34757c2 100644 --- a/include/wx/msw/treectrl.h +++ b/include/wx/msw/treectrl.h @@ -2,297 +2,522 @@ // Name: treectrl.h // Purpose: wxTreeCtrl class // Author: Julian Smart -// Modified by: +// Modified by: Vadim Zeitlin to be less MSW-specific on 10/10/98 // Created: 01/02/97 // RCS-ID: $Id$ // Copyright: (c) Julian Smart and Markus Holzem -// Licence: wxWindows license +// Licence: wxWindows license ///////////////////////////////////////////////////////////////////////////// #ifndef _WX_TREECTRL_H_ #define _WX_TREECTRL_H_ +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- #ifdef __GNUG__ -#pragma interface "treectrl.h" + #pragma interface "treectrl.h" #endif #include "wx/control.h" #include "wx/event.h" -#include "wx/imaglist.h" -// WXDLLEXPORT_DATA(extern const char*) wxTreeNameStr; +// the type for "untyped" data +typedef long wxDataType; -#define wxTREE_MASK_HANDLE 0x0001 -#define wxTREE_MASK_STATE 0x0002 -#define wxTREE_MASK_TEXT 0x0004 -#define wxTREE_MASK_IMAGE 0x0008 -#define wxTREE_MASK_SELECTED_IMAGE 0x0010 -#define wxTREE_MASK_CHILDREN 0x0020 -#define wxTREE_MASK_DATA 0x0040 +// fwd decl +class wxImageList; +struct wxTreeViewItem; -#define wxTREE_STATE_BOLD 0x0001 -#define wxTREE_STATE_DROPHILITED 0x0002 -#define wxTREE_STATE_EXPANDED 0x0004 -#define wxTREE_STATE_EXPANDEDONCE 0x0008 -#define wxTREE_STATE_FOCUSED 0x0010 -#define wxTREE_STATE_SELECTED 0x0020 -#define wxTREE_STATE_CUT 0x0040 +// a callback function used for sorting tree items, it should return -1 if the +// first item precedes the second, +1 if the second precedes the first or 0 if +// they're equivalent +class wxTreeItemData; +typedef int (*wxTreeItemCmpFunc)(wxTreeItemData *item1, wxTreeItemData *item2); -#define wxTREE_HITTEST_ABOVE 0x0001 // Above the client area. -#define wxTREE_HITTEST_BELOW 0x0002 // Below the client area. -#define wxTREE_HITTEST_NOWHERE 0x0004 // In the client area but below the last item. -#define wxTREE_HITTEST_ONITEMBUTTON 0x0010 // On the button associated with an item. -#define wxTREE_HITTEST_ONITEMICON 0x0020 // On the bitmap associated with an item. -#define wxTREE_HITTEST_ONITEMINDENT 0x0040 // In the indentation associated with an item. -#define wxTREE_HITTEST_ONITEMLABEL 0x0080 // On the label (string) associated with an item. -#define wxTREE_HITTEST_ONITEMRIGHT 0x0100 // In the area to the right of an item. -#define wxTREE_HITTEST_ONITEMSTATEICON 0x0200 // On the state icon for a tree view item that is in a user-defined state. -#define wxTREE_HITTEST_TOLEFT 0x0400 // To the right of the client area. -#define wxTREE_HITTEST_TORIGHT 0x0800 // To the left of the client area. +// ---------------------------------------------------------------------------- +// constants +// ---------------------------------------------------------------------------- -#define wxTREE_HITTEST_ONITEM (wxTREE_HITTEST_ONITEMICON | wxTREE_HITTEST_ONITEMLABEL | wxTREE_HITTEST_ONITEMSTATEICON) +// values for the `flags' parameter of wxTreeCtrl::HitTest() which determine +// where exactly the specified point is situated: + // above the client area. +static const int wxTREE_HITTEST_ABOVE = 0x0001; + // below the client area. +static const int wxTREE_HITTEST_BELOW = 0x0002; + // in the client area but below the last item. +static const int wxTREE_HITTEST_NOWHERE = 0x0004; + // on the button associated with an item. +static const int wxTREE_HITTEST_ONITEMBUTTON = 0x0010; + // on the bitmap associated with an item. +static const int wxTREE_HITTEST_ONITEMICON = 0x0020; + // in the indentation associated with an item. +static const int wxTREE_HITTEST_ONITEMINDENT = 0x0040; + // on the label (string) associated with an item. +static const int wxTREE_HITTEST_ONITEMLABEL = 0x0080; + // in the area to the right of an item. +static const int wxTREE_HITTEST_ONITEMRIGHT = 0x0100; + // on the state icon for a tree view item that is in a user-defined state. +static const int wxTREE_HITTEST_ONITEMSTATEICON = 0x0200; + // to the right of the client area. +static const int wxTREE_HITTEST_TOLEFT = 0x0400; + // to the left of the client area. +static const int wxTREE_HITTEST_TORIGHT = 0x0800; + // anywhere on the item +static const int wxTREE_HITTEST_ONITEM = wxTREE_HITTEST_ONITEMICON | + wxTREE_HITTEST_ONITEMLABEL | + wxTREE_HITTEST_ONITEMSTATEICON; -// Flags for GetNextItem -enum { - wxTREE_NEXT_CARET, // Retrieves the currently selected item. - wxTREE_NEXT_CHILD, // Retrieves the first child item. The hItem parameter must be NULL. - wxTREE_NEXT_DROPHILITE, // Retrieves the item that is the target of a drag-and-drop operation. - wxTREE_NEXT_FIRSTVISIBLE, // Retrieves the first visible item. - wxTREE_NEXT_NEXT, // Retrieves the next sibling item. - wxTREE_NEXT_NEXTVISIBLE, // Retrieves the next visible item that follows the specified item. - wxTREE_NEXT_PARENT, // Retrieves the parent of the specified item. - wxTREE_NEXT_PREVIOUS, // Retrieves the previous sibling item. - wxTREE_NEXT_PREVIOUSVISIBLE, // Retrieves the first visible item that precedes the specified item. - wxTREE_NEXT_ROOT // Retrieves the first child item of the root item of which the specified item is a part. -}; +// NB: all the following flags are for compatbility only and will be removed in the +// next versions -// Flags for ExpandItem -enum { +// flags for deprecated `Expand(int action)' +enum +{ wxTREE_EXPAND_EXPAND, wxTREE_EXPAND_COLLAPSE, wxTREE_EXPAND_COLLAPSE_RESET, wxTREE_EXPAND_TOGGLE }; -// Flags for InsertItem +// flags for deprecated InsertItem() variant #define wxTREE_INSERT_FIRST 0xFFFF0001 #define wxTREE_INSERT_LAST 0xFFFF0002 -#define wxTREE_INSERT_SORT 0xFFFF0003 -class WXDLLEXPORT wxTreeItem: public wxObject +// ---------------------------------------------------------------------------- +// wxTreeItemId identifies an element of the tree. In this implementation, it's +// just a trivial wrapper around Win32 HTREEITEM. It's opaque for the +// application. +// ---------------------------------------------------------------------------- +class WXDLLEXPORT wxTreeItemId { - DECLARE_DYNAMIC_CLASS(wxTreeItem) public: - long m_mask; - long m_itemId; - long m_state; - long m_stateMask; - wxString m_text; - int m_image; - int m_selectedImage; - int m_children; - long m_data; + // ctors + // 0 is invalid value for HTREEITEM + wxTreeItemId() { m_itemId = 0; } - wxTreeItem(); + // default copy ctor/assignment operator are ok for us -// Accessors - inline long GetMask() const { return m_mask; } - inline long GetItemId() const { return m_itemId; } - inline long GetState() const { return m_state; } - inline long GetStateMask() const { return m_stateMask; } - inline wxString GetText() const { return m_text; } - inline int GetImage() const { return m_image; } - inline int GetSelectedImage() const { return m_selectedImage; } - inline int GetChildren() const { return m_children; } - inline long GetData() const { return m_data; } + // accessors + // is this a valid tree item? + bool IsOk() const { return m_itemId != 0; } - inline void SetMask(long mask) { m_mask = mask; } - inline void SetItemId(long id) { m_itemId = m_itemId = id; } - inline void SetState(long state) { m_state = state; } - inline void SetStateMask(long stateMask) { m_stateMask = stateMask; } - inline void GetText(const wxString& text) { m_text = text; } - inline void SetImage(int image) { m_image = image; } - inline void GetSelectedImage(int selImage) { m_selectedImage = selImage; } - inline void SetChildren(int children) { m_children = children; } - inline void SetData(long data) { m_data = data; } + // conversion to/from either real (system-dependent) tree item id or + // to "long" which used to be the type for tree item ids in previous + // versions of wxWindows + +#ifdef wxHTREEITEM_DEFINED + // for wxTreeCtrl usage only + wxTreeItemId(HTREEITEM itemId) { m_itemId = (long)itemId; } + operator HTREEITEM() const { return (HTREEITEM)m_itemId; } +#else // !wxHTREEITEM_DEFINED + // deprecated: only for compatibility + wxTreeItemId(long itemId) { m_itemId = itemId; } + operator long() const { return m_itemId; } +#endif // wxHTREEITEM_DEFINED + +protected: + long m_itemId; }; -class WXDLLEXPORT wxTreeCtrl: public wxControl +// ---------------------------------------------------------------------------- +// wxTreeItemData is some (arbitrary) user class associated with some item. The +// main advantage of having this class (compared to old untyped interface) is +// that wxTreeItemData's are destroyed automatically by the tree and, as this +// class has virtual dtor, it means that the memory will be automatically +// freed. OTOH, we don't just use wxObject instead of wxTreeItemData because +// the size of this class is critical: in any real application, each tree leaf +// will have wxTreeItemData associated with it and number of leaves may be +// quite big. +// +// Because the objects of this class are deleted by the tree, they should +// always be allocated on the heap! +// ---------------------------------------------------------------------------- +class WXDLLEXPORT wxTreeItemData +{ +friend class wxTreeCtrl; +public: + // creation/destruction + // -------------------- + // default ctor + wxTreeItemData() { } + + // default copy ctor/assignment operator are ok + + // dtor is virtual and all the items are deleted by the tree control + // when it's deleted, so you normally don't have to care about freeing + // memory allocated in your wxTreeItemData-derived class + virtual ~wxTreeItemData() { } + + // accessor: get the item associated with us + const wxTreeItemId& GetItemId() const { return m_itemId; } + +protected: + wxTreeItemId m_itemId; +}; + +// ---------------------------------------------------------------------------- +// wxTreeCtrl +// ---------------------------------------------------------------------------- +class WXDLLEXPORT wxTreeCtrl : public wxControl { public: - /* - * Public interface - */ - // creation // -------- - wxTreeCtrl(); - - inline wxTreeCtrl(wxWindow *parent, wxWindowID id = -1, - const wxPoint& pos = wxDefaultPosition, - const wxSize& size = wxDefaultSize, - long style = wxTR_HAS_BUTTONS|wxTR_LINES_AT_ROOT, - const wxValidator& validator = wxDefaultValidator, - const wxString& name = "wxTreeCtrl") + wxTreeCtrl() { Init(); } + + wxTreeCtrl(wxWindow *parent, wxWindowID id = -1, + const wxPoint& pos = wxDefaultPosition, + const wxSize& size = wxDefaultSize, + long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT, + const wxValidator& validator = wxDefaultValidator, + const wxString& name = "wxTreeCtrl") { Create(parent, id, pos, size, style, validator, name); } - ~wxTreeCtrl(); - + + virtual ~wxTreeCtrl(); + bool Create(wxWindow *parent, wxWindowID id = -1, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, - long style = wxTR_HAS_BUTTONS|wxTR_LINES_AT_ROOT, + long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT, const wxValidator& validator = wxDefaultValidator, const wxString& name = "wxTreeCtrl"); - + // accessors // --------- - // - int GetCount() const; - // indent - int GetIndent() const; - void SetIndent(int indent); - // image list - wxImageList *GetImageList(int which = wxIMAGE_LIST_NORMAL) const; - void SetImageList(wxImageList *imageList, int which = wxIMAGE_LIST_NORMAL); + // get the total number of items in the control + size_t GetCount() const; - // navigation inside the tree - long GetNextItem(long item, int code) const; - bool ItemHasChildren(long item) const; - long GetChild(long item) const; - long GetParent(long item) const; - long GetFirstVisibleItem() const; - long GetNextVisibleItem(long item) const; - long GetSelection() const; - long GetRootItem() 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); - // generic function for (g|s)etting item attributes - bool GetItem(wxTreeItem& info) const; - bool SetItem(wxTreeItem& info); - // item state - int GetItemState(long item, long stateMask) const; - bool SetItemState(long item, long state, long stateMask); - // item image - bool SetItemImage(long item, int image, int selImage); - // item text - wxString GetItemText(long item) const; - void SetItemText(long item, const wxString& str); - // custom data associated with the item - long GetItemData(long item) const; - bool SetItemData(long item, long data); - // convenience function - bool IsItemExpanded(long item) - { - return (GetItemState(item, wxTREE_STATE_EXPANDED) & - wxTREE_STATE_EXPANDED) != 0; - } + // 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; + wxImageList *GetStateImageList() const; + + void SetImageList(wxImageList *imageList); + void SetStateImageList(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 the normal item image + int GetItemImage(const wxTreeItemId& item) const; + // get the selected item image + int GetItemSelectedImage(const wxTreeItemId& item) const; + // get the data associated with the item + wxTreeItemData *GetItemData(const wxTreeItemId& item) const; + + // modifiers + // --------- + + // set items label + void SetItemText(const wxTreeItemId& item, const wxString& text); + // set the normal item image + void SetItemImage(const wxTreeItemId& item, int image); + // set the selected item image + void SetItemSelectedImage(const wxTreeItemId& item, int image); + // associate some data with the item + void SetItemData(const wxTreeItemId& item, wxTreeItemData *data); + + // 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; + + // navigation + // ---------- + + // wxTreeItemId.IsOk() will return FALSE if there is no such item + + // get the root tree item + wxTreeItemId GetRootItem() const; + + // get the item currently selected (may return NULL if no selection) + wxTreeItemId GetSelection() const; + + // get the parent of this item (may return NULL if root) + wxTreeItemId GetParent(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, long& cookie) const; + // get the next child + wxTreeItemId GetNextChild(const wxTreeItemId& item, long& cookie) 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; - // bounding rect - bool GetItemRect(long item, wxRectangle& rect, bool textOnly = FALSE) const; - // - wxTextCtrl* GetEditControl() const; - // operations // ---------- - // adding/deleting items - bool DeleteItem(long item); - long InsertItem(long parent, wxTreeItem& info, - long insertAfter = wxTREE_INSERT_LAST); - // If image > -1 and selImage == -1, the same image is used for - // both selected and unselected items. - long InsertItem(long parent, const wxString& label, - int image = -1, int selImage = -1, - long insertAfter = wxTREE_INSERT_LAST); - // changing item state - bool ExpandItem(long item) { return ExpandItem(item, wxTREE_EXPAND_EXPAND); } - bool CollapseItem(long item) { return ExpandItem(item, wxTREE_EXPAND_COLLAPSE); } - bool ToggleItem(long item) { return ExpandItem(item, wxTREE_EXPAND_TOGGLE); } - // common interface for {Expand|Collapse|Toggle}Item - bool ExpandItem(long item, int action); - - // - bool SelectItem(long item); - bool ScrollTo(long item); - bool DeleteAllItems(); + // add the root node to the tree + wxTreeItemId AddRoot(const wxString& text, + int image = -1, int selectedImage = -1, + wxTreeItemData *data = NULL); - // Edit the label (tree must have the focus) - wxTextCtrl* EditLabel(long item, wxClassInfo* textControlClass = CLASSINFO(wxTextCtrl)); + // insert a new item in as the first child of the parent + wxTreeItemId PrependItem(const wxTreeItemId& parent, + const wxString& text, + int image = -1, int selectedImage = -1, + wxTreeItemData *data = NULL); - // End label editing, optionally cancelling the edit - bool EndEditLabel(bool cancel); + // insert a new item after a given one + wxTreeItemId InsertItem(const wxTreeItemId& parent, + const wxTreeItemId& idPrevious, + const wxString& text, + int image = -1, int selectedImage = -1, + wxTreeItemData *data = NULL); - long HitTest(const wxPoint& point, int& flags); - // wxImageList *CreateDragImage(long item); - bool SortChildren(long item); - bool EnsureVisible(long item); - - // IMPLEMENTATION + // insert a new item in as the last child of the parent + wxTreeItemId AppendItem(const wxTreeItemId& parent, + const wxString& text, + int image = -1, int selectedImage = -1, + wxTreeItemData *data = NULL); + + // delete this item and associated data if any + void Delete(const wxTreeItemId& item); + // delete all items from the tree + void DeleteAllItems(); + + // 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); + + // remove the selection from currently selected item (if any) + void Unselect(); + // select this item + void SelectItem(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, + 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& item, bool discardChanges = FALSE); + + // sort the children of this item using the specified callback function + // (it should return -1, 0 or +1 as usual), if it's not specified + // alphabetical comparaison is performed. + // + // NB: this function is not reentrant! + void SortChildren(const wxTreeItemId& item, + wxTreeItemCmpFunc *cmpFunction = NULL); + + // helpers + // ------- + + // @@@ do we really need to expose these functions to the application? + + // get the bounding rectangle of the item (or of its label only) + void GetBoundingRect(const wxTreeItemId& item, + wxRectangle& rect, + bool textOnly = FALSE) const; + + // 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 + wxTreeItemId HitTest(const wxPoint& point, int& flags); + + // deprecated + // ---------- + + // these methods are deprecated and will be removed in future versions of + // wxWindows, they're here for compatibility only, don't use them in new + // code (the comments indicate why these methods are now useless and how to + // replace them) + + // use Expand, Collapse, CollapseAndReset or Toggle + void ExpandItem(const wxTreeItemId& item, int action); + + // use AddRoot, PrependItem or AppendItem + wxTreeItemId InsertItem(const wxTreeItemId& parent, + const wxString& text, + int image = -1, int selImage = -1, + long insertAfter = wxTREE_INSERT_LAST); + + // use Set/GetImageList and Set/GetStateImageList + wxImageList *GetImageList(int) const + { return GetImageList(); } + void SetImageList(wxImageList *imageList, int) + { SetImageList(imageList); } + + // implementation + // -------------- void Command(wxCommandEvent& event) { ProcessCommand(event); }; bool MSWCommand(WXUINT param, WXWORD id); bool MSWNotify(WXWPARAM wParam, WXLPARAM lParam); - + protected: - wxTextCtrl* m_textCtrl; - wxImageList* m_imageListNormal; - wxImageList* m_imageListState; + // SetImageList helper + void SetAnyImageList(wxImageList *imageList, int which); + + wxTextCtrl* m_textCtrl; // used while editing the item label + wxImageList *m_imageListNormal, // images for tree elements + *m_imageListState; // special images for app defined states + +private: + // the common part of all ctors + void Init(); + + // helper functions + inline bool DoGetItem(wxTreeViewItem *tvItem) const; + inline void DoSetItem(wxTreeViewItem *tvItem); + + inline void DoExpand(const wxTreeItemId& item, int flag); + + wxTreeItemId DoInsertItem(const wxTreeItemId& parent, + wxTreeItemId hInsertAfter, + const wxString& text, + int image, int selectedImage, + wxTreeItemData *data); + + void DeleteTextCtrl(); DECLARE_DYNAMIC_CLASS(wxTreeCtrl) }; -/* - wxEVT_COMMAND_TREE_BEGIN_DRAG, - wxEVT_COMMAND_TREE_BEGIN_RDRAG, - wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, - wxEVT_COMMAND_TREE_END_LABEL_EDIT, - wxEVT_COMMAND_TREE_DELETE_ITEM, - wxEVT_COMMAND_TREE_GET_INFO, - wxEVT_COMMAND_TREE_SET_INFO, - wxEVT_COMMAND_TREE_ITEM_EXPANDED, - wxEVT_COMMAND_TREE_ITEM_EXPANDING, - wxEVT_COMMAND_TREE_ITEM_COLLAPSED, - wxEVT_COMMAND_TREE_ITEM_COLLAPSING, - wxEVT_COMMAND_TREE_SEL_CHANGED, - wxEVT_COMMAND_TREE_SEL_CHANGING, - wxEVT_COMMAND_TREE_KEY_DOWN -*/ - -class WXDLLEXPORT wxTreeEvent: public wxCommandEvent +// ---------------------------------------------------------------------------- +// wxTreeEvent is a special class for all events associated with tree controls +// +// NB: note that not all accessors make sense for all events, see the event +// descriptions below +// ---------------------------------------------------------------------------- +class WXDLLEXPORT wxTreeEvent : public wxCommandEvent { - DECLARE_DYNAMIC_CLASS(wxTreeEvent) +friend wxTreeCtrl; +public: + wxTreeEvent(wxEventType commandType = wxEVT_NULL, int id = 0); - public: - wxTreeEvent(wxEventType commandType = wxEVT_NULL, int id = 0); + // accessors + // get the item on which the operation was performed or the newly + // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events + wxTreeItemId GetItem() const { return m_item; } - int m_code; - wxTreeItem m_item; - long m_oldItem; - wxPoint m_pointDrag; + // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously + // selected item + wxTreeItemId GetOldItem() const { return m_itemOld; } - inline long GetOldItem() const { return m_oldItem; } - inline wxTreeItem& GetItem() const { return (wxTreeItem&) m_item; } - inline wxPoint GetPoint() const { return m_pointDrag; } - inline int GetCode() const { return m_code; } + // the point where the mouse was when the drag operation started (for + // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only) + wxPoint GetPoint() const { return m_pointDrag; } + + // keyboard code (for wxEVT_COMMAND_TREE_KEY_DOWN only) + int GetCode() const { return m_code; } + + // set return code for wxEVT_COMMAND_TREE_ITEM_{EXPAND|COLLAPS}ING events + // call this to forbid the change in item status + void Veto() { m_code = TRUE; } + +private: + // @@ we could save some space by using union here + int m_code; + wxTreeItemId m_item, + m_itemOld; + wxPoint m_pointDrag; + + DECLARE_DYNAMIC_CLASS(wxTreeEvent) }; typedef void (wxEvtHandler::*wxTreeEventFunction)(wxTreeEvent&); +// ---------------------------------------------------------------------------- +// macros for handling tree control events +// ---------------------------------------------------------------------------- + +// GetItem() returns the item being dragged, GetPoint() the mouse coords #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, + +// GetItem() returns the itme whose label is being edited #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, -#define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, + +// provide/update information about GetItem() item #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, + +// GetItem() is the item being expanded/collapsed, the "ING" versions can use #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, #define EVT_TREE_ITEM_COLLAPSED(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, #define EVT_TREE_ITEM_COLLAPSING(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL }, + +// GetOldItem() is the item which had the selection previously, GetItem() is +// the item which acquires selection #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL }, #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL }, + +// GetCode() returns the key code +// NB: this is the only message for which GetItem() is invalid (you may get the +// item from GetSelection()) #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL }, +// GetItem() returns the item being deleted, the associated data (if any) will +// be deleted just after the return of this event handler (if any) +#define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, + #endif // _WX_TREECTRL_H_ diff --git a/src/msw/treectrl.cpp b/src/msw/treectrl.cpp index 24acc2ab18..98cd3568a1 100644 --- a/src/msw/treectrl.cpp +++ b/src/msw/treectrl.cpp @@ -2,1004 +2,909 @@ // Name: treectrl.cpp // Purpose: wxTreeCtrl // Author: Julian Smart -// Modified by: +// Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98 // Created: 1997 // RCS-ID: $Id$ // Copyright: (c) Julian Smart -// Licence: wxWindows licence +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- #ifdef __GNUG__ -#pragma implementation "treectrl.h" + #pragma implementation "treectrl.h" #endif // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ -#pragma hdrstop + #pragma hdrstop #endif #ifndef WX_PRECOMP -#include "wx.h" + #include "wx.h" #endif -#include "wx/log.h" - #if defined(__WIN95__) -#include "wx/treectrl.h" +#include "wx/log.h" +#include "wx/imaglist.h" + #include "wx/msw/private.h" #ifndef __GNUWIN32__ -#include + #include #endif +#define wxHTREEITEM_DEFINED // flag used in wx/msw/treectrl.h +#include "wx/treectrl.h" + // Bug in headers, sometimes #ifndef TVIS_FOCUSED -#define TVIS_FOCUSED 0x0001 + #define TVIS_FOCUSED 0x0001 #endif -static void wxConvertToMSWTreeItem(wxTreeItem& info, TV_ITEM& tvItem); -static void wxConvertFromMSWTreeItem(wxTreeItem& info, TV_ITEM& tvItem, HWND getFullInfo = 0); +// ---------------------------------------------------------------------------- +// private classes +// ---------------------------------------------------------------------------- + +// a convenient wrapper around TV_ITEM struct which adds a ctor +struct wxTreeViewItem : public TV_ITEM +{ + wxTreeViewItem(const wxTreeItemId& item, + UINT mask_, UINT stateMask_ = 0) + { + mask = mask_; + stateMask = stateMask_; + hItem = (HTREEITEM)item; + } +}; + +// ---------------------------------------------------------------------------- +// macros +// ---------------------------------------------------------------------------- #if !USE_SHARED_LIBRARY -IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl) -IMPLEMENT_DYNAMIC_CLASS(wxTreeItem, wxObject) - + IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl) #endif -wxTreeCtrl::wxTreeCtrl(void) +// hide the ugly cast (of course, the macro is _quite_ ugly too...) +#define hwnd ((HWND)m_hWnd) + +// ---------------------------------------------------------------------------- +// variables +// ---------------------------------------------------------------------------- + +// handy table for sending events +static const wxEventType g_events[2][2] = { - m_imageListNormal = NULL; - m_imageListState = NULL; - m_textCtrl = NULL; + { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING }, + { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING } +}; + +// ============================================================================ +// implementation +// ============================================================================ + +// ---------------------------------------------------------------------------- +// construction and destruction +// ---------------------------------------------------------------------------- + +void wxTreeCtrl::Init() +{ + m_imageListNormal = NULL; + m_imageListState = NULL; + m_textCtrl = NULL; } -bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, - long style, const wxValidator& validator, const wxString& name) +bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id, + const wxPoint& pos, const wxSize& size, + long style, const wxValidator& validator, + const wxString& name) { - wxSystemSettings settings; - SetBackgroundColour(settings.GetSystemColour(wxSYS_COLOUR_WINDOW)); - SetForegroundColour(parent->GetForegroundColour()); + Init(); - SetName(name); - SetValidator(validator); + wxSystemSettings settings; - m_imageListNormal = NULL; - m_imageListState = NULL; - m_textCtrl = NULL; + SetName(name); + SetValidator(validator); - int x = pos.x; - int y = pos.y; - int width = size.x; - int height = size.y; + m_windowStyle = style; - m_windowStyle = style; + SetParent(parent); -// SetFont(wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL)); + m_windowId = (id == -1) ? NewControlId() : id; - SetParent(parent); + DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES; - if (width <= 0) - width = 100; - if (height <= 0) - height = 30; - if (x < 0) - x = 0; - if (y < 0) - y = 0; + bool want3D; + WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ; - m_windowId = (id == -1) ? NewControlId() : id; - - DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES; - - bool want3D; - WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ; - - // Even with extended styles, need to combine with WS_BORDER - // for them to look right. - if (want3D || (m_windowStyle & wxSIMPLE_BORDER) || (m_windowStyle & wxRAISED_BORDER) || - (m_windowStyle & wxSUNKEN_BORDER) || (m_windowStyle & wxDOUBLE_BORDER)) - wstyle |= WS_BORDER; - - if ( m_windowStyle & wxTR_HAS_BUTTONS ) - wstyle |= TVS_HASBUTTONS; - - if ( m_windowStyle & wxTR_EDIT_LABELS ) - wstyle |= TVS_EDITLABELS; - - if ( m_windowStyle & wxTR_LINES_AT_ROOT ) - wstyle |= TVS_LINESATROOT; - - // Create the toolbar control. - HWND hWndTreeControl = CreateWindowEx(exStyle, - WC_TREEVIEW, - "", - wstyle, - x, y, width, height, - (HWND) parent->GetHWND(), - (HMENU)m_windowId, - wxGetInstance(), - NULL ); - - m_hWnd = (WXHWND) hWndTreeControl; - if (parent) parent->AddChild(this); - - SubclassWin((WXHWND) m_hWnd); - - return TRUE; -} - -wxTreeCtrl::~wxTreeCtrl(void) -{ - if (m_textCtrl) + // Even with extended styles, need to combine with WS_BORDER + // for them to look right. + if ( want3D || wxStyleHasBorder(m_windowStyle) ) { - m_textCtrl->UnsubclassWin(); - m_textCtrl->SetHWND((WXHWND) 0); - delete m_textCtrl; + wstyle |= WS_BORDER; + } + + if ( m_windowStyle & wxTR_HAS_BUTTONS ) + wstyle |= TVS_HASBUTTONS; + + if ( m_windowStyle & wxTR_EDIT_LABELS ) + wstyle |= TVS_EDITLABELS; + + if ( m_windowStyle & wxTR_LINES_AT_ROOT ) + wstyle |= TVS_LINESATROOT; + + // Create the tree control. + m_hWnd = (WXHWND)::CreateWindowEx + ( + exStyle, + WC_TREEVIEW, + "", + wstyle, + pos.x, pos.y, size.x, size.y, + (HWND)parent->GetHWND(), + (HMENU)m_windowId, + wxGetInstance(), + NULL + ); + + wxCHECK_MSG( m_hWnd, FALSE, "Failed to create tree ctrl" ); + + if ( parent ) + parent->AddChild(this); + + SubclassWin(m_hWnd); + + return TRUE; +} + +wxTreeCtrl::~wxTreeCtrl() +{ + DeleteTextCtrl(); + + // delete user data to prevent memory leaks + DeleteAllItems(); +} + +// ---------------------------------------------------------------------------- +// accessors +// ---------------------------------------------------------------------------- + +// simple wrappers which add error checking in debug mode + +bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const +{ + if ( !TreeView_GetItem(hwnd, tvItem) ) + { + wxLogLastError("TreeView_GetItem"); + + return FALSE; + } + + return TRUE; +} + +void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem) +{ + if ( TreeView_SetItem(hwnd, tvItem) == -1 ) + { + wxLogLastError("TreeView_SetItem"); } } -// Attributes -int wxTreeCtrl::GetCount(void) const +size_t wxTreeCtrl::GetCount() const { - return (int) TreeView_GetCount((HWND) GetHWND()); + return (size_t)TreeView_GetCount(hwnd); } -int wxTreeCtrl::GetIndent(void) const +unsigned int wxTreeCtrl::GetIndent() const { - return (int) TreeView_GetIndent((HWND) GetHWND()); + return TreeView_GetIndent(hwnd); } -void wxTreeCtrl::SetIndent(int indent) +void wxTreeCtrl::SetIndent(unsigned int indent) { - TreeView_SetIndent((HWND) GetHWND(), indent); + TreeView_SetIndent(hwnd, indent); } -wxImageList *wxTreeCtrl::GetImageList(int which) const +wxImageList *wxTreeCtrl::GetImageList() const { - if ( which == wxIMAGE_LIST_NORMAL ) - { return m_imageListNormal; - } - else if ( which == wxIMAGE_LIST_STATE ) +} + +wxImageList *wxTreeCtrl::GetStateImageList() const +{ + return m_imageListNormal; +} + +void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which) +{ + // no error return + TreeView_SetImageList(hwnd, + imageList ? imageList->GetHIMAGELIST() : 0, + which); +} + +void wxTreeCtrl::SetImageList(wxImageList *imageList) +{ + SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL); +} + +void wxTreeCtrl::SetStateImageList(wxImageList *imageList) +{ + SetAnyImageList(m_imageListState = imageList, TVSIL_STATE); +} + +// ---------------------------------------------------------------------------- +// Item access +// ---------------------------------------------------------------------------- + +wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const +{ + char buf[512]; // the size is arbitrary... + + wxTreeViewItem tvItem(item, TVIF_TEXT); + tvItem.pszText = buf; + tvItem.cchTextMax = WXSIZEOF(buf); + if ( !DoGetItem(&tvItem) ) { - return m_imageListState; - } - return NULL; + // don't return some garbage which was on stack, but an empty string + buf[0] = '\0'; + } + + return wxString(buf); } -void wxTreeCtrl::SetImageList(wxImageList *imageList, int which) +void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) { - int flags = 0; - if ( which == wxIMAGE_LIST_NORMAL ) + wxTreeViewItem tvItem(item, TVIF_TEXT); + tvItem.pszText = (char *)text.c_str(); // conversion is ok + DoSetItem(&tvItem); +} + +int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const +{ + wxTreeViewItem tvItem(item, TVIF_IMAGE); + DoGetItem(&tvItem); + + return tvItem.iImage; +} + +void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image) +{ + wxTreeViewItem tvItem(item, TVIF_IMAGE); + tvItem.iImage = image; + DoSetItem(&tvItem); +} + +int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const +{ + wxTreeViewItem tvItem(item, TVIF_SELECTEDIMAGE); + DoGetItem(&tvItem); + + return tvItem.iSelectedImage; +} + +void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image) +{ + wxTreeViewItem tvItem(item, TVIF_SELECTEDIMAGE); + tvItem.iSelectedImage = image; + DoSetItem(&tvItem); +} + +wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const +{ + wxTreeViewItem tvItem(item, TVIF_PARAM); + if ( !DoGetItem(&tvItem) ) { - flags = TVSIL_NORMAL; - m_imageListNormal = imageList; - } - else if ( which == wxIMAGE_LIST_STATE ) + return NULL; + } + + wxTreeItemData *data = (wxTreeItemData *)tvItem.lParam; + if ( data != NULL ) { - flags = TVSIL_STATE; - m_imageListState = imageList; - } - TreeView_SetImageList((HWND) GetHWND(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags); + // the data object should know about its id + data->m_itemId = item; + } + + return data; } -long wxTreeCtrl::GetNextItem(long item, int code) const +void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) { - UINT flag = 0; - switch ( code ) - { - case wxTREE_NEXT_CARET: - flag = TVGN_CARET; - break; - case wxTREE_NEXT_CHILD: - flag = TVGN_CHILD; - break; - case wxTREE_NEXT_DROPHILITE: - flag = TVGN_DROPHILITE; - break; - case wxTREE_NEXT_FIRSTVISIBLE: - flag = TVGN_FIRSTVISIBLE; - break; - case wxTREE_NEXT_NEXT: - flag = TVGN_NEXT; - break; - case wxTREE_NEXT_NEXTVISIBLE: - flag = TVGN_NEXTVISIBLE; - break; - case wxTREE_NEXT_PARENT: - flag = TVGN_PARENT; - break; - case wxTREE_NEXT_PREVIOUS: - flag = TVGN_PREVIOUS; - break; - case wxTREE_NEXT_PREVIOUSVISIBLE: - flag = TVGN_PREVIOUSVISIBLE; - break; - case wxTREE_NEXT_ROOT: - flag = TVGN_ROOT; - break; - - default : - break; - } - return (long) TreeView_GetNextItem( (HWND) GetHWND(), (HTREEITEM) item, flag); + wxTreeViewItem tvItem(item, TVIF_PARAM); + tvItem.lParam = (LPARAM)data; + DoSetItem(&tvItem); } -bool wxTreeCtrl::ItemHasChildren(long item) const +// ---------------------------------------------------------------------------- +// Item status +// ---------------------------------------------------------------------------- + +bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const { - TV_ITEM item2; - item2.hItem = (HTREEITEM) item; - item2.mask = TVIF_CHILDREN; - TreeView_GetItem((HWND) GetHWND(), &item2); - return (item2.cChildren != 0); + RECT rect; + return TreeView_GetItemRect(hwnd, (HTREEITEM)item, &rect, FALSE) != 0; } -long wxTreeCtrl::GetChild(long item) const +bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const { - return (long) ::SendMessage((HWND) GetHWND(), TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)item); + wxTreeViewItem tvItem(item, TVIF_CHILDREN); + DoGetItem(&tvItem); + + return tvItem.cChildren != 0; } -long wxTreeCtrl::GetParent(long item) const +bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const { - return (long) ::SendMessage((HWND) GetHWND(), TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)item); + // probably not a good idea to put it here + //wxASSERT( ItemHasChildren(item) ); + + wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED); + DoGetItem(&tvItem); + + return (tvItem.state & TVIS_EXPANDED) != 0; } -long wxTreeCtrl::GetFirstVisibleItem(void) const +bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const { - return (long) ::SendMessage((HWND) GetHWND(), TVM_GETNEXTITEM, TVGN_FIRSTVISIBLE, 0); + wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED); + DoGetItem(&tvItem); + + return (tvItem.state & TVIS_SELECTED) != 0; } -long wxTreeCtrl::GetNextVisibleItem(long item) const +// ---------------------------------------------------------------------------- +// navigation +// ---------------------------------------------------------------------------- + +wxTreeItemId wxTreeCtrl::GetRootItem() const { - return (long) ::SendMessage((HWND) GetHWND(), TVM_GETNEXTITEM, TVGN_NEXTVISIBLE, (LPARAM)item); + return wxTreeItemId(TreeView_GetRoot(hwnd)); } -long wxTreeCtrl::GetSelection(void) const +wxTreeItemId wxTreeCtrl::GetSelection() const { - return (long) ::SendMessage((HWND) GetHWND(), TVM_GETNEXTITEM, TVGN_CARET, 0); + return wxTreeItemId(TreeView_GetSelection(hwnd)); } -long wxTreeCtrl::GetRootItem(void) const +wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const { - return (long) ::SendMessage((HWND) GetHWND(), TVM_GETNEXTITEM, TVGN_ROOT, 0); + return wxTreeItemId(TreeView_GetParent(hwnd, item)); } -bool wxTreeCtrl::GetItem(wxTreeItem& info) const +wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, + long& cookie) const { - TV_ITEM tvItem; - tvItem.hItem = (HTREEITEM)info.m_itemId; - tvItem.pszText = NULL; - tvItem.mask = - tvItem.stateMask = 0; + // remember the last child returned in 'cookie' + cookie = (long)TreeView_GetChild(hwnd, (HTREEITEM)item); - // TODO: convert other bits in the mask - if ( info.m_mask & wxTREE_MASK_TEXT ) - { - tvItem.mask |= TVIF_TEXT; - tvItem.pszText = new char[513]; - tvItem.cchTextMax = 512; - } - if ( info.m_mask & wxTREE_MASK_DATA ) - tvItem.mask |= TVIF_PARAM; - if ( info.m_stateMask & wxTREE_STATE_EXPANDED ) { - tvItem.mask |= TVIF_STATE; - tvItem.stateMask |= TVIS_EXPANDED; - } - - bool success = TreeView_GetItem((HWND)GetHWND(), &tvItem) != 0; - - if ( !success ) - { - wxLogSysError("TreeView_GetItem failed"); - - if (tvItem.pszText) - delete[] tvItem.pszText; - - return FALSE; - } - - wxConvertFromMSWTreeItem(info, tvItem); - - if (tvItem.pszText) - delete[] tvItem.pszText; - - return success; + return wxTreeItemId((HTREEITEM)cookie); } -bool wxTreeCtrl::SetItem(wxTreeItem& info) +wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item), + long& cookie) const { - TV_ITEM item; - wxConvertToMSWTreeItem(info, item); - return (::SendMessage((HWND) GetHWND(), TVM_SETITEM, 0, (LPARAM)&item) != 0); + return wxTreeItemId(TreeView_GetNextSibling(hwnd, + (HTREEITEM)cookie)); } -int wxTreeCtrl::GetItemState(long item, long stateMask) const +wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const { - wxTreeItem info; - - info.m_mask = wxTREE_MASK_STATE ; - info.m_stateMask = stateMask; - info.m_itemId = item; - - if (!GetItem(info)) - return 0; - - return info.m_state; + return wxTreeItemId(TreeView_GetNextSibling(hwnd, item)); } -bool wxTreeCtrl::SetItemState(long item, long state, long stateMask) +wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const { - wxTreeItem info; - - info.m_mask = wxTREE_MASK_STATE ; - info.m_state = state; - info.m_stateMask = stateMask; - info.m_itemId = item; - - return SetItem(info); + return wxTreeItemId(TreeView_GetPrevSibling(hwnd, item)); } -bool wxTreeCtrl::SetItemImage(long item, int image, int selImage) +wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const { - wxTreeItem info; - - info.m_mask = wxTREE_MASK_IMAGE ; - info.m_image = image; - if ( selImage > -1) - { - info.m_selectedImage = selImage; - info.m_mask |= wxTREE_MASK_SELECTED_IMAGE; - } - info.m_itemId = item; - - return SetItem(info); + return wxTreeItemId(TreeView_GetFirstVisible(hwnd)); } -wxString wxTreeCtrl::GetItemText(long item) const +wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const { - wxTreeItem info; + wxASSERT_MSG( IsVisible(item), "The item you call GetNextVisible() " + "for must be visible itself!"); - info.m_mask = wxTREE_MASK_TEXT ; - info.m_itemId = item; - - if (!GetItem(info)) - return wxString(""); - return info.m_text; + return wxTreeItemId(TreeView_GetNextVisible(hwnd, item)); } -void wxTreeCtrl::SetItemText(long item, const wxString& str) +wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const { - wxTreeItem info; + wxASSERT_MSG( IsVisible(item), "The item you call GetPrevVisible() " + "for must be visible itself!"); - info.m_mask = wxTREE_MASK_TEXT ; - info.m_itemId = item; - info.m_text = str; - - SetItem(info); + return wxTreeItemId(TreeView_GetPrevVisible(hwnd, item)); } -long wxTreeCtrl::GetItemData(long item) const +// ---------------------------------------------------------------------------- +// Usual operations +// ---------------------------------------------------------------------------- + +wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent, + wxTreeItemId hInsertAfter, + const wxString& text, + int image, int selectedImage, + wxTreeItemData *data) { - wxTreeItem info; + TV_INSERTSTRUCT tvIns; + tvIns.hParent = (HTREEITEM)parent; + tvIns.hInsertAfter = hInsertAfter; + UINT mask = 0; + if ( !text.IsEmpty() ) + { + mask |= TVIF_TEXT; + tvIns.item.pszText = (char *)text.c_str(); // cast is ok + } - info.m_mask = wxTREE_MASK_DATA ; - info.m_itemId = item; + if ( image != -1 ) + { + mask |= TVIF_IMAGE; + tvIns.item.iImage = image; + } - if (!GetItem(info)) - return 0; - return info.m_data; + if ( selectedImage != -1 ) + { + mask |= TVIF_SELECTEDIMAGE; + tvIns.item.iSelectedImage = selectedImage; + } + + if ( data != NULL ) + { + mask |= TVIF_PARAM; + tvIns.item.lParam = (LPARAM)data; + } + + tvIns.item.mask = mask; + + HTREEITEM id = TreeView_InsertItem(hwnd, &tvIns); + if ( id == 0 ) + { + wxLogLastError("TreeView_InsertItem"); + } + + return wxTreeItemId(id); } -bool wxTreeCtrl::SetItemData(long item, long data) +// for compatibility only +wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, + const wxString& text, + int image, int selImage, + long insertAfter) { - wxTreeItem info; - - info.m_mask = wxTREE_MASK_DATA ; - info.m_itemId = item; - info.m_data = data; - - return SetItem(info); + return DoInsertItem(parent, (HTREEITEM)insertAfter, text, + image, selImage, NULL); } -bool wxTreeCtrl::GetItemRect(long item, wxRectangle& rect, bool textOnly) const +wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, + int image, int selectedImage, + wxTreeItemData *data) { - RECT rect2; - - *(HTREEITEM*)& rect2 = (HTREEITEM) item; - bool success = (::SendMessage((HWND) GetHWND(), TVM_GETITEMRECT, (WPARAM)textOnly, - (LPARAM)&rect2) != 0); - - rect.x = rect2.left; - rect.y = rect2.top; - rect.width = rect2.right - rect2.left; - rect.height = rect2.bottom - rect2.left; - return success; + return DoInsertItem(wxTreeItemId(0), 0, + text, image, selectedImage, data); } -wxTextCtrl* wxTreeCtrl::GetEditControl(void) const +wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent, + 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::AppendItem(const wxTreeItemId& parent, + const wxString& text, + int image, int selectedImage, + wxTreeItemData *data) +{ + return DoInsertItem(parent, TVI_LAST, + text, image, selectedImage, data); +} + +void wxTreeCtrl::Delete(const wxTreeItemId& item) +{ + wxTreeItemData *data = GetItemData(item); + delete data; // may be NULL, ok + + if ( !TreeView_DeleteItem(hwnd, (HTREEITEM)item) ) + { + wxLogLastError("TreeView_DeleteItem"); + } +} + +void wxTreeCtrl::DeleteAllItems() +{ + if ( !TreeView_DeleteAllItems(hwnd) ) + { + wxLogLastError("TreeView_DeleteAllItems"); + } +} + +void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag) +{ + wxASSERT_MSG( flag == TVE_COLLAPSE || flag == TVE_COLLAPSERESET || + flag == TVE_EXPAND || flag == TVE_TOGGLE, + "Unknown flag in wxTreeCtrl::DoExpand" ); + + // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must + // emulate them + if ( TreeView_Expand(hwnd, item, flag) != 0 ) + { + wxTreeEvent event(wxEVT_NULL, m_windowId); + event.m_item = item; + + bool isExpanded = IsExpanded(item); + + event.SetEventObject(this); + + // @@@ return values of {EXPAND|COLLAPS}ING event handler is discarded + event.SetEventType(g_events[isExpanded][TRUE]); + GetEventHandler()->ProcessEvent(event); + + event.SetEventType(g_events[isExpanded][FALSE]); + GetEventHandler()->ProcessEvent(event); + } + else + { + // I wonder if it really ever happens... + wxLogDebug("TreeView_Expand: change didn't took place."); + } +} + +void wxTreeCtrl::Expand(const wxTreeItemId& item) +{ + DoExpand(item, TVE_EXPAND); +} + +void wxTreeCtrl::Collapse(const wxTreeItemId& item) +{ + DoExpand(item, TVE_COLLAPSE); +} + +void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item) +{ + DoExpand(item, TVE_COLLAPSERESET); +} + +void wxTreeCtrl::Toggle(const wxTreeItemId& item) +{ + DoExpand(item, TVE_TOGGLE); +} + +void wxTreeCtrl::Unselect() +{ + SelectItem(wxTreeItemId(0)); +} + +void wxTreeCtrl::SelectItem(const wxTreeItemId& item) +{ + if ( !TreeView_SelectItem(hwnd, item) ) + { + wxLogLastError("TreeView_SelectItem"); + } +} + +void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item) +{ + // no error return + TreeView_EnsureVisible(hwnd, item); +} + +void wxTreeCtrl::ScrollTo(const wxTreeItemId& item) +{ + if ( !TreeView_SelectSetFirstVisible(hwnd, item) ) + { + wxLogLastError("TreeView_SelectSetFirstVisible"); + } +} + +wxTextCtrl* wxTreeCtrl::GetEditControl() const { return m_textCtrl; } -// Operations -bool wxTreeCtrl::DeleteItem(long item) +void wxTreeCtrl::DeleteTextCtrl() { - return (TreeView_DeleteItem((HWND) GetHWND(), (HTREEITEM) item) != 0); -} - -bool wxTreeCtrl::ExpandItem(long item, int action) -{ - UINT mswAction = TVE_EXPAND; - switch ( action ) - { - case wxTREE_EXPAND_EXPAND: - mswAction = TVE_EXPAND; - break; - - case wxTREE_EXPAND_COLLAPSE: - mswAction = TVE_COLLAPSE; - break; - - case wxTREE_EXPAND_COLLAPSE_RESET: - // @@@ it should also delete all the items! currently, if you do use this - // code your program will probaly crash. Feel free to remove this if - // it does work - wxFAIL_MSG("wxTREE_EXPAND_COLLAPSE_RESET probably doesn't work."); - mswAction = TVE_COLLAPSERESET; - break; - - case wxTREE_EXPAND_TOGGLE: - mswAction = TVE_TOGGLE; - break; - - default: - wxFAIL_MSG("unknown action in wxTreeCtrl::ExpandItem"); - } - - bool bOk = TreeView_Expand((HWND)GetHWND(), (HTREEITEM)item, mswAction) != 0; - - // TreeView_Expand doesn't send TVN_EXPAND(ING) messages, so emulate them - if ( bOk ) { - wxTreeEvent event(wxEVT_NULL, m_windowId); - event.m_item.m_itemId = item; - event.m_item.m_mask = - event.m_item.m_stateMask = 0xffff; // get all - GetItem(event.m_item); - - bool bIsExpanded = (event.m_item.m_state & wxTREE_STATE_EXPANDED) != 0; - - event.m_code = action; - event.SetEventObject(this); - - // @@@ return values of {EXPAND|COLLAPS}ING event handler is discarded - event.SetEventType(bIsExpanded ? wxEVT_COMMAND_TREE_ITEM_EXPANDING - : wxEVT_COMMAND_TREE_ITEM_COLLAPSING); - GetEventHandler()->ProcessEvent(event); - - event.SetEventType(bIsExpanded ? wxEVT_COMMAND_TREE_ITEM_EXPANDED - : wxEVT_COMMAND_TREE_ITEM_COLLAPSED); - GetEventHandler()->ProcessEvent(event); - } - - return bOk; -} - -long wxTreeCtrl::InsertItem(long parent, wxTreeItem& info, long insertAfter) -{ - TV_INSERTSTRUCT tvInsertStruct; - tvInsertStruct.hParent = (HTREEITEM) parent ; - tvInsertStruct.hInsertAfter = (HTREEITEM) insertAfter ; - - wxConvertToMSWTreeItem(info, tvInsertStruct.item); - - return (long) TreeView_InsertItem((HWND) GetHWND(), & tvInsertStruct); -} - -long wxTreeCtrl::InsertItem(long parent, const wxString& label, int image, int selImage, - long insertAfter) -{ - wxTreeItem info; - info.m_text = label; - info.m_mask = wxTREE_MASK_TEXT; - if ( image > -1 ) - { - info.m_mask |= wxTREE_MASK_IMAGE | wxTREE_MASK_SELECTED_IMAGE; - info.m_image = image; - if ( selImage == -1 ) - info.m_selectedImage = image; - else - info.m_selectedImage = selImage; - } - - return InsertItem(parent, info, insertAfter); -} - -bool wxTreeCtrl::SelectItem(long item) -{ - return (TreeView_SelectItem((HWND) GetHWND(), (HTREEITEM) item) != 0); -} - -bool wxTreeCtrl::ScrollTo(long item) -{ - return (TreeView_SelectSetFirstVisible((HWND) GetHWND(), (HTREEITEM) item) != 0); -} - -bool wxTreeCtrl::DeleteAllItems(void) -{ - return (TreeView_DeleteAllItems((HWND) GetHWND()) != 0); -} - -wxTextCtrl* wxTreeCtrl::EditLabel(long item, wxClassInfo* textControlClass) -{ - wxASSERT( (textControlClass->IsKindOf(CLASSINFO(wxTextCtrl))) ); - - HWND hWnd = (HWND) TreeView_EditLabel((HWND) GetHWND(), item); - - if (m_textCtrl) + if ( m_textCtrl ) { - m_textCtrl->UnsubclassWin(); - m_textCtrl->SetHWND(0); - delete m_textCtrl; - m_textCtrl = NULL; + m_textCtrl->UnsubclassWin(); + m_textCtrl->SetHWND(0); + delete m_textCtrl; + m_textCtrl = NULL; } +} - m_textCtrl = (wxTextCtrl*) textControlClass->CreateObject(); - m_textCtrl->SetHWND((WXHWND) hWnd); - m_textCtrl->SubclassWin((WXHWND) hWnd); +wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item, + wxClassInfo* textControlClass) +{ + wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) ); + + HWND hWnd = TreeView_EditLabel(hwnd, item); + + wxCHECK_MSG( hWnd, NULL, "Can't edit tree ctrl label" ); + + DeleteTextCtrl(); + + m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); + m_textCtrl->SetHWND((WXHWND)hWnd); + m_textCtrl->SubclassWin((WXHWND)hWnd); return m_textCtrl; } // End label editing, optionally cancelling the edit -bool wxTreeCtrl::EndEditLabel(bool cancel) +void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges) { - bool success = (TreeView_EndEditLabelNow((HWND) GetHWND(), cancel) != 0); + TreeView_EndEditLabelNow(hwnd, discardChanges); - if (m_textCtrl) - { - m_textCtrl->UnsubclassWin(); - m_textCtrl->SetHWND(0); - delete m_textCtrl; - m_textCtrl = NULL; - } - return success; + DeleteTextCtrl(); } -long wxTreeCtrl::HitTest(const wxPoint& point, int& flags) +wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags) { TV_HITTESTINFO hitTestInfo; - hitTestInfo.pt.x = (int) point.x; - hitTestInfo.pt.y = (int) point.y; + hitTestInfo.pt.x = (int)point.x; + hitTestInfo.pt.y = (int)point.y; - TreeView_HitTest((HWND) GetHWND(), & hitTestInfo); + TreeView_HitTest(hwnd, &hitTestInfo); - flags = 0; - if ( hitTestInfo.flags & TVHT_ABOVE ) - flags |= wxTREE_HITTEST_ABOVE; - if ( hitTestInfo.flags & TVHT_BELOW ) - flags |= wxTREE_HITTEST_BELOW; - if ( hitTestInfo.flags & TVHT_NOWHERE ) - flags |= wxTREE_HITTEST_NOWHERE; - if ( hitTestInfo.flags & TVHT_ONITEMBUTTON ) - flags |= wxTREE_HITTEST_ONITEMBUTTON; - if ( hitTestInfo.flags & TVHT_ONITEMICON ) - flags |= wxTREE_HITTEST_ONITEMICON; - if ( hitTestInfo.flags & TVHT_ONITEMINDENT ) - flags |= wxTREE_HITTEST_ONITEMINDENT; - if ( hitTestInfo.flags & TVHT_ONITEMLABEL ) - flags |= wxTREE_HITTEST_ONITEMLABEL; - if ( hitTestInfo.flags & TVHT_ONITEMRIGHT ) - flags |= wxTREE_HITTEST_ONITEMRIGHT; - if ( hitTestInfo.flags & TVHT_ONITEMSTATEICON ) - flags |= wxTREE_HITTEST_ONITEMSTATEICON; - if ( hitTestInfo.flags & TVHT_TOLEFT ) - flags |= wxTREE_HITTEST_TOLEFT; - if ( hitTestInfo.flags & TVHT_TORIGHT ) - flags |= wxTREE_HITTEST_TORIGHT; + flags = 0; - return (long) hitTestInfo.hItem ; + // avoid repetition + #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \ + flags |= wxTREE_HITTEST_##flag + + TRANSLATE_FLAG(ABOVE); + TRANSLATE_FLAG(BELOW); + TRANSLATE_FLAG(NOWHERE); + TRANSLATE_FLAG(ONITEMBUTTON); + TRANSLATE_FLAG(ONITEMICON); + TRANSLATE_FLAG(ONITEMINDENT); + TRANSLATE_FLAG(ONITEMLABEL); + TRANSLATE_FLAG(ONITEMRIGHT); + TRANSLATE_FLAG(ONITEMSTATEICON); + TRANSLATE_FLAG(TOLEFT); + TRANSLATE_FLAG(TORIGHT); + + #undef TRANSLATE_FLAG + + return wxTreeItemId(hitTestInfo.hItem); } -/* -wxImageList *wxTreeCtrl::CreateDragImage(long item) +void wxTreeCtrl::SortChildren(const wxTreeItemId& item, + wxTreeItemCmpFunc *cmpFunction) { -} -*/ - -bool wxTreeCtrl::SortChildren(long item) -{ - return (TreeView_SortChildren((HWND) GetHWND(), (HTREEITEM) item, 0) != 0); + if ( cmpFunction == NULL ) + { + TreeView_SortChildren(hwnd, item, 0); + } + else + { + // TODO: use TreeView_SortChildrenCB + wxFAIL_MSG("wxTreeCtrl::SortChildren not implemented"); + } } -bool wxTreeCtrl::EnsureVisible(long item) -{ - return (TreeView_EnsureVisible((HWND) GetHWND(), (HTREEITEM) item) != 0); -} +// ---------------------------------------------------------------------------- +// implementation +// ---------------------------------------------------------------------------- bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id) { - if (cmd == EN_UPDATE) - { - wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id); - event.SetEventObject( this ); - ProcessCommand(event); + if ( cmd == EN_UPDATE ) + { + wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id); + event.SetEventObject( this ); + ProcessCommand(event); + } + else if ( cmd == EN_KILLFOCUS ) + { + wxCommandEvent event(wxEVT_KILL_FOCUS, id); + event.SetEventObject( this ); + ProcessCommand(event); + } + else + { + // nothing done + return FALSE; + } + + // command processed return TRUE; - } - else if (cmd == EN_KILLFOCUS) - { - wxCommandEvent event(wxEVT_KILL_FOCUS, id); - event.SetEventObject( this ); - ProcessCommand(event); - return TRUE; - } - else return FALSE; } +// process WM_NOTIFY Windows message bool wxTreeCtrl::MSWNotify(WXWPARAM wParam, WXLPARAM lParam) { - wxTreeEvent event(wxEVT_NULL, m_windowId); - wxEventType eventType = wxEVT_NULL; - NMHDR* hdr1 = (NMHDR*) lParam; - switch ( hdr1->code ) - { - case TVN_BEGINDRAG: - { - eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG; - NM_TREEVIEW* hdr = (NM_TREEVIEW*)lParam; - wxConvertFromMSWTreeItem(event.m_item, hdr->itemNew, (HWND) GetHWND()); - event.m_pointDrag.x = hdr->ptDrag.x; - event.m_pointDrag.y = hdr->ptDrag.y; - break; - } - case TVN_BEGINLABELEDIT: - { - eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT; - TV_DISPINFO *info = (TV_DISPINFO *)lParam; - wxConvertFromMSWTreeItem(event.m_item, info->item, (HWND) GetHWND()); - break; - } - case TVN_BEGINRDRAG: - { - eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG; - NM_TREEVIEW* hdr = (NM_TREEVIEW*)lParam; - wxConvertFromMSWTreeItem(event.m_item, hdr->itemNew, (HWND) GetHWND()); - event.m_pointDrag.x = hdr->ptDrag.x; - event.m_pointDrag.y = hdr->ptDrag.y; - break; - } - case TVN_DELETEITEM: - { - eventType = wxEVT_COMMAND_TREE_DELETE_ITEM; - NM_TREEVIEW* hdr = (NM_TREEVIEW*)lParam; - wxConvertFromMSWTreeItem(event.m_item, hdr->itemOld, (HWND) GetHWND()); - event.m_pointDrag.x = hdr->ptDrag.x; - event.m_pointDrag.y = hdr->ptDrag.y; - break; - } - case TVN_ENDLABELEDIT: - { - eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT; - TV_DISPINFO *info = (TV_DISPINFO *)lParam; - wxConvertFromMSWTreeItem(event.m_item, info->item, (HWND) GetHWND()); - break; - } - case TVN_GETDISPINFO: - { - eventType = wxEVT_COMMAND_TREE_GET_INFO; - TV_DISPINFO *info = (TV_DISPINFO *)lParam; - wxConvertFromMSWTreeItem(event.m_item, info->item, (HWND) GetHWND()); - break; - } - case TVN_ITEMEXPANDING: - { - NM_TREEVIEW* hdr = (NM_TREEVIEW*)lParam; - wxConvertFromMSWTreeItem(event.m_item, hdr->itemNew, (HWND) GetHWND()); + wxTreeEvent event(wxEVT_NULL, m_windowId); + wxEventType eventType = wxEVT_NULL; + NMHDR *hdr = (NMHDR *)lParam; - switch ( hdr->action ) - { - case TVE_EXPAND: - eventType = wxEVT_COMMAND_TREE_ITEM_EXPANDING; - break; + switch ( hdr->code ) + { + case TVN_BEGINDRAG: + eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG; + // fall through - case TVE_COLLAPSE: - eventType = wxEVT_COMMAND_TREE_ITEM_COLLAPSING; - break; + case TVN_BEGINRDRAG: + { + if ( eventType == wxEVT_NULL ) + eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG; + //else: left drag, already set above - case TVE_COLLAPSERESET: - case TVE_TOGGLE: - wxLogDebug("unexpected code in TVN_ITEMEXPANDING message"); - break; - } - break; + NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; + + event.m_item = tv->itemNew.hItem; + event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y); + break; + } + + case TVN_BEGINLABELEDIT: + { + eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT; + TV_DISPINFO *info = (TV_DISPINFO *)lParam; + + event.m_item = info->item.hItem; + break; + } + + case TVN_DELETEITEM: + { + eventType = wxEVT_COMMAND_TREE_DELETE_ITEM; + NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; + + event.m_item = tv->itemOld.hItem; + break; + } + + case TVN_ENDLABELEDIT: + { + eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT; + TV_DISPINFO *info = (TV_DISPINFO *)lParam; + + event.m_item = info->item.hItem; + break; + } + + case TVN_GETDISPINFO: + eventType = wxEVT_COMMAND_TREE_GET_INFO; + // fall through + + case TVN_SETDISPINFO: + { + if ( eventType == wxEVT_NULL ) + eventType = wxEVT_COMMAND_TREE_SET_INFO; + //else: get, already set above + + TV_DISPINFO *info = (TV_DISPINFO *)lParam; + + event.m_item = info->item.hItem; + break; + } + + case TVN_ITEMEXPANDING: + event.m_code = FALSE; + // fall through + + case TVN_ITEMEXPANDED: + { + NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam; + + bool expand = FALSE; + switch ( tv->action ) + { + case TVE_EXPAND: + expand = TRUE; + break; + + case TVE_COLLAPSE: + expand = FALSE; + break; + + default: + wxLogDebug("unexpected code %d in TVN_ITEMEXPAND " + "message", tv->action); + } + + bool ing = hdr->code == TVN_ITEMEXPANDING; + eventType = g_events[expand][ing]; + + event.m_item = tv->itemNew.hItem; + break; + } + + case TVN_KEYDOWN: + { + eventType = wxEVT_COMMAND_TREE_KEY_DOWN; + TV_KEYDOWN *info = (TV_KEYDOWN *)lParam; + + event.m_code = wxCharCodeMSWToWX(info->wVKey); + break; + } + + case TVN_SELCHANGED: + eventType = wxEVT_COMMAND_TREE_SEL_CHANGED; + // fall through + + case TVN_SELCHANGING: + { + if ( eventType == wxEVT_NULL ) + eventType = wxEVT_COMMAND_TREE_SEL_CHANGING; + //else: already set above + + NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam; + + event.m_item = tv->itemNew.hItem; + event.m_itemOld = tv->itemOld.hItem; + break; + } + + default: + return wxControl::MSWNotify(wParam, lParam); } - case TVN_ITEMEXPANDED: - { - NM_TREEVIEW* hdr = (NM_TREEVIEW*)lParam; - wxConvertFromMSWTreeItem(event.m_item, hdr->itemNew, (HWND) GetHWND()); - switch ( hdr->action ) - { - case TVE_EXPAND: - eventType = wxEVT_COMMAND_TREE_ITEM_EXPANDED; - break; + event.SetEventObject(this); + event.SetEventType(eventType); - case TVE_COLLAPSE: - eventType = wxEVT_COMMAND_TREE_ITEM_COLLAPSED; - break; + bool rc = GetEventHandler()->ProcessEvent(event); - case TVE_COLLAPSERESET: - case TVE_TOGGLE: - wxLogDebug("unexpected code in TVN_ITEMEXPANDED message"); - break; - } - break; - } - case TVN_KEYDOWN: + // post processing + switch ( hdr->code ) { - eventType = wxEVT_COMMAND_TREE_KEY_DOWN; - TV_KEYDOWN *info = (TV_KEYDOWN *)lParam; - event.m_code = wxCharCodeMSWToWX(info->wVKey); - break; - } - case TVN_SELCHANGED: - { - eventType = wxEVT_COMMAND_TREE_SEL_CHANGED; - NM_TREEVIEW* hdr = (NM_TREEVIEW*)lParam; - wxConvertFromMSWTreeItem(event.m_item, hdr->itemNew, (HWND) GetHWND()); - event.m_oldItem = (long) hdr->itemNew.hItem; + // NB: we might process this message using wxWindows event tables, but + // due to overhead of wxWin event system we prefer to do it here + // (otherwise deleting a tree with many items is just too slow) + case TVN_DELETEITEM: + { + NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam; + wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam; + delete data; // may be NULL, ok + } + break; - break; - } - case TVN_SELCHANGING: - { - eventType = wxEVT_COMMAND_TREE_SEL_CHANGING; - NM_TREEVIEW* hdr = (NM_TREEVIEW*)lParam; - wxConvertFromMSWTreeItem(event.m_item, hdr->itemNew, (HWND) GetHWND()); - event.m_oldItem = (long) hdr->itemNew.hItem; - break; - } - case TVN_SETDISPINFO: - { - eventType = wxEVT_COMMAND_TREE_SET_INFO; - TV_DISPINFO *info = (TV_DISPINFO *)lParam; - wxConvertFromMSWTreeItem(event.m_item, info->item, (HWND) GetHWND()); - break; + case TVN_ITEMEXPANDING: + // if user called Veto(), don't allow expansion/collapse by + // returning TRUE from here + rc = event.m_code != 0; + break; } - default : - return wxControl::MSWNotify(wParam, lParam); - break; - } - - event.SetEventObject(this); - event.SetEventType(eventType); - - if ( !GetEventHandler()->ProcessEvent(event) ) - return FALSE; - - if (hdr1->code == TVN_GETDISPINFO) - { - TV_DISPINFO *info = (TV_DISPINFO *)lParam; - wxConvertToMSWTreeItem(event.m_item, info->item); - } - - return TRUE; -} - -// Tree item structure -wxTreeItem::wxTreeItem(void) -{ - m_mask = 0; - m_itemId = 0; - m_state = 0; - m_stateMask = 0; - m_image = -1; - m_selectedImage = -1; - m_children = 0; - m_data = 0; -} - -// If getFullInfo is TRUE, we explicitly query for more info if we haven't got it all. -static void wxConvertFromMSWTreeItem(wxTreeItem& info, TV_ITEM& tvItem, HWND getFullInfo) -{ - info.m_data = tvItem.lParam; - info.m_mask = 0; - info.m_state = 0; - info.m_stateMask = 0; - - long oldMask = tvItem.mask; - - bool needText = FALSE; - if (getFullInfo != 0) - { - if ( tvItem.mask & TVIF_TEXT ) - needText = FALSE; - else - needText = TRUE; - - if ( needText ) - { - tvItem.pszText = new char[513]; - tvItem.cchTextMax = 512; - } - tvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM ; - ::SendMessage(getFullInfo, TVM_GETITEM, 0, (LPARAM)& tvItem) ; - } - - if ( tvItem.mask & TVIF_HANDLE ) - { - info.m_mask |= wxTREE_MASK_HANDLE; - info.m_itemId = (long) tvItem.hItem; - } - if ( tvItem.mask & TVIF_STATE ) - { - info.m_mask |= wxTREE_MASK_STATE; - if ( tvItem.stateMask & TVIS_BOLD) - { - info.m_stateMask |= wxTREE_STATE_BOLD ; - if ( tvItem.state & TVIS_BOLD ) - info.m_state |= wxTREE_STATE_BOLD ; - } - - if ( tvItem.stateMask & TVIS_CUT) - { - info.m_stateMask |= wxTREE_STATE_CUT ; - if ( tvItem.state & TVIS_CUT ) - info.m_state |= wxTREE_STATE_CUT ; - } - - if ( tvItem.stateMask & TVIS_DROPHILITED) - { - info.m_stateMask |= wxTREE_STATE_DROPHILITED ; - if ( tvItem.state & TVIS_DROPHILITED ) - info.m_state |= wxTREE_STATE_DROPHILITED ; - } - if ( tvItem.stateMask & TVIS_EXPANDED) - { - info.m_stateMask |= wxTREE_STATE_EXPANDED ; - if ( tvItem.state & TVIS_EXPANDED ) - info.m_state |= wxTREE_STATE_EXPANDED ; - } - if ( tvItem.stateMask & TVIS_EXPANDEDONCE) - { - info.m_stateMask |= wxTREE_STATE_EXPANDEDONCE ; - if ( tvItem.state & TVIS_EXPANDEDONCE ) - info.m_state |= wxTREE_STATE_EXPANDEDONCE ; - } - if ( tvItem.stateMask & TVIS_FOCUSED) - { - info.m_stateMask |= wxTREE_STATE_FOCUSED ; - if ( tvItem.state & TVIS_FOCUSED ) - info.m_state |= wxTREE_STATE_FOCUSED ; - } - if ( tvItem.stateMask & TVIS_SELECTED) - { - info.m_stateMask |= wxTREE_STATE_SELECTED ; - if ( tvItem.state & TVIS_SELECTED ) - info.m_state |= wxTREE_STATE_SELECTED ; - } - } - - if ( tvItem.mask & TVIF_TEXT ) - { - info.m_mask |= wxTREE_MASK_TEXT; - info.m_text = tvItem.pszText; - } - if ( tvItem.mask & TVIF_IMAGE ) - { - info.m_mask |= wxTREE_MASK_IMAGE; - info.m_image = tvItem.iImage; - } - if ( tvItem.mask & TVIF_SELECTEDIMAGE ) - { - info.m_mask |= wxTREE_MASK_SELECTED_IMAGE; - info.m_selectedImage = tvItem.iSelectedImage; - } - if ( tvItem.mask & TVIF_CHILDREN ) - { - info.m_mask |= wxTREE_MASK_CHILDREN; - info.m_children = tvItem.cChildren; - } - if ( tvItem.mask & TVIF_PARAM ) - info.m_mask |= wxTREE_MASK_DATA; - - if (needText) - { - if (tvItem.pszText) - delete[] tvItem.pszText; - } - tvItem.mask = oldMask ; -} - -static void wxConvertToMSWTreeItem(wxTreeItem& info, TV_ITEM& tvItem) -{ - tvItem.hItem = (HTREEITEM) info.m_itemId ; - - tvItem.iImage = info.m_image ; - tvItem.iSelectedImage = info.m_selectedImage; - tvItem.cChildren = info.m_children; - tvItem.lParam = info.m_data; - tvItem.mask = 0; - tvItem.stateMask = 0; - tvItem.state = 0; - - if (info.m_mask & wxTREE_MASK_HANDLE) - tvItem.mask |= TVIF_HANDLE ; - if (info.m_mask & wxTREE_MASK_STATE) - tvItem.mask |= TVIF_STATE ; - if (info.m_mask & wxTREE_MASK_TEXT) - { - tvItem.mask |= TVIF_TEXT ; - tvItem.pszText = (char *) (const char *)info.m_text ; - if ( tvItem.pszText ) - tvItem.cchTextMax = info.m_text.Length(); - else - tvItem.cchTextMax = 0; - } - if (info.m_mask & wxTREE_MASK_IMAGE) - tvItem.mask |= TVIF_IMAGE ; - if (info.m_mask & wxTREE_MASK_SELECTED_IMAGE) - tvItem.mask |= TVIF_SELECTEDIMAGE ; - if (info.m_mask & wxTREE_MASK_CHILDREN) - tvItem.mask |= TVIF_CHILDREN ; - if (info.m_mask & wxTREE_MASK_DATA) - tvItem.mask |= TVIF_PARAM ; - - if (info.m_stateMask & wxTREE_STATE_BOLD) - { - tvItem.stateMask |= TVIS_BOLD ; - tvItem.state |= TVIS_BOLD; - } - if (info.m_stateMask & wxTREE_STATE_CUT) - { - tvItem.stateMask |= TVIS_CUT ; - if ( info.m_state & wxTREE_STATE_CUT ) - tvItem.state |= TVIS_CUT; - } - if (info.m_stateMask & wxTREE_STATE_DROPHILITED) - { - tvItem.stateMask |= TVIS_DROPHILITED; - if ( info.m_state & wxTREE_STATE_DROPHILITED ) - tvItem.state |= TVIS_DROPHILITED; - } - if (info.m_stateMask & wxTREE_STATE_EXPANDED) - { - tvItem.stateMask |= TVIS_EXPANDED; - if ( info.m_state & wxTREE_STATE_EXPANDED ) - tvItem.state |= TVIS_EXPANDED; - } - if (info.m_stateMask & wxTREE_STATE_EXPANDEDONCE) - { - tvItem.stateMask |= TVIS_EXPANDEDONCE; - if ( info.m_state & wxTREE_STATE_EXPANDEDONCE ) - tvItem.state |= TVIS_EXPANDEDONCE; - } - if (info.m_stateMask & wxTREE_STATE_FOCUSED) - { - tvItem.stateMask |= TVIS_FOCUSED; - if ( info.m_state & wxTREE_STATE_FOCUSED ) - tvItem.state |= TVIS_FOCUSED; - } - if (info.m_stateMask & wxTREE_STATE_SELECTED) - { - tvItem.stateMask |= TVIS_SELECTED; - if ( info.m_state & wxTREE_STATE_SELECTED ) - tvItem.state |= TVIS_SELECTED; - } + return rc; } +// ---------------------------------------------------------------------------- // Tree event +// ---------------------------------------------------------------------------- + IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxCommandEvent) -wxTreeEvent::wxTreeEvent(wxEventType commandType, int id): - wxCommandEvent(commandType, id) +wxTreeEvent::wxTreeEvent(wxEventType commandType, int id) + : wxCommandEvent(commandType, id) { - m_code = 0; - m_oldItem = 0; + m_code = 0; + m_itemOld = 0; } -#endif +#endif // __WIN95__