Added companion window. Now it's working pretty much as expected except
[wxWidgets.git] / contrib / samples / gizmos / splittree / tree.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tree.cpp
3 // Purpose: Minimal wxWindows sample
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
32 // need because it includes almost all "standard" wxWindows headers)
33 #ifndef WX_PRECOMP
34 #include "wx/wx.h"
35 #endif
36
37 #include "wx/imaglist.h"
38 #include "tree.h"
39
40 // ----------------------------------------------------------------------------
41 // resources
42 // ----------------------------------------------------------------------------
43 // the application icon
44 #if defined(__WXGTK__) || defined(__WXMOTIF__)
45 #include "mondrian.xpm"
46 #endif
47
48 // ----------------------------------------------------------------------------
49 // event tables and other macros for wxWindows
50 // ----------------------------------------------------------------------------
51
52 // the event tables connect the wxWindows events with the functions (event
53 // handlers) which process them. It can be also done at run-time, but for the
54 // simple menu events like this the static method is much simpler.
55 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
56 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
57 EVT_MENU(Minimal_About, MyFrame::OnAbout)
58 END_EVENT_TABLE()
59
60 // Create a new application object: this macro will allow wxWindows to create
61 // the application object during program execution (it's better than using a
62 // static object for many reasons) and also declares the accessor function
63 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
64 // not wxApp)
65 IMPLEMENT_APP(MyApp)
66
67 // ============================================================================
68 // implementation
69 // ============================================================================
70
71 // ----------------------------------------------------------------------------
72 // the application class
73 // ----------------------------------------------------------------------------
74
75 // 'Main program' equivalent: the program execution "starts" here
76 bool MyApp::OnInit()
77 {
78 // create the main application window
79 MyFrame *frame = new MyFrame("Tree Testing",
80 wxPoint(50, 50), wxSize(450, 340));
81
82 // and show it (the frames, unlike simple controls, are not shown when
83 // created initially)
84 frame->Show(TRUE);
85
86 // success: wxApp::OnRun() will be called which will enter the main message
87 // loop and the application will run. If we returned FALSE here, the
88 // application would exit immediately.
89 return TRUE;
90 }
91
92 // ----------------------------------------------------------------------------
93 // main frame
94 // ----------------------------------------------------------------------------
95
96 // frame constructor
97 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
98 : wxFrame((wxFrame *)NULL, idMAIN_FRAME, title, pos, size)
99 {
100 m_splitter = NULL;
101 m_scrolledWindow = NULL;
102 m_tree = NULL;
103 m_valueWindow = NULL;
104 #ifdef __WXMAC__
105 // we need this in order to allow the about menu relocation, since ABOUT is
106 // not the default id of the about menu
107 wxApp::s_macAboutMenuItemId = Minimal_About;
108 #endif
109
110 m_scrolledWindow = new wxSplitterScrolledWindow(this, idSCROLLED_WINDOW, wxDefaultPosition,
111 wxDefaultSize, wxNO_BORDER | wxCLIP_CHILDREN | wxVSCROLL);
112 m_splitter = new wxThinSplitterWindow(m_scrolledWindow, idSPLITTER_WINDOW, wxDefaultPosition,
113 wxDefaultSize, wxSP_3DBORDER | wxCLIP_CHILDREN /* | wxSP_LIVE_UPDATE */);
114 m_splitter->SetSashSize(2);
115 m_tree = new TestTree(m_splitter, idTREE_CTRL, wxDefaultPosition,
116 wxDefaultSize, wxTR_HAS_BUTTONS | wxTR_NO_LINES | wxNO_BORDER );
117 m_valueWindow = new TestValueWindow(m_splitter, idVALUE_WINDOW, wxDefaultPosition,
118 wxDefaultSize, wxNO_BORDER);
119 m_splitter->SplitVertically(m_tree, m_valueWindow);
120 //m_splitter->AdjustScrollbars();
121 m_splitter->SetSashPosition(200);
122 m_scrolledWindow->SetTargetWindow(m_tree);
123
124 m_scrolledWindow->EnableScrolling(FALSE, FALSE);
125
126 // Let the two controls know about each other
127 m_valueWindow->SetTreeCtrl(m_tree);
128 m_tree->SetCompanionWindow(m_valueWindow);
129
130 // set the frame icon
131 SetIcon(wxICON(mondrian));
132
133 // create a menu bar
134 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
135
136 // the "About" item should be in the help menu
137 wxMenu *helpMenu = new wxMenu;
138 helpMenu->Append(Minimal_About, "&About...\tCtrl-A", "Show about dialog");
139
140 menuFile->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
141
142 // now append the freshly created menu to the menu bar...
143 wxMenuBar *menuBar = new wxMenuBar();
144 menuBar->Append(menuFile, "&File");
145 menuBar->Append(helpMenu, "&Help");
146
147 // ... and attach this menu bar to the frame
148 SetMenuBar(menuBar);
149 }
150
151
152 // event handlers
153
154 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
155 {
156 // TRUE is to force the frame to close
157 Close(TRUE);
158 }
159
160 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
161 {
162 wxString msg;
163 msg.Printf( _T("This is the about dialog of tree sample.\n")
164 _T("Welcome to %s"), wxVERSION_STRING);
165
166 wxMessageBox(msg, "About Tree Test", wxOK | wxICON_INFORMATION, this);
167 }
168
169 /*
170 * TesTree
171 */
172
173 IMPLEMENT_CLASS(TestTree, wxRemotelyScrolledTreeCtrl)
174
175 BEGIN_EVENT_TABLE(TestTree, wxRemotelyScrolledTreeCtrl)
176 EVT_PAINT(TestTree::OnPaint)
177 END_EVENT_TABLE()
178
179 TestTree::TestTree(wxWindow* parent, wxWindowID id, const wxPoint& pt,
180 const wxSize& sz, long style):
181 wxRemotelyScrolledTreeCtrl(parent, id, pt, sz, style)
182 {
183 m_imageList = new wxImageList(16, 16, TRUE);
184 #if !defined(__WXMSW__) // || wxUSE_XPM_IN_MSW
185 m_imageList->Add(wxIcon(icon1_xpm));
186 m_imageList->Add(wxIcon(icon2_xpm));
187 m_imageList->Add(wxIcon(icon3_xpm));
188 m_imageList->Add(wxIcon(icon4_xpm));
189 m_imageList->Add(wxIcon(icon5_xpm));
190 m_imageList->Add(wxIcon(icon6_xpm));
191 m_imageList->Add(wxIcon(icon7_xpm));
192 m_imageList->Add(wxIcon(icon8_xpm));
193 #elif defined(__WXMSW__)
194 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_CLOSED_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE));
195 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_OPEN_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE));
196 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_FILE"), wxBITMAP_TYPE_ICO_RESOURCE));
197 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_COMPUTER"), wxBITMAP_TYPE_ICO_RESOURCE));
198 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_DRIVE"), wxBITMAP_TYPE_ICO_RESOURCE));
199 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_CDROM"), wxBITMAP_TYPE_ICO_RESOURCE));
200 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_FLOPPY"), wxBITMAP_TYPE_ICO_RESOURCE));
201 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_REMOVEABLE"), wxBITMAP_TYPE_ICO_RESOURCE));
202 #else
203 #error "Sorry, we don't have icons available for this platforms."
204 #endif
205 SetImageList(m_imageList);
206
207
208 // Add some dummy items
209 wxTreeItemId rootId = AddRoot(_("Root"), 3, -1);
210 int i;
211 for (i = 1; i <= 20; i++)
212 {
213 wxString label;
214 label.Printf(wxT("Item %d"), i);
215 wxTreeItemId id = AppendItem(rootId, label, 0);
216 SetItemImage( id, 1, wxTreeItemIcon_Expanded );
217
218 int j;
219 for (j = 0; j < 10; j++)
220 AppendItem(id, _("Child"), 2);
221 }
222 Expand(rootId);
223 }
224
225 TestTree::~TestTree()
226 {
227 SetImageList(NULL);
228 delete m_imageList;
229 }
230
231 void TestTree::OnPaint(wxPaintEvent& event)
232 {
233 wxPaintDC dc(this);
234
235 wxTreeCtrl::OnPaint(event);
236
237 // Reset the device origin since it may have been set
238 dc.SetDeviceOrigin(0, 0);
239
240 wxSize sz = GetClientSize();
241
242 wxPen pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID);
243 dc.SetPen(pen);
244 dc.SetBrush(* wxTRANSPARENT_BRUSH);
245
246 wxSize clientSize = GetClientSize();
247 wxRect itemRect;
248 int cy=0;
249 wxTreeItemId h, lastH;
250 for(h=GetFirstVisibleItem();h;h=GetNextVisible(h))
251 {
252 if (GetBoundingRect(h, itemRect))
253 {
254 cy = itemRect.GetTop();
255 dc.DrawLine(0, cy, clientSize.x, cy);
256 lastH = h;
257 }
258 }
259 if (GetBoundingRect(lastH, itemRect))
260 {
261 cy = itemRect.GetBottom();
262 dc.DrawLine(0, cy, clientSize.x, cy);
263 }
264 }
265
266 /*
267 * TestValueWindow
268 */
269
270 //IMPLEMENT_CLASS(TestValueWindow, wxWindow)
271
272 BEGIN_EVENT_TABLE(TestValueWindow, wxTreeCompanionWindow)
273 END_EVENT_TABLE()
274
275 TestValueWindow::TestValueWindow(wxWindow* parent, wxWindowID id,
276 const wxPoint& pos,
277 const wxSize& sz,
278 long style):
279 wxTreeCompanionWindow(parent, id, pos, sz, style)
280 {
281 SetBackgroundColour(* wxWHITE);
282 }