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"
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
, wxDefaultValidator
, name
) )
148 // notebook, so explicitly specify 0 as last parameter
149 if ( !MSWCreateControl(WC_TABCONTROL
, _T(""), pos
, size
,
150 style
| wxTAB_TRAVERSAL
) )
153 SetBackgroundColour(wxColour(::GetSysColor(COLOR_BTNFACE
)));
158 WXDWORD
wxNotebook::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
160 WXDWORD tabStyle
= wxControl::MSWGetStyle(style
, exstyle
);
162 tabStyle
|= WS_TABSTOP
| TCS_TABS
;
164 if ( style
& wxTC_MULTILINE
)
165 tabStyle
|= TCS_MULTILINE
;
166 if ( style
& wxNB_FIXEDWIDTH
)
167 tabStyle
|= TCS_FIXEDWIDTH
;
169 if ( style
& wxNB_BOTTOM
)
170 tabStyle
|= TCS_RIGHT
;
171 else if ( style
& wxNB_LEFT
)
172 tabStyle
|= TCS_VERTICAL
;
173 else if ( style
& wxNB_RIGHT
)
174 tabStyle
|= TCS_VERTICAL
| TCS_RIGHT
;
179 // note that we never want to have the default WS_EX_CLIENTEDGE style
180 // as it looks too ugly for the notebooks
187 // ----------------------------------------------------------------------------
188 // wxNotebook accessors
189 // ----------------------------------------------------------------------------
191 int wxNotebook::GetPageCount() const
194 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
196 return m_pages
.Count();
199 int wxNotebook::GetRowCount() const
201 return TabCtrl_GetRowCount(m_hwnd
);
204 int wxNotebook::SetSelection(int nPage
)
206 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
208 ChangePage(m_nSelection
, nPage
);
210 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
213 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
215 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
218 tcItem
.mask
= TCIF_TEXT
;
219 tcItem
.pszText
= (wxChar
*)strText
.c_str();
221 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
224 wxString
wxNotebook::GetPageText(int nPage
) const
226 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
230 tcItem
.mask
= TCIF_TEXT
;
231 tcItem
.pszText
= buf
;
232 tcItem
.cchTextMax
= WXSIZEOF(buf
);
235 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
236 str
= tcItem
.pszText
;
241 int wxNotebook::GetPageImage(int nPage
) const
243 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
246 tcItem
.mask
= TCIF_IMAGE
;
248 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
251 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
253 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
256 tcItem
.mask
= TCIF_IMAGE
;
257 tcItem
.iImage
= nImage
;
259 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
262 void wxNotebook::SetImageList(wxImageList
* imageList
)
264 wxNotebookBase::SetImageList(imageList
);
268 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
272 // ----------------------------------------------------------------------------
273 // wxNotebook size settings
274 // ----------------------------------------------------------------------------
276 void wxNotebook::SetPageSize(const wxSize
& size
)
278 // transform the page size into the notebook size
285 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
288 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
291 void wxNotebook::SetPadding(const wxSize
& padding
)
293 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
296 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
298 void wxNotebook::SetTabSize(const wxSize
& sz
)
300 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
303 // ----------------------------------------------------------------------------
304 // wxNotebook operations
305 // ----------------------------------------------------------------------------
307 // remove one page from the notebook, without deleting
308 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
310 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
314 TabCtrl_DeleteItem(m_hwnd
, nPage
);
316 if ( m_pages
.IsEmpty() )
318 // no selection any more, the notebook becamse empty
321 else // notebook still not empty
323 // change the selected page if it was deleted or became invalid
325 if ( m_nSelection
== GetPageCount() )
327 // last page deleted, make the new last page the new selection
328 selNew
= m_nSelection
- 1;
330 else if ( nPage
<= m_nSelection
)
332 // we must show another page, even if it has the same index
333 selNew
= m_nSelection
;
335 else // nothing changes for the currently selected page
339 // we still must refresh the current page: this needs to be done
340 // for some unknown reason if the tab control shows the up-down
341 // control (i.e. when there are too many pages) -- otherwise after
342 // deleting a page nothing at all is shown
343 m_pages
[m_nSelection
]->Refresh();
348 // m_nSelection must be always valid so reset it before calling
351 SetSelection(selNew
);
359 bool wxNotebook::DeleteAllPages()
361 int nPageCount
= GetPageCount();
363 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
364 delete m_pages
[nPage
];
368 TabCtrl_DeleteAllItems(m_hwnd
);
375 // add a page to the notebook
376 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
377 const wxString
& strText
,
381 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
384 // same as AddPage() but does it at given position
385 bool wxNotebook::InsertPage(int nPage
,
386 wxNotebookPage
*pPage
,
387 const wxString
& strText
,
391 wxASSERT( pPage
!= NULL
);
392 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
394 // do add the tab to the control
396 // init all fields to 0
398 memset(&tcItem
, 0, sizeof(tcItem
));
402 tcItem
.mask
|= TCIF_IMAGE
;
403 tcItem
.iImage
= imageId
;
406 if ( !strText
.IsEmpty() )
408 tcItem
.mask
|= TCIF_TEXT
;
409 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
412 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
413 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
418 // if the inserted page is before the selected one, we must update the
419 // index of the selected page
420 if ( nPage
<= m_nSelection
)
422 // one extra page added
426 // save the pointer to the page
427 m_pages
.Insert(pPage
, nPage
);
429 // don't show pages by default (we'll need to adjust their size first)
430 HWND hwnd
= GetWinHwnd(pPage
);
431 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
433 // this updates internal flag too - otherwise it will get out of sync
436 // fit the notebook page to the tab control's display area
438 rc
.left
= rc
.top
= 0;
439 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
440 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
441 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
443 // some page should be selected: either this one or the first one if there is
444 // still no selection
448 else if ( m_nSelection
== -1 )
452 SetSelection(selNew
);
457 // ----------------------------------------------------------------------------
458 // wxNotebook callbacks
459 // ----------------------------------------------------------------------------
461 void wxNotebook::OnSize(wxSizeEvent
& event
)
463 // fit the notebook page to the tab control's display area
465 rc
.left
= rc
.top
= 0;
466 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
468 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
470 int width
= rc
.right
- rc
.left
,
471 height
= rc
.bottom
- rc
.top
;
472 size_t nCount
= m_pages
.Count();
473 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
474 wxNotebookPage
*pPage
= m_pages
[nPage
];
475 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
481 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
483 // is it our tab control?
484 if ( event
.GetEventObject() == this )
486 int sel
= event
.GetOldSelection();
488 m_pages
[sel
]->Show(FALSE
);
490 sel
= event
.GetSelection();
493 wxNotebookPage
*pPage
= m_pages
[sel
];
501 // we want to give others a chance to process this message as well
505 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
507 // this function is only called when the focus is explicitly set (i.e. from
508 // the program) to the notebook - in this case we don't need the
509 // complicated OnNavigationKey() logic because the programmer knows better
512 // set focus to the currently selected page if any
513 if ( m_nSelection
!= -1 )
514 m_pages
[m_nSelection
]->SetFocus();
519 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
521 if ( event
.IsWindowChange() ) {
523 AdvanceSelection(event
.GetDirection());
526 // we get this event in 2 cases
528 // a) one of our pages might have generated it because the user TABbed
529 // out from it in which case we should propagate the event upwards and
530 // our parent will take care of setting the focus to prev/next sibling
534 // b) the parent panel wants to give the focus to us so that we
535 // forward it to our selected page. We can't deal with this in
536 // OnSetFocus() because we don't know which direction the focus came
537 // from in this case and so can't choose between setting the focus to
538 // first or last panel child
540 wxWindow
*parent
= GetParent();
541 if ( event
.GetEventObject() == parent
)
543 // no, it doesn't come from child, case (b): forward to a page
544 if ( m_nSelection
!= -1 )
546 // so that the page knows that the event comes from it's parent
547 // and is being propagated downwards
548 event
.SetEventObject(this);
550 wxWindow
*page
= m_pages
[m_nSelection
];
551 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
555 //else: page manages focus inside it itself
559 // we have no pages - still have to give focus to _something_
565 // it comes from our child, case (a), pass to the parent
567 event
.SetCurrentFocus(this);
568 parent
->GetEventHandler()->ProcessEvent(event
);
574 // ----------------------------------------------------------------------------
575 // wxNotebook base class virtuals
576 // ----------------------------------------------------------------------------
578 // override these 2 functions to do nothing: everything is done in OnSize
580 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
582 // don't set the sizes of the pages - their correct size is not yet known
583 wxControl::SetConstraintSizes(FALSE
);
586 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
591 // ----------------------------------------------------------------------------
592 // wxNotebook Windows message handlers
593 // ----------------------------------------------------------------------------
595 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
596 WXWORD pos
, WXHWND control
)
598 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
603 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
606 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
608 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
610 NMHDR
* hdr
= (NMHDR
*)lParam
;
611 switch ( hdr
->code
) {
613 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
616 case TCN_SELCHANGING
:
617 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
621 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
624 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
625 event
.SetOldSelection(m_nSelection
);
626 event
.SetEventObject(this);
627 event
.SetInt(idCtrl
);
629 bool processed
= GetEventHandler()->ProcessEvent(event
);
630 *result
= !event
.IsAllowed();
634 // ----------------------------------------------------------------------------
635 // wxNotebook helper functions
636 // ----------------------------------------------------------------------------
638 // generate the page changing and changed events, hide the currently active
639 // panel and show the new one
640 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
642 // MT-FIXME should use a real semaphore
643 static bool s_bInsideChangePage
= FALSE
;
645 // when we call ProcessEvent(), our own OnSelChange() is called which calls
646 // this function - break the infinite loop
647 if ( s_bInsideChangePage
)
650 // it's not an error (the message may be generated by the tab control itself)
651 // and it may happen - just do nothing
652 if ( nSel
== nOldSel
)
655 s_bInsideChangePage
= TRUE
;
657 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
658 event
.SetSelection(nSel
);
659 event
.SetOldSelection(nOldSel
);
660 event
.SetEventObject(this);
661 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
663 // program doesn't allow the page change
664 s_bInsideChangePage
= FALSE
;
668 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
669 GetEventHandler()->ProcessEvent(event
);
671 s_bInsideChangePage
= FALSE
;
674 #endif // wxUSE_NOTEBOOK