]>
git.saurik.com Git - wxWidgets.git/blob - samples/treectrl/treetest.cpp
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"
31 #include "mondrian.xpm"
34 #include "wx/treectrl.h"
38 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
39 EVT_MENU(TREE_QUIT
, MyFrame::OnQuit
)
40 EVT_MENU(TREE_ABOUT
, MyFrame::OnAbout
)
43 BEGIN_EVENT_TABLE(MyTreeCtrl
, wxTreeCtrl
)
44 EVT_TREE_BEGIN_DRAG(TREE_CTRL
, MyTreeCtrl::OnBeginDrag
)
45 EVT_TREE_BEGIN_RDRAG(TREE_CTRL
, MyTreeCtrl::OnBeginRDrag
)
46 EVT_TREE_BEGIN_LABEL_EDIT(TREE_CTRL
, MyTreeCtrl::OnBeginLabelEdit
)
47 EVT_TREE_END_LABEL_EDIT(TREE_CTRL
, MyTreeCtrl::OnEndLabelEdit
)
48 EVT_TREE_DELETE_ITEM(TREE_CTRL
, MyTreeCtrl::OnDeleteItem
)
49 EVT_TREE_GET_INFO(TREE_CTRL
, MyTreeCtrl::OnGetInfo
)
50 EVT_TREE_SET_INFO(TREE_CTRL
, MyTreeCtrl::OnSetInfo
)
51 EVT_TREE_ITEM_EXPANDED(TREE_CTRL
, MyTreeCtrl::OnItemExpanded
)
52 EVT_TREE_ITEM_EXPANDING(TREE_CTRL
, MyTreeCtrl::OnItemExpanding
)
53 EVT_TREE_SEL_CHANGED(TREE_CTRL
, MyTreeCtrl::OnSelChanged
)
54 EVT_TREE_SEL_CHANGING(TREE_CTRL
, MyTreeCtrl::OnSelChanging
)
55 EVT_TREE_KEY_DOWN(TREE_CTRL
, MyTreeCtrl::OnKeyDown
)
60 // `Main program' equivalent, creating windows and returning main app frame
63 // Create the main frame window
64 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxTreeCtrl Test", 50, 50, 450, 340);
66 // This reduces flicker effects - even better would be to define OnEraseBackground
67 // to do nothing. When the tree control's scrollbars are show or hidden, the
68 // frame is sent a background erase event.
69 frame
->SetBackgroundColour(wxColour(255, 255, 255));
72 frame
->SetIcon(wxICON(mondrian
));
74 // Make an image list containing small icons
75 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
77 m_imageListNormal
->Add(wxICON(icon1
));
78 m_imageListNormal
->Add(wxICON(icon2
));
81 wxMenu
*file_menu
= new wxMenu
;
83 file_menu
->Append(TREE_ABOUT
, "&About");
84 file_menu
->Append(TREE_QUIT
, "E&xit");
85 wxMenuBar
*menu_bar
= new wxMenuBar
;
86 menu_bar
->Append(file_menu
, "&File");
87 frame
->SetMenuBar(menu_bar
);
89 // Make a panel with a message
90 frame
->m_treeCtrl
= new MyTreeCtrl(frame
, TREE_CTRL
, wxPoint(0, 0), wxSize(400, 200),
91 wxTR_HAS_BUTTONS
|wxSUNKEN_BORDER
);
92 frame
->m_logWindow
= new wxTextCtrl(frame
, -1, "", wxPoint(0, 0), wxSize(400, 200),
93 wxTE_MULTILINE
|wxSUNKEN_BORDER
);
95 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
96 c
->top
.SameAs (frame
, wxTop
);
97 c
->left
.SameAs (frame
, wxLeft
);
98 c
->right
.SameAs (frame
, wxRight
);
99 c
->height
.PercentOf (frame
, wxHeight
, 66);
100 frame
->m_treeCtrl
->SetConstraints(c
);
102 c
= new wxLayoutConstraints
;
103 c
->top
.Below (frame
->m_treeCtrl
);
104 c
->left
.SameAs (frame
, wxLeft
);
105 c
->right
.SameAs (frame
, wxRight
);
106 c
->bottom
.SameAs (frame
, wxBottom
);
107 frame
->m_logWindow
->SetConstraints(c
);
108 frame
->SetAutoLayout(TRUE
);
110 frame
->m_treeCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
112 long rootId
= frame
->m_treeCtrl
->InsertItem(0, "Root", 0);
118 for ( i
= 0; i
< 10; i
++)
120 sprintf(buf
, "Folder child %d", i
);
122 long id
= frame
->m_treeCtrl
->InsertItem(rootId
, str
, 0);
124 for ( j
= 0; j
< 5; j
++)
126 sprintf(buf
, "File child %d", j
);
128 frame
->m_treeCtrl
->InsertItem(id
, str
, 1);
131 for ( i
= 0; i
< 10; i
++)
133 sprintf(buf
, "File child %d", i
);
135 frame
->m_treeCtrl
->InsertItem(rootId
, str
, 1);
138 frame
->CreateStatusBar(3);
139 frame
->SetStatusText("", 0);
149 // My frame constructor
150 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
151 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
153 m_treeCtrl
= (MyTreeCtrl
*) NULL
;
154 m_logWindow
= (wxTextCtrl
*) NULL
;
159 delete wxGetApp().m_imageListNormal
;
162 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
167 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
169 wxMessageDialog
dialog(this, "Tree test sample\nJulian Smart (c) 1997",
170 "About tree test", wxOK
|wxCANCEL
);
177 void MyTreeCtrl::OnBeginDrag(wxTreeEvent
& WXUNUSED(event
) )
179 if ( !wxGetApp().GetTopWindow() )
182 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
189 str
<< "OnBeginDrag\n";
194 void MyTreeCtrl::OnBeginRDrag(wxTreeEvent
& WXUNUSED(event
) )
196 if ( !wxGetApp().GetTopWindow() )
199 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
206 str
<< "OnBeginRDrag\n";
211 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent
& WXUNUSED(event
) )
213 if ( !wxGetApp().GetTopWindow() )
216 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
223 str
<< "OnBeginLabelEdit\n";
228 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent
& WXUNUSED(event
) )
230 if ( !wxGetApp().GetTopWindow() )
233 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
240 str
<< "OnEndLabelEdit\n";
245 void MyTreeCtrl::OnDeleteItem(wxTreeEvent
& WXUNUSED(event
) )
247 if ( !wxGetApp().GetTopWindow() )
250 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
257 str
<< "OnDeleteItem\n";
262 void MyTreeCtrl::OnGetInfo(wxTreeEvent
& WXUNUSED(event
) )
264 if ( !wxGetApp().GetTopWindow() )
267 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
274 str
<< "OnGetInfo\n";
279 void MyTreeCtrl::OnSetInfo(wxTreeEvent
& WXUNUSED(event
) )
281 if ( !wxGetApp().GetTopWindow() )
284 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
291 str
<< "OnSetInfo\n";
296 void MyTreeCtrl::OnItemExpanded(wxTreeEvent
& WXUNUSED(event
) )
298 if ( !wxGetApp().GetTopWindow() )
301 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
308 str
<< "OnItemExpanded\n";
313 void MyTreeCtrl::OnItemExpanding(wxTreeEvent
& WXUNUSED(event
) )
315 if ( !wxGetApp().GetTopWindow() )
318 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
325 str
<< "OnItemExpanding\n";
330 void MyTreeCtrl::OnSelChanged(wxTreeEvent
& WXUNUSED(event
) )
332 if ( !wxGetApp().GetTopWindow() )
335 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
342 str
<< "OnSelChanged\n";
347 void MyTreeCtrl::OnSelChanging(wxTreeEvent
& WXUNUSED(event
) )
349 if ( !wxGetApp().GetTopWindow() )
352 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
359 str
<< "OnSelChanging\n";
364 void MyTreeCtrl::OnKeyDown(wxTreeEvent
& WXUNUSED(event
) )
366 if ( !wxGetApp().GetTopWindow() )
369 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
376 str
<< "OnKeyDown\n";