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 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
) 
  57 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
) 
  59 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
) 
  60     EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
) 
  61     EVT_SIZE(wxNotebook::OnSize
) 
  62     EVT_SET_FOCUS(wxNotebook::OnSetFocus
) 
  63     EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
) 
  66 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
) 
  67 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
) 
  69 // ============================================================================ 
  71 // ============================================================================ 
  73 // ---------------------------------------------------------------------------- 
  74 // wxNotebook construction 
  75 // ---------------------------------------------------------------------------- 
  78 // Common part of all ctors 
  80 void wxNotebook::Init() 
  85 } // end of wxNotebook::Init 
  88 // Default for dynamic class 
  90 wxNotebook::wxNotebook() 
  93 } // end of wxNotebook::wxNotebook 
  96 // The same arguments as for wxControl 
  98 wxNotebook::wxNotebook( 
 101 , const wxPoint
&                    rPos
 
 102 , const wxSize
&                     rSize
 
 104 , const wxString
&                   rsName
 
 115 } // end of wxNotebook::wxNotebook 
 120 bool wxNotebook::Create( wxWindow
*       pParent
, 
 125                          const wxString
& rsName 
) 
 127     if ( (lStyle 
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT 
) 
 132     if (!CreateControl( pParent
 
 143     // Notebook, so explicitly specify 0 as last parameter 
 145     if (!OS2CreateControl( wxT("NOTEBOOK") 
 149                           ,lStyle 
| wxTAB_TRAVERSAL
 
 153     SetBackgroundColour(wxColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
))); 
 155 } // end of wxNotebook::Create 
 157 WXDWORD 
wxNotebook::OS2GetStyle ( 
 159 , WXDWORD
*                          pdwExstyle
 
 162     WXDWORD                         dwTabStyle 
= wxControl::OS2GetStyle( lStyle
 
 166     dwTabStyle 
|= WS_TABSTOP 
| BKS_SOLIDBIND 
| BKS_ROUNDEDTABS 
| BKS_TABTEXTCENTER 
| BKS_TABBEDDIALOG
; 
 168     if (lStyle 
& wxBK_BOTTOM
) 
 169         dwTabStyle 
|= BKS_MAJORTABBOTTOM 
| BKS_BACKPAGESBL
; 
 170     else if (lStyle 
& wxBK_RIGHT
) 
 171         dwTabStyle 
|= BKS_MAJORTABRIGHT 
| BKS_BACKPAGESBR
; 
 172     else if (lStyle 
& wxBK_LEFT
) 
 173         dwTabStyle 
|= BKS_MAJORTABLEFT 
| BKS_BACKPAGESTL
; 
 174     else // default to top 
 175         dwTabStyle 
|= BKS_MAJORTABTOP 
| BKS_BACKPAGESTR
; 
 183         // Note that we never want to have the default WS_EX_CLIENTEDGE style 
 184         // as it looks too ugly for the notebooks 
 189 } // end of wxNotebook::OS2GetStyle 
 191 // ---------------------------------------------------------------------------- 
 192 // wxNotebook accessors 
 193 // ---------------------------------------------------------------------------- 
 195 size_t wxNotebook::GetPageCount() const 
 200     wxASSERT((int)m_pages
.Count() == (int)::WinSendMsg(GetHWND(), BKM_QUERYPAGECOUNT
, (MPARAM
)0, (MPARAM
)BKA_END
)); 
 201     return m_pages
.Count(); 
 202 } // end of wxNotebook::GetPageCount 
 204 int wxNotebook::GetRowCount() const 
 206     return (int)::WinSendMsg( GetHWND() 
 211 } // end of wxNotebook::GetRowCount 
 213 int wxNotebook::SetSelection( size_t nPage 
) 
 215     wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") ); 
 217     if (nPage 
!= (size_t)m_nSelection
) 
 219         wxNotebookEvent             
vEvent( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
 
 223         vEvent
.SetSelection(nPage
); 
 224         vEvent
.SetOldSelection(m_nSelection
); 
 225         vEvent
.SetEventObject(this); 
 226         if (!GetEventHandler()->ProcessEvent(vEvent
) || vEvent
.IsAllowed()) 
 230             // Program allows the page change 
 232             vEvent
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
); 
 233             GetEventHandler()->ProcessEvent(vEvent
); 
 235             ::WinSendMsg( GetHWND() 
 237                          ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 242     m_nSelection 
= nPage
; 
 244 } // end of wxNotebook::SetSelection 
 246 int wxNotebook::ChangeSelection( size_t nPage 
) 
 248     wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") ); 
 250     if (nPage 
!= (size_t)m_nSelection
) 
 252         ::WinSendMsg( GetHWND() 
 254                 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 258     m_nSelection 
= nPage
; 
 262 bool wxNotebook::SetPageText( size_t nPage
, 
 263                               const wxString
& rsStrText 
) 
 265     wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") ); 
 266     return (bool)::WinSendMsg( m_hWnd
 
 268                               ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 269                               ,MPFROMP((const char*)rsStrText
.c_str()) 
 271 } // end of wxNotebook::SetPageText 
 273 wxString 
wxNotebook::GetPageText ( size_t nPage 
) const 
 280     wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("notebook page out of range") ); 
 282     memset(&vBookText
, '\0', sizeof(BOOKTEXT
)); 
 283     vBookText
.textLen 
= 0; // This will get the length 
 284     ulRc 
= LONGFROMMR(::WinSendMsg( m_hWnd
 
 286                                    ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 289     if (ulRc 
== (ULONG
)BOOKERR_INVALID_PARAMETERS 
|| ulRc 
== 0L) 
 291         if (ulRc 
== (ULONG
)BOOKERR_INVALID_PARAMETERS
) 
 293             wxLogError(wxT("Invalid Page Id for page text querry.")); 
 295         return wxEmptyString
; 
 297     vBookText
.textLen 
= ulRc 
+ 1; // To get the null terminator 
 298     vBookText
.pString 
= (char*)zBuf
; 
 301     // Now get the actual text 
 303     ulRc 
= LONGFROMMR(::WinSendMsg( m_hWnd
 
 305                                    ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 308     if (ulRc 
== (ULONG
)BOOKERR_INVALID_PARAMETERS 
|| ulRc 
== 0L) 
 310         return wxEmptyString
; 
 315     vBookText
.pString
[ulRc
] = '\0'; 
 316     sStr 
= (wxChar
*)vBookText
.pString
; 
 318 } // end of wxNotebook::GetPageText 
 320 int wxNotebook::GetPageImage ( size_t nPage 
) const 
 322     wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") ); 
 325     // For OS/2 just return the page 
 328 } // end of wxNotebook::GetPageImage 
 330 bool wxNotebook::SetPageImage ( 
 335     wxBitmap                        vBitmap 
= (wxBitmap
)m_imageList
->GetBitmap(nImage
); 
 337     return (bool)::WinSendMsg( GetHWND() 
 339                               ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 340                               ,(MPARAM
)wxCopyBmp(vBitmap
.GetHBITMAP(), true) 
 342 } // end of wxNotebook::SetPageImage 
 344 void wxNotebook::SetImageList ( 
 345   wxImageList
*                      pImageList
 
 349     // Does not really do anything yet, but at least we need to 
 350     // update the base class. 
 352     wxNotebookBase::SetImageList(pImageList
); 
 353 } // end of wxNotebook::SetImageList 
 355 // ---------------------------------------------------------------------------- 
 356 // wxNotebook size settings 
 357 // ---------------------------------------------------------------------------- 
 358 void wxNotebook::SetPageSize ( 
 363 } // end of wxNotebook::SetPageSize 
 365 void wxNotebook::SetPadding ( 
 366   const wxSize
&                     WXUNUSED(rPadding
) 
 370     // No padding in OS/2 
 372 } // end of wxNotebook::SetPadding 
 374 void wxNotebook::SetTabSize ( 
 378     ::WinSendMsg( GetHWND() 
 380                  ,MPFROM2SHORT( (USHORT
)rSize
.x
 
 383                  ,(MPARAM
)BKA_MAJORTAB
 
 385 } // end of wxNotebook::SetTabSize 
 387 // ---------------------------------------------------------------------------- 
 388 // wxNotebook operations 
 389 // ---------------------------------------------------------------------------- 
 392 // Remove one page from the notebook, without deleting 
 394 wxNotebookPage
* wxNotebook::DoRemovePage ( size_t nPage 
) 
 396     wxNotebookPage
* pPageRemoved 
= wxNotebookBase::DoRemovePage(nPage
); 
 401     ::WinSendMsg( GetHWND() 
 403                  ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 406     if (m_pages
.IsEmpty()) 
 409         // No selection any more, the notebook becamse empty 
 413     else // notebook still not empty 
 416         // Change the selected page if it was deleted or became invalid 
 420         if (m_nSelection 
== (int)GetPageCount()) 
 423             // Last page deleted, make the new last page the new selection 
 425             nSelNew 
= m_nSelection 
- 1; 
 427         else if (nPage 
<= (size_t)m_nSelection
) 
 430             // We must show another page, even if it has the same index 
 432             nSelNew 
= m_nSelection
; 
 434         else // nothing changes for the currently selected page 
 439             // We still must refresh the current page: this needs to be done 
 440             // for some unknown reason if the tab control shows the up-down 
 441             // control (i.e. when there are too many pages) -- otherwise after 
 442             // deleting a page nothing at all is shown 
 444             m_pages
[m_nSelection
]->Refresh(); 
 450             // m_nSelection must be always valid so reset it before calling 
 454             SetSelection(nSelNew
); 
 458 } // end of wxNotebook::DoRemovePage 
 463 bool wxNotebook::DeleteAllPages() 
 465     int                             nPageCount 
= GetPageCount(); 
 468     for (nPage 
= 0; nPage 
< nPageCount
; nPage
++) 
 469         delete m_pages
[nPage
]; 
 471     ::WinSendMsg( GetHWND() 
 479 } // end of wxNotebook::DeleteAllPages 
 482 // Add a page to the notebook 
 484 bool wxNotebook::AddPage ( 
 485   wxNotebookPage
*                   pPage
 
 486 , const wxString
&                   rStrText
 
 491     return InsertPage( GetPageCount() 
 497 } // end of wxNotebook::AddPage 
 500 // Same as AddPage() but does it at given position 
 502 bool wxNotebook::InsertPage ( size_t          nPage
, 
 503                               wxNotebookPage
* pPage
, 
 504                               const wxString
& rsStrText
, 
 510     wxASSERT( pPage 
!= NULL 
); 
 511     wxCHECK( IS_VALID_PAGE(nPage
) || nPage 
== GetPageCount(), false ); 
 514     // Under OS/2 we can only insert FIRST, LAST, NEXT or PREV.  Requires 
 515     // two different calls to the API.  Page 1 uses the BKA_FIRST.  Subsequent 
 516     // pages use the previous page ID coupled with a BKA_NEXT call.  Unlike 
 517     // Windows, OS/2 uses an internal Page ID to ID the pages. 
 519     // OS/2 also has a nice auto-size feature that automatically sizes the 
 520     // the attached window so we don't have to worry about the size of the 
 521     // window on the page. 
 525         ulApiPage 
= LONGFROMMR(::WinSendMsg( GetHWND() 
 528                                             ,MPFROM2SHORT(BKA_AUTOPAGESIZE 
| BKA_MAJOR
, BKA_FIRST
) 
 535             vError 
= ::WinGetLastError(vHabmain
); 
 536             sError 
= wxPMErrorToStr(vError
); 
 539         m_alPageId
.Insert((long)ulApiPage
, nPage
); 
 543         ulApiPage 
= LONGFROMMR(::WinSendMsg( GetHWND() 
 545                                             ,MPFROMLONG((ULONG
)m_alPageId
[nPage 
- 1]) 
 546                                             ,MPFROM2SHORT(BKA_AUTOPAGESIZE 
| BKA_MAJOR
, BKA_NEXT
) 
 553             vError 
= ::WinGetLastError(vHabmain
); 
 554             sError 
= wxPMErrorToStr(vError
); 
 557         m_alPageId
.Insert((long)ulApiPage
, nPage
); 
 561     // Associate a window handle with the page 
 565         if (!::WinSendMsg( GetHWND() 
 566                           ,BKM_SETPAGEWINDOWHWND
 
 567                           ,MPFROMLONG((ULONG
)m_alPageId
[nPage
]) 
 568                           ,MPFROMHWND(pPage
->GetHWND()) 
 573     // If the inserted page is before the selected one, we must update the 
 574     // index of the selected page 
 576     if (nPage 
<= (size_t)m_nSelection
) 
 579         // One extra page added 
 587         // Save the pointer to the page 
 589         m_pages
.Insert( pPage
 
 595     // Now set TAB dimenstions 
 598     wxWindowDC 
vDC(this); 
 602     vDC
.GetTextExtent(rsStrText
, &nTextX
, &nTextY
); 
 604     nTextX  
= (wxCoord
)(nTextX 
* 1.3); 
 605     if (nTextX 
> m_nTabSize
) 
 608         ::WinSendMsg( GetHWND() 
 610                      ,MPFROM2SHORT((USHORT
)m_nTabSize
, (USHORT
)nTextY
) 
 611                      ,(MPARAM
)BKA_MAJORTAB
 
 615     // Now set any TAB text 
 617     if (!rsStrText
.empty()) 
 619         if (!SetPageText( nPage
 
 626     // Now set any TAB bitmap image 
 630         if (!SetPageImage( nPage
 
 639         // Don't show pages by default (we'll need to adjust their size first) 
 641         HWND hWnd 
= GetWinHwnd(pPage
); 
 643         WinSetWindowULong( hWnd
 
 645                           ,WinQueryWindowULong( hWnd
 
 651         // This updates internal flag too - otherwise it will get out of sync 
 657     // Some page should be selected: either this one or the first one if there is 
 658     // still no selection 
 664     else if ( m_nSelection 
== -1 ) 
 668         SetSelection(nSelNew
); 
 670     InvalidateBestSize(); 
 673 } // end of wxNotebook::InsertPage 
 675 // ---------------------------------------------------------------------------- 
 676 // wxNotebook callbacks 
 677 // ---------------------------------------------------------------------------- 
 678 void wxNotebook::OnSize( 
 683 } // end of wxNotebook::OnSize 
 685 void wxNotebook::OnSelChange ( 
 686   wxNotebookEvent
&                  rEvent
 
 690     // Is it our tab control? 
 692     if (rEvent
.GetEventObject() == this) 
 694         int   nPageCount 
= GetPageCount(); 
 696         ULONG ulOS2Sel 
= (ULONG
)rEvent
.GetOldSelection(); 
 699         for (nSel 
= 0; nSel 
< nPageCount
; nSel
++) 
 701             if (ulOS2Sel 
== (ULONG
)m_alPageId
[nSel
]) 
 711         m_pages
[nSel
]->Show(false); 
 713         ulOS2Sel 
= (ULONG
)rEvent
.GetSelection(); 
 717         for (nSel 
= 0; nSel 
< nPageCount
; nSel
++) 
 719             if (ulOS2Sel 
== (ULONG
)m_alPageId
[nSel
]) 
 729         wxNotebookPage
*         pPage 
= m_pages
[nSel
]; 
 736     // We want to give others a chance to process this message as well 
 739 } // end of wxNotebook::OnSelChange 
 741 void wxNotebook::OnSetFocus ( 
 746     // This function is only called when the focus is explicitly set (i.e. from 
 747     // the program) to the notebook - in this case we don't need the 
 748     // complicated OnNavigationKey() logic because the programmer knows better 
 751     // set focus to the currently selected page if any 
 753     if (m_nSelection 
!= -1) 
 754         m_pages
[m_nSelection
]->SetFocus(); 
 756 } // end of wxNotebook::OnSetFocus 
 758 void wxNotebook::OnNavigationKey ( 
 759   wxNavigationKeyEvent
&             rEvent
 
 762     if (rEvent
.IsWindowChange()) 
 767         AdvanceSelection(rEvent
.GetDirection()); 
 772         // We get this event in 2 cases 
 774         // a) one of our pages might have generated it because the user TABbed 
 775         // out from it in which case we should propagate the event upwards and 
 776         // our parent will take care of setting the focus to prev/next sibling 
 780         // b) the parent panel wants to give the focus to us so that we 
 781         // forward it to our selected page. We can't deal with this in 
 782         // OnSetFocus() because we don't know which direction the focus came 
 783         // from in this case and so can't choose between setting the focus to 
 784         // first or last panel child 
 786         wxWindow
*                   pParent 
= GetParent(); 
 788         if (rEvent
.GetEventObject() == pParent
) 
 791             // No, it doesn't come from child, case (b): forward to a page 
 793             if (m_nSelection 
!= -1) 
 796                 // So that the page knows that the event comes from it's parent 
 797                 // and is being propagated downwards 
 799                 rEvent
.SetEventObject(this); 
 801                 wxWindow
*           pPage 
= m_pages
[m_nSelection
]; 
 803                 if (!pPage
->GetEventHandler()->ProcessEvent(rEvent
)) 
 807                 //else: page manages focus inside it itself 
 812                 // We have no pages - still have to give focus to _something_ 
 820             // It comes from our child, case (a), pass to the parent 
 824                 rEvent
.SetCurrentFocus(this); 
 825                 pParent
->GetEventHandler()->ProcessEvent(rEvent
); 
 829 } // end of wxNotebook::OnNavigationKey 
 831 // ---------------------------------------------------------------------------- 
 832 // wxNotebook base class virtuals 
 833 // ---------------------------------------------------------------------------- 
 836 // Override these 2 functions to do nothing: everything is done in OnSize 
 838 void wxNotebook::SetConstraintSizes( bool WXUNUSED(bRecurse
) ) 
 841     // Don't set the sizes of the pages - their correct size is not yet known 
 843     wxControl::SetConstraintSizes(false); 
 844 } // end of wxNotebook::SetConstraintSizes 
 846 bool wxNotebook::DoPhase ( int WXUNUSED(nPhase
) ) 
 849 } // end of wxNotebook::DoPhase 
 851 // ---------------------------------------------------------------------------- 
 852 // wxNotebook Windows message handlers 
 853 // ---------------------------------------------------------------------------- 
 854 bool wxNotebook::OS2OnScroll ( int    nOrientation
, 
 860     // Don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the 
 865     return wxNotebookBase::OS2OnScroll( nOrientation
 
 870 } // end of wxNotebook::OS2OnScroll 
 872 #endif // wxUSE_NOTEBOOK