]>
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_Bold
, MyFrame::OnSetBold
)
47 EVT_MENU(TreeTest_UnBold
, MyFrame::OnClearBold
)
48 EVT_MENU(TreeTest_Delete
, MyFrame::OnDelete
)
49 EVT_MENU(TreeTest_DeleteAll
, MyFrame::OnDeleteAll
)
50 EVT_MENU(TreeTest_Recreate
, MyFrame::OnRecreate
)
53 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
54 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
55 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
56 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
57 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
58 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
59 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
60 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
61 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
62 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
63 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
64 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
65 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
66 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
67 EVT_CHAR(MyTreeCtrl::OnKeyDown
)
72 // `Main program' equivalent, creating windows and returning main app frame
75 // Create the main frame window
76 MyFrame
*frame
= new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
86 // My frame constructor
87 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
88 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
90 // This reduces flicker effects - even better would be to define
91 // OnEraseBackground to do nothing. When the tree control's scrollbars are
92 // show or hidden, the frame is sent a background erase event.
93 SetBackgroundColour(wxColour(255, 255, 255));
96 SetIcon(wxICON(mondrian
));
99 wxMenu
*file_menu
= new wxMenu
;
101 file_menu
->Append(TreeTest_Dump
, "D&ump tree items");
102 file_menu
->Append(TreeTest_Delete
, "&Delete this item");
103 file_menu
->Append(TreeTest_DeleteAll
, "Delete &all items");
104 file_menu
->Append(TreeTest_Recreate
, "&Recreate the tree");
105 file_menu
->AppendSeparator();
106 file_menu
->Append(TreeTest_Bold
, "Make item &bold");
107 file_menu
->Append(TreeTest_UnBold
, "Make item ¬ bold");
108 file_menu
->AppendSeparator();
109 file_menu
->Append(TreeTest_About
, "&About...");
110 file_menu
->AppendSeparator();
111 file_menu
->Append(TreeTest_Quit
, "E&xit");
113 wxMenuBar
*menu_bar
= new wxMenuBar
;
114 menu_bar
->Append(file_menu
, "&File");
115 SetMenuBar(menu_bar
);
117 // Make a panel with a message
118 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
119 wxDefaultPosition
, wxDefaultSize
,
120 wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
);
121 wxTextCtrl
*textCtrl
= new wxTextCtrl(this, -1, "",
122 wxDefaultPosition
, wxDefaultSize
,
123 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
125 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
126 c
->top
.SameAs(this, wxTop
);
127 c
->left
.SameAs(this, wxLeft
);
128 c
->right
.SameAs(this, wxRight
);
129 c
->height
.PercentOf(this, wxHeight
, 66);
130 m_treeCtrl
->SetConstraints(c
);
132 c
= new wxLayoutConstraints
;
133 c
->top
.Below(m_treeCtrl
);
134 c
->left
.SameAs(this, wxLeft
);
135 c
->right
.SameAs(this, wxRight
);
136 c
->bottom
.SameAs(this, wxBottom
);
137 textCtrl
->SetConstraints(c
);
140 // create a status bar with 3 panes
142 SetStatusText("", 0);
144 // set our text control as the log target
145 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(textCtrl
);
146 delete wxLog::SetActiveTarget(logWindow
);
151 delete wxLog::SetActiveTarget(NULL
);
154 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
159 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
161 wxMessageDialog
dialog(this,
163 "Julian Smart (c) 1997",
164 "About tree test", wxOK
);
169 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
171 wxTreeItemId root
= m_treeCtrl
->GetSelection();
172 m_treeCtrl
->GetItemsRecursively(root
, -1);
175 void MyFrame::DoSetBold(bool bold
)
177 // m_treeCtrl->SetItemBold(m_treeCtrl->GetSelection(), bold);
180 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
182 wxTreeItemId item
= m_treeCtrl
->GetSelection();
183 m_treeCtrl
->Delete(item
);
186 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
188 m_treeCtrl
->DeleteAllItems();
191 void MyFrame::OnRecreate(wxCommandEvent
& event
)
194 m_treeCtrl
->AddTestItemsToTree(3, 2);
197 // MyTreeCtrl implementation
198 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
199 const wxPoint
& pos
, const wxSize
& size
,
201 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
203 // Make an image list containing small icons
204 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
206 // should correspond to TreeCtrlIcon_xxx enum
207 m_imageListNormal
->Add(wxICON(icon1
));
208 m_imageListNormal
->Add(wxICON(icon2
));
210 SetImageList(m_imageListNormal
);
212 // Add some items to the tree
213 AddTestItemsToTree(3, 2);
216 MyTreeCtrl::~MyTreeCtrl()
218 delete m_imageListNormal
;
221 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
229 for ( size_t n
= 0; n
< numChildren
; n
++ )
231 // at depth 1 elements won't have any more children
233 str
.Printf("%s child %d.%d", "File", folder
, n
+ 1);
235 str
.Printf("%s child %d","Folder", n
+ 1);
237 int image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
238 wxTreeItemId id
= AppendItem(idParent
, str
, image
, image
,
239 new MyTreeItemData(str
));
240 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
246 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
249 wxTreeItemId rootId
= AddRoot("Root",
250 TreeCtrlIcon_Folder
, TreeCtrlIcon_Folder
,
251 new MyTreeItemData("Root item"));
253 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
256 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
261 id
= GetFirstChild(idParent
, cookie
);
263 id
= GetNextChild(idParent
, cookie
);
268 wxString text
=GetItemText(id
);
271 if (ItemHasChildren(id
))
272 GetItemsRecursively(id
,-1);
274 GetItemsRecursively(idParent
, cookie
);
279 #define TREE_EVENT_HANDLER(name) \
280 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
282 wxLogMessage(#name); \
285 TREE_EVENT_HANDLER(OnBeginDrag
)
286 TREE_EVENT_HANDLER(OnBeginRDrag
)
287 TREE_EVENT_HANDLER(OnBeginLabelEdit
)
288 TREE_EVENT_HANDLER(OnEndLabelEdit
)
289 TREE_EVENT_HANDLER(OnDeleteItem
)
290 TREE_EVENT_HANDLER(OnGetInfo
)
291 TREE_EVENT_HANDLER(OnSetInfo
)
292 TREE_EVENT_HANDLER(OnItemExpanded
)
293 TREE_EVENT_HANDLER(OnItemExpanding
)
294 TREE_EVENT_HANDLER(OnItemCollapsed
)
295 TREE_EVENT_HANDLER(OnSelChanged
)
296 TREE_EVENT_HANDLER(OnSelChanging
)
298 #undef TREE_EVENT_HANDLER
300 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
302 wxLogMessage("OnItemCollapsing");
304 // for testing, prevent the user from collapsing the root item
305 wxTreeItemId itemId
= event
.GetItem();
306 if ( !GetParent(itemId
).IsOk() )
308 wxMessageBox("You can't collapse the root item.");
314 void MyTreeCtrl::OnKeyDown(wxTreeEvent
& WXUNUSED(event
))
316 // show some info about this item
317 wxTreeItemId itemId
= GetSelection();
318 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
322 item
->ShowInfo(this);
325 wxLogMessage("OnKeyDown");
328 static inline const char *Bool2String(bool b
)
330 return b
? "" : "not ";
333 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
336 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
337 "%u children (%u immediately under this item).",
339 Bool2String(tree->IsSelected(GetId())),
340 Bool2String(tree->IsExpanded(GetId())),
341 Bool2String(tree->IsBold(GetId())),
342 tree->GetChildrenCount(GetId()),
343 tree->GetChildrenCount(GetId(), FALSE));