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
& wxNB_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 if ( nPage
!= m_nSelection
)
210 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
211 event
.SetSelection(nPage
);
212 event
.SetOldSelection(m_nSelection
);
213 event
.SetEventObject(this);
214 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
216 // program allows the page change
217 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
218 (void)GetEventHandler()->ProcessEvent(event
);
220 TabCtrl_SetCurSel(m_hwnd
, nPage
);
227 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
229 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
232 tcItem
.mask
= TCIF_TEXT
;
233 tcItem
.pszText
= (wxChar
*)strText
.c_str();
235 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
238 wxString
wxNotebook::GetPageText(int nPage
) const
240 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
244 tcItem
.mask
= TCIF_TEXT
;
245 tcItem
.pszText
= buf
;
246 tcItem
.cchTextMax
= WXSIZEOF(buf
);
249 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
250 str
= tcItem
.pszText
;
255 int wxNotebook::GetPageImage(int nPage
) const
257 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
260 tcItem
.mask
= TCIF_IMAGE
;
262 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
265 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
267 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
270 tcItem
.mask
= TCIF_IMAGE
;
271 tcItem
.iImage
= nImage
;
273 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
276 void wxNotebook::SetImageList(wxImageList
* imageList
)
278 wxNotebookBase::SetImageList(imageList
);
282 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
286 // ----------------------------------------------------------------------------
287 // wxNotebook size settings
288 // ----------------------------------------------------------------------------
290 void wxNotebook::SetPageSize(const wxSize
& size
)
292 // transform the page size into the notebook size
299 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
302 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
305 void wxNotebook::SetPadding(const wxSize
& padding
)
307 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
310 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
312 void wxNotebook::SetTabSize(const wxSize
& sz
)
314 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
317 // ----------------------------------------------------------------------------
318 // wxNotebook operations
319 // ----------------------------------------------------------------------------
321 // remove one page from the notebook, without deleting
322 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
324 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
328 TabCtrl_DeleteItem(m_hwnd
, nPage
);
330 if ( m_pages
.IsEmpty() )
332 // no selection any more, the notebook becamse empty
335 else // notebook still not empty
337 // change the selected page if it was deleted or became invalid
339 if ( m_nSelection
== GetPageCount() )
341 // last page deleted, make the new last page the new selection
342 selNew
= m_nSelection
- 1;
344 else if ( nPage
<= m_nSelection
)
346 // we must show another page, even if it has the same index
347 selNew
= m_nSelection
;
349 else // nothing changes for the currently selected page
353 // we still must refresh the current page: this needs to be done
354 // for some unknown reason if the tab control shows the up-down
355 // control (i.e. when there are too many pages) -- otherwise after
356 // deleting a page nothing at all is shown
357 m_pages
[m_nSelection
]->Refresh();
362 // m_nSelection must be always valid so reset it before calling
365 SetSelection(selNew
);
373 bool wxNotebook::DeleteAllPages()
375 int nPageCount
= GetPageCount();
377 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
378 delete m_pages
[nPage
];
382 TabCtrl_DeleteAllItems(m_hwnd
);
389 // add a page to the notebook
390 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
391 const wxString
& strText
,
395 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
398 // same as AddPage() but does it at given position
399 bool wxNotebook::InsertPage(int nPage
,
400 wxNotebookPage
*pPage
,
401 const wxString
& strText
,
405 wxCHECK_MSG( pPage
!= NULL
, FALSE
, _T("NULL page in wxNotebook::InsertPage") );
406 wxCHECK_MSG( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
,
407 _T("invalid index in wxNotebook::InsertPage") );
410 // add a new tab to the control
411 // ----------------------------
413 // init all fields to 0
415 wxZeroMemory(tcItem
);
417 // set the image, if any
420 tcItem
.mask
|= TCIF_IMAGE
;
421 tcItem
.iImage
= imageId
;
425 if ( !strText
.IsEmpty() )
427 tcItem
.mask
|= TCIF_TEXT
;
428 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
431 // fit the notebook page to the tab control's display area: this should be
432 // done before adding it to the notebook or TabCtrl_InsertItem() will
433 // change the notebooks size itself!
435 rc
.left
= rc
.top
= 0;
436 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
437 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
438 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
441 // finally do insert it
442 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
443 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
448 // succeeded: save the pointer to the page
449 m_pages
.Insert(pPage
, nPage
);
451 // hide the page: unless it is selected, it shouldn't be shown (and if it
452 // is selected it will be shown later)
453 HWND hwnd
= GetWinHwnd(pPage
);
454 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
456 // this updates internal flag too -- otherwise it would get out of sync
457 // with the real state
461 // now deal with the selection
462 // ---------------------------
464 // if the inserted page is before the selected one, we must update the
465 // index of the selected page
466 if ( nPage
<= m_nSelection
)
468 // one extra page added
472 // some page should be selected: either this one or the first one if there
473 // is still no selection
477 else if ( m_nSelection
== -1 )
481 SetSelection(selNew
);
486 // ----------------------------------------------------------------------------
487 // wxNotebook callbacks
488 // ----------------------------------------------------------------------------
490 void wxNotebook::OnSize(wxSizeEvent
& event
)
492 // fit the notebook page to the tab control's display area
494 rc
.left
= rc
.top
= 0;
495 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
497 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
499 int width
= rc
.right
- rc
.left
,
500 height
= rc
.bottom
- rc
.top
;
501 size_t nCount
= m_pages
.Count();
502 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
503 wxNotebookPage
*pPage
= m_pages
[nPage
];
504 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
510 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
512 // is it our tab control?
513 if ( event
.GetEventObject() == this )
515 int sel
= event
.GetOldSelection();
517 m_pages
[sel
]->Show(FALSE
);
519 sel
= event
.GetSelection();
522 wxNotebookPage
*pPage
= m_pages
[sel
];
530 // we want to give others a chance to process this message as well
534 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
536 // this function is only called when the focus is explicitly set (i.e. from
537 // the program) to the notebook - in this case we don't need the
538 // complicated OnNavigationKey() logic because the programmer knows better
541 // set focus to the currently selected page if any
542 if ( m_nSelection
!= -1 )
543 m_pages
[m_nSelection
]->SetFocus();
548 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
550 if ( event
.IsWindowChange() ) {
552 AdvanceSelection(event
.GetDirection());
555 // we get this event in 2 cases
557 // a) one of our pages might have generated it because the user TABbed
558 // out from it in which case we should propagate the event upwards and
559 // our parent will take care of setting the focus to prev/next sibling
563 // b) the parent panel wants to give the focus to us so that we
564 // forward it to our selected page. We can't deal with this in
565 // OnSetFocus() because we don't know which direction the focus came
566 // from in this case and so can't choose between setting the focus to
567 // first or last panel child
569 wxWindow
*parent
= GetParent();
570 if ( event
.GetEventObject() == parent
)
572 // no, it doesn't come from child, case (b): forward to a page
573 if ( m_nSelection
!= -1 )
575 // so that the page knows that the event comes from it's parent
576 // and is being propagated downwards
577 event
.SetEventObject(this);
579 wxWindow
*page
= m_pages
[m_nSelection
];
580 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
584 //else: page manages focus inside it itself
588 // we have no pages - still have to give focus to _something_
594 // it comes from our child, case (a), pass to the parent
596 event
.SetCurrentFocus(this);
597 parent
->GetEventHandler()->ProcessEvent(event
);
603 // ----------------------------------------------------------------------------
604 // wxNotebook base class virtuals
605 // ----------------------------------------------------------------------------
607 // override these 2 functions to do nothing: everything is done in OnSize
609 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
611 // don't set the sizes of the pages - their correct size is not yet known
612 wxControl::SetConstraintSizes(FALSE
);
615 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
620 // ----------------------------------------------------------------------------
621 // wxNotebook Windows message handlers
622 // ----------------------------------------------------------------------------
624 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
625 WXWORD pos
, WXHWND control
)
627 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
632 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
635 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
637 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
639 NMHDR
* hdr
= (NMHDR
*)lParam
;
640 switch ( hdr
->code
) {
642 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
645 case TCN_SELCHANGING
:
646 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
650 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
653 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
654 event
.SetOldSelection(m_nSelection
);
655 event
.SetEventObject(this);
656 event
.SetInt(idCtrl
);
658 bool processed
= GetEventHandler()->ProcessEvent(event
);
659 *result
= !event
.IsAllowed();
663 #endif // wxUSE_NOTEBOOK