]>
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_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnTreeKeyDown
)
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);
145 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
146 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
147 delete wxLog::SetActiveTarget(new wxLogStderr
);
149 // set our text control as the log target
150 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(textCtrl
);
151 delete wxLog::SetActiveTarget(logWindow
);
157 delete wxLog::SetActiveTarget(NULL
);
160 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
165 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
167 wxMessageDialog
dialog(this,
169 "Julian Smart (c) 1997",
170 "About tree test", wxOK
);
175 void MyFrame::OnDump(wxCommandEvent
& WXUNUSED(event
))
177 wxTreeItemId root
= m_treeCtrl
->GetSelection();
178 m_treeCtrl
->GetItemsRecursively(root
, -1);
181 void MyFrame::DoSetBold(bool bold
)
183 m_treeCtrl
->SetItemBold(m_treeCtrl
->GetSelection(), bold
);
186 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
188 wxTreeItemId item
= m_treeCtrl
->GetSelection();
189 m_treeCtrl
->Delete(item
);
192 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
194 m_treeCtrl
->DeleteAllItems();
197 void MyFrame::OnRecreate(wxCommandEvent
& event
)
200 m_treeCtrl
->AddTestItemsToTree(3, 2);
203 // MyTreeCtrl implementation
204 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
205 const wxPoint
& pos
, const wxSize
& size
,
207 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
209 // Make an image list containing small icons
210 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
212 // should correspond to TreeCtrlIcon_xxx enum
213 m_imageListNormal
->Add(wxICON(icon1
));
214 m_imageListNormal
->Add(wxICON(icon2
));
216 SetImageList(m_imageListNormal
);
218 // Add some items to the tree
219 AddTestItemsToTree(3, 2);
222 MyTreeCtrl::~MyTreeCtrl()
224 delete m_imageListNormal
;
227 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
235 for ( size_t n
= 0; n
< numChildren
; n
++ )
237 // at depth 1 elements won't have any more children
239 str
.Printf("%s child %d.%d", "File", folder
, n
+ 1);
241 str
.Printf("%s child %d","Folder", n
+ 1);
243 int image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
244 wxTreeItemId id
= AppendItem(idParent
, str
, image
, image
,
245 new MyTreeItemData(str
));
246 AddItemsRecursively(id
, numChildren
, depth
- 1, n
+ 1);
252 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
255 wxTreeItemId rootId
= AddRoot("Root",
256 TreeCtrlIcon_Folder
, TreeCtrlIcon_Folder
,
257 new MyTreeItemData("Root item"));
259 AddItemsRecursively(rootId
, numChildren
, depth
, 0);
262 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId
& idParent
, long cookie
)
267 id
= GetFirstChild(idParent
, cookie
);
269 id
= GetNextChild(idParent
, cookie
);
274 wxString text
=GetItemText(id
);
277 if (ItemHasChildren(id
))
278 GetItemsRecursively(id
,-1);
280 GetItemsRecursively(idParent
, cookie
);
285 #define TREE_EVENT_HANDLER(name) \
286 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
288 wxLogMessage(#name); \
291 TREE_EVENT_HANDLER(OnBeginDrag
)
292 TREE_EVENT_HANDLER(OnBeginRDrag
)
293 TREE_EVENT_HANDLER(OnBeginLabelEdit
)
294 TREE_EVENT_HANDLER(OnEndLabelEdit
)
295 TREE_EVENT_HANDLER(OnDeleteItem
)
296 TREE_EVENT_HANDLER(OnGetInfo
)
297 TREE_EVENT_HANDLER(OnSetInfo
)
298 TREE_EVENT_HANDLER(OnItemExpanded
)
299 TREE_EVENT_HANDLER(OnItemExpanding
)
300 TREE_EVENT_HANDLER(OnItemCollapsed
)
301 TREE_EVENT_HANDLER(OnSelChanged
)
302 TREE_EVENT_HANDLER(OnSelChanging
)
304 #undef TREE_EVENT_HANDLER
306 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
308 wxLogMessage("OnItemCollapsing");
310 // for testing, prevent the user from collapsing the root item
311 wxTreeItemId itemId
= event
.GetItem();
312 if ( !GetParent(itemId
).IsOk() )
314 wxMessageBox("You can't collapse the root item.");
320 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent
&WXUNUSED(event
))
322 // show some info about this item
323 wxTreeItemId itemId
= GetSelection();
324 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
328 item
->ShowInfo(this);
331 wxLogMessage("OnTreeKeyDown");
334 static inline const char *Bool2String(bool b
)
336 return b
? "" : "not ";
339 void MyTreeItemData::ShowInfo(wxTreeCtrl */
*tree*/
)
342 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
343 "%u children (%u immediately under this item).",
345 Bool2String(tree->IsSelected(GetId())),
346 Bool2String(tree->IsExpanded(GetId())),
347 Bool2String(tree->IsBold(GetId())),
348 tree->GetChildrenCount(GetId()),
349 tree->GetChildrenCount(GetId(), FALSE));