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   #include "wx/msw/gnuwin32/extra.h" 
  49 #if !defined(__GNUWIN32__) || 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 #if !USE_SHARED_LIBRARIES 
  68   BEGIN_EVENT_TABLE(wxNotebook
, wxControl
) 
  69     EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
) 
  71     EVT_SIZE(wxNotebook::OnSize
) 
  72     EVT_ERASE_BACKGROUND(wxNotebook::OnEraseBackground
) 
  73     EVT_SET_FOCUS(wxNotebook::OnSetFocus
) 
  74     EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
) 
  77   IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
) 
  78   IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
) 
  81 // ============================================================================ 
  83 // ============================================================================ 
  85 // ---------------------------------------------------------------------------- 
  86 // wxNotebook construction 
  87 // ---------------------------------------------------------------------------- 
  89 // common part of all ctors 
  90 void wxNotebook::Init() 
  96 // default for dynamic class 
  97 wxNotebook::wxNotebook() 
 102 // the same arguments as for wxControl 
 103 wxNotebook::wxNotebook(wxWindow 
*parent
, 
 108                        const wxString
& name
) 
 112   Create(parent
, id
, pos
, size
, style
, name
); 
 116 bool wxNotebook::Create(wxWindow 
*parent
, 
 121                         const wxString
& name
) 
 127   m_windowId 
= id 
== -1 ? NewControlId() : id
; 
 130   m_backgroundColour 
= wxColour(GetSysColor(COLOR_BTNFACE
)); 
 131   m_foregroundColour 
= *wxBLACK 
; 
 134   m_windowStyle 
= style 
| wxTAB_TRAVERSAL
; 
 136   long tabStyle 
= WS_CHILD 
| WS_VISIBLE 
| WS_TABSTOP 
| TCS_TABS 
| WS_CLIPCHILDREN
; 
 137   if ( m_windowStyle 
& wxTC_MULTILINE 
) 
 138     tabStyle 
|= TCS_MULTILINE
; 
 139   if ( m_windowStyle 
& wxBORDER 
) 
 140     tabStyle 
&= WS_BORDER
; 
 141   if (m_windowStyle 
& wxNB_FIXEDWIDTH
) 
 142     tabStyle 
|= TCS_FIXEDWIDTH 
; 
 144   // create the tab control. 
 145   m_hWnd 
= (WXHWND
)CreateWindowEx
 
 148       WC_TABCONTROL
,                          // class name for the tab control 
 151       pos
.x
, pos
.y
, size
.x
, size
.y
,           // size and position 
 152       (HWND
)parent
->GetHWND(),                // parent window 
 153       (HMENU
)m_windowId
,                      // child id 
 154       wxGetInstance(),                        // current instance 
 155       NULL                                    
// no class data 
 159     wxLogSysError("Can't create the notebook control"); 
 163   // Not all compilers recognise SetWindowFont 
 164 //  SetWindowFont((HWND)m_hwnd, ::GetStockObject(DEFAULT_GUI_FONT), FALSE); 
 165   ::SendMessage((HWND
) m_hwnd
, WM_SETFONT
, 
 166                   (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
),TRUE
); 
 169   if ( parent 
!= NULL 
)  
 170     parent
->AddChild(this); 
 178 wxNotebook::~wxNotebook() 
 182 // ---------------------------------------------------------------------------- 
 183 // wxNotebook accessors 
 184 // ---------------------------------------------------------------------------- 
 185 int wxNotebook::GetPageCount() const 
 188   wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) ); 
 190   return m_aPages
.Count(); 
 193 int wxNotebook::GetRowCount() const 
 195   return TabCtrl_GetRowCount(m_hwnd
); 
 198 int wxNotebook::SetSelection(int nPage
) 
 200   wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" ); 
 202   ChangePage(m_nSelection
, nPage
); 
 204   return TabCtrl_SetCurSel(m_hwnd
, nPage
); 
 207 void wxNotebook::AdvanceSelection(bool bForward
) 
 209   int nSel 
= GetSelection(); 
 210   int nMax 
= GetPageCount() - 1; 
 212     SetSelection(nSel 
== nMax 
? 0 : nSel 
+ 1); 
 214     SetSelection(nSel 
== 0 ? nMax 
: nSel 
- 1); 
 217 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
) 
 219   wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" ); 
 222   tcItem
.mask 
= TCIF_TEXT
; 
 223   tcItem
.pszText 
= (char *)strText
.c_str(); 
 225   return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0; 
 228 wxString 
