1 /////////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/os2/notebook.cpp 
   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" 
  17 #include  "wx/notebook.h" 
  22     #include "wx/dcclient.h" 
  23     #include "wx/string.h" 
  24     #include "wx/settings.h" 
  27     #include  "wx/control.h" 
  30 #include  "wx/imaglist.h" 
  32 #include  "wx/os2/private.h" 
  34 // ---------------------------------------------------------------------------- 
  36 // ---------------------------------------------------------------------------- 
  38 // check that the page index is valid 
  39 #define IS_VALID_PAGE(nPage) (                                \ 
  40                                /* size_t is _always_ >= 0 */  \ 
  41                                /* ((nPage) >= 0) && */        \ 
  42                                ((nPage) < GetPageCount())     \ 
  46 #define m_hWnd    (HWND)GetHWND() 
  48 // ---------------------------------------------------------------------------- 
  50 // ---------------------------------------------------------------------------- 
  52 // ---------------------------------------------------------------------------- 
  54 // ---------------------------------------------------------------------------- 
  56 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
) 
  57     EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
) 
  58     EVT_SIZE(wxNotebook::OnSize
) 
  59     EVT_SET_FOCUS(wxNotebook::OnSetFocus
) 
  60     EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
) 
  63 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxBookCtrlBase
) 
  65 // ============================================================================ 
  67 // ============================================================================ 
  69 // ---------------------------------------------------------------------------- 
  70 // wxNotebook construction 
  71 // ---------------------------------------------------------------------------- 
  74 // Common part of all ctors 
  76 void wxNotebook::Init() 
  81 } // end of wxNotebook::Init 
  84 // Default for dynamic class 
  86 wxNotebook::wxNotebook() 
  89 } // end of wxNotebook::wxNotebook 
  92 // The same arguments as for wxControl 
  94 wxNotebook::wxNotebook( 
 100 , const wxString
&                   rsName
 
 111 } // end of wxNotebook::wxNotebook 
 116 bool wxNotebook::Create( wxWindow
*       pParent
, 
 121                          const wxString
& rsName 
) 
 123     if ( (lStyle 
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT 
) 
 128     if (!CreateControl( pParent
 
 139     // Notebook, so explicitly specify 0 as last parameter 
 141     if (!OS2CreateControl( wxT("NOTEBOOK") 
 145                           ,lStyle 
| wxTAB_TRAVERSAL
 
 149     SetBackgroundColour(wxColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
))); 
 151 } // end of wxNotebook::Create 
 153 WXDWORD 
wxNotebook::OS2GetStyle ( 
 155 , WXDWORD
*                          pdwExstyle
 
 158     WXDWORD                         dwTabStyle 
= wxControl::OS2GetStyle( lStyle
 
 162     dwTabStyle 
|= WS_TABSTOP 
| BKS_SOLIDBIND 
| BKS_ROUNDEDTABS 
| BKS_TABTEXTCENTER 
| BKS_TABBEDDIALOG
; 
 164     if (lStyle 
& wxBK_BOTTOM
) 
 165         dwTabStyle 
|= BKS_MAJORTABBOTTOM 
| BKS_BACKPAGESBL
; 
 166     else if (lStyle 
& wxBK_RIGHT
) 
 167         dwTabStyle 
|= BKS_MAJORTABRIGHT 
| BKS_BACKPAGESBR
; 
 168     else if (lStyle 
& wxBK_LEFT
) 
 169         dwTabStyle 
|= BKS_MAJORTABLEFT 
| BKS_BACKPAGESTL
; 
 170     else // default to top 
 171         dwTabStyle 
|= BKS_MAJORTABTOP 
| BKS_BACKPAGESTR
; 
 179         // Note that we never want to have the default WS_EX_CLIENTEDGE style 
 180         // as it looks too ugly for the notebooks 
 185 } // end of wxNotebook::OS2GetStyle 
 187 // ---------------------------------------------------------------------------- 
 188 // wxNotebook accessors 
 189 // ---------------------------------------------------------------------------- 
 191 size_t wxNotebook::GetPageCount() const 
 196     wxASSERT((int)m_pages
.Count() == (int)::WinSendMsg(GetHWND(), BKM_QUERYPAGECOUNT
, (MPARAM
)0, (MPARAM
)BKA_END
)); 
 197     return m_pages
.Count(); 
 198 } // end of wxNotebook::GetPageCount 
 200 int wxNotebook::GetRowCount() const 
 202     return (int)::WinSendMsg( GetHWND() 
 207 } // end of wxNotebook::GetRowCount 
 209 int wxNotebook::SetSelection( size_t nPage 
) 
 211     wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") ); 
 213     if (nPage 
!= (size_t)m_nSelection
) 
 215         wxBookCtrlEvent             
vEvent( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
 
 219         vEvent
.SetSelection(nPage
); 
 220         vEvent
.SetOldSelection(m_nSelection
); 
 221         vEvent
.SetEventObject(this); 
 222         if (!HandleWindowEvent(vEvent
) || vEvent
.IsAllowed()) 
 226             // Program allows the page change 
 228             vEvent
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
); 
 229             HandleWindowEvent(vEvent
); 
 231             ::WinSendMsg( GetHWND() 
 233                          ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 238     m_nSelection 
= nPage
; 
 240 } // end of wxNotebook::SetSelection 
 242 int wxNotebook::ChangeSelection( size_t nPage 
) 
 244     wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") ); 
 246     if (nPage 
!= (size_t)m_nSelection
) 
 248         ::WinSendMsg( GetHWND() 
 250                 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 254     m_nSelection 
= nPage
; 
 258 bool wxNotebook::SetPageText( size_t nPage
, 
 259                               const wxString
& rsStrText 
) 
 261     wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") ); 
 262     return (bool)::WinSendMsg( m_hWnd
 
 264                               ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 265                               ,MPFROMP((const char*)rsStrText
.c_str()) 
 267 } // end of wxNotebook::SetPageText 
 269 wxString 
wxNotebook::GetPageText ( size_t nPage 
) const 
 276     wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("notebook page out of range") ); 
 278     memset(&vBookText
, '\0', sizeof(BOOKTEXT
)); 
 279     vBookText
.textLen 
= 0; // This will get the length 
 280     ulRc 
= LONGFROMMR(::WinSendMsg( m_hWnd
 
 282                                    ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 285     if (ulRc 
== (ULONG
)BOOKERR_INVALID_PARAMETERS 
|| ulRc 
== 0L) 
 287         if (ulRc 
== (ULONG
)BOOKERR_INVALID_PARAMETERS
) 
 289             wxLogError(wxT("Invalid Page Id for page text querry.")); 
 291         return wxEmptyString
; 
 293     vBookText
.textLen 
= ulRc 
+ 1; // To get the null terminator 
 294     vBookText
.pString 
= (char*)zBuf
; 
 297     // Now get the actual text 
 299     ulRc 
= LONGFROMMR(::WinSendMsg( m_hWnd
 
 301                                    ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 304     if (ulRc 
== (ULONG
)BOOKERR_INVALID_PARAMETERS 
|| ulRc 
== 0L) 
 306         return wxEmptyString
; 
 311     vBookText
.pString
[ulRc
] = '\0'; 
 312     sStr 
= (wxChar
*)vBookText
.pString
; 
 314 } // end of wxNotebook::GetPageText 
 316 int wxNotebook::GetPageImage ( size_t nPage 
) const 
 318     wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") ); 
 321     // For OS/2 just return the page 
 324 } // end of wxNotebook::GetPageImage 
 326 bool wxNotebook::SetPageImage ( 
 331     wxBitmap                        vBitmap 
= (wxBitmap
)m_imageList
->GetBitmap(nImage
); 
 333     return (bool)::WinSendMsg( GetHWND() 
 335                               ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 336                               ,(MPARAM
)wxCopyBmp(vBitmap
.GetHBITMAP(), true) 
 338 } // end of wxNotebook::SetPageImage 
 340 void wxNotebook::SetImageList ( 
 341   wxImageList
*                      pImageList
 
 345     // Does not really do anything yet, but at least we need to 
 346     // update the base class. 
 348     wxNotebookBase::SetImageList(pImageList
); 
 349 } // end of wxNotebook::SetImageList 
 351 // ---------------------------------------------------------------------------- 
 352 // wxNotebook size settings 
 353 // ---------------------------------------------------------------------------- 
 354 void wxNotebook::SetPageSize ( 
 359 } // end of wxNotebook::SetPageSize 
 361 void wxNotebook::SetPadding ( 
 362   const wxSize
&                     WXUNUSED(rPadding
) 
 366     // No padding in OS/2 
 368 } // end of wxNotebook::SetPadding 
 370 void wxNotebook::SetTabSize ( 
 374     ::WinSendMsg( GetHWND() 
 376                  ,MPFROM2SHORT( (USHORT
)rSize
.x
 
 379                  ,(MPARAM
)BKA_MAJORTAB
 
 381 } // end of wxNotebook::SetTabSize 
 383 // ---------------------------------------------------------------------------- 
 384 // wxNotebook operations 
 385 // ---------------------------------------------------------------------------- 
 388 // Remove one page from the notebook, without deleting 
 390 wxNotebookPage
* wxNotebook::DoRemovePage ( size_t nPage 
) 
 392     wxNotebookPage
* pPageRemoved 
= wxNotebookBase::DoRemovePage(nPage
); 
 397     ::WinSendMsg( GetHWND() 
 399                  ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 402     if (m_pages
.IsEmpty()) 
 405         // No selection any more, the notebook becamse empty 
 409     else // notebook still not empty 
 412         // Change the selected page if it was deleted or became invalid 
 416         if (m_nSelection 
== (int)GetPageCount()) 
 419             // Last page deleted, make the new last page the new selection 
 421             nSelNew 
= m_nSelection 
- 1; 
 423         else if (nPage 
<= (size_t)m_nSelection
) 
 426             // We must show another page, even if it has the same index 
 428             nSelNew 
= m_nSelection
; 
 430         else // nothing changes for the currently selected page 
 435             // We still must refresh the current page: this needs to be done 
 436             // for some unknown reason if the tab control shows the up-down 
 437             // control (i.e. when there are too many pages) -- otherwise after 
 438             // deleting a page nothing at all is shown 
 440             m_pages
[m_nSelection
]->Refresh(); 
 446             // m_nSelection must be always valid so reset it before calling 
 450             SetSelection(nSelNew
); 
 454 } // end of wxNotebook::DoRemovePage 
 459 bool wxNotebook::DeleteAllPages() 
 461     int                             nPageCount 
= GetPageCount(); 
 464     for (nPage 
= 0; nPage 
< nPageCount
; nPage
++) 
 465         delete m_pages
[nPage
]; 
 467     ::WinSendMsg( GetHWND() 
 475 } // end of wxNotebook::DeleteAllPages 
 478 // Add a page to the notebook 
 480 bool wxNotebook::AddPage ( 
 481   wxNotebookPage
*                   pPage
 
 482 , const wxString
&                   rStrText
 
 487     return InsertPage( GetPageCount() 
 493 } // end of wxNotebook::AddPage 
 496 // Same as AddPage() but does it at given position 
 498 bool wxNotebook::InsertPage ( size_t          nPage
, 
 499                               wxNotebookPage
* pPage
, 
 500                               const wxString
& rsStrText
, 
 506     wxASSERT( pPage 
!= NULL 
); 
 507     wxCHECK( IS_VALID_PAGE(nPage
) || nPage 
== GetPageCount(), false ); 
 510     // Under OS/2 we can only insert FIRST, LAST, NEXT or PREV.  Requires 
 511     // two different calls to the API.  Page 1 uses the BKA_FIRST.  Subsequent 
 512     // pages use the previous page ID coupled with a BKA_NEXT call.  Unlike 
 513     // Windows, OS/2 uses an internal Page ID to ID the pages. 
 515     // OS/2 also has a nice auto-size feature that automatically sizes the 
 516     // the attached window so we don't have to worry about the size of the 
 517     // window on the page. 
 521         ulApiPage 
= LONGFROMMR(::WinSendMsg( GetHWND() 
 524                                             ,MPFROM2SHORT(BKA_AUTOPAGESIZE 
| BKA_MAJOR
, BKA_FIRST
) 
 531             vError 
= ::WinGetLastError(vHabmain
); 
 532             sError 
= wxPMErrorToStr(vError
); 
 535         m_alPageId
.Insert((long)ulApiPage
, nPage
); 
 539         ulApiPage 
= LONGFROMMR(::WinSendMsg( GetHWND() 
 541                                             ,MPFROMLONG((ULONG
)m_alPageId
[nPage 
- 1]) 
 542                                             ,MPFROM2SHORT(BKA_AUTOPAGESIZE 
| BKA_MAJOR
, BKA_NEXT
) 
 549             vError 
= ::WinGetLastError(vHabmain
); 
 550             sError 
= wxPMErrorToStr(vError
); 
 553         m_alPageId
.Insert((long)ulApiPage
, nPage
); 
 557     // Associate a window handle with the page 
 561         if (!::WinSendMsg( GetHWND() 
 562                           ,BKM_SETPAGEWINDOWHWND
 
 563                           ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 564                           ,MPFROMHWND(pPage
->GetHWND()) 
 569     // If the inserted page is before the selected one, we must update the 
 570     // index of the selected page 
 572     if (nPage 
<= (size_t)m_nSelection
) 
 575         // One extra page added 
 583         // Save the pointer to the page 
 585         m_pages
.Insert( pPage
 
 591     // Now set TAB dimenstions 
 594     wxWindowDC 
vDC(this); 
 598     vDC
.GetTextExtent(rsStrText
, &nTextX
, &nTextY
); 
 600     nTextX  
= (wxCoord
)(nTextX 
* 1.3); 
 601     if (nTextX 
> m_nTabSize
) 
 604         ::WinSendMsg( GetHWND() 
 606                      ,MPFROM2SHORT((USHORT
)m_nTabSize
, (USHORT
)nTextY
) 
 607                      ,(MPARAM
)BKA_MAJORTAB
 
 611     // Now set any TAB text 
 613     if (!rsStrText
.empty()) 
 615         if (!SetPageText( nPage
 
 622     // Now set any TAB bitmap image 
 626         if (!SetPageImage( nPage
 
 635         // Don't show pages by default (we'll need to adjust their size first) 
 637         HWND hWnd 
= GetWinHwnd(pPage
); 
 639         WinSetWindowULong( hWnd
 
 641                           ,WinQueryWindowULong( hWnd
 
 647         // This updates internal flag too - otherwise it will get out of sync 
 653     // Some page should be selected: either this one or the first one if there is 
 654     // still no selection 
 660     else if ( m_nSelection 
== -1 ) 
 664         SetSelection(nSelNew
); 
 666     InvalidateBestSize(); 
 669 } // end of wxNotebook::InsertPage 
 671 // ---------------------------------------------------------------------------- 
 672 // wxNotebook callbacks 
 673 // ---------------------------------------------------------------------------- 
 674 void wxNotebook::OnSize( 
 679 } // end of wxNotebook::OnSize 
 681 void wxNotebook::OnSelChange ( 
 682   wxBookCtrlEvent
&                  rEvent
 
 686     // Is it our tab control? 
 688     if (rEvent
.GetEventObject() == this) 
 690         int   nPageCount 
= GetPageCount(); 
 692         ULONG ulOS2Sel 
= (ULONG
)rEvent
.GetOldSelection(); 
 695         for (nSel 
= 0; nSel 
< nPageCount
; nSel
++) 
 697             if (ulOS2Sel 
== (ULONG
)m_alPageId
[nSel
]) 
 707         m_pages
[nSel
]->Show(false); 
 709         ulOS2Sel 
= (ULONG
)rEvent
.GetSelection(); 
 713         for (nSel 
= 0; nSel 
< nPageCount
; nSel
++) 
 715             if (ulOS2Sel 
== (ULONG
)m_alPageId
[nSel
]) 
 725         wxNotebookPage
*         pPage 
= m_pages
[nSel
]; 
 732     // We want to give others a chance to process this message as well 
 735 } // end of wxNotebook::OnSelChange 
 737 void wxNotebook::OnSetFocus ( 
 742     // This function is only called when the focus is explicitly set (i.e. from 
 743     // the program) to the notebook - in this case we don't need the 
 744     // complicated OnNavigationKey() logic because the programmer knows better 
 747     // set focus to the currently selected page if any 
 749     if (m_nSelection 
!= -1) 
 750         m_pages
[m_nSelection
]->SetFocus(); 
 752 } // end of wxNotebook::OnSetFocus 
 754 void wxNotebook::OnNavigationKey ( 
 755   wxNavigationKeyEvent
&             rEvent
 
 758     if (rEvent
.IsWindowChange()) 
 763         AdvanceSelection(rEvent
.GetDirection()); 
 768         // We get this event in 2 cases 
 770         // a) one of our pages might have generated it because the user TABbed 
 771         // out from it in which case we should propagate the event upwards and 
 772         // our parent will take care of setting the focus to prev/next sibling 
 776         // b) the parent panel wants to give the focus to us so that we 
 777         // forward it to our selected page. We can't deal with this in 
 778         // OnSetFocus() because we don't know which direction the focus came 
 779         // from in this case and so can't choose between setting the focus to 
 780         // first or last panel child 
 782         wxWindow
*                   pParent 
= GetParent(); 
 784         if (rEvent
.GetEventObject() == pParent
) 
 787             // No, it doesn't come from child, case (b): forward to a page 
 789             if (m_nSelection 
!= -1) 
 792                 // So that the page knows that the event comes from it's parent 
 793                 // and is being propagated downwards 
 795                 rEvent
.SetEventObject(this); 
 797                 wxWindow
*           pPage 
= m_pages
[m_nSelection
]; 
 799                 if (!pPage
->HandleWindowEvent(rEvent
)) 
 803                 //else: page manages focus inside it itself 
 808                 // We have no pages - still have to give focus to _something_ 
 816             // It comes from our child, case (a), pass to the parent 
 820                 rEvent
.SetCurrentFocus(this); 
 821                 pParent
->HandleWindowEvent(rEvent
); 
 825 } // end of wxNotebook::OnNavigationKey 
 827 // ---------------------------------------------------------------------------- 
 828 // wxNotebook base class virtuals 
 829 // ---------------------------------------------------------------------------- 
 832 // Override these 2 functions to do nothing: everything is done in OnSize 
 834 void wxNotebook::SetConstraintSizes( bool WXUNUSED(bRecurse
) ) 
 837     // Don't set the sizes of the pages - their correct size is not yet known 
 839     wxControl::SetConstraintSizes(false); 
 840 } // end of wxNotebook::SetConstraintSizes 
 842 bool wxNotebook::DoPhase ( int WXUNUSED(nPhase
) ) 
 845 } // end of wxNotebook::DoPhase 
 847 // ---------------------------------------------------------------------------- 
 848 // wxNotebook Windows message handlers 
 849 // ---------------------------------------------------------------------------- 
 850 bool wxNotebook::OS2OnScroll ( int    nOrientation
, 
 856     // Don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the 
 861     return wxNotebookBase::OS2OnScroll( nOrientation
 
 866 } // end of wxNotebook::OS2OnScroll 
 868 #endif // wxUSE_NOTEBOOK