]> git.saurik.com Git - wxWidgets.git/blame - contrib/samples/gizmos/splittree/tree.cpp
fixed typo, used word 'assigned' instead of 'allotted'
[wxWidgets.git] / contrib / samples / gizmos / splittree / tree.cpp
CommitLineData
58580a7e
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: tree.cpp
be5a51fb 3// Purpose: Minimal wxWidgets sample
58580a7e
JS
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19#ifdef __GNUG__
20 #pragma implementation "tree.cpp"
21 #pragma interface "tree.cpp"
22#endif
23
24// For compilers that support precompilation, includes "wx/wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31// for all others, include the necessary headers (this file is usually all you
be5a51fb 32// need because it includes almost all "standard" wxWidgets headers)
58580a7e
JS
33#ifndef WX_PRECOMP
34 #include "wx/wx.h"
35#endif
36
352d44d2
JS
37#if !defined(__WXMSW__) || wxUSE_XPM_IN_MSW
38/* Closed folder */
39static char * icon1_xpm[] = {
40/* width height ncolors chars_per_pixel */
41"16 16 6 1",
42/* colors */
43" s None c None",
44". c #000000",
45"+ c #c0c0c0",
46"@ c #808080",
47"# c #ffff00",
48"$ c #ffffff",
49/* pixels */
50" ",
51" @@@@@ ",
52" @#+#+#@ ",
53" @#+#+#+#@@@@@@ ",
54" @$$$$$$$$$$$$@.",
55" @$#+#+#+#+#+#@.",
56" @$+#+#+#+#+#+@.",
57" @$#+#+#+#+#+#@.",
58" @$+#+#+#+#+#+@.",
59" @$#+#+#+#+#+#@.",
60" @$+#+#+#+#+#+@.",
61" @$#+#+#+#+#+#@.",
62" @@@@@@@@@@@@@@.",
63" ..............",
64" ",
65" "};
66
67/* File */
68static char * icon2_xpm[] = {
69/* width height ncolors chars_per_pixel */
70"16 16 3 1",
71/* colors */
72" s None c None",
73". c #000000",
74"+ c #ffffff",
75/* pixels */
76" ",
77" ........ ",
78" .++++++.. ",
79" .+.+.++.+. ",
80" .++++++.... ",
81" .+.+.+++++. ",
82" .+++++++++. ",
83" .+.+.+.+.+. ",
84" .+++++++++. ",
85" .+.+.+.+.+. ",
86" .+++++++++. ",
87" .+.+.+.+.+. ",
88" .+++++++++. ",
89" ........... ",
90" ",
91" "};
92#endif
93
58580a7e
JS
94#include "wx/imaglist.h"
95#include "tree.h"
96
97// ----------------------------------------------------------------------------
98// resources
99// ----------------------------------------------------------------------------
100// the application icon
618f2efa 101#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
58580a7e
JS
102 #include "mondrian.xpm"
103#endif
104
105// ----------------------------------------------------------------------------
be5a51fb 106// event tables and other macros for wxWidgets
58580a7e
JS
107// ----------------------------------------------------------------------------
108
be5a51fb 109// the event tables connect the wxWidgets events with the functions (event
58580a7e
JS
110// handlers) which process them. It can be also done at run-time, but for the
111// simple menu events like this the static method is much simpler.
112BEGIN_EVENT_TABLE(MyFrame, wxFrame)
113 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
114 EVT_MENU(Minimal_About, MyFrame::OnAbout)
115END_EVENT_TABLE()
116
be5a51fb 117// Create a new application object: this macro will allow wxWidgets to create
58580a7e
JS
118// the application object during program execution (it's better than using a
119// static object for many reasons) and also declares the accessor function
120// wxGetApp() which will return the reference of the right type (i.e. MyApp and
121// not wxApp)
122IMPLEMENT_APP(MyApp)
123
124// ============================================================================
125// implementation
126// ============================================================================
127
128// ----------------------------------------------------------------------------
129// the application class
130// ----------------------------------------------------------------------------
131
132// 'Main program' equivalent: the program execution "starts" here
133bool MyApp::OnInit()
134{
135 // create the main application window
019abbf2 136 MyFrame *frame = new MyFrame(wxT("Tree Testing"),
58580a7e
JS
137 wxPoint(50, 50), wxSize(450, 340));
138
139 // and show it (the frames, unlike simple controls, are not shown when
140 // created initially)
a2d49353 141 frame->Show(true);
58580a7e
JS
142
143 // success: wxApp::OnRun() will be called which will enter the main message
a2d49353 144 // loop and the application will run. If we returned false here, the
58580a7e 145 // application would exit immediately.
a2d49353 146 return true;
58580a7e
JS
147}
148
149// ----------------------------------------------------------------------------
150// main frame
151// ----------------------------------------------------------------------------
152
153// frame constructor
154MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
155 : wxFrame((wxFrame *)NULL, idMAIN_FRAME, title, pos, size)
156{
157 m_splitter = NULL;
a2d49353 158 m_scrolledWindow = NULL;
58580a7e
JS
159 m_tree = NULL;
160 m_valueWindow = NULL;
161#ifdef __WXMAC__
162 // we need this in order to allow the about menu relocation, since ABOUT is
163 // not the default id of the about menu
164 wxApp::s_macAboutMenuItemId = Minimal_About;
165#endif
166
167 m_scrolledWindow = new wxSplitterScrolledWindow(this, idSCROLLED_WINDOW, wxDefaultPosition,
a2d49353 168 wxSize(300, 400), wxNO_BORDER | wxCLIP_CHILDREN | wxVSCROLL);
58580a7e 169 m_splitter = new wxThinSplitterWindow(m_scrolledWindow, idSPLITTER_WINDOW, wxDefaultPosition,
a2d49353
WS
170 wxDefaultSize, wxSP_3DBORDER | wxCLIP_CHILDREN /* | wxSP_LIVE_UPDATE */);
171 m_splitter->SetSashSize(2);
a18b162d 172
a2d49353 173 /* Note the wxTR_ROW_LINES style: draws horizontal lines between items */
a18b162d 174 m_tree = new TestTree(m_splitter , idTREE_CTRL, wxDefaultPosition,
a2d49353 175 wxDefaultSize, wxTR_HAS_BUTTONS | wxTR_NO_LINES | wxNO_BORDER | wxTR_ROW_LINES );
58580a7e 176 m_valueWindow = new TestValueWindow(m_splitter, idVALUE_WINDOW, wxDefaultPosition,
a2d49353 177 wxDefaultSize, wxNO_BORDER);
58580a7e 178 m_splitter->SplitVertically(m_tree, m_valueWindow);
a2d49353
WS
179 //m_splitter->AdjustScrollbars();
180 m_splitter->SetSashPosition(200);
181 m_scrolledWindow->SetTargetWindow(m_tree);
58580a7e 182
a2d49353 183 m_scrolledWindow->EnableScrolling(false, false);
58580a7e 184
a2d49353
WS
185 // Let the two controls know about each other
186 m_valueWindow->SetTreeCtrl(m_tree);
187 m_tree->SetCompanionWindow(m_valueWindow);
1a584f14 188
58580a7e
JS
189 // set the frame icon
190 SetIcon(wxICON(mondrian));
191
192 // create a menu bar
019abbf2 193 wxMenu *menuFile = new wxMenu(wxT(""), wxMENU_TEAROFF);
58580a7e
JS
194
195 // the "About" item should be in the help menu
196 wxMenu *helpMenu = new wxMenu;
019abbf2 197 helpMenu->Append(Minimal_About, wxT("&About...\tCtrl-A"), wxT("Show about dialog"));
58580a7e 198
019abbf2 199 menuFile->Append(Minimal_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
58580a7e
JS
200
201 // now append the freshly created menu to the menu bar...
202 wxMenuBar *menuBar = new wxMenuBar();
019abbf2
VZ
203 menuBar->Append(menuFile, wxT("&File"));
204 menuBar->Append(helpMenu, wxT("&Help"));
58580a7e
JS
205
206 // ... and attach this menu bar to the frame
207 SetMenuBar(menuBar);
208}
209
210
211// event handlers
212
213void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
214{
a2d49353
WS
215 // true is to force the frame to close
216 Close(true);
58580a7e
JS
217}
218
219void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
220{
221 wxString msg;
019abbf2
VZ
222 msg.Printf( wxT("This is the about dialog of splittree sample.\n")
223 wxT("Welcome to %s"), wxVERSION_STRING);
58580a7e 224
019abbf2 225 wxMessageBox(msg, wxT("About Tree Test"), wxOK | wxICON_INFORMATION, this);
58580a7e
JS
226}
227
228/*
229 * TesTree
230 */
231
232IMPLEMENT_CLASS(TestTree, wxRemotelyScrolledTreeCtrl)
233
234BEGIN_EVENT_TABLE(TestTree, wxRemotelyScrolledTreeCtrl)
235END_EVENT_TABLE()
236
237TestTree::TestTree(wxWindow* parent, wxWindowID id, const wxPoint& pt,
238 const wxSize& sz, long style):
239 wxRemotelyScrolledTreeCtrl(parent, id, pt, sz, style)
240{
a2d49353 241 m_imageList = new wxImageList(16, 16, true);
58580a7e
JS
242#if !defined(__WXMSW__) // || wxUSE_XPM_IN_MSW
243 m_imageList->Add(wxIcon(icon1_xpm));
244 m_imageList->Add(wxIcon(icon2_xpm));
58580a7e
JS
245#elif defined(__WXMSW__)
246 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_CLOSED_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE));
58580a7e 247 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_FILE"), wxBITMAP_TYPE_ICO_RESOURCE));
58580a7e
JS
248#else
249#error "Sorry, we don't have icons available for this platforms."
250#endif
251 SetImageList(m_imageList);
252
a2d49353
WS
253
254 // Add some dummy items
255 wxTreeItemId rootId = AddRoot(_("Root"), -1, -1);
256 int i;
257 for (i = 1; i <= 20; i++)
258 {
259 wxString label;
260 label.Printf(wxT("Item %d"), i);
261 wxTreeItemId id = AppendItem(rootId, label, 0);
262 //SetItemImage( id, 1, wxTreeItemIcon_Expanded );
263
264 int j;
265 for (j = 0; j < 10; j++)
266 AppendItem(id, _("Child"), 1);
267 }
268 Expand(rootId);
58580a7e
JS
269}
270
271TestTree::~TestTree()
272{
a2d49353
WS
273 SetImageList(NULL);
274 delete m_imageList;
58580a7e
JS
275}
276
277/*
278 * TestValueWindow
279 */
280
281//IMPLEMENT_CLASS(TestValueWindow, wxWindow)
282
1a584f14 283BEGIN_EVENT_TABLE(TestValueWindow, wxTreeCompanionWindow)
58580a7e
JS
284END_EVENT_TABLE()
285
286TestValueWindow::TestValueWindow(wxWindow* parent, wxWindowID id,
287 const wxPoint& pos,
288 const wxSize& sz,
289 long style):
1a584f14 290 wxTreeCompanionWindow(parent, id, pos, sz, style)
58580a7e 291{
a2d49353 292 SetBackgroundColour(* wxWHITE);
58580a7e 293}