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 ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
148 parent
->AddChild(this);
151 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
153 long tabStyle
= WS_TABSTOP
| TCS_TABS
;
155 if ( m_windowStyle
& wxBORDER
)
156 tabStyle
|= WS_BORDER
;
157 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
158 tabStyle
|= WS_CLIPSIBLINGS
;
159 if (m_windowStyle
& wxCLIP_CHILDREN
)
160 tabStyle
|= WS_CLIPCHILDREN
;
162 if ( m_windowStyle
& wxTC_MULTILINE
)
163 tabStyle
|= TCS_MULTILINE
;
164 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
165 tabStyle
|= TCS_FIXEDWIDTH
;
166 if (m_windowStyle
& wxNB_BOTTOM
)
167 tabStyle
|= TCS_RIGHT
;
168 if (m_windowStyle
& wxNB_LEFT
)
169 tabStyle
|= TCS_VERTICAL
;
170 if (m_windowStyle
& wxNB_RIGHT
)
171 tabStyle
|= TCS_VERTICAL
|TCS_RIGHT
;
173 // note that we don't want to have the default WS_EX_CLIENTEDGE style for the
174 // notebook, so explicitly specify 0 as last parameter
175 if ( !MSWCreateControl(WC_TABCONTROL
, tabStyle
, pos
, size
, _T(""), 0) )
180 SetBackgroundColour(wxColour(::GetSysColor(COLOR_BTNFACE
)));
185 // ----------------------------------------------------------------------------
186 // wxNotebook accessors
187 // ----------------------------------------------------------------------------
189 int wxNotebook::GetPageCount() const
192 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
194 return m_pages
.Count();
197 int wxNotebook::GetRowCount() const
199 return TabCtrl_GetRowCount(m_hwnd
);
202 int wxNotebook::SetSelection(int nPage
)
204 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
206 ChangePage(m_nSelection
, nPage
);
208 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
211 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
213 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
216 tcItem
.mask
= TCIF_TEXT
;
217 tcItem
.pszText
= (wxChar
*)strText
.c_str();
219 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
222 wxString
wxNotebook::GetPageText(int nPage
) const
224 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
228 tcItem
.mask
= TCIF_TEXT
;
229 tcItem
.pszText
= buf
;
230 tcItem
.cchTextMax
= WXSIZEOF(buf
);
233 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
234 str
= tcItem
.pszText
;
239 int wxNotebook::GetPageImage(int nPage
) const
241 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
244 tcItem
.mask
= TCIF_IMAGE
;
246 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
249 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
251 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
254 tcItem
.mask
= TCIF_IMAGE
;
255 tcItem
.iImage
= nImage
;
257 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
260 void wxNotebook::SetImageList(wxImageList
* imageList
)
262 wxNotebookBase::SetImageList(imageList
);
266 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
270 // ----------------------------------------------------------------------------
271 // wxNotebook size settings
272 // ----------------------------------------------------------------------------
274 void wxNotebook::SetPageSize(const wxSize
& size
)
276 // transform the page size into the notebook size
283 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
286 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
289 void wxNotebook::SetPadding(const wxSize
& padding
)
291 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
294 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
296 void wxNotebook::SetTabSize(const wxSize
& sz
)
298 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
301 // ----------------------------------------------------------------------------
302 // wxNotebook operations
303 // ----------------------------------------------------------------------------
305 // remove one page from the notebook
306 bool wxNotebook::DeletePage(int nPage
)
308 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
310 if ( m_nSelection
== nPage
) {
311 // advance selection backwards - the page being deleted shouldn't be left
313 AdvanceSelection(FALSE
);
316 TabCtrl_DeleteItem(m_hwnd
, nPage
);
318 delete m_pages
[nPage
];
319 m_pages
.RemoveAt(nPage
);
321 if ( m_pages
.IsEmpty() ) {
322 // no selection if the notebook became empty
326 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
332 // remove one page from the notebook, without deleting
333 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
335 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
339 TabCtrl_DeleteItem(m_hwnd
, nPage
);
341 if ( m_pages
.IsEmpty() )
344 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
350 bool wxNotebook::DeleteAllPages()
352 int nPageCount
= GetPageCount();
354 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
355 delete m_pages
[nPage
];
359 TabCtrl_DeleteAllItems(m_hwnd
);
366 // add a page to the notebook
367 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
368 const wxString
& strText
,
372 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
375 // same as AddPage() but does it at given position
376 bool wxNotebook::InsertPage(int nPage
,
377 wxNotebookPage
*pPage
,
378 const wxString
& strText
,
382 wxASSERT( pPage
!= NULL
);
383 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
385 // do add the tab to the control
387 // init all fields to 0
389 memset(&tcItem
, 0, sizeof(tcItem
));
393 tcItem
.mask
|= TCIF_IMAGE
;
394 tcItem
.iImage
= imageId
;
397 if ( !strText
.IsEmpty() )
399 tcItem
.mask
|= TCIF_TEXT
;
400 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
403 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
404 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
409 // if the inserted page is before the selected one, we must update the
410 // index of the selected page
411 if ( nPage
<= m_nSelection
)
413 // one extra page added
417 // save the pointer to the page
418 m_pages
.Insert(pPage
, nPage
);
420 // don't show pages by default (we'll need to adjust their size first)
421 HWND hwnd
= GetWinHwnd(pPage
);
422 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
424 // this updates internal flag too - otherwise it will get out of sync
427 // fit the notebook page to the tab control's display area
429 rc
.left
= rc
.top
= 0;
430 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
431 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
432 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
434 // some page should be selected: either this one or the first one if there is
435 // still no selection
439 else if ( m_nSelection
== -1 )
443 SetSelection(selNew
);
448 // ----------------------------------------------------------------------------
449 // wxNotebook callbacks
450 // ----------------------------------------------------------------------------
452 void wxNotebook::OnSize(wxSizeEvent
& event
)
454 // fit the notebook page to the tab control's display area
456 rc
.left
= rc
.top
= 0;
457 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
459 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
461 int width
= rc
.right
- rc
.left
,
462 height
= rc
.bottom
- rc
.top
;
463 size_t nCount
= m_pages
.Count();
464 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
465 wxNotebookPage
*pPage
= m_pages
[nPage
];
466 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
472 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
474 // is it our tab control?
475 if ( event
.GetEventObject() == this )
477 int sel
= event
.GetOldSelection();
479 m_pages
[sel
]->Show(FALSE
);
481 sel
= event
.GetSelection();
484 wxNotebookPage
*pPage
= m_pages
[sel
];
492 // we want to give others a chance to process this message as well
496 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
498 // this function is only called when the focus is explicitly set (i.e. from
499 // the program) to the notebook - in this case we don't need the
500 // complicated OnNavigationKey() logic because the programmer knows better
503 // set focus to the currently selected page if any
504 if ( m_nSelection
!= -1 )
505 m_pages
[m_nSelection
]->SetFocus();
510 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
512 if ( event
.IsWindowChange() ) {
514 AdvanceSelection(event
.GetDirection());
517 // we get this event in 2 cases
519 // a) one of our pages might have generated it because the user TABbed
520 // out from it in which case we should propagate the event upwards and
521 // our parent will take care of setting the focus to prev/next sibling
525 // b) the parent panel wants to give the focus to us so that we
526 // forward it to our selected page. We can't deal with this in
527 // OnSetFocus() because we don't know which direction the focus came
528 // from in this case and so can't choose between setting the focus to
529 // first or last panel child
531 wxWindow
*parent
= GetParent();
532 if ( event
.GetEventObject() == parent
)
534 // no, it doesn't come from child, case (b): forward to a page
535 if ( m_nSelection
!= -1 )
537 // so that the page knows that the event comes from it's parent
538 // and is being propagated downwards
539 event
.SetEventObject(this);
541 wxWindow
*page
= m_pages
[m_nSelection
];
542 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
546 //else: page manages focus inside it itself
550 // we have no pages - still have to give focus to _something_
556 // it comes from our child, case (a), pass to the parent
558 event
.SetCurrentFocus(this);
559 parent
->GetEventHandler()->ProcessEvent(event
);
565 // ----------------------------------------------------------------------------
566 // wxNotebook base class virtuals
567 // ----------------------------------------------------------------------------
569 // override these 2 functions to do nothing: everything is done in OnSize
571 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
573 // don't set the sizes of the pages - their correct size is not yet known
574 wxControl::SetConstraintSizes(FALSE
);
577 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
582 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
584 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
586 NMHDR
* hdr
= (NMHDR
*)lParam
;
587 switch ( hdr
->code
) {
589 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
592 case TCN_SELCHANGING
:
593 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
597 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
600 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
601 event
.SetOldSelection(m_nSelection
);
602 event
.SetEventObject(this);
603 event
.SetInt(idCtrl
);
605 bool processed
= GetEventHandler()->ProcessEvent(event
);
606 *result
= !event
.IsAllowed();
610 // ----------------------------------------------------------------------------
611 // wxNotebook helper functions
612 // ----------------------------------------------------------------------------
614 // generate the page changing and changed events, hide the currently active
615 // panel and show the new one
616 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
618 // MT-FIXME should use a real semaphore
619 static bool s_bInsideChangePage
= FALSE
;
621 // when we call ProcessEvent(), our own OnSelChange() is called which calls
622 // this function - break the infinite loop
623 if ( s_bInsideChangePage
)
626 // it's not an error (the message may be generated by the tab control itself)
627 // and it may happen - just do nothing
628 if ( nSel
== nOldSel
)
631 s_bInsideChangePage
= TRUE
;
633 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
634 event
.SetSelection(nSel
);
635 event
.SetOldSelection(nOldSel
);
636 event
.SetEventObject(this);
637 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
639 // program doesn't allow the page change
640 s_bInsideChangePage
= FALSE
;
644 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
645 GetEventHandler()->ProcessEvent(event
);
647 s_bInsideChangePage
= FALSE
;
650 #endif // wxUSE_NOTEBOOK