1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: More control (widget) classes for wxPython
9 // Copyright: (c) 1998 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
20 #include <wx/listctrl.h>
21 #include <wx/treectrl.h>
22 #include <wx/imaglist.h>
25 //----------------------------------------------------------------------
28 %include my_typemaps.i
30 // Import some definitions of other classes, etc.
38 %pragma(python) code = "import wx"
40 //----------------------------------------------------------------------
50 wxLIST_STATE_DONTCARE,
51 wxLIST_STATE_DROPHILITED,
53 wxLIST_STATE_SELECTED,
57 wxLIST_HITTEST_NOWHERE,
58 wxLIST_HITTEST_ONITEMICON,
59 wxLIST_HITTEST_ONITEMLABEL,
60 wxLIST_HITTEST_ONITEMRIGHT,
61 wxLIST_HITTEST_ONITEMSTATEICON,
62 wxLIST_HITTEST_TOLEFT,
63 wxLIST_HITTEST_TORIGHT,
64 wxLIST_HITTEST_ONITEM,
73 wxLIST_ALIGN_SNAP_TO_GRID,
75 wxLIST_AUTOSIZE_USEHEADER,
86 enum wxListColumnFormat
91 wxLIST_FORMAT_CENTER = wxLIST_FORMAT_CENTRE
96 /* List control event types */
97 wxEVT_COMMAND_LIST_BEGIN_DRAG,
98 wxEVT_COMMAND_LIST_BEGIN_RDRAG,
99 wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT,
100 wxEVT_COMMAND_LIST_END_LABEL_EDIT,
101 wxEVT_COMMAND_LIST_DELETE_ITEM,
102 wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS,
103 wxEVT_COMMAND_LIST_GET_INFO,
104 wxEVT_COMMAND_LIST_SET_INFO,
105 wxEVT_COMMAND_LIST_ITEM_SELECTED,
106 wxEVT_COMMAND_LIST_ITEM_DESELECTED,
107 wxEVT_COMMAND_LIST_KEY_DOWN,
108 wxEVT_COMMAND_LIST_INSERT_ITEM,
109 wxEVT_COMMAND_LIST_COL_CLICK,
110 wxEVT_COMMAND_LIST_ITEM_ACTIVATED,
111 wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK,
112 wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK,
130 wxLC_SORT_DESCENDING,
146 //wxListItemAttr(const wxColour& colText,
147 // const wxColour& colBack,
148 // const wxFont& font)
149 // : m_colText(colText), m_colBack(colBack), m_font(font) { }
152 void SetTextColour(const wxColour& colText);
153 void SetBackgroundColour(const wxColour& colBack);
154 void SetFont(const wxFont& font);
157 bool HasTextColour();
158 bool HasBackgroundColour();
161 const wxColour& GetTextColour();
162 const wxColour& GetBackgroundColour();
163 const wxFont& GetFont();
167 class wxListItem : public wxObject {
174 void ClearAttributes();
177 void SetMask(long mask);
179 void SetColumn(int col);
180 void SetState(long state);
181 void SetStateMask(long stateMask);
182 void SetText(const wxString& text);
183 void SetImage(int image);
184 void SetData(long data);
186 void SetWidth(int width);
187 void SetAlign(wxListColumnFormat align);
189 void SetTextColour(const wxColour& colText);
190 void SetBackgroundColour(const wxColour& colBack);
191 void SetFont(const wxFont& font);
198 const wxString& GetText();
203 wxListColumnFormat GetAlign();
205 wxListItemAttr *GetAttributes();
206 bool HasAttributes();
208 wxColour GetTextColour() const;
209 wxColour GetBackgroundColour() const;
210 wxFont GetFont() const;
212 // these members are public for compatibility
213 long m_mask; // Indicates what fields are valid
214 long m_itemId; // The zero-based item position
215 int m_col; // Zero-based column, if in report mode
216 long m_state; // The state of the item
217 long m_stateMask;// Which flags of m_state are valid (uses same flags)
218 wxString m_text; // The label/header text
219 int m_image; // The zero-based index into an image list
220 long m_data; // App-defined data
223 int m_format; // left, right, centre
224 int m_width; // width of column
229 class wxListEvent: public wxNotifyEvent {
246 const wxString& GetLabel();
247 const wxString& GetText();
251 const wxListItem& GetItem();
256 class wxListCtrl : public wxControl {
258 wxListCtrl(wxWindow* parent, wxWindowID id,
259 const wxPoint& pos = wxDefaultPosition,
260 const wxSize& size = wxDefaultSize,
261 long style = wxLC_ICON,
262 const wxValidator& validator = wxDefaultValidator,
263 char* name = "listCtrl");
265 %pragma(python) addtomethod = "__init__:#wx._StdWindowCallbacks(self)"
267 bool Arrange(int flag = wxLIST_ALIGN_DEFAULT);
268 void AssignImageList(wxImageList* imageList, int which);
269 %pragma(python) addtomethod = "AssignImageList:_args[0].thisown = 0"
270 bool DeleteItem(long item);
271 bool DeleteAllItems();
272 bool DeleteColumn(int col);
273 bool DeleteAllColumns(void);
276 wxTextCtrl* EditLabel(long item);
277 bool EndEditLabel(bool cancel);
278 wxTextCtrl* GetEditControl();
280 void EditLabel(long item);
282 bool EnsureVisible(long item);
283 long FindItem(long start, const wxString& str, bool partial = FALSE);
284 %name(FindItemData)long FindItem(long start, long data);
285 %name(FindItemAtPos)long FindItem(long start, const wxPoint& pt,
287 bool GetColumn(int col, wxListItem& item);
288 int GetColumnWidth(int col);
289 int GetCountPerPage();
290 wxImageList* GetImageList(int which);
291 long GetItemData(long item);
294 %new wxListItem* GetItem(long itemId, int col=0) {
295 wxListItem* info = new wxListItem;
296 info->m_itemId = itemId;
298 info->m_mask = 0xFFFF;
299 self->GetItem(*info);
302 } // The OOR typemaps don't know what to do with the %new, so fix it up.
303 %pragma(python) addtoclass = "
304 def GetItem(self, *_args, **_kwargs):
305 val = apply(controls2c.wxListCtrl_GetItem,(self,) + _args, _kwargs)
311 %new wxPoint* GetItemPosition(long item) {
312 wxPoint* pos = new wxPoint;
313 self->GetItemPosition(item, *pos);
316 %new wxRect* GetItemRect(long item, int code = wxLIST_RECT_BOUNDS) {
317 wxRect* rect= new wxRect;
318 self->GetItemRect(item, *rect, code);
323 int GetItemState(long item, long stateMask);
325 int GetItemSpacing(bool isSmall);
326 wxString GetItemText(long item);
327 long GetNextItem(long item,
328 int geometry = wxLIST_NEXT_ALL,
329 int state = wxLIST_STATE_DONTCARE);
330 int GetSelectedItemCount();
332 wxColour GetTextColour();
333 void SetTextColour(const wxColour& col);
336 long HitTest(const wxPoint& point, int& OUTPUT);
337 %name(InsertColumnInfo)long InsertColumn(long col, wxListItem& info);
338 long InsertColumn(long col, const wxString& heading,
339 int format = wxLIST_FORMAT_LEFT,
342 long InsertItem(wxListItem& info);
343 %name(InsertStringItem) long InsertItem(long index, const wxString& label);
344 %name(InsertImageItem) long InsertItem(long index, int imageIndex);
345 %name(InsertImageStringItem)long InsertItem(long index, const wxString& label,
348 bool ScrollList(int dx, int dy);
349 void SetBackgroundColour(const wxColour& col);
350 bool SetColumn(int col, wxListItem& item);
351 bool SetColumnWidth(int col, int width);
352 void SetImageList(wxImageList* imageList, int which);
354 bool SetItem(wxListItem& info);
355 %name(SetStringItem)long SetItem(long index, int col, const wxString& label,
358 bool SetItemData(long item, long data);
359 bool SetItemImage(long item, int image, int selImage);
360 bool SetItemPosition(long item, const wxPoint& pos);
361 bool SetItemState(long item, long state, long stateMask);
362 void SetItemText(long item, const wxString& text);
363 void SetSingleStyle(long style, bool add = TRUE);
364 void SetWindowStyleFlag(long style);
366 // bool SortItems(wxListCtrlCompare fn, long data);
368 bool SortItems(PyObject* func) {
369 if (!PyCallable_Check(func))
372 return self->SortItems(wxPyListCtrl_SortItems, (long)func);
378 int wxCALLBACK wxPyListCtrl_SortItems(long item1, long item2, long funcPtr) {
380 PyObject* func = (PyObject*)funcPtr;
381 bool doSave = wxPyRestoreThread();
383 PyObject* args = Py_BuildValue("(ii)", item1, item2);
384 PyObject* result = PyEval_CallObject(func, args);
387 retval = PyInt_AsLong(result);
391 wxPySaveThread(doSave);
397 //----------------------------------------------------------------------
401 wxTreeItemIcon_Normal, // not selected, not expanded
402 wxTreeItemIcon_Selected, // selected, not expanded
403 wxTreeItemIcon_Expanded, // not selected, expanded
404 wxTreeItemIcon_SelectedExpanded, // selected, expanded
409 // constants for HitTest
411 wxTREE_HITTEST_ABOVE,
412 wxTREE_HITTEST_BELOW,
413 wxTREE_HITTEST_NOWHERE,
414 wxTREE_HITTEST_ONITEMBUTTON,
415 wxTREE_HITTEST_ONITEMICON,
416 wxTREE_HITTEST_ONITEMINDENT,
417 wxTREE_HITTEST_ONITEMLABEL,
418 wxTREE_HITTEST_ONITEMRIGHT,
419 wxTREE_HITTEST_ONITEMSTATEICON,
420 wxTREE_HITTEST_TOLEFT,
421 wxTREE_HITTEST_TORIGHT,
422 wxTREE_HITTEST_ONITEMUPPERPART,
423 wxTREE_HITTEST_ONITEMLOWERPART,
424 wxTREE_HITTEST_ONITEM
429 /* Tree control event types */
430 wxEVT_COMMAND_TREE_BEGIN_DRAG,
431 wxEVT_COMMAND_TREE_BEGIN_RDRAG,
432 wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT,
433 wxEVT_COMMAND_TREE_END_LABEL_EDIT,
434 wxEVT_COMMAND_TREE_DELETE_ITEM,
435 wxEVT_COMMAND_TREE_GET_INFO,
436 wxEVT_COMMAND_TREE_SET_INFO,
437 wxEVT_COMMAND_TREE_ITEM_EXPANDED,
438 wxEVT_COMMAND_TREE_ITEM_EXPANDING,
439 wxEVT_COMMAND_TREE_ITEM_COLLAPSED,
440 wxEVT_COMMAND_TREE_ITEM_COLLAPSING,
441 wxEVT_COMMAND_TREE_SEL_CHANGED,
442 wxEVT_COMMAND_TREE_SEL_CHANGING,
443 wxEVT_COMMAND_TREE_KEY_DOWN,
444 wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
445 wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK,
446 wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK,
447 wxEVT_COMMAND_TREE_END_DRAG,
458 int __cmp__(wxTreeItemId* other) {
459 if (! other) return -1;
460 return *self != *other;
468 class wxPyTreeItemData : public wxTreeItemData {
470 wxPyTreeItemData(PyObject* obj = NULL) {
477 ~wxPyTreeItemData() {
478 bool doSave = wxPyRestoreThread();
480 wxPySaveThread(doSave);
483 PyObject* GetData() {
488 void SetData(PyObject* obj) {
489 bool doSave = wxPyRestoreThread();
491 wxPySaveThread(doSave);
502 %name(wxTreeItemData) class wxPyTreeItemData : public wxObject {
504 wxPyTreeItemData(PyObject* obj = NULL);
507 void SetData(PyObject* obj);
509 const wxTreeItemId& GetId();
510 void SetId(const wxTreeItemId& id);
515 class wxTreeEvent : public wxNotifyEvent {
517 wxTreeItemId GetItem();
518 wxTreeItemId GetOldItem();
521 const wxString& GetLabel();
527 class wxPyTreeCtrl : public wxTreeCtrl {
528 DECLARE_ABSTRACT_CLASS(wxPyTreeCtrl);
530 wxPyTreeCtrl(wxWindow *parent, wxWindowID id,
534 const wxValidator& validator,
536 wxTreeCtrl(parent, id, pos, size, style, validator, name) {}
538 int OnCompareItems(const wxTreeItemId& item1,
539 const wxTreeItemId& item2) {
541 bool doSave = wxPyRestoreThread();
542 if (m_myInst.findCallback("OnCompareItems"))
543 rval = m_myInst.callCallback(Py_BuildValue(
545 wxPyConstructObject((void*)&item1, "wxTreeItemId"),
546 wxPyConstructObject((void*)&item2, "wxTreeItemId")));
548 rval = wxTreeCtrl::OnCompareItems(item1, item2);
549 wxPySaveThread(doSave);
555 IMPLEMENT_ABSTRACT_CLASS(wxPyTreeCtrl, wxTreeCtrl);
559 // These are for the GetFirstChild/GetNextChild methods below
560 %typemap(python, in) long& INOUT = long* INOUT;
561 %typemap(python, argout) long& INOUT = long* INOUT;
564 %name(wxTreeCtrl)class wxPyTreeCtrl : public wxControl {
566 wxPyTreeCtrl(wxWindow *parent, wxWindowID id = -1,
567 const wxPoint& pos = wxDefaultPosition,
568 const wxSize& size = wxDefaultSize,
569 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
570 const wxValidator& validator = wxDefaultValidator,
571 char* name = "wxTreeCtrl");
573 void _setSelf(PyObject* self, PyObject* _class);
574 %pragma(python) addtomethod = "__init__:#wx._StdWindowCallbacks(self)"
575 %pragma(python) addtomethod = "__init__:self._setSelf(self, wxTreeCtrl)"
577 void AssignImageList(wxImageList* imageList);
578 %pragma(python) addtomethod = "AssignImageList:_args[0].thisown = 0"
580 unsigned int GetIndent();
581 void SetIndent(unsigned int indent);
582 wxImageList *GetImageList();
583 wxImageList *GetStateImageList();
584 void SetImageList(wxImageList *imageList);
585 void SetStateImageList(wxImageList *imageList);
587 unsigned int GetSpacing();
588 void SetSpacing(unsigned int spacing);
590 wxString GetItemText(const wxTreeItemId& item);
591 int GetItemImage(const wxTreeItemId& item,
592 wxTreeItemIcon which = wxTreeItemIcon_Normal);
593 int GetItemSelectedImage(const wxTreeItemId& item);
595 void SetItemText(const wxTreeItemId& item, const wxString& text);
596 void SetItemImage(const wxTreeItemId& item, int image,
597 wxTreeItemIcon which = wxTreeItemIcon_Normal);
598 void SetItemSelectedImage(const wxTreeItemId& item, int image);
599 void SetItemHasChildren(const wxTreeItemId& item, bool hasChildren = TRUE);
602 // [Get|Set]ItemData substitutes. Automatically create wxPyTreeItemData
604 wxPyTreeItemData* GetItemData(const wxTreeItemId& item) {
605 wxPyTreeItemData* data = (wxPyTreeItemData*)self->GetItemData(item);
607 data = new wxPyTreeItemData();
608 data->SetId(item); // set the id
609 self->SetItemData(item, data);
614 void SetItemData(const wxTreeItemId& item, wxPyTreeItemData* data) {
615 data->SetId(item); // set the id
616 self->SetItemData(item, data);
619 // [Get|Set]PyData are short-cuts. Also made somewhat crash-proof by
620 // automatically creating data classes.
621 PyObject* GetPyData(const wxTreeItemId& item) {
622 wxPyTreeItemData* data = (wxPyTreeItemData*)self->GetItemData(item);
624 data = new wxPyTreeItemData();
625 data->SetId(item); // set the id
626 self->SetItemData(item, data);
628 return data->GetData();
631 void SetPyData(const wxTreeItemId& item, PyObject* obj) {
632 wxPyTreeItemData* data = (wxPyTreeItemData*)self->GetItemData(item);
634 data = new wxPyTreeItemData(obj);
635 data->SetId(item); // set the id
636 self->SetItemData(item, data);
643 bool IsVisible(const wxTreeItemId& item);
644 bool ItemHasChildren(const wxTreeItemId& item);
645 bool IsExpanded(const wxTreeItemId& item);
646 bool IsSelected(const wxTreeItemId& item);
648 wxTreeItemId GetRootItem();
649 wxTreeItemId GetSelection();
650 %name(GetItemParent) wxTreeItemId GetParent(const wxTreeItemId& item);
651 //size_t GetSelections(wxArrayTreeItemIds& selection);
653 PyObject* GetSelections() {
654 bool doSave = wxPyRestoreThread();
655 PyObject* rval = PyList_New(0);
656 wxArrayTreeItemIds array;
658 num = self->GetSelections(array);
659 for (x=0; x < num; x++) {
660 wxTreeItemId *tii = new wxTreeItemId(array.Item(x));
661 PyObject* item = wxPyConstructObject((void*)tii, "wxTreeItemId", TRUE);
662 PyList_Append(rval, item);
664 wxPySaveThread(doSave);
671 size_t GetChildrenCount(const wxTreeItemId& item, bool recursively = TRUE);
673 wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& INOUT);
674 wxTreeItemId GetNextChild(const wxTreeItemId& item, long& INOUT);
675 wxTreeItemId GetNextSibling(const wxTreeItemId& item);
676 wxTreeItemId GetPrevSibling(const wxTreeItemId& item);
677 wxTreeItemId GetFirstVisibleItem();
678 wxTreeItemId GetNextVisible(const wxTreeItemId& item);
679 wxTreeItemId GetPrevVisible(const wxTreeItemId& item);
680 wxTreeItemId GetLastChild(const wxTreeItemId& item);
684 wxTreeItemId AddRoot(const wxString& text,
685 int image = -1, int selectedImage = -1,
686 wxPyTreeItemData *data = NULL);
687 wxTreeItemId PrependItem(const wxTreeItemId& parent,
688 const wxString& text,
689 int image = -1, int selectedImage = -1,
690 wxPyTreeItemData *data = NULL);
691 wxTreeItemId InsertItem(const wxTreeItemId& parent,
692 const wxTreeItemId& idPrevious,
693 const wxString& text,
694 int image = -1, int selectedImage = -1,
695 wxPyTreeItemData *data = NULL);
696 %name(InsertItemBefore)
697 wxTreeItemId InsertItem(const wxTreeItemId& parent,
699 const wxString& text,
700 int image = -1, int selectedImage = -1,
701 wxTreeItemData *data = NULL);
702 wxTreeItemId AppendItem(const wxTreeItemId& parent,
703 const wxString& text,
704 int image = -1, int selectedImage = -1,
705 wxPyTreeItemData *data = NULL);
707 void Delete(const wxTreeItemId& item);
708 void DeleteChildren(const wxTreeItemId& item);
709 void DeleteAllItems();
711 void Expand(const wxTreeItemId& item);
712 void Collapse(const wxTreeItemId& item);
713 void CollapseAndReset(const wxTreeItemId& item);
714 void Toggle(const wxTreeItemId& item);
718 void SelectItem(const wxTreeItemId& item);
719 void EnsureVisible(const wxTreeItemId& item);
720 void ScrollTo(const wxTreeItemId& item);
722 wxTextCtrl* EditLabel(const wxTreeItemId& item);
723 wxTextCtrl* GetEditControl();
724 void EndEditLabel(const wxTreeItemId& item, int discardChanges = FALSE);
726 void EditLabel(const wxTreeItemId& item);
729 void SortChildren(const wxTreeItemId& item);
731 void SetItemBold(const wxTreeItemId& item, int bold = TRUE);
732 bool IsBold(const wxTreeItemId& item) const;
733 wxTreeItemId HitTest(const wxPoint& point, int& OUTPUT);
737 void SetItemTextColour(const wxTreeItemId& item, const wxColour& col);
738 void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& col);
739 void SetItemFont(const wxTreeItemId& item, const wxFont& font);
742 void SetItemDropHighlight(const wxTreeItemId& item, int highlight = TRUE);
744 //bool GetBoundingRect(const wxTreeItemId& item, wxRect& rect, int textOnly = FALSE)
746 PyObject* GetBoundingRect(const wxTreeItemId& item, int textOnly = FALSE) {
748 if (self->GetBoundingRect(item, rect, textOnly)) {
749 bool doSave = wxPyRestoreThread();
750 wxRect* r = new wxRect(rect);
751 PyObject* val = wxPyConstructObject((void*)r, "wxRect");
752 wxPySaveThread(doSave);
763 %pragma(python) addtoclass = "
764 # Redefine some methods that SWIG gets a bit confused on...
765 def GetFirstChild(self, *_args, **_kwargs):
766 val1,val2 = apply(controls2c.wxTreeCtrl_GetFirstChild,(self,) + _args, _kwargs)
767 val1 = wxTreeItemIdPtr(val1)
770 def GetNextChild(self, *_args, **_kwargs):
771 val1,val2 = apply(controls2c.wxTreeCtrl_GetNextChild,(self,) + _args, _kwargs)
772 val1 = wxTreeItemIdPtr(val1)
775 def HitTest(self, *_args, **_kwargs):
776 val1, val2 = apply(controls2c.wxTreeCtrl_HitTest,(self,) + _args, _kwargs)
777 val1 = wxTreeItemIdPtr(val1)
784 //----------------------------------------------------------------------
791 /* tab control event types */
792 wxEVT_COMMAND_TAB_SEL_CHANGED,
793 wxEVT_COMMAND_TAB_SEL_CHANGING,
797 class wxTabEvent : public wxCommandEvent {
803 class wxTabCtrl : public wxControl {
805 wxTabCtrl(wxWindow* parent, wxWindowID id,
806 const wxPoint& pos = wxDefaultPosition,
807 const wxSize& size = wxDefaultSize,
809 char* name = "tabCtrl");
811 %pragma(python) addtomethod = "__init__:#wx._StdWindowCallbacks(self)"
813 bool DeleteAllItems();
814 bool DeleteItem(int item);
815 wxImageList* GetImageList();
817 // TODO: void* GetItemData();
818 int GetItemImage(int item);
821 %new wxRect* GetItemRect(int item) {
822 wxRect* rect = new wxRect;
823 self->GetItemRect(item, *rect);
828 wxString GetItemText(int item);
831 int HitTest(const wxPoint& pt, long& OUTPUT);
832 void InsertItem(int item, const wxString& text,
833 int imageId = -1, void* clientData = NULL);
834 // TODO: bool SetItemData(int item, void* data);
835 bool SetItemImage(int item, int image);
836 void SetImageList(wxImageList* imageList);
837 void SetItemSize(const wxSize& size);
838 bool SetItemText(int item, const wxString& text);
839 void SetPadding(const wxSize& padding);
840 int SetSelection(int item);
847 //----------------------------------------------------------------------
850 wxPyPtrTypeMap_Add("wxTreeItemData", "wxPyTreeItemData");
851 wxPyPtrTypeMap_Add("wxTreeCtrl", "wxPyTreeCtrl");
854 //----------------------------------------------------------------------