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
) )
149 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
150 m_foregroundColour
= *wxBLACK
;
153 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
155 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
157 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
158 tabStyle
|= WS_CLIPSIBLINGS
;
159 if (m_windowStyle
& wxCLIP_CHILDREN
)
160 tabStyle
|= WS_CLIPCHILDREN
;
161 if ( m_windowStyle
& wxTC_MULTILINE
)
162 tabStyle
|= TCS_MULTILINE
;
163 if ( m_windowStyle
& wxBORDER
)
164 tabStyle
|= WS_BORDER
;
165 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
166 tabStyle
|= TCS_FIXEDWIDTH
;
167 if (m_windowStyle
& wxNB_BOTTOM
)
168 tabStyle
|= TCS_RIGHT
;
169 if (m_windowStyle
& wxNB_LEFT
)
170 tabStyle
|= TCS_VERTICAL
;
171 if (m_windowStyle
& wxNB_RIGHT
)
172 tabStyle
|= TCS_VERTICAL
|TCS_RIGHT
;
174 // note that we don't want to have the default WS_EX_CLIENTEDGE style for the
175 // notebook, so explicitly specify 0 as last parameter
176 if ( !MSWCreateControl(WC_TABCONTROL
, tabStyle
, pos
, size
, _T(""), 0) )
181 // Not all compilers recognise SetWindowFont
182 ::SendMessage(GetHwnd(), WM_SETFONT
,
183 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
), TRUE
);
186 if ( parent
!= NULL
)
187 parent
->AddChild(this);
192 // ----------------------------------------------------------------------------
193 // wxNotebook accessors
194 // ----------------------------------------------------------------------------
196 int wxNotebook::GetPageCount() const
199 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
201 return m_pages
.Count();
204 int wxNotebook::GetRowCount() const
206 return TabCtrl_GetRowCount(m_hwnd
);
209 int wxNotebook::SetSelection(int nPage
)
211 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
213 ChangePage(m_nSelection
, nPage
);
215 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
218 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
220 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
223 tcItem
.mask
= TCIF_TEXT
;
224 tcItem
.pszText
= (wxChar
*)strText
.c_str();
226 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
229 wxString
wxNotebook::GetPageText(int nPage
) const
231 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
235 tcItem
.mask
= TCIF_TEXT
;
236 tcItem
.pszText
= buf
;
237 tcItem
.cchTextMax
= WXSIZEOF(buf
);
240 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
241 str
= tcItem
.pszText
;
246 int wxNotebook::GetPageImage(int nPage
) const
248 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
251 tcItem
.mask
= TCIF_IMAGE
;
253 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
256 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
258 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
261 tcItem
.mask
= TCIF_IMAGE
;
262 tcItem
.iImage
= nImage
;
264 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
267 void wxNotebook::SetImageList(wxImageList
* imageList
)
269 wxNotebookBase::SetImageList(imageList
);
273 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
277 // ----------------------------------------------------------------------------
278 // wxNotebook size settings
279 // ----------------------------------------------------------------------------
281 void wxNotebook::SetPageSize(const wxSize
& size
)
283 // transform the page size into the notebook size
290 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
293 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
296 void wxNotebook::SetPadding(const wxSize
& padding
)
298 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
301 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
303 void wxNotebook::SetTabSize(const wxSize
& sz
)
305 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
308 // ----------------------------------------------------------------------------
309 // wxNotebook operations
310 // ----------------------------------------------------------------------------
312 // remove one page from the notebook
313 bool wxNotebook::DeletePage(int nPage
)
315 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
317 if ( m_nSelection
== nPage
) {
318 // advance selection backwards - the page being deleted shouldn't be left
320 AdvanceSelection(FALSE
);
323 TabCtrl_DeleteItem(m_hwnd
, nPage
);
325 delete m_pages
[nPage
];
326 m_pages
.RemoveAt(nPage
);
328 if ( m_pages
.IsEmpty() ) {
329 // no selection if the notebook became empty
333 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
339 // remove one page from the notebook, without deleting
340 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
342 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
346 TabCtrl_DeleteItem(m_hwnd
, nPage
);
348 if ( m_pages
.IsEmpty() )
351 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
357 bool wxNotebook::DeleteAllPages()
359 int nPageCount
= GetPageCount();
361 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
362 delete m_pages
[nPage
];
366 TabCtrl_DeleteAllItems(m_hwnd
);
373 // add a page to the notebook
374 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
375 const wxString
& strText
,
379 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
382 // same as AddPage() but does it at given position
383 bool wxNotebook::InsertPage(int nPage
,
384 wxNotebookPage
*pPage
,
385 const wxString
& strText
,
389 wxASSERT( pPage
!= NULL
);
390 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
392 // do add the tab to the control
394 // init all fields to 0
396 memset(&tcItem
, 0, sizeof(tcItem
));
400 tcItem
.mask
|= TCIF_IMAGE
;
401 tcItem
.iImage
= imageId
;
404 if ( !strText
.IsEmpty() )
406 tcItem
.mask
|= TCIF_TEXT
;
407 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
410 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
411 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
416 // if the inserted page is before the selected one, we must update the
417 // index of the selected page
418 if ( nPage
<= m_nSelection
)
420 // one extra page added
424 // save the pointer to the page
425 m_pages
.Insert(pPage
, nPage
);
427 // don't show pages by default (we'll need to adjust their size first)
428 HWND hwnd
= GetWinHwnd(pPage
);
429 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
431 // this updates internal flag too - otherwise it will get out of sync
434 // fit the notebook page to the tab control's display area
436 rc
.left
= rc
.top
= 0;
437 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
438 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
439 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
441 // some page should be selected: either this one or the first one if there is
442 // still no selection
446 else if ( m_nSelection
== -1 )
450 SetSelection(selNew
);
455 // ----------------------------------------------------------------------------
456 // wxNotebook callbacks
457 // ----------------------------------------------------------------------------
459 void wxNotebook::OnSize(wxSizeEvent
& event
)
461 // fit the notebook page to the tab control's display area
463 rc
.left
= rc
.top
= 0;
464 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
466 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
468 int width
= rc
.right
- rc
.left
,
469 height
= rc
.bottom
- rc
.top
;
470 size_t nCount
= m_pages
.Count();
471 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
472 wxNotebookPage
*pPage
= m_pages
[nPage
];
473 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
479 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
481 // is it our tab control?
482 if ( event
.GetEventObject() == this )
484 int sel
= event
.GetOldSelection();
486 m_pages
[sel
]->Show(FALSE
);
488 sel
= event
.GetSelection();
491 wxNotebookPage
*pPage
= m_pages
[sel
];
499 // we want to give others a chance to process this message as well
503 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
505 // this function is only called when the focus is explicitly set (i.e. from
506 // the program) to the notebook - in this case we don't need the
507 // complicated OnNavigationKey() logic because the programmer knows better
510 // set focus to the currently selected page if any
511 if ( m_nSelection
!= -1 )
512 m_pages
[m_nSelection
]->SetFocus();
517 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
519 if ( event
.IsWindowChange() ) {
521 AdvanceSelection(event
.GetDirection());
524 // we get this event in 2 cases
526 // a) one of our pages might have generated it because the user TABbed
527 // out from it in which case we should propagate the event upwards and
528 // our parent will take care of setting the focus to prev/next sibling
532 // b) the parent panel wants to give the focus to us so that we
533 // forward it to our selected page. We can't deal with this in
534 // OnSetFocus() because we don't know which direction the focus came
535 // from in this case and so can't choose between setting the focus to
536 // first or last panel child
538 wxWindow
*parent
= GetParent();
539 if ( event
.GetEventObject() == parent
)
541 // no, it doesn't come from child, case (b): forward to a page
542 if ( m_nSelection
!= -1 )
544 // so that the page knows that the event comes from it's parent
545 // and is being propagated downwards
546 event
.SetEventObject(this);
548 wxWindow
*page
= m_pages
[m_nSelection
];
549 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
553 //else: page manages focus inside it itself
557 // we have no pages - still have to give focus to _something_
563 // it comes from our child, case (a), pass to the parent
565 event
.SetCurrentFocus(this);
566 parent
->GetEventHandler()->ProcessEvent(event
);
572 // ----------------------------------------------------------------------------
573 // wxNotebook base class virtuals
574 // ----------------------------------------------------------------------------
576 // override these 2 functions to do nothing: everything is done in OnSize
578 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
580 // don't set the sizes of the pages - their correct size is not yet known
581 wxControl::SetConstraintSizes(FALSE
);
584 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
589 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
591 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
593 NMHDR
* hdr
= (NMHDR
*)lParam
;
594 switch ( hdr
->code
) {
596 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
599 case TCN_SELCHANGING
:
600 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
604 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
607 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
608 event
.SetOldSelection(m_nSelection
);
609 event
.SetEventObject(this);
610 event
.SetInt(idCtrl
);
612 bool processed
= GetEventHandler()->ProcessEvent(event
);
613 *result
= !event
.IsAllowed();
617 // ----------------------------------------------------------------------------
618 // wxNotebook helper functions
619 // ----------------------------------------------------------------------------
621 // generate the page changing and changed events, hide the currently active
622 // panel and show the new one
623 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
625 // MT-FIXME should use a real semaphore
626 static bool s_bInsideChangePage
= FALSE
;
628 // when we call ProcessEvent(), our own OnSelChange() is called which calls
629 // this function - break the infinite loop
630 if ( s_bInsideChangePage
)
633 // it's not an error (the message may be generated by the tab control itself)
634 // and it may happen - just do nothing
635 if ( nSel
== nOldSel
)
638 s_bInsideChangePage
= TRUE
;
640 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
641 event
.SetSelection(nSel
);
642 event
.SetOldSelection(nOldSel
);
643 event
.SetEventObject(this);
644 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
646 // program doesn't allow the page change
647 s_bInsideChangePage
= FALSE
;
651 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
652 GetEventHandler()->ProcessEvent(event
);
654 s_bInsideChangePage
= FALSE
;
657 #endif // wxUSE_NOTEBOOK