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 #include "wx/treectrl.h"
32 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
33 EVT_MENU(TREE_QUIT
, MyFrame::OnQuit
)
34 EVT_MENU(TREE_ABOUT
, MyFrame::OnAbout
)
37 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
38 EVT_TREE_BEGIN_DRAG(TREE_CTRL
, MyTreeCtrl::OnBeginDrag
)
39 EVT_TREE_BEGIN_RDRAG(TREE_CTRL
, MyTreeCtrl::OnBeginRDrag
)
40 EVT_TREE_BEGIN_LABEL_EDIT(TREE_CTRL
, MyTreeCtrl::OnBeginLabelEdit
)
41 EVT_TREE_END_LABEL_EDIT(TREE_CTRL
, MyTreeCtrl::OnEndLabelEdit
)
42 EVT_TREE_DELETE_ITEM(TREE_CTRL
, MyTreeCtrl::OnDeleteItem
)
43 EVT_TREE_GET_INFO(TREE_CTRL
, MyTreeCtrl::OnGetInfo
)
44 EVT_TREE_SET_INFO(TREE_CTRL
, MyTreeCtrl::OnSetInfo
)
45 EVT_TREE_ITEM_EXPANDED(TREE_CTRL
, MyTreeCtrl::OnItemExpanded
)
46 EVT_TREE_ITEM_EXPANDING(TREE_CTRL
, MyTreeCtrl::OnItemExpanding
)
47 EVT_TREE_SEL_CHANGED(TREE_CTRL
, MyTreeCtrl::OnSelChanged
)
48 EVT_TREE_SEL_CHANGING(TREE_CTRL
, MyTreeCtrl::OnSelChanging
)
49 EVT_TREE_KEY_DOWN(TREE_CTRL
, MyTreeCtrl::OnKeyDown
)
54 // `Main program' equivalent, creating windows and returning main app frame
55 bool MyApp::OnInit(void)
57 // Create the main frame window
58 MyFrame
*frame
= new MyFrame(NULL
, "wxTreeCtrl Test", 50, 50, 450, 340);
60 // This reduces flicker effects - even better would be to define OnEraseBackground
61 // to do nothing. When the tree control's scrollbars are show or hidden, the
62 // frame is sent a background erase event.
63 frame
->SetBackgroundColour(wxColour(255, 255, 255));
67 frame
->SetIcon(wxIcon("mondrian"));
70 frame
->SetIcon(wxIcon("aiai.xbm"));
73 // Make an image list containing small icons
74 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
77 wxIcon
icon1("icon1", wxBITMAP_TYPE_ICO_RESOURCE
);
78 m_imageListNormal
->Add(icon1
);
79 wxIcon
icon2("icon2", wxBITMAP_TYPE_ICO_RESOURCE
);
80 m_imageListNormal
->Add(icon2
);
85 wxMenu
*file_menu
= new wxMenu
;
87 file_menu
->Append(TREE_ABOUT
, "&About");
88 file_menu
->Append(TREE_QUIT
, "E&xit");
89 wxMenuBar
*menu_bar
= new wxMenuBar
;
90 menu_bar
->Append(file_menu
, "&File");
91 frame
->SetMenuBar(menu_bar
);
93 // Make a panel with a message
94 frame
->m_treeCtrl
= new MyTreeCtrl(frame
, TREE_CTRL
, wxPoint(0, 0), wxSize(400, 200),
95 wxTR_HAS_BUTTONS
|wxSUNKEN_BORDER
);
96 frame
->m_logWindow
= new wxTextCtrl(frame
, -1, "", wxPoint(0, 0), wxSize(400, 200),
97 wxTE_MULTILINE
|wxSUNKEN_BORDER
);
99 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
100 c
->top
.SameAs (frame
, wxTop
);
101 c
->left
.SameAs (frame
, wxLeft
);
102 c
->right
.SameAs (frame
, wxRight
);
103 c
->height
.PercentOf (frame
, wxHeight
, 66);
104 frame
->m_treeCtrl
->SetConstraints(c
);
106 c
= new wxLayoutConstraints
;
107 c
->top
.Below (frame
->m_treeCtrl
);
108 c
->left
.SameAs (frame
, wxLeft
);
109 c
->right
.SameAs (frame
, wxRight
);
110 c
->bottom
.SameAs (frame
, wxBottom
);
111 frame
->m_logWindow
->SetConstraints(c
);
112 frame
->SetAutoLayout(TRUE
);
114 frame
->m_treeCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
116 long rootId
= frame
->m_treeCtrl
->InsertItem(0, "Root", 0);
122 for ( i
= 0; i
< 10; i
++)
124 sprintf(buf
, "Folder child %d", i
);
126 long id
= frame
->m_treeCtrl
->InsertItem(rootId
, str
, 0);
128 for ( j
= 0; j
< 5; j
++)
130 sprintf(buf
, "File child %d", j
);
132 frame
->m_treeCtrl
->InsertItem(id
, str
, 1);
135 for ( i
= 0; i
< 10; i
++)
137 sprintf(buf
, "File child %d", i
);
139 frame
->m_treeCtrl
->InsertItem(rootId
, str
, 1);
142 frame
->CreateStatusBar(3);
143 frame
->SetStatusText("", 0);
153 // My frame constructor
154 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
155 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
161 MyFrame::~MyFrame(void)
163 delete wxGetApp().m_imageListNormal
;
166 void MyFrame::OnQuit(wxCommandEvent
& event
)
171 void MyFrame::OnAbout(wxCommandEvent
& event
)
173 wxMessageDialog
dialog(this, "Tree test sample\nJulian Smart (c) 1997",
174 "About tree test", wxOK
|wxCANCEL
);
181 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
183 if ( !wxGetApp().GetTopWindow() )
186 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
193 str
<< "OnBeginDrag\n";
198 void MyTreeCtrl::OnBeginRDrag(wxTreeEvent
& event
)
200 if ( !wxGetApp().GetTopWindow() )
203 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
210 str
<< "OnBeginRDrag\n";
215 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
217 if ( !wxGetApp().GetTopWindow() )
220 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
227 str
<< "OnBeginLabelEdit\n";
232 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
234 if ( !wxGetApp().GetTopWindow() )
237 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
244 str
<< "OnEndLabelEdit\n";
249 void MyTreeCtrl::OnDeleteItem(wxTreeEvent
& event
)
251 if ( !wxGetApp().GetTopWindow() )
254 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
261 str
<< "OnDeleteItem\n";
266 void MyTreeCtrl::OnGetInfo(wxTreeEvent
& event
)
268 if ( !wxGetApp().GetTopWindow() )
271 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
278 str
<< "OnGetInfo\n";
283 void MyTreeCtrl::OnSetInfo(wxTreeEvent
& event
)
285 if ( !wxGetApp().GetTopWindow() )
288 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
295 str
<< "OnSetInfo\n";
300 void MyTreeCtrl::OnItemExpanded(wxTreeEvent
& event
)
302 if ( !wxGetApp().GetTopWindow() )
305 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
312 str
<< "OnItemExpanded\n";
317 void MyTreeCtrl::OnItemExpanding(wxTreeEvent
& event
)
319 if ( !wxGetApp().GetTopWindow() )
322 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
329 str
<< "OnItemExpanding\n";
334 void MyTreeCtrl::OnSelChanged(wxTreeEvent
& event
)
336 if ( !wxGetApp().GetTopWindow() )
339 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
346 str
<< "OnSelChanged\n";
351 void MyTreeCtrl::OnSelChanging(wxTreeEvent
& event
)
353 if ( !wxGetApp().GetTopWindow() )
356 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
363 str
<< "OnSelChanging\n";
368 void MyTreeCtrl::OnKeyDown(wxTreeEvent
& event
)
370 if ( !wxGetApp().GetTopWindow() )
373 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
380 str
<< "OnKeyDown\n";