wxNotebook::GetPageText(int nPage
) const 
 230   wxCHECK_MSG( IS_VALID_PAGE(nPage
), "", "notebook page out of range" ); 
 234   tcItem
.mask 
= TCIF_TEXT
; 
 235   tcItem
.pszText 
= buf
; 
 236   tcItem
.cchTextMax 
= WXSIZEOF(buf
); 
 239   if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ) 
 240     str 
= tcItem
.pszText
; 
 245 int wxNotebook::GetPageImage(int nPage
) const 
 247   wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" ); 
 250   tcItem
.mask 
= TCIF_IMAGE
; 
 252   return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage 
: -1; 
 255 bool wxNotebook::SetPageImage(int nPage
, int nImage
) 
 257   wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" ); 
 260   tcItem
.mask 
= TCIF_IMAGE
; 
 261   tcItem
.iImage 
= nImage
; 
 263   return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0; 
 266 void wxNotebook::SetImageList(wxImageList
* imageList
) 
 268   m_pImageList 
= imageList
; 
 269   TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST()); 
 272 // ---------------------------------------------------------------------------- 
 273 // wxNotebook operations 
 274 // ---------------------------------------------------------------------------- 
 276 // remove one page from the notebook 
 277 bool wxNotebook::DeletePage(int nPage
) 
 279   wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" ); 
 281   TabCtrl_DeleteItem(m_hwnd
, nPage
); 
 283   delete m_aPages
[nPage
]; 
 284   m_aPages
.Remove(nPage
); 
 289 // remove one page from the notebook, without deleting 
 290 bool wxNotebook::RemovePage(int nPage
) 
 292   wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" ); 
 294   TabCtrl_DeleteItem(m_hwnd
, nPage
); 
 296   m_aPages
.Remove(nPage
); 
 302 bool wxNotebook::DeleteAllPages() 
 304   TabCtrl_DeleteAllItems(m_hwnd
); 
 306   int nPageCount 
= GetPageCount(); 
 308   for ( nPage 
= 0; nPage 
< nPageCount
; nPage
++ ) 
 309     delete m_aPages
[nPage
]; 
 316 // add a page to the notebook 
 317 bool wxNotebook::AddPage(wxNotebookPage 
*pPage
, 
 318                          const wxString
& strText
, 
 322   return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
); 
 325 // same as AddPage() but does it at given position 
 326 bool wxNotebook::InsertPage(int nPage
, 
 327                             wxNotebookPage 
*pPage
, 
 328                             const wxString
& strText
, 
 332   wxASSERT( pPage 
!= NULL 
); 
 333   wxCHECK( IS_VALID_PAGE(nPage
) || nPage 
== GetPageCount(), FALSE 
); 
 335   // add the tab to the control 
 341     tcItem
.mask 
|= TCIF_IMAGE
; 
 342     tcItem
.iImage  
= imageId
; 
 347   if (!strText
.IsEmpty()) 
 349     tcItem
.mask    
|= TCIF_TEXT
; 
 350     tcItem
.pszText 
= (char *)strText
.c_str(); 
 353     tcItem
.pszText 
= (char *) NULL
; 
 355   if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) { 
 356     wxLogError("Can't create the notebook page '%s'.", strText
.c_str()); 
 360   // save the pointer to the page 
 361   m_aPages
.Insert(pPage
, nPage
); 
 363   // some page must be selected: either this one or the first one if there is  
 364   // still no selection 
 366     m_nSelection 
= nPage
; 
 367   else if ( m_nSelection 
== -1 ) 
 370   // don't show pages by default (we'll need to adjust their size first) 
 371   HWND hwnd 
= (HWND
)pPage
->GetHWND(); 
 372   SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
); 
 377 // ---------------------------------------------------------------------------- 
 378 // wxNotebook callbacks 
 379 // ---------------------------------------------------------------------------- 
 381 void wxNotebook::OnSize(wxSizeEvent
& event
) 
 383   // make sure the current page is shown and has focus (it's useful because all 
 384   // pages are created invisible initially) 
 385   if ( m_nSelection 
!= -1 ) { 
 386     wxNotebookPage 
*pPage 
= m_aPages
[m_nSelection
]; 
 391   // fit the notebook page to the tab control's display area 
 393   rc
.left 
= rc
.top 
= 0; 
 394   GetSize((int *)&rc
.right
, (int *)&rc
.bottom
); 
 396   TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
); 
 397   size_t nCount 
