1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/palmos/treectrl.cpp
4 // Author: William Osborne
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"
35 // Set this to 1 to be _absolutely_ sure that repainting will work for all
36 // comctl32.dll versions
37 #define wxUSE_COMCTL32_SAFELY 0
41 #include "wx/dynarray.h"
42 #include "wx/imaglist.h"
43 #include "wx/settings.h"
45 #include "wx/palmos/wrapcctl.h"
47 // macros to hide the cast ugliness
48 // --------------------------------
50 // ptr is the real item id, i.e. wxTreeItemId::m_pItem
51 #define HITEM_PTR(ptr) (HTREEITEM)(ptr)
53 // item here is a wxTreeItemId
54 #define HITEM(item) HITEM_PTR((item).m_pItem)
56 // the native control doesn't support multiple selections under MSW and we
57 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
58 // checkboxes be the selection status (checked == selected) or by really
59 // emulating everything, i.e. intercepting mouse and key events &c. The first
60 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
62 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 #if wxUSE_EXTENDED_RTTI
77 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
79 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
80 // new style border flags, we put them first to
81 // use them for streaming out
82 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
83 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
84 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
85 wxFLAGS_MEMBER(wxBORDER_RAISED
)
86 wxFLAGS_MEMBER(wxBORDER_STATIC
)
87 wxFLAGS_MEMBER(wxBORDER_NONE
)
89 // old style border flags
90 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
91 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
92 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
93 wxFLAGS_MEMBER(wxRAISED_BORDER
)
94 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
95 wxFLAGS_MEMBER(wxBORDER
)
97 // standard window styles
98 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
99 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
100 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
101 wxFLAGS_MEMBER(wxWANTS_CHARS
)
102 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
103 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
104 wxFLAGS_MEMBER(wxVSCROLL
)
105 wxFLAGS_MEMBER(wxHSCROLL
)
107 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
108 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
109 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
110 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
111 wxFLAGS_MEMBER(wxTR_NO_LINES
)
112 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
113 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
114 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
115 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
116 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
117 wxFLAGS_MEMBER(wxTR_SINGLE
)
118 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
119 wxFLAGS_MEMBER(wxTR_EXTENDED
)
120 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
122 wxEND_FLAGS( wxTreeCtrlStyle
)
124 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
126 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
127 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
128 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
129 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
130 wxEND_PROPERTIES_TABLE()
132 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
133 wxEND_HANDLERS_TABLE()
135 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
137 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 // indices in gs_expandEvents table below
159 // handy table for sending events - it has to be initialized during run-time
160 // now so can't be const any more
161 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
164 but logically it's a const table with the following entries:
167 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
168 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
172 // ============================================================================
174 // ============================================================================
176 // ----------------------------------------------------------------------------
177 // construction and destruction
178 // ----------------------------------------------------------------------------
180 void wxTreeCtrl::Init()
184 bool wxTreeCtrl::Create(wxWindow
*parent
,
189 const wxValidator
& validator
,
190 const wxString
& name
)
195 wxTreeCtrl::~wxTreeCtrl()
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
203 /* static */ wxVisualAttributes
204 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
206 wxVisualAttributes attrs
;
212 // simple wrappers which add error checking in debug mode
214 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
219 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
223 size_t wxTreeCtrl::GetCount() const
228 unsigned int wxTreeCtrl::GetIndent() const
233 void wxTreeCtrl::SetIndent(unsigned int indent
)
237 wxImageList
*wxTreeCtrl::GetImageList() const
239 return m_imageListNormal
;
242 wxImageList
*wxTreeCtrl::GetStateImageList() const
244 return m_imageListState
;
247 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
251 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
255 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
259 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
263 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
267 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
268 bool recursively
) const
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
282 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
291 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
296 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
300 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
301 wxTreeItemIcon which
) const
306 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
308 wxTreeItemIcon which
) const
312 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
318 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
319 wxTreeItemIcon which
) const
324 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
325 wxTreeItemIcon which
)
329 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
334 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
338 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
339 wxTreeItemIndirectData
*data
)
343 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
348 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
352 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
356 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
360 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
364 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
369 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
374 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
379 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
384 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
389 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
397 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
402 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
407 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
412 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
417 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
422 // ----------------------------------------------------------------------------
424 // ----------------------------------------------------------------------------
426 wxTreeItemId
wxTreeCtrl::GetRootItem() const
428 // Root may be real (visible) or virtual (hidden).
429 if ( GET_VIRTUAL_ROOT() )
432 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
435 wxTreeItemId
wxTreeCtrl::GetSelection() const
440 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
445 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
446 wxTreeItemIdValue
& cookie
) const
451 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
452 wxTreeItemIdValue
& cookie
) const
457 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
462 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
467 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
472 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
477 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
482 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
487 // ----------------------------------------------------------------------------
488 // multiple selections emulation
489 // ----------------------------------------------------------------------------
491 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
496 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
500 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
505 // ----------------------------------------------------------------------------
507 // ----------------------------------------------------------------------------
509 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
510 wxTreeItemId hInsertAfter
,
511 const wxString
& text
,
512 int image
, int selectedImage
,
513 wxTreeItemData
*data
)
518 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
519 int image
, int selectedImage
,
520 wxTreeItemData
*data
)
525 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
526 const wxString
& text
,
527 int image
, int selectedImage
,
528 wxTreeItemData
*data
)
533 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
534 const wxTreeItemId
& idPrevious
,
535 const wxString
& text
,
536 int image
, int selectedImage
,
537 wxTreeItemData
*data
)
542 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
544 const wxString
& text
,
545 int image
, int selectedImage
,
546 wxTreeItemData
*data
)
551 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
552 const wxString
& text
,
553 int image
, int selectedImage
,
554 wxTreeItemData
*data
)
559 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
564 // delete all children (but don't delete the item itself)
565 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
569 void wxTreeCtrl::DeleteAllItems()
573 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
577 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
581 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
585 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
589 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
593 void wxTreeCtrl::Unselect()
597 void wxTreeCtrl::UnselectAll()
601 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
605 void wxTreeCtrl::UnselectItem(const wxTreeItemId
& item
)
609 void wxTreeCtrl::ToggleItemSelection(const wxTreeItemId
& item
)
613 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
617 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
621 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
626 void wxTreeCtrl::DeleteTextCtrl()
630 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
631 wxClassInfo
* textControlClass
)
636 // End label editing, optionally cancelling the edit
637 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
641 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
646 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
653 // ----------------------------------------------------------------------------
655 // ----------------------------------------------------------------------------
657 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
658 // functions such as IsDataIndirect()
659 class wxTreeSortHelper
662 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
665 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
667 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
668 if ( tree
->IsDataIndirect(data
) )
670 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
673 return data
->GetId();
677 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
681 wxCHECK_MSG( pItem1
&& pItem2
, 0,
682 wxT("sorting tree without data doesn't make sense") );
684 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
686 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
687 GetIdFromData(tree
, pItem2
));
690 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
691 const wxTreeItemId
& item2
)
693 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
696 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
698 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
700 // rely on the fact that TreeView_SortChildren does the same thing as our
701 // default behaviour, i.e. sorts items alphabetically and so call it
702 // directly if we're not in derived class (much more efficient!)
703 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
705 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
710 tvSort
.hParent
= HITEM(item
);
711 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
712 tvSort
.lParam
= (LPARAM
)this;
713 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
717 // ----------------------------------------------------------------------------
719 // ----------------------------------------------------------------------------
721 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
722 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
724 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
728 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
733 #endif // wxUSE_TREECTRL