]>
git.saurik.com Git - wxWidgets.git/blob - samples/treectrl/treetest.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
28 // under Windows the icons are in the .rc file
32 #include "mondrian.xpm"
37 #include "wx/imaglist.h"
38 #include "wx/treectrl.h"
42 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
43 EVT_MENU(TreeTest_Quit
, MyFrame::OnQuit
)
44 EVT_MENU(TreeTest_About
, MyFrame::OnAbout
)
45 EVT_MENU(TreeTest_Dump
, MyFrame::OnDump
)
48 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
49 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
50 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
51 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
52 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
53 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
54 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
55 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
56 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
57 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
58 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
59 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
60 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
61 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
62 EVT_CHAR(MyTreeCtrl::OnKeyDown
)
67 // `Main program' equivalent, creating windows and returning main app frame
70 // Create the main frame window
71 MyFrame
*frame
= new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
81 // My frame constructor
82 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
83 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
85 // This reduces flicker effects - even better would be to define
86 // OnEraseBackground to do nothing. When the tree control's scrollbars are
87 // show or hidden, the frame is sent a background erase event.
88 SetBackgroundColour(wxColour(255, 255, 255));
91 SetIcon(wxICON(mondrian
));
94 wxMenu
*file_menu
= new wxMenu
;
96 file_menu
->Append(TreeTest_Dump
, "&Dump tree items");
97 file_menu
->AppendSeparator();
98 file_menu
->Append(TreeTest_About
, "&About...");
99 file_menu
->AppendSeparator();
100 file_menu
->Append(TreeTest_Quit
, "E&xit");
102 wxMenuBar
*menu_bar
= new wxMenuBar
;
103 menu_bar
->Append(file_menu
, "&File");
104 SetMenuBar(menu_bar
);
106 // Make a panel with a message
107 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
108 wxDefaultPosition
, wxDefaultSize
,
109 wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
);
110 wxTextCtrl
*textCtrl
= new wxTextCtrl(this, -1, "",
111 wxDefaultPosition
, wxDefaultSize
,
112 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
114 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
115 c
->top
.SameAs(this, wxTop
);
116 c
->left
.SameAs(this, wxLeft
);
117 c
->right
.SameAs(this, wxRight
);
118 c
->height
.PercentOf(this, wxHeight
, 66);
119 m_treeCtrl
->SetConstraints(c
);
121 c
= new wxLayoutConstraints
;
122 c
->top
.Below(m_treeCtrl
);
123 c
->left
.SameAs(this, wxLeft
);
124 c
->right
.SameAs(this, wxRight
);
125 c
->bottom
.SameAs(this, wxBottom
);
126 textCtrl
->SetConstraints(c
);
129 // create a status bar with 3 panes
131 SetStatusText("", 0);
133 // set our text control as the log target
134 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(textCtrl
);
135 delete wxLog::SetActiveTarget(logWindow
);
140 delete wxLog::SetActiveTarget(NULL
);
143 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
148 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
150 wxMessageDialog
dialog(this,
152 "Julian Smart (c) 1997",
153 "About tree test", wxOK
);
158 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
160 wxTreeItemId root
=m_treeCtrl
->GetSelection();
161 m_treeCtrl
->GetItemsRecursively(root
, -1);
164 // MyTreeCtrl implementation
165 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
166 const wxPoint
& pos
, const wxSize
& size
,
168 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
170 // Make an image list containing small icons
171 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
173 // should correspond to TreeCtrlIcon_xxx enum
174 m_imageListNormal
->Add(wxICON(icon2
));
175 m_imageListNormal
->Add(wxICON(icon1
));
177 SetImageList(m_imageListNormal
);
179 // Add some items to the tree
180 AddTestItemsToTree(3, 2);
183 MyTreeCtrl::~MyTreeCtrl()
185 delete m_imageListNormal
;
188 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
196 for ( size_t n
= 0; n
< numChildren
; n
++ )
198 // at depth 1 elements won't have any more children
200 str
.Printf("%s child %d.%d", "File", folder
, n
+ 1);
202 str
.Printf("%s child %d","Folder", n
+ 1);
204 int image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
205 wxTreeItemId id
= AppendItem(idParent
, str
, image
, image
,
206 new MyTreeItemData(str
));
207 AddItemsRecursively(id
, numChildren
, depth
- 1,n
+1);
213 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
216 wxTreeItemId rootId
= AddRoot("Root",
217 TreeCtrlIcon_Folder
, TreeCtrlIcon_Folder
,
218 new MyTreeItemData("Root item"));
220 AddItemsRecursively(rootId
, numChildren
, depth
,0);
223 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
228 id
= GetFirstChild(idParent
, cookie
);
230 id
= GetNextChild(idParent
, cookie
);
235 wxString text
=GetItemText(id
);
238 if (ItemHasChildren(id
))
239 GetItemsRecursively(id
,-1);
241 GetItemsRecursively(idParent
, cookie
);
246 #define TREE_EVENT_HANDLER(name) \
247 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
249 wxLogMessage(#name); \
252 TREE_EVENT_HANDLER(OnBeginDrag
)
253 TREE_EVENT_HANDLER(OnBeginRDrag
)
254 TREE_EVENT_HANDLER(OnBeginLabelEdit
)
255 TREE_EVENT_HANDLER(OnEndLabelEdit
)
256 TREE_EVENT_HANDLER(OnDeleteItem
)
257 TREE_EVENT_HANDLER(OnGetInfo
)
258 TREE_EVENT_HANDLER(OnSetInfo
)
259 TREE_EVENT_HANDLER(OnItemExpanded
)
260 TREE_EVENT_HANDLER(OnItemExpanding
)
261 TREE_EVENT_HANDLER(OnItemCollapsed
)
262 TREE_EVENT_HANDLER(OnSelChanged
)
263 TREE_EVENT_HANDLER(OnSelChanging
)
265 #undef TREE_EVENT_HANDLER
267 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
269 wxLogMessage("OnItemCollapsing");
271 // for testing, prevent the user from collapsing the root item
272 wxTreeItemId itemId
= event
.GetItem();
273 if ( !GetParent(itemId
).IsOk() )
275 wxMessageBox("You can't collapse the root item.");
281 void MyTreeCtrl::OnKeyDown(wxTreeEvent
& WXUNUSED(event
))
283 // show some info about this item
284 wxTreeItemId itemId
= GetSelection();
285 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
289 item
->ShowInfo(this);
292 wxLogMessage("OnKeyDown");
295 static inline const char *Bool2String(bool b
)
297 return b
? "" : "not ";
300 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
302 wxLogMessage("Item '%s': %sselected, %sexpanded, "
303 "%u children (%u immediately under this item).",
305 Bool2String(tree
->IsSelected(GetId())),
306 Bool2String(tree
->IsExpanded(GetId())),
307 tree
->GetChildrenCount(GetId()),
308 tree
->GetChildrenCount(GetId(), FALSE
));