]> git.saurik.com Git - wxWidgets.git/blame - samples/treelay/treelay.cpp
A fix for attribrute sorting, but it's still broken if there are
[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
94799627 27wxTreeLayoutStored *myTree = 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
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
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
82 frame->SetStatusText("Hello, tree!");
83
84 // Return the main frame window
85 return TRUE;
86}
87
94799627 88void MyApp::TreeTest(wxTreeLayoutStored& tree, wxDC& dc)
4414cc1d
JS
89{
90 tree.Initialize(200);
91
92 tree.AddChild("animal");
93 tree.AddChild("mammal", "animal");
94 tree.AddChild("insect", "animal");
95 tree.AddChild("bird", "animal");
96
97 tree.AddChild("man", "mammal");
98 tree.AddChild("cat", "mammal");
99 tree.AddChild("dog", "mammal");
100 tree.AddChild("giraffe", "mammal");
101 tree.AddChild("elephant", "mammal");
102 tree.AddChild("donkey", "mammal");
103 tree.AddChild("horse", "mammal");
104
105 tree.AddChild("fido", "dog");
106 tree.AddChild("domestic cat", "cat");
107 tree.AddChild("lion", "cat");
108 tree.AddChild("tiger", "cat");
109 tree.AddChild("felix", "domestic cat");
110 tree.AddChild("socks", "domestic cat");
111
112 tree.AddChild("beetle", "insect");
113 tree.AddChild("earwig", "insect");
114 tree.AddChild("eagle", "bird");
115 tree.AddChild("bluetit", "bird");
116 tree.AddChild("sparrow", "bird");
117 tree.AddChild("blackbird", "bird");
118 tree.AddChild("emu", "bird");
119 tree.AddChild("crow", "bird");
120
121 tree.DoLayout(dc);
122}
123
124BEGIN_EVENT_TABLE(MyFrame, wxFrame)
125 EVT_MENU(TEST_QUIT, MyFrame::OnQuit)
126 EVT_MENU(TEST_ABOUT, MyFrame::OnAbout)
127 EVT_MENU(TEST_LEFT_RIGHT, MyFrame::OnLeftRight)
128 EVT_MENU(TEST_TOP_BOTTOM, MyFrame::OnTopBottom)
129 EVT_CLOSE(MyFrame::OnCloseWindow)
130END_EVENT_TABLE()
131
132// Define my frame constructor
133MyFrame::MyFrame(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size):
134 wxFrame(parent, -1, title, pos, size)
135{
136}
137
138void MyFrame::OnQuit(wxCommandEvent& event)
139{
140 Close(TRUE);
141}
142
143void MyFrame::OnLeftRight(wxCommandEvent& event)
144{
145 if (myTree)
146 {
147 myTree->SetOrientation(FALSE);
148 wxClientDC dc(canvas);
149 wxFont font(10, wxROMAN, wxNORMAL, wxBOLD);
150 dc.SetFont(font);
151 wxGetApp().TreeTest(*myTree, dc);
152 canvas->Refresh();
153 }
154}
155
156void MyFrame::OnTopBottom(wxCommandEvent& event)
157{
158 if (myTree)
159 {
160 myTree->SetOrientation(TRUE);
161 wxClientDC dc(canvas);
162 wxFont font(10, wxROMAN, wxNORMAL, wxBOLD);
163 dc.SetFont(font);
164 wxGetApp().TreeTest(*myTree, dc);
165 canvas->Refresh();
166 }
167}
168
169void MyFrame::OnAbout(wxCommandEvent& event)
170{
171 (void)wxMessageBox("wxWindows tree library demo Vsn 2.0\nAuthor: Julian Smart (c) 1998", "About tree test");
172}
173
174void MyFrame::OnCloseWindow(wxCloseEvent& event)
175{
176 Destroy();
177}
178
179BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
180 EVT_PAINT(MyCanvas::OnPaint)
181END_EVENT_TABLE()
182
183// Define a constructor for my canvas
184MyCanvas::MyCanvas(wxWindow *parent):
185 wxScrolledWindow(parent, -1)
186{
3f1af920 187 SetBackgroundColour(*wxWHITE);
4414cc1d
JS
188}
189
190// Define the repainting behaviour
191void MyCanvas::OnPaint(wxPaintEvent& event)
192{
193 wxPaintDC dc(this);
194 PrepareDC(dc);
195 if (myTree)
196 {
197 wxFont font(10, wxROMAN, wxNORMAL, wxBOLD);
198 dc.SetFont(font);
199 myTree->Draw(dc);
200 }
201}
202