1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/notebook.cpp
3 // Purpose: implementation of wxNotebook
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "notebook.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include <wx/string.h>
29 #include <wx/imaglist.h>
31 #include <wx/control.h>
32 #include <wx/notebook.h>
34 #include <wx/msw/private.h>
36 // Windows standard headers
38 #error "wxNotebook is only supported Windows 95 and above"
41 #include <windowsx.h> // for SetWindowFont
45 #ifndef wxUSE_NORLANDER_HEADERS
46 #include "wx/msw/gnuwin32/extra.h"
51 #if !defined(__GNUWIN32__) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // check that the page index is valid
60 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
63 #define m_hwnd (HWND)GetHWND()
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 #if !USE_SHARED_LIBRARIES
70 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
71 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
73 EVT_SIZE(wxNotebook::OnSize
)
75 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
77 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
80 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
81 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
84 // ============================================================================
86 // ============================================================================
88 // ----------------------------------------------------------------------------
89 // wxNotebook construction
90 // ----------------------------------------------------------------------------
92 // common part of all ctors
93 void wxNotebook::Init()
99 // default for dynamic class
100 wxNotebook::wxNotebook()
105 // the same arguments as for wxControl
106 wxNotebook::wxNotebook(wxWindow
*parent
,
111 const wxString
& name
)
115 Create(parent
, id
, pos
, size
, style
, name
);
119 bool wxNotebook::Create(wxWindow
*parent
,
124 const wxString
& name
)
127 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
131 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
132 m_foregroundColour
= *wxBLACK
;
135 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
137 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
139 if (m_windowStyle
& wxCLIP_CHILDREN
)
140 tabStyle
|= WS_CLIPCHILDREN
;
141 if ( m_windowStyle
& wxTC_MULTILINE
)
142 tabStyle
|= TCS_MULTILINE
;
143 if ( m_windowStyle
& wxBORDER
)
144 tabStyle
&= WS_BORDER
;
145 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
146 tabStyle
|= TCS_FIXEDWIDTH
;
148 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL
,
149 this, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
,
155 // Not all compilers recognise SetWindowFont
156 ::SendMessage(GetHwnd(), WM_SETFONT
,
157 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
), TRUE
);
160 if ( parent
!= NULL
)
161 parent
->AddChild(this);
169 wxNotebook::~wxNotebook()
173 // ----------------------------------------------------------------------------
174 // wxNotebook accessors
175 // ----------------------------------------------------------------------------
176 int wxNotebook::GetPageCount() const
179 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
181 return m_aPages
.Count();
184 int wxNotebook::GetRowCount() const
186 return TabCtrl_GetRowCount(m_hwnd
);
189 int wxNotebook::SetSelection(int nPage
)
191 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("notebook page out of range") );
193 ChangePage(m_nSelection
, nPage
);
195 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
198 void wxNotebook::AdvanceSelection(bool bForward
)
200 int nSel
= GetSelection();
201 int nMax
= GetPageCount() - 1;
203 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
205 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
208 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
210 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
213 tcItem
.mask
= TCIF_TEXT
;
214 tcItem
.pszText
= (wxChar
*)strText
.c_str();
216 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
219 wxString
wxNotebook::GetPageText(int nPage
) const
221 wxCHECK_MSG( IS_VALID_PAGE(nPage
), _T(""), _T("notebook page out of range") );
225 tcItem
.mask
= TCIF_TEXT
;
226 tcItem
.pszText
= buf
;
227 tcItem
.cchTextMax
= WXSIZEOF(buf
);
230 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
231 str
= tcItem
.pszText
;
236 int wxNotebook::GetPageImage(int nPage
) const
238 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("notebook page out of range") );
241 tcItem
.mask
= TCIF_IMAGE
;
243 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
246 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
248 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
251 tcItem
.mask
= TCIF_IMAGE
;
252 tcItem
.iImage
= nImage
;
254 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
257 void wxNotebook::SetImageList(wxImageList
* imageList
)
259 m_pImageList
= imageList
;
260 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
264 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
266 void wxNotebook::SetTabSize(const wxSize
& sz
)
268 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
271 // ----------------------------------------------------------------------------
272 // wxNotebook operations
273 // ----------------------------------------------------------------------------
275 // remove one page from the notebook
276 bool wxNotebook::DeletePage(int nPage
)
278 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
280 if ( m_nSelection
== nPage
) {
281 // advance selection backwards - the page being deleted shouldn't be left
283 AdvanceSelection(FALSE
);
286 TabCtrl_DeleteItem(m_hwnd
, nPage
);
288 delete m_aPages
[nPage
];
289 m_aPages
.Remove(nPage
);
291 if ( m_aPages
.IsEmpty() ) {
292 // no selection if the notebook became empty
299 // remove one page from the notebook, without deleting
300 bool wxNotebook::RemovePage(int nPage
)
302 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
304 TabCtrl_DeleteItem(m_hwnd
, nPage
);
306 m_aPages
.Remove(nPage
);
312 bool wxNotebook::DeleteAllPages()
314 int nPageCount
= GetPageCount();
316 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
317 delete m_aPages
[nPage
];
321 TabCtrl_DeleteAllItems(m_hwnd
);
326 // add a page to the notebook
327 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
328 const wxString
& strText
,
332 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
335 // same as AddPage() but does it at given position
336 bool wxNotebook::InsertPage(int nPage
,
337 wxNotebookPage
*pPage
,
338 const wxString
& strText
,
342 wxASSERT( pPage
!= NULL
);
343 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
345 // do add the tab to the control
347 // init all fields to 0
349 memset(&tcItem
, 0, sizeof(tcItem
));
353 tcItem
.mask
|= TCIF_IMAGE
;
354 tcItem
.iImage
= imageId
;
357 if ( !strText
.IsEmpty() )
359 tcItem
.mask
|= TCIF_TEXT
;
360 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
363 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
364 wxLogError(_T("Can't create the notebook page '%s'."), strText
.c_str());
369 // if the inserted page is before the selected one, we must update the
370 // index of the selected page
371 if ( nPage
<= m_nSelection
)
373 // one extra page added
377 // save the pointer to the page
378 m_aPages
.Insert(pPage
, nPage
);
380 // don't show pages by default (we'll need to adjust their size first)
381 HWND hwnd
= GetWinHwnd(pPage
);
382 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
384 // this updates internal flag too - otherwise it will get out of sync
387 // some page should be selected: either this one or the first one if there is
388 // still no selection
392 else if ( m_nSelection
== -1 )
396 SetSelection(selNew
);
401 // ----------------------------------------------------------------------------
402 // wxNotebook callbacks
403 // ----------------------------------------------------------------------------
405 void wxNotebook::OnSize(wxSizeEvent
& event
)
407 // fit the notebook page to the tab control's display area
409 rc
.left
= rc
.top
= 0;
410 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
412 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
413 size_t nCount
= m_aPages
.Count();
414 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
415 wxNotebookPage
*pPage
= m_aPages
[nPage
];
416 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
417 if ( pPage
->GetAutoLayout() )
424 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
426 // is it our tab control?
427 if ( event
.GetEventObject() == this )
429 int sel
= event
.GetOldSelection();
431 m_aPages
[sel
]->Show(FALSE
);
433 sel
= event
.GetSelection();
436 wxNotebookPage
*pPage
= m_aPages
[sel
];
444 // we want to give others a chance to process this message as well
448 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
450 // set focus to the currently selected page if any
451 if ( m_nSelection
!= -1 )
452 m_aPages
[m_nSelection
]->SetFocus();
457 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
459 if ( event
.IsWindowChange() ) {
461 AdvanceSelection(event
.GetDirection());
464 // pass to the parent
466 event
.SetCurrentFocus(this);
467 GetParent()->GetEventHandler()->ProcessEvent(event
);
472 // ----------------------------------------------------------------------------
473 // wxNotebook base class virtuals
474 // ----------------------------------------------------------------------------
476 // override these 2 functions to do nothing: everything is done in OnSize
478 void wxNotebook::SetConstraintSizes(bool /* recurse */)
480 // don't set the sizes of the pages - their correct size is not yet known
481 wxControl::SetConstraintSizes(FALSE
);
484 bool wxNotebook::DoPhase(int /* nPhase */)
489 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
491 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
493 NMHDR
* hdr
= (NMHDR
*)lParam
;
494 switch ( hdr
->code
) {
496 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
499 case TCN_SELCHANGING
:
500 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
504 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
507 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
508 event
.SetOldSelection(m_nSelection
);
509 event
.SetEventObject(this);
510 event
.SetInt(idCtrl
);
512 bool processed
= GetEventHandler()->ProcessEvent(event
);
513 *result
= !event
.IsAllowed();
517 // ----------------------------------------------------------------------------
518 // wxNotebook helper functions
519 // ----------------------------------------------------------------------------
521 // generate the page changing and changed events, hide the currently active
522 // panel and show the new one
523 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
525 // MT-FIXME should use a real semaphore
526 static bool s_bInsideChangePage
= FALSE
;
528 // when we call ProcessEvent(), our own OnSelChange() is called which calls
529 // this function - break the infinite loop
530 if ( s_bInsideChangePage
)
533 // it's not an error (the message may be generated by the tab control itself)
534 // and it may happen - just do nothing
535 if ( nSel
== nOldSel
)
538 s_bInsideChangePage
= TRUE
;
540 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
541 event
.SetSelection(nSel
);
542 event
.SetOldSelection(nOldSel
);
543 event
.SetEventObject(this);
544 if ( ProcessEvent(event
) && !event
.IsAllowed() )
546 // program doesn't allow the page change
547 s_bInsideChangePage
= FALSE
;
551 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
554 s_bInsideChangePage
= FALSE
;