Implemented IsVisible and GetFirst/NextVisibleItem for generic tree control
[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 // set the frame icon
127 SetIcon(wxICON(mondrian));
128
129 // create a menu bar
130 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
131
132 // the "About" item should be in the help menu
133 wxMenu *helpMenu = new wxMenu;
134 helpMenu->Append(Minimal_About, "&About...\tCtrl-A", "Show about dialog");
135
136 menuFile->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
137
138 // now append the freshly created menu to the menu bar...
139 wxMenuBar *menuBar = new wxMenuBar();
140 menuBar->Append(menuFile, "&File");
141 menuBar->Append(helpMenu, "&Help");
142
143 // ... and attach this menu bar to the frame
144 SetMenuBar(menuBar);
145 }
146
147
148 // event handlers
149
150 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
151 {
152 // TRUE is to force the frame to close
153 Close(TRUE);
154 }
155
156 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
157 {
158 wxString msg;
159 msg.Printf( _T("This is the about dialog of tree sample.\n")
160 _T("Welcome to %s"), wxVERSION_STRING);
161
162 wxMessageBox(msg, "About Tree Test", wxOK | wxICON_INFORMATION, this);
163 }
164
165 /*
166 * TesTree
167 */
168
169 IMPLEMENT_CLASS(TestTree, wxRemotelyScrolledTreeCtrl)
170
171 BEGIN_EVENT_TABLE(TestTree, wxRemotelyScrolledTreeCtrl)
172 EVT_PAINT(TestTree::OnPaint)
173 END_EVENT_TABLE()
174
175 TestTree::TestTree(wxWindow* parent, wxWindowID id, const wxPoint& pt,
176 const wxSize& sz, long style):
177 wxRemotelyScrolledTreeCtrl(parent, id, pt, sz, style)
178 {
179 m_imageList = new wxImageList(16, 16, TRUE);
180 #if !defined(__WXMSW__) // || wxUSE_XPM_IN_MSW
181 m_imageList->Add(wxIcon(icon1_xpm));
182 m_imageList->Add(wxIcon(icon2_xpm));
183 m_imageList->Add(wxIcon(icon3_xpm));
184 m_imageList->Add(wxIcon(icon4_xpm));
185 m_imageList->Add(wxIcon(icon5_xpm));
186 m_imageList->Add(wxIcon(icon6_xpm));
187 m_imageList->Add(wxIcon(icon7_xpm));
188 m_imageList->Add(wxIcon(icon8_xpm));
189 #elif defined(__WXMSW__)
190 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_CLOSED_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE));
191 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_OPEN_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE));
192 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_FILE"), wxBITMAP_TYPE_ICO_RESOURCE));
193 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_COMPUTER"), wxBITMAP_TYPE_ICO_RESOURCE));
194 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_DRIVE"), wxBITMAP_TYPE_ICO_RESOURCE));
195 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_CDROM"), wxBITMAP_TYPE_ICO_RESOURCE));
196 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_FLOPPY"), wxBITMAP_TYPE_ICO_RESOURCE));
197 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_REMOVEABLE"), wxBITMAP_TYPE_ICO_RESOURCE));
198 #else
199 #error "Sorry, we don't have icons available for this platforms."
200 #endif
201 SetImageList(m_imageList);
202
203
204 // Add some dummy items
205 wxTreeItemId rootId = AddRoot(_("Root"), 3, -1);
206 int i;
207 for (i = 1; i <= 20; i++)
208 {
209 wxString label;
210 label.Printf(wxT("Item %d"), i);
211 wxTreeItemId id = AppendItem(rootId, label, 0);
212 SetItemImage( id, 1, wxTreeItemIcon_Expanded );
213
214 int j;
215 for (j = 0; j < 10; j++)
216 AppendItem(id, _("Child"), 2);
217 }
218 Expand(rootId);
219 }
220
221 TestTree::~TestTree()
222 {
223 SetImageList(NULL);
224 delete m_imageList;
225 }
226
227 void TestTree::OnPaint(wxPaintEvent& event)
228 {
229 wxPaintDC dc(this);
230
231 wxTreeCtrl::OnPaint(event);
232
233 // Reset the device origin since it may have been set
234 dc.SetDeviceOrigin(0, 0);
235
236 wxSize sz = GetClientSize();
237
238 wxPen pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID);
239 dc.SetPen(pen);
240 dc.SetBrush(* wxTRANSPARENT_BRUSH);
241
242 wxRect itemRect;
243 if (GetBoundingRect(GetRootItem(), itemRect))
244 {
245 int itemHeight = itemRect.GetHeight();
246 wxRect rcClient = GetRect();
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(rcClient.x, cy, rcClient.x + rcClient.width, cy);
256 lastH = h;
257 //cy += itemHeight;
258 }
259 }
260 if (GetBoundingRect(lastH, itemRect))
261 {
262 cy = itemRect.GetBottom();
263 dc.DrawLine(rcClient.x, cy, rcClient.x + rcClient.width, cy);
264 }
265 }
266 }
267
268 /*
269 * TestValueWindow
270 */
271
272 //IMPLEMENT_CLASS(TestValueWindow, wxWindow)
273
274 BEGIN_EVENT_TABLE(TestValueWindow, wxWindow)
275 EVT_SIZE(TestValueWindow::OnSize)
276 END_EVENT_TABLE()
277
278 TestValueWindow::TestValueWindow(wxWindow* parent, wxWindowID id,
279 const wxPoint& pos,
280 const wxSize& sz,
281 long style):
282 wxWindow(parent, id, pos, sz, style)
283 {
284 SetBackgroundColour(* wxWHITE);
285 }
286
287 void TestValueWindow::OnSize(wxSizeEvent& event)
288 {
289 }