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 #include "wx/msw/gnuwin32/extra.h"
49 #if !defined(__GNUWIN32__) || 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 #if !USE_SHARED_LIBRARIES
68 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
69 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
71 EVT_SIZE(wxNotebook::OnSize
)
73 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
75 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
78 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
79 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
82 // ============================================================================
84 // ============================================================================
86 // ----------------------------------------------------------------------------
87 // wxNotebook construction
88 // ----------------------------------------------------------------------------
90 // common part of all ctors
91 void wxNotebook::Init()
97 // default for dynamic class
98 wxNotebook::wxNotebook()
103 // the same arguments as for wxControl
104 wxNotebook::wxNotebook(wxWindow
*parent
,
109 const wxString
& name
)
113 Create(parent
, id
, pos
, size
, style
, name
);
117 bool wxNotebook::Create(wxWindow
*parent
,
122 const wxString
& name
)
125 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
129 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
130 m_foregroundColour
= *wxBLACK
;
133 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
135 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
137 if (m_windowStyle
& wxCLIP_CHILDREN
)
138 tabStyle
|= WS_CLIPCHILDREN
;
139 if ( m_windowStyle
& wxTC_MULTILINE
)
140 tabStyle
|= TCS_MULTILINE
;
141 if ( m_windowStyle
& wxBORDER
)
142 tabStyle
&= WS_BORDER
;
143 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
144 tabStyle
|= TCS_FIXEDWIDTH
;
146 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL
,
147 this, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
,
153 // Not all compilers recognise SetWindowFont
154 ::SendMessage(GetHwnd(), WM_SETFONT
,
155 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
), TRUE
);
158 if ( parent
!= NULL
)
159 parent
->AddChild(this);
167 wxNotebook::~wxNotebook()
171 // ----------------------------------------------------------------------------
172 // wxNotebook accessors
173 // ----------------------------------------------------------------------------
174 int wxNotebook::GetPageCount() const
177 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
179 return m_aPages
.Count();
182 int wxNotebook::GetRowCount() const
184 return TabCtrl_GetRowCount(m_hwnd
);
187 int wxNotebook::SetSelection(int nPage
)
189 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("notebook page out of range") );
191 ChangePage(m_nSelection
, nPage
);
193 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
196 void wxNotebook::AdvanceSelection(bool bForward
)
198 int nSel
= GetSelection();
199 int nMax
= GetPageCount() - 1;
201 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
203 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
206 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
208 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
211 tcItem
.mask
= TCIF_TEXT
;
212 tcItem
.pszText
= (wxChar
*)strText
.c_str();
214 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
217 wxString
wxNotebook::GetPageText(int nPage
) const
219 wxCHECK_MSG( IS_VALID_PAGE(nPage
), _T(""), _T("notebook page out of range") );
223 tcItem
.mask
= TCIF_TEXT
;
224 tcItem
.pszText
= buf
;
225 tcItem
.cchTextMax
= WXSIZEOF(buf
);
228 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
229 str
= tcItem
.pszText
;
234 int wxNotebook::GetPageImage(int nPage
) const
236 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("notebook page out of range") );
239 tcItem
.mask
= TCIF_IMAGE
;
241 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
244 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
246 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
249 tcItem
.mask
= TCIF_IMAGE
;
250 tcItem
.iImage
= nImage
;
252 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
255 void wxNotebook::SetImageList(wxImageList
* imageList
)
257 m_pImageList
= imageList
;
258 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
262 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
264 void wxNotebook::SetTabSize(const wxSize
& sz
)
266 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
269 // ----------------------------------------------------------------------------
270 // wxNotebook operations
271 // ----------------------------------------------------------------------------
273 // remove one page from the notebook
274 bool wxNotebook::DeletePage(int nPage
)
276 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
278 if ( m_nSelection
== nPage
) {
279 // advance selection backwards - the page being deleted shouldn't be left
281 AdvanceSelection(FALSE
);
284 TabCtrl_DeleteItem(m_hwnd
, nPage
);
286 delete m_aPages
[nPage
];
287 m_aPages
.Remove(nPage
);
289 if ( m_aPages
.IsEmpty() ) {
290 // no selection if the notebook became empty
297 // remove one page from the notebook, without deleting
298 bool wxNotebook::RemovePage(int nPage
)
300 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
302 TabCtrl_DeleteItem(m_hwnd
, nPage
);
304 m_aPages
.Remove(nPage
);
310 bool wxNotebook::DeleteAllPages()
312 int nPageCount
= GetPageCount();
314 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
315 delete m_aPages
[nPage
];
319 TabCtrl_DeleteAllItems(m_hwnd
);
324 // add a page to the notebook
325 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
326 const wxString
& strText
,
330 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
333 // same as AddPage() but does it at given position
334 bool wxNotebook::InsertPage(int nPage
,
335 wxNotebookPage
*pPage
,
336 const wxString
& strText
,
340 wxASSERT( pPage
!= NULL
);
341 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
343 // add the tab to the control
349 tcItem
.mask
|= TCIF_IMAGE
;
350 tcItem
.iImage
= imageId
;
355 if (!strText
.IsEmpty())
357 tcItem
.mask
|= TCIF_TEXT
;
358 tcItem
.pszText
= (wxChar
*)strText
.c_str();
361 tcItem
.pszText
= (wxChar
*) NULL
;
363 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
364 wxLogError(_T("Can't create the notebook page '%s'."), strText
.c_str());
368 // save the pointer to the page
369 m_aPages
.Insert(pPage
, nPage
);
371 // some page must be selected: either this one or the first one if there is
372 // still no selection
374 m_nSelection
= nPage
;
375 else if ( m_nSelection
== -1 )
378 // don't show pages by default (we'll need to adjust their size first)
379 HWND hwnd
= GetWinHwnd(pPage
);
380 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
382 // this updates internal flag too - otherwise it will get out of sync
385 // FIXME this is ugly, I'm breaking my own rules... but needed to get display
393 // ----------------------------------------------------------------------------
394 // wxNotebook callbacks
395 // ----------------------------------------------------------------------------
397 void wxNotebook::OnSize(wxSizeEvent
& event
)
399 // make sure the current page is shown and has focus (it's useful because all
400 // pages are created invisible initially)
401 if ( m_nSelection
!= -1 ) {
402 wxNotebookPage
*pPage
= m_aPages
[m_nSelection
];
407 // fit the notebook page to the tab control's display area
409 rc
.left
= rc
.top
= 0;
410 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
412 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
413 size_t nCount
= m_aPages
.Count();
414 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
415 wxNotebookPage
*pPage
= m_aPages
[nPage
];
416 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
417 if ( pPage
->GetAutoLayout() )
424 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
426 // is it our tab control?
427 if ( event
.GetEventObject() == this )
429 // don't call ChangePage() here because it will generate redundant
430 // notification events
431 int sel
= event
.GetOldSelection();
433 m_aPages
[sel
]->Show(FALSE
);
435 sel
= event
.GetSelection();
438 wxNotebookPage
*pPage
= m_aPages
[sel
];
446 // we want to give others a chance to process this message as well
450 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
452 // set focus to the currently selected page if any
453 if ( m_nSelection
!= -1 )
454 m_aPages
[m_nSelection
]->SetFocus();
459 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
461 if ( event
.IsWindowChange() ) {
463 AdvanceSelection(event
.GetDirection());
466 // pass to the parent
468 event
.SetCurrentFocus(this);
469 GetParent()->GetEventHandler()->ProcessEvent(event
);
474 // ----------------------------------------------------------------------------
475 // wxNotebook base class virtuals
476 // ----------------------------------------------------------------------------
478 // override these 2 functions to do nothing: everything is done in OnSize
480 void wxNotebook::SetConstraintSizes(bool /* recurse */)
482 // don't set the sizes of the pages - their correct size is not yet known
483 wxControl::SetConstraintSizes(FALSE
);
486 bool wxNotebook::DoPhase(int /* nPhase */)
491 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
493 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
495 NMHDR
* hdr
= (NMHDR
*)lParam
;
496 switch ( hdr
->code
) {
498 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
501 case TCN_SELCHANGING
:
502 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
506 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
509 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
510 event
.SetOldSelection(m_nSelection
);
511 event
.SetEventObject(this);
512 event
.SetInt(idCtrl
);
514 bool processed
= GetEventHandler()->ProcessEvent(event
);
515 *result
= !event
.IsAllowed();
519 // ----------------------------------------------------------------------------
520 // wxNotebook helper functions
521 // ----------------------------------------------------------------------------
523 // hide the currently active panel and show the new one
524 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
526 // MT-FIXME should use a real semaphore
527 static bool s_bInsideChangePage
= FALSE
;
529 // when we call ProcessEvent(), our own OnSelChange() is called which calls
530 // this function - break the infinite loop
531 if ( s_bInsideChangePage
)
534 // it's not an error (the message may be generated by the tab control itself)
535 // and it may happen - just do nothing
536 if ( nSel
== nOldSel
)
539 s_bInsideChangePage
= TRUE
;
541 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
542 event
.SetSelection(nSel
);
543 event
.SetOldSelection(nOldSel
);
544 event
.SetEventObject(this);
545 if ( ProcessEvent(event
) && !event
.IsAllowed() )
547 // program doesn't allow the page change
548 s_bInsideChangePage
= FALSE
;
553 m_aPages
[nOldSel
]->Show(FALSE
);
555 wxNotebookPage
*pPage
= m_aPages
[nSel
];
559 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
563 s_bInsideChangePage
= FALSE
;