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
44 #ifdef __GNUWIN32_OLD__
45 #include "wx/msw/gnuwin32/extra.h"
49 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || 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 // This is a work-around for missing defines in gcc-2.95 headers
69 #define TCS_RIGHT 0x0002
73 #define TCS_VERTICAL 0x0080
77 #define TCS_BOTTOM TCS_RIGHT
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
85 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
87 EVT_SIZE(wxNotebook::OnSize
)
89 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
91 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
94 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
95 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
97 // ============================================================================
99 // ============================================================================
101 // ----------------------------------------------------------------------------
102 // wxNotebook construction
103 // ----------------------------------------------------------------------------
105 // common part of all ctors
106 void wxNotebook::Init()
109 m_bOwnsImageList
= FALSE
;
113 // default for dynamic class
114 wxNotebook::wxNotebook()
119 // the same arguments as for wxControl
120 wxNotebook::wxNotebook(wxWindow
*parent
,
125 const wxString
& name
)
129 Create(parent
, id
, pos
, size
, style
, name
);
133 bool wxNotebook::Create(wxWindow
*parent
,
138 const wxString
& name
)
141 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
145 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
146 m_foregroundColour
= *wxBLACK
;
149 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
151 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
153 if (m_windowStyle
& wxCLIP_CHILDREN
)
154 tabStyle
|= WS_CLIPCHILDREN
;
155 if ( m_windowStyle
& wxTC_MULTILINE
)
156 tabStyle
|= TCS_MULTILINE
;
157 if ( m_windowStyle
& wxBORDER
)
158 tabStyle
&= WS_BORDER
;
159 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
160 tabStyle
|= TCS_FIXEDWIDTH
;
161 if (m_windowStyle
& wxNB_BOTTOM
)
162 tabStyle
|= TCS_RIGHT
;
163 if (m_windowStyle
& wxNB_LEFT
)
164 tabStyle
|= TCS_VERTICAL
;
165 if (m_windowStyle
& wxNB_RIGHT
)
166 tabStyle
|= TCS_VERTICAL
|TCS_RIGHT
;
169 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL
,
170 this, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
,
176 // Not all compilers recognise SetWindowFont
177 ::SendMessage(GetHwnd(), WM_SETFONT
,
178 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
), TRUE
);
181 if ( parent
!= NULL
)
182 parent
->AddChild(this);
190 wxNotebook::~wxNotebook()
192 if (m_bOwnsImageList
) delete m_pImageList
;
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 if (m_bOwnsImageList
) delete m_pImageList
;
282 m_pImageList
= imageList
;
283 m_bOwnsImageList
= FALSE
;
284 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
287 void wxNotebook::AssignImageList(wxImageList
* imageList
)
289 SetImageList(imageList
);
290 m_bOwnsImageList
= TRUE
;
293 // ----------------------------------------------------------------------------
294 // wxNotebook size settings
295 // ----------------------------------------------------------------------------
297 void wxNotebook::SetPageSize(const wxSize
& size
)
299 // transform the page size into the notebook size
306 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
309 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
312 void wxNotebook::SetPadding(const wxSize
& padding
)
314 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
317 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
319 void wxNotebook::SetTabSize(const wxSize
& sz
)
321 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
324 // ----------------------------------------------------------------------------
325 // wxNotebook operations
326 // ----------------------------------------------------------------------------
328 // remove one page from the notebook
329 bool wxNotebook::DeletePage(int nPage
)
331 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
333 if ( m_nSelection
== nPage
) {
334 // advance selection backwards - the page being deleted shouldn't be left
336 AdvanceSelection(FALSE
);
339 TabCtrl_DeleteItem(m_hwnd
, nPage
);
341 delete m_aPages
[nPage
];
342 m_aPages
.Remove(nPage
);
344 if ( m_aPages
.IsEmpty() ) {
345 // no selection if the notebook became empty
349 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
355 // remove one page from the notebook, without deleting
356 bool wxNotebook::RemovePage(int nPage
)
358 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
360 TabCtrl_DeleteItem(m_hwnd
, nPage
);
362 m_aPages
.Remove(nPage
);
364 if ( m_aPages
.IsEmpty() )
367 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
373 bool wxNotebook::DeleteAllPages()
375 int nPageCount
= GetPageCount();
377 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
378 delete m_aPages
[nPage
];
382 TabCtrl_DeleteAllItems(m_hwnd
);
389 // add a page to the notebook
390 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
391 const wxString
& strText
,
395 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
398 // same as AddPage() but does it at given position
399 bool wxNotebook::InsertPage(int nPage
,
400 wxNotebookPage
*pPage
,
401 const wxString
& strText
,
405 wxASSERT( pPage
!= NULL
);
406 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
408 // do add the tab to the control
410 // init all fields to 0
412 memset(&tcItem
, 0, sizeof(tcItem
));
416 tcItem
.mask
|= TCIF_IMAGE
;
417 tcItem
.iImage
= imageId
;
420 if ( !strText
.IsEmpty() )
422 tcItem
.mask
|= TCIF_TEXT
;
423 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
426 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
427 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
432 // if the inserted page is before the selected one, we must update the
433 // index of the selected page
434 if ( nPage
<= m_nSelection
)
436 // one extra page added
440 // save the pointer to the page
441 m_aPages
.Insert(pPage
, nPage
);
443 // don't show pages by default (we'll need to adjust their size first)
444 HWND hwnd
= GetWinHwnd(pPage
);
445 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
447 // this updates internal flag too - otherwise it will get out of sync
450 // fit the notebook page to the tab control's display area
452 rc
.left
= rc
.top
= 0;
453 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
454 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
455 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
457 // some page should be selected: either this one or the first one if there is
458 // still no selection
462 else if ( m_nSelection
== -1 )
466 SetSelection(selNew
);
471 // ----------------------------------------------------------------------------
472 // wxNotebook callbacks
473 // ----------------------------------------------------------------------------
475 void wxNotebook::OnSize(wxSizeEvent
& event
)
477 // fit the notebook page to the tab control's display area
479 rc
.left
= rc
.top
= 0;
480 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
482 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
484 int width
= rc
.right
- rc
.left
,
485 height
= rc
.bottom
- rc
.top
;
486 size_t nCount
= m_aPages
.Count();
487 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
488 wxNotebookPage
*pPage
= m_aPages
[nPage
];
489 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
495 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
497 // is it our tab control?
498 if ( event
.GetEventObject() == this )
500 int sel
= event
.GetOldSelection();
502 m_aPages
[sel
]->Show(FALSE
);
504 sel
= event
.GetSelection();
507 wxNotebookPage
*pPage
= m_aPages
[sel
];
515 // we want to give others a chance to process this message as well
519 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
521 // this function is only called when the focus is explicitly set (i.e. from
522 // the program) to the notebook - in this case we don't need the
523 // complicated OnNavigationKey() logic because the programmer knows better
526 // set focus to the currently selected page if any
527 if ( m_nSelection
!= -1 )
528 m_aPages
[m_nSelection
]->SetFocus();
533 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
535 if ( event
.IsWindowChange() ) {
537 AdvanceSelection(event
.GetDirection());
540 // we get this event in 2 cases
542 // a) one of our pages might have generated it because the user TABbed
543 // out from it in which case we should propagate the event upwards and
544 // our parent will take care of setting the focus to prev/next sibling
548 // b) the parent panel wants to give the focus to us so that we
549 // forward it to our selected page. We can't deal with this in
550 // OnSetFocus() because we don't know which direction the focus came
551 // from in this case and so can't choose between setting the focus to
552 // first or last panel child
554 wxWindow
*parent
= GetParent();
555 if ( event
.GetEventObject() == parent
)
557 // no, it doesn't come from child, case (b): forward to a page
558 if ( m_nSelection
!= -1 )
560 // so that the page knows that the event comes from it's parent
561 // and is being propagated downwards
562 event
.SetEventObject(this);
564 wxWindow
*page
= m_aPages
[m_nSelection
];
565 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
569 //else: page manages focus inside it itself
573 // we have no pages - still have to give focus to _something_
579 // it comes from our child, case (a), pass to the parent
581 event
.SetCurrentFocus(this);
582 parent
->GetEventHandler()->ProcessEvent(event
);
588 // ----------------------------------------------------------------------------
589 // wxNotebook base class virtuals
590 // ----------------------------------------------------------------------------
592 // override these 2 functions to do nothing: everything is done in OnSize
594 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
596 // don't set the sizes of the pages - their correct size is not yet known
597 wxControl::SetConstraintSizes(FALSE
);
600 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
605 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
607 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
609 NMHDR
* hdr
= (NMHDR
*)lParam
;
610 switch ( hdr
->code
) {
612 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
615 case TCN_SELCHANGING
:
616 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
620 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
623 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
624 event
.SetOldSelection(m_nSelection
);
625 event
.SetEventObject(this);
626 event
.SetInt(idCtrl
);
628 bool processed
= GetEventHandler()->ProcessEvent(event
);
629 *result
= !event
.IsAllowed();
633 // ----------------------------------------------------------------------------
634 // wxNotebook helper functions
635 // ----------------------------------------------------------------------------
637 // generate the page changing and changed events, hide the currently active
638 // panel and show the new one
639 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
641 // MT-FIXME should use a real semaphore
642 static bool s_bInsideChangePage
= FALSE
;
644 // when we call ProcessEvent(), our own OnSelChange() is called which calls
645 // this function - break the infinite loop
646 if ( s_bInsideChangePage
)
649 // it's not an error (the message may be generated by the tab control itself)
650 // and it may happen - just do nothing
651 if ( nSel
== nOldSel
)
654 s_bInsideChangePage
= TRUE
;
656 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
657 event
.SetSelection(nSel
);
658 event
.SetOldSelection(nOldSel
);
659 event
.SetEventObject(this);
660 if ( ProcessEvent(event
) && !event
.IsAllowed() )
662 // program doesn't allow the page change
663 s_bInsideChangePage
= FALSE
;
667 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
670 s_bInsideChangePage
= FALSE
;