]>
Commit | Line | Data |
---|---|---|
eca15c0d VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/generic/treebkg.cpp | |
3 | // Purpose: generic implementation of wxTreebook | |
4 | // Author: Evgeniy Tarassov, Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 2005-09-15 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2005 Vadim Zeitlin <vadim@wxwidgets.org> | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #if wxUSE_TREEBOOK | |
28 | ||
29 | #include "wx/treebook.h" | |
30 | #include "wx/imaglist.h" | |
31 | #include "wx/settings.h" | |
32 | ||
33 | // ---------------------------------------------------------------------------- | |
34 | // various wxWidgets macros | |
35 | // ---------------------------------------------------------------------------- | |
36 | ||
37 | // check that the page index is valid | |
38 | #define IS_VALID_PAGE(nPage) ((nPage) < DoInternalGetPageCount()) | |
39 | ||
40 | // ---------------------------------------------------------------------------- | |
41 | // event table | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
2ddb4d13 | 44 | IMPLEMENT_DYNAMIC_CLASS(wxTreebook, wxBookCtrlBase) |
eca15c0d VZ |
45 | IMPLEMENT_DYNAMIC_CLASS(wxTreebookEvent, wxNotifyEvent) |
46 | ||
9cf0f6ae | 47 | #if !WXWIN_COMPATIBILITY_EVENT_TYPES |
eca15c0d VZ |
48 | const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING = wxNewEventType(); |
49 | const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED = wxNewEventType(); | |
50 | const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED = wxNewEventType(); | |
51 | const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED = wxNewEventType(); | |
9cf0f6ae | 52 | #endif |
eca15c0d VZ |
53 | const int wxID_TREEBOOKTREEVIEW = wxNewId(); |
54 | ||
55 | BEGIN_EVENT_TABLE(wxTreebook, wxBookCtrlBase) | |
eca15c0d VZ |
56 | EVT_TREE_SEL_CHANGED (wxID_TREEBOOKTREEVIEW, wxTreebook::OnTreeSelectionChange) |
57 | EVT_TREE_ITEM_EXPANDED (wxID_TREEBOOKTREEVIEW, wxTreebook::OnTreeNodeExpandedCollapsed) | |
58 | EVT_TREE_ITEM_COLLAPSED(wxID_TREEBOOKTREEVIEW, wxTreebook::OnTreeNodeExpandedCollapsed) | |
59 | END_EVENT_TABLE() | |
60 | ||
61 | // ============================================================================ | |
62 | // wxTreebook implementation | |
63 | // ============================================================================ | |
64 | ||
65 | // ---------------------------------------------------------------------------- | |
66 | // wxTreebook creation | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | void wxTreebook::Init() | |
70 | { | |
eca15c0d VZ |
71 | m_selection = |
72 | m_actualSelection = wxNOT_FOUND; | |
73 | } | |
74 | ||
75 | bool | |
76 | wxTreebook::Create(wxWindow *parent, | |
77 | wxWindowID id, | |
78 | const wxPoint& pos, | |
79 | const wxSize& size, | |
80 | long style, | |
81 | const wxString& name) | |
82 | { | |
83 | // Check the style flag to have either wxTBK_RIGHT or wxTBK_LEFT | |
2ddb4d13 | 84 | if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT ) |
eca15c0d | 85 | { |
2ddb4d13 | 86 | style |= wxBK_LEFT; |
eca15c0d VZ |
87 | } |
88 | ||
89 | // no border for this control, it doesn't look nice together with the tree | |
90 | style &= ~wxBORDER_MASK; | |
91 | style |= wxBORDER_NONE; | |
92 | ||
93 | if ( !wxControl::Create(parent, id, pos, size, | |
94 | style, wxDefaultValidator, name) ) | |
95 | return false; | |
96 | ||
2ddb4d13 | 97 | m_bookctrl = new wxTreeCtrl |
eca15c0d VZ |
98 | ( |
99 | this, | |
100 | wxID_TREEBOOKTREEVIEW, | |
101 | wxDefaultPosition, | |
102 | wxDefaultSize, | |
103 | wxBORDER_SIMPLE | | |
bc454ef8 | 104 | wxTR_DEFAULT_STYLE | |
eca15c0d | 105 | wxTR_HIDE_ROOT | |
eca15c0d VZ |
106 | wxTR_SINGLE |
107 | ); | |
2ddb4d13 | 108 | GetTreeCtrl()->AddRoot(wxEmptyString); // label doesn't matter, it's hidden |
eca15c0d VZ |
109 | |
110 | #ifdef __WXMSW__ | |
2ddb4d13 | 111 | // We need to add dummy size event to force possible scrollbar hiding |
eca15c0d VZ |
112 | wxSizeEvent evt; |
113 | GetEventHandler()->AddPendingEvent(evt); | |
114 | #endif | |
115 | ||
116 | return true; | |
117 | } | |
118 | ||
119 | ||
120 | // insert a new page just before the pagePos | |
121 | bool wxTreebook::InsertPage(size_t pagePos, | |
122 | wxWindow *page, | |
123 | const wxString& text, | |
124 | bool bSelect, | |
125 | int imageId) | |
126 | { | |
127 | return DoInsertPage(pagePos, page, text, bSelect, imageId); | |
128 | } | |
129 | ||
9d5371c6 VZ |
130 | bool wxTreebook::InsertSubPage(size_t pagePos, |
131 | wxWindow *page, | |
132 | const wxString& text, | |
133 | bool bSelect, | |
134 | int imageId) | |
eca15c0d VZ |
135 | { |
136 | return DoInsertSubPage(pagePos, page, text, bSelect, imageId); | |
137 | } | |
138 | ||
139 | bool wxTreebook::AddPage(wxWindow *page, const wxString& text, bool bSelect, | |
140 | int imageId) | |
141 | { | |
142 | return DoInsertPage(m_treeIds.GetCount(), page, text, bSelect, imageId); | |
143 | } | |
144 | ||
145 | // insertion time is linear to the number of top-pages | |
146 | bool wxTreebook::AddSubPage(wxWindow *page, const wxString& text, bool bSelect, int imageId) | |
147 | { | |
148 | return DoAddSubPage(page, text, bSelect, imageId); | |
149 | } | |
150 | ||
151 | ||
152 | bool wxTreebook::DoInsertPage(size_t pagePos, | |
153 | wxWindow *page, | |
154 | const wxString& text, | |
155 | bool bSelect, | |
156 | int imageId) | |
157 | { | |
158 | wxCHECK_MSG( pagePos <= DoInternalGetPageCount(), false, | |
159 | wxT("Invalid treebook page position") ); | |
160 | ||
161 | if ( !wxBookCtrlBase::InsertPage(pagePos, page, text, bSelect, imageId) ) | |
162 | return false; | |
163 | ||
2ddb4d13 | 164 | wxTreeCtrl *tree = GetTreeCtrl(); |
eca15c0d VZ |
165 | wxTreeItemId newId; |
166 | if ( pagePos == DoInternalGetPageCount() ) | |
167 | { | |
168 | // append the page to the end | |
2ddb4d13 | 169 | wxTreeItemId rootId = tree->GetRootItem(); |
eca15c0d | 170 | |
2ddb4d13 | 171 | newId = tree->AppendItem(rootId, text, imageId); |
eca15c0d VZ |
172 | } |
173 | else // insert the new page before the given one | |
174 | { | |
175 | wxTreeItemId nodeId = m_treeIds[pagePos]; | |
176 | ||
2ddb4d13 WS |
177 | wxTreeItemId previousId = tree->GetPrevSibling(nodeId); |
178 | wxTreeItemId parentId = tree->GetItemParent(nodeId); | |
eca15c0d VZ |
179 | |
180 | if ( previousId.IsOk() ) | |
181 | { | |
182 | // insert before the sibling - previousId | |
2ddb4d13 | 183 | newId = tree->InsertItem(parentId, previousId, text, imageId); |
eca15c0d VZ |
184 | } |
185 | else // no prev siblings -- insert as a first child | |
186 | { | |
187 | wxASSERT_MSG( parentId.IsOk(), wxT( "Tree has no root node?" ) ); | |
188 | ||
2ddb4d13 | 189 | newId = tree->PrependItem(parentId, text, imageId); |
eca15c0d VZ |
190 | } |
191 | } | |
192 | ||
193 | if ( !newId.IsOk() ) | |
194 | { | |
195 | //something wrong -> cleaning and returning with false | |
196 | (void)wxBookCtrlBase::DoRemovePage(pagePos); | |
197 | ||
198 | wxFAIL_MSG( wxT("Failed to insert treebook page") ); | |
199 | return false; | |
200 | } | |
201 | ||
202 | DoInternalAddPage(pagePos, page, newId); | |
203 | ||
204 | DoUpdateSelection(bSelect, pagePos); | |
205 | ||
2ddb4d13 | 206 | m_bookctrl->InvalidateBestSize(); |
bc454ef8 | 207 | |
eca15c0d VZ |
208 | return true; |
209 | } | |
210 | ||
211 | bool wxTreebook::DoAddSubPage(wxWindow *page, const wxString& text, bool bSelect, int imageId) | |
212 | { | |
2ddb4d13 | 213 | wxTreeCtrl *tree = GetTreeCtrl(); |
eca15c0d | 214 | |
2ddb4d13 WS |
215 | wxTreeItemId rootId = tree->GetRootItem(); |
216 | ||
217 | wxTreeItemId lastNodeId = tree->GetLastChild(rootId); | |
eca15c0d VZ |
218 | |
219 | wxCHECK_MSG( lastNodeId.IsOk(), false, | |
220 | _T("Can't insert sub page when there are no pages") ); | |
221 | ||
222 | // now calculate its position (should we save/update it too?) | |
2ddb4d13 WS |
223 | size_t newPos = tree->GetCount() - |
224 | (tree->GetChildrenCount(lastNodeId, true) + 1); | |
eca15c0d VZ |
225 | |
226 | return DoInsertSubPage(newPos, page, text, bSelect, imageId); | |
227 | } | |
228 | ||
229 | bool wxTreebook::DoInsertSubPage(size_t pagePos, | |
230 | wxTreebookPage *page, | |
231 | const wxString& text, | |
232 | bool bSelect, | |
233 | int imageId) | |
234 | { | |
235 | wxTreeItemId parentId = DoInternalGetPage(pagePos); | |
236 | wxCHECK_MSG( parentId.IsOk(), false, wxT("invalid tree item") ); | |
237 | ||
2ddb4d13 WS |
238 | wxTreeCtrl *tree = GetTreeCtrl(); |
239 | ||
240 | size_t newPos = pagePos + tree->GetChildrenCount(parentId, true) + 1; | |
eca15c0d VZ |
241 | wxASSERT_MSG( newPos <= DoInternalGetPageCount(), |
242 | wxT("Internal error in tree insert point calculation") ); | |
243 | ||
244 | if ( !wxBookCtrlBase::InsertPage(newPos, page, text, bSelect, imageId) ) | |
245 | return false; | |
246 | ||
2ddb4d13 | 247 | wxTreeItemId newId = tree->AppendItem(parentId, text, imageId); |
eca15c0d VZ |
248 | |
249 | if ( !newId.IsOk() ) | |
250 | { | |
251 | (void)wxBookCtrlBase::DoRemovePage(newPos); | |
252 | ||
253 | wxFAIL_MSG( wxT("Failed to insert treebook page") ); | |
254 | return false; | |
255 | } | |
256 | ||
257 | DoInternalAddPage(newPos, page, newId); | |
258 | ||
259 | DoUpdateSelection(bSelect, newPos); | |
260 | ||
2ddb4d13 | 261 | m_bookctrl->InvalidateBestSize(); |
bc454ef8 | 262 | |
eca15c0d VZ |
263 | return true; |
264 | } | |
265 | ||
266 | bool wxTreebook::DeletePage(size_t pagePos) | |
267 | { | |
268 | wxCHECK_MSG( IS_VALID_PAGE(pagePos), false, wxT("Invalid tree index") ); | |
269 | ||
270 | wxTreebookPage *oldPage = DoRemovePage(pagePos); | |
271 | if ( !oldPage ) | |
272 | return false; | |
273 | ||
274 | delete oldPage; | |
275 | ||
276 | return true; | |
277 | } | |
278 | ||
279 | wxTreebookPage *wxTreebook::DoRemovePage(size_t pagePos) | |
280 | { | |
281 | wxTreeItemId pageId = DoInternalGetPage(pagePos); | |
282 | wxCHECK_MSG( pageId.IsOk(), NULL, wxT("Invalid tree index") ); | |
283 | ||
284 | wxTreebookPage * oldPage = GetPage(pagePos); | |
2ddb4d13 | 285 | wxTreeCtrl *tree = GetTreeCtrl(); |
eca15c0d | 286 | |
2ddb4d13 | 287 | size_t subCount = tree->GetChildrenCount(pageId, true); |
eca15c0d VZ |
288 | wxASSERT_MSG ( IS_VALID_PAGE(pagePos + subCount), |
289 | wxT("Internal error in wxTreebook::DoRemovePage") ); | |
290 | ||
291 | // here we are going to delete ALL the pages in the range | |
292 | // [pagePos, pagePos + subCount] -- the page and its children | |
293 | ||
294 | // deleting all the pages from the base class | |
295 | for ( size_t i = 0; i <= subCount; ++i ) | |
296 | { | |
297 | wxTreebookPage *page = wxBookCtrlBase::DoRemovePage(pagePos); | |
298 | ||
299 | // don't delete the page itself though -- it will be deleted in | |
300 | // DeletePage() when we return | |
301 | if ( i ) | |
302 | { | |
303 | delete page; | |
304 | } | |
305 | } | |
306 | ||
307 | DoInternalRemovePageRange(pagePos, subCount); | |
308 | ||
2ddb4d13 WS |
309 | tree->DeleteChildren( pageId ); |
310 | tree->Delete( pageId ); | |
311 | tree->InvalidateBestSize(); | |
eca15c0d VZ |
312 | |
313 | return oldPage; | |
314 | } | |
315 | ||
316 | bool wxTreebook::DeleteAllPages() | |
317 | { | |
318 | wxBookCtrlBase::DeleteAllPages(); | |
319 | m_treeIds.Clear(); | |
320 | m_selection = | |
321 | m_actualSelection = wxNOT_FOUND; | |
322 | ||
2ddb4d13 WS |
323 | wxTreeCtrl *tree = GetTreeCtrl(); |
324 | tree->DeleteChildren(tree->GetRootItem()); | |
eca15c0d VZ |
325 | |
326 | return true; | |
327 | } | |
328 | ||
329 | void wxTreebook::DoInternalAddPage(size_t newPos, | |
330 | wxTreebookPage *page, | |
331 | wxTreeItemId pageId) | |
332 | { | |
333 | wxASSERT_MSG( newPos <= m_treeIds.GetCount(), wxT("Ivalid index passed to wxTreebook::DoInternalAddPage") ); | |
334 | ||
335 | // hide newly inserted page initially (it will be shown when selected) | |
336 | if ( page ) | |
337 | page->Hide(); | |
338 | ||
339 | if ( newPos == m_treeIds.GetCount() ) | |
340 | { | |
341 | // append | |
342 | m_treeIds.Add(pageId); | |
343 | } | |
344 | else // insert | |
345 | { | |
346 | m_treeIds.Insert(pageId, newPos); | |
347 | ||
348 | if ( m_selection != wxNOT_FOUND && newPos <= (size_t)m_selection ) | |
349 | { | |
350 | // selection has been moved one unit toward the end | |
351 | ++m_selection; | |
352 | if ( m_actualSelection != wxNOT_FOUND ) | |
353 | ++m_actualSelection; | |
354 | } | |
355 | else if ( m_actualSelection != wxNOT_FOUND && | |
356 | newPos <= (size_t)m_actualSelection ) | |
357 | { | |
358 | DoSetSelection(m_selection); | |
359 | } | |
360 | } | |
361 | } | |
362 | ||
363 | void wxTreebook::DoInternalRemovePageRange(size_t pagePos, size_t subCount) | |
364 | { | |
365 | // Attention: this function is only for a situation when we delete a node | |
366 | // with all its children so pagePos is the node's index and subCount is the | |
367 | // node children count | |
368 | wxASSERT_MSG( pagePos + subCount < m_treeIds.GetCount(), | |
369 | wxT("Ivalid page index") ); | |
370 | ||
371 | wxTreeItemId pageId = m_treeIds[pagePos]; | |
372 | ||
373 | m_treeIds.RemoveAt(pagePos, subCount + 1); | |
374 | ||
375 | if ( m_selection != wxNOT_FOUND ) | |
376 | { | |
377 | if ( (size_t)m_selection > pagePos + subCount) | |
378 | { | |
379 | // selection is far after the deleted page, so just update the index and move on | |
380 | m_selection -= 1 + subCount; | |
381 | if ( m_actualSelection != wxNOT_FOUND) | |
382 | { | |
383 | m_actualSelection -= subCount + 1; | |
384 | } | |
385 | } | |
386 | else if ( (size_t)m_selection >= pagePos ) | |
387 | { | |
2ddb4d13 WS |
388 | wxTreeCtrl *tree = GetTreeCtrl(); |
389 | ||
eca15c0d VZ |
390 | // as selected page is going to be deleted, try to select the next |
391 | // sibling if exists, if not then the parent | |
2ddb4d13 | 392 | wxTreeItemId nodeId = tree->GetNextSibling(pageId); |
eca15c0d VZ |
393 | |
394 | m_selection = wxNOT_FOUND; | |
395 | m_actualSelection = wxNOT_FOUND; | |
396 | ||
397 | if ( nodeId.IsOk() ) | |
398 | { | |
399 | // selecting next siblings | |
2ddb4d13 | 400 | tree->SelectItem(nodeId); |
eca15c0d VZ |
401 | } |
402 | else // no next sibling, select the parent | |
403 | { | |
2ddb4d13 | 404 | wxTreeItemId parentId = tree->GetItemParent(pageId); |
eca15c0d | 405 | |
2ddb4d13 | 406 | if ( parentId.IsOk() && parentId != tree->GetRootItem() ) |
eca15c0d | 407 | { |
2ddb4d13 | 408 | tree->SelectItem(parentId); |
eca15c0d VZ |
409 | } |
410 | else // parent is root | |
411 | { | |
412 | // we can't select it as it's hidden | |
413 | DoUpdateSelection(false, wxNOT_FOUND); | |
414 | } | |
415 | } | |
416 | } | |
417 | else if ( m_actualSelection != wxNOT_FOUND && | |
418 | (size_t)m_actualSelection >= pagePos ) | |
419 | { | |
420 | // nothing to do -- selection is before the deleted node, but | |
421 | // actually shown page (the first (sub)child with page != NULL) is | |
422 | // already deleted | |
423 | m_actualSelection = m_selection; | |
424 | DoSetSelection(m_selection); | |
425 | } | |
426 | //else: nothing to do -- selection is before the deleted node | |
427 | } | |
428 | else | |
429 | { | |
430 | DoUpdateSelection(false, wxNOT_FOUND); | |
431 | } | |
432 | } | |
433 | ||
434 | ||
435 | void wxTreebook::DoUpdateSelection(bool bSelect, int newPos) | |
436 | { | |
437 | int newSelPos; | |
438 | if ( bSelect ) | |
439 | { | |
440 | newSelPos = newPos; | |
441 | } | |
442 | else if ( m_selection == wxNOT_FOUND && DoInternalGetPageCount() > 0 ) | |
443 | { | |
444 | newSelPos = 0; | |
445 | } | |
446 | else | |
447 | { | |
448 | newSelPos = wxNOT_FOUND; | |
449 | } | |
450 | ||
451 | if ( newSelPos != wxNOT_FOUND ) | |
452 | { | |
453 | SetSelection((size_t)newSelPos); | |
454 | } | |
455 | } | |
456 | ||
457 | wxTreeItemId wxTreebook::DoInternalGetPage(size_t pagePos) const | |
458 | { | |
459 | if ( pagePos >= m_treeIds.GetCount() ) | |
460 | { | |
461 | // invalid position but ok here, in this internal function, don't assert | |
462 | // (the caller will do it) | |
463 | return wxTreeItemId(); | |
464 | } | |
465 | ||
466 | return m_treeIds[pagePos]; | |
467 | } | |
468 | ||
469 | int wxTreebook::DoInternalFindPageById(wxTreeItemId pageId) const | |
470 | { | |
471 | const size_t count = m_treeIds.GetCount(); | |
472 | for ( size_t i = 0; i < count; ++i ) | |
473 | { | |
474 | if ( m_treeIds[i] == pageId ) | |
475 | return i; | |
476 | } | |
477 | ||
478 | return wxNOT_FOUND; | |
479 | } | |
480 | ||
481 | bool wxTreebook::IsNodeExpanded(size_t pagePos) const | |
482 | { | |
483 | wxTreeItemId pageId = DoInternalGetPage(pagePos); | |
484 | ||
485 | wxCHECK_MSG( pageId.IsOk(), false, wxT("invalid tree item") ); | |
486 | ||
2ddb4d13 | 487 | return GetTreeCtrl()->IsExpanded(pageId); |
eca15c0d VZ |
488 | } |
489 | ||
490 | bool wxTreebook::ExpandNode(size_t pagePos, bool expand) | |
491 | { | |
492 | wxTreeItemId pageId = DoInternalGetPage(pagePos); | |
493 | ||
494 | wxCHECK_MSG( pageId.IsOk(), false, wxT("invalid tree item") ); | |
495 | ||
496 | if ( expand ) | |
497 | { | |
2ddb4d13 | 498 | GetTreeCtrl()->Expand( pageId ); |
eca15c0d VZ |
499 | } |
500 | else // collapse | |
501 | { | |
2ddb4d13 | 502 | GetTreeCtrl()->Collapse( pageId ); |
eca15c0d VZ |
503 | |
504 | // rely on the events generated by wxTreeCtrl to update selection | |
505 | } | |
506 | ||
507 | return true; | |
508 | } | |
509 | ||
510 | int wxTreebook::GetPageParent(size_t pagePos) const | |
511 | { | |
512 | wxTreeItemId nodeId = DoInternalGetPage( pagePos ); | |
513 | wxCHECK_MSG( nodeId.IsOk(), wxNOT_FOUND, wxT("Invalid page index spacified!") ); | |
514 | ||
2ddb4d13 | 515 | const wxTreeItemId parent = GetTreeCtrl()->GetItemParent( nodeId ); |
eca15c0d VZ |
516 | |
517 | return parent.IsOk() ? DoInternalFindPageById(parent) : wxNOT_FOUND; | |
518 | } | |
519 | ||
520 | bool wxTreebook::SetPageText(size_t n, const wxString& strText) | |
521 | { | |
522 | wxTreeItemId pageId = DoInternalGetPage(n); | |
523 | ||
524 | wxCHECK_MSG( pageId.IsOk(), false, wxT("invalid tree item") ); | |
525 | ||
2ddb4d13 | 526 | GetTreeCtrl()->SetItemText(pageId, strText); |
eca15c0d VZ |
527 | |
528 | return true; | |
529 | } | |
530 | ||
531 | wxString wxTreebook::GetPageText(size_t n) const | |
532 | { | |
533 | wxTreeItemId pageId = DoInternalGetPage(n); | |
534 | ||
535 | wxCHECK_MSG( pageId.IsOk(), wxString(), wxT("invalid tree item") ); | |
536 | ||
2ddb4d13 | 537 | return GetTreeCtrl()->GetItemText(pageId); |
eca15c0d VZ |
538 | } |
539 | ||
540 | int wxTreebook::GetPageImage(size_t n) const | |
541 | { | |
542 | wxTreeItemId pageId = DoInternalGetPage(n); | |
543 | ||
544 | wxCHECK_MSG( pageId.IsOk(), wxNOT_FOUND, wxT("invalid tree item") ); | |
545 | ||
2ddb4d13 | 546 | return GetTreeCtrl()->GetItemImage(pageId); |
eca15c0d VZ |
547 | } |
548 | ||
549 | bool wxTreebook::SetPageImage(size_t n, int imageId) | |
550 | { | |
551 | wxTreeItemId pageId = DoInternalGetPage(n); | |
552 | ||
553 | wxCHECK_MSG( pageId.IsOk(), false, wxT("invalid tree item") ); | |
554 | ||
2ddb4d13 | 555 | GetTreeCtrl()->SetItemImage(pageId, imageId); |
eca15c0d VZ |
556 | |
557 | return true; | |
558 | } | |
559 | ||
560 | wxSize wxTreebook::CalcSizeFromPage(const wxSize& sizePage) const | |
561 | { | |
2ddb4d13 | 562 | const wxSize sizeTree = GetControllerSize(); |
eca15c0d VZ |
563 | |
564 | wxSize size = sizePage; | |
565 | size.x += sizeTree.x; | |
566 | ||
567 | return size; | |
568 | } | |
569 | ||
570 | int wxTreebook::GetSelection() const | |
571 | { | |
572 | return m_selection; | |
573 | } | |
574 | ||
575 | int wxTreebook::SetSelection(size_t pagePos) | |
576 | { | |
577 | if ( (size_t)m_selection != pagePos ) | |
578 | return DoSetSelection(pagePos); | |
579 | ||
580 | return m_selection; | |
581 | } | |
582 | ||
583 | int wxTreebook::DoSetSelection(size_t pagePos) | |
584 | { | |
585 | wxCHECK_MSG( IS_VALID_PAGE(pagePos), wxNOT_FOUND, | |
586 | wxT("invalid page index in wxListbook::SetSelection()") ); | |
587 | wxASSERT_MSG( GetPageCount() == DoInternalGetPageCount(), | |
588 | wxT("wxTreebook logic error: m_treeIds and m_pages not in sync!")); | |
589 | ||
590 | const int oldSel = m_selection; | |
2ddb4d13 | 591 | wxTreeCtrl *tree = GetTreeCtrl(); |
eca15c0d VZ |
592 | |
593 | wxTreebookEvent event(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING, m_windowId); | |
594 | event.SetEventObject(this); | |
595 | event.SetSelection(pagePos); | |
596 | event.SetOldSelection(m_selection); | |
597 | ||
598 | // don't send the event if the old and new pages are the same; do send it | |
599 | // otherwise and be prepared for it to be vetoed | |
600 | if ( (int)pagePos == m_selection || | |
601 | !GetEventHandler()->ProcessEvent(event) || | |
602 | event.IsAllowed() ) | |
603 | { | |
604 | // hide the previously shown page | |
605 | wxTreebookPage * const oldPage = DoGetCurrentPage(); | |
606 | if ( oldPage ) | |
607 | oldPage->Hide(); | |
608 | ||
609 | // then show the new one | |
610 | m_selection = pagePos; | |
611 | wxTreebookPage *page = wxBookCtrlBase::GetPage(m_selection); | |
612 | if ( !page ) | |
613 | { | |
614 | // find the next page suitable to be shown: the first (grand)child | |
615 | // of this one with a non-NULL associated page | |
616 | wxTreeItemId childId = m_treeIds[pagePos]; | |
617 | m_actualSelection = pagePos; | |
618 | while ( !page && childId.IsOk() ) | |
619 | { | |
620 | wxTreeItemIdValue cookie; | |
2ddb4d13 | 621 | childId = tree->GetFirstChild( childId, cookie ); |
eca15c0d VZ |
622 | if ( childId.IsOk() ) |
623 | { | |
624 | page = wxBookCtrlBase::GetPage(++m_actualSelection); | |
625 | } | |
626 | } | |
627 | ||
628 | wxASSERT_MSG( page, wxT("no page to show found!") ); | |
629 | } | |
630 | ||
631 | if ( page ) | |
632 | { | |
633 | page->SetSize(GetPageRect()); | |
634 | page->Show(); | |
635 | } | |
636 | ||
2ddb4d13 | 637 | tree->SelectItem(DoInternalGetPage(pagePos)); |
eca15c0d VZ |
638 | |
639 | // notify about the (now completed) page change | |
640 | event.SetEventType(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED); | |
641 | (void)GetEventHandler()->ProcessEvent(event); | |
642 | } | |
643 | else // page change vetoed | |
644 | { | |
645 | // tree selection might have already had changed | |
2ddb4d13 | 646 | tree->SelectItem(DoInternalGetPage(oldSel)); |
eca15c0d VZ |
647 | } |
648 | ||
649 | return oldSel; | |
650 | } | |
651 | ||
652 | void wxTreebook::SetImageList(wxImageList *imageList) | |
653 | { | |
654 | wxBookCtrlBase::SetImageList(imageList); | |
2ddb4d13 | 655 | GetTreeCtrl()->SetImageList(imageList); |
eca15c0d VZ |
656 | } |
657 | ||
658 | void wxTreebook::AssignImageList(wxImageList *imageList) | |
659 | { | |
660 | wxBookCtrlBase::AssignImageList(imageList); | |
2ddb4d13 | 661 | GetTreeCtrl()->SetImageList(imageList); |
eca15c0d VZ |
662 | } |
663 | ||
664 | // ---------------------------------------------------------------------------- | |
665 | // event handlers | |
666 | // ---------------------------------------------------------------------------- | |
667 | ||
668 | void wxTreebook::OnTreeSelectionChange(wxTreeEvent& event) | |
669 | { | |
670 | wxTreeItemId newId = event.GetItem(); | |
671 | ||
672 | if ( (m_selection == wxNOT_FOUND && | |
2ddb4d13 | 673 | (!newId.IsOk() || newId == GetTreeCtrl()->GetRootItem())) || |
eca15c0d VZ |
674 | (m_selection != wxNOT_FOUND && newId == m_treeIds[m_selection]) ) |
675 | { | |
676 | // this event can only come when we modify the tree selection ourselves | |
677 | // so we should simply ignore it | |
678 | return; | |
679 | } | |
680 | ||
681 | int newPos = DoInternalFindPageById(newId); | |
682 | ||
683 | if ( newPos != wxNOT_FOUND ) | |
684 | SetSelection( newPos ); | |
685 | } | |
686 | ||
687 | void wxTreebook::OnTreeNodeExpandedCollapsed(wxTreeEvent & event) | |
688 | { | |
689 | wxTreeItemId nodeId = event.GetItem(); | |
2ddb4d13 | 690 | if ( !nodeId.IsOk() || nodeId == GetTreeCtrl()->GetRootItem() ) |
eca15c0d VZ |
691 | return; |
692 | int pagePos = DoInternalFindPageById(nodeId); | |
693 | wxCHECK_RET( pagePos != wxNOT_FOUND, wxT("Internal problem in wxTreebook!..") ); | |
694 | ||
2ddb4d13 | 695 | wxTreebookEvent ev(GetTreeCtrl()->IsExpanded(nodeId) |
eca15c0d VZ |
696 | ? wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED |
697 | : wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED, | |
698 | m_windowId); | |
699 | ||
700 | ev.SetSelection(pagePos); | |
701 | ev.SetOldSelection(pagePos); | |
702 | ev.SetEventObject(this); | |
703 | ||
704 | GetEventHandler()->ProcessEvent(ev); | |
705 | } | |
706 | ||
707 | // ---------------------------------------------------------------------------- | |
708 | // wxTreebook geometry management | |
709 | // ---------------------------------------------------------------------------- | |
710 | ||
eca15c0d VZ |
711 | wxTreebookPage * wxTreebook::DoGetCurrentPage() const |
712 | { | |
713 | if ( m_selection == wxNOT_FOUND ) | |
714 | return NULL; | |
715 | ||
716 | wxTreebookPage *page = wxBookCtrlBase::GetPage(m_selection); | |
717 | if ( !page && m_actualSelection != wxNOT_FOUND ) | |
718 | { | |
719 | page = wxBookCtrlBase::GetPage(m_actualSelection); | |
720 | } | |
721 | ||
722 | return page; | |
723 | } | |
724 | ||
725 | #endif // wxUSE_TREEBOOK |