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,
122 //wxListItemAttr(const wxColour& colText,
123 // const wxColour& colBack,
124 // const wxFont& font)
125 // : m_colText(colText), m_colBack(colBack), m_font(font) { }
128 void SetTextColour(const wxColour& colText);
129 void SetBackgroundColour(const wxColour& colBack);
130 void SetFont(const wxFont& font);
133 bool HasTextColour();
134 bool HasBackgroundColour();
137 const wxColour& GetTextColour();
138 const wxColour& GetBackgroundColour();
139 const wxFont& GetFont();
143 class wxListItem : public wxObject {
150 void ClearAttributes();
153 void SetMask(long mask);
155 void SetColumn(int col);
156 void SetState(long state);
157 void SetStateMask(long stateMask);
158 void SetText(const wxString& text);
159 void SetImage(int image);
160 void SetData(long data);
162 void SetWidth(int width);
163 void SetAlign(wxListColumnFormat align);
165 void SetTextColour(const wxColour& colText);
166 void SetBackgroundColour(const wxColour& colBack);
167 void SetFont(const wxFont& font);
174 const wxString& GetText();
179 wxListColumnFormat GetAlign();
181 wxListItemAttr *GetAttributes();
182 bool HasAttributes();
184 wxColour GetTextColour() const;
185 wxColour GetBackgroundColour() const;
186 wxFont GetFont() const;
188 // these members are public for compatibility
189 long m_mask; // Indicates what fields are valid
190 long m_itemId; // The zero-based item position
191 int m_col; // Zero-based column, if in report mode
192 long m_state; // The state of the item
193 long m_stateMask;// Which flags of m_state are valid (uses same flags)
194 wxString m_text; // The label/header text
195 int m_image; // The zero-based index into an image list
196 long m_data; // App-defined data
199 int m_format; // left, right, centre
200 int m_width; // width of column
205 class wxListEvent: public wxNotifyEvent {
222 const wxString& GetLabel();
223 const wxString& GetText();
227 const wxListItem& GetItem();
232 class wxListCtrl : public wxControl {
234 wxListCtrl(wxWindow* parent, wxWindowID id,
235 const wxPoint& pos = wxDefaultPosition,
236 const wxSize& size = wxDefaultSize,
237 long style = wxLC_ICON,
238 const wxValidator& validator = wxDefaultValidator,
239 char* name = "listCtrl");
241 %pragma(python) addtomethod = "__init__:#wx._StdWindowCallbacks(self)"
243 bool Arrange(int flag = wxLIST_ALIGN_DEFAULT);
244 void AssignImageList(wxImageList* imageList, int which);
245 %pragma(python) addtomethod = "AssignImageList:_args[0].thisown = 0"
246 bool DeleteItem(long item);
247 bool DeleteAllItems();
248 bool DeleteColumn(int col);
249 bool DeleteAllColumns(void);
252 wxTextCtrl* EditLabel(long item);
253 bool EndEditLabel(bool cancel);
254 wxTextCtrl* GetEditControl();
256 void EditLabel(long item);
258 bool EnsureVisible(long item);
259 long FindItem(long start, const wxString& str, bool partial = FALSE);
260 %name(FindItemData)long FindItem(long start, long data);
261 %name(FindItemAtPos)long FindItem(long start, const wxPoint& pt,
263 bool GetColumn(int col, wxListItem& item);
264 int GetColumnWidth(int col);
265 int GetCountPerPage();
266 wxImageList* GetImageList(int which);
267 long GetItemData(long item);
270 %new wxListItem* GetItem(long itemId, int col=0) {
271 wxListItem* info = new wxListItem;
272 info->m_itemId = itemId;
274 info->m_mask = 0xFFFF;
275 self->GetItem(*info);
278 %new wxPoint* GetItemPosition(long item) {
279 wxPoint* pos = new wxPoint;
280 self->GetItemPosition(item, *pos);
283 %new wxRect* GetItemRect(long item, int code = wxLIST_RECT_BOUNDS) {
284 wxRect* rect= new wxRect;
285 self->GetItemRect(item, *rect, code);
290 int GetItemState(long item, long stateMask);
292 int GetItemSpacing(bool isSmall);
293 wxString GetItemText(long item);
294 long GetNextItem(long item,
295 int geometry = wxLIST_NEXT_ALL,
296 int state = wxLIST_STATE_DONTCARE);
297 int GetSelectedItemCount();
299 wxColour GetTextColour();
300 void SetTextColour(const wxColour& col);
303 long HitTest(const wxPoint& point, int& OUTPUT);
304 %name(InsertColumnInfo)long InsertColumn(long col, wxListItem& info);
305 long InsertColumn(long col, const wxString& heading,
306 int format = wxLIST_FORMAT_LEFT,
309 long InsertItem(wxListItem& info);
310 %name(InsertStringItem) long InsertItem(long index, const wxString& label);
311 %name(InsertImageItem) long InsertItem(long index, int imageIndex);
312 %name(InsertImageStringItem)long InsertItem(long index, const wxString& label,
315 bool ScrollList(int dx, int dy);
316 void SetBackgroundColour(const wxColour& col);
317 bool SetColumn(int col, wxListItem& item);
318 bool SetColumnWidth(int col, int width);
319 void SetImageList(wxImageList* imageList, int which);
321 bool SetItem(wxListItem& info);
322 %name(SetStringItem)long SetItem(long index, int col, const wxString& label,
325 bool SetItemData(long item, long data);
326 bool SetItemImage(long item, int image, int selImage);
327 bool SetItemPosition(long item, const wxPoint& pos);
328 bool SetItemState(long item, long state, long stateMask);
329 void SetItemText(long item, const wxString& text);
330 void SetSingleStyle(long style, bool add = TRUE);
331 void SetWindowStyleFlag(long style);
333 // bool SortItems(wxListCtrlCompare fn, long data);
335 bool SortItems(PyObject* func) {
336 if (!PyCallable_Check(func))
339 return self->SortItems(wxPyListCtrl_SortItems, (long)func);
345 int wxCALLBACK wxPyListCtrl_SortItems(long item1, long item2, long funcPtr) {
347 PyObject* func = (PyObject*)funcPtr;
348 bool doSave = wxPyRestoreThread();
350 PyObject* args = Py_BuildValue("(ii)", item1, item2);
351 PyObject* result = PyEval_CallObject(func, args);
354 retval = PyInt_AsLong(result);
358 wxPySaveThread(doSave);
364 //----------------------------------------------------------------------
368 wxTreeItemIcon_Normal, // not selected, not expanded
369 wxTreeItemIcon_Selected, // selected, not expanded
370 wxTreeItemIcon_Expanded, // not selected, expanded
371 wxTreeItemIcon_SelectedExpanded, // selected, expanded
376 // constants for HitTest
378 wxTREE_HITTEST_ABOVE,
379 wxTREE_HITTEST_BELOW,
380 wxTREE_HITTEST_NOWHERE,
381 wxTREE_HITTEST_ONITEMBUTTON,
382 wxTREE_HITTEST_ONITEMICON,
383 wxTREE_HITTEST_ONITEMINDENT,
384 wxTREE_HITTEST_ONITEMLABEL,
385 wxTREE_HITTEST_ONITEMRIGHT,
386 wxTREE_HITTEST_ONITEMSTATEICON,
387 wxTREE_HITTEST_TOLEFT,
388 wxTREE_HITTEST_TORIGHT,
389 wxTREE_HITTEST_ONITEMUPPERPART,
390 wxTREE_HITTEST_ONITEMLOWERPART,
391 wxTREE_HITTEST_ONITEM
396 /* Tree control event types */
397 wxEVT_COMMAND_TREE_BEGIN_DRAG,
398 wxEVT_COMMAND_TREE_BEGIN_RDRAG,
399 wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT,
400 wxEVT_COMMAND_TREE_END_LABEL_EDIT,
401 wxEVT_COMMAND_TREE_DELETE_ITEM,
402 wxEVT_COMMAND_TREE_GET_INFO,
403 wxEVT_COMMAND_TREE_SET_INFO,
404 wxEVT_COMMAND_TREE_ITEM_EXPANDED,
405 wxEVT_COMMAND_TREE_ITEM_EXPANDING,
406 wxEVT_COMMAND_TREE_ITEM_COLLAPSED,
407 wxEVT_COMMAND_TREE_ITEM_COLLAPSING,
408 wxEVT_COMMAND_TREE_SEL_CHANGED,
409 wxEVT_COMMAND_TREE_SEL_CHANGING,
410 wxEVT_COMMAND_TREE_KEY_DOWN,
411 wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
412 wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK,
413 wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK,
414 wxEVT_COMMAND_TREE_END_DRAG,
425 int __cmp__(wxTreeItemId* other) {
426 if (! other) return -1;
427 return *self != *other;
435 class wxPyTreeItemData : public wxTreeItemData {
437 wxPyTreeItemData(PyObject* obj = NULL) {
444 ~wxPyTreeItemData() {
445 bool doSave = wxPyRestoreThread();
447 wxPySaveThread(doSave);
450 PyObject* GetData() {
455 void SetData(PyObject* obj) {
456 bool doSave = wxPyRestoreThread();
458 wxPySaveThread(doSave);
469 %name(wxTreeItemData) class wxPyTreeItemData : public wxObject {
471 wxPyTreeItemData(PyObject* obj = NULL);
474 void SetData(PyObject* obj);
476 const wxTreeItemId& GetId();
477 void SetId(const wxTreeItemId& id);
482 class wxTreeEvent : public wxNotifyEvent {
484 wxTreeItemId GetItem();
485 wxTreeItemId GetOldItem();
488 const wxString& GetLabel();
494 class wxPyTreeCtrl : public wxTreeCtrl {
495 DECLARE_ABSTRACT_CLASS(wxPyTreeCtrl);
497 wxPyTreeCtrl(wxWindow *parent, wxWindowID id,
501 const wxValidator& validator,
503 wxTreeCtrl(parent, id, pos, size, style, validator, name) {}
505 int OnCompareItems(const wxTreeItemId& item1,
506 const wxTreeItemId& item2) {
508 bool doSave = wxPyRestoreThread();
509 if (m_myInst.findCallback("OnCompareItems"))
510 rval = m_myInst.callCallback(Py_BuildValue(
512 wxPyConstructObject((void*)&item1, "wxTreeItemId"),
513 wxPyConstructObject((void*)&item2, "wxTreeItemId")));
515 rval = wxTreeCtrl::OnCompareItems(item1, item2);
516 wxPySaveThread(doSave);
522 IMPLEMENT_ABSTRACT_CLASS(wxPyTreeCtrl, wxTreeCtrl);
526 // These are for the GetFirstChild/GetNextChild methods below
527 %typemap(python, in) long& INOUT = long* INOUT;
528 %typemap(python, argout) long& INOUT = long* INOUT;
531 %name(wxTreeCtrl)class wxPyTreeCtrl : public wxControl {
533 wxPyTreeCtrl(wxWindow *parent, wxWindowID id = -1,
534 const wxPoint& pos = wxDefaultPosition,
535 const wxSize& size = wxDefaultSize,
536 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
537 const wxValidator& validator = wxDefaultValidator,
538 char* name = "wxTreeCtrl");
540 void _setSelf(PyObject* self, PyObject* _class);
541 %pragma(python) addtomethod = "__init__:#wx._StdWindowCallbacks(self)"
542 %pragma(python) addtomethod = "__init__:self._setSelf(self, wxTreeCtrl)"
544 void AssignImageList(wxImageList* imageList);
545 %pragma(python) addtomethod = "AssignImageList:_args[0].thisown = 0"
547 unsigned int GetIndent();
548 void SetIndent(unsigned int indent);
549 wxImageList *GetImageList();
550 wxImageList *GetStateImageList();
551 void SetImageList(wxImageList *imageList);
552 void SetStateImageList(wxImageList *imageList);
554 unsigned int GetSpacing();
555 void SetSpacing(unsigned int spacing);
557 wxString GetItemText(const wxTreeItemId& item);
558 int GetItemImage(const wxTreeItemId& item,
559 wxTreeItemIcon which = wxTreeItemIcon_Normal);
560 int GetItemSelectedImage(const wxTreeItemId& item);
562 void SetItemText(const wxTreeItemId& item, const wxString& text);
563 void SetItemImage(const wxTreeItemId& item, int image,
564 wxTreeItemIcon which = wxTreeItemIcon_Normal);
565 void SetItemSelectedImage(const wxTreeItemId& item, int image);
566 void SetItemHasChildren(const wxTreeItemId& item, bool hasChildren = TRUE);
569 // [Get|Set]ItemData substitutes. Automatically create wxPyTreeItemData
571 wxPyTreeItemData* GetItemData(const wxTreeItemId& item) {
572 wxPyTreeItemData* data = (wxPyTreeItemData*)self->GetItemData(item);
574 data = new wxPyTreeItemData();
575 data->SetId(item); // set the id
576 self->SetItemData(item, data);
581 void SetItemData(const wxTreeItemId& item, wxPyTreeItemData* data) {
582 data->SetId(item); // set the id
583 self->SetItemData(item, data);
586 // [Get|Set]PyData are short-cuts. Also made somewhat crash-proof by
587 // automatically creating data classes.
588 PyObject* GetPyData(const wxTreeItemId& item) {
589 wxPyTreeItemData* data = (wxPyTreeItemData*)self->GetItemData(item);
591 data = new wxPyTreeItemData();
592 data->SetId(item); // set the id
593 self->SetItemData(item, data);
595 return data->GetData();
598 void SetPyData(const wxTreeItemId& item, PyObject* obj) {
599 wxPyTreeItemData* data = (wxPyTreeItemData*)self->GetItemData(item);
601 data = new wxPyTreeItemData(obj);
602 data->SetId(item); // set the id
603 self->SetItemData(item, data);
610 bool IsVisible(const wxTreeItemId& item);
611 bool ItemHasChildren(const wxTreeItemId& item);
612 bool IsExpanded(const wxTreeItemId& item);
613 bool IsSelected(const wxTreeItemId& item);
615 wxTreeItemId GetRootItem();
616 wxTreeItemId GetSelection();
617 %name(GetItemParent) wxTreeItemId GetParent(const wxTreeItemId& item);
618 //size_t GetSelections(wxArrayTreeItemIds& selection);
620 PyObject* GetSelections() {
621 bool doSave = wxPyRestoreThread();
622 PyObject* rval = PyList_New(0);
623 wxArrayTreeItemIds array;
625 num = self->GetSelections(array);
626 for (x=0; x < num; x++) {
627 wxTreeItemId *tii = new wxTreeItemId(array.Item(x));
628 PyObject* item = wxPyConstructObject((void*)tii, "wxTreeItemId", TRUE);
629 PyList_Append(rval, item);
631 wxPySaveThread(doSave);
638 size_t GetChildrenCount(const wxTreeItemId& item, bool recursively = TRUE);
640 wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& INOUT);
641 wxTreeItemId GetNextChild(const wxTreeItemId& item, long& INOUT);
642 wxTreeItemId GetNextSibling(const wxTreeItemId& item);
643 wxTreeItemId GetPrevSibling(const wxTreeItemId& item);
644 wxTreeItemId GetFirstVisibleItem();
645 wxTreeItemId GetNextVisible(const wxTreeItemId& item);
646 wxTreeItemId GetPrevVisible(const wxTreeItemId& item);
647 wxTreeItemId GetLastChild(const wxTreeItemId& item);
651 wxTreeItemId AddRoot(const wxString& text,
652 int image = -1, int selectedImage = -1,
653 wxPyTreeItemData *data = NULL);
654 wxTreeItemId PrependItem(const wxTreeItemId& parent,
655 const wxString& text,
656 int image = -1, int selectedImage = -1,
657 wxPyTreeItemData *data = NULL);
658 wxTreeItemId InsertItem(const wxTreeItemId& parent,
659 const wxTreeItemId& idPrevious,
660 const wxString& text,
661 int image = -1, int selectedImage = -1,
662 wxPyTreeItemData *data = NULL);
663 %name(InsertItemBefore)
664 wxTreeItemId InsertItem(const wxTreeItemId& parent,
666 const wxString& text,
667 int image = -1, int selectedImage = -1,
668 wxTreeItemData *data = NULL);
669 wxTreeItemId AppendItem(const wxTreeItemId& parent,
670 const wxString& text,
671 int image = -1, int selectedImage = -1,
672 wxPyTreeItemData *data = NULL);
674 void Delete(const wxTreeItemId& item);
675 void DeleteChildren(const wxTreeItemId& item);
676 void DeleteAllItems();
678 void Expand(const wxTreeItemId& item);
679 void Collapse(const wxTreeItemId& item);
680 void CollapseAndReset(const wxTreeItemId& item);
681 void Toggle(const wxTreeItemId& item);
685 void SelectItem(const wxTreeItemId& item);
686 void EnsureVisible(const wxTreeItemId& item);
687 void ScrollTo(const wxTreeItemId& item);
689 wxTextCtrl* EditLabel(const wxTreeItemId& item);
690 wxTextCtrl* GetEditControl();
691 void EndEditLabel(const wxTreeItemId& item, int discardChanges = FALSE);
693 void EditLabel(const wxTreeItemId& item);
696 void SortChildren(const wxTreeItemId& item);
698 void SetItemBold(const wxTreeItemId& item, int bold = TRUE);
699 bool IsBold(const wxTreeItemId& item) const;
700 wxTreeItemId HitTest(const wxPoint& point, int& OUTPUT);
704 void SetItemTextColour(const wxTreeItemId& item, const wxColour& col);
705 void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& col);
706 void SetItemFont(const wxTreeItemId& item, const wxFont& font);
709 void SetItemDropHighlight(const wxTreeItemId& item, int highlight = TRUE);
711 //bool GetBoundingRect(const wxTreeItemId& item, wxRect& rect, int textOnly = FALSE)
713 PyObject* GetBoundingRect(const wxTreeItemId& item, int textOnly = FALSE) {
715 if (self->GetBoundingRect(item, rect, textOnly)) {
716 bool doSave = wxPyRestoreThread();
717 wxRect* r = new wxRect(rect);
718 PyObject* val = wxPyConstructObject((void*)r, "wxRect");
719 wxPySaveThread(doSave);
730 %pragma(python) addtoclass = "
731 # Redefine some methods that SWIG gets a bit confused on...
732 def GetFirstChild(self, *_args, **_kwargs):
733 val1,val2 = apply(controls2c.wxTreeCtrl_GetFirstChild,(self,) + _args, _kwargs)
734 val1 = wxTreeItemIdPtr(val1)
737 def GetNextChild(self, *_args, **_kwargs):
738 val1,val2 = apply(controls2c.wxTreeCtrl_GetNextChild,(self,) + _args, _kwargs)
739 val1 = wxTreeItemIdPtr(val1)
742 def HitTest(self, *_args, **_kwargs):
743 val1, val2 = apply(controls2c.wxTreeCtrl_HitTest,(self,) + _args, _kwargs)
744 val1 = wxTreeItemIdPtr(val1)
751 //----------------------------------------------------------------------
758 /* tab control event types */
759 wxEVT_COMMAND_TAB_SEL_CHANGED,
760 wxEVT_COMMAND_TAB_SEL_CHANGING,
764 class wxTabEvent : public wxCommandEvent {
770 class wxTabCtrl : public wxControl {
772 wxTabCtrl(wxWindow* parent, wxWindowID id,
773 const wxPoint& pos = wxDefaultPosition,
774 const wxSize& size = wxDefaultSize,
776 char* name = "tabCtrl");
778 %pragma(python) addtomethod = "__init__:#wx._StdWindowCallbacks(self)"
780 bool DeleteAllItems();
781 bool DeleteItem(int item);
782 wxImageList* GetImageList();
784 // TODO: void* GetItemData();
785 int GetItemImage(int item);
788 %new wxRect* GetItemRect(int item) {
789 wxRect* rect = new wxRect;
790 self->GetItemRect(item, *rect);
795 wxString GetItemText(int item);
798 int HitTest(const wxPoint& pt, long& OUTPUT);
799 void InsertItem(int item, const wxString& text,
800 int imageId = -1, void* clientData = NULL);
801 // TODO: bool SetItemData(int item, void* data);
802 bool SetItemImage(int item, int image);
803 void SetImageList(wxImageList* imageList);
804 void SetItemSize(const wxSize& size);
805 bool SetItemText(int item, const wxString& text);
806 void SetPadding(const wxSize& padding);
807 int SetSelection(int item);
814 //----------------------------------------------------------------------
817 wxPyPtrTypeMap_Add("wxTreeItemData", "wxPyTreeItemData");
818 wxPyPtrTypeMap_Add("wxTreeCtrl", "wxPyTreeCtrl");
821 //----------------------------------------------------------------------