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__)) && !defined(__CYGWIN10__))
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 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
87 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
88 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
90 EVT_SIZE(wxNotebook::OnSize
)
92 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
94 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
97 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
98 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
100 // ============================================================================
102 // ============================================================================
104 // ----------------------------------------------------------------------------
105 // wxNotebook construction
106 // ----------------------------------------------------------------------------
108 // common part of all ctors
109 void wxNotebook::Init()
112 m_bOwnsImageList
= FALSE
;
116 // default for dynamic class
117 wxNotebook::wxNotebook()
122 // the same arguments as for wxControl
123 wxNotebook::wxNotebook(wxWindow
*parent
,
128 const wxString
& name
)
132 Create(parent
, id
, pos
, size
, style
, name
);
136 bool wxNotebook::Create(wxWindow
*parent
,
141 const wxString
& name
)
144 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
148 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
149 m_foregroundColour
= *wxBLACK
;
152 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
154 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
156 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
157 tabStyle
|= WS_CLIPSIBLINGS
;
158 if (m_windowStyle
& wxCLIP_CHILDREN
)
159 tabStyle
|= WS_CLIPCHILDREN
;
160 if ( m_windowStyle
& wxTC_MULTILINE
)
161 tabStyle
|= TCS_MULTILINE
;
162 if ( m_windowStyle
& wxBORDER
)
163 tabStyle
&= WS_BORDER
;
164 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
165 tabStyle
|= TCS_FIXEDWIDTH
;
166 if (m_windowStyle
& wxNB_BOTTOM
)
167 tabStyle
|= TCS_RIGHT
;
168 if (m_windowStyle
& wxNB_LEFT
)
169 tabStyle
|= TCS_VERTICAL
;
170 if (m_windowStyle
& wxNB_RIGHT
)
171 tabStyle
|= TCS_VERTICAL
|TCS_RIGHT
;
174 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL
,
175 this, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
,
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);
195 wxNotebook::~wxNotebook()
197 if (m_bOwnsImageList
) delete m_pImageList
;
200 // ----------------------------------------------------------------------------
201 // wxNotebook accessors
202 // ----------------------------------------------------------------------------
203 int wxNotebook::GetPageCount() const
206 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
208 return m_aPages
.Count();
211 int wxNotebook::GetRowCount() const
213 return TabCtrl_GetRowCount(m_hwnd
);
216 int wxNotebook::SetSelection(int nPage
)
218 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
220 ChangePage(m_nSelection
, nPage
);
222 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
225 void wxNotebook::AdvanceSelection(bool bForward
)
227 int nSel
= GetSelection();
228 int nMax
= GetPageCount() - 1;
230 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
232 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
235 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
237 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
240 tcItem
.mask
= TCIF_TEXT
;
241 tcItem
.pszText
= (wxChar
*)strText
.c_str();
243 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
246 wxString
wxNotebook::GetPageText(int nPage
) const
248 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
252 tcItem
.mask
= TCIF_TEXT
;
253 tcItem
.pszText
= buf
;
254 tcItem
.cchTextMax
= WXSIZEOF(buf
);
257 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
258 str
= tcItem
.pszText
;
263 int wxNotebook::GetPageImage(int nPage
) const
265 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
268 tcItem
.mask
= TCIF_IMAGE
;
270 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
273 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
275 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
278 tcItem
.mask
= TCIF_IMAGE
;
279 tcItem
.iImage
= nImage
;
281 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
284 void wxNotebook::SetImageList(wxImageList
* imageList
)
286 if (m_bOwnsImageList
) delete m_pImageList
;
287 m_pImageList
= imageList
;
288 m_bOwnsImageList
= FALSE
;
289 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
292 void wxNotebook::AssignImageList(wxImageList
* imageList
)
294 SetImageList(imageList
);
295 m_bOwnsImageList
= TRUE
;
298 // ----------------------------------------------------------------------------
299 // wxNotebook size settings
300 // ----------------------------------------------------------------------------
302 void wxNotebook::SetPageSize(const wxSize
& size
)
304 // transform the page size into the notebook size
311 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
314 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
317 void wxNotebook::SetPadding(const wxSize
& padding
)
319 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
322 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
324 void wxNotebook::SetTabSize(const wxSize
& sz
)
326 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
329 // ----------------------------------------------------------------------------
330 // wxNotebook operations
331 // ----------------------------------------------------------------------------
333 // remove one page from the notebook
334 bool wxNotebook::DeletePage(int nPage
)
336 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
338 if ( m_nSelection
== nPage
) {
339 // advance selection backwards - the page being deleted shouldn't be left
341 AdvanceSelection(FALSE
);
344 TabCtrl_DeleteItem(m_hwnd
, nPage
);
346 delete m_aPages
[nPage
];
347 m_aPages
.Remove(nPage
);
349 if ( m_aPages
.IsEmpty() ) {
350 // no selection if the notebook became empty
354 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
360 // remove one page from the notebook, without deleting
361 bool wxNotebook::RemovePage(int nPage
)
363 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
365 TabCtrl_DeleteItem(m_hwnd
, nPage
);
367 m_aPages
.Remove(nPage
);
369 if ( m_aPages
.IsEmpty() )
372 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
378 bool wxNotebook::DeleteAllPages()
380 int nPageCount
= GetPageCount();
382 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
383 delete m_aPages
[nPage
];
387 TabCtrl_DeleteAllItems(m_hwnd
);
394 // add a page to the notebook
395 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
396 const wxString
& strText
,
400 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
403 // same as AddPage() but does it at given position
404 bool wxNotebook::InsertPage(int nPage
,
405 wxNotebookPage
*pPage
,
406 const wxString
& strText
,
410 wxASSERT( pPage
!= NULL
);
411 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
413 // do add the tab to the control
415 // init all fields to 0
417 memset(&tcItem
, 0, sizeof(tcItem
));
421 tcItem
.mask
|= TCIF_IMAGE
;
422 tcItem
.iImage
= imageId
;
425 if ( !strText
.IsEmpty() )
427 tcItem
.mask
|= TCIF_TEXT
;
428 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
431 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
432 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
437 // if the inserted page is before the selected one, we must update the
438 // index of the selected page
439 if ( nPage
<= m_nSelection
)
441 // one extra page added
445 // save the pointer to the page
446 m_aPages
.Insert(pPage
, nPage
);
448 // don't show pages by default (we'll need to adjust their size first)
449 HWND hwnd
= GetWinHwnd(pPage
);
450 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
452 // this updates internal flag too - otherwise it will get out of sync
455 // fit the notebook page to the tab control's display area
457 rc
.left
= rc
.top
= 0;
458 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
459 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
460 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
462 // some page should be selected: either this one or the first one if there is
463 // still no selection
467 else if ( m_nSelection
== -1 )
471 SetSelection(selNew
);
476 // ----------------------------------------------------------------------------
477 // wxNotebook callbacks
478 // ----------------------------------------------------------------------------
480 void wxNotebook::OnSize(wxSizeEvent
& event
)
482 // fit the notebook page to the tab control's display area
484 rc
.left
= rc
.top
= 0;
485 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
487 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
489 int width
= rc
.right
- rc
.left
,
490 height
= rc
.bottom
- rc
.top
;
491 size_t nCount
= m_aPages
.Count();
492 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
493 wxNotebookPage
*pPage
= m_aPages
[nPage
];
494 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
500 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
502 // is it our tab control?
503 if ( event
.GetEventObject() == this )
505 int sel
= event
.GetOldSelection();
507 m_aPages
[sel
]->Show(FALSE
);
509 sel
= event
.GetSelection();
512 wxNotebookPage
*pPage
= m_aPages
[sel
];
520 // we want to give others a chance to process this message as well
524 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
526 // this function is only called when the focus is explicitly set (i.e. from
527 // the program) to the notebook - in this case we don't need the
528 // complicated OnNavigationKey() logic because the programmer knows better
531 // set focus to the currently selected page if any
532 if ( m_nSelection
!= -1 )
533 m_aPages
[m_nSelection
]->SetFocus();
538 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
540 if ( event
.IsWindowChange() ) {
542 AdvanceSelection(event
.GetDirection());
545 // we get this event in 2 cases
547 // a) one of our pages might have generated it because the user TABbed
548 // out from it in which case we should propagate the event upwards and
549 // our parent will take care of setting the focus to prev/next sibling
553 // b) the parent panel wants to give the focus to us so that we
554 // forward it to our selected page. We can't deal with this in
555 // OnSetFocus() because we don't know which direction the focus came
556 // from in this case and so can't choose between setting the focus to
557 // first or last panel child
559 wxWindow
*parent
= GetParent();
560 if ( event
.GetEventObject() == parent
)
562 // no, it doesn't come from child, case (b): forward to a page
563 if ( m_nSelection
!= -1 )
565 // so that the page knows that the event comes from it's parent
566 // and is being propagated downwards
567 event
.SetEventObject(this);
569 wxWindow
*page
= m_aPages
[m_nSelection
];
570 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
574 //else: page manages focus inside it itself
578 // we have no pages - still have to give focus to _something_
584 // it comes from our child, case (a), pass to the parent
586 event
.SetCurrentFocus(this);
587 parent
->GetEventHandler()->ProcessEvent(event
);
593 // ----------------------------------------------------------------------------
594 // wxNotebook base class virtuals
595 // ----------------------------------------------------------------------------
597 // override these 2 functions to do nothing: everything is done in OnSize
599 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
601 // don't set the sizes of the pages - their correct size is not yet known
602 wxControl::SetConstraintSizes(FALSE
);
605 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
610 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
612 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
614 NMHDR
* hdr
= (NMHDR
*)lParam
;
615 switch ( hdr
->code
) {
617 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
620 case TCN_SELCHANGING
:
621 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
625 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
628 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
629 event
.SetOldSelection(m_nSelection
);
630 event
.SetEventObject(this);
631 event
.SetInt(idCtrl
);
633 bool processed
= GetEventHandler()->ProcessEvent(event
);
634 *result
= !event
.IsAllowed();
638 // ----------------------------------------------------------------------------
639 // wxNotebook helper functions
640 // ----------------------------------------------------------------------------
642 // generate the page changing and changed events, hide the currently active
643 // panel and show the new one
644 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
646 // MT-FIXME should use a real semaphore
647 static bool s_bInsideChangePage
= FALSE
;
649 // when we call ProcessEvent(), our own OnSelChange() is called which calls
650 // this function - break the infinite loop
651 if ( s_bInsideChangePage
)
654 // it's not an error (the message may be generated by the tab control itself)
655 // and it may happen - just do nothing
656 if ( nSel
== nOldSel
)
659 s_bInsideChangePage
= TRUE
;
661 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
662 event
.SetSelection(nSel
);
663 event
.SetOldSelection(nOldSel
);
664 event
.SetEventObject(this);
665 if ( ProcessEvent(event
) && !event
.IsAllowed() )
667 // program doesn't allow the page change
668 s_bInsideChangePage
= FALSE
;
672 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
675 s_bInsideChangePage
= FALSE
;