1 /////////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     implementation of wxNotebook 
   4 // Author:      David Webster 
   8 // Copyright:   (c) David Webster 
   9 // Licence:     wxWindows licence 
  10 /////////////////////////////////////////////////////////////////////////////// 
  12 // For compilers that support precompilation, includes "wx.h". 
  13 #include "wx/wxprec.h" 
  20   #include "wx/dcclient.h" 
  21   #include "wx/string.h" 
  22   #include "wx/settings.h" 
  26 #include  "wx/imaglist.h" 
  28 #include  "wx/control.h" 
  29 #include  "wx/notebook.h" 
  31 #include  "wx/os2/private.h" 
  33 // ---------------------------------------------------------------------------- 
  35 // ---------------------------------------------------------------------------- 
  37 // check that the page index is valid 
  38 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount())) 
  41 #define m_hWnd    (HWND)GetHWND() 
  43 // ---------------------------------------------------------------------------- 
  45 // ---------------------------------------------------------------------------- 
  47 // ---------------------------------------------------------------------------- 
  49 // ---------------------------------------------------------------------------- 
  51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
) 
  52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
) 
  54 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
) 
  55     EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
) 
  56     EVT_SIZE(wxNotebook::OnSize
) 
  57     EVT_SET_FOCUS(wxNotebook::OnSetFocus
) 
  58     EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
) 
  61 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
) 
  62 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
) 
  64 // ============================================================================ 
  66 // ============================================================================ 
  68 // ---------------------------------------------------------------------------- 
  69 // wxNotebook construction 
  70 // ---------------------------------------------------------------------------- 
  73 // Common part of all ctors 
  75 void wxNotebook::Init() 
  80 } // end of wxNotebook::Init 
  83 // Default for dynamic class 
  85 wxNotebook::wxNotebook() 
  88 } // end of wxNotebook::wxNotebook 
  91 // The same arguments as for wxControl 
  93 wxNotebook::wxNotebook( 
  99 , const wxString
&                   rsName
 
 110 } // end of wxNotebook::wxNotebook 
 115 bool wxNotebook::Create( 
 118 , const wxPoint
&                    rPos
 
 119 , const wxSize
&                     rSize
 
 121 , const wxString
&                   rsName
 
 127     if (!CreateControl( pParent
 
 138     // Notebook, so explicitly specify 0 as last parameter 
 140     if (!OS2CreateControl( "NOTEBOOK" 
 144                           ,lStyle 
| wxTAB_TRAVERSAL
 
 148     SetBackgroundColour(wxColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
))); 
 150 } // end of wxNotebook::Create 
 152 WXDWORD 
wxNotebook::OS2GetStyle ( 
 154 , WXDWORD
*                          pdwExstyle
 
 157     WXDWORD                         dwTabStyle 
= wxControl::OS2GetStyle( lStyle
 
 161     dwTabStyle 
|= WS_TABSTOP 
| BKS_SOLIDBIND 
| BKS_ROUNDEDTABS 
| BKS_TABTEXTCENTER
; 
 163     if (lStyle 
& wxNB_BOTTOM
) 
 164         dwTabStyle 
|= BKS_MAJORTABBOTTOM 
| BKS_BACKPAGESBL
; 
 165     else if (lStyle 
& wxNB_RIGHT
) 
 166         dwTabStyle 
|= BKS_MAJORTABRIGHT 
| BKS_BACKPAGESBR
; 
 167     else if (lStyle 
& wxNB_LEFT
) 
 168         dwTabStyle 
|= BKS_MAJORTABLEFT 
| BKS_BACKPAGESTL
; 
 169     else // default to top 
 170         dwTabStyle 
|= BKS_MAJORTABTOP 
| BKS_BACKPAGESTR
; 
 178         // Note that we never want to have the default WS_EX_CLIENTEDGE style 
 179         // as it looks too ugly for the notebooks 
 184 } // end of wxNotebook::OS2GetStyle 
 186 // ---------------------------------------------------------------------------- 
 187 // wxNotebook accessors 
 188 // ---------------------------------------------------------------------------- 
 190 size_t wxNotebook::GetPageCount() const 
 195     wxASSERT((int)m_pages
.Count() == (int)::WinSendMsg(GetHWND(), BKM_QUERYPAGECOUNT
, (MPARAM
)0, (MPARAM
)BKA_END
)); 
 196     return m_pages
.Count(); 
 197 } // end of wxNotebook::GetPageCount 
 199 int wxNotebook::GetRowCount() const 
 201     return (int)::WinSendMsg( GetHWND() 
 206 } // end of wxNotebook::GetRowCount 
 208 int wxNotebook::SetSelection( 
 212     wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") ); 
 214     if (nPage 
!= (size_t)m_nSelection
) 
 216         wxNotebookEvent             
vEvent( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
 
 220         vEvent
.SetSelection(nPage
); 
 221         vEvent
.SetOldSelection(m_nSelection
); 
 222         vEvent
.SetEventObject(this); 
 223         if (!GetEventHandler()->ProcessEvent(vEvent
) || vEvent
.IsAllowed()) 
 227             // Program allows the page change 
 229             vEvent
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
); 
 230             GetEventHandler()->ProcessEvent(vEvent
); 
 232             ::WinSendMsg( GetHWND() 
 234                          ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 239     m_nSelection 
= nPage
; 
 241 } // end of wxNotebook::SetSelection 
 243 bool wxNotebook::SetPageText( 
 245 , const wxString
&                   rsStrText
 
 248     wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") ); 
 249     return (bool)::WinSendMsg( m_hWnd
 
 251                               ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 252                               ,MPFROMP((PSZ
)rsStrText
.c_str()) 
 254 } // end of wxNotebook::SetPageText 
 256 wxString 
wxNotebook::GetPageText ( 
 265     wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") ); 
 267     memset(&vBookText
, '\0', sizeof(BOOKTEXT
)); 
 268     vBookText
.textLen 
= 0; // This will get the length 
 269     ulRc 
= LONGFROMMR(::WinSendMsg( m_hWnd
 
 271                                    ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 274     if (ulRc 
== (ULONG
)BOOKERR_INVALID_PARAMETERS 
|| ulRc 
== 0L) 
 276         if (ulRc 
== (ULONG
)BOOKERR_INVALID_PARAMETERS
) 
 278             wxLogError(wxT("Invalid Page Id for page text querry.")); 
 280         return wxEmptyString
; 
 282     vBookText
.textLen 
= ulRc 
+ 1; // To get the null terminator 
 283     vBookText
.pString 
= zBuf
; 
 286     // Now get the actual text 
 288     ulRc 
= LONGFROMMR(::WinSendMsg( m_hWnd
 
 290                                    ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 293     if (ulRc 
== (ULONG
)BOOKERR_INVALID_PARAMETERS 
|| ulRc 
== 0L) 
 295         return wxEmptyString
; 
 300     vBookText
.pString
[ulRc
] = '\0'; 
 301     sStr 
= vBookText
.pString
; 
 303 } // end of wxNotebook::GetPageText 
 305 int wxNotebook::GetPageImage ( 
 309     wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") ); 
 312     // For OS/2 just return the page 
 315 } // end of wxNotebook::GetPageImage 
 317 bool wxNotebook::SetPageImage ( 
 322     wxBitmap
*                       pBitmap 
= (wxBitmap
*)m_imageList
->GetBitmap(nImage
); 
 324     return (bool)::WinSendMsg( GetHWND() 
 326                               ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 327                               ,(MPARAM
)pBitmap
->GetHBITMAP() 
 329 } // end of wxNotebook::SetPageImage 
 331 void wxNotebook::SetImageList ( 
 332   wxImageList
*                      pImageList
 
 336     // Does not really do anything yet, but at least we need to 
 337     // update the base class. 
 339     wxNotebookBase::SetImageList(pImageList
); 
 340 } // end of wxNotebook::SetImageList 
 342 // ---------------------------------------------------------------------------- 
 343 // wxNotebook size settings 
 344 // ---------------------------------------------------------------------------- 
 345 void wxNotebook::SetPageSize ( 
 352     // Transform the page size into the notebook size 
 354     vRect
.xLeft   
= vRect
.yTop 
= 0; 
 355     vRect
.xRight  
= rSize
.x
; 
 356     vRect
.yBottom 
= rSize
.y
; 
 362     SetSize( vRect
.xRight 
- vRect
.xLeft
 
 363             ,vRect
.yBottom 
- vRect
.yTop
 
 365 } // end of wxNotebook::SetPageSize 
 367 void wxNotebook::SetPadding ( 
 368   const wxSize
&                     WXUNUSED(rPadding
) 
 372     // No padding in OS/2 
 374 } // end of wxNotebook::SetPadding 
 376 void wxNotebook::SetTabSize ( 
 380     ::WinSendMsg( GetHWND() 
 382                  ,MPFROM2SHORT( (USHORT
)rSize
.x
 
 385                  ,(MPARAM
)BKA_MAJORTAB
 
 387 } // end of wxNotebook::SetTabSize 
 389 // ---------------------------------------------------------------------------- 
 390 // wxNotebook operations 
 391 // ---------------------------------------------------------------------------- 
 394 // Remove one page from the notebook, without deleting 
 396 wxNotebookPage
* wxNotebook::DoRemovePage ( 
 400     wxNotebookPage
*                 pPageRemoved 
= wxNotebookBase::DoRemovePage(nPage
); 
 405     ::WinSendMsg( GetHWND() 
 407                  ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 410     if (m_pages
.IsEmpty()) 
 413         // No selection any more, the notebook becamse empty 
 417     else // notebook still not empty 
 420         // Change the selected page if it was deleted or became invalid 
 424         if (m_nSelection 
== (int)GetPageCount()) 
 427             // Last page deleted, make the new last page the new selection 
 429             nSelNew 
= m_nSelection 
- 1; 
 431         else if (nPage 
<= (size_t)m_nSelection
) 
 434             // We must show another page, even if it has the same index 
 436             nSelNew 
= m_nSelection
; 
 438         else // nothing changes for the currently selected page 
 443             // We still must refresh the current page: this needs to be done 
 444             // for some unknown reason if the tab control shows the up-down 
 445             // control (i.e. when there are too many pages) -- otherwise after 
 446             // deleting a page nothing at all is shown 
 448             m_pages
[m_nSelection
]->Refresh(); 
 454             // m_nSelection must be always valid so reset it before calling 
 458             SetSelection(nSelNew
); 
 462 } // end of wxNotebook::DoRemovePage 
 467 bool wxNotebook::DeleteAllPages() 
 469     int                             nPageCount 
= GetPageCount(); 
 472     for (nPage 
= 0; nPage 
< nPageCount
; nPage
++) 
 473         delete m_pages
[nPage
]; 
 475     ::WinSendMsg( GetHWND() 
 482 } // end of wxNotebook::DeleteAllPages 
 485 // Add a page to the notebook 
 487 bool wxNotebook::AddPage ( 
 488   wxNotebookPage
*                   pPage
 
 489 , const wxString
&                   rStrText
 
 494     return InsertPage( GetPageCount() 
 500 } // end of wxNotebook::AddPage 
 503 // Same as AddPage() but does it at given position 
 505 bool wxNotebook::InsertPage ( 
 507 , wxNotebookPage
*                   pPage
 
 508 , const wxString
&                   rsStrText
 
 515     wxASSERT( pPage 
!= NULL 
); 
 516     wxCHECK( IS_VALID_PAGE(nPage
) || nPage 
== GetPageCount(), FALSE 
); 
 519     // Under OS/2 we can only insert FIRST, LAST, NEXT or PREV.  Requires 
 520     // two different calls to the API.  Page 1 uses the BKA_FIRST.  Subsequent 
 521     // pages use the previous page ID coupled with a BKA_NEXT call.  Unlike 
 522     // Windows, OS/2 uses an internal Page ID to ID the pages. 
 524     // OS/2 also has a nice auto-size feature that automatically sizes the 
 525     // the attached window so we don't have to worry about the size of the 
 526     // window on the page. 
 530         ulApiPage 
= LONGFROMMR(::WinSendMsg( GetHWND() 
 533                                             ,MPFROM2SHORT(BKA_AUTOPAGESIZE 
| BKA_MAJOR
, BKA_FIRST
) 
 540             vError 
= ::WinGetLastError(vHabmain
); 
 541             sError 
= wxPMErrorToStr(vError
); 
 544         m_alPageId
.Insert((long)ulApiPage
, nPage
); 
 548         ulApiPage 
= LONGFROMMR(::WinSendMsg( GetHWND() 
 550                                             ,MPFROMLONG((ULONG
)m_alPageId
[nPage 
- 1]) 
 551                                             ,MPFROM2SHORT(BKA_AUTOPAGESIZE 
| BKA_MAJOR
, BKA_NEXT
) 
 558             vError 
= ::WinGetLastError(vHabmain
); 
 559             sError 
= wxPMErrorToStr(vError
); 
 562         m_alPageId
.Insert((long)ulApiPage
, nPage
); 
 566     // Associate a window handle with the page 
 570         if (!::WinSendMsg( GetHWND() 
 571                           ,BKM_SETPAGEWINDOWHWND
 
 572                           ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 573                           ,MPFROMHWND(pPage
->GetHWND()) 
 578     // If the inserted page is before the selected one, we must update the 
 579     // index of the selected page 
 581     if (nPage 
<= (size_t)m_nSelection
) 
 584         // One extra page added 
 592         // Save the pointer to the page 
 594         m_pages
.Insert( pPage
 
 600     // Now set TAB dimenstions 
 603     wxWindowDC                      
vDC(this); 
 607     vDC
.GetTextExtent(rsStrText
, &nTextX
, &nTextY
); 
 609     nTextX  
= (wxCoord
)(nTextX 
* 1.3); 
 610     if (nTextX 
> m_nTabSize
) 
 613         ::WinSendMsg( GetHWND() 
 615                      ,MPFROM2SHORT((USHORT
)m_nTabSize
, (USHORT
)nTextY
) 
 616                      ,(MPARAM
)BKA_MAJORTAB
 
 620     // Now set any TAB text 
 622     if (!rsStrText
.IsEmpty()) 
 624         if (!SetPageText( nPage
 
 631     // Now set any TAB bitmap image 
 635         if (!SetPageImage( nPage
 
 644         // Don't show pages by default (we'll need to adjust their size first) 
 646         HWND                        hWnd 
= GetWinHwnd(pPage
); 
 648         WinSetWindowULong( hWnd
 
 650                           ,WinQueryWindowULong( hWnd
 
 656         // This updates internal flag too - otherwise it will get out of sync 
 662     // Some page should be selected: either this one or the first one if there is 
 663     // still no selection 
 669     else if ( m_nSelection 
== -1 ) 
 673         SetSelection(nSelNew
); 
 675 } // end of wxNotebook::InsertPage 
 677 // ---------------------------------------------------------------------------- 
 678 // wxNotebook callbacks 
 679 // ---------------------------------------------------------------------------- 
 680 void wxNotebook::OnSize( 
 685     int                             nCount 
= (int)m_pages
.Count(); 
 687     for (nPage 
= 0; nPage 
< nCount
; nPage
++) 
 689         if (m_nSelection 
== nPage
) 
 690             m_pages
[nPage
]->Refresh(); 
 692             ::WinSetWindowPos(m_pages
[nPage
]->GetHWND() 
 699 } // end of wxNotebook::OnSize 
 701 void wxNotebook::OnSelChange ( 
 702   wxNotebookEvent
&                  rEvent
 
 706     // Is it our tab control? 
 708     if (rEvent
.GetEventObject() == this) 
 710         int                         nPageCount 
= GetPageCount(); 
 712         ULONG                       ulOS2Sel 
= (ULONG
)rEvent
.GetOldSelection(); 
 715         for (nSel 
= 0; nSel 
< nPageCount
; nSel
++) 
 717             if (ulOS2Sel 
== (ULONG
)m_alPageId
[nSel
]) 
 727         m_pages
[nSel
]->Show(FALSE
); 
 729         ulOS2Sel 
= (ULONG
)rEvent
.GetSelection(); 
 733         for (nSel 
= 0; nSel 
< nPageCount
; nSel
++) 
 735             if (ulOS2Sel 
== (ULONG
)m_alPageId
[nSel
]) 
 745         wxNotebookPage
*         pPage 
= m_pages
[nSel
]; 
 752     // We want to give others a chance to process this message as well 
 755 } // end of wxNotebook::OnSelChange 
 757 void wxNotebook::OnSetFocus ( 
 762     // This function is only called when the focus is explicitly set (i.e. from 
 763     // the program) to the notebook - in this case we don't need the 
 764     // complicated OnNavigationKey() logic because the programmer knows better 
 767     // set focus to the currently selected page if any 
 769     if (m_nSelection 
!= -1) 
 770         m_pages
[m_nSelection
]->SetFocus(); 
 772 } // end of wxNotebook::OnSetFocus 
 774 void wxNotebook::OnNavigationKey ( 
 775   wxNavigationKeyEvent
&             rEvent
 
 778     if (rEvent
.IsWindowChange()) 
 783         AdvanceSelection(rEvent
.GetDirection()); 
 788         // We get this event in 2 cases 
 790         // a) one of our pages might have generated it because the user TABbed 
 791         // out from it in which case we should propagate the event upwards and 
 792         // our parent will take care of setting the focus to prev/next sibling 
 796         // b) the parent panel wants to give the focus to us so that we 
 797         // forward it to our selected page. We can't deal with this in 
 798         // OnSetFocus() because we don't know which direction the focus came 
 799         // from in this case and so can't choose between setting the focus to 
 800         // first or last panel child 
 802         wxWindow
*                   pParent 
= GetParent(); 
 804         if (rEvent
.GetEventObject() == pParent
) 
 807             // No, it doesn't come from child, case (b): forward to a page 
 809             if (m_nSelection 
!= -1) 
 812                 // So that the page knows that the event comes from it's parent 
 813                 // and is being propagated downwards 
 815                 rEvent
.SetEventObject(this); 
 817                 wxWindow
*           pPage 
= m_pages
[m_nSelection
]; 
 819                 if (!pPage
->GetEventHandler()->ProcessEvent(rEvent
)) 
 823                 //else: page manages focus inside it itself 
 828                 // We have no pages - still have to give focus to _something_ 
 836             // It comes from our child, case (a), pass to the parent 
 840                 rEvent
.SetCurrentFocus(this); 
 841                 pParent
->GetEventHandler()->ProcessEvent(rEvent
); 
 845 } // end of wxNotebook::OnNavigationKey 
 847 // ---------------------------------------------------------------------------- 
 848 // wxNotebook base class virtuals 
 849 // ---------------------------------------------------------------------------- 
 852 // Override these 2 functions to do nothing: everything is done in OnSize 
 854 void wxNotebook::SetConstraintSizes( 
 855   bool                              WXUNUSED(bRecurse
) 
 859     // Don't set the sizes of the pages - their correct size is not yet known 
 861     wxControl::SetConstraintSizes(FALSE
); 
 862 } // end of wxNotebook::SetConstraintSizes 
 864 bool wxNotebook::DoPhase ( 
 869 } // end of wxNotebook::DoPhase 
 871 // ---------------------------------------------------------------------------- 
 872 // wxNotebook Windows message handlers 
 873 // ---------------------------------------------------------------------------- 
 874 bool wxNotebook::OS2OnScroll ( 
 882     // Don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the 
 887     return wxNotebookBase::OS2OnScroll( nOrientation
 
 892 } // end of wxNotebook::OS2OnScroll 
 894 #endif // wxUSE_NOTEBOOK