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 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
87 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
89 EVT_SIZE(wxNotebook::OnSize
)
91 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
93 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
96 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
97 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
99 // ============================================================================
101 // ============================================================================
103 // ----------------------------------------------------------------------------
104 // wxNotebook construction
105 // ----------------------------------------------------------------------------
107 // common part of all ctors
108 void wxNotebook::Init()
114 // default for dynamic class
115 wxNotebook::wxNotebook()
120 // the same arguments as for wxControl
121 wxNotebook::wxNotebook(wxWindow
*parent
,
126 const wxString
& name
)
130 Create(parent
, id
, pos
, size
, style
, name
);
134 bool wxNotebook::Create(wxWindow
*parent
,
139 const wxString
& name
)
142 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
146 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
147 m_foregroundColour
= *wxBLACK
;
150 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
152 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
154 if (m_windowStyle
& wxCLIP_CHILDREN
)
155 tabStyle
|= WS_CLIPCHILDREN
;
156 if ( m_windowStyle
& wxTC_MULTILINE
)
157 tabStyle
|= TCS_MULTILINE
;
158 if ( m_windowStyle
& wxBORDER
)
159 tabStyle
&= WS_BORDER
;
160 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
161 tabStyle
|= TCS_FIXEDWIDTH
;
162 if (m_windowStyle
& wxNB_BOTTOM
)
163 tabStyle
|= TCS_RIGHT
;
164 if (m_windowStyle
& wxNB_LEFT
)
165 tabStyle
|= TCS_VERTICAL
;
166 if (m_windowStyle
& wxNB_RIGHT
)
167 tabStyle
|= TCS_VERTICAL
|TCS_RIGHT
;
170 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL
,
171 this, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
,
177 // Not all compilers recognise SetWindowFont
178 ::SendMessage(GetHwnd(), WM_SETFONT
,
179 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
), TRUE
);
182 if ( parent
!= NULL
)
183 parent
->AddChild(this);
191 wxNotebook::~wxNotebook()
195 // ----------------------------------------------------------------------------
196 // wxNotebook accessors
197 // ----------------------------------------------------------------------------
198 int wxNotebook::GetPageCount() const
201 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
203 return m_aPages
.Count();
206 int wxNotebook::GetRowCount() const
208 return TabCtrl_GetRowCount(m_hwnd
);
211 int wxNotebook::SetSelection(int nPage
)
213 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
215 ChangePage(m_nSelection
, nPage
);
217 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
220 void wxNotebook::AdvanceSelection(bool bForward
)
222 int nSel
= GetSelection();
223 int nMax
= GetPageCount() - 1;
225 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
227 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
230 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
232 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
235 tcItem
.mask
= TCIF_TEXT
;
236 tcItem
.pszText
= (wxChar
*)strText
.c_str();
238 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
241 wxString
wxNotebook::GetPageText(int nPage
) const
243 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
247 tcItem
.mask
= TCIF_TEXT
;
248 tcItem
.pszText
= buf
;
249 tcItem
.cchTextMax
= WXSIZEOF(buf
);
252 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
253 str
= tcItem
.pszText
;
258 int wxNotebook::GetPageImage(int nPage
) const
260 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
263 tcItem
.mask
= TCIF_IMAGE
;
265 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
268 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
270 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
273 tcItem
.mask
= TCIF_IMAGE
;
274 tcItem
.iImage
= nImage
;
276 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
279 void wxNotebook::SetImageList(wxImageList
* imageList
)
281 m_pImageList
= imageList
;
282 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
286 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
288 void wxNotebook::SetTabSize(const wxSize
& sz
)
290 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
293 // ----------------------------------------------------------------------------
294 // wxNotebook operations
295 // ----------------------------------------------------------------------------
297 // remove one page from the notebook
298 bool wxNotebook::DeletePage(int nPage
)
300 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
302 if ( m_nSelection
== nPage
) {
303 // advance selection backwards - the page being deleted shouldn't be left
305 AdvanceSelection(FALSE
);
308 TabCtrl_DeleteItem(m_hwnd
, nPage
);
310 delete m_aPages
[nPage
];
311 m_aPages
.Remove(nPage
);
313 if ( m_aPages
.IsEmpty() ) {
314 // no selection if the notebook became empty
318 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
324 // remove one page from the notebook, without deleting
325 bool wxNotebook::RemovePage(int nPage
)
327 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
329 TabCtrl_DeleteItem(m_hwnd
, nPage
);
331 m_aPages
.Remove(nPage
);
333 if ( m_aPages
.IsEmpty() )
336 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
342 bool wxNotebook::DeleteAllPages()
344 int nPageCount
= GetPageCount();
346 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
347 delete m_aPages
[nPage
];
351 TabCtrl_DeleteAllItems(m_hwnd
);
358 // add a page to the notebook
359 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
360 const wxString
& strText
,
364 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
367 // same as AddPage() but does it at given position
368 bool wxNotebook::InsertPage(int nPage
,
369 wxNotebookPage
*pPage
,
370 const wxString
& strText
,
374 wxASSERT( pPage
!= NULL
);
375 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
377 // do add the tab to the control
379 // init all fields to 0
381 memset(&tcItem
, 0, sizeof(tcItem
));
385 tcItem
.mask
|= TCIF_IMAGE
;
386 tcItem
.iImage
= imageId
;
389 if ( !strText
.IsEmpty() )
391 tcItem
.mask
|= TCIF_TEXT
;
392 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
395 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
396 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
401 // if the inserted page is before the selected one, we must update the
402 // index of the selected page
403 if ( nPage
<= m_nSelection
)
405 // one extra page added
409 // save the pointer to the page
410 m_aPages
.Insert(pPage
, nPage
);
412 // don't show pages by default (we'll need to adjust their size first)
413 HWND hwnd
= GetWinHwnd(pPage
);
414 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
416 // this updates internal flag too - otherwise it will get out of sync
419 // fit the notebook page to the tab control's display area
421 rc
.left
= rc
.top
= 0;
422 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
423 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
424 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
427 // some page should be selected: either this one or the first one if there is
428 // still no selection
432 else if ( m_nSelection
== -1 )
436 SetSelection(selNew
);
441 // ----------------------------------------------------------------------------
442 // wxNotebook callbacks
443 // ----------------------------------------------------------------------------
445 void wxNotebook::OnSize(wxSizeEvent
& event
)
447 // fit the notebook page to the tab control's display area
449 rc
.left
= rc
.top
= 0;
450 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
452 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
454 int width
= rc
.right
- rc
.left
,
455 height
= rc
.bottom
- rc
.top
;
456 size_t nCount
= m_aPages
.Count();
457 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
458 wxNotebookPage
*pPage
= m_aPages
[nPage
];
459 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
465 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
467 // is it our tab control?
468 if ( event
.GetEventObject() == this )
470 int sel
= event
.GetOldSelection();
472 m_aPages
[sel
]->Show(FALSE
);
474 sel
= event
.GetSelection();
477 wxNotebookPage
*pPage
= m_aPages
[sel
];
485 // we want to give others a chance to process this message as well
489 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
491 // set focus to the currently selected page if any
492 if ( m_nSelection
!= -1 )
493 m_aPages
[m_nSelection
]->SetFocus();
498 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
500 if ( event
.IsWindowChange() ) {
502 AdvanceSelection(event
.GetDirection());
505 // pass to the parent
507 event
.SetCurrentFocus(this);
508 GetParent()->GetEventHandler()->ProcessEvent(event
);
513 // ----------------------------------------------------------------------------
514 // wxNotebook base class virtuals
515 // ----------------------------------------------------------------------------
517 // override these 2 functions to do nothing: everything is done in OnSize
519 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
521 // don't set the sizes of the pages - their correct size is not yet known
522 wxControl::SetConstraintSizes(FALSE
);
525 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
530 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
532 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
534 NMHDR
* hdr
= (NMHDR
*)lParam
;
535 switch ( hdr
->code
) {
537 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
540 case TCN_SELCHANGING
:
541 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
545 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
548 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
549 event
.SetOldSelection(m_nSelection
);
550 event
.SetEventObject(this);
551 event
.SetInt(idCtrl
);
553 bool processed
= GetEventHandler()->ProcessEvent(event
);
554 *result
= !event
.IsAllowed();
558 // ----------------------------------------------------------------------------
559 // wxNotebook helper functions
560 // ----------------------------------------------------------------------------
562 // generate the page changing and changed events, hide the currently active
563 // panel and show the new one
564 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
566 // MT-FIXME should use a real semaphore
567 static bool s_bInsideChangePage
= FALSE
;
569 // when we call ProcessEvent(), our own OnSelChange() is called which calls
570 // this function - break the infinite loop
571 if ( s_bInsideChangePage
)
574 // it's not an error (the message may be generated by the tab control itself)
575 // and it may happen - just do nothing
576 if ( nSel
== nOldSel
)
579 s_bInsideChangePage
= TRUE
;
581 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
582 event
.SetSelection(nSel
);
583 event
.SetOldSelection(nOldSel
);
584 event
.SetEventObject(this);
585 if ( ProcessEvent(event
) && !event
.IsAllowed() )
587 // program doesn't allow the page change
588 s_bInsideChangePage
= FALSE
;
592 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
595 s_bInsideChangePage
= FALSE
;