1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxNotebook
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "notebook.h"
25 #pragma message disable unscomzer
28 // For compilers that support precompilation, includes "wx.h".
29 #include "wx/wxprec.h"
37 #include "wx/string.h"
39 #include "wx/settings.h"
40 #include "wx/generic/imaglist.h"
41 #include "wx/notebook.h"
42 #include "wx/dcclient.h"
43 #include "wx/generic/tabg.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // check that the page index is valid
50 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
57 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
59 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
60 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
61 EVT_SIZE(wxNotebook::OnSize
)
62 EVT_PAINT(wxNotebook::OnPaint
)
63 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent
)
64 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
65 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
68 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
69 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
71 // ============================================================================
73 // ============================================================================
75 // ============================================================================
77 // ============================================================================
79 // This reuses wxTabView to draw the tabs.
80 class WXDLLEXPORT wxNotebookTabView
: public wxTabView
82 DECLARE_DYNAMIC_CLASS(wxNotebookTabView
)
84 wxNotebookTabView(wxNotebook
* notebook
, long style
= wxTAB_STYLE_DRAW_BOX
| wxTAB_STYLE_COLOUR_INTERIOR
);
85 ~wxNotebookTabView(void);
87 // Called when a tab is activated
88 virtual void OnTabActivate(int activateId
, int deactivateId
);
90 virtual bool OnTabPreActivate(int activateId
, int deactivateId
);
93 wxNotebook
* m_notebook
;
96 // ----------------------------------------------------------------------------
97 // wxNotebook construction
98 // ----------------------------------------------------------------------------
100 // common part of all ctors
101 void wxNotebook::Init()
103 m_tabView
= (wxNotebookTabView
*) NULL
;
107 // default for dynamic class
108 wxNotebook::wxNotebook()
113 // the same arguments as for wxControl
114 wxNotebook::wxNotebook(wxWindow
*parent
,
119 const wxString
& name
)
123 Create(parent
, id
, pos
, size
, style
, name
);
127 bool wxNotebook::Create(wxWindow
*parent
,
132 const wxString
& name
)
137 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
139 if (!wxControl::Create(parent
, id
, pos
, size
, style
|wxNO_BORDER
, wxDefaultValidator
, name
))
142 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
144 SetTabView(new wxNotebookTabView(this));
150 wxNotebook::~wxNotebook()
155 // ----------------------------------------------------------------------------
156 // wxNotebook accessors
157 // ----------------------------------------------------------------------------
158 int wxNotebook::GetRowCount() const
164 int wxNotebook::SetSelection(size_t nPage
)
166 wxASSERT( IS_VALID_PAGE(nPage
) );
168 wxNotebookPage
* pPage
= GetPage(nPage
);
170 m_tabView
->SetTabSelection((int) (long) pPage
);
177 void wxNotebook::AdvanceSelection(bool bForward
)
179 int nSel
= GetSelection();
180 int nMax
= GetPageCount() - 1;
182 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
184 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
188 bool wxNotebook::SetPageText(size_t nPage
, const wxString
& strText
)
190 wxASSERT( IS_VALID_PAGE(nPage
) );
192 wxNotebookPage
* page
= GetPage(nPage
);
195 m_tabView
->SetTabText((int) (long) page
, strText
);
203 wxString
wxNotebook::GetPageText(size_t nPage
) const
205 wxASSERT( IS_VALID_PAGE(nPage
) );
207 wxNotebookPage
* page
= ((wxNotebook
*)this)->GetPage(nPage
);
209 return m_tabView
->GetTabText((int) (long) page
);
211 return wxEmptyString
;
214 int wxNotebook::GetPageImage(size_t nPage
) const
216 wxASSERT( IS_VALID_PAGE(nPage
) );
222 bool wxNotebook::SetPageImage(size_t nPage
, int nImage
)
224 wxASSERT( IS_VALID_PAGE(nPage
) );
230 // set the size (the same for all pages)
231 void wxNotebook::SetPageSize(const wxSize
& size
)
236 // set the padding between tabs (in pixels)
237 void wxNotebook::SetPadding(const wxSize
& padding
)
242 // set the size of the tabs for wxNB_FIXEDWIDTH controls
243 void wxNotebook::SetTabSize(const wxSize
& sz
)
248 // ----------------------------------------------------------------------------
249 // wxNotebook operations
250 // ----------------------------------------------------------------------------
252 // remove one page from the notebook and delete it
253 bool wxNotebook::DeletePage(size_t nPage
)
255 wxCHECK( IS_VALID_PAGE(nPage
), false );
257 if (m_nSelection
!= -1)
259 m_pages
[m_nSelection
]->Show(false);
260 m_pages
[m_nSelection
]->Lower();
263 wxNotebookPage
* pPage
= GetPage(nPage
);
265 m_tabView
->RemoveTab((int) (long) pPage
);
267 m_pages
.Remove(pPage
);
270 if (m_pages
.GetCount() == 0)
273 m_tabView
->SetTabSelection(-1, false);
275 else if (m_nSelection
> -1)
279 m_tabView
->SetTabSelection((int) (long) GetPage(0), false);
281 if (m_nSelection
!= 0)
285 RefreshLayout(false);
290 bool wxNotebook::DeletePage(wxNotebookPage
* page
)
292 int pagePos
= FindPagePosition(page
);
294 return DeletePage(pagePos
);
299 bool wxNotebook::RemovePage(size_t nPage
)
301 return DoRemovePage(nPage
) != NULL
;
304 // remove one page from the notebook
305 wxWindow
* wxNotebook::DoRemovePage(size_t nPage
)
307 wxCHECK( IS_VALID_PAGE(nPage
), NULL
);
309 m_pages
[nPage
]->Show(false);
310 // m_pages[nPage]->Lower();
312 wxNotebookPage
* pPage
= GetPage(nPage
);
314 m_tabView
->RemoveTab((int) (long) pPage
);
316 m_pages
.Remove(pPage
);
318 if (m_pages
.GetCount() == 0)
321 m_tabView
->SetTabSelection(-1, true);
323 else if (m_nSelection
> -1)
325 // Only change the selection if the page we
326 // deleted was the selection.
327 if (nPage
== (size_t)m_nSelection
)
330 // Select the first tab. Generates a ChangePage.
331 m_tabView
->SetTabSelection(0, true);
335 // We must adjust which tab we think is selected.
336 // If greater than the page we deleted, it must be moved down
338 if (size_t(m_nSelection
) > nPage
)
343 RefreshLayout(false);
348 bool wxNotebook::RemovePage(wxNotebookPage
* page
)
350 int pagePos
= FindPagePosition(page
);
352 return RemovePage(pagePos
);
357 // Find the position of the wxNotebookPage, -1 if not found.
358 int wxNotebook::FindPagePosition(wxNotebookPage
* page
) const
360 size_t nPageCount
= GetPageCount();
362 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
363 if (m_pages
[nPage
] == page
)
369 bool wxNotebook::DeleteAllPages()
371 m_tabView
->ClearTabs(true);
373 size_t nPageCount
= GetPageCount();
375 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
376 delete m_pages
[nPage
];
383 // same as AddPage() but does it at given position
384 bool wxNotebook::InsertPage(size_t nPage
,
385 wxNotebookPage
*pPage
,
386 const wxString
& strText
,
390 wxASSERT( pPage
!= NULL
);
391 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false );
393 m_tabView
->AddTab((int) (long) pPage
, strText
);
398 // save the pointer to the page
399 m_pages
.Insert(pPage
, nPage
);
403 // This will cause ChangePage to be called, via OnSelPage
405 m_tabView
->SetTabSelection((int) (long) pPage
, true);
408 // some page must be selected: either this one or the first one if there is
409 // still no selection
410 if ( m_nSelection
== -1 )
413 RefreshLayout(false);
418 // ----------------------------------------------------------------------------
419 // wxNotebook callbacks
420 // ----------------------------------------------------------------------------
422 // @@@ OnSize() is used for setting the font when it's called for the first
423 // time because doing it in ::Create() doesn't work (for unknown reasons)
424 void wxNotebook::OnSize(wxSizeEvent
& event
)
426 static bool s_bFirstTime
= true;
427 if ( s_bFirstTime
) {
428 // TODO: any first-time-size processing.
429 s_bFirstTime
= false;
434 // Processing continues to next OnSize
438 // This was supposed to cure the non-display of the notebook
439 // until the user resizes the window.
441 void wxNotebook::OnInternalIdle()
443 wxWindow::OnInternalIdle();
446 static bool s_bFirstTime
= true;
447 if ( s_bFirstTime
) {
449 wxSize sz(GetSize());
457 wxSize sz(GetSize());
458 wxSizeEvent sizeEvent(sz, GetId());
459 sizeEvent.SetEventObject(this);
460 GetEventHandler()->ProcessEvent(sizeEvent);
463 s_bFirstTime
= false;
468 // Implementation: calculate the layout of the view rect
469 // and resize the children if required
470 bool wxNotebook::RefreshLayout(bool force
)
474 wxRect oldRect
= m_tabView
->GetViewRect();
477 GetClientSize(& cw
, & ch
);
479 int tabHeight
= m_tabView
->GetTotalTabHeight();
482 rect
.y
= tabHeight
+ 4;
484 rect
.height
= ch
- 4 - rect
.y
;
486 m_tabView
->SetViewRect(rect
);
488 m_tabView
->LayoutTabs();
490 // Need to do it a 2nd time to get the tab height with
491 // the new view width, since changing the view width changes the
493 tabHeight
= m_tabView
->GetTotalTabHeight();
495 rect
.y
= tabHeight
+ 4;
497 rect
.height
= ch
- 4 - rect
.y
;
499 m_tabView
->SetViewRect(rect
);
501 m_tabView
->LayoutTabs();
503 if (!force
&& (rect
== oldRect
))
506 // fit the notebook page to the tab control's display area
508 size_t nCount
= m_pages
.Count();
509 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
510 wxNotebookPage
*pPage
= m_pages
[nPage
];
511 wxRect clientRect
= GetAvailableClientSize();
512 if (pPage
->IsShown())
514 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
515 if ( pPage
->GetAutoLayout() )
524 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
526 // is it our tab control?
527 if ( event
.GetEventObject() == this )
529 if (event
.GetSelection() != m_nSelection
)
530 ChangePage(event
.GetOldSelection(), event
.GetSelection());
533 // we want to give others a chance to process this message as well
537 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
539 // set focus to the currently selected page if any
540 if ( m_nSelection
!= -1 )
541 m_pages
[m_nSelection
]->SetFocus();
546 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
548 if ( event
.IsWindowChange() ) {
550 AdvanceSelection(event
.GetDirection());
553 // pass to the parent
555 event
.SetCurrentFocus(this);
556 GetParent()->ProcessEvent(event
);
561 // ----------------------------------------------------------------------------
562 // wxNotebook base class virtuals
563 // ----------------------------------------------------------------------------
565 // override these 2 functions to do nothing: everything is done in OnSize
567 void wxNotebook::SetConstraintSizes(bool /* recurse */)
569 // don't set the sizes of the pages - their correct size is not yet known
570 wxControl::SetConstraintSizes(false);
573 bool wxNotebook::DoPhase(int /* nPhase */)
578 void wxNotebook::Command(wxCommandEvent
& WXUNUSED(event
))
580 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
583 // ----------------------------------------------------------------------------
584 // wxNotebook helper functions
585 // ----------------------------------------------------------------------------
587 // hide the currently active panel and show the new one
588 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
590 // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n";
591 wxASSERT( nOldSel
!= nSel
); // impossible
593 if ( nOldSel
!= -1 ) {
594 m_pages
[nOldSel
]->Show(false);
595 m_pages
[nOldSel
]->Lower();
598 wxNotebookPage
*pPage
= m_pages
[nSel
];
600 wxRect clientRect
= GetAvailableClientSize();
601 pPage
->SetSize(clientRect
.x
, clientRect
.y
, clientRect
.width
, clientRect
.height
);
612 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
615 m_tabView
->OnEvent(event
);
618 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
625 wxRect
wxNotebook::GetAvailableClientSize()
628 GetClientSize(& cw
, & ch
);
630 int tabHeight
= m_tabView
->GetTotalTabHeight();
632 // TODO: these margins should be configurable.
635 rect
.y
= tabHeight
+ 6;
636 rect
.width
= cw
- 12;
637 rect
.height
= ch
- 4 - rect
.y
;
646 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
648 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
650 m_notebook
= notebook
;
652 m_notebook
->SetTabView(this);
654 SetWindow(m_notebook
);
657 wxNotebookTabView::~wxNotebookTabView(void)
661 // Called when a tab is activated
662 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
667 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
, m_notebook
->GetId());
669 // Translate from wxTabView's ids (which aren't position-dependent)
670 // to wxNotebook's (which are).
671 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
672 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
674 int activatePos
= m_notebook
->FindPagePosition(pActive
);
675 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
677 event
.SetEventObject(m_notebook
);
678 event
.SetSelection(activatePos
);
679 event
.SetOldSelection(deactivatePos
);
680 m_notebook
->GetEventHandler()->ProcessEvent(event
);
684 bool wxNotebookTabView::OnTabPreActivate(int activateId
, int deactivateId
)
690 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_notebook
->GetId());
692 // Translate from wxTabView's ids (which aren't position-dependent)
693 // to wxNotebook's (which are).
694 wxNotebookPage
* pActive
= (wxNotebookPage
*) activateId
;
695 wxNotebookPage
* pDeactive
= (wxNotebookPage
*) deactivateId
;
697 int activatePos
= m_notebook
->FindPagePosition(pActive
);
698 int deactivatePos
= m_notebook
->FindPagePosition(pDeactive
);
700 event
.SetEventObject(m_notebook
);
701 event
.SetSelection(activatePos
);
702 event
.SetOldSelection(deactivatePos
);
703 if (m_notebook
->GetEventHandler()->ProcessEvent(event
))
705 retval
= event
.IsAllowed();
711 #endif // __WXPALMOS__