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 #if !USE_SHARED_LIBRARIES
87 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
88 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
90 EVT_SIZE(wxNotebook::OnSize
)
92 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
94 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
97 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
98 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
101 // ============================================================================
103 // ============================================================================
105 // ----------------------------------------------------------------------------
106 // wxNotebook construction
107 // ----------------------------------------------------------------------------
109 // common part of all ctors
110 void wxNotebook::Init()
116 // default for dynamic class
117 wxNotebook::wxNotebook()
122 // the same arguments as for wxControl
123 wxNotebook::wxNotebook(wxWindow
*parent
,
128 const wxString
& name
)
132 Create(parent
, id
, pos
, size
, style
, name
);
136 bool wxNotebook::Create(wxWindow
*parent
,
141 const wxString
& name
)
144 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
148 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
149 m_foregroundColour
= *wxBLACK
;
152 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
154 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
156 if (m_windowStyle
& wxCLIP_CHILDREN
)
157 tabStyle
|= WS_CLIPCHILDREN
;
158 if ( m_windowStyle
& wxTC_MULTILINE
)
159 tabStyle
|= TCS_MULTILINE
;
160 if ( m_windowStyle
& wxBORDER
)
161 tabStyle
&= WS_BORDER
;
162 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
163 tabStyle
|= TCS_FIXEDWIDTH
;
164 if (m_windowStyle
& wxNB_BOTTOM
)
165 tabStyle
|= TCS_RIGHT
;
166 if (m_windowStyle
& wxNB_LEFT
)
167 tabStyle
|= TCS_VERTICAL
;
168 if (m_windowStyle
& wxNB_RIGHT
)
169 tabStyle
|= TCS_VERTICAL
|TCS_RIGHT
;
172 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL
,
173 this, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
,
179 // Not all compilers recognise SetWindowFont
180 ::SendMessage(GetHwnd(), WM_SETFONT
,
181 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
), TRUE
);
184 if ( parent
!= NULL
)
185 parent
->AddChild(this);
193 wxNotebook::~wxNotebook()
197 // ----------------------------------------------------------------------------
198 // wxNotebook accessors
199 // ----------------------------------------------------------------------------
200 int wxNotebook::GetPageCount() const
203 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
205 return m_aPages
.Count();
208 int wxNotebook::GetRowCount() const
210 return TabCtrl_GetRowCount(m_hwnd
);
213 int wxNotebook::SetSelection(int nPage
)
215 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
217 ChangePage(m_nSelection
, nPage
);
219 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
222 void wxNotebook::AdvanceSelection(bool bForward
)
224 int nSel
= GetSelection();
225 int nMax
= GetPageCount() - 1;
227 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
229 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
232 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
234 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
237 tcItem
.mask
= TCIF_TEXT
;
238 tcItem
.pszText
= (wxChar
*)strText
.c_str();
240 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
243 wxString
wxNotebook::GetPageText(int nPage
) const
245 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
249 tcItem
.mask
= TCIF_TEXT
;
250 tcItem
.pszText
= buf
;
251 tcItem
.cchTextMax
= WXSIZEOF(buf
);
254 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
255 str
= tcItem
.pszText
;
260 int wxNotebook::GetPageImage(int nPage
) const
262 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
265 tcItem
.mask
= TCIF_IMAGE
;
267 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
270 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
272 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
275 tcItem
.mask
= TCIF_IMAGE
;
276 tcItem
.iImage
= nImage
;
278 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
281 void wxNotebook::SetImageList(wxImageList
* imageList
)
283 m_pImageList
= imageList
;
284 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
288 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
290 void wxNotebook::SetTabSize(const wxSize
& sz
)
292 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
295 // ----------------------------------------------------------------------------
296 // wxNotebook operations
297 // ----------------------------------------------------------------------------
299 // remove one page from the notebook
300 bool wxNotebook::DeletePage(int nPage
)
302 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
304 if ( m_nSelection
== nPage
) {
305 // advance selection backwards - the page being deleted shouldn't be left
307 AdvanceSelection(FALSE
);
310 TabCtrl_DeleteItem(m_hwnd
, nPage
);
312 delete m_aPages
[nPage
];
313 m_aPages
.Remove(nPage
);
315 if ( m_aPages
.IsEmpty() ) {
316 // no selection if the notebook became empty
323 // remove one page from the notebook, without deleting
324 bool wxNotebook::RemovePage(int nPage
)
326 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
328 TabCtrl_DeleteItem(m_hwnd
, nPage
);
330 m_aPages
.Remove(nPage
);
332 if ( m_aPages
.IsEmpty() )
339 bool wxNotebook::DeleteAllPages()
341 int nPageCount
= GetPageCount();
343 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
344 delete m_aPages
[nPage
];
348 TabCtrl_DeleteAllItems(m_hwnd
);
355 // add a page to the notebook
356 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
357 const wxString
& strText
,
361 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
364 // same as AddPage() but does it at given position
365 bool wxNotebook::InsertPage(int nPage
,
366 wxNotebookPage
*pPage
,
367 const wxString
& strText
,
371 wxASSERT( pPage
!= NULL
);
372 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
374 // do add the tab to the control
376 // init all fields to 0
378 memset(&tcItem
, 0, sizeof(tcItem
));
382 tcItem
.mask
|= TCIF_IMAGE
;
383 tcItem
.iImage
= imageId
;
386 if ( !strText
.IsEmpty() )
388 tcItem
.mask
|= TCIF_TEXT
;
389 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
392 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
393 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
398 // if the inserted page is before the selected one, we must update the
399 // index of the selected page
400 if ( nPage
<= m_nSelection
)
402 // one extra page added
406 // save the pointer to the page
407 m_aPages
.Insert(pPage
, nPage
);
409 // don't show pages by default (we'll need to adjust their size first)
410 HWND hwnd
= GetWinHwnd(pPage
);
411 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
413 // this updates internal flag too - otherwise it will get out of sync
416 // fit the notebook page to the tab control's display area
418 rc
.left
= rc
.top
= 0;
419 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
420 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
421 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
424 // some page should be selected: either this one or the first one if there is
425 // still no selection
429 else if ( m_nSelection
== -1 )
433 SetSelection(selNew
);
438 // ----------------------------------------------------------------------------
439 // wxNotebook callbacks
440 // ----------------------------------------------------------------------------
442 void wxNotebook::OnSize(wxSizeEvent
& event
)
444 // fit the notebook page to the tab control's display area
446 rc
.left
= rc
.top
= 0;
447 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
449 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
450 size_t nCount
= m_aPages
.Count();
451 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
452 wxNotebookPage
*pPage
= m_aPages
[nPage
];
453 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
459 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
461 // is it our tab control?
462 if ( event
.GetEventObject() == this )
464 int sel
= event
.GetOldSelection();
466 m_aPages
[sel
]->Show(FALSE
);
468 sel
= event
.GetSelection();
471 wxNotebookPage
*pPage
= m_aPages
[sel
];
479 // we want to give others a chance to process this message as well
483 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
485 // set focus to the currently selected page if any
486 if ( m_nSelection
!= -1 )
487 m_aPages
[m_nSelection
]->SetFocus();
492 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
494 if ( event
.IsWindowChange() ) {
496 AdvanceSelection(event
.GetDirection());
499 // pass to the parent
501 event
.SetCurrentFocus(this);
502 GetParent()->GetEventHandler()->ProcessEvent(event
);
507 // ----------------------------------------------------------------------------
508 // wxNotebook base class virtuals
509 // ----------------------------------------------------------------------------
511 // override these 2 functions to do nothing: everything is done in OnSize
513 void wxNotebook::SetConstraintSizes(bool /* recurse */)
515 // don't set the sizes of the pages - their correct size is not yet known
516 wxControl::SetConstraintSizes(FALSE
);
519 bool wxNotebook::DoPhase(int /* nPhase */)
524 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
526 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
528 NMHDR
* hdr
= (NMHDR
*)lParam
;
529 switch ( hdr
->code
) {
531 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
534 case TCN_SELCHANGING
:
535 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
539 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
542 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
543 event
.SetOldSelection(m_nSelection
);
544 event
.SetEventObject(this);
545 event
.SetInt(idCtrl
);
547 bool processed
= GetEventHandler()->ProcessEvent(event
);
548 *result
= !event
.IsAllowed();
552 // ----------------------------------------------------------------------------
553 // wxNotebook helper functions
554 // ----------------------------------------------------------------------------
556 // generate the page changing and changed events, hide the currently active
557 // panel and show the new one
558 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
560 // MT-FIXME should use a real semaphore
561 static bool s_bInsideChangePage
= FALSE
;
563 // when we call ProcessEvent(), our own OnSelChange() is called which calls
564 // this function - break the infinite loop
565 if ( s_bInsideChangePage
)
568 // it's not an error (the message may be generated by the tab control itself)
569 // and it may happen - just do nothing
570 if ( nSel
== nOldSel
)
573 s_bInsideChangePage
= TRUE
;
575 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
576 event
.SetSelection(nSel
);
577 event
.SetOldSelection(nOldSel
);
578 event
.SetEventObject(this);
579 if ( ProcessEvent(event
) && !event
.IsAllowed() )
581 // program doesn't allow the page change
582 s_bInsideChangePage
= FALSE
;
586 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
589 s_bInsideChangePage
= FALSE
;