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"
27 #include "wx/string.h"
31 #include "wx/imaglist.h"
33 #include "wx/control.h"
34 #include "wx/notebook.h"
36 #include "wx/msw/private.h"
38 // Windows standard headers
40 #error "wxNotebook is only supported Windows 95 and above"
43 #include <windowsx.h> // for SetWindowFont
46 #ifdef __GNUWIN32_OLD__
47 #include "wx/msw/gnuwin32/extra.h"
51 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
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 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
89 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
90 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
92 EVT_SIZE(wxNotebook::OnSize
)
94 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
96 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
99 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
100 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
102 // ============================================================================
104 // ============================================================================
106 // ----------------------------------------------------------------------------
107 // wxNotebook construction
108 // ----------------------------------------------------------------------------
110 // common part of all ctors
111 void wxNotebook::Init()
117 // default for dynamic class
118 wxNotebook::wxNotebook()
123 // the same arguments as for wxControl
124 wxNotebook::wxNotebook(wxWindow
*parent
,
129 const wxString
& name
)
133 Create(parent
, id
, pos
, size
, style
, name
);
137 bool wxNotebook::Create(wxWindow
*parent
,
142 const wxString
& name
)
145 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
149 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
150 m_foregroundColour
= *wxBLACK
;
153 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
155 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
157 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
158 tabStyle
|= WS_CLIPSIBLINGS
;
159 if (m_windowStyle
& wxCLIP_CHILDREN
)
160 tabStyle
|= WS_CLIPCHILDREN
;
161 if ( m_windowStyle
& wxTC_MULTILINE
)
162 tabStyle
|= TCS_MULTILINE
;
163 if ( m_windowStyle
& wxBORDER
)
164 tabStyle
|= WS_BORDER
;
165 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
166 tabStyle
|= TCS_FIXEDWIDTH
;
167 if (m_windowStyle
& wxNB_BOTTOM
)
168 tabStyle
|= TCS_RIGHT
;
169 if (m_windowStyle
& wxNB_LEFT
)
170 tabStyle
|= TCS_VERTICAL
;
171 if (m_windowStyle
& wxNB_RIGHT
)
172 tabStyle
|= TCS_VERTICAL
|TCS_RIGHT
;
175 if ( !MSWCreateControl(WC_TABCONTROL
, tabStyle
, pos
, size
) )
180 // Not all compilers recognise SetWindowFont
181 ::SendMessage(GetHwnd(), WM_SETFONT
,
182 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
), TRUE
);
185 if ( parent
!= NULL
)
186 parent
->AddChild(this);
193 // ----------------------------------------------------------------------------
194 // wxNotebook accessors
195 // ----------------------------------------------------------------------------
197 int wxNotebook::GetPageCount() const
200 wxASSERT( (int)m_pages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
202 return m_pages
.Count();
205 int wxNotebook::GetRowCount() const
207 return TabCtrl_GetRowCount(m_hwnd
);
210 int wxNotebook::SetSelection(int nPage
)
212 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
214 ChangePage(m_nSelection
, nPage
);
216 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
219 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
221 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
224 tcItem
.mask
= TCIF_TEXT
;
225 tcItem
.pszText
= (wxChar
*)strText
.c_str();
227 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
230 wxString
wxNotebook::GetPageText(int nPage
) const
232 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
236 tcItem
.mask
= TCIF_TEXT
;
237 tcItem
.pszText
= buf
;
238 tcItem
.cchTextMax
= WXSIZEOF(buf
);
241 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
242 str
= tcItem
.pszText
;
247 int wxNotebook::GetPageImage(int nPage
) const
249 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
252 tcItem
.mask
= TCIF_IMAGE
;
254 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
257 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
259 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
262 tcItem
.mask
= TCIF_IMAGE
;
263 tcItem
.iImage
= nImage
;
265 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
268 void wxNotebook::SetImageList(wxImageList
* imageList
)
270 wxNotebookBase::SetImageList(imageList
);
274 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
278 // ----------------------------------------------------------------------------
279 // wxNotebook size settings
280 // ----------------------------------------------------------------------------
282 void wxNotebook::SetPageSize(const wxSize
& size
)
284 // transform the page size into the notebook size
291 TabCtrl_AdjustRect(GetHwnd(), TRUE
, &rc
);
294 SetSize(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
297 void wxNotebook::SetPadding(const wxSize
& padding
)
299 TabCtrl_SetPadding(GetHwnd(), padding
.x
, padding
.y
);
302 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
304 void wxNotebook::SetTabSize(const wxSize
& sz
)
306 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
309 // ----------------------------------------------------------------------------
310 // wxNotebook operations
311 // ----------------------------------------------------------------------------
313 // remove one page from the notebook
314 bool wxNotebook::DeletePage(int nPage
)
316 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
318 if ( m_nSelection
== nPage
) {
319 // advance selection backwards - the page being deleted shouldn't be left
321 AdvanceSelection(FALSE
);
324 TabCtrl_DeleteItem(m_hwnd
, nPage
);
326 delete m_pages
[nPage
];
327 m_pages
.RemoveAt(nPage
);
329 if ( m_pages
.IsEmpty() ) {
330 // no selection if the notebook became empty
334 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
340 // remove one page from the notebook, without deleting
341 wxNotebookPage
*wxNotebook::DoRemovePage(int nPage
)
343 wxNotebookPage
*pageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
347 TabCtrl_DeleteItem(m_hwnd
, nPage
);
349 if ( m_pages
.IsEmpty() )
352 m_nSelection
= TabCtrl_GetCurSel(m_hwnd
);
358 bool wxNotebook::DeleteAllPages()
360 int nPageCount
= GetPageCount();
362 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
363 delete m_pages
[nPage
];
367 TabCtrl_DeleteAllItems(m_hwnd
);
374 // add a page to the notebook
375 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
376 const wxString
& strText
,
380 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
383 // same as AddPage() but does it at given position
384 bool wxNotebook::InsertPage(int nPage
,
385 wxNotebookPage
*pPage
,
386 const wxString
& strText
,
390 wxASSERT( pPage
!= NULL
);
391 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
393 // do add the tab to the control
395 // init all fields to 0
397 memset(&tcItem
, 0, sizeof(tcItem
));
401 tcItem
.mask
|= TCIF_IMAGE
;
402 tcItem
.iImage
= imageId
;
405 if ( !strText
.IsEmpty() )
407 tcItem
.mask
|= TCIF_TEXT
;
408 tcItem
.pszText
= (wxChar
*)strText
.c_str(); // const_cast
411 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
412 wxLogError(wxT("Can't create the notebook page '%s'."), strText
.c_str());
417 // if the inserted page is before the selected one, we must update the
418 // index of the selected page
419 if ( nPage
<= m_nSelection
)
421 // one extra page added
425 // save the pointer to the page
426 m_pages
.Insert(pPage
, nPage
);
428 // don't show pages by default (we'll need to adjust their size first)
429 HWND hwnd
= GetWinHwnd(pPage
);
430 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
432 // this updates internal flag too - otherwise it will get out of sync
435 // fit the notebook page to the tab control's display area
437 rc
.left
= rc
.top
= 0;
438 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
439 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
440 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
442 // some page should be selected: either this one or the first one if there is
443 // still no selection
447 else if ( m_nSelection
== -1 )
451 SetSelection(selNew
);
456 // ----------------------------------------------------------------------------
457 // wxNotebook callbacks
458 // ----------------------------------------------------------------------------
460 void wxNotebook::OnSize(wxSizeEvent
& event
)
462 // fit the notebook page to the tab control's display area
464 rc
.left
= rc
.top
= 0;
465 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
467 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
469 int width
= rc
.right
- rc
.left
,
470 height
= rc
.bottom
- rc
.top
;
471 size_t nCount
= m_pages
.Count();
472 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
473 wxNotebookPage
*pPage
= m_pages
[nPage
];
474 pPage
->SetSize(rc
.left
, rc
.top
, width
, height
);
480 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
482 // is it our tab control?
483 if ( event
.GetEventObject() == this )
485 int sel
= event
.GetOldSelection();
487 m_pages
[sel
]->Show(FALSE
);
489 sel
= event
.GetSelection();
492 wxNotebookPage
*pPage
= m_pages
[sel
];
500 // we want to give others a chance to process this message as well
504 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
506 // this function is only called when the focus is explicitly set (i.e. from
507 // the program) to the notebook - in this case we don't need the
508 // complicated OnNavigationKey() logic because the programmer knows better
511 // set focus to the currently selected page if any
512 if ( m_nSelection
!= -1 )
513 m_pages
[m_nSelection
]->SetFocus();
518 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
520 if ( event
.IsWindowChange() ) {
522 AdvanceSelection(event
.GetDirection());
525 // we get this event in 2 cases
527 // a) one of our pages might have generated it because the user TABbed
528 // out from it in which case we should propagate the event upwards and
529 // our parent will take care of setting the focus to prev/next sibling
533 // b) the parent panel wants to give the focus to us so that we
534 // forward it to our selected page. We can't deal with this in
535 // OnSetFocus() because we don't know which direction the focus came
536 // from in this case and so can't choose between setting the focus to
537 // first or last panel child
539 wxWindow
*parent
= GetParent();
540 if ( event
.GetEventObject() == parent
)
542 // no, it doesn't come from child, case (b): forward to a page
543 if ( m_nSelection
!= -1 )
545 // so that the page knows that the event comes from it's parent
546 // and is being propagated downwards
547 event
.SetEventObject(this);
549 wxWindow
*page
= m_pages
[m_nSelection
];
550 if ( !page
->GetEventHandler()->ProcessEvent(event
) )
554 //else: page manages focus inside it itself
558 // we have no pages - still have to give focus to _something_
564 // it comes from our child, case (a), pass to the parent
566 event
.SetCurrentFocus(this);
567 parent
->GetEventHandler()->ProcessEvent(event
);
573 // ----------------------------------------------------------------------------
574 // wxNotebook base class virtuals
575 // ----------------------------------------------------------------------------
577 // override these 2 functions to do nothing: everything is done in OnSize
579 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse
))
581 // don't set the sizes of the pages - their correct size is not yet known
582 wxControl::SetConstraintSizes(FALSE
);
585 bool wxNotebook::DoPhase(int WXUNUSED(nPhase
))
590 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
592 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
594 NMHDR
* hdr
= (NMHDR
*)lParam
;
595 switch ( hdr
->code
) {
597 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
600 case TCN_SELCHANGING
:
601 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
605 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
608 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
609 event
.SetOldSelection(m_nSelection
);
610 event
.SetEventObject(this);
611 event
.SetInt(idCtrl
);
613 bool processed
= GetEventHandler()->ProcessEvent(event
);
614 *result
= !event
.IsAllowed();
618 // ----------------------------------------------------------------------------
619 // wxNotebook helper functions
620 // ----------------------------------------------------------------------------
622 // generate the page changing and changed events, hide the currently active
623 // panel and show the new one
624 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
626 // MT-FIXME should use a real semaphore
627 static bool s_bInsideChangePage
= FALSE
;
629 // when we call ProcessEvent(), our own OnSelChange() is called which calls
630 // this function - break the infinite loop
631 if ( s_bInsideChangePage
)
634 // it's not an error (the message may be generated by the tab control itself)
635 // and it may happen - just do nothing
636 if ( nSel
== nOldSel
)
639 s_bInsideChangePage
= TRUE
;
641 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
642 event
.SetSelection(nSel
);
643 event
.SetOldSelection(nOldSel
);
644 event
.SetEventObject(this);
645 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
647 // program doesn't allow the page change
648 s_bInsideChangePage
= FALSE
;
652 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
653 GetEventHandler()->ProcessEvent(event
);
655 s_bInsideChangePage
= FALSE
;
658 #endif // wxUSE_NOTEBOOK