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/palmos/private.h"
33 #include "wx/dynarray.h"
34 #include "wx/imaglist.h"
35 #include "wx/settings.h"
37 // macros to hide the cast ugliness
38 // --------------------------------
40 // ptr is the real item id, i.e. wxTreeItemId::m_pItem
41 #define HITEM_PTR(ptr) (HTREEITEM)(ptr)
43 // item here is a wxTreeItemId
44 #define HITEM(item) HITEM_PTR((item).m_pItem)
46 // the native control doesn't support multiple selections under MSW and we
47 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
48 // checkboxes be the selection status (checked == selected) or by really
49 // emulating everything, i.e. intercepting mouse and key events &c. The first
50 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
52 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 #if wxUSE_EXTENDED_RTTI
67 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
69 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
70 // new style border flags, we put them first to
71 // use them for streaming out
72 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
73 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
74 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
75 wxFLAGS_MEMBER(wxBORDER_RAISED
)
76 wxFLAGS_MEMBER(wxBORDER_STATIC
)
77 wxFLAGS_MEMBER(wxBORDER_NONE
)
79 // old style border flags
80 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
81 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
82 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
83 wxFLAGS_MEMBER(wxRAISED_BORDER
)
84 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
85 wxFLAGS_MEMBER(wxBORDER
)
87 // standard window styles
88 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
89 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
90 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
91 wxFLAGS_MEMBER(wxWANTS_CHARS
)
92 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
93 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
94 wxFLAGS_MEMBER(wxVSCROLL
)
95 wxFLAGS_MEMBER(wxHSCROLL
)
97 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
98 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
99 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
100 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
101 wxFLAGS_MEMBER(wxTR_NO_LINES
)
102 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
103 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
104 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
105 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
106 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
107 wxFLAGS_MEMBER(wxTR_SINGLE
)
108 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
109 wxFLAGS_MEMBER(wxTR_EXTENDED
)
110 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
112 wxEND_FLAGS( wxTreeCtrlStyle
)
114 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
116 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
117 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
118 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
119 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
120 wxEND_PROPERTIES_TABLE()
122 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
123 wxEND_HANDLERS_TABLE()
125 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
127 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 // indices in gs_expandEvents table below
149 // handy table for sending events - it has to be initialized during run-time
150 // now so can't be const any more
151 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
154 but logically it's a const table with the following entries:
157 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
158 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
162 // ============================================================================
164 // ============================================================================
166 // ----------------------------------------------------------------------------
167 // construction and destruction
168 // ----------------------------------------------------------------------------
170 void wxTreeCtrl::Init()
174 bool wxTreeCtrl::Create(wxWindow
*parent
,
179 const wxValidator
& validator
,
180 const wxString
& name
)
185 wxTreeCtrl::~wxTreeCtrl()
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
193 /* static */ wxVisualAttributes
194 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
196 wxVisualAttributes attrs
;
202 // simple wrappers which add error checking in debug mode
204 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
209 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
213 size_t wxTreeCtrl::GetCount() const
218 unsigned int wxTreeCtrl::GetIndent() const
223 void wxTreeCtrl::SetIndent(unsigned int indent
)
227 wxImageList
*wxTreeCtrl::GetImageList() const
229 return m_imageListNormal
;
232 wxImageList
*wxTreeCtrl::GetStateImageList() const
234 return m_imageListState
;
237 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
241 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
245 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
249 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
253 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
257 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
258 bool recursively
) const
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
267 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
272 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
277 // ----------------------------------------------------------------------------
279 // ----------------------------------------------------------------------------
281 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
286 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
290 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
291 wxTreeItemIcon which
) const
296 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
298 wxTreeItemIcon which
) const
302 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
308 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
309 wxTreeItemIcon which
) const
314 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
315 wxTreeItemIcon which
)
319 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
324 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
328 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
329 wxTreeItemIndirectData
*data
)
333 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
338 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
342 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
346 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
350 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
354 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
359 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
364 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
369 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
374 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
379 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
383 // ----------------------------------------------------------------------------
385 // ----------------------------------------------------------------------------
387 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
392 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
397 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
402 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
407 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
412 // ----------------------------------------------------------------------------
414 // ----------------------------------------------------------------------------
416 wxTreeItemId
wxTreeCtrl::GetRootItem() const
418 // Root may be real (visible) or virtual (hidden).
419 if ( GET_VIRTUAL_ROOT() )
422 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
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
)
554 // delete all children (but don't delete the item itself)
555 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
559 void wxTreeCtrl::DeleteAllItems()
563 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
567 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
571 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
575 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
579 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
583 void wxTreeCtrl::Unselect()
587 void wxTreeCtrl::UnselectAll()
591 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
595 void wxTreeCtrl::UnselectItem(const wxTreeItemId
& item
)
599 void wxTreeCtrl::ToggleItemSelection(const wxTreeItemId
& item
)
603 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
607 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
611 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
616 void wxTreeCtrl::DeleteTextCtrl()
620 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
621 wxClassInfo
* textControlClass
)
626 // End label editing, optionally cancelling the edit
627 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
631 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
636 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
643 // ----------------------------------------------------------------------------
645 // ----------------------------------------------------------------------------
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(wxTreeCtrl
*tree
, LPARAM item
)
657 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
658 if ( tree
->IsDataIndirect(data
) )
660 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
663 return data
->GetId();
667 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
671 wxCHECK_MSG( pItem1
&& pItem2
, 0,
672 wxT("sorting tree without data doesn't make sense") );
674 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
676 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
677 GetIdFromData(tree
, pItem2
));
680 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
681 const wxTreeItemId
& item2
)
683 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
686 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
688 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
690 // rely on the fact that TreeView_SortChildren does the same thing as our
691 // default behaviour, i.e. sorts items alphabetically and so call it
692 // directly if we're not in derived class (much more efficient!)
693 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
695 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
700 tvSort
.hParent
= HITEM(item
);
701 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
702 tvSort
.lParam
= (LPARAM
)this;
703 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
707 // ----------------------------------------------------------------------------
709 // ----------------------------------------------------------------------------
711 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
712 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
714 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
718 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
723 #endif // wxUSE_TREECTRL