1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/palmos/treectrl.cpp
4 // Author: William Osborne - minimal working wxPalmOS port
8 // Copyright: (c) William Osborne
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treectrl.h"
32 #include "wx/dynarray.h"
35 #include "wx/settings.h"
38 #include "wx/imaglist.h"
40 // macros to hide the cast ugliness
41 // --------------------------------
43 // ptr is the real item id, i.e. wxTreeItemId::m_pItem
44 #define HITEM_PTR(ptr) (HTREEITEM)(ptr)
46 // item here is a wxTreeItemId
47 #define HITEM(item) HITEM_PTR((item).m_pItem)
49 // the native control doesn't support multiple selections under MSW and we
50 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
51 // checkboxes be the selection status (checked == selected) or by really
52 // emulating everything, i.e. intercepting mouse and key events &c. The first
53 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
55 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 #if wxUSE_EXTENDED_RTTI
70 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
72 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
73 // new style border flags, we put them first to
74 // use them for streaming out
75 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
76 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
77 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
78 wxFLAGS_MEMBER(wxBORDER_RAISED
)
79 wxFLAGS_MEMBER(wxBORDER_STATIC
)
80 wxFLAGS_MEMBER(wxBORDER_NONE
)
82 // old style border flags
83 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
84 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
85 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
86 wxFLAGS_MEMBER(wxRAISED_BORDER
)
87 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
88 wxFLAGS_MEMBER(wxBORDER
)
90 // standard window styles
91 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
92 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
93 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
94 wxFLAGS_MEMBER(wxWANTS_CHARS
)
95 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
96 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
97 wxFLAGS_MEMBER(wxVSCROLL
)
98 wxFLAGS_MEMBER(wxHSCROLL
)
100 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
101 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
102 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
103 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
104 wxFLAGS_MEMBER(wxTR_NO_LINES
)
105 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
106 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
107 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
108 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
109 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
110 wxFLAGS_MEMBER(wxTR_SINGLE
)
111 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
112 wxFLAGS_MEMBER(wxTR_EXTENDED
)
113 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
115 wxEND_FLAGS( wxTreeCtrlStyle
)
117 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
119 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
120 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
121 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
122 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
123 wxEND_PROPERTIES_TABLE()
125 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
126 wxEND_HANDLERS_TABLE()
128 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
130 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 // indices in gs_expandEvents table below
152 // handy table for sending events - it has to be initialized during run-time
153 // now so can't be const any more
154 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
157 but logically it's a const table with the following entries:
160 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
161 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
165 // ============================================================================
167 // ============================================================================
169 // ----------------------------------------------------------------------------
170 // construction and destruction
171 // ----------------------------------------------------------------------------
173 void wxTreeCtrl::Init()
177 bool wxTreeCtrl::Create(wxWindow
*parent
,
182 const wxValidator
& validator
,
183 const wxString
& name
)
188 wxTreeCtrl::~wxTreeCtrl()
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 /* static */ wxVisualAttributes
197 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
199 wxVisualAttributes attrs
;
205 // simple wrappers which add error checking in debug mode
207 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
212 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
216 unsigned int wxTreeCtrl::GetCount() const
222 unsigned int wxTreeCtrl::GetIndent() const
227 void wxTreeCtrl::SetIndent(unsigned int indent
)
231 wxImageList
*wxTreeCtrl::GetImageList() const
233 return m_imageListNormal
;
236 wxImageList
*wxTreeCtrl::GetStateImageList() const
238 return m_imageListState
;
241 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
245 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
249 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
253 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
257 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
261 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
262 bool recursively
) const
267 // ----------------------------------------------------------------------------
269 // ----------------------------------------------------------------------------
271 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
276 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
287 return wxString(wxT(""));
290 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
294 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
295 wxTreeItemIcon which
) const
300 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
302 wxTreeItemIcon which
) const
306 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
312 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
313 wxTreeItemIcon which
) const
318 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
319 wxTreeItemIcon which
)
323 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
328 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
332 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
333 wxTreeItemIndirectData
*data
)
337 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
342 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
346 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
350 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
354 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
358 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
363 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
368 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
373 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
378 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
383 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
387 // ----------------------------------------------------------------------------
389 // ----------------------------------------------------------------------------
391 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
396 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
401 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
406 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
411 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
416 // ----------------------------------------------------------------------------
418 // ----------------------------------------------------------------------------
420 wxTreeItemId
wxTreeCtrl::GetRootItem() const
422 return wxTreeItemId();
425 wxTreeItemId
wxTreeCtrl::GetSelection() const
430 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
435 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
436 wxTreeItemIdValue
& cookie
) const
441 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
442 wxTreeItemIdValue
& cookie
) const
447 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
452 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
457 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
462 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
467 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
472 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
477 // ----------------------------------------------------------------------------
478 // multiple selections emulation
479 // ----------------------------------------------------------------------------
481 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
486 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
490 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
495 // ----------------------------------------------------------------------------
497 // ----------------------------------------------------------------------------
499 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
500 wxTreeItemId hInsertAfter
,
501 const wxString
& text
,
502 int image
, int selectedImage
,
503 wxTreeItemData
*data
)
508 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
509 int image
, int selectedImage
,
510 wxTreeItemData
*data
)
515 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
516 const wxString
& text
,
517 int image
, int selectedImage
,
518 wxTreeItemData
*data
)
523 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
524 const wxTreeItemId
& idPrevious
,
525 const wxString
& text
,
526 int image
, int selectedImage
,
527 wxTreeItemData
*data
)
532 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
534 const wxString
& text
,
535 int image
, int selectedImage
,
536 wxTreeItemData
*data
)
541 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
542 const wxString
& text
,
543 int image
, int selectedImage
,
544 wxTreeItemData
*data
)
549 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
553 // delete all children (but don't delete the item itself)
554 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
558 void wxTreeCtrl::DeleteAllItems()
562 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
566 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
570 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
574 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
578 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
582 void wxTreeCtrl::Unselect()
586 void wxTreeCtrl::UnselectAll()
590 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
594 void wxTreeCtrl::UnselectItem(const wxTreeItemId
& item
)
598 void wxTreeCtrl::ToggleItemSelection(const wxTreeItemId
& item
)
602 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
606 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
610 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
615 void wxTreeCtrl::DeleteTextCtrl()
619 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
620 wxClassInfo
* textControlClass
)
625 // End label editing, optionally cancelling the edit
626 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
630 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
635 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
642 // ----------------------------------------------------------------------------
644 // ----------------------------------------------------------------------------
647 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
648 // functions such as IsDataIndirect()
649 class wxTreeSortHelper
652 static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree);
655 static wxTreeItemId GetIdFromData(LPARAM lParam)
657 return ((wxTreeItemParam*)lParam)->GetItem();
661 int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1,
665 wxCHECK_MSG( pItem1 && pItem2, 0,
666 wxT("sorting tree without data doesn't make sense") );
668 wxTreeCtrl *tree = (wxTreeCtrl *)htree;
670 return tree->OnCompareItems(GetIdFromData(tree, pItem1),
671 GetIdFromData(tree, pItem2));
674 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
675 const wxTreeItemId
& item2
)
677 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
680 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
682 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
686 // ----------------------------------------------------------------------------
688 // ----------------------------------------------------------------------------
690 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
691 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
693 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
697 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
702 #endif // wxUSE_TREECTRL