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 CreateBase(parent
, id
, pos
, size
, style
, name
);
128 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
129 m_foregroundColour
= *wxBLACK
;
132 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
134 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
136 if (m_windowStyle
& wxCLIP_CHILDREN
)
137 tabStyle
|= WS_CLIPCHILDREN
;
138 if ( m_windowStyle
& wxTC_MULTILINE
)
139 tabStyle
|= TCS_MULTILINE
;
140 if ( m_windowStyle
& wxBORDER
)
141 tabStyle
&= WS_BORDER
;
142 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
143 tabStyle
|= TCS_FIXEDWIDTH
;
145 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL
,
146 this, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
,
152 // Not all compilers recognise SetWindowFont
153 ::SendMessage(GetHwnd(), WM_SETFONT
,
154 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
), TRUE
);
157 if ( parent
!= NULL
)
158 parent
->AddChild(this);
166 wxNotebook::~wxNotebook()
170 // ----------------------------------------------------------------------------
171 // wxNotebook accessors
172 // ----------------------------------------------------------------------------
173 int wxNotebook::GetPageCount() const
176 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
178 return m_aPages
.Count();
181 int wxNotebook::GetRowCount() const
183 return TabCtrl_GetRowCount(m_hwnd
);
186 int wxNotebook::SetSelection(int nPage
)
188 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("notebook page out of range") );
190 ChangePage(m_nSelection
, nPage
);
192 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
195 void wxNotebook::AdvanceSelection(bool bForward
)
197 int nSel
= GetSelection();
198 int nMax
= GetPageCount() - 1;
200 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
202 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
205 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
207 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
210 tcItem
.mask
= TCIF_TEXT
;
211 tcItem
.pszText
= (wxChar
*)strText
.c_str();
213 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
216 wxString
wxNotebook::GetPageText(int nPage
) const
218 wxCHECK_MSG( IS_VALID_PAGE(nPage
), _T(""), _T("notebook page out of range") );
222 tcItem
.mask
= TCIF_TEXT
;
223 tcItem
.pszText
= buf
;
224 tcItem
.cchTextMax
= WXSIZEOF(buf
);
227 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
228 str
= tcItem
.pszText
;
233 int wxNotebook::GetPageImage(int nPage
) const
235 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, _T("notebook page out of range") );
238 tcItem
.mask
= TCIF_IMAGE
;
240 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
243 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
245 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
248 tcItem
.mask
= TCIF_IMAGE
;
249 tcItem
.iImage
= nImage
;
251 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
254 void wxNotebook::SetImageList(wxImageList
* imageList
)
256 m_pImageList
= imageList
;
257 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
261 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
263 void wxNotebook::SetTabSize(const wxSize
& sz
)
265 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
268 // ----------------------------------------------------------------------------
269 // wxNotebook operations
270 // ----------------------------------------------------------------------------
272 // remove one page from the notebook
273 bool wxNotebook::DeletePage(int nPage
)
275 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
277 if ( m_nSelection
== nPage
) {
278 // advance selection backwards - the page being deleted shouldn't be left
280 AdvanceSelection(FALSE
);
283 TabCtrl_DeleteItem(m_hwnd
, nPage
);
285 delete m_aPages
[nPage
];
286 m_aPages
.Remove(nPage
);
288 if ( m_aPages
.IsEmpty() ) {
289 // no selection if the notebook became empty
296 // remove one page from the notebook, without deleting
297 bool wxNotebook::RemovePage(int nPage
)
299 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, _T("notebook page out of range") );
301 TabCtrl_DeleteItem(m_hwnd
, nPage
);
303 m_aPages
.Remove(nPage
);
309 bool wxNotebook::DeleteAllPages()
311 int nPageCount
= GetPageCount();
313 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
314 delete m_aPages
[nPage
];
318 TabCtrl_DeleteAllItems(m_hwnd
);
323 // add a page to the notebook
324 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
325 const wxString
& strText
,
329 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
332 // same as AddPage() but does it at given position
333 bool wxNotebook::InsertPage(int nPage
,
334 wxNotebookPage
*pPage
,
335 const wxString
& strText
,
339 wxASSERT( pPage
!= NULL
);
340 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
342 // add the tab to the control
348 tcItem
.mask
|= TCIF_IMAGE
;
349 tcItem
.iImage
= imageId
;
354 if (!strText
.IsEmpty())
356 tcItem
.mask
|= TCIF_TEXT
;
357 tcItem
.pszText
= (wxChar
*)strText
.c_str();
360 tcItem
.pszText
= (wxChar
*) NULL
;
362 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
363 wxLogError(_T("Can't create the notebook page '%s'."), strText
.c_str());
367 // save the pointer to the page
368 m_aPages
.Insert(pPage
, nPage
);
370 // some page must be selected: either this one or the first one if there is
371 // still no selection
373 m_nSelection
= nPage
;
374 else if ( m_nSelection
== -1 )
377 // don't show pages by default (we'll need to adjust their size first)
378 HWND hwnd
= GetWinHwnd(pPage
);
379 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
381 // this updates internal flag too - otherwise it will get out of sync
384 // FIXME this is ugly, I'm breaking my own rules... but needed to get display
392 // ----------------------------------------------------------------------------
393 // wxNotebook callbacks
394 // ----------------------------------------------------------------------------
396 void wxNotebook::OnSize(wxSizeEvent
& event
)
398 // make sure the current page is shown and has focus (it's useful because all
399 // pages are created invisible initially)
400 if ( m_nSelection
!= -1 ) {
401 wxNotebookPage
*pPage
= m_aPages
[m_nSelection
];
406 // fit the notebook page to the tab control's display area
408 rc
.left
= rc
.top
= 0;
409 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
411 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
412 size_t nCount
= m_aPages
.Count();
413 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
414 wxNotebookPage
*pPage
= m_aPages
[nPage
];
415 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
416 if ( pPage
->GetAutoLayout() )
423 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
425 // is it our tab control?
426 if ( event
.GetEventObject() == this )
427 ChangePage(event
.GetOldSelection(), event
.GetSelection());
429 // we want to give others a chance to process this message as well
433 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
435 // set focus to the currently selected page if any
436 if ( m_nSelection
!= -1 )
437 m_aPages
[m_nSelection
]->SetFocus();
442 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
444 if ( event
.IsWindowChange() ) {
446 AdvanceSelection(event
.GetDirection());
449 // pass to the parent
451 event
.SetCurrentFocus(this);
452 GetParent()->GetEventHandler()->ProcessEvent(event
);
457 // ----------------------------------------------------------------------------
458 // wxNotebook base class virtuals
459 // ----------------------------------------------------------------------------
461 // override these 2 functions to do nothing: everything is done in OnSize
463 void wxNotebook::SetConstraintSizes(bool /* recurse */)
465 // don't set the sizes of the pages - their correct size is not yet known
466 wxControl::SetConstraintSizes(FALSE
);
469 bool wxNotebook::DoPhase(int /* nPhase */)
474 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
476 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
478 NMHDR
* hdr
= (NMHDR
*)lParam
;
479 switch ( hdr
->code
) {
481 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
484 case TCN_SELCHANGING
:
485 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
489 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
492 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
493 event
.SetOldSelection(m_nSelection
);
494 event
.SetEventObject(this);
495 event
.SetInt(idCtrl
);
497 bool processed
= GetEventHandler()->ProcessEvent(event
);
498 *result
= !event
.IsAllowed();
502 // ----------------------------------------------------------------------------
503 // wxNotebook helper functions
504 // ----------------------------------------------------------------------------
506 // hide the currently active panel and show the new one
507 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
509 // MT-FIXME should use a real semaphore
510 static bool s_bInsideChangePage
= FALSE
;
512 // when we call ProcessEvent(), our own OnSelChange() is called which calls
513 // this function - break the infinite loop
514 if ( s_bInsideChangePage
)
517 // it's not an error (the message may be generated by the tab control itself)
518 // and it may happen - just do nothing
519 if ( nSel
== nOldSel
)
522 s_bInsideChangePage
= TRUE
;
524 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
525 event
.SetSelection(nSel
);
526 event
.SetOldSelection(nOldSel
);
527 event
.SetEventObject(this);
528 if ( ProcessEvent(event
) && !event
.IsAllowed() )
530 // program doesn't allow the page change
531 s_bInsideChangePage
= FALSE
;
536 m_aPages
[nOldSel
]->Show(FALSE
);
538 wxNotebookPage
*pPage
= m_aPages
[nSel
];
542 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
546 s_bInsideChangePage
= FALSE
;