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"
25 #include "wx/string.h"
29 #include "wx/imaglist.h"
31 #include "wx/control.h"
32 #include "wx/notebook.h"
34 #include "wx/msw/private.h"
36 // Windows standard headers
38 #error "wxNotebook is only supported Windows 95 and above"
41 #include <windowsx.h> // for SetWindowFont
44 #ifdef __GNUWIN32_OLD__
45 #include "wx/msw/gnuwin32/extra.h"
49 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // check that the page index is valid
58 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
61 #define m_hwnd (HWND)GetHWND()
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // This is a work-around for missing defines in gcc-2.95 headers
69 #define TCS_RIGHT 0x0002
73 #define TCS_VERTICAL 0x0080
77 #define TCS_BOTTOM TCS_RIGHT
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
85 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
87 EVT_SIZE(wxNotebook::OnSize
)
89 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
91 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
94 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
95 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
97 // ============================================================================
99 // ============================================================================
101 // ----------------------------------------------------------------------------
102 // wxNotebook construction
103 // ----------------------------------------------------------------------------
105 // common part of all ctors
106 void wxNotebook::Init()
112 // default for dynamic class
113 wxNotebook::wxNotebook()
118 // the same arguments as for wxControl
119 wxNotebook::wxNotebook(wxWindow
*parent
,
124 const wxString
& name
)
128 Create(parent
, id
, pos
, size
, style
, name
);
132 bool wxNotebook::Create(wxWindow
*parent
,
137 const wxString
& name
)
140 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
144 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
145 m_foregroundColour
= *wxBLACK
;
148 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
150 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
152 if (m_windowStyle
& wxCLIP_CHILDREN
)
153 tabStyle
|= WS_CLIPCHILDREN
;
154 if ( m_windowStyle
& wxTC_MULTILINE
)
155 tabStyle
|= TCS_MULTILINE
;
156 if ( m_windowStyle
& wxBORDER
)
157 tabStyle
&= WS_BORDER
;
158 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
159 tabStyle
|= TCS_FIXEDWIDTH
;
160 if (m_windowStyle
& wxNB_BOTTOM
)
161 tabStyle
|= TCS_RIGHT
;
162 if (m_windowStyle
& wxNB_LEFT
)
163 tabStyle
|= TCS_VERTICAL
;
164 if (m_windowStyle
& wxNB_RIGHT
)
165 tabStyle
|= TCS_VERTICAL
|TCS_RIGHT
;
168 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL
,
169 this, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
,
175 // Not all compilers recognise SetWindowFont
176 ::SendMessage(GetHwnd(), WM_SETFONT
,
177 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
), TRUE
);
180 if ( parent
!= NULL
)
181 parent
->AddChild(this);
189 wxNotebook::~wxNotebook()
193 // ----------------------------------------------------------------------------
194 // wxNotebook accessors
195 // ----------------------------------------------------------------------------
196 int wxNotebook::GetPageCount() const
199 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
201 return m_aPages
.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 void wxNotebook::AdvanceSelection(bool bForward
)
220 int nSel
= GetSelection();
221 int nMax
= GetPageCount() - 1;
223 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
225 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
228 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
230 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
233 tcItem
.mask
= TCIF_TEXT
;
234 tcItem
.pszText
= (wxChar
*)strText
.c_str();
236 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
239 wxString
wxNotebook::GetPageText(int nPage
) const
241 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
245 tcItem
.mask
= TCIF_TEXT
;
246 tcItem
.pszText
= buf
;
247 tcItem
.cchTextMax
= WXSIZEOF(buf
);
250 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
251 str
= tcItem
.pszText
;
256 int wxNotebook::GetPageImage(int nPage
) const
258 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
261 tcItem
.mask
= TCIF_IMAGE
;
263 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
266 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
268 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
271 tcItem
.mask
= TCIF_IMAGE
;
272 tcItem
.iImage
= nImage
;
274 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
277 void wxNotebook::SetImageList(wxImageList
* imageList
)
279 m_pImageList
= imageList
;
280 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
283 // ----------------------------------------------------------------------------
284 // wxNotebook size settings
285 // ----------------------------------------------------------------------------
287 void wxNotebook::SetPageSize(const wxSize
& size
)
289 // transform the page size into the notebook size
296 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
299 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
302 void wxNotebook::SetPadding(const wxSize
& padding
)
304 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
307 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
309 void wxNotebook::SetTabSize(const wxSize
& sz
)
311 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
314 // ----------------------------------------------------------------------------
315 // wxNotebook operations
316 // ----------------------------------------------------------------------------
318 // remove one page from the notebook
319 bool wxNotebook::DeletePage(int nPage
)
321 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
323 if ( m_nSelection
== nPage
) {
324 // advance selection backwards - the page being deleted shouldn't be left
326 AdvanceSelection(FALSE
);
329 TabCtrl_DeleteItem(m_hwnd
, nPage
);
331 delete m_aPages
[nPage
];
332 m_aPages
.Remove(nPage
);
334 if ( m_aPages
.IsEmpty() ) {
335 // no selection if the notebook became empty
339 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
345 // remove one page from the notebook, without deleting
346 bool wxNotebook::RemovePage(int nPage
)
348 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
350 TabCtrl_DeleteItem(m_hwnd
, nPage
);
352 m_aPages
.Remove(nPage
);
354 if ( m_aPages
.IsEmpty() )
357 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
363 bool wxNotebook::DeleteAllPages()
365 int nPageCount
= GetPageCount();
367 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
368 delete m_aPages
[nPage
];
372 TabCtrl_DeleteAllItems(m_hwnd
);
379 // add a page to the notebook
380 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
381 const wxString
& strText
,
385 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
388 // same as AddPage() but does it at given position
389 bool wxNotebook::InsertPage(int nPage
,
390 wxNotebookPage
*pPage
,
391 const wxString
& strText
,
395 wxASSERT( pPage
!= NULL
);
396 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
398 // do add the tab to the control
400 // init all fields to 0
402 memset(&tcItem
, 0, sizeof(tcItem
));
406 tcItem
.mask
|= TCIF_IMAGE
;
407 tcItem
.iImage
= imageId
;
410 if ( !strText
.IsEmpty() )
412 tcItem
.mask
|= TCIF_TEXT
;
413 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
416 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
417 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
422 // if the inserted page is before the selected one, we must update the
423 // index of the selected page
424 if ( nPage
<= m_nSelection
)
426 // one extra page added
430 // save the pointer to the page
431 m_aPages
.Insert(pPage
, nPage
);
433 // don't show pages by default (we'll need to adjust their size first)
434 HWND hwnd
= GetWinHwnd(pPage
);
435 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
437 // this updates internal flag too - otherwise it will get out of sync
440 // fit the notebook page to the tab control's display area
442 rc
.left
= rc
.top
= 0;
443 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
444 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
445 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
447 // some page should be selected: either this one or the first one if there is
448 // still no selection
452 else if ( m_nSelection
== -1 )
456 SetSelection(selNew
);
461 // ----------------------------------------------------------------------------
462 // wxNotebook callbacks
463 // ----------------------------------------------------------------------------
465 void wxNotebook::OnSize(wxSizeEvent
& event
)
467 // fit the notebook page to the tab control's display area
469 rc
.left
= rc
.top
= 0;
470 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
472 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
474 int width
= rc
.right
- rc
.left
,
475 height
= rc
.bottom
- rc
.top
;
476 size_t nCount
= m_aPages
.Count();
477 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
478 wxNotebookPage
*pPage
= m_aPages
[nPage
];
479 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
485 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
487 // is it our tab control?
488 if ( event
.GetEventObject() == this )
490 int sel
= event
.GetOldSelection();
492 m_aPages
[sel
]->Show(FALSE
);
494 sel
= event
.GetSelection();
497 wxNotebookPage
*pPage
= m_aPages
[sel
];
505 // we want to give others a chance to process this message as well
509 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
511 // this function is only called when the focus is explicitly set (i.e. from
512 // the program) to the notebook - in this case we don't need the
513 // complicated OnNavigationKey() logic because the programmer knows better
516 // set focus to the currently selected page if any
517 if ( m_nSelection
!= -1 )
518 m_aPages
[m_nSelection
]->SetFocus();
523 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
525 if ( event
.IsWindowChange() ) {
527 AdvanceSelection(event
.GetDirection());
530 // we get this event in 2 cases
532 // a) one of our pages might have generated it because the user TABbed
533 // out from it in which case we should propagate the event upwards and
534 // our parent will take care of setting the focus to prev/next sibling
538 // b) the parent panel wants to give the focus to us so that we
539 // forward it to our selected page. We can't deal with this in
540 // OnSetFocus() because we don't know which direction the focus came
541 // from in this case and so can't choose between setting the focus to
542 // first or last panel child
544 wxWindow
*parent
= GetParent();
545 if ( event
.GetEventObject() == parent
)
547 // no, it doesn't come from child, case (b): forward to a page
548 if ( m_nSelection
!= -1 )
550 // so that the page knows that the event comes from it's parent
551 // and is being propagated downwards
552 event
.SetEventObject(this);
554 wxWindow
*page
= m_aPages
[m_nSelection
];
555 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
559 //else: page manages focus inside it itself
563 // we have no pages - still have to give focus to _something_
569 // it comes from our child, case (a), pass to the parent
571 event
.SetCurrentFocus(this);
572 parent
->GetEventHandler()->ProcessEvent(event
);
578 // ----------------------------------------------------------------------------
579 // wxNotebook base class virtuals
580 // ----------------------------------------------------------------------------
582 // override these 2 functions to do nothing: everything is done in OnSize
584 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
586 // don't set the sizes of the pages - their correct size is not yet known
587 wxControl::SetConstraintSizes(FALSE
);
590 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
595 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
597 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
599 NMHDR
* hdr
= (NMHDR
*)lParam
;
600 switch ( hdr
->code
) {
602 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
605 case TCN_SELCHANGING
:
606 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
610 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
613 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
614 event
.SetOldSelection(m_nSelection
);
615 event
.SetEventObject(this);
616 event
.SetInt(idCtrl
);
618 bool processed
= GetEventHandler()->ProcessEvent(event
);
619 *result
= !event
.IsAllowed();
623 // ----------------------------------------------------------------------------
624 // wxNotebook helper functions
625 // ----------------------------------------------------------------------------
627 // generate the page changing and changed events, hide the currently active
628 // panel and show the new one
629 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
631 // MT-FIXME should use a real semaphore
632 static bool s_bInsideChangePage
= FALSE
;
634 // when we call ProcessEvent(), our own OnSelChange() is called which calls
635 // this function - break the infinite loop
636 if ( s_bInsideChangePage
)
639 // it's not an error (the message may be generated by the tab control itself)
640 // and it may happen - just do nothing
641 if ( nSel
== nOldSel
)
644 s_bInsideChangePage
= TRUE
;
646 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
647 event
.SetSelection(nSel
);
648 event
.SetOldSelection(nOldSel
);
649 event
.SetEventObject(this);
650 if ( ProcessEvent(event
) && !event
.IsAllowed() )
652 // program doesn't allow the page change
653 s_bInsideChangePage
= FALSE
;
657 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
660 s_bInsideChangePage
= FALSE
;