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 #if !USE_SHARED_LIBRARIES
60 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
61 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
62 EVT_SIZE(wxNotebook::OnSize
)
63 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
64 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
67 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
68 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
71 // ============================================================================
73 // ============================================================================
75 // ----------------------------------------------------------------------------
76 // wxNotebook construction
77 // ----------------------------------------------------------------------------
79 // common part of all ctors
80 void wxNotebook::Init()
86 // default for dynamic class
87 wxNotebook::wxNotebook()
92 // the same arguments as for wxControl
93 wxNotebook::wxNotebook(wxWindow
*parent
,
102 Create(parent
, id
, pos
, size
, style
, name
);
106 bool wxNotebook::Create(wxWindow
*parent
,
111 const wxString
& name
)
114 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
118 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
119 m_foregroundColour
= *wxBLACK
;
124 m_windowStyle = style | wxTAB_TRAVERSAL;
126 long tabStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | TCS_TABS;
128 if (m_windowStyle & wxCLIP_CHILDREN)
129 tabStyle |= WS_CLIPCHILDREN;
130 if ( m_windowStyle & wxTC_MULTILINE )
131 tabStyle |= TCS_MULTILINE;
132 if ( m_windowStyle & wxBORDER )
133 tabStyle &= WS_BORDER;
134 if (m_windowStyle & wxNB_FIXEDWIDTH)
135 tabStyle |= TCS_FIXEDWIDTH ;
136 if (m_windowStyle & wxNB_BOTTOM)
137 tabStyle |= TCS_RIGHT;
138 if (m_windowStyle & wxNB_LEFT)
139 tabStyle |= TCS_VERTICAL;
140 if (m_windowStyle & wxNB_RIGHT)
141 tabStyle |= TCS_VERTICAL|TCS_RIGHT;
144 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL,
145 this, NULL, pos.x, pos.y, size.x, size.y,
151 // Not all compilers recognise SetWindowFont
152 ::SendMessage(GetHwnd(), WM_SETFONT,
153 (WPARAM)::GetStockObject(DEFAULT_GUI_FONT), TRUE);
156 if ( parent != NULL )
157 parent->AddChild(this);
165 wxNotebook::~wxNotebook()
169 // ----------------------------------------------------------------------------
170 // wxNotebook accessors
171 // ----------------------------------------------------------------------------
172 int wxNotebook::GetPageCount() const
174 return m_aPages
.Count();
177 int wxNotebook::GetRowCount() const
183 int wxNotebook::SetSelection(int nPage
)
185 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
187 ChangePage(m_nSelection
, nPage
);
193 void wxNotebook::AdvanceSelection(bool bForward
)
195 int nSel
= GetSelection();
196 int nMax
= GetPageCount() - 1;
198 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
200 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
203 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
205 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
211 wxString
wxNotebook::GetPageText(int nPage
) const
213 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
219 int wxNotebook::GetPageImage(int nPage
) const
221 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
227 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
229 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
235 void wxNotebook::SetImageList(wxImageList
* imageList
)
237 m_pImageList
= imageList
;
241 void wxNotebook::SetTabSize(const wxSize
& sz
)
246 // ----------------------------------------------------------------------------
247 // wxNotebook operations
248 // ----------------------------------------------------------------------------
250 // remove one page from the notebook
251 bool wxNotebook::DeletePage(int nPage
)
253 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
255 // TODO: delete native widget page
257 delete m_aPages
[nPage
];
258 m_aPages
.Remove(nPage
);
263 // remove one page from the notebook, without deleting the window
264 bool wxNotebook::RemovePage(int nPage
)
266 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
268 m_aPages
.Remove(nPage
);
274 bool wxNotebook::DeleteAllPages()
276 // TODO: delete native widget pages
278 int nPageCount
= GetPageCount();
280 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
281 delete m_aPages
[nPage
];
288 // add a page to the notebook
289 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
290 const wxString
& strText
,
294 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
297 // same as AddPage() but does it at given position
298 bool wxNotebook::InsertPage(int nPage
,
299 wxNotebookPage
*pPage
,
300 const wxString
& strText
,
304 wxASSERT( pPage
!= NULL
);
305 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
307 // TODO: insert native widget page
309 // save the pointer to the page
310 m_aPages
.Insert(pPage
, nPage
);
312 // some page must be selected: either this one or the first one if there is
313 // still no selection
315 m_nSelection
= nPage
;
316 else if ( m_nSelection
== -1 )
322 // ----------------------------------------------------------------------------
323 // wxNotebook callbacks
324 // ----------------------------------------------------------------------------
326 // @@@ OnSize() is used for setting the font when it's called for the first
327 // time because doing it in ::Create() doesn't work (for unknown reasons)
328 void wxNotebook::OnSize(wxSizeEvent
& event
)
330 static bool s_bFirstTime
= TRUE
;
331 if ( s_bFirstTime
) {
332 // TODO: any first-time-size processing.
333 s_bFirstTime
= FALSE
;
336 // TODO: all this may or may not be necessary for your platform
338 // emulate page change (it's esp. important to do it first time because
339 // otherwise our page would stay invisible)
340 int nSel
= m_nSelection
;
344 // fit the notebook page to the tab control's display area
348 unsigned int nCount
= m_aPages
.Count();
349 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
350 wxNotebookPage
*pPage
= m_aPages
[nPage
];
351 pPage
->SetSize(0, 0, w
, h
);
352 if ( pPage
->GetAutoLayout() )
356 // Processing continues to next OnSize
360 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
362 // is it our tab control?
363 if ( event
.GetEventObject() == this )
365 int sel
= event
.GetOldSelection();
367 m_aPages
[sel
]->Show(FALSE
);
369 sel
= event
.GetSelection();
372 wxNotebookPage
*pPage
= m_aPages
[sel
];
379 // we want to give others a chance to process this message as well
383 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
385 // set focus to the currently selected page if any
386 if ( m_nSelection
!= -1 )
387 m_aPages
[m_nSelection
]->SetFocus();
392 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
394 if ( event
.IsWindowChange() ) {
396 AdvanceSelection(event
.GetDirection());
399 // pass to the parent
401 event
.SetCurrentFocus(this);
402 GetParent()->ProcessEvent(event
);
407 // ----------------------------------------------------------------------------
408 // wxNotebook base class virtuals
409 // ----------------------------------------------------------------------------
411 // override these 2 functions to do nothing: everything is done in OnSize
413 void wxNotebook::SetConstraintSizes(bool /* recurse */)
415 // don't set the sizes of the pages - their correct size is not yet known
416 wxControl::SetConstraintSizes(FALSE
);
419 bool wxNotebook::DoPhase(int /* nPhase */)
424 bool wxNotebook::OS2OnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
426 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
429 NMHDR* hdr = (NMHDR *)lParam;
430 switch ( hdr->code ) {
432 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
435 case TCN_SELCHANGING:
436 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
440 return wxControl::MSWOnNotify(idCtrl, lParam, result);
443 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
444 event
.SetOldSelection(m_nSelection
);
445 event
.SetEventObject(this);
446 event
.SetInt(idCtrl
);
448 bool processed
= GetEventHandler()->ProcessEvent(event
);
449 *result
= !event
.IsAllowed();
453 // ----------------------------------------------------------------------------
454 // wxNotebook helper functions
455 // ----------------------------------------------------------------------------
457 // hide the currently active panel and show the new one
458 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
460 // MT-FIXME should use a real semaphore
461 static bool s_bInsideChangePage
= FALSE
;
463 // when we call ProcessEvent(), our own OnSelChange() is called which calls
464 // this function - break the infinite loop
465 if ( s_bInsideChangePage
)
468 // it's not an error (the message may be generated by the tab control itself)
469 // and it may happen - just do nothing
470 if ( nSel
== nOldSel
)
473 s_bInsideChangePage
= TRUE
;
475 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
476 event
.SetSelection(nSel
);
477 event
.SetOldSelection(nOldSel
);
478 event
.SetEventObject(this);
479 if ( ProcessEvent(event
) && !event
.IsAllowed() )
481 // program doesn't allow the page change
482 s_bInsideChangePage
= FALSE
;
486 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
489 s_bInsideChangePage
= FALSE
;