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
);
76 wxIcon
icon1("icon1", wxBITMAP_TYPE_ICO_RESOURCE
);
77 m_imageListNormal
->Add(icon1
);
78 wxIcon
icon2("icon2", wxBITMAP_TYPE_ICO_RESOURCE
);
79 m_imageListNormal
->Add(icon2
);
82 wxMenu
*file_menu
= new wxMenu
;
84 file_menu
->Append(TREE_ABOUT
, "&About");
85 file_menu
->Append(TREE_QUIT
, "E&xit");
86 wxMenuBar
*menu_bar
= new wxMenuBar
;
87 menu_bar
->Append(file_menu
, "&File");
88 frame
->SetMenuBar(menu_bar
);
90 // Make a panel with a message
91 frame
->m_treeCtrl
= new MyTreeCtrl(frame
, TREE_CTRL
, wxPoint(0, 0), wxSize(400, 200),
92 wxTR_HAS_BUTTONS
|wxSUNKEN_BORDER
);
93 frame
->m_logWindow
= new wxTextCtrl(frame
, -1, "", wxPoint(0, 0), wxSize(400, 200),
94 wxTE_MULTILINE
|wxSUNKEN_BORDER
);
96 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
97 c
->top
.SameAs (frame
, wxTop
);
98 c
->left
.SameAs (frame
, wxLeft
);
99 c
->right
.SameAs (frame
, wxRight
);
100 c
->height
.PercentOf (frame
, wxHeight
, 66);
101 frame
->m_treeCtrl
->SetConstraints(c
);
103 c
= new wxLayoutConstraints
;
104 c
->top
.Below (frame
->m_treeCtrl
);
105 c
->left
.SameAs (frame
, wxLeft
);
106 c
->right
.SameAs (frame
, wxRight
);
107 c
->bottom
.SameAs (frame
, wxBottom
);
108 frame
->m_logWindow
->SetConstraints(c
);
109 frame
->SetAutoLayout(TRUE
);
111 frame
->m_treeCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
113 long rootId
= frame
->m_treeCtrl
->InsertItem(0, "Root", 0);
119 for ( i
= 0; i
< 10; i
++)
121 sprintf(buf
, "Folder child %d", i
);
123 long id
= frame
->m_treeCtrl
->InsertItem(rootId
, str
, 0);
125 for ( j
= 0; j
< 5; j
++)
127 sprintf(buf
, "File child %d", j
);
129 frame
->m_treeCtrl
->InsertItem(id
, str
, 1);
132 for ( i
= 0; i
< 10; i
++)
134 sprintf(buf
, "File child %d", i
);
136 frame
->m_treeCtrl
->InsertItem(rootId
, str
, 1);
139 frame
->CreateStatusBar(3);
140 frame
->SetStatusText("", 0);
150 // My frame constructor
151 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
152 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
158 MyFrame::~MyFrame(void)
160 delete wxGetApp().m_imageListNormal
;
163 void MyFrame::OnQuit(wxCommandEvent
& event
)
168 void MyFrame::OnAbout(wxCommandEvent
& event
)
170 wxMessageDialog
dialog(this, "Tree test sample\nJulian Smart (c) 1997",
171 "About tree test", wxOK
|wxCANCEL
);
178 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
180 if ( !wxGetApp().GetTopWindow() )
183 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
190 str
<< "OnBeginDrag\n";
195 void MyTreeCtrl::OnBeginRDrag(wxTreeEvent
& event
)
197 if ( !wxGetApp().GetTopWindow() )
200 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
207 str
<< "OnBeginRDrag\n";
212 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
214 if ( !wxGetApp().GetTopWindow() )
217 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
224 str
<< "OnBeginLabelEdit\n";
229 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
231 if ( !wxGetApp().GetTopWindow() )
234 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
241 str
<< "OnEndLabelEdit\n";
246 void MyTreeCtrl::OnDeleteItem(wxTreeEvent
& event
)
248 if ( !wxGetApp().GetTopWindow() )
251 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
258 str
<< "OnDeleteItem\n";
263 void MyTreeCtrl::OnGetInfo(wxTreeEvent
& event
)
265 if ( !wxGetApp().GetTopWindow() )
268 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
275 str
<< "OnGetInfo\n";
280 void MyTreeCtrl::OnSetInfo(wxTreeEvent
& event
)
282 if ( !wxGetApp().GetTopWindow() )
285 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
292 str
<< "OnSetInfo\n";
297 void MyTreeCtrl::OnItemExpanded(wxTreeEvent
& event
)
299 if ( !wxGetApp().GetTopWindow() )
302 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
309 str
<< "OnItemExpanded\n";
314 void MyTreeCtrl::OnItemExpanding(wxTreeEvent
& event
)
316 if ( !wxGetApp().GetTopWindow() )
319 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
326 str
<< "OnItemExpanding\n";
331 void MyTreeCtrl::OnSelChanged(wxTreeEvent
& event
)
333 if ( !wxGetApp().GetTopWindow() )
336 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
343 str
<< "OnSelChanged\n";
348 void MyTreeCtrl::OnSelChanging(wxTreeEvent
& event
)
350 if ( !wxGetApp().GetTopWindow() )
353 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
360 str
<< "OnSelChanging\n";
365 void MyTreeCtrl::OnKeyDown(wxTreeEvent
& event
)
367 if ( !wxGetApp().GetTopWindow() )
370 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
377 str
<< "OnKeyDown\n";