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"
29 #include "mondrian.xpm"
32 #include "wx/treectrl.h"
36 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
37 EVT_MENU(TREE_QUIT
, MyFrame::OnQuit
)
38 EVT_MENU(TREE_ABOUT
, MyFrame::OnAbout
)
41 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
42 EVT_TREE_BEGIN_DRAG(TREE_CTRL
, MyTreeCtrl::OnBeginDrag
)
43 EVT_TREE_BEGIN_RDRAG(TREE_CTRL
, MyTreeCtrl::OnBeginRDrag
)
44 EVT_TREE_BEGIN_LABEL_EDIT(TREE_CTRL
, MyTreeCtrl::OnBeginLabelEdit
)
45 EVT_TREE_END_LABEL_EDIT(TREE_CTRL
, MyTreeCtrl::OnEndLabelEdit
)
46 EVT_TREE_DELETE_ITEM(TREE_CTRL
, MyTreeCtrl::OnDeleteItem
)
47 EVT_TREE_GET_INFO(TREE_CTRL
, MyTreeCtrl::OnGetInfo
)
48 EVT_TREE_SET_INFO(TREE_CTRL
, MyTreeCtrl::OnSetInfo
)
49 EVT_TREE_ITEM_EXPANDED(TREE_CTRL
, MyTreeCtrl::OnItemExpanded
)
50 EVT_TREE_ITEM_EXPANDING(TREE_CTRL
, MyTreeCtrl::OnItemExpanding
)
51 EVT_TREE_SEL_CHANGED(TREE_CTRL
, MyTreeCtrl::OnSelChanged
)
52 EVT_TREE_SEL_CHANGING(TREE_CTRL
, MyTreeCtrl::OnSelChanging
)
53 EVT_TREE_KEY_DOWN(TREE_CTRL
, MyTreeCtrl::OnKeyDown
)
58 // `Main program' equivalent, creating windows and returning main app frame
59 bool MyApp::OnInit(void)
61 // Create the main frame window
62 MyFrame
*frame
= new MyFrame(NULL
, "wxTreeCtrl Test", 50, 50, 450, 340);
64 // This reduces flicker effects - even better would be to define OnEraseBackground
65 // to do nothing. When the tree control's scrollbars are show or hidden, the
66 // frame is sent a background erase event.
67 frame
->SetBackgroundColour(wxColour(255, 255, 255));
71 frame
->SetIcon(wxIcon("mondrian"));
73 frame
->SetIcon(wxIcon(mondrian_xpm
));
76 // Make an image list containing small icons
77 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
80 wxIcon
icon1("icon1", wxBITMAP_TYPE_ICO_RESOURCE
);
81 m_imageListNormal
->Add(icon1
);
82 wxIcon
icon2("icon2", wxBITMAP_TYPE_ICO_RESOURCE
);
83 m_imageListNormal
->Add(icon2
);
88 wxMenu
*file_menu
= new wxMenu
;
90 file_menu
->Append(TREE_ABOUT
, "&About");
91 file_menu
->Append(TREE_QUIT
, "E&xit");
92 wxMenuBar
*menu_bar
= new wxMenuBar
;
93 menu_bar
->Append(file_menu
, "&File");
94 frame
->SetMenuBar(menu_bar
);
96 // Make a panel with a message
97 frame
->m_treeCtrl
= new MyTreeCtrl(frame
, TREE_CTRL
, wxPoint(0, 0), wxSize(400, 200),
98 wxTR_HAS_BUTTONS
|wxSUNKEN_BORDER
);
99 frame
->m_logWindow
= new wxTextCtrl(frame
, -1, "", wxPoint(0, 0), wxSize(400, 200),
100 wxTE_MULTILINE
|wxSUNKEN_BORDER
);
102 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
103 c
->top
.SameAs (frame
, wxTop
);
104 c
->left
.SameAs (frame
, wxLeft
);
105 c
->right
.SameAs (frame
, wxRight
);
106 c
->height
.PercentOf (frame
, wxHeight
, 66);
107 frame
->m_treeCtrl
->SetConstraints(c
);
109 c
= new wxLayoutConstraints
;
110 c
->top
.Below (frame
->m_treeCtrl
);
111 c
->left
.SameAs (frame
, wxLeft
);
112 c
->right
.SameAs (frame
, wxRight
);
113 c
->bottom
.SameAs (frame
, wxBottom
);
114 frame
->m_logWindow
->SetConstraints(c
);
115 frame
->SetAutoLayout(TRUE
);
117 frame
->m_treeCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
119 long rootId
= frame
->m_treeCtrl
->InsertItem(0, "Root", 0);
125 for ( i
= 0; i
< 10; i
++)
127 sprintf(buf
, "Folder child %d", i
);
129 long id
= frame
->m_treeCtrl
->InsertItem(rootId
, str
, 0);
131 for ( j
= 0; j
< 5; j
++)
133 sprintf(buf
, "File child %d", j
);
135 frame
->m_treeCtrl
->InsertItem(id
, str
, 1);
138 for ( i
= 0; i
< 10; i
++)
140 sprintf(buf
, "File child %d", i
);
142 frame
->m_treeCtrl
->InsertItem(rootId
, str
, 1);
145 frame
->CreateStatusBar(3);
146 frame
->SetStatusText("", 0);
156 // My frame constructor
157 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
158 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
164 MyFrame::~MyFrame(void)
166 delete wxGetApp().m_imageListNormal
;
169 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
174 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
176 wxMessageDialog
dialog(this, "Tree test sample\nJulian Smart (c) 1997",
177 "About tree test", wxOK
|wxCANCEL
);
184 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& WXUNUSED(event
) )
186 if ( !wxGetApp().GetTopWindow() )
189 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
196 str
<< "OnBeginDrag\n";
201 void MyTreeCtrl::OnBeginRDrag(wxTreeEvent
& WXUNUSED(event
) )
203 if ( !wxGetApp().GetTopWindow() )
206 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
213 str
<< "OnBeginRDrag\n";
218 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& WXUNUSED(event
) )
220 if ( !wxGetApp().GetTopWindow() )
223 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
230 str
<< "OnBeginLabelEdit\n";
235 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& WXUNUSED(event
) )
237 if ( !wxGetApp().GetTopWindow() )
240 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
247 str
<< "OnEndLabelEdit\n";
252 void MyTreeCtrl::OnDeleteItem(wxTreeEvent
& WXUNUSED(event
) )
254 if ( !wxGetApp().GetTopWindow() )
257 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
264 str
<< "OnDeleteItem\n";
269 void MyTreeCtrl::OnGetInfo(wxTreeEvent
& WXUNUSED(event
) )
271 if ( !wxGetApp().GetTopWindow() )
274 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
281 str
<< "OnGetInfo\n";
286 void MyTreeCtrl::OnSetInfo(wxTreeEvent
& WXUNUSED(event
) )
288 if ( !wxGetApp().GetTopWindow() )
291 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
298 str
<< "OnSetInfo\n";
303 void MyTreeCtrl::OnItemExpanded(wxTreeEvent
& WXUNUSED(event
) )
305 if ( !wxGetApp().GetTopWindow() )
308 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
315 str
<< "OnItemExpanded\n";
320 void MyTreeCtrl::OnItemExpanding(wxTreeEvent
& WXUNUSED(event
) )
322 if ( !wxGetApp().GetTopWindow() )
325 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
332 str
<< "OnItemExpanding\n";
337 void MyTreeCtrl::OnSelChanged(wxTreeEvent
& WXUNUSED(event
) )
339 if ( !wxGetApp().GetTopWindow() )
342 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
349 str
<< "OnSelChanged\n";
354 void MyTreeCtrl::OnSelChanging(wxTreeEvent
& WXUNUSED(event
) )
356 if ( !wxGetApp().GetTopWindow() )
359 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
366 str
<< "OnSelChanging\n";
371 void MyTreeCtrl::OnKeyDown(wxTreeEvent
& WXUNUSED(event
) )
373 if ( !wxGetApp().GetTopWindow() )
376 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
383 str
<< "OnKeyDown\n";