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()
113 m_bOwnsImageList
= FALSE
;
118 // default for dynamic class
119 wxNotebook::wxNotebook()
124 // the same arguments as for wxControl
125 wxNotebook::wxNotebook(wxWindow
*parent
,
130 const wxString
& name
)
134 Create(parent
, id
, pos
, size
, style
, name
);
138 bool wxNotebook::Create(wxWindow
*parent
,
143 const wxString
& name
)
146 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
150 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
151 m_foregroundColour
= *wxBLACK
;
154 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
156 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
158 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
159 tabStyle
|= WS_CLIPSIBLINGS
;
160 if (m_windowStyle
& wxCLIP_CHILDREN
)
161 tabStyle
|= WS_CLIPCHILDREN
;
162 if ( m_windowStyle
& wxTC_MULTILINE
)
163 tabStyle
|= TCS_MULTILINE
;
164 if ( m_windowStyle
& wxBORDER
)
165 tabStyle
&= WS_BORDER
;
166 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
167 tabStyle
|= TCS_FIXEDWIDTH
;
168 if (m_windowStyle
& wxNB_BOTTOM
)
169 tabStyle
|= TCS_RIGHT
;
170 if (m_windowStyle
& wxNB_LEFT
)
171 tabStyle
|= TCS_VERTICAL
;
172 if (m_windowStyle
& wxNB_RIGHT
)
173 tabStyle
|= TCS_VERTICAL
|TCS_RIGHT
;
176 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL
,
177 this, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
,
183 // Not all compilers recognise SetWindowFont
184 ::SendMessage(GetHwnd(), WM_SETFONT
,
185 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
), TRUE
);
188 if ( parent
!= NULL
)
189 parent
->AddChild(this);
197 wxNotebook::~wxNotebook()
199 if (m_bOwnsImageList
)
203 // ----------------------------------------------------------------------------
204 // wxNotebook accessors
205 // ----------------------------------------------------------------------------
206 int wxNotebook::GetPageCount() const
209 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
211 return m_pages
.Count();
214 int wxNotebook::GetRowCount() const
216 return TabCtrl_GetRowCount(m_hwnd
);
219 int wxNotebook::SetSelection(int nPage
)
221 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
223 ChangePage(m_nSelection
, nPage
);
225 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
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 if ( m_bOwnsImageList
)
284 m_bOwnsImageList
= FALSE
;
285 m_imageList
= imageList
;
287 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
290 void wxNotebook::AssignImageList(wxImageList
* imageList
)
292 SetImageList(imageList
);
293 m_bOwnsImageList
= TRUE
;
296 // ----------------------------------------------------------------------------
297 // wxNotebook size settings
298 // ----------------------------------------------------------------------------
300 void wxNotebook::SetPageSize(const wxSize
& size
)
302 // transform the page size into the notebook size
309 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
312 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
315 void wxNotebook::SetPadding(const wxSize
& padding
)
317 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
320 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
322 void wxNotebook::SetTabSize(const wxSize
& sz
)
324 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
327 // ----------------------------------------------------------------------------
328 // wxNotebook operations
329 // ----------------------------------------------------------------------------
331 // remove one page from the notebook
332 bool wxNotebook::DeletePage(int nPage
)
334 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
336 if ( m_nSelection
== nPage
) {
337 // advance selection backwards - the page being deleted shouldn't be left
339 AdvanceSelection(FALSE
);
342 TabCtrl_DeleteItem(m_hwnd
, nPage
);
344 delete m_pages
[nPage
];
345 m_pages
.Remove(nPage
);
347 if ( m_pages
.IsEmpty() ) {
348 // no selection if the notebook became empty
352 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
358 // remove one page from the notebook, without deleting
359 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
361 wxCHECK_MSG( IS_VALID_PAGE(nPage
), NULL
, wxT("notebook page out of range") );
363 TabCtrl_DeleteItem(m_hwnd
, nPage
);
365 wxNotebookPage
*pageRemoved
= m_pages
[nPage
];
366 m_pages
.Remove(nPage
);
368 if ( m_pages
.IsEmpty() )
371 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
377 bool wxNotebook::DeleteAllPages()
379 int nPageCount
= GetPageCount();
381 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
382 delete m_pages
[nPage
];
386 TabCtrl_DeleteAllItems(m_hwnd
);
393 // add a page to the notebook
394 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
395 const wxString
& strText
,
399 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
402 // same as AddPage() but does it at given position
403 bool wxNotebook::InsertPage(int nPage
,
404 wxNotebookPage
*pPage
,
405 const wxString
& strText
,
409 wxASSERT( pPage
!= NULL
);
410 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
412 // do add the tab to the control
414 // init all fields to 0
416 memset(&tcItem
, 0, sizeof(tcItem
));
420 tcItem
.mask
|= TCIF_IMAGE
;
421 tcItem
.iImage
= imageId
;
424 if ( !strText
.IsEmpty() )
426 tcItem
.mask
|= TCIF_TEXT
;
427 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
430 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
431 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
436 // if the inserted page is before the selected one, we must update the
437 // index of the selected page
438 if ( nPage
<= m_nSelection
)
440 // one extra page added
444 // save the pointer to the page
445 m_pages
.Insert(pPage
, nPage
);
447 // don't show pages by default (we'll need to adjust their size first)
448 HWND hwnd
= GetWinHwnd(pPage
);
449 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
451 // this updates internal flag too - otherwise it will get out of sync
454 // fit the notebook page to the tab control's display area
456 rc
.left
= rc
.top
= 0;
457 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
458 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
459 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
461 // some page should be selected: either this one or the first one if there is
462 // still no selection
466 else if ( m_nSelection
== -1 )
470 SetSelection(selNew
);
475 // ----------------------------------------------------------------------------
476 // wxNotebook callbacks
477 // ----------------------------------------------------------------------------
479 void wxNotebook::OnSize(wxSizeEvent
& event
)
481 // fit the notebook page to the tab control's display area
483 rc
.left
= rc
.top
= 0;
484 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
486 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
488 int width
= rc
.right
- rc
.left
,
489 height
= rc
.bottom
- rc
.top
;
490 size_t nCount
= m_pages
.Count();
491 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
492 wxNotebookPage
*pPage
= m_pages
[nPage
];
493 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
499 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
501 // is it our tab control?
502 if ( event
.GetEventObject() == this )
504 int sel
= event
.GetOldSelection();
506 m_pages
[sel
]->Show(FALSE
);
508 sel
= event
.GetSelection();
511 wxNotebookPage
*pPage
= m_pages
[sel
];
519 // we want to give others a chance to process this message as well
523 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
525 // this function is only called when the focus is explicitly set (i.e. from
526 // the program) to the notebook - in this case we don't need the
527 // complicated OnNavigationKey() logic because the programmer knows better
530 // set focus to the currently selected page if any
531 if ( m_nSelection
!= -1 )
532 m_pages
[m_nSelection
]->SetFocus();
537 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
539 if ( event
.IsWindowChange() ) {
541 AdvanceSelection(event
.GetDirection());
544 // we get this event in 2 cases
546 // a) one of our pages might have generated it because the user TABbed
547 // out from it in which case we should propagate the event upwards and
548 // our parent will take care of setting the focus to prev/next sibling
552 // b) the parent panel wants to give the focus to us so that we
553 // forward it to our selected page. We can't deal with this in
554 // OnSetFocus() because we don't know which direction the focus came
555 // from in this case and so can't choose between setting the focus to
556 // first or last panel child
558 wxWindow
*parent
= GetParent();
559 if ( event
.GetEventObject() == parent
)
561 // no, it doesn't come from child, case (b): forward to a page
562 if ( m_nSelection
!= -1 )
564 // so that the page knows that the event comes from it's parent
565 // and is being propagated downwards
566 event
.SetEventObject(this);
568 wxWindow
*page
= m_pages
[m_nSelection
];
569 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
573 //else: page manages focus inside it itself
577 // we have no pages - still have to give focus to _something_
583 // it comes from our child, case (a), pass to the parent
585 event
.SetCurrentFocus(this);
586 parent
->GetEventHandler()->ProcessEvent(event
);
592 // ----------------------------------------------------------------------------
593 // wxNotebook base class virtuals
594 // ----------------------------------------------------------------------------
596 // override these 2 functions to do nothing: everything is done in OnSize
598 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
600 // don't set the sizes of the pages - their correct size is not yet known
601 wxControl::SetConstraintSizes(FALSE
);
604 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
609 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
611 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
613 NMHDR
* hdr
= (NMHDR
*)lParam
;
614 switch ( hdr
->code
) {
616 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
619 case TCN_SELCHANGING
:
620 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
624 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
627 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
628 event
.SetOldSelection(m_nSelection
);
629 event
.SetEventObject(this);
630 event
.SetInt(idCtrl
);
632 bool processed
= GetEventHandler()->ProcessEvent(event
);
633 *result
= !event
.IsAllowed();
637 // ----------------------------------------------------------------------------
638 // wxNotebook helper functions
639 // ----------------------------------------------------------------------------
641 // generate the page changing and changed events, hide the currently active
642 // panel and show the new one
643 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
645 // MT-FIXME should use a real semaphore
646 static bool s_bInsideChangePage
= FALSE
;
648 // when we call ProcessEvent(), our own OnSelChange() is called which calls
649 // this function - break the infinite loop
650 if ( s_bInsideChangePage
)
653 // it's not an error (the message may be generated by the tab control itself)
654 // and it may happen - just do nothing
655 if ( nSel
== nOldSel
)
658 s_bInsideChangePage
= TRUE
;
660 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
661 event
.SetSelection(nSel
);
662 event
.SetOldSelection(nOldSel
);
663 event
.SetEventObject(this);
664 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
666 // program doesn't allow the page change
667 s_bInsideChangePage
= FALSE
;
671 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
672 GetEventHandler()->ProcessEvent(event
);
674 s_bInsideChangePage
= FALSE
;
677 #endif // wxUSE_NOTEBOOK