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 licence
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "notebook.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/string.h"
31 #include "wx/imaglist.h"
33 #include "wx/control.h"
34 #include "wx/notebook.h"
36 #include "wx/msw/private.h"
38 // Windows standard headers
40 #error "wxNotebook is only supported Windows 95 and above"
43 #include <windowsx.h> // for SetWindowFont
46 #ifdef __GNUWIN32_OLD__
47 #include "wx/msw/gnuwin32/extra.h"
51 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
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 // This is a work-around for missing defines in gcc-2.95 headers
71 #define TCS_RIGHT 0x0002
75 #define TCS_VERTICAL 0x0080
79 #define TCS_BOTTOM TCS_RIGHT
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
89 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
90 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
92 EVT_SIZE(wxNotebook::OnSize
)
94 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
96 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
99 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
100 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
102 // ============================================================================
104 // ============================================================================
106 // ----------------------------------------------------------------------------
107 // wxNotebook construction
108 // ----------------------------------------------------------------------------
110 // common part of all ctors
111 void wxNotebook::Init()
117 // default for dynamic class
118 wxNotebook::wxNotebook()
123 // the same arguments as for wxControl
124 wxNotebook::wxNotebook(wxWindow
*parent
,
129 const wxString
& name
)
133 Create(parent
, id
, pos
, size
, style
, name
);
137 bool wxNotebook::Create(wxWindow
*parent
,
142 const wxString
& name
)
145 if ( !CreateControl(parent
, id
, pos
, size
, style
| wxTAB_TRAVERSAL
,
146 wxDefaultValidator
, name
) )
149 if ( !MSWCreateControl(WC_TABCONTROL
, _T(""), pos
, size
) )
152 SetBackgroundColour(wxColour(::GetSysColor(COLOR_BTNFACE
)));
157 WXDWORD
wxNotebook::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
159 WXDWORD tabStyle
= wxControl::MSWGetStyle(style
, exstyle
);
161 tabStyle
|= WS_TABSTOP
| TCS_TABS
;
163 if ( style
& wxNB_MULTILINE
)
164 tabStyle
|= TCS_MULTILINE
;
165 if ( style
& wxNB_FIXEDWIDTH
)
166 tabStyle
|= TCS_FIXEDWIDTH
;
168 if ( style
& wxNB_BOTTOM
)
169 tabStyle
|= TCS_RIGHT
;
170 else if ( style
& wxNB_LEFT
)
171 tabStyle
|= TCS_VERTICAL
;
172 else if ( style
& wxNB_RIGHT
)
173 tabStyle
|= TCS_VERTICAL
| TCS_RIGHT
;
178 // note that we never want to have the default WS_EX_CLIENTEDGE style
179 // as it looks too ugly for the notebooks
186 // ----------------------------------------------------------------------------
187 // wxNotebook accessors
188 // ----------------------------------------------------------------------------
190 int wxNotebook::GetPageCount() const
193 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
195 return m_pages
.Count();
198 int wxNotebook::GetRowCount() const
200 return TabCtrl_GetRowCount(m_hwnd
);
203 int wxNotebook::SetSelection(int nPage
)
205 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
207 if ( nPage
!= m_nSelection
)
209 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
210 event
.SetSelection(nPage
);
211 event
.SetOldSelection(m_nSelection
);
212 event
.SetEventObject(this);
213 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
215 // program allows the page change
216 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
217 (void)GetEventHandler()->ProcessEvent(event
);
219 TabCtrl_SetCurSel(m_hwnd
, nPage
);
226 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
228 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
231 tcItem
.mask
= TCIF_TEXT
;
232 tcItem
.pszText
= (wxChar
*)strText
.c_str();
234 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
237 wxString
wxNotebook::GetPageText(int nPage
) const
239 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
243 tcItem
.mask
= TCIF_TEXT
;
244 tcItem
.pszText
= buf
;
245 tcItem
.cchTextMax
= WXSIZEOF(buf
);
248 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
249 str
= tcItem
.pszText
;
254 int wxNotebook::GetPageImage(int nPage
) const
256 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
259 tcItem
.mask
= TCIF_IMAGE
;
261 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
264 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
266 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
269 tcItem
.mask
= TCIF_IMAGE
;
270 tcItem
.iImage
= nImage
;
272 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
275 void wxNotebook::SetImageList(wxImageList
* imageList
)
277 wxNotebookBase::SetImageList(imageList
);
281 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
285 // ----------------------------------------------------------------------------
286 // wxNotebook size settings
287 // ----------------------------------------------------------------------------
289 void wxNotebook::SetPageSize(const wxSize
& size
)
291 // transform the page size into the notebook size
298 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
301 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
304 void wxNotebook::SetPadding(const wxSize
& padding
)
306 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
309 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
311 void wxNotebook::SetTabSize(const wxSize
& sz
)
313 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
316 void wxNotebook::AdjustPageSize(wxNotebookPage
*page
)
318 wxCHECK_RET( page
, _T("NULL page in wxNotebook::AdjustPageSize") );
324 // get the page size from the notebook size
325 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
326 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
328 page
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
331 // ----------------------------------------------------------------------------
332 // wxNotebook operations
333 // ----------------------------------------------------------------------------
335 // remove one page from the notebook, without deleting
336 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
338 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
342 TabCtrl_DeleteItem(m_hwnd
, nPage
);
344 if ( m_pages
.IsEmpty() )
346 // no selection any more, the notebook becamse empty
349 else // notebook still not empty
351 // change the selected page if it was deleted or became invalid
353 if ( m_nSelection
== GetPageCount() )
355 // last page deleted, make the new last page the new selection
356 selNew
= m_nSelection
- 1;
358 else if ( nPage
<= m_nSelection
)
360 // we must show another page, even if it has the same index
361 selNew
= m_nSelection
;
363 else // nothing changes for the currently selected page
367 // we still must refresh the current page: this needs to be done
368 // for some unknown reason if the tab control shows the up-down
369 // control (i.e. when there are too many pages) -- otherwise after
370 // deleting a page nothing at all is shown
371 m_pages
[m_nSelection
]->Refresh();
376 // m_nSelection must be always valid so reset it before calling
379 SetSelection(selNew
);
387 bool wxNotebook::DeleteAllPages()
389 int nPageCount
= GetPageCount();
391 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
392 delete m_pages
[nPage
];
396 TabCtrl_DeleteAllItems(m_hwnd
);
403 // same as AddPage() but does it at given position
404 bool wxNotebook::InsertPage(int nPage
,
405 wxNotebookPage
*pPage
,
406 const wxString
& strText
,
410 wxCHECK_MSG( pPage
!= NULL
, FALSE
, _T("NULL page in wxNotebook::InsertPage") );
411 wxCHECK_MSG( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
,
412 _T("invalid index in wxNotebook::InsertPage") );
414 wxASSERT_MSG( pPage
->GetParent() == this,
415 _T("notebook pages must have notebook as parent") );
417 // add a new tab to the control
418 // ----------------------------
420 // init all fields to 0
422 wxZeroMemory(tcItem
);
424 // set the image, if any
427 tcItem
.mask
|= TCIF_IMAGE
;
428 tcItem
.iImage
= imageId
;
432 if ( !strText
.IsEmpty() )
434 tcItem
.mask
|= TCIF_TEXT
;
435 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
438 // fit the notebook page to the tab control's display area: this should be
439 // done before adding it to the notebook or TabCtrl_InsertItem() will
440 // change the notebooks size itself!
441 AdjustPageSize(pPage
);
443 // finally do insert it
444 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 )
446 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
451 // succeeded: save the pointer to the page
452 m_pages
.Insert(pPage
, nPage
);
454 // for the first page (only) we need to adjust the size again because the
455 // notebook size changed: the tabs which hadn't been there before are now
457 if ( m_pages
.GetCount() == 1 )
459 AdjustPageSize(pPage
);
462 // hide the page: unless it is selected, it shouldn't be shown (and if it
463 // is selected it will be shown later)
464 HWND hwnd
= GetWinHwnd(pPage
);
465 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
467 // this updates internal flag too -- otherwise it would get out of sync
468 // with the real state
472 // now deal with the selection
473 // ---------------------------
475 // if the inserted page is before the selected one, we must update the
476 // index of the selected page
477 if ( nPage
<= m_nSelection
)
479 // one extra page added
483 // some page should be selected: either this one or the first one if there
484 // is still no selection
488 else if ( m_nSelection
== -1 )
492 SetSelection(selNew
);
497 // ----------------------------------------------------------------------------
498 // wxNotebook callbacks
499 // ----------------------------------------------------------------------------
501 void wxNotebook::OnSize(wxSizeEvent
& event
)
503 // fit the notebook page to the tab control's display area
505 rc
.left
= rc
.top
= 0;
506 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
508 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
510 int width
= rc
.right
- rc
.left
,
511 height
= rc
.bottom
- rc
.top
;
512 size_t nCount
= m_pages
.Count();
513 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
514 wxNotebookPage
*pPage
= m_pages
[nPage
];
515 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
521 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
523 // is it our tab control?
524 if ( event
.GetEventObject() == this )
526 int sel
= event
.GetOldSelection();
528 m_pages
[sel
]->Show(FALSE
);
530 sel
= event
.GetSelection();
533 wxNotebookPage
*pPage
= m_pages
[sel
];
541 // we want to give others a chance to process this message as well
545 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
547 // this function is only called when the focus is explicitly set (i.e. from
548 // the program) to the notebook - in this case we don't need the
549 // complicated OnNavigationKey() logic because the programmer knows better
552 // set focus to the currently selected page if any
553 if ( m_nSelection
!= -1 )
554 m_pages
[m_nSelection
]->SetFocus();
559 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
561 if ( event
.IsWindowChange() ) {
563 AdvanceSelection(event
.GetDirection());
566 // we get this event in 2 cases
568 // a) one of our pages might have generated it because the user TABbed
569 // out from it in which case we should propagate the event upwards and
570 // our parent will take care of setting the focus to prev/next sibling
574 // b) the parent panel wants to give the focus to us so that we
575 // forward it to our selected page. We can't deal with this in
576 // OnSetFocus() because we don't know which direction the focus came
577 // from in this case and so can't choose between setting the focus to
578 // first or last panel child
580 wxWindow
*parent
= GetParent();
581 if ( event
.GetEventObject() == parent
)
583 // no, it doesn't come from child, case (b): forward to a page
584 if ( m_nSelection
!= -1 )
586 // so that the page knows that the event comes from it's parent
587 // and is being propagated downwards
588 event
.SetEventObject(this);
590 wxWindow
*page
= m_pages
[m_nSelection
];
591 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
595 //else: page manages focus inside it itself
599 // we have no pages - still have to give focus to _something_
605 // it comes from our child, case (a), pass to the parent
607 event
.SetCurrentFocus(this);
608 parent
->GetEventHandler()->ProcessEvent(event
);
614 // ----------------------------------------------------------------------------
615 // wxNotebook base class virtuals
616 // ----------------------------------------------------------------------------
618 #if wxUSE_CONSTRAINTS
620 // override these 2 functions to do nothing: everything is done in OnSize
622 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
624 // don't set the sizes of the pages - their correct size is not yet known
625 wxControl::SetConstraintSizes(FALSE
);
628 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
633 #endif // wxUSE_CONSTRAINTS
635 // ----------------------------------------------------------------------------
636 // wxNotebook Windows message handlers
637 // ----------------------------------------------------------------------------
639 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
640 WXWORD pos
, WXHWND control
)
642 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
647 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
650 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
652 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
654 NMHDR
* hdr
= (NMHDR
*)lParam
;
655 switch ( hdr
->code
) {
657 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
660 case TCN_SELCHANGING
:
661 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
665 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
668 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
669 event
.SetOldSelection(m_nSelection
);
670 event
.SetEventObject(this);
671 event
.SetInt(idCtrl
);
673 bool processed
= GetEventHandler()->ProcessEvent(event
);
674 *result
= !event
.IsAllowed();
678 #endif // wxUSE_NOTEBOOK