]> git.saurik.com Git - wxWidgets.git/blob - src/generic/notebook.cpp
Fixed linking errors related to wxAnyButton in wxUniv.
[wxWidgets.git] / src / generic / notebook.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/notebook.cpp
3 // Purpose: generic implementation of wxNotebook
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/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
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_NOTEBOOK
28
29 #include "wx/notebook.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/string.h"
33 #include "wx/log.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
36 #endif
37
38 #include "wx/imaglist.h"
39 #include "wx/generic/tabg.h"
40
41 // ----------------------------------------------------------------------------
42 // macros
43 // ----------------------------------------------------------------------------
44
45 // check that the page index is valid
46 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
47
48 // ----------------------------------------------------------------------------
49 // event table
50 // ----------------------------------------------------------------------------
51
52 BEGIN_EVENT_TABLE(wxNotebook, wxBookCtrlBase)
53 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, wxNotebook::OnSelChange)
54 EVT_SIZE(wxNotebook::OnSize)
55 EVT_PAINT(wxNotebook::OnPaint)
56 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent)
57 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
58 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
59 END_EVENT_TABLE()
60
61 // ============================================================================
62 // implementation
63 // ============================================================================
64
65 // ============================================================================
66 // Private class
67 // ============================================================================
68
69 WX_DECLARE_HASH_MAP(int, wxNotebookPage*, wxIntegerHash, wxIntegerEqual,
70 wxIntToNotebookPageHashMap);
71
72 WX_DECLARE_HASH_MAP(wxNotebookPage*, int, wxPointerHash, wxPointerEqual,
73 wxNotebookPageToIntHashMap);
74
75 // This reuses wxTabView to draw the tabs.
76 class WXDLLEXPORT wxNotebookTabView: public wxTabView
77 {
78 DECLARE_DYNAMIC_CLASS(wxNotebookTabView)
79 public:
80 wxNotebookTabView(wxNotebook* notebook, long style = wxTAB_STYLE_DRAW_BOX | wxTAB_STYLE_COLOUR_INTERIOR);
81 virtual ~wxNotebookTabView(void);
82
83 // Called when a tab is activated
84 virtual void OnTabActivate(int activateId, int deactivateId);
85 // Allows vetoing
86 virtual bool OnTabPreActivate(int activateId, int deactivateId);
87
88 // map integer ids used by wxTabView to wxNotebookPage pointers
89 int GetId(wxNotebookPage *page);
90 wxNotebookPage *GetPage(int id) { return m_idToPage[id]; }
91
92 protected:
93 wxNotebook* m_notebook;
94
95 private:
96 wxIntToNotebookPageHashMap m_idToPage;
97 wxNotebookPageToIntHashMap m_pageToId;
98 int m_nextid;
99 };
100
101 static int GetPageId(wxTabView *tabview, wxNotebookPage *page)
102 {
103 return static_cast<wxNotebookTabView*>(tabview)->GetId(page);
104 }
105
106 // ----------------------------------------------------------------------------
107 // wxNotebook construction
108 // ----------------------------------------------------------------------------
109
110 // common part of all ctors
111 void wxNotebook::Init()
112 {
113 m_tabView = NULL;
114 m_selection = -1;
115 }
116
117 // default for dynamic class
118 wxNotebook::wxNotebook()
119 {
120 Init();
121 }
122
123 // the same arguments as for wxControl
124 wxNotebook::wxNotebook(wxWindow *parent,
125 wxWindowID id,
126 const wxPoint& pos,
127 const wxSize& size,
128 long style,
129 const wxString& name)
130 {
131 Init();
132
133 Create(parent, id, pos, size, style, name);
134 }
135
136 // Create() function
137 bool wxNotebook::Create(wxWindow *parent,
138 wxWindowID id,
139 const wxPoint& pos,
140 const wxSize& size,
141 long style,
142 const wxString& name)
143 {
144 // base init
145 SetName(name);
146
147 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
148 style |= wxBK_TOP;
149
150 m_windowId = id == wxID_ANY ? NewControlId() : id;
151
152 if (!wxControl::Create(parent, id, pos, size, style|wxNO_BORDER, wxDefaultValidator, name))
153 return false;
154
155 SetTabView(new wxNotebookTabView(this));
156
157 return true;
158 }
159
160 // dtor
161 wxNotebook::~wxNotebook()
162 {
163 delete m_tabView;
164 }
165
166 // ----------------------------------------------------------------------------
167 // wxNotebook accessors
168 // ----------------------------------------------------------------------------
169 int wxNotebook::GetRowCount() const
170 {
171 // TODO
172 return 0;
173 }
174
175 int wxNotebook::SetSelection(size_t nPage)
176 {
177 wxASSERT( IS_VALID_PAGE(nPage) );
178
179 wxNotebookPage* pPage = GetPage(nPage);
180
181 m_tabView->SetTabSelection(GetPageId(m_tabView, pPage));
182
183 // TODO
184 return 0;
185 }
186
187 int wxNotebook::ChangeSelection(size_t nPage)
188 {
189 // FIXME: currently it does generate events too
190 return SetSelection(nPage);
191 }
192
193 #if 0
194 void wxNotebook::AdvanceSelection(bool bForward)
195 {
196 int nSel = GetSelection();
197 int nMax = GetPageCount() - 1;
198 if ( bForward )
199 SetSelection(nSel == nMax ? 0 : nSel + 1);
200 else
201 SetSelection(nSel == 0 ? nMax : nSel - 1);
202 }
203 #endif
204
205 bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
206 {
207 wxASSERT( IS_VALID_PAGE(nPage) );
208
209 wxNotebookPage* page = GetPage(nPage);
210 if (page)
211 {
212 m_tabView->SetTabText(GetPageId(m_tabView, page), strText);
213 Refresh();
214 return true;
215 }
216
217 return false;
218 }
219
220 wxString wxNotebook::GetPageText(size_t nPage) const
221 {
222 wxASSERT( IS_VALID_PAGE(nPage) );
223
224 wxNotebookPage* page = ((wxNotebook*)this)->GetPage(nPage);
225 if (page)
226 return m_tabView->GetTabText(GetPageId(m_tabView, page));
227 else
228 return wxEmptyString;
229 }
230
231 int wxNotebook::GetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage)) const
232 {
233 wxASSERT( IS_VALID_PAGE(nPage) );
234
235 // TODO
236 return 0;
237 }
238
239 bool wxNotebook::SetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage),
240 int WXUNUSED(nImage))
241 {
242 wxASSERT( IS_VALID_PAGE(nPage) );
243
244 // TODO
245 return false;
246 }
247
248 // set the size (the same for all pages)
249 void wxNotebook::SetPageSize(const wxSize& WXUNUSED(size))
250 {
251 // TODO
252 }
253
254 // set the padding between tabs (in pixels)
255 void wxNotebook::SetPadding(const wxSize& WXUNUSED(padding))
256 {
257 // TODO
258 }
259
260 // set the size of the tabs for wxNB_FIXEDWIDTH controls
261 void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
262 {
263 // TODO
264 }
265
266 // ----------------------------------------------------------------------------
267 // wxNotebook operations
268 // ----------------------------------------------------------------------------
269
270 // remove one page from the notebook and delete it
271 bool wxNotebook::DeletePage(size_t nPage)
272 {
273 wxCHECK( IS_VALID_PAGE(nPage), false );
274
275 if (m_selection != -1)
276 {
277 m_pages[m_selection]->Show(false);
278 m_pages[m_selection]->Lower();
279 }
280
281 wxNotebookPage* pPage = GetPage(nPage);
282
283 m_tabView->RemoveTab(GetPageId(m_tabView, pPage));
284
285 m_pages.Remove(pPage);
286 delete pPage;
287
288 if (m_pages.GetCount() == 0)
289 {
290 m_selection = -1;
291 m_tabView->SetTabSelection(-1, false);
292 }
293 else if (m_selection > -1)
294 {
295 m_selection = -1;
296
297 m_tabView->SetTabSelection(GetPageId(m_tabView, GetPage(0)), false);
298
299 if (m_selection != 0)
300 ChangePage(-1, 0);
301 }
302
303 RefreshLayout(false);
304
305 return true;
306 }
307
308 bool wxNotebook::DeletePage(wxNotebookPage* page)
309 {
310 int pagePos = FindPagePosition(page);
311 if (pagePos > -1)
312 return DeletePage(pagePos);
313 else
314 return false;
315 }
316
317 bool wxNotebook::RemovePage(size_t nPage)
318 {
319 return DoRemovePage(nPage) != NULL;
320 }
321
322 // remove one page from the notebook
323 wxWindow* wxNotebook::DoRemovePage(size_t nPage)
324 {
325 wxCHECK( IS_VALID_PAGE(nPage), NULL );
326
327 m_pages[nPage]->Show(false);
328 // m_pages[nPage]->Lower();
329
330 wxNotebookPage* pPage = GetPage(nPage);
331
332 m_tabView->RemoveTab(GetPageId(m_tabView, pPage));
333
334 m_pages.Remove(pPage);
335
336 if (m_pages.GetCount() == 0)
337 {
338 m_selection = -1;
339 m_tabView->SetTabSelection(-1, true);
340 }
341 else if (m_selection > -1)
342 {
343 // Only change the selection if the page we
344 // deleted was the selection.
345 if (nPage == (size_t)m_selection)
346 {
347 m_selection = -1;
348 // Select the first tab. Generates a ChangePage.
349 m_tabView->SetTabSelection(0, true);
350 }
351 else
352 {
353 // We must adjust which tab we think is selected.
354 // If greater than the page we deleted, it must be moved down
355 // a notch.
356 if (size_t(m_selection) > nPage)
357 m_selection -- ;
358 }
359 }
360
361 RefreshLayout(false);
362
363 return pPage;
364 }
365
366 bool wxNotebook::RemovePage(wxNotebookPage* page)
367 {
368 int pagePos = FindPagePosition(page);
369 if (pagePos > -1)
370 return RemovePage(pagePos);
371 else
372 return false;
373 }
374
375 // Find the position of the wxNotebookPage, -1 if not found.
376 int wxNotebook::FindPagePosition(wxNotebookPage* page) const
377 {
378 size_t nPageCount = GetPageCount();
379 size_t nPage;
380 for ( nPage = 0; nPage < nPageCount; nPage++ )
381 if (m_pages[nPage] == page)
382 return nPage;
383 return -1;
384 }
385
386 // remove all pages
387 bool wxNotebook::DeleteAllPages()
388 {
389 m_tabView->ClearTabs(true);
390
391 size_t nPageCount = GetPageCount();
392 size_t nPage;
393 for ( nPage = 0; nPage < nPageCount; nPage++ )
394 delete m_pages[nPage];
395
396 m_pages.Clear();
397
398 return true;
399 }
400
401 // same as AddPage() but does it at given position
402 bool wxNotebook::InsertPage(size_t nPage,
403 wxNotebookPage *pPage,
404 const wxString& strText,
405 bool bSelect,
406 int WXUNUSED(imageId))
407 {
408 wxASSERT( pPage != NULL );
409 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), false );
410
411 m_tabView->AddTab(GetPageId(m_tabView, pPage), strText);
412
413 if (!bSelect)
414 pPage->Show(false);
415
416 // save the pointer to the page
417 m_pages.Insert(pPage, nPage);
418
419 if (bSelect)
420 {
421 // This will cause ChangePage to be called, via OnSelPage
422
423 m_tabView->SetTabSelection(GetPageId(m_tabView, pPage), true);
424 }
425
426 // some page must be selected: either this one or the first one if there is
427 // still no selection
428 if ( m_selection == -1 )
429 ChangePage(-1, 0);
430
431 RefreshLayout(false);
432
433 return true;
434 }
435
436 // ----------------------------------------------------------------------------
437 // wxNotebook callbacks
438 // ----------------------------------------------------------------------------
439
440 // @@@ OnSize() is used for setting the font when it's called for the first
441 // time because doing it in ::Create() doesn't work (for unknown reasons)
442 void wxNotebook::OnSize(wxSizeEvent& event)
443 {
444 static bool s_bFirstTime = true;
445 if ( s_bFirstTime ) {
446 // TODO: any first-time-size processing.
447 s_bFirstTime = false;
448 }
449
450 RefreshLayout();
451
452 // Processing continues to next OnSize
453 event.Skip();
454 }
455
456 // This was supposed to cure the non-display of the notebook
457 // until the user resizes the window.
458 // What's going on?
459 void wxNotebook::OnInternalIdle()
460 {
461 wxWindow::OnInternalIdle();
462
463 #if 0
464 static bool s_bFirstTime = true;
465 if ( s_bFirstTime ) {
466 /*
467 wxSize sz(GetSize());
468 sz.x ++;
469 SetSize(sz);
470 sz.x --;
471 SetSize(sz);
472 */
473
474 /*
475 wxSize sz(GetSize());
476 wxSizeEvent sizeEvent(sz, GetId());
477 sizeEvent.SetEventObject(this);
478 GetEventHandler()->ProcessEvent(sizeEvent);
479 Refresh();
480 */
481 s_bFirstTime = false;
482 }
483 #endif
484 }
485
486 // Implementation: calculate the layout of the view rect
487 // and resize the children if required
488 bool wxNotebook::RefreshLayout(bool force)
489 {
490 if (m_tabView)
491 {
492 wxRect oldRect = m_tabView->GetViewRect();
493
494 int cw, ch;
495 GetClientSize(& cw, & ch);
496
497 int tabHeight = m_tabView->GetTotalTabHeight();
498 wxRect rect;
499 rect.x = 4;
500 rect.y = tabHeight + 4;
501 rect.width = cw - 8;
502 rect.height = ch - 4 - rect.y ;
503
504 m_tabView->SetViewRect(rect);
505
506 m_tabView->LayoutTabs();
507
508 // Need to do it a 2nd time to get the tab height with
509 // the new view width, since changing the view width changes the
510 // tab layout.
511 tabHeight = m_tabView->GetTotalTabHeight();
512 rect.x = 4;
513 rect.y = tabHeight + 4;
514 rect.width = cw - 8;
515 rect.height = ch - 4 - rect.y ;
516
517 m_tabView->SetViewRect(rect);
518
519 m_tabView->LayoutTabs();
520
521 if (!force && (rect == oldRect))
522 return false;
523
524 // fit the notebook page to the tab control's display area
525
526 size_t nCount = m_pages.Count();
527 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
528 wxNotebookPage *pPage = m_pages[nPage];
529 wxRect clientRect = GetAvailableClientSize();
530 if (pPage->IsShown())
531 {
532 pPage->SetSize(clientRect.x, clientRect.y, clientRect.width, clientRect.height);
533 if ( pPage->GetAutoLayout() )
534 pPage->Layout();
535 }
536 }
537 Refresh();
538 }
539 return true;
540 }
541
542 void wxNotebook::OnSelChange(wxBookCtrlEvent& event)
543 {
544 // is it our tab control?
545 if ( event.GetEventObject() == this )
546 {
547 if (event.GetSelection() != m_selection)
548 ChangePage(event.GetOldSelection(), event.GetSelection());
549 }
550
551 // we want to give others a chance to process this message as well
552 event.Skip();
553 }
554
555 void wxNotebook::OnSetFocus(wxFocusEvent& event)
556 {
557 // set focus to the currently selected page if any
558 if ( m_selection != -1 )
559 m_pages[m_selection]->SetFocus();
560
561 event.Skip();
562 }
563
564 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
565 {
566 if ( event.IsWindowChange() )
567 {
568 // change pages
569 AdvanceSelection(event.GetDirection());
570 }
571 else {
572 // pass to the parent
573 if ( GetParent() )
574 {
575 event.SetCurrentFocus(this);
576 GetParent()->ProcessWindowEvent(event);
577 }
578 }
579 }
580
581 // ----------------------------------------------------------------------------
582 // wxNotebook base class virtuals
583 // ----------------------------------------------------------------------------
584
585 // override these 2 functions to do nothing: everything is done in OnSize
586
587 void wxNotebook::SetConstraintSizes(bool /* recurse */)
588 {
589 // don't set the sizes of the pages - their correct size is not yet known
590 wxControl::SetConstraintSizes(false);
591 }
592
593 bool wxNotebook::DoPhase(int /* nPhase */)
594 {
595 return true;
596 }
597
598 void wxNotebook::Command(wxCommandEvent& WXUNUSED(event))
599 {
600 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
601 }
602
603 // ----------------------------------------------------------------------------
604 // wxNotebook helper functions
605 // ----------------------------------------------------------------------------
606
607 // hide the currently active panel and show the new one
608 void wxNotebook::ChangePage(int nOldSel, int nSel)
609 {
610 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
611 wxASSERT( nOldSel != nSel ); // impossible
612
613 if ( nOldSel != -1 ) {
614 m_pages[nOldSel]->Show(false);
615 m_pages[nOldSel]->Lower();
616 }
617
618 wxNotebookPage *pPage = m_pages[nSel];
619
620 wxRect clientRect = GetAvailableClientSize();
621 pPage->SetSize(clientRect.x, clientRect.y, clientRect.width, clientRect.height);
622
623 Refresh();
624
625 pPage->Show(true);
626 pPage->Raise();
627 pPage->SetFocus();
628
629 m_selection = nSel;
630 }
631
632 void wxNotebook::OnMouseEvent(wxMouseEvent& event)
633 {
634 if (m_tabView)
635 m_tabView->OnEvent(event);
636 }
637
638 void wxNotebook::OnPaint(wxPaintEvent& WXUNUSED(event) )
639 {
640 wxPaintDC dc(this);
641 if (m_tabView)
642 m_tabView->Draw(dc);
643 }
644
645 wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
646 {
647 // MBN: since the total tab height is really a function of the
648 // width, this should really call
649 // GetTotalTabHeightPretendingWidthIs(), but the current
650 // implementation will suffice, provided the wxNotebook has been
651 // created with a sensible initial width.
652 return wxSize( sizePage.x + 12,
653 sizePage.y + m_tabView->GetTotalTabHeight() + 6 + 4 );
654 }
655
656 wxRect wxNotebook::GetAvailableClientSize()
657 {
658 int cw, ch;
659 GetClientSize(& cw, & ch);
660
661 int tabHeight = m_tabView->GetTotalTabHeight();
662
663 // TODO: these margins should be configurable.
664 wxRect rect;
665 rect.x = 6;
666 rect.y = tabHeight + 6;
667 rect.width = cw - 12;
668 rect.height = ch - 4 - rect.y ;
669
670 return rect;
671 }
672
673 /*
674 * wxNotebookTabView
675 */
676
677 IMPLEMENT_CLASS(wxNotebookTabView, wxTabView)
678
679 wxNotebookTabView::wxNotebookTabView(wxNotebook *notebook, long style)
680 : wxTabView(style), m_nextid(1)
681 {
682 m_notebook = notebook;
683
684 m_notebook->SetTabView(this);
685
686 SetWindow(m_notebook);
687 }
688
689 wxNotebookTabView::~wxNotebookTabView(void)
690 {
691 }
692
693 int wxNotebookTabView::GetId(wxNotebookPage *page)
694 {
695 int& id = m_pageToId[page];
696
697 if (!id)
698 {
699 id = m_nextid++;
700 m_idToPage[id] = page;
701 }
702
703 return id;
704 }
705
706 // Called when a tab is activated
707 void wxNotebookTabView::OnTabActivate(int activateId, int deactivateId)
708 {
709 if (!m_notebook)
710 return;
711
712 wxBookCtrlEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_notebook->GetId());
713
714 // Translate from wxTabView's ids (which aren't position-dependent)
715 // to wxNotebook's (which are).
716 wxNotebookPage* pActive = GetPage(activateId);
717 wxNotebookPage* pDeactive = GetPage(deactivateId);
718
719 int activatePos = m_notebook->FindPagePosition(pActive);
720 int deactivatePos = m_notebook->FindPagePosition(pDeactive);
721
722 event.SetEventObject(m_notebook);
723 event.SetSelection(activatePos);
724 event.SetOldSelection(deactivatePos);
725 m_notebook->GetEventHandler()->ProcessEvent(event);
726 }
727
728 // Allows Vetoing
729 bool wxNotebookTabView::OnTabPreActivate(int activateId, int deactivateId)
730 {
731 bool retval = true;
732
733 if (m_notebook)
734 {
735 wxBookCtrlEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_notebook->GetId());
736
737 // Translate from wxTabView's ids (which aren't position-dependent)
738 // to wxNotebook's (which are).
739 wxNotebookPage* pActive = GetPage(activateId);
740 wxNotebookPage* pDeactive = GetPage(deactivateId);
741
742 int activatePos = m_notebook->FindPagePosition(pActive);
743 int deactivatePos = m_notebook->FindPagePosition(pDeactive);
744
745 event.SetEventObject(m_notebook);
746 event.SetSelection(activatePos);
747 event.SetOldSelection(deactivatePos);
748 if (m_notebook->GetEventHandler()->ProcessEvent(event))
749 {
750 retval = event.IsAllowed();
751 }
752 }
753 return retval;
754 }
755
756 #endif // wxUSE_NOTEBOOK