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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "treectrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/palmos/private.h"
37 #include "wx/dynarray.h"
38 #include "wx/imaglist.h"
39 #include "wx/settings.h"
41 // macros to hide the cast ugliness
42 // --------------------------------
44 // ptr is the real item id, i.e. wxTreeItemId::m_pItem
45 #define HITEM_PTR(ptr) (HTREEITEM)(ptr)
47 // item here is a wxTreeItemId
48 #define HITEM(item) HITEM_PTR((item).m_pItem)
50 // the native control doesn't support multiple selections under MSW and we
51 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
52 // checkboxes be the selection status (checked == selected) or by really
53 // emulating everything, i.e. intercepting mouse and key events &c. The first
54 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
56 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 #if wxUSE_EXTENDED_RTTI
71 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
73 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
74 // new style border flags, we put them first to
75 // use them for streaming out
76 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
77 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
78 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
79 wxFLAGS_MEMBER(wxBORDER_RAISED
)
80 wxFLAGS_MEMBER(wxBORDER_STATIC
)
81 wxFLAGS_MEMBER(wxBORDER_NONE
)
83 // old style border flags
84 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
85 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
86 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
87 wxFLAGS_MEMBER(wxRAISED_BORDER
)
88 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
89 wxFLAGS_MEMBER(wxBORDER
)
91 // standard window styles
92 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
93 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
94 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
95 wxFLAGS_MEMBER(wxWANTS_CHARS
)
96 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
97 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
98 wxFLAGS_MEMBER(wxVSCROLL
)
99 wxFLAGS_MEMBER(wxHSCROLL
)
101 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
102 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
103 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
104 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
105 wxFLAGS_MEMBER(wxTR_NO_LINES
)
106 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
107 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
108 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
109 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
110 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
111 wxFLAGS_MEMBER(wxTR_SINGLE
)
112 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
113 wxFLAGS_MEMBER(wxTR_EXTENDED
)
114 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
116 wxEND_FLAGS( wxTreeCtrlStyle
)
118 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
120 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
121 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
122 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
123 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
124 wxEND_PROPERTIES_TABLE()
126 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
127 wxEND_HANDLERS_TABLE()
129 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
131 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 // indices in gs_expandEvents table below
153 // handy table for sending events - it has to be initialized during run-time
154 // now so can't be const any more
155 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
158 but logically it's a const table with the following entries:
161 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
162 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
166 // ============================================================================
168 // ============================================================================
170 // ----------------------------------------------------------------------------
171 // construction and destruction
172 // ----------------------------------------------------------------------------
174 void wxTreeCtrl::Init()
178 bool wxTreeCtrl::Create(wxWindow
*parent
,
183 const wxValidator
& validator
,
184 const wxString
& name
)
189 wxTreeCtrl::~wxTreeCtrl()
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
197 /* static */ wxVisualAttributes
198 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
200 wxVisualAttributes attrs
;
206 // simple wrappers which add error checking in debug mode
208 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
213 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
217 size_t 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
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 // Root may be real (visible) or virtual (hidden).
423 if ( GET_VIRTUAL_ROOT() )
426 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
429 wxTreeItemId
wxTreeCtrl::GetSelection() const
434 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
439 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
440 wxTreeItemIdValue
& cookie
) const
445 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
446 wxTreeItemIdValue
& cookie
) const
451 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
456 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
461 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
466 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
471 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
476 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
481 // ----------------------------------------------------------------------------
482 // multiple selections emulation
483 // ----------------------------------------------------------------------------
485 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
490 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
494 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
499 // ----------------------------------------------------------------------------
501 // ----------------------------------------------------------------------------
503 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
504 wxTreeItemId hInsertAfter
,
505 const wxString
& text
,
506 int image
, int selectedImage
,
507 wxTreeItemData
*data
)
512 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
513 int image
, int selectedImage
,
514 wxTreeItemData
*data
)
519 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
520 const wxString
& text
,
521 int image
, int selectedImage
,
522 wxTreeItemData
*data
)
527 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
528 const wxTreeItemId
& idPrevious
,
529 const wxString
& text
,
530 int image
, int selectedImage
,
531 wxTreeItemData
*data
)
536 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
538 const wxString
& text
,
539 int image
, int selectedImage
,
540 wxTreeItemData
*data
)
545 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
546 const wxString
& text
,
547 int image
, int selectedImage
,
548 wxTreeItemData
*data
)
553 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
558 // delete all children (but don't delete the item itself)
559 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
563 void wxTreeCtrl::DeleteAllItems()
567 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
571 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
575 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
579 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
583 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
587 void wxTreeCtrl::Unselect()
591 void wxTreeCtrl::UnselectAll()
595 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
599 void wxTreeCtrl::UnselectItem(const wxTreeItemId
& item
)
603 void wxTreeCtrl::ToggleItemSelection(const wxTreeItemId
& item
)
607 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
611 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
615 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
620 void wxTreeCtrl::DeleteTextCtrl()
624 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
625 wxClassInfo
* textControlClass
)
630 // End label editing, optionally cancelling the edit
631 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
635 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
640 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
647 // ----------------------------------------------------------------------------
649 // ----------------------------------------------------------------------------
651 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
652 // functions such as IsDataIndirect()
653 class wxTreeSortHelper
656 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
659 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
661 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
662 if ( tree
->IsDataIndirect(data
) )
664 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
667 return data
->GetId();
671 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
675 wxCHECK_MSG( pItem1
&& pItem2
, 0,
676 wxT("sorting tree without data doesn't make sense") );
678 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
680 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
681 GetIdFromData(tree
, pItem2
));
684 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
685 const wxTreeItemId
& item2
)
687 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
690 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
692 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
694 // rely on the fact that TreeView_SortChildren does the same thing as our
695 // default behaviour, i.e. sorts items alphabetically and so call it
696 // directly if we're not in derived class (much more efficient!)
697 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
699 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
704 tvSort
.hParent
= HITEM(item
);
705 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
706 tvSort
.lParam
= (LPARAM
)this;
707 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
711 // ----------------------------------------------------------------------------
713 // ----------------------------------------------------------------------------
715 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
716 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
718 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
722 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
727 #endif // wxUSE_TREECTRL