X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/d50fc4dc6d4832d4678c6e99085198cafaeabb9a..8e77fd8bca165aab9709649d79a7cbc6a172d4e1:/include/wx/treelist.h diff --git a/include/wx/treelist.h b/include/wx/treelist.h index 1016e262c1..3fa1cdaabe 100644 --- a/include/wx/treelist.h +++ b/include/wx/treelist.h @@ -3,7 +3,7 @@ // Purpose: wxTreeListCtrl class declaration. // Author: Vadim Zeitlin // Created: 2011-08-17 -// RCS-ID: $Id: wxhead.h,v 1.12 2010-04-22 12:44:51 zeitlin Exp $ +// RCS-ID: $Id$ // Copyright: (c) 2011 Vadim Zeitlin // Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// @@ -15,6 +15,8 @@ #if wxUSE_TREELISTCTRL +#include "wx/compositewin.h" +#include "wx/containr.h" #include "wx/headercol.h" #include "wx/itemid.h" #include "wx/vector.h" @@ -24,8 +26,9 @@ class WXDLLIMPEXP_FWD_ADV wxDataViewCtrl; class WXDLLIMPEXP_FWD_ADV wxDataViewEvent; -extern WXDLLIMPEXP_DATA_CORE(const char) wxTreeListCtrlNameStr[]; +extern WXDLLIMPEXP_DATA_ADV(const char) wxTreeListCtrlNameStr[]; +class wxTreeListCtrl; class wxTreeListModel; class wxTreeListModelNode; @@ -44,6 +47,7 @@ enum wxTL_CHECKBOX = 0x0002, // Show checkboxes in the first column. wxTL_3STATE = 0x0004, // Allow 3rd state in checkboxes. wxTL_USER_3STATE = 0x0008, // Allow user to set 3rd state. + wxTL_NO_HEADER = 0x0010, // Column titles not visible. wxTL_DEFAULT_STYLE = wxTL_SINGLE, wxTL_STYLE_MASK = wxTL_SINGLE | @@ -75,6 +79,33 @@ typedef wxVector wxTreeListItems; extern WXDLLIMPEXP_DATA_ADV(const wxTreeListItem) wxTLI_FIRST; extern WXDLLIMPEXP_DATA_ADV(const wxTreeListItem) wxTLI_LAST; +// ---------------------------------------------------------------------------- +// wxTreeListItemComparator: defines order of wxTreeListCtrl items. +// ---------------------------------------------------------------------------- + +class wxTreeListItemComparator +{ +public: + wxTreeListItemComparator() { } + + // The comparison function should return negative, null or positive value + // depending on whether the first item is less than, equal to or greater + // than the second one. The items should be compared using their values for + // the given column. + virtual int + Compare(wxTreeListCtrl* treelist, + unsigned column, + wxTreeListItem first, + wxTreeListItem second) = 0; + + // Although this class is not used polymorphically by wxWidgets itself, + // provide virtual dtor in case it's used like this in the user code. + virtual ~wxTreeListItemComparator() { } + +private: + wxDECLARE_NO_COPY_CLASS(wxTreeListItemComparator); +}; + // ---------------------------------------------------------------------------- // wxTreeListCtrl: a control combining wxTree- and wxListCtrl features. // ---------------------------------------------------------------------------- @@ -87,8 +118,9 @@ extern WXDLLIMPEXP_DATA_ADV(const wxTreeListItem) wxTLI_LAST; // with wxDataViewCtrl directly but doing this makes your unportable to possible // future non-wxDataViewCtrl-based implementations of this class. -class WXDLLIMPEXP_ADV wxTreeListCtrl : public wxWindow, - public wxWithImages +class WXDLLIMPEXP_ADV wxTreeListCtrl + : public wxCompositeWindow< wxNavigationEnabled >, + public wxWithImages { public: // Constructors and such @@ -319,10 +351,53 @@ public: wxCheckBoxState state) const; + + // Sorting. + // -------- + + // Sort by the given column, either in ascending (default) or descending + // sort order. + // + // By default, simple alphabetical sorting is done by this column contents + // but SetItemComparator() may be called to perform comparison in some + // other way. + void SetSortColumn(unsigned col, bool ascendingOrder = true); + + // If the control contents is sorted, return true and fill the output + // parameters with the column which is currently used for sorting and + // whether we sort using ascending or descending order. Otherwise, i.e. if + // the control contents is unsorted, simply return false. + bool GetSortColumn(unsigned* col, bool* ascendingOrder = NULL); + + // Set the object to use for comparing the items. It will be called when + // the control is being sorted because the user clicked on a sortable + // column. + // + // The provided pointer is stored by the control so the object it points to + // must have a life-time equal or greater to that of the control itself. In + // addition, the pointer can be NULL to stop using custom comparator and + // revert to the default alphabetical comparison. + void SetItemComparator(wxTreeListItemComparator* comparator); + + + // View window functions. + // ---------------------- + + // This control itself is entirely covered by the "view window" which is + // currently a wxDataViewCtrl but if you want to avoid relying on this to + // allow your code to work with later versions which might not be + // wxDataViewCtrl-based, use the first function only and only use the + // second one if you really need to call wxDataViewCtrl methods on it. + wxWindow* GetView() const; + wxDataViewCtrl* GetDataView() const { return m_view; } + private: // Common part of all ctors. void Init(); + // Pure virtual method inherited from wxCompositeWindow. + virtual wxWindowList GetCompositeWindowParts() const; + // Implementation of AppendColumn(). int DoInsertColumn(const wxString& title, int pos, // May be -1 meaning "append". @@ -338,10 +413,14 @@ private: int imageOpened, wxClientData* data); - // Send wxTreeListEvent corresponding to the given wxDataViewEvent. + // Send wxTreeListEvent corresponding to the given wxDataViewEvent for an + // item (as opposed for column-oriented events). // // Also updates the original event "skipped" and "vetoed" flags. - void SendEvent(wxEventType evt, wxDataViewEvent& event); + void SendItemEvent(wxEventType evt, wxDataViewEvent& event); + + // Send wxTreeListEvent corresponding to the given column wxDataViewEvent. + void SendColumnEvent(wxEventType evt, wxDataViewEvent& event); // Called by wxTreeListModel when an item is toggled by the user. @@ -353,6 +432,7 @@ private: void OnItemExpanded(wxDataViewEvent& event); void OnItemActivated(wxDataViewEvent& event); void OnItemContextMenu(wxDataViewEvent& event); + void OnColumnSorted(wxDataViewEvent& event); void OnSize(wxSizeEvent& event); wxDECLARE_EVENT_TABLE(); @@ -361,6 +441,8 @@ private: wxDataViewCtrl* m_view; wxTreeListModel* m_model; + wxTreeListItemComparator* m_comparator; + // It calls our inherited protected wxWithImages::GetImage() method. friend class wxTreeListModel; @@ -372,19 +454,34 @@ private: // wxTreeListEvent: event generated by wxTreeListCtrl. // ---------------------------------------------------------------------------- -class wxTreeListEvent : public wxNotifyEvent +class WXDLLIMPEXP_ADV wxTreeListEvent : public wxNotifyEvent { public: - // The item affected by the event. + // Default ctor is provided for wxRTTI needs only but should never be used. + wxTreeListEvent() { Init(); } + + // The item affected by the event. Valid for all events except + // column-specific ones such as COLUMN_SORTED. wxTreeListItem GetItem() const { return m_item; } // The previous state of the item checkbox for ITEM_CHECKED events only. wxCheckBoxState GetOldCheckedState() const { return m_oldCheckedState; } + // The index of the column affected by the event. Currently only used by + // COLUMN_SORTED event. + unsigned GetColumn() const { return m_column; } virtual wxEvent* Clone() const { return new wxTreeListEvent(*this); } private: + // Common part of all ctors. + void Init() + { + m_column = static_cast(-1); + + m_oldCheckedState = wxCHK_UNDETERMINED; + } + // Ctor is private, only wxTreeListCtrl can create events of this type. wxTreeListEvent(wxEventType evtType, wxTreeListCtrl* treelist, @@ -393,6 +490,8 @@ private: m_item(item) { SetEventObject(treelist); + + Init(); } // Set the checkbox state before this event for ITEM_CHECKED events. @@ -401,14 +500,22 @@ private: m_oldCheckedState = state; } + // Set the column affected by this event for COLUMN_SORTED events. + void SetColumn(unsigned column) + { + m_column = column; + } + const wxTreeListItem m_item; wxCheckBoxState m_oldCheckedState; + unsigned m_column; + friend class wxTreeListCtrl; - wxDECLARE_ABSTRACT_CLASS(wxTreeListEvent); + wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTreeListEvent); }; // Event types and event table macros. @@ -419,11 +526,11 @@ typedef void (wxEvtHandler::*wxTreeListEventFunction)(wxTreeListEvent&); wxEVENT_HANDLER_CAST(wxTreeListEventFunction, func) #define wxEVT_TREELIST_GENERIC(name, id, fn) \ - wx__DECLARE_EVT1(wxEVT_COMMAND_TREELIST_##name, id, wxTreeListEventHandler(fn)) + wx__DECLARE_EVT1(wxEVT_TREELIST_##name, id, wxTreeListEventHandler(fn)) #define wxDECLARE_TREELIST_EVENT(name) \ wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, \ - wxEVT_COMMAND_TREELIST_##name, \ + wxEVT_TREELIST_##name, \ wxTreeListEvent) wxDECLARE_TREELIST_EVENT(SELECTION_CHANGED); @@ -450,8 +557,21 @@ wxDECLARE_TREELIST_EVENT(ITEM_CONTEXT_MENU); #define EVT_TREELIST_ITEM_CONTEXT_MENU(id, fn) \ wxEVT_TREELIST_GENERIC(ITEM_CONTEXT_MENU, id, fn) +wxDECLARE_TREELIST_EVENT(COLUMN_SORTED); +#define EVT_TREELIST_COLUMN_SORTED(id, fn) \ + wxEVT_TREELIST_GENERIC(COLUMN_SORTED, id, fn) + #undef wxDECLARE_TREELIST_EVENT +// old wxEVT_COMMAND_* constants +#define wxEVT_COMMAND_TREELIST_SELECTION_CHANGED wxEVT_TREELIST_SELECTION_CHANGED +#define wxEVT_COMMAND_TREELIST_ITEM_EXPANDING wxEVT_TREELIST_ITEM_EXPANDING +#define wxEVT_COMMAND_TREELIST_ITEM_EXPANDED wxEVT_TREELIST_ITEM_EXPANDED +#define wxEVT_COMMAND_TREELIST_ITEM_CHECKED wxEVT_TREELIST_ITEM_CHECKED +#define wxEVT_COMMAND_TREELIST_ITEM_ACTIVATED wxEVT_TREELIST_ITEM_ACTIVATED +#define wxEVT_COMMAND_TREELIST_ITEM_CONTEXT_MENU wxEVT_TREELIST_ITEM_CONTEXT_MENU +#define wxEVT_COMMAND_TREELIST_COLUMN_SORTED wxEVT_TREELIST_COLUMN_SORTED + #endif // wxUSE_TREELISTCTRL #endif // _WX_TREELIST_H_