]> git.saurik.com Git - wxWidgets.git/blob - samples/treelay/treelay.cpp
tried to make Close() docs more clear
[wxWidgets.git] / samples / treelay / treelay.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: treelay.cpp
3 // Purpose: wxTreeLayout sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 7/4/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #include "wx/treelay.h"
24
25 #include "treelay.h"
26
27 wxTreeLayoutStored *myTree = (wxTreeLayoutStored *) NULL;
28
29 // A macro needed for some compilers (AIX) that need 'main' to be defined
30 // in the application itself.
31 IMPLEMENT_APP(MyApp)
32
33 // The `main program' equivalent, creating the windows and returning the
34 // main frame
35 bool MyApp::OnInit()
36 {
37 // Create the main frame window
38 MyFrame* frame = new MyFrame(NULL, _T("Tree Test"), wxPoint(-1, -1), wxSize(400, 550));
39
40 // Give it a status line
41 frame->CreateStatusBar(2);
42
43 // Give it an icon
44 #ifdef __WINDOWS__
45 wxIcon icon(_T("tree_icn"));
46 frame->SetIcon(icon);
47 #endif
48
49 // Make a menubar
50 wxMenu *file_menu = new wxMenu;
51 file_menu->Append(TEST_LEFT_RIGHT, _T("&Left to right"), _T("Redraw left to right"));
52 file_menu->Append(TEST_TOP_BOTTOM, _T("&Top to bottom"), _T("Redraw top to bottom"));
53 file_menu->AppendSeparator();
54 file_menu->Append(TEST_QUIT, _T("E&xit"), _T("Quit program"));
55
56 wxMenu *help_menu = new wxMenu;
57 help_menu->Append(TEST_ABOUT, _T("&About"), _T("About Tree Test"));
58
59 wxMenuBar* menu_bar = new wxMenuBar;
60
61 menu_bar->Append(file_menu, _T("&File"));
62 menu_bar->Append(help_menu, _T("&Help"));
63
64 // Associate the menu bar with the frame
65 frame->SetMenuBar(menu_bar);
66
67 MyCanvas *canvas = new MyCanvas(frame);
68
69 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
70 canvas->SetScrollbars(20, 20, 50, 50);
71 frame->canvas = canvas;
72
73 myTree = new wxTreeLayoutStored();
74
75 wxClientDC dc(canvas);
76 wxFont font(10, wxROMAN, wxNORMAL, wxBOLD);
77 dc.SetFont(font);
78 TreeTest(*myTree, dc);
79
80 frame->Show(TRUE);
81
82 frame->SetStatusText(_T("Hello, tree!"));
83
84 // Return the main frame window
85 return TRUE;
86 }
87
88 int MyApp::OnExit()
89 {
90 if (myTree)
91 {
92 delete myTree;
93 myTree = (wxTreeLayoutStored *) NULL;
94 }
95
96 return 0;
97 }
98
99 void MyApp::TreeTest(wxTreeLayoutStored& tree, wxDC& dc)
100 {
101 tree.Initialize(200);
102
103 tree.AddChild(_T("animal"));
104 tree.AddChild(_T("mammal"), _T("animal"));
105 tree.AddChild(_T("insect"), _T("animal"));
106 tree.AddChild(_T("bird"), _T("animal"));
107
108 tree.AddChild(_T("man"), _T("mammal"));
109 tree.AddChild(_T("cat"), _T("mammal"));
110 tree.AddChild(_T("dog"), _T("mammal"));
111 tree.AddChild(_T("giraffe"), _T("mammal"));
112 tree.AddChild(_T("elephant"), _T("mammal"));
113 tree.AddChild(_T("donkey"), _T("mammal"));
114 tree.AddChild(_T("horse"), _T("mammal"));
115
116 tree.AddChild(_T("fido"), _T("dog"));
117 tree.AddChild(_T("domestic cat"), _T("cat"));
118 tree.AddChild(_T("lion"), _T("cat"));
119 tree.AddChild(_T("tiger"), _T("cat"));
120 tree.AddChild(_T("felix"), _T("domestic cat"));
121 tree.AddChild(_T("socks"), _T("domestic cat"));
122
123 tree.AddChild(_T("beetle"), _T("insect"));
124 tree.AddChild(_T("earwig"), _T("insect"));
125 tree.AddChild(_T("eagle"), _T("bird"));
126 tree.AddChild(_T("bluetit"), _T("bird"));
127 tree.AddChild(_T("sparrow"), _T("bird"));
128 tree.AddChild(_T("blackbird"), _T("bird"));
129 tree.AddChild(_T("emu"), _T("bird"));
130 tree.AddChild(_T("crow"), _T("bird"));
131
132 tree.DoLayout(dc);
133 }
134
135 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
136 EVT_MENU(TEST_QUIT, MyFrame::OnQuit)
137 EVT_MENU(TEST_ABOUT, MyFrame::OnAbout)
138 EVT_MENU(TEST_LEFT_RIGHT, MyFrame::OnLeftRight)
139 EVT_MENU(TEST_TOP_BOTTOM, MyFrame::OnTopBottom)
140 EVT_CLOSE(MyFrame::OnCloseWindow)
141 END_EVENT_TABLE()
142
143 // Define my frame constructor
144 MyFrame::MyFrame(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size):
145 wxFrame(parent, -1, title, pos, size)
146 {
147 }
148
149 void MyFrame::OnQuit(wxCommandEvent& event)
150 {
151 Close(TRUE);
152 }
153
154 void MyFrame::OnLeftRight(wxCommandEvent& event)
155 {
156 if (myTree)
157 {
158 myTree->SetOrientation(FALSE);
159 wxClientDC dc(canvas);
160 wxFont font(10, wxROMAN, wxNORMAL, wxBOLD);
161 dc.SetFont(font);
162 wxGetApp().TreeTest(*myTree, dc);
163 canvas->Refresh();
164 }
165 }
166
167 void MyFrame::OnTopBottom(wxCommandEvent& event)
168 {
169 if (myTree)
170 {
171 myTree->SetOrientation(TRUE);
172 wxClientDC dc(canvas);
173 wxFont font(10, wxROMAN, wxNORMAL, wxBOLD);
174 dc.SetFont(font);
175 wxGetApp().TreeTest(*myTree, dc);
176 canvas->Refresh();
177 }
178 }
179
180 void MyFrame::OnAbout(wxCommandEvent& event)
181 {
182 (void)wxMessageBox(_T("wxWindows tree library demo Vsn 2.0\nAuthor: Julian Smart (c) 1998"), _T("About tree test"));
183 }
184
185 void MyFrame::OnCloseWindow(wxCloseEvent& event)
186 {
187 Destroy();
188 }
189
190 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
191 EVT_PAINT(MyCanvas::OnPaint)
192 END_EVENT_TABLE()
193
194 // Define a constructor for my canvas
195 MyCanvas::MyCanvas(wxWindow *parent):
196 wxScrolledWindow(parent, -1)
197 {
198 SetBackgroundColour(*wxWHITE);
199 }
200
201 // Define the repainting behaviour
202 void MyCanvas::OnPaint(wxPaintEvent& event)
203 {
204 wxPaintDC dc(this);
205 PrepareDC(dc);
206 if (myTree)
207 {
208 wxFont font(10, wxROMAN, wxNORMAL, wxBOLD);
209 dc.SetFont(font);
210 myTree->Draw(dc);
211 }
212 }
213