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 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
60 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
61 EVT_SIZE(wxNotebook::OnSize
)
62 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
63 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
66 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
67 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
69 // ============================================================================
71 // ============================================================================
73 // ----------------------------------------------------------------------------
74 // wxNotebook construction
75 // ----------------------------------------------------------------------------
77 // common part of all ctors
78 void wxNotebook::Init()
84 // default for dynamic class
85 wxNotebook::wxNotebook()
90 // the same arguments as for wxControl
91 wxNotebook::wxNotebook(wxWindow
*parent
,
100 Create(parent
, id
, pos
, size
, style
, name
);
104 bool wxNotebook::Create(wxWindow
*parent
,
109 const wxString
& name
)
112 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
116 // TODO: m_backgroundColour = wxColour(GetSysColor(COLOR_BTNFACE));
117 m_foregroundColour
= *wxBLACK
;
122 m_windowStyle = style | wxTAB_TRAVERSAL;
124 long tabStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | TCS_TABS;
126 if (m_windowStyle & wxCLIP_CHILDREN)
127 tabStyle |= WS_CLIPCHILDREN;
128 if ( m_windowStyle & wxTC_MULTILINE )
129 tabStyle |= TCS_MULTILINE;
130 if ( m_windowStyle & wxBORDER )
131 tabStyle &= WS_BORDER;
132 if (m_windowStyle & wxNB_FIXEDWIDTH)
133 tabStyle |= TCS_FIXEDWIDTH ;
134 if (m_windowStyle & wxNB_BOTTOM)
135 tabStyle |= TCS_RIGHT;
136 if (m_windowStyle & wxNB_LEFT)
137 tabStyle |= TCS_VERTICAL;
138 if (m_windowStyle & wxNB_RIGHT)
139 tabStyle |= TCS_VERTICAL|TCS_RIGHT;
142 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL,
143 this, NULL, pos.x, pos.y, size.x, size.y,
149 // Not all compilers recognise SetWindowFont
150 ::SendMessage(GetHwnd(), WM_SETFONT,
151 (WPARAM)::GetStockObject(DEFAULT_GUI_FONT), TRUE);
154 if ( parent != NULL )
155 parent->AddChild(this);
163 wxNotebook::~wxNotebook()
167 // ----------------------------------------------------------------------------
168 // wxNotebook accessors
169 // ----------------------------------------------------------------------------
170 int wxNotebook::GetPageCount() const
172 return m_aPages
.Count();
175 int wxNotebook::GetRowCount() const
181 int wxNotebook::SetSelection(int nPage
)
183 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
185 ChangePage(m_nSelection
, nPage
);
191 void wxNotebook::AdvanceSelection(bool bForward
)
193 int nSel
= GetSelection();
194 int nMax
= GetPageCount() - 1;
196 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
198 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
201 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
203 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
209 wxString
wxNotebook::GetPageText(int nPage
) const
211 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
217 int wxNotebook::GetPageImage(int nPage
) const
219 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
225 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
227 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
233 void wxNotebook::SetImageList(wxImageList
* imageList
)
235 m_pImageList
= imageList
;
239 void wxNotebook::SetTabSize(const wxSize
& sz
)
244 // ----------------------------------------------------------------------------
245 // wxNotebook operations
246 // ----------------------------------------------------------------------------
248 // remove one page from the notebook
249 bool wxNotebook::DeletePage(int nPage
)
251 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
253 // TODO: delete native widget page
255 delete m_aPages
[nPage
];
256 m_aPages
.Remove(nPage
);
261 // remove one page from the notebook, without deleting the window
262 bool wxNotebook::RemovePage(int nPage
)
264 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
266 m_aPages
.Remove(nPage
);
272 bool wxNotebook::DeleteAllPages()
274 // TODO: delete native widget pages
276 int nPageCount
= GetPageCount();
278 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
279 delete m_aPages
[nPage
];
286 // add a page to the notebook
287 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
288 const wxString
& strText
,
292 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
295 // same as AddPage() but does it at given position
296 bool wxNotebook::InsertPage(int nPage
,
297 wxNotebookPage
*pPage
,
298 const wxString
& strText
,
302 wxASSERT( pPage
!= NULL
);
303 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
305 // TODO: insert native widget page
307 // save the pointer to the page
308 m_aPages
.Insert(pPage
, nPage
);
310 // some page must be selected: either this one or the first one if there is
311 // still no selection
313 m_nSelection
= nPage
;
314 else if ( m_nSelection
== -1 )
320 // ----------------------------------------------------------------------------
321 // wxNotebook callbacks
322 // ----------------------------------------------------------------------------
324 // @@@ OnSize() is used for setting the font when it's called for the first
325 // time because doing it in ::Create() doesn't work (for unknown reasons)
326 void wxNotebook::OnSize(wxSizeEvent
& event
)
328 static bool s_bFirstTime
= TRUE
;
329 if ( s_bFirstTime
) {
330 // TODO: any first-time-size processing.
331 s_bFirstTime
= FALSE
;
334 // TODO: all this may or may not be necessary for your platform
336 // emulate page change (it's esp. important to do it first time because
337 // otherwise our page would stay invisible)
338 int nSel
= m_nSelection
;
342 // fit the notebook page to the tab control's display area
346 unsigned int nCount
= m_aPages
.Count();
347 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
348 wxNotebookPage
*pPage
= m_aPages
[nPage
];
349 pPage
->SetSize(0, 0, w
, h
);
350 if ( pPage
->GetAutoLayout() )
354 // Processing continues to next OnSize
358 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
360 // is it our tab control?
361 if ( event
.GetEventObject() == this )
363 int sel
= event
.GetOldSelection();
365 m_aPages
[sel
]->Show(FALSE
);
367 sel
= event
.GetSelection();
370 wxNotebookPage
*pPage
= m_aPages
[sel
];
377 // we want to give others a chance to process this message as well
381 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
383 // set focus to the currently selected page if any
384 if ( m_nSelection
!= -1 )
385 m_aPages
[m_nSelection
]->SetFocus();
390 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
392 if ( event
.IsWindowChange() ) {
394 AdvanceSelection(event
.GetDirection());
397 // pass to the parent
399 event
.SetCurrentFocus(this);
400 GetParent()->ProcessEvent(event
);
405 // ----------------------------------------------------------------------------
406 // wxNotebook base class virtuals
407 // ----------------------------------------------------------------------------
409 // override these 2 functions to do nothing: everything is done in OnSize
411 void wxNotebook::SetConstraintSizes(bool /* recurse */)
413 // don't set the sizes of the pages - their correct size is not yet known
414 wxControl::SetConstraintSizes(FALSE
);
417 bool wxNotebook::DoPhase(int /* nPhase */)
422 bool wxNotebook::OS2OnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
424 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
427 NMHDR* hdr = (NMHDR *)lParam;
428 switch ( hdr->code ) {
430 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
433 case TCN_SELCHANGING:
434 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
438 return wxControl::MSWOnNotify(idCtrl, lParam, result);
441 // TODO: event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
442 event
.SetOldSelection(m_nSelection
);
443 event
.SetEventObject(this);
444 event
.SetInt(idCtrl
);
446 bool processed
= GetEventHandler()->ProcessEvent(event
);
447 // TODO: *result = !event.IsAllowed();
451 // ----------------------------------------------------------------------------
452 // wxNotebook helper functions
453 // ----------------------------------------------------------------------------
455 // hide the currently active panel and show the new one
456 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
458 // MT-FIXME should use a real semaphore
459 static bool s_bInsideChangePage
= FALSE
;
461 // when we call ProcessEvent(), our own OnSelChange() is called which calls
462 // this function - break the infinite loop
463 if ( s_bInsideChangePage
)
466 // it's not an error (the message may be generated by the tab control itself)
467 // and it may happen - just do nothing
468 if ( nSel
== nOldSel
)
471 s_bInsideChangePage
= TRUE
;
473 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
474 event
.SetSelection(nSel
);
475 event
.SetOldSelection(nOldSel
);
476 event
.SetEventObject(this);
477 if ( ProcessEvent(event
) && !event
.IsAllowed() )
479 // program doesn't allow the page change
480 s_bInsideChangePage
= FALSE
;
484 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
487 s_bInsideChangePage
= FALSE
;