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 ( !MSWCreateControl(WC_TABCONTROL
, tabStyle
, pos
, size
) )
180 // Not all compilers recognise SetWindowFont
181 ::SendMessage(GetHwnd(), WM_SETFONT
,
182 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
), TRUE
);
185 if ( parent
!= NULL
)
186 parent
->AddChild(this);
191 // ----------------------------------------------------------------------------
192 // wxNotebook accessors
193 // ----------------------------------------------------------------------------
195 int wxNotebook::GetPageCount() const
198 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
200 return m_pages
.Count();
203 int wxNotebook::GetRowCount() const
205 return TabCtrl_GetRowCount(m_hwnd
);
208 int wxNotebook::SetSelection(int nPage
)
210 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
212 ChangePage(m_nSelection
, nPage
);
214 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
217 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
219 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
222 tcItem
.mask
= TCIF_TEXT
;
223 tcItem
.pszText
= (wxChar
*)strText
.c_str();
225 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
228 wxString
wxNotebook::GetPageText(int nPage
) const
230 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
234 tcItem
.mask
= TCIF_TEXT
;
235 tcItem
.pszText
= buf
;
236 tcItem
.cchTextMax
= WXSIZEOF(buf
);
239 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
240 str
= tcItem
.pszText
;
245 int wxNotebook::GetPageImage(int nPage
) const
247 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
250 tcItem
.mask
= TCIF_IMAGE
;
252 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
255 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
257 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
260 tcItem
.mask
= TCIF_IMAGE
;
261 tcItem
.iImage
= nImage
;
263 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
266 void wxNotebook::SetImageList(wxImageList
* imageList
)
268 wxNotebookBase::SetImageList(imageList
);
272 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
276 // ----------------------------------------------------------------------------
277 // wxNotebook size settings
278 // ----------------------------------------------------------------------------
280 void wxNotebook::SetPageSize(const wxSize
& size
)
282 // transform the page size into the notebook size
289 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
292 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
295 void wxNotebook::SetPadding(const wxSize
& padding
)
297 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
300 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
302 void wxNotebook::SetTabSize(const wxSize
& sz
)
304 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
307 // ----------------------------------------------------------------------------
308 // wxNotebook operations
309 // ----------------------------------------------------------------------------
311 // remove one page from the notebook
312 bool wxNotebook::DeletePage(int nPage
)
314 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
316 if ( m_nSelection
== nPage
) {
317 // advance selection backwards - the page being deleted shouldn't be left
319 AdvanceSelection(FALSE
);
322 TabCtrl_DeleteItem(m_hwnd
, nPage
);
324 delete m_pages
[nPage
];
325 m_pages
.RemoveAt(nPage
);
327 if ( m_pages
.IsEmpty() ) {
328 // no selection if the notebook became empty
332 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
338 // remove one page from the notebook, without deleting
339 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
341 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
345 TabCtrl_DeleteItem(m_hwnd
, nPage
);
347 if ( m_pages
.IsEmpty() )
350 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
356 bool wxNotebook::DeleteAllPages()
358 int nPageCount
= GetPageCount();
360 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
361 delete m_pages
[nPage
];
365 TabCtrl_DeleteAllItems(m_hwnd
);
372 // add a page to the notebook
373 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
374 const wxString
& strText
,
378 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
381 // same as AddPage() but does it at given position
382 bool wxNotebook::InsertPage(int nPage
,
383 wxNotebookPage
*pPage
,
384 const wxString
& strText
,
388 wxASSERT( pPage
!= NULL
);
389 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
391 // do add the tab to the control
393 // init all fields to 0
395 memset(&tcItem
, 0, sizeof(tcItem
));
399 tcItem
.mask
|= TCIF_IMAGE
;
400 tcItem
.iImage
= imageId
;
403 if ( !strText
.IsEmpty() )
405 tcItem
.mask
|= TCIF_TEXT
;
406 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
409 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
410 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
415 // if the inserted page is before the selected one, we must update the
416 // index of the selected page
417 if ( nPage
<= m_nSelection
)
419 // one extra page added
423 // save the pointer to the page
424 m_pages
.Insert(pPage
, nPage
);
426 // don't show pages by default (we'll need to adjust their size first)
427 HWND hwnd
= GetWinHwnd(pPage
);
428 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
430 // this updates internal flag too - otherwise it will get out of sync
433 // fit the notebook page to the tab control's display area
435 rc
.left
= rc
.top
= 0;
436 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
437 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
438 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
440 // some page should be selected: either this one or the first one if there is
441 // still no selection
445 else if ( m_nSelection
== -1 )
449 SetSelection(selNew
);
454 // ----------------------------------------------------------------------------
455 // wxNotebook callbacks
456 // ----------------------------------------------------------------------------
458 void wxNotebook::OnSize(wxSizeEvent
& event
)
460 // fit the notebook page to the tab control's display area
462 rc
.left
= rc
.top
= 0;
463 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
465 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
467 int width
= rc
.right
- rc
.left
,
468 height
= rc
.bottom
- rc
.top
;
469 size_t nCount
= m_pages
.Count();
470 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
471 wxNotebookPage
*pPage
= m_pages
[nPage
];
472 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
478 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
480 // is it our tab control?
481 if ( event
.GetEventObject() == this )
483 int sel
= event
.GetOldSelection();
485 m_pages
[sel
]->Show(FALSE
);
487 sel
= event
.GetSelection();
490 wxNotebookPage
*pPage
= m_pages
[sel
];
498 // we want to give others a chance to process this message as well
502 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
504 // this function is only called when the focus is explicitly set (i.e. from
505 // the program) to the notebook - in this case we don't need the
506 // complicated OnNavigationKey() logic because the programmer knows better
509 // set focus to the currently selected page if any
510 if ( m_nSelection
!= -1 )
511 m_pages
[m_nSelection
]->SetFocus();
516 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
518 if ( event
.IsWindowChange() ) {
520 AdvanceSelection(event
.GetDirection());
523 // we get this event in 2 cases
525 // a) one of our pages might have generated it because the user TABbed
526 // out from it in which case we should propagate the event upwards and
527 // our parent will take care of setting the focus to prev/next sibling
531 // b) the parent panel wants to give the focus to us so that we
532 // forward it to our selected page. We can't deal with this in
533 // OnSetFocus() because we don't know which direction the focus came
534 // from in this case and so can't choose between setting the focus to
535 // first or last panel child
537 wxWindow
*parent
= GetParent();
538 if ( event
.GetEventObject() == parent
)
540 // no, it doesn't come from child, case (b): forward to a page
541 if ( m_nSelection
!= -1 )
543 // so that the page knows that the event comes from it's parent
544 // and is being propagated downwards
545 event
.SetEventObject(this);
547 wxWindow
*page
= m_pages
[m_nSelection
];
548 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
552 //else: page manages focus inside it itself
556 // we have no pages - still have to give focus to _something_
562 // it comes from our child, case (a), pass to the parent
564 event
.SetCurrentFocus(this);
565 parent
->GetEventHandler()->ProcessEvent(event
);
571 // ----------------------------------------------------------------------------
572 // wxNotebook base class virtuals
573 // ----------------------------------------------------------------------------
575 // override these 2 functions to do nothing: everything is done in OnSize
577 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
579 // don't set the sizes of the pages - their correct size is not yet known
580 wxControl::SetConstraintSizes(FALSE
);
583 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
588 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
590 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
592 NMHDR
* hdr
= (NMHDR
*)lParam
;
593 switch ( hdr
->code
) {
595 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
598 case TCN_SELCHANGING
:
599 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
603 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
606 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
607 event
.SetOldSelection(m_nSelection
);
608 event
.SetEventObject(this);
609 event
.SetInt(idCtrl
);
611 bool processed
= GetEventHandler()->ProcessEvent(event
);
612 *result
= !event
.IsAllowed();
616 // ----------------------------------------------------------------------------
617 // wxNotebook helper functions
618 // ----------------------------------------------------------------------------
620 // generate the page changing and changed events, hide the currently active
621 // panel and show the new one
622 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
624 // MT-FIXME should use a real semaphore
625 static bool s_bInsideChangePage
= FALSE
;
627 // when we call ProcessEvent(), our own OnSelChange() is called which calls
628 // this function - break the infinite loop
629 if ( s_bInsideChangePage
)
632 // it's not an error (the message may be generated by the tab control itself)
633 // and it may happen - just do nothing
634 if ( nSel
== nOldSel
)
637 s_bInsideChangePage
= TRUE
;
639 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
640 event
.SetSelection(nSel
);
641 event
.SetOldSelection(nOldSel
);
642 event
.SetEventObject(this);
643 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
645 // program doesn't allow the page change
646 s_bInsideChangePage
= FALSE
;
650 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
651 GetEventHandler()->ProcessEvent(event
);
653 s_bInsideChangePage
= FALSE
;
656 #endif // wxUSE_NOTEBOOK