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