1 /////////////////////////////////////////////////////////////////////////////
2 // Name: treelistctrl.h
3 // Purpose: wxTreeListCtrl class
4 // Author: Robert Roebling
5 // Maintainer: Otto Wyss
8 // Copyright: (c) 2004 Robert Roebling, Julian Smart, Alberto Griggio,
9 // Vadim Zeitlin, Otto Wyss
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
14 #ifndef TREELISTCTRL_H
15 #define TREELISTCTRL_H
17 #include <wx/treectrl.h>
18 #include <wx/control.h>
20 #include <wx/listctrl.h> // for wxListEvent
23 #define GIZMODLLEXPORT WXDLLEXPORT
25 #define GIZMODLLEXPORT
29 class GIZMODLLEXPORT wxTreeListItem
;
30 class GIZMODLLEXPORT wxTreeListHeaderWindow
;
31 class GIZMODLLEXPORT wxTreeListMainWindow
;
33 #define wxTR_COLUMN_LINES 0x1000 // put border around items
34 #define wxTR_VIRTUAL 0x4000 // The application provides items text on demand.
36 // Using this typedef removes an ambiguity when calling Remove()
38 #if !wxCHECK_VERSION(2, 5, 0)
39 typedef long wxTreeItemIdValue
;
41 typedef void *wxTreeItemIdValue
;
45 //-----------------------------------------------------------------------------
46 // wxTreeListColumnAttrs
47 //-----------------------------------------------------------------------------
50 DEFAULT_COL_WIDTH
= 100
53 class GIZMODLLEXPORT wxTreeListColumnInfo
: public wxObject
{
56 wxTreeListColumnInfo (const wxString
&text
= wxEmptyString
,
57 int width
= DEFAULT_COL_WIDTH
,
58 int flag
= wxALIGN_LEFT
,
66 m_selected_image
= -1;
71 wxTreeListColumnInfo (const wxTreeListColumnInfo
& other
) {
72 m_text
= other
.m_text
;
73 m_width
= other
.m_width
;
74 m_flag
= other
.m_flag
;
75 m_image
= other
.m_image
;
76 m_selected_image
= other
.m_selected_image
;
77 m_shown
= other
.m_shown
;
78 m_edit
= other
.m_edit
;
81 ~wxTreeListColumnInfo() {}
84 wxString
GetText() const { return m_text
; }
85 wxTreeListColumnInfo
& SetText (const wxString
& text
) { m_text
= text
; return *this; }
87 int GetWidth() const { return m_width
; }
88 wxTreeListColumnInfo
& SetWidth (int width
) { m_width
= width
; return *this; }
90 int GetAlignment() const { return m_flag
; }
91 wxTreeListColumnInfo
& SetAlignment (int flag
) { m_flag
= flag
; return *this; }
93 int GetImage() const { return m_image
; }
94 wxTreeListColumnInfo
& SetImage (int image
) { m_image
= image
; return *this; }
96 int GetSelectedImage() const { return m_selected_image
; }
97 wxTreeListColumnInfo
& SetSelectedImage (int image
) { m_selected_image
= image
; return *this; }
99 bool IsEditable() const { return m_edit
; }
100 wxTreeListColumnInfo
& SetEditable (bool edit
)
101 { m_edit
= edit
; return *this; }
103 bool IsShown() const { return m_shown
; }
104 wxTreeListColumnInfo
& SetShown(bool shown
) { m_shown
= shown
; return *this; }
111 int m_selected_image
;
116 //----------------------------------------------------------------------------
117 // wxTreeListCtrl - the multicolumn tree control
118 //----------------------------------------------------------------------------
120 // modes for navigation
121 const int wxTL_MODE_NAV_FULLTREE
= 0x0000; // default
122 const int wxTL_MODE_NAV_EXPANDED
= 0x0001;
123 const int wxTL_MODE_NAV_VISIBLE
= 0x0002;
124 const int wxTL_MODE_NAV_LEVEL
= 0x0004;
126 // modes for FindItem
127 const int wxTL_MODE_FIND_EXACT
= 0x0000; // default
128 const int wxTL_MODE_FIND_PARTIAL
= 0x0010;
129 const int wxTL_MODE_FIND_NOCASE
= 0x0020;
131 // additional flag for HitTest
132 const int wxTREE_HITTEST_ONITEMCOLUMN
= 0x2000;
133 extern GIZMODLLEXPORT
const wxChar
* wxTreeListCtrlNameStr
;
136 class GIZMODLLEXPORT wxTreeListCtrl
: public wxControl
142 : m_header_win(0), m_main_win(0), m_headerHeight(0)
145 wxTreeListCtrl(wxWindow
*parent
, wxWindowID id
= -1,
146 const wxPoint
& pos
= wxDefaultPosition
,
147 const wxSize
& size
= wxDefaultSize
,
148 long style
= wxTR_DEFAULT_STYLE
,
149 const wxValidator
&validator
= wxDefaultValidator
,
150 const wxString
& name
= wxTreeListCtrlNameStr
)
151 : m_header_win(0), m_main_win(0), m_headerHeight(0)
153 Create(parent
, id
, pos
, size
, style
, validator
, name
);
156 virtual ~wxTreeListCtrl() {}
158 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
159 const wxPoint
& pos
= wxDefaultPosition
,
160 const wxSize
& size
= wxDefaultSize
,
161 long style
= wxTR_DEFAULT_STYLE
,
162 const wxValidator
&validator
= wxDefaultValidator
,
163 const wxString
& name
= wxTreeListCtrlNameStr
);
165 void Refresh(bool erase
=TRUE
, const wxRect
* rect
=NULL
);
170 // get the total number of items in the control
171 size_t GetCount() const;
173 // indent is the number of pixels the children are indented relative to
174 // the parents position. SetIndent() also redraws the control
176 unsigned int GetIndent() const;
177 void SetIndent(unsigned int indent
);
179 // line spacing is the space above and below the text on each line
180 unsigned int GetLineSpacing() const;
181 void SetLineSpacing(unsigned int spacing
);
183 // image list: these functions allow to associate an image list with
184 // the control and retrieve it. Note that when assigned with
185 // SetImageList, the control does _not_ delete
186 // the associated image list when it's deleted in order to allow image
187 // lists to be shared between different controls. If you use
188 // AssignImageList, the control _does_ delete the image list.
190 // The normal image list is for the icons which correspond to the
191 // normal tree item state (whether it is selected or not).
192 // Additionally, the application might choose to show a state icon
193 // which corresponds to an app-defined item state (for example,
194 // checked/unchecked) which are taken from the state image list.
195 wxImageList
*GetImageList() const;
196 wxImageList
*GetStateImageList() const;
197 wxImageList
*GetButtonsImageList() const;
199 void SetImageList(wxImageList
*imageList
);
200 void SetStateImageList(wxImageList
*imageList
);
201 void SetButtonsImageList(wxImageList
*imageList
);
202 void AssignImageList(wxImageList
*imageList
);
203 void AssignStateImageList(wxImageList
*imageList
);
204 void AssignButtonsImageList(wxImageList
*imageList
);
207 // Functions to work with columns
210 void AddColumn (const wxString
& text
,
211 int width
= DEFAULT_COL_WIDTH
,
212 int flag
= wxALIGN_LEFT
,
216 AddColumn (wxTreeListColumnInfo (text
, width
, flag
, image
, shown
, edit
));
218 void AddColumn (const wxTreeListColumnInfo
& colInfo
);
220 // inserts a column before the given one
221 void InsertColumn (int before
,
222 const wxString
& text
,
223 int width
= DEFAULT_COL_WIDTH
,
224 int flag
= wxALIGN_LEFT
,
228 InsertColumn (before
,
229 wxTreeListColumnInfo (text
, width
, flag
, image
, shown
, edit
));
231 void InsertColumn (int before
, const wxTreeListColumnInfo
& colInfo
);
233 // deletes the given column - does not delete the corresponding column
234 void RemoveColumn (int column
);
236 // returns the number of columns in the ctrl
237 int GetColumnCount() const;
239 // tells which column is the "main" one, i.e. the "threaded" one
240 void SetMainColumn (int column
);
241 int GetMainColumn() const;
243 void SetColumn (int column
, const wxTreeListColumnInfo
& colInfo
);
244 wxTreeListColumnInfo
& GetColumn (int column
);
245 const wxTreeListColumnInfo
& GetColumn (int column
) const;
247 void SetColumnText (int column
, const wxString
& text
);
248 wxString
GetColumnText (int column
) const;
250 void SetColumnWidth (int column
, int width
);
251 int GetColumnWidth (int column
) const;
253 void SetColumnAlignment (int column
, int flag
);
254 int GetColumnAlignment (int column
) const;
256 void SetColumnImage (int column
, int image
);
257 int GetColumnImage (int column
) const;
259 void SetColumnShown (int column
, bool shown
= true);
260 bool IsColumnShown (int column
) const;
262 void SetColumnEditable (int column
, bool edit
= true);
263 bool IsColumnEditable (int column
) const;
265 // Functions to work with items.
270 // retrieve item's label (of the main column)
271 wxString
GetItemText (const wxTreeItemId
& item
) const
272 { return GetItemText (item
, GetMainColumn()); }
273 // retrieves item's label of the given column
274 wxString
GetItemText (const wxTreeItemId
& item
, int column
) const;
276 // get one of the images associated with the item (normal by default)
277 int GetItemImage (const wxTreeItemId
& item
,
278 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
279 { return GetItemImage (item
, GetMainColumn(), which
); }
280 int GetItemImage (const wxTreeItemId
& item
, int column
,
281 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
283 // get the data associated with the item
284 wxTreeItemData
*GetItemData (const wxTreeItemId
& item
) const;
286 bool GetItemBold (const wxTreeItemId
& item
) const;
287 wxColour
GetItemTextColour (const wxTreeItemId
& item
) const;
288 wxColour
GetItemBackgroundColour (const wxTreeItemId
& item
) const;
289 wxFont
GetItemFont (const wxTreeItemId
& item
) const;
294 void SetItemText (const wxTreeItemId
& item
, const wxString
& text
)
295 { SetItemText (item
, GetMainColumn(), text
); }
296 void SetItemText (const wxTreeItemId
& item
, int column
, const wxString
& text
);
298 // get one of the images associated with the item (normal by default)
299 void SetItemImage (const wxTreeItemId
& item
, int image
,
300 wxTreeItemIcon which
= wxTreeItemIcon_Normal
)
301 { SetItemImage (item
, GetMainColumn(), image
, which
); }
302 // the which parameter is ignored for all columns but the main one
303 void SetItemImage (const wxTreeItemId
& item
, int column
, int image
,
304 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
306 // associate some data with the item
307 void SetItemData (const wxTreeItemId
& item
, wxTreeItemData
*data
);
309 // force appearance of [+] button near the item. This is useful to
310 // allow the user to expand the items which don't have any children now
311 // - but instead add them only when needed, thus minimizing memory
312 // usage and loading time.
313 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= true);
315 // the item will be shown in bold
316 void SetItemBold (const wxTreeItemId
& item
, bool bold
= true);
318 // set the item's text colour
319 void SetItemTextColour (const wxTreeItemId
& item
, const wxColour
& colour
);
321 // set the item's background colour
322 void SetItemBackgroundColour (const wxTreeItemId
& item
, const wxColour
& colour
);
324 // set the item's font (should be of the same height for all items)
325 void SetItemFont (const wxTreeItemId
& item
, const wxFont
& font
);
327 // set the window font
328 virtual bool SetFont ( const wxFont
&font
);
331 void SetWindowStyle (const long styles
);
332 long GetWindowStyle() const;
333 long GetWindowStyleFlag () const { return GetWindowStyle(); }
335 // item status inquiries
336 // ---------------------
338 // is the item visible (it might be outside the view or not expanded)?
339 bool IsVisible (const wxTreeItemId
& item
, bool fullRow
= false) const;
340 // does the item has any children?
341 bool HasChildren (const wxTreeItemId
& item
) const;
342 // is the item expanded (only makes sense if HasChildren())?
343 bool IsExpanded (const wxTreeItemId
& item
) const;
344 // is this item currently selected (the same as has focus)?
345 bool IsSelected (const wxTreeItemId
& item
) const;
346 // is item text in bold font?
347 bool IsBold (const wxTreeItemId
& item
) const;
348 // does the layout include space for a button?
350 // number of children
351 // ------------------
353 // if 'recursively' is FALSE, only immediate children count, otherwise
354 // the returned number is the number of all items in this branch
355 size_t GetChildrenCount (const wxTreeItemId
& item
, bool recursively
= true);
360 // wxTreeItemId.IsOk() will return FALSE if there is no such item
362 // get the root tree item
363 wxTreeItemId
GetRootItem() const;
365 // get the item currently selected (may return NULL if no selection)
366 wxTreeItemId
GetSelection() const;
368 // get the items currently selected, return the number of such item
369 size_t GetSelections (wxArrayTreeItemIds
&) const;
371 // get the parent of this item (may return NULL if root)
372 wxTreeItemId
GetItemParent (const wxTreeItemId
& item
) const;
374 // for this enumeration function you must pass in a "cookie" parameter
375 // which is opaque for the application but is necessary for the library
376 // to make these functions reentrant (i.e. allow more than one
377 // enumeration on one and the same object simultaneously). Of course,
378 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
381 // get child of this item
382 #if !wxCHECK_VERSION(2, 5, 0)
383 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const;
384 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& cookie
) const;
385 wxTreeItemId
GetPrevChild(const wxTreeItemId
& item
, long& cookie
) const;
386 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
, long& cookie
) const;
388 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, wxTreeItemIdValue
& cookie
) const;
389 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, wxTreeItemIdValue
& cookie
) const;
390 wxTreeItemId
GetPrevChild(const wxTreeItemId
& item
, wxTreeItemIdValue
& cookie
) const;
391 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
, wxTreeItemIdValue
& cookie
) const;
394 // get sibling of this item
395 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
396 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
398 // get item in the full tree (currently only for internal use)
399 wxTreeItemId
GetNext(const wxTreeItemId
& item
) const;
400 wxTreeItemId
GetPrev(const wxTreeItemId
& item
) const;
402 // get expanded item, see IsExpanded()
403 wxTreeItemId
GetFirstExpandedItem() const;
404 wxTreeItemId
GetNextExpanded(const wxTreeItemId
& item
) const;
405 wxTreeItemId
GetPrevExpanded(const wxTreeItemId
& item
) const;
407 // get visible item, see IsVisible()
408 wxTreeItemId
GetFirstVisibleItem(bool fullRow
= false) const;
409 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
, bool fullRow
= false) const;
410 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
, bool fullRow
= false) const;
415 // add the root node to the tree
416 wxTreeItemId
AddRoot (const wxString
& text
,
417 int image
= -1, int selectedImage
= -1,
418 wxTreeItemData
*data
= NULL
);
420 // insert a new item in as the first child of the parent
421 wxTreeItemId
PrependItem (const wxTreeItemId
& parent
,
422 const wxString
& text
,
423 int image
= -1, int selectedImage
= -1,
424 wxTreeItemData
*data
= NULL
);
426 // insert a new item after a given one
427 wxTreeItemId
InsertItem (const wxTreeItemId
& parent
,
428 const wxTreeItemId
& idPrevious
,
429 const wxString
& text
,
430 int image
= -1, int selectedImage
= -1,
431 wxTreeItemData
*data
= NULL
);
433 // insert a new item before the one with the given index
434 wxTreeItemId
InsertItem (const wxTreeItemId
& parent
,
436 const wxString
& text
,
437 int image
= -1, int selectedImage
= -1,
438 wxTreeItemData
*data
= NULL
);
440 // insert a new item in as the last child of the parent
441 wxTreeItemId
AppendItem (const wxTreeItemId
& parent
,
442 const wxString
& text
,
443 int image
= -1, int selectedImage
= -1,
444 wxTreeItemData
*data
= NULL
);
446 // delete this item (except root) and associated data if any
447 void Delete (const wxTreeItemId
& item
);
448 // delete all children (but don't delete the item itself)
449 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
450 void DeleteChildren (const wxTreeItemId
& item
);
451 // delete the root and all its children from the tree
452 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
456 void Expand (const wxTreeItemId
& item
);
457 // expand this item and all subitems recursively
458 void ExpandAll (const wxTreeItemId
& item
);
459 // collapse the item without removing its children
460 void Collapse (const wxTreeItemId
& item
);
461 // collapse the item and remove all children
462 void CollapseAndReset(const wxTreeItemId
& item
); //? TODO ???
463 // toggles the current state
464 void Toggle (const wxTreeItemId
& item
);
466 // remove the selection from currently selected item (if any)
470 void SelectItem (const wxTreeItemId
& item
,
471 const wxTreeItemId
& last
= (wxTreeItemId
*)NULL
,
472 bool unselect_others
= true);
473 // select all items in the expanded tree
475 // make sure this item is visible (expanding the parent item and/or
476 // scrolling to this item if necessary)
477 void EnsureVisible (const wxTreeItemId
& item
);
478 // scroll to this item (but don't expand its parent)
479 void ScrollTo (const wxTreeItemId
& item
);
481 // The first function is more portable (because easier to implement
482 // on other platforms), but the second one returns some extra info.
483 wxTreeItemId
HitTest (const wxPoint
& point
)
484 { int flags
; int column
; return HitTest (point
, flags
, column
); }
485 wxTreeItemId
HitTest (const wxPoint
& point
, int& flags
)
486 { int column
; return HitTest (point
, flags
, column
); }
487 wxTreeItemId
HitTest (const wxPoint
& point
, int& flags
, int& column
);
489 // get the bounding rectangle of the item (or of its label only)
490 bool GetBoundingRect (const wxTreeItemId
& item
, wxRect
& rect
,
491 bool textOnly
= false) const;
493 // Start editing the item label: this (temporarily) replaces the item
494 // with a one line edit control. The item will be selected if it hadn't
496 void EditLabel (const wxTreeItemId
& item
)
497 { EditLabel (item
, GetMainColumn()); }
498 // edit item's label of the given column
499 void EditLabel (const wxTreeItemId
& item
, int column
);
502 virtual wxString
OnGetItemText( wxTreeItemData
* item
, long column
) const;
505 // this function is called to compare 2 items and should return -1, 0
506 // or +1 if the first item is less than, equal to or greater than the
507 // second one. The base class version performs alphabetic comparaison
508 // of item labels (GetText)
509 virtual int OnCompareItems (const wxTreeItemId
& item1
, const wxTreeItemId
& item2
);
510 // sort the children of this item using OnCompareItems
511 // NB: this function is not reentrant and not MT-safe (FIXME)!
512 void SortChildren(const wxTreeItemId
& item
);
515 wxTreeItemId
FindItem (const wxTreeItemId
& item
, const wxString
& str
, int mode
= 0);
517 // overridden base class virtuals
518 virtual bool SetBackgroundColour (const wxColour
& colour
);
519 virtual bool SetForegroundColour (const wxColour
& colour
);
522 void SetDragItem (const wxTreeItemId
& item
= (wxTreeItemId
*)NULL
);
525 wxTreeListHeaderWindow
* GetHeaderWindow() const
526 { return m_header_win
; }
528 wxTreeListMainWindow
* GetMainWindow() const
529 { return m_main_win
; }
531 virtual wxSize
DoGetBestSize() const;
534 // header window, responsible for column visualization and manipulation
535 wxTreeListHeaderWindow
* m_header_win
;
537 // main window, the "true" tree ctrl
538 wxTreeListMainWindow
* m_main_win
;
540 void CalculateAndSetHeaderHeight();
541 void DoHeaderLayout();
542 void OnSize(wxSizeEvent
& event
);
547 DECLARE_EVENT_TABLE()
548 DECLARE_DYNAMIC_CLASS(wxTreeListCtrl
)
551 #endif // TREELISTCTRL_H