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
);
453 size_t nCount
= m_aPages
.Count();
454 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
455 wxNotebookPage
*pPage
= m_aPages
[nPage
];
456 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
462 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
464 // is it our tab control?
465 if ( event
.GetEventObject() == this )
467 int sel
= event
.GetOldSelection();
469 m_aPages
[sel
]->Show(FALSE
);
471 sel
= event
.GetSelection();
474 wxNotebookPage
*pPage
= m_aPages
[sel
];
482 // we want to give others a chance to process this message as well
486 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
488 // set focus to the currently selected page if any
489 if ( m_nSelection
!= -1 )
490 m_aPages
[m_nSelection
]->SetFocus();
495 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
497 if ( event
.IsWindowChange() ) {
499 AdvanceSelection(event
.GetDirection());
502 // pass to the parent
504 event
.SetCurrentFocus(this);
505 GetParent()->GetEventHandler()->ProcessEvent(event
);
510 // ----------------------------------------------------------------------------
511 // wxNotebook base class virtuals
512 // ----------------------------------------------------------------------------
514 // override these 2 functions to do nothing: everything is done in OnSize
516 void wxNotebook::SetConstraintSizes(bool /* recurse */)
518 // don't set the sizes of the pages - their correct size is not yet known
519 wxControl::SetConstraintSizes(FALSE
);
522 bool wxNotebook::DoPhase(int /* nPhase */)
527 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
529 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
531 NMHDR
* hdr
= (NMHDR
*)lParam
;
532 switch ( hdr
->code
) {
534 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
537 case TCN_SELCHANGING
:
538 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
542 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
545 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
546 event
.SetOldSelection(m_nSelection
);
547 event
.SetEventObject(this);
548 event
.SetInt(idCtrl
);
550 bool processed
= GetEventHandler()->ProcessEvent(event
);
551 *result
= !event
.IsAllowed();
555 // ----------------------------------------------------------------------------
556 // wxNotebook helper functions
557 // ----------------------------------------------------------------------------
559 // generate the page changing and changed events, hide the currently active
560 // panel and show the new one
561 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
563 // MT-FIXME should use a real semaphore
564 static bool s_bInsideChangePage
= FALSE
;
566 // when we call ProcessEvent(), our own OnSelChange() is called which calls
567 // this function - break the infinite loop
568 if ( s_bInsideChangePage
)
571 // it's not an error (the message may be generated by the tab control itself)
572 // and it may happen - just do nothing
573 if ( nSel
== nOldSel
)
576 s_bInsideChangePage
= TRUE
;
578 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
579 event
.SetSelection(nSel
);
580 event
.SetOldSelection(nOldSel
);
581 event
.SetEventObject(this);
582 if ( ProcessEvent(event
) && !event
.IsAllowed() )
584 // program doesn't allow the page change
585 s_bInsideChangePage
= FALSE
;
589 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
592 s_bInsideChangePage
= FALSE
;