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
308 bool wxNotebook::DeletePage(int nPage
)
310 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
312 if ( m_nSelection
== nPage
) {
313 // advance selection backwards - the page being deleted shouldn't be left
315 AdvanceSelection(FALSE
);
318 TabCtrl_DeleteItem(m_hwnd
, nPage
);
320 delete m_pages
[nPage
];
321 m_pages
.RemoveAt(nPage
);
323 if ( m_pages
.IsEmpty() ) {
324 // no selection if the notebook became empty
328 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
334 // remove one page from the notebook, without deleting
335 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
337 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
341 TabCtrl_DeleteItem(m_hwnd
, nPage
);
343 if ( m_pages
.IsEmpty() )
346 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
352 bool wxNotebook::DeleteAllPages()
354 int nPageCount
= GetPageCount();
356 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
357 delete m_pages
[nPage
];
361 TabCtrl_DeleteAllItems(m_hwnd
);
368 // add a page to the notebook
369 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
370 const wxString
& strText
,
374 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
377 // same as AddPage() but does it at given position
378 bool wxNotebook::InsertPage(int nPage
,
379 wxNotebookPage
*pPage
,
380 const wxString
& strText
,
384 wxASSERT( pPage
!= NULL
);
385 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
387 // do add the tab to the control
389 // init all fields to 0
391 memset(&tcItem
, 0, sizeof(tcItem
));
395 tcItem
.mask
|= TCIF_IMAGE
;
396 tcItem
.iImage
= imageId
;
399 if ( !strText
.IsEmpty() )
401 tcItem
.mask
|= TCIF_TEXT
;
402 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
405 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
406 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
411 // if the inserted page is before the selected one, we must update the
412 // index of the selected page
413 if ( nPage
<= m_nSelection
)
415 // one extra page added
419 // save the pointer to the page
420 m_pages
.Insert(pPage
, nPage
);
422 // don't show pages by default (we'll need to adjust their size first)
423 HWND hwnd
= GetWinHwnd(pPage
);
424 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
426 // this updates internal flag too - otherwise it will get out of sync
429 // fit the notebook page to the tab control's display area
431 rc
.left
= rc
.top
= 0;
432 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
433 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
434 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
436 // some page should be selected: either this one or the first one if there is
437 // still no selection
441 else if ( m_nSelection
== -1 )
445 SetSelection(selNew
);
450 // ----------------------------------------------------------------------------
451 // wxNotebook callbacks
452 // ----------------------------------------------------------------------------
454 void wxNotebook::OnSize(wxSizeEvent
& event
)
456 // fit the notebook page to the tab control's display area
458 rc
.left
= rc
.top
= 0;
459 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
461 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
463 int width
= rc
.right
- rc
.left
,
464 height
= rc
.bottom
- rc
.top
;
465 size_t nCount
= m_pages
.Count();
466 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
467 wxNotebookPage
*pPage
= m_pages
[nPage
];
468 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
474 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
476 // is it our tab control?
477 if ( event
.GetEventObject() == this )
479 int sel
= event
.GetOldSelection();
481 m_pages
[sel
]->Show(FALSE
);
483 sel
= event
.GetSelection();
486 wxNotebookPage
*pPage
= m_pages
[sel
];
494 // we want to give others a chance to process this message as well
498 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
500 // this function is only called when the focus is explicitly set (i.e. from
501 // the program) to the notebook - in this case we don't need the
502 // complicated OnNavigationKey() logic because the programmer knows better
505 // set focus to the currently selected page if any
506 if ( m_nSelection
!= -1 )
507 m_pages
[m_nSelection
]->SetFocus();
512 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
514 if ( event
.IsWindowChange() ) {
516 AdvanceSelection(event
.GetDirection());
519 // we get this event in 2 cases
521 // a) one of our pages might have generated it because the user TABbed
522 // out from it in which case we should propagate the event upwards and
523 // our parent will take care of setting the focus to prev/next sibling
527 // b) the parent panel wants to give the focus to us so that we
528 // forward it to our selected page. We can't deal with this in
529 // OnSetFocus() because we don't know which direction the focus came
530 // from in this case and so can't choose between setting the focus to
531 // first or last panel child
533 wxWindow
*parent
= GetParent();
534 if ( event
.GetEventObject() == parent
)
536 // no, it doesn't come from child, case (b): forward to a page
537 if ( m_nSelection
!= -1 )
539 // so that the page knows that the event comes from it's parent
540 // and is being propagated downwards
541 event
.SetEventObject(this);
543 wxWindow
*page
= m_pages
[m_nSelection
];
544 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
548 //else: page manages focus inside it itself
552 // we have no pages - still have to give focus to _something_
558 // it comes from our child, case (a), pass to the parent
560 event
.SetCurrentFocus(this);
561 parent
->GetEventHandler()->ProcessEvent(event
);
567 // ----------------------------------------------------------------------------
568 // wxNotebook base class virtuals
569 // ----------------------------------------------------------------------------
571 // override these 2 functions to do nothing: everything is done in OnSize
573 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
575 // don't set the sizes of the pages - their correct size is not yet known
576 wxControl::SetConstraintSizes(FALSE
);
579 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
584 // ----------------------------------------------------------------------------
585 // wxNotebook Windows message handlers
586 // ----------------------------------------------------------------------------
588 bool wxNotebook::MSWOnScroll(int orientation
, WXWORD nSBCode
,
589 WXWORD pos
, WXHWND control
)
591 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
596 return wxNotebookBase::MSWOnScroll(orientation
, nSBCode
, pos
, control
);
599 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
601 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
603 NMHDR
* hdr
= (NMHDR
*)lParam
;
604 switch ( hdr
->code
) {
606 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
609 case TCN_SELCHANGING
:
610 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
614 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
617 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
618 event
.SetOldSelection(m_nSelection
);
619 event
.SetEventObject(this);
620 event
.SetInt(idCtrl
);
622 bool processed
= GetEventHandler()->ProcessEvent(event
);
623 *result
= !event
.IsAllowed();
627 // ----------------------------------------------------------------------------
628 // wxNotebook helper functions
629 // ----------------------------------------------------------------------------
631 // generate the page changing and changed events, hide the currently active
632 // panel and show the new one
633 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
635 // MT-FIXME should use a real semaphore
636 static bool s_bInsideChangePage
= FALSE
;
638 // when we call ProcessEvent(), our own OnSelChange() is called which calls
639 // this function - break the infinite loop
640 if ( s_bInsideChangePage
)
643 // it's not an error (the message may be generated by the tab control itself)
644 // and it may happen - just do nothing
645 if ( nSel
== nOldSel
)
648 s_bInsideChangePage
= TRUE
;
650 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
651 event
.SetSelection(nSel
);
652 event
.SetOldSelection(nOldSel
);
653 event
.SetEventObject(this);
654 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
656 // program doesn't allow the page change
657 s_bInsideChangePage
= FALSE
;
661 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
662 GetEventHandler()->ProcessEvent(event
);
664 s_bInsideChangePage
= FALSE
;
667 #endif // wxUSE_NOTEBOOK