1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeListCtrl and helpers
7 // Created: 12-Sept-2006
9 // Copyright: (c) 2006 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
16 //---------------------------------------------------------------------------
19 #include "wx/treelistctrl.h"
20 #include "wx/wxPython/pytree.h"
24 //---------------------------------------------------------------------------
28 MAKE_CONST_WXSTRING2(TreeListCtrlNameStr, wxT("treelistctrl"));
31 //----------------------------------------------------------------------------
32 // wxTreeListCtrl - the multicolumn tree control
33 //----------------------------------------------------------------------------
38 DEFAULT_COL_WIDTH = 100
41 // modes for navigation
43 wxTL_MODE_NAV_FULLTREE,
44 wxTL_MODE_NAV_EXPANDED,
45 wxTL_MODE_NAV_VISIBLE,
52 wxTL_MODE_FIND_PARTIAL,
56 // additional flag for HitTest
58 wxTREE_HITTEST_ONITEMCOLUMN
60 %pythoncode { wx.TREE_HITTEST_ONITEMCOLUMN = TREE_HITTEST_ONITEMCOLUMN }
63 // additional style flags
65 wxTR_COLUMN_LINES, // put border around items
66 wxTR_VIRTUAL // The application provides items text on demand.
69 wx.TR_COLUMN_LINES = TR_COLUMN_LINES
70 wxTR_VIRTUAL = TR_VIRTUAL
75 %#// Compatibility aliases for old names/values
76 TL_ALIGN_LEFT = wx.ALIGN_LEFT
77 TL_ALIGN_RIGHT = wx.ALIGN_RIGHT
78 TL_ALIGN_CENTER = wx.ALIGN_CENTER
80 TL_SEARCH_VISIBLE = TL_MODE_NAV_VISIBLE
81 TL_SEARCH_LEVEL = TL_MODE_NAV_LEVEL
82 TL_SEARCH_FULL = TL_MODE_FIND_EXACT
83 TL_SEARCH_PARTIAL = TL_MODE_FIND_PARTIAL
84 TL_SEARCH_NOCASE = TL_MODE_FIND_NOCASE
86 TR_DONT_ADJUST_MAC = 0
87 wx.TR_DONT_ADJUST_MAC = TR_DONT_ADJUST_MAC
93 class wxTreeListColumnInfo: public wxObject {
95 wxTreeListColumnInfo(const wxString& text = wxPyEmptyString,
96 int width = DEFAULT_COL_WIDTH,
97 int flag = wxALIGN_LEFT,
102 ~wxTreeListColumnInfo();
104 int GetAlignment() const;
105 wxString GetText() const;
106 int GetImage() const;
107 int GetSelectedImage() const;
108 size_t GetWidth() const;
109 bool IsEditable() const { return m_edit; }
110 bool IsShown() const { return m_shown; }
112 // TODO: These all actually return wxTreeListColumnInfo&, any problem with doing it for Python too?
113 void SetAlignment(int alignment);
114 void SetText(const wxString& text);
115 void SetImage(int image);
116 void SetSelectedImage(int image);
117 void SetWidth(size_t with);
118 void SetEditable (bool edit);
119 void SetShown(bool shown);
126 %{ // C++ version of Python aware control
127 class wxPyTreeListCtrl : public wxTreeListCtrl {
128 DECLARE_ABSTRACT_CLASS(wxPyTreeListCtrl);
130 wxPyTreeListCtrl() : wxTreeListCtrl() {}
131 wxPyTreeListCtrl(wxWindow *parent, wxWindowID id,
135 const wxValidator &validator,
136 const wxString& name) :
137 wxTreeListCtrl(parent, id, pos, size, style, validator, name) {}
139 virtual int OnCompareItems(const wxTreeItemId& item1,
140 const wxTreeItemId& item2) {
143 wxPyBlock_t blocked = wxPyBeginBlockThreads();
144 if ((found = wxPyCBH_findCallback(m_myInst, "OnCompareItems"))) {
145 PyObject *o1 = wxPyConstructObject((void*)&item1, wxT("wxTreeItemId"), 0);
146 PyObject *o2 = wxPyConstructObject((void*)&item2, wxT("wxTreeItemId"), 0);
147 rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("(OO)",o1,o2));
151 wxPyEndBlockThreads(blocked);
153 rval = wxTreeListCtrl::OnCompareItems(item1, item2);
157 virtual wxString OnGetItemText( wxTreeItemData* item, long column ) const {
160 wxPyBlock_t blocked = wxPyBeginBlockThreads();
161 if ((found = wxPyCBH_findCallback(m_myInst, "OnGetItemText"))) {
163 PyObject* itemo = wxPyConstructObject((void*)&item, wxT("wxTreeItemId"), 0);
164 ro = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("(Oi)", itemo, column));
167 rval = Py2wxString(ro);
171 wxPyEndBlockThreads(blocked);
173 rval = wxTreeListCtrl::OnGetItemText(item, column);
180 IMPLEMENT_ABSTRACT_CLASS(wxPyTreeListCtrl, wxTreeListCtrl)
189 MustHaveApp(wxPyTreeListCtrl);
191 %rename(TreeListCtrl) wxPyTreeListCtrl;
192 class wxPyTreeListCtrl : public wxControl
195 %pythonAppend wxPyTreeListCtrl "self._setOORInfo(self);self._setCallbackInfo(self, TreeListCtrl)"
196 %pythonAppend wxPyTreeListCtrl() ""
198 wxPyTreeListCtrl(wxWindow *parent, wxWindowID id = -1,
199 const wxPoint& pos = wxDefaultPosition,
200 const wxSize& size = wxDefaultSize,
201 long style = wxTR_DEFAULT_STYLE,
202 const wxValidator &validator = wxDefaultValidator,
203 const wxString& name = wxPyTreeListCtrlNameStr );
204 %RenameCtor(PreTreeListCtrl, wxPyTreeListCtrl());
206 bool Create(wxWindow *parent, wxWindowID id = -1,
207 const wxPoint& pos = wxDefaultPosition,
208 const wxSize& size = wxDefaultSize,
209 long style = wxTR_DEFAULT_STYLE,
210 const wxValidator &validator = wxDefaultValidator,
211 const wxString& name = wxPyTreeListCtrlNameStr );
213 void _setCallbackInfo(PyObject* self, PyObject* _class);
216 // get the total number of items in the control
217 size_t GetCount() const;
219 // indent is the number of pixels the children are indented relative to
220 // the parents position. SetIndent() also redraws the control
222 unsigned int GetIndent() const;
223 void SetIndent(unsigned int indent);
225 // line spacing is the space above and below the text on each line
226 unsigned int GetLineSpacing() const;
227 void SetLineSpacing(unsigned int spacing);
229 // image list: these functions allow to associate an image list with
230 // the control and retrieve it. Note that when assigned with
231 // SetImageList, the control does _not_ delete
232 // the associated image list when it's deleted in order to allow image
233 // lists to be shared between different controls. If you use
234 // AssignImageList, the control _does_ delete the image list.
236 // The normal image list is for the icons which correspond to the
237 // normal tree item state (whether it is selected or not).
238 // Additionally, the application might choose to show a state icon
239 // which corresponds to an app-defined item state (for example,
240 // checked/unchecked) which are taken from the state image list.
241 wxImageList *GetImageList() const;
242 wxImageList *GetStateImageList() const;
243 wxImageList *GetButtonsImageList() const;
245 void SetImageList(wxImageList *imageList);
246 void SetStateImageList(wxImageList *imageList);
247 void SetButtonsImageList(wxImageList *imageList);
249 %disownarg( wxImageList *imageList );
250 void AssignImageList(wxImageList *imageList);
251 void AssignStateImageList(wxImageList *imageList);
252 void AssignButtonsImageList(wxImageList *imageList);
253 %cleardisown( wxImageList *imageList );
257 void AddColumn (const wxString& text,
258 int width = DEFAULT_COL_WIDTH,
259 int flag = wxALIGN_LEFT,
263 %Rename(AddColumnInfo, void, AddColumn(const wxTreeListColumnInfo& col));
265 // inserts a column before the given one
266 void InsertColumn (int before,
267 const wxString& text,
268 int width = DEFAULT_COL_WIDTH,
269 int flag = wxALIGN_LEFT,
273 %Rename(InsertColumnInfo, void, InsertColumn(size_t before, const wxTreeListColumnInfo& col));
275 // deletes the given column - does not delete the corresponding column
277 void RemoveColumn(size_t column);
279 // returns the number of columns in the ctrl
280 size_t GetColumnCount() const;
282 // tells which column is the "main" one, i.e. the "threaded" one
283 void SetMainColumn(size_t column);
284 size_t GetMainColumn() const;
286 void SetColumn (int column, const wxTreeListColumnInfo& colInfo);
287 wxTreeListColumnInfo& GetColumn (int column);
289 void SetColumnText (int column, const wxString& text);
290 wxString GetColumnText (int column) const;
292 void SetColumnWidth (int column, int width);
293 int GetColumnWidth (int column) const;
295 void SetColumnAlignment (int column, int flag);
296 int GetColumnAlignment (int column) const;
298 void SetColumnImage (int column, int image);
299 int GetColumnImage (int column) const;
301 void SetColumnShown (int column, bool shown = true);
302 bool IsColumnShown (int column) const;
303 %pythoncode { ShowColumn = SetColumnShown }
305 void SetColumnEditable (int column, bool edit = true);
306 bool IsColumnEditable (int column) const;
312 // retrieves item's label of the given column (main column by default)
313 wxString GetItemText(const wxTreeItemId& item, int column = -1) {
314 if (column < 0) column = self->GetMainColumn();
315 return self->GetItemText(item, column);
318 // get one of the images associated with the item (normal by default)
319 int GetItemImage(const wxTreeItemId& item, int column = -1,
320 wxTreeItemIcon which = wxTreeItemIcon_Normal) {
321 if (column < 0) column = self->GetMainColumn();
322 return self->GetItemImage(item, column, which);
325 // set item's label (main column by default)
326 void SetItemText(const wxTreeItemId& item, const wxString& text, int column = -1) {
327 if (column < 0) column = self->GetMainColumn();
328 self->SetItemText(item, column, text);
331 // set one of the images associated with the item (normal by default)
332 // the which parameter is ignored for all columns but the main one
333 void SetItemImage(const wxTreeItemId& item, int image, int column = -1,
334 wxTreeItemIcon which = wxTreeItemIcon_Normal) {
335 if (column < 0) column = self->GetMainColumn();
336 self->SetItemImage(item, column, image, which);
340 // [Get|Set]ItemData substitutes. Automatically create wxPyTreeItemData
342 wxPyTreeItemData* GetItemData(const wxTreeItemId& item) {
343 wxPyTreeItemData* data = (wxPyTreeItemData*)self->GetItemData(item);
345 data = new wxPyTreeItemData();
346 data->SetId(item); // set the id
347 self->SetItemData(item, data);
352 %disownarg( wxPyTreeItemData* data );
353 void SetItemData(const wxTreeItemId& item, wxPyTreeItemData* data) {
354 data->SetId(item); // set the id
355 self->SetItemData(item, data);
357 %cleardisown(wxPyTreeItemData* data );
359 // [Get|Set]ItemPyData are short-cuts. Also made somewhat crash-proof by
360 // automatically creating data classes.
361 PyObject* GetItemPyData(const wxTreeItemId& item) {
362 wxPyTreeItemData* data = (wxPyTreeItemData*)self->GetItemData(item);
364 data = new wxPyTreeItemData();
365 data->SetId(item); // set the id
366 self->SetItemData(item, data);
368 return data->GetData();
371 void SetItemPyData(const wxTreeItemId& item, PyObject* obj) {
372 wxPyTreeItemData* data = (wxPyTreeItemData*)self->GetItemData(item);
374 data = new wxPyTreeItemData(obj);
375 data->SetId(item); // set the id
376 self->SetItemData(item, data);
381 %pythoncode { GetPyData = GetItemPyData }
382 %pythoncode { SetPyData = SetItemPyData }
385 bool GetItemBold(const wxTreeItemId& item) const;
386 wxColour GetItemTextColour(const wxTreeItemId& item) const;
387 wxColour GetItemBackgroundColour(const wxTreeItemId& item) const;
388 wxFont GetItemFont(const wxTreeItemId& item) const;
391 // force appearance of [+] button near the item. This is useful to
392 // allow the user to expand the items which don't have any children now
393 // - but instead add them only when needed, thus minimizing memory
394 // usage and loading time.
395 void SetItemHasChildren(const wxTreeItemId& item, bool has = true);
397 // the item will be shown in bold
398 void SetItemBold(const wxTreeItemId& item, bool bold = true);
400 // set the item's text colour
401 void SetItemTextColour(const wxTreeItemId& item, const wxColour& colour);
403 // set the item's background colour
404 void SetItemBackgroundColour(const wxTreeItemId& item,
405 const wxColour& colour);
407 // set the item's font (should be of the same height for all items)
408 void SetItemFont(const wxTreeItemId& item, const wxFont& font);
412 // is the item visible (it might be outside the view or not expanded)?
413 bool IsVisible(const wxTreeItemId& item) const;
415 // does the item has any children?
416 bool HasChildren(const wxTreeItemId& item) const;
417 %pythoncode { ItemHasChildren = HasChildren }
419 // is the item expanded (only makes sense if HasChildren())?
420 bool IsExpanded(const wxTreeItemId& item) const;
422 // is this item currently selected (the same as has focus)?
423 bool IsSelected(const wxTreeItemId& item) const;
425 // is item text in bold font?
426 bool IsBold(const wxTreeItemId& item) const;
429 // if 'recursively' is False, only immediate children count, otherwise
430 // the returned number is the number of all items in this branch
431 size_t GetChildrenCount(const wxTreeItemId& item, bool recursively = true);
434 // wxTreeItemId.IsOk() will return False if there is no such item
436 // get the root tree item
437 wxTreeItemId GetRootItem() const;
439 // get the item currently selected (may return NULL if no selection)
440 wxTreeItemId GetSelection() const;
442 // get the items currently selected, return the number of such item
443 //size_t GetSelections(wxArrayTreeItemIds&) const;
445 PyObject* GetSelections() {
446 wxPyBlock_t blocked = wxPyBeginBlockThreads();
447 PyObject* rval = PyList_New(0);
448 wxArrayTreeItemIds array;
450 num = self->GetSelections(array);
451 for (x=0; x < num; x++) {
452 wxTreeItemId *tii = new wxTreeItemId(array.Item(x));
453 PyObject* item = wxPyConstructObject((void*)tii, wxT("wxTreeItemId"), true);
454 PyList_Append(rval, item);
457 wxPyEndBlockThreads(blocked);
463 // get the parent of this item (may return NULL if root)
464 wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
466 // for this enumeration function you must pass in a "cookie" parameter
467 // which is opaque for the application but is necessary for the library
468 // to make these functions reentrant (i.e. allow more than one
469 // enumeration on one and the same object simultaneously). Of course,
470 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
474 // NOTE: These are a copy of the same methods in _treectrl.i, be sure to
475 // update both at the same time. (Or find a good way to refactor!)
477 // Get the first child of this item. Returns a wxTreeItemId and an
478 // opaque "cookie" value that should be passed to GetNextChild in
479 // order to continue the search.
480 PyObject* GetFirstChild(const wxTreeItemId& item) {
482 wxTreeItemId* ritem = new wxTreeItemId(self->GetFirstChild(item, cookie));
483 wxPyBlock_t blocked = wxPyBeginBlockThreads();
484 PyObject* tup = PyTuple_New(2);
485 PyTuple_SET_ITEM(tup, 0, wxPyConstructObject(ritem, wxT("wxTreeItemId"), true));
486 PyTuple_SET_ITEM(tup, 1, wxPyMakeSwigPtr(cookie, wxT("void")));
487 wxPyEndBlockThreads(blocked);
492 // Get the next child of this item. The cookie parameter is the 2nd
493 // value returned from GetFirstChild or the previous GetNextChild.
494 // Returns a wxTreeItemId and an opaque "cookie" value that should be
495 // passed to GetNextChild in order to continue the search.
496 PyObject* GetNextChild(const wxTreeItemId& item, void* cookie) {
497 wxTreeItemId* ritem = new wxTreeItemId(self->GetNextChild(item, cookie));
498 wxPyBlock_t blocked = wxPyBeginBlockThreads();
499 PyObject* tup = PyTuple_New(2);
500 PyTuple_SET_ITEM(tup, 0, wxPyConstructObject(ritem, wxT("wxTreeItemId"), true));
501 PyTuple_SET_ITEM(tup, 1, wxPyMakeSwigPtr(cookie, wxT("void")));
502 wxPyEndBlockThreads(blocked);
507 PyObject* GetLastChild(const wxTreeItemId& item) {
509 wxTreeItemId* ritem = new wxTreeItemId(self->GetLastChild(item, cookie));
510 wxPyBlock_t blocked = wxPyBeginBlockThreads();
511 PyObject* tup = PyTuple_New(2);
512 PyTuple_SET_ITEM(tup, 0, wxPyConstructObject(ritem, wxT("wxTreeItemId"), true));
513 PyTuple_SET_ITEM(tup, 1, wxPyMakeSwigPtr(cookie, wxT("void")));
514 wxPyEndBlockThreads(blocked);
519 PyObject* GetPrevChild(const wxTreeItemId& item, void* cookie) {
520 wxTreeItemId* ritem = new wxTreeItemId(self->GetPrevChild(item, cookie));
521 wxPyBlock_t blocked = wxPyBeginBlockThreads();
522 PyObject* tup = PyTuple_New(2);
523 PyTuple_SET_ITEM(tup, 0, wxPyConstructObject(ritem, wxT("wxTreeItemId"), true));
524 PyTuple_SET_ITEM(tup, 1, wxPyMakeSwigPtr(cookie, wxT("void")));
525 wxPyEndBlockThreads(blocked);
531 // get the next sibling of this item
532 wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
534 // get the previous sibling
535 wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
538 // get item in the full tree (currently only for internal use)
539 wxTreeItemId GetNext(const wxTreeItemId& item) const;
540 wxTreeItemId GetPrev(const wxTreeItemId& item) const;
542 // get expanded item, see IsExpanded()
543 wxTreeItemId GetFirstExpandedItem() const;
544 wxTreeItemId GetNextExpanded(const wxTreeItemId& item) const;
545 wxTreeItemId GetPrevExpanded(const wxTreeItemId& item) const;
547 // get visible item, see IsVisible()
548 wxTreeItemId GetFirstVisibleItem(bool fullRow = false) const;
549 wxTreeItemId GetNextVisible(const wxTreeItemId& item, bool fullRow = false) const;
550 wxTreeItemId GetPrevVisible(const wxTreeItemId& item, bool fullRow = false) const;
554 %disownarg( wxPyTreeItemData* data );
556 // add the root node to the tree
557 wxTreeItemId AddRoot(const wxString& text,
558 int image = -1, int selectedImage = -1,
559 wxPyTreeItemData *data = NULL);
561 // insert a new item in as the first child of the parent
562 wxTreeItemId PrependItem(const wxTreeItemId& parent,
563 const wxString& text,
564 int image = -1, int selectedImage = -1,
565 wxPyTreeItemData *data = NULL);
567 // insert a new item after a given one
568 wxTreeItemId InsertItem(const wxTreeItemId& parent,
569 const wxTreeItemId& idPrevious,
570 const wxString& text,
571 int image = -1, int selectedImage = -1,
572 wxPyTreeItemData *data = NULL);
574 // insert a new item before the one with the given index
575 %Rename(InsertItemBefore,
576 wxTreeItemId, InsertItem(const wxTreeItemId& parent,
578 const wxString& text,
579 int image = -1, int selectedImage = -1,
580 wxPyTreeItemData *data = NULL));
582 // insert a new item in as the last child of the parent
583 wxTreeItemId AppendItem(const wxTreeItemId& parent,
584 const wxString& text,
585 int image = -1, int selectedImage = -1,
586 wxPyTreeItemData *data = NULL);
588 %cleardisown(wxPyTreeItemData* data );
590 // delete this item and associated data if any
591 void Delete(const wxTreeItemId& item);
593 // delete all children (but don't delete the item itself)
594 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
595 void DeleteChildren(const wxTreeItemId& item);
597 // delete all items from the tree
598 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
600 %pythoncode { DeleteAllItems = DeleteRoot }
605 void Expand(const wxTreeItemId& item);
607 // expand this item and all subitems recursively
608 void ExpandAll(const wxTreeItemId& item);
610 // collapse the item without removing its children
611 void Collapse(const wxTreeItemId& item);
613 // collapse the item and remove all children
614 void CollapseAndReset(const wxTreeItemId& item);
616 // toggles the current state
617 void Toggle(const wxTreeItemId& item);
619 // remove the selection from currently selected item (if any)
624 void SelectItem(const wxTreeItemId& item,
625 const wxTreeItemId& last = (wxTreeItemId*)NULL,
626 bool unselect_others=true);
630 // make sure this item is visible (expanding the parent item and/or
631 // scrolling to this item if necessary)
632 void EnsureVisible(const wxTreeItemId& item);
634 // scroll to this item (but don't expand its parent)
635 void ScrollTo(const wxTreeItemId& item);
637 // Returns wxTreeItemId, flags, and column
638 wxTreeItemId HitTest(const wxPoint& point, int& OUTPUT, int& OUTPUT);
641 // get the bounding rectangle of the item (or of its label only)
642 PyObject* GetBoundingRect(const wxTreeItemId& item, bool textOnly = false) {
644 if (self->GetBoundingRect(item, rect, textOnly)) {
645 wxPyBlock_t blocked = wxPyBeginBlockThreads();
646 wxRect* r = new wxRect(rect);
647 PyObject* val = wxPyConstructObject((void*)r, wxT("wxRect"), 1);
648 wxPyEndBlockThreads(blocked);
659 // Start editing the item label: this (temporarily) replaces the item
660 // with a one line edit control. The item will be selected if it hadn't
662 void EditLabel(const wxTreeItemId& item, int column = -1) {
663 if (column < 0) column = self->GetMainColumn();
664 self->EditLabel(item, column);
667 %pythoncode { Edit = EditLabel }
669 // sort the children of this item using OnCompareItems
670 void SortChildren(const wxTreeItemId& item);
673 wxTreeItemId FindItem (const wxTreeItemId& item, const wxString& str, int flags = 0);
676 void SetDragItem (const wxTreeItemId& item = (wxTreeItemId*)NULL);
678 wxWindow* GetHeaderWindow() const;
679 wxScrolledWindow* GetMainWindow() const;
683 //----------------------------------------------------------------------
686 wxPyPtrTypeMap_Add("wxTreeListCtrl", "wxPyTreeListCtrl");
689 //----------------------------------------------------------------------