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