1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxNotebook
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include <wx/string.h>
21 #include <wx/imaglist.h>
23 #include <wx/control.h>
24 #include <wx/notebook.h>
26 #include <wx/os2/private.h>
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
32 // check that the page index is valid
33 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
36 #define m_hwnd (HWND)GetHWND()
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // This is a work-around for missing defines in gcc-2.95 headers
44 #define TCS_RIGHT 0x0002
48 #define TCS_VERTICAL 0x0080
52 #define TCS_BOTTOM TCS_RIGHT
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
60 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
62 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
63 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
64 EVT_SIZE(wxNotebook::OnSize
)
65 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
66 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
69 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
70 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
72 // ============================================================================
74 // ============================================================================
76 // ----------------------------------------------------------------------------
77 // wxNotebook construction
78 // ----------------------------------------------------------------------------
80 // common part of all ctors
81 void wxNotebook::Init()
87 // default for dynamic class
88 wxNotebook::wxNotebook()
93 // the same arguments as for wxControl
94 wxNotebook::wxNotebook(wxWindow
*parent
,
103 Create(parent
, id
, pos
, size
, style
, name
);
107 bool wxNotebook::Create(wxWindow
*parent
,
112 const wxString
& name
)
115 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
119 // TODO: m_backgroundColour = wxColour(GetSysColor(COLOR_BTNFACE));
120 m_foregroundColour
= *wxBLACK
;
125 m_windowStyle = style | wxTAB_TRAVERSAL;
127 long tabStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | TCS_TABS;
129 if (m_windowStyle & wxCLIP_CHILDREN)
130 tabStyle |= WS_CLIPCHILDREN;
131 if ( m_windowStyle & wxTC_MULTILINE )
132 tabStyle |= TCS_MULTILINE;
133 if ( m_windowStyle & wxBORDER )
134 tabStyle &= WS_BORDER;
135 if (m_windowStyle & wxNB_FIXEDWIDTH)
136 tabStyle |= TCS_FIXEDWIDTH ;
137 if (m_windowStyle & wxNB_BOTTOM)
138 tabStyle |= TCS_RIGHT;
139 if (m_windowStyle & wxNB_LEFT)
140 tabStyle |= TCS_VERTICAL;
141 if (m_windowStyle & wxNB_RIGHT)
142 tabStyle |= TCS_VERTICAL|TCS_RIGHT;
145 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL,
146 this, NULL, pos.x, pos.y, size.x, size.y,
152 // Not all compilers recognise SetWindowFont
153 ::SendMessage(GetHwnd(), WM_SETFONT,
154 (WPARAM)::GetStockObject(DEFAULT_GUI_FONT), TRUE);
157 if ( parent != NULL )
158 parent->AddChild(this);
166 wxNotebook::~wxNotebook()
170 // ----------------------------------------------------------------------------
171 // wxNotebook accessors
172 // ----------------------------------------------------------------------------
173 int wxNotebook::GetPageCount() const
175 return m_aPages
.Count();
178 int wxNotebook::GetRowCount() const
184 int wxNotebook::SetSelection(int nPage
)
186 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
188 ChangePage(m_nSelection
, nPage
);
194 void wxNotebook::AdvanceSelection(bool bForward
)
196 int nSel
= GetSelection();
197 int nMax
= GetPageCount() - 1;
199 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
201 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
204 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
206 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
212 wxString
wxNotebook::GetPageText(int nPage
) const
214 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
220 int wxNotebook::GetPageImage(int nPage
) const
222 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
228 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
230 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
236 void wxNotebook::SetImageList(wxImageList
* imageList
)
238 m_pImageList
= imageList
;
242 void wxNotebook::SetTabSize(const wxSize
& sz
)
247 // ----------------------------------------------------------------------------
248 // wxNotebook operations
249 // ----------------------------------------------------------------------------
251 // remove one page from the notebook
252 bool wxNotebook::DeletePage(int nPage
)
254 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
256 // TODO: delete native widget page
258 delete m_aPages
[nPage
];
259 m_aPages
.Remove(nPage
);
264 // remove one page from the notebook, without deleting the window
265 bool wxNotebook::RemovePage(int nPage
)
267 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
269 m_aPages
.Remove(nPage
);
275 bool wxNotebook::DeleteAllPages()
277 // TODO: delete native widget pages
279 int nPageCount
= GetPageCount();
281 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
282 delete m_aPages
[nPage
];
289 // add a page to the notebook
290 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
291 const wxString
& strText
,
295 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
298 // same as AddPage() but does it at given position
299 bool wxNotebook::InsertPage(int nPage
,
300 wxNotebookPage
*pPage
,
301 const wxString
& strText
,
305 wxASSERT( pPage
!= NULL
);
306 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
308 // TODO: insert native widget page
310 // save the pointer to the page
311 m_aPages
.Insert(pPage
, nPage
);
313 // some page must be selected: either this one or the first one if there is
314 // still no selection
316 m_nSelection
= nPage
;
317 else if ( m_nSelection
== -1 )
323 // ----------------------------------------------------------------------------
324 // wxNotebook callbacks
325 // ----------------------------------------------------------------------------
327 // @@@ OnSize() is used for setting the font when it's called for the first
328 // time because doing it in ::Create() doesn't work (for unknown reasons)
329 void wxNotebook::OnSize(wxSizeEvent
& event
)
331 static bool s_bFirstTime
= TRUE
;
332 if ( s_bFirstTime
) {
333 // TODO: any first-time-size processing.
334 s_bFirstTime
= FALSE
;
337 // TODO: all this may or may not be necessary for your platform
339 // emulate page change (it's esp. important to do it first time because
340 // otherwise our page would stay invisible)
341 int nSel
= m_nSelection
;
345 // fit the notebook page to the tab control's display area
349 unsigned int nCount
= m_aPages
.Count();
350 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
351 wxNotebookPage
*pPage
= m_aPages
[nPage
];
352 pPage
->SetSize(0, 0, w
, h
);
353 #if wxUSE_CONSTRAINTS
354 if ( pPage
->GetAutoLayout() )
356 #endif //wxUSE_CONSTRAINTS
360 // Processing continues to next OnSize
364 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
366 // is it our tab control?
367 if ( event
.GetEventObject() == this )
369 int sel
= event
.GetOldSelection();
371 m_aPages
[sel
]->Show(FALSE
);
373 sel
= event
.GetSelection();
376 wxNotebookPage
*pPage
= m_aPages
[sel
];
383 // we want to give others a chance to process this message as well
387 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
389 // set focus to the currently selected page if any
390 if ( m_nSelection
!= -1 )
391 m_aPages
[m_nSelection
]->SetFocus();
396 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
398 if ( event
.IsWindowChange() ) {
400 AdvanceSelection(event
.GetDirection());
403 // pass to the parent
405 event
.SetCurrentFocus(this);
406 GetParent()->ProcessEvent(event
);
411 // ----------------------------------------------------------------------------
412 // wxNotebook base class virtuals
413 // ----------------------------------------------------------------------------
415 // override these 2 functions to do nothing: everything is done in OnSize
417 void wxNotebook::SetConstraintSizes(bool /* recurse */)
419 #if wxUSE_CONSTRAINTS
420 // don't set the sizes of the pages - their correct size is not yet known
421 wxControl::SetConstraintSizes(FALSE
);
425 bool wxNotebook::DoPhase(int /* nPhase */)
430 bool wxNotebook::OS2OnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
432 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
435 NMHDR* hdr = (NMHDR *)lParam;
436 switch ( hdr->code ) {
438 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
441 case TCN_SELCHANGING:
442 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
446 return wxControl::MSWOnNotify(idCtrl, lParam, result);
449 // TODO: event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
450 event
.SetOldSelection(m_nSelection
);
451 event
.SetEventObject(this);
452 event
.SetInt(idCtrl
);
454 bool processed
= GetEventHandler()->ProcessEvent(event
);
455 // TODO: *result = !event.IsAllowed();
459 // ----------------------------------------------------------------------------
460 // wxNotebook helper functions
461 // ----------------------------------------------------------------------------
463 // hide the currently active panel and show the new one
464 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
466 // MT-FIXME should use a real semaphore
467 static bool s_bInsideChangePage
= FALSE
;
469 // when we call ProcessEvent(), our own OnSelChange() is called which calls
470 // this function - break the infinite loop
471 if ( s_bInsideChangePage
)
474 // it's not an error (the message may be generated by the tab control itself)
475 // and it may happen - just do nothing
476 if ( nSel
== nOldSel
)
479 s_bInsideChangePage
= TRUE
;
481 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
482 event
.SetSelection(nSel
);
483 event
.SetOldSelection(nOldSel
);
484 event
.SetEventObject(this);
485 if ( ProcessEvent(event
) && !event
.IsAllowed() )
487 // program doesn't allow the page change
488 s_bInsideChangePage
= FALSE
;
492 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
495 s_bInsideChangePage
= FALSE
;