]> git.saurik.com Git - wxWidgets.git/blame - samples/treelay/treelay.cpp
update libpng-dev deps.
[wxWidgets.git] / samples / treelay / treelay.cpp
CommitLineData
4414cc1d 1///////////////////////////////////////////////////////////////////////////////
07a9af32 2// Name: treelay.cpp
4414cc1d
JS
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".
07a9af32 13#include "wx/wxprec.h"
4414cc1d
JS
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
07a9af32 20#include "wx/wx.h"
4414cc1d
JS
21#endif
22
07a9af32 23#include "wx/treelay.h"
babc9758 24
07a9af32 25#include "treelay.h"
4414cc1d 26
2f6c54eb 27wxTreeLayoutStored *myTree = (wxTreeLayoutStored *) NULL;
4414cc1d
JS
28
29// A macro needed for some compilers (AIX) that need 'main' to be defined
30// in the application itself.
31IMPLEMENT_APP(MyApp)
32
33// The `main program' equivalent, creating the windows and returning the
34// main frame
35bool MyApp::OnInit()
36{
37 // Create the main frame window
39960310 38 MyFrame* frame = new MyFrame(NULL, _T("Tree Test"), wxPoint(-1, -1), wxSize(400, 550));
4414cc1d
JS
39
40 // Give it a status line
41 frame->CreateStatusBar(2);
42
43 // Give it an icon
44#ifdef __WINDOWS__
39960310 45 wxIcon icon(_T("tree_icn"));
4414cc1d
JS
46 frame->SetIcon(icon);
47#endif
48
49 // Make a menubar
50 wxMenu *file_menu = new wxMenu;
39960310
MB
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"));
4414cc1d 53 file_menu->AppendSeparator();
39960310 54 file_menu->Append(TEST_QUIT, _T("E&xit"), _T("Quit program"));
4414cc1d
JS
55
56 wxMenu *help_menu = new wxMenu;
39960310 57 help_menu->Append(TEST_ABOUT, _T("&About"), _T("About Tree Test"));
4414cc1d
JS
58
59 wxMenuBar* menu_bar = new wxMenuBar;
60
39960310
MB
61 menu_bar->Append(file_menu, _T("&File"));
62 menu_bar->Append(help_menu, _T("&Help"));
4414cc1d
JS
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
94799627 73 myTree = new wxTreeLayoutStored();
4414cc1d
JS
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
39960310 82 frame->SetStatusText(_T("Hello, tree!"));
4414cc1d
JS
83
84 // Return the main frame window
85 return TRUE;
86}
87
2f6c54eb
VZ
88int MyApp::OnExit()
89{
90 if (myTree)
91 {
92 delete myTree;
93 myTree = (wxTreeLayoutStored *) NULL;
94 }
95
96 return 0;
97}
98
94799627 99void MyApp::TreeTest(wxTreeLayoutStored& tree, wxDC& dc)
4414cc1d
JS
100{
101 tree.Initialize(200);
102
39960310
MB
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"));
4414cc1d
JS
131
132 tree.DoLayout(dc);
133}
134
135BEGIN_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)
141END_EVENT_TABLE()
142
143// Define my frame constructor
144MyFrame::MyFrame(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size):
145 wxFrame(parent, -1, title, pos, size)
146{
147}
148
149void MyFrame::OnQuit(wxCommandEvent& event)
150{
151 Close(TRUE);
152}
153
154void 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
167void 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
180void MyFrame::OnAbout(wxCommandEvent& event)
181{
39960310 182 (void)wxMessageBox(_T("wxWindows tree library demo Vsn 2.0\nAuthor: Julian Smart (c) 1998"), _T("About tree test"));
4414cc1d
JS
183}
184
185void MyFrame::OnCloseWindow(wxCloseEvent& event)
186{
187 Destroy();
188}
189
190BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
191 EVT_PAINT(MyCanvas::OnPaint)
192END_EVENT_TABLE()
193
194// Define a constructor for my canvas
195MyCanvas::MyCanvas(wxWindow *parent):
196 wxScrolledWindow(parent, -1)
197{
3f1af920 198 SetBackgroundColour(*wxWHITE);
4414cc1d
JS
199}
200
201// Define the repainting behaviour
202void 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