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
45 #ifndef wxUSE_NORLANDER_HEADERS
46 #include "wx/msw/gnuwin32/extra.h"
51 #if !defined(__GNUWIN32__) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
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 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
87 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
89 EVT_SIZE(wxNotebook::OnSize
)
91 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
93 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
96 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
97 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
99 // ============================================================================
101 // ============================================================================
103 // ----------------------------------------------------------------------------
104 // wxNotebook construction
105 // ----------------------------------------------------------------------------
107 // common part of all ctors
108 void wxNotebook::Init()
114 // default for dynamic class
115 wxNotebook::wxNotebook()
120 // the same arguments as for wxControl
121 wxNotebook::wxNotebook(wxWindow
*parent
,
126 const wxString
& name
)
130 Create(parent
, id
, pos
, size
, style
, name
);
134 bool wxNotebook::Create(wxWindow
*parent
,
139 const wxString
& name
)
142 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
146 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
147 m_foregroundColour
= *wxBLACK
;
150 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
152 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
154 if (m_windowStyle
& wxCLIP_CHILDREN
)
155 tabStyle
|= WS_CLIPCHILDREN
;
156 if ( m_windowStyle
& wxTC_MULTILINE
)
157 tabStyle
|= TCS_MULTILINE
;
158 if ( m_windowStyle
& wxBORDER
)
159 tabStyle
&= WS_BORDER
;
160 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
161 tabStyle
|= TCS_FIXEDWIDTH
;
162 if (m_windowStyle
& wxNB_BOTTOM
)
163 tabStyle
|= TCS_RIGHT
;
164 if (m_windowStyle
& wxNB_LEFT
)
165 tabStyle
|= TCS_VERTICAL
;
166 if (m_windowStyle
& wxNB_RIGHT
)
167 tabStyle
|= TCS_VERTICAL
|TCS_RIGHT
;
170 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL
,
171 this, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
,
177 // Not all compilers recognise SetWindowFont
178 ::SendMessage(GetHwnd(), WM_SETFONT
,
179 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
), TRUE
);
182 if ( parent
!= NULL
)
183 parent
->AddChild(this);
191 wxNotebook::~wxNotebook()
195 // ----------------------------------------------------------------------------
196 // wxNotebook accessors
197 // ----------------------------------------------------------------------------
198 int wxNotebook::GetPageCount() const
201 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
203 return m_aPages
.Count();
206 int wxNotebook::GetRowCount() const
208 return TabCtrl_GetRowCount(m_hwnd
);
211 int wxNotebook::SetSelection(int nPage
)
213 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
215 ChangePage(m_nSelection
, nPage
);
217 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
220 void wxNotebook::AdvanceSelection(bool bForward
)
222 int nSel
= GetSelection();
223 int nMax
= GetPageCount() - 1;
225 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
227 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
230 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
232 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
235 tcItem
.mask
= TCIF_TEXT
;
236 tcItem
.pszText
= (wxChar
*)strText
.c_str();
238 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
241 wxString
wxNotebook::GetPageText(int nPage
) const
243 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
247 tcItem
.mask
= TCIF_TEXT
;
248 tcItem
.pszText
= buf
;
249 tcItem
.cchTextMax
= WXSIZEOF(buf
);
252 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
253 str
= tcItem
.pszText
;
258 int wxNotebook::GetPageImage(int nPage
) const
260 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
263 tcItem
.mask
= TCIF_IMAGE
;
265 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
268 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
270 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
273 tcItem
.mask
= TCIF_IMAGE
;
274 tcItem
.iImage
= nImage
;
276 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
279 void wxNotebook::SetImageList(wxImageList
* imageList
)
281 m_pImageList
= imageList
;
282 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
285 // ----------------------------------------------------------------------------
286 // wxNotebook size settings
287 // ----------------------------------------------------------------------------
289 void wxNotebook::SetPageSize(const wxSize
& size
)
291 // transform the page size into the notebook size
298 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
301 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
304 void wxNotebook::SetPadding(const wxSize
& padding
)
306 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
309 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
311 void wxNotebook::SetTabSize(const wxSize
& sz
)
313 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
316 // ----------------------------------------------------------------------------
317 // wxNotebook operations
318 // ----------------------------------------------------------------------------
320 // remove one page from the notebook
321 bool wxNotebook::DeletePage(int nPage
)
323 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
325 if ( m_nSelection
== nPage
) {
326 // advance selection backwards - the page being deleted shouldn't be left
328 AdvanceSelection(FALSE
);
331 TabCtrl_DeleteItem(m_hwnd
, nPage
);
333 delete m_aPages
[nPage
];
334 m_aPages
.Remove(nPage
);
336 if ( m_aPages
.IsEmpty() ) {
337 // no selection if the notebook became empty
341 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
347 // remove one page from the notebook, without deleting
348 bool wxNotebook::RemovePage(int nPage
)
350 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
352 TabCtrl_DeleteItem(m_hwnd
, nPage
);
354 m_aPages
.Remove(nPage
);
356 if ( m_aPages
.IsEmpty() )
359 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
365 bool wxNotebook::DeleteAllPages()
367 int nPageCount
= GetPageCount();
369 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
370 delete m_aPages
[nPage
];
374 TabCtrl_DeleteAllItems(m_hwnd
);
381 // add a page to the notebook
382 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
383 const wxString
& strText
,
387 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
390 // same as AddPage() but does it at given position
391 bool wxNotebook::InsertPage(int nPage
,
392 wxNotebookPage
*pPage
,
393 const wxString
& strText
,
397 wxASSERT( pPage
!= NULL
);
398 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
400 // do add the tab to the control
402 // init all fields to 0
404 memset(&tcItem
, 0, sizeof(tcItem
));
408 tcItem
.mask
|= TCIF_IMAGE
;
409 tcItem
.iImage
= imageId
;
412 if ( !strText
.IsEmpty() )
414 tcItem
.mask
|= TCIF_TEXT
;
415 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
418 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
419 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
424 // if the inserted page is before the selected one, we must update the
425 // index of the selected page
426 if ( nPage
<= m_nSelection
)
428 // one extra page added
432 // save the pointer to the page
433 m_aPages
.Insert(pPage
, nPage
);
435 // don't show pages by default (we'll need to adjust their size first)
436 HWND hwnd
= GetWinHwnd(pPage
);
437 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
439 // this updates internal flag too - otherwise it will get out of sync
442 // fit the notebook page to the tab control's display area
444 rc
.left
= rc
.top
= 0;
445 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
446 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
447 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
449 // some page should be selected: either this one or the first one if there is
450 // still no selection
454 else if ( m_nSelection
== -1 )
458 SetSelection(selNew
);
463 // ----------------------------------------------------------------------------
464 // wxNotebook callbacks
465 // ----------------------------------------------------------------------------
467 void wxNotebook::OnSize(wxSizeEvent
& event
)
469 // fit the notebook page to the tab control's display area
471 rc
.left
= rc
.top
= 0;
472 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
474 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
476 int width
= rc
.right
- rc
.left
,
477 height
= rc
.bottom
- rc
.top
;
478 size_t nCount
= m_aPages
.Count();
479 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
480 wxNotebookPage
*pPage
= m_aPages
[nPage
];
481 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
487 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
489 // is it our tab control?
490 if ( event
.GetEventObject() == this )
492 int sel
= event
.GetOldSelection();
494 m_aPages
[sel
]->Show(FALSE
);
496 sel
= event
.GetSelection();
499 wxNotebookPage
*pPage
= m_aPages
[sel
];
507 // we want to give others a chance to process this message as well
511 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
513 // this function is only called when the focus is explicitly set (i.e. from
514 // the program) to the notebook - in this case we don't need the
515 // complicated OnNavigationKey() logic because the programmer knows better
518 // set focus to the currently selected page if any
519 if ( m_nSelection
!= -1 )
520 m_aPages
[m_nSelection
]->SetFocus();
525 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
527 if ( event
.IsWindowChange() ) {
529 AdvanceSelection(event
.GetDirection());
532 // we get this event in 2 cases
534 // a) one of our pages might have generated it because the user TABbed
535 // out from it in which case we should propagate the event upwards and
536 // our parent will take care of setting the focus to prev/next sibling
540 // b) the parent panel wants to give the focus to us so that we
541 // forward it to our selected page. We can't deal with this in
542 // OnSetFocus() because we don't know which direction the focus came
543 // from in this case and so can't choose between setting the focus to
544 // first or last panel child
546 wxWindow
*parent
= GetParent();
547 if ( event
.GetEventObject() == parent
)
549 // no, it doesn't come from child, case (b): forward to a page
550 if ( m_nSelection
!= -1 )
552 // so that the page knows that the event comes from it's parent
553 // and is being propagated downwards
554 event
.SetEventObject(this);
556 wxWindow
*page
= m_aPages
[m_nSelection
];
557 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
561 //else: page manages focus inside it itself
565 // we have no pages - still have to give focus to _something_
571 // it comes from our child, case (a), pass to the parent
573 event
.SetCurrentFocus(this);
574 parent
->GetEventHandler()->ProcessEvent(event
);
580 // ----------------------------------------------------------------------------
581 // wxNotebook base class virtuals
582 // ----------------------------------------------------------------------------
584 // override these 2 functions to do nothing: everything is done in OnSize
586 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
588 // don't set the sizes of the pages - their correct size is not yet known
589 wxControl::SetConstraintSizes(FALSE
);
592 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
597 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
599 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
601 NMHDR
* hdr
= (NMHDR
*)lParam
;
602 switch ( hdr
->code
) {
604 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
607 case TCN_SELCHANGING
:
608 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
612 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
615 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
616 event
.SetOldSelection(m_nSelection
);
617 event
.SetEventObject(this);
618 event
.SetInt(idCtrl
);
620 bool processed
= GetEventHandler()->ProcessEvent(event
);
621 *result
= !event
.IsAllowed();
625 // ----------------------------------------------------------------------------
626 // wxNotebook helper functions
627 // ----------------------------------------------------------------------------
629 // generate the page changing and changed events, hide the currently active
630 // panel and show the new one
631 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
633 // MT-FIXME should use a real semaphore
634 static bool s_bInsideChangePage
= FALSE
;
636 // when we call ProcessEvent(), our own OnSelChange() is called which calls
637 // this function - break the infinite loop
638 if ( s_bInsideChangePage
)
641 // it's not an error (the message may be generated by the tab control itself)
642 // and it may happen - just do nothing
643 if ( nSel
== nOldSel
)
646 s_bInsideChangePage
= TRUE
;
648 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
649 event
.SetSelection(nSel
);
650 event
.SetOldSelection(nOldSel
);
651 event
.SetEventObject(this);
652 if ( ProcessEvent(event
) && !event
.IsAllowed() )
654 // program doesn't allow the page change
655 s_bInsideChangePage
= FALSE
;
659 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
662 s_bInsideChangePage
= FALSE
;