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
;
175 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL
,
176 this, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
,
182 // Not all compilers recognise SetWindowFont
183 ::SendMessage(GetHwnd(), WM_SETFONT
,
184 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
), TRUE
);
187 if ( parent
!= NULL
)
188 parent
->AddChild(this);
195 // ----------------------------------------------------------------------------
196 // wxNotebook accessors
197 // ----------------------------------------------------------------------------
199 int wxNotebook::GetPageCount() const
202 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
204 return m_pages
.Count();
207 int wxNotebook::GetRowCount() const
209 return TabCtrl_GetRowCount(m_hwnd
);
212 int wxNotebook::SetSelection(int nPage
)
214 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
216 ChangePage(m_nSelection
, nPage
);
218 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
221 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
223 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
226 tcItem
.mask
= TCIF_TEXT
;
227 tcItem
.pszText
= (wxChar
*)strText
.c_str();
229 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
232 wxString
wxNotebook::GetPageText(int nPage
) const
234 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
238 tcItem
.mask
= TCIF_TEXT
;
239 tcItem
.pszText
= buf
;
240 tcItem
.cchTextMax
= WXSIZEOF(buf
);
243 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
244 str
= tcItem
.pszText
;
249 int wxNotebook::GetPageImage(int nPage
) const
251 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
254 tcItem
.mask
= TCIF_IMAGE
;
256 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
259 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
261 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
264 tcItem
.mask
= TCIF_IMAGE
;
265 tcItem
.iImage
= nImage
;
267 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
270 void wxNotebook::SetImageList(wxImageList
* imageList
)
272 wxNotebookBase::SetImageList(imageList
);
276 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
280 // ----------------------------------------------------------------------------
281 // wxNotebook size settings
282 // ----------------------------------------------------------------------------
284 void wxNotebook::SetPageSize(const wxSize
& size
)
286 // transform the page size into the notebook size
293 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
296 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
299 void wxNotebook::SetPadding(const wxSize
& padding
)
301 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
304 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
306 void wxNotebook::SetTabSize(const wxSize
& sz
)
308 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
311 // ----------------------------------------------------------------------------
312 // wxNotebook operations
313 // ----------------------------------------------------------------------------
315 // remove one page from the notebook
316 bool wxNotebook::DeletePage(int nPage
)
318 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
320 if ( m_nSelection
== nPage
) {
321 // advance selection backwards - the page being deleted shouldn't be left
323 AdvanceSelection(FALSE
);
326 TabCtrl_DeleteItem(m_hwnd
, nPage
);
328 delete m_pages
[nPage
];
329 m_pages
.Remove(nPage
);
331 if ( m_pages
.IsEmpty() ) {
332 // no selection if the notebook became empty
336 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
342 // remove one page from the notebook, without deleting
343 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
345 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
349 TabCtrl_DeleteItem(m_hwnd
, nPage
);
351 if ( m_pages
.IsEmpty() )
354 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
360 bool wxNotebook::DeleteAllPages()
362 int nPageCount
= GetPageCount();
364 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
365 delete m_pages
[nPage
];
369 TabCtrl_DeleteAllItems(m_hwnd
);
376 // add a page to the notebook
377 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
378 const wxString
& strText
,
382 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
385 // same as AddPage() but does it at given position
386 bool wxNotebook::InsertPage(int nPage
,
387 wxNotebookPage
*pPage
,
388 const wxString
& strText
,
392 wxASSERT( pPage
!= NULL
);
393 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
395 // do add the tab to the control
397 // init all fields to 0
399 memset(&tcItem
, 0, sizeof(tcItem
));
403 tcItem
.mask
|= TCIF_IMAGE
;
404 tcItem
.iImage
= imageId
;
407 if ( !strText
.IsEmpty() )
409 tcItem
.mask
|= TCIF_TEXT
;
410 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
413 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
414 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
419 // if the inserted page is before the selected one, we must update the
420 // index of the selected page
421 if ( nPage
<= m_nSelection
)
423 // one extra page added
427 // save the pointer to the page
428 m_pages
.Insert(pPage
, nPage
);
430 // don't show pages by default (we'll need to adjust their size first)
431 HWND hwnd
= GetWinHwnd(pPage
);
432 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
434 // this updates internal flag too - otherwise it will get out of sync
437 // fit the notebook page to the tab control's display area
439 rc
.left
= rc
.top
= 0;
440 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
441 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
442 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
444 // some page should be selected: either this one or the first one if there is
445 // still no selection
449 else if ( m_nSelection
== -1 )
453 SetSelection(selNew
);
458 // ----------------------------------------------------------------------------
459 // wxNotebook callbacks
460 // ----------------------------------------------------------------------------
462 void wxNotebook::OnSize(wxSizeEvent
& event
)
464 // fit the notebook page to the tab control's display area
466 rc
.left
= rc
.top
= 0;
467 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
469 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
471 int width
= rc
.right
- rc
.left
,
472 height
= rc
.bottom
- rc
.top
;
473 size_t nCount
= m_pages
.Count();
474 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
475 wxNotebookPage
*pPage
= m_pages
[nPage
];
476 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
482 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
484 // is it our tab control?
485 if ( event
.GetEventObject() == this )
487 int sel
= event
.GetOldSelection();
489 m_pages
[sel
]->Show(FALSE
);
491 sel
= event
.GetSelection();
494 wxNotebookPage
*pPage
= m_pages
[sel
];
502 // we want to give others a chance to process this message as well
506 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
508 // this function is only called when the focus is explicitly set (i.e. from
509 // the program) to the notebook - in this case we don't need the
510 // complicated OnNavigationKey() logic because the programmer knows better
513 // set focus to the currently selected page if any
514 if ( m_nSelection
!= -1 )
515 m_pages
[m_nSelection
]->SetFocus();
520 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
522 if ( event
.IsWindowChange() ) {
524 AdvanceSelection(event
.GetDirection());
527 // we get this event in 2 cases
529 // a) one of our pages might have generated it because the user TABbed
530 // out from it in which case we should propagate the event upwards and
531 // our parent will take care of setting the focus to prev/next sibling
535 // b) the parent panel wants to give the focus to us so that we
536 // forward it to our selected page. We can't deal with this in
537 // OnSetFocus() because we don't know which direction the focus came
538 // from in this case and so can't choose between setting the focus to
539 // first or last panel child
541 wxWindow
*parent
= GetParent();
542 if ( event
.GetEventObject() == parent
)
544 // no, it doesn't come from child, case (b): forward to a page
545 if ( m_nSelection
!= -1 )
547 // so that the page knows that the event comes from it's parent
548 // and is being propagated downwards
549 event
.SetEventObject(this);
551 wxWindow
*page
= m_pages
[m_nSelection
];
552 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
556 //else: page manages focus inside it itself
560 // we have no pages - still have to give focus to _something_
566 // it comes from our child, case (a), pass to the parent
568 event
.SetCurrentFocus(this);
569 parent
->GetEventHandler()->ProcessEvent(event
);
575 // ----------------------------------------------------------------------------
576 // wxNotebook base class virtuals
577 // ----------------------------------------------------------------------------
579 // override these 2 functions to do nothing: everything is done in OnSize
581 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
583 // don't set the sizes of the pages - their correct size is not yet known
584 wxControl::SetConstraintSizes(FALSE
);
587 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
592 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
594 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
596 NMHDR
* hdr
= (NMHDR
*)lParam
;
597 switch ( hdr
->code
) {
599 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
602 case TCN_SELCHANGING
:
603 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
607 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
610 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
611 event
.SetOldSelection(m_nSelection
);
612 event
.SetEventObject(this);
613 event
.SetInt(idCtrl
);
615 bool processed
= GetEventHandler()->ProcessEvent(event
);
616 *result
= !event
.IsAllowed();
620 // ----------------------------------------------------------------------------
621 // wxNotebook helper functions
622 // ----------------------------------------------------------------------------
624 // generate the page changing and changed events, hide the currently active
625 // panel and show the new one
626 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
628 // MT-FIXME should use a real semaphore
629 static bool s_bInsideChangePage
= FALSE
;
631 // when we call ProcessEvent(), our own OnSelChange() is called which calls
632 // this function - break the infinite loop
633 if ( s_bInsideChangePage
)
636 // it's not an error (the message may be generated by the tab control itself)
637 // and it may happen - just do nothing
638 if ( nSel
== nOldSel
)
641 s_bInsideChangePage
= TRUE
;
643 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
644 event
.SetSelection(nSel
);
645 event
.SetOldSelection(nOldSel
);
646 event
.SetEventObject(this);
647 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
649 // program doesn't allow the page change
650 s_bInsideChangePage
= FALSE
;
654 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
655 GetEventHandler()->ProcessEvent(event
);
657 s_bInsideChangePage
= FALSE
;
660 #endif // wxUSE_NOTEBOOK