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