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
45 #ifdef __GNUWIN32_OLD__
46 #include "wx/msw/gnuwin32/extra.h"
49 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // check that the page index is valid
58 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
61 #define m_hwnd (HWND)GetHWND()
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // This is a work-around for missing defines in gcc-2.95 headers
69 #define TCS_RIGHT 0x0002
73 #define TCS_VERTICAL 0x0080
77 #define TCS_BOTTOM TCS_RIGHT
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
87 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
88 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
90 EVT_SIZE(wxNotebook::OnSize
)
92 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
94 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
97 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
98 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
100 // ============================================================================
102 // ============================================================================
104 // ----------------------------------------------------------------------------
105 // wxNotebook construction
106 // ----------------------------------------------------------------------------
108 // common part of all ctors
109 void wxNotebook::Init()
115 // default for dynamic class
116 wxNotebook::wxNotebook()
121 // the same arguments as for wxControl
122 wxNotebook::wxNotebook(wxWindow
*parent
,
127 const wxString
& name
)
131 Create(parent
, id
, pos
, size
, style
, name
);
135 bool wxNotebook::Create(wxWindow
*parent
,
140 const wxString
& name
)
143 if ( !CreateControl(parent
, id
, pos
, size
, style
| wxTAB_TRAVERSAL
,
144 wxDefaultValidator
, name
) )
147 if ( !MSWCreateControl(WC_TABCONTROL
, _T(""), pos
, size
) )
150 SetBackgroundColour(wxColour(::GetSysColor(COLOR_BTNFACE
)));
155 WXDWORD
wxNotebook::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
157 WXDWORD tabStyle
= wxControl::MSWGetStyle(style
, exstyle
);
159 tabStyle
|= WS_TABSTOP
| TCS_TABS
;
161 if ( style
& wxNB_MULTILINE
)
162 tabStyle
|= TCS_MULTILINE
;
163 if ( style
& wxNB_FIXEDWIDTH
)
164 tabStyle
|= TCS_FIXEDWIDTH
;
166 if ( style
& wxNB_BOTTOM
)
167 tabStyle
|= TCS_RIGHT
;
168 else if ( style
& wxNB_LEFT
)
169 tabStyle
|= TCS_VERTICAL
;
170 else if ( style
& wxNB_RIGHT
)
171 tabStyle
|= TCS_VERTICAL
| TCS_RIGHT
;
176 // note that we never want to have the default WS_EX_CLIENTEDGE style
177 // as it looks too ugly for the notebooks
184 // ----------------------------------------------------------------------------
185 // wxNotebook accessors
186 // ----------------------------------------------------------------------------
188 int wxNotebook::GetPageCount() const
191 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
193 return m_pages
.Count();
196 int wxNotebook::GetRowCount() const
198 return TabCtrl_GetRowCount(m_hwnd
);
201 int wxNotebook::SetSelection(int nPage
)
203 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
205 if ( nPage
!= m_nSelection
)
207 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
208 event
.SetSelection(nPage
);
209 event
.SetOldSelection(m_nSelection
);
210 event
.SetEventObject(this);
211 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
213 // program allows the page change
214 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
215 (void)GetEventHandler()->ProcessEvent(event
);
217 TabCtrl_SetCurSel(m_hwnd
, nPage
);
224 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
226 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
229 tcItem
.mask
= TCIF_TEXT
;
230 tcItem
.pszText
= (wxChar
*)strText
.c_str();
232 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
235 wxString
wxNotebook::GetPageText(int nPage
) const
237 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
241 tcItem
.mask
= TCIF_TEXT
;
242 tcItem
.pszText
= buf
;
243 tcItem
.cchTextMax
= WXSIZEOF(buf
);
246 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
247 str
= tcItem
.pszText
;
252 int wxNotebook::GetPageImage(int nPage
) const
254 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
257 tcItem
.mask
= TCIF_IMAGE
;
259 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
262 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
264 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
267 tcItem
.mask
= TCIF_IMAGE
;
268 tcItem
.iImage
= nImage
;
270 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
273 void wxNotebook::SetImageList(wxImageList
* imageList
)
275 wxNotebookBase::SetImageList(imageList
);
279 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
283 // ----------------------------------------------------------------------------
284 // wxNotebook size settings
285 // ----------------------------------------------------------------------------
287 void wxNotebook::SetPageSize(const wxSize
& size
)
289 // transform the page size into the notebook size
296 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
299 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
302 void wxNotebook::SetPadding(const wxSize
& padding
)
304 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
307 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
309 void wxNotebook::SetTabSize(const wxSize
& sz
)
311 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
314 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
316 wxSize sizeTotal
= sizePage
;
318 // We need to make getting tab size part of the wxWindows API.
319 wxSize
tabSize(0, 0);
320 if (GetPageCount() > 0)
323 TabCtrl_GetItemRect((HWND
) GetHWND(), 0, & rect
);
324 tabSize
.x
= rect
.right
- rect
.left
;
325 tabSize
.y
= rect
.bottom
- rect
.top
;
327 if ( HasFlag(wxNB_LEFT
) || HasFlag(wxNB_RIGHT
) )
329 sizeTotal
.x
+= tabSize
.x
+ 7;
335 sizeTotal
.y
+= tabSize
.y
+ 7;
341 void wxNotebook::AdjustPageSize(wxNotebookPage
*page
)
343 wxCHECK_RET( page
, _T("NULL page in wxNotebook::AdjustPageSize") );
349 // get the page size from the notebook size
350 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
351 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
353 page
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
356 // ----------------------------------------------------------------------------
357 // wxNotebook operations
358 // ----------------------------------------------------------------------------
360 // remove one page from the notebook, without deleting
361 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
363 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
367 TabCtrl_DeleteItem(m_hwnd
, nPage
);
369 if ( m_pages
.IsEmpty() )
371 // no selection any more, the notebook becamse empty
374 else // notebook still not empty
376 // change the selected page if it was deleted or became invalid
378 if ( m_nSelection
== GetPageCount() )
380 // last page deleted, make the new last page the new selection
381 selNew
= m_nSelection
- 1;
383 else if ( nPage
<= m_nSelection
)
385 // we must show another page, even if it has the same index
386 selNew
= m_nSelection
;
388 else // nothing changes for the currently selected page
392 // we still must refresh the current page: this needs to be done
393 // for some unknown reason if the tab control shows the up-down
394 // control (i.e. when there are too many pages) -- otherwise after
395 // deleting a page nothing at all is shown
396 m_pages
[m_nSelection
]->Refresh();
401 // m_nSelection must be always valid so reset it before calling
404 SetSelection(selNew
);
412 bool wxNotebook::DeleteAllPages()
414 int nPageCount
= GetPageCount();
416 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
417 delete m_pages
[nPage
];
421 TabCtrl_DeleteAllItems(m_hwnd
);
428 // same as AddPage() but does it at given position
429 bool wxNotebook::InsertPage(int nPage
,
430 wxNotebookPage
*pPage
,
431 const wxString
& strText
,
435 wxCHECK_MSG( pPage
!= NULL
, FALSE
, _T("NULL page in wxNotebook::InsertPage") );
436 wxCHECK_MSG( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
,
437 _T("invalid index in wxNotebook::InsertPage") );
439 wxASSERT_MSG( pPage
->GetParent() == this,
440 _T("notebook pages must have notebook as parent") );
442 // add a new tab to the control
443 // ----------------------------
445 // init all fields to 0
447 wxZeroMemory(tcItem
);
449 // set the image, if any
452 tcItem
.mask
|= TCIF_IMAGE
;
453 tcItem
.iImage
= imageId
;
457 if ( !strText
.IsEmpty() )
459 tcItem
.mask
|= TCIF_TEXT
;
460 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
463 // fit the notebook page to the tab control's display area: this should be
464 // done before adding it to the notebook or TabCtrl_InsertItem() will
465 // change the notebooks size itself!
466 AdjustPageSize(pPage
);
468 // finally do insert it
469 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 )
471 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
476 // succeeded: save the pointer to the page
477 m_pages
.Insert(pPage
, nPage
);
479 // for the first page (only) we need to adjust the size again because the
480 // notebook size changed: the tabs which hadn't been there before are now
482 if ( m_pages
.GetCount() == 1 )
484 AdjustPageSize(pPage
);
487 // hide the page: unless it is selected, it shouldn't be shown (and if it
488 // is selected it will be shown later)
489 HWND hwnd
= GetWinHwnd(pPage
);
490 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
492 // this updates internal flag too -- otherwise it would get out of sync
493 // with the real state
497 // now deal with the selection
498 // ---------------------------
500 // if the inserted page is before the selected one, we must update the
501 // index of the selected page
502 if ( nPage
<= m_nSelection
)
504 // one extra page added
508 // some page should be selected: either this one or the first one if there
509 // is still no selection
513 else if ( m_nSelection
== -1 )
517 SetSelection(selNew
);
522 // ----------------------------------------------------------------------------
523 // wxNotebook callbacks
524 // ----------------------------------------------------------------------------
526 void wxNotebook::OnSize(wxSizeEvent
& event
)
528 // fit the notebook page to the tab control's display area
530 rc
.left
= rc
.top
= 0;
531 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
533 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
535 int width
= rc
.right
- rc
.left
,
536 height
= rc
.bottom
- rc
.top
;
537 size_t nCount
= m_pages
.Count();
538 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
539 wxNotebookPage
*pPage
= m_pages
[nPage
];
540 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
546 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
548 // is it our tab control?
549 if ( event
.GetEventObject() == this )
551 int sel
= event
.GetOldSelection();
553 m_pages
[sel
]->Show(FALSE
);
555 sel
= event
.GetSelection();
558 wxNotebookPage
*pPage
= m_pages
[sel
];
566 // we want to give others a chance to process this message as well
570 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
572 // this function is only called when the focus is explicitly set (i.e. from
573 // the program) to the notebook - in this case we don't need the
574 // complicated OnNavigationKey() logic because the programmer knows better
577 // set focus to the currently selected page if any
578 if ( m_nSelection
!= -1 )
579 m_pages
[m_nSelection
]->SetFocus();
584 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
586 if ( event
.IsWindowChange() ) {
588 AdvanceSelection(event
.GetDirection());
591 // we get this event in 2 cases
593 // a) one of our pages might have generated it because the user TABbed
594 // out from it in which case we should propagate the event upwards and
595 // our parent will take care of setting the focus to prev/next sibling
599 // b) the parent panel wants to give the focus to us so that we
600 // forward it to our selected page. We can't deal with this in
601 // OnSetFocus() because we don't know which direction the focus came
602 // from in this case and so can't choose between setting the focus to
603 // first or last panel child
605 wxWindow
*parent
= GetParent();
606 if ( event
.GetEventObject() == parent
)
608 // no, it doesn't come from child, case (b): forward to a page
609 if ( m_nSelection
!= -1 )
611 // so that the page knows that the event comes from it's parent
612 // and is being propagated downwards
613 event
.SetEventObject(this);
615 wxWindow
*page
= m_pages
[m_nSelection
];
616 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
620 //else: page manages focus inside it itself
624 // we have no pages - still have to give focus to _something_
630 // it comes from our child, case (a), pass to the parent
632 event
.SetCurrentFocus(this);
633 parent
->GetEventHandler()->ProcessEvent(event
);
639 // ----------------------------------------------------------------------------
640 // wxNotebook base class virtuals
641 // ----------------------------------------------------------------------------
643 #if wxUSE_CONSTRAINTS
645 // override these 2 functions to do nothing: everything is done in OnSize
647 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
649 // don't set the sizes of the pages - their correct size is not yet known
650 wxControl::SetConstraintSizes(FALSE
);
653 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
658 #endif // wxUSE_CONSTRAINTS
660 // ----------------------------------------------------------------------------
661 // wxNotebook Windows message handlers
662 // ----------------------------------------------------------------------------
664 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
665 WXWORD pos
, WXHWND control
)
667 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
672 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
675 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
677 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
679 NMHDR
* hdr
= (NMHDR
*)lParam
;
680 switch ( hdr
->code
) {
682 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
685 case TCN_SELCHANGING
:
686 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
690 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
693 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
694 event
.SetOldSelection(m_nSelection
);
695 event
.SetEventObject(this);
696 event
.SetInt(idCtrl
);
698 bool processed
= GetEventHandler()->ProcessEvent(event
);
699 *result
= !event
.IsAllowed();
703 #endif // wxUSE_NOTEBOOK