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>
24 //----------------------------------------------------------------------
27 %include my_typemaps.i
29 // Import some definitions of other classes, etc.
37 %pragma(python) code = "import wx"
39 //----------------------------------------------------------------------
49 wxLIST_STATE_DONTCARE,
50 wxLIST_STATE_DROPHILITED,
52 wxLIST_STATE_SELECTED,
56 wxLIST_HITTEST_NOWHERE,
57 wxLIST_HITTEST_ONITEMICON,
58 wxLIST_HITTEST_ONITEMLABEL,
59 wxLIST_HITTEST_ONITEMRIGHT,
60 wxLIST_HITTEST_ONITEMSTATEICON,
61 wxLIST_HITTEST_TOLEFT,
62 wxLIST_HITTEST_TORIGHT,
63 wxLIST_HITTEST_ONITEM,
72 wxLIST_ALIGN_SNAP_TO_GRID,
74 wxLIST_AUTOSIZE_USEHEADER,
85 enum wxListColumnFormat
90 wxLIST_FORMAT_CENTER = wxLIST_FORMAT_CENTRE
95 /* List control event types */
96 wxEVT_COMMAND_LIST_BEGIN_DRAG,
97 wxEVT_COMMAND_LIST_BEGIN_RDRAG,
98 wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT,
99 wxEVT_COMMAND_LIST_END_LABEL_EDIT,
100 wxEVT_COMMAND_LIST_DELETE_ITEM,
101 wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS,
102 wxEVT_COMMAND_LIST_GET_INFO,
103 wxEVT_COMMAND_LIST_SET_INFO,
104 wxEVT_COMMAND_LIST_ITEM_SELECTED,
105 wxEVT_COMMAND_LIST_ITEM_DESELECTED,
106 wxEVT_COMMAND_LIST_KEY_DOWN,
107 wxEVT_COMMAND_LIST_INSERT_ITEM,
108 wxEVT_COMMAND_LIST_COL_CLICK,
109 wxEVT_COMMAND_LIST_ITEM_ACTIVATED,
110 wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK,
111 wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK,
121 //wxListItemAttr(const wxColour& colText,
122 // const wxColour& colBack,
123 // const wxFont& font)
124 // : m_colText(colText), m_colBack(colBack), m_font(font) { }
127 void SetTextColour(const wxColour& colText);
128 void SetBackgroundColour(const wxColour& colBack);
129 void SetFont(const wxFont& font);
132 bool HasTextColour();
133 bool HasBackgroundColour();
136 const wxColour& GetTextColour();
137 const wxColour& GetBackgroundColour();
138 const wxFont& GetFont();
149 void ClearAttributes();
152 void SetMask(long mask);
154 void SetColumn(int col);
155 void SetState(long state);
156 void SetStateMask(long stateMask);
157 void SetText(const wxString& text);
158 void SetImage(int image);
159 void SetData(long data);
161 void SetWidth(int width);
162 void SetAlign(wxListColumnFormat align);
164 void SetTextColour(const wxColour& colText);
165 void SetBackgroundColour(const wxColour& colBack);
166 void SetFont(const wxFont& font);
173 const wxString& GetText();
178 wxListColumnFormat GetAlign();
180 wxListItemAttr *GetAttributes();
181 bool HasAttributes();
183 wxColour GetTextColour() const;
184 wxColour GetBackgroundColour() const;
185 wxFont GetFont() const;
187 // these members are public for compatibility
188 long m_mask; // Indicates what fields are valid
189 long m_itemId; // The zero-based item position
190 int m_col; // Zero-based column, if in report mode
191 long m_state; // The state of the item
192 long m_stateMask;// Which flags of m_state are valid (uses same flags)
193 wxString m_text; // The label/header text
194 int m_image; // The zero-based index into an image list
195 long m_data; // App-defined data
198 int m_format; // left, right, centre
199 int m_width; // width of column
204 class wxListEvent: public wxNotifyEvent {
221 const wxString& GetLabel();
222 const wxString& GetText();
226 const wxListItem& GetItem();
231 class wxListCtrl : public wxControl {
233 wxListCtrl(wxWindow* parent, wxWindowID id,
234 const wxPoint& pos = wxDefaultPosition,
235 const wxSize& size = wxDefaultSize,
236 long style = wxLC_ICON,
237 const wxValidator& validator = wxDefaultValidator,
238 char* name = "listCtrl");
240 %pragma(python) addtomethod = "__init__:#wx._StdWindowCallbacks(self)"
242 bool Arrange(int flag = wxLIST_ALIGN_DEFAULT);
243 void AssignImageList(wxImageList* imageList, int which);
244 bool DeleteItem(long item);
245 bool DeleteAllItems();
246 bool DeleteColumn(int col);
247 bool DeleteAllColumns(void);
250 wxTextCtrl* EditLabel(long item);
251 bool EndEditLabel(bool cancel);
252 wxTextCtrl* GetEditControl();
254 void EditLabel(long item);
256 bool EnsureVisible(long item);
257 long FindItem(long start, const wxString& str, bool partial = FALSE);
258 %name(FindItemData)long FindItem(long start, long data);
259 %name(FindItemAtPos)long FindItem(long start, const wxPoint& pt,
261 bool GetColumn(int col, wxListItem& item);
262 int GetColumnWidth(int col);
263 int GetCountPerPage();
264 wxImageList* GetImageList(int which);
265 long GetItemData(long item);
268 %new wxListItem* GetItem(long itemId, int col=0) {
269 wxListItem* info = new wxListItem;
270 info->m_itemId = itemId;
272 info->m_mask = 0xFFFF;
273 self->GetItem(*info);
276 %new wxPoint* GetItemPosition(long item) {
277 wxPoint* pos = new wxPoint;
278 self->GetItemPosition(item, *pos);
281 %new wxRect* GetItemRect(long item, int code = wxLIST_RECT_BOUNDS) {
282 wxRect* rect= new wxRect;
283 self->GetItemRect(item, *rect, code);
288 int GetItemState(long item, long stateMask);
290 int GetItemSpacing(bool isSmall);
291 wxString GetItemText(long item);
292 long GetNextItem(long item,
293 int geometry = wxLIST_NEXT_ALL,
294 int state = wxLIST_STATE_DONTCARE);
295 int GetSelectedItemCount();
297 wxColour GetTextColour();
298 void SetTextColour(const wxColour& col);
301 long HitTest(const wxPoint& point, int& OUTPUT);
302 %name(InsertColumnInfo)long InsertColumn(long col, wxListItem& info);
303 long InsertColumn(long col, const wxString& heading,
304 int format = wxLIST_FORMAT_LEFT,
307 long InsertItem(wxListItem& info);
308 %name(InsertStringItem) long InsertItem(long index, const wxString& label);
309 %name(InsertImageItem) long InsertItem(long index, int imageIndex);
310 %name(InsertImageStringItem)long InsertItem(long index, const wxString& label,
313 bool ScrollList(int dx, int dy);
314 void SetBackgroundColour(const wxColour& col);
315 bool SetColumn(int col, wxListItem& item);
316 bool SetColumnWidth(int col, int width);
317 void SetImageList(wxImageList* imageList, int which);
319 bool SetItem(wxListItem& info);
320 %name(SetStringItem)long SetItem(long index, int col, const wxString& label,
323 bool SetItemData(long item, long data);
324 bool SetItemImage(long item, int image, int selImage);
325 bool SetItemPosition(long item, const wxPoint& pos);
326 bool SetItemState(long item, long state, long stateMask);
327 void SetItemText(long item, const wxString& text);
328 void SetSingleStyle(long style, bool add = TRUE);
329 void SetWindowStyleFlag(long style);
331 // bool SortItems(wxListCtrlCompare fn, long data);
333 bool SortItems(PyObject* func) {
334 if (!PyCallable_Check(func))
337 return self->SortItems(wxPyListCtrl_SortItems, (long)func);
343 int wxCALLBACK wxPyListCtrl_SortItems(long item1, long item2, long funcPtr) {
345 PyObject* func = (PyObject*)funcPtr;
346 bool doSave = wxPyRestoreThread();
348 PyObject* args = Py_BuildValue("(ii)", item1, item2);
349 PyObject* result = PyEval_CallObject(func, args);
352 retval = PyInt_AsLong(result);
356 wxPySaveThread(doSave);
362 //----------------------------------------------------------------------
366 wxTreeItemIcon_Normal, // not selected, not expanded
367 wxTreeItemIcon_Selected, // selected, not expanded
368 wxTreeItemIcon_Expanded, // not selected, expanded
369 wxTreeItemIcon_SelectedExpanded, // selected, expanded
374 // constants for HitTest
376 wxTREE_HITTEST_ABOVE,
377 wxTREE_HITTEST_BELOW,
378 wxTREE_HITTEST_NOWHERE,
379 wxTREE_HITTEST_ONITEMBUTTON,
380 wxTREE_HITTEST_ONITEMICON,
381 wxTREE_HITTEST_ONITEMINDENT,
382 wxTREE_HITTEST_ONITEMLABEL,
383 wxTREE_HITTEST_ONITEMRIGHT,
384 wxTREE_HITTEST_ONITEMSTATEICON,
385 wxTREE_HITTEST_TOLEFT,
386 wxTREE_HITTEST_TORIGHT,
387 wxTREE_HITTEST_ONITEMUPPERPART,
388 wxTREE_HITTEST_ONITEMLOWERPART,
389 wxTREE_HITTEST_ONITEM
394 /* Tree control event types */
395 wxEVT_COMMAND_TREE_BEGIN_DRAG,
396 wxEVT_COMMAND_TREE_BEGIN_RDRAG,
397 wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT,
398 wxEVT_COMMAND_TREE_END_LABEL_EDIT,
399 wxEVT_COMMAND_TREE_DELETE_ITEM,
400 wxEVT_COMMAND_TREE_GET_INFO,
401 wxEVT_COMMAND_TREE_SET_INFO,
402 wxEVT_COMMAND_TREE_ITEM_EXPANDED,
403 wxEVT_COMMAND_TREE_ITEM_EXPANDING,
404 wxEVT_COMMAND_TREE_ITEM_COLLAPSED,
405 wxEVT_COMMAND_TREE_ITEM_COLLAPSING,
406 wxEVT_COMMAND_TREE_SEL_CHANGED,
407 wxEVT_COMMAND_TREE_SEL_CHANGING,
408 wxEVT_COMMAND_TREE_KEY_DOWN,
409 wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
410 wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK,
411 wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK,
412 wxEVT_COMMAND_TREE_END_DRAG,
423 int __cmp__(wxTreeItemId* other) {
424 if (! other) return -1;
425 return *self != *other;
433 class wxPyTreeItemData : public wxTreeItemData {
435 wxPyTreeItemData(PyObject* obj = NULL) {
442 ~wxPyTreeItemData() {
443 bool doSave = wxPyRestoreThread();
445 wxPySaveThread(doSave);
448 PyObject* GetData() {
453 void SetData(PyObject* obj) {
454 bool doSave = wxPyRestoreThread();
456 wxPySaveThread(doSave);
467 %name(wxTreeItemData) class wxPyTreeItemData {
469 wxPyTreeItemData(PyObject* obj = NULL);
472 void SetData(PyObject* obj);
474 const wxTreeItemId& GetId();
475 void SetId(const wxTreeItemId& id);
480 class wxTreeEvent : public wxNotifyEvent {
482 wxTreeItemId GetItem();
483 wxTreeItemId GetOldItem();
486 const wxString& GetLabel();
492 class wxPyTreeCtrl : public wxTreeCtrl {
494 wxPyTreeCtrl(wxWindow *parent, wxWindowID id,
498 const wxValidator& validator,
500 wxTreeCtrl(parent, id, pos, size, style, validator, name) {}
503 int OnCompareItems(const wxTreeItemId& item1,
504 const wxTreeItemId& item2) {
506 bool doSave = wxPyRestoreThread();
507 if (m_myInst.findCallback("OnCompareItems"))
508 rval = m_myInst.callCallback(Py_BuildValue(
510 wxPyConstructObject((void*)&item1, "wxTreeItemId"),
511 wxPyConstructObject((void*)&item2, "wxTreeItemId")));
513 rval = wxTreeCtrl::OnCompareItems(item1, item2);
514 wxPySaveThread(doSave);
522 // These are for the GetFirstChild/GetNextChild methods below
523 %typemap(python, in) long& INOUT = long* INOUT;
524 %typemap(python, argout) long& INOUT = long* INOUT;
527 %name(wxTreeCtrl)class wxPyTreeCtrl : public wxControl {
529 wxPyTreeCtrl(wxWindow *parent, wxWindowID id = -1,
530 const wxPoint& pos = wxDefaultPosition,
531 const wxSize& size = wxDefaultSize,
532 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
533 const wxValidator& validator = wxDefaultValidator,
534 char* name = "wxTreeCtrl");
536 void _setSelf(PyObject* self, PyObject* _class);
537 %pragma(python) addtomethod = "__init__:#wx._StdWindowCallbacks(self)"
538 %pragma(python) addtomethod = "__init__:self._setSelf(self, wxTreeCtrl)"
540 void AssignImageList(wxImageList* imageList);
542 unsigned int GetIndent();
543 void SetIndent(unsigned int indent);
544 wxImageList *GetImageList();
545 wxImageList *GetStateImageList();
546 void SetImageList(wxImageList *imageList);
547 void SetStateImageList(wxImageList *imageList);
549 unsigned int GetSpacing();
550 void SetSpacing(unsigned int spacing);
552 wxString GetItemText(const wxTreeItemId& item);
553 int GetItemImage(const wxTreeItemId& item,
554 wxTreeItemIcon which = wxTreeItemIcon_Normal);
555 int GetItemSelectedImage(const wxTreeItemId& item);
557 void SetItemText(const wxTreeItemId& item, const wxString& text);
558 void SetItemImage(const wxTreeItemId& item, int image,
559 wxTreeItemIcon which = wxTreeItemIcon_Normal);
560 void SetItemSelectedImage(const wxTreeItemId& item, int image);
561 void SetItemHasChildren(const wxTreeItemId& item, bool hasChildren = TRUE);
564 // [Get|Set]ItemData substitutes. Automatically create wxPyTreeItemData
566 wxPyTreeItemData* GetItemData(const wxTreeItemId& item) {
567 wxPyTreeItemData* data = (wxPyTreeItemData*)self->GetItemData(item);
569 data = new wxPyTreeItemData();
570 data->SetId(item); // set the id
571 self->SetItemData(item, data);
576 void SetItemData(const wxTreeItemId& item, wxPyTreeItemData* data) {
577 data->SetId(item); // set the id
578 self->SetItemData(item, data);
581 // [Get|Set]PyData are short-cuts. Also made somewhat crash-proof by
582 // automatically creating data classes.
583 PyObject* GetPyData(const wxTreeItemId& item) {
584 wxPyTreeItemData* data = (wxPyTreeItemData*)self->GetItemData(item);
586 data = new wxPyTreeItemData();
587 data->SetId(item); // set the id
588 self->SetItemData(item, data);
590 return data->GetData();
593 void SetPyData(const wxTreeItemId& item, PyObject* obj) {
594 wxPyTreeItemData* data = (wxPyTreeItemData*)self->GetItemData(item);
596 data = new wxPyTreeItemData(obj);
597 data->SetId(item); // set the id
598 self->SetItemData(item, data);
605 bool IsVisible(const wxTreeItemId& item);
606 bool ItemHasChildren(const wxTreeItemId& item);
607 bool IsExpanded(const wxTreeItemId& item);
608 bool IsSelected(const wxTreeItemId& item);
610 wxTreeItemId GetRootItem();
611 wxTreeItemId GetSelection();
612 %name(GetItemParent) wxTreeItemId GetParent(const wxTreeItemId& item);
613 //size_t GetSelections(wxArrayTreeItemIds& selection);
615 PyObject* GetSelections() {
616 bool doSave = wxPyRestoreThread();
617 PyObject* rval = PyList_New(0);
618 wxArrayTreeItemIds array;
620 num = self->GetSelections(array);
621 for (x=0; x < num; x++) {
622 wxTreeItemId *tii = new wxTreeItemId(array.Item(x));
623 PyObject* item = wxPyConstructObject((void*)tii, "wxTreeItemId", TRUE);
624 PyList_Append(rval, item);
626 wxPySaveThread(doSave);
633 size_t GetChildrenCount(const wxTreeItemId& item, bool recursively = TRUE);
635 wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& INOUT);
636 wxTreeItemId GetNextChild(const wxTreeItemId& item, long& INOUT);
637 wxTreeItemId GetNextSibling(const wxTreeItemId& item);
638 wxTreeItemId GetPrevSibling(const wxTreeItemId& item);
639 wxTreeItemId GetFirstVisibleItem();
640 wxTreeItemId GetNextVisible(const wxTreeItemId& item);
641 wxTreeItemId GetPrevVisible(const wxTreeItemId& item);
642 wxTreeItemId GetLastChild(const wxTreeItemId& item);
646 wxTreeItemId AddRoot(const wxString& text,
647 int image = -1, int selectedImage = -1,
648 wxPyTreeItemData *data = NULL);
649 wxTreeItemId PrependItem(const wxTreeItemId& parent,
650 const wxString& text,
651 int image = -1, int selectedImage = -1,
652 wxPyTreeItemData *data = NULL);
653 wxTreeItemId InsertItem(const wxTreeItemId& parent,
654 const wxTreeItemId& idPrevious,
655 const wxString& text,
656 int image = -1, int selectedImage = -1,
657 wxPyTreeItemData *data = NULL);
658 %name(InsertItemBefore)
659 wxTreeItemId InsertItem(const wxTreeItemId& parent,
661 const wxString& text,
662 int image = -1, int selectedImage = -1,
663 wxTreeItemData *data = NULL);
664 wxTreeItemId AppendItem(const wxTreeItemId& parent,
665 const wxString& text,
666 int image = -1, int selectedImage = -1,
667 wxPyTreeItemData *data = NULL);
669 void Delete(const wxTreeItemId& item);
670 void DeleteChildren(const wxTreeItemId& item);
671 void DeleteAllItems();
673 void Expand(const wxTreeItemId& item);
674 void Collapse(const wxTreeItemId& item);
675 void CollapseAndReset(const wxTreeItemId& item);
676 void Toggle(const wxTreeItemId& item);
680 void SelectItem(const wxTreeItemId& item);
681 void EnsureVisible(const wxTreeItemId& item);
682 void ScrollTo(const wxTreeItemId& item);
684 wxTextCtrl* EditLabel(const wxTreeItemId& item);
685 wxTextCtrl* GetEditControl();
686 void EndEditLabel(const wxTreeItemId& item, int discardChanges = FALSE);
688 void EditLabel(const wxTreeItemId& item);
691 void SortChildren(const wxTreeItemId& item);
693 void SetItemBold(const wxTreeItemId& item, int bold = TRUE);
694 bool IsBold(const wxTreeItemId& item) const;
695 wxTreeItemId HitTest(const wxPoint& point, int& OUTPUT);
699 void SetItemTextColour(const wxTreeItemId& item, const wxColour& col);
700 void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& col);
701 void SetItemFont(const wxTreeItemId& item, const wxFont& font);
704 void SetItemDropHighlight(const wxTreeItemId& item, int highlight = TRUE);
706 //bool GetBoundingRect(const wxTreeItemId& item, wxRect& rect, int textOnly = FALSE)
708 PyObject* GetBoundingRect(const wxTreeItemId& item, int textOnly = FALSE) {
710 if (self->GetBoundingRect(item, rect, textOnly)) {
711 bool doSave = wxPyRestoreThread();
712 wxRect* r = new wxRect(rect);
713 PyObject* val = wxPyConstructObject((void*)r, "wxRect");
714 wxPySaveThread(doSave);
725 %pragma(python) addtoclass = "
726 # Redefine some methods that SWIG gets a bit confused on...
727 def GetFirstChild(self, *_args, **_kwargs):
728 val1,val2 = apply(controls2c.wxTreeCtrl_GetFirstChild,(self,) + _args, _kwargs)
729 val1 = wxTreeItemIdPtr(val1)
732 def GetNextChild(self, *_args, **_kwargs):
733 val1,val2 = apply(controls2c.wxTreeCtrl_GetNextChild,(self,) + _args, _kwargs)
734 val1 = wxTreeItemIdPtr(val1)
737 def HitTest(self, *_args, **_kwargs):
738 val1, val2 = apply(controls2c.wxTreeCtrl_HitTest,(self,) + _args, _kwargs)
739 val1 = wxTreeItemIdPtr(val1)
746 //----------------------------------------------------------------------
753 /* tab control event types */
754 wxEVT_COMMAND_TAB_SEL_CHANGED,
755 wxEVT_COMMAND_TAB_SEL_CHANGING,
759 class wxTabEvent : public wxCommandEvent {
765 class wxTabCtrl : public wxControl {
767 wxTabCtrl(wxWindow* parent, wxWindowID id,
768 const wxPoint& pos = wxDefaultPosition,
769 const wxSize& size = wxDefaultSize,
771 char* name = "tabCtrl");
773 %pragma(python) addtomethod = "__init__:#wx._StdWindowCallbacks(self)"
775 bool DeleteAllItems();
776 bool DeleteItem(int item);
777 wxImageList* GetImageList();
779 // TODO: void* GetItemData();
780 int GetItemImage(int item);
783 %new wxRect* GetItemRect(int item) {
784 wxRect* rect = new wxRect;
785 self->GetItemRect(item, *rect);
790 wxString GetItemText(int item);
793 int HitTest(const wxPoint& pt, long& OUTPUT);
794 void InsertItem(int item, const wxString& text,
795 int imageId = -1, void* clientData = NULL);
796 // TODO: bool SetItemData(int item, void* data);
797 bool SetItemImage(int item, int image);
798 void SetImageList(wxImageList* imageList);
799 void SetItemSize(const wxSize& size);
800 bool SetItemText(int item, const wxString& text);
801 void SetPadding(const wxSize& padding);
802 int SetSelection(int item);
809 //----------------------------------------------------------------------