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