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 large icons
74 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
76 wxIcon
*icon
= new wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE
);
77 m_imageListNormal
->Add(*icon
);
79 icon
= new wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE
);
80 m_imageListNormal
->Add(*icon
);
84 wxMenu
*file_menu
= new wxMenu
;
86 file_menu
->Append(TREE_ABOUT
, "&About");
87 file_menu
->Append(TREE_QUIT
, "E&xit");
88 wxMenuBar
*menu_bar
= new wxMenuBar
;
89 menu_bar
->Append(file_menu
, "&File");
90 frame
->SetMenuBar(menu_bar
);
92 // Make a panel with a message
93 frame
->m_treeCtrl
= new MyTreeCtrl(frame
, TREE_CTRL
, wxPoint(0, 0), wxSize(400, 200),
94 wxTR_HAS_BUTTONS
|wxSUNKEN_BORDER
);
95 frame
->m_logWindow
= new wxTextCtrl(frame
, -1, "", wxPoint(0, 0), wxSize(400, 200),
96 wxTE_MULTILINE
|wxSUNKEN_BORDER
);
98 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
99 c
->top
.SameAs (frame
, wxTop
);
100 c
->left
.SameAs (frame
, wxLeft
);
101 c
->right
.SameAs (frame
, wxRight
);
102 c
->height
.PercentOf (frame
, wxHeight
, 66);
103 frame
->m_treeCtrl
->SetConstraints(c
);
105 c
= new wxLayoutConstraints
;
106 c
->top
.Below (frame
->m_treeCtrl
);
107 c
->left
.SameAs (frame
, wxLeft
);
108 c
->right
.SameAs (frame
, wxRight
);
109 c
->bottom
.SameAs (frame
, wxBottom
);
110 frame
->m_logWindow
->SetConstraints(c
);
111 frame
->SetAutoLayout(TRUE
);
113 frame
->m_treeCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
115 long rootId
= frame
->m_treeCtrl
->InsertItem(0, "Root", 0);
121 for ( i
= 0; i
< 10; i
++)
123 sprintf(buf
, "Folder child %d", i
);
125 long id
= frame
->m_treeCtrl
->InsertItem(rootId
, str
, 0);
127 for ( j
= 0; j
< 5; j
++)
129 sprintf(buf
, "File child %d", j
);
131 frame
->m_treeCtrl
->InsertItem(id
, str
, 1);
134 for ( i
= 0; i
< 10; i
++)
136 sprintf(buf
, "File child %d", i
);
138 frame
->m_treeCtrl
->InsertItem(rootId
, str
, 1);
141 frame
->CreateStatusBar(3);
142 frame
->SetStatusText("", 0);
152 // My frame constructor
153 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
154 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
160 MyFrame::~MyFrame(void)
162 delete wxGetApp().m_imageListNormal
;
165 void MyFrame::OnQuit(wxCommandEvent
& event
)
170 void MyFrame::OnAbout(wxCommandEvent
& event
)
172 wxMessageDialog
dialog(this, "Tree test sample\nJulian Smart (c) 1997",
173 "About tree test", wxOK
|wxCANCEL
);
180 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
182 if ( !wxGetApp().GetTopWindow() )
185 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
192 str
<< "OnBeginDrag\n";
197 void MyTreeCtrl::OnBeginRDrag(wxTreeEvent
& event
)
199 if ( !wxGetApp().GetTopWindow() )
202 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
209 str
<< "OnBeginRDrag\n";
214 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& event
)
216 if ( !wxGetApp().GetTopWindow() )
219 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
226 str
<< "OnBeginLabelEdit\n";
231 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& event
)
233 if ( !wxGetApp().GetTopWindow() )
236 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
243 str
<< "OnEndLabelEdit\n";
248 void MyTreeCtrl::OnDeleteItem(wxTreeEvent
& event
)
250 if ( !wxGetApp().GetTopWindow() )
253 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
260 str
<< "OnDeleteItem\n";
265 void MyTreeCtrl::OnGetInfo(wxTreeEvent
& event
)
267 if ( !wxGetApp().GetTopWindow() )
270 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
277 str
<< "OnGetInfo\n";
282 void MyTreeCtrl::OnSetInfo(wxTreeEvent
& event
)
284 if ( !wxGetApp().GetTopWindow() )
287 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
294 str
<< "OnSetInfo\n";
299 void MyTreeCtrl::OnItemExpanded(wxTreeEvent
& event
)
301 if ( !wxGetApp().GetTopWindow() )
304 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
311 str
<< "OnItemExpanded\n";
316 void MyTreeCtrl::OnItemExpanding(wxTreeEvent
& event
)
318 if ( !wxGetApp().GetTopWindow() )
321 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
328 str
<< "OnItemExpanding\n";
333 void MyTreeCtrl::OnSelChanged(wxTreeEvent
& event
)
335 if ( !wxGetApp().GetTopWindow() )
338 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
345 str
<< "OnSelChanged\n";
350 void MyTreeCtrl::OnSelChanging(wxTreeEvent
& event
)
352 if ( !wxGetApp().GetTopWindow() )
355 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
362 str
<< "OnSelChanging\n";
367 void MyTreeCtrl::OnKeyDown(wxTreeEvent
& event
)
369 if ( !wxGetApp().GetTopWindow() )
372 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
379 str
<< "OnKeyDown\n";