= m_aPages
.Count(); 
 398   for ( size_t nPage 
= 0; nPage 
< nCount
; nPage
++ ) { 
 399     wxNotebookPage 
*pPage 
= m_aPages
[nPage
]; 
 400     pPage
->SetSize(rc
.left
, rc
.top
, rc
.right 
- rc
.left
, rc
.bottom 
- rc
.top
); 
 401     if ( pPage
->GetAutoLayout() ) 
 408 void wxNotebook::OnSelChange(wxNotebookEvent
& event
) 
 410   // is it our tab control? 
 411   if ( event
.GetEventObject() == this ) 
 412     ChangePage(event
.GetOldSelection(), event
.GetSelection()); 
 414   // we want to give others a chance to process this message as well 
 418 void wxNotebook::OnSetFocus(wxFocusEvent
& event
) 
 420   // set focus to the currently selected page if any 
 421   if ( m_nSelection 
!= -1 ) 
 422     m_aPages
[m_nSelection
]->SetFocus(); 
 427 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
) 
 429   if ( event
.IsWindowChange() ) { 
 431     AdvanceSelection(event
.GetDirection()); 
 434     // pass to the parent 
 436       event
.SetCurrentFocus(this); 
 437       GetParent()->GetEventHandler()->ProcessEvent(event
); 
 442 // ---------------------------------------------------------------------------- 
 443 // wxNotebook base class virtuals 
 444 // ---------------------------------------------------------------------------- 
 446 // override these 2 functions to do nothing: everything is done in OnSize 
 448 void wxNotebook::SetConstraintSizes(bool /* recurse */) 
 450   // don't set the sizes of the pages - their correct size is not yet known 
 451   wxControl::SetConstraintSizes(FALSE
); 
 454 bool wxNotebook::DoPhase(int /* nPhase */) 
 459 void wxNotebook::Command(wxCommandEvent
& event
) 
 461   wxFAIL_MSG("wxNotebook::Command not implemented"); 
 464 bool wxNotebook::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
* result
) 
 466   wxNotebookEvent 
event(wxEVT_NULL
, m_windowId
); 
 468   NMHDR
* hdr 
= (NMHDR 
*)lParam
; 
 469   switch ( hdr
->code 
) { 
 471       event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
); 
 474     case TCN_SELCHANGING
: 
 475       event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
); 
 479       return wxControl::MSWNotify(wParam
, lParam
, result
); 
 482   event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
)); 
 483   event
.SetOldSelection(m_nSelection
); 
 484   event
.SetEventObject(this); 
 485   event
.SetInt(LOWORD(wParam
)); // ctrl id 
 487   bool processed 
= GetEventHandler()->ProcessEvent(event
); 
 488   *result 
= !event
.IsAllowed(); 
 492 // ---------------------------------------------------------------------------- 
 493 // wxNotebook helper functions 
 494 // ---------------------------------------------------------------------------- 
 496 // hide the currently active panel and show the new one 
 497 void wxNotebook::ChangePage(int nOldSel
, int nSel
) 
 499   // MT-FIXME should use a real semaphore 
 500   static bool s_bInsideChangePage 
= FALSE
; 
 502   // when we call ProcessEvent(), our own OnSelChange() is called which calls 
 503   // this function - break the infinite loop 
 504   if ( s_bInsideChangePage 
) 
 507   // it's not an error (the message may be generated by the tab control itself) 
 508   // and it may happen - just do nothing 
 509   if ( nSel 
== nOldSel 
) 
 512   s_bInsideChangePage 
= TRUE
; 
 514   wxNotebookEvent 
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
); 
 515   event
.SetSelection(nSel
); 
 516   event
.SetOldSelection(nOldSel
); 
 517   event
.SetEventObject(this); 
 518   if ( ProcessEvent(event
) && !event
.IsAllowed() ) 
 520     // program doesn't allow the page change 
 521     s_bInsideChangePage 
= FALSE
; 
 526     m_aPages
[nOldSel
]->Show(FALSE
); 
 528   wxNotebookPage 
*pPage 
= m_aPages
[nSel
]; 
 532   event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
); 
 536   s_bInsideChangePage 
= FALSE
; 
 539 void wxNotebook::OnEraseBackground(wxEraseEvent
& event
) 
 544 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH 
 546 void wxNotebook::SetTabSize(const wxSize
& sz
) 
 548     ::SendMessage((HWND
) GetHWND(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));