]> git.saurik.com Git - wxWidgets.git/blob - samples/treelay/treelay.cpp
don't generate an endless stream of asserts for the 0-sized images
[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, "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("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, "&Left to right", "Redraw left to right");
52 file_menu->Append(TEST_TOP_BOTTOM, "&Top to bottom", "Redraw top to bottom");
53 file_menu->AppendSeparator();
54 file_menu->Append(TEST_QUIT, "E&xit", "Quit program");
55
56 wxMenu *help_menu = new wxMenu;
57 help_menu->Append(TEST_ABOUT, "&About", "About Tree Test");
58
59 wxMenuBar* menu_bar = new wxMenuBar;
60
61 menu_bar->Append(file_menu, "&File");
62 menu_bar->Append(help_menu, "&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("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("animal");
104 tree.AddChild("mammal", "animal");
105 tree.AddChild("insect", "animal");
106 tree.AddChild("bird", "animal");
107
108 tree.AddChild("man", "mammal");
109 tree.AddChild("cat", "mammal");
110 tree.AddChild("dog", "mammal");
111 tree.AddChild("giraffe", "mammal");
112 tree.AddChild("elephant", "mammal");
113 tree.AddChild("donkey", "mammal");
114 tree.AddChild("horse", "mammal");
115
116 tree.AddChild("fido", "dog");
117 tree.AddChild("domestic cat", "cat");
118 tree.AddChild("lion", "cat");
119 tree.AddChild("tiger", "cat");
120 tree.AddChild("felix", "domestic cat");
121 tree.AddChild("socks", "domestic cat");
122
123 tree.AddChild("beetle", "insect");
124 tree.AddChild("earwig", "insect");
125 tree.AddChild("eagle", "bird");
126 tree.AddChild("bluetit", "bird");
127 tree.AddChild("sparrow", "bird");
128 tree.AddChild("blackbird", "bird");
129 tree.AddChild("emu", "bird");
130 tree.AddChild("crow", "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("wxWindows tree library demo Vsn 2.0\nAuthor: Julian Smart (c) 1998", "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