]>
git.saurik.com Git - wxWidgets.git/blob - samples/treectrl/treetest.cpp
65e281c84e0483245131bf3dc40eb6f2cb5fc104
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
)
47 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
48 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginDrag
)
49 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl
, MyTreeCtrl::OnBeginRDrag
)
50 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnBeginLabelEdit
)
51 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl
, MyTreeCtrl::OnEndLabelEdit
)
52 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl
, MyTreeCtrl::OnDeleteItem
)
53 EVT_TREE_GET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnGetInfo
)
54 EVT_TREE_SET_INFO(TreeTest_Ctrl
, MyTreeCtrl::OnSetInfo
)
55 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanded
)
56 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl
, MyTreeCtrl::OnItemExpanding
)
57 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsed
)
58 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl
, MyTreeCtrl::OnItemCollapsing
)
59 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanged
)
60 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl
, MyTreeCtrl::OnSelChanging
)
61 EVT_TREE_KEY_DOWN(TreeTest_Ctrl
, MyTreeCtrl::OnKeyDown
)
66 // `Main program' equivalent, creating windows and returning main app frame
69 // Create the main frame window
70 MyFrame
*frame
= new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
80 // My frame constructor
81 MyFrame::MyFrame(const wxString
& title
, int x
, int y
, int w
, int h
)
82 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
84 // This reduces flicker effects - even better would be to define
85 // OnEraseBackground to do nothing. When the tree control's scrollbars are
86 // show or hidden, the frame is sent a background erase event.
87 SetBackgroundColour(wxColour(255, 255, 255));
90 SetIcon(wxICON(mondrian
));
93 wxMenu
*file_menu
= new wxMenu
;
95 file_menu
->Append(TreeTest_About
, "&About...");
96 file_menu
->Append(TreeTest_Quit
, "E&xit");
97 wxMenuBar
*menu_bar
= new wxMenuBar
;
98 menu_bar
->Append(file_menu
, "&File");
101 // Make a panel with a message
102 m_treeCtrl
= new MyTreeCtrl(this, TreeTest_Ctrl
,
103 wxDefaultPosition
, wxDefaultSize
,
104 wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
);
105 wxTextCtrl
*textCtrl
= new wxTextCtrl(this, -1, "",
106 wxDefaultPosition
, wxDefaultSize
,
107 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
109 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
110 c
->top
.SameAs(this, wxTop
);
111 c
->left
.SameAs(this, wxLeft
);
112 c
->right
.SameAs(this, wxRight
);
113 c
->height
.PercentOf(this, wxHeight
, 66);
114 m_treeCtrl
->SetConstraints(c
);
116 c
= new wxLayoutConstraints
;
117 c
->top
.Below(m_treeCtrl
);
118 c
->left
.SameAs(this, wxLeft
);
119 c
->right
.SameAs(this, wxRight
);
120 c
->bottom
.SameAs(this, wxBottom
);
121 textCtrl
->SetConstraints(c
);
124 // create a status bar with 3 panes
126 SetStatusText("", 0);
128 // set our text control as the log target
129 wxLogTextCtrl
*logWindow
= new wxLogTextCtrl(textCtrl
);
130 delete wxLog::SetActiveTarget(logWindow
);
135 delete wxLog::SetActiveTarget(NULL
);
138 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
143 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
145 wxMessageDialog
dialog(this, "Tree test sample\nJulian Smart (c) 1997",
146 "About tree test", wxOK
);
151 // MyTreeCtrl implementation
152 MyTreeCtrl::MyTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
153 const wxPoint
& pos
, const wxSize
& size
,
155 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
157 // Make an image list containing small icons
158 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
160 // should correspond to TreeCtrlIcon_xxx enum
161 m_imageListNormal
->Add(wxICON(icon2
));
162 m_imageListNormal
->Add(wxICON(icon1
));
164 SetImageList(m_imageListNormal
);
166 // Add some items to the tree
167 AddTestItemsToTree(4, 3);
170 MyTreeCtrl::~MyTreeCtrl()
172 delete m_imageListNormal
;
175 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId
& idParent
,
182 for ( size_t n
= 0; n
< numChildren
; n
++ )
184 // at depth 1 elements won't have any more children
185 str
.Printf("%s child %d", depth
== 1 ? "File" : "Folder", n
+ 1);
186 int image
= depth
== 1 ? TreeCtrlIcon_File
: TreeCtrlIcon_Folder
;
187 wxTreeItemId id
= AppendItem(idParent
, str
, image
, image
,
188 new MyTreeItemData(str
));
189 AddItemsRecursively(id
, numChildren
, depth
- 1);
195 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren
,
198 wxTreeItemId rootId
= AddRoot("Root",
199 TreeCtrlIcon_Folder
, TreeCtrlIcon_Folder
,
200 new MyTreeItemData("Root item"));
202 AddItemsRecursively(rootId
, numChildren
, depth
);
206 #define TREE_EVENT_HANDLER(name) \
207 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
209 wxLogMessage(#name); \
212 TREE_EVENT_HANDLER(OnBeginDrag
)
213 TREE_EVENT_HANDLER(OnBeginRDrag
)
214 TREE_EVENT_HANDLER(OnBeginLabelEdit
)
215 TREE_EVENT_HANDLER(OnEndLabelEdit
)
216 TREE_EVENT_HANDLER(OnDeleteItem
)
217 TREE_EVENT_HANDLER(OnGetInfo
)
218 TREE_EVENT_HANDLER(OnSetInfo
)
219 TREE_EVENT_HANDLER(OnItemExpanded
)
220 TREE_EVENT_HANDLER(OnItemExpanding
)
221 TREE_EVENT_HANDLER(OnItemCollapsed
)
222 TREE_EVENT_HANDLER(OnSelChanged
)
223 TREE_EVENT_HANDLER(OnSelChanging
)
225 #undef TREE_EVENT_HANDLER
227 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent
& event
)
229 wxLogMessage("OnItemCollapsing");
231 // for testing, prevent the user from collapsing the root item
232 wxTreeItemId itemId
= event
.GetItem();
233 if ( !GetParent(itemId
).IsOk() )
235 wxMessageBox("You can't collapse the root item.");
241 void MyTreeCtrl::OnKeyDown(wxTreeEvent
& event
)
243 // show some info about this item
244 wxTreeItemId itemId
= GetSelection();
245 MyTreeItemData
*item
= (MyTreeItemData
*)GetItemData(itemId
);
249 item
->ShowInfo(this);
252 wxLogMessage("OnKeyDown");
255 static inline const char *Bool2String(bool b
)
257 return b
? "" : "not ";
260 void MyTreeItemData::ShowInfo(wxTreeCtrl
*tree
)
262 wxLogMessage("Item '%s': %sselected, %sexpanded.",
264 Bool2String(tree
->IsSelected(GetId())),
265 Bool2String(tree
->IsExpanded(GetId())));