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
;
147 if (m_windowStyle
& wxNB_BOTTOM
)
148 tabStyle
|= TCS_RIGHT
;
149 if (m_windowStyle
& wxNB_LEFT
)
150 tabStyle
|= TCS_VERTICAL
;
151 if (m_windowStyle
& wxNB_RIGHT
)
152 tabStyle
|= TCS_VERTICAL
|TCS_RIGHT
;
155 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL
,
156 this, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
,
162 // Not all compilers recognise SetWindowFont
163 ::SendMessage(GetHwnd(), WM_SETFONT
,
164 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
), TRUE
);
167 if ( parent
!= NULL
)
168 parent
->AddChild(this);
176 wxNotebook::~wxNotebook()
180 // ----------------------------------------------------------------------------
181 // wxNotebook accessors
182 // ----------------------------------------------------------------------------
183 int wxNotebook::GetPageCount() const
186 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
188 return m_aPages
.Count();
191 int wxNotebook::GetRowCount() const
193 return TabCtrl_GetRowCount(m_hwnd
);
196 int wxNotebook::SetSelection(int nPage
)
198 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("notebook page out of range") );
200 ChangePage(m_nSelection
, nPage
);
202 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
205 void wxNotebook::AdvanceSelection(bool bForward
)
207 int nSel
= GetSelection();
208 int nMax
= GetPageCount() - 1;
210 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
212 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
215 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
217 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
220 tcItem
.mask
= TCIF_TEXT
;
221 tcItem
.pszText
= (wxChar
*)strText
.c_str();
223 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
226 wxString
wxNotebook::GetPageText(int nPage
) const
228 wxCHECK_MSG( IS_VALID_PAGE(nPage
), _T(""), _T("notebook page out of range") );
232 tcItem
.mask
= TCIF_TEXT
;
233 tcItem
.pszText
= buf
;
234 tcItem
.cchTextMax
= WXSIZEOF(buf
);
237 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
238 str
= tcItem
.pszText
;
243 int wxNotebook::GetPageImage(int nPage
) const
245 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("notebook page out of range") );
248 tcItem
.mask
= TCIF_IMAGE
;
250 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
253 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
255 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
258 tcItem
.mask
= TCIF_IMAGE
;
259 tcItem
.iImage
= nImage
;
261 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
264 void wxNotebook::SetImageList(wxImageList
* imageList
)
266 m_pImageList
= imageList
;
267 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
271 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
273 void wxNotebook::SetTabSize(const wxSize
& sz
)
275 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
278 // ----------------------------------------------------------------------------
279 // wxNotebook operations
280 // ----------------------------------------------------------------------------
282 // remove one page from the notebook
283 bool wxNotebook::DeletePage(int nPage
)
285 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
287 if ( m_nSelection
== nPage
) {
288 // advance selection backwards - the page being deleted shouldn't be left
290 AdvanceSelection(FALSE
);
293 TabCtrl_DeleteItem(m_hwnd
, nPage
);
295 delete m_aPages
[nPage
];
296 m_aPages
.Remove(nPage
);
298 if ( m_aPages
.IsEmpty() ) {
299 // no selection if the notebook became empty
306 // remove one page from the notebook, without deleting
307 bool wxNotebook::RemovePage(int nPage
)
309 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
311 TabCtrl_DeleteItem(m_hwnd
, nPage
);
313 m_aPages
.Remove(nPage
);
319 bool wxNotebook::DeleteAllPages()
321 int nPageCount
= GetPageCount();
323 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
324 delete m_aPages
[nPage
];
328 TabCtrl_DeleteAllItems(m_hwnd
);
333 // add a page to the notebook
334 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
335 const wxString
& strText
,
339 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
342 // same as AddPage() but does it at given position
343 bool wxNotebook::InsertPage(int nPage
,
344 wxNotebookPage
*pPage
,
345 const wxString
& strText
,
349 wxASSERT( pPage
!= NULL
);
350 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
352 // do add the tab to the control
354 // init all fields to 0
356 memset(&tcItem
, 0, sizeof(tcItem
));
360 tcItem
.mask
|= TCIF_IMAGE
;
361 tcItem
.iImage
= imageId
;
364 if ( !strText
.IsEmpty() )
366 tcItem
.mask
|= TCIF_TEXT
;
367 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
370 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
371 wxLogError(_T("Can't create the notebook page '%s'."), strText
.c_str());
376 // if the inserted page is before the selected one, we must update the
377 // index of the selected page
378 if ( nPage
<= m_nSelection
)
380 // one extra page added
384 // save the pointer to the page
385 m_aPages
.Insert(pPage
, nPage
);
387 // don't show pages by default (we'll need to adjust their size first)
388 HWND hwnd
= GetWinHwnd(pPage
);
389 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
391 // this updates internal flag too - otherwise it will get out of sync
394 // some page should be selected: either this one or the first one if there is
395 // still no selection
399 else if ( m_nSelection
== -1 )
403 SetSelection(selNew
);
408 // ----------------------------------------------------------------------------
409 // wxNotebook callbacks
410 // ----------------------------------------------------------------------------
412 void wxNotebook::OnSize(wxSizeEvent
& event
)
414 // fit the notebook page to the tab control's display area
416 rc
.left
= rc
.top
= 0;
417 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
419 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
420 size_t nCount
= m_aPages
.Count();
421 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
422 wxNotebookPage
*pPage
= m_aPages
[nPage
];
423 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
424 if ( pPage
->GetAutoLayout() )
431 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
433 // is it our tab control?
434 if ( event
.GetEventObject() == this )
436 int sel
= event
.GetOldSelection();
438 m_aPages
[sel
]->Show(FALSE
);
440 sel
= event
.GetSelection();
443 wxNotebookPage
*pPage
= m_aPages
[sel
];
451 // we want to give others a chance to process this message as well
455 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
457 // set focus to the currently selected page if any
458 if ( m_nSelection
!= -1 )
459 m_aPages
[m_nSelection
]->SetFocus();
464 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
466 if ( event
.IsWindowChange() ) {
468 AdvanceSelection(event
.GetDirection());
471 // pass to the parent
473 event
.SetCurrentFocus(this);
474 GetParent()->GetEventHandler()->ProcessEvent(event
);
479 // ----------------------------------------------------------------------------
480 // wxNotebook base class virtuals
481 // ----------------------------------------------------------------------------
483 // override these 2 functions to do nothing: everything is done in OnSize
485 void wxNotebook::SetConstraintSizes(bool /* recurse */)
487 // don't set the sizes of the pages - their correct size is not yet known
488 wxControl::SetConstraintSizes(FALSE
);
491 bool wxNotebook::DoPhase(int /* nPhase */)
496 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
498 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
500 NMHDR
* hdr
= (NMHDR
*)lParam
;
501 switch ( hdr
->code
) {
503 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
506 case TCN_SELCHANGING
:
507 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
511 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
514 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
515 event
.SetOldSelection(m_nSelection
);
516 event
.SetEventObject(this);
517 event
.SetInt(idCtrl
);
519 bool processed
= GetEventHandler()->ProcessEvent(event
);
520 *result
= !event
.IsAllowed();
524 // ----------------------------------------------------------------------------
525 // wxNotebook helper functions
526 // ----------------------------------------------------------------------------
528 // generate the page changing and changed events, hide the currently active
529 // panel and show the new one
530 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
532 // MT-FIXME should use a real semaphore
533 static bool s_bInsideChangePage
= FALSE
;
535 // when we call ProcessEvent(), our own OnSelChange() is called which calls
536 // this function - break the infinite loop
537 if ( s_bInsideChangePage
)
540 // it's not an error (the message may be generated by the tab control itself)
541 // and it may happen - just do nothing
542 if ( nSel
== nOldSel
)
545 s_bInsideChangePage
= TRUE
;
547 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
548 event
.SetSelection(nSel
);
549 event
.SetOldSelection(nOldSel
);
550 event
.SetEventObject(this);
551 if ( ProcessEvent(event
) && !event
.IsAllowed() )
553 // program doesn't allow the page change
554 s_bInsideChangePage
= FALSE
;
558 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
561 s_bInsideChangePage
= FALSE
;