]>
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
)
46 EVT_MENU(TreeTest_Delete
, MyFrame::OnDelete
)
47 EVT_MENU(TreeTest_DeleteAll
, MyFrame::OnDeleteAll
)
48 EVT_MENU(TreeTest_Recreate
, MyFrame::OnRecreate
)
51 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
52 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
53 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
54 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
55 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
56 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
57 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
58 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
59 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
60 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
61 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
62 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
63 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
64 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
65 EVT_CHAR(MyTreeCtrl::OnKeyDown
)
70 // `Main program' equivalent, creating windows and returning main app frame
73 // Create the main frame window
74 MyFrame
*frame
= new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
84 // My frame constructor
85 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
86 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
88 // This reduces flicker effects - even better would be to define
89 // OnEraseBackground to do nothing. When the tree control's scrollbars are
90 // show or hidden, the frame is sent a background erase event.
91 SetBackgroundColour(wxColour(255, 255, 255));
94 SetIcon(wxICON(mondrian
));
97 wxMenu
*file_menu
= new wxMenu
;
99 file_menu
->Append(TreeTest_Dump
, "D&ump tree items");
100 file_menu
->Append(TreeTest_Delete
, "&Delete this item");
101 file_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
102 file_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
103 file_menu
->AppendSeparator();
104 file_menu
->Append(TreeTest_About
, "&About...");
105 file_menu
->AppendSeparator();
106 file_menu
->Append(TreeTest_Quit
, "E&xit");
108 wxMenuBar
*menu_bar
= new wxMenuBar
;
109 menu_bar
->Append(file_menu
, "&File");
110 SetMenuBar(menu_bar
);
112 // Make a panel with a message
113 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
114 wxDefaultPosition
, wxDefaultSize
,
115 wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
);
116 wxTextCtrl
*textCtrl
= new wxTextCtrl(this, -1, "",
117 wxDefaultPosition
, wxDefaultSize
,
118 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
120 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
121 c
->top
.SameAs(this, wxTop
);
122 c
->left
.SameAs(this, wxLeft
);
123 c
->right
.SameAs(this, wxRight
);
124 c
->height
.PercentOf(this, wxHeight
, 66);
125 m_treeCtrl
->SetConstraints(c
);
127 c
= new wxLayoutConstraints
;
128 c
->top
.Below(m_treeCtrl
);
129 c
->left
.SameAs(this, wxLeft
);
130 c
->right
.SameAs(this, wxRight
);
131 c
->bottom
.SameAs(this, wxBottom
);
132 textCtrl
->SetConstraints(c
);
135 // create a status bar with 3 panes
137 SetStatusText("", 0);
139 // set our text control as the log target
140 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(textCtrl
);
141 delete wxLog::SetActiveTarget(logWindow
);
146 delete wxLog::SetActiveTarget(NULL
);
149 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
154 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
156 wxMessageDialog
dialog(this,
158 "Julian Smart (c) 1997",
159 "About tree test", wxOK
);
164 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
166 wxTreeItemId root
= m_treeCtrl
->GetSelection();
167 m_treeCtrl
->GetItemsRecursively(root
, -1);
170 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
172 wxTreeItemId item
= m_treeCtrl
->GetSelection();
173 m_treeCtrl
->Delete(item
);
176 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
178 m_treeCtrl
->DeleteAllItems();
181 void MyFrame::OnRecreate(wxCommandEvent
& event
)
184 m_treeCtrl
->AddTestItemsToTree(3, 2);
187 // MyTreeCtrl implementation
188 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
189 const wxPoint
& pos
, const wxSize
& size
,
191 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
193 // Make an image list containing small icons
194 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
196 // should correspond to TreeCtrlIcon_xxx enum
197 m_imageListNormal
->Add(wxICON(icon2
));
198 m_imageListNormal
->Add(wxICON(icon1
));
200 SetImageList(m_imageListNormal
);
202 // Add some items to the tree
203 AddTestItemsToTree(3, 2);
206 MyTreeCtrl::~MyTreeCtrl()
208 delete m_imageListNormal
;
211 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
219 for ( size_t n
= 0; n
< numChildren
; n
++ )
221 // at depth 1 elements won't have any more children
223 str
.Printf("%s child %d.%d", "File", folder
, n
+ 1);
225 str
.Printf("%s child %d","Folder", n
+ 1);
227 int image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
228 wxTreeItemId id
= AppendItem(idParent
, str
, image
, image
,
229 new MyTreeItemData(str
));
230 AddItemsRecursively(id
, numChildren
, depth
- 1,n
+1);
236 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
239 wxTreeItemId rootId
= AddRoot("Root",
240 TreeCtrlIcon_Folder
, TreeCtrlIcon_Folder
,
241 new MyTreeItemData("Root item"));
243 AddItemsRecursively(rootId
, numChildren
, depth
,0);
246 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
251 id
= GetFirstChild(idParent
, cookie
);
253 id
= GetNextChild(idParent
, cookie
);
258 wxString text
=GetItemText(id
);
261 if (ItemHasChildren(id
))
262 GetItemsRecursively(id
,-1);
264 GetItemsRecursively(idParent
, cookie
);
269 #define TREE_EVENT_HANDLER(name) \
270 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
272 wxLogMessage(#name); \
275 TREE_EVENT_HANDLER(OnBeginDrag
)
276 TREE_EVENT_HANDLER(OnBeginRDrag
)
277 TREE_EVENT_HANDLER(OnBeginLabelEdit
)
278 TREE_EVENT_HANDLER(OnEndLabelEdit
)
279 TREE_EVENT_HANDLER(OnDeleteItem
)
280 TREE_EVENT_HANDLER(OnGetInfo
)
281 TREE_EVENT_HANDLER(OnSetInfo
)
282 TREE_EVENT_HANDLER(OnItemExpanded
)
283 TREE_EVENT_HANDLER(OnItemExpanding
)
284 TREE_EVENT_HANDLER(OnItemCollapsed
)
285 TREE_EVENT_HANDLER(OnSelChanged
)
286 TREE_EVENT_HANDLER(OnSelChanging
)
288 #undef TREE_EVENT_HANDLER
290 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
292 wxLogMessage("OnItemCollapsing");
294 // for testing, prevent the user from collapsing the root item
295 wxTreeItemId itemId
= event
.GetItem();
296 if ( !GetParent(itemId
).IsOk() )
298 wxMessageBox("You can't collapse the root item.");
304 void MyTreeCtrl::OnKeyDown(wxTreeEvent
& WXUNUSED(event
))
306 // show some info about this item
307 wxTreeItemId itemId
= GetSelection();
308 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
312 item
->ShowInfo(this);
315 wxLogMessage("OnKeyDown");
318 static inline const char *Bool2String(bool b
)
320 return b
? "" : "not ";
323 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
325 wxLogMessage("Item '%s': %sselected, %sexpanded, "
326 "%u children (%u immediately under this item).",
328 Bool2String(tree
->IsSelected(GetId())),
329 Bool2String(tree
->IsExpanded(GetId())),
330 tree
->GetChildrenCount(GetId()),
331 tree
->GetChildrenCount(GetId(), FALSE
));