1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        treelistctrl.h
3 // Purpose:     wxTreeListCtrl class
4 // Created:     01/02/97
5 // Author:      Robert Roebling
6 // Maintainer:  Ronan Chartois (pgriddev)
7 // Version:     $Id: treelistctrl.h 10771 2016-02-06 14:29:31Z mortenmacfly $
8 // Copyright:   (c) 2004-2011 Robert Roebling, Julian Smart, Alberto Griggio,
9 //              Vadim Zeitlin, Otto Wyss, Ronan Chartois
10 // Licence:     wxWindows
11 /////////////////////////////////////////////////////////////////////////////
12 
13 
14 #ifndef TREELISTCTRL_H
15 #define TREELISTCTRL_H
16 
17 #if defined(__GNUG__) && !defined(__APPLE__)
18     #pragma interface "treelistctrl.h"
19 #endif
20 
21 #include <wx/treectrl.h>
22 #include <wx/control.h>
23 #include <wx/pen.h>
24 #include <wx/listctrl.h> // for wxListEvent
25 
26 class WXDLLEXPORT wxTreeListItem;
27 class WXDLLEXPORT wxTreeListHeaderWindow;
28 class WXDLLEXPORT wxTreeListMainWindow;
29 
30 #define wxTR_COLUMN_LINES 0x1000 // put border around items
31 #define wxTR_VIRTUAL      0x4000 // The application provides items text on demand.
32 
33 // Using this typedef removes an ambiguity when calling Remove()
34 #ifdef __WXMSW__
35 typedef void *wxTreeItemIdValue;
36 #endif
37 
38 //-----------------------------------------------------------------------------
39 // wxTreeListColumnAttrs
40 //-----------------------------------------------------------------------------
41 
42 enum {
43     DEFAULT_COL_WIDTH = 100
44 };
45 
46 class WXDLLEXPORT wxTreeListColumnInfo: public wxObject {
47 
48 public:
49     wxTreeListColumnInfo (const wxString &text = wxEmptyString,
50                           int width = DEFAULT_COL_WIDTH,
51                           int flag = wxALIGN_LEFT,
52                           int image = -1,
53                           bool shown = true,
54                           bool edit = false) {
55         m_text = text;
56         m_width = width;
57         m_flag = flag;
58         m_image = image;
59         m_selected_image = -1;
60         m_shown = shown;
61         m_edit = edit;
62     }
63 
wxTreeListColumnInfo(const wxTreeListColumnInfo & other)64     wxTreeListColumnInfo (const wxTreeListColumnInfo& other) {
65         m_text = other.m_text;
66         m_width = other.m_width;
67         m_flag = other.m_flag;
68         m_image = other.m_image;
69         m_selected_image = other.m_selected_image;
70         m_shown = other.m_shown;
71         m_edit = other.m_edit;
72     }
73 
~wxTreeListColumnInfo()74     ~wxTreeListColumnInfo() {}
75 
76     // get/set
GetText()77     wxString GetText() const { return m_text; }
SetText(const wxString & text)78     wxTreeListColumnInfo& SetText (const wxString& text) { m_text = text; return *this; }
79 
GetWidth()80     int GetWidth() const { return m_width; }
SetWidth(int width)81     wxTreeListColumnInfo& SetWidth (int width) { m_width = width; return *this; }
82 
GetAlignment()83     int GetAlignment() const { return m_flag; }
SetAlignment(int flag)84     wxTreeListColumnInfo& SetAlignment (int flag) { m_flag = flag; return *this; }
85 
GetImage()86     int GetImage() const { return m_image; }
SetImage(int image)87     wxTreeListColumnInfo& SetImage (int image) { m_image = image; return *this; }
88 
GetSelectedImage()89     int GetSelectedImage() const { return m_selected_image; }
SetSelectedImage(int image)90     wxTreeListColumnInfo& SetSelectedImage (int image) { m_selected_image = image; return *this; }
91 
IsEditable()92     bool IsEditable() const { return m_edit; }
SetEditable(bool edit)93     wxTreeListColumnInfo& SetEditable (bool edit)
94         { m_edit = edit; return *this; }
95 
IsShown()96     bool IsShown() const { return m_shown; }
SetShown(bool shown)97     wxTreeListColumnInfo& SetShown(bool shown) { m_shown = shown; return *this; }
98 
99 private:
100     wxString m_text;
101     int m_width;
102     int m_flag;
103     int m_image;
104     int m_selected_image;
105     bool m_shown;
106     bool m_edit;
107 };
108 
109 //----------------------------------------------------------------------------
110 // wxTreeListCtrl - the multicolumn tree control
111 //----------------------------------------------------------------------------
112 
113 // modes for navigation
114 const int wxTL_MODE_NAV_FULLTREE = 0x0000; // default
115 const int wxTL_MODE_NAV_EXPANDED = 0x0001;
116 const int wxTL_MODE_NAV_VISIBLE  = 0x0002;
117 const int wxTL_MODE_NAV_LEVEL    = 0x0004;
118 
119 // modes for FindItem
120 const int wxTL_MODE_FIND_EXACT   = 0x0000; // default
121 const int wxTL_MODE_FIND_PARTIAL = 0x0010;
122 const int wxTL_MODE_FIND_NOCASE  = 0x0020;
123 
124 // additional flag for HitTest
125 const int wxTREE_HITTEST_ONITEMCOLUMN = 0x2000;
126 extern WXDLLEXPORT const wxChar* wxTreeListCtrlNameStr;
127 
128 
129 class WXDLLEXPORT wxTreeListCtrl : public wxControl
130 {
131 friend class wxTreeListHeaderWindow;
132 friend class wxTreeListMainWindow;
133 friend class wxTreeListItem;
134 public:
135 
136     // ---------- creation ----------
137 
wxTreeListCtrl()138     wxTreeListCtrl()
139         : m_header_win(0), m_main_win(0), m_headerHeight(0)
140     {}
141 
142     wxTreeListCtrl(wxWindow *parent, wxWindowID id = -1,
143                const wxPoint& pos = wxDefaultPosition,
144                const wxSize& size = wxDefaultSize,
145                long style = wxTR_DEFAULT_STYLE,
146                const wxValidator &validator = wxDefaultValidator,
147                const wxString& name = wxTreeListCtrlNameStr )
148         : m_header_win(0), m_main_win(0), m_headerHeight(0)
149     {
150         Create(parent, id, pos, size, style, validator, name);
151     }
152 
~wxTreeListCtrl()153     virtual ~wxTreeListCtrl() {}
154 
155     bool Create(wxWindow *parent, wxWindowID id = -1,
156                 const wxPoint& pos = wxDefaultPosition,
157                 const wxSize& size = wxDefaultSize,
158                 long style = wxTR_DEFAULT_STYLE,
159                 const wxValidator &validator = wxDefaultValidator,
160                 const wxString& name = wxTreeListCtrlNameStr );
161 
162     void Refresh(bool erase=TRUE, const wxRect* rect=NULL);
163     void SetFocus();
164 
165 
166     // ---------- general methods ----------
167 
168     // get the total number of items in the control
169     size_t GetCount() const;
170 
171     // indent is the number of pixels the children are indented relative to
172     // the parents position. SetIndent() also redraws the control
173     // immediately.
174     unsigned int GetIndent() const;
175     void SetIndent(unsigned int indent);
176 
177     // line spacing is the space above and below the text on each line
178     unsigned int GetLineSpacing() const;
179     void SetLineSpacing(unsigned int spacing);
180 
181     // image list: these functions allow to associate an image list with
182     // the control and retrieve it. Note that when assigned with
183     // SetImageList, the control does _not_ delete
184     // the associated image list when it's deleted in order to allow image
185     // lists to be shared between different controls. If you use
186     // AssignImageList, the control _does_ delete the image list.
187     //
188     // The normal image list is for the icons which correspond to the
189     // normal tree item state (whether it is selected or not).
190     // Additionally, the application might choose to show a state icon
191     // which corresponds to an app-defined item state (for example,
192     // checked/unchecked) which are taken from the state image list.
193     wxImageList *GetImageList() const;
194     wxImageList *GetStateImageList() const;
195     wxImageList *GetButtonsImageList() const;
196 
197     void SetImageList(wxImageList *imageList);
198     void SetStateImageList(wxImageList *imageList);
199     void SetButtonsImageList(wxImageList *imageList);
200     void AssignImageList(wxImageList *imageList);
201     void AssignStateImageList(wxImageList *imageList);
202     void AssignButtonsImageList(wxImageList *imageList);
203 
204     void SetToolTip(const wxString& tip);
205     void SetToolTip (wxToolTip *tip);
206     void SetItemToolTip(const wxTreeItemId& item, const wxString &tip);
207 
208     // ---------- Functions to work with columns ----------
209 
210     // adds a column
211     void AddColumn (const wxString& text,
212                     int width = DEFAULT_COL_WIDTH,
213                     int flag = wxALIGN_LEFT,
214                     int image = -1,
215                     bool shown = true,
216                     bool edit = false) {
217         AddColumn (wxTreeListColumnInfo (text, width, flag, image, shown, edit));
218     }
219     void AddColumn (const wxTreeListColumnInfo& colInfo);
220 
221     // inserts a column before the given one
222     void InsertColumn (int before,
223                        const wxString& text,
224                        int width = DEFAULT_COL_WIDTH,
225                        int flag = wxALIGN_LEFT,
226                        int image = -1,
227                        bool shown = true,
228                        bool edit = false) {
229         InsertColumn (before,
230                       wxTreeListColumnInfo (text, width, flag, image, shown, edit));
231     }
232     void InsertColumn (int before, const wxTreeListColumnInfo& colInfo);
233 
234     // deletes the given column - does not delete the corresponding column
235     void RemoveColumn (int column);
236 
237     // returns the number of columns in the ctrl
238     int GetColumnCount() const;
239 
240     // tells which column is the "main" one, i.e. the "threaded" one
241     void SetMainColumn (int column);
242     int GetMainColumn() const;
243 
244     void SetColumn (int column, const wxTreeListColumnInfo& colInfo);
245     wxTreeListColumnInfo& GetColumn (int column);
246     const wxTreeListColumnInfo& GetColumn (int column) const;
247 
248     void SetColumnText (int column, const wxString& text);
249     wxString GetColumnText (int column) const;
250 
251     void SetColumnWidth (int column, int width);
252     int GetColumnWidth (int column) const;
253 
254     void SetColumnAlignment (int column, int flag);
255     int GetColumnAlignment (int column) const;
256 
257     void SetColumnImage (int column, int image);
258     int GetColumnImage (int column) const;
259 
260     void SetColumnShown (int column, bool shown = true);
261     bool IsColumnShown (int column) const;
262 
263     void SetColumnEditable (int column, bool edit = true);
264     bool IsColumnEditable (int column) const;
265 
266     // ----------  Functions to work with items. ----------
267 
268     // accessors (most properties have a default at row/item level)
269     // ---------
270 
GetItemText(const wxTreeItemId & item)271     wxString GetItemText (const wxTreeItemId& item)             const { return GetItemText (item, GetMainColumn()); };
272     wxString GetItemText (const wxTreeItemId& item, int column) const;
273 
274     int GetItemImage (const wxTreeItemId& item, wxTreeItemIcon which = wxTreeItemIcon_Normal) const;
275     int GetItemImage (const wxTreeItemId& item, int column) const;
276 
277     wxTreeItemData *GetItemData (const wxTreeItemId& item)             const;
278     wxTreeItemData *GetItemData (const wxTreeItemId& item, int column) const;
279 
280     bool GetItemBold (const wxTreeItemId& item)             const;
281     bool GetItemBold (const wxTreeItemId& item, int column) const;
282 
283     wxColour GetItemTextColour (const wxTreeItemId& item)             const;
284     wxColour GetItemTextColour (const wxTreeItemId& item, int column) const;
285 
286     wxColour GetItemBackgroundColour (const wxTreeItemId& item)             const;
287     wxColour GetItemBackgroundColour (const wxTreeItemId& item, int column) const;
288 
289     wxFont GetItemFont (const wxTreeItemId& item)             const;
290     wxFont GetItemFont (const wxTreeItemId& item, int column) const;
291 
292     // modifiers (most properties have a default at row/item level)
293     // ---------
294 
295     void SetItemText (const wxTreeItemId& item,             const wxString& text);
296     void SetItemText (const wxTreeItemId& item, int column, const wxString& text);
297 
298     // the which parameter is ignored for all columns but the main one
299     void SetItemImage (const wxTreeItemId& item, int image, wxTreeItemIcon which = wxTreeItemIcon_Normal);
300     void SetItemImage (const wxTreeItemId& item, int column, int image);
301 
302     void SetItemData (const wxTreeItemId& item,             wxTreeItemData *data);
303     void SetItemData (const wxTreeItemId& item, int column, wxTreeItemData *data);
304 
305     void SetItemBold (const wxTreeItemId& item,             bool bold = true);
306     void SetItemBold (const wxTreeItemId& item, int column, bool bold = true);
307 
308     void SetItemTextColour (const wxTreeItemId& item,             const wxColour& colour);
309     void SetItemTextColour (const wxTreeItemId& item, int column, const wxColour& colour);
310 
311     void SetItemBackgroundColour (const wxTreeItemId& item,             const wxColour& colour);
312     void SetItemBackgroundColour (const wxTreeItemId& item, int column, const wxColour& colour);
313 
314     // font should be of the same height for all items
315     void SetItemFont (const wxTreeItemId& item,             const wxFont& font);
316     void SetItemFont (const wxTreeItemId& item, int column, const wxFont& font);
317 
318     // force appearance of [+] button near the item. This is useful to
319     // allow the user to expand the items which don't have any children now
320     // - but instead add them only when needed, thus minimizing memory
321     // usage and loading time.
322     void SetItemHasChildren(const wxTreeItemId& item, bool has = true);
323 
324     // item status inquiries
325     // ---------------------
326 
327     // is the item visible (it might be outside the view or not expanded)?
328     bool IsVisible (const wxTreeItemId& item, bool fullRow = false, bool within = true) const;
329     // does the item has any children?
330     bool HasChildren (const wxTreeItemId& item) const;
331     // is the item expanded (only makes sense if HasChildren())?
332     bool IsExpanded (const wxTreeItemId& item) const;
333     // is this item currently selected (the same as has focus)?
334     bool IsSelected (const wxTreeItemId& item) const;
335     // is item text in bold font?
IsBold(const wxTreeItemId & item)336     bool IsBold (const wxTreeItemId& item)             const { return IsBold(item, GetMainColumn()); };
IsBold(const wxTreeItemId & item,int column)337     bool IsBold (const wxTreeItemId& item, int column) const { return GetItemBold(item, column); };
338     // does the layout include space for a button?
339 
340 
341     // set the window font
342     virtual bool SetFont ( const wxFont &font );
343 
344     // set the styles.
345     void SetWindowStyle (const long styles);
346     long GetWindowStyle() const;
GetWindowStyleFlag()347     long GetWindowStyleFlag () const { return GetWindowStyle(); }
348 
349     // number of children
350     // ------------------
351 
352     // if 'recursively' is FALSE, only immediate children count, otherwise
353     // the returned number is the number of all items in this branch
354     size_t GetChildrenCount (const wxTreeItemId& item, bool recursively = true);
355 
356     // navigation
357     // ----------
358 
359     // wxTreeItemId.IsOk() will return FALSE if there is no such item
360 
361     // get the root tree item
362     wxTreeItemId GetRootItem() const;
363 
364     // get the item currently selected (may return NULL if no selection)
365     wxTreeItemId GetSelection() const;
366 
367     // get the items currently selected, return the number of such item
368     size_t GetSelections (wxArrayTreeItemIds&) const;
369 
370     // get the parent of this item (may return NULL if root)
371     wxTreeItemId GetItemParent (const wxTreeItemId& item) const;
372 
373     // for this enumeration function you must pass in a "cookie" parameter
374     // which is opaque for the application but is necessary for the library
375     // to make these functions reentrant (i.e. allow more than one
376     // enumeration on one and the same object simultaneously). Of course,
377     // the "cookie" passed to GetFirstChild() and GetNextChild() should be
378     // the same!
379 
380     // get child of this item
381     wxTreeItemId GetFirstChild(const wxTreeItemId& item, wxTreeItemIdValue& cookie) const;
382     wxTreeItemId GetNextChild(const wxTreeItemId& item, wxTreeItemIdValue& cookie) const;
383     wxTreeItemId GetPrevChild(const wxTreeItemId& item, wxTreeItemIdValue& cookie) const;
384     wxTreeItemId GetLastChild(const wxTreeItemId& item, wxTreeItemIdValue& cookie) const;
385 
386     // get sibling of this item
387     wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
388     wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
389 
390     // get item in the full tree (currently only for internal use)
391     wxTreeItemId GetNext(const wxTreeItemId& item) const;
392     wxTreeItemId GetPrev(const wxTreeItemId& item) const;
393 
394     // get expanded item, see IsExpanded()
395     wxTreeItemId GetFirstExpandedItem() const;
396     wxTreeItemId GetNextExpanded(const wxTreeItemId& item) const;
397     wxTreeItemId GetPrevExpanded(const wxTreeItemId& item) const;
398 
399     // get visible item, see IsVisible()
400     wxTreeItemId GetFirstVisibleItem(                      bool fullRow = false) const;
401     wxTreeItemId GetFirstVisible(                          bool fullRow = false, bool within = true) const;
402     wxTreeItemId GetNextVisible (const wxTreeItemId& item, bool fullRow = false, bool within = true) const;
403     wxTreeItemId GetPrevVisible (const wxTreeItemId& item, bool fullRow = false, bool within = true) const;
404     wxTreeItemId GetLastVisible (                          bool fullRow = false, bool within = true) const;
405 
406     // operations
407     // ----------
408 
409     // add the root node to the tree
410     wxTreeItemId AddRoot (const wxString& text,
411                           int image = -1, int selectedImage = -1,
412                           wxTreeItemData *data = NULL);
413 
414     // insert a new item in as the first child of the parent
415     wxTreeItemId PrependItem (const wxTreeItemId& parent,
416                               const wxString& text,
417                               int image = -1, int selectedImage = -1,
418                               wxTreeItemData *data = NULL);
419 
420     // insert a new item after a given one
421     wxTreeItemId InsertItem (const wxTreeItemId& parent,
422                              const wxTreeItemId& idPrevious,
423                              const wxString& text,
424                              int image = -1, int selectedImage = -1,
425                              wxTreeItemData *data = NULL);
426 
427     // insert a new item before the one with the given index
428     wxTreeItemId InsertItem (const wxTreeItemId& parent,
429                              size_t index,
430                              const wxString& text,
431                              int image = -1, int selectedImage = -1,
432                              wxTreeItemData *data = NULL);
433 
434     // insert a new item in as the last child of the parent
435     wxTreeItemId AppendItem (const wxTreeItemId& parent,
436                              const wxString& text,
437                              int image = -1, int selectedImage = -1,
438                              wxTreeItemData *data = NULL);
439 
440     // delete this item (except root) + children and associated data if any
441     void Delete (const wxTreeItemId& item);
442     // delete all children (but don't delete the item itself)
443     void DeleteChildren (const wxTreeItemId& item);
444     // delete the root and all its children from the tree
445     void DeleteRoot();
446 
447     // expand this item
448     void Expand (const wxTreeItemId& item);
449     // expand this item and all subitems recursively
450     void ExpandAll (const wxTreeItemId& item);
451     // collapse the item without removing its children
452     void Collapse (const wxTreeItemId& item);
453     // collapse the item and remove all children
454     void CollapseAndReset(const wxTreeItemId& item); //? TODO ???
455     // toggles the current state
456     void Toggle (const wxTreeItemId& item);
457 
458     // set cursor item (indicated by black rectangle)
459     void SetCurrentItem(const wxTreeItemId& item = (wxTreeItemId*)NULL);
460 
461     // remove the selection from currently selected item (if any)
462     void Unselect();
463     void UnselectAll();
464     // select this item - return true if selection was allowed (no veto)
465     bool SelectItem (const wxTreeItemId& item,
466                      const wxTreeItemId& last = (wxTreeItemId*)NULL,
467                      bool unselect_others = true);
468     // select all items in the expanded tree
469     void SelectAll();
470     // make sure this item is visible (expanding the parent item and/or
471     // scrolling to this item if necessary)
472     void EnsureVisible (const wxTreeItemId& item);
473     // scroll to this item (but don't expand its parent)
474     void ScrollTo (const wxTreeItemId& item);
475 
476     // The first function is more portable (because easier to implement
477     // on other platforms), but the second one returns some extra info.
HitTest(const wxPoint & point)478     wxTreeItemId HitTest (const wxPoint& point)
479         { int flags; int column; return HitTest (point, flags, column); }
HitTest(const wxPoint & point,int & flags)480     wxTreeItemId HitTest (const wxPoint& point, int& flags)
481         { int column; return HitTest (point, flags, column); }
482     wxTreeItemId HitTest (const wxPoint& point, int& flags, int& column);
483 
484     // get the bounding rectangle of the item (or of its label only)
485     bool GetBoundingRect (const wxTreeItemId& item, wxRect& rect,
486                           bool textOnly = false) const;
487 
488     // Start editing the item label: this (temporarily) replaces the item
489     // with a one line edit control. The item will be selected if it hadn't
490     // been before.
EditLabel(const wxTreeItemId & item)491     void EditLabel (const wxTreeItemId& item)
492         { EditLabel (item, GetMainColumn()); }
493     // edit item's label of the given column
494     void EditLabel (const wxTreeItemId& item, int column);
495     void EndEdit(bool isCancelled);
496 
497     // virtual mode
498     virtual wxString OnGetItemText( wxTreeItemData* item, long column ) const;
499 
500     // sorting
501     // this function is called to compare 2 items and should return -1, 0
502     // or +1 if the first item is less than, equal to or greater than the
503     // second one. The base class version performs alphabetic comparaison
504     // of item labels (GetText)
505     virtual int OnCompareItems (const wxTreeItemId& item1, const wxTreeItemId& item2);
506     virtual int OnCompareItems (const wxTreeItemId& item1, const wxTreeItemId& item2, int column);
507     // sort the children of this item using OnCompareItems
508     // NB: this function is not reentrant and not MT-safe (TODO)!
509     void SortChildren(const wxTreeItemId& item, int column = -1, bool reverseOrder = false);
510 
511     // searching (by column only)
512     wxTreeItemId FindItem (const wxTreeItemId& item,             const wxString& str, int mode = 0) { return FindItem(item, GetMainColumn(), str, mode); };
513     wxTreeItemId FindItem (const wxTreeItemId& item, int column, const wxString& str, int mode = 0);
514 
515     // overridden base class virtuals
516     virtual bool SetBackgroundColour (const wxColour& colour);
517     virtual bool SetForegroundColour (const wxColour& colour);
518 
519     // drop over item
520     void SetDragItem (const wxTreeItemId& item = (wxTreeItemId*)NULL);
521 
522 
523     virtual wxSize DoGetBestSize() const;
524 
525 protected:
526     // header window, responsible for column visualization and manipulation
GetHeaderWindow()527     wxTreeListHeaderWindow* GetHeaderWindow() const
528         { return m_header_win; }
529     wxTreeListHeaderWindow* m_header_win;  // future cleanup: make private or remove GetHeaderWindow()
530 
531     // main window, the "true" tree ctrl
GetMainWindow()532     wxTreeListMainWindow* GetMainWindow() const
533         { return m_main_win; }
534     wxTreeListMainWindow* m_main_win;  // future cleanup: make private or remove GetMainWindow()
535 
GetHeaderHeight()536     int GetHeaderHeight() const { return m_headerHeight; }
537 
538     void CalculateAndSetHeaderHeight();
539     void DoHeaderLayout();
540     void OnSize(wxSizeEvent& event);
541 
542 private:
543     int m_headerHeight;
544 
545     DECLARE_EVENT_TABLE()
546     DECLARE_DYNAMIC_CLASS(wxTreeListCtrl)
547 };
548 
549 #endif // TREELISTCTRL_H
550 